|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU Affero General Public License as published by the |
| 8 | + * Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 11 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Shared drag helpers for integ tests. |
| 21 | + * |
| 22 | + * The test harness loads the app inside a child window (testWindow). Production |
| 23 | + * code — most importantly `utils/Resizer` — attaches `mousemove` / `mouseup` |
| 24 | + * listeners to that window's document via jQuery, and commits its work in |
| 25 | + * `requestAnimationFrame` callbacks. To exercise those paths we must: |
| 26 | + * |
| 27 | + * - construct MouseEvents with `view: testWindow` so `event.view` is set and |
| 28 | + * jQuery's normalization picks up the correct document, |
| 29 | + * - dispatch `mousedown` against the drag handle itself (Resizer only arms |
| 30 | + * its pointer-tracking after seeing the mousedown), |
| 31 | + * - dispatch `mousemove` / `mouseup` against `testWindow.document`, |
| 32 | + * - yield for at least one `requestAnimationFrame` tick between moves so |
| 33 | + * `doRedraw` can run. |
| 34 | + * |
| 35 | + * Works under the harness's modern browser engines (the Chrome-based Tauri |
| 36 | + * shell, Firefox, Safari, and the standalone Electron test runner). The |
| 37 | + * MouseEvent constructor is the only spec-blessed path that sets coordinates |
| 38 | + * on bubbled events across those engines — `document.createEvent("MouseEvents")` |
| 39 | + * + `initMouseEvent` is deprecated and drops clientX/clientY in some Safari |
| 40 | + * versions, so we deliberately avoid it. |
| 41 | + */ |
| 42 | +define(function (require, exports, module) { |
| 43 | + |
| 44 | + function _awaitFrames(win, n) { |
| 45 | + return new Promise(function (resolve) { |
| 46 | + let remaining = n; |
| 47 | + function tick() { |
| 48 | + remaining -= 1; |
| 49 | + if (remaining <= 0) { |
| 50 | + resolve(); |
| 51 | + return; |
| 52 | + } |
| 53 | + win.requestAnimationFrame(tick); |
| 54 | + } |
| 55 | + win.requestAnimationFrame(tick); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + function _fireMouse(target, type, x, y, win, buttons) { |
| 60 | + const ev = new win.MouseEvent(type, { |
| 61 | + bubbles: true, |
| 62 | + cancelable: true, |
| 63 | + view: win, |
| 64 | + clientX: x, |
| 65 | + clientY: y, |
| 66 | + button: 0, |
| 67 | + buttons: buttons |
| 68 | + }); |
| 69 | + target.dispatchEvent(ev); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Simulate a user drag from the center of `startEl` to (endX, endY) inside |
| 74 | + * `testWindow`. Emits one mousedown on the handle, `steps` mousemoves along |
| 75 | + * the straight line to the destination, and a final mouseup, yielding to |
| 76 | + * rAF between moves so Resizer's doRedraw commits each increment. |
| 77 | + * |
| 78 | + * @param {DOMElement|jQuery} startEl The element the drag starts on |
| 79 | + * (typically the resizer handle). |
| 80 | + * @param {number} endX Final clientX (in testWindow viewport). |
| 81 | + * @param {number} endY Final clientY (in testWindow viewport). |
| 82 | + * @param {Window} testWindow The child window that owns the DOM. |
| 83 | + * @param {?number} steps Number of intermediate mousemoves |
| 84 | + * (default 8). More steps = smoother drag, more rAF waits. |
| 85 | + */ |
| 86 | + async function dragFromElement(startEl, endX, endY, testWindow, steps) { |
| 87 | + const el = startEl.jquery ? startEl[0] : startEl; |
| 88 | + const rect = el.getBoundingClientRect(); |
| 89 | + const startX = rect.left + rect.width / 2; |
| 90 | + const startY = rect.top + rect.height / 2; |
| 91 | + const doc = testWindow.document; |
| 92 | + const moveSteps = (typeof steps === "number" && steps > 0) ? steps : 8; |
| 93 | + |
| 94 | + _fireMouse(el, "mousedown", startX, startY, testWindow, 1); |
| 95 | + await _awaitFrames(testWindow, 1); |
| 96 | + |
| 97 | + for (let i = 1; i <= moveSteps; i++) { |
| 98 | + const t = i / moveSteps; |
| 99 | + const x = startX + (endX - startX) * t; |
| 100 | + const y = startY + (endY - startY) * t; |
| 101 | + _fireMouse(doc, "mousemove", x, y, testWindow, 1); |
| 102 | + await _awaitFrames(testWindow, 1); |
| 103 | + } |
| 104 | + |
| 105 | + _fireMouse(doc, "mouseup", endX, endY, testWindow, 0); |
| 106 | + await _awaitFrames(testWindow, 2); |
| 107 | + } |
| 108 | + |
| 109 | + exports.dragFromElement = dragFromElement; |
| 110 | +}); |
0 commit comments