Skip to content

Commit e06b099

Browse files
committed
test(DropdownMenuItem): the audit captures a row's highlight once it has painted
The audit's 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: the outline drawn, the background transparent. Motion now stops before the interaction, and instead of sleeping 40 ms the capture waits for what it records: fonts loaded, every finite animation and transition finished (the menu's entrance included), and an interacted row's background equal to --color-overlay-hover resolved in its theme and color scheme, then two rendered frames.
1 parent 16fe5f1 commit e06b099

1 file changed

Lines changed: 68 additions & 6 deletions

File tree

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

Lines changed: 68 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 (`component:DropdownMenuItem`). */
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,58 @@ 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: fonts
169+
* loaded, every finite animation and transition on the page finished (the
170+
* menu's entrance included), and an interacted row painting the highlight.
171+
* Then two more frames render, so the screenshot shows those styles.
172+
*/
173+
async function settlePaint(page: Page, highlighted: Locator | null) {
174+
await page.evaluate(async () => {
175+
await document.fonts.ready;
176+
await Promise.all(
177+
document
178+
.getAnimations()
179+
.filter(animation =>
180+
Number.isFinite(
181+
Number(animation.effect?.getComputedTiming().endTime),
182+
),
183+
)
184+
.map(async animation => animation.finished.catch(() => undefined)),
185+
);
186+
});
187+
if (highlighted) {
188+
await expect
189+
.poll(
190+
async () => highlighted.evaluate(paintsHighlight, HIGHLIGHT_TOKEN),
191+
{
192+
message: `the interacted row paints ${HIGHLIGHT_TOKEN}`,
193+
},
194+
)
195+
.toBe(true);
196+
}
197+
await page.evaluate(
198+
async () =>
199+
new Promise<void>(resolve => {
200+
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
201+
}),
202+
);
153203
}
154204

155205
test.beforeAll(async () => {
@@ -211,13 +261,25 @@ for (const item of cases) {
211261
await expect(linkRow).toHaveAttribute('role', 'menuitem');
212262
await expect(menu.locator('button, [role="button"]')).toHaveCount(0);
213263

264+
// Motion stops before the interaction, so a focus or hover starts no
265+
// background transition: interactive rows transition background-color,
266+
// and a transition already running keeps its duration whatever a later
267+
// style sets.
268+
await holdMotionStill(page);
214269
if (item.interaction === 'focus') {
215270
await defaultRow.focus();
216271
await expect(defaultRow).toBeFocused();
217272
} else if (item.interaction === 'hover') {
218273
await supportingRow.hover();
219274
}
220-
await settle(page);
275+
await settlePaint(
276+
page,
277+
item.interaction === 'focus'
278+
? defaultRow
279+
: item.interaction === 'hover'
280+
? supportingRow
281+
: null,
282+
);
221283

222284
const observed = await menu.evaluate(element => {
223285
const currentRows = Array.from(

0 commit comments

Comments
 (0)