Skip to content
Draft
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
4 changes: 4 additions & 0 deletions packages/extension/src/__tests__/service_worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ describe("Service Worker Migration", () => {

vi.doMock("@/services/storage", () => ({
Storage: {
addListener: vi.fn(),
get: vi.fn(),
},
SESSION_STORAGE_KEY: {},
Expand Down Expand Up @@ -619,6 +620,7 @@ describe("Popup Auto-Close Delay", () => {
const mockStorageSet = vi.fn().mockResolvedValue(undefined)
vi.doMock("@/services/storage", () => ({
Storage: {
addListener: vi.fn(),
set: mockStorageSet,
},
SESSION_STORAGE_KEY: {},
Expand Down Expand Up @@ -681,6 +683,7 @@ describe("Popup Auto-Close Delay", () => {
const mockStorageSet = vi.fn().mockResolvedValue(undefined)
vi.doMock("@/services/storage", () => ({
Storage: {
addListener: vi.fn(),
set: mockStorageSet,
},
SESSION_STORAGE_KEY: {},
Expand Down Expand Up @@ -746,6 +749,7 @@ describe("Popup Auto-Close Delay", () => {
const mockStorageSet = vi.fn().mockResolvedValue(undefined)
vi.doMock("@/services/storage", () => ({
Storage: {
addListener: vi.fn(),
set: mockStorageSet,
},
SESSION_STORAGE_KEY: {},
Expand Down
52 changes: 42 additions & 10 deletions packages/extension/src/action/__tests__/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import { enhancedSettings } from "@/services/settings/enhancedSettings"
import { incrementCommandExecutionCount } from "@/services/commandMetrics"
import { registerSidePanelHostTab as _registerSidePanelHostTab } from "@/services/pageAction/serviceWorker-sidePanel"
import { isSidePanelSupported } from "@/services/sidePanelSupport"
import {
preOpenSidePanel,
__test as sidePanelPreOpenTest,
} from "@/services/sidePanelPreOpen"

// Mock dependencies
vi.mock("@/services/serviceWorkerData", () => ({
Expand Down Expand Up @@ -48,6 +52,7 @@ vi.mock("@/services/settings/enhancedSettings", () => ({

vi.mock("@/services/sidePanelSupport", () => ({
isSidePanelSupported: vi.fn(() => true),
getSidePanelEvent: vi.fn(() => undefined),
}))

vi.mock("@/services/commandMetrics", () => ({
Expand All @@ -58,6 +63,7 @@ describe("helper", () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(ServiceWorkerData.ready).mockResolvedValue(undefined)
sidePanelPreOpenTest.resetPreOpenedSidePanels()
})

describe("navigateSidePanel", () => {
Expand Down Expand Up @@ -297,7 +303,7 @@ describe("helper", () => {

await new Promise((resolve) => setTimeout(resolve, 10))

expect(_openSidePanel).toHaveBeenCalledWith({ ...param, tabId })
expect(_openSidePanel).toHaveBeenCalledWith({ ...param, tabId }, false)
})

it("OSP-02: Should fall back to serviceWorkerData.activeTabId when sender.tab.id is null", async () => {
Expand All @@ -323,10 +329,10 @@ describe("helper", () => {

await new Promise((resolve) => setTimeout(resolve, 10))

expect(_openSidePanel).toHaveBeenCalledWith({
...param,
tabId: activeTabId,
})
expect(_openSidePanel).toHaveBeenCalledWith(
{ ...param, tabId: activeTabId },
false,
)
expect(response).toHaveBeenCalledWith(true)
})

Expand Down Expand Up @@ -422,7 +428,10 @@ describe("helper", () => {
openSidePanel(param, sender, response)

// Called synchronously to keep the user gesture
expect(_openSidePanel).toHaveBeenCalledWith({ ...param, tabId: 123 })
expect(_openSidePanel).toHaveBeenCalledWith(
{ ...param, tabId: 123 },
false,
)
expect(ServiceWorkerData.ready).not.toHaveBeenCalled()
})

Expand Down Expand Up @@ -461,10 +470,10 @@ describe("helper", () => {
await vi.waitFor(() => {
expect(response).toHaveBeenCalledWith(true)
})
expect(_openSidePanel).toHaveBeenCalledWith({
...param,
tabId: activeTabId,
})
expect(_openSidePanel).toHaveBeenCalledWith(
{ ...param, tabId: activeTabId },
false,
)
})

it("OSP-08: Should respond false when ServiceWorkerData fails to load", async () => {
Expand Down Expand Up @@ -512,6 +521,29 @@ describe("helper", () => {

consoleDebugSpy.mockRestore()
})

it("OSP-10: Should only set the page when the side panel was pre-opened", async () => {
const tabId = 123
const param = { url: "https://example.com", isLinkCommand: false }
const sender = { tab: { id: tabId } } as chrome.runtime.MessageSender
const response = vi.fn()

vi.mocked(_openSidePanel).mockResolvedValue({ tabId } as any)
vi.mocked(ServiceWorkerData.update).mockResolvedValue(true as any)

preOpenSidePanel(tabId)
openSidePanel(param, sender, response)

await vi.waitFor(() => {
expect(response).toHaveBeenCalledWith(true)
})
expect(_openSidePanel).toHaveBeenCalledWith({ ...param, tabId }, true)

// The pre-opened state is used only once.
vi.mocked(_openSidePanel).mockClear()
openSidePanel(param, sender, vi.fn())
expect(_openSidePanel).toHaveBeenCalledWith({ ...param, tabId }, false)
})
})

describe("closeSidePanel", () => {
Expand Down
15 changes: 11 additions & 4 deletions packages/extension/src/action/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { enhancedSettings } from "@/services/settings/enhancedSettings"
import { Ipc, TabCommand, NavigateSidePanelProps } from "@/services/ipc"
import { ServiceWorkerData } from "@/services/serviceWorkerData"
import { isSidePanelSupported } from "@/services/sidePanelSupport"
import { consumePreOpenedSidePanel } from "@/services/sidePanelPreOpen"
import {
SIDE_PANEL_RELAY_PATH,
isRelayableUrl,
Expand Down Expand Up @@ -145,10 +146,16 @@ export const openSidePanel = (
}

const open = (tabId: number) => {
_openSidePanel({
...param,
tabId,
})
// The side panel may have been opened by the service worker before the
// command ran, when the command started from the browser UI.
const preOpened = consumePreOpenedSidePanel(tabId)
_openSidePanel(
{
...param,
tabId,
},
preOpened,
)
.then(() => {
incrementCommandExecutionCount(tabId)
})
Expand Down
6 changes: 6 additions & 0 deletions packages/extension/src/hooks/useContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ export function useContextMenu() {
useEffect(() => {
return Ipc.addListener(TabCommand.executeAction, ((
param: executeActionProps,
_sender,
response,
) => {
setAction(param)
// Tell the sender that the command is accepted. Otherwise it cannot
// tell this from a tab without the content script, which also
// resolves to undefined.
response(true)
return false
}) as IpcCallback)
}, [])
Expand Down
2 changes: 2 additions & 0 deletions packages/extension/src/service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ if (isDebug) {
// syncCommandIdObj catches its own errors, so this promise never rejects.
const commandIdObjReady = ContextMenu.syncCommandIdObj()
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
// Must run before any await to keep the user gesture.
ContextMenu.preOpenSidePanel(info, tab)
await commandIdObjReady
await ContextMenu.onClicked(info, tab)
})
Expand Down
24 changes: 24 additions & 0 deletions packages/extension/src/services/__tests__/chrome.sidePanel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ describe("chrome.ts side panel functions", () => {
})
})

it("CSP-OP-04: 正常系: 事前オープン済みなら setOptions だけを呼び出す", async () => {
const result = await openSidePanel(
{ url: "https://example.com", tabId: 1 },
true,
)

expect(sidePanel.setOptions).toHaveBeenCalledWith({
tabId: 1,
path: buildRelayPath("https://example.com", 1),
enabled: true,
})
expect(sidePanel.open).not.toHaveBeenCalled()
expect(result).toEqual({ tabId: 1 })
})

it("CSP-OP-02: 異常系: API 非対応時は reject し API を呼び出さない", async () => {
setSidePanel(undefined)

Expand All @@ -77,6 +92,15 @@ describe("chrome.ts side panel functions", () => {
})

describe("closeSidePanel", () => {
it("CSP-CL-04: 正常系: アニメーション後に shouldDisable が false なら無効化しない", async () => {
const promise = closeSidePanel(1, () => false)
await vi.advanceTimersByTimeAsync(1000)
await promise

expect(sidePanel.close).toHaveBeenCalledWith({ tabId: 1 })
expect(sidePanel.setOptions).not.toHaveBeenCalled()
})

it("CSP-CL-01: 正常系: close 後にアニメーションを待って無効化する", async () => {
const promise = closeSidePanel(1)
await vi.advanceTimersByTimeAsync(0)
Expand Down
70 changes: 69 additions & 1 deletion packages/extension/src/services/__tests__/contextMenus.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, it, expect, vi, beforeEach } from "vitest"
import { enhancedSettings } from "@/services/settings/enhancedSettings"
import { ContextMenu } from "../contextMenus"
import { ContextMenu, __test } from "../contextMenus"

const { toMenuId } = __test
import { OPEN_MODE, OPTION_FOLDER, ROOT_FOLDER } from "@/const"
import type { Command, CommandFolder } from "@/types"

Expand Down Expand Up @@ -630,6 +632,72 @@ describe("Context Menu Multi-level Hierarchy", () => {
)
})

it("CM-17: should prefix the menu ID of a side panel command", () => {
const sidePanelCommand = {
...createCommand("cmd-sp", "Side panel", "folder1"),
openMode: OPEN_MODE.SIDE_PANEL,
}
const popupCommand = createCommand("cmd-popup", "Popup", "folder1")

expect(toMenuId(sidePanelCommand)).toBe("sidePanel:cmd-sp")
expect(toMenuId(popupCommand)).toBe("cmd-popup")
})

it("CM-18: should pre-open the side panel only for a side panel menu ID", () => {
const original = global.chrome.sidePanel
const open = vi.fn().mockResolvedValue(undefined)
global.chrome.sidePanel = {
open,
setOptions: vi.fn().mockResolvedValue(undefined),
} as any

// Works without commandIdObj, which may not be restored yet.
ContextMenu.commandIdObj = {}
ContextMenu.preOpenSidePanel(
{ menuItemId: "cmd-popup" } as any,
{ id: 123 } as any,
)
expect(open).not.toHaveBeenCalled()

ContextMenu.preOpenSidePanel(
{ menuItemId: "sidePanel:cmd-sp" } as any,
{ id: 123 } as any,
)
expect(open).toHaveBeenCalledWith({ tabId: 123 })

global.chrome.sidePanel = original
})

it("CM-19: should close the pre-opened side panel when the tab does not accept the command", async () => {
const original = global.chrome.sidePanel
const close = vi.fn().mockResolvedValue(undefined)
const setOptions = vi.fn().mockResolvedValue(undefined)
global.chrome.sidePanel = {
open: vi.fn().mockResolvedValue(undefined),
close,
setOptions,
} as any
// The tab without the content script resolves to undefined.
global.chrome.tabs = {
sendMessage: vi.fn().mockResolvedValue(undefined),
} as any
const command = {
...createCommand("cmd-sp", "Side panel", "folder1"),
openMode: OPEN_MODE.SIDE_PANEL,
}
ContextMenu.commandIdObj = { "sidePanel:cmd-sp": command }
const info = { menuItemId: "sidePanel:cmd-sp" } as any
const tab = { id: 123 } as any

ContextMenu.preOpenSidePanel(info, tab)
await ContextMenu.onClicked(info, tab)

await vi.waitFor(() => {
expect(close).toHaveBeenCalledWith({ tabId: 123 })
})
global.chrome.sidePanel = original
})

it("CM-16: should not send executeAction when command is not found", async () => {
const mockSendMessage = vi.fn().mockResolvedValue("success")
global.chrome.tabs = {
Expand Down
Loading