| 1 | // fixtures/engines/wait-keys.mjs — during a wait for a font, the keys a |
| 2 | // person was editing with stay on the page, in Chromium and in WebKit |
| 3 | // (branch review 8). The editor is inert while it waits, so focus is on |
| 4 | // the page itself, where Backspace is the browser's: WebKit went back in |
| 5 | // history and the waiting paste was lost. Space and the arrows scrolled. |
| 6 | // |
| 7 | // Run by internal/docs/fonts_engines_browser_test.go with the same |
| 8 | // DOCS_ENGINES_CONFIG as first-reveal.mjs. |
| 9 | // |
| 10 | // Each engine: the documents list, then a long document (so there is a |
| 11 | // page to go back to and a page to scroll). With every font but the |
| 12 | // default two seconds away: |
| 13 | // |
| 14 | // 1. Paste in Caveat; during the wait type, Backspace, Space, Page Down, |
| 15 | // the arrows, Home and End. The page stays where it is, at the top, |
| 16 | // and the paste lands. |
| 17 | // 2. Paste in Playfair; during the wait Backspace in the title still |
| 18 | // edits the title, and leaving the page asks first (beforeunload). |
| 19 | // Staying, the paste lands. |
| 20 | |
| 21 | const { chromium, webkit } = await import('playwright') |
| 22 | |
| 23 | const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) |
| 24 | const pause = (ms) => new Promise((r) => setTimeout(r, ms)) |
| 25 | |
| 26 | const paste = ([text, font]) => { |
| 27 | const el = document.querySelector('[docs-text]') |
| 28 | const p = el.querySelector('p') |
| 29 | const n = document.createTreeWalker(p, NodeFilter.SHOW_TEXT).nextNode() |
| 30 | const r = document.createRange() |
| 31 | r.setStart(n, 0) |
| 32 | r.collapse(true) |
| 33 | el.focus() |
| 34 | getSelection().removeAllRanges() |
| 35 | getSelection().addRange(r) |
| 36 | const d = new DataTransfer() |
| 37 | d.setData('text/html', `<span style="font-family: ${font}">${text}</span>`) |
| 38 | el.dispatchEvent(new ClipboardEvent('paste', { clipboardData: d, bubbles: true, cancelable: true })) |
| 39 | } |
| 40 | const state = () => { |
| 41 | const s = document.querySelector('[docs-fonts-status]') |
| 42 | return (s && s.getAttribute('data-state')) || '' |
| 43 | } |
| 44 | const settled = () => { |
| 45 | const s = document.querySelector('[docs-fonts-status]') |
| 46 | return !['loading', 'refused'].includes((s && s.getAttribute('data-state')) || '') && !document.querySelector('[docs-text]').inert |
| 47 | } |
| 48 | const firstParagraph = () => document.querySelector('[docs-text] p').textContent |
| 49 | |
| 50 | const failures = [] |
| 51 | for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.DOCS_ENGINES_ENGINE || n === process.env.DOCS_ENGINES_ENGINE)) { |
| 52 | const browser = await engine.launch({ headless: true }) |
| 53 | const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) |
| 54 | await context.addCookies([ |
| 55 | ...config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })), |
| 56 | { name: 'docs-view', value: 'paged', url: config.origin }, |
| 57 | ]) |
| 58 | await context.route('**/*', async (route) => { |
| 59 | const url = route.request().url() |
| 60 | if (url.endsWith('.woff2') && !url.includes('Carlito') && !url.includes('/preview-')) { |
| 61 | const response = await route.fetch() |
| 62 | await pause(2000) |
| 63 | try { await route.fulfill({ response }) } catch {} |
| 64 | return |
| 65 | } |
| 66 | return route.continue() |
| 67 | }) |
| 68 | const page = await context.newPage() |
| 69 | const fail = (msg) => failures.push(`${engineName}: ${msg}`) |
| 70 | const sqid = config.docs.long.shift() |
| 71 | const docURL = config.origin + '/d/' + sqid |
| 72 | await page.goto(config.origin + '/') |
| 73 | await page.goto(docURL) |
| 74 | await page.waitForSelector('html[data-docs-fonts="shown"] [docs-text][contenteditable="true"]', { timeout: 15000 }) |
| 75 | await pause(500) |
| 76 | |
| 77 | // 1. Editing keys during a wait. |
| 78 | await page.evaluate(paste, ['PASTE', 'Caveat']) |
| 79 | if ((await page.evaluate(state)) !== 'loading') fail('the paste did not start a wait') |
| 80 | await page.keyboard.type('ab') |
| 81 | for (const key of ['Backspace', 'Space', 'PageDown', 'ArrowDown', 'End', 'ArrowLeft', 'Home', 'Backspace']) await page.keyboard.press(key) |
| 82 | await pause(300) |
| 83 | const during = { url: page.url(), scroll: await page.evaluate(() => scrollY), state: await page.evaluate(state) } |
| 84 | if (during.url !== docURL) fail(`a key during the wait left the document for ${during.url}`) |
| 85 | else { |
| 86 | if (during.scroll !== 0) fail(`a key during the wait scrolled the page to ${during.scroll}`) |
| 87 | await page.waitForFunction(settled, null, { timeout: 10000 }).catch(() => fail('the wait never ended')) |
| 88 | const text = await page.evaluate(firstParagraph) |
| 89 | if (!text.startsWith('PASTEParagraph 1.')) fail(`after the wait the first paragraph is ${JSON.stringify(text.slice(0, 40))}`) |
| 90 | } |
| 91 | |
| 92 | // 2. A real control keeps its keys, and leaving asks first. |
| 93 | if (page.url() === docURL) { |
| 94 | let dialog = '' |
| 95 | page.on('dialog', (d) => { |
| 96 | dialog = d.type() |
| 97 | d.dismiss().catch(() => {}) |
| 98 | }) |
| 99 | await page.evaluate(paste, ['SECOND', 'Playfair Display']) |
| 100 | if ((await page.evaluate(state)) !== 'loading') fail('the second paste did not start a wait') |
| 101 | const title = page.locator('input[aria-label="Document title"]') |
| 102 | await title.focus() |
| 103 | await page.keyboard.press('ControlOrMeta+a') |
| 104 | await page.keyboard.type('Renamed') |
| 105 | await page.keyboard.press('Backspace') |
| 106 | const after = await title.inputValue() |
| 107 | if (after !== 'Rename') fail(`typing and Backspace in the title during the wait left ${JSON.stringify(after)}, not "Rename"`) |
| 108 | await page.evaluate(() => { location.href = '/' }).catch(() => {}) |
| 109 | await pause(800) |
| 110 | if (dialog !== 'beforeunload') fail(`leaving during the wait did not ask first (dialog: ${JSON.stringify(dialog)})`) |
| 111 | if (page.url() !== docURL) fail(`leaving during the wait went to ${page.url()}`) |
| 112 | else { |
| 113 | await page.waitForFunction(settled, null, { timeout: 10000 }).catch(() => fail('the second wait never ended')) |
| 114 | const text = await page.evaluate(firstParagraph) |
| 115 | if (!text.includes('SECOND')) fail(`after the second wait the first paragraph is ${JSON.stringify(text.slice(0, 40))}`) |
| 116 | } |
| 117 | } |
| 118 | console.log(JSON.stringify({ engine: engineName, failures: failures.filter((f) => f.startsWith(engineName)) })) |
| 119 | await browser.close() |
| 120 | } |
| 121 | if (failures.length) { |
| 122 | for (const f of failures) console.log(f) |
| 123 | process.exit(1) |
| 124 | } |
| 125 | |