An open source design system that's fully customizable and agent ready
TypeScript
test(core): the Switch's colour busy proof dates frames on the controls (#10758)
Christopher Chedeauf3c7a0234cb2
diff --git a/packages/core/src/Switch/__tests__/Switch.interaction.browser.spec.ts b/packages/core/src/Switch/__tests__/Switch.interaction.browser.spec.tsindex 63eae428d6..3535248573 100644--- a/packages/core/src/Switch/__tests__/Switch.interaction.browser.spec.ts+++ b/packages/core/src/Switch/__tests__/Switch.interaction.browser.spec.ts@@ -63,6 +63,7 @@ import {type BusyRun,type CastFrame,type TouchDriver,+ type TrackedFrame,CONTROL_SIDE,holdInputUntil,LiftPlacementError,@@ -72,10 +73,19 @@ import {BROWSER_CLICK_WAIT_MS} from '../../utils/browserClickWait';import {CLICK_GUARD_WINDOW_MS} from '../../utils/clickGuard';import {PROMOTION_LINGER_MS} from '../../motion/animator';+import {WEBKIT_LATE_STAMPS} from './Switch.webkitLateStamps.fixture';+/** How close to its end colour the fade is when its block ends: the cast's noise, red units. */const FADE_SETTLED = 6;/** Where judging the fade stops, red units from its end (see its proof). */const FADE_TAIL = 16;+/** The track-colour proof's pool (`pooledBusyProof`) options. */+const FADE_PROOF = {+ axis: 'x',+ settledWithin: FADE_TAIL,+ minRunFrames: 2,+ minJudgedRuns: 2,+} as const;/*** How long the toggle proof blocks the main thread, ms. The travel rests* about 330 ms into the block, so its last frames in the block show it at rest.@@ -2224,6 +2234,17 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',/** How long each busy-window control takes to cross and come back, ms. */const CONTROL_CYCLE_MS = 800;+ /**+ * How far each control travels in a leg, px: the harness's control, as+ * `CONTROL_CYCLE_MS` is its cycle.+ */+ const CONTROL_TRAVEL_PX = 240;+ /**+ * How long a control takes to travel one px, ms. The controls' clock reads+ * a frame's time from the control's position, to the px. So two frames one+ * display interval apart can read this much closer.+ */+ const CONTROL_READ_MS = CONTROL_CYCLE_MS / 2 / CONTROL_TRAVEL_PX;/** One 60 Hz frame, ms. */const FRAME_60_MS = 1000 / 60;@@ -2335,6 +2356,93 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',return [first.start, ahead.start];}+ /**+ * Each cast frame's time on the page clock, read from the two controls'+ * positions (`startControls`, tracked with `trackControl`). The compositor+ * samples every animation at one time in a frame, so the controls give the+ * frame's own time. The cast's stamp can run late: WebKit stamps a frame+ * that follows a delivery gap tens of ms after the frame it shows. A frame+ * that shows neither control mid-leg, or repeats the frame before it, gets+ * null.+ */+ function controlPageTime(+ control: ReadonlyArray<TrackedFrame>,+ ahead: ReadonlyArray<TrackedFrame>,+ starts: [number, number],+ ): (stamp: number) => number | null {+ const readers = [+ {clock: controlClock(control), origin: starts[0]},+ {clock: controlClock(ahead), origin: starts[1]},+ ];+ return stamp => {+ for (const {clock, origin} of readers) {+ const ms = clock.get(stamp);+ if (ms != null) {+ // The stamp only picks the control's cycle.+ const cycles = Math.round((stamp - origin - ms) / CONTROL_CYCLE_MS);+ return origin + ms + cycles * CONTROL_CYCLE_MS;+ }+ }+ return null;+ };+ }++ /** `controlPageTime` for a cast, with the first control's own frames. */+ async function castPageTime(+ page: Page,+ castFrames: Parameters<typeof trackMarker>[1],+ starts: [number, number],+ ): Promise<{+ pageTime: (stamp: number) => number | null;+ control: TrackedFrame[];+ }> {+ const control = await trackControl(page, castFrames);+ const ahead = await trackControl(page, castFrames, {ahead: true});+ return {pageTime: controlPageTime(control, ahead, starts), control};+ }++ /**+ * The frames shown at display rate: at most one each 60 Hz interval on the+ * page clock (`castPageTime`), read to the px (`CONTROL_READ_MS`). A frame+ * under one interval after the last one kept is left out. WebKit's+ * compositor can draw two frames closer than that, and a slow curve then+ * shows no step between them. Each pair of kept frames one interval apart+ * stays a pair.+ */+ function atDisplayRate<T extends {t: number}>(frames: ReadonlyArray<T>): T[] {+ const kept: T[] = [];+ for (const frame of frames) {+ const last = kept.at(-1);+ if (last == null || frame.t - last.t >= FRAME_60_MS - CONTROL_READ_MS) {+ kept.push(frame);+ }+ }+ return kept;+ }++ /**+ * How many pairs of a run's frames in its judged part are one display+ * interval apart (`atDisplayRate`), with the control moved between them. A+ * fade that holds every other frame shows no step on such a pair.+ */+ function displayIntervalPairs(+ run: BusyRun,+ judged: {from: number; to: number},+ ): number {+ let pairs = 0;+ for (let i = 1; i < run.tracked.length; i++) {+ const [a, b] = [run.tracked[i - 1], run.tracked[i]];+ const inJudged = a.t >= judged.from && b.t <= judged.to;+ const oneInterval = b.t - a.t < 2 * FRAME_60_MS - CONTROL_READ_MS;+ const controlMoved =+ run.control[i]?.box?.x !== run.control[i - 1]?.box?.x;+ if (inJudged && oneInterval && controlMoved) {+ pairs++;+ }+ }+ return pairs;+ }+/*** The thumb's center in each presented frame, on the page clock, `[ms,* px]`. The two controls time each frame by the compositor's own@@ -2352,29 +2460,7 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',castFrames: Parameters<typeof trackMarker>[1],starts: [number, number],): Promise<ClockedFrame[]> {- const readers = [- {- clock: controlClock(await trackControl(page, castFrames)),- origin: starts[0],- },- {- clock: controlClock(- await trackControl(page, castFrames, {ahead: true}),- ),- origin: starts[1],- },- ];- const pageTime = (stamp: number): number | null => {- for (const {clock, origin} of readers) {- const ms = clock.get(stamp);- if (ms != null) {- // The stamp only picks the control's cycle.- const cycles = Math.round((stamp - origin - ms) / CONTROL_CYCLE_MS);- return origin + ms + cycles * CONTROL_CYCLE_MS;- }- }- return null;- };+ const {pageTime} = await castPageTime(page, castFrames, starts);const tracked = await trackMarker(page, castFrames);const shown = tracked.filter((f): f is typeof f & {box: NonNullable<typeof f.box>} =>@@ -3247,10 +3333,6 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',page,browserName,}, testInfo) => {- test.skip(- browserName !== 'chromium',- "WebKit's JPEG cast can show the fade's slow tail unchanged for a frame, so one run in 20 reads a stall",- );const {driver,input,@@ -3274,7 +3356,7 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',// The track's colour on a flat part of it, away from its rounded ends:// JPEG noise on an edge reads as change.const probe = {x: box.x + box.width / 2 - 4, y: box.y + box.height / 2 - 4};- const control = await startBusyControl(page);+ const starts = await startControls(page);// A fade gives few frames in one block. The proof pools two runs, in and// then out, and judges them as one longer window.const runs: BusyRun[] = [];@@ -3319,14 +3401,28 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',f.t >= block.from - BUSY_PROOF_LOOKAROUND_MS &&f.t <= block.to + BUSY_PROOF_LOOKAROUND_MS,);- // The red channel's mean in an 8 px square: a scalar on x.- const tracked = await trackBrightness(page, read, {- ...probe,- size: 8,- });+ // The red channel's mean in an 8 px square: a scalar on x. Each frame+ // is judged at its time on the controls' clock, not its cast stamp, and+ // at display rate.+ const {pageTime, control} = await castPageTime(page, read, starts);+ const dated = <T extends {t: number}>(frames: ReadonlyArray<T>): T[] =>+ atDisplayRate(+ frames+ .flatMap(frame => {+ const at = pageTime(frame.t);+ return at == null ? [] : [{...frame, t: at}];+ })+ .sort((a, b) => a.t - b.t),+ );+ const tracked = dated(+ await trackBrightness(page, read, {+ ...probe,+ size: 8,+ }),+ );runs.push({tracked,- control: await trackControl(page, read),+ control: dated(control),window: {from: block.from, to: Math.min(block.to, fade.to)},});evidence.push({@@ -3358,18 +3454,12 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',gap: Math.abs((last?.box?.x ?? 0) - rest.box.x),});}- await control.remove();// The harness's tail rule for a curve, on a brightness channel: judging// stops once the fade moves under two levels a frame. A loaded cast can// then show no change between two frames. On the well springs, critically// damped over the track's 166-level red span, that is 16 levels from rest// for wellOn and 9 for wellOff. The pool takes one bound, the larger.- const proof = pooledBusyProof(runs, {- axis: 'x',- settledWithin: FADE_TAIL,- minRunFrames: 2,- minJudgedRuns: 2,- });+ const proof = pooledBusyProof(runs, FADE_PROOF);await writeEvidence('busy-main-thread/switch-track-colour',{@@ -3385,6 +3475,21 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',type: 'busy window',description: proof.summary,});+ // WebKit's cast gives few pairs of frames one display interval apart.+ // So a fade that holds every other frame can still show a step on each+ // frame judged there. This run does not judge that cadence, and says so.+ if (browserName === 'webkit') {+ const pairs = runs.reduce((count, run, i) => {+ const judged = proof.runs[i]?.judged;+ return judged == null+ ? count+ : count + displayIntervalPairs(run, judged);+ }, 0);+ test.info().annotations.push({+ type: 'cadence',+ description: `cadence not judged: ${pairs} display-interval pairs`,+ });+ }// The colour changes on every frame the control moves on, and in each run// it stalls no longer than the control.expect(proof.verdict.failures, proof.summary).toEqual([]);@@ -3416,6 +3521,82 @@ test.describe('Switch touch presentation (UISwitch, iOS 26.2), under a finger',});});+ test("P10 · on two WebKit casts that stamped frames late, the track colour proof judges each frame at its time on the control's clock, at display rate", () => {+ WEBKIT_LATE_STAMPS.forEach((recorded, cast) => {+ const stamped: BusyRun[] = recorded.map(run => ({+ tracked: run.colour.map(([t, red]) => ({+ t,+ box: {x: red, y: 0, width: 1, height: 1},+ pixels: 1,+ })),+ control: run.control.map(([t, x]) => ({+ t,+ box:+ x == null+ ? null+ : {x, y: 0, width: CONTROL_SIDE, height: CONTROL_SIDE},+ pixels: x == null ? 0 : CONTROL_SIDE * CONTROL_SIDE,+ })),+ window: run.window,+ }));+ const onClock = (run: BusyRun, displayRate: boolean): BusyRun => {+ // The cast recorded one control and not its start: the start is the+ // one that most frames' stamps agree with.+ const offsets = [...controlClock(run.control).entries()].map(+ ([stamp, ms]) => stamp - ms,+ );+ const unwrapped = offsets+ .map(+ offset =>+ offset -+ Math.round((offset - offsets[0]) / CONTROL_CYCLE_MS) *+ CONTROL_CYCLE_MS,+ )+ .sort((a, b) => a - b);+ const start = unwrapped[Math.floor(unwrapped.length / 2)];+ const pageTime = controlPageTime(run.control, [], [start, start]);+ const dated = <T extends {t: number}>(+ frames: ReadonlyArray<T>,+ ): T[] => {+ const timed = frames+ .flatMap(frame => {+ const at = pageTime(frame.t);+ return at == null ? [] : [{...frame, t: at}];+ })+ .sort((a, b) => a.t - b.t);+ return displayRate ? atDisplayRate(timed) : timed;+ };+ return {+ ...run,+ tracked: dated(run.tracked),+ control: dated(run.control),+ };+ };+ // By the cast's stamps, as each run was judged live: it fails.+ expect(+ pooledBusyProof(stamped, FADE_PROOF).verdict.failures,+ `cast ${cast + 1}, by its stamps`,+ ).not.toEqual([]);+ // On the control's clock alone it still fails. A frame drawn 6 to 9 ms+ // after the one before shows no step of the fade's slow tail.+ expect(+ pooledBusyProof(+ stamped.map(run => onClock(run, false)),+ FADE_PROOF,+ ).verdict.failures,+ `cast ${cast + 1}, on the control's clock`,+ ).not.toEqual([]);+ const proof = pooledBusyProof(+ stamped.map(run => onClock(run, true)),+ FADE_PROOF,+ );+ expect(+ proof.verdict.failures,+ `cast ${cast + 1}, at display rate: ${proof.summary}`,+ ).toEqual([]);+ });+ });+test('P10 · a change does no Layout or Paint per frame', async ({page,browserName,diff --git a/packages/core/src/Switch/__tests__/Switch.webkitLateStamps.fixture.ts b/packages/core/src/Switch/__tests__/Switch.webkitLateStamps.fixture.tsnew file mode 100644index 0000000000..19dc38dccd--- /dev/null+++ b/packages/core/src/Switch/__tests__/Switch.webkitLateStamps.fixture.ts@@ -0,0 +1,297 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Switch.webkitLateStamps.fixture.ts+ * @input Two WebKit runs of the track-colour busy proof, recorded live+ * @output `WEBKIT_LATE_STAMPS`: each run's block, and per cast frame its+ * stamp, the track's red level and the control's x+ * @position Replayed by the Switch interaction spec: the track-colour proof+ * dated by the cast's stamps fails on both, as it did live.+ */++/** One run: its judged window, then `[stamp, red]` and `[stamp, x]` per frame. */+export interface RecordedColourRun {+ readonly window: {readonly from: number; readonly to: number};+ readonly colour: ReadonlyArray<readonly [number, number]>;+ readonly control: ReadonlyArray<readonly [number, number | null]>;+}++/**+ * Two runs of the proof (in, then out) each, in the WebKit container under+ * CI's quota profile. The cast recorded the first control only, so a frame+ * near its turn has no time on its clock.+ */+export const WEBKIT_LATE_STAMPS: ReadonlyArray<+ readonly [RecordedColourRun, RecordedColourRun]+> = [+ [+ {+ window: {from: 1794187969702, to: 1794187970402},+ colour: [+ [1794187969506.7, 212],+ [1794187969523.5, 212],+ [1794187969601.5, 212],+ [1794187969618.4, 212],+ [1794187969628.3, 212],+ [1794187969705.1, 212],+ [1794187969722.5, 175],+ [1794187969802, 163],+ [1794187969819, 117],+ [1794187969829.3, 108],+ [1794187969906.2, 103],+ [1794187969923.5, 73],+ [1794187969933.3, 68],+ [1794187970007.6, 51],+ [1794187970024.7, 51],+ [1794187970101.6, 47],+ [1794187970118.4, 38],+ [1794187970129.1, 38],+ [1794187970206.5, 37],+ [1794187970223.4, 32],+ [1794187970302.5, 31],+ [1794187970319.4, 29],+ [1794187970329.1, 29],+ [1794187970405.8, 29],+ [1794187970423.4, 28],+ [1794187970433.8, 28],+ [1794187970507.7, 28],+ [1794187970525.2, 28],+ [1794187970600.6, 28],+ [1794187970617.8, 27],+ [1794187970628.1, 27],+ ],+ control: [+ [1794187969506.7, 61],+ [1794187969523.5, 108],+ [1794187969601.5, 118],+ [1794187969618.4, 165],+ [1794187969628.3, 174],+ [1794187969705.1, 181],+ [1794187969722.5, 226],+ [1794187969802, 237],+ [1794187969819, 197],+ [1794187969829.3, 187],+ [1794187969906.2, 181],+ [1794187969923.5, 135],+ [1794187969933.3, 124],+ [1794187970007.6, 78],+ [1794187970024.7, 74],+ [1794187970101.6, 62],+ [1794187970118.4, 17],+ [1794187970129.1, 7],+ [1794187970206.5, 0],+ [1794187970223.4, 48],+ [1794187970302.5, 58],+ [1794187970319.4, 105],+ [1794187970329.1, 114],+ [1794187970405.8, 121],+ [1794187970423.4, 167],+ [1794187970433.8, 178],+ [1794187970507.7, 222],+ [1794187970525.2, 228],+ [1794187970600.6, 238],+ [1794187970617.8, 198],+ [1794187970628.1, 188],+ ],+ },+ {+ window: {from: 1794187973526, to: 1794187974087.8},+ colour: [+ [1794187973321.6, 27],+ [1794187973403.1, 27],+ [1794187973420.6, 27],+ [1794187973499.1, 27],+ [1794187973516.4, 27],+ [1794187973527, 32],+ [1794187973606.9, 40],+ [1794187973623.7, 120],+ [1794187973700.7, 133],+ [1794187973717.3, 179],+ [1794187973727.9, 184],+ [1794187973806.6, 188],+ [1794187973824.2, 203],+ [1794187973903.1, 205],+ [1794187973920.8, 209],+ [1794187974001.6, 209],+ [1794187974018.3, 211],+ [1794187974028.9, 211],+ [1794187974106.7, 211],+ [1794187974124.3, 212],+ [1794187974198.7, 212],+ [1794187974216.4, 212],+ [1794187974227, 212],+ [1794187974301.1, 212],+ [1794187974318.8, 212],+ [1794187974329.5, 212],+ [1794187974406, 212],+ [1794187974423.6, 212],+ [1794187974434.1, 212],+ ],+ control: [+ [1794187973321.6, 14],+ [1794187973403.1, 5],+ [1794187973420.6, 46],+ [1794187973499.1, 57],+ [1794187973516.4, 103],+ [1794187973527, 114],+ [1794187973606.9, 120],+ [1794187973623.7, 168],+ [1794187973700.7, 178],+ [1794187973717.3, 224],+ [1794187973727.9, 234],+ [1794187973806.6, 241],+ [1794187973824.2, 194],+ [1794187973903.1, 183],+ [1794187973920.8, 136],+ [1794187974001.6, 125],+ [1794187974018.3, 77],+ [1794187974028.9, 66],+ [1794187974106.7, 61],+ [1794187974124.3, 14],+ [1794187974198.7, 2],+ [1794187974216.4, 43],+ [1794187974227, 54],+ [1794187974301.1, 60],+ [1794187974318.8, 105],+ [1794187974329.5, 114],+ [1794187974406, 122],+ [1794187974423.6, 168],+ [1794187974434.1, 178],+ ],+ },+ ],+ [+ {+ window: {from: 1794189860738, to: 1794189861438},+ colour: [+ [1794189860527.6, 212],+ [1794189860544.6, 212],+ [1794189860555.1, 212],+ [1794189860631.7, 212],+ [1794189860649.1, 212],+ [1794189860731.5, 210],+ [1794189860748.9, 169],+ [1794189860827.1, 158],+ [1794189860844.2, 112],+ [1794189860854.4, 105],+ [1794189860931.9, 99],+ [1794189860948.8, 71],+ [1794189861027.3, 66],+ [1794189861044.3, 50],+ [1794189861054.2, 46],+ [1794189861133.6, 46],+ [1794189861150.5, 38],+ [1794189861230.2, 36],+ [1794189861247.3, 32],+ [1794189861296.7, 31],+ [1794189861334.6, 30],+ [1794189861352, 29],+ [1794189861432.3, 29],+ [1794189861449.3, 28],+ [1794189861529.6, 28],+ [1794189861546.5, 28],+ [1794189861627.1, 28],+ [1794189861644.2, 27],+ [1794189861654.2, 27],+ ],+ control: [+ [1794189860527.6, 75],+ [1794189860544.6, 122],+ [1794189860555.1, 132],+ [1794189860631.7, 138],+ [1794189860649.1, 184],+ [1794189860731.5, 194],+ [1794189860748.9, 238],+ [1794189860827.1, 228],+ [1794189860844.2, 181],+ [1794189860854.4, 170],+ [1794189860931.9, 164],+ [1794189860948.8, 118],+ [1794189861027.3, 108],+ [1794189861044.3, 61],+ [1794189861054.2, 50],+ [1794189861133.6, 45],+ [1794189861150.5, 5],+ [1794189861230.2, 14],+ [1794189861247.3, 62],+ [1794189861296.7, 73],+ [1794189861334.6, 103],+ [1794189861352, 126],+ [1794189861432.3, 136],+ [1794189861449.3, 184],+ [1794189861529.6, 194],+ [1794189861546.5, 238],+ [1794189861627.1, 229],+ [1794189861644.2, 181],+ [1794189861654.2, 170],+ ],+ },+ {+ window: {from: 1794189867528, to: 1794189868020.8},+ colour: [+ [1794189867328.9, 27],+ [1794189867346.2, 27],+ [1794189867356.5, 27],+ [1794189867432.9, 27],+ [1794189867450, 27],+ [1794189867527.7, 32],+ [1794189867544.7, 106],+ [1794189867555.8, 123],+ [1794189867635.8, 133],+ [1794189867653.1, 180],+ [1794189867728.9, 185],+ [1794189867746.3, 201],+ [1794189867756.5, 204],+ [1794189867835.5, 208],+ [1794189867852.6, 209],+ [1794189867929.2, 209],+ [1794189867946.6, 211],+ [1794189868026.9, 211],+ [1794189868044.6, 212],+ [1794189868055.8, 212],+ [1794189868133.4, 212],+ [1794189868150.8, 212],+ [1794189868227.3, 212],+ [1794189868244.7, 212],+ [1794189868255.8, 212],+ [1794189868334.5, 212],+ [1794189868351.1, 212],+ [1794189868429.5, 212],+ [1794189868446.5, 212],+ [1794189868457.3, 212],+ ],+ control: [+ [1794189867328.9, 164],+ [1794189867346.2, 120],+ [1794189867356.5, 110],+ [1794189867432.9, 104],+ [1794189867450, 58],+ [1794189867527.7, 48],+ [1794189867544.7, 0],+ [1794189867555.8, 11],+ [1794189867635.8, 18],+ [1794189867653.1, 66],+ [1794189867728.9, 76],+ [1794189867746.3, 122],+ [1794189867756.5, 132],+ [1794189867835.5, 162],+ [1794189867852.6, 186],+ [1794189867929.2, 196],+ [1794189867946.6, 240],+ [1794189868026.9, 230],+ [1794189868044.6, 181],+ [1794189868055.8, 171],+ [1794189868133.4, 164],+ [1794189868150.8, 118],+ [1794189868227.3, 107],+ [1794189868244.7, 61],+ [1794189868255.8, 50],+ [1794189868334.5, 44],+ [1794189868351.1, 5],+ [1794189868429.5, 14],+ [1794189868446.5, 62],+ [1794189868457.3, 72],+ ],+ },+ ],+];