repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui widget browser layout tests

cccb7a805763fede57cbd5652d8dada9635da8d2

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

+101 / −0 text lines

Diff layout

extensions/ui/__tests__/widget-layout.test.ts

Added
new file mode 100644

+101 / −0 text lines

1import { existsSync } from "node:fs";
2import { type BrowserType, chromium, firefox, type Page } from "playwright";
3import { describe, expect, it } from "vitest";
4
5const chromePath = "C:/Program Files/Google/Chrome/Application/chrome.exe";
6
7describe("ui widget browser layout", () => {
8 it("lets unsandboxed srcdoc widgets call parent window.pi in Chrome and Firefox", async () => {
9 for (const browser of browsers()) {
10 const result = await withBrowser(browser, async (page) => {
11 await page.setContent(bridgeProbeHtml());
12 await page.waitForFunction(() => window.calls?.length === 1);
13 return await page.evaluate(() => window.calls[0]);
14 });
15 expect(result).toEqual({ from: "srcdoc", sameOrigin: true });
16 }
17 }, 120_000);
19 it("keeps over-wide iframe content bounded at phone width in Chrome and Firefox", async () => {
20 for (const browser of browsers()) {
21 const result = await withBrowser(browser, async (page) => {
22 await page.setViewportSize({ width: 390, height: 760 });
23 await page.setContent(layoutProbeHtml());
24 await page.waitForFunction(
25 () => document.querySelector("iframe")?.clientHeight === 600,
26 );
27 return await page.evaluate(() => ({
28 viewport: document.documentElement.clientWidth,
29 scrollWidth: document.documentElement.scrollWidth,
30 iframeHeight: document
31 .querySelector("iframe")
32 ?.getBoundingClientRect().height,
33 }));
34 });
35 expect(result).toEqual({
36 viewport: 390,
37 scrollWidth: 390,
38 iframeHeight: 600,
39 });
40 }
41 }, 120_000);
42});
44function browsers(): Array<{
45 name: string;
46 type: BrowserType;
47 executablePath?: string;
48}> {
49 return [
50 ...(existsSync(chromePath)
51 ? [{ name: "chrome", type: chromium, executablePath: chromePath }]
52 : []),
53 { name: "firefox", type: firefox },
54 ];
55}
57async function withBrowser<T>(
58 browser: { name: string; type: BrowserType; executablePath?: string },
59 fn: (page: Page) => Promise<T>,
60): Promise<T> {
61 const instance = await browser.type.launch({
62 headless: true,
63 executablePath: browser.executablePath,
64 });
65 try {
66 const page = await instance.newPage();
67 return await fn(page);
68 } finally {
69 await instance.close();
70 }
71}
73function bridgeProbeHtml(): string {
74 const srcdoc =
75 "<script>parent.pi.action({from:'srcdoc',sameOrigin:!!parent.document});</" +
76 "script>";
77 const attr = srcdoc.replaceAll(String.fromCharCode(34), "&quot;");
78 return [
79 "<!doctype html>",
80 "<script>window.calls=[];window.pi={action(x){window.calls.push(x)}};</script>",
81 `<iframe srcdoc="${attr}"></iframe>`,
82 ].join("");
83}
85function layoutProbeHtml(): string {
86 const srcdoc = [
87 "<style>body{margin:0}.wide{width:2200px;height:80px}.tall{height:900px}</style>",
88 "<div class=wide>x</div><div class=tall>y</div>",
89 "<script>parent.postMessage({height:document.documentElement.scrollHeight},'*')</" +
90 "script>",
91 ].join("");
92 const attr = srcdoc
93 .replaceAll("&", "&amp;")
94 .replaceAll(String.fromCharCode(34), "&quot;");
95 return [
96 "<!doctype html>",
97 "<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>",
98 `<div class=wrap><div class=visual><iframe srcdoc="${attr}"></iframe></div></div>`,
99 "<script>addEventListener('message',e=>{document.querySelector('iframe').style.height=Math.min(e.data.height,600)+'px'})</script>",
100 ].join("");
101}