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