]> git.proxmox.com Git - mirror_novnc.git/blame - tests/test.helper.js
Merge branch 'qemufix' of https://github.com/CendioOssman/noVNC
[mirror_novnc.git] / tests / test.helper.js
CommitLineData
dfae3209 1var assert = chai.assert;
f00b6fb6 2var expect = chai.expect;
3
dfae3209
SR
4import keysyms from '../core/input/keysymdef.js';
5import * as KeyboardUtil from "../core/input/util.js";
6
099eb856
PO
7function isIE() {
8 return navigator && !!(/trident/i).exec(navigator.userAgent);
9}
10function isEdge() {
11 return navigator && !!(/edge/i).exec(navigator.userAgent);
12}
13
f00b6fb6 14describe('Helpers', function() {
15 "use strict";
f00b6fb6 16
524d67f2 17 describe('keysyms.lookup', function() {
f00b6fb6 18 it('should map ASCII characters to keysyms', function() {
524d67f2
PO
19 expect(keysyms.lookup('a'.charCodeAt())).to.be.equal(0x61);
20 expect(keysyms.lookup('A'.charCodeAt())).to.be.equal(0x41);
f00b6fb6 21 });
22 it('should map Latin-1 characters to keysyms', function() {
524d67f2 23 expect(keysyms.lookup('ø'.charCodeAt())).to.be.equal(0xf8);
f00b6fb6 24
524d67f2 25 expect(keysyms.lookup('é'.charCodeAt())).to.be.equal(0xe9);
f00b6fb6 26 });
27 it('should map characters that are in Windows-1252 but not in Latin-1 to keysyms', function() {
524d67f2 28 expect(keysyms.lookup('Š'.charCodeAt())).to.be.equal(0x01a9);
f00b6fb6 29 });
30 it('should map characters which aren\'t in Latin1 *or* Windows-1252 to keysyms', function() {
278a5e7f 31 expect(keysyms.lookup('ũ'.charCodeAt())).to.be.equal(0x03fd);
f00b6fb6 32 });
115eedf6 33 it('should map unknown codepoints to the Unicode range', function() {
524d67f2
PO
34 expect(keysyms.lookup('\n'.charCodeAt())).to.be.equal(0x100000a);
35 expect(keysyms.lookup('\u262D'.charCodeAt())).to.be.equal(0x100262d);
e9ddbec5 36 });
331ae153
SM
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() {
524d67f2 39 //expect(keysyms.lookup('\u{1F686}'.codePointAt())).to.be.equal(0x101f686);
f00b6fb6 40 });
41 });
42
80cb8ffd
PO
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
9782d4a3 108 describe('getKey', function() {
bfa1b237 109 it('should prefer key', function() {
099eb856 110 if (isIE() || isEdge()) this.skip();
9782d4a3
PO
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');
e7c4d669 118 expect(KeyboardUtil.getKey({key: 'UIKeyInputLeftArrow'})).to.be.equal('ArrowLeft');
9782d4a3
PO
119 });
120 it('should use code if no key', function() {
121 expect(KeyboardUtil.getKey({code: 'NumpadBackspace'})).to.be.equal('Backspace');
122 });
123 it('should not use code fallback for character keys', function() {
124 expect(KeyboardUtil.getKey({code: 'KeyA'})).to.be.equal('Unidentified');
125 expect(KeyboardUtil.getKey({code: 'Digit1'})).to.be.equal('Unidentified');
126 expect(KeyboardUtil.getKey({code: 'Period'})).to.be.equal('Unidentified');
127 expect(KeyboardUtil.getKey({code: 'Numpad1'})).to.be.equal('Unidentified');
f00b6fb6 128 });
bfa1b237 129 it('should use charCode if no key', function() {
9782d4a3 130 expect(KeyboardUtil.getKey({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('Š');
f00b6fb6 131 });
9782d4a3
PO
132 it('should return Unidentified when it cannot map the key', function() {
133 expect(KeyboardUtil.getKey({keycode: 0x42})).to.be.equal('Unidentified');
134 });
135
136 describe('Broken key AltGraph on IE/Edge', function() {
137 var origNavigator;
138 beforeEach(function () {
139 // window.navigator is a protected read-only property in many
140 // environments, so we need to redefine it whilst running these
141 // tests.
142 origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
143 if (origNavigator === undefined) {
144 // Object.getOwnPropertyDescriptor() doesn't work
145 // properly in any version of IE
146 this.skip();
147 }
a5c8a755 148
9782d4a3
PO
149 Object.defineProperty(window, "navigator", {value: {}});
150 if (window.navigator.platform !== undefined) {
151 // Object.defineProperty() doesn't work properly in old
152 // versions of Chrome
153 this.skip();
154 }
155 });
156 afterEach(function () {
157 Object.defineProperty(window, "navigator", origNavigator);
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 });
177 });
178
179 describe('getKeysym', function() {
a5c8a755
PO
180 describe('Non-character keys', function() {
181 it('should recognize the right keys', function() {
9782d4a3
PO
182 expect(KeyboardUtil.getKeysym({key: 'Enter'})).to.be.equal(0xFF0D);
183 expect(KeyboardUtil.getKeysym({key: 'Backspace'})).to.be.equal(0xFF08);
184 expect(KeyboardUtil.getKeysym({key: 'Tab'})).to.be.equal(0xFF09);
185 expect(KeyboardUtil.getKeysym({key: 'Shift'})).to.be.equal(0xFFE1);
186 expect(KeyboardUtil.getKeysym({key: 'Control'})).to.be.equal(0xFFE3);
187 expect(KeyboardUtil.getKeysym({key: 'Alt'})).to.be.equal(0xFFE9);
188 expect(KeyboardUtil.getKeysym({key: 'Meta'})).to.be.equal(0xFFEB);
189 expect(KeyboardUtil.getKeysym({key: 'Escape'})).to.be.equal(0xFF1B);
190 expect(KeyboardUtil.getKeysym({key: 'ArrowUp'})).to.be.equal(0xFF52);
191 });
192 it('should map left/right side', function() {
193 expect(KeyboardUtil.getKeysym({key: 'Shift', location: 1})).to.be.equal(0xFFE1);
194 expect(KeyboardUtil.getKeysym({key: 'Shift', location: 2})).to.be.equal(0xFFE2);
195 expect(KeyboardUtil.getKeysym({key: 'Control', location: 1})).to.be.equal(0xFFE3);
196 expect(KeyboardUtil.getKeysym({key: 'Control', location: 2})).to.be.equal(0xFFE4);
a5c8a755 197 });
f714f7de 198 it('should handle AltGraph', function() {
9782d4a3
PO
199 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Alt', location: 2})).to.be.equal(0xFFEA);
200 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph', location: 2})).to.be.equal(0xFE03);
f714f7de 201 });
9782d4a3
PO
202 it('should return null for unknown keys', function() {
203 expect(KeyboardUtil.getKeysym({key: 'Semicolon'})).to.be.null;
204 expect(KeyboardUtil.getKeysym({key: 'BracketRight'})).to.be.null;
bfa1b237 205 });
9782d4a3
PO
206 it('should handle remappings', function() {
207 expect(KeyboardUtil.getKeysym({code: 'ControlLeft', key: 'Tab'})).to.be.equal(0xFF09);
f714f7de
PO
208 });
209 });
210
211 describe('Numpad', function() {
212 it('should handle Numpad numbers', function() {
099eb856 213 if (isIE() || isEdge()) this.skip();
f714f7de
PO
214 expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
215 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
216 });
217 it('should handle Numpad non-character keys', function() {
218 expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50);
219 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95);
220 expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF);
221 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
222 });
f714f7de 223 it('should handle Numpad Decimal key', function() {
099eb856 224 if (isIE() || isEdge()) this.skip();
f714f7de
PO
225 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
226 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
a5c8a755
PO
227 });
228 });
f00b6fb6 229 });
f00b6fb6 230});