| 1 | import type * as Electron from "electron"; |
| 2 | import { beforeEach, expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | const switches = vi.hoisted(() => new Set<string>()); |
| 5 | vi.mock("electron", () => ({ |
| 6 | app: { |
| 7 | commandLine: { |
| 8 | hasSwitch: (name: string) => switches.has(name), |
| 9 | appendSwitch: (name: string) => switches.add(name), |
| 10 | removeSwitch: (name: string) => switches.delete(name), |
| 11 | }, |
| 12 | }, |
| 13 | })); |
| 14 | |
| 15 | import { showWindowsCaptureOverlay } from "./WindowsCaptureFeedback.ts"; |
| 16 | |
| 17 | const animationSwitch = "wm-window-animations-disabled"; |
| 18 | |
| 19 | function overlay(showInactive: () => void) { |
| 20 | return { showInactive } as Electron.BaseWindow; |
| 21 | } |
| 22 | |
| 23 | beforeEach(() => { |
| 24 | switches.clear(); |
| 25 | switches.add("unrelated-switch"); |
| 26 | }); |
| 27 | |
| 28 | it("disables Chromium window animation only while showing the capture overlay", () => { |
| 29 | const flagsDuringShow: string[][] = []; |
| 30 | |
| 31 | showWindowsCaptureOverlay(overlay(() => flagsDuringShow.push([...switches]))); |
| 32 | |
| 33 | expect(flagsDuringShow).toEqual([["unrelated-switch", animationSwitch]]); |
| 34 | expect([...switches]).toEqual(["unrelated-switch"]); |
| 35 | }); |
| 36 | |
| 37 | it("preserves an animation switch supplied before capture", () => { |
| 38 | switches.add(animationSwitch); |
| 39 | const flagsDuringShow: string[][] = []; |
| 40 | |
| 41 | showWindowsCaptureOverlay(overlay(() => flagsDuringShow.push([...switches]))); |
| 42 | |
| 43 | expect(flagsDuringShow).toEqual([["unrelated-switch", animationSwitch]]); |
| 44 | expect([...switches]).toEqual(["unrelated-switch", animationSwitch]); |
| 45 | }); |
| 46 | |
| 47 | it.each([false, true])( |
| 48 | "restores the original switch state when showing throws (preexisting: %s)", |
| 49 | (preexisting) => { |
| 50 | if (preexisting) switches.add(animationSwitch); |
| 51 | const original = [...switches]; |
| 52 | const failure = new Error("Overlay was destroyed"); |
| 53 | const window = overlay(() => { |
| 54 | expect(switches.has(animationSwitch)).toBe(true); |
| 55 | throw failure; |
| 56 | }); |
| 57 | |
| 58 | expect(() => showWindowsCaptureOverlay(window)).toThrow(failure); |
| 59 | expect([...switches]).toEqual(original); |
| 60 | }, |
| 61 | ); |