]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.helper.js
Remove generated HTML by Cursor when it detaches
[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 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('getKey', function () {
102 it('should prefer key', function () {
103 if (browser.isIE() || browser.isEdge()) this.skip();
104 expect(KeyboardUtil.getKey({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('a');
105 });
106 it('should map legacy values', function () {
107 expect(KeyboardUtil.getKey({key: 'Spacebar'})).to.be.equal(' ');
108 expect(KeyboardUtil.getKey({key: 'Left'})).to.be.equal('ArrowLeft');
109 expect(KeyboardUtil.getKey({key: 'OS'})).to.be.equal('Meta');
110 expect(KeyboardUtil.getKey({key: 'Win'})).to.be.equal('Meta');
111 expect(KeyboardUtil.getKey({key: 'UIKeyInputLeftArrow'})).to.be.equal('ArrowLeft');
112 });
113 it('should handle broken Delete', function () {
114 expect(KeyboardUtil.getKey({key: '\x00', code: 'NumpadDecimal'})).to.be.equal('Delete');
115 });
116 it('should use code if no key', function () {
117 expect(KeyboardUtil.getKey({code: 'NumpadBackspace'})).to.be.equal('Backspace');
118 });
119 it('should not use code fallback for character keys', function () {
120 expect(KeyboardUtil.getKey({code: 'KeyA'})).to.be.equal('Unidentified');
121 expect(KeyboardUtil.getKey({code: 'Digit1'})).to.be.equal('Unidentified');
122 expect(KeyboardUtil.getKey({code: 'Period'})).to.be.equal('Unidentified');
123 expect(KeyboardUtil.getKey({code: 'Numpad1'})).to.be.equal('Unidentified');
124 });
125 it('should use charCode if no key', function () {
126 expect(KeyboardUtil.getKey({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('Š');
127 });
128 it('should return Unidentified when it cannot map the key', function () {
129 expect(KeyboardUtil.getKey({keycode: 0x42})).to.be.equal('Unidentified');
130 });
131
132 describe('Broken key AltGraph on IE/Edge', function () {
133 let origNavigator;
134 beforeEach(function () {
135 // window.navigator is a protected read-only property in many
136 // environments, so we need to redefine it whilst running these
137 // tests.
138 origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
139 if (origNavigator === undefined) {
140 // Object.getOwnPropertyDescriptor() doesn't work
141 // properly in any version of IE
142 this.skip();
143 }
144
145 Object.defineProperty(window, "navigator", {value: {}});
146 if (window.navigator.platform !== undefined) {
147 // Object.defineProperty() doesn't work properly in old
148 // versions of Chrome
149 this.skip();
150 }
151 });
152 afterEach(function () {
153 Object.defineProperty(window, "navigator", origNavigator);
154 });
155
156 it('should ignore printable character key on IE', function () {
157 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
158 expect(KeyboardUtil.getKey({key: 'a'})).to.be.equal('Unidentified');
159 });
160 it('should ignore printable character key on Edge', function () {
161 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";
162 expect(KeyboardUtil.getKey({key: 'a'})).to.be.equal('Unidentified');
163 });
164 it('should allow non-printable character key on IE', function () {
165 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
166 expect(KeyboardUtil.getKey({key: 'Shift'})).to.be.equal('Shift');
167 });
168 it('should allow non-printable character key on Edge', function () {
169 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";
170 expect(KeyboardUtil.getKey({key: 'Shift'})).to.be.equal('Shift');
171 });
172 it('should allow printable character key with charCode on IE', function () {
173 window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
174 expect(KeyboardUtil.getKey({key: 'a', charCode: 0x61})).to.be.equal('a');
175 expect(KeyboardUtil.getKey({key: 'Unidentified', charCode: 0x61})).to.be.equal('a');
176 });
177 it('should allow printable character key with charCode on Edge', function () {
178 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";
179 expect(KeyboardUtil.getKey({key: 'a', charCode: 0x61})).to.be.equal('a');
180 expect(KeyboardUtil.getKey({key: 'Unidentified', charCode: 0x61})).to.be.equal('a');
181 });
182 });
183 });
184
185 describe('getKeysym', function () {
186 describe('Non-character keys', function () {
187 it('should recognize the right keys', function () {
188 expect(KeyboardUtil.getKeysym({key: 'Enter'})).to.be.equal(0xFF0D);
189 expect(KeyboardUtil.getKeysym({key: 'Backspace'})).to.be.equal(0xFF08);
190 expect(KeyboardUtil.getKeysym({key: 'Tab'})).to.be.equal(0xFF09);
191 expect(KeyboardUtil.getKeysym({key: 'Shift'})).to.be.equal(0xFFE1);
192 expect(KeyboardUtil.getKeysym({key: 'Control'})).to.be.equal(0xFFE3);
193 expect(KeyboardUtil.getKeysym({key: 'Alt'})).to.be.equal(0xFFE9);
194 expect(KeyboardUtil.getKeysym({key: 'Meta'})).to.be.equal(0xFFEB);
195 expect(KeyboardUtil.getKeysym({key: 'Escape'})).to.be.equal(0xFF1B);
196 expect(KeyboardUtil.getKeysym({key: 'ArrowUp'})).to.be.equal(0xFF52);
197 });
198 it('should map left/right side', function () {
199 expect(KeyboardUtil.getKeysym({key: 'Shift', location: 1})).to.be.equal(0xFFE1);
200 expect(KeyboardUtil.getKeysym({key: 'Shift', location: 2})).to.be.equal(0xFFE2);
201 expect(KeyboardUtil.getKeysym({key: 'Control', location: 1})).to.be.equal(0xFFE3);
202 expect(KeyboardUtil.getKeysym({key: 'Control', location: 2})).to.be.equal(0xFFE4);
203 });
204 it('should handle AltGraph', function () {
205 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Alt', location: 2})).to.be.equal(0xFFEA);
206 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph', location: 2})).to.be.equal(0xFE03);
207 });
208 it('should handle Windows key with incorrect location', function () {
209 expect(KeyboardUtil.getKeysym({key: 'Meta', location: 0})).to.be.equal(0xFFEC);
210 });
211 it('should handle Clear/NumLock key with incorrect location', function () {
212 this.skip(); // Broken because of Clear/NumLock override
213 expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock', location: 3})).to.be.equal(0xFF0B);
214 });
215 it('should handle Meta/Windows distinction', function () {
216 expect(KeyboardUtil.getKeysym({code: 'AltLeft', key: 'Meta', location: 1})).to.be.equal(0xFFE7);
217 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Meta', location: 2})).to.be.equal(0xFFE8);
218 expect(KeyboardUtil.getKeysym({code: 'MetaLeft', key: 'Meta', location: 1})).to.be.equal(0xFFEB);
219 expect(KeyboardUtil.getKeysym({code: 'MetaRight', key: 'Meta', location: 2})).to.be.equal(0xFFEC);
220 });
221 it('should send NumLock even if key is Clear', function () {
222 expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock'})).to.be.equal(0xFF7F);
223 });
224 it('should return null for unknown keys', function () {
225 expect(KeyboardUtil.getKeysym({key: 'Semicolon'})).to.be.null;
226 expect(KeyboardUtil.getKeysym({key: 'BracketRight'})).to.be.null;
227 });
228 it('should handle remappings', function () {
229 expect(KeyboardUtil.getKeysym({code: 'ControlLeft', key: 'Tab'})).to.be.equal(0xFF09);
230 });
231 });
232
233 describe('Numpad', function () {
234 it('should handle Numpad numbers', function () {
235 if (browser.isIE() || browser.isEdge()) this.skip();
236 expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
237 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
238 });
239 it('should handle Numpad non-character keys', function () {
240 expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50);
241 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95);
242 expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF);
243 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
244 });
245 it('should handle Numpad Decimal key', function () {
246 if (browser.isIE() || browser.isEdge()) this.skip();
247 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
248 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
249 });
250 });
251 });
252 });