|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file MenuPressContinuation.interaction.browser.spec.ts |
| 5 | + * @input The built `a11y/Menu press continuation` story, real touch input |
| 6 | + * (Chromium CDP / WebKit's input command), the interaction harness, and the |
| 7 | + * menu press's handoff distance from the UIKit constants table |
| 8 | + * @output Interaction proofs for a pointer handed to a menu as it opens |
| 9 | + * (`module:DropdownMenu/useMenuPress` FR11), under a finger (the same on |
| 10 | + * every device, `spec:AST-094` FR43): held on a button until the menu opens |
| 11 | + * under it, the finger picks the row it lifts on only once it has travelled the |
| 12 | + * handoff distance from the first position the menu sees; a lift before |
| 13 | + * that acts on nothing and leaves the menu open |
| 14 | + * @position The real-browser case FR11's verification names, beside the |
| 15 | + * continuation suite in useMenuPress.test.tsx; the fixture is |
| 16 | + * menuPressContinuation.fixture.tsx |
| 17 | + */ |
| 18 | + |
| 19 | +import {expect, test, type Page} from '@playwright/test'; |
| 20 | +import { |
| 21 | + frames, |
| 22 | + openStory, |
| 23 | + touchDriver, |
| 24 | + withStorybook, |
| 25 | + type TouchDriver, |
| 26 | + type TouchFrame, |
| 27 | +} from '@astryxdesign/interaction-harness'; |
| 28 | +import {UIKIT_MENU_PRESS} from '../../motion/uikit/gestures'; |
| 29 | +import { |
| 30 | + CONTINUED_PRESS_HOLD_MS, |
| 31 | + CONTINUED_PRESS_ROW_PX, |
| 32 | +} from './menuPressContinuation.geometry'; |
| 33 | + |
| 34 | +const storybook = withStorybook(test); |
| 35 | + |
| 36 | +interface OpenFixture { |
| 37 | + driver: TouchDriver; |
| 38 | + /** The button's center, where the finger lands. */ |
| 39 | + center: {x: number; y: number}; |
| 40 | + /** The center of a row of the menu that opens under the button. */ |
| 41 | + rowCenter: (index: number) => {x: number; y: number}; |
| 42 | +} |
| 43 | + |
| 44 | +async function openFixture(page: Page): Promise<OpenFixture> { |
| 45 | + await openStory( |
| 46 | + page, |
| 47 | + storybook(), |
| 48 | + 'a11y-menu-press-continuation--continued-press', |
| 49 | + ); |
| 50 | + const driver = await touchDriver(page); |
| 51 | + const box = await page.getByRole('button', {name: 'Hold me'}).boundingBox(); |
| 52 | + if (box == null) { |
| 53 | + throw new Error('no button'); |
| 54 | + } |
| 55 | + const center = {x: box.x + box.width / 2, y: box.y + box.height / 2}; |
| 56 | + // The menu renders right below the button, one 44 px row after another. |
| 57 | + const rowCenter = (index: number) => ({ |
| 58 | + x: center.x, |
| 59 | + y: box.y + box.height + CONTINUED_PRESS_ROW_PX * (index + 0.5), |
| 60 | + }); |
| 61 | + return {driver, center, rowCenter}; |
| 62 | +} |
| 63 | + |
| 64 | +const picked = (page: Page) => page.getByTestId('picked'); |
| 65 | + |
| 66 | +/** One finger at `point` at time `t`; no point lifts it. */ |
| 67 | +const at = (t: number, point?: {x: number; y: number}): TouchFrame => ({ |
| 68 | + t, |
| 69 | + fingers: point == null ? [] : [{id: 0, ...point}], |
| 70 | +}); |
| 71 | + |
| 72 | +/** When the finger first moves: after the menu has opened under it. */ |
| 73 | +const FIRST_MOVE_MS = CONTINUED_PRESS_HOLD_MS + 250; |
| 74 | + |
| 75 | +test('a finger handed to the menu as it opens picks the row it lifts on, past the handoff distance (module FR11)', async ({ |
| 76 | + page, |
| 77 | +}) => { |
| 78 | + const fixture = await openFixture(page); |
| 79 | + // Held until the menu opens under it; the first position the menu sees is |
| 80 | + // over the first row, and the finger then slides a whole row, past the |
| 81 | + // handoff distance, onto the second and lifts there. |
| 82 | + const alpha = fixture.rowCenter(0); |
| 83 | + const bravo = fixture.rowCenter(1); |
| 84 | + await fixture.driver.play([ |
| 85 | + at(0, fixture.center), |
| 86 | + at(FIRST_MOVE_MS, alpha), |
| 87 | + at(FIRST_MOVE_MS + 50, {x: alpha.x, y: (alpha.y + bravo.y) / 2}), |
| 88 | + at(FIRST_MOVE_MS + 100, bravo), |
| 89 | + at(FIRST_MOVE_MS + 160, bravo), |
| 90 | + at(FIRST_MOVE_MS + 220), |
| 91 | + ]); |
| 92 | + await expect(picked(page)).toHaveText('Bravo'); |
| 93 | + await expect(page.getByRole('menu')).toHaveCount(0); |
| 94 | +}); |
| 95 | + |
| 96 | +test('a lift before the handoff distance acts on nothing and leaves the menu open', async ({ |
| 97 | + page, |
| 98 | +}) => { |
| 99 | + const fixture = await openFixture(page); |
| 100 | + const handoff = UIKIT_MENU_PRESS.handoff.value; |
| 101 | + // Held until the menu opens; the first position the menu sees is already |
| 102 | + // over the first row, and the finger moves less than the handoff distance |
| 103 | + // there before it lifts. Under the settle rule this lift would pick the row. |
| 104 | + const alpha = fixture.rowCenter(0); |
| 105 | + await fixture.driver.play([ |
| 106 | + at(0, fixture.center), |
| 107 | + at(FIRST_MOVE_MS, alpha), |
| 108 | + at(FIRST_MOVE_MS + 50, {x: alpha.x, y: alpha.y + handoff / 2}), |
| 109 | + at(FIRST_MOVE_MS + 160, {x: alpha.x, y: alpha.y + handoff / 2}), |
| 110 | + at(FIRST_MOVE_MS + 220), |
| 111 | + ]); |
| 112 | + await frames(page, 2); |
| 113 | + await expect(page.getByRole('menu')).toBeVisible(); |
| 114 | + await expect(picked(page)).toHaveText(''); |
| 115 | +}); |
0 commit comments