Файли
openidea/src/features/plugins/__tests__/webviewKeys.test.tsx
T
Deng Liuтаdengliu e13ed093ec fix(workbench): skip hidden tool-window focus targets and track plugin iframe focus (#6094)
Activating a tool window focused its first [data-autofocus] or focusable descendant without checking visibility, so a hidden file input or an inactive tab panel took the focus as a no-op and left it in the previous window. Activation now skips hidden, inert and display:none / visibility:hidden targets and falls back to the tool window body.

Focus inside a sandboxed plugin iframe doesn't bubble focusin to the host, so focus scopes, toolWindowFocus / activeToolWindow and auto-hide of unpinned tool windows stayed stale. The frame bridge now reports activation, accepted only while the host document has focus and the frame is its active element, and window blur into an iframe is tracked too.

Co-authored-by: dengliu <[email protected]>
2026-11-27 01:37:36 +08:00

198 рядки
7.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { render, waitFor } from "@testing-library/react";
import type { PluginInfo } from "@/bindings";
import { registerAction } from "@/platform/actions";
import { installKeyboardDispatcher, useKeymap } from "@/platform/keymap";
import { installFocusScopeTracking } from "@/ui/focusScope";
import { installFocusTracking } from "@/workbench/focus";
import { createMemoryPluginsBackend, setPluginsBackend } from "../backend";
import { usePlugins } from "../store";
import { FRAME_TAG } from "../views/frame";
import { WebView } from "../views/WebView";
let uninstall = () => {};
afterEach(() => {
uninstall();
setPluginsBackend(null);
usePlugins.setState({ plugins: [] });
useKeymap.setState({ overrides: {} });
Reflect.deleteProperty(navigator, "userActivation");
vi.restoreAllMocks();
});
/** What a browser reports after the user's own input in the frame (`isActive`), or long after it. */
function reportUserActivation(isActive: boolean) {
Object.defineProperty(navigator, "userActivation", { configurable: true, value: { isActive } });
}
async function mount(capabilities: { id: string; granted: boolean }[]) {
setPluginsBackend({ ...createMemoryPluginsBackend([]), webPageUrl: async () => "about:blank" });
usePlugins.setState({
plugins: [{ id: "evil.view", hasCode: false, enabled: true, capabilities } as unknown as PluginInfo],
});
const view = render(<WebView pluginId="evil.view" view="v" label="V" />);
const frame = await waitFor(() => {
const found = view.container.querySelector("iframe");
expect(found).not.toBeNull();
return found!;
});
return frame;
}
/** What page script in the frame can post at any time, with no user gesture. */
function pass(frame: HTMLIFrameElement, key: KeyboardEventInit) {
window.dispatchEvent(new MessageEvent("message", { data: { tag: FRAME_TAG, key }, source: frame.contentWindow }));
}
const SAVE = { key: "s", code: "KeyS", metaKey: true };
const RUN = { key: "F10", code: "F10", shiftKey: true };
function bind() {
const save = vi.fn();
const run = vi.fn();
registerAction({ id: "file.save", title: "Save", run: save });
registerAction({ id: "t.runSelected", title: "Run 'app'", run });
useKeymap.setState({ overrides: { "file.save": ["Meta+S"], "t.runSelected": ["Shift+F10"] } });
uninstall = installKeyboardDispatcher(window);
return { save, run };
}
const flush = async () => {
await Promise.resolve();
await Promise.resolve();
};
describe("keys a plugin web view passes on", () => {
it("run nothing while the user is not in the frame", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(true);
const { save, run } = bind();
const frame = await mount([]);
pass(frame, SAVE);
pass(frame, RUN);
await flush();
expect(save).not.toHaveBeenCalled();
expect(run).not.toHaveBeenCalled();
});
it("run nothing while the window has no focus", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(false);
const { save } = bind();
const frame = await mount([]);
frame.focus();
pass(frame, SAVE);
await flush();
expect(save).not.toHaveBeenCalled();
});
it("run a safe action, and only a safe one, for a plugin without ide.commands", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(true);
const { save, run } = bind();
const frame = await mount([{ id: "clipboard", granted: true }]);
frame.focus();
expect(document.activeElement).toBe(frame);
pass(frame, SAVE);
pass(frame, RUN);
await flush();
expect(save).toHaveBeenCalledOnce();
expect(run).not.toHaveBeenCalled();
});
it("run any bound action for a plugin granted ide.commands, but still only while the user is in the frame", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(true);
const { run } = bind();
const frame = await mount([{ id: "ide.commands", granted: true }]);
pass(frame, RUN);
await flush();
expect(run).not.toHaveBeenCalled();
frame.focus();
pass(frame, RUN);
await flush();
expect(run).toHaveBeenCalledOnce();
});
it("run nothing when the user has not touched the frame lately, even if the page took the focus", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(true);
reportUserActivation(false);
const { save } = bind();
const frame = await mount([]);
// The page can call focus() on itself; it cannot give itself user activation.
frame.focus();
expect(document.activeElement).toBe(frame);
pass(frame, SAVE);
await flush();
expect(save).not.toHaveBeenCalled();
});
it("run a safe action right after the user pressed a key in the frame", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(true);
reportUserActivation(true);
const { save } = bind();
const frame = await mount([]);
frame.focus();
pass(frame, SAVE);
await flush();
expect(save).toHaveBeenCalledOnce();
});
it("do not run any bound action for a plugin whose ide.commands is declared but not granted", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(true);
const { run } = bind();
const frame = await mount([{ id: "ide.commands", granted: false }]);
frame.focus();
pass(frame, RUN);
await flush();
expect(run).not.toHaveBeenCalled();
});
it("reports iframe focus to the host only while the frame actually holds window focus", async () => {
const hasFocus = vi.spyOn(document, "hasFocus").mockReturnValue(true);
setPluginsBackend({ ...createMemoryPluginsBackend([]), webPageUrl: async () => "about:blank" });
usePlugins.setState({
plugins: [{ id: "k8s.view", hasCode: false, enabled: true, capabilities: [] } as unknown as PluginInfo],
});
const calls: Array<[string | null, boolean]> = [];
const disposeScope = installFocusScopeTracking();
const disposeFocus = installFocusTracking((id, editor) => calls.push([id, editor]));
try {
const view = render(
<section data-toolwindow="plugin.k8s.view" data-focus-scope="">
<WebView pluginId="k8s.view" view="v" label="Kubernetes" />
</section>,
);
const frame = await waitFor(() => {
const found = view.container.querySelector("iframe");
expect(found).not.toBeNull();
return found!;
});
const scope = view.container.querySelector('[data-toolwindow="plugin.k8s.view"]')!;
// Spoofed focus message while another element has focus is ignored.
window.dispatchEvent(
new MessageEvent("message", { data: { tag: FRAME_TAG, focus: true }, source: frame.contentWindow }),
);
expect(calls).toEqual([]);
expect(scope).not.toHaveAttribute("data-focus-active");
// Ignored when the window itself does not have focus.
frame.focus();
calls.length = 0;
hasFocus.mockReturnValue(false);
window.dispatchEvent(
new MessageEvent("message", { data: { tag: FRAME_TAG, focus: true }, source: frame.contentWindow }),
);
expect(calls).toEqual([]);
// Accepted once the frame is activeElement and the window has focus.
hasFocus.mockReturnValue(true);
window.dispatchEvent(
new MessageEvent("message", { data: { tag: FRAME_TAG, focus: true }, source: frame.contentWindow }),
);
expect(calls).toEqual([["plugin.k8s.view", false]]);
expect(scope).toHaveAttribute("data-focus-active", "");
} finally {
disposeFocus();
disposeScope();
}
});
});