An open source design system that's fully customizable and agent ready
TypeScript
test(Slider): three touch proofs read the page's own frames (#10562)
Christopher Chedeau81d480b80fc3
diff --git a/packages/core/src/Slider/__tests__/SliderTouch.interaction.browser.spec.ts b/packages/core/src/Slider/__tests__/SliderTouch.interaction.browser.spec.tsindex 1ae4ef88f9..24b8e262dd 100644--- a/packages/core/src/Slider/__tests__/SliderTouch.interaction.browser.spec.ts+++ b/packages/core/src/Slider/__tests__/SliderTouch.interaction.browser.spec.ts@@ -28,12 +28,14 @@ import {countBetween,drag,flick,+ FRAME_STARVATION_MS,frames,holdAnimations,markElement,openStory,type Point,releaseAnimations,+ requireFrames,sampleCurve,springStep,startBusyControl,@@ -688,6 +690,65 @@ test('Reduce Motion: no lift; a release keeps coasting and a track tap sets thereturn samples;}, ms);type Sample = Awaited<ReturnType<typeof watch>>[number];+ /**+ * `watch`'s samples for `ms` after the page's own lift, timed from the lift.+ * The record starts before the gesture plays, so it holds the release's+ * first frames however late the runner reads the lift.+ */+ const watchFromLift = async (ms: number) => {+ await thumb.evaluate(element => {+ const record = {+ on: true,+ liftAt: null as number | null,+ samples: [] as Sample[],+ };+ (window as unknown as {__liftWatch: typeof record}).__liftWatch = record;+ window.addEventListener(+ 'touchend',+ () => {+ record.liftAt ??= performance.now();+ },+ {capture: true, passive: true},+ );+ const read = () => {+ if (!record.on) {+ return;+ }+ const style = getComputedStyle(element);+ record.samples.push({+ t: performance.now(),+ opacity: Number(style.opacity),+ scale: style.scale === 'none' ? 1 : Number(style.scale),+ x: element.getBoundingClientRect().x,+ value: Number(element.getAttribute('aria-valuenow')),+ });+ requestAnimationFrame(read);+ };+ requestAnimationFrame(read);+ });+ return async (): Promise<Sample[]> => {+ await page.waitForFunction(duration => {+ const {liftAt} = (+ window as unknown as {__liftWatch: {liftAt: number | null}}+ ).__liftWatch;+ return liftAt != null && performance.now() - liftAt >= duration;+ }, ms);+ return page.evaluate(duration => {+ const record = (+ window as unknown as {+ __liftWatch: {on: boolean; liftAt: number; samples: Sample[]};+ }+ ).__liftWatch;+ record.on = false;+ return record.samples+ .filter(+ sample =>+ sample.t >= record.liftAt && sample.t <= record.liftAt + duration,+ )+ .map(sample => ({...sample, t: sample.t - record.liftAt}));+ }, ms);+ };+ };const opacities = (samples: ReadonlyArray<Sample>) =>samples.map(sample => sample.opacity);// A held thumb keeps its size: the press shows in its paint alone (DR1).@@ -706,16 +767,28 @@ test('Reduce Motion: no lift; a release keeps coasting and a track tap sets the// value, at full opacity (DR2).const from = await center();const script = flick(from, {velocity: {x: 400, y: 0}, durationMs: 100});- const [release] = await playReading(- page,- driver,- script,- [script.length - 1],- async () => watch(500),- );+ const releaseFrames = await watchFromLift(500);+ await driver.play(script);+ const release = await releaseFrames();+ // The frames that show the coast run from the lift, at t 0, to the+ // thumb's last move. A starved page draws too few of them to show five+ // places. Then the proof fails by name, even when it stalled at the lift.+ const place = (sample: Sample) => Math.round(sample.x * 4);+ let lastMoveAt = release[0]?.t ?? 0;+ release.forEach((sample, index) => {+ if (index > 0 && place(sample) !== place(release[index - 1])) {+ lastMoveAt = sample.t;+ }+ });+ const lift = {t: 0};+ requireFrames([lift, ...release], {+ min: 5,+ during: 'the released thumb coasted',+ where: sample => sample !== lift && sample.t <= lastMoveAt,+ });expect(Math.min(...opacities(release))).toBe(1);expect(Math.max(...release.map(sample => sample.scale))).toBe(1);- const positions = new Set(release.map(sample => Math.round(sample.x * 4)));+ const positions = new Set(release.map(place));expect(positions.size).toBeGreaterThan(4);expect(release[release.length - 1].value).toBeGreaterThan(release[0].value);await frames(page, 6);@@ -1503,7 +1576,7 @@ test('a drag lays out nothing per move, and the coast neither lays out nor paintconst at = await center();const before = await value();let moves = {from: 0, to: 0};- let coast = {from: 0, to: 0};+ let coast = {liftedAt: 0, restAt: 0};const {script, ends} = legs(at,[@@ -1512,21 +1585,136 @@ test('a drag lays out nothing per move, and the coast neither lays out nor paint],{hz: 60},);+ // The lift drops the thumb's pressed paint in one commit, which paints+ // once. The page records that commit, whether it came in the lift's own+ // task, and its own frames until the test stops the record.+ await thumb.evaluate(element => {+ const release = {+ on: true,+ frames: [] as number[],+ liftAt: null as number | null,+ commitAt: null as number | null,+ inLiftTask: false,+ drawnAt: null as number | null,+ };+ (window as unknown as {__release: typeof release}).__release = release;+ const now = () => performance.timeOrigin + performance.now();+ const frame = () => {+ if (!release.on) {+ return;+ }+ release.frames.push(now());+ requestAnimationFrame(frame);+ };+ requestAnimationFrame(frame);+ let liftTask = false;+ // The lift's task dispatches its pointerup, then its touchend.+ const lift = () => {+ if (release.liftAt != null) {+ return;+ }+ release.liftAt = now();+ liftTask = true;+ // The next task or the next frame ends the lift's task.+ const channel = new MessageChannel();+ channel.port1.onmessage = () => {+ liftTask = false;+ channel.port1.close();+ };+ channel.port2.postMessage(null);+ requestAnimationFrame(() => {+ liftTask = false;+ });+ };+ for (const type of ['pointerup', 'touchend']) {+ window.addEventListener(type, lift, {capture: true, passive: true});+ }+ new MutationObserver((_, observer) => {+ if (release.liftAt == null) {+ return;+ }+ observer.disconnect();+ release.commitAt = now();+ release.inLiftTask = liftTask;+ requestAnimationFrame(() =>+ requestAnimationFrame(() => {+ release.drawnAt = now();+ }),+ );+ }).observe(element, {attributes: true, attributeFilter: ['class']});+ });const trace = await traceRendering(page, async () => {const played = await driver.play(script);moves = {from: played[3].dispatchedAt,to: played[ends[0]].dispatchedAt,};- const start = played[played.length - 1].dispatchedAt;+ const liftedAt = played[played.length - 1].dispatchedAt;await waitForRest(thumb);- coast = {from: start, to: Date.now()};+ coast = {liftedAt, restAt: Date.now()};});// The drag moved the thumb, so "nothing laid out" measures a real drag.expect(await value()).toBeGreaterThan(before + 20);const duringMoves = countBetween(trace, moves.from, moves.to);expect(duringMoves.Layout ?? 0, JSON.stringify(duringMoves)).toBe(0);- const duringCoast = countBetween(trace, coast.from + 80, coast.to - 120);+ const release = await page.evaluate(() => {+ const record = (+ window as unknown as {+ __release: {+ on: boolean;+ frames: number[];+ liftAt: number | null;+ commitAt: number | null;+ inLiftTask: boolean;+ drawnAt: number | null;+ };+ }+ ).__release;+ record.on = false;+ return record;+ });+ const {liftAt, drawnAt} = release;+ if (liftAt == null || release.commitAt == null || drawnAt == null) {+ throw new Error('the lift did not drop the thumb’s pressed paint');+ }+ // When the lift's task ran more than FRAME_STARVATION_MS after its dispatch,+ // with no frame in the FRAME_STARVATION_MS before it, the page was starved.+ // Then the run proves nothing about when the release paints.+ const waited = liftAt - coast.liftedAt;+ if (waited > FRAME_STARVATION_MS) {+ const around = [+ ...release.frames.filter(t => t <= liftAt).slice(-1),+ ...release.frames.filter(t => t > liftAt).slice(0, 1),+ ];+ requireFrames(+ around.map(t => ({t})),+ {+ min: 1,+ during: `the lift waited ${waited.toFixed(0)} ms for the page`,+ where: frame =>+ frame.t >= liftAt - FRAME_STARVATION_MS && frame.t <= liftAt,+ },+ );+ }+ // The pressed paint drops in the lift's own task. A later task would keep+ // it on the thumb after the finger lifts.+ expect(release.inLiftTask, 'the pressed paint drops in the lift’s task').toBe(+ true,+ );+ // The coast's window opens two frames after that commit, on the page's+ // clock. A loaded page draws the commit late, past any fixed time after+ // the dispatch. The window must hold frames of the page's own, or a late+ // commit would leave nothing to judge.+ const coastEnd = coast.restAt - 120;+ requireFrames(+ release.frames.map(t => ({t})),+ {+ min: 4,+ during: 'the thumb coasted',+ where: frame => frame.t >= drawnAt && frame.t <= coastEnd,+ },+ );+ const duringCoast = countBetween(trace, drawnAt, coastEnd);expect(duringCoast.Layout ?? 0, JSON.stringify(duringCoast)).toBe(0);expect(duringCoast.Paint ?? 0, JSON.stringify(duringCoast)).toBe(0);});diff --git a/packages/core/src/Slider/__tests__/SliderValueTooltip.interaction.browser.spec.ts b/packages/core/src/Slider/__tests__/SliderValueTooltip.interaction.browser.spec.tsindex 0420e2622d..d6ab9d1573 100644--- a/packages/core/src/Slider/__tests__/SliderValueTooltip.interaction.browser.spec.ts+++ b/packages/core/src/Slider/__tests__/SliderValueTooltip.interaction.browser.spec.ts@@ -18,7 +18,9 @@ import {castStep,CONTROL_RGB,flick,+ FRAME_STARVATION_MS,frames,+ FrameStarvationError,MARKER_RGB,openStory,startScreencast,@@ -405,12 +407,37 @@ test.describe('with a finger', () => {throw new Error('the thumb has no box');}const at = {x: box.x + box.width / 2, y: box.y + box.height / 2};+ // The page's own frames, recorded through the cast. A cast sends no frame+ // while the screen is still, so only these frames show a starved page.+ await page.evaluate(() => {+ const record = {on: true, frames: [] as number[]};+ (window as unknown as {__pageFrames: typeof record}).__pageFrames =+ record;+ const frame = () => {+ if (!record.on) {+ return;+ }+ record.frames.push(performance.timeOrigin + performance.now());+ requestAnimationFrame(frame);+ };+ requestAnimationFrame(frame);+ });const cast = await startScreencast(page);await frames(page, 4);- await driver.play(flick(at, {velocity: {x: 300, y: 0}, durationMs: 300}));+ const played = await driver.play(+ flick(at, {velocity: {x: 300, y: 0}, durationMs: 300}),+ );await waitForRest(thumb);+ const restAt = Date.now();await frames(page, 6);const shot = await cast.stop();+ const pageFrames = await page.evaluate(() => {+ const record = (+ window as unknown as {__pageFrames: {on: boolean; frames: number[]}}+ ).__pageFrames;+ record.on = false;+ return record.frames;+ });const thumbs = await trackMarker(page, shot);const tooltips = await trackMarker(page, shot, {rgb: CONTROL_RGB});const pairs = thumbs.flatMap((frame, index) => {@@ -419,6 +446,31 @@ test.describe('with a finger', () => {? [frame.box.x + frame.box.width / 2 - (other.x + other.width / 2)]: [];});+ // A starved page draws too few frames to show every frame. Then a gap+ // over FRAME_STARVATION_MS in its own frames, from the touch to the rest,+ // names the failure. The touch and the rest bound the gaps, so a page that+ // drew no frame between them shows one long gap. A short cast from a page+ // that drew every frame fails on the count.+ if (pairs.length <= 30) {+ const from = played[0].dispatchedAt;+ const edges = [+ from,+ ...pageFrames.filter(t => t > from && t < restAt),+ restAt,+ ];+ const longestGapMs = Math.max(+ ...edges.slice(1).map((t, index) => t - edges[index]),+ );+ if (longestGapMs > FRAME_STARVATION_MS) {+ throw new FrameStarvationError({+ during:+ 'the thumb and its value tooltip moved through a drag and its coast',+ frames: pairs.length,+ min: 31,+ longestGapMs,+ });+ }+ }expect(pairs.length).toBeGreaterThan(30);// One step of the cast's position grid: 1 px in Chromium's PNG cast,// 2 px in WebKit's JPEG cast.