Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(motion): the busy spring proof reads every frame, with a second control half a leg ahead (#10099)

Christopher Chedeauc33656a0695e
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index 7febeacde6..b2ac6e2d53 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -271,6 +271,13 @@ 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.
+A proof that needs every frame read starts a second control after the first,
+`startBusyControl(page, {ahead: true})`: an `AHEAD_CONTROL_RGB` box in the
+strip below, half a leg (200 ms) ahead. Wherever the first control is near a
+turn, the one ahead is mid-leg. Track it with `trackControl(page, frames,
+{ahead: true})` and read it with its own `controlClock`. `startBusyControl`
+resolves with each control's `start` on the page clock, where its own time
+is zero. Keep the component's marker out of the top 28 px then.
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
diff --git a/internal/interaction-harness/src/busy.ts b/internal/interaction-harness/src/busy.ts
index d025502b52..40a25d2a4b 100644
--- a/internal/interaction-harness/src/busy.ts
+++ b/internal/interaction-harness/src/busy.ts
@@ -12,7 +12,7 @@
* 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.
+ * clock from the screencast, a second control's too (`ahead`).
* @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
@@ -44,20 +44,33 @@ export const CONTROL_SIDE = 12;
const CONTROL_TRAVEL = 240;
/** How long the control takes to travel `CONTROL_TRAVEL` one way, ms. */
const CONTROL_LEG_MS = 400;
+/**
+ * The colour of the control ahead (`startBusyControl`'s `ahead`), apart from
+ * the first control's and the component's marker.
+ */
+export const AHEAD_CONTROL_RGB: readonly [number, number, number] = [
+ 128, 0, 255,
+];
+/** Where the strip of the control ahead starts, CSS px: below the first's. */
+const AHEAD_TOP = CONTROL_SIDE + 4;
+/** How far the control ahead runs ahead of the first, ms: half a leg. */
+const AHEAD_MS = CONTROL_LEG_MS / 2;
/**
* The control in each frame (`startBusyControl`'s box): `trackMarker` with
- * `CONTROL_RGB`, searching only the strip the box crosses.
+ * `CONTROL_RGB`, searching only the strip the box crosses. With `ahead`, the
+ * control ahead, with `AHEAD_CONTROL_RGB` in its own strip.
*/
export async function trackControl(
page: Page,
frames: readonly CastFrame[],
+ {ahead = false}: {ahead?: boolean} = {},
): Promise<TrackedFrame[]> {
return trackMarker(page, frames, {
- rgb: CONTROL_RGB,
+ rgb: ahead ? AHEAD_CONTROL_RGB : CONTROL_RGB,
region: {
x: 0,
- y: 0,
+ y: ahead ? AHEAD_TOP - 2 : 0,
width: CONTROL_SIDE + CONTROL_TRAVEL + 4,
height: CONTROL_SIDE + 4,
},
@@ -86,14 +99,32 @@ export async function trackControl(
* compositor; see `blockMainThread`). Keep the component's marker out of the
* strip it crosses (the top `CONTROL_SIDE` px, `CONTROL_SIDE +
* CONTROL_TRAVEL` px wide).
+ *
+ * It resolves with `start`, the animation's start on the page clock (epoch
+ * ms). The control's own time at a frame, past this start, is that frame's
+ * time on the page clock (`controlClock`).
+ *
+ * With `ahead`, it starts a second control after the first: the same box in
+ * `AHEAD_CONTROL_RGB`, in the strip below, half a leg (200 ms) ahead.
+ * `controlClock` reads no frame within `CONTROL_EDGE` px of a turn. Wherever
+ * the first control is there, the one ahead is mid-leg, so the two read
+ * every frame. Track it with `trackControl(page, frames, {ahead: true})`, and
+ * keep the marker out of its strip too (the top `2 * CONTROL_SIDE + 4` px).
*/
export async function startBusyControl(
page: Page,
{
- rgb = CONTROL_RGB,
+ ahead = false,
+ rgb = ahead ? AHEAD_CONTROL_RGB : CONTROL_RGB,
within,
- }: {rgb?: readonly [number, number, number]; within?: Locator} = {},
+ }: {
+ ahead?: boolean;
+ rgb?: readonly [number, number, number];
+ within?: Locator;
+ } = {},
): Promise<{
+ /** The control's start on the page clock, epoch ms. */
+ readonly start: number;
readonly raise: () => Promise<void>;
readonly remove: () => Promise<void>;
}> {
@@ -102,14 +133,14 @@ export async function startBusyControl(
host.setAttribute('data-harness-control-host', ''),
);
}
- await page.evaluate(
- ({color, side, travel, legMs}) =>
- new Promise<void>((resolve, reject) => {
+ const start = await page.evaluate(
+ ({color, side, travel, legMs, top, aheadMs}) =>
+ new Promise<number>((resolve, reject) => {
const host = document.querySelector('[data-harness-control-host]');
host?.removeAttribute('data-harness-control-host');
const box = document.createElement('div');
- box.setAttribute('data-harness-control', '');
- box.style.cssText = `position:fixed;inset:auto;left:0;top:0;margin:0;padding:0;border:0;overflow:visible;width:${side}px;height:${side}px;background:rgb(${color.join(',')});z-index:2147483647;pointer-events:none;`;
+ box.setAttribute('data-harness-control', aheadMs > 0 ? 'ahead' : '');
+ box.style.cssText = `position:fixed;inset:auto;left:0;top:${top}px;margin:0;padding:0;border:0;overflow:visible;width:${side}px;height:${side}px;background:rgb(${color.join(',')});z-index:2147483647;pointer-events:none;`;
if (host == null) {
box.setAttribute('popover', 'manual');
document.body.appendChild(box);
@@ -150,12 +181,38 @@ export async function startBusyControl(
);
return;
}
+ if (aheadMs > 0) {
+ // Half a leg ahead of the first control, on the same timeline.
+ const first = [
+ ...document.querySelectorAll('[data-harness-control=""]'),
+ ]
+ .map(el => el.getAnimations()[0])
+ .find(running => running?.startTime != null);
+ if (first == null) {
+ box.remove();
+ reject(
+ new Error(
+ 'startBusyControl: start the first control before the one ahead',
+ ),
+ );
+ return;
+ }
+ animation.startTime = Number(first.startTime) - aheadMs;
+ }
const frame = gate?.frame ?? window.requestAnimationFrame.bind(window);
const realNow = gate?.now ?? (() => performance.now());
const deadline = realNow() + 5_000;
+ // The one ahead starts running at once: give it a frame on the
+ // compositor too.
+ let ticks = 0;
const tick = () => {
- if (!animation.pending && Number(animation.currentTime ?? 0) >= 16) {
- resolve();
+ ticks += 1;
+ if (
+ !animation.pending &&
+ Number(animation.currentTime ?? 0) >= 16 &&
+ (aheadMs === 0 || ticks >= 2)
+ ) {
+ resolve(performance.timeOrigin + Number(animation.startTime));
} else if (realNow() > deadline) {
reject(new Error('startBusyControl: the control did not start'));
} else {
@@ -169,9 +226,12 @@ export async function startBusyControl(
side: CONTROL_SIDE,
travel: CONTROL_TRAVEL,
legMs: CONTROL_LEG_MS,
+ top: ahead ? AHEAD_TOP : 0,
+ aheadMs: ahead ? AHEAD_MS : 0,
},
);
return {
+ start,
raise: () =>
page.evaluate(() => {
for (const box of document.querySelectorAll<HTMLElement>(
@@ -212,7 +272,8 @@ const CONTROL_EDGE = 36;
* 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
+ * `CONTROL_EDGE` px of a turn. There a control started `ahead` is mid-leg:
+ * read it with its own clock. 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.
*/
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index 4d97e53f9c..189631b5d7 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -67,6 +67,8 @@ import {
countBetween,
cubicBezier,
CONTROL_RGB,
+ CONTROL_SIDE,
+ controlClock,
curveOf,
drag,
flick,
@@ -108,6 +110,7 @@ import {
TouchPageStallError,
touchStoryboard,
traceRendering,
+ trackControl,
trackMarker,
waitForAnimation,
releaseAnimationLog,
@@ -1061,6 +1064,39 @@ test.describe('frame evidence', () => {
await control.remove();
});
+ test('a control started ahead runs half a leg ahead of the first, in the strip below, and the two read every frame', async ({
+ page,
+ }, testInfo) => {
+ await busyFixture(page);
+ const first = await startBusyControl(page);
+ const ahead = await startBusyControl(page, {ahead: true});
+ // Half a leg ahead of the first, on the page clock.
+ expect(first.start - ahead.start).toBeCloseTo(200, 0);
+ const cast = await startScreencast(page);
+ // Five turns of the first control.
+ await page.waitForTimeout(2000);
+ const castFrames = await cast.stop();
+ const one = await trackControl(page, castFrames);
+ const two = await trackControl(page, castFrames, {ahead: true});
+ // Each box shows on every frame, the one ahead in the strip below.
+ expect(one.every(f => f.box != null)).toBe(true);
+ expect(two.every(f => f.box != null && f.box.y >= CONTROL_SIDE)).toBe(true);
+ // Where the first control reads nothing, within 36 px of a turn, the one
+ // ahead reads the frame.
+ const clocks = [controlClock(one), controlClock(two)];
+ const unread = castFrames.filter(f => clocks.every(c => !c.has(f.t)));
+ testInfo.annotations.push({
+ type: 'the controls read (frames)',
+ description: JSON.stringify({
+ frames: castFrames.length,
+ firstBlind: castFrames.filter(f => !clocks[0].has(f.t)).length,
+ unread: unread.length,
+ }),
+ });
+ expect(unread.map(f => f.t)).toEqual([]);
+ await first.remove();
+ });
+
test('a hold that starts before the block is judged from its first presented change inside it; a single change and then a freeze still fails', async ({
page,
}) => {
diff --git a/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts
index 021ff0d470..a6b2a362c6 100644
--- a/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts
+++ b/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts
@@ -33,6 +33,7 @@ import {
startBusyControl,
startScreencast,
traceRendering,
+ trackControl,
trackMarker,
} from '@astryxdesign/interaction-harness';
import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
@@ -131,16 +132,11 @@ async function blockedRun(
const box = page.locator('#box');
await markElement(box, {inset: MARKER_INSET});
// Before the animation, so it is on the compositor first; in the top
- // strip, clear of the box (top 40 px) and its marker.
- await startBusyControl(page);
- // The control's start on the page clock. Its own time at a frame, past
- // this start, is that frame's time on the page clock.
- const controlStart = await page.evaluate(() => {
- const animation = document
- .querySelector('[data-harness-control]')
- ?.getAnimations()[0];
- return performance.timeOrigin + Number(animation?.startTime ?? NaN);
- });
+ // strips, clear of the box (top 40 px) and its marker. The second runs half
+ // a leg ahead of the first. Each start is on the page clock: a control's
+ // own time at a frame, past it, is that frame's time on the page clock.
+ const {start: controlStart} = await startBusyControl(page);
+ const {start: secondStart} = await startBusyControl(page, {ahead: true});
const cast = await startScreencast(page);
// The curve starts when the browser commits it (handle.started).
const start = await page.evaluate(
@@ -160,17 +156,16 @@ async function blockedRun(
const frames = await cast.stop();
const tracked = await trackMarker(page, frames);
const control = await trackMarker(page, frames, {rgb: CONTROL_RGB});
- return {start, controlStart, busy, tracked, control};
+ const second = await trackControl(page, frames, {ahead: true});
+ return {start, controlStart, secondStart, busy, tracked, control, second};
}
test('a spring keeps advancing every frame through a 1 s main-thread block, on its curve', async ({
page,
browserName,
}) => {
- const {start, controlStart, busy, tracked, control} = await blockedRun(
- page,
- SLOW,
- );
+ const {start, controlStart, secondStart, busy, tracked, control, second} =
+ await blockedRun(page, SLOW);
// Judged against what the compositor presented in the same window, not a
// display-rate bound: a loaded runner's compositor skips frames for every
// animation alike, and the harness reads a slow tail inside the block at
@@ -191,24 +186,31 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i
// main-thread animation never does; see the next test).
expect(proof.verdict.failures, proof.summary).toEqual([]);
expect(proof.monotonic).toBe(true);
- // And it is the spring. The control moves 240 px out and back every 800
+ // And it is the spring. Each control moves 240 px out and back every 800
// ms, linearly, on the compositor. Its start plus its own time places each
// frame on the page clock (controlClock). The frame's stamp only picks the
- // whole cycle, so a late stamp moves no frame.
+ // whole cycle, so a late stamp moves no frame. controlClock reads no frame
+ // within 36 px of a turn, 120 ms of every 400. The second control is
+ // mid-leg there, half a leg ahead, so it reads the frames the first cannot.
const inside = tracked.filter(
f => f.box != null && f.t > busy.from + 20 && f.t < busy.to - 20,
);
- const clock = controlClock(control);
+ const clocks = [
+ {clock: controlClock(control), origin: controlStart},
+ {clock: controlClock(second), origin: secondStart},
+ ];
const timed = inside.flatMap(f => {
- const ms = clock.get(f.t);
- if (ms == null) {
+ const reader = clocks.find(({clock}) => clock.has(f.t));
+ if (reader == null) {
return [];
}
- const cycles = Math.round((f.t - controlStart - ms) / 800);
+ const ms = reader.clock.get(f.t) ?? NaN;
+ const cycles = Math.round((f.t - reader.origin - ms) / 800);
return [
{
x: (f.box?.x ?? NaN) - MARKER_INSET,
- at: controlStart + ms + cycles * 800 - start,
+ at: reader.origin + ms + cycles * 800 - start,
+ bySecond: reader !== clocks[0],
},
];
});
@@ -267,6 +269,7 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i
const [spring, faster] = judged;
const read = {
frames: timed.length,
+ bySecond: timed.filter(f => f.bySecond).length,
quarters: [...quarters].sort((p, q) => p - q),
worst: spring.worst,
faster: faster.worst,