// fixtures/engines/pickers.mjs — every toolbar picker opens to a list a // person can use, at a phone's width and a desktop's, in Chromium and in // WebKit (branch review on #91: on a 390px phone Style, Font and Size all // opened about 10px tall, and at 320px the font list ran 6px off the // screen). // // Run by internal/docs/toolbar_engines_browser_test.go with the same // DOCS_ENGINES_CONFIG as first-reveal.mjs. // // For each engine, at 320, 390 and 1280px wide, each picker is opened // with a click, as a person would, and must: // // 1. show its first five rows, each really painted there: the point at // a row's centre hits the row, so a list clipped by the toolbar or // covered by something fails even where its box measures right; // 2. lie inside the viewport, and leave the page no wider than it; // 3. bring every row into view by scrolling, each then the thing at its // own centre: no row is out of reach or under something else. // // And again with a keyboard over all but 70px below the toolbar, told // through rst:select-viewport as a host tells a frame. There is more room // above the box than that, up to the top of the screen, but it is under // estilo's sticky title bar: a list that opened up into it hid its first // rows (review on #95). Every row must still be reachable. // // A third leg is Chromium with the page's anchor-positioning gate forced // false: how a browser without anchor positioning (Firefox, when this was // written) draws the bar, which wraps instead of scrolling and leaves the // lists absolutely positioned and as wide as their widest row. Playwright's // Firefox does not launch on the machine this was written on, and // Chromium's anchor positioning cannot be switched off with a flag. const { chromium, webkit } = await import('playwright') const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) const widths = [ { width: 320, height: 568, phone: true }, { width: 390, height: 844, phone: true }, { width: 1280, height: 900, phone: false }, ] const pickers = ['block', 'font', 'size'] // Two frames and a task: select.js places a list in a frame callback. const settle = () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(r, 0)))) // Measured in the page, with the list open. const measure = async (id) => { const frame = () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(r, 0)))) const input = document.getElementById(id) const wrap = input.closest('[rst-combo]') const list = wrap.querySelector('[rst-combo-list]') const vw = document.documentElement.clientWidth const vh = window.innerHeight const lr = list.getBoundingClientRect() const rows = [...list.querySelectorAll('[rst-combo-option]')].filter((li) => !li.hidden && li.getClientRects().length) const painted = (li) => { const r = li.getBoundingClientRect() if (r.height <= 0 || r.top < lr.top - 0.5 || r.bottom > lr.bottom + 0.5) return false const hit = document.elementFromPoint(Math.min(r.left + r.width / 2, vw - 1), r.top + r.height / 2) return !!hit && li.contains(hit) } const out = { open: !list.hidden && input.getAttribute('aria-expanded') === 'true', up: wrap.hasAttribute('rst-combo-up'), rows: rows.length, list: { left: Math.round(lr.left), right: Math.round(lr.right), top: Math.round(lr.top), bottom: Math.round(lr.bottom), height: Math.round(lr.height) }, box: (({ top, bottom }) => ({ top: Math.round(top), bottom: Math.round(bottom) }))(wrap.getBoundingClientRect()), viewport: { width: vw, height: vh }, pageWidth: document.documentElement.scrollWidth, shown: rows.slice(0, 5).filter(painted).length, } out.inside = lr.left >= -0.5 && lr.right <= vw + 0.5 && lr.top >= -0.5 && lr.bottom <= vh + 0.5 // Each row scrolled to the middle of the list, as far as it goes. out.unreachable = [] for (const li of rows) { list.scrollTop = li.offsetTop - (list.clientHeight - li.offsetHeight) / 2 await frame() if (!painted(li)) out.unreachable.push(li.textContent.trim()) } list.scrollTop = 0 return out } const gate = '@supports (anchor-name: --docs-probe)' const legs = { chromium: { engine: chromium }, webkit: { engine: webkit }, 'chromium without anchor positioning': { engine: chromium, noAnchor: true }, } const failures = [] const results = [] for (const [engineName, leg] of Object.entries(legs).filter(([n, l]) => !process.env.DOCS_ENGINES_ENGINE || l.engine.name() === process.env.DOCS_ENGINES_ENGINE)) { const browser = await leg.engine.launch({ headless: true }) for (const size of widths) { const context = await browser.newContext({ viewport: { width: size.width, height: size.height }, deviceScaleFactor: 1, // A phone's coarse pointer, which takes the rows to 44px. hasTouch: size.phone, }) await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) if (leg.noAnchor) { await context.route('**/d/*', async (route) => { const response = await route.fetch() const body = await response.text() if (!body.includes(gate)) failures.push(`${engineName}: the page has no anchor-positioning gate to force false`) await route.fulfill({ response, body: body.replaceAll(gate, '@supports (anchor-name: --docs-probe) and (not (display: block))') }) }) } const page = await context.newPage() const at = `${engineName} at ${size.width}px` const fail = (msg) => failures.push(`${at}: ${msg}`) await page.goto(config.origin + '/d/' + config.docs.simple.shift()) await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) await page.waitForSelector('#docs-select-font-combo:not([disabled])', { timeout: 30000 }) for (const kind of pickers) { const id = `docs-select-${kind}-combo` await page.click('#' + id) await page.evaluate(settle) const m = await page.evaluate(measure, id) results.push({ at, kind, ...m }) if (!m.open) fail(`${kind}: the list did not open`) if (m.shown < Math.min(5, m.rows)) fail(`${kind}: ${m.shown} of its first ${Math.min(5, m.rows)} rows can be seen; the list is ${m.list.height}px tall at ${m.list.top}..${m.list.bottom}`) if (!m.inside) fail(`${kind}: the list spans ${m.list.left}..${m.list.right} x ${m.list.top}..${m.list.bottom}, outside the ${m.viewport.width}x${m.viewport.height} viewport`) if (m.pageWidth > m.viewport.width) fail(`${kind}: with the list open the page is ${m.pageWidth}px wide in a ${m.viewport.width}px viewport`) if (m.unreachable.length) fail(`${kind}: scrolled into view, ${m.unreachable.length} of its ${m.rows} rows still cannot be seen: ${m.unreachable.join(', ')}`) await page.keyboard.press('Escape') await page.evaluate(() => document.activeElement && document.activeElement.blur()) } for (const kind of pickers) { const id = `docs-select-${kind}-combo` await page.evaluate((id) => { document.getElementById(id).scrollIntoView({ block: 'nearest', inline: 'nearest' }) const box = document.getElementById(id).closest('[rst-combo]').getBoundingClientRect() document.dispatchEvent(new CustomEvent('rst:select-viewport', { detail: { top: 0, bottom: box.bottom + 70 } })) }, id) await page.click('#' + id) await page.evaluate(settle) const m = await page.evaluate(measure, id) results.push({ at, kind: kind + ', keyboard', ...m }) if (!m.inside) fail(`${kind}, keyboard: the list spans ${m.list.left}..${m.list.right} x ${m.list.top}..${m.list.bottom}, outside the viewport`) if (m.unreachable.length) fail(`${kind}, keyboard: ${m.unreachable.length} of its ${m.rows} rows cannot be seen: ${m.unreachable.join(', ')}; the list spans ${m.list.top}..${m.list.bottom}, opened ${m.up ? 'up' : 'down'} from its box at ${m.box.top}..${m.box.bottom}`) await page.keyboard.press('Escape') await page.evaluate(() => document.activeElement && document.activeElement.blur()) await page.evaluate(() => document.dispatchEvent(new CustomEvent('rst:select-viewport', { detail: null }))) } await context.close() } await browser.close() } for (const r of results) console.log(JSON.stringify(r)) if (failures.length) { console.log('FAIL\n' + failures.join('\n')) process.exit(1) } console.log('ok: every picker opens to a usable list, inside the viewport, in every engine and width')