Skip to content

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

TypeScript

test(Button, gestures): arm press holds and blocks before the gesture (#10521)

Christopher Chedeauc8b4cba38181
diff --git a/packages/core/src/Button/__tests__/ButtonPress.interaction.browser.spec.ts b/packages/core/src/Button/__tests__/ButtonPress.interaction.browser.spec.ts
index 97ce58fbb5..295796d004 100644
--- a/packages/core/src/Button/__tests__/ButtonPress.interaction.browser.spec.ts
+++ b/packages/core/src/Button/__tests__/ButtonPress.interaction.browser.spec.ts
@@ -24,11 +24,13 @@ import {
type Page,
} from '@astryxdesign/a11y-spec/playwright';
import {
+ armHold,
flick,
freezeTimers,
holdThenDrag,
installTimerGate,
openStory,
+ releaseAnimations,
tap,
thawTimers,
touchDriver,
@@ -296,6 +298,19 @@ test(
width: box.width + 32,
height: box.height + 32,
};
+ // The release ends when its animation does, and on a busy machine it can
+ // end before the lift's photo. So the page holds the release itself as
+ // soon as it runs. The press runs one animation (the scale) and the
+ // release runs two (the scale back and the layer's fade), so the hold
+ // waits for two.
+ const hold = await armHold(page, opened.surface, {
+ count: 2,
+ judges: {
+ target: opened.surface,
+ property: 'opacity',
+ pseudoElement: '::before',
+ },
+ });
const storyboard = await touchStoryboard(
page,
opened.driver,
@@ -309,8 +324,10 @@ test(
columns: 3,
},
);
+ await hold.held();
await freezeTimers(page);
await storyboard.write(testInfo);
+ await releaseAnimations(page, {finish: true});
await thawTimers(page);
const states = storyboard.frames.map(f => f.data.state as string | null);
// Before the touch, the touch itself (already highlighted), the lift.
diff --git a/packages/core/src/Button/__tests__/ButtonPressRelease.interaction.browser.spec.ts b/packages/core/src/Button/__tests__/ButtonPressRelease.interaction.browser.spec.ts
index 4d7e0aeee9..8f956ec20c 100644
--- a/packages/core/src/Button/__tests__/ButtonPressRelease.interaction.browser.spec.ts
+++ b/packages/core/src/Button/__tests__/ButtonPressRelease.interaction.browser.spec.ts
@@ -26,28 +26,36 @@ import {
type Page,
} from '@astryxdesign/a11y-spec/playwright';
import {
+ FRAME_STARVATION_MS,
+ FrameStarvationError,
animationStoryboard,
armHold,
blockMainThread,
+ busyWindowProof,
castStoryboard,
compareCurve,
countBetween,
cubicBezier,
freezeTimers,
- holdAnimations,
installTimerGate,
openStory,
releaseAnimations,
+ requireFrames,
seekAnimations,
+ startBusyControl,
startScreencast,
tap,
thawTimers,
timeline,
touchDriver,
traceRendering,
+ trackBrightness,
+ trackControl,
+ waitForRest,
watchAnimations,
withStorybook,
writeEvidence,
+ type ArmedHold,
type CastFrame,
type TouchDriver,
} from '@astryxdesign/interaction-harness';
@@ -204,18 +212,23 @@ for (const [name, story, locate] of [
await page.emulateMedia({reducedMotion: 'reduce'});
const opened = await openSurface(page, story, locate);
await installTimerGate(page);
- await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
- expect(await opened.surface.getAttribute('data-astryx-press')).toBe(
- 'fading',
- );
- await freezeTimers(page);
- const held = await holdAnimations(page, opened.surface, {
+ // The page holds the release itself as soon as it runs. A hold the test
+ // sends after the lift reaches the page later on a busy machine, and the
+ // release can end first. Under reduced motion the press runs no
+ // animation, so the first one is the release.
+ const hold = await armHold(page, opened.surface, {
judges: {
target: opened.surface,
property: 'opacity',
pseudoElement: '::before',
},
});
+ await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
+ expect(await opened.surface.getAttribute('data-astryx-press')).toBe(
+ 'fading',
+ );
+ await freezeTimers(page);
+ const held = await hold.held();
await releaseAnimations(page, {finish: true});
await thawTimers(page);
// One animation, of opacity alone, finished within 200 ms; nothing
@@ -264,16 +277,35 @@ for (const [name, story, locate, pseudo] of [
await page.evaluate(() => matchMedia('(pointer: coarse)').matches),
).toBe(false);
await installTimerGate(page);
- await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
+ // The page holds the release itself as soon as it runs. A hold the
+ // test sends after the lift reaches the page later on a busy machine,
+ // and the release can end first. The press runs the scale, and the
+ // release runs the strength's animation alone. So the hold is armed
+ // once the press is at rest, before the lift.
+ let hold: ArmedHold | undefined;
+ await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}), {
+ onEvent: async step => {
+ if (step.type === 'touchStart') {
+ await waitForRest(opened.surface);
+ hold = await armHold(page, opened.surface, {
+ judges: {
+ target: opened.surface,
+ property: '--astryx-press-alpha',
+ },
+ });
+ }
+ },
+ });
expect(await opened.surface.getAttribute('data-astryx-press')).toBe(
'fading',
);
await freezeTimers(page);
- const held = (
- await holdAnimations(page, opened.surface, {
- judges: {target: opened.surface, property: '--astryx-press-alpha'},
- })
- ).filter(animation => animation.kind === 'CSSAnimation');
+ if (hold == null) {
+ throw new Error('the press started no hold');
+ }
+ const held = (await hold.held()).filter(
+ animation => animation.kind === 'CSSAnimation',
+ );
const layer = await opened.surface.evaluate(
el => getComputedStyle(el, '::before').content,
);
@@ -323,18 +355,25 @@ for (const [name, story, locate, pseudo] of [
// Gate the page's timers before the press, so the release timer (which
// removes the attribute when the clock runs out) holds while seeking.
await installTimerGate(page);
- await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
- expect(await opened.surface.getAttribute('data-astryx-press')).toBe(
- 'fading',
- );
- await freezeTimers(page);
- const held = await holdAnimations(page, opened.surface, {
+ // The page holds the release itself as soon as it runs. A hold the test
+ // sends after the lift reaches the page later on a busy machine, and the
+ // release can end first. The press runs one animation (the scale) and the
+ // release runs two (the scale back and the layer's fade), so the hold
+ // waits for two.
+ const hold = await armHold(page, opened.surface, {
+ count: 2,
judges: {
target: opened.surface,
property: 'opacity',
pseudoElement: pseudo,
},
});
+ await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
+ expect(await opened.surface.getAttribute('data-astryx-press')).toBe(
+ 'fading',
+ );
+ await freezeTimers(page);
+ const held = await hold.held();
const release = held.find(
a => a.kind === 'CSSAnimation' && a.durationMs === RELEASE_MS,
);
@@ -548,18 +587,29 @@ test('the release of a button keeps fading on every frame while the main thread
await opened.surface.evaluate(el =>
(el as HTMLElement).style.setProperty('--duration-exit', '400ms'),
);
+ // The harness's busy-window control: a box the compositor moves, in the
+ // same screencast. The cast's frames are judged against what the compositor
+ // presented, not against a frame rate.
+ const busyControl = await startBusyControl(page);
const cast = await startScreencast(page);
const watch = await watchAnimations(opened.surface);
+ // A 1 s block, as spec:AST-094 FR5's verification names, covers the
+ // whole release. The page starts it itself once the layer's fade has run a
+ // frame. A block the test sends after the lift reaches the page later on a
+ // busy machine, and most of the release can be over first.
+ const blocked = blockMainThread(page, 1000, {
+ during: opened.surface,
+ pseudoElement: '::before',
+ });
await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
- // The release is the longest animation running: the `::before`
- // opacity fade, not the 125 ms press-scale transition that starts with it.
+ const block = await blocked;
+ // The release is the longest animation: the `::before` opacity fade, not
+ // the 125 ms press-scale transition that starts with it.
const running = await watch.started({minProgressMs: 16});
const release = longestOf(running);
- // A 1 s block, as spec:AST-094 FR5's verification names, covers the
- // whole release.
- const block = await blockMainThread(page, 1000);
await page.waitForTimeout(150);
const frames = await cast.stop();
+ await busyControl.remove();
const pixels = await samplePixel(page, frames, opened.probe);
const rest = pixels[0].value;
const pressed = Math.min(
@@ -572,13 +622,16 @@ test('the release of a button keeps fading on every frame while the main thread
t: +(p.t - block.from).toFixed(1),
strength: +strength(p.value).toFixed(3),
}));
- const intervals = during
- .map((p, i) => (i === 0 ? p.t - block.from : p.t - during[i - 1].t))
- .filter(dt => dt > 0);
- const longestGap = Math.max(
- end - (during.at(-1)?.t ?? block.from),
- ...intervals,
+ // The frames on which the compositor presented a new position of the
+ // control: each is a frame that the release had to show.
+ const controlFrames = await trackControl(
+ page,
+ frames.filter(f => f.t >= block.from - 100 && f.t <= end + 100),
);
+ const presented = controlFrames.filter((frame, i) => {
+ const before = controlFrames[i - 1]?.box;
+ return frame.box != null && before != null && frame.box.x !== before.x;
+ });
await writeEvidence(
'busy-main-thread/button-press-release',
{
@@ -604,10 +657,51 @@ test('the release of a button keeps fading on every frame while the main thread
});
await film.write(testInfo, {block, release});
expect(rest - pressed).toBeGreaterThan(10);
- expect(during.length, JSON.stringify(strengths)).toBeGreaterThanOrEqual(
- Math.floor((end - block.from) / (1000 / 60)) - 2,
+ // A page starved of frames fails here by name, not on a bare count.
+ requireFrames(presented, {
+ min: 4,
+ during: 'the release faded under the block',
+ where: frame => frame.t > block.from && frame.t <= end,
+ });
+ // The release's colour in a 4 px square at the probe, each frame: a scalar
+ // on x. The square stays clear of the button's edge as the press scale
+ // moves it. Chromium's PNG cast shows the colour in whole levels, so its
+ // step is one level.
+ const tracked = (
+ await trackBrightness(page, frames, {
+ x: opened.probe.x - 2,
+ y: opened.probe.y - 2,
+ size: 4,
+ })
+ ).map(frame => ({...frame, step: {x: 1, y: 1}}));
+ // The fade moves about a level a frame, so a frame can show no change. The
+ // proof reads such runs against the cast's own step, and stops judging 2
+ // steps from rest. It starts at 80 ms of fade. The exit curve's first
+ // frames move under one level, which a frame by frame read cannot tell
+ // from a freeze. The block and the checks above still span the whole
+ // release.
+ const proof = busyWindowProof(
+ tracked,
+ {
+ ...block,
+ from: Math.max(block.from, release.from + 80),
+ to: Math.min(block.to, release.to),
+ },
+ {axis: 'x', settledWithinSteps: 2, control: controlFrames},
);
- expect(longestGap).toBeLessThan(40);
+ // A stall of the control is the machine's: the compositor or the recorder
+ // starved. Such a run fails here by name, with the numbers.
+ if (
+ proof.control != null &&
+ proof.control.longestStallMs > FRAME_STARVATION_MS
+ ) {
+ throw new FrameStarvationError({
+ during: 'the release faded under the block',
+ frames: proof.control.moved,
+ min: 4,
+ longestGapMs: proof.control.longestStallMs,
+ });
+ }
for (let i = 1; i < strengths.length; i++) {
expect(strengths[i].strength).toBeLessThanOrEqual(
strengths[i - 1].strength + 0.01,
@@ -616,6 +710,11 @@ test('the release of a button keeps fading on every frame while the main thread
const last = strengths.at(-1);
expect(last).toBeDefined();
expect(strengths[0].strength).toBeGreaterThan((last?.strength ?? 1) + 0.2);
+ // On every frame the control moved on, the release moved too, and it
+ // stalled no longer than the control. A fade that jumps, or that freezes,
+ // fails here.
+ testInfo.annotations.push({type: 'busy window', description: proof.summary});
+ expect(proof.verdict.failures, proof.summary).toEqual([]);
});
test('after a busy first frame, the release starts at that frame and keeps a button’s layer until its strength reaches 0', async ({
diff --git a/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts b/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts
index 68f0aff72b..cb4b0e838b 100644
--- a/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts
+++ b/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts
@@ -3226,7 +3226,10 @@ test("a press in a same-origin iframe's scroll view waits the content-touch dela
const row = doc.getElementById('row') as HTMLElement;
const times: {touch?: number; handled?: number; highlight?: number} = {};
(window as unknown as {__frameTimes: typeof times}).__frameTimes = times;
- doc.addEventListener(
+ // On the window's capture phase, added before the host's listener there,
+ // so this reads its clocks first. The host reads its own after, never
+ // before, however long a busy machine holds the page between them.
+ view.addEventListener(
'touchstart',
event => {
times.touch = event.timeStamp;