summaryrefslogtreecommitdiff
path: root/tests/test.keyboard.js
blob: f9d5ff11c5d04b5e826564dec744aae00eab1613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var assert = chai.assert;
var expect = chai.expect;

import { Keyboard } from '../core/input/devices.js';
import keysyms from '../core/input/keysymdef.js';
import * as KeyboardUtil from '../core/input/util.js';

/* jshint newcap: false, expr: true */
describe('Key Event Handling', function() {
    "use strict";

    // The real KeyboardEvent constructor might not work everywhere we
    // want to run these tests
    function keyevent(typeArg, KeyboardEventInit) {
        var e = { type: typeArg };
        for (var key in KeyboardEventInit) {
            e[key] = KeyboardEventInit[key];
        }
        e.stopPropagation = sinon.spy();
        e.preventDefault = sinon.spy();
        return e;
    };

    describe('Decode Keyboard Events', function() {
        it('should decode keydown events', function(done) {
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                expect(keysym).to.be.equal(0x61);
                expect(code).to.be.equal('KeyA');
                expect(down).to.be.equal(true);
                done();
            }});
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
        });
        it('should decode keyup events', function(done) {
            var calls = 0;
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                expect(keysym).to.be.equal(0x61);
                expect(code).to.be.equal('KeyA');
                if (calls++ === 1) {
                    expect(down).to.be.equal(false);
                    done();
                }
            }});
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
            kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
        });

        describe('Legacy keypress Events', function() {
            it('should wait for keypress when needed', function() {
                var callback = sinon.spy();
                var kbd = new Keyboard({onKeyEvent: callback});
                kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
                expect(callback).to.not.have.been.called;
            });
            it('should decode keypress events', function(done) {
                var kbd = new Keyboard({
                onKeyEvent: function(keysym, code, down) {
                    expect(keysym).to.be.equal(0x61);
                    expect(code).to.be.equal('KeyA');
                    expect(down).to.be.equal(true);
                    done();
                }});
                kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
                kbd._handleKeyPress(keyevent('keypress', {code: 'KeyA', charCode: 0x61}));
            });
            it('should ignore keypress with different code', function() {
                var callback = sinon.spy();
                var kbd = new Keyboard({onKeyEvent: callback});
                kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
                kbd._handleKeyPress(keyevent('keypress', {code: 'KeyB', charCode: 0x61}));
                expect(callback).to.not.have.been.called;
            });
            it('should handle keypress with missing code', function(done) {
                var kbd = new Keyboard({
                onKeyEvent: function(keysym, code, down) {
                    expect(keysym).to.be.equal(0x61);
                    expect(code).to.be.equal('KeyA');
                    expect(down).to.be.equal(true);
                    done();
                }});
                kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
                kbd._handleKeyPress(keyevent('keypress', {charCode: 0x61}));
            });
        });

        describe('suppress the right events at the right time', function() {
            it('should suppress anything with a valid key', function() {
                var kbd = new Keyboard({});
                var evt = keyevent('keydown', {code: 'KeyA', key: 'a'});
                kbd._handleKeyDown(evt);
                expect(evt.preventDefault).to.have.been.called;
                evt = keyevent('keyup', {code: 'KeyA', key: 'a'});
                kbd._handleKeyUp(evt);
                expect(evt.preventDefault).to.have.been.called;
            });
            it('should not suppress keys without key', function() {
                var kbd = new Keyboard({});
                var evt = keyevent('keydown', {code: 'KeyA', keyCode: 0x41});
                kbd._handleKeyDown(evt);
                expect(evt.preventDefault).to.not.have.been.called;
            });
            it('should suppress the following keypress event', function() {
                var kbd = new Keyboard({});
                var evt = keyevent('keydown', {code: 'KeyA', keyCode: 0x41});
                kbd._handleKeyDown(evt);
                var evt = keyevent('keypress', {code: 'KeyA', charCode: 0x41});
                kbd._handleKeyPress(evt);
                expect(evt.preventDefault).to.have.been.called;
            });
        });
    });

    describe('Track Key State', function() {
        it('should send release using the same keysym as the press', function(done) {
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                expect(keysym).to.be.equal(0x61);
                expect(code).to.be.equal('KeyA');
                if (!down) {
                    done();
                }
            }});
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
            kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'b'}));
        });
        it('should send the same keysym for multiple presses', function() {
            var count = 0;
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                expect(keysym).to.be.equal(0x61);
                expect(code).to.be.equal('KeyA');
                expect(down).to.be.equal(true);
                count++;
            }});
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'b'}));
            expect(count).to.be.equal(2);
        });
        it('should do nothing on keyup events if no keys are down', function() {
            var callback = sinon.spy();
            var kbd = new Keyboard({onKeyEvent: callback});
            kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
            expect(callback).to.not.have.been.called;
        });
    });

    describe('Escape Modifiers', function() {
        var origNavigator;
        beforeEach(function () {
            // window.navigator is a protected read-only property in many
            // environments, so we need to redefine it whilst running these
            // tests.
            origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
            if (origNavigator === undefined) {
                // Object.getOwnPropertyDescriptor() doesn't work
                // properly in any version of IE
                this.skip();
            }

            Object.defineProperty(window, "navigator", {value: {}});
            if (window.navigator.platform !== undefined) {
                // Object.defineProperty() doesn't work properly in old
                // versions of Chrome
                this.skip();
            }

            window.navigator.platform = "Windows x86_64";
        });
        afterEach(function () {
            Object.defineProperty(window, "navigator", origNavigator);
        });

        it('should generate fake undo/redo events on press when a char modifier is down', function() {
            var times_called = 0;
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                switch(times_called++) {
                case 0:
                    expect(keysym).to.be.equal(0xFFE3);
                    expect(code).to.be.equal('ControlLeft');
                    expect(down).to.be.equal(true);
                    break;
                case 1:
                    expect(keysym).to.be.equal(0xFFE9);
                    expect(code).to.be.equal('AltLeft');
                    expect(down).to.be.equal(true);
                    break;
                case 2:
                    expect(keysym).to.be.equal(0xFFE9);
                    expect(code).to.be.equal('Unidentified');
                    expect(down).to.be.equal(false);
                    break;
                case 3:
                    expect(keysym).to.be.equal(0xFFE3);
                    expect(code).to.be.equal('Unidentified');
                    expect(down).to.be.equal(false);
                    break;
                case 4:
                    expect(keysym).to.be.equal(0x61);
                    expect(code).to.be.equal('KeyA');
                    expect(down).to.be.equal(true);
                    break;
                case 5:
                    expect(keysym).to.be.equal(0xFFE9);
                    expect(code).to.be.equal('Unidentified');
                    expect(down).to.be.equal(true);
                    break;
                case 6:
                    expect(keysym).to.be.equal(0xFFE3);
                    expect(code).to.be.equal('Unidentified');
                    expect(down).to.be.equal(true);
                    break;
                }
            }});
            // First the modifier combo
            kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control'}));
            kbd._handleKeyDown(keyevent('keydown', {code: 'AltLeft', key: 'Alt'}));
            // Next a normal character
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
            expect(times_called).to.be.equal(7);
        });
        it('should no do anything on key release', function() {
            var times_called = 0;
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                switch(times_called++) {
                case 7:
                    expect(keysym).to.be.equal(0x61);
                    expect(code).to.be.equal('KeyA');
                    expect(down).to.be.equal(false);
                    break;
                }
            }});
            // First the modifier combo
            kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control'}));
            kbd._handleKeyDown(keyevent('keydown', {code: 'AltLeft', key: 'Alt'}));
            // Next a normal character
            kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
            kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
            expect(times_called).to.be.equal(8);
        });
        it('should not consider a char modifier to be down on the modifier key itself', function() {
            var times_called = 0;
            var kbd = new Keyboard({
            onKeyEvent: function(keysym, code, down) {
                switch(times_called++) {
                case 0:
                    expect(keysym).to.be.equal(0xFFE3);
                    expect(code).to.be.equal('ControlLeft');
                    expect(down).to.be.equal(true);
                    break;
                case 1:
                    expect(keysym).to.be.equal(0xFFE9);
                    expect(code).to.be.equal('AltLeft');
                    expect(down).to.be.equal(true);
                    break;
                case 2:
                    expect(keysym).to.be.equal(0xFFE3);
                    expect(code).to.be.equal('ControlLeft');
                    expect(down).to.be.equal(true);
                    break;
                }
            }});
            // First the modifier combo
            kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control'}));
            kbd._handleKeyDown(keyevent('keydown', {code: 'AltLeft', key: 'Alt'}));
            // Then one of the keys again
            kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control'}));
            expect(times_called).to.be.equal(3);
        });
    });
});