]>
Commit | Line | Data |
---|---|---|
2b5f94fa | 1 | const expect = chai.expect; |
f00b6fb6 | 2 | |
dfae3209 SR |
3 | import keysyms from '../core/input/keysymdef.js'; |
4 | import * as KeyboardUtil from "../core/input/util.js"; | |
099eb856 | 5 | |
2c5491e1 | 6 | describe('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 PO |
102 | expect(KeyboardUtil.getKey({key: 'Spacebar'})).to.be.equal(' '); |
103 | expect(KeyboardUtil.getKey({key: 'Left'})).to.be.equal('ArrowLeft'); | |
104 | expect(KeyboardUtil.getKey({key: 'OS'})).to.be.equal('Meta'); | |
105 | expect(KeyboardUtil.getKey({key: 'Win'})).to.be.equal('Meta'); | |
e7c4d669 | 106 | expect(KeyboardUtil.getKey({key: 'UIKeyInputLeftArrow'})).to.be.equal('ArrowLeft'); |
9782d4a3 | 107 | }); |
9d956e91 PO |
108 | it('should handle broken Delete', function () { |
109 | expect(KeyboardUtil.getKey({key: '\x00', code: 'NumpadDecimal'})).to.be.equal('Delete'); | |
110 | }); | |
2c5491e1 | 111 | it('should use code if no key', function () { |
9782d4a3 PO |
112 | expect(KeyboardUtil.getKey({code: 'NumpadBackspace'})).to.be.equal('Backspace'); |
113 | }); | |
2c5491e1 | 114 | it('should not use code fallback for character keys', function () { |
9782d4a3 PO |
115 | expect(KeyboardUtil.getKey({code: 'KeyA'})).to.be.equal('Unidentified'); |
116 | expect(KeyboardUtil.getKey({code: 'Digit1'})).to.be.equal('Unidentified'); | |
117 | expect(KeyboardUtil.getKey({code: 'Period'})).to.be.equal('Unidentified'); | |
118 | expect(KeyboardUtil.getKey({code: 'Numpad1'})).to.be.equal('Unidentified'); | |
f00b6fb6 | 119 | }); |
2c5491e1 | 120 | it('should use charCode if no key', function () { |
9782d4a3 | 121 | expect(KeyboardUtil.getKey({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('Š'); |
f00b6fb6 | 122 | }); |
2c5491e1 | 123 | it('should return Unidentified when it cannot map the key', function () { |
9782d4a3 PO |
124 | expect(KeyboardUtil.getKey({keycode: 0x42})).to.be.equal('Unidentified'); |
125 | }); | |
9782d4a3 PO |
126 | }); |
127 | ||
2c5491e1 PO |
128 | describe('getKeysym', function () { |
129 | describe('Non-character keys', function () { | |
130 | it('should recognize the right keys', function () { | |
9782d4a3 PO |
131 | expect(KeyboardUtil.getKeysym({key: 'Enter'})).to.be.equal(0xFF0D); |
132 | expect(KeyboardUtil.getKeysym({key: 'Backspace'})).to.be.equal(0xFF08); | |
133 | expect(KeyboardUtil.getKeysym({key: 'Tab'})).to.be.equal(0xFF09); | |
134 | expect(KeyboardUtil.getKeysym({key: 'Shift'})).to.be.equal(0xFFE1); | |
135 | expect(KeyboardUtil.getKeysym({key: 'Control'})).to.be.equal(0xFFE3); | |
136 | expect(KeyboardUtil.getKeysym({key: 'Alt'})).to.be.equal(0xFFE9); | |
137 | expect(KeyboardUtil.getKeysym({key: 'Meta'})).to.be.equal(0xFFEB); | |
138 | expect(KeyboardUtil.getKeysym({key: 'Escape'})).to.be.equal(0xFF1B); | |
139 | expect(KeyboardUtil.getKeysym({key: 'ArrowUp'})).to.be.equal(0xFF52); | |
140 | }); | |
2c5491e1 | 141 | it('should map left/right side', function () { |
9782d4a3 PO |
142 | expect(KeyboardUtil.getKeysym({key: 'Shift', location: 1})).to.be.equal(0xFFE1); |
143 | expect(KeyboardUtil.getKeysym({key: 'Shift', location: 2})).to.be.equal(0xFFE2); | |
144 | expect(KeyboardUtil.getKeysym({key: 'Control', location: 1})).to.be.equal(0xFFE3); | |
145 | expect(KeyboardUtil.getKeysym({key: 'Control', location: 2})).to.be.equal(0xFFE4); | |
a5c8a755 | 146 | }); |
2c5491e1 | 147 | it('should handle AltGraph', function () { |
9782d4a3 PO |
148 | expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Alt', location: 2})).to.be.equal(0xFFEA); |
149 | expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph', location: 2})).to.be.equal(0xFE03); | |
f714f7de | 150 | }); |
3388c92c PO |
151 | it('should handle Windows key with incorrect location', function () { |
152 | expect(KeyboardUtil.getKeysym({key: 'Meta', location: 0})).to.be.equal(0xFFEC); | |
153 | }); | |
154 | it('should handle Clear/NumLock key with incorrect location', function () { | |
155 | this.skip(); // Broken because of Clear/NumLock override | |
156 | expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock', location: 3})).to.be.equal(0xFF0B); | |
157 | }); | |
75839905 PO |
158 | it('should handle Meta/Windows distinction', function () { |
159 | expect(KeyboardUtil.getKeysym({code: 'AltLeft', key: 'Meta', location: 1})).to.be.equal(0xFFE7); | |
160 | expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Meta', location: 2})).to.be.equal(0xFFE8); | |
161 | expect(KeyboardUtil.getKeysym({code: 'MetaLeft', key: 'Meta', location: 1})).to.be.equal(0xFFEB); | |
162 | expect(KeyboardUtil.getKeysym({code: 'MetaRight', key: 'Meta', location: 2})).to.be.equal(0xFFEC); | |
163 | }); | |
3388c92c PO |
164 | it('should send NumLock even if key is Clear', function () { |
165 | expect(KeyboardUtil.getKeysym({key: 'Clear', code: 'NumLock'})).to.be.equal(0xFF7F); | |
166 | }); | |
2c5491e1 | 167 | it('should return null for unknown keys', function () { |
9782d4a3 PO |
168 | expect(KeyboardUtil.getKeysym({key: 'Semicolon'})).to.be.null; |
169 | expect(KeyboardUtil.getKeysym({key: 'BracketRight'})).to.be.null; | |
bfa1b237 | 170 | }); |
2c5491e1 | 171 | it('should handle remappings', function () { |
9782d4a3 | 172 | expect(KeyboardUtil.getKeysym({code: 'ControlLeft', key: 'Tab'})).to.be.equal(0xFF09); |
f714f7de PO |
173 | }); |
174 | }); | |
175 | ||
2c5491e1 PO |
176 | describe('Numpad', function () { |
177 | it('should handle Numpad numbers', function () { | |
f714f7de PO |
178 | expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035); |
179 | expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5); | |
180 | }); | |
2c5491e1 | 181 | it('should handle Numpad non-character keys', function () { |
f714f7de PO |
182 | expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50); |
183 | expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95); | |
184 | expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF); | |
185 | expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F); | |
186 | }); | |
2c5491e1 | 187 | it('should handle Numpad Decimal key', function () { |
f714f7de PO |
188 | expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE); |
189 | expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC); | |
a5c8a755 PO |
190 | }); |
191 | }); | |
f00b6fb6 | 192 | }); |
f00b6fb6 | 193 | }); |