]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.helper.js
Improve lookup of special keys
[mirror_novnc.git] / tests / test.helper.js
1 var assert = chai.assert;
2 var expect = chai.expect;
3
4 import keysyms from '../core/input/keysymdef.js';
5 import * as KeyboardUtil from "../core/input/util.js";
6
7 describe('Helpers', function() {
8 "use strict";
9
10 describe('keysyms.lookup', function() {
11 it('should map ASCII characters to keysyms', function() {
12 expect(keysyms.lookup('a'.charCodeAt())).to.be.equal(0x61);
13 expect(keysyms.lookup('A'.charCodeAt())).to.be.equal(0x41);
14 });
15 it('should map Latin-1 characters to keysyms', function() {
16 expect(keysyms.lookup('ø'.charCodeAt())).to.be.equal(0xf8);
17
18 expect(keysyms.lookup('é'.charCodeAt())).to.be.equal(0xe9);
19 });
20 it('should map characters that are in Windows-1252 but not in Latin-1 to keysyms', function() {
21 expect(keysyms.lookup('Š'.charCodeAt())).to.be.equal(0x01a9);
22 });
23 it('should map characters which aren\'t in Latin1 *or* Windows-1252 to keysyms', function() {
24 expect(keysyms.lookup('ũ'.charCodeAt())).to.be.equal(0x03fd);
25 });
26 it('should map unknown codepoints to the Unicode range', function() {
27 expect(keysyms.lookup('\n'.charCodeAt())).to.be.equal(0x100000a);
28 expect(keysyms.lookup('\u262D'.charCodeAt())).to.be.equal(0x100262d);
29 });
30 // This requires very recent versions of most browsers... skipping for now
31 it.skip('should map UCS-4 codepoints to the Unicode range', function() {
32 //expect(keysyms.lookup('\u{1F686}'.codePointAt())).to.be.equal(0x101f686);
33 });
34 });
35
36 describe('getKeycode', function() {
37 it('should pass through proper code', function() {
38 expect(KeyboardUtil.getKeycode({code: 'Semicolon'})).to.be.equal('Semicolon');
39 });
40 it('should map legacy values', function() {
41 expect(KeyboardUtil.getKeycode({code: ''})).to.be.equal('Unidentified');
42 expect(KeyboardUtil.getKeycode({code: 'OSLeft'})).to.be.equal('MetaLeft');
43 });
44 it('should map keyCode to code when possible', function() {
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 });
50 it('should map keyCode left/right side', function() {
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 });
56 it('should map keyCode on numpad', function() {
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 });
62 it('should return Unidentified when it cannot map the keyCode', function() {
63 expect(KeyboardUtil.getKeycode({keycode: 0x42})).to.be.equal('Unidentified');
64 });
65
66 describe('Fix Meta on macOS', function() {
67 var origNavigator;
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 () {
89 Object.defineProperty(window, "navigator", origNavigator);
90 });
91
92 it('should respect ContextMenu on modern browser', function() {
93 expect(KeyboardUtil.getKeycode({code: 'ContextMenu', keyCode: 0x5d})).to.be.equal('ContextMenu');
94 });
95 it('should translate legacy ContextMenu to MetaRight', function() {
96 expect(KeyboardUtil.getKeycode({keyCode: 0x5d})).to.be.equal('MetaRight');
97 });
98 });
99 });
100
101 describe('getKeysym', function() {
102 it('should prefer key', function() {
103 expect(KeyboardUtil.getKeysym({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x61);
104 });
105 it('should use charCode if no key', function() {
106 expect(KeyboardUtil.getKeysym({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x01a9);
107 });
108
109 describe('Non-character keys', function() {
110 it('should recognize the right keys', function() {
111 expect(KeyboardUtil.getKeysym({code: 'Enter'})).to.be.equal(0xFF0D);
112 expect(KeyboardUtil.getKeysym({code: 'Backspace'})).to.be.equal(0xFF08);
113 expect(KeyboardUtil.getKeysym({code: 'Tab'})).to.be.equal(0xFF09);
114 expect(KeyboardUtil.getKeysym({code: 'ShiftLeft'})).to.be.equal(0xFFE1);
115 expect(KeyboardUtil.getKeysym({code: 'ControlLeft'})).to.be.equal(0xFFE3);
116 expect(KeyboardUtil.getKeysym({code: 'AltLeft'})).to.be.equal(0xFFE9);
117 expect(KeyboardUtil.getKeysym({code: 'MetaLeft'})).to.be.equal(0xFFEB);
118 expect(KeyboardUtil.getKeysym({code: 'Escape'})).to.be.equal(0xFF1B);
119 expect(KeyboardUtil.getKeysym({code: 'ArrowUp'})).to.be.equal(0xFF52);
120 });
121 it('should handle AltGraph', function() {
122 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltRight'})).to.be.equal(0xFFEA);
123 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph'})).to.be.equal(0xFE03);
124 });
125 it('should return null for unknown codes', function() {
126 expect(KeyboardUtil.getKeysym({code: 'Semicolon'})).to.be.null;
127 expect(KeyboardUtil.getKeysym({code: 'BracketRight'})).to.be.null;
128 });
129 it('should not recognize character keys', function() {
130 expect(KeyboardUtil.getKeysym({code: 'KeyA'})).to.be.null;
131 expect(KeyboardUtil.getKeysym({code: 'Digit1'})).to.be.null;
132 expect(KeyboardUtil.getKeysym({code: 'Period'})).to.be.null;
133 expect(KeyboardUtil.getKeysym({code: 'Numpad1'})).to.be.null;
134 });
135 });
136
137 describe('Numpad', function() {
138 it('should handle Numpad numbers', function() {
139 expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
140 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
141 });
142 it('should handle Numpad non-character keys', function() {
143 expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50);
144 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95);
145 expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF);
146 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
147 });
148 it('should handle IE/Edge key names', function() {
149 expect(KeyboardUtil.getKeysym({code: 'Numpad6', key: 'Right', location: 3})).to.be.equal(0xFF98);
150 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Del', location: 3})).to.be.equal(0xFF9F);
151 });
152 it('should handle Numpad Decimal key', function() {
153 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
154 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
155 });
156 });
157 });
158
159 describe('Modifier Sync', function() { // return a list of fake events necessary to fix modifier state
160 describe('Toggle all modifiers', function() {
161 var sync = KeyboardUtil.ModifierSync();
162 it ('should do nothing if all modifiers are up as expected', function() {
163 expect(sync.keydown({
164 code: 'KeyA',
165 ctrlKey: false,
166 altKey: false,
167 altGraphKey: false,
168 shiftKey: false,
169 metaKey: false})
170 ).to.have.lengthOf(0);
171 });
172 it ('should synthesize events if all keys are unexpectedly down', function() {
173 var result = sync.keydown({
174 code: 'KeyA',
175 ctrlKey: true,
176 altKey: true,
177 altGraphKey: true,
178 shiftKey: true,
179 metaKey: true
180 });
181 expect(result).to.have.lengthOf(5);
182 var keysyms = {};
183 for (var i = 0; i < result.length; ++i) {
184 keysyms[result[i].keysym] = (result[i].type == 'keydown');
185 }
186 expect(keysyms[0xffe3]);
187 expect(keysyms[0xffe9]);
188 expect(keysyms[0xfe03]);
189 expect(keysyms[0xffe1]);
190 expect(keysyms[0xffe7]);
191 });
192 it ('should do nothing if all modifiers are down as expected', function() {
193 expect(sync.keydown({
194 code: 'KeyA',
195 ctrlKey: true,
196 altKey: true,
197 altGraphKey: true,
198 shiftKey: true,
199 metaKey: true
200 })).to.have.lengthOf(0);
201 });
202 });
203 describe('Toggle Ctrl', function() {
204 var sync = KeyboardUtil.ModifierSync();
205 it('should sync if modifier is suddenly down', function() {
206 expect(sync.keydown({
207 code: 'KeyA',
208 ctrlKey: true,
209 })).to.be.deep.equal([{keysym: 0xffe3, type: 'keydown'}]);
210 });
211 it('should sync if modifier is suddenly up', function() {
212 expect(sync.keydown({
213 code: 'KeyA',
214 ctrlKey: false
215 })).to.be.deep.equal([{keysym: 0xffe3, type: 'keyup'}]);
216 });
217 });
218 describe('Toggle Alt', function() {
219 var sync = KeyboardUtil.ModifierSync();
220 it('should sync if modifier is suddenly down', function() {
221 expect(sync.keydown({
222 code: 'KeyA',
223 altKey: true,
224 })).to.be.deep.equal([{keysym: 0xffe9, type: 'keydown'}]);
225 });
226 it('should sync if modifier is suddenly up', function() {
227 expect(sync.keydown({
228 code: 'KeyA',
229 altKey: false
230 })).to.be.deep.equal([{keysym: 0xffe9, type: 'keyup'}]);
231 });
232 });
233 describe('Toggle AltGr', function() {
234 var sync = KeyboardUtil.ModifierSync();
235 it('should sync if modifier is suddenly down', function() {
236 expect(sync.keydown({
237 code: 'KeyA',
238 altGraphKey: true,
239 })).to.be.deep.equal([{keysym: 0xfe03, type: 'keydown'}]);
240 });
241 it('should sync if modifier is suddenly up', function() {
242 expect(sync.keydown({
243 code: 'KeyA',
244 altGraphKey: false
245 })).to.be.deep.equal([{keysym: 0xfe03, type: 'keyup'}]);
246 });
247 });
248 describe('Toggle Shift', function() {
249 var sync = KeyboardUtil.ModifierSync();
250 it('should sync if modifier is suddenly down', function() {
251 expect(sync.keydown({
252 code: 'KeyA',
253 shiftKey: true,
254 })).to.be.deep.equal([{keysym: 0xffe1, type: 'keydown'}]);
255 });
256 it('should sync if modifier is suddenly up', function() {
257 expect(sync.keydown({
258 code: 'KeyA',
259 shiftKey: false
260 })).to.be.deep.equal([{keysym: 0xffe1, type: 'keyup'}]);
261 });
262 });
263 describe('Toggle Meta', function() {
264 var sync = KeyboardUtil.ModifierSync();
265 it('should sync if modifier is suddenly down', function() {
266 expect(sync.keydown({
267 code: 'KeyA',
268 metaKey: true,
269 })).to.be.deep.equal([{keysym: 0xffe7, type: 'keydown'}]);
270 });
271 it('should sync if modifier is suddenly up', function() {
272 expect(sync.keydown({
273 code: 'KeyA',
274 metaKey: false
275 })).to.be.deep.equal([{keysym: 0xffe7, type: 'keyup'}]);
276 });
277 });
278 describe('Modifier keyevents', function() {
279 it('should not sync a modifier on its own events', function() {
280 expect(KeyboardUtil.ModifierSync().keydown({
281 code: 'ControlLeft',
282 ctrlKey: false
283 })).to.be.deep.equal([]);
284 expect(KeyboardUtil.ModifierSync().keydown({
285 code: 'ControlLeft',
286 ctrlKey: true
287 }), 'B').to.be.deep.equal([]);
288 })
289 it('should update state on modifier keyevents', function() {
290 var sync = KeyboardUtil.ModifierSync();
291 sync.keydown({
292 code: 'ControlLeft',
293 });
294 expect(sync.keydown({
295 code: 'KeyA',
296 ctrlKey: true,
297 })).to.be.deep.equal([]);
298 });
299 it('should sync other modifiers on ctrl events', function() {
300 expect(KeyboardUtil.ModifierSync().keydown({
301 code: 'ControlLeft',
302 altKey: true
303 })).to.be.deep.equal([{keysym: 0xffe9, type: 'keydown'}]);
304 })
305 });
306 describe('sync modifiers on non-key events', function() {
307 it('should generate sync events when receiving non-keyboard events', function() {
308 expect(KeyboardUtil.ModifierSync().syncAny({
309 altKey: true
310 })).to.be.deep.equal([{keysym: 0xffe9, type: 'keydown'}]);
311 });
312 });
313 describe('do not treat shift as a modifier key', function() {
314 it('should not treat shift as a char modifier', function() {
315 expect(KeyboardUtil.hasCharModifier([], {0xffe1 : true})).to.be.false;
316 });
317 });
318 });
319 });