This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeyboard-test.js
353 lines (263 loc) · 11.1 KB
/
keyboard-test.js
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import Ember from 'ember';
import $ from 'jquery';
import { moduleFor, test } from 'ember-qunit';
const { run } = Ember;
// Haven't found a reliable way of spoofing user agent on phantom js.
var setUserAgent, resetUserAgent;
if (!/PhantomJS/i.test(window.navigator.userAgent)) {
const originalNavigator = window.navigator.__lookupGetter__('userAgent');
setUserAgent = function setUserAgent(str) {
window.navigator.__defineGetter__('userAgent', function(){
return str;
});
};
resetUserAgent = function resetUserAgent() {
window.navigator.__defineGetter__('userAgent', originalNavigator);
};
}
moduleFor('service:keyboard', 'Unit | Service | keyboard');
test('it listens for key down presses', function(assert) {
assert.expect(3);
const service = this.subject();
service.listenFor('f', this, function() { assert.ok(true); });
service.listenFor('o', this, function() { assert.ok(true); });
// Types 'foo'
$(document.body).trigger($.Event('keydown', { key: 'f' }));
$(document.body).trigger($.Event('keydown', { key: 'o' }));
$(document.body).trigger($.Event('keydown', { key: 'o' }));
});
test('listener can be a function name', function(assert) {
assert.expect(1);
const service = this.subject();
const context = {
foo() { assert.ok(true); }
};
service.listenFor('x', context, 'foo');
$(document.body).trigger($.Event('keydown', { key: 'x' }));
});
test('the first argument to the callback is the event object', function(assert) {
const service = this.subject();
const event = { key: 'x' };
service.listenFor('x', this, function(e) { assert.equal(e.key, event.key); });
$(document.body).trigger($.Event('keydown', event));
});
test('it can handle an array of static arguments as option', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function(e, n) { assert.equal(n, 42); }, {
arguments: [42]
});
$(document.body).trigger($.Event('keydown', { key: 'x' }));
});
test('it throws when no key is given', function(assert) {
assert.expect(1);
const service = this.subject();
assert.throws(() => {
service.listenFor();
});
});
test('it can handle ctrl modifier', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(false, 'should not run with ctrl pressed'); });
service.listenFor('x', this, function() { assert.ok(true); }, {
requireCtrl: true
});
$(document.body).trigger($.Event('keydown', { key: 'x', ctrlKey: true }));
});
test('it can handle alt key modifier', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(false, 'should not run with alt pressed'); });
service.listenFor('x', this, function() { assert.ok(true); }, {
requireAlt: true
});
$(document.body).trigger($.Event('keydown', { key: 'x', altKey: true }));
});
test('it can handle shift key modifier', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(false, 'should not run with shift pressed'); });
service.listenFor('x', this, function() { assert.ok(true); }, {
requireShift: true
});
$(document.body).trigger($.Event('keydown', { key: 'x', shiftKey: true }));
});
test('it can handle meta key modifier', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(false, 'should not run with meta pressed'); });
service.listenFor('x', this, function() { assert.ok(true); }, {
requireMeta: true
});
$(document.body).trigger($.Event('keydown', { key: 'x', metaKey: true }));
});
// Haven't found a reliable way of spoofing user agent on phantom js.
if (!/PhantomJS/i.test(window.navigator.userAgent)) {
test('it can handle meta as ctrl key modifier on Mac OS X when useCmdOnMac is enabled', function(assert) {
setUserAgent('Mac OS X');
const service = this.subject();
let cmdTriggered = 0;
let ctrlTriggered = 0;
service.listenFor('ctrl+x', this, function() {
ctrlTriggered += 1;
});
service.listenFor('ctrl+x', this, function() {
cmdTriggered += 1;
}, {
useCmdOnMac: true
});
$(document.body).trigger($.Event('keydown', { key: 'x', ctrlKey: true }));
assert.equal(ctrlTriggered, 1, 'ctrl hit on mac - ctrl count is 1');
assert.equal(cmdTriggered, 0, 'ctrl hit on mac - cmd count is 0');
$(document.body).trigger($.Event('keydown', { key: 'x', metaKey: true }));
assert.equal(ctrlTriggered, 1, 'cmd hit on mac - ctrl count is 1');
assert.equal(cmdTriggered, 1, 'cmd hit on mac - cmd count is 1');
setUserAgent('Windows NT');
$(document.body).trigger($.Event('keydown', { key: 'x', ctrlKey: true }));
assert.equal(ctrlTriggered, 2, 'ctrl hit on win - ctrl count is 2');
assert.equal(cmdTriggered, 2, 'ctrl hit on win - cmd count is 2');
$(document.body).trigger($.Event('keydown', { key: 'x', metaKey: true }));
assert.equal(ctrlTriggered, 2, 'cmd hit on win - ctrl count is 2');
assert.equal(cmdTriggered, 2, 'cmd hit on win - cmd count is 2');
resetUserAgent();
});
}
test('it can handle a combination of key modifiers', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('Del', this, function() { assert.ok(false, 'should not run'); });
service.listenFor('Del', this, function() { assert.ok(false, 'should not run'); }, {
requireAlt: true
});
service.listenFor('Del', this, function() { assert.ok(true); }, {
requireCtrl: true,
requireAlt: true
});
$(document.body).trigger($.Event('keydown', { key: 'Del', ctrlKey: true, altKey: true }));
});
test('it can handle a combination shorthand', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(false, 'should not run with ctrl pressed'); });
service.listenFor('ctrl+x', this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { key: 'x', ctrlKey: true }));
});
test('stopListeningFor removes the keyboard handler', function(assert) {
assert.expect(3);
const service = this.subject();
const listener = function() { assert.ok(true); };
service.listenFor('ctrl+x', this, listener);
service.listenFor('ctrl+x', this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { key: 'x', ctrlKey: true }));
service.stopListeningFor('ctrl+x', this, listener);
$(document.body).trigger($.Event('keydown', { key: 'x', ctrlKey: true }));
});
test('listenForOnce only gets called once', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenForOnce('x', this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { key: 'x' }));
$(document.body).trigger($.Event('keydown', { key: 'x' }));
});
test('it falls back to event.keyCode if event.key is not set', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { keyCode: 88 }));
});
test('it can handle the space key', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor(' ', this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { key: ' ' }));
});
test('multiple shortcuts can be specified at once', function(assert) {
assert.expect(2);
const service = this.subject();
service.listenFor(['a', 'b'], this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { key: 'a' }));
$(document.body).trigger($.Event('keydown', { key: 'b' }));
});
test('multiple shortcuts can be specified at once - with modifiers', function(assert) {
assert.expect(2);
const service = this.subject();
service.listenFor(['ctrl+a', 'shift+b'], this, function() { assert.ok(true); });
$(document.body).trigger($.Event('keydown', { key: 'a', ctrlKey: true }));
$(document.body).trigger($.Event('keydown', { key: 'b', ctrlKey: true }));
$(document.body).trigger($.Event('keydown', { key: 'b', shiftKey: true }));
});
test('stopListingFor also support multiple shortcuts', function(assert) {
assert.expect(0);
const service = this.subject();
const callback = function() { assert.ok(false); };
service.listenFor(['ctrl+a', 'shift+b'], this, callback);
service.stopListeningFor(['ctrl+a', 'shift+b'], this, callback);
$(document.body).trigger($.Event('keydown', { key: 'a', ctrlKey: true }));
$(document.body).trigger($.Event('keydown', { key: 'b', shiftKey: true }));
});
test('if event.target is an input ignore by default', function(assert) {
assert.expect(0);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(true); });
const input = document.createElement('input');
const textarea = document.createElement('textarea');
$(document.body).trigger($.Event('keydown', { key: 'x', target: input }));
$(document.body).trigger($.Event('keydown', { key: 'x', target: textarea }));
});
test('if event.target is an input handle if option is set', function(assert) {
assert.expect(2);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(true); }, {
actOnInputElement: true
});
const input = document.createElement('input');
const textarea = document.createElement('textarea');
$(document.body).trigger($.Event('keydown', { key: 'x', target: input }));
$(document.body).trigger($.Event('keydown', { key: 'x', target: textarea }));
});
test('options.debounce wraps callback in run.debounce with specified time', function(assert) {
assert.expect(1);
const done = assert.async();
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(true); done(); }, {
debounce: 1,
});
$(document.body).trigger($.Event('keydown', { key: 'x' }));
$(document.body).trigger($.Event('keydown', { key: 'x' }));
});
test('options.throttle wraps callback in run.throttle with specified time', function(assert) {
assert.expect(2);
const done = assert.async();
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(true); }, {
throttle: 1,
});
$(document.body).trigger($.Event('keydown', { key: 'x' }));
$(document.body).trigger($.Event('keydown', { key: 'x' }));
setTimeout(() => {
$(document.body).trigger($.Event('keydown', { key: 'x' }));
done();
}, 1);
});
test('options.scheduleOnce wraps callback in run.once', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('x', this, function() { assert.ok(true); }, {
scheduleOnce: true,
});
run(() => {
$(document.body).trigger($.Event('keydown', { key: 'x' }));
$(document.body).trigger($.Event('keydown', { key: 'x' }));
$(document.body).trigger($.Event('keydown', { key: 'x' }));
});
});
// regressions
test('listening for "." works', function(assert) {
assert.expect(1);
const service = this.subject();
service.listenFor('.', this, function() { assert.ok(true); });
run(() => {
$(document.body).trigger($.Event('keydown', { key: '.' }));
});
});