Skip to content

Commit 94a23bf

Browse files
committed
test(interaction-harness): a busy-window control clock and a marker search in a region
A busy-main-thread proof reads the screencast. On a loaded machine the compositor presents fewer frames and the screencast stamps some late. So a proof that times its frames by their stamps fails with nothing wrong in the component. The control animation's own position now gives the time instead: `controlClock` reads it across the control's turns, and `trackControl` finds the control in its own strip. `trackMarker` takes an optional region, so a proof decodes only the strip it reads.
1 parent ccfadbe commit 94a23bf

3 files changed

Lines changed: 209 additions & 15 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file busy.clock.test.ts
5+
* @input Synthetic control frames (no browser)
6+
* @output The control's clock. Its readings follow the control's animation
7+
* time across turns, whatever the stamps say. A frame beside a turn gets no
8+
* reading. A gap of a whole leg throws.
9+
* @position Unit test of ./busy's controlClock
10+
*/
11+
12+
import {describe, expect, it} from 'vitest';
13+
import {controlClock} from './busy';
14+
import type {TrackedFrame} from './frames';
15+
16+
const FRAME = 1000 / 60;
17+
18+
/** The control's x at animation time `ms`: 240 px one way every 400 ms. */
19+
function xAt(ms: number): number {
20+
const leg = Math.floor(ms / 400);
21+
const along = ((ms % 400) / 400) * 240;
22+
return leg % 2 === 0 ? along : 240 - along;
23+
}
24+
25+
/** One frame per animation time in `times`, stamped `late(i)` ms after it. */
26+
function control(
27+
times: readonly number[],
28+
late: (i: number) => number = () => 0,
29+
): TrackedFrame[] {
30+
return times.map((ms, i) => ({
31+
t: 5000 + ms + late(i),
32+
box: {x: xAt(ms), y: 0, width: 12, height: 12},
33+
pixels: 144,
34+
}));
35+
}
36+
37+
/** Animation times one frame apart, from `from` to `to`. */
38+
function frameTimes(from: number, to: number): number[] {
39+
const out: number[] = [];
40+
for (let ms = from; ms <= to; ms += FRAME) {
41+
out.push(ms);
42+
}
43+
return out;
44+
}
45+
46+
describe('controlClock', () => {
47+
it('reads the animation time across turns, whatever the stamps say', () => {
48+
// Three turns, at 400, 800 and 1200 ms; every other frame stamped 12 ms
49+
// late, as a loaded machine presents them.
50+
const times = frameTimes(250, 1450);
51+
const frames = control(times, i => (i % 2) * 12);
52+
const clock = controlClock(frames);
53+
const read = frames.flatMap((frame, i) => {
54+
const at = clock.get(frame.t);
55+
return at == null ? [] : [{ms: times[i], at}];
56+
});
57+
// Only the frames beside the three turns go unread.
58+
expect(read.length).toBeGreaterThanOrEqual(times.length - 6);
59+
for (const reading of read) {
60+
expect(reading.at - read[0].at).toBeCloseTo(reading.ms - read[0].ms, 6);
61+
}
62+
});
63+
64+
it('gives no reading to a frame beside a turn', () => {
65+
// The turn is at 400 ms. At 410 ms the box is 6 px back from the end. It
66+
// is ahead of the frame before and behind the frame after, so either leg
67+
// could hold it.
68+
const frames = control([340, 360, 380, 410, 430]);
69+
const clock = controlClock(frames);
70+
expect(clock.has(frames[3].t)).toBe(false);
71+
expect(clock.get(frames[4].t)! - clock.get(frames[2].t)!).toBeCloseTo(
72+
50,
73+
6,
74+
);
75+
});
76+
77+
it('throws when two readings are a whole leg apart', () => {
78+
expect(() => controlClock(control([100, 120, 540, 560]))).toThrow(
79+
/whole leg/,
80+
);
81+
});
82+
});

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

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,45 @@
2222

2323
import type {Locator, Page} from '@playwright/test';
2424
import {defineCurveEnd} from './curve-end';
25-
import {frameStats, type FrameStats, type TrackedFrame} from './frames';
25+
import {
26+
frameStats,
27+
trackMarker,
28+
type CastFrame,
29+
type FrameStats,
30+
type TrackedFrame,
31+
} from './frames';
2632

2733
/** The control's colour, apart from the component's marker (`MARKER_RGB`). */
2834
export const CONTROL_RGB: readonly [number, number, number] = [0, 255, 255];
2935

30-
/** The control box's side, and how far it travels along the top edge, CSS px. */
31-
const CONTROL_SIDE = 12;
36+
/**
37+
* The control box's side, CSS px: it crosses the page's top `CONTROL_SIDE` px,
38+
* where a proof reads nothing of the component.
39+
*/
40+
export const CONTROL_SIDE = 12;
41+
/** How far the control travels along the top edge, CSS px. */
3242
const CONTROL_TRAVEL = 240;
43+
/** How long the control takes to travel `CONTROL_TRAVEL` one way, ms. */
44+
const CONTROL_LEG_MS = 400;
45+
46+
/**
47+
* The control in each frame (`startBusyControl`'s box): `trackMarker` with
48+
* `CONTROL_RGB`, searching only the strip the box crosses.
49+
*/
50+
export async function trackControl(
51+
page: Page,
52+
frames: readonly CastFrame[],
53+
): Promise<TrackedFrame[]> {
54+
return trackMarker(page, frames, {
55+
rgb: CONTROL_RGB,
56+
region: {
57+
x: 0,
58+
y: 0,
59+
width: CONTROL_SIDE + CONTROL_TRAVEL + 4,
60+
height: CONTROL_SIDE + 4,
61+
},
62+
});
63+
}
3364

3465
/**
3566
* Starts a plain compositor-only control animation: a `CONTROL_RGB` box in the
@@ -70,7 +101,7 @@ export async function startBusyControl(
70101
);
71102
}
72103
await page.evaluate(
73-
({color, side, travel}) =>
104+
({color, side, travel, legMs}) =>
74105
new Promise<void>((resolve, reject) => {
75106
const host = document.querySelector('[data-harness-control-host]');
76107
host?.removeAttribute('data-harness-control-host');
@@ -90,7 +121,7 @@ export async function startBusyControl(
90121
{transform: `translateX(${travel}px)`},
91122
],
92123
{
93-
duration: 400,
124+
duration: legMs,
94125
iterations: Infinity,
95126
direction: 'alternate',
96127
easing: 'linear',
@@ -131,7 +162,12 @@ export async function startBusyControl(
131162
};
132163
frame(tick);
133164
}),
134-
{color: [...rgb], side: CONTROL_SIDE, travel: CONTROL_TRAVEL},
165+
{
166+
color: [...rgb],
167+
side: CONTROL_SIDE,
168+
travel: CONTROL_TRAVEL,
169+
legMs: CONTROL_LEG_MS,
170+
},
135171
);
136172
return {
137173
raise: () =>
@@ -152,6 +188,58 @@ export async function startBusyControl(
152188
};
153189
}
154190

191+
/**
192+
* The compositor's clock in each frame of `control` (`startBusyControl`'s box,
193+
* tracked with `CONTROL_RGB`), ms from the start of the first leg seen. The
194+
* box moves `CONTROL_TRAVEL` px every `CONTROL_LEG_MS`, back and forth, so its
195+
* position and direction say how far its animation has run. The compositor
196+
* samples every animation at one time in a frame. So a component can be
197+
* judged against this clock, not the screencast's stamps. A loaded machine
198+
* can set those a frame or more after the frame's own time.
199+
*
200+
* A frame beside a turn could be on either leg, so it gets no reading, and
201+
* neither does a frame without the box. Two readings a whole leg or more
202+
* apart could hide two turns, so that throws: the compositor presented too
203+
* little there to count the turns.
204+
*/
205+
export function controlClock(
206+
control: readonly TrackedFrame[],
207+
): Map<number, number> {
208+
const seen = control.flatMap(f =>
209+
f.box == null ? [] : [{t: f.t, x: f.box.x}],
210+
);
211+
const step = (from: number, to: number) =>
212+
Math.abs(to - from) > 0.25 ? Math.sign(to - from) : 0;
213+
const clock = new Map<number, number>();
214+
let leg = -1;
215+
let way = 0;
216+
let last: number | null = null;
217+
seen.forEach((frame, i) => {
218+
const back = i > 0 ? step(seen[i - 1].x, frame.x) : 0;
219+
const ahead = i + 1 < seen.length ? step(frame.x, seen[i + 1].x) : 0;
220+
if (back !== 0 && ahead !== 0 && back !== ahead) {
221+
return;
222+
}
223+
const direction = back || ahead;
224+
if (direction === 0) {
225+
return;
226+
}
227+
if (last != null && frame.t - last >= CONTROL_LEG_MS) {
228+
throw new Error(
229+
`controlClock: no reading for ${(frame.t - last).toFixed(0)} ms, a whole leg or more, so the control's turns cannot be counted`,
230+
);
231+
}
232+
if (direction !== way) {
233+
leg = leg < 0 ? (direction > 0 ? 0 : 1) : leg + 1;
234+
way = direction;
235+
}
236+
const along = direction > 0 ? frame.x : CONTROL_TRAVEL - frame.x;
237+
clock.set(frame.t, (leg + along / CONTROL_TRAVEL) * CONTROL_LEG_MS);
238+
last = frame.t;
239+
});
240+
return clock;
241+
}
242+
155243
/** What the control presented in the judged part (see `busyWindowProof`). */
156244
export interface BusyControlRead {
157245
/** Frames in the judged part that presented a new control position. */

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,21 @@ export function colorBlockPx(
353353
/**
354354
* Finds the marker in every frame. Decoding runs in a scratch page of the same
355355
* browser, so neither the page under test nor Node needs an image decoder.
356+
* With `region` (CSS px), only that part of each frame is searched. On a
357+
* loaded machine, a search of every pixel can take longer than the test.
356358
*/
357359
export async function trackMarker(
358360
page: Page,
359361
frames: readonly CastFrame[],
360362
{
361363
rgb = MARKER_RGB,
362364
tolerance = 40,
363-
}: {rgb?: readonly [number, number, number]; tolerance?: number} = {},
365+
region,
366+
}: {
367+
rgb?: readonly [number, number, number];
368+
tolerance?: number;
369+
region?: Box;
370+
} = {},
364371
): Promise<TrackedFrame[]> {
365372
const isDefaultMarker = rgb.every((value, i) => value === MARKER_RGB[i]);
366373
const scratch = await page.context().newPage();
@@ -370,7 +377,7 @@ export async function trackMarker(
370377
for (let i = 0; i < frames.length; i += 12) {
371378
const batch = frames.slice(i, i + 12);
372379
const boxes = await scratch.evaluate(
373-
async ({batch: items, color, tol}) => {
380+
async ({batch: items, color, tol, area}) => {
374381
const results: {
375382
box: {x: number; y: number; width: number; height: number} | null;
376383
pixels: number;
@@ -387,11 +394,28 @@ export async function trackMarker(
387394
throw new Error('no 2d context');
388395
}
389396
context.drawImage(bitmap, 0, 0);
397+
const scale = bitmap.width / item.viewportWidth;
398+
const left = area == null ? 0 : Math.floor(area.x * scale);
399+
const top = area == null ? 0 : Math.floor(area.y * scale);
400+
const right =
401+
area == null
402+
? bitmap.width
403+
: Math.min(
404+
bitmap.width,
405+
Math.ceil((area.x + area.width) * scale),
406+
);
407+
const bottom =
408+
area == null
409+
? bitmap.height
410+
: Math.min(
411+
bitmap.height,
412+
Math.ceil((area.y + area.height) * scale),
413+
);
390414
const {data, width, height} = context.getImageData(
391-
0,
392-
0,
393-
bitmap.width,
394-
bitmap.height,
415+
left,
416+
top,
417+
right - left,
418+
bottom - top,
395419
);
396420
let minX = Infinity;
397421
let minY = Infinity;
@@ -422,14 +446,13 @@ export async function trackMarker(
422446
}
423447
}
424448
}
425-
const scale = width / item.viewportWidth;
426449
results.push({
427450
box:
428451
pixels === 0
429452
? null
430453
: {
431-
x: minX / scale,
432-
y: minY / scale,
454+
x: (left + minX) / scale,
455+
y: (top + minY) / scale,
433456
width: (maxX - minX + 1) / scale,
434457
height: (maxY - minY + 1) / scale,
435458
},
@@ -447,6 +470,7 @@ export async function trackMarker(
447470
})),
448471
color: [...rgb],
449472
tol: tolerance,
473+
area: region ?? null,
450474
},
451475
);
452476
boxes.forEach((result, k) => {

0 commit comments

Comments
 (0)