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