]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.helper.js
Merge branch 'camelcase' of https://github.com/samhed/noVNC
[mirror_novnc.git] / tests / test.helper.js
1 const expect = chai.expect;
2
3 import keysyms from '../core/input/keysymdef.js';
4 import * as KeyboardUtil from "../core/input/util.js";
5 import * as browser from '../core/util/browser.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 let 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 if (origNavigator !== undefined) {
90 Object.defineProperty(window, "navigator", origNavigator);
91 }
92 });
93
94 it('should respect ContextMenu on modern browser', function () {
95 expect(KeyboardUtil.getKeycode({code: 'ContextMenu', keyCode: 0x5d})).to.be.equal('ContextMenu');
96 });
97 it('should translate legacy ContextMenu to MetaRight', function () {
98 expect(KeyboardUtil.getKeycode({keyCode: 0x5d})).to.be.equal('MetaRight');
99 });
100 });
101 });
102
103 describe('getKey', function () {
104 it('should prefer key', function () {
105 if (browser.isIE() || browser.isEdge()) this.skip();
106 expect(KeyboardUtil.getKey({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('a');
107 });
108 it('should map legacy values', function () {
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');
113 expect(KeyboardUtil.getKey({key: 'UIKeyInputLeftArrow'})).to.be.equal('ArrowLeft');
114 });
115 it('should handle broken Delete', function () {
116 expect(KeyboardUtil.getKey({key: '\x00', code: 'NumpadDecimal'})).to.be.equal('Delete');
117 });
118 it('should use code if no key', function () {
119 expect(KeyboardUtil.getKey({code: 'NumpadBackspace'})).to.be.equal('Backspace');
120 });
121 it('should not use code fallback for character keys', function () {
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');
126 });
127 it('should use charCode if no key', function () {
128 expect(KeyboardUtil.getKey({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('Š');
129 });
130 it('should return Unidentified when it cannot map the key', function () {
131 expect(KeyboardUtil.getKey({keycode: 0x42})).to.be.equal('Unidentified');
132 });
133
134 describe('Broken key AltGraph on IE/Edge', function () {
135 let origNavigator;
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 }
146
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 () {
155 if (origNavigator !== undefined) {
156 Object.defineProperty(window, "navigator", origNavigator);
157 }
158 });
159
160 it('should ignore printable character key on IE', function () {
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 });
164 it('should ignore printable character key on Edge', function () {
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 });
168 it('should allow non-printable character key on IE', function () {
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 });
172 it('should allow non-printable character key on Edge', function () {
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 });
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 });
186 });
187 });
188
189 describe('getKeysym', function () {
190 describe('Non-character keys', function () {
191 it('should recognize the right keys', function () {
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 });
202 it('should map left/right side', function () {
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);
207 });
208 it('should handle AltGraph', function () {
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);
211 });
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 });
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 });
225 it('should send NumLock even if key is Clear', function () {
226 expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock'})).to.be.equal(0xFF7F);
227 });
228 it('should return null for unknown keys', function () {
229 expect(KeyboardUtil.getKeysym({key: 'Semicolon'})).to.be.null;
230 expect(KeyboardUtil.getKeysym({key: 'BracketRight'})).to.be.null;
231 });
232 it('should handle remappings', function () {
233 expect(KeyboardUtil.getKeysym({code: 'ControlLeft', key: 'Tab'})).to.be.equal(0xFF09);
234 });
235 });
236
237 describe('Numpad', function () {
238 it('should handle Numpad numbers', function () {
239 if (browser.isIE() || browser.isEdge()) this.skip();
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 });
243 it('should handle Numpad non-character keys', function () {
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 });
249 it('should handle Numpad Decimal key', function () {
250 if (browser.isIE() || browser.isEdge()) this.skip();
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);
253 });
254 });
255 });
256 });