An open source design system that's fully customizable and agent ready
TypeScript
test(Popover): the rendering proof waits for the content to settle (#10326)
Christopher Chedeau39f244171587
diff --git a/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts b/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.tsindex 4cd96b5035..6fde845250 100644--- a/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts+++ b/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts@@ -101,6 +101,12 @@ interface Box {height: number;}+/** When the touch hit areas' new insets were written and drawn, epoch ms. */+interface HitAreaInsets {+ written: number;+ drawn: number;+}+interface OpenStory {driver: TouchDriver;trigger: Locator;@@ -271,6 +277,9 @@ const LONG_MOTIONS =/** Where each rendering window starts, ms after its motion starts. */const WINDOW_START_MS = 400;+/** The shortest rendering window the proof judges, ms: about 18 frames. */+const MIN_WINDOW_MS = 300;+/** The blocked-thread proof's growth: 600 ms at a constant speed. */const STEADY_GROWTH ='[popover]:has([role="dialog"]) { --duration-medium-min: 600ms; --ease-entrance: linear; }';@@ -315,6 +324,70 @@ async function waitPastScaleStart();}+/**+ * The motion promotions the popover's content holds, read from inline styles:+ * an element's `will-change`, or the custom property that promotes its+ * `::before` or `::after`. Reading them restyles nothing.+ */+async function contentPromotions(layer: Locator): Promise<string[]> {+ return layer.locator('[role="dialog"]').evaluate(dialog =>+ Array.from(dialog.querySelectorAll<HTMLElement>('*')).flatMap(element => {+ const name = element.classList.item(0) ?? element.tagName;+ return Array.from(element.style)+ .filter(+ property =>+ property === 'will-change' ||+ property.startsWith('--astryx-motion-will-change-'),+ )+ .map(+ property =>+ `${name} ${property}: ${element.style.getPropertyValue(property)}`,+ );+ }),+ );+}++/**+ * Logs each time the content's touch hit areas take new insets: when the+ * style write runs, and when the next frame has drawn, epoch ms. The mutation+ * observer reads no layout, and its one frame callback runs before any window+ * that waits for it.+ */+async function logHitAreaInsets(page: Page) {+ await page.evaluate(() => {+ const log: HitAreaInsets[] = [];+ Object.defineProperty(window, '__popoverHitAreaInsets', {value: log});+ const isInsets = (style: string | null) =>+ style?.includes('--astryx-hit-inset-') ?? false;+ new MutationObserver(records => {+ if (+ !records.some(+ record =>+ isInsets(record.oldValue) ||+ isInsets((record.target as Element).getAttribute('style')),+ )+ ) {+ return;+ }+ const entry = {+ written: performance.timeOrigin + performance.now(),+ drawn: 0,+ };+ log.push(entry);+ requestAnimationFrame(() =>+ setTimeout(() => {+ entry.drawn = performance.timeOrigin + performance.now();+ }),+ );+ }).observe(document.body, {+ attributes: true,+ attributeFilter: ['style'],+ attributeOldValue: true,+ subtree: true,+ });+ });+}+/** The static copy the dismissal plays on. */const exitCopyOf = (page: Page) =>page.locator('[data-astryx-popover-exit]').first();@@ -602,6 +675,17 @@ test('the presentation and the dismissal do no Layout or Paint per frame', asyncconst exit = await themeTiming(story.layer, POPOVER_EXIT_TOKENS);expect(entrance.ms).toBe(1200);expect(exit.ms).toBe(1200);+ // Each switch's motion engine promotes its fades as the story mounts and+ // ends that promotion 500 ms later, with a style write. During a+ // presentation, that write makes Chromium lay out this anchored layer. So+ // the presentation starts once nothing in the content holds a promotion.+ await expect+ .poll(async () => contentPromotions(story.layer), {+ message: 'promotions left in the popover content',+ intervals: [50],+ timeout: 10_000,+ })+ .toEqual([]);// It opens from the keyboard to keep the trigger's own press feedback out// of the measurement. The phone's primary pointer is still coarse, so the// touch presentation runs. Nothing reads the page inside the windows: on@@ -610,31 +694,59 @@ test('the presentation and the dismissal do no Layout or Paint per frame', async// reads every animation each frame, so the windows come from when each// scale animation starts.await story.trigger.focus();+ await logHitAreaInsets(page);+ let opened = 0;+ let closed = 0;let present = 0;let dismiss = 0;const trace = await traceRendering(page, async () => {- const opened = await pageNow(page);+ opened = await pageNow(page);await page.keyboard.press('Enter');present = await waitPastScaleStart(page, opened, entrance.ms + 150);- const closed = await pageNow(page);+ closed = await pageNow(page);await page.keyboard.press('Escape');dismiss = await waitPastScaleStart(page, closed, exit.ms + 150);});- // Each window starts 400 ms in, after the focus ring and the touch hit- // areas' first measurement, and ends one frame before its curve does. A- // page frame callback makes Chromium lay out this anchored layer every- // frame. So these zeros also show that no frame loop runs here, Popover's- // own included.- for (const [name, base, ms] of [- ['present', present, entrance.ms],- ['dismiss', dismiss, exit.ms],+ const insets = await page.evaluate(() => [+ ...(window as unknown as {__popoverHitAreaInsets: HitAreaInsets[]})+ .__popoverHitAreaInsets,+ ]);+ // Each window starts 400 ms in, after the focus ring, and ends one frame+ // before its curve does. Each motion's start also measures the content's+ // touch hit areas: layout reads, then a style write. A stalled machine can+ // delay that past 400 ms and spread it over frames, a few controls each. So+ // a window starts no sooner than the frame that draws the last of those+ // insets. A page frame callback makes Chromium lay out this anchored layer+ // every frame. So these zeros also show that no frame loop runs here,+ // Popover's own included.+ for (const [name, action, base, ms] of [+ ['present', opened, present, entrance.ms],+ ['dismiss', closed, dismiss, exit.ms],] as const) {- const from = base + WINDOW_START_MS;const end = base + ms - 20;+ // A write that lands before the previous one has drawn belongs to the+ // same pass.+ let drawn = 0;+ for (const entry of insets) {+ if (entry.written < action || entry.written >= end) {+ continue;+ }+ if (drawn !== 0 && entry.written > drawn) {+ break;+ }+ drawn = Math.max(drawn, entry.drawn);+ }+ const from = Math.max(base + WINDOW_START_MS, drawn);+ // A late setup leaves too short a window to judge, so it fails by name.+ expect(+ end - from,+ `${name}: the hit areas drew ${Math.round(from - base)} ms in, too late to leave a window`,+ ).toBeGreaterThanOrEqual(MIN_WINDOW_MS);const during = countBetween(trace, from, end);// On failure, every rendering event of the window, in ms after its// animation's start.const detail = `${name} ${JSON.stringify({+ from: Math.round(from - base),during,events: trace.events.filter(e => e.t >= from && e.t <= end)