]> git.proxmox.com Git - mirror_novnc.git/blame - tests/test.helper.js
Add deflator helper class for deflating data
[mirror_novnc.git] / tests / test.helper.js
CommitLineData
2b5f94fa 1const expect = chai.expect;
f00b6fb6 2
dfae3209
SR
3import keysyms from '../core/input/keysymdef.js';
4import * as KeyboardUtil from "../core/input/util.js";
59ef2916 5import * as browser from '../core/util/browser.js';
099eb856 6
2c5491e1 7describe('Helpers', function () {
f00b6fb6 8 "use strict";
f00b6fb6 9
2c5491e1
PO
10 describe('keysyms.lookup', function () {
11 it('should map ASCII characters to keysyms', function () {
524d67f2
PO
12 expect(keysyms.lookup('a'.charCodeAt())).to.be.equal(0x61);
13 expect(keysyms.lookup('A'.charCodeAt())).to.be.equal(0x41);
7b536961 14 });
2c5491e1 15 it('should map Latin-1 characters to keysyms', function () {
524d67f2 16 expect(keysyms.lookup('ø'.charCodeAt())).to.be.equal(0xf8);
f00b6fb6 17
524d67f2 18 expect(keysyms.lookup('é'.charCodeAt())).to.be.equal(0xe9);
f00b6fb6 19 });
2c5491e1 20 it('should map characters that are in Windows-1252 but not in Latin-1 to keysyms', function () {
524d67f2 21 expect(keysyms.lookup('Š'.charCodeAt())).to.be.equal(0x01a9);
f00b6fb6 22 });
2c5491e1 23 it('should map characters which aren\'t in Latin1 *or* Windows-1252 to keysyms', function () {
278a5e7f 24 expect(keysyms.lookup('ũ'.charCodeAt())).to.be.equal(0x03fd);
f00b6fb6 25 });
2c5491e1 26 it('should map unknown codepoints to the Unicode range', function () {
524d67f2
PO
27 expect(keysyms.lookup('\n'.charCodeAt())).to.be.equal(0x100000a);
28 expect(keysyms.lookup('\u262D'.charCodeAt())).to.be.equal(0x100262d);
e9ddbec5 29 });
331ae153 30 // This requires very recent versions of most browsers... skipping for now
2c5491e1 31 it.skip('should map UCS-4 codepoints to the Unicode range', function () {
524d67f2 32 //expect(keysyms.lookup('\u{1F686}'.codePointAt())).to.be.equal(0x101f686);
f00b6fb6 33 });
34 });
35
2c5491e1
PO
36 describe('getKeycode', function () {
37 it('should pass through proper code', function () {
80cb8ffd
PO
38 expect(KeyboardUtil.getKeycode({code: 'Semicolon'})).to.be.equal('Semicolon');
39 });
2c5491e1 40 it('should map legacy values', function () {
80cb8ffd
PO
41 expect(KeyboardUtil.getKeycode({code: ''})).to.be.equal('Unidentified');
42 expect(KeyboardUtil.getKeycode({code: 'OSLeft'})).to.be.equal('MetaLeft');
43 });
2c5491e1 44 it('should map keyCode to code when possible', function () {
80cb8ffd
PO
45 expect(KeyboardUtil.getKeycode({keyCode: 0x14})).to.be.equal('CapsLock');
46 expect(KeyboardUtil.getKeycode({keyCode: 0x5b})).to.be.equal('MetaLeft');
47 expect(KeyboardUtil.getKeycode({keyCode: 0x35})).to.be.equal('Digit5');
48 expect(KeyboardUtil.getKeycode({keyCode: 0x65})).to.be.equal('Numpad5');
49 });
2c5491e1 50 it('should map keyCode left/right side', function () {
80cb8ffd
PO
51 expect(KeyboardUtil.getKeycode({keyCode: 0x10, location: 1})).to.be.equal('ShiftLeft');
52 expect(KeyboardUtil.getKeycode({keyCode: 0x10, location: 2})).to.be.equal('ShiftRight');
53 expect(KeyboardUtil.getKeycode({keyCode: 0x11, location: 1})).to.be.equal('ControlLeft');
54 expect(KeyboardUtil.getKeycode({keyCode: 0x11, location: 2})).to.be.equal('ControlRight');
55 });
2c5491e1 56 it('should map keyCode on numpad', function () {
80cb8ffd
PO
57 expect(KeyboardUtil.getKeycode({keyCode: 0x0d, location: 0})).to.be.equal('Enter');
58 expect(KeyboardUtil.getKeycode({keyCode: 0x0d, location: 3})).to.be.equal('NumpadEnter');
59 expect(KeyboardUtil.getKeycode({keyCode: 0x23, location: 0})).to.be.equal('End');
60 expect(KeyboardUtil.getKeycode({keyCode: 0x23, location: 3})).to.be.equal('Numpad1');
61 });
2c5491e1 62 it('should return Unidentified when it cannot map the keyCode', function () {
80cb8ffd
PO
63 expect(KeyboardUtil.getKeycode({keycode: 0x42})).to.be.equal('Unidentified');
64 });
65
2c5491e1 66 describe('Fix Meta on macOS', function () {
2b5f94fa 67 let origNavigator;
80cb8ffd
PO
68 beforeEach(function () {
69 // window.navigator is a protected read-only property in many
70 // environments, so we need to redefine it whilst running these
71 // tests.
72 origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
73 if (origNavigator === undefined) {
74 // Object.getOwnPropertyDescriptor() doesn't work
75 // properly in any version of IE
76 this.skip();
77 }
78
79 Object.defineProperty(window, "navigator", {value: {}});
80 if (window.navigator.platform !== undefined) {
81 // Object.defineProperty() doesn't work properly in old
82 // versions of Chrome
83 this.skip();
84 }
85
86 window.navigator.platform = "Mac x86_64";
87 });
88 afterEach(function () {
eb05b45b
PO
89 if (origNavigator !== undefined) {
90 Object.defineProperty(window, "navigator", origNavigator);
91 }
80cb8ffd
PO
92 });
93
2c5491e1 94 it('should respect ContextMenu on modern browser', function () {
80cb8ffd
PO
95 expect(KeyboardUtil.getKeycode({code: 'ContextMenu', keyCode: 0x5d})).to.be.equal('ContextMenu');
96 });
2c5491e1 97 it('should translate legacy ContextMenu to MetaRight', function () {
80cb8ffd
PO
98 expect(KeyboardUtil.getKeycode({keyCode: 0x5d})).to.be.equal('MetaRight');
99 });
100 });
101 });
102
2c5491e1
PO
103 describe('getKey', function () {
104 it('should prefer key', function () {
59ef2916 105 if (browser.isIE() || browser.isEdge()) this.skip();
9782d4a3
PO
106 expect(KeyboardUtil.getKey({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('a');
107 });
2c5491e1 108 it('should map legacy values', function () {
9782d4a3
PO
109 expect(KeyboardUtil.getKey({key: 'Spacebar'})).to.be.equal(' ');
110 expect(KeyboardUtil.getKey({key: 'Left'})).to.be.equal('ArrowLeft');
111 expect(KeyboardUtil.getKey({key: 'OS'})).to.be.equal('Meta');
112 expect(KeyboardUtil.getKey({key: 'Win'})).to.be.equal('Meta');
e7c4d669 113 expect(KeyboardUtil.getKey({key: 'UIKeyInputLeftArrow'})).to.be.equal('ArrowLeft');
9782d4a3 114 });
9d956e91
PO
115 it('should handle broken Delete', function () {
116 expect(KeyboardUtil.getKey({key: '\x00', code: 'NumpadDecimal'})).to.be.equal('Delete');
117 });
2c5491e1 118 it('should use code if no key', function () {
9782d4a3
PO
119 expect(KeyboardUtil.getKey({code: 'NumpadBackspace'})).to.be.equal('Backspace');
120 });
2c5491e1 121 it('should not use code fallback for character keys', function () {
9782d4a3
PO
122 expect(KeyboardUtil.getKey({code: 'KeyA'})).to.be.equal('Unidentified');
123 expect(KeyboardUtil.getKey({code: 'Digit1'})).to.be.equal('Unidentified');
124 expect(KeyboardUtil.getKey({code: 'Period'})).to.be.equal('Unidentified');
125 expect(KeyboardUtil.getKey({code: 'Numpad1'})).to.be.equal('Unidentified');
f00b6fb6 126 });
2c5491e1 127 it('should use charCode if no key', function () {
9782d4a3 128 expect(KeyboardUtil.getKey({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('Š');
f00b6fb6 129 });
2c5491e1 130 it('should return Unidentified when it cannot map the key', function () {
9782d4a3
PO
131 expect(KeyboardUtil.getKey({keycode: 0x42})).to.be.equal('Unidentified');
132 });
133
2c5491e1 134 describe('Broken key AltGraph on IE/Edge', function () {
2b5f94fa 135 let origNavigator;
9782d4a3
PO
136 beforeEach(function () {
137 // window.navigator is a protected read-only property in many
138 // environments, so we need to redefine it whilst running these
139 // tests.
140 origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
141 if (origNavigator === undefined) {
142 // Object.getOwnPropertyDescriptor() doesn't work
143 // properly in any version of IE
144 this.skip();
145 }
a5c8a755 146
9782d4a3
PO
147 Object.defineProperty(window, "navigator", {value: {}});
148 if (window.navigator.platform !== undefined) {
149 // Object.defineProperty() doesn't work properly in old
150 // versions of Chrome
151 this.skip();
152 }
153 });
154 afterEach(function () {
eb05b45b
PO
155 if (origNavigator !== undefined) {
156 Object.defineProperty(window, "navigator", origNavigator);
157 }
9782d4a3
PO
158 });
159
2c5491e1 160 it('should ignore printable character key on IE', function () {
9782d4a3
PO
161 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
162 expect(KeyboardUtil.getKey({key: 'a'})).to.be.equal('Unidentified');
163 });
2c5491e1 164 it('should ignore printable character key on Edge', function () {
9782d4a3
PO
165 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
166 expect(KeyboardUtil.getKey({key: 'a'})).to.be.equal('Unidentified');
167 });
2c5491e1 168 it('should allow non-printable character key on IE', function () {
9782d4a3
PO
169 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
170 expect(KeyboardUtil.getKey({key: 'Shift'})).to.be.equal('Shift');
171 });
2c5491e1 172 it('should allow non-printable character key on Edge', function () {
9782d4a3
PO
173 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
174 expect(KeyboardUtil.getKey({key: 'Shift'})).to.be.equal('Shift');
175 });
5736ea0b
PO
176 it('should allow printable character key with charCode on IE', function () {
177 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
178 expect(KeyboardUtil.getKey({key: 'a', charCode: 0x61})).to.be.equal('a');
179 expect(KeyboardUtil.getKey({key: 'Unidentified', charCode: 0x61})).to.be.equal('a');
180 });
181 it('should allow printable character key with charCode on Edge', function () {
182 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
183 expect(KeyboardUtil.getKey({key: 'a', charCode: 0x61})).to.be.equal('a');
184 expect(KeyboardUtil.getKey({key: 'Unidentified', charCode: 0x61})).to.be.equal('a');
185 });
9782d4a3
PO
186 });
187 });
188
2c5491e1
PO
189 describe('getKeysym', function () {
190 describe('Non-character keys', function () {
191 it('should recognize the right keys', function () {
9782d4a3
PO
192 expect(KeyboardUtil.getKeysym({key: 'Enter'})).to.be.equal(0xFF0D);
193 expect(KeyboardUtil.getKeysym({key: 'Backspace'})).to.be.equal(0xFF08);
194 expect(KeyboardUtil.getKeysym({key: 'Tab'})).to.be.equal(0xFF09);
195 expect(KeyboardUtil.getKeysym({key: 'Shift'})).to.be.equal(0xFFE1);
196 expect(KeyboardUtil.getKeysym({key: 'Control'})).to.be.equal(0xFFE3);
197 expect(KeyboardUtil.getKeysym({key: 'Alt'})).to.be.equal(0xFFE9);
198 expect(KeyboardUtil.getKeysym({key: 'Meta'})).to.be.equal(0xFFEB);
199 expect(KeyboardUtil.getKeysym({key: 'Escape'})).to.be.equal(0xFF1B);
200 expect(KeyboardUtil.getKeysym({key: 'ArrowUp'})).to.be.equal(0xFF52);
201 });
2c5491e1 202 it('should map left/right side', function () {
9782d4a3
PO
203 expect(KeyboardUtil.getKeysym({key: 'Shift', location: 1})).to.be.equal(0xFFE1);
204 expect(KeyboardUtil.getKeysym({key: 'Shift', location: 2})).to.be.equal(0xFFE2);
205 expect(KeyboardUtil.getKeysym({key: 'Control', location: 1})).to.be.equal(0xFFE3);
206 expect(KeyboardUtil.getKeysym({key: 'Control', location: 2})).to.be.equal(0xFFE4);
a5c8a755 207 });
2c5491e1 208 it('should handle AltGraph', function () {
9782d4a3
PO
209 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Alt', location: 2})).to.be.equal(0xFFEA);
210 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph', location: 2})).to.be.equal(0xFE03);
f714f7de 211 });
3388c92c
PO
212 it('should handle Windows key with incorrect location', function () {
213 expect(KeyboardUtil.getKeysym({key: 'Meta', location: 0})).to.be.equal(0xFFEC);
214 });
215 it('should handle Clear/NumLock key with incorrect location', function () {
216 this.skip(); // Broken because of Clear/NumLock override
217 expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock', location: 3})).to.be.equal(0xFF0B);
218 });
75839905
PO
219 it('should handle Meta/Windows distinction', function () {
220 expect(KeyboardUtil.getKeysym({code: 'AltLeft', key: 'Meta', location: 1})).to.be.equal(0xFFE7);
221 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Meta', location: 2})).to.be.equal(0xFFE8);
222 expect(KeyboardUtil.getKeysym({code: 'MetaLeft', key: 'Meta', location: 1})).to.be.equal(0xFFEB);
223 expect(KeyboardUtil.getKeysym({code: 'MetaRight', key: 'Meta', location: 2})).to.be.equal(0xFFEC);
224 });
3388c92c
PO
225 it('should send NumLock even if key is Clear', function () {
226 expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock'})).to.be.equal(0xFF7F);
227 });
2c5491e1 228 it('should return null for unknown keys', function () {
9782d4a3
PO
229 expect(KeyboardUtil.getKeysym({key: 'Semicolon'})).to.be.null;
230 expect(KeyboardUtil.getKeysym({key: 'BracketRight'})).to.be.null;
bfa1b237 231 });
2c5491e1 232 it('should handle remappings', function () {
9782d4a3 233 expect(KeyboardUtil.getKeysym({code: 'ControlLeft', key: 'Tab'})).to.be.equal(0xFF09);
f714f7de
PO
234 });
235 });
236
2c5491e1
PO
237 describe('Numpad', function () {
238 it('should handle Numpad numbers', function () {
59ef2916 239 if (browser.isIE() || browser.isEdge()) this.skip();
f714f7de
PO
240 expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
241 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
242 });
2c5491e1 243 it('should handle Numpad non-character keys', function () {
f714f7de
PO
244 expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50);
245 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95);
246 expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF);
247 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
248 });
2c5491e1 249 it('should handle Numpad Decimal key', function () {
59ef2916 250 if (browser.isIE() || browser.isEdge()) this.skip();
f714f7de
PO
251 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
252 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
a5c8a755
PO
253 });
254 });
f00b6fb6 255 });
f00b6fb6 256});