Skip to content

Commit 46ee9c5

Browse files
committed
test(interaction-harness): the control clock reads late, reordered frames at their own time
`controlClock` counted a turn at each direction flip in stamp order. A frame stamped more than a frame late lands after frames that it came before, and the flip that it fakes shifted every later reading by a leg, with no error. The clock now takes the cycle origin that most frames agree with, each within 20 ms of one of the two times that its position gives. 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, so the whole-leg throw goes. A frame within 36 px of a turn, or one that repeats the position before it, gets no reading. Core's animator spec reads its control the same way (motion/__tests__/controlClock.ts). The unit test adds frames stamped 20 to 50 ms late and put in presentation order, a gap of more than a leg, and a repeated position. The file header and the README name trackControl and controlClock.
1 parent 94a23bf commit 46ee9c5

3 files changed

Lines changed: 164 additions & 63 deletions

File tree

‎internal/interaction-harness/README.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ sheet's dim held for 155 to 225 ms into a block while the control moved, and
219219
inside the dialog it moves in step with the sheet. `waitForAnimation` and
220220
`blockMainThread`'s `during` never count the control's own animation, so on
221221
a dialog they still wait for the backdrop's. Track it from the
222-
same screencast (`trackMarker(page, frames, {rgb: CONTROL_RGB})`) and pass it
223-
as `control`: the component passes when it moved on every frame the control
222+
same screencast with `trackControl(page, frames)` (`trackMarker` with
223+
`CONTROL_RGB`, searching only the strip it crosses) and pass it as `control`: the component passes when it moved on every frame the control
224224
moved and its longest stall is no longer than the control's. Both are read from
225225
the same screencast frames, so a compositor animation misses none. A window in
226226
which the control moved on fewer than four frames fails, since the machine
@@ -245,6 +245,13 @@ is read from the value's changes in the judged part, so a position the page
245245
commits off the grid once the block ends does not shrink it.
246246
A main-thread animation still fails: the control moves and it does not.
247247

248+
`controlClock(control)` reads the compositor's own time in each frame off the
249+
control's position, for a proof that judges motion against time, not against
250+
the screencast's stamps. A loaded machine stamps frames late, and
251+
`inPresentationOrder` can then put a frame after frames that came after it.
252+
The clock takes the cycle origin that most frames agree with, so such a frame
253+
moves only its own reading. A frame within 36 px of a turn gets no reading.
254+
248255
An animation that lasts a fraction of a second, such as a 0.2 s fade, leaves a
249256
block only a few frames to judge: in WebKit, or on a loaded machine, the control
250257
can present fewer than four, and one run cannot decide. Play it several times

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

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* @file busy.clock.test.ts
55
* @input Synthetic control frames (no browser)
66
* @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.
7+
* time across turns and gaps, whatever the stamps say, also for frames
8+
* stamped late and put out of order. A frame near a turn, or one that
9+
* repeats the position before it, gets no reading.
910
* @position Unit test of ./busy's controlClock
1011
*/
1112

1213
import {describe, expect, it} from 'vitest';
1314
import {controlClock} from './busy';
14-
import type {TrackedFrame} from './frames';
15+
import {inPresentationOrder, type TrackedFrame} from './frames';
1516

1617
const FRAME = 1000 / 60;
1718

@@ -43,40 +44,95 @@ function frameTimes(from: number, to: number): number[] {
4344
return out;
4445
}
4546

47+
/** Each frame's animation time, by its stamp (frames as `control` made them). */
48+
function timesByStamp(
49+
times: readonly number[],
50+
frames: readonly TrackedFrame[],
51+
): Map<number, number> {
52+
return new Map(frames.map((frame, i) => [frame.t, times[i]]));
53+
}
54+
55+
/**
56+
* Expects every reading to equal its frame's animation time, both measured
57+
* from the earliest frame read, and returns the readings.
58+
*/
59+
function expectAnimationTimes(
60+
clock: Map<number, number>,
61+
times: Map<number, number>,
62+
): {ms: number; at: number}[] {
63+
const read = [...clock].map(([t, at]) => ({ms: times.get(t) ?? NaN, at}));
64+
read.sort((a, b) => a.ms - b.ms);
65+
for (const reading of read) {
66+
expect(reading.at - read[0].at).toBeCloseTo(reading.ms - read[0].ms, 6);
67+
}
68+
return read;
69+
}
70+
4671
describe('controlClock', () => {
4772
it('reads the animation time across turns, whatever the stamps say', () => {
4873
// Three turns, at 400, 800 and 1200 ms; every other frame stamped 12 ms
4974
// late, as a loaded machine presents them.
5075
const times = frameTimes(250, 1450);
5176
const frames = control(times, i => (i % 2) * 12);
5277
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);
78+
expectAnimationTimes(clock, timesByStamp(times, frames));
79+
// Every frame at least 36 px from both ends is read, and no other.
80+
for (const frame of frames) {
81+
const x = frame.box?.x ?? -1;
82+
expect(clock.has(frame.t)).toBe(x >= 36 && x <= 204);
6183
}
6284
});
6385

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]);
86+
it('gives no reading to a frame near a turn', () => {
87+
// The turn is at 400 ms. At 360 and 410 ms the box is 24 and 6 px from
88+
// the end, where the two times that its position gives are close.
89+
const frames = control([300, 360, 410, 470]);
6990
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,
91+
expect(clock.has(frames[1].t)).toBe(false);
92+
expect(clock.has(frames[2].t)).toBe(false);
93+
expect(clock.get(frames[3].t)! - clock.get(frames[0].t)!).toBeCloseTo(
94+
170,
7395
6,
7496
);
7597
});
7698

77-
it('throws when two readings are a whole leg apart', () => {
78-
expect(() => controlClock(control([100, 120, 540, 560]))).toThrow(
79-
/whole leg/,
99+
it('reads across a gap of a whole leg or more', () => {
100+
const times = [100, 120, 540, 560, 1300];
101+
const frames = control(times);
102+
const read = expectAnimationTimes(
103+
controlClock(frames),
104+
timesByStamp(times, frames),
105+
);
106+
expect(read).toHaveLength(times.length);
107+
});
108+
109+
it('reads late frames that inPresentationOrder put out of order at their own time', () => {
110+
// Frames one frame apart across the turn at 400 ms. Their stamps run 20
111+
// to 50 ms late in turn, so a frame can land after later frames.
112+
const times = frameTimes(50, 750);
113+
const late = [0, 45, 22, 0, 30, 50, 0, 20];
114+
const made = control(times, i => late[i % late.length]);
115+
const byStamp = timesByStamp(times, made);
116+
const frames = inPresentationOrder(made);
117+
// The stamps did reorder the frames.
118+
expect(
119+
frames.some(
120+
(frame, i) =>
121+
i > 0 && byStamp.get(frame.t)! < byStamp.get(frames[i - 1].t)!,
122+
),
123+
).toBe(true);
124+
const read = expectAnimationTimes(controlClock(frames), byStamp);
125+
expect(read.length).toBeGreaterThan(frames.length / 2);
126+
});
127+
128+
it('gives no reading to a frame that repeats the position before it', () => {
129+
// The second frame shows the first frame's position, stamped a frame later.
130+
const frames = control([100, 100, 150], i => (i === 1 ? FRAME : 0));
131+
const clock = controlClock(frames);
132+
expect(clock.has(frames[1].t)).toBe(false);
133+
expect(clock.get(frames[2].t)! - clock.get(frames[0].t)!).toBeCloseTo(
134+
50,
135+
6,
80136
);
81137
});
82138
});

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

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* read from its frames, `pooledBusyProof`, which judges several short
1212
* blocks as one, and `BusyWindowTooLateError`, for a block that began
1313
* after the motion it was meant to catch had ended.
14+
* `trackControl` and `controlClock` read the control's box and its own
15+
* clock from the screencast.
1416
* @position The busy-main-thread proof: an animation that runs on the
1517
* compositor keeps advancing at display rate while JavaScript holds the main
1618
* thread; one driven from the main thread (rAF, style writes, layout
@@ -188,55 +190,91 @@ export async function startBusyControl(
188190
};
189191
}
190192

193+
/** How far a frame's stamp may run late and still agree with the others, ms. */
194+
const CONTROL_AGREE_MS = 20;
195+
/**
196+
* Frames this near a turn get no reading. There the two times that a position
197+
* gives are under 120 ms apart, and a stamp 60 ms late could pick either.
198+
*/
199+
const CONTROL_EDGE = 36;
200+
191201
/**
192202
* 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.
203+
* tracked with `trackControl`), ms. The compositor samples every animation at
204+
* one time in a frame. So a component can be judged against this clock, not
205+
* the screencast's stamps. A loaded machine can stamp a frame late, and
206+
* `inPresentationOrder` then puts it after frames that it came before.
199207
*
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.
208+
* The box's position gives two times a cycle, on the way out and on the way
209+
* back. The cycle's origin is the one that the most frames agree with, each
210+
* within `CONTROL_AGREE_MS` of one of its two times. So no single frame sets
211+
* it. Each frame's stamp, measured from that origin, then picks its own time.
212+
* A late or reordered frame moves only its own reading, and a gap moves none.
213+
*
214+
* A frame without the box gets no reading, and so does a frame within
215+
* `CONTROL_EDGE` px of a turn. A frame that repeats the position before it
216+
* shows no new time, so it gets none either. Readings count up from the
217+
* origin a whole cycle or less before the first reading.
204218
*/
205219
export function controlClock(
206220
control: readonly TrackedFrame[],
207221
): 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;
222+
const cycle = 2 * CONTROL_LEG_MS;
223+
const usable = control.flatMap((frame, i) => {
224+
const x = frame.box?.x;
225+
const before = i > 0 ? control[i - 1].box?.x : undefined;
226+
if (
227+
x == null ||
228+
x < CONTROL_EDGE ||
229+
x > CONTROL_TRAVEL - CONTROL_EDGE ||
230+
(before != null && Math.abs(x - before) <= 0.25)
231+
) {
232+
return [];
226233
}
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-
);
234+
const out = (x / CONTROL_TRAVEL) * CONTROL_LEG_MS;
235+
return [{t: frame.t, out, back: cycle - out}];
236+
});
237+
const clock = new Map<number, number>();
238+
if (usable.length === 0) {
239+
return clock;
240+
}
241+
const onCycle = (a: number, b: number) => {
242+
const d = Math.abs(a - b) % cycle;
243+
return Math.min(d, cycle - d);
244+
};
245+
let origin = 0;
246+
let agreeing = -1;
247+
let spread = Number.POSITIVE_INFINITY;
248+
for (let candidate = 0; candidate < cycle; candidate += 1) {
249+
let count = 0;
250+
let sum = 0;
251+
for (const frame of usable) {
252+
const phase = frame.t - candidate;
253+
const d = Math.min(onCycle(phase, frame.out), onCycle(phase, frame.back));
254+
if (d <= CONTROL_AGREE_MS) {
255+
count += 1;
256+
sum += d;
257+
}
231258
}
232-
if (direction !== way) {
233-
leg = leg < 0 ? (direction > 0 ? 0 : 1) : leg + 1;
234-
way = direction;
259+
if (count > agreeing || (count === agreeing && sum < spread)) {
260+
origin = candidate;
261+
agreeing = count;
262+
spread = sum;
235263
}
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-
});
264+
}
265+
const first = Math.min(...usable.map(frame => frame.t));
266+
const anchored = origin + Math.floor((first - origin) / cycle) * cycle;
267+
for (const frame of usable) {
268+
const expected = frame.t - anchored;
269+
const nearest = (ms: number) =>
270+
ms + Math.round((expected - ms) / cycle) * cycle;
271+
const out = nearest(frame.out);
272+
const back = nearest(frame.back);
273+
clock.set(
274+
frame.t,
275+
Math.abs(out - expected) <= Math.abs(back - expected) ? out : back,
276+
);
277+
}
240278
return clock;
241279
}
242280

0 commit comments

Comments
 (0)