An open source design system that's fully customizable and agent ready
TypeScript
test(interaction): a fine-pointer page that really reads (pointer: fine) (#8345)
Christopher Chedeau2d8681789d38
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.mdindex d62e2483d8..6dcaef089c 100644--- a/internal/interaction-harness/README.md+++ b/internal/interaction-harness/README.md@@ -64,6 +64,16 @@ committed: a pull request links or attaches it.| `storyboard.ts` | `animationStoryboard` (seek to t = 0, 16.7, 33.3 … ms), `touchStoryboard` (a frame per scripted event), `castStoryboard` (real screencast frames): PNG contact sheet + JSON trace. || `output.ts` | `evidenceDir`, `writeEvidence`: where evidence goes and how it is attached to the report. || `story.ts` | `phone`, `withStorybook`, `openStory`, `frames`, and the timer gate (`installTimerGate`, `freezeTimers`, `thawTimers`). |+| `pointer.ts` | `newFinePointerPage(browser)`: a page under a mouse that really reads `(pointer: fine)`. `assertFinePointer` / `assertCoarsePointer`: fail before a case's checks when the page reads the other pointer. |++## A fine pointer++Every interaction project is the touch phone, and a context a test makes with+`browser.newContext()` inherits it: it reads `(pointer: coarse)` unless it+passes `hasTouch: false`. A case that shows touch work costs nothing under a+mouse opens its page with `newFinePointerPage(browser)`, which turns touch and+mobile off and checks the pointer first. A case that builds its own context+calls `assertFinePointer(page)` before its checks.## The proofsdiff --git a/internal/interaction-harness/src/index.ts b/internal/interaction-harness/src/index.tsindex e4d636021e..6c6b32e7e5 100644--- a/internal/interaction-harness/src/index.ts+++ b/internal/interaction-harness/src/index.ts@@ -20,6 +20,7 @@ export * from './frames';export * from './freeze';export * from './math';export * from './output';+export * from './pointer';export * from './scripts';export * from './story';export * from './storyboard';diff --git a/internal/interaction-harness/src/pointer.interaction.browser.spec.ts b/internal/interaction-harness/src/pointer.interaction.browser.spec.tsnew file mode 100644index 0000000000..7e6e3081a5--- /dev/null+++ b/internal/interaction-harness/src/pointer.interaction.browser.spec.ts@@ -0,0 +1,80 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file pointer.interaction.browser.spec.ts+ * @input Real Chromium/WebKit in the interaction projects (the touch phone)+ * @output Proof that `newFinePointerPage` gives a page that reads a fine+ * pointer, that a context made inside a test does not, and that the guards+ * catch both.+ * @position Why a fine-pointer case may trust `newFinePointerPage`.+ */++import {expect, test} from '@astryxdesign/a11y-spec/playwright';+import {+ assertCoarsePointer,+ assertFinePointer,+ newFinePointerPage,+ readPointer,+} from './index';++test('a context made inside a test inherits the touch phone: the guard catches it', async ({+ browser,+}) => {+ const context = await browser.newContext({+ viewport: {width: 1280, height: 800},+ });+ try {+ const page = await context.newPage();+ expect((await readPointer(page)).coarse).toBe(true);+ await expect(assertFinePointer(page)).rejects.toThrow(+ /Not a fine pointer: the page reads \(pointer: coarse\) true/,+ );+ } finally {+ await context.close();+ }+});++test('newFinePointerPage reads a fine pointer with hover and no touch', async ({+ browser,+}) => {+ const {context, page} = await newFinePointerPage(browser);+ try {+ expect(await readPointer(page)).toEqual({+ coarse: false,+ fine: true,+ anyCoarse: false,+ hover: true,+ maxTouchPoints: 0,+ });+ await page.setContent(+ '<style>@media (pointer: coarse) { p { color: red } }</style><p>x</p>',+ );+ expect(+ await page.locator('p').evaluate(p => getComputedStyle(p).color),+ ).toBe('rgb(0, 0, 0)');+ await expect(assertCoarsePointer(page)).rejects.toThrow(+ /Not a coarse pointer/,+ );+ } finally {+ await context.close();+ }+});++test('options cannot turn touch back on', async ({browser}) => {+ const {context, page} = await newFinePointerPage(browser, {+ hasTouch: true,+ isMobile: true,+ viewport: {width: 800, height: 600},+ });+ try {+ expect((await readPointer(page)).fine).toBe(true);+ expect(page.viewportSize()).toEqual({width: 800, height: 600});+ } finally {+ await context.close();+ }+});++test("the phone project's own page reads a coarse pointer", async ({page}) => {+ await assertCoarsePointer(page);+ await expect(assertFinePointer(page)).rejects.toThrow(/Not a fine pointer/);+});diff --git a/internal/interaction-harness/src/pointer.ts b/internal/interaction-harness/src/pointer.tsnew file mode 100644index 0000000000..4f2ca048fa--- /dev/null+++ b/internal/interaction-harness/src/pointer.ts@@ -0,0 +1,104 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file pointer.ts+ * @input Uses a Playwright `Browser` and `Page` (Chromium or WebKit)+ * @output `desktop` (the fine-pointer browser context), `newFinePointerPage`+ * (a page in that context, checked), and the guards `assertFinePointer` /+ * `assertCoarsePointer`.+ * @position The other half of every "under a finger" proof: touch work must+ * cost nothing under a fine pointer, and a proof of that has to run under a+ * pointer the page really reads as fine.+ *+ * A context a test makes with `browser.newContext()` inherits the project's+ * own `use` options, and every interaction project is the touch phone: such a+ * context matches `(pointer: coarse)` unless it says `hasTouch: false` itself+ * (measured with Playwright 1.61, Chromium and WebKit). So a fine-pointer case+ * opens its page through `newFinePointerPage`, which forces `hasTouch: false`+ * and `isMobile: false` and fails before the test's checks when the page still+ * reads a coarse pointer.+ */++import type {+ Browser,+ BrowserContext,+ BrowserContextOptions,+ Page,+} from '@playwright/test';++/** A desktop browser with a mouse: fine pointer, hover, no touch. */+export const desktop = {+ viewport: {width: 1280, height: 800},+ deviceScaleFactor: 1,+ isMobile: false,+ hasTouch: false,+} as const;++/** What the page itself reads about its pointer. */+export interface PointerReading {+ coarse: boolean;+ fine: boolean;+ anyCoarse: boolean;+ hover: boolean;+ maxTouchPoints: number;+}++export async function readPointer(page: Page): Promise<PointerReading> {+ return page.evaluate(() => ({+ coarse: matchMedia('(pointer: coarse)').matches,+ fine: matchMedia('(pointer: fine)').matches,+ anyCoarse: matchMedia('(any-pointer: coarse)').matches,+ hover: matchMedia('(hover: hover)').matches,+ maxTouchPoints: navigator.maxTouchPoints,+ }));+}++/**+ * Fails loudly unless the page reads a fine primary pointer and no coarse+ * one. Call it before a fine-pointer case's checks when the case makes its+ * own context.+ */+export async function assertFinePointer(page: Page): Promise<void> {+ const reading = await readPointer(page);+ if (reading.coarse || !reading.fine || reading.anyCoarse) {+ throw new Error(+ `Not a fine pointer: the page reads (pointer: coarse) ${reading.coarse}, ` ++ `(pointer: fine) ${reading.fine}, (any-pointer: coarse) ${reading.anyCoarse}. ` ++ 'A context made inside a test inherits the touch phone project; open the ' ++ 'page with newFinePointerPage(browser), or pass hasTouch: false and ' ++ 'isMobile: false yourself.',+ );+ }+}++/** Fails loudly unless the page reads a coarse primary pointer. */+export async function assertCoarsePointer(page: Page): Promise<void> {+ const reading = await readPointer(page);+ if (!reading.coarse || reading.fine) {+ throw new Error(+ `Not a coarse pointer: the page reads (pointer: coarse) ${reading.coarse}, ` ++ `(pointer: fine) ${reading.fine}. Run the case in the phone project's own ` ++ 'page, or a context with hasTouch: true.',+ );+ }+}++/**+ * Opens a page under a fine pointer: the `desktop` context, with `options`+ * layered on top (a viewport, a color scheme), except that touch and mobile+ * stay off. Checks the pointer before returning. Close the context when done.+ */+export async function newFinePointerPage(+ browser: Browser,+ options: BrowserContextOptions = {},+): Promise<{context: BrowserContext; page: Page}> {+ const context = await browser.newContext({+ ...desktop,+ ...options,+ isMobile: false,+ hasTouch: false,+ });+ const page = await context.newPage();+ await assertFinePointer(page);+ return {context, page};+}