Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions internal/interaction-harness/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,11 @@ entry: in WebKit, with the popover above BottomSheet's open modal dialog, the
sheet's dim held for 155 to 225 ms into a block while the control moved, and
inside the dialog it moves in step with the sheet. `waitForAnimation` and
`blockMainThread`'s `during` never count the control's own animation, so on
a dialog they still wait for the backdrop's. Track it from the
same screencast (`trackMarker(page, frames, {rgb: CONTROL_RGB})`) and pass it
as `control`: the component passes when it moved on every frame the control
a dialog they still wait for the backdrop's. Track a top-layer control from
the same screencast with `trackControl(page, frames)` (`trackMarker` with
`CONTROL_RGB`, searching only the top strip it crosses). A control mounted
`within` a dialog moves with it, so track that one with `trackMarker(page,
frames, {rgb: CONTROL_RGB})`. Pass either as `control`: the component passes when it moved on every frame the control
moved and its longest stall is no longer than the control's. Both are read from
the same screencast frames, so a compositor animation misses none. A window in
which the control moved on fewer than four frames fails, since the machine
Expand All @@ -245,6 +247,13 @@ is read from the value's changes in the judged part, so a position the page
commits off the grid once the block ends does not shrink it.
A main-thread animation still fails: the control moves and it does not.

`controlClock(control)` reads the compositor's own time in each frame off the
control's position, for a proof that judges motion against time, not against
the screencast's stamps. A loaded machine stamps frames late, and
`inPresentationOrder` can then put a frame after frames that came after it.
The clock takes the cycle origin that most frames agree with, so such a frame
moves only its own reading. A frame within 36 px of a turn gets no reading.

An animation that lasts a fraction of a second, such as a 0.2 s fade, leaves a
block only a few frames to judge: in WebKit, or on a loaded machine, the control
can present fewer than four, and one run cannot decide. Play it several times
Expand Down
161 changes: 161 additions & 0 deletions internal/interaction-harness/src/busy.clock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file busy.clock.test.ts
* @input Synthetic control frames (no browser)
* @output The control's clock. Its readings follow the control's animation
* time across turns and gaps, whatever the stamps say, also for frames
* stamped late and put out of order. A frame near a turn, or one that
* repeats the position before it, gets no reading. Includes the cases of
* the animator spec's former clock.
* @position Unit test of ./busy's controlClock
*/

import {describe, expect, it} from 'vitest';
import {controlClock} from './busy';
import {inPresentationOrder, type TrackedFrame} from './frames';

const FRAME = 1000 / 60;

/** The control's x at animation time `ms`: 240 px one way every 400 ms. */
function xAt(ms: number): number {
const leg = Math.floor(ms / 400);
const along = ((ms % 400) / 400) * 240;
return leg % 2 === 0 ? along : 240 - along;
}

/** One frame per animation time in `times`, stamped `late(i)` ms after it. */
function control(
times: readonly number[],
late: (i: number) => number = () => 0,
): TrackedFrame[] {
return times.map((ms, i) => ({
t: 5000 + ms + late(i),
box: {x: xAt(ms), y: 0, width: 12, height: 12},
pixels: 144,
}));
}

/** Animation times one frame apart, from `from` to `to`. */
function frameTimes(from: number, to: number): number[] {
const out: number[] = [];
for (let ms = from; ms <= to; ms += FRAME) {
out.push(ms);
}
return out;
}

/** Each frame's animation time, by its stamp (frames as `control` made them). */
function timesByStamp(
times: readonly number[],
frames: readonly TrackedFrame[],
): Map<number, number> {
return new Map(frames.map((frame, i) => [frame.t, times[i]]));
}

/**
* Expects every reading to equal its frame's animation time, both measured
* from the earliest frame read, and returns the readings.
*/
function expectAnimationTimes(
clock: Map<number, number>,
times: Map<number, number>,
): {ms: number; at: number}[] {
const read = [...clock].map(([t, at]) => ({ms: times.get(t) ?? NaN, at}));
read.sort((a, b) => a.ms - b.ms);
for (const reading of read) {
expect(reading.at - read[0].at).toBeCloseTo(reading.ms - read[0].ms, 6);
}
return read;
}

describe('controlClock', () => {
it('reads the animation time across turns, whatever the stamps say', () => {
// Three turns, at 400, 800 and 1200 ms; every other frame stamped 12 ms
// late, as a loaded machine presents them.
const times = frameTimes(250, 1450);
const frames = control(times, i => (i % 2) * 12);
Comment thread
vjeux marked this conversation as resolved.
Resolved
const clock = controlClock(frames);
expectAnimationTimes(clock, timesByStamp(times, frames));
// Every frame at least 36 px from both ends is read, and no other.
for (const frame of frames) {
const x = frame.box?.x ?? -1;
expect(clock.has(frame.t)).toBe(x >= 36 && x <= 204);
}
});

it('gives no reading to a frame near a turn', () => {
// The turn is at 400 ms. At 360 and 410 ms the box is 24 and 6 px from
// the end, where the two times that its position gives are close.
const frames = control([300, 360, 410, 470]);
const clock = controlClock(frames);
expect(clock.has(frames[1].t)).toBe(false);
expect(clock.has(frames[2].t)).toBe(false);
expect(clock.get(frames[3].t)! - clock.get(frames[0].t)!).toBeCloseTo(
170,
6,
);
});

it('reads across a gap of a whole leg or more', () => {
const times = [100, 120, 540, 560, 1300];
const frames = control(times);
const read = expectAnimationTimes(
controlClock(frames),
timesByStamp(times, frames),
);
expect(read).toHaveLength(times.length);
});

it('reads late frames that inPresentationOrder put out of order at their own time', () => {
// Frames one frame apart across the turn at 400 ms. Their stamps run 20
// to 50 ms late in turn, so a frame can land after later frames.
const times = frameTimes(50, 750);
const late = [0, 45, 22, 0, 30, 50, 0, 20];
const made = control(times, i => late[i % late.length]);
const byStamp = timesByStamp(times, made);
const frames = inPresentationOrder(made);
// The stamps did reorder the frames.
expect(
frames.some(
(frame, i) =>
i > 0 && byStamp.get(frame.t)! < byStamp.get(frames[i - 1].t)!,
),
).toBe(true);
const read = expectAnimationTimes(controlClock(frames), byStamp);
expect(read.length).toBeGreaterThan(frames.length / 2);
});

it('reads the first usable frame and the next one right when a turn lies between them', () => {
// At 338.3 ms the box is at 203 px on its way out, just inside the band.
// The frames after it sit within 36 px of the end until 466.3 ms. There
// the next usable frame is at 200.2 px, on its way back.
const times = Array.from({length: 60}, (_, i) => 338 + 1 / 3 + i * 16);
const frames = control(times);
const clock = controlClock(frames);
expect(clock.has(frames[0].t)).toBe(true);
expect(frames.slice(1, 8).some(frame => clock.has(frame.t))).toBe(false);
expect(clock.has(frames[8].t)).toBe(true);
expectAnimationTimes(clock, timesByStamp(times, frames));
});

it('reads every frame at its own time when the first frame is stamped 50 ms late', () => {
// The late first frame does not set the origin, so it moves no reading.
const times = Array.from({length: 60}, (_, i) => 100 + i * 16);
const frames = control(times, i => (i === 0 ? 50 : 0));
const clock = controlClock(frames);
expect(clock.has(frames[0].t)).toBe(true);
expectAnimationTimes(clock, timesByStamp(times, frames));
});

it('gives no reading to a frame that repeats the position before it', () => {
// The second frame shows the first frame's position, stamped a frame later.
const frames = control([100, 100, 150], i => (i === 1 ? FRAME : 0));
const clock = controlClock(frames);
expect(clock.has(frames[1].t)).toBe(false);
expect(clock.get(frames[2].t)! - clock.get(frames[0].t)!).toBeCloseTo(
50,
6,
);
});
});
138 changes: 132 additions & 6 deletions internal/interaction-harness/src/busy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* read from its frames, `pooledBusyProof`, which judges several short
* blocks as one, and `BusyWindowTooLateError`, for a block that began
* after the motion it was meant to catch had ended.
* `trackControl` and `controlClock` read the control's box and its own
* clock from the screencast.
* @position The busy-main-thread proof: an animation that runs on the
* compositor keeps advancing at display rate while JavaScript holds the main
* thread; one driven from the main thread (rAF, style writes, layout
Expand All @@ -22,14 +24,45 @@

import type {Locator, Page} from '@playwright/test';
import {defineCurveEnd} from './curve-end';
import {frameStats, type FrameStats, type TrackedFrame} from './frames';
import {
frameStats,
trackMarker,
type CastFrame,
type FrameStats,
type TrackedFrame,
} from './frames';

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

/** The control box's side, and how far it travels along the top edge, CSS px. */
const CONTROL_SIDE = 12;
/**
* The control box's side, CSS px: it crosses the page's top `CONTROL_SIDE` px,
* where a proof reads nothing of the component.
*/
export const CONTROL_SIDE = 12;
/** How far the control travels along the top edge, CSS px. */
const CONTROL_TRAVEL = 240;
/** How long the control takes to travel `CONTROL_TRAVEL` one way, ms. */
const CONTROL_LEG_MS = 400;

/**
* The control in each frame (`startBusyControl`'s box): `trackMarker` with
* `CONTROL_RGB`, searching only the strip the box crosses.
*/
export async function trackControl(
page: Page,
frames: readonly CastFrame[],
): Promise<TrackedFrame[]> {
return trackMarker(page, frames, {
rgb: CONTROL_RGB,
region: {
x: 0,
y: 0,
width: CONTROL_SIDE + CONTROL_TRAVEL + 4,
height: CONTROL_SIDE + 4,
},
});
}

/**
* Starts a plain compositor-only control animation: a `CONTROL_RGB` box in the
Expand Down Expand Up @@ -70,7 +103,7 @@ export async function startBusyControl(
);
}
await page.evaluate(
({color, side, travel}) =>
({color, side, travel, legMs}) =>
new Promise<void>((resolve, reject) => {
const host = document.querySelector('[data-harness-control-host]');
host?.removeAttribute('data-harness-control-host');
Expand All @@ -90,7 +123,7 @@ export async function startBusyControl(
{transform: `translateX(${travel}px)`},
],
{
duration: 400,
duration: legMs,
iterations: Infinity,
direction: 'alternate',
easing: 'linear',
Expand Down Expand Up @@ -131,7 +164,12 @@ export async function startBusyControl(
};
frame(tick);
}),
{color: [...rgb], side: CONTROL_SIDE, travel: CONTROL_TRAVEL},
{
color: [...rgb],
side: CONTROL_SIDE,
travel: CONTROL_TRAVEL,
legMs: CONTROL_LEG_MS,
},
);
return {
raise: () =>
Expand All @@ -152,6 +190,94 @@ export async function startBusyControl(
};
}

/** How far a frame's stamp may run late and still agree with the others, ms. */
const CONTROL_AGREE_MS = 20;
/**
* Frames this near a turn get no reading. There the two times that a position
* gives are under 120 ms apart, and a stamp 60 ms late could pick either.
*/
const CONTROL_EDGE = 36;

/**
* The compositor's clock in each frame of `control` (`startBusyControl`'s box,
* tracked with `trackControl`), ms. The compositor samples every animation at
* one time in a frame. So a component can be judged against this clock, not
* the screencast's stamps. A loaded machine can stamp a frame late, and
* `inPresentationOrder` then puts it after frames that it came before.
*
* The box's position gives two times a cycle, on the way out and on the way
* back. The cycle's origin is the one that the most frames agree with, each
* within `CONTROL_AGREE_MS` of one of its two times. So no single frame sets
* it. Each frame's stamp, measured from that origin, then picks its own time.
* A late or reordered frame moves only its own reading, and a gap moves none.
*
* A frame without the box gets no reading, and so does a frame within
* `CONTROL_EDGE` px of a turn. A frame that repeats the position before it
* shows no new time, so it gets none either. Readings count up from the
* origin a whole cycle or less before the first reading.
*/
export function controlClock(
Comment thread
vjeux marked this conversation as resolved.
Resolved
control: readonly TrackedFrame[],
): Map<number, number> {
const cycle = 2 * CONTROL_LEG_MS;
const usable = control.flatMap((frame, i) => {
const x = frame.box?.x;
const before = i > 0 ? control[i - 1].box?.x : undefined;
if (
x == null ||
x < CONTROL_EDGE ||
x > CONTROL_TRAVEL - CONTROL_EDGE ||
(before != null && Math.abs(x - before) <= 0.25)
) {
return [];
}
const out = (x / CONTROL_TRAVEL) * CONTROL_LEG_MS;
return [{t: frame.t, out, back: cycle - out}];
});
const clock = new Map<number, number>();
if (usable.length === 0) {
return clock;
}
const onCycle = (a: number, b: number) => {
const d = Math.abs(a - b) % cycle;
return Math.min(d, cycle - d);
};
let origin = 0;
let agreeing = -1;
let spread = Number.POSITIVE_INFINITY;
for (let candidate = 0; candidate < cycle; candidate += 1) {
let count = 0;
let sum = 0;
for (const frame of usable) {
const phase = frame.t - candidate;
const d = Math.min(onCycle(phase, frame.out), onCycle(phase, frame.back));
if (d <= CONTROL_AGREE_MS) {
count += 1;
sum += d;
}
}
if (count > agreeing || (count === agreeing && sum < spread)) {
origin = candidate;
agreeing = count;
spread = sum;
}
}
const first = Math.min(...usable.map(frame => frame.t));
const anchored = origin + Math.floor((first - origin) / cycle) * cycle;
for (const frame of usable) {
const expected = frame.t - anchored;
const nearest = (ms: number) =>
ms + Math.round((expected - ms) / cycle) * cycle;
const out = nearest(frame.out);
const back = nearest(frame.back);
clock.set(
frame.t,
Math.abs(out - expected) <= Math.abs(back - expected) ? out : back,
);
}
return clock;
}

/** What the control presented in the judged part (see `busyWindowProof`). */
export interface BusyControlRead {
/** Frames in the judged part that presented a new control position. */
Expand Down
Loading
Loading