]> git.proxmox.com Git - mirror_novnc.git/blame - tests/test.util.js
Add unit tests for encodeUTF8 and decodeUTF8
[mirror_novnc.git] / tests / test.util.js
CommitLineData
8727f598 1/* eslint-disable no-console */
2b5f94fa 2const expect = chai.expect;
d21cd6c1 3
dfae3209 4import * as Log from '../core/util/logging.js';
80c72e92 5import { encodeUTF8, decodeUTF8 } from '../core/util/strings.js';
dfae3209 6
2c5491e1 7describe('Utils', function () {
d21cd6c1
SR
8 "use strict";
9
d21cd6c1
SR
10 describe('logging functions', function () {
11 beforeEach(function () {
12 sinon.spy(console, 'log');
0ae5b50a 13 sinon.spy(console, 'debug');
d21cd6c1
SR
14 sinon.spy(console, 'warn');
15 sinon.spy(console, 'error');
e4fef7be 16 sinon.spy(console, 'info');
d21cd6c1
SR
17 });
18
19 afterEach(function () {
0242c032
SM
20 console.log.restore();
21 console.debug.restore();
22 console.warn.restore();
23 console.error.restore();
24 console.info.restore();
25 Log.init_logging();
d21cd6c1
SR
26 });
27
28 it('should use noop for levels lower than the min level', function () {
dfae3209
SR
29 Log.init_logging('warn');
30 Log.Debug('hi');
31 Log.Info('hello');
d21cd6c1
SR
32 expect(console.log).to.not.have.been.called;
33 });
34
0ae5b50a 35 it('should use console.debug for Debug', function () {
dfae3209
SR
36 Log.init_logging('debug');
37 Log.Debug('dbg');
0ae5b50a 38 expect(console.debug).to.have.been.calledWith('dbg');
e4fef7be 39 });
c464f47e 40
e4fef7be 41 it('should use console.info for Info', function () {
dfae3209
SR
42 Log.init_logging('debug');
43 Log.Info('inf');
e4fef7be 44 expect(console.info).to.have.been.calledWith('inf');
d21cd6c1
SR
45 });
46
47 it('should use console.warn for Warn', function () {
dfae3209
SR
48 Log.init_logging('warn');
49 Log.Warn('wrn');
d21cd6c1
SR
50 expect(console.warn).to.have.been.called;
51 expect(console.warn).to.have.been.calledWith('wrn');
52 });
53
54 it('should use console.error for Error', function () {
dfae3209
SR
55 Log.init_logging('error');
56 Log.Error('err');
d21cd6c1
SR
57 expect(console.error).to.have.been.called;
58 expect(console.error).to.have.been.calledWith('err');
59 });
60 });
61
80c72e92
SM
62 describe('string functions', function () {
63 it('should decode UTF-8 to DOMString correctly', function () {
64 // The capital cyrillic letter 'PE' has the hexcode 'd0 9f' in UTF-8
65 const utf8string = String.fromCodePoint(parseInt("d0", 16),
66 parseInt("9f", 16));
67 const domstring = decodeUTF8(utf8string);
68 expect(domstring).to.equal("П");
69 });
70
71 it('should encode DOMString to UTF-8 correctly', function () {
72 const domstring = "åäöa";
73 const utf8string = encodeUTF8(domstring);
74
75 // The hexcode in UTF-8 for 'å' is 'c3 a5'
76 expect(utf8string.codePointAt(0).toString(16)).to.equal("c3");
77 expect(utf8string.codePointAt(1).toString(16)).to.equal("a5");
78 // ä
79 expect(utf8string.codePointAt(2).toString(16)).to.equal("c3");
80 expect(utf8string.codePointAt(3).toString(16)).to.equal("a4");
81 // ö
82 expect(utf8string.codePointAt(4).toString(16)).to.equal("c3");
83 expect(utf8string.codePointAt(5).toString(16)).to.equal("b6");
84 // a
85 expect(utf8string.codePointAt(6).toString(16)).to.equal("61");
86 });
87
88 it('should keep the string intact if encoding to UTF-8 and then decoding', function() {
89 const domstring = "κόσμε";
90 const utf8string = encodeUTF8(domstring);
91 expect(decodeUTF8(utf8string)).to.equal(domstring);
92 });
93
94 it('should ignore URIErrors when UTF-8 decoding if allowLatin1 is set', function() {
95 expect(() => decodeUTF8("�")).to.throw(URIError);
96 expect(() => decodeUTF8("�", true)).to.not.throw(URIError);
97
98 // Only URIError should be ignored
99 expect(() => decodeUTF8(undefVar, true)).to.throw(Error);
100 });
101 });
102
d21cd6c1 103 // TODO(directxman12): test the conf_default and conf_defaults methods
d21cd6c1
SR
104 // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
105 // TODO(directxman12): figure out a good way to test getPosition and getEventPosition
ee7d4c61 106 // TODO(directxman12): figure out how to test the browser detection functions properly
d21cd6c1
SR
107 // (we can't really test them against the browsers, except for Gecko
108 // via PhantomJS, the default test driver)
d21cd6c1 109});
8727f598 110/* eslint-enable no-console */