|
| 1 | +// Control UI E2E tests cover pointer activation near the mobile safe area. |
| 2 | +import { chromium, type Locator, type Page } from "playwright"; |
| 3 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 4 | +import { |
| 5 | + canRunPlaywrightChromium, |
| 6 | + installMockGateway, |
| 7 | + resolvePlaywrightChromiumExecutablePath, |
| 8 | + startControlUiE2eServer, |
| 9 | + type ControlUiE2eServer, |
| 10 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 11 | + |
| 12 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 13 | +const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath); |
| 14 | +const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1"; |
| 15 | +const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip; |
| 16 | + |
| 17 | +type PointerTraceEntry = { |
| 18 | + activeTag: string; |
| 19 | + bottom: number; |
| 20 | + centerHitLabel: string | null; |
| 21 | + clientX: number; |
| 22 | + clientY: number; |
| 23 | + defaultPrevented: boolean; |
| 24 | + eventTargetLabel: string | null; |
| 25 | + top: number; |
| 26 | + type: string; |
| 27 | +}; |
| 28 | + |
| 29 | +let server: ControlUiE2eServer; |
| 30 | + |
| 31 | +async function installPointerTrace(page: Page, button: Locator): Promise<void> { |
| 32 | + await button.evaluate((element) => { |
| 33 | + const target = element as HTMLButtonElement; |
| 34 | + const trace: PointerTraceEntry[] = []; |
| 35 | + const traceWindow = window as Window & { openclawPointerTrace?: PointerTraceEntry[] }; |
| 36 | + traceWindow.openclawPointerTrace = trace; |
| 37 | + const record = (event: Event) => { |
| 38 | + const pointerEvent = event as PointerEvent; |
| 39 | + const bounds = target.getBoundingClientRect(); |
| 40 | + const hit = document.elementFromPoint(pointerEvent.clientX, pointerEvent.clientY); |
| 41 | + trace.push({ |
| 42 | + activeTag: document.activeElement?.tagName ?? "", |
| 43 | + bottom: bounds.bottom, |
| 44 | + clientX: pointerEvent.clientX, |
| 45 | + clientY: pointerEvent.clientY, |
| 46 | + centerHitLabel: hit?.closest("button")?.getAttribute("aria-label") ?? null, |
| 47 | + defaultPrevented: event.defaultPrevented, |
| 48 | + eventTargetLabel: |
| 49 | + event.target instanceof Element |
| 50 | + ? (event.target.closest("button")?.getAttribute("aria-label") ?? null) |
| 51 | + : null, |
| 52 | + top: bounds.top, |
| 53 | + type: event.type, |
| 54 | + }); |
| 55 | + }; |
| 56 | + for (const type of ["pointerdown", "pointerup", "click"]) { |
| 57 | + target.addEventListener(type, record); |
| 58 | + } |
| 59 | + // Android dismisses the software-keyboard focus as an action button is |
| 60 | + // activated. Model that transition explicitly because desktop Chromium |
| 61 | + // normally keeps a textarea focused for a synthetic touch tap. |
| 62 | + target.addEventListener( |
| 63 | + "pointerdown", |
| 64 | + (event) => { |
| 65 | + if ((event as PointerEvent).pointerType === "touch" && !event.defaultPrevented) { |
| 66 | + document.querySelector<HTMLTextAreaElement>(".agent-chat__input textarea")?.blur(); |
| 67 | + } |
| 68 | + }, |
| 69 | + { once: true }, |
| 70 | + ); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +async function readPointerTrace(page: Page): Promise<PointerTraceEntry[]> { |
| 75 | + return page.evaluate( |
| 76 | + () => |
| 77 | + (window as Window & { openclawPointerTrace?: PointerTraceEntry[] }).openclawPointerTrace ?? |
| 78 | + [], |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +function expectStablePointerActivation(trace: PointerTraceEntry[]): void { |
| 83 | + const pointerDown = trace.find((entry) => entry.type === "pointerdown"); |
| 84 | + const pointerUp = trace.find((entry) => entry.type === "pointerup"); |
| 85 | + expect(trace, `pointer trace: ${JSON.stringify(trace)}`).toContainEqual( |
| 86 | + expect.objectContaining({ type: "click" }), |
| 87 | + ); |
| 88 | + expect(pointerDown, `pointer trace: ${JSON.stringify(trace)}`).toBeDefined(); |
| 89 | + expect(pointerUp, `pointer trace: ${JSON.stringify(trace)}`).toBeDefined(); |
| 90 | + if (!pointerDown || !pointerUp) { |
| 91 | + return; |
| 92 | + } |
| 93 | + expect( |
| 94 | + Math.abs(pointerUp.top - pointerDown.top), |
| 95 | + `pointer trace: ${JSON.stringify(trace)}`, |
| 96 | + ).toBeLessThanOrEqual(1.5); |
| 97 | + expect(pointerUp.centerHitLabel).toBe(pointerDown.centerHitLabel); |
| 98 | + expect(pointerUp.eventTargetLabel).toBe(pointerDown.eventTargetLabel); |
| 99 | +} |
| 100 | + |
| 101 | +async function clickMouseAtCurrentCenter(page: Page, button: Locator): Promise<void> { |
| 102 | + const bounds = await button.boundingBox(); |
| 103 | + expect(bounds).not.toBeNull(); |
| 104 | + if (!bounds) { |
| 105 | + return; |
| 106 | + } |
| 107 | + const x = bounds.x + bounds.width / 2; |
| 108 | + const y = bounds.y + bounds.height / 2; |
| 109 | + await page.mouse.move(x, y); |
| 110 | + await page.mouse.down(); |
| 111 | + await page.mouse.up(); |
| 112 | +} |
| 113 | + |
| 114 | +describeControlUiE2e("Control UI composer pointer controls", () => { |
| 115 | + beforeAll(async () => { |
| 116 | + server = await startControlUiE2eServer(); |
| 117 | + }); |
| 118 | + |
| 119 | + afterAll(async () => { |
| 120 | + await server?.close(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("sends and stops without moving the action out from under an Android-like tap", async () => { |
| 124 | + const browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 125 | + const context = await browser.newContext({ |
| 126 | + hasTouch: true, |
| 127 | + isMobile: true, |
| 128 | + serviceWorkers: "block", |
| 129 | + viewport: { width: 393, height: 852 }, |
| 130 | + }); |
| 131 | + const page = await context.newPage(); |
| 132 | + const gateway = await installMockGateway(page, { |
| 133 | + assistantName: "OpenClaw", |
| 134 | + deferredMethods: ["chat.send"], |
| 135 | + }); |
| 136 | + |
| 137 | + try { |
| 138 | + await page.goto(`${server.baseUrl}chat`); |
| 139 | + await gateway.waitForRequest("chat.startup"); |
| 140 | + await page.addStyleTag({ |
| 141 | + content: ":root { --safe-area-bottom: 34px !important; }", |
| 142 | + }); |
| 143 | + |
| 144 | + const composerShell = page.locator(".agent-chat__composer-shell"); |
| 145 | + const textarea = page.locator(".agent-chat__input textarea"); |
| 146 | + await textarea.fill("Verify mobile safe-area touch controls"); |
| 147 | + await textarea.focus(); |
| 148 | + await expect |
| 149 | + .poll(() => composerShell.evaluate((node) => getComputedStyle(node).marginBottom)) |
| 150 | + .toBe("0px"); |
| 151 | + |
| 152 | + const send = page.getByRole("button", { name: "Send message" }); |
| 153 | + await expect.poll(() => send.isVisible()).toBe(true); |
| 154 | + await installPointerTrace(page, send); |
| 155 | + await send.tap(); |
| 156 | + expectStablePointerActivation(await readPointerTrace(page)); |
| 157 | + await expect |
| 158 | + .poll(() => textarea.evaluate((node) => document.activeElement === node)) |
| 159 | + .toBe(true); |
| 160 | + |
| 161 | + const sendRequest = await gateway.waitForRequest("chat.send"); |
| 162 | + const runId = |
| 163 | + typeof sendRequest.params === "object" && |
| 164 | + sendRequest.params !== null && |
| 165 | + "idempotencyKey" in sendRequest.params |
| 166 | + ? String(sendRequest.params.idempotencyKey) |
| 167 | + : ""; |
| 168 | + expect(runId).not.toBe(""); |
| 169 | + expect(await gateway.getRequests("chat.send")).toHaveLength(1); |
| 170 | + |
| 171 | + await gateway.resolveDeferred("chat.send", { runId, status: "started" }); |
| 172 | + await gateway.emitGatewayEvent("chat", { |
| 173 | + deltaText: "Working on it.", |
| 174 | + message: { |
| 175 | + content: [{ text: "Working on it.", type: "text" }], |
| 176 | + role: "assistant", |
| 177 | + timestamp: Date.now(), |
| 178 | + }, |
| 179 | + runId, |
| 180 | + sessionKey: "main", |
| 181 | + state: "delta", |
| 182 | + }); |
| 183 | + |
| 184 | + const stop = page.getByRole("button", { name: "Stop generating" }); |
| 185 | + await expect.poll(() => stop.isVisible()).toBe(true); |
| 186 | + await textarea.focus(); |
| 187 | + await expect |
| 188 | + .poll(() => composerShell.evaluate((node) => getComputedStyle(node).marginBottom)) |
| 189 | + .toBe("0px"); |
| 190 | + await installPointerTrace(page, stop); |
| 191 | + await stop.tap(); |
| 192 | + expectStablePointerActivation(await readPointerTrace(page)); |
| 193 | + await expect |
| 194 | + .poll(() => textarea.evaluate((node) => document.activeElement === node)) |
| 195 | + .toBe(true); |
| 196 | + |
| 197 | + const abortRequest = await gateway.waitForRequest("chat.abort"); |
| 198 | + expect(abortRequest.params).toMatchObject({ |
| 199 | + runId, |
| 200 | + sessionKey: "main", |
| 201 | + }); |
| 202 | + await expect.poll(() => stop.count()).toBe(0); |
| 203 | + } finally { |
| 204 | + await context.close(); |
| 205 | + await browser.close(); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + it("sends and stops in a narrow desktop viewport without moving under the mouse", async () => { |
| 210 | + const browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 211 | + const context = await browser.newContext({ |
| 212 | + hasTouch: false, |
| 213 | + isMobile: false, |
| 214 | + serviceWorkers: "block", |
| 215 | + viewport: { width: 393, height: 852 }, |
| 216 | + }); |
| 217 | + const page = await context.newPage(); |
| 218 | + const gateway = await installMockGateway(page, { |
| 219 | + assistantName: "OpenClaw", |
| 220 | + deferredMethods: ["chat.send"], |
| 221 | + }); |
| 222 | + |
| 223 | + try { |
| 224 | + await page.goto(`${server.baseUrl}chat`); |
| 225 | + await gateway.waitForRequest("chat.startup"); |
| 226 | + await page.addStyleTag({ |
| 227 | + content: ":root { --safe-area-bottom: 34px !important; }", |
| 228 | + }); |
| 229 | + |
| 230 | + const composerShell = page.locator(".agent-chat__composer-shell"); |
| 231 | + const textarea = page.locator(".agent-chat__input textarea"); |
| 232 | + await textarea.fill("Verify narrow desktop pointer controls"); |
| 233 | + await textarea.focus(); |
| 234 | + await expect |
| 235 | + .poll(() => composerShell.evaluate((node) => getComputedStyle(node).marginBottom)) |
| 236 | + .toBe("0px"); |
| 237 | + |
| 238 | + const send = page.getByRole("button", { name: "Send message" }); |
| 239 | + await expect.poll(() => send.isVisible()).toBe(true); |
| 240 | + await installPointerTrace(page, send); |
| 241 | + await clickMouseAtCurrentCenter(page, send); |
| 242 | + |
| 243 | + expectStablePointerActivation(await readPointerTrace(page)); |
| 244 | + const sendRequest = await gateway.waitForRequest("chat.send"); |
| 245 | + expect(await gateway.getRequests("chat.send")).toHaveLength(1); |
| 246 | + await expect |
| 247 | + .poll(() => textarea.evaluate((node) => document.activeElement === node)) |
| 248 | + .toBe(true); |
| 249 | + |
| 250 | + const runId = |
| 251 | + typeof sendRequest.params === "object" && |
| 252 | + sendRequest.params !== null && |
| 253 | + "idempotencyKey" in sendRequest.params |
| 254 | + ? String(sendRequest.params.idempotencyKey) |
| 255 | + : ""; |
| 256 | + expect(runId).not.toBe(""); |
| 257 | + await gateway.resolveDeferred("chat.send", { runId, status: "started" }); |
| 258 | + await gateway.emitGatewayEvent("chat", { |
| 259 | + deltaText: "Working on it.", |
| 260 | + message: { |
| 261 | + content: [{ text: "Working on it.", type: "text" }], |
| 262 | + role: "assistant", |
| 263 | + timestamp: Date.now(), |
| 264 | + }, |
| 265 | + runId, |
| 266 | + sessionKey: "main", |
| 267 | + state: "delta", |
| 268 | + }); |
| 269 | + |
| 270 | + const stop = page.getByRole("button", { name: "Stop generating" }); |
| 271 | + await expect.poll(() => stop.isVisible()).toBe(true); |
| 272 | + await textarea.focus(); |
| 273 | + await installPointerTrace(page, stop); |
| 274 | + await clickMouseAtCurrentCenter(page, stop); |
| 275 | + expectStablePointerActivation(await readPointerTrace(page)); |
| 276 | + const abortRequest = await gateway.waitForRequest("chat.abort"); |
| 277 | + expect(abortRequest.params).toMatchObject({ runId, sessionKey: "main" }); |
| 278 | + await expect |
| 279 | + .poll(() => textarea.evaluate((node) => document.activeElement === node)) |
| 280 | + .toBe(true); |
| 281 | + } finally { |
| 282 | + await context.close(); |
| 283 | + await browser.close(); |
| 284 | + } |
| 285 | + }); |
| 286 | + |
| 287 | + it("keeps wide desktop mouse activation and keyboard activation working", async () => { |
| 288 | + const browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 289 | + const context = await browser.newContext({ |
| 290 | + hasTouch: false, |
| 291 | + isMobile: false, |
| 292 | + serviceWorkers: "block", |
| 293 | + viewport: { width: 1280, height: 900 }, |
| 294 | + }); |
| 295 | + const page = await context.newPage(); |
| 296 | + const gateway = await installMockGateway(page, { |
| 297 | + assistantName: "OpenClaw", |
| 298 | + deferredMethods: ["chat.send"], |
| 299 | + }); |
| 300 | + |
| 301 | + try { |
| 302 | + await page.goto(`${server.baseUrl}chat`); |
| 303 | + await gateway.waitForRequest("chat.startup"); |
| 304 | + const textarea = page.locator(".agent-chat__input textarea"); |
| 305 | + await textarea.fill("Verify wide desktop pointer controls"); |
| 306 | + await textarea.focus(); |
| 307 | + const send = page.getByRole("button", { name: "Send message" }); |
| 308 | + await installPointerTrace(page, send); |
| 309 | + await clickMouseAtCurrentCenter(page, send); |
| 310 | + expectStablePointerActivation(await readPointerTrace(page)); |
| 311 | + const sendRequest = await gateway.waitForRequest("chat.send"); |
| 312 | + expect(await gateway.getRequests("chat.send")).toHaveLength(1); |
| 313 | + |
| 314 | + const runId = |
| 315 | + typeof sendRequest.params === "object" && |
| 316 | + sendRequest.params !== null && |
| 317 | + "idempotencyKey" in sendRequest.params |
| 318 | + ? String(sendRequest.params.idempotencyKey) |
| 319 | + : ""; |
| 320 | + await gateway.resolveDeferred("chat.send", { runId, status: "started" }); |
| 321 | + await gateway.emitGatewayEvent("chat", { |
| 322 | + deltaText: "Working on it.", |
| 323 | + message: { |
| 324 | + content: [{ text: "Working on it.", type: "text" }], |
| 325 | + role: "assistant", |
| 326 | + timestamp: Date.now(), |
| 327 | + }, |
| 328 | + runId, |
| 329 | + sessionKey: "main", |
| 330 | + state: "delta", |
| 331 | + }); |
| 332 | + |
| 333 | + const stop = page.getByRole("button", { name: "Stop generating" }); |
| 334 | + await stop.focus(); |
| 335 | + await expect.poll(() => stop.evaluate((node) => document.activeElement === node)).toBe(true); |
| 336 | + await stop.press("Space"); |
| 337 | + const abortRequest = await gateway.waitForRequest("chat.abort"); |
| 338 | + expect(abortRequest.params).toMatchObject({ runId, sessionKey: "main" }); |
| 339 | + |
| 340 | + await textarea.fill("Verify keyboard Send"); |
| 341 | + await textarea.focus(); |
| 342 | + await textarea.press("Tab"); |
| 343 | + const keyboardSend = page.getByRole("button", { name: "Send message" }); |
| 344 | + await expect |
| 345 | + .poll(() => keyboardSend.evaluate((node) => document.activeElement === node)) |
| 346 | + .toBe(true); |
| 347 | + await keyboardSend.press("Enter"); |
| 348 | + await expect.poll(async () => (await gateway.getRequests("chat.send")).length).toBe(2); |
| 349 | + } finally { |
| 350 | + await context.close(); |
| 351 | + await browser.close(); |
| 352 | + } |
| 353 | + }); |
| 354 | + |
| 355 | + it("does not send when a mouse gesture leaves the button before release", async () => { |
| 356 | + const browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 357 | + const context = await browser.newContext({ |
| 358 | + hasTouch: false, |
| 359 | + isMobile: false, |
| 360 | + serviceWorkers: "block", |
| 361 | + viewport: { width: 393, height: 852 }, |
| 362 | + }); |
| 363 | + const page = await context.newPage(); |
| 364 | + const gateway = await installMockGateway(page, { assistantName: "OpenClaw" }); |
| 365 | + |
| 366 | + try { |
| 367 | + await page.goto(`${server.baseUrl}chat`); |
| 368 | + await gateway.waitForRequest("chat.startup"); |
| 369 | + await page.addStyleTag({ |
| 370 | + content: ":root { --safe-area-bottom: 34px !important; }", |
| 371 | + }); |
| 372 | + const textarea = page.locator(".agent-chat__input textarea"); |
| 373 | + await textarea.fill("Do not send this draft"); |
| 374 | + await textarea.focus(); |
| 375 | + const send = page.getByRole("button", { name: "Send message" }); |
| 376 | + const bounds = await send.boundingBox(); |
| 377 | + expect(bounds).not.toBeNull(); |
| 378 | + if (!bounds) { |
| 379 | + return; |
| 380 | + } |
| 381 | + await page.mouse.move(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); |
| 382 | + await page.mouse.down(); |
| 383 | + await page.mouse.move(bounds.x - 20, bounds.y - 20); |
| 384 | + await page.mouse.up(); |
| 385 | + await page.evaluate( |
| 386 | + () => new Promise<void>((resolve) => requestAnimationFrame(() => resolve())), |
| 387 | + ); |
| 388 | + expect(await gateway.getRequests("chat.send")).toHaveLength(0); |
| 389 | + await expect.poll(() => textarea.inputValue()).toBe("Do not send this draft"); |
| 390 | + } finally { |
| 391 | + await context.close(); |
| 392 | + await browser.close(); |
| 393 | + } |
| 394 | + }); |
| 395 | +}); |
0 commit comments