Skip to content

Write HTML. Render video. Built for agents.

test(studio): sample gesture replay from one preview state (#6076)

Miguel Ángel3dd9f5178522
diff --git a/packages/studio/tests/e2e/edit-accuracy/case.mjs b/packages/studio/tests/e2e/edit-accuracy/case.mjs
index dbe5139956..0b7bbd297c 100644
--- a/packages/studio/tests/e2e/edit-accuracy/case.mjs
+++ b/packages/studio/tests/e2e/edit-accuracy/case.mjs
@@ -203,7 +203,7 @@ async function findHandles(ctx) {
}
async function readQuads({ handles }) {
- return Promise.all([
+ const read = await Promise.all([
contentQuad(handles.root),
contentQuad(handles.target),
handles.target.evaluate((e) => ({
@@ -212,6 +212,12 @@ async function readQuads({ handles }) {
clip: getComputedStyle(e).clipPath,
})),
]);
+ const shown = await handles.target.evaluate((e) => {
+ const host = e.ownerDocument.defaultView?.frameElement;
+ return !host || host.checkVisibility({ visibilityProperty: true });
+ });
+ if (!shown) throw new Error("the measured preview was retired");
+ return read;
}
/** The target's rendered quad, visible (cropped) quad and the screen/composition mapping, from CDP quads. */
@@ -672,12 +678,26 @@ async function waitingQuad(page, map) {
return quad;
}
-async function sample(ctx, gesture, point, pointerScreen) {
- const m = await measure(ctx);
- if (gesture === "crop") m.outline = await cropOutline(ctx, m.map);
- const waiting = await waitingQuad(ctx.page, m.map);
- const shown = waiting ? { ...m, quad: waiting, visible: waiting } : m;
- return { m: shown, actual: m.visible, p: point(shown), c: m.map.toComp(pointerScreen) };
+export async function sample(ctx, gesture, point, pointerScreen) {
+ for (;;) {
+ const waiting = await ctx.page.$("[data-dom-edit-press-waiting]");
+ try {
+ const m = await measure(ctx);
+ if (gesture === "crop") m.outline = await cropOutline(ctx, m.map);
+ const quad = waiting && (await contentQuad(waiting)).map(m.map.toComp);
+ // Replay removes the marker before moving the element. Do not combine a pre-replay
+ // element read with a post-replay marker read into a position nobody displayed.
+ if (
+ waiting &&
+ !(await waiting.evaluate((e) => e.hasAttribute("data-dom-edit-press-waiting")))
+ )
+ continue;
+ const shown = quad ? { ...m, quad, visible: quad } : m;
+ return { m: shown, actual: m.visible, p: point(shown), c: m.map.toComp(pointerScreen) };
+ } finally {
+ await waiting?.dispose();
+ }
+ }
}
const TRACE_CATEGORIES = ["toplevel", "devtools.timeline", "blink.user_timing"];
diff --git a/packages/studio/tests/e2e/edit-accuracy/sampling.test.mjs b/packages/studio/tests/e2e/edit-accuracy/sampling.test.mjs
new file mode 100644
index 0000000000..097f8bc5b7
--- /dev/null
+++ b/packages/studio/tests/e2e/edit-accuracy/sampling.test.mjs
@@ -0,0 +1,68 @@
+// @vitest-environment happy-dom
+import { describe, expect, it } from "vitest";
+import { sample, measure } from "./case.mjs";
+
+const quad = (x, width = 20, height = 10) => [x, 0, x + width, 0, x + width, height, x, height];
+function handle(element, read) {
+ Object.defineProperties(element, { offsetWidth: { value: 20 }, offsetHeight: { value: 10 } });
+ return {
+ client: { send: async () => ({ quads: [read()] }) },
+ remoteObject: () => ({ objectId: "element" }),
+ evaluate: async (fn) => fn(element),
+ dispose: async () => {},
+ };
+}
+const root = () => handle(document.createElement("div"), () => quad(0, 1920, 1080));
+
+describe("sampling the displayed gesture", () => {
+ it("re-reads when a waiting press replays between geometry and marker reads", async () => {
+ let waiting = true;
+ let x = 0;
+ const marker = document.createElement("div");
+ marker.setAttribute("data-dom-edit-press-waiting", "true");
+ const target = handle(document.createElement("div"), () => {
+ const before = quad(x);
+ // The queued press starts after CDP captured the old element position.
+ x = 8;
+ waiting = false;
+ marker.removeAttribute("data-dom-edit-press-waiting");
+ return before;
+ });
+ const outline = handle(marker, () => quad(8));
+ const ctx = {
+ page: { $: async () => (waiting ? outline : null) },
+ handles: { target, root: root() },
+ };
+ const result = await sample(ctx, "move", (m) => m.quad[0], [8, 0]);
+ expect(result.p).toEqual([8, 0]);
+ expect(result.actual[0]).toEqual([8, 0]);
+ });
+
+ it("finds the promoted preview even while the hidden old handle still answers CDP", async () => {
+ const iframe = document.createElement("iframe");
+ document.body.append(iframe);
+ iframe.checkVisibility = () => false;
+ Object.defineProperty(iframe.contentWindow, "frameElement", { value: iframe });
+ const oldTarget = handle(iframe.contentDocument.createElement("div"), () => quad(0));
+ const target = handle(document.createElement("div"), () => quad(8));
+ const liveRoot = root();
+ const host = {
+ evaluate: async () => true,
+ boundingBox: async () => ({ width: 1920, height: 1080 }),
+ };
+ const frame = {
+ url: () => "/preview?new",
+ frameElement: async () => host,
+ $: async (selector) => (selector === '[data-composition-id="main"]' ? liveRoot : target),
+ };
+ try {
+ const result = await measure({
+ page: { frames: () => [frame] },
+ handles: { root: root(), target: oldTarget },
+ });
+ expect(result.quad[0]).toEqual([8, 0]);
+ } finally {
+ iframe.remove();
+ }
+ });
+});