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