oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/engines/pickers.mjs — every toolbar picker opens to a list a
2// person can use, at a phone's width and a desktop's, in Chromium and in
3// WebKit (branch review on #91: on a 390px phone Style, Font and Size all
4// opened about 10px tall, and at 320px the font list ran 6px off the
5// screen).
6//
7// Run by internal/docs/toolbar_engines_browser_test.go with the same
8// DOCS_ENGINES_CONFIG as first-reveal.mjs.
9//
10// For each engine, at 320, 390 and 1280px wide, each picker is opened
11// with a click, as a person would, and must:
12//
13// 1. show its first five rows, each really painted there: the point at
14// a row's centre hits the row, so a list clipped by the toolbar or
15// covered by something fails even where its box measures right;
16// 2. lie inside the viewport, and leave the page no wider than it;
17// 3. bring every row into view by scrolling, each then the thing at its
18// own centre: no row is out of reach or under something else.
19//
20// And again with a keyboard over all but 70px below the toolbar, told
21// through rst:select-viewport as a host tells a frame. There is more room
22// above the box than that, up to the top of the screen, but it is under
23// estilo's sticky title bar: a list that opened up into it hid its first
24// rows (review on #95). Every row must still be reachable.
25//
26// A third leg is Chromium with the page's anchor-positioning gate forced
27// false: how a browser without anchor positioning (Firefox, when this was
28// written) draws the bar, which wraps instead of scrolling and leaves the
29// lists absolutely positioned and as wide as their widest row. Playwright's
30// Firefox does not launch on the machine this was written on, and
31// Chromium's anchor positioning cannot be switched off with a flag.
32
33const { chromium, webkit } = await import('playwright')
34
35const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG)
36
37const widths = [
38 { width: 320, height: 568, phone: true },
39 { width: 390, height: 844, phone: true },
40 { width: 1280, height: 900, phone: false },
41]
42const pickers = ['block', 'font', 'size']
43
44// Two frames and a task: select.js places a list in a frame callback.
45const settle = () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(r, 0))))
46
47// Measured in the page, with the list open.
48const measure = async (id) => {
49 const frame = () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(r, 0))))
50 const input = document.getElementById(id)
51 const wrap = input.closest('[rst-combo]')
52 const list = wrap.querySelector('[rst-combo-list]')
53 const vw = document.documentElement.clientWidth
54 const vh = window.innerHeight
55 const lr = list.getBoundingClientRect()
56 const rows = [...list.querySelectorAll('[rst-combo-option]')].filter((li) => !li.hidden && li.getClientRects().length)
57 const painted = (li) => {
58 const r = li.getBoundingClientRect()
59 if (r.height <= 0 || r.top < lr.top - 0.5 || r.bottom > lr.bottom + 0.5) return false
60 const hit = document.elementFromPoint(Math.min(r.left + r.width / 2, vw - 1), r.top + r.height / 2)
61 return !!hit && li.contains(hit)
62 }
63 const out = {
64 open: !list.hidden && input.getAttribute('aria-expanded') === 'true',
65 up: wrap.hasAttribute('rst-combo-up'),
66 rows: rows.length,
67 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) },
68 box: (({ top, bottom }) => ({ top: Math.round(top), bottom: Math.round(bottom) }))(wrap.getBoundingClientRect()),
69 viewport: { width: vw, height: vh },
70 pageWidth: document.documentElement.scrollWidth,
71 shown: rows.slice(0, 5).filter(painted).length,
72 }
73 out.inside = lr.left >= -0.5 && lr.right <= vw + 0.5 && lr.top >= -0.5 && lr.bottom <= vh + 0.5
74 // Each row scrolled to the middle of the list, as far as it goes.
75 out.unreachable = []
76 for (const li of rows) {
77 list.scrollTop = li.offsetTop - (list.clientHeight - li.offsetHeight) / 2
78 await frame()
79 if (!painted(li)) out.unreachable.push(li.textContent.trim())
80 }
81 list.scrollTop = 0
82 return out
83}
84
85const gate = '@supports (anchor-name: --docs-probe)'
86const legs = {
87 chromium: { engine: chromium },
88 webkit: { engine: webkit },
89 'chromium without anchor positioning': { engine: chromium, noAnchor: true },
90}
91
92const failures = []
93const results = []
94for (const [engineName, leg] of Object.entries(legs).filter(([n, l]) => !process.env.DOCS_ENGINES_ENGINE || l.engine.name() === process.env.DOCS_ENGINES_ENGINE)) {
95 const browser = await leg.engine.launch({ headless: true })
96 for (const size of widths) {
97 const context = await browser.newContext({
98 viewport: { width: size.width, height: size.height },
99 deviceScaleFactor: 1,
100 // A phone's coarse pointer, which takes the rows to 44px.
101 hasTouch: size.phone,
102 })
103 await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })))
104 if (leg.noAnchor) {
105 await context.route('**/d/*', async (route) => {
106 const response = await route.fetch()
107 const body = await response.text()
108 if (!body.includes(gate)) failures.push(`${engineName}: the page has no anchor-positioning gate to force false`)
109 await route.fulfill({ response, body: body.replaceAll(gate, '@supports (anchor-name: --docs-probe) and (not (display: block))') })
110 })
111 }
112 const page = await context.newPage()
113 const at = `${engineName} at ${size.width}px`
114 const fail = (msg) => failures.push(`${at}: ${msg}`)
115 await page.goto(config.origin + '/d/' + config.docs.simple.shift())
116 await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 })
117 await page.waitForSelector('#docs-select-font-combo:not([disabled])', { timeout: 30000 })
118
119 for (const kind of pickers) {
120 const id = `docs-select-${kind}-combo`
121 await page.click('#' + id)
122 await page.evaluate(settle)
123 const m = await page.evaluate(measure, id)
124 results.push({ at, kind, ...m })
125 if (!m.open) fail(`${kind}: the list did not open`)
126 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}`)
127 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`)
128 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`)
129 if (m.unreachable.length) fail(`${kind}: scrolled into view, ${m.unreachable.length} of its ${m.rows} rows still cannot be seen: ${m.unreachable.join(', ')}`)
130 await page.keyboard.press('Escape')
131 await page.evaluate(() => document.activeElement && document.activeElement.blur())
132 }
133
134 for (const kind of pickers) {
135 const id = `docs-select-${kind}-combo`
136 await page.evaluate((id) => {
137 document.getElementById(id).scrollIntoView({ block: 'nearest', inline: 'nearest' })
138 const box = document.getElementById(id).closest('[rst-combo]').getBoundingClientRect()
139 document.dispatchEvent(new CustomEvent('rst:select-viewport', { detail: { top: 0, bottom: box.bottom + 70 } }))
140 }, id)
141 await page.click('#' + id)
142 await page.evaluate(settle)
143 const m = await page.evaluate(measure, id)
144 results.push({ at, kind: kind + ', keyboard', ...m })
145 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`)
146 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}`)
147 await page.keyboard.press('Escape')
148 await page.evaluate(() => document.activeElement && document.activeElement.blur())
149 await page.evaluate(() => document.dispatchEvent(new CustomEvent('rst:select-viewport', { detail: null })))
150 }
151
152 await context.close()
153 }
154 await browser.close()
155}
156
157for (const r of results) console.log(JSON.stringify(r))
158if (failures.length) {
159 console.log('FAIL\n' + failures.join('\n'))
160 process.exit(1)
161}
162console.log('ok: every picker opens to a usable list, inside the viewport, in every engine and width')
163