Lightpanda: the headless browser designed for AI and automation
Merge branch 'main' into touch-input
Adrià Arrufat2a97286aad28
diff --cc src/server/bidi/input.zigindex 8096b6798,7dd4aa5f6..8a8c4d420--- a/src/server/bidi/input.zig+++ b/src/server/bidi/input.zig@@@ -802,83 -781,53 +802,130 @@@ test "bidi.input: click via element ori} }, .{ .id = 9 });}+test "bidi.input: touch tap" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const context_id = try ctx.createContext(.{ .url = "bidi/input.html" });++ try ctx.processMessage(.{+ .id = 1,+ .method = "browsingContext.locateNodes",+ .params = .{ .context = context_id, .locator = .{ .type = "css", .value = "#btn" } },+ });+ try ctx.expectSentResult(.{ .nodes = .{.{ .sharedId = "1" }} }, .{ .id = 1 });++ try evaluate(&ctx, 2, context_id,+ \\for (const t of ['pointerdown', 'pointerup', 'pointercancel', 'touchstart', 'touchmove', 'touchend']) {+ \\ document.addEventListener(t, (e) => window.events.push(t + ':' + (e.pointerType ?? e.changedTouches[0].target.id)));+ \\}+ \\'ok'+ );+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "string", .value = "ok" } }, .{ .id = 2 });++ try ctx.processMessage(.{+ .id = 3,+ .method = "input.performActions",+ .params = .{ .context = context_id, .actions = .{.{+ .type = "pointer",+ .id = "finger",+ .parameters = .{ .pointerType = "touch" },+ .actions = .{+ .{ .type = "pointerMove", .x = 0, .y = 0, .origin = .{ .type = "element", .element = .{ .sharedId = "1" } } },+ .{ .type = "pointerDown", .button = 0 },+ .{ .type = "pointerUp", .button = 0 },+ },+ }} },+ });+ try ctx.expectSentResult(null, .{ .id = 3 });+ try evaluate(&ctx, 4, context_id, "window.events.join(' ')");+ try ctx.expectSentResult(.{ .type = "success", .result = .{+ .type = "string",+ .value = "pointerdown:touch touchstart:btn pointerup:touch touchend:btn mousemove@btn mousedown@btn mouseup@btn click@btn",+ } }, .{ .id = 4 });++ // A drag past the slop scrolls: the touch events go on, but the pointer is+ // cancelled and nothing clicks.+ try ctx.processMessage(.{+ .id = 5,+ .method = "input.performActions",+ .params = .{ .context = context_id, .actions = .{.{+ .type = "pointer",+ .id = "finger",+ .parameters = .{ .pointerType = "touch" },+ .actions = .{+ .{ .type = "pointerDown", .button = 0 },+ .{ .type = "pointerMove", .x = 40, .y = 0, .origin = "pointer" },+ .{ .type = "pointerUp", .button = 0 },+ },+ }} },+ });+ try ctx.expectSentResult(null, .{ .id = 5 });+ try evaluate(&ctx, 6, context_id, "window.events.slice(8).join(' ')");+ try ctx.expectSentResult(.{ .type = "success", .result = .{+ .type = "string",+ .value = "pointerdown:touch touchstart:btn touchmove:btn pointercancel:touch touchend:btn",+ } }, .{ .id = 6 });++ // The source's pointerType is fixed once it exists.+ try ctx.processMessage(.{+ .id = 7,+ .method = "input.performActions",+ .params = .{ .context = context_id, .actions = .{.{+ .type = "pointer",+ .id = "finger",+ .actions = .{.{ .type = "pointerDown", .button = 0 }},+ }} },+ });+ try ctx.expectSentError("invalid argument", "input source id already used with a different type", .{ .id = 7 });+}++ test "bidi.input: later releases of a three-button chord report detail 0" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const context_id = try ctx.createContext(.{ .url = "bidi/input.html" });++ try evaluate(&ctx, 1, context_id,+ \\window.releases = [];+ \\for (const type of ['mouseup', 'click', 'auxclick', 'dblclick']) {+ \\ document.addEventListener(type, e => releases.push(`${e.type}:${e.button}:${e.buttons}:${e.detail}`));+ \\}+ \\'ready'+ );+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "string", .value = "ready" } }, .{ .id = 1 });++ try ctx.processMessage(.{+ .id = 2,+ .method = "browsingContext.locateNodes",+ .params = .{ .context = context_id, .locator = .{ .type = "css", .value = "#btn" } },+ });+ try ctx.expectSentResult(.{ .nodes = .{.{ .sharedId = "1" }} }, .{ .id = 2 });++ try ctx.processMessage(.{+ .id = 3,+ .method = "input.performActions",+ .params = .{ .context = context_id, .actions = .{.{+ .type = "pointer",+ .id = "mouse",+ .actions = .{+ .{ .type = "pointerMove", .x = 0, .y = 0, .origin = .{ .type = "element", .element = .{ .sharedId = "1" } } },+ .{ .type = "pointerDown", .button = 0 },+ .{ .type = "pointerDown", .button = 2 },+ .{ .type = "pointerDown", .button = 1 },+ .{ .type = "pointerUp", .button = 2 },+ .{ .type = "pointerUp", .button = 1 },+ .{ .type = "pointerUp", .button = 0 },+ },+ }} },+ });+ try ctx.expectSentResult(null, .{ .id = 3 });++ try evaluate(&ctx, 4, context_id, "window.releases.join(' ')");+ try ctx.expectSentResult(.{ .type = "success", .result = .{+ .type = "string",+ .value = "mouseup:2:5:1 auxclick:2:5:1 mouseup:1:1:0 mouseup:0:0:0",+ } }, .{ .id = 4 });+ }+test "bidi.input: keys and modifiers" {var ctx = try testing.context();defer ctx.deinit();diff --cc src/server/cdp/domains/input.zigindex 7fd6e9274,cf7753211..1b207635e--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@@ -1603,706 -1586,72 +1603,776 @@@ test "cdp.input: dispatchTouchEvent a tvar ctx = try testing.context();defer ctx.deinit();- const bc = try ctx.loadBrowserContext(.{ .url = "mcp_actions.html" });- const frame = bc.mainFrame().?;+ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;- var ls: lp.js.Local.Scope = undefined;- frame.js.localScope(&ls);- defer ls.deinit();+ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = 0, .y = 3000 }} },+ });+ try testing.expect(frame.page.input_touch_contact != null);++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{} },+ });+ try testing.expect(frame.page.input_touch_contact == null);+}++test "cdp.input: dispatchTouchEvent empty touchStart is InvalidParams" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ _ = try bc.session.createPage();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 1 });+}++test "cdp.input: dispatchTouchEvent a multi-contact touchStart/touchMove is InvalidParams" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ _ = try bc.session.createPage();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{ .{ .x = 0, .y = 0 }, .{ .x = 10, .y = 10 } } },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 1 });++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchMove", .touchPoints = &.{ .{ .x = 0, .y = 0 }, .{ .x = 10, .y = 10 } } },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 2 });+}++test "cdp.input: dispatchTouchEvent empty touchEnd with no active contact is InvalidParams" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ _ = try bc.session.createPage();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 1 });+}++test "cdp.input: dispatchTouchEvent touchMove with no active contact is InvalidParams" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ _ = try bc.session.createPage();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = 0, .y = 0 }} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 1 });+}++test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mismatches" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;+ try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();+ _ = try ls.local.compileAndRun(+ \\window.touchEvents = [];+ \\const target = document.getElementById('hoverTarget');+ \\for (const type of ['touchstart', 'touchmove', 'touchend']) {+ \\ target.addEventListener(type, e => {+ \\ const t = e.changedTouches[0];+ \\ touchEvents.push([e.type, t.identifier, t.clientX, t.clientY]);+ \\ });+ \\}+ , null);+ const x = try (try ls.local.compileAndRun("target.getBoundingClientRect().x", null)).toF64();+ const y = try (try ls.local.compileAndRun("target.getBoundingClientRect().y", null)).toF64();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = x, .y = y, .id = 0.5 }} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 1 });+ try testing.expect(frame.page.input_touch_contact == null);++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = x, .y = y, .id = 7 }} },+ });+ try testing.expectEqual(@as(i32, 7), frame.page.input_touch_contact.?.point.id);++ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + 100, .y = y + 100, .id = 8 }} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 3 });+ try testing.expectEqual(x, frame.page.input_touch_contact.?.point.x);+ try testing.expectEqual(y, frame.page.input_touch_contact.?.point.y);++ try ctx.processMessage(.{+ .id = 4,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + 1, .y = y + 20, .id = 7 }} },+ });++ try ctx.processMessage(.{+ .id = 5,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 20, .id = 8 }} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 5 });+ try testing.expect(frame.page.input_touch_contact != null);++ try ctx.processMessage(.{+ .id = 6,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 20, .id = 7 }} },+ });+ try testing.expect(frame.page.input_touch_contact == null);+ try testing.expect((try ls.local.compileAndRun(+ \\JSON.stringify(touchEvents) === JSON.stringify([+ \\ ['touchstart', 7, target.getBoundingClientRect().x, target.getBoundingClientRect().y],+ \\ ['touchmove', 7, target.getBoundingClientRect().x + 1, target.getBoundingClientRect().y + 20],+ \\ ['touchend', 7, target.getBoundingClientRect().x + 1, target.getBoundingClientRect().y + 20]+ \\])+ , null)).isTrue());+}++test "cdp.input: dispatchTouchEvent a second touchStart while a contact is active is InvalidParams" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ _ = try ls.local.compileAndRun(+ \\document.getElementById('hoverTarget')+ \\ .addEventListener('touchstart', () => { window.starts = (window.starts || 0) + 1; });+ , null);++ const first_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();+ const first_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();+ const other_x = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().x", null)).toF64();+ const other_y = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().y", null)).toF64();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = first_x, .y = first_y }} },+ });++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = other_x, .y = other_y }} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 2 });++ _ = try ls.local.compileAndRun(+ \\document.getElementById('hoverTarget')+ \\ .addEventListener('touchend', (e) => {+ \\ window.endedOnOriginal = e.changedTouches[0].clientX === document.getElementById('hoverTarget').getBoundingClientRect().x;+ \\ });+ , null);+ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{} },+ });++ const result = try ls.local.compileAndRun("window.starts === 1 && window.endedOnOriginal === true", null);+ try testing.expect(result.isTrue());+}++// Puppeteer releases without an intervening touchMove.+test "cdp.input: dispatchTouchEvent a touchEnd carrying the released point is accepted" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ _ = try ls.local.compileAndRun(+ \\const t = document.getElementById('hoverTarget');+ \\t.addEventListener('touchend', (e) => {+ \\ const touch = e.changedTouches[0];+ \\ window.endTarget = touch.target === t;+ \\ window.endX = touch.clientX;+ \\ window.endY = touch.clientY;+ \\});+ , null);++ const rect_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();+ const end_x = rect_x + 5;+ const end_y = rect_y + 9;++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = rect_x, .y = rect_y }} },+ });++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = end_x, .y = end_y }} },+ });+ try testing.expect(frame.page.input_touch_contact == null);++ const result = try ls.local.compileAndRun(+ "window.endTarget === true && window.endX === " ++ "document.getElementById('hoverTarget').getBoundingClientRect().x + 5" +++ " && window.endY === document.getElementById('hoverTarget').getBoundingClientRect().y + 9",+ null,+ );+ try testing.expect(result.isTrue());++ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = rect_x, .y = rect_y }} },+ });+ try testing.expect(frame.page.input_touch_contact != null);+}++test "cdp.input: dispatchTouchEvent an empty touchEnd uses the stored position" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ _ = try ls.local.compileAndRun(+ \\const t = document.getElementById('hoverTarget');+ \\t.addEventListener('touchend', (e) => {+ \\ const touch = e.changedTouches[0];+ \\ window.endX = touch.clientX;+ \\ window.endY = touch.clientY;+ \\});+ , null);++ const rect_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = rect_x, .y = rect_y }} },+ });+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{} },+ });++ const result = try ls.local.compileAndRun("window.endX === document.getElementById('hoverTarget').getBoundingClientRect().x && window.endY === document.getElementById('hoverTarget').getBoundingClientRect().y", null);+ try testing.expect(result.isTrue());+}++test "cdp.input: dispatchTouchEvent rejects populated touchCancel without lifting contact" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ _ = try ls.local.compileAndRun(+ \\const t = document.getElementById('hoverTarget');+ \\t.addEventListener('touchcancel', (e) => {+ \\ const touch = e.changedTouches[0];+ \\ window.cancelable = e.cancelable;+ \\ window.cancelX = touch.clientX;+ \\ window.cancelY = touch.clientY;+ \\});+ , null);++ const rect_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();+ const cancel_x = rect_x + 3;+ const cancel_y = rect_y + 4;++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = rect_x, .y = rect_y }} },+ });+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchCancel", .touchPoints = &.{.{ .x = cancel_x, .y = cancel_y }} },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 2 });+ try testing.expect(frame.page.input_touch_contact != null);++ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchCancel", .touchPoints = &.{} },+ });+ try testing.expect(frame.page.input_touch_contact == null);++ const result = try ls.local.compileAndRun(+ "window.cancelable === false && window.cancelX === " ++ "document.getElementById('hoverTarget').getBoundingClientRect().x" +++ " && window.cancelY === document.getElementById('hoverTarget').getBoundingClientRect().y",+ null,+ );+ try testing.expect(result.isTrue());+}++test "cdp.input: dispatchTouchEvent a multi-point touchEnd/touchCancel is InvalidParams" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ const rect_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = rect_x, .y = rect_y }} },+ });++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchTouchEvent",+ .params = .{ .type = "touchEnd", .touchPoints = &.{ .{ .x = rect_x, .y = rect_y }, .{ .x = rect_x, .y = rect_y, .id = 1 } } },+ });+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 2 });+}++test "cdp.input: dispatchKeyEvent Tab runs sequential focus navigation" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ var try_catch: lp.js.TryCatch = undefined;+ try_catch.init(&ls.local);+ defer try_catch.deinit();++ // Three controls whose tabindex order (1, 2, 3) differs from document+ // order (2, 1, 3): focus order must follow tabindex, not the tree.+ _ = try ls.local.compileAndRun(+ \\document.body.innerHTML =+ \\ '<input id="i2" tabindex="2">' ++ \\ '<button id="b1" tabindex="1">b</button>' ++ \\ '<select id="s3" tabindex="3"></select>';+ , null);++ // Nothing focused yet → activeElement is <body>.+ try testing.expect((try ls.local.compileAndRun("document.activeElement === document.body", null)).isTrue());++ // First Tab → lowest positive tabindex (#b1), regardless of document order.+ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Tab", .code = "Tab" },+ });+ try testing.expect((try ls.local.compileAndRun("document.activeElement.id === 'b1'", null)).isTrue());++ // Second Tab → next in tabindex order (#i2).+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Tab", .code = "Tab" },+ });+ try testing.expect((try ls.local.compileAndRun("document.activeElement.id === 'i2'", null)).isTrue());++ // Shift+Tab (modifiers bit 8) walks backward → back to #b1.+ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Tab", .code = "Tab", .modifiers = 8 },+ });+ try testing.expect((try ls.local.compileAndRun("document.activeElement.id === 'b1'", null)).isTrue());+}++test "cdp.input: dispatchKeyEvent beforeinput can veto the edit" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;+ try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{+ .reason = .address_bar,+ .kind = .{ .push = null },+ });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ // A listener that rejects every keystroke, the way masked-input libraries+ // do. Records what it saw so we can assert the event was cancelable.+ _ = try ls.local.compileAndRun(+ \\const inp = document.getElementById('inp');+ \\inp.value = 'ab';+ \\inp.focus();+ \\window.seen = [];+ \\inp.addEventListener('beforeinput', (e) => {+ \\ window.seen.push(['beforeinput', e.cancelable].join(':'));+ \\ e.preventDefault();+ \\ window.seen.push(['defaultPrevented', e.defaultPrevented].join(':'));+ \\});+ \\inp.addEventListener('input', () => window.seen.push('input'));+ , null);++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "c", .text = "c" },+ });++ // The veto held: no character inserted and no `input` event followed.+ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === 'ab' &&+ \\window.seen.join(',') === 'beforeinput:true,defaultPrevented:true'+ , null)).isTrue());++ // Backspace goes through the same pre-edit gate.+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" },+ });+ try testing.expect((try ls.local.compileAndRun("inp.value === 'ab'", null)).isTrue());++ // Without the veto, the edit goes through and `input` is dispatched — and+ // `input` itself is not cancelable.+ _ = try ls.local.compileAndRun(+ \\const clone = inp.cloneNode(true);+ \\inp.replaceWith(clone);+ \\clone.focus();+ \\window.seen = [];+ \\clone.addEventListener('input', (e) => window.seen.push(['input', e.cancelable].join(':')));+ , null);++ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "c", .text = "c" },+ });+ try testing.expect((try ls.local.compileAndRun(+ \\clone.value === 'abc' && window.seen.join(',') === 'input:false'+ , null)).isTrue());+}++test "cdp.input: editing keys honor the selection of a default-valued textarea" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ // This <textarea>'s value comes from its child text node — nothing assigns+ // to `value` — so the selection has to clamp against the parsed text.+ _ = try ls.local.compileAndRun(+ \\document.body.innerHTML = '<textarea id="ta">abcdef</textarea>';+ \\const ta = document.getElementById('ta');+ \\ta.focus();+ \\ta.setSelectionRange(2, 4);+ , null);+ try testing.expect((try ls.local.compileAndRun("ta.selectionStart === 2 && ta.selectionEnd === 4", null)).isTrue());++ // Backspace over a range deletes the range, leaving the caret at its start.+ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" },+ });+ try testing.expect((try ls.local.compileAndRun("ta.value === 'abef'", null)).isTrue());+ try testing.expect((try ls.local.compileAndRun("ta.selectionStart === 2 && ta.selectionEnd === 2", null)).isTrue());++ // Collapsed caret: Backspace removes the character on its left.+ _ = try ls.local.compileAndRun("ta.setSelectionRange(3, 3)", null);+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" },+ });+ try testing.expect((try ls.local.compileAndRun("ta.value === 'abf'", null)).isTrue());+}++test "cdp.input: dispatchKeyEvent caret movement keys move the text entry cursor" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ var try_catch: lp.js.TryCatch = undefined;+ try_catch.init(&ls.local);+ defer try_catch.deinit();++ _ = try ls.local.compileAndRun(+ \\document.body.innerHTML = '<input id="t" type="text"><textarea id="ta"></textarea>';+ \\const t = document.getElementById('t');+ \\const ta = document.getElementById('ta');+ \\const sel = (e) => [e.selectionStart, e.selectionEnd, e.selectionDirection].join(',');+ \\t.focus(); t.value = 'abcdef'; t.setSelectionRange(6, 6);+ , null);++ const Step = struct { key: []const u8, modifiers: u4 = 0, expect: []const u8 };+ const shift = 8;++ // <input>: plain moves collapse, Shift extends from the anchor, a plain+ // arrow on a selection collapses to its edge, ArrowUp/ArrowDown reach the+ // ends of a single-line value.+ const input_steps = [_]Step{+ .{ .key = "ArrowLeft", .expect = "5,5,none" },+ .{ .key = "ArrowRight", .expect = "6,6,none" },+ .{ .key = "Home", .expect = "0,0,none" },+ .{ .key = "End", .expect = "6,6,none" },+ .{ .key = "ArrowUp", .expect = "0,0,none" },+ .{ .key = "ArrowDown", .expect = "6,6,none" },+ .{ .key = "ArrowRight", .expect = "6,6,none" },+ .{ .key = "ArrowLeft", .modifiers = shift, .expect = "5,6,backward" },+ .{ .key = "Home", .modifiers = shift, .expect = "0,6,backward" },+ .{ .key = "ArrowLeft", .expect = "0,0,none" },+ .{ .key = "ArrowLeft", .expect = "0,0,none" },+ .{ .key = "End", .modifiers = shift, .expect = "0,6,forward" },+ .{ .key = "ArrowLeft", .modifiers = shift, .expect = "0,5,forward" },+ .{ .key = "ArrowRight", .expect = "5,5,none" },+ };+ var id: u32 = 1;+ for (input_steps) |step| {+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = step.key, .code = step.key, .modifiers = step.modifiers },+ });+ id += 1;+ const got = try (try ls.local.compileAndRun("sel(t)", null)).toStringSlice();+ try testing.expectEqualSlices(u8, step.expect, got);+ }++ // Type-then-correct: move back two characters and insert in the middle.+ _ = try ls.local.compileAndRun("t.setSelectionRange(6, 6)", null);+ for (0..2) |_| {+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "ArrowLeft", .code = "ArrowLeft" },+ });+ id += 1;+ }+ try ctx.processMessage(.{ .id = id, .method = "Input.insertText", .params = .{ .text = "X" } });+ id += 1;+ try testing.expect((try ls.local.compileAndRun("t.value === 'abcdXef' && sel(t) === '5,5,none'", null)).isTrue());++ // Moves step over whole UTF-8 sequences, never landing inside one.+ _ = try ls.local.compileAndRun("t.value = 'aé'; t.setSelectionRange(t.value.length, t.value.length)", null);+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "ArrowLeft", .code = "ArrowLeft" },+ });+ id += 1;+ try ctx.processMessage(.{ .id = id, .method = "Input.insertText", .params = .{ .text = "X" } });+ id += 1;+ try testing.expect((try ls.local.compileAndRun("t.value === 'aXé'", null)).isTrue());++ // <textarea>: Home/End stop at the enclosing line break, ArrowLeft crosses it.+ _ = try ls.local.compileAndRun("ta.focus(); ta.value = 'ab\\ncd'; ta.setSelectionRange(5, 5)", null);+ const textarea_steps = [_]Step{+ .{ .key = "Home", .expect = "3,3,none" },+ .{ .key = "End", .expect = "5,5,none" },+ .{ .key = "Home", .modifiers = shift, .expect = "3,5,backward" },+ .{ .key = "ArrowLeft", .expect = "3,3,none" },+ .{ .key = "ArrowLeft", .expect = "2,2,none" },+ };+ for (textarea_steps) |step| {+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = step.key, .code = step.key, .modifiers = step.modifiers },+ });+ id += 1;+ const got = try (try ls.local.compileAndRun("sel(ta)", null)).toStringSlice();+ try testing.expectEqualSlices(u8, step.expect, got);+ }+}+++test "cdp.input: dispatchKeyEvent Ctrl+A selects the whole value" {++ var ctx = try testing.context();++ defer ctx.deinit();++++ const bc = try ctx.loadBrowserContext(.{ .url = "mcp_actions.html" });++ const frame = bc.mainFrame().?;++++ var ls: lp.js.Local.Scope = undefined;++ frame.js.localScope(&ls);++ defer ls.deinit();++ _ = try ls.local.compileAndRun(+ \\document.body.innerHTML = '<input id="t" type="text"><textarea id="ta"></textarea>';+ \\const t = document.getElementById('t');+ \\const ta = document.getElementById('ta');+ \\const sel = (e) => [e.selectionStart, e.selectionEnd].join(',');+ , null);++ const alt = 1;+ const ctrl = 2;+ const meta = 4;+ const shift = 8;+ const Step = struct { setup: [:0]const u8, key: []const u8, modifiers: u4, expect: [:0]const u8 };+ const steps = [_]Step{+ // the Puppeteer/Playwright way to clear a field: select all, then delete+ .{ .setup = "t.focus(); t.value = 'hello'; t.setSelectionRange(2, 2)", .key = "a", .modifiers = ctrl, .expect = "sel(t) === '0,5'" },+ .{ .setup = "t.focus(); t.value = 'hello'; t.setSelectionRange(2, 2)", .key = "A", .modifiers = ctrl, .expect = "sel(t) === '0,5'" },+ .{ .setup = "ta.focus(); ta.value = 'ab\\ncd'; ta.setSelectionRange(1, 1)", .key = "a", .modifiers = ctrl, .expect = "sel(ta) === '0,5'" },+ // Meta+A, Ctrl+Alt+A and Ctrl+Shift+A do nothing in Chrome on Linux+ .{ .setup = "t.focus(); t.value = 'hello'; t.setSelectionRange(2, 2)", .key = "a", .modifiers = meta, .expect = "sel(t) === '2,2'" },+ .{ .setup = "t.focus(); t.value = 'hello'; t.setSelectionRange(2, 2)", .key = "a", .modifiers = ctrl | alt, .expect = "sel(t) === '2,2'" },+ .{ .setup = "t.focus(); t.value = 'hello'; t.setSelectionRange(2, 2)", .key = "A", .modifiers = ctrl | shift, .expect = "sel(t) === '2,2'" },+ };++ var id: u32 = 1;+ for (steps) |step| {+ _ = try ls.local.compileAndRun(step.setup, null);+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = step.key, .code = "KeyA", .modifiers = step.modifiers },+ });+ id += 1;+ try testing.expect((try ls.local.compileAndRun(step.expect, null)).isTrue());+ }++ // a keydown from script has no default action, as in Chrome+ try testing.expect((try ls.local.compileAndRun(+ \\t.focus(); t.value = 'hello'; t.setSelectionRange(2, 2);+ \\t.dispatchEvent(new KeyboardEvent('keydown', { key: 'a', ctrlKey: true, bubbles: true, cancelable: true }));+ \\t.dispatchEvent(new KeyboardEvent('keydown', { key: 'Backspace', bubbles: true, cancelable: true }));+ \\t.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true }));+ \\t.value === 'hello' && sel(t) === '2,2' && document.activeElement === t+ , null)).isTrue());++ _ = try ls.local.compileAndRun("t.focus(); t.value = 'hello'; t.setSelectionRange(5, 5)", null);+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "a", .code = "KeyA", .modifiers = ctrl },+ });+ id += 1;+ try ctx.processMessage(.{+ .id = id,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" },+ });+ try testing.expect((try ls.local.compileAndRun("t.value === ''", null)).isTrue());+ }+// chromedp's SendKeys shape: a text-less keyDown, the char with the text, keyUp.test "cdp.input: dispatchKeyEvent text-less keyDown then char types once" {var ctx = try testing.context();@@@ -3131,50 -2052,166 +3201,214 @@@ test "cdp.input: re-navigating an iframtry testing.expectEqual(0, page.input_pointer.held);}+test "cdp.input: mouse activation rechecks disabled state between events" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;+ try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);+ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();+ const cases = .{+ .{ "t.disabled = true;", "clicks === 0 && !t.checked && events.join(',') === 'pointerdown,pointerup'", 1 },+ .{ "fieldset.disabled = true;", "clicks === 0 && !t.checked", 1 },+ .{ "fieldset.disabled = true; const legend = document.createElement('legend'); fieldset.prepend(legend); legend.append(t);", "clicks === 1 && t.checked", 1 },+ .{ "t.disabled = true; t.addEventListener('pointerdown', e => e.preventDefault());", "clicks === 0 && !t.checked", 1 },+ .{ "t.addEventListener('pointerdown', () => t.disabled = true);", "clicks === 0 && !t.checked && !events.includes('mousedown')", 1 },+ .{ "t.addEventListener('mousedown', () => t.disabled = true);", "clicks === 0 && !t.checked && !events.includes('mouseup')", 1 },+ .{ "t.addEventListener('pointerup', () => t.disabled = true);", "clicks === 0 && !t.checked && !events.includes('mouseup')", 1 },+ .{ "t.addEventListener('mouseup', () => t.disabled = true);", "clicks === 0 && !t.checked", 1 },+ .{ "t.disabled = true; t.addEventListener('pointerdown', () => t.disabled = false);", "clicks === 1 && t.checked && events.includes('mousedown')", 1 },+ .{ "t.disabled = true; t.addEventListener('pointerup', () => t.disabled = false);", "clicks === 1 && t.checked && !events.includes('mousedown') && events.includes('mouseup')", 1 },+ .{ "t.addEventListener('click', () => t.disabled = true);", "clicks === 1", 2 },+ };+ inline for (cases) |case| {+ _ = try ls.local.compileAndRun(+ \\document.body.innerHTML = '<fieldset id="fieldset"><input id="t" type="checkbox"></fieldset>';+ \\window.t = document.getElementById('t'); window.fieldset = document.getElementById('fieldset');+ \\window.clicks = 0; window.events = [];+ \\t.addEventListener('click', () => clicks++);+ \\for (const type of ['pointerdown', 'mousedown', 'pointerup', 'mouseup']) t.addEventListener(type, e => events.push(e.type));+ ++ case[0], null);+ const x = try (try ls.local.compileAndRun("t.getBoundingClientRect().x + 1", null)).toF64();+ const y = try (try ls.local.compileAndRun("t.getBoundingClientRect().y + 1", null)).toF64();+ for (0..case[2]) |_| {+ try ctx.processMessage(.{ .id = 1, .method = "Input.dispatchMouseEvent", .params = .{ .type = "mousePressed", .x = x, .y = y, .button = "left", .clickCount = 1 } });+ try ctx.processMessage(.{ .id = 2, .method = "Input.dispatchMouseEvent", .params = .{ .type = "mouseReleased", .x = x, .y = y, .button = "left", .clickCount = 1 } });+ }+ const passed = (try ls.local.compileAndRun(case[1], null)).isTrue();+ if (!passed) std.debug.print("disabled mouse case: {s}\n", .{case[0]});+ try testing.expect(passed);+ try testing.expectEqual(@as(u16, 0), frame.page.input_pointer.held);+ try testing.expect(frame.page.input_pointer.down_target == null);+ }+ // Explicit script dispatch is not trusted input and must still reach listeners.+ try testing.expect((try ls.local.compileAndRun("t.disabled = true; clicks = 0; t.dispatchEvent(new MouseEvent('click')); clicks === 1", null)).isTrue());+}+++ const MouseStep = struct {+ type: []const u8,+ button: []const u8,+ x: ?f64 = null,+ y: ?f64 = null,+ };++ /// Dispatches `steps` as Input.dispatchMouseEvent at #btn unless overridden,+ /// clickCount 2 throughout,+ /// and checks the whole `window.seq` log against `expected` (JS array elements).+ /// `setup` runs first, e.g. to add listeners that log into `window.seq`.+ fn expectMouseSequence(setup: []const u8, steps: []const MouseStep, comptime expected: []const u8) !void {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{});+ const page = try bc.session.createPage();+ const frame = page.frame().?;++ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ try testing.waitForPage(bc);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ var try_catch: lp.js.TryCatch = undefined;+ try_catch.init(&ls.local);+ defer try_catch.deinit();++ _ = try ls.local.compileAndRun(setup, null);+ const rect_x = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().y", null)).toF64();++ for (steps, 1..) |step, id| {+ try ctx.processMessage(.{+ .id = @as(i32, @intCast(id)),+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = step.type, .x = step.x orelse rect_x, .y = step.y orelse rect_y, .button = step.button, .clickCount = 2 },+ });+ }++ const result = try ls.local.compileAndRun("JSON.stringify(window.seq) === JSON.stringify([" ++ expected ++ "])", null);+ try testing.expect(result.isTrue());+ }++ test "cdp.input: the release that ends a chord reports detail 0" {+ try expectMouseSequence("", &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true', 'mouseup:2:1:2::true',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+ }++ test "cdp.input: the release that ends a chord reports detail 0 when the secondary button ends it" {+ try expectMouseSequence("", &.{+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mouseReleased", .button = "left" },+ .{ .type = "mouseReleased", .button = "right" },+ },+ \\'pointerdown:2:2:0:mouse:true', 'mousedown:2:2:2::true',+ \\'mousedown:0:3:2::true', 'mouseup:0:2:2::true', 'click:0:2:2:mouse:true',+ \\'pointerup:2:0:0:mouse:true', 'mouseup:2:0:0::true'+ );+ }++ test "cdp.input: every release after the first of a three-button chord reports detail 0" {+ try expectMouseSequence("", &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mousePressed", .button = "middle" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mouseReleased", .button = "middle" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true', 'mousedown:1:7:2::true',+ \\'mouseup:2:5:2::true', 'mouseup:1:1:0::true',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+ }++ test "cdp.input: a release with no press behind it reports detail 0" {+ try expectMouseSequence("", &.{+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+ }++ test "cdp.input: pressing a button again mid-chord re-arms its auxclick" {+ try expectMouseSequence(+ \\document.getElementById('btn').addEventListener('auxclick', e => window.seq.push(`auxclick:${e.button}:${e.buttons}:${e.detail}`));+ , &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true', 'mouseup:2:1:2::true', 'auxclick:2:1:2',+ \\'mousedown:2:3:2::true', 'mouseup:2:1:2::true', 'auxclick:2:1:2',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+ }++ test "cdp.input: a release that misses every element consumes the latest press" {+ const points = [_]struct { x: f64, y: f64 }{+ .{ .x = -1000, .y = -1000 },+ .{ .x = 700, .y = 500 },+ };+ for (points) |point| {+ try expectMouseSequence(+ // Bound the fixture's root boxes so (700,500) is a genuine miss.+ \\document.body.lastElementChild.remove();+ \\for (const el of [document.documentElement, document.body]) {+ \\ el.style.width = '200px'; el.style.height = '100px';+ \\}+ \\if (document.elementFromPoint(700, 500) !== null) throw new Error('expected an empty hit-test point');+ \\document.getElementById('btn').addEventListener('dblclick', e => window.seq.push(`dblclick:${e.detail}`));+ , &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right", .x = point.x, .y = point.y },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+ }+ }++ test "cdp.input: a missed release preserves held buttons and mouse suppression" {+ try expectMouseSequence(+ \\{+ \\ const button = document.getElementById('btn');+ \\ button.addEventListener('pointerdown', e => e.preventDefault());+ \\ button.addEventListener('pointermove', e => window.seq.push(`pointermove:${e.button}:${e.buttons}:${e.detail}`));+ \\ button.addEventListener('auxclick', e => window.seq.push(`auxclick:${e.button}:${e.buttons}:${e.detail}`));+ \\}+ , &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right", .x = -1000, .y = -1000 },+ .{ .type = "mousePressed", .button = "middle" },+ .{ .type = "mouseReleased", .button = "middle" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'pointermove:2:3:0',+ \\'pointermove:1:5:0', 'pointermove:1:1:0', 'auxclick:1:1:2',+ \\'pointerup:0:0:0:mouse:true'+ );+ }