|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file ListDividers.interaction.browser.spec.ts |
| 5 | + * @input The built List story with dividers, real touch input (Chromium CDP / |
| 6 | + * WebKit's input command) and the interaction harness |
| 7 | + * @output Interaction proofs for a divided list under a touch press: the |
| 8 | + * pressed row's divider and the one above it are hidden in the frame the |
| 9 | + * press is believed, with the press layer reaching over the pressed row's |
| 10 | + * own; both come back in the first frame of the release; the last row's |
| 11 | + * layer never reaches past the list; a storyboard of rest, press and the |
| 12 | + * release's first frame |
| 13 | + * @position Interaction evidence for ListItem's divider styles together with |
| 14 | + * the press layer (utils/pressLayer.stylex.ts) |
| 15 | + * |
| 16 | + * UIKit's reference: a highlighted cell hides its own separator and the one |
| 17 | + * above it, unanimated, in the frame the highlight appears; on an animated |
| 18 | + * deselect both return in the first frame while the gray is still fading. |
| 19 | + * The states are read from the engine's computed styles, which carry the |
| 20 | + * real cascade (layers, specificity), and photographed in the storyboard. |
| 21 | + */ |
| 22 | + |
| 23 | +import {expect, test, type Locator, type Page} from '@playwright/test'; |
| 24 | +import { |
| 25 | + holdAnimations, |
| 26 | + openStory, |
| 27 | + releaseAnimations, |
| 28 | + seekAnimations, |
| 29 | + Storyboard, |
| 30 | + tap, |
| 31 | + touchDriver, |
| 32 | + withStorybook, |
| 33 | +} from '@astryxdesign/interaction-harness'; |
| 34 | + |
| 35 | +const storybook = withStorybook(test); |
| 36 | + |
| 37 | +const STORY = 'core-list--with-media'; |
| 38 | +const HOLD_MS = 400; |
| 39 | +/** Past the press onset (150 ms), well before the lift. */ |
| 40 | +const PRESSED_AT_MS = 260; |
| 41 | + |
| 42 | +const rows = (page: Page) => page.locator('li[data-astryx-pressable]'); |
| 43 | + |
| 44 | +interface RowState { |
| 45 | + readonly press: string | null; |
| 46 | + /** The divider at the row's bottom edge, as painted. */ |
| 47 | + readonly divider: string; |
| 48 | + readonly dividerWidth: string; |
| 49 | + /** Where the press layer's bottom edge sits, relative to the row's padding box. */ |
| 50 | + readonly layerBottom: string; |
| 51 | + readonly layerOpacity: string; |
| 52 | +} |
| 53 | + |
| 54 | +async function rowStates(page: Page): Promise<RowState[]> { |
| 55 | + return rows(page).evaluateAll(elements => |
| 56 | + elements.map(el => { |
| 57 | + const style = getComputedStyle(el); |
| 58 | + const layer = getComputedStyle(el, '::before'); |
| 59 | + return { |
| 60 | + press: el.getAttribute('data-astryx-press'), |
| 61 | + divider: style.borderBottomColor, |
| 62 | + dividerWidth: style.borderBottomWidth, |
| 63 | + layerBottom: layer.bottom, |
| 64 | + layerOpacity: layer.opacity, |
| 65 | + }; |
| 66 | + }), |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +const TRANSPARENT = 'rgba(0, 0, 0, 0)'; |
| 71 | + |
| 72 | +async function center(row: Locator) { |
| 73 | + const box = await row.boundingBox(); |
| 74 | + if (box == null) { |
| 75 | + throw new Error('no row'); |
| 76 | + } |
| 77 | + return {x: box.x + box.width / 2, y: box.y + box.height / 2}; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Holds row `index` with a finger and returns the rows' states while the |
| 82 | + * press is on and in the release's first frame (its animations held at 0). |
| 83 | + */ |
| 84 | +async function pressRow(page: Page, index: number) { |
| 85 | + const driver = await touchDriver(page); |
| 86 | + const at = await center(rows(page).nth(index)); |
| 87 | + let pressed: RowState[] = []; |
| 88 | + let released: RowState[] = []; |
| 89 | + await driver.play(tap(at, {holdMs: HOLD_MS}), { |
| 90 | + onEvent: async (_, event) => { |
| 91 | + if (event === 0) { |
| 92 | + await page.waitForTimeout(PRESSED_AT_MS); |
| 93 | + pressed = await rowStates(page); |
| 94 | + } else { |
| 95 | + await holdAnimations(page); |
| 96 | + await seekAnimations(page, 0); |
| 97 | + released = await rowStates(page); |
| 98 | + } |
| 99 | + }, |
| 100 | + }); |
| 101 | + await releaseAnimations(page); |
| 102 | + return {pressed, released}; |
| 103 | +} |
| 104 | + |
| 105 | +test.describe('a divided list under a touch press', () => { |
| 106 | + test.beforeEach(async ({page}) => { |
| 107 | + await openStory(page, storybook(), STORY); |
| 108 | + await expect(rows(page)).toHaveCount(3); |
| 109 | + }); |
| 110 | + |
| 111 | + test('hides the pressed row’s divider and the one above it; the release shows both at once', async ({ |
| 112 | + page, |
| 113 | + }) => { |
| 114 | + const rest = await rowStates(page); |
| 115 | + // At rest: a divider under every row but the last. |
| 116 | + expect(rest.map(row => row.divider === TRANSPARENT)).toEqual([ |
| 117 | + false, |
| 118 | + false, |
| 119 | + false, |
| 120 | + ]); |
| 121 | + expect(rest.map(row => row.dividerWidth)).toEqual(['1px', '1px', '0px']); |
| 122 | + const resting = rest[0].divider; |
| 123 | + |
| 124 | + const {pressed, released} = await pressRow(page, 1); |
| 125 | + expect(pressed.map(row => row.press)).toEqual([null, 'on', null]); |
| 126 | + // The divider above the pressed row (the first row's) and its own. |
| 127 | + expect(pressed[0].divider).toBe(TRANSPARENT); |
| 128 | + expect(pressed[1].divider).toBe(TRANSPARENT); |
| 129 | + // The layer covers the pressed row down through its hidden divider. |
| 130 | + expect(pressed[1].layerOpacity).toBe('1'); |
| 131 | + expect(pressed[1].layerBottom).toBe(`-${rest[1].dividerWidth}`); |
| 132 | + // Rows that are not pressed keep their layer inside their padding box. |
| 133 | + expect(pressed[0].layerBottom).toBe('0px'); |
| 134 | + |
| 135 | + // The first frame of the release: both dividers are back while the |
| 136 | + // press is still at full strength, and the layer is back inside the row. |
| 137 | + expect(released[1].press).toBe('fading'); |
| 138 | + expect(released[1].layerOpacity).toBe('1'); |
| 139 | + expect(released[0].divider).toBe(resting); |
| 140 | + expect(released[1].divider).toBe(resting); |
| 141 | + expect(released[1].layerBottom).toBe('0px'); |
| 142 | + }); |
| 143 | + |
| 144 | + test('the last row hides the divider above it and keeps its layer inside the list', async ({ |
| 145 | + page, |
| 146 | + }) => { |
| 147 | + const rest = await rowStates(page); |
| 148 | + const {pressed} = await pressRow(page, 2); |
| 149 | + expect(pressed[2].press).toBe('on'); |
| 150 | + // The second row's divider is the one above the pressed last row. |
| 151 | + expect(pressed[1].divider).toBe(TRANSPARENT); |
| 152 | + // The divider above that stays. |
| 153 | + expect(pressed[0].divider).toBe(rest[0].divider); |
| 154 | + // The last row has no divider, so its layer does not reach below it. |
| 155 | + expect(pressed[2].layerBottom).toBe('0px'); |
| 156 | + }); |
| 157 | + |
| 158 | + test('storyboard: rest, press, the release’s first frame @storyboard', async ({ |
| 159 | + page, |
| 160 | + }, testInfo) => { |
| 161 | + test.skip( |
| 162 | + testInfo.project.name !== 'chromium', |
| 163 | + 'One engine is enough for the record', |
| 164 | + ); |
| 165 | + // The list the rows live in. |
| 166 | + const list = rows(page).first().locator('xpath=..'); |
| 167 | + const storyboard = new Storyboard(page, { |
| 168 | + name: 'list-dividers-press', |
| 169 | + clip: list, |
| 170 | + columns: 3, |
| 171 | + // Full size: the dividers are hairlines. |
| 172 | + cellScale: 1, |
| 173 | + }); |
| 174 | + await storyboard.capture('rest', 0, {rows: await rowStates(page)}); |
| 175 | + const driver = await touchDriver(page); |
| 176 | + const at = await center(rows(page).nth(1)); |
| 177 | + await driver.play(tap(at, {holdMs: HOLD_MS}), { |
| 178 | + onEvent: async (_, event) => { |
| 179 | + if (event === 0) { |
| 180 | + await page.waitForTimeout(PRESSED_AT_MS); |
| 181 | + await storyboard.capture('pressed', PRESSED_AT_MS, { |
| 182 | + rows: await rowStates(page), |
| 183 | + }); |
| 184 | + } else { |
| 185 | + await holdAnimations(page); |
| 186 | + await seekAnimations(page, 0); |
| 187 | + await storyboard.capture('release, first frame', HOLD_MS, { |
| 188 | + rows: await rowStates(page), |
| 189 | + }); |
| 190 | + } |
| 191 | + }, |
| 192 | + }); |
| 193 | + await releaseAnimations(page); |
| 194 | + await storyboard.write(testInfo); |
| 195 | + }); |
| 196 | +}); |
0 commit comments