diff --git a/e2e/toolbarCustomization.spec.ts b/e2e/toolbarCustomization.spec.ts index 725f497bfc..748b54a8f6 100644 --- a/e2e/toolbarCustomization.spec.ts +++ b/e2e/toolbarCustomization.spec.ts @@ -1,4 +1,4 @@ -import { expect, test } from "./fixtures"; +import { expect, openFromTree, test } from "./fixtures"; test("Customize Main Toolbar keeps layout actions above the groups and stacks them in a narrow window", async ({ workbench: page, @@ -136,3 +136,58 @@ test("the project colour gradient follows the Project widget", async ({ workbenc await expect(icon).toHaveCount(0); await expect.poll(anchor).toBe(""); }); + +test("main toolbar editor actions keep editor focus and run Collapse All / Expand All", async ({ workbench: page }) => { + const editor = await openFromTree(page, "App.tsx"); + const content = editor.locator(".cm-content"); + await content.click(); + await page.keyboard.press("Meta+a"); + await page.keyboard.insertText( + [ + "export function formatOrder(total: number) {", + ' const currency = "USD";', + " return currency + total.toFixed(2);", + "}", + ].join("\n"), + ); + await expect(content).toBeFocused(); + + await page.evaluate(async () => { + const path = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ path); + const settings = useProject.getState().settings; + useProject.setState({ + settings: { + ...settings, + appearance: { + ...settings?.appearance, + mainToolbar: { + left: ["project"], + center: ["action:code.foldAll", "action:code.unfoldAll", "action:editor.nextTab"], + right: ["action:navigate.searchEverywhere"], + hidden: [], + }, + }, + }, + }); + }); + + const bar = page.locator("header").first(); + await bar.getByRole("button", { name: "Collapse All", exact: true }).click({ delay: 80 }); + await expect(editor.locator(".cm-foldPlaceholder")).toHaveCount(1); + await expect(content).toBeFocused(); + + await bar.getByRole("button", { name: "Expand All", exact: true }).click({ delay: 80 }); + await expect(editor.locator(".cm-foldPlaceholder")).toHaveCount(0); + await expect(content).toBeFocused(); + + // With focus in a tool window, a toolbar click still leaves it so `!toolWindowFocus` actions run. + await openFromTree(page, "README.md"); + const activeTab = page.locator("[role=tab][aria-selected=true]"); + await expect(activeTab).toContainText("README.md"); + const projectTree = page.getByRole("tree", { name: "Project" }); + await projectTree.click(); + await expect(projectTree).toBeFocused(); + await bar.getByRole("button", { name: "Select Next Tab", exact: true }).click({ delay: 80 }); + await expect(activeTab).toContainText("App.tsx"); +}); diff --git a/src/workbench/ContributedToolbar.test.tsx b/src/workbench/ContributedToolbar.test.tsx index 9a7fe97858..795a36a6c3 100644 --- a/src/workbench/ContributedToolbar.test.tsx +++ b/src/workbench/ContributedToolbar.test.tsx @@ -1,5 +1,6 @@ import { afterEach, expect, it, vi } from "vitest"; import { render, screen, fireEvent } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; import { ContributedToolbar } from "./ContributedToolbar"; import { registerMenuSlotItem } from "./contributions"; import { registerAction } from "@/platform/actions"; @@ -31,3 +32,27 @@ it("renders toolbar slots with command icons and honors when clauses", () => { render(); expect(screen.queryByRole("button")).toBeNull(); }); +it("takes focus on a click from a tool window only", async () => { + const focused: (Element | null)[] = []; + offs.push( + registerAction({ id: "test.focus", title: "Act", run: () => void focused.push(document.activeElement) }), + registerMenuSlotItem("editor.title", { action: "test.focus", label: "Act", icon: "run" }), + ); + render( + <> +