import { fr } from '@/i18n/fr' import type { Page, TestInfo } from '@playwright/test' import { expect, test } from '../support/browser' import { dropLateReplays, routeToSandbox } from '../support/sandbox' import { backToDrawing, editorActor, openEditor, openSection, place, pointOf, seedDraft, selectTab, shapeWith, } from '../support/editor' // R2 UX finish, task 1 (R2F-1): the browser's Back and Forward walk the editor's tabs and // Annotations sections. Chromium's own history (page.goBack) drives them; the top-layer and // focus rules are the browser's, so they are proved here and not in happy-dom. const dict = fr const isPhone = (info: TestInfo) => info.project.name === 'chromium-375' const dialog = (page: Page) => page.getByRole('dialog') const selected = (page: Page) => page.getByRole('tab', { selected: true }) const tabName = (tab: string) => new RegExp(`^${dict[`editor.tab.${tab}`]}( |$)`) /** * Waits until the entry on top of the history names `tab`. The adapter writes from an * effect a few milliseconds after a click, so a script pressing Back at once would land * on the wrong entry (a user cannot be that quick): every Back below waits through this. */ async function written(page: Page, tab: string) { await page.waitForFunction( (wanted) => history.state?.usr?.editorEntry?.view?.tab === wanted, tab, ) } async function sectionWritten(page: Page, section: string) { await page.waitForFunction( (wanted) => history.state?.usr?.editorEntry?.view?.section === wanted, section, ) } async function open(page: Page) { const actor = await editorActor('fr') const linked = pointOf('p1', 'main', { cx: 500, cy: 400, r: 40 }, 'Point lié') const seed = await seedDraft(actor, { shape: shapeWith(linked) }) await openEditor(page, dict, actor, seed.sheetId) await expect(selected(page)).toHaveText(tabName('annotations')) return seed } test.describe('editor browser history', () => { test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) test.beforeEach(async ({ context }, info) => { test.skip(isPhone(info), 'editing needs 1 024 px') // floor-guard: allow C16 editing needs >= 1024 px; phone read-only covered in editor-access await routeToSandbox(context) }) test.afterEach(async ({ context }) => { await dropLateReplays(context) }) test('tabs: Back and Forward restore them, the tab button holds the focus', async ({ page, }) => { await open(page) await selectTab(page, dict, 'findings') await written(page, 'findings') await selectTab(page, dict, 'references') await written(page, 'references') await page.goBack() await expect(selected(page)).toHaveText(tabName('findings')) await expect(page.locator(':focus')).toHaveAttribute( 'id', 'editor-tab-findings', ) await page.goBack() await expect(selected(page)).toHaveText(tabName('annotations')) await expect(page.locator(':focus')).toHaveAttribute( 'id', 'editor-tab-annotations', ) await page.goForward() await expect(selected(page)).toHaveText(tabName('findings')) await page.goForward() await expect(selected(page)).toHaveText(tabName('references')) }) test('an Annotations section is an entry: Back shows the drawing, Forward it', async ({ page, }) => { await open(page) await openSection(page, dict, 'categories') await sectionWritten(page, 'categories') await page.goBack() await expect(place(page, 'annotations.drawing')).toBeVisible() await expect(place(page, 'annotations.categories')).toBeHidden() await expect(page.locator(':focus')).not.toHaveJSProperty('tagName', 'BODY') await page.goForward() await expect(place(page, 'annotations.categories')).toBeVisible() }) test('arrow keys along the tab row replace the entry: one Back leaves the walk', async ({ page, }) => { await open(page) await selectTab(page, dict, 'findings') await written(page, 'findings') await page.keyboard.press('ArrowRight') await expect(selected(page)).toHaveText(tabName('references')) await written(page, 'references') await page.keyboard.press('ArrowRight') await expect(selected(page)).toHaveText(tabName('verification')) await written(page, 'verification') await page.goBack() await expect(selected(page)).toHaveText(tabName('annotations')) }) test('« Retour au dessin » pops the section: Back leaves the drawing', async ({ page, }) => { await open(page) await selectTab(page, dict, 'general') await written(page, 'general') await selectTab(page, dict, 'annotations') await written(page, 'annotations') await openSection(page, dict, 'views') await sectionWritten(page, 'views') await backToDrawing(page, dict) await sectionWritten(page, 'drawing') // no duplicate drawing entry: this Back lands on Général await page.goBack() await expect(selected(page)).toHaveText(tabName('general')) }) test('the scroll offset of a view comes back with Back and Forward', async ({ page, }) => { await open(page) await page.evaluate(() => { const spacer = document.createElement('div') spacer.style.height = '4000px' document.body.append(spacer) window.scrollTo(0, 600) }) // The keyboard: a real click would scroll the tab row into view first, and a script // event carries no user activation (Chromium skips such entries on Back). await page .getByRole('tab', { name: tabName('findings') }) .evaluate((tab) => tab.focus({ preventScroll: true })) await page.keyboard.press('Enter') await written(page, 'findings') await page.evaluate(() => window.scrollTo(0, 200)) await page.goBack() await expect(selected(page)).toHaveText(tabName('annotations')) await page.waitForFunction(() => Math.abs(window.scrollY - 600) <= 2) await page.goForward() await expect(selected(page)).toHaveText(tabName('findings')) await page.waitForFunction(() => Math.abs(window.scrollY - 200) <= 2) }) test('a reload reopens the view of the entry', async ({ page }) => { await open(page) await selectTab(page, dict, 'findings') await written(page, 'findings') await page.reload() await expect(selected(page)).toHaveText(tabName('findings')) await page.goBack() await expect(selected(page)).toHaveText(tabName('annotations')) }) test('a DeleteDialog open when Back is pressed stays open, the view unchanged', async ({ page, }) => { await open(page) await openSection(page, dict, 'categories') await sectionWritten(page, 'categories') await page .getByRole('listitem') .filter({ has: page.locator('#category-cat-a-name-fr') }) .getByRole('button', { name: dict['editor.action.delete'] }) .click() await expect(dialog(page)).toBeVisible() await page.goBack() await expect(dialog(page)).toBeVisible() await expect(place(page, 'annotations.categories')).toBeVisible() await dialog(page) .getByRole('button', { name: dict['editor.delete.close'], exact: true }) .last() .click() await expect(dialog(page)).toHaveCount(0) // The refused Back left the section one entry above the drawing: this one goes. await page.goBack() await expect(place(page, 'annotations.drawing')).toBeVisible() }) })