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