forked from PolymerElements/iron-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock-interactions.js
499 lines (460 loc) · 14.5 KB
/
mock-interactions.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
* @license
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
* Google as part of the polymer project is also subject to an additional IP
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
import {Base} from '@polymer/polymer/polymer-legacy.js';
const HAS_NEW_MOUSE = (() => {
let has = false;
try {
has = Boolean(new MouseEvent('x'));
} catch (_) {
}
return has;
})();
const HAS_NEW_TOUCH = (() => {
let has = false;
try {
has = Boolean(new TouchEvent('x'));
} catch (_) {
}
return has;
})();
/**
* Returns the (x,y) coordinates representing the middle of a node.
*
* @param {!Element} node An element.
* @return {{x: number, y:number}}
*/
export function middleOfNode(node) {
const bcr = node.getBoundingClientRect();
return {y: bcr.top + (bcr.height / 2), x: bcr.left + (bcr.width / 2)};
}
/**
* Returns the (x,y) coordinates representing the top left corner of a node.
*
* @param {!Element} node An element.
* @return {{x: number, y:number}}
*/
export function topLeftOfNode(node) {
const bcr = node.getBoundingClientRect();
return {y: bcr.top, x: bcr.left};
}
/**
* Returns a list of Touch objects that correspond to an array of positions
* and a target node. The Touch instances will each have a unique Touch
* identifier.
*
* @param {!Array<{ x: number, y: number }>} xyList A list of (x,y) coordinate
* objects.
* @param {!Element} node A target element node.
* @return {!Array<!Touch>}
*/
export function makeTouches(xyList, node) {
let id = 0;
return xyList.map(function(xy) {
var touchInit =
{identifier: id++, target: node, clientX: xy.x, clientY: xy.y};
return HAS_NEW_TOUCH ? new window.Touch(touchInit) : touchInit;
});
}
/**
* Generates and dispatches a TouchEvent of a given type, at a specified
* position of a target node.
*
* @param {string} type The type of TouchEvent to generate.
* @param {{ x: number, y: number }} xy An (x,y) coordinate for the generated
* TouchEvent.
* @param {!Element} node The target element node for the generated
* TouchEvent to be dispatched on.
* @return {undefined}
*/
export function makeSoloTouchEvent(type, xy, node) {
xy = xy || middleOfNode(node);
const touches = makeTouches([xy], node);
const touchEventInit = {
touches: touches,
targetTouches: touches,
changedTouches: touches
};
let event;
if (HAS_NEW_TOUCH) {
touchEventInit.bubbles = true;
touchEventInit.cancelable = true;
event = new TouchEvent(type, touchEventInit);
} else {
event = new CustomEvent(type, {
bubbles: true,
cancelable: true,
// Allow event to go outside a ShadowRoot.
composed: true
});
for (const property in touchEventInit) {
event[property] = touchEventInit[property];
}
}
node.dispatchEvent(event);
}
/**
* Fires a mouse event on a specific node, at a given set of coordinates.
* This event bubbles and is cancellable.
*
* @param {string} type The type of mouse event (such as 'tap' or 'down').
* @param {{ x: number, y: number }} xy The (x,y) coordinates the mouse event
* should be fired from.
* @param {!Element} node The node to fire the event on.
* @return {undefined}
*/
export function makeMouseEvent(type, xy, node) {
const props = {
bubbles: true,
cancelable: true,
clientX: xy.x,
clientY: xy.y,
// Allow event to go outside a ShadowRoot.
composed: true,
// Make this a primary input.
buttons:
1 // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
};
let e;
if (HAS_NEW_MOUSE) {
e = new MouseEvent(type, props);
} else {
e = document.createEvent('MouseEvent');
e.initMouseEvent(
type,
props.bubbles,
props.cancelable,
null, /* view */
null, /* detail */
0, /* screenX */
0, /* screenY */
props.clientX,
props.clientY,
false, /*ctrlKey */
false, /*altKey */
false, /*shiftKey */
false, /*metaKey */
0, /*button */
null /*relatedTarget*/);
}
node.dispatchEvent(e);
}
/**
* Simulates a mouse move action by firing a `move` mouse event on a
* specific node, between a set of coordinates.
*
* @param {!Element} node The node to fire the event on.
* @param {Object} fromXY The (x,y) coordinates the dragging should start from.
* @param {Object} toXY The (x,y) coordinates the dragging should end at.
* @param {?number=} steps Optional. The numbers of steps in the move motion.
* If not specified, the default is 5.
* @return {undefined}
*/
export function move(node, fromXY, toXY, steps) {
steps = steps || 5;
var dx = Math.round((fromXY.x - toXY.x) / steps);
var dy = Math.round((fromXY.y - toXY.y) / steps);
var xy = {x: fromXY.x, y: fromXY.y};
for (var i = steps; i > 0; i--) {
makeMouseEvent('mousemove', xy, node);
xy.x += dx;
xy.y += dy;
}
makeMouseEvent('mousemove', {x: toXY.x, y: toXY.y}, node);
}
/**
* Simulates a mouse dragging action originating in the middle of a specific
* node.
*
* @param {!Element} target The node to fire the event on.
* @param {?number} dx The horizontal displacement.
* @param {?number} dy The vertical displacement
* @param {?number=} steps Optional. The numbers of steps in the dragging
* motion. If not specified, the default is 5.
*/
export function track(target, dx, dy, steps) {
dx = dx | 0;
dy = dy | 0;
steps = steps || 5;
down(target);
var xy = middleOfNode(target);
var xy2 = {x: xy.x + dx, y: xy.y + dy};
move(target, xy, xy2, steps);
up(target, xy2);
}
/**
* Fires a `down` mouse event on a specific node, at a given set of coordinates.
* This event bubbles and is cancellable. If the (x,y) coordinates are
* not specified, the middle of the node will be used instead.
*
* @param {!Element} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the
* mouse event should be fired from.
* @return {undefined}
*/
export function down(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('mousedown', xy, node);
}
/**
* Fires an `up` mouse event on a specific node, at a given set of coordinates.
* This event bubbles and is cancellable. If the (x,y) coordinates are
* not specified, the middle of the node will be used instead.
*
* @param {!Element} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the
* mouse event should be fired from.
*/
export function up(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('mouseup', xy, node);
}
/**
* Generate a click event on a given node, optionally at a given coordinate.
* @param {!Element} node The node to fire the click event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the
* mouse event should be fired from.
* @return {undefined}
*/
export function click(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('click', xy, node);
}
/**
* Generate a touchstart event on a given node, optionally at a given
* coordinate.
* @param {!Element} node The node to fire the click event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the
* touch event should be fired from.
* @return {undefined}
*/
export function touchstart(node, xy) {
xy = xy || middleOfNode(node);
makeSoloTouchEvent('touchstart', xy, node);
}
/**
* Generate a touchend event on a given node, optionally at a given coordinate.
* @param {!Element} node The node to fire the click event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the
* touch event should be fired from.
* @return {undefined}
*/
export function touchend(node, xy) {
xy = xy || middleOfNode(node);
makeSoloTouchEvent('touchend', xy, node);
}
/**
* Simulates a complete mouse click by firing a `down` mouse event, followed
* by an asynchronous `up` and `tap` events on a specific node. Calls the
*`callback` after the `tap` event is fired.
*
* @param {!Element} target The node to fire the event on.
* @param {?Function=} callback Optional. The function to be called after the
*action ends.
* @param {?{
* emulateTouch: boolean
* }=} options Optional. Configure the emulation fidelity of the mouse events.
* @return {undefined}
*/
export function downAndUp(target, callback, options) {
if (options && options.emulateTouch) {
touchstart(target);
touchend(target);
}
down(target);
Base.async(function() {
up(target);
click(target);
callback && callback();
});
}
/**
* Fires a 'tap' mouse event on a specific node. This respects the
* pointer-events set on the node, and will not fire on disabled nodes.
*
* @param {!Element} node The node to fire the event on.
* @param {?{
* emulateTouch: boolean
* }=} options Optional. Configure the emulation fidelity of the mouse event.
* @return {undefined}
*/
export function tap(node, options) {
// Respect nodes that are disabled in the UI.
if (window.getComputedStyle(node)['pointer-events'] === 'none') {
return;
}
const xy = middleOfNode(node);
if (options && options.emulateTouch) {
touchstart(node, xy);
touchend(node, xy);
}
down(node, xy);
up(node, xy);
click(node, xy);
}
/**
* Focuses a node by firing a `focus` event. This event does not bubble.
*
* @param {!Element} target The node to fire the event on.
*/
export function focus(target) {
Base.fire('focus', {}, {bubbles: false, node: target});
}
/**
* Blurs a node by firing a `blur` event. This event does not bubble.
*
* @param {!Element} target The node to fire the event on.
* @return {undefined}
*/
export function blur(target) {
Base.fire('blur', {}, {bubbles: false, node: target});
}
/**
* Returns a keyboard event. This event bubbles and is cancellable.
*
* @param {string} type The type of keyboard event (such as 'keyup' or
* 'keydown').
* @param {number} keyCode The keyCode for the event.
* @param {(string|Array<string>)=} modifiers The key modifiers for the event.
* Accepted values are shift, ctrl, alt, meta.
* @param {string=} key The KeyboardEvent.key value for the event.
* @return {!Event}
*/
export function keyboardEventFor(type, keyCode, modifiers, key) {
const event = new CustomEvent(type, {
detail: 0,
bubbles: true,
cancelable: true,
// Allow event to go outside a ShadowRoot.
composed: true
});
event.keyCode = keyCode;
event.code = keyCode;
modifiers = modifiers || [];
if (typeof modifiers === 'string') {
modifiers = [modifiers];
}
event.shiftKey = modifiers.indexOf('shift') !== -1;
event.altKey = modifiers.indexOf('alt') !== -1;
event.ctrlKey = modifiers.indexOf('ctrl') !== -1;
event.metaKey = modifiers.indexOf('meta') !== -1;
event.key = key;
return event;
}
/**
* Fires a keyboard event on a specific node. This event bubbles and is
* cancellable.
*
* @param {!Element} target The node to fire the event on.
* @param {string} type The type of keyboard event (such as 'keyup' or
* 'keydown').
* @param {number} keyCode The keyCode for the event.
* @param {(string|Array<string>)=} modifiers The key modifiers for the event.
* Accepted values are shift, ctrl, alt, meta.
* @param {string=} key The KeyboardEvent.key value for the event.
* @return {undefined}
*/
export function keyEventOn(target, type, keyCode, modifiers, key) {
target.dispatchEvent(keyboardEventFor(type, keyCode, modifiers, key));
}
/**
* Fires a 'keydown' event on a specific node. This event bubbles and is
* cancellable.
*
* @param {!Element} target The node to fire the event on.
* @param {number} keyCode The keyCode for the event.
* @param {(string|Array<string>)=} modifiers The key modifiers for the event.
* Accepted values are shift, ctrl, alt, meta.
* @param {string=} key The KeyboardEvent.key value for the event.
* @return {undefined}
*/
export function keyDownOn(target, keyCode, modifiers, key) {
keyEventOn(target, 'keydown', keyCode, modifiers, key);
}
/**
* Fires a 'keyup' event on a specific node. This event bubbles and is
* cancellable.
*
* @param {!Element} target The node to fire the event on.
* @param {number} keyCode The keyCode for the event.
* @param {(string|Array<string>)=} modifiers The key modifiers for the event.
* Accepted values are shift, ctrl, alt, meta.
* @param {string=} key The KeyboardEvent.key value for the event.
* @return {undefined}
*/
export function keyUpOn(target, keyCode, modifiers, key) {
keyEventOn(target, 'keyup', keyCode, modifiers, key);
}
/**
* Simulates a complete key press by firing a `keydown` keyboard event, followed
* by an asynchronous `keyup` event on a specific node.
*
* @param {!Element} target The node to fire the event on.
* @param {number} keyCode The keyCode for the event.
* @param {(string|Array<string>)=} modifiers The key modifiers for the event.
* Accepted values are shift, ctrl, alt, meta.
* @param {string=} key The KeyboardEvent.key value for the event.
* @return {undefined}
*/
export function pressAndReleaseKeyOn(target, keyCode, modifiers, key) {
keyDownOn(target, keyCode, modifiers, key);
Base.async(function() {
keyUpOn(target, keyCode, modifiers, key);
}, 1);
}
/**
* Simulates a complete 'enter' key press by firing a `keydown` keyboard event,
* followed by an asynchronous `keyup` event on a specific node.
*
* @param {!Element} target The node to fire the event on.
* @return {undefined}
*/
export function pressEnter(target) {
pressAndReleaseKeyOn(target, 13);
}
/**
* Simulates a complete 'space' key press by firing a `keydown` keyboard event,
* followed by an asynchronous `keyup` event on a specific node.
*
* @param {!Element} target The node to fire the event on.
*/
export function pressSpace(target) {
pressAndReleaseKeyOn(target, 32);
}
/**
* This global is provided for backwards compatibility and will be removed in
* the next major version. All users should migrate to importing functions
* directly from this module instead of accessing them via the global.
*/
window.MockInteractions = {
middleOfNode,
topLeftOfNode,
makeTouches,
makeSoloTouchEvent,
makeMouseEvent,
move,
track,
down,
up,
click,
touchstart,
touchend,
downAndUp,
tap,
focus,
blur,
keyboardEventFor,
keyEventOn,
keyDownOn,
keyUpOn,
pressAndReleaseKeyOn,
pressEnter,
pressSpace,
};