Skip to content

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

TypeScript

test(a11y): add hover and pointerAway to the accessibility harness (#7828)

Cindy Zhang54b15c623883
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f920367af9..1b7b182c23 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1751,6 +1751,33 @@ jobs:
retention-days: 30
if-no-files-found: warn
+ # The Chromium harness's own self-tests: what it reads from the engine's
+ # accessibility tree, rendered visibility, and the pointer primitives
+ # (hover, pointerAway) contracts drive. Run them whenever the harness
+ # changes, so a harness change carries its own browser evidence.
+ - name: Run accessibility harness self-tests
+ if: ${{ !cancelled() && github.event_name == 'pull_request' }}
+ env:
+ BASE_SHA: ${{ github.event.pull_request.base.sha }}
+ run: |
+ # The helper comes from the base, since an older PR head lacks it.
+ # Exit 1 means out of scope; 0 in scope; 2 unknown, so run.
+ scope=2
+ helper="$(mktemp)"
+ if git fetch --quiet --no-tags --depth=1 origin "$BASE_SHA" \
+ && git show "$BASE_SHA:.github/scripts/pr-scope-touches.sh" > "$helper"; then
+ scope=0
+ bash "$helper" "$BASE_SHA" '^internal/a11y-spec/src/harness' || scope=$?
+ fi
+ if [ "$scope" = 1 ]; then
+ echo "The accessibility harness is not in PR scope; skipping its self-tests."
+ exit 0
+ fi
+ pnpm exec playwright test \
+ internal/a11y-spec/src/harness/chromium.chromium.spec.ts \
+ --project chromium \
+ --output a11y-harness-output
+
# The accordion contract's header role and name, the page Tab step between
# headers, the absence of arrow-key focus movement, and the single-expansion
# sibling collapse are accessibility-tree and real-browser facts that jsdom
diff --git a/internal/a11y-spec/src/check.test.ts b/internal/a11y-spec/src/check.test.ts
index 63430ba1b0..198863bbb9 100644
--- a/internal/a11y-spec/src/check.test.ts
+++ b/internal/a11y-spec/src/check.test.ts
@@ -51,6 +51,8 @@ function harness(observes: readonly EvidenceLayer[]): Harness {
click: async () => {},
abortedPress: async () => {},
clickBeside: async () => {},
+ hover: async () => {},
+ pointerAway: async () => {},
typeText: async () => {},
clearText: async () => {},
press: async () => {},
diff --git a/internal/a11y-spec/src/harness.ts b/internal/a11y-spec/src/harness.ts
index 6a3e54b4c1..e3df10c8ee 100644
--- a/internal/a11y-spec/src/harness.ts
+++ b/internal/a11y-spec/src/harness.ts
@@ -208,6 +208,16 @@ export interface Harness {
* harness only puts the pointer there.
*/
clickBeside(subject: Subject): Promise<void>;
+ /**
+ * Real-browser layer: move a mouse pointer onto the subject and leave it
+ * resting there, the way a mouse user hovers a control to see its hint.
+ */
+ hover(subject: Subject): Promise<void>;
+ /**
+ * Real-browser layer: move the pointer to a point in the viewport that is
+ * off the subject and every related subject, the way a mouse user moves on.
+ */
+ pointerAway(): Promise<void>;
/** Real-browser layer: type text into the focused subject. */
typeText(subject: Subject, text: string): Promise<void>;
/** Real-browser layer: select and delete all text from the subject. */
diff --git a/internal/a11y-spec/src/harness/chromium.chromium.spec.ts b/internal/a11y-spec/src/harness/chromium.chromium.spec.ts
index 0fe9eee38c..e44212da08 100644
--- a/internal/a11y-spec/src/harness/chromium.chromium.spec.ts
+++ b/internal/a11y-spec/src/harness/chromium.chromium.spec.ts
@@ -3,7 +3,7 @@
/**
* @file chromium.chromium.spec.ts
* @input Uses @playwright/test and the public Chromium harness seam
- * @output Focused accessibility-tree mapping and clipping-aware rendered-visibility tests
+ * @output Focused accessibility-tree mapping, clipping-aware rendered-visibility, and pointer hover/away tests
* @position Harness regression coverage; proves browser AX properties are exposed without DOM substitution.
*/
@@ -176,3 +176,108 @@ test('subjects report rendered visibility', async ({page}) => {
);
expect(await (await harness.related('offscreen')).isVisible()).toBe(true);
});
+
+test('hover rests the pointer on a subject and pointerAway moves it off every subject', async ({
+ page,
+}) => {
+ await page.setContent(`
+ <div style="padding:40px">
+ <button id="trigger">Trigger</button>
+ <span id="tip" style="display:inline-block;padding:8px">Tip</span>
+ </div>
+ <output id="log"></output>
+ <script>
+ for (const id of ['trigger', 'tip']) {
+ const element = document.getElementById(id);
+ element.addEventListener('mouseenter', () => { log.textContent += id + '+ '; });
+ element.addEventListener('mouseleave', () => { log.textContent += id + '- '; });
+ }
+ </script>
+ `);
+ const cdp = await page.context().newCDPSession(page);
+ const harness = createChromiumHarness({
+ page,
+ subject: page.locator('#trigger'),
+ cdp,
+ related: {tip: page.locator('#tip')},
+ });
+
+ await harness.hover(await harness.subject());
+ await harness.hover(await harness.related('tip'));
+ await harness.pointerAway();
+
+ await expect(page.locator('#log')).toHaveText('trigger+ trigger- tip+ tip-');
+});
+
+test('pointerAway skips a candidate point a related subject covers', async ({
+ page,
+}) => {
+ await page.setContent(`
+ <button id="trigger">Trigger</button>
+ <div id="corner" style="position:fixed;right:0;bottom:0;width:80px;height:80px">Corner</div>
+ <output id="log"></output>
+ <script>
+ const corner = document.getElementById('corner');
+ corner.addEventListener('mouseenter', () => { log.textContent += 'corner+ '; });
+ corner.addEventListener('mouseleave', () => { log.textContent += 'corner- '; });
+ </script>
+ `);
+ const cdp = await page.context().newCDPSession(page);
+ const harness = createChromiumHarness({
+ page,
+ subject: page.locator('#trigger'),
+ cdp,
+ related: {corner: page.locator('#corner')},
+ });
+
+ await harness.hover(await harness.related('corner'));
+ await harness.pointerAway();
+
+ await expect(page.locator('#log')).toHaveText('corner+ corner-');
+});
+
+test('pointerAway skips a candidate point the subject pointer target covers', async ({
+ page,
+}) => {
+ await page.setContent(`
+ <label id="target" style="position:fixed;right:0;bottom:0;width:120px;height:60px">
+ <input id="box" type="checkbox"> Remember me
+ </label>
+ <output id="log"></output>
+ <script>
+ const target = document.getElementById('target');
+ target.addEventListener('mouseenter', () => { log.textContent += 'target+ '; });
+ target.addEventListener('mouseleave', () => { log.textContent += 'target- '; });
+ </script>
+ `);
+ const cdp = await page.context().newCDPSession(page);
+ const harness = createChromiumHarness({
+ page,
+ subject: page.locator('#box'),
+ pointerTarget: page.locator('#target'),
+ cdp,
+ });
+
+ await harness.hover(await harness.subject());
+ await harness.pointerAway();
+
+ await expect(page.locator('#log')).toHaveText('target+ target-');
+});
+
+test('pointerAway refuses when every candidate point is on a watched subject', async ({
+ page,
+}) => {
+ await page.setContent(
+ '<div id="cover" style="position:fixed;inset:0">Cover</div>',
+ );
+ const cdp = await page.context().newCDPSession(page);
+ const harness = createChromiumHarness({
+ page,
+ subject: page.locator('#cover'),
+ cdp,
+ });
+
+ await expect(harness.pointerAway()).rejects.toThrow(
+ 'nowhere to move the pointer away to',
+ );
+});
diff --git a/internal/a11y-spec/src/harness/chromium.ts b/internal/a11y-spec/src/harness/chromium.ts
index dd28fee813..510ca9c892 100644
--- a/internal/a11y-spec/src/harness/chromium.ts
+++ b/internal/a11y-spec/src/harness/chromium.ts
@@ -1203,6 +1203,79 @@ export function createChromiumHarness(
await page.mouse.move(releaseX, releaseY);
await page.mouse.up();
},
+ hover: async targetSubject => {
+ const target = pointerTargets.get(targetSubject);
+ if (target == null) {
+ throw new Error(
+ 'the Chromium harness was asked to hover a subject it did not create',
+ );
+ }
+ // The same bounded reach as `click`: a subject a pointer can never rest
+ // on reports why instead of sitting out the whole test timeout.
+ try {
+ await target.hover({timeout: POINTER_REACH_BUDGET_MS});
+ } catch (error) {
+ if (isTimeout(error)) {
+ throw new Error(
+ `a pointer could not reach this subject within ${POINTER_REACH_BUDGET_MS}ms: the browser never found it visible, stable, and able to receive a pointer event. Something is covering it, or it is clipped to nothing.`,
+ {cause: error},
+ );
+ }
+ throw error;
+ }
+ },
+ pointerAway: async () => {
+ const viewport = await page.evaluate(() => ({
+ width: window.innerWidth,
+ height: window.innerHeight,
+ }));
+ // Corners first, then edge midpoints: the first point that is off the
+ // subject and every related subject. A point over one of them would
+ // leave the pointer where the outcome is being judged.
+ const candidates = [
+ [viewport.width - 2, viewport.height - 2],
+ [2, viewport.height - 2],
+ [viewport.width - 2, 2],
+ [2, 2],
+ [viewport.width / 2, viewport.height - 2],
+ [viewport.width - 2, viewport.height / 2],
+ ] as const;
+ // The pointer target is watched too: `hover` rests on it, and a binding's
+ // pointer target can differ from its semantic subject.
+ const watched = [
+ locator,
+ pointerLocator,
+ ...Object.values(options.related ?? {}),
+ ];
+ for (const [x, y] of candidates) {
+ const handles = (
+ await Promise.all(watched.map(item => item.elementHandles()))
+ ).flat();
+ try {
+ const clear = await page.evaluate(
+ ([pointX, pointY, elements]) => {
+ const hit = document.elementFromPoint(pointX, pointY);
+ return (
+ hit != null &&
+ !elements.some(
+ element => element === hit || element.contains(hit),
+ )
+ );
+ },
+ [x, y, handles] as const,
+ );
+ if (clear) {
+ await page.mouse.move(x, y);
+ return;
+ }
+ } finally {
+ await Promise.all(handles.map(handle => handle.dispose()));
+ }
+ }
+ throw new Error(
+ 'every edge of the viewport is covered by the subject or a related subject, so there is nowhere to move the pointer away to',
+ );
+ },
typeText: async (_subject, text) => {
await locator.focus();
await page.keyboard.type(text);
diff --git a/internal/a11y-spec/src/harness/jsdom.ts b/internal/a11y-spec/src/harness/jsdom.ts
index 4d5c3e66f9..158f4928c6 100644
--- a/internal/a11y-spec/src/harness/jsdom.ts
+++ b/internal/a11y-spec/src/harness/jsdom.ts
@@ -198,6 +198,9 @@ export function createJsdomHarness(options: JsdomHarnessOptions): Harness {
unobservable('real-browser', 'a real pointer press'),
clickBeside: async () =>
unobservable('real-browser', 'a real pointer click beside the subject'),
+ hover: async () => unobservable('real-browser', 'a real pointer hover'),
+ pointerAway: async () =>
+ unobservable('real-browser', 'a real pointer moving away'),
typeText: async () =>
unobservable('real-browser', 'real keyboard text entry'),
clearText: async () =>