diff options
-rw-r--r-- | src/background/di.ts | 2 | ||||
-rw-r--r-- | src/background/operators/impls/ResetZoomOperator.ts | 2 | ||||
-rw-r--r-- | src/background/operators/impls/ZoomInOperator.ts | 2 | ||||
-rw-r--r-- | src/background/operators/impls/ZoomOperatorFactoryChain.ts | 2 | ||||
-rw-r--r-- | src/background/operators/impls/ZoomOutOperator.ts | 2 | ||||
-rw-r--r-- | src/background/presenters/ZoomPresenter.ts (renamed from src/background/usecases/ZoomPresenter.ts) | 0 | ||||
-rw-r--r-- | src/background/usecases/AddonEnabledUseCase.ts | 2 | ||||
-rw-r--r-- | src/background/usecases/CommandUseCase.ts | 2 | ||||
-rw-r--r-- | src/background/usecases/ConsoleUseCase.ts | 65 | ||||
-rw-r--r-- | src/background/usecases/FindUseCase.ts | 17 | ||||
-rw-r--r-- | src/background/usecases/NavigateUseCase.ts | 58 | ||||
-rw-r--r-- | src/background/usecases/TabSelectUseCase.ts | 51 | ||||
-rw-r--r-- | src/background/usecases/TabUseCase.ts | 105 | ||||
-rw-r--r-- | src/background/usecases/ZoomUseCase.ts | 51 | ||||
-rw-r--r-- | test/background/mock/MockZoomPresenter.ts | 2 | ||||
-rw-r--r-- | test/background/usecases/NavigateUseCase.test.ts | 185 |
16 files changed, 10 insertions, 538 deletions
diff --git a/src/background/di.ts b/src/background/di.ts index 9ec3c74..efdf363 100644 --- a/src/background/di.ts +++ b/src/background/di.ts @@ -16,7 +16,7 @@ import { NavigateClientImpl } from "./clients/NavigateClient"; import { ConsoleClientImpl } from "./infrastructures/ConsoleClient"; import { BrowserSettingRepositoryImpl } from "./repositories/BrowserSettingRepository"; import { RepeatRepositoryImpl } from "./repositories/RepeatRepository"; -import { ZoomPresenterImpl } from "./usecases/ZoomPresenter"; +import { ZoomPresenterImpl } from "./presenters/ZoomPresenter"; import { WindowPresenterImpl } from "./presenters/WindowPresenter"; container.register("LocalSettingRepository", { diff --git a/src/background/operators/impls/ResetZoomOperator.ts b/src/background/operators/impls/ResetZoomOperator.ts index 48db1d5..96af733 100644 --- a/src/background/operators/impls/ResetZoomOperator.ts +++ b/src/background/operators/impls/ResetZoomOperator.ts @@ -1,5 +1,5 @@ import Operator from "../Operator"; -import ZoomPresenter from "../../usecases/ZoomPresenter"; +import ZoomPresenter from "../../presenters/ZoomPresenter"; export default class ResetZoomOperator implements Operator { constructor(private readonly zoomPresenter: ZoomPresenter) {} diff --git a/src/background/operators/impls/ZoomInOperator.ts b/src/background/operators/impls/ZoomInOperator.ts index 9ed1861..e900f0d 100644 --- a/src/background/operators/impls/ZoomInOperator.ts +++ b/src/background/operators/impls/ZoomInOperator.ts @@ -1,5 +1,5 @@ import Operator from "../Operator"; -import ZoomPresenter from "../../usecases/ZoomPresenter"; +import ZoomPresenter from "../../presenters/ZoomPresenter"; export default class ZoomInOperator implements Operator { constructor(private readonly zoomPresenter: ZoomPresenter) {} diff --git a/src/background/operators/impls/ZoomOperatorFactoryChain.ts b/src/background/operators/impls/ZoomOperatorFactoryChain.ts index ebcf2a5..bf930a7 100644 --- a/src/background/operators/impls/ZoomOperatorFactoryChain.ts +++ b/src/background/operators/impls/ZoomOperatorFactoryChain.ts @@ -4,7 +4,7 @@ import OperatorFactoryChain from "../OperatorFactoryChain"; import ZoomInOperator from "./ZoomInOperator"; import ZoomOutOperator from "./ZoomOutOperator"; import ResetZoomOperator from "./ResetZoomOperator"; -import ZoomPresenter from "../../usecases/ZoomPresenter"; +import ZoomPresenter from "../../presenters/ZoomPresenter"; import * as operations from "../../../shared/operations"; @injectable() diff --git a/src/background/operators/impls/ZoomOutOperator.ts b/src/background/operators/impls/ZoomOutOperator.ts index 35f5d3d..0c0389e 100644 --- a/src/background/operators/impls/ZoomOutOperator.ts +++ b/src/background/operators/impls/ZoomOutOperator.ts @@ -1,5 +1,5 @@ import Operator from "../Operator"; -import ZoomPresenter from "../../usecases/ZoomPresenter"; +import ZoomPresenter from "../../presenters/ZoomPresenter"; export default class ZoomOutOperator implements Operator { constructor(private readonly zoomPresenter: ZoomPresenter) {} diff --git a/src/background/usecases/ZoomPresenter.ts b/src/background/presenters/ZoomPresenter.ts index 5a3c64d..5a3c64d 100644 --- a/src/background/usecases/ZoomPresenter.ts +++ b/src/background/presenters/ZoomPresenter.ts diff --git a/src/background/usecases/AddonEnabledUseCase.ts b/src/background/usecases/AddonEnabledUseCase.ts index f563ab0..f9bafde 100644 --- a/src/background/usecases/AddonEnabledUseCase.ts +++ b/src/background/usecases/AddonEnabledUseCase.ts @@ -22,7 +22,7 @@ export default class AddonEnabledUseCase { return this.indicatorPresentor.indicate(enabled); } - onIndicatorClick(tabId: number): Promise<void> { + private onIndicatorClick(tabId: number): Promise<void> { return this.contentMessageClient.toggleAddonEnabled(tabId); } diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts index cadcad1..18ddd4d 100644 --- a/src/background/usecases/CommandUseCase.ts +++ b/src/background/usecases/CommandUseCase.ts @@ -12,7 +12,7 @@ import ContentMessageClient from "../infrastructures/ContentMessageClient"; import RepeatUseCase from "../usecases/RepeatUseCase"; @injectable() -export default class CommandIndicator { +export default class CommandUseCase { constructor( @inject("TabPresenter") private readonly tabPresenter: TabPresenter, diff --git a/src/background/usecases/ConsoleUseCase.ts b/src/background/usecases/ConsoleUseCase.ts deleted file mode 100644 index 4adba65..0000000 --- a/src/background/usecases/ConsoleUseCase.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; -import ConsoleClient from "../infrastructures/ConsoleClient"; - -@injectable() -export default class ConsoleUseCase { - constructor( - @inject("TabPresenter") - private readonly tabPresenter: TabPresenter, - @inject("ConsoleClient") - private readonly consoleClient: ConsoleClient - ) {} - - async showCommand(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.consoleClient.showCommand(tab.id as number, ""); - } - - async showOpenCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "open "; - if (alter) { - command += tab.url || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showTabopenCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "tabopen "; - if (alter) { - command += tab.url || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showWinopenCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "winopen "; - if (alter) { - command += tab.url || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showBufferCommand(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const command = "buffer "; - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showAddbookmarkCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "addbookmark "; - if (alter) { - command += tab.title || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async hideConsole(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.consoleClient.hide(tab.id as number); - } -} diff --git a/src/background/usecases/FindUseCase.ts b/src/background/usecases/FindUseCase.ts index 404fb48..ce96e03 100644 --- a/src/background/usecases/FindUseCase.ts +++ b/src/background/usecases/FindUseCase.ts @@ -1,17 +1,9 @@ -import { inject, injectable } from "tsyringe"; +import { injectable } from "tsyringe"; import FindRepository from "../repositories/FindRepository"; -import TabPresenter from "../presenters/TabPresenter"; -import ConsoleClient from "../infrastructures/ConsoleClient"; @injectable() export default class FindUseCase { - constructor( - @inject("TabPresenter") - private readonly tabPresenter: TabPresenter, - private readonly findRepository: FindRepository, - @inject("ConsoleClient") - private readonly consoleClient: ConsoleClient - ) {} + constructor(private readonly findRepository: FindRepository) {} getKeyword(): Promise<string> { return this.findRepository.getKeyword(); @@ -20,9 +12,4 @@ export default class FindUseCase { setKeyword(keyword: string): Promise<any> { return this.findRepository.setKeyword(keyword); } - - async findStart(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.consoleClient.showFind(tab.id as number); - } } diff --git a/src/background/usecases/NavigateUseCase.ts b/src/background/usecases/NavigateUseCase.ts deleted file mode 100644 index 29e629a..0000000 --- a/src/background/usecases/NavigateUseCase.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import NavigateClient from "../clients/NavigateClient"; -import TabPresenter from "../presenters/TabPresenter"; - -@injectable() -export default class NavigateUseCase { - constructor( - @inject("TabPresenter") - private readonly tabPresenter: TabPresenter, - @inject("NavigateClient") - private readonly navigateClient: NavigateClient - ) {} - - async openHistoryNext(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.historyNext(tab.id!); - } - - async openHistoryPrev(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.historyPrev(tab.id!); - } - - async openLinkNext(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.linkNext(tab.id!); - } - - async openLinkPrev(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.linkPrev(tab.id!); - } - - async openParent(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - const url = new URL(tab.url!); - if (url.hash.length > 0) { - url.hash = ""; - } else if (url.search.length > 0) { - url.search = ""; - } else { - const basenamePattern = /\/[^/]+$/; - const lastDirPattern = /\/[^/]+\/$/; - if (basenamePattern.test(url.pathname)) { - url.pathname = url.pathname.replace(basenamePattern, "/"); - } else if (lastDirPattern.test(url.pathname)) { - url.pathname = url.pathname.replace(lastDirPattern, "/"); - } - } - await this.tabPresenter.open(url.href); - } - - async openRoot(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - const url = new URL(tab.url!); - await this.tabPresenter.open(url.origin); - } -} diff --git a/src/background/usecases/TabSelectUseCase.ts b/src/background/usecases/TabSelectUseCase.ts deleted file mode 100644 index 663ceb8..0000000 --- a/src/background/usecases/TabSelectUseCase.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; - -@injectable() -export default class TabSelectUseCase { - constructor(@inject("TabPresenter") private tabPresenter: TabPresenter) {} - - async selectPrev(count: number): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - if (tabs.length < 2) { - return; - } - const tab = tabs.find((t) => t.active); - if (!tab) { - return; - } - const select = (tab.index - count + tabs.length) % tabs.length; - return this.tabPresenter.select(tabs[select].id as number); - } - - async selectNext(count: number): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - if (tabs.length < 2) { - return; - } - const tab = tabs.find((t) => t.active); - if (!tab) { - return; - } - const select = (tab.index + count) % tabs.length; - return this.tabPresenter.select(tabs[select].id as number); - } - - async selectFirst(): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - return this.tabPresenter.select(tabs[0].id as number); - } - - async selectLast(): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - return this.tabPresenter.select(tabs[tabs.length - 1].id as number); - } - - async selectPrevSelected(): Promise<any> { - const tabId = await this.tabPresenter.getLastSelectedId(); - if (tabId === null || typeof tabId === "undefined") { - return Promise.resolve(); - } - return this.tabPresenter.select(tabId); - } -} diff --git a/src/background/usecases/TabUseCase.ts b/src/background/usecases/TabUseCase.ts deleted file mode 100644 index be01032..0000000 --- a/src/background/usecases/TabUseCase.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; -import WindowPresenter from "../presenters/WindowPresenter"; -import BrowserSettingRepository from "../repositories/BrowserSettingRepository"; - -@injectable() -export default class TabUseCase { - constructor( - @inject("TabPresenter") - private readonly tabPresenter: TabPresenter, - @inject("WindowPresenter") - private readonly windowPresenter: WindowPresenter, - @inject("BrowserSettingRepository") - private readonly browserSettingRepository: BrowserSettingRepository - ) {} - - async close(force: boolean, selectLeft = false): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - if (!force && tab.pinned) { - return Promise.resolve(); - } - if (selectLeft && tab.index > 0) { - const tabs = await this.tabPresenter.getAll(); - await this.tabPresenter.select(tabs[tab.index - 1].id as number); - } - return this.tabPresenter.remove([tab.id as number]); - } - - async closeRight(): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - tabs.sort((t1, t2) => t1.index - t2.index); - const index = tabs.findIndex((t) => t.active); - if (index < 0) { - return; - } - for (let i = index + 1; i < tabs.length; ++i) { - const tab = tabs[i]; - if (!tab.pinned) { - this.tabPresenter.remove([tab.id as number]); - } - } - } - - reopen(): Promise<any> { - return this.tabPresenter.reopen(); - } - - async reload(cache: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.reload(tab.id as number, cache); - } - - async setPinned(pinned: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setPinned(tab.id as number, pinned); - } - - async togglePinned(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setPinned(tab.id as number, !tab.pinned); - } - - async duplicate(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.duplicate(tab.id as number); - } - - async openPageSource(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const url = "view-source:" + tab.url; - return this.tabPresenter.create(url); - } - - async openHome(newTab: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const urls = await this.browserSettingRepository.getHomepageUrls(); - if (urls.length === 1 && urls[0] === "about:home") { - // eslint-disable-next-line max-len - throw new Error( - "Cannot open Firefox Home (about:home) by WebExtensions, set your custom URLs" - ); - } - if (urls.length === 1 && !newTab) { - return this.tabPresenter.open(urls[0], tab.id); - } - for (const url of urls) { - this.tabPresenter.create(url); - } - } - - async openURL( - url: string, - newTab?: boolean, - newWindow?: boolean - ): Promise<void> { - if (newWindow) { - await this.windowPresenter.create(url); - } else if (newTab) { - await this.tabPresenter.create(url); - } else { - const tab = await this.tabPresenter.getCurrent(); - await this.tabPresenter.open(url, tab.id); - } - } -} diff --git a/src/background/usecases/ZoomUseCase.ts b/src/background/usecases/ZoomUseCase.ts deleted file mode 100644 index 173e4d7..0000000 --- a/src/background/usecases/ZoomUseCase.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; - -const ZOOM_SETTINGS: number[] = [ - 0.33, - 0.5, - 0.66, - 0.75, - 0.8, - 0.9, - 1.0, - 1.1, - 1.25, - 1.5, - 1.75, - 2.0, - 2.5, - 3.0, -]; - -@injectable() -export default class ZoomUseCase { - constructor(@inject("TabPresenter") private tabPresenter: TabPresenter) {} - - async zoomIn(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const tabId = tab.id as number; - const current = await this.tabPresenter.getZoom(tabId); - const factor = ZOOM_SETTINGS.find((f) => f > current); - if (factor) { - return this.tabPresenter.setZoom(tabId as number, factor); - } - } - - async zoomOut(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const tabId = tab.id as number; - const current = await this.tabPresenter.getZoom(tabId); - const factor = ZOOM_SETTINGS.slice(0) - .reverse() - .find((f) => f < current); - if (factor) { - return this.tabPresenter.setZoom(tabId as number, factor); - } - } - - async zoomNutoral(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setZoom(tab.id as number, 1); - } -} diff --git a/test/background/mock/MockZoomPresenter.ts b/test/background/mock/MockZoomPresenter.ts index a1ca100..53d1980 100644 --- a/test/background/mock/MockZoomPresenter.ts +++ b/test/background/mock/MockZoomPresenter.ts @@ -1,4 +1,4 @@ -import ZoomPresenter from "../../../src/background/usecases/ZoomPresenter"; +import ZoomPresenter from "../../../src/background/presenters/ZoomPresenter"; export default class MockZoomPresenter implements ZoomPresenter { resetZoom(): Promise<void> { diff --git a/test/background/usecases/NavigateUseCase.test.ts b/test/background/usecases/NavigateUseCase.test.ts deleted file mode 100644 index f1b94a7..0000000 --- a/test/background/usecases/NavigateUseCase.test.ts +++ /dev/null @@ -1,185 +0,0 @@ -import "reflect-metadata"; -import TabPresenter from "../../../src/background/presenters/TabPresenter"; -import NavigateUseCase from "../../../src/background/usecases/NavigateUseCase"; -import NavigateClient, { - NavigateClientImpl, -} from "../../../src/background/clients/NavigateClient"; -import * as sinon from "sinon"; - -class MockTabPresenter implements TabPresenter { - create(_url: string, _opts?: unknown): Promise<browser.tabs.Tab> { - throw new Error("not implemented"); - } - - duplicate(_id: number): Promise<browser.tabs.Tab> { - throw new Error("not implemented"); - } - - getAll(): Promise<browser.tabs.Tab[]> { - throw new Error("not implemented"); - } - - getByKeyword( - _keyword: string, - _excludePinned: boolean - ): Promise<browser.tabs.Tab[]> { - throw new Error("not implemented"); - } - - getCurrent(): Promise<browser.tabs.Tab> { - throw new Error("not implemented"); - } - - getLastSelectedId(): Promise<number | undefined> { - throw new Error("not implemented"); - } - - getZoom(_tabId: number): Promise<number> { - throw new Error("not implemented"); - } - - onSelected( - _listener: (arg: { tabId: number; windowId: number }) => void - ): void { - throw new Error("not implemented"); - } - - open(_url: string, _tabId?: number): Promise<browser.tabs.Tab> { - throw new Error("not implemented"); - } - - reload(_tabId: number, _cache: boolean): Promise<void> { - throw new Error("not implemented"); - } - - remove(_ids: number[]): Promise<void> { - throw new Error("not implemented"); - } - - reopen(): Promise<void> { - throw new Error("not implemented"); - } - - select(_tabId: number): Promise<void> { - throw new Error("not implemented"); - } - - setPinned(_tabId: number, _pinned: boolean): Promise<void> { - throw new Error("not implemented"); - } - - setZoom(_tabId: number, _factor: number): Promise<void> { - throw new Error("not implemented"); - } -} - -describe("NavigateUseCase", () => { - let sut: NavigateUseCase; - let tabPresenter: TabPresenter; - let navigateClient: NavigateClient; - - beforeEach(() => { - tabPresenter = new MockTabPresenter(); - navigateClient = new NavigateClientImpl(); - sut = new NavigateUseCase(tabPresenter, navigateClient); - }); - - const newTab = (url: string): browser.tabs.Tab => { - return { - index: 0, - title: "dummy title", - url: url, - active: true, - hidden: false, - highlighted: false, - incognito: false, - isArticle: false, - isInReaderMode: false, - lastAccessed: 1585446733000, - pinned: false, - selected: false, - windowId: 0, - }; - }; - - describe("#openParent()", async () => { - it("opens parent directory of file", async () => { - sinon - .stub(tabPresenter, "getCurrent") - .returns( - Promise.resolve(newTab("https://google.com/fruits/yellow/banana")) - ); - - const mock = sinon - .mock(tabPresenter) - .expects("open") - .withArgs("https://google.com/fruits/yellow/"); - - await sut.openParent(); - - mock.verify(); - }); - - it("opens parent directory of directory", async () => { - sinon - .stub(tabPresenter, "getCurrent") - .returns(Promise.resolve(newTab("https://google.com/fruits/yellow/"))); - - const mock = sinon - .mock(tabPresenter) - .expects("open") - .withArgs("https://google.com/fruits/"); - - await sut.openParent(); - - mock.verify(); - }); - - it("removes hash", async () => { - sinon - .stub(tabPresenter, "getCurrent") - .returns(Promise.resolve(newTab("https://google.com/#top"))); - - const mock = sinon - .mock(tabPresenter) - .expects("open") - .withArgs("https://google.com/"); - - await sut.openParent(); - - mock.verify(); - }); - - it("removes search query", async () => { - sinon - .stub(tabPresenter, "getCurrent") - .returns(Promise.resolve(newTab("https://google.com/search?q=apple"))); - - const mock = sinon - .mock(tabPresenter) - .expects("open") - .withArgs("https://google.com/search"); - - await sut.openParent(); - - mock.verify(); - }); - }); - - describe("#openRoot()", () => { - it("opens root direectory", async () => { - sinon - .stub(tabPresenter, "getCurrent") - .returns(Promise.resolve(newTab("https://google.com/seach?q=apple"))); - - const mock = sinon - .mock(tabPresenter) - .expects("open") - .withArgs("https://google.com"); - - await sut.openRoot(); - - mock.verify(); - }); - }); -}); |