Skip to content

Commit 1ca51a4

Browse files
committed
fix(interaction-harness): waitForPresentedMotion reads only the frames from its call on, finds two moves anywhere in them, and decodes PNG frames without a page
C17 ran BottomSheet's dim proof with waitForPresentedMotion at 37b3c4f and it failed in both engines: - In WebKit it timed out in 12 of 12 runs, though the stopped cast showed the sheet moving on every frame. The wait decoded every frame since the cast's start and asked for the moves to be its newest frames. A cast that started long before the release kept it decoding until the sheet's 550 ms settle had ended, and the frames after that hold still. - In Chromium its scratch page tripped the proof's compositor trace, which refuses any page opened during its browser-wide record. The wait now reads only the frames captured from its call on (with the frame before it as where the element stood), and resolves on `frames` consecutive moves anywhere among them. PNG frames, Chromium's, are decoded in Node with zlib and open no page. JPEG frames, WebKit's, still use one scratch page, opened only when needed. Red first: the self-test's new case starts a 400 ms move after a 1.5 s cast. On 37b3c4f the wait opened a page in Chromium, 8 of 8. Now it opens none and resolves, 16 of 16 at 16 workers in each engine. WebKit's late-catch-up timeout did not reproduce on this page, so C17's sheet is that case. [skip ci]
1 parent 37b3c4f commit 1ca51a4

2 files changed

Lines changed: 175 additions & 38 deletions

File tree

‎internal/interaction-harness/src/busy.ts‎

Lines changed: 145 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
* SYNC: Keep the README's busy-main-thread section equal to this contract.
1919
*/
2020

21+
import {inflateSync} from 'node:zlib';
2122
import type {Locator, Page} from '@playwright/test';
2223
import {
2324
frameStats,
2425
inPresentationOrder,
2526
MARKER_RGB,
2627
trackMarkerIn,
28+
type CastFrame,
2729
type FrameStats,
2830
type Screencast,
2931
type TrackedFrame,
@@ -139,15 +141,123 @@ export async function startBusyControl(
139141
};
140142
}
141143

144+
/**
145+
* The marker's box in a PNG cast frame, decoded here: a frame decoded in Node
146+
* needs no scratch page, which a browser-wide compositor trace refuses
147+
* (another page's frames would be credited to the traced one). Null for a
148+
* PNG layout the screencast does not send (8-bit RGB or RGBA, no interlace).
149+
*/
150+
function markerInPng(
151+
frame: CastFrame,
152+
rgb: readonly [number, number, number],
153+
tolerance = 40,
154+
): TrackedFrame | null {
155+
const png = Buffer.from(frame.data, 'base64');
156+
let width = 0;
157+
let height = 0;
158+
let layout = '';
159+
const data: Buffer[] = [];
160+
for (let at = 8; at + 8 <= png.length;) {
161+
const length = png.readUInt32BE(at);
162+
const type = png.toString('ascii', at + 4, at + 8);
163+
const chunk = png.subarray(at + 8, at + 8 + length);
164+
if (type === 'IHDR') {
165+
width = chunk.readUInt32BE(0);
166+
height = chunk.readUInt32BE(4);
167+
layout = `${chunk[8]}/${chunk[9]}/${chunk[12]}`;
168+
} else if (type === 'IDAT') {
169+
data.push(chunk);
170+
} else if (type === 'IEND') {
171+
break;
172+
}
173+
at += 12 + length;
174+
}
175+
const channels = layout === '8/6/0' ? 4 : layout === '8/2/0' ? 3 : 0;
176+
if (channels === 0) {
177+
return null;
178+
}
179+
const raw = inflateSync(Buffer.concat(data));
180+
const stride = width * channels;
181+
const pixels = Buffer.alloc(height * stride);
182+
for (let y = 0; y < height; y += 1) {
183+
const filter = raw[y * (stride + 1)];
184+
const line = y * (stride + 1) + 1;
185+
const row = y * stride;
186+
for (let x = 0; x < stride; x += 1) {
187+
const left = x >= channels ? pixels[row + x - channels] : 0;
188+
const up = y > 0 ? pixels[row - stride + x] : 0;
189+
const upLeft =
190+
y > 0 && x >= channels ? pixels[row - stride + x - channels] : 0;
191+
let predicted = 0;
192+
if (filter === 1) {
193+
predicted = left;
194+
} else if (filter === 2) {
195+
predicted = up;
196+
} else if (filter === 3) {
197+
predicted = (left + up) >> 1;
198+
} else if (filter === 4) {
199+
const estimate = left + up - upLeft;
200+
const toLeft = Math.abs(estimate - left);
201+
const toUp = Math.abs(estimate - up);
202+
const toUpLeft = Math.abs(estimate - upLeft);
203+
predicted =
204+
toLeft <= toUp && toLeft <= toUpLeft
205+
? left
206+
: toUp <= toUpLeft
207+
? up
208+
: upLeft;
209+
}
210+
pixels[row + x] = (raw[line + x] + predicted) & 0xff;
211+
}
212+
}
213+
let minX = Infinity;
214+
let minY = Infinity;
215+
let maxX = -Infinity;
216+
let maxY = -Infinity;
217+
let found = 0;
218+
for (let y = 0; y < height; y += 1) {
219+
for (let x = 0; x < width; x += 1) {
220+
const o = y * stride + x * channels;
221+
if (
222+
Math.abs(pixels[o] - rgb[0]) <= tolerance &&
223+
Math.abs(pixels[o + 1] - rgb[1]) <= tolerance &&
224+
Math.abs(pixels[o + 2] - rgb[2]) <= tolerance
225+
) {
226+
found += 1;
227+
minX = Math.min(minX, x);
228+
maxX = Math.max(maxX, x);
229+
minY = Math.min(minY, y);
230+
maxY = Math.max(maxY, y);
231+
}
232+
}
233+
}
234+
const scale = width / frame.viewportWidth;
235+
return {
236+
t: frame.t,
237+
box:
238+
found === 0
239+
? null
240+
: {
241+
x: minX / scale,
242+
y: minY / scale,
243+
width: (maxX - minX + 1) / scale,
244+
height: (maxY - minY + 1) / scale,
245+
},
246+
pixels: found,
247+
};
248+
}
249+
142250
/**
143251
* Waits until the screencast has presented the marked element moving: a new
144-
* position on `frames` consecutive cast frames (default 2), read from the
145-
* live cast as its frames arrive. Block the main thread after it, not after
146-
* the document timeline alone: in WebKit a just-started animation runs on the
147-
* timeline before its first frame is presented, 20 to 300 ms later for a
148-
* layer that must first be drawn (a blur, a shadow, a sheet's first
149-
* release), and a block started in that gap judges a hold that is not the
150-
* component's. Throws past `timeout` ms.
252+
* position on `frames` consecutive cast frames (default 2) captured after the
253+
* call (the last frame before it counts as where the element stood), read
254+
* from the live cast as its frames arrive. Block the main thread after it,
255+
* not after the document timeline alone: in WebKit a just-started animation
256+
* runs on the timeline before its first frame is presented, 130 to 300 ms
257+
* later for a sheet's release, and a block started in that gap judges a hold
258+
* that is not the component's. PNG frames (Chromium's) are decoded in Node,
259+
* so the wait opens no page under a compositor trace; JPEG frames (WebKit's)
260+
* in one scratch page. Throws past `timeout` ms.
151261
*/
152262
export async function waitForPresentedMotion(
153263
page: Page,
@@ -163,35 +273,36 @@ export async function waitForPresentedMotion(
163273
} = {},
164274
): Promise<void> {
165275
const deadline = Date.now() + timeout;
166-
// One scratch page for the whole wait: each poll would otherwise open one.
167-
const scratch = await page.context().newPage();
276+
let scratch: Page | null = null;
168277
try {
169278
const seen: TrackedFrame[] = [];
170-
let read = 0;
279+
let read = Math.max(0, cast.framesSoFar().length - 1);
171280
for (;;) {
172-
const captured = cast.framesSoFar();
173-
if (captured.length > read) {
174-
seen.push(
175-
...(await trackMarkerIn(scratch, captured.slice(read), {rgb})),
176-
);
177-
read = captured.length;
178-
const ordered = inPresentationOrder(seen);
179-
let moved = 0;
180-
for (let i = ordered.length - 1; i > 0; i -= 1) {
181-
const now = ordered[i].box;
182-
const before = ordered[i - 1].box;
183-
if (
184-
now == null ||
185-
before == null ||
186-
(now.x === before.x && now.y === before.y)
187-
) {
188-
break;
189-
}
190-
moved += 1;
191-
}
192-
if (moved >= frames) {
193-
return;
194-
}
281+
const fresh = cast.framesSoFar().slice(read);
282+
read += fresh.length;
283+
const inNode = fresh.map(f =>
284+
f.format === 'png' ? markerInPng(f, rgb) : null,
285+
);
286+
const rest = fresh.filter((_, i) => inNode[i] == null);
287+
if (rest.length > 0) {
288+
scratch ??= await page.context().newPage();
289+
seen.push(...(await trackMarkerIn(scratch, rest, {rgb})));
290+
}
291+
seen.push(...inNode.filter(f => f != null));
292+
const ordered = inPresentationOrder(seen);
293+
let moved = 0;
294+
for (let i = 1; i < ordered.length && moved < frames; i += 1) {
295+
const now = ordered[i].box;
296+
const before = ordered[i - 1].box;
297+
moved =
298+
now != null &&
299+
before != null &&
300+
(now.x !== before.x || now.y !== before.y)
301+
? moved + 1
302+
: 0;
303+
}
304+
if (moved >= frames) {
305+
return;
195306
}
196307
if (Date.now() > deadline) {
197308
throw new Error(
@@ -201,7 +312,7 @@ export async function waitForPresentedMotion(
201312
await new Promise(resolve => setTimeout(resolve, 16));
202313
}
203314
} finally {
204-
await scratch.close();
315+
await scratch?.close();
205316
}
206317
}
207318

‎internal/interaction-harness/src/harness.interaction.browser.spec.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,9 @@ test.describe('frame evidence', () => {
605605
expect(proof.verdict.failures, proof.summary).toEqual([]);
606606
});
607607

608-
test('waitForPresentedMotion resolves once the cast shows the element moving, and names a cast that never does', async ({
608+
test('waitForPresentedMotion resolves once the cast shows the element moving, however long the cast ran before, and names a cast that never does', async ({
609609
page,
610+
browserName,
610611
}) => {
611612
await busyFixture(page);
612613
const box = page.locator('#box');
@@ -631,9 +632,34 @@ test.describe('frame evidence', () => {
631632
});
632633
await waitForPresentedMotion(page, cast);
633634
expect(Date.now()).toBeGreaterThan(startsAt);
634-
// Two consecutive moves after the start: three positions at least.
635-
const positions = (await trackMarker(page, await cast.stop()))
636-
.filter(f => f.t > startsAt && f.box != null)
635+
// A short move after a long-running cast: only frames from the call on
636+
// are read, and two moves anywhere in them count, so it resolves even
637+
// when the move has ended before it caught up. In Chromium its PNG frames
638+
// are decoded without opening a page, which a browser-wide compositor
639+
// trace would refuse.
640+
await box.evaluate(el => el.getAnimations().forEach(a => a.cancel()));
641+
await page.waitForTimeout(1500);
642+
let opened = 0;
643+
const onPage = () => {
644+
opened += 1;
645+
};
646+
page.context().on('page', onPage);
647+
await box.evaluate(el => {
648+
el.animate(
649+
[{transform: 'translateY(0px)'}, {transform: 'translateY(200px)'}],
650+
{duration: 400, easing: 'linear'},
651+
);
652+
});
653+
const shortStart = Date.now();
654+
await waitForPresentedMotion(page, cast);
655+
page.context().off('page', onPage);
656+
if (browserName === 'chromium') {
657+
expect(opened).toBe(0);
658+
}
659+
// Two consecutive moves after each start: three positions at least.
660+
const tracked = await trackMarker(page, await cast.stop());
661+
const positions = tracked
662+
.filter(f => f.t > startsAt && f.t < shortStart && f.box != null)
637663
.map(f => f.box?.y);
638664
expect(new Set(positions).size).toBeGreaterThanOrEqual(3);
639665
});

0 commit comments

Comments
 (0)