// fixtures/engines/wait-keys.mjs — during a wait for a font, the keys a // person was editing with stay on the page, in Chromium and in WebKit // (branch review 8). The editor is inert while it waits, so focus is on // the page itself, where Backspace is the browser's: WebKit went back in // history and the waiting paste was lost. Space and the arrows scrolled. // // Run by internal/docs/fonts_engines_browser_test.go with the same // DOCS_ENGINES_CONFIG as first-reveal.mjs. // // Each engine: the documents list, then a long document (so there is a // page to go back to and a page to scroll). With every font but the // default two seconds away: // // 1. Paste in Caveat; during the wait type, Backspace, Space, Page Down, // the arrows, Home and End. The page stays where it is, at the top, // and the paste lands. // 2. Paste in Playfair; during the wait Backspace in the title still // edits the title, and leaving the page asks first (beforeunload). // Staying, the paste lands. const { chromium, webkit } = await import('playwright') const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) const pause = (ms) => new Promise((r) => setTimeout(r, ms)) const paste = ([text, font]) => { const el = document.querySelector('[docs-text]') const p = el.querySelector('p') const n = document.createTreeWalker(p, NodeFilter.SHOW_TEXT).nextNode() const r = document.createRange() r.setStart(n, 0) r.collapse(true) el.focus() getSelection().removeAllRanges() getSelection().addRange(r) const d = new DataTransfer() d.setData('text/html', `${text}`) el.dispatchEvent(new ClipboardEvent('paste', { clipboardData: d, bubbles: true, cancelable: true })) } const state = () => { const s = document.querySelector('[docs-fonts-status]') return (s && s.getAttribute('data-state')) || '' } const settled = () => { const s = document.querySelector('[docs-fonts-status]') return !['loading', 'refused'].includes((s && s.getAttribute('data-state')) || '') && !document.querySelector('[docs-text]').inert } const firstParagraph = () => document.querySelector('[docs-text] p').textContent const failures = [] for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.DOCS_ENGINES_ENGINE || n === process.env.DOCS_ENGINES_ENGINE)) { const browser = await engine.launch({ headless: true }) const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) await context.addCookies([ ...config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })), { name: 'docs-view', value: 'paged', url: config.origin }, ]) await context.route('**/*', async (route) => { const url = route.request().url() if (url.endsWith('.woff2') && !url.includes('Carlito') && !url.includes('/preview-')) { const response = await route.fetch() await pause(2000) try { await route.fulfill({ response }) } catch {} return } return route.continue() }) const page = await context.newPage() const fail = (msg) => failures.push(`${engineName}: ${msg}`) const sqid = config.docs.long.shift() const docURL = config.origin + '/d/' + sqid await page.goto(config.origin + '/') await page.goto(docURL) await page.waitForSelector('html[data-docs-fonts="shown"] [docs-text][contenteditable="true"]', { timeout: 15000 }) await pause(500) // 1. Editing keys during a wait. await page.evaluate(paste, ['PASTE', 'Caveat']) if ((await page.evaluate(state)) !== 'loading') fail('the paste did not start a wait') await page.keyboard.type('ab') for (const key of ['Backspace', 'Space', 'PageDown', 'ArrowDown', 'End', 'ArrowLeft', 'Home', 'Backspace']) await page.keyboard.press(key) await pause(300) const during = { url: page.url(), scroll: await page.evaluate(() => scrollY), state: await page.evaluate(state) } if (during.url !== docURL) fail(`a key during the wait left the document for ${during.url}`) else { if (during.scroll !== 0) fail(`a key during the wait scrolled the page to ${during.scroll}`) await page.waitForFunction(settled, null, { timeout: 10000 }).catch(() => fail('the wait never ended')) const text = await page.evaluate(firstParagraph) if (!text.startsWith('PASTEParagraph 1.')) fail(`after the wait the first paragraph is ${JSON.stringify(text.slice(0, 40))}`) } // 2. A real control keeps its keys, and leaving asks first. if (page.url() === docURL) { let dialog = '' page.on('dialog', (d) => { dialog = d.type() d.dismiss().catch(() => {}) }) await page.evaluate(paste, ['SECOND', 'Playfair Display']) if ((await page.evaluate(state)) !== 'loading') fail('the second paste did not start a wait') const title = page.locator('input[aria-label="Document title"]') await title.focus() await page.keyboard.press('ControlOrMeta+a') await page.keyboard.type('Renamed') await page.keyboard.press('Backspace') const after = await title.inputValue() if (after !== 'Rename') fail(`typing and Backspace in the title during the wait left ${JSON.stringify(after)}, not "Rename"`) await page.evaluate(() => { location.href = '/' }).catch(() => {}) await pause(800) if (dialog !== 'beforeunload') fail(`leaving during the wait did not ask first (dialog: ${JSON.stringify(dialog)})`) if (page.url() !== docURL) fail(`leaving during the wait went to ${page.url()}`) else { await page.waitForFunction(settled, null, { timeout: 10000 }).catch(() => fail('the second wait never ended')) const text = await page.evaluate(firstParagraph) if (!text.includes('SECOND')) fail(`after the second wait the first paragraph is ${JSON.stringify(text.slice(0, 40))}`) } } console.log(JSON.stringify({ engine: engineName, failures: failures.filter((f) => f.startsWith(engineName)) })) await browser.close() } if (failures.length) { for (const f of failures) console.log(f) process.exit(1) }