|
| 1 | +// End-to-end tests for zoom and pan interactions |
| 2 | +import { test, expect } from "./fixtures"; |
| 3 | +import { wait_for_ulabel_init } from "../testing-utils/init_utils"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Returns the current zoom_val from the ULabel instance. |
| 7 | + * @param {import('@playwright/test').Page} page |
| 8 | + */ |
| 9 | +async function get_zoom_val(page) { |
| 10 | + return await page.evaluate(() => window.ulabel.state.zoom_val); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns the current annbox scroll position {left, top}. |
| 15 | + * @param {import('@playwright/test').Page} page |
| 16 | + */ |
| 17 | +async function get_annbox_scroll(page) { |
| 18 | + return await page.evaluate(() => { |
| 19 | + const annbox = document.getElementById(window.ulabel.config.annbox_id); |
| 20 | + return { left: annbox.scrollLeft, top: annbox.scrollTop }; |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Returns the canvas element id for the current subtask. |
| 26 | + * @param {import('@playwright/test').Page} page |
| 27 | + */ |
| 28 | +async function get_canvas_fid(page) { |
| 29 | + return await page.evaluate(() => window.ulabel.get_current_subtask().canvas_fid); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Performs a click-drag from start to end with the given mouse button and |
| 34 | + * optional modifier keys. |
| 35 | + * @param {import('@playwright/test').Page} page |
| 36 | + * @param {{x: number, y: number}} start |
| 37 | + * @param {{x: number, y: number}} end |
| 38 | + * @param {{button?: "left"|"middle"|"right", modifiers?: string[], steps?: number}} opts |
| 39 | + */ |
| 40 | +async function click_drag(page, start, end, opts = {}) { |
| 41 | + const button = opts.button ?? "left"; |
| 42 | + const modifiers = opts.modifiers ?? []; |
| 43 | + const steps = opts.steps ?? 10; |
| 44 | + |
| 45 | + for (const mod of modifiers) { |
| 46 | + await page.keyboard.down(mod); |
| 47 | + } |
| 48 | + await page.mouse.move(start.x, start.y); |
| 49 | + await page.mouse.down({ button }); |
| 50 | + await page.mouse.move(end.x, end.y, { steps }); |
| 51 | + await page.mouse.up({ button }); |
| 52 | + for (const mod of modifiers) { |
| 53 | + await page.keyboard.up(mod); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +test.describe("Zoom and Pan Interactions", () => { |
| 58 | + test("mouse wheel up zooms in", async ({ page }) => { |
| 59 | + await wait_for_ulabel_init(page); |
| 60 | + |
| 61 | + const initial_zoom = await get_zoom_val(page); |
| 62 | + |
| 63 | + // Move into the annbox before scrolling |
| 64 | + await page.mouse.move(400, 400); |
| 65 | + await page.mouse.wheel(0, -100); // negative deltaY = zoom in |
| 66 | + await page.waitForTimeout(50); |
| 67 | + |
| 68 | + const new_zoom = await get_zoom_val(page); |
| 69 | + expect(new_zoom).toBeGreaterThan(initial_zoom); |
| 70 | + }); |
| 71 | + |
| 72 | + test("mouse wheel down zooms out", async ({ page }) => { |
| 73 | + await wait_for_ulabel_init(page); |
| 74 | + |
| 75 | + // Zoom in first so we have room to zoom out |
| 76 | + await page.mouse.move(400, 400); |
| 77 | + await page.mouse.wheel(0, -200); |
| 78 | + await page.waitForTimeout(50); |
| 79 | + |
| 80 | + const before_out = await get_zoom_val(page); |
| 81 | + |
| 82 | + await page.mouse.wheel(0, 100); // positive deltaY = zoom out |
| 83 | + await page.waitForTimeout(50); |
| 84 | + |
| 85 | + const after_out = await get_zoom_val(page); |
| 86 | + expect(after_out).toBeLessThan(before_out); |
| 87 | + }); |
| 88 | + |
| 89 | + test("middle-click drag pans the annbox", async ({ page }) => { |
| 90 | + await wait_for_ulabel_init(page); |
| 91 | + |
| 92 | + // Zoom in so the image is larger than the viewport and can be panned |
| 93 | + await page.mouse.move(400, 400); |
| 94 | + await page.mouse.wheel(0, -300); |
| 95 | + await page.waitForTimeout(50); |
| 96 | + |
| 97 | + const before = await get_annbox_scroll(page); |
| 98 | + |
| 99 | + // Drag from (500, 400) to (300, 250) — should pan the view |
| 100 | + await click_drag(page, { x: 500, y: 400 }, { x: 300, y: 250 }, { button: "middle" }); |
| 101 | + await page.waitForTimeout(50); |
| 102 | + |
| 103 | + const after = await get_annbox_scroll(page); |
| 104 | + // Pan should change at least one of the scroll positions |
| 105 | + expect(after.left !== before.left || after.top !== before.top).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + test("ctrl+left-click drag on canvas pans the annbox", async ({ page }) => { |
| 109 | + await wait_for_ulabel_init(page); |
| 110 | + |
| 111 | + // Zoom in so we can pan |
| 112 | + await page.mouse.move(400, 400); |
| 113 | + await page.mouse.wheel(0, -300); |
| 114 | + await page.waitForTimeout(50); |
| 115 | + |
| 116 | + const canvas_fid = await get_canvas_fid(page); |
| 117 | + const canvas = page.locator(`#${canvas_fid}`); |
| 118 | + const box = await canvas.boundingBox(); |
| 119 | + expect(box).not.toBeNull(); |
| 120 | + |
| 121 | + const start = { x: box.x + box.width / 2, y: box.y + box.height / 2 }; |
| 122 | + const end = { x: start.x - 80, y: start.y - 60 }; |
| 123 | + |
| 124 | + const before = await get_annbox_scroll(page); |
| 125 | + await click_drag(page, start, end, { button: "left", modifiers: ["Control"] }); |
| 126 | + await page.waitForTimeout(50); |
| 127 | + const after = await get_annbox_scroll(page); |
| 128 | + |
| 129 | + expect(after.left !== before.left || after.top !== before.top).toBe(true); |
| 130 | + }); |
| 131 | + |
| 132 | + test("shift+left-click drag on canvas zooms", async ({ page }) => { |
| 133 | + await wait_for_ulabel_init(page); |
| 134 | + |
| 135 | + const canvas_fid = await get_canvas_fid(page); |
| 136 | + const canvas = page.locator(`#${canvas_fid}`); |
| 137 | + const box = await canvas.boundingBox(); |
| 138 | + expect(box).not.toBeNull(); |
| 139 | + |
| 140 | + const before_zoom = await get_zoom_val(page); |
| 141 | + |
| 142 | + // Drag upward — drag_rezoom raises zoom when mouse moves up |
| 143 | + const start = { x: box.x + box.width / 2, y: box.y + box.height / 2 }; |
| 144 | + const end = { x: start.x, y: start.y - 150 }; |
| 145 | + |
| 146 | + await click_drag(page, start, end, { button: "left", modifiers: ["Shift"] }); |
| 147 | + await page.waitForTimeout(50); |
| 148 | + |
| 149 | + const after_zoom = await get_zoom_val(page); |
| 150 | + expect(after_zoom).toBeGreaterThan(before_zoom); |
| 151 | + }); |
| 152 | + |
| 153 | + test("shift+left-click drag still zooms when subtask is vanished", async ({ page }) => { |
| 154 | + await wait_for_ulabel_init(page); |
| 155 | + |
| 156 | + // Vanish the current subtask |
| 157 | + await page.evaluate(() => { |
| 158 | + window.ulabel.get_current_subtask().state.is_vanished = true; |
| 159 | + }); |
| 160 | + |
| 161 | + const canvas_fid = await get_canvas_fid(page); |
| 162 | + const canvas = page.locator(`#${canvas_fid}`); |
| 163 | + const box = await canvas.boundingBox(); |
| 164 | + expect(box).not.toBeNull(); |
| 165 | + |
| 166 | + const before_zoom = await get_zoom_val(page); |
| 167 | + |
| 168 | + const start = { x: box.x + box.width / 2, y: box.y + box.height / 2 }; |
| 169 | + const end = { x: start.x, y: start.y - 150 }; |
| 170 | + |
| 171 | + await click_drag(page, start, end, { button: "left", modifiers: ["Shift"] }); |
| 172 | + await page.waitForTimeout(50); |
| 173 | + |
| 174 | + const after_zoom = await get_zoom_val(page); |
| 175 | + expect(after_zoom).toBeGreaterThan(before_zoom); |
| 176 | + }); |
| 177 | + |
| 178 | + test("middle-click drag still pans when subtask is vanished and does not start an annotation drag", async ({ page }) => { |
| 179 | + await wait_for_ulabel_init(page); |
| 180 | + |
| 181 | + // Zoom in so the image overflows the viewport (otherwise scroll positions |
| 182 | + // cannot change anyway, which would make the assertion meaningless). |
| 183 | + await page.mouse.move(400, 400); |
| 184 | + await page.mouse.wheel(0, -300); |
| 185 | + await page.waitForTimeout(50); |
| 186 | + |
| 187 | + // Vanish the current subtask |
| 188 | + await page.evaluate(() => { |
| 189 | + window.ulabel.get_current_subtask().state.is_vanished = true; |
| 190 | + }); |
| 191 | + |
| 192 | + // Record annotation count and observe drag_state.active_key during the drag |
| 193 | + // by patching start_drag — this lets us catch any annotation drag that may |
| 194 | + // start before mouseup clears active_key back to null. |
| 195 | + const initial_annotation_count = await page.evaluate(() => { |
| 196 | + const st = window.ulabel.get_current_subtask(); |
| 197 | + return Object.keys(st.annotations.access).length; |
| 198 | + }); |
| 199 | + await page.evaluate(() => { |
| 200 | + window.__observed_drag_keys = []; |
| 201 | + const ul = window.ulabel; |
| 202 | + const original_start_drag = ul.start_drag.bind(ul); |
| 203 | + ul.start_drag = function (drag_key, mouse_button, mouse_event) { |
| 204 | + window.__observed_drag_keys.push(drag_key); |
| 205 | + return original_start_drag(drag_key, mouse_button, mouse_event); |
| 206 | + }; |
| 207 | + }); |
| 208 | + |
| 209 | + const before = await get_annbox_scroll(page); |
| 210 | + await click_drag(page, { x: 500, y: 400 }, { x: 300, y: 250 }, { button: "middle" }); |
| 211 | + await page.waitForTimeout(50); |
| 212 | + const after = await get_annbox_scroll(page); |
| 213 | + |
| 214 | + // Pan must have moved the annbox scroll position |
| 215 | + expect(after.left !== before.left || after.top !== before.top).toBe(true); |
| 216 | + |
| 217 | + // Only a "pan" drag should have started — no annotation drag |
| 218 | + const observed = await page.evaluate(() => window.__observed_drag_keys); |
| 219 | + expect(observed).toEqual(["pan"]); |
| 220 | + |
| 221 | + // No annotation should have been created |
| 222 | + const final_annotation_count = await page.evaluate(() => { |
| 223 | + const st = window.ulabel.get_current_subtask(); |
| 224 | + return Object.keys(st.annotations.access).length; |
| 225 | + }); |
| 226 | + expect(final_annotation_count).toBe(initial_annotation_count); |
| 227 | + }); |
| 228 | + |
| 229 | + test("annotation drag is blocked when subtask is vanished", async ({ page }) => { |
| 230 | + await wait_for_ulabel_init(page); |
| 231 | + |
| 232 | + // Ensure bbox mode |
| 233 | + await page.click("a#md-btn--bbox"); |
| 234 | + await page.waitForTimeout(50); |
| 235 | + |
| 236 | + // Vanish the current subtask |
| 237 | + await page.evaluate(() => { |
| 238 | + window.ulabel.get_current_subtask().state.is_vanished = true; |
| 239 | + }); |
| 240 | + |
| 241 | + const initial_count = await page.evaluate(() => { |
| 242 | + const st = window.ulabel.get_current_subtask(); |
| 243 | + return Object.keys(st.annotations.access).length; |
| 244 | + }); |
| 245 | + |
| 246 | + // Attempt to draw a bbox via plain left-drag on the canvas |
| 247 | + const canvas_fid = await get_canvas_fid(page); |
| 248 | + const canvas = page.locator(`#${canvas_fid}`); |
| 249 | + const box = await canvas.boundingBox(); |
| 250 | + const start = { x: box.x + box.width / 2 - 60, y: box.y + box.height / 2 - 40 }; |
| 251 | + const end = { x: start.x + 120, y: start.y + 80 }; |
| 252 | + await click_drag(page, start, end, { button: "left" }); |
| 253 | + await page.waitForTimeout(100); |
| 254 | + |
| 255 | + // No annotation should have been created — annotation drags are blocked while vanished |
| 256 | + const final_count = await page.evaluate(() => { |
| 257 | + const st = window.ulabel.get_current_subtask(); |
| 258 | + return Object.keys(st.annotations.access).length; |
| 259 | + }); |
| 260 | + expect(final_count).toBe(initial_count); |
| 261 | + |
| 262 | + // drag_state.active_key should still be null |
| 263 | + const active_key = await page.evaluate(() => window.ulabel.drag_state.active_key); |
| 264 | + expect(active_key).toBeNull(); |
| 265 | + }); |
| 266 | +}); |
0 commit comments