| new file mode 100644 |
| index 0000000..4bf7a1c |
| --- /dev/null |
| +++ b/fixtures/engines/pickers.mjs |
| @@ -0,0 +1,161 @@ |
| +// 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. scroll to its last row, if it has more than it shows. |
| +// |
| +// And at 390px, told only a band just below the toolbar is visible (as a |
| +// phone's keyboard does), a list that opens upward ends above its box and |
| +// still inside the screen; it had been wholly above it. Its rows are not |
| +// hit-tested: the title bar above the toolbar is drawn over them. |
| +// |
| +// 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 |
| + out.scrolls = list.scrollHeight > list.clientHeight + 1 |
| + if (out.scrolls) { |
| + list.scrollTop = list.scrollHeight |
| + await frame() |
| + out.scrolledTo = list.scrollTop |
| + out.lastShown = painted(rows[rows.length - 1]) |
| + } else { |
| + out.lastShown = rows.length > 0 && painted(rows[rows.length - 1]) |
| + } |
| + 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.scrolls && !(m.scrolledTo > 0)) fail(`${kind}: the list has more rows than it shows but does not scroll`) |
| + if (!m.lastShown) fail(`${kind}: its last row cannot be reached`) |
| + await page.keyboard.press('Escape') |
| + await page.evaluate(() => document.activeElement && document.activeElement.blur()) |
| + } |
| + |
| + if (size.width === 390) { |
| + // A keyboard over everything but the toolbar and 40px under it: |
| + // there is more room above the box, so the list opens upward. |
| + const id = 'docs-select-block-combo' |
| + await page.evaluate((id) => { |
| + const box = document.getElementById(id).closest('[rst-combo]').getBoundingClientRect() |
| + document.dispatchEvent(new CustomEvent('rst:select-viewport', { detail: { top: 0, bottom: box.bottom + 40 } })) |
| + }, id) |
| + await page.click('#' + id) |
| + await page.evaluate(settle) |
| + const m = await page.evaluate(measure, id) |
| + results.push({ at, kind: 'block, upward', ...m }) |
| + if (!m.up) fail(`block, upward: the list did not open upward with ${m.box.top}px above its box and 40px below`) |
| + if (m.list.bottom > m.box.top + 1 || m.list.top < 0 || m.list.height < 20) fail(`block, upward: the list spans ${m.list.top}..${m.list.bottom} with its box at ${m.box.top}; want above the box and inside the screen`) |
| + await page.keyboard.press('Escape') |
| + 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') |