| 1 | // compact.browser.mjs: open the compact, stacked bar at each width, with |
| 2 | // a finger where a phone or a tablet has one, and measure it. Driven by |
| 3 | // compact_browser_test.go (go test -tags browser ./chrome), which serves |
| 4 | // the page and makes every assertion; this file only drives the browsers |
| 5 | // and reports what it measured. With job.shots set it saves screenshots |
| 6 | // there: each case at rest, and with the ••• menu (or File) open. |
| 7 | // |
| 8 | // ESTILO_PLAYWRIGHT=<path to the playwright package> \ |
| 9 | // node compact.browser.mjs <job JSON> |
| 10 | import { createRequire } from 'node:module' |
| 11 | import { mkdirSync } from 'node:fs' |
| 12 | |
| 13 | const require = createRequire(import.meta.url) |
| 14 | const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright') |
| 15 | const job = JSON.parse(process.argv[2]) |
| 16 | |
| 17 | // kind, width, height, touch |
| 18 | const sizes = [ |
| 19 | ['phone', 360, 780, true], ['phone', 390, 844, true], ['phone', 430, 932, true], |
| 20 | ['landscape', 844, 390, true], |
| 21 | ['tablet', 768, 1024, true], ['tablet', 1024, 768, true], |
| 22 | ['desktop', 1280, 800, false], |
| 23 | ] |
| 24 | const cases = [] |
| 25 | for (const [kind, w, h, touch] of sizes) { |
| 26 | for (const lang of ['en', 'ar', 'ru']) { |
| 27 | for (const scheme of ['light', 'dark']) { |
| 28 | if (lang !== 'en' && scheme === 'dark' && !(lang === 'ar' && w === 390)) continue |
| 29 | if (lang !== 'en' && kind === 'landscape') continue |
| 30 | cases.push({ name: `${kind} ${w} ${lang} ${scheme}`, kind, w, h, touch, lang, scheme }) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | function measure() { |
| 36 | const q = (s) => document.querySelector(s) |
| 37 | const shown = (el) => !!el && el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden' |
| 38 | const box = (el) => { const r = el.getBoundingClientRect(); return [r.x, r.y, r.width, r.height] } |
| 39 | const bar = q('[estilo-bar]') |
| 40 | const probe = document.createElement('div') |
| 41 | probe.style.blockSize = 'var(--estilo-bar-block-size)' |
| 42 | document.body.append(probe) |
| 43 | const v = probe.getBoundingClientRect().height |
| 44 | probe.remove() |
| 45 | const input = q('[estilo-bar-title-input]') |
| 46 | const out = { |
| 47 | header: bar.getBoundingClientRect().height, |
| 48 | var: v, |
| 49 | coarse: matchMedia('(pointer: coarse)').matches, |
| 50 | overflow: document.documentElement.scrollWidth > window.innerWidth, |
| 51 | shown: { |
| 52 | menubar: shown(q('[estilo-bar-menubar]')), actions: shown(q('[estilo-bar-actions]')), |
| 53 | switcher: shown(q('[estilo-switcher]')), account: shown(q('[estilo-account]')), |
| 54 | more: shown(q('[estilo-bar-more]')), icon: shown(q('[estilo-bar-brand] img')), |
| 55 | back: shown(q('[estilo-bar-back]')), name: shown(q('[estilo-bar-name]')), |
| 56 | title: shown(input), lead: shown(q('[estilo-bar-lead]')), trail: shown(q('[estilo-bar-trail]')), |
| 57 | }, |
| 58 | boxes: { back: box(q('[estilo-bar-brand]')), title: box(q('[estilo-bar-title]')), end: box(q('[estilo-bar-end]')), menubar: box(q('[estilo-bar-menubar]')) }, |
| 59 | ellipsis: getComputedStyle(input).textOverflow, |
| 60 | } |
| 61 | Array.prototype.filter.call(document.querySelectorAll('[estilo-bar-tool]'), shown).forEach((el, i) => { out.boxes['tool-' + i] = box(el) }) |
| 62 | return out |
| 63 | } |
| 64 | |
| 65 | function panel(sel) { |
| 66 | const d = document.querySelector(sel) |
| 67 | const p = d.querySelector('[estilo-menu]') |
| 68 | const r = p.getBoundingClientRect() |
| 69 | const shown = (el) => el.getClientRects().length > 0 |
| 70 | return { |
| 71 | panel: [r.x, r.y, r.width, r.height], |
| 72 | rows: Array.prototype.filter.call(p.querySelectorAll('[estilo-menu-item]'), shown).map((el) => el.getBoundingClientRect().height), |
| 73 | keys: Array.prototype.filter.call(p.querySelectorAll('[estilo-menu-key]'), shown).length, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | function large() { |
| 78 | const q = (s) => document.querySelector(s) |
| 79 | const box = (el) => { const r = el.getBoundingClientRect(); return [r.x, r.y, r.width, r.height] } |
| 80 | const t = q('[estilo-bar-compact-title]') |
| 81 | const cs = getComputedStyle(t) |
| 82 | return { |
| 83 | title: cs.visibility === 'visible' && parseFloat(cs.opacity) > 0.99, |
| 84 | name: q('[estilo-bar-name]').getBoundingClientRect().width, |
| 85 | label: q('[estilo-bar-brand]').innerText, // drops undisplayed text, keeps a visually hidden name |
| 86 | border: getComputedStyle(q('[estilo-bar]')).borderBlockEndColor, |
| 87 | box: box(t), back: box(q('[estilo-bar-brand]')), end: box(q('[estilo-bar-end]')), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | const results = [] |
| 92 | for (const engine of job.engines) { |
| 93 | const result = { engine, version: '', error: '', cases: [] } |
| 94 | results.push(result) |
| 95 | let browser |
| 96 | try { |
| 97 | browser = await pw[engine].launch() |
| 98 | result.version = browser.version() |
| 99 | if (job.shots) mkdirSync(job.shots, { recursive: true }) |
| 100 | for (const c of cases) { |
| 101 | // One case that cannot be driven is reported and the rest go on, |
| 102 | // so a broken layout shows as the assertions it breaks. |
| 103 | try { |
| 104 | const opts = { viewport: { width: c.w, height: c.h }, deviceScaleFactor: 2, colorScheme: c.scheme } |
| 105 | if (c.touch) Object.assign(opts, { hasTouch: true, isMobile: engine !== 'firefox' }) |
| 106 | const ctx = await browser.newContext(opts) |
| 107 | const page = await ctx.newPage() |
| 108 | page.setDefaultTimeout(5000) |
| 109 | await page.goto(`${job.origin}/?lang=${c.lang}&scheme=${c.scheme}`, { waitUntil: 'load' }) |
| 110 | await page.evaluate(() => document.fonts.ready) |
| 111 | await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) |
| 112 | const m = await page.evaluate(measure) |
| 113 | const file = (s) => `${job.shots}/${engine}-${c.name.replaceAll(' ', '-')}${s}.png` |
| 114 | if (job.shots) await page.screenshot({ path: file('') }) |
| 115 | const sel = (c.kind === 'phone' || c.kind === 'landscape') ? '[data-demo="more"]' : '[estilo-menubar-menu]:has([data-demo="file"])' |
| 116 | await page.locator(sel + ' > summary').click() |
| 117 | await page.evaluate(() => new Promise((r) => requestAnimationFrame(r))) |
| 118 | const p = await page.evaluate(panel, sel) |
| 119 | if (job.shots) await page.screenshot({ path: file('-open') }) |
| 120 | const entry = { name: c.name, width: c.w, touch: c.touch, ...m, ...p } |
| 121 | if (c.kind === 'phone' || c.kind === 'landscape') { |
| 122 | await page.goto(`${job.origin}/?lang=${c.lang}&scheme=${c.scheme}&inline=1`, { waitUntil: 'load' }) |
| 123 | await page.evaluate(() => document.fonts.ready) |
| 124 | entry.rest = await page.evaluate(large) |
| 125 | if (job.shots) await page.screenshot({ path: file('-large') }) |
| 126 | await page.evaluate(() => document.querySelector('[estilo-bar]').setAttribute('estilo-bar-folded', '')) |
| 127 | await page.waitForTimeout(300) |
| 128 | entry.folded = await page.evaluate(large) |
| 129 | if (job.shots) await page.screenshot({ path: file('-folded') }) |
| 130 | } |
| 131 | if (c.kind === 'phone' && c.lang === 'en' && c.scheme === 'light') { |
| 132 | await page.goto(`${job.origin}/?lang=en&scheme=light&public=1`, { waitUntil: 'load' }) |
| 133 | entry.public = await page.evaluate(() => { |
| 134 | const probe = document.createElement('span') |
| 135 | probe.style.color = 'var(--rst-accent)' |
| 136 | document.body.append(probe) |
| 137 | const accent = getComputedStyle(probe).color |
| 138 | probe.remove() |
| 139 | return { brand: getComputedStyle(document.querySelector('[estilo-bar-brand]')).color, accent } |
| 140 | }) |
| 141 | if (job.shots) await page.screenshot({ path: file('-public') }) |
| 142 | } else { |
| 143 | entry.public = { brand: 'n/a', accent: '' } |
| 144 | } |
| 145 | result.cases.push(entry) |
| 146 | await ctx.close() |
| 147 | } catch (e) { |
| 148 | result.cases.push({ name: c.name, width: c.w, touch: c.touch, error: String(e && e.message || e).split('\n')[0] }) |
| 149 | } |
| 150 | } |
| 151 | // A compact bar loaded narrow and widened into the 704-800px band is |
| 152 | // the 56px header there; and in forced colours the band's controls |
| 153 | // keep the border the forced-colors rule gives them, as from 800px. |
| 154 | result.extra = {} |
| 155 | try { |
| 156 | const ctx = await browser.newContext({ viewport: { width: 650, height: 900 }, deviceScaleFactor: 2, hasTouch: true, isMobile: engine !== 'firefox' }) |
| 157 | const page = await ctx.newPage() |
| 158 | await page.goto(`${job.origin}/?lang=en&scheme=light`, { waitUntil: 'load' }) |
| 159 | await page.evaluate(() => document.fonts.ready) |
| 160 | const at650 = await page.evaluate(measure) |
| 161 | await page.setViewportSize({ width: 768, height: 1024 }) |
| 162 | await page.waitForTimeout(300) |
| 163 | const at768 = await page.evaluate(measure) |
| 164 | result.extra.resized = { from: at650.header, header: at768.header, var: at768.var } |
| 165 | await ctx.close() |
| 166 | } catch (e) { |
| 167 | result.extra.resizedError = String(e && e.message || e).split('\n')[0] |
| 168 | } |
| 169 | result.extra.forced = {} |
| 170 | for (const w of [768, 1280]) { |
| 171 | try { |
| 172 | const ctx = await browser.newContext({ viewport: { width: w, height: 800 }, forcedColors: 'active' }) |
| 173 | const page = await ctx.newPage() |
| 174 | await page.goto(`${job.origin}/?lang=en&scheme=light`, { waitUntil: 'load' }) |
| 175 | result.extra.forced[w] = await page.evaluate(() => { |
| 176 | const el = document.querySelector('[estilo-bar-control~="primary"] > a') |
| 177 | return { border: parseFloat(getComputedStyle(el).borderTopWidth), forced: matchMedia('(forced-colors: active)').matches } |
| 178 | }) |
| 179 | await ctx.close() |
| 180 | } catch (e) { |
| 181 | result.extra.forced[w] = { error: String(e && e.message || e).split('\n')[0] } |
| 182 | } |
| 183 | } |
| 184 | // Mapa and Fichas, as stand-ins: stacked bars with nav, not compact. |
| 185 | result.standIns = [] |
| 186 | for (const kind of ['mapa', 'fichas']) { |
| 187 | for (const [w, h, touch] of [[390, 844, true], [768, 1024, true], [844, 390, true], [1280, 800, false]]) { |
| 188 | const name = `${kind} ${w}x${h}${touch ? ' touch' : ''}` |
| 189 | try { |
| 190 | const opts = { viewport: { width: w, height: h }, deviceScaleFactor: 2, colorScheme: 'light' } |
| 191 | if (touch) Object.assign(opts, { hasTouch: true, isMobile: engine !== 'firefox' }) |
| 192 | const ctx = await browser.newContext(opts) |
| 193 | const page = await ctx.newPage() |
| 194 | page.setDefaultTimeout(5000) |
| 195 | await page.goto(`${job.origin}/?kind=${kind}&lang=en&scheme=light`, { waitUntil: 'load' }) |
| 196 | await page.evaluate(() => document.fonts.ready) |
| 197 | await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) |
| 198 | const shot = (s) => `${job.shots}/${engine}-${kind}-${w}x${h}${s}.png` |
| 199 | if (job.shots) await page.screenshot({ path: shot('') }) |
| 200 | const m = await page.evaluate(() => { |
| 201 | const shown = (el) => el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden' |
| 202 | const label = (el) => (el.getAttribute('aria-label') || el.textContent).trim() |
| 203 | const bar = document.querySelector('[estilo-bar]') |
| 204 | const probe = document.createElement('div') |
| 205 | probe.style.blockSize = 'var(--estilo-bar-block-size)' |
| 206 | document.body.append(probe) |
| 207 | const v = probe.getBoundingClientRect().height |
| 208 | probe.remove() |
| 209 | const nav = [...bar.querySelectorAll('[estilo-bar-nav] a')] |
| 210 | const acts = [...bar.querySelectorAll('[estilo-bar-actions] :is(a, button)')].filter((el) => !el.matches('[estilo-bar-wide], [estilo-bar-control~="wide"] *')) |
| 211 | return { |
| 212 | header: bar.getBoundingClientRect().height, var: v, |
| 213 | overflow: document.documentElement.scrollWidth > window.innerWidth, |
| 214 | wanted: [...nav, ...acts].map(label), |
| 215 | reached: [...nav, ...acts].filter(shown).map(label), |
| 216 | more: !!bar.querySelector('[estilo-bar-more]') && shown(bar.querySelector('[estilo-bar-more]')), |
| 217 | } |
| 218 | }) |
| 219 | if (m.more) { |
| 220 | await page.locator('[estilo-bar-more] > summary').click() |
| 221 | await page.evaluate(() => new Promise((r) => requestAnimationFrame(r))) |
| 222 | const rows = await page.evaluate(() => [...document.querySelectorAll('[estilo-bar-more] [estilo-menu-item]')].filter((el) => el.getClientRects().length > 0).map((el) => el.textContent.trim())) |
| 223 | m.reached.push(...rows) |
| 224 | if (job.shots) await page.screenshot({ path: shot('-open') }) |
| 225 | } |
| 226 | result.standIns.push({ name, width: w, ...m }) |
| 227 | await ctx.close() |
| 228 | } catch (e) { |
| 229 | result.standIns.push({ name, width: w, error: String(e && e.message || e).split('\n')[0] }) |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } catch (e) { |
| 234 | result.error = String(e && e.stack || e) |
| 235 | } finally { |
| 236 | if (browser) await browser.close() |
| 237 | } |
| 238 | } |
| 239 | process.stdout.write(JSON.stringify(results)) |
| 240 | |