import { fileURLToPath } from "node:url"; import { expect, test, type Page, } from "../support/browser/strict-browser-test.ts"; import { KO_MESSAGES } from "../../src/presentation/i18n/catalog.ts"; /** * 1106/08-03. `scrollbar-gutter: stable` (base.css, 1105/15-05) keeps a * classic scrollbar's strip on the right even when the page does not scroll. * Only the root background paints that strip; the page and every fixed box, * the modal `::backdrop` included, end at its left edge. The goldens run with * Playwright's `--hide-scrollbars`, so this spec starts Chromium with the * scrollbar most Windows and Linux desktops draw and reads the drawn pixels. * Hit testing is no evidence here: the strip answers `null` either way. */ test.use({ launchOptions: { // `test.use` replaces the config's launchOptions whole: the font pin // from playwright.visual.config.ts is repeated. env: { ...process.env, FONTCONFIG_FILE: fileURLToPath(new URL("./fonts.conf", import.meta.url)), }, ignoreDefaultArgs: ["--hide-scrollbars"], }, }); type Colour = readonly number[]; type Strip = Readonly<{ x: number; width: number }>; /** * The largest channel difference the gutter may show against the strip * beside it. The drawer's shadow (`--shadow-dialog`, 70 px blur) still * darkens the strip beside the gutter by about 3 levels; rounding adds 1 or * 2. In the light theme a gutter the scrim does not cover * (the page surface against 55% of the content colour over it) differs by * about 120. */ const SAME_COLOUR = 12; /** The mean colour of each strip, from one screenshot of the band. */ async function stripColours( page: Page, band: Readonly<{ y: number; height: number }>, strips: readonly Strip[], ): Promise { const left = Math.min(...strips.map((strip) => strip.x)); const right = Math.max(...strips.map((strip) => strip.x + strip.width)); const png = await page.screenshot({ animations: "disabled", clip: { x: left, y: band.y, width: right - left, height: band.height }, scale: "css", }); // Decoded in a blank page, so the page under test runs nothing extra. const decoder = await page.context().newPage(); try { return await decoder.evaluate( async ({ base64, offsets }) => { const bytes = Uint8Array.from(atob(base64), (character) => character.charCodeAt(0), ); const bitmap = await createImageBitmap( new Blob([bytes], { type: "image/png" }), ); const canvas = new OffscreenCanvas(bitmap.width, bitmap.height); const context = canvas.getContext("2d"); if (context === null) throw new Error("no 2d canvas context"); context.drawImage(bitmap, 0, 0); return offsets.map(({ x, width }) => { const { data } = context.getImageData(x, 0, width, bitmap.height); const sums = [0, 0, 0]; for (let index = 0; index < data.length; index += 4) { for (let channel = 0; channel < 3; channel += 1) { sums[channel] += data[index + channel]; } } return sums.map((sum) => Math.round(sum / (data.length / 4))); }); }, { base64: png.toString("base64"), offsets: strips.map((strip) => ({ x: strip.x - left, width: strip.width })), }, ); } finally { await decoder.close(); } } async function expectGutterLikeBeside( page: Page, geometry: Readonly<{ band: Readonly<{ y: number; height: number }>; beside: Strip; gutter: Strip; }>, what: string, ) { const [beside, gutter] = await stripColours(page, geometry.band, [ geometry.beside, geometry.gutter, ]); const difference = Math.max( ...gutter.map((value, channel) => Math.abs(value - beside[channel])), ); // On failure, tells a rule that did not match from a colour not drawn. const root = await page.evaluate(() => { const style = getComputedStyle(document.documentElement); return `${style.backgroundColor} / ${style.backgroundImage}, open dialogs ${document.querySelectorAll(".ui-dialog[open]").length}`; }); expect( difference, `${what}: gutter rgb(${gutter.join(" ")}), beside it rgb(${beside.join(" ")}); root background ${root}`, ).toBeLessThanOrEqual(SAME_COLOUR); } test("the open drawer's scrim covers the classic scrollbar gutter on a page that does not scroll", async ({ page, }) => { await page.setViewportSize({ width: 390, height: 844 }); await page.goto("/not-found"); await expect(page.getByRole("heading", { level: 1 })).toHaveText( KO_MESSAGES["page.notFound.title"], ); const layout = await page.evaluate(() => ({ windowWidth: window.innerWidth, windowHeight: window.innerHeight, // With the gutter empty, documentElement.clientWidth is the window // width (08-R2); the page's own box ends at the gutter. pageRight: Math.round(document.body.getBoundingClientRect().right), scrolls: document.documentElement.scrollHeight > document.documentElement.clientHeight, })); expect(layout.scrolls, "a page that scrolls draws its scrollbar in the gutter").toBe( false, ); expect( layout.windowWidth - layout.pageRight, "Chromium without --hide-scrollbars reserves a classic scrollbar's gutter", ).toBeGreaterThanOrEqual(10); // A band at mid-height. The strip beside the gutter lies in the page's own // padding, 5 px in from its edge so the scrim's 2 px blur stays inside it; // the gutter strip keeps 3 px off both of its edges. const geometry = { band: { y: Math.round(layout.windowHeight / 2) - 20, height: 40 }, beside: { x: layout.pageRight - 10, width: 5 }, gutter: { x: layout.pageRight + 3, width: layout.windowWidth - layout.pageRight - 6 }, }; await expectGutterLikeBeside(page, geometry, "closed, the gutter shows the page surface"); const menu = page.getByRole("button", { name: KO_MESSAGES["shell.menu"], exact: true }); await menu.click(); await expect(page.locator(".ui-drawer")).toHaveJSProperty("open", true); await expectGutterLikeBeside(page, geometry, "open, the scrim reaches the window edge"); await page.keyboard.press("Escape"); await expect(page.locator(".ui-drawer")).toHaveJSProperty("open", false); await expectGutterLikeBeside(page, geometry, "closed again, the gutter shows the page surface"); });