Files
openidea/src/workbench/fullscreenEscape.test.ts
T
l0ng-ai 34f2e18ff1 fix(macos): keep full screen when Escape goes unhandled (#4006)
Escape the page leaves unhandled goes back from WebKit as the web view's doCommandBySelector:(cancelOperation:), run through NSView's own implementation, which leaves full screen directly; the window-level cancelOperation: hook from #3134 was never reached and is removed. In macOS full screen the workbench now marks Escape handled after every key handler has seen it (also when one stops it); Ctrl+Cmd+F leaves full screen.

Refs #3970, #3104
2026-11-17 20:58:44 +08:00

54 lines
1.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { usePlatform } from "@/platform/platformInfo";
import { installFullscreenEscapeGuard } from "./fullscreenEscape";
let fullscreen = true;
let dispose = () => {};
const escape = (target: EventTarget) => {
const e = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true });
target.dispatchEvent(e);
return e;
};
beforeEach(() => {
fullscreen = true;
usePlatform.setState({ os: "macos" });
dispose = installFullscreenEscapeGuard(window, () => fullscreen);
});
afterEach(() => {
dispose();
document.body.replaceChildren();
});
describe("Escape in full screen", () => {
it("is marked handled when nothing handles it, so the window stays in full screen", () => {
expect(escape(document.body).defaultPrevented).toBe(true);
});
it("is marked handled when a handler stops it", () => {
const popup = document.body.appendChild(document.createElement("div"));
popup.addEventListener("keydown", (e) => e.stopPropagation());
expect(escape(popup).defaultPrevented).toBe(true);
// The next key press is not affected by a listener left behind.
fullscreen = false;
expect(escape(document.body).defaultPrevented).toBe(false);
});
it("reaches other handlers not yet default-prevented", () => {
let seen: boolean | null = null;
const onKey = (e: KeyboardEvent) => (seen = e.defaultPrevented);
window.addEventListener("keydown", onKey);
escape(document.body);
window.removeEventListener("keydown", onKey);
expect(seen).toBe(false);
});
it("is left alone outside full screen and on other systems", () => {
fullscreen = false;
expect(escape(document.body).defaultPrevented).toBe(false);
fullscreen = true;
usePlatform.setState({ os: "windows" });
expect(escape(document.body).defaultPrevented).toBe(false);
});
});