Skip to content

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

TypeScript

test(NavigationStack): a programmatic pop reads its layers held (#10743)

Christopher Chedeau58e528570ec5
diff --git a/packages/lab/src/NavigationStack/__tests__/NavigationStack.interaction.browser.spec.ts b/packages/lab/src/NavigationStack/__tests__/NavigationStack.interaction.browser.spec.ts
index b0a3a35d5b..19097792aa 100644
--- a/packages/lab/src/NavigationStack/__tests__/NavigationStack.interaction.browser.spec.ts
+++ b/packages/lab/src/NavigationStack/__tests__/NavigationStack.interaction.browser.spec.ts
@@ -46,6 +46,7 @@ import {
requireFrames,
sampleCurve,
sampleRects,
+ seekAnimations,
startBusyControl,
startScreencast,
thawPage,
@@ -189,20 +190,18 @@ interface BarFrame {
}
/**
- * Starts sampling, every frame, each card's and bar's x relative to the
- * stack and the back button's effective opacities. The returned function
- * stops and returns them. A layer's x is its computed `translate`: the cards
- * and bars sit at the stack's leading edge, checked when the trace starts. So
- * a frame reads no layout. Under load, WebKit can evaluate two layers that
- * share one clock at two times in one frame, as far apart as their reads.
- * This holds for boxes and translates alike. So each card is read right
- * before its bar. `animating` says which frames an animation drives.
+ * Installs the page's reader of each card's and bar's x relative to the
+ * stack and the back button's effective opacities. A layer's x is its
+ * computed `translate`: the cards and bars sit at the stack's leading edge,
+ * checked here. So a read needs no layout. Under load, WebKit can evaluate
+ * two layers that share one clock at two times in one frame, as far apart as
+ * their reads. This holds for boxes and translates alike. So each card is
+ * read right before its bar. `animating` says which reads an animation
+ * drives.
*/
-async function traceBars(page: Page): Promise<() => Promise<BarFrame[]>> {
+async function installBarReader(page: Page): Promise<void> {
await page.evaluate(() => {
- const w = window as unknown as {__bars: unknown[]; __barsStop: boolean};
- w.__bars = [];
- w.__barsStop = false;
+ const w = window as unknown as {__readBarFrame: (t: number) => unknown};
const stack = document.querySelector('[data-testid="navigation-stack"]');
const effective = (element: Element) => {
let opacity = 1;
@@ -260,12 +259,11 @@ async function traceBars(page: Page): Promise<() => Promise<BarFrame[]>> {
throw new Error(`a layer sits ${offset} px from the stack's edge`);
}
}
- const t0 = performance.now();
- const tick = () => {
+ w.__readBarFrame = t => {
const button = document.querySelector('[data-navigation-back]');
const chevron = button?.firstElementChild ?? null;
- w.__bars.push({
- t: performance.now() - t0,
+ return {
+ t,
...readLayers(),
back:
button == null || chevron == null
@@ -278,7 +276,28 @@ async function traceBars(page: Page): Promise<() => Promise<BarFrame[]>> {
].some(element =>
element.getAnimations().some(a => a.playState === 'running'),
),
- });
+ };
+ };
+ });
+}
+
+/**
+ * Starts sampling the layers every frame (`installBarReader`). The returned
+ * function stops and returns the frames.
+ */
+async function traceBars(page: Page): Promise<() => Promise<BarFrame[]>> {
+ await installBarReader(page);
+ await page.evaluate(() => {
+ const w = window as unknown as {
+ __bars: unknown[];
+ __barsStop: boolean;
+ __readBarFrame: (t: number) => unknown;
+ };
+ w.__bars = [];
+ w.__barsStop = false;
+ const t0 = performance.now();
+ const tick = () => {
+ w.__bars.push(w.__readBarFrame(performance.now() - t0));
if (!w.__barsStop) {
requestAnimationFrame(tick);
}
@@ -293,6 +312,25 @@ async function traceBars(page: Page): Promise<() => Promise<BarFrame[]>> {
}) as Promise<BarFrame[]>;
}
+/**
+ * The layers read once (`installBarReader`), labelled with the held
+ * timeline time `t`: every held animation sits at `t`, whenever each read
+ * runs.
+ */
+async function readBarsAt(page: Page, t: number): Promise<BarFrame> {
+ return page.evaluate(
+ ms =>
+ (
+ window as unknown as {__readBarFrame: (t: number) => unknown}
+ ).__readBarFrame(ms),
+ t,
+ ) as Promise<BarFrame>;
+}
+
+/** The bar `installBarReader` reads for `key`: the last one marked with it. */
+const bar = (page: Page, key: string) =>
+ page.locator(`[data-navigation-bar="${key}"]`).last();
+
/** The first frame at which `key`'s card has left where it started. */
const firstMove = (frames: BarFrame[], key: string) => {
const start = frames.find(f => f.cards[key] != null)?.cards[key];
@@ -1508,21 +1546,52 @@ test.describe('the bars ride with their screens, as UIKit’s', () => {
test('a programmatic pop rides the revealed bar in from its parallax and the leaving one out', async ({
page,
}) => {
- const {driver, width} = await openStack(page, 'settings');
+ const {driver, stack, width} = await openStack(page, 'settings');
await driver.tap(await rowCenter(page, 'General'));
await waitForRest(card(page, 'screen-General'));
- const stop = await traceBars(page);
- await page.locator('[data-navigation-back]').click();
- await page.waitForTimeout(900);
- const frames = await stop();
- const moving = requireFrames(frames, {
- min: 6,
- during: 'the popped screen and the revealed one move',
- where: f =>
+ await installBarReader(page);
+ // The pointer comes to rest on the button first, so the hold cannot take
+ // a hover run in place of the pop.
+ const back = page.locator('[data-navigation-back]');
+ await back.hover();
+ await waitForRest(back);
+ // Every layer of the pop is held, then read at one timeline time. Two
+ // reads in one live frame can fall on two engine times on a busy runner.
+ // A split read looks like layers out of step.
+ const hold = await armHold(page, stack, {
+ judges: [
+ {target: card(page, 'screen-General'), property: 'translate'},
+ {target: card(page, 'root'), property: 'translate'},
+ {target: bar(page, 'screen-General'), property: 'translate'},
+ {target: bar(page, 'root'), property: 'translate'},
+ {target: back.locator(':scope > *').first(), property: 'opacity'},
+ ],
+ count: 5,
+ newOnly: true,
+ });
+ await back.click();
+ await hold.held();
+ const frames: BarFrame[] = [];
+ for (let i = 0; i <= 36; i++) {
+ frames.push(
+ await readBarsAt(page, await seekAnimations(page, i * 16.667)),
+ );
+ }
+ await releaseAnimations(page, {finish: true});
+ // The pop is over once its back button leaves, within the 900 ms the
+ // live trace gave it.
+ await expect(back).toHaveCount(0, {timeout: 900});
+ const moving = frames.filter(
+ f =>
f.cards['screen-General'] != null &&
f.cards['screen-General'] > 0.5 &&
f.cards.root < -0.5,
- });
+ );
+ // The held course shows both screens moving, in enough samples to judge.
+ expect(
+ moving.length,
+ JSON.stringify(frames.map(f => [f.t, f.cards])),
+ ).toBeGreaterThanOrEqual(6);
for (const f of moving) {
expect(
Math.abs(f.bars['screen-General'] - f.cards['screen-General']),
@@ -1541,7 +1610,6 @@ test.describe('the bars ride with their screens, as UIKit’s', () => {
JSON.stringify({ms: Math.round(ms), ...f.back}),
).toBeLessThan(0.1);
}
- expect(frames.at(-1)?.back ?? null).toBeNull();
});
test('a back swipe holds the revealed bar still while the top one follows the finger', async ({