|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// @pr-a11y scope core/Tokenizer core/PowerSearch ^packages/core/src/internal/SheetConfirmation ^apps/storybook/stories/(Tokenizer|PowerSearch) |
| 3 | + |
| 4 | +/** |
| 5 | + * @file BottomSheetRefusedDismissal.a11y.browser.spec.ts |
| 6 | + * @input The Tokenizer and PowerSearch sheet stories in a built Storybook, on |
| 7 | + * a phone viewport with touch; the swipe is real touch input through the |
| 8 | + * interaction harness. |
| 9 | + * @output Real-engine proof that a sheet whose dismissal its host refuses — a |
| 10 | + * swipe, a scrim tap or Escape on a changed draft opens the discard |
| 11 | + * confirmation — keeps its scrim while the confirmation is open, keeps it |
| 12 | + * after Keep editing, and leaves with it on Discard. |
| 13 | + * @position Chromium and WebKit. The scrim is the sheet dialog's `::backdrop`, |
| 14 | + * whose opacity the drag thins on the compositor and the host restores; only |
| 15 | + * a shipping engine computes the pseudo-element's style. |
| 16 | + */ |
| 17 | + |
| 18 | +import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright'; |
| 19 | +import { |
| 20 | + DEFAULT_STORYBOOK_DIR, |
| 21 | + serveStorybook, |
| 22 | + type StaticServer, |
| 23 | +} from '@astryxdesign/a11y-spec/storybook'; |
| 24 | +import {drag, touchDriver} from '@astryxdesign/interaction-harness'; |
| 25 | + |
| 26 | +let storybook: StaticServer; |
| 27 | +test.beforeAll(async () => { |
| 28 | + storybook = await serveStorybook( |
| 29 | + process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR, |
| 30 | + ); |
| 31 | +}); |
| 32 | +test.afterAll(async () => { |
| 33 | + await storybook?.close(); |
| 34 | +}); |
| 35 | + |
| 36 | +test.use({viewport: {width: 390, height: 844}, hasTouch: true}); |
| 37 | + |
| 38 | +interface SheetFixture { |
| 39 | + readonly name: string; |
| 40 | + readonly story: string; |
| 41 | + /** The sheet dialog's accessible name. */ |
| 42 | + readonly sheetLabel: string; |
| 43 | + /** The control that reopens the sheet, and takes focus back after a close. */ |
| 44 | + trigger(page: Page): ReturnType<Page['locator']>; |
| 45 | + /** One edit, so the draft differs from the selection at open. */ |
| 46 | + dirty(page: Page): Promise<void>; |
| 47 | + /** Where Keep editing puts focus. */ |
| 48 | + readonly focusAfterKeepEditing: string; |
| 49 | +} |
| 50 | + |
| 51 | +const FIXTURES: ReadonlyArray<SheetFixture> = [ |
| 52 | + { |
| 53 | + name: 'Tokenizer', |
| 54 | + story: 'core-tokenizer--sheet-presentation', |
| 55 | + sheetLabel: 'Tags', |
| 56 | + trigger: page => page.getByRole('button', {name: 'Tags'}), |
| 57 | + dirty: async page => { |
| 58 | + await page.getByRole('checkbox', {name: 'Tag 03'}).tap(); |
| 59 | + await expect( |
| 60 | + page.getByRole('checkbox', {name: 'Tag 03'}), |
| 61 | + ).toHaveAttribute('aria-checked', 'true'); |
| 62 | + }, |
| 63 | + focusAfterKeepEditing: 'Search Tags', |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'PowerSearch', |
| 67 | + story: 'core-powersearch--sheet-presentation', |
| 68 | + sheetLabel: 'Filters', |
| 69 | + trigger: page => page.locator('button[aria-haspopup="dialog"]').first(), |
| 70 | + dirty: async page => { |
| 71 | + const remove = page |
| 72 | + .getByRole('button', {name: /^Remove .* filter$/}) |
| 73 | + .first(); |
| 74 | + const name = (await remove.getAttribute('aria-label')) ?? ''; |
| 75 | + await remove.tap(); |
| 76 | + await expect(page.getByRole('button', {name})).toHaveCount(0); |
| 77 | + }, |
| 78 | + focusAfterKeepEditing: 'Add filter', |
| 79 | + }, |
| 80 | +]; |
| 81 | + |
| 82 | +type ClosePath = 'swipe' | 'scrim' | 'escape'; |
| 83 | +const CLOSE_PATHS: ReadonlyArray<ClosePath> = ['swipe', 'scrim', 'escape']; |
| 84 | + |
| 85 | +interface ScrimReading { |
| 86 | + readonly open: boolean; |
| 87 | + readonly modal: boolean; |
| 88 | + /** The sheet dialog's `::backdrop` opacity, computed. */ |
| 89 | + readonly opacity: number; |
| 90 | + /** The panel's transform at rest is the identity. */ |
| 91 | + readonly panelAtRest: boolean; |
| 92 | +} |
| 93 | + |
| 94 | +function sheetDialog(page: Page, fixture: SheetFixture) { |
| 95 | + return page.locator(`dialog[aria-label="${fixture.sheetLabel}"]`); |
| 96 | +} |
| 97 | + |
| 98 | +async function readScrim( |
| 99 | + page: Page, |
| 100 | + fixture: SheetFixture, |
| 101 | +): Promise<ScrimReading> { |
| 102 | + return sheetDialog(page, fixture).evaluate(node => { |
| 103 | + const dialog = node as HTMLDialogElement; |
| 104 | + const panel = dialog.querySelector<HTMLElement>('[tabindex="-1"]'); |
| 105 | + const transform = |
| 106 | + panel == null ? 'none' : getComputedStyle(panel).transform; |
| 107 | + return { |
| 108 | + open: dialog.open, |
| 109 | + modal: dialog.matches(':modal'), |
| 110 | + opacity: Number(getComputedStyle(dialog, '::backdrop').opacity), |
| 111 | + panelAtRest: |
| 112 | + transform === 'none' || transform === 'matrix(1, 0, 0, 1, 0, 0)', |
| 113 | + }; |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +/** The sheet's own motion has ended: its entrance, or a return to rest. */ |
| 118 | +async function waitForSheetRest(page: Page, fixture: SheetFixture) { |
| 119 | + const dialog = sheetDialog(page, fixture); |
| 120 | + await expect |
| 121 | + .poll(async () => |
| 122 | + dialog.evaluate( |
| 123 | + node => |
| 124 | + node |
| 125 | + .getAnimations({subtree: true}) |
| 126 | + .filter(animation => animation.playState === 'running').length, |
| 127 | + ), |
| 128 | + ) |
| 129 | + .toBe(0); |
| 130 | +} |
| 131 | + |
| 132 | +async function openDirtySheet(page: Page, fixture: SheetFixture) { |
| 133 | + await page.goto( |
| 134 | + `${storybook.origin}/iframe.html?id=${fixture.story}&viewMode=story`, |
| 135 | + {waitUntil: 'load'}, |
| 136 | + ); |
| 137 | + await fixture.trigger(page).waitFor({state: 'visible'}); |
| 138 | + await page.evaluate(async () => { |
| 139 | + await document.fonts.ready; |
| 140 | + }); |
| 141 | + await fixture.trigger(page).tap(); |
| 142 | + await expect( |
| 143 | + page.getByRole('dialog', {name: fixture.sheetLabel}), |
| 144 | + ).toBeVisible(); |
| 145 | + await waitForSheetRest(page, fixture); |
| 146 | + await fixture.dirty(page); |
| 147 | + const before = await readScrim(page, fixture); |
| 148 | + expect(before).toEqual({ |
| 149 | + open: true, |
| 150 | + modal: true, |
| 151 | + opacity: 1, |
| 152 | + panelAtRest: true, |
| 153 | + }); |
| 154 | +} |
| 155 | + |
| 156 | +/** Asks to close the sheet by `path`; the changed draft then asks first. */ |
| 157 | +async function requestClose( |
| 158 | + page: Page, |
| 159 | + fixture: SheetFixture, |
| 160 | + path: ClosePath, |
| 161 | +) { |
| 162 | + const panel = sheetDialog(page, fixture).locator('[tabindex="-1"]').first(); |
| 163 | + const box = await panel.boundingBox(); |
| 164 | + if (box == null) { |
| 165 | + throw new Error('The sheet panel has no box'); |
| 166 | + } |
| 167 | + const x = Math.round(box.x + box.width / 2); |
| 168 | + if (path === 'swipe') { |
| 169 | + // A finger on the grabber, pulled past the dismiss line and lifted: the |
| 170 | + // live drag thins the scrim with the sheet's travel. |
| 171 | + const driver = await touchDriver(page); |
| 172 | + const from = {x, y: Math.round(box.y + 12)}; |
| 173 | + const to = {x, y: Math.round(Math.min(box.y + box.height, 844 - 8))}; |
| 174 | + await driver.play(drag(from, to, {durationMs: 320}), {pacing: 'spacing'}); |
| 175 | + } else if (path === 'scrim') { |
| 176 | + // The dialog above the panel is the scrim. |
| 177 | + await page.touchscreen.tap(x, Math.max(12, Math.round(box.y - 40))); |
| 178 | + } else { |
| 179 | + // A tooltip a focused control shows would take the press first (the |
| 180 | + // dismissal stack's order), so the press starts from the sheet's panel, |
| 181 | + // once that tooltip has gone. |
| 182 | + await panel.focus(); |
| 183 | + await expect(page.getByRole('tooltip')).toHaveCount(0); |
| 184 | + await page.keyboard.press('Escape'); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +const confirmation = (page: Page) => |
| 189 | + page.getByRole('alertdialog', {name: 'Discard changes?'}); |
| 190 | + |
| 191 | +for (const fixture of FIXTURES) { |
| 192 | + for (const path of CLOSE_PATHS) { |
| 193 | + test(`${fixture.name}: the scrim stays while the confirmation a ${path} opened is up, and after Keep editing`, async ({ |
| 194 | + page, |
| 195 | + }) => { |
| 196 | + await openDirtySheet(page, fixture); |
| 197 | + await requestClose(page, fixture, path); |
| 198 | + await expect(confirmation(page)).toBeVisible(); |
| 199 | + await expect( |
| 200 | + page.getByRole('button', {name: 'Keep editing'}), |
| 201 | + ).toBeFocused(); |
| 202 | + |
| 203 | + // The sheet stayed presented and modal, with its backdrop painting. |
| 204 | + // After a swipe the panel returns to rest and the scrim with it, over |
| 205 | + // the sheet's own motion, so the reading waits for that rest. |
| 206 | + await waitForSheetRest(page, fixture); |
| 207 | + await expect |
| 208 | + .poll(async () => readScrim(page, fixture), { |
| 209 | + message: `the ${fixture.name} sheet's scrim while the confirmation is open`, |
| 210 | + }) |
| 211 | + .toEqual({open: true, modal: true, opacity: 1, panelAtRest: true}); |
| 212 | + |
| 213 | + await page.getByRole('button', {name: 'Keep editing'}).tap(); |
| 214 | + await expect(confirmation(page)).toHaveCount(0); |
| 215 | + await expect( |
| 216 | + page.getByRole('dialog', {name: fixture.sheetLabel}), |
| 217 | + ).toBeVisible(); |
| 218 | + await expect( |
| 219 | + page.getByRole(fixture.name === 'Tokenizer' ? 'textbox' : 'button', { |
| 220 | + name: fixture.focusAfterKeepEditing, |
| 221 | + }), |
| 222 | + ).toBeFocused(); |
| 223 | + // Keep editing changes nothing of the sheet's own presentation. |
| 224 | + expect(await readScrim(page, fixture)).toEqual({ |
| 225 | + open: true, |
| 226 | + modal: true, |
| 227 | + opacity: 1, |
| 228 | + panelAtRest: true, |
| 229 | + }); |
| 230 | + }); |
| 231 | + } |
| 232 | + |
| 233 | + test(`${fixture.name}: Discard after a swipe closes the sheet with its exit, and focus returns to the trigger`, async ({ |
| 234 | + page, |
| 235 | + }) => { |
| 236 | + await openDirtySheet(page, fixture); |
| 237 | + await requestClose(page, fixture, 'swipe'); |
| 238 | + await expect(confirmation(page)).toBeVisible(); |
| 239 | + await waitForSheetRest(page, fixture); |
| 240 | + await page.getByRole('button', {name: 'Discard changes'}).tap(); |
| 241 | + await expect(confirmation(page)).toHaveCount(0); |
| 242 | + // The exit runs: the dialog is still presented for a moment, then gone, |
| 243 | + // and the backdrop has faded with it. |
| 244 | + await expect( |
| 245 | + page.locator(`dialog[aria-label="${fixture.sheetLabel}"][open]`), |
| 246 | + ).toHaveCount(0, {timeout: 5000}); |
| 247 | + await expect(fixture.trigger(page)).toBeFocused(); |
| 248 | + }); |
| 249 | +} |
0 commit comments