Skip to content

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

TypeScript

test(ProgressBar, CircularProgress): motion traces measure what they claim on a loaded runner (#9413)

Christopher Chedeau30c3fdf264a3
diff --git a/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts b/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts
index 13e6867812..67e026a160 100644
--- a/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts
+++ b/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts
@@ -2,12 +2,15 @@
/**
* @file ProgressBarMotion.a11y.chromium.spec.ts
- * @input Built ProgressBar stories in real Chromium (CDP screencast, tracing)
+ * @input Built ProgressBar stories in real Chromium (CDP screencast, tracing,
+ * the interaction harness's animation holds, storyboards and busy-window
+ * control)
* @output Browser evidence that a determinate value change moves the fill on
* the compositor: it keeps moving while a script holds the main thread,
* lays out and paints nothing per frame, and its leading edge lands on the
* value (mirrored in RTL). Writes a storyboard (contact sheet + JSON trace)
- * to test-results/progress-bar-motion/.
+ * to test-results/progress-bar-motion/, the change held and seeked frame by
+ * frame.
* @position Chromium proof for ProgressBar's sliding fill; the iOS Safari side
* of the same proof is a simulator recording attached to the pull request.
*
@@ -22,7 +25,17 @@ import {
serveStorybook,
type StaticServer,
} from '@astryxdesign/a11y-spec/storybook';
-import {inPresentationOrder} from '@astryxdesign/interaction-harness';
+import {
+ animationStoryboard,
+ armHold,
+ busyWindowProof,
+ CONTROL_RGB,
+ inPresentationOrder,
+ releaseAnimations,
+ startBusyControl,
+ trackMarker,
+ type TrackedFrame,
+} from '@astryxdesign/interaction-harness';
let storybook: StaticServer;
test.beforeAll(async () => {
@@ -81,12 +94,13 @@ async function setValue(page: Page, value: number) {
* Sets a new value and, once the fill's transition has started and one more
* frame has been presented, holds the main thread: the pattern of a progress
* update followed by the work it reports on. Returns the block's start and
- * end (epoch ms).
+ * end (epoch ms), and where the fill's leading edge was as the block started
+ * (CSS px from the track's start, read on the page).
*/
async function setValueThenBlock(page: Page, value: number, ms: number) {
return page.evaluate(
async ({storyId, updatedArgs, duration}) =>
- new Promise<{from: number; to: number}>(resolve => {
+ new Promise<{from: number; to: number; startEdge: number}>(resolve => {
const channel = (
window as unknown as {
__STORYBOOK_ADDONS_CHANNEL__: {
@@ -103,12 +117,20 @@ async function setValueThenBlock(page: Page, value: number, ms: number) {
document.removeEventListener('transitionstart', onStart, true);
requestAnimationFrame(() =>
setTimeout(() => {
+ const fill = bar.firstElementChild as Element;
+ const startEdge =
+ fill.getBoundingClientRect().right -
+ bar.getBoundingClientRect().left;
const from = performance.timeOrigin + performance.now();
const end = performance.now() + duration;
while (performance.now() < end) {
// hold
}
- resolve({from, to: performance.timeOrigin + performance.now()});
+ resolve({
+ from,
+ to: performance.timeOrigin + performance.now(),
+ startEdge,
+ });
}),
);
};
@@ -274,29 +296,72 @@ test('the fill keeps moving to a new value while a script holds the main thread'
const track = await trackBox(page);
const cast = await startScreencast(page);
await page.waitForTimeout(300);
+ // The harness's busy-window control, through the block: a box the
+ // compositor moves along the top edge, above the track, in the same
+ // screencast.
+ const control = await startBusyControl(page);
const busy = await setValueThenBlock(page, 90, BLOCK_MS);
+ // Gone once the block ends, so the page is still again when the
+ // screencast stops.
+ await control.remove();
await page.waitForTimeout(300);
const frames = await cast.stop();
const edges = await leadingEdges(page, frames, track);
const start = track.width * 0.1;
const target = track.width * 0.9;
+ // The main thread reached the block before the fill arrived: the one thing
+ // this test cannot arrange, since the page can block only after the frame
+ // that hands the transition to the compositor, and a loaded machine can
+ // keep the main thread from the block for most of the move. Read on the
+ // page as the block starts; the fill still had a step of the cast to go.
+ expect(busy.startEdge).toBeGreaterThanOrEqual(start);
+ expect(busy.startEdge).toBeLessThan(target - 1 / DPR);
// Inside the block, a frame clear of each edge.
const inside = frames
.map((frame, i) => ({t: frame.t, edge: edges[i]}))
.filter(f => f.t >= busy.from + 20 && f.t <= busy.to - 20);
- // The transition (the theme's `--duration-medium`) is shorter than the
- // block, so the fill must make its whole trip inside it: frame after frame
- // through distinct positions…
- const positions = new Set(inside.map(f => Math.round(f.edge)));
- expect(positions.size).toBeGreaterThanOrEqual(5);
- // …and arrives before the block ends (the transition is shorter than it).
+ // The fill moves through the block: its leading edge shows a new position
+ // on every frame the control moved on, and stalls no longer than the
+ // control, until it is within half a pixel of its end (the harness's busy
+ // proof: the screencast's capture rate thins both alike). On the change's
+ // own clock each half-pixel step of the reading takes 10 ms or less until
+ // the last, which takes 20 ms, longer than a frame, so two frames can read
+ // it while the fill still moves.
+ const controlFrames = await trackMarker(
+ page,
+ frames
+ .filter(f => f.t >= busy.from - 250 && f.t <= busy.to)
+ .map(f => ({
+ t: f.t,
+ data: f.data,
+ format: 'png' as const,
+ viewportWidth: page.viewportSize()?.width ?? 402,
+ viewportHeight: page.viewportSize()?.height ?? 200,
+ })),
+ {rgb: CONTROL_RGB},
+ );
+ const fill: TrackedFrame[] = frames.map((f, i) => ({
+ t: f.t,
+ box: {x: edges[i], y: 0, width: 1, height: 1},
+ pixels: 1,
+ }));
+ const proof = busyWindowProof(fill, busy, {
+ axis: 'x',
+ settledWithin: 1 / DPR,
+ settledAt: target,
+ control: controlFrames,
+ });
+ test
+ .info()
+ .annotations.push({type: 'busy window', description: proof.summary});
+ expect(proof.verdict.failures, proof.summary).toEqual([]);
+ // It arrives before the block ends (the transition is shorter than it)…
const last = inside[inside.length - 1].edge;
expect(Math.abs(last - target)).toBeLessThan(2);
- // It never runs backwards.
+ // …and never runs backwards.
for (let i = 1; i < inside.length; i += 1) {
expect(inside[i].edge).toBeGreaterThanOrEqual(inside[i - 1].edge - 1);
}
- expect(inside[0].edge).toBeLessThan(target - (target - start) * 0.2);
});
test('moving the fill lays out and paints nothing per frame', async ({
@@ -735,12 +800,22 @@ test('storyboard: the fill moving from 20% to 80% @storyboard', async ({
}, testInfo) => {
await open(page, 20);
const track = await trackBox(page);
- const cast = await startScreencast(page);
- await page.waitForTimeout(200);
+ // The change's own frames, on its own clock: the page holds the fill's
+ // transition as soon as it runs, and the harness films it, seeked one
+ // frame interval at a time through its end. A screencast of the change
+ // shows only the frames a loaded runner gets to capture, and one it misses
+ // doubles the step between the two around it.
+ const hold = await armHold(page, page.getByRole('progressbar'));
await setValue(page, 80);
- // Recorded until the change has finished, however long the machine takes.
- await settle(page);
- const frames = await cast.stop();
+ const {storyboard} = await animationStoryboard(page, {
+ name: 'progress-bar-fill-move',
+ held: await hold.held(),
+ });
+ await releaseAnimations(page, {finish: true});
+ const frames: Frame[] = storyboard.frames.map(f => ({
+ t: f.t,
+ data: f.png.toString('base64'),
+ }));
const edges = await leadingEdges(page, frames, track);
// The trace is the assertion: the edge moves forward only, from 20% to
// 80%, with no jump bigger than a quarter of the travel between frames.
diff --git a/packages/lab/src/CircularProgress/__tests__/CircularProgressMotion.a11y.chromium.spec.ts b/packages/lab/src/CircularProgress/__tests__/CircularProgressMotion.a11y.chromium.spec.ts
index e9778b98fd..e3e5cb6775 100644
--- a/packages/lab/src/CircularProgress/__tests__/CircularProgressMotion.a11y.chromium.spec.ts
+++ b/packages/lab/src/CircularProgress/__tests__/CircularProgressMotion.a11y.chromium.spec.ts
@@ -3,7 +3,8 @@
/**
* @file CircularProgressMotion.a11y.chromium.spec.ts
* @input Built CircularProgress stories in real Chromium (Web Animations
- * seeking, CDP screencast, tracing)
+ * seeking, CDP screencast, tracing), the interaction harness's busy-window
+ * control and proof
* @output Browser evidence that a determinate value change keeps moving while
* a script holds the main thread and paints nothing per frame, that the
* ring at rest draws what the stroked circle drew, and that the arc stays
@@ -24,7 +25,14 @@ import {
serveStorybook,
type StaticServer,
} from '@astryxdesign/a11y-spec/storybook';
-import {inPresentationOrder} from '@astryxdesign/interaction-harness';
+import {
+ busyWindowProof,
+ CONTROL_RGB,
+ inPresentationOrder,
+ startBusyControl,
+ trackMarker,
+ type TrackedFrame,
+} from '@astryxdesign/interaction-harness';
let storybook: StaticServer;
test.beforeAll(async () => {
@@ -152,35 +160,59 @@ async function colors(page: Page) {
/**
* For each degree clockwise from 12 o'clock, whether the ring's centerline is
- * drawn in the fill color in the given screenshot (PNGs of the ring box).
+ * drawn in the fill color in the given screenshot: PNGs of the ring box, or,
+ * with `frame`, PNGs of the whole viewport (`viewportWidth` CSS px wide) read
+ * at the ring box, the same pixels a crop to that box would hold.
*/
async function inkByDegree(
page: Page,
shots: string[],
palette: {fill: number[]; track: number[]},
+ frame?: {ring: Box; viewportWidth: number},
): Promise<boolean[][]> {
const scratch = await page.context().newPage();
try {
return await scratch.evaluate(
- async ({items, fill, track, diameter, stroke}) => {
+ async ({items, fill, track, diameter, stroke, region}) => {
const out: boolean[][] = [];
for (const item of items) {
const bitmap = await createImageBitmap(
await (await fetch(`data:image/png;base64,${item}`)).blob(),
);
- const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
+ // The ring box in the shot's pixels: the whole shot, or the box's
+ // pixels in a viewport frame.
+ const frameScale =
+ region == null ? 1 : bitmap.width / region.viewportWidth;
+ const x0 =
+ region == null ? 0 : Math.round(region.ring.x * frameScale);
+ const y0 =
+ region == null ? 0 : Math.round(region.ring.y * frameScale);
+ const boxWidth =
+ region == null
+ ? bitmap.width
+ : Math.round(region.ring.width * frameScale);
+ const boxHeight =
+ region == null
+ ? bitmap.height
+ : Math.round(region.ring.height * frameScale);
+ const canvas = new OffscreenCanvas(boxWidth, boxHeight);
const context = canvas.getContext('2d');
if (context == null) {
throw new Error('no 2d context');
}
- context.drawImage(bitmap, 0, 0);
- const {data, width} = context.getImageData(
+ context.drawImage(
+ bitmap,
+ x0,
+ y0,
+ boxWidth,
+ boxHeight,
0,
0,
- bitmap.width,
- bitmap.height,
+ boxWidth,
+ boxHeight,
);
- const scale = bitmap.width / diameter;
+ const {data, width} = context.getImageData(0, 0, boxWidth, boxHeight);
+ const scale = boxWidth / diameter;
const r = ((diameter - stroke) / 2) * scale;
const c = (diameter / 2) * scale;
const d = (a: number[], b: number[]) =>
@@ -200,7 +232,13 @@ async function inkByDegree(
}
return out;
},
- {items: shots, ...palette, diameter: DIAMETER, stroke: STROKE},
+ {
+ items: shots,
+ ...palette,
+ diameter: DIAMETER,
+ stroke: STROKE,
+ region: frame ?? null,
+ },
);
} finally {
await scratch.close();
@@ -263,6 +301,10 @@ test('a value change keeps moving while a script holds the main thread', async (
everyNthFrame: 1,
});
await page.waitForTimeout(300);
+ // The harness's busy-window control, through the block: a box the
+ // compositor moves along the top edge, clear of the ring, in the same
+ // screencast.
+ const control = await startBusyControl(page);
// A value change, one presented frame, then 700 ms of script.
const busy = await page.evaluate(
async ({storyId}) =>
@@ -298,6 +340,9 @@ test('a value change keeps moving while a script holds the main thread', async (
}),
{storyId: STORY},
);
+ // Gone once the block ends, so the page is still again when the
+ // screencast stops.
+ await control.remove();
await page.waitForTimeout(300);
await cdp.send('Page.stopScreencast');
await Promise.all(acks);
@@ -309,61 +354,54 @@ test('a value change keeps moving while a script holds the main thread', async (
// that shows the arc behind an earlier one still has its own later
// timestamp, so it stays in place.
const presented = inPresentationOrder(raw);
- // Crop each frame to the ring, then read how far the arc reaches.
- const scratch = await page.context().newPage();
- let crops: string[];
- try {
- crops = await scratch.evaluate(
- async ({items, ring}) => {
- const out: string[] = [];
- for (const item of items) {
- const bitmap = await createImageBitmap(
- await (await fetch(`data:image/png;base64,${item}`)).blob(),
- );
- const scale = bitmap.width / 240;
- const canvas = new OffscreenCanvas(
- Math.round(ring.width * scale),
- Math.round(ring.height * scale),
- );
- const context = canvas.getContext('2d');
- if (context == null) {
- throw new Error('no 2d context');
- }
- context.drawImage(
- bitmap,
- Math.round(ring.x * scale),
- Math.round(ring.y * scale),
- canvas.width,
- canvas.height,
- 0,
- 0,
- canvas.width,
- canvas.height,
- );
- const blob = await canvas.convertToBlob({type: 'image/png'});
- const bytes = new Uint8Array(await blob.arrayBuffer());
- let binary = '';
- for (const byte of bytes) {
- binary += String.fromCharCode(byte);
- }
- out.push(btoa(binary));
- }
- return out;
- },
- {items: presented.map(f => f.data), ring: box},
- );
- } finally {
- await scratch.close();
- }
- const rows = await inkByDegree(page, crops, palette);
+ // How far the arc reaches in each frame, read at the ring.
+ const rows = await inkByDegree(
+ page,
+ presented.map(f => f.data),
+ palette,
+ {ring: box, viewportWidth: 240},
+ );
const frames = rows.map((row, i) => ({t: presented[i].t, ...span(row)}));
const inside = frames.filter(
f => f.t >= busy.from + 20 && f.t <= busy.to - 20,
);
- // The change lasts the theme's 410 ms, so it moves through most of the
- // block, one piece the whole way, and lands at 90%.
- const reaches = new Set(inside.map(f => f.last));
- expect(reaches.size).toBeGreaterThanOrEqual(10);
+ // The control, in the frames around the block (the proof reads it from the
+ // last frame before the block through the block's end).
+ const controlFrames = await trackMarker(
+ page,
+ presented
+ .filter(f => f.t >= busy.from - 250 && f.t <= busy.to)
+ .map(f => ({
+ t: f.t,
+ data: f.data,
+ format: 'png' as const,
+ viewportWidth: 240,
+ viewportHeight: 160,
+ })),
+ {rgb: CONTROL_RGB},
+ );
+ // The change runs through the block: the arc's reach shows a new degree on
+ // every frame the control moved on, and stalls no longer than the control,
+ // until it settles (the harness's busy proof: the screencast's capture rate
+ // thins both alike). It settles 3 degrees short of its end: on the change's
+ // own clock each 1-degree step of the reading takes 2 to 8 ms up to 324,
+ // then 12, 12 and 18 ms for 325, 326 and 327 before it rests at 328, so
+ // from 325 two frames can read the same degree while the arc still moves.
+ // It stays one piece the whole way, and lands at 90%.
+ const arc: TrackedFrame[] = frames.map(f => ({
+ t: f.t,
+ box: {x: f.last, y: 0, width: 1, height: 1},
+ pixels: 1,
+ }));
+ const proof = busyWindowProof(arc, busy, {
+ axis: 'x',
+ settledWithin: 3,
+ control: controlFrames,
+ });
+ test
+ .info()
+ .annotations.push({type: 'busy window', description: proof.summary});
+ expect(proof.verdict.failures, proof.summary).toEqual([]);
for (const frame of inside) {
expect(frame.pieces, `pieces at ${frame.t}`).toBe(1);
}