| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { BrowserVisibility } from "./browser-visibility.mjs"; |
| 4 | |
| 5 | function fixture() { |
| 6 | const windows = new Map([[1, { windowState: "fullscreen", left: 0, top: 0, width: 1200, height: 800 }]]); |
| 7 | const targets = new Map([["one", 1], ["two", 1]]); |
| 8 | const calls = []; |
| 9 | let legacyViewer = false; |
| 10 | const cdp = async (method, input) => { |
| 11 | calls.push({ method, ...input }); |
| 12 | if (method === "Browser.getWindowForTarget") { |
| 13 | const windowId = targets.get(input.targetId); |
| 14 | return { windowId, bounds: { ...windows.get(windowId) } }; |
| 15 | } |
| 16 | if (method === "Browser.getWindowBounds") return { bounds: { ...windows.get(input.windowId) } }; |
| 17 | const current = windows.get(input.windowId); |
| 18 | assert.ok(current); |
| 19 | if (input.bounds.windowState === "minimized") assert.equal(current.windowState, "normal"); |
| 20 | Object.assign(current, input.bounds); |
| 21 | }; |
| 22 | const visibility = new BrowserVisibility(cdp, () => [...targets.keys()], async () => !legacyViewer); |
| 23 | return { visibility, windows, targets, calls, setLegacyViewer: value => { legacyViewer = value; } }; |
| 24 | } |
| 25 | |
| 26 | test("browser visibility restores fullscreen and processes shared windows once", async () => { |
| 27 | const f = fixture(); |
| 28 | await f.visibility.set(false); |
| 29 | assert.equal(f.windows.get(1).windowState, "minimized"); |
| 30 | assert.equal(f.calls.filter(call => call.method === "Browser.setWindowBounds").length, 2); |
| 31 | await f.visibility.set(false); |
| 32 | assert.equal(f.calls.filter(call => call.method === "Browser.setWindowBounds").length, 2); |
| 33 | await f.visibility.set(true); |
| 34 | assert.equal(f.windows.get(1).windowState, "fullscreen"); |
| 35 | assert.equal(f.visibility.saved.size, 0); |
| 36 | }); |
| 37 | |
| 38 | test("browser visibility preserves geometry, closed windows and legacy viewers", async () => { |
| 39 | const f = fixture(); |
| 40 | const bounds = { windowState: "normal", left: 40, top: 80, width: 900, height: 600 }; |
| 41 | f.windows.set(2, { ...bounds }); f.targets.set("popup", 2); |
| 42 | f.setLegacyViewer(true); |
| 43 | await f.visibility.set(false); |
| 44 | assert.equal(f.calls.filter(call => call.method === "Browser.setWindowBounds").length, 0); |
| 45 | f.setLegacyViewer(false); |
| 46 | await f.visibility.set(false); |
| 47 | f.targets.delete("one"); f.targets.delete("two"); f.windows.delete(1); |
| 48 | await f.visibility.set(true); |
| 49 | assert.deepEqual(f.windows.get(2), bounds); |
| 50 | assert.equal(f.visibility.saved.size, 0); |
| 51 | }); |
| 52 | |
| 53 | test("a newer browser viewer restores a hide that is already running", async () => { |
| 54 | let release, entered; |
| 55 | const blocked = new Promise(resolve => { entered = resolve; }); |
| 56 | const ready = new Promise(resolve => { release = resolve; }); |
| 57 | let state = "fullscreen"; |
| 58 | const visibility = new BrowserVisibility(async (method, input) => { |
| 59 | if (method === "Target.activateTarget") return {}; |
| 60 | if (method === "Browser.getWindowForTarget") return { windowId: 1, bounds: { windowState: state } }; |
| 61 | if (method === "Browser.getWindowBounds") return { bounds: { windowState: state } }; |
| 62 | if (input.bounds.windowState === "minimized") { entered(); await ready; } |
| 63 | state = input.bounds.windowState; |
| 64 | }, () => ["one"]); |
| 65 | const hidden = visibility.set(false); |
| 66 | await blocked; |
| 67 | const activated = visibility.activate("one"); |
| 68 | const shown = visibility.set(true); |
| 69 | release(); |
| 70 | await Promise.all([hidden, activated, shown]); |
| 71 | assert.equal(state, "fullscreen"); |
| 72 | assert.equal(visibility.saved.size, 0); |
| 73 | }); |