Skip to content

Commit d40b519

Browse files
authored
test(DropdownMenuItem): the audit opens its menu and captures a row's highlight under load (#9495)
test(DropdownMenuItem): the audit opens its menu and captures a row's highlight under load Two load causes in the item-states audit. The capture. The focus and hover cases read the interacted row's background right after the interaction, but the row transitions background-color (Item's interactive rows, --duration-fast-min): a focus draws the outline at once and starts the background's transition, and the spec stopped motion only after the interaction, when a running transition keeps its duration. With starved frames the capture read the background before the transition started. Motion now stops before the interaction, and instead of sleeping 40 ms the capture waits for fonts, every finite animation and transition to finish, and an interacted row's background to equal --color-overlay-hover resolved in its theme and color scheme, then two rendered frames. That wait doesn't fail by itself: a row that never paints still writes its receipt and screenshot, and the receipt's check fails. The opening. The story's play opened the menu with a click that carries no coordinates, so its release landed at the page's corner. DropdownMenu lets the release of the press that opened it act once the press has lasted 300 ms, and on a loaded machine the open's own render takes that long; a mouse release outside the trigger and the menu then closes the menu it opened. The play now presses and releases at the trigger's center, where a release never closes it.
1 parent 45cc3fe commit d40b519

2 files changed

Lines changed: 80 additions & 9 deletions

File tree

‎apps/storybook/stories/DropdownMenuItem.stories.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,20 @@ export const ItemStates: Story = {
8989
render: () => <ItemStatesFixture />,
9090
play: async ({canvasElement}) => {
9191
const canvas = within(canvasElement);
92-
await userEvent.click(
93-
canvas.getByRole('button', {name: 'Open item evidence'}),
94-
);
92+
const trigger = canvas.getByRole('button', {name: 'Open item evidence'});
93+
const box = trigger.getBoundingClientRect();
94+
// Press and release on the trigger itself. A click without coordinates
95+
// releases at the page's corner. The open's own render can outlast the
96+
// menu's settle delay on a loaded machine. After that delay, a release
97+
// outside the trigger and the menu closes it.
98+
await userEvent.pointer({
99+
keys: '[MouseLeft]',
100+
target: trigger,
101+
coords: {
102+
clientX: box.x + box.width / 2,
103+
clientY: box.y + box.height / 2,
104+
},
105+
});
95106
const menu = await within(document.body).findByRole('menu');
96107
const scope = within(menu);
97108
await expect(scope.getAllByRole('menuitem')).toHaveLength(5);

‎packages/core/src/DropdownMenu/__tests__/DropdownMenuItemAudit.a11y.chromium.spec.ts‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {createHash} from 'node:crypto';
1212
import {execFileSync} from 'node:child_process';
1313
import * as fs from 'node:fs';
1414
import * as path from 'node:path';
15-
import {expect, test, type Page} from '@playwright/test';
15+
import {expect, test, type Locator, type Page} from '@playwright/test';
1616
import {holdMotionStill} from '@astryxdesign/a11y-spec/chromium';
1717
import {
1818
DEFAULT_STORYBOOK_DIR,
@@ -22,6 +22,8 @@ import {
2222

2323
const OUTPUT = path.resolve('dropdown-menu-item-audit-evidence');
2424
const STORY_ID = 'core-dropdownmenuitem--item-states';
25+
/** The overlay a focused or hovered row paints: its `:focus` background in DropdownMenuItem.tsx. */
26+
const HIGHLIGHT_TOKEN = '--color-overlay-hover';
2527
const WIDE = {width: 1024, height: 768};
2628
const NARROW = {width: 320, height: 720};
2729

@@ -146,10 +148,57 @@ function storyUrl(item: AuditCase) {
146148
return `${server.origin}/iframe.html?id=${STORY_ID}&viewMode=story&globals=colorMode:${item.mode};astryxTheme:${item.theme};direction:${item.direction}`;
147149
}
148150

149-
async function settle(page: Page) {
150-
await holdMotionStill(page);
151-
await page.evaluate(async () => document.fonts.ready);
152-
await page.waitForTimeout(40);
151+
/**
152+
* Whether the row paints the highlight token, resolved where the row is (its
153+
* theme and color scheme). An unresolved token reads transparent and fails.
154+
*/
155+
function paintsHighlight(row: Element, token: string): boolean {
156+
const probe = document.createElement('span');
157+
probe.style.backgroundColor = `var(${token})`;
158+
row.append(probe);
159+
const expected = getComputedStyle(probe).backgroundColor;
160+
probe.remove();
161+
return (
162+
expected !== 'rgba(0, 0, 0, 0)' &&
163+
getComputedStyle(row).backgroundColor === expected
164+
);
165+
}
166+
167+
/**
168+
* Waits, without sleeping, until what the capture records is painted. It
169+
* waits for fonts and for every finite animation and transition, the menu's
170+
* entrance included. It also waits for an interacted row to paint the
171+
* highlight. If that poll times out, the receipt's checks decide. Then two
172+
* more frames render before the receipt is read.
173+
*/
174+
async function settlePaint(page: Page, highlighted: Locator | null) {
175+
await page.evaluate(async () => {
176+
await document.fonts.ready;
177+
await Promise.all(
178+
document
179+
.getAnimations()
180+
.filter(animation =>
181+
Number.isFinite(
182+
Number(animation.effect?.getComputedTiming().endTime),
183+
),
184+
)
185+
.map(async animation => animation.finished.catch(() => undefined)),
186+
);
187+
});
188+
if (highlighted) {
189+
// Waits for the highlight without failing here: a row that never paints
190+
// still writes its receipt and screenshot, and interactionPaintPass fails.
191+
await expect
192+
.poll(async () => highlighted.evaluate(paintsHighlight, HIGHLIGHT_TOKEN))
193+
.toBe(true)
194+
.catch(() => undefined);
195+
}
196+
await page.evaluate(
197+
async () =>
198+
new Promise<void>(resolve => {
199+
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
200+
}),
201+
);
153202
}
154203

155204
test.beforeAll(async () => {
@@ -211,13 +260,24 @@ for (const item of cases) {
211260
await expect(linkRow).toHaveAttribute('role', 'menuitem');
212261
await expect(menu.locator('button, [role="button"]')).toHaveCount(0);
213262

263+
// Motion stops before the interaction, so a focus or hover starts no
264+
// background transition. Interactive rows transition background-color,
265+
// and a running transition keeps its duration whatever a later style sets.
266+
await holdMotionStill(page);
214267
if (item.interaction === 'focus') {
215268
await defaultRow.focus();
216269
await expect(defaultRow).toBeFocused();
217270
} else if (item.interaction === 'hover') {
218271
await supportingRow.hover();
219272
}
220-
await settle(page);
273+
await settlePaint(
274+
page,
275+
item.interaction === 'focus'
276+
? defaultRow
277+
: item.interaction === 'hover'
278+
? supportingRow
279+
: null,
280+
);
221281

222282
const observed = await menu.evaluate(element => {
223283
const currentRows = Array.from(

0 commit comments

Comments
 (0)