Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/js/popup/activate-tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// bring the tab's window forward and make the tab active. both calls are
// made synchronously, before anything is awaited, because the toolbar menu
// is an action popup: focusing a browser window takes focus away from it,
// and Chrome then closes it and destroys its page. a tabs.update() chained
// after windows.update() resolved would never be sent, so the menu would
// just close without switching tabs (issues #170 and #173). once a call
// has been dispatched, the browser carries it out even if the page is gone.
//
// the window is still focused first, since activating the window can
// sometimes put keyboard focus on the very first tab button on macOS 10.14
// (could never repro on 10.12), and focusing the tab afterwards fixes that.
export function activateTab(
tab,
options)
{
const windowFocused = chrome.windows.update(tab.windowId, { focused: true });
const tabActivated = chrome.tabs.update(tab.id, { active: true, ...options });

return Promise.all([windowFocused, tabActivated])
.then(([, activatedTab]) => activatedTab);
}
11 changes: 5 additions & 6 deletions src/js/popup/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import MessageItem from "./message-item";
import Banner from "./banner";
import OptionsButton from "./options-button";
import { connectPopupWindow } from "./popup-window";
import { activateTab } from "./activate-tab";
import scoreItems from "./score/score-items";
import initTabs from "./data/init-tabs";
import getBookmarks from "./data/get-bookmarks";
Expand Down Expand Up @@ -797,12 +798,10 @@ export default class App extends React.Component {
// focusing the tab's window blurs the popup, which closes it
this.pendingCloseReason = "select";

// bring the tab's window forward *before* focusing the tab, since
// activating the window can sometimes put keyboard focus on the
// very first tab button on macOS 10.14 (could never repro on
// 10.12). then focus the tab, which should fix any focus issues.
return chrome.windows.update(tab.windowId, { focused: true })
.then(() => chrome.tabs.update(tab.id, { active: true, ...options }))
// focus the window and the tab in one go, since in the toolbar
// menu, focusing the window closes the menu before anything
// chained after it can run. see activateTab().
return activateTab(tab, options)
.then((focusedTab) => {
this.executeAddTab();

Expand Down
46 changes: 46 additions & 0 deletions test/unit/activate-tab.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { activateTab } from "@/popup/activate-tab";

// the toolbar menu is an action popup, which Chrome closes, destroying its
// page, as soon as focusing a browser window takes focus away from it. any
// chrome call queued behind an await of windows.update() never gets sent,
// so selecting a tab just closed the menu (issues #170 and #173). both
// calls have to be dispatched synchronously, before the first await.
describe("activateTab", () => {
let windowsUpdate;
let tabsUpdate;

beforeEach(() => {
// windows.update() never settles, as if the page died mid-await
windowsUpdate = vi.spyOn(chrome.windows, "update")
.mockImplementation(() => new Promise(() => {}));
tabsUpdate = vi.spyOn(chrome.tabs, "update")
.mockImplementation((id, props) => Promise.resolve({ id, ...props }));
});

it("dispatches both the window focus and tab activation synchronously", () => {
activateTab({ id: 7, windowId: 3 });

expect(windowsUpdate).toHaveBeenCalledWith(3, { focused: true });
expect(tabsUpdate).toHaveBeenCalledWith(7, { active: true });
});

it("focuses the window before activating the tab", () => {
activateTab({ id: 7, windowId: 3 });

expect(windowsUpdate.mock.invocationCallOrder[0])
.toBeLessThan(tabsUpdate.mock.invocationCallOrder[0]);
});

it("passes extra options, like an unsuspend URL, to tabs.update()", () => {
activateTab({ id: 7, windowId: 3 }, { url: "https://example.com/" });

expect(tabsUpdate).toHaveBeenCalledWith(7, { active: true, url: "https://example.com/" });
});

it("resolves to the updated tab", async () => {
windowsUpdate.mockResolvedValue({ id: 3 });

await expect(activateTab({ id: 7, windowId: 3 })).resolves.toEqual({ id: 7, active: true });
});
});