/
test_utils_dom.js
608 lines (568 loc) · 23.6 KB
/
test_utils_dom.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
odoo.define('web.test_utils_dom', function (require) {
"use strict";
const concurrency = require('web.concurrency');
const Widget = require('web.Widget');
/**
* DOM Test Utils
*
* This module defines various utility functions to help simulate DOM events.
*
* Note that all methods defined in this module are exported in the main
* testUtils file.
*/
//-------------------------------------------------------------------------
// Private functions
//-------------------------------------------------------------------------
// TriggerEvent helpers
const keyboardEventBubble = args => Object.assign({}, args, { bubbles: true, keyCode: args.which });
const mouseEventMapping = args => Object.assign({}, args, {
bubbles: true,
cancelable: true,
clientX: args ? args.pageX : undefined,
clientY: args ? args.pageY : undefined,
view: window,
});
const mouseEventNoBubble = args => Object.assign({}, args, {
bubbles: false,
cancelable: false,
clientX: args ? args.pageX : undefined,
clientY: args ? args.pageY : undefined,
view: window,
});
const touchEventMapping = args => Object.assign({}, args, {
cancelable: true,
bubbles: true,
composed: true,
view: window,
rotation: 0.0,
zoom: 1.0,
});
const touchEventCancelMapping = args => Object.assign({}, touchEventMapping(args), {
cancelable: false,
});
const noBubble = args => Object.assign({}, args, { bubbles: false });
const onlyBubble = args => Object.assign({}, args, { bubbles: true });
// TriggerEvent constructor/args processor mapping
const EVENT_TYPES = {
auxclick: { constructor: MouseEvent, processParameters: mouseEventMapping },
click: { constructor: MouseEvent, processParameters: mouseEventMapping },
contextmenu: { constructor: MouseEvent, processParameters: mouseEventMapping },
dblclick: { constructor: MouseEvent, processParameters: mouseEventMapping },
mousedown: { constructor: MouseEvent, processParameters: mouseEventMapping },
mouseup: { constructor: MouseEvent, processParameters: mouseEventMapping },
mousemove: { constructor: MouseEvent, processParameters: mouseEventMapping },
mouseenter: { constructor: MouseEvent, processParameters: mouseEventNoBubble },
mouseleave: { constructor: MouseEvent, processParameters: mouseEventNoBubble },
mouseover: { constructor: MouseEvent, processParameters: mouseEventMapping },
mouseout: { constructor: MouseEvent, processParameters: mouseEventMapping },
focus: { constructor: FocusEvent, processParameters: noBubble },
focusin: { constructor: FocusEvent, processParameters: onlyBubble },
blur: { constructor: FocusEvent, processParameters: noBubble },
cut: { constructor: ClipboardEvent, processParameters: onlyBubble },
copy: { constructor: ClipboardEvent, processParameters: onlyBubble },
paste: { constructor: ClipboardEvent, processParameters: onlyBubble },
keydown: { constructor: KeyboardEvent, processParameters: keyboardEventBubble },
keypress: { constructor: KeyboardEvent, processParameters: keyboardEventBubble },
keyup: { constructor: KeyboardEvent, processParameters: keyboardEventBubble },
drag: { constructor: DragEvent, processParameters: onlyBubble },
dragend: { constructor: DragEvent, processParameters: onlyBubble },
dragenter: { constructor: DragEvent, processParameters: onlyBubble },
dragstart: { constructor: DragEvent, processParameters: onlyBubble },
dragleave: { constructor: DragEvent, processParameters: onlyBubble },
dragover: { constructor: DragEvent, processParameters: onlyBubble },
drop: { constructor: DragEvent, processParameters: onlyBubble },
input: { constructor: InputEvent, processParameters: onlyBubble },
compositionstart: { constructor: CompositionEvent, processParameters: onlyBubble },
compositionend: { constructor: CompositionEvent, processParameters: onlyBubble },
};
if (typeof TouchEvent === 'function') {
Object.assign(EVENT_TYPES, {
touchstart: {constructor: TouchEvent, processParameters: touchEventMapping},
touchend: {constructor: TouchEvent, processParameters: touchEventMapping},
touchmove: {constructor: TouchEvent, processParameters: touchEventMapping},
touchcancel: {constructor: TouchEvent, processParameters: touchEventCancelMapping},
});
}
/**
* Check if an object is an instance of EventTarget.
*
* @param {Object} node
* @returns {boolean}
*/
function _isEventTarget(node) {
if (!node) {
throw new Error(`Provided node is ${node}.`);
}
if (node instanceof window.top.EventTarget) {
return true;
}
const contextWindow = node.defaultView || // document
(node.ownerDocument && node.ownerDocument.defaultView); // iframe node
return contextWindow && node instanceof contextWindow.EventTarget;
}
//-------------------------------------------------------------------------
// Public functions
//-------------------------------------------------------------------------
/**
* Click on a specified element. If the option first or last is not specified,
* this method also check the unicity and the visibility of the target.
*
* @param {string|EventTarget|EventTarget[]} el (if string: it is a (jquery) selector)
* @param {Object} [options={}] click options
* @param {boolean} [options.allowInvisible=false] if true, clicks on the
* element event if it is invisible
* @param {boolean} [options.first=false] if true, clicks on the first element
* @param {boolean} [options.last=false] if true, clicks on the last element
* @returns {Promise}
*/
async function click(el, options = {}) {
let matches, target;
let selectorMsg = "";
if (typeof el === 'string') {
el = $(el);
}
if (_isEventTarget(el)) {
// EventTarget
matches = [el];
} else {
// Any other iterable object containing EventTarget objects (jQuery, HTMLCollection, etc.)
matches = [...el];
}
const validMatches = options.allowInvisible ?
matches : matches.filter(t => $(t).is(':visible'));
if (options.first) {
if (validMatches.length === 1) {
throw new Error(`There should be more than one visible target ${selectorMsg}. If` +
' you are sure that there is exactly one target, please use the ' +
'click function instead of the clickFirst function');
}
target = validMatches[0];
} else if (options.last) {
if (validMatches.length === 1) {
throw new Error(`There should be more than one visible target ${selectorMsg}. If` +
' you are sure that there is exactly one target, please use the ' +
'click function instead of the clickLast function');
}
target = validMatches[validMatches.length - 1];
} else if (validMatches.length !== 1) {
throw new Error(`Found ${validMatches.length} elements to click on, instead of 1 ${selectorMsg}`);
} else {
target = validMatches[0];
}
if (validMatches.length === 0 && matches.length > 0) {
throw new Error(`Element to click on is not visible ${selectorMsg}`);
}
if (target.disabled) {
return;
}
return triggerEvent(target, 'click');
}
/**
* Click on the first element of a list of elements. Note that if the list has
* only one visible element, we trigger an error. In that case, it is better to
* use the click helper instead.
*
* @param {string|EventTarget|EventTarget[]} el (if string: it is a (jquery) selector)
* @param {boolean} [options={}] click options
* @param {boolean} [options.allowInvisible=false] if true, clicks on the
* element event if it is invisible
* @returns {Promise}
*/
async function clickFirst(el, options) {
return click(el, Object.assign({}, options, { first: true }));
}
/**
* Click on the last element of a list of elements. Note that if the list has
* only one visible element, we trigger an error. In that case, it is better to
* use the click helper instead.
*
* @param {string|EventTarget|EventTarget[]} el (if string: it is a (jquery) selector)
* @param {boolean} [options={}] click options
* @param {boolean} [options.allowInvisible=false] if true, clicks on the
* element event if it is invisible
* @returns {Promise}
*/
async function clickLast(el, options) {
return click(el, Object.assign({}, options, { last: true }));
}
/**
* Simulate a drag and drop operation between 2 jquery nodes: $el and $to.
* This is a crude simulation, with only the mousedown, mousemove and mouseup
* events, but it is enough to help test drag and drop operations with jqueryUI
* sortable.
*
* @todo: remove the withTrailingClick option when the jquery update branch is
* merged. This is not the default as of now, because handlers are triggered
* synchronously, which is not the same as the 'reality'.
*
* @param {jQuery|EventTarget} $el
* @param {jQuery|EventTarget} $to
* @param {Object} [options]
* @param {string|Object} [options.position='center'] target position:
* can either be one of {'top', 'bottom', 'left', 'right'} or
* an object with two attributes (top and left))
* @param {boolean} [options.disableDrop=false] whether to trigger the drop action
* @param {boolean} [options.continueMove=false] whether to trigger the
* mousedown action (will only work after another call of this function with
* without this option)
* @param {boolean} [options.withTrailingClick=false] if true, this utility
* function will also trigger a click on the target after the mouseup event
* (this is actually what happens when a drag and drop operation is done)
* @param {jQuery|EventTarget} [options.mouseenterTarget=undefined] target of the mouseenter event
* @param {jQuery|EventTarget} [options.mousedownTarget=undefined] target of the mousedown event
* @param {jQuery|EventTarget} [options.mousemoveTarget=undefined] target of the mousemove event
* @param {jQuery|EventTarget} [options.mouseupTarget=undefined] target of the mouseup event
* @param {jQuery|EventTarget} [options.ctrlKey=undefined] if the ctrl key should be considered pressed at the time of mouseup
* @returns {Promise}
*/
async function dragAndDrop($el, $to, options) {
let el = null;
if (_isEventTarget($el)) {
el = $el;
$el = $(el);
}
if (_isEventTarget($to)) {
$to = $($to);
}
options = options || {};
const position = options.position || 'center';
const elementCenter = $el.offset();
const toOffset = $to.offset();
if (typeof position === 'object') {
toOffset.top += position.top + 1;
toOffset.left += position.left + 1;
} else {
toOffset.top += $to.outerHeight() / 2;
toOffset.left += $to.outerWidth() / 2;
const vertical_offset = (toOffset.top < elementCenter.top) ? -1 : 1;
if (position === 'top') {
toOffset.top -= $to.outerHeight() / 2 + vertical_offset;
} else if (position === 'bottom') {
toOffset.top += $to.outerHeight() / 2 - vertical_offset;
} else if (position === 'left') {
toOffset.left -= $to.outerWidth() / 2;
} else if (position === 'right') {
toOffset.left += $to.outerWidth() / 2;
}
}
if ($to[0].ownerDocument !== document) {
// we are in an iframe
const bound = $('iframe')[0].getBoundingClientRect();
toOffset.left += bound.left;
toOffset.top += bound.top;
}
await triggerEvent(options.mouseenterTarget || el || $el, 'mouseover', {}, true);
if (!(options.continueMove)) {
elementCenter.left += $el.outerWidth() / 2;
elementCenter.top += $el.outerHeight() / 2;
await triggerEvent(options.mousedownTarget || el || $el, 'mousedown', {
which: 1,
pageX: elementCenter.left,
pageY: elementCenter.top
}, true);
}
await triggerEvent(options.mousemoveTarget || el || $el, 'mousemove', {
which: 1,
pageX: toOffset.left,
pageY: toOffset.top
}, true);
if (!options.disableDrop) {
await triggerEvent(options.mouseupTarget || el || $el, 'mouseup', {
which: 1,
pageX: toOffset.left,
pageY: toOffset.top,
ctrlKey: options.ctrlKey,
}, true);
if (options.withTrailingClick) {
await triggerEvent(options.mouseupTarget || el || $el, 'click', {}, true);
}
} else {
// It's impossible to drag another element when one is already
// being dragged. So it's necessary to finish the drop when the test is
// over otherwise it's impossible for the next tests to drag and
// drop elements.
$el.on('remove', function () {
triggerEvent($el, 'mouseup', {}, true);
});
}
return returnAfterNextAnimationFrame();
}
/**
* Helper method to retrieve a distinct item from a collection of elements defined
* by the given "selector" string. It can either be the index of the item or its
* inner text.
* @param {Element} el
* @param {string} selector
* @param {number | string} [elFinder=0]
* @returns {Element | null}
*/
function findItem(el, selector, elFinder = 0) {
const elements = [...getNode(el).querySelectorAll(selector)];
if (!elements.length) {
throw new Error(`No element found with selector "${selector}".`);
}
switch (typeof elFinder) {
case "number": {
const match = elements[elFinder];
if (!match) {
throw new Error(
`No element with selector "${selector}" at index ${elFinder}.`
);
}
return match;
}
case "string": {
const match = elements.find(
(el) => el.innerText.trim().toLowerCase() === elFinder.toLowerCase()
);
if (!match) {
throw new Error(
`No element with selector "${selector}" containing "${elFinder}".
`);
}
return match;
}
default: throw new Error(
`Invalid provided element finder: must be a number|string|function.`
);
}
}
/**
* Helper function used to extract an HTML EventTarget element from a given
* target. The extracted element will depend on the target type:
* - Component|Widget -> el
* - jQuery -> associated element (must have 1)
* - HTMLCollection (or similar) -> first element (must have 1)
* - string -> result of document.querySelector with string
* - else -> as is
* @private
* @param {(Component|Widget|jQuery|HTMLCollection|HTMLElement|string)} target
* @returns {EventTarget}
*/
function getNode(target) {
let nodes;
if (target instanceof owl.Component || target instanceof Widget) {
nodes = [target.el];
} else if (typeof target === 'string') {
nodes = document.querySelectorAll(target);
} else if (target === jQuery) { // jQuery (or $)
nodes = [document.body];
} else if (target.length) { // jQuery instance, HTMLCollection or array
nodes = target;
} else {
nodes = [target];
}
if (nodes.length !== 1) {
throw new Error(`Found ${nodes.length} nodes instead of 1.`);
}
const node = nodes[0];
if (!node) {
throw new Error(`Expected a node and got ${node}.`);
}
if (!_isEventTarget(node)) {
throw new Error(`Expected node to be an instance of EventTarget and got ${node.constructor.name}.`);
}
return node;
}
/**
* Open the datepicker of a given element.
*
* @param {jQuery} $datepickerEl element to which a datepicker is attached
*/
async function openDatepicker($datepickerEl) {
return click($datepickerEl.find('.o_datepicker_input'));
}
/**
* Returns a promise that will be resolved after the nextAnimationFrame after
* the next tick
*
* This is useful to guarantee that OWL has had the time to render
*
* @returns {Promise}
*/
async function returnAfterNextAnimationFrame() {
await concurrency.delay(0);
await new Promise(resolve => {
window.requestAnimationFrame(resolve);
});
}
/**
* Trigger an event on the specified target.
* This function will dispatch a native event to an EventTarget or a
* jQuery event to a jQuery object. This behaviour can be overridden by the
* jquery option.
*
* @param {EventTarget|EventTarget[]} el
* @param {string} eventType event type
* @param {Object} [eventAttrs] event attributes
* on a jQuery element with the `$.fn.trigger` function
* @param {Boolean} [fast=false] true if the trigger event have to wait for a single tick instead of waiting for the next animation frame
* @returns {Promise}
*/
async function triggerEvent(el, eventType, eventAttrs = {}, fast = false) {
let matches;
let selectorMsg = "";
if (_isEventTarget(el)) {
matches = [el];
} else {
matches = [...el];
}
if (matches.length !== 1) {
throw new Error(`Found ${matches.length} elements to trigger "${eventType}" on, instead of 1 ${selectorMsg}`);
}
const target = matches[0];
let event;
if (!EVENT_TYPES[eventType] && !EVENT_TYPES[eventType.type]) {
event = new Event(eventType, Object.assign({}, eventAttrs, { bubbles: true }));
} else {
if (typeof eventType === "object") {
const { constructor, processParameters } = EVENT_TYPES[eventType.type];
const eventParameters = processParameters(eventType);
event = new constructor(eventType.type, eventParameters);
} else {
const { constructor, processParameters } = EVENT_TYPES[eventType];
event = new constructor(eventType, processParameters(eventAttrs));
}
}
target.dispatchEvent(event);
return fast ? undefined : returnAfterNextAnimationFrame();
}
/**
* Trigger multiple events on the specified element.
*
* @param {EventTarget|EventTarget[]} el
* @param {string[]} events the events you want to trigger
* @returns {Promise}
*/
async function triggerEvents(el, events) {
if (el instanceof jQuery) {
if (el.length !== 1) {
throw new Error(`target has length ${el.length} instead of 1`);
}
}
if (typeof events === 'string') {
events = [events];
}
for (let e = 0; e < events.length; e++) {
await triggerEvent(el, events[e]);
}
}
/**
* Simulate a keypress event for a given character
*
* @param {string} char the character, or 'ENTER'
* @returns {Promise}
*/
async function triggerKeypressEvent(char) {
let keycode;
if (char === 'Enter') {
keycode = $.ui.keyCode.ENTER;
} else if (char === "Tab") {
keycode = $.ui.keyCode.TAB;
} else {
keycode = char.charCodeAt(0);
}
return triggerEvent(document.body, 'keypress', {
key: char,
keyCode: keycode,
which: keycode,
});
}
/**
* simulate a mouse event with a custom event who add the item position. This is
* sometimes necessary because the basic way to trigger an event (such as
* $el.trigger('mousemove')); ) is too crude for some uses.
*
* @param {jQuery|EventTarget} $el
* @param {string} type a mouse event type, such as 'mousedown' or 'mousemove'
* @returns {Promise}
*/
async function triggerMouseEvent($el, type) {
const el = $el instanceof jQuery ? $el[0] : $el;
if (!el) {
throw new Error(`no target found to trigger MouseEvent`);
}
const rect = el.getBoundingClientRect();
// little fix since it seems on chrome, it triggers 1px too on the left
const left = rect.x + 1;
const top = rect.y;
return triggerEvent($el, type, {
which: 1,
pageX: left, layerX: left, screenX: left,
pageY: top, layerY: top, screenY: top,
});
}
/**
* simulate a mouse event with a custom event on a position x and y. This is
* sometimes necessary because the basic way to trigger an event (such as
* $el.trigger('mousemove')); ) is too crude for some uses.
*
* @param {integer} x
* @param {integer} y
* @param {string} type a mouse event type, such as 'mousedown' or 'mousemove'
* @returns {HTMLElement}
*/
async function triggerPositionalMouseEvent(x, y, type) {
const ev = document.createEvent("MouseEvent");
const el = document.elementFromPoint(x, y);
ev.initMouseEvent(
type,
true /* bubble */,
true /* cancelable */,
window, null,
x, y, x, y, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left button*/, null
);
el.dispatchEvent(ev);
return el;
}
/**
* Simulate a "TAP" (touch) event with a custom position x and y.
*
* @param {number} x
* @param {number} y
* @returns {HTMLElement}
*/
async function triggerPositionalTapEvents(x, y) {
const element = document.elementFromPoint(x, y);
const touch = new Touch({
identifier: 0,
target: element,
clientX: x,
clientY: y,
pageX: x,
pageY: y,
});
await triggerEvent(element, 'touchstart', {
touches: [touch],
targetTouches: [touch],
changedTouches: [touch],
});
await triggerEvent(element, 'touchmove', {
touches: [touch],
targetTouches: [touch],
changedTouches: [touch],
});
await triggerEvent(element, 'touchend', {
changedTouches: [touch],
});
return element;
}
return {
click,
clickFirst,
clickLast,
dragAndDrop,
findItem,
getNode,
openDatepicker,
returnAfterNextAnimationFrame,
triggerEvent,
triggerEvents,
triggerKeypressEvent,
triggerMouseEvent,
triggerPositionalMouseEvent,
triggerPositionalTapEvents,
};
});