From c96e3b96f307685d03d240fac81fb0e83ca85f1d Mon Sep 17 00:00:00 2001 From: Romeo Fragomeli Date: Wed, 20 Jan 2021 09:27:45 +0000 Subject: [PATCH] [IMP] web: add support for TAP events in test utils This commit add the support to simulate a "TAP" (touch) using coordinate. This utils will be used in a future commit in web_enterprise to be able to test some specific case for FullCalendar. Task ID: 1891957 --- addons/web/static/tests/helpers/test_utils.js | 1 + .../static/tests/helpers/test_utils_dom.js | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/addons/web/static/tests/helpers/test_utils.js b/addons/web/static/tests/helpers/test_utils.js index 863a9edc993..1ce6dac94ee 100644 --- a/addons/web/static/tests/helpers/test_utils.js +++ b/addons/web/static/tests/helpers/test_utils.js @@ -197,6 +197,7 @@ odoo.define('web.test_utils', async function (require) { triggerKeypressEvent: testUtilsDom.triggerKeypressEvent, triggerMouseEvent: testUtilsDom.triggerMouseEvent, triggerPositionalMouseEvent: testUtilsDom.triggerPositionalMouseEvent, + triggerPositionalTapEvents: testUtilsDom.triggerPositionalTapEvents, dragAndDrop: testUtilsDom.dragAndDrop, find: testUtilsDom.findItem, getNode: testUtilsDom.getNode, diff --git a/addons/web/static/tests/helpers/test_utils_dom.js b/addons/web/static/tests/helpers/test_utils_dom.js index eedbcbe5155..f85fe9bec8b 100644 --- a/addons/web/static/tests/helpers/test_utils_dom.js +++ b/addons/web/static/tests/helpers/test_utils_dom.js @@ -33,6 +33,17 @@ odoo.define('web.test_utils_dom', function (require) { 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 @@ -70,6 +81,11 @@ odoo.define('web.test_utils_dom', function (require) { dragover: { constructor: DragEvent, processParameters: onlyBubble }, drop: { constructor: DragEvent, processParameters: onlyBubble }, + touchstart: { constructor: TouchEvent, processParameters: touchEventMapping }, + touchend: { constructor: TouchEvent, processParameters: touchEventMapping }, + touchmove: { constructor: TouchEvent, processParameters: touchEventMapping }, + touchcancel: { constructor: TouchEvent, processParameters: touchEventCancelMapping }, + input: { constructor: InputEvent, processParameters: onlyBubble }, compositionstart: { constructor: CompositionEvent, processParameters: onlyBubble }, @@ -533,6 +549,39 @@ odoo.define('web.test_utils_dom', function (require) { 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, @@ -547,5 +596,6 @@ odoo.define('web.test_utils_dom', function (require) { triggerKeypressEvent, triggerMouseEvent, triggerPositionalMouseEvent, + triggerPositionalTapEvents, }; });