Add ui widget browser layout tests
cccb7a805763fede57cbd5652d8dada9635da8d2
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
- Parent 1: fbbfeae7849e
bugabingas pi extensions
cccb7a805763fede57cbd5652d8dada9635da8d2
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
+101 / −0 text lines
new file mode 100644+101 / −0 text lines
import { existsSync } from "node:fs";import { type BrowserType, chromium, firefox, type Page } from "playwright";import { describe, expect, it } from "vitest";const chromePath = "C:/Program Files/Google/Chrome/Application/chrome.exe";describe("ui widget browser layout", () => { it("lets unsandboxed srcdoc widgets call parent window.pi in Chrome and Firefox", async () => { for (const browser of browsers()) { const result = await withBrowser(browser, async (page) => { await page.setContent(bridgeProbeHtml()); await page.waitForFunction(() => window.calls?.length === 1); return await page.evaluate(() => window.calls[0]); }); expect(result).toEqual({ from: "srcdoc", sameOrigin: true }); } }, 120_000); it("keeps over-wide iframe content bounded at phone width in Chrome and Firefox", async () => { for (const browser of browsers()) { const result = await withBrowser(browser, async (page) => { await page.setViewportSize({ width: 390, height: 760 }); await page.setContent(layoutProbeHtml()); await page.waitForFunction( () => document.querySelector("iframe")?.clientHeight === 600, ); return await page.evaluate(() => ({ viewport: document.documentElement.clientWidth, scrollWidth: document.documentElement.scrollWidth, iframeHeight: document .querySelector("iframe") ?.getBoundingClientRect().height, })); }); expect(result).toEqual({ viewport: 390, scrollWidth: 390, iframeHeight: 600, }); } }, 120_000);});function browsers(): Array<{ name: string; type: BrowserType; executablePath?: string;}> { return [ ...(existsSync(chromePath) ? [{ name: "chrome", type: chromium, executablePath: chromePath }] : []), { name: "firefox", type: firefox }, ];}async function withBrowser<T>( browser: { name: string; type: BrowserType; executablePath?: string }, fn: (page: Page) => Promise<T>,): Promise<T> { const instance = await browser.type.launch({ headless: true, executablePath: browser.executablePath, }); try { const page = await instance.newPage(); return await fn(page); } finally { await instance.close(); }}function bridgeProbeHtml(): string { const srcdoc = "<script>parent.pi.action({from:'srcdoc',sameOrigin:!!parent.document});</" + "script>"; const attr = srcdoc.replaceAll(String.fromCharCode(34), """); return [ "<!doctype html>", "<script>window.calls=[];window.pi={action(x){window.calls.push(x)}};</script>", `<iframe srcdoc="${attr}"></iframe>`, ].join("");}function layoutProbeHtml(): string { const srcdoc = [ "<style>body{margin:0}.wide{width:2200px;height:80px}.tall{height:900px}</style>", "<div class=wide>x</div><div class=tall>y</div>", "<script>parent.postMessage({height:document.documentElement.scrollHeight},'*')</" + "script>", ].join(""); const attr = srcdoc .replaceAll("&", "&") .replaceAll(String.fromCharCode(34), """); return [ "<!doctype html>", "<style>*{box-sizing:border-box}body{margin:0}.wrap{width:min(960px,100vw);overflow:auto}.visual{min-width:0;overflow:auto;contain:layout paint;padding:12px}.visual iframe{display:block;width:100%;height:120px;max-height:600px;border:0}</style>", `<div class=wrap><div class=visual><iframe srcdoc="${attr}"></iframe></div></div>`, "<script>addEventListener('message',e=>{document.querySelector('iframe').style.height=Math.min(e.data.height,600)+'px'})</script>", ].join("");}