|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file TabPressPointer.interaction.browser.spec.ts |
| 5 | + * @input Uses @playwright/test and the interaction harness (a built |
| 6 | + * Storybook) |
| 7 | + * @output Proves a Tab's hover and press layer under a fine and a coarse |
| 8 | + * primary pointer |
| 9 | + * @position Interaction proof for packages/core/src/TabList/Tab.tsx |
| 10 | + * |
| 11 | + * Tab's press layer has a hover branch and a coarse-pointer rule, which |
| 12 | + * StyleX makes exclusive. The outcome on every pointer: a fine pointer |
| 13 | + * paints the hover, then a pressed overlay that differs from it; under a |
| 14 | + * coarse pointer a finger's press paints as an image (`data-astryx-press`), |
| 15 | + * and the colour layer stays transparent while the tab is `:active`. |
| 16 | + * Chromium only: it holds a finger, and forces `:active`, through the |
| 17 | + * DevTools protocol. |
| 18 | + */ |
| 19 | + |
| 20 | +import {expect, test, type CDPSession, type Page} from '@playwright/test'; |
| 21 | +import {openStory, withStorybook} from '@astryxdesign/interaction-harness'; |
| 22 | + |
| 23 | +const storybook = withStorybook(test); |
| 24 | + |
| 25 | +const TRANSPARENT = 'rgba(0, 0, 0, 0)'; |
| 26 | + |
| 27 | +/** The "Projects" tab: not the story's current one. */ |
| 28 | +function projectsTab(page: Page) { |
| 29 | + return page.locator('button', {hasText: 'Projects'}).first(); |
| 30 | +} |
| 31 | + |
| 32 | +/** The tab's colour layer (its first aria-hidden span) and the pointer. */ |
| 33 | +async function tabState(page: Page): Promise<{ |
| 34 | + colour: string; |
| 35 | + image: string; |
| 36 | + press: string | null; |
| 37 | + coarse: boolean; |
| 38 | + hover: boolean; |
| 39 | +}> { |
| 40 | + return projectsTab(page).evaluate(element => { |
| 41 | + const layer = element.querySelector('span[aria-hidden="true"]'); |
| 42 | + const style = layer == null ? null : getComputedStyle(layer); |
| 43 | + return { |
| 44 | + colour: style?.backgroundColor ?? '', |
| 45 | + image: style?.backgroundImage ?? '', |
| 46 | + press: element.getAttribute('data-astryx-press'), |
| 47 | + coarse: matchMedia('(pointer: coarse)').matches, |
| 48 | + hover: matchMedia('(hover: hover)').matches, |
| 49 | + }; |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +/** The layer's colour once its transition has landed (three equal reads). */ |
| 54 | +async function settledColour(page: Page): Promise<string> { |
| 55 | + let last = ''; |
| 56 | + let same = 0; |
| 57 | + for (let read = 0; read < 60 && same < 3; read++) { |
| 58 | + const colour = (await tabState(page)).colour; |
| 59 | + same = colour === last ? same + 1 : 1; |
| 60 | + last = colour; |
| 61 | + await page.waitForTimeout(50); |
| 62 | + } |
| 63 | + return last; |
| 64 | +} |
| 65 | + |
| 66 | +/** The tab's centre, in page coordinates. */ |
| 67 | +async function tabCentre(page: Page): Promise<{x: number; y: number}> { |
| 68 | + const box = await projectsTab(page).boundingBox(); |
| 69 | + if (box == null) { |
| 70 | + throw new Error('the Projects tab has no box'); |
| 71 | + } |
| 72 | + return {x: box.x + box.width / 2, y: box.y + box.height / 2}; |
| 73 | +} |
| 74 | + |
| 75 | +/** A finger held on the tab's centre; returns the lift. */ |
| 76 | +async function holdFinger( |
| 77 | + page: Page, |
| 78 | + cdp: CDPSession, |
| 79 | +): Promise<() => Promise<void>> { |
| 80 | + await cdp.send('Input.dispatchTouchEvent', { |
| 81 | + type: 'touchStart', |
| 82 | + touchPoints: [await tabCentre(page)], |
| 83 | + }); |
| 84 | + return async () => { |
| 85 | + await cdp.send('Input.dispatchTouchEvent', { |
| 86 | + type: 'touchEnd', |
| 87 | + touchPoints: [], |
| 88 | + }); |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +/** Puts the tab in `:active` (or out of it) through the DevTools protocol. */ |
| 93 | +async function forceActive(cdp: CDPSession, isActive: boolean): Promise<void> { |
| 94 | + await cdp.send('DOM.enable'); |
| 95 | + await cdp.send('CSS.enable'); |
| 96 | + const {root} = await cdp.send('DOM.getDocument', {depth: -1}); |
| 97 | + const {nodeIds} = await cdp.send('DOM.querySelectorAll', { |
| 98 | + nodeId: root.nodeId, |
| 99 | + selector: 'button', |
| 100 | + }); |
| 101 | + for (const nodeId of nodeIds) { |
| 102 | + const {outerHTML} = await cdp.send('DOM.getOuterHTML', {nodeId}); |
| 103 | + if (outerHTML.includes('Projects')) { |
| 104 | + await cdp.send('CSS.forcePseudoState', { |
| 105 | + nodeId, |
| 106 | + forcedPseudoClasses: isActive ? ['active'] : [], |
| 107 | + }); |
| 108 | + return; |
| 109 | + } |
| 110 | + } |
| 111 | + throw new Error('the Projects tab has no node'); |
| 112 | +} |
| 113 | + |
| 114 | +test.describe('Tab press layer by pointer', () => { |
| 115 | + test.skip( |
| 116 | + ({browserName}) => browserName !== 'chromium', |
| 117 | + 'Chromium holds a finger and forces :active through the DevTools protocol', |
| 118 | + ); |
| 119 | + |
| 120 | + test.describe('fine pointer with hover', () => { |
| 121 | + test.use({isMobile: false, hasTouch: false}); |
| 122 | + test('paints the hover, then a pressed overlay that differs from it', async ({ |
| 123 | + page, |
| 124 | + }) => { |
| 125 | + await openStory(page, storybook(), 'core-tablist--default'); |
| 126 | + await projectsTab(page).hover(); |
| 127 | + const state = await tabState(page); |
| 128 | + expect(state.coarse).toBe(false); |
| 129 | + expect(state.hover).toBe(true); |
| 130 | + await expect |
| 131 | + .poll(async () => (await tabState(page)).colour) |
| 132 | + .not.toBe(TRANSPARENT); |
| 133 | + const hovered = await settledColour(page); |
| 134 | + await page.mouse.down(); |
| 135 | + // The pressed overlay is its own colour, not the hover paint held on. |
| 136 | + await expect |
| 137 | + .poll(async () => (await tabState(page)).colour) |
| 138 | + .not.toBe(hovered); |
| 139 | + expect(await settledColour(page)).not.toBe(hovered); |
| 140 | + await page.mouse.up(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + test("coarse pointer: a finger's press paints as an image, and the colour layer stays transparent while the tab is :active", async ({ |
| 145 | + page, |
| 146 | + }) => { |
| 147 | + await openStory(page, storybook(), 'core-tablist--default'); |
| 148 | + const cdp = await page.context().newCDPSession(page); |
| 149 | + expect((await tabState(page)).coarse).toBe(true); |
| 150 | + // The finger's press: the press model marks the tab, and the layer |
| 151 | + // paints the pressed overlay as an image. |
| 152 | + const lift = await holdFinger(page, cdp); |
| 153 | + await expect.poll(async () => (await tabState(page)).press).toBe('on'); |
| 154 | + expect((await tabState(page)).image).not.toBe('none'); |
| 155 | + await lift(); |
| 156 | + await expect.poll(async () => (await tabState(page)).press).not.toBe('on'); |
| 157 | + // The tab in :active under the coarse pointer: the reset keeps the |
| 158 | + // colour layer transparent, past any transition. |
| 159 | + await forceActive(cdp, true); |
| 160 | + expect(await settledColour(page)).toBe(TRANSPARENT); |
| 161 | + await forceActive(cdp, false); |
| 162 | + }); |
| 163 | +}); |
0 commit comments