|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file ButtonPress.interaction.browser.spec.ts |
| 5 | + * @input The built press feedback stories (Buttons, Buttons in a scroller), |
| 6 | + * real touch input (Chromium CDP / WebKit's input command), the |
| 7 | + * interaction harness, and the control profile's clocks |
| 8 | + * (gestures/PressTracker.ts, PRESS_PROFILES) |
| 9 | + * @output Interaction proofs for the control press profile: outside a |
| 10 | + * scroller a finger's press highlights in the frame it lands and releases |
| 11 | + * on UIButton's clock; inside one it waits UIScrollView's content-touch |
| 12 | + * delay, a flick paints nothing, and a scroll that takes a held press |
| 13 | + * cancels it with no fade and no click; a quick tap highlights at the lift; |
| 14 | + * the engine's own tap paints and activates once; a storyboard of a press, |
| 15 | + * its trace asserted |
| 16 | + * @position Interaction evidence for the press model's control profile |
| 17 | + * (gestures/PressTracker.ts, gestures/delegatedPress.ts as installed by |
| 18 | + * hooks/usePressFeedback.ts) |
| 19 | + */ |
| 20 | + |
| 21 | +import {expect, test, type Locator, type Page} from '@playwright/test'; |
| 22 | +import { |
| 23 | + flick, |
| 24 | + freezeTimers, |
| 25 | + holdThenDrag, |
| 26 | + installTimerGate, |
| 27 | + openStory, |
| 28 | + tap, |
| 29 | + thawTimers, |
| 30 | + touchDriver, |
| 31 | + touchStoryboard, |
| 32 | + withStorybook, |
| 33 | + type TouchDriver, |
| 34 | +} from '@astryxdesign/interaction-harness'; |
| 35 | +import {PRESS_PROFILES} from '../../gestures/PressTracker'; |
| 36 | +import {scroll} from '../../motion/uikit/scroll'; |
| 37 | + |
| 38 | +const storybook = withStorybook(test); |
| 39 | + |
| 40 | +/** The control profile's release clock: UIButton's. */ |
| 41 | +const RELEASE_MS = PRESS_PROFILES.control.releaseMs; |
| 42 | +const CONTENT_TOUCH_DELAY_MS = scroll.delaysContentTouchesMs; |
| 43 | +/** Long enough that a press in a scroller is believed. */ |
| 44 | +const HOLD_MS = 400; |
| 45 | +/** Two frames at 60 Hz: "in the frame the finger lands", with dispatch slack. */ |
| 46 | +const SAME_FRAME_MS = 34; |
| 47 | + |
| 48 | +const BUTTONS = 'core-press-feedback-touch--buttons'; |
| 49 | +const SCROLLER = 'core-press-feedback-touch--buttons-in-scroller'; |
| 50 | + |
| 51 | +interface Opened { |
| 52 | + driver: TouchDriver; |
| 53 | + surface: Locator; |
| 54 | + center: {x: number; y: number}; |
| 55 | + /** What the page saw, on its own clock (performance.now()). */ |
| 56 | + log: () => Promise<LogEntry[]>; |
| 57 | +} |
| 58 | + |
| 59 | +type LogEntry = [kind: string, t: number, value: string | null]; |
| 60 | + |
| 61 | +async function openSurface( |
| 62 | + page: Page, |
| 63 | + story: string, |
| 64 | + surface: (page: Page) => Locator, |
| 65 | +): Promise<Opened> { |
| 66 | + await openStory(page, storybook(), story); |
| 67 | + const driver = await touchDriver(page); |
| 68 | + const element = surface(page); |
| 69 | + await expect(element).toBeVisible(); |
| 70 | + const box = await element.boundingBox(); |
| 71 | + if (box == null) { |
| 72 | + throw new Error(`${story}: the surface has no box`); |
| 73 | + } |
| 74 | + await element.evaluate(el => { |
| 75 | + const entries: LogEntry[] = []; |
| 76 | + (window as unknown as {__press: LogEntry[]}).__press = entries; |
| 77 | + for (const type of ['pointerdown', 'pointerup', 'pointercancel', 'click']) { |
| 78 | + document.addEventListener( |
| 79 | + type, |
| 80 | + () => entries.push([type, performance.now(), null]), |
| 81 | + true, |
| 82 | + ); |
| 83 | + } |
| 84 | + new MutationObserver(records => { |
| 85 | + for (const record of records) { |
| 86 | + entries.push([ |
| 87 | + 'press', |
| 88 | + performance.now(), |
| 89 | + (record.target as Element).getAttribute('data-astryx-press'), |
| 90 | + ]); |
| 91 | + } |
| 92 | + }).observe(el, {attributes: true, attributeFilter: ['data-astryx-press']}); |
| 93 | + }); |
| 94 | + return { |
| 95 | + driver, |
| 96 | + surface: element, |
| 97 | + center: {x: box.x + box.width / 2, y: box.y + box.height / 2}, |
| 98 | + log: async () => |
| 99 | + page.evaluate(() => (window as unknown as {__press: LogEntry[]}).__press), |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +const secondaryButton = (page: Page) => |
| 104 | + page.getByRole('button', {name: 'Secondary', exact: true}); |
| 105 | +const scrollerButton = (page: Page) => |
| 106 | + page.getByRole('button', {name: 'Action 2', exact: true}); |
| 107 | + |
| 108 | +const first = (entries: LogEntry[], kind: string, value?: string | null) => |
| 109 | + entries.find( |
| 110 | + ([k, , v]) => k === kind && (value === undefined || v === value), |
| 111 | + ); |
| 112 | + |
| 113 | +test('outside a scroller, a finger’s press highlights in the frame it lands, and the lift fades it on UIButton’s clock', async ({ |
| 114 | + page, |
| 115 | +}) => { |
| 116 | + const opened = await openSurface(page, BUTTONS, secondaryButton); |
| 117 | + await opened.driver.play(tap(opened.center, {holdMs: 300})); |
| 118 | + await expect |
| 119 | + .poll(async () => opened.surface.getAttribute('data-astryx-press')) |
| 120 | + .toBeNull(); |
| 121 | + const entries = await opened.log(); |
| 122 | + const down = first(entries, 'pointerdown'); |
| 123 | + const on = first(entries, 'press', 'on'); |
| 124 | + const up = first(entries, 'pointerup'); |
| 125 | + const fading = first(entries, 'press', 'fading'); |
| 126 | + const gone = first(entries, 'press', null); |
| 127 | + expect( |
| 128 | + down && on && up && fading && gone, |
| 129 | + JSON.stringify(entries), |
| 130 | + ).toBeTruthy(); |
| 131 | + if (!down || !on || !up || !fading || !gone) { |
| 132 | + return; |
| 133 | + } |
| 134 | + expect(on[1] - down[1]).toBeLessThan(SAME_FRAME_MS); |
| 135 | + expect(fading[1] - up[1]).toBeLessThan(SAME_FRAME_MS); |
| 136 | + // The attribute leaves when UIButton's release clock runs out. |
| 137 | + expect(gone[1] - fading[1]).toBeGreaterThanOrEqual(RELEASE_MS - 5); |
| 138 | + expect(gone[1] - fading[1]).toBeLessThan(RELEASE_MS + 80); |
| 139 | + // WebKit synthesises a click only from its own tap path, not from a raw |
| 140 | + // touch sequence (harness README, engine limits); Chromium does from both. |
| 141 | + if (opened.driver.engine === 'chromium') { |
| 142 | + expect(entries.filter(([k]) => k === 'click')).toHaveLength(1); |
| 143 | + } |
| 144 | +}); |
| 145 | + |
| 146 | +test('inside a scroller, a held press waits UIScrollView’s content-touch delay', async ({ |
| 147 | + page, |
| 148 | +}) => { |
| 149 | + const opened = await openSurface(page, SCROLLER, scrollerButton); |
| 150 | + await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS})); |
| 151 | + const entries = await opened.log(); |
| 152 | + const down = first(entries, 'pointerdown'); |
| 153 | + const on = first(entries, 'press', 'on'); |
| 154 | + expect(down && on, JSON.stringify(entries)).toBeTruthy(); |
| 155 | + if (!down || !on) { |
| 156 | + return; |
| 157 | + } |
| 158 | + expect(on[1] - down[1]).toBeGreaterThanOrEqual(CONTENT_TOUCH_DELAY_MS - 5); |
| 159 | + expect(on[1] - down[1]).toBeLessThan(CONTENT_TOUCH_DELAY_MS + 60); |
| 160 | + // WebKit synthesises a click only from its own tap path, not from a raw |
| 161 | + // touch sequence (harness README, engine limits); Chromium does from both. |
| 162 | + if (opened.driver.engine === 'chromium') { |
| 163 | + expect(entries.filter(([k]) => k === 'click')).toHaveLength(1); |
| 164 | + } |
| 165 | +}); |
| 166 | + |
| 167 | +test('inside a scroller, a tap quicker than the delay highlights at the lift, then fades', async ({ |
| 168 | + page, |
| 169 | +}) => { |
| 170 | + const opened = await openSurface(page, SCROLLER, scrollerButton); |
| 171 | + await opened.driver.play(tap(opened.center, {holdMs: 60})); |
| 172 | + await expect |
| 173 | + .poll(async () => opened.surface.getAttribute('data-astryx-press')) |
| 174 | + .toBeNull(); |
| 175 | + const entries = await opened.log(); |
| 176 | + const up = first(entries, 'pointerup'); |
| 177 | + const on = first(entries, 'press', 'on'); |
| 178 | + const fading = first(entries, 'press', 'fading'); |
| 179 | + expect(up && on && fading, JSON.stringify(entries)).toBeTruthy(); |
| 180 | + if (!up || !on || !fading) { |
| 181 | + return; |
| 182 | + } |
| 183 | + // Nothing before the lift, the highlight at it, held for the quick-tap |
| 184 | + // flash, then the release. |
| 185 | + expect(on[1]).toBeGreaterThanOrEqual(up[1] - 5); |
| 186 | + expect(fading[1] - on[1]).toBeGreaterThanOrEqual(90); |
| 187 | + // WebKit synthesises a click only from its own tap path, not from a raw |
| 188 | + // touch sequence (harness README, engine limits); Chromium does from both. |
| 189 | + if (opened.driver.engine === 'chromium') { |
| 190 | + expect(entries.filter(([k]) => k === 'click')).toHaveLength(1); |
| 191 | + } |
| 192 | +}); |
| 193 | + |
| 194 | +test('a tap through the engine’s own tap path paints the press and activates once', async ({ |
| 195 | + page, |
| 196 | +}) => { |
| 197 | + const opened = await openSurface(page, BUTTONS, secondaryButton); |
| 198 | + await opened.driver.tap(opened.center); |
| 199 | + await expect |
| 200 | + .poll(async () => opened.surface.getAttribute('data-astryx-press')) |
| 201 | + .toBeNull(); |
| 202 | + const entries = await opened.log(); |
| 203 | + expect(first(entries, 'press', 'on'), JSON.stringify(entries)).toBeTruthy(); |
| 204 | + expect(first(entries, 'press', 'fading')).toBeTruthy(); |
| 205 | + expect(entries.filter(([k]) => k === 'click')).toHaveLength(1); |
| 206 | +}); |
| 207 | + |
| 208 | +test('inside a scroller, a flick scrolls the list and paints nothing', async ({ |
| 209 | + page, |
| 210 | + browserName, |
| 211 | +}) => { |
| 212 | + test.skip( |
| 213 | + browserName !== 'chromium', |
| 214 | + 'injected touches start no native scroll in this WebKit build; the iOS simulator covers it', |
| 215 | + ); |
| 216 | + const opened = await openSurface(page, SCROLLER, scrollerButton); |
| 217 | + await opened.driver.play( |
| 218 | + flick(opened.center, {velocity: {x: 0, y: -1500}, durationMs: 80}), |
| 219 | + ); |
| 220 | + await page.waitForTimeout(300); |
| 221 | + const entries = await opened.log(); |
| 222 | + expect( |
| 223 | + entries.filter(([k]) => k === 'press'), |
| 224 | + JSON.stringify(entries), |
| 225 | + ).toEqual([]); |
| 226 | + expect(entries.filter(([k]) => k === 'click')).toHaveLength(0); |
| 227 | + const scrolled = await page |
| 228 | + .getByTestId('button-scroller') |
| 229 | + .evaluate(el => el.scrollTop); |
| 230 | + expect(scrolled).toBeGreaterThan(0); |
| 231 | +}); |
| 232 | + |
| 233 | +test('inside a scroller, a scroll that takes a held press cancels it: the paint goes at once, with no fade and no click', async ({ |
| 234 | + page, |
| 235 | + browserName, |
| 236 | +}) => { |
| 237 | + test.skip( |
| 238 | + browserName !== 'chromium', |
| 239 | + 'injected touches start no native scroll in this WebKit build; the iOS simulator covers it', |
| 240 | + ); |
| 241 | + const opened = await openSurface(page, SCROLLER, scrollerButton); |
| 242 | + await opened.driver.play( |
| 243 | + holdThenDrag( |
| 244 | + opened.center, |
| 245 | + {x: opened.center.x, y: opened.center.y - 150}, |
| 246 | + {holdMs: 300, durationMs: 300}, |
| 247 | + ), |
| 248 | + ); |
| 249 | + await page.waitForTimeout(100); |
| 250 | + const entries = await opened.log(); |
| 251 | + const presses = entries.filter(([k]) => k === 'press').map(([, , v]) => v); |
| 252 | + // Highlighted while held; the browser's takeover removes it, never fades it. |
| 253 | + expect(presses, JSON.stringify(entries)).toEqual(['on', null]); |
| 254 | + expect(first(entries, 'pointercancel')).toBeTruthy(); |
| 255 | + expect(entries.filter(([k]) => k === 'click')).toHaveLength(0); |
| 256 | + const scrolled = await page |
| 257 | + .getByTestId('button-scroller') |
| 258 | + .evaluate(el => el.scrollTop); |
| 259 | + expect(scrolled).toBeGreaterThan(0); |
| 260 | +}); |
| 261 | + |
| 262 | +test( |
| 263 | + 'storyboard: a button pressed and lifted outside a scroller', |
| 264 | + {tag: '@storyboard'}, |
| 265 | + async ({page}, testInfo) => { |
| 266 | + const opened = await openSurface(page, BUTTONS, secondaryButton); |
| 267 | + await installTimerGate(page); |
| 268 | + const box = await opened.surface.boundingBox(); |
| 269 | + if (box == null) { |
| 270 | + throw new Error('no button'); |
| 271 | + } |
| 272 | + const clip = { |
| 273 | + x: Math.max(0, box.x - 16), |
| 274 | + y: Math.max(0, box.y - 16), |
| 275 | + width: box.width + 32, |
| 276 | + height: box.height + 32, |
| 277 | + }; |
| 278 | + const storyboard = await touchStoryboard( |
| 279 | + page, |
| 280 | + opened.driver, |
| 281 | + tap(opened.center, {holdMs: 300}), |
| 282 | + { |
| 283 | + name: 'button-press-control-profile', |
| 284 | + clip, |
| 285 | + measure: async () => ({ |
| 286 | + state: await opened.surface.getAttribute('data-astryx-press'), |
| 287 | + }), |
| 288 | + columns: 3, |
| 289 | + }, |
| 290 | + ); |
| 291 | + await freezeTimers(page); |
| 292 | + await storyboard.write(testInfo); |
| 293 | + await thawTimers(page); |
| 294 | + const states = storyboard.frames.map(f => f.data.state as string | null); |
| 295 | + // Before the touch, the touch itself (already highlighted), the lift. |
| 296 | + expect(states).toEqual([null, 'on', 'fading']); |
| 297 | + }, |
| 298 | +); |
0 commit comments