diff --git a/packages/core/src/utils/__tests__/browser/adaptiveEdges.interaction.browser.spec.ts b/packages/core/src/utils/__tests__/browser/adaptiveEdges.interaction.browser.spec.ts new file mode 100644 index 0000000000000..428fe3ee08390 --- /dev/null +++ b/packages/core/src/utils/__tests__/browser/adaptiveEdges.interaction.browser.spec.ts @@ -0,0 +1,100 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file adaptiveEdges.interaction.browser.spec.ts + * @input fixture.ts bundled with esbuild, mounted in four frames sized at the + * compact edges; the interaction harness's phone + * (playwright.interaction.config.ts), and the same engines with a mouse + * @output Where an adaptive menu takes the bottom sheet: at 767 px wide and + * 499 px tall under a finger, never at 768 or 500, never with a mouse + * @position Real-engine proof of the compact definition (`spec:AST-012` + * FR9) as useAdaptivePresentation reads it + * + * A frame's media queries read the frame's own size, so one page holds all + * four edges; the pointer is the device's. The same page, opened in Safari, + * is the manual Safari check. + */ + +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import {build} from 'esbuild'; +import {expect, test, type Page} from '@playwright/test'; + +const EDGES = [ + {name: 'w767', width: 767, height: 900, compact: true}, + {name: 'w768', width: 768, height: 900, compact: false}, + {name: 'h499', width: 1024, height: 499, compact: true}, + {name: 'h500', width: 1024, height: 500, compact: false}, +] as const; + +let bundle = ''; + +test.beforeAll(async () => { + const result = await build({ + entryPoints: [ + path.join(path.dirname(fileURLToPath(import.meta.url)), 'fixture.ts'), + ], + bundle: true, + write: false, + format: 'iife', + platform: 'browser', + target: 'es2022', + define: {'process.env.NODE_ENV': '"production"'}, + }); + bundle = result.outputFiles[0].text; +}); + +async function expectEdges(page: Page, coarse: boolean) { + await page.goto('about:blank'); + await page.setContent( + '
', + ); + expect( + await page.evaluate(() => matchMedia('(pointer: coarse)').matches), + ).toBe(coarse); + await page.evaluate( + ({edges, script}) => { + for (const edge of edges) { + const frame = document.createElement('iframe'); + frame.name = edge.name; + frame.width = String(edge.width); + frame.height = String(edge.height); + frame.style.border = '0'; + frame.style.display = 'block'; + frame.srcdoc = ``; + document.body.append(frame); + } + }, + {edges: EDGES, script: bundle.replace(/<\/script/gi, '<\\/script')}, + ); + for (const edge of EDGES) { + await expect.poll(() => page.frame({name: edge.name}) != null).toBe(true); + const frame = page.frame({name: edge.name}); + if (frame == null) { + throw new Error(`no frame named ${edge.name}`); + } + await frame.waitForFunction(() => window.__answers != null); + const answers = await frame.evaluate(() => window.__answers); + expect(answers, edge.name).toMatchObject({ + width: edge.width, + height: edge.height, + coarse, + presentation: {isTouch: coarse, isCompact: edge.compact}, + adaptive: coarse && edge.compact ? 'bottom-sheet' : 'popover', + }); + } +} + +test('under a finger, an adaptive menu takes the sheet only below the compact edges', async ({ + page, +}) => { + await expectEdges(page, true); +}); + +test.describe('with a mouse', () => { + test.use({hasTouch: false, isMobile: false}); + + test('an adaptive menu never takes the sheet', async ({page}) => { + await expectEdges(page, false); + }); +}); diff --git a/packages/core/src/utils/__tests__/browser/fixture.ts b/packages/core/src/utils/__tests__/browser/fixture.ts new file mode 100644 index 0000000000000..ab50e7ff6124f --- /dev/null +++ b/packages/core/src/utils/__tests__/browser/fixture.ts @@ -0,0 +1,70 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +'use client'; + +/** + * @file fixture.ts + * @input Nothing: bundled by esbuild and injected into a blank page + * @output The resolver's answers on `window.__answers` (hooks after + * hydration, the one-moment read, the engine facts), and two styled probes: + * one under the touch query, one under the iOS gate written as core writes + * it (the media query outside, the supports query inside) + * @position Browser fixture of touchPresentation.interaction.browser.spec.ts + * and adaptiveEdges.interaction.browser.spec.ts; test-only + */ + +import {createElement, useEffect} from 'react'; +import {createRoot} from 'react-dom/client'; +import { + IOS_SUPPORTS_QUERY, + IOS_TOUCH_STYLE_KEYS, + TOUCH_QUERY, + readTouchPresentation, + useTouchPresentation, +} from '../../touchPresentation'; +import {useAdaptivePresentation} from '../../../hooks/useAdaptivePresentation'; +import {DEFAULT_COMPACT_DEFINITION} from '../../../theme/themeAdaptations'; + +declare global { + interface Window { + __answers: Record