]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Add unit tests for encodeUTF8 and decodeUTF8
authorSamuel Mannehed <samuel@cendio.se>
Thu, 2 Jan 2020 16:29:41 +0000 (17:29 +0100)
committerSamuel Mannehed <samuel@cendio.se>
Thu, 2 Jan 2020 16:29:41 +0000 (17:29 +0100)
tests/test.util.js

index 201acc8bb0d25c5e652ce2fbe777984d2d038c97..1f670bdbe70fc3b72ce7e1ba4ed4518a153bf109 100644 (file)
@@ -2,6 +2,7 @@
 const expect = chai.expect;
 
 import * as Log from '../core/util/logging.js';
+import { encodeUTF8, decodeUTF8 } from '../core/util/strings.js';
 
 describe('Utils', function () {
     "use strict";
@@ -58,8 +59,48 @@ describe('Utils', function () {
         });
     });
 
+    describe('string functions', function () {
+        it('should decode UTF-8 to DOMString correctly', function () {
+            // The capital cyrillic letter 'PE' has the hexcode 'd0 9f' in UTF-8
+            const utf8string = String.fromCodePoint(parseInt("d0", 16),
+                                                    parseInt("9f", 16));
+            const domstring = decodeUTF8(utf8string);
+            expect(domstring).to.equal("П");
+        });
+
+        it('should encode DOMString to UTF-8 correctly', function () {
+            const domstring = "åäöa";
+            const utf8string = encodeUTF8(domstring);
+
+            // The hexcode in UTF-8 for 'å' is 'c3 a5'
+            expect(utf8string.codePointAt(0).toString(16)).to.equal("c3");
+            expect(utf8string.codePointAt(1).toString(16)).to.equal("a5");
+            // ä
+            expect(utf8string.codePointAt(2).toString(16)).to.equal("c3");
+            expect(utf8string.codePointAt(3).toString(16)).to.equal("a4");
+            // ö
+            expect(utf8string.codePointAt(4).toString(16)).to.equal("c3");
+            expect(utf8string.codePointAt(5).toString(16)).to.equal("b6");
+            // a
+            expect(utf8string.codePointAt(6).toString(16)).to.equal("61");
+        });
+
+        it('should keep the string intact if encoding to UTF-8 and then decoding', function() {
+            const domstring = "κόσμε";
+            const utf8string = encodeUTF8(domstring);
+            expect(decodeUTF8(utf8string)).to.equal(domstring);
+        });
+
+        it('should ignore URIErrors when UTF-8 decoding if allowLatin1 is set', function() {
+            expect(() => decodeUTF8("�")).to.throw(URIError);
+            expect(() => decodeUTF8("�", true)).to.not.throw(URIError);
+
+            // Only URIError should be ignored
+            expect(() => decodeUTF8(undefVar, true)).to.throw(Error);
+        });
+    });
+
     // TODO(directxman12): test the conf_default and conf_defaults methods
-    // TODO(directxman12): test decodeUTF8
     // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
     // TODO(directxman12): figure out a good way to test getPosition and getEventPosition
     // TODO(directxman12): figure out how to test the browser detection functions properly