|
| 1 | +import { createServer } from "node:http" |
| 2 | +import electron from "electron" |
| 3 | +import { createExtensionHost } from "./profile/extension-api/host" |
| 4 | +import { createManifests } from "./profile/extension-api/manifest" |
| 5 | + |
| 6 | +// The Electron main process of `electron-app-badge.test.ts`, bundled and run by the pinned Electron. A page of a |
| 7 | +// partition without the profile's preload sets the app's badge to 7. Then a page of a partition with it, a child |
| 8 | +// frame of that page and its service worker each ask for another number, and the page clears the badge. It prints |
| 9 | +// the app's badge count after each as one JSON line. |
| 10 | + |
| 11 | +const child = `<!doctype html><script>navigator.setAppBadge(4).then(() => parent.postMessage("frame", "*"))</script>` |
| 12 | + |
| 13 | +const script = `onmessage = (event) => navigator.setAppBadge(5).then(() => event.source.postMessage("worker"))` |
| 14 | + |
| 15 | +const page = `<!doctype html><title>page</title><body><script> |
| 16 | +const heard = (target) => new Promise((resolve) => target.addEventListener("message", (event) => resolve(event.data), { once: true })) |
| 17 | +const frame = () => { |
| 18 | + const reply = heard(window) |
| 19 | + document.body.append(Object.assign(document.createElement("iframe"), { src: "/frame" })) |
| 20 | + return reply |
| 21 | +} |
| 22 | +const worker = async () => { |
| 23 | + await navigator.serviceWorker.register("/worker.js") |
| 24 | + const registration = await navigator.serviceWorker.ready |
| 25 | + const reply = heard(navigator.serviceWorker) |
| 26 | + registration.active.postMessage("badge") |
| 27 | + return reply |
| 28 | +} |
| 29 | +</script>` |
| 30 | + |
| 31 | +const server = createServer((request, response) => { |
| 32 | + const worker = request.url === "/worker.js" |
| 33 | + response.writeHead(200, { "content-type": worker ? "text/javascript" : "text/html" }) |
| 34 | + response.end(worker ? script : request.url === "/frame" ? child : page) |
| 35 | +}) |
| 36 | + |
| 37 | +void electron.app.whenReady().then(async () => { |
| 38 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)) |
| 39 | + const address = server.address() |
| 40 | + const origin = `http://127.0.0.1:${address instanceof Object ? address.port : 0}` |
| 41 | + const win = new electron.BrowserWindow({ show: false }) |
| 42 | + |
| 43 | + // A page as the pane opens one for a server on this machine (`createBrowserPage`), in a partition kept on disk as |
| 44 | + // the profile's is: Electron answers the badge calls of no other kind of partition. |
| 45 | + const open = async (partition: string) => { |
| 46 | + const view = new electron.WebContentsView({ |
| 47 | + webPreferences: { partition, sandbox: true, contextIsolation: true, nodeIntegrationInSubFrames: true }, |
| 48 | + }) |
| 49 | + |
| 50 | + win.contentView.addChildView(view) |
| 51 | + await view.webContents.loadURL(origin) |
| 52 | + |
| 53 | + return view.webContents |
| 54 | + } |
| 55 | + |
| 56 | + const host = await createExtensionHost({ |
| 57 | + session: electron.session.fromPartition("persist:profile"), |
| 58 | + directory: electron.app.getPath("userData"), |
| 59 | + modules: [], |
| 60 | + log: { write: () => undefined }, |
| 61 | + manifest: createManifests(), |
| 62 | + }) |
| 63 | + |
| 64 | + const plain = await open("persist:plain") |
| 65 | + await plain.executeJavaScript("navigator.setAppBadge(7)") |
| 66 | + // Main hears of a badge a moment after the page hears its call resolve. |
| 67 | + const deadline = Date.now() + 10_000 |
| 68 | + |
| 69 | + while (electron.app.getBadgeCount() !== 7 && Date.now() < deadline) |
| 70 | + await new Promise((resolve) => setTimeout(resolve, 20)) |
| 71 | + |
| 72 | + const unguarded = electron.app.getBadgeCount() |
| 73 | + const profile = await open("persist:profile") |
| 74 | + |
| 75 | + // The app's count once the profile's page, frame or worker heard its own call resolve. The page's answer can |
| 76 | + // overtake a badge on its way to main, so one more round trip to the page goes first. A frame's badge can take |
| 77 | + // longer still, and then shows in the next count. |
| 78 | + const after = async (call: string) => { |
| 79 | + await profile.executeJavaScript(call) |
| 80 | + await profile.executeJavaScript("0") |
| 81 | + |
| 82 | + return electron.app.getBadgeCount() |
| 83 | + } |
| 84 | + |
| 85 | + const result = { |
| 86 | + unguarded, |
| 87 | + page: await after("navigator.setAppBadge(3)"), |
| 88 | + frame: await after("frame()"), |
| 89 | + worker: await after("worker()"), |
| 90 | + cleared: await after("navigator.clearAppBadge()"), |
| 91 | + } |
| 92 | + |
| 93 | + console.log(JSON.stringify(result)) |
| 94 | + host.dispose() |
| 95 | + server.close() |
| 96 | + electron.app.exit(0) |
| 97 | +}) |
0 commit comments