aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-03-29 21:10:47 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2020-03-29 21:11:13 +0900
commit0340c82bc82738a63c8a374930cf39cbed5c7c8c (patch)
tree12e13959351bd8c726258adc24a83b9e579be74a
parent1368fa2fc0f22d1ec4763c77c2ae983cf5037e92 (diff)
Complete tab by an index and a flag
-rw-r--r--src/background/completion/TabCompletionUseCase.ts25
-rw-r--r--src/background/completion/TabRepository.ts2
-rw-r--r--src/background/completion/impl/TabRepositoryImpl.ts32
-rw-r--r--test/background/completion/TabCompletionUseCase.test.ts53
4 files changed, 94 insertions, 18 deletions
diff --git a/src/background/completion/TabCompletionUseCase.ts b/src/background/completion/TabCompletionUseCase.ts
index 7e6dce3..dec86e9 100644
--- a/src/background/completion/TabCompletionUseCase.ts
+++ b/src/background/completion/TabCompletionUseCase.ts
@@ -1,6 +1,6 @@
import { inject, injectable } from "tsyringe";
import TabItem from "./TabItem";
-import TabRepository from "./TabRepository";
+import TabRepository, { Tab } from "./TabRepository";
import TabPresenter from "../presenters/TabPresenter";
import TabFlag from "../../shared/TabFlag";
@@ -14,7 +14,28 @@ export default class TabCompletionUseCase {
async queryTabs(query: string, excludePinned: boolean): Promise<TabItem[]> {
const lastTabId = await this.tabPresenter.getLastSelectedId();
- const tabs = await this.tabRepository.queryTabs(query, excludePinned);
+ const allTabs = await this.tabRepository.getAllTabs(excludePinned);
+ const num = parseInt(query, 10);
+ let tabs: Tab[] = [];
+ if (!isNaN(num)) {
+ const tab = allTabs.find(t => t.index === num - 1);
+ if (tab) {
+ tabs = [tab];
+ }
+ } else if (query == '%') {
+ const tab = allTabs.find(t => t.active);
+ if (tab) {
+ tabs = [tab];
+ }
+ } else if (query == '#') {
+ const tab = allTabs.find(t => t.id === lastTabId);
+ if (tab) {
+ tabs = [tab];
+ }
+ } else {
+ tabs = await this.tabRepository.queryTabs(query, excludePinned);
+ }
+
return tabs.map(tab => {
let flag = TabFlag.None;
if (tab.active) {
diff --git a/src/background/completion/TabRepository.ts b/src/background/completion/TabRepository.ts
index 61fac3b..fe1b601 100644
--- a/src/background/completion/TabRepository.ts
+++ b/src/background/completion/TabRepository.ts
@@ -9,4 +9,6 @@ export type Tab = {
export default interface TabRepository {
queryTabs(query: string, excludePinned: boolean): Promise<Tab[]>;
+
+ getAllTabs(excludePinned: boolean): Promise<Tab[]>
}
diff --git a/src/background/completion/impl/TabRepositoryImpl.ts b/src/background/completion/impl/TabRepositoryImpl.ts
index 6692b27..adcaba7 100644
--- a/src/background/completion/impl/TabRepositoryImpl.ts
+++ b/src/background/completion/impl/TabRepositoryImpl.ts
@@ -15,13 +15,27 @@ export default class TabRepositoryImpl implements TabRepository {
})
.filter(item => item.id && item.title && item.url)
.slice(0, COMPLETION_ITEM_LIMIT)
- .map(item => ({
- id: item.id!!,
- url: item.url!!,
- active: item.active,
- title: item.title!!,
- faviconUrl: item.favIconUrl,
- index: item.index,
- }))
+ .map(TabRepositoryImpl.toEntity);
}
-} \ No newline at end of file
+
+ async getAllTabs(excludePinned: boolean): Promise<Tab[]> {
+ if (excludePinned) {
+ return (await browser.tabs.query({ currentWindow: true, pinned: true }))
+ .map(TabRepositoryImpl.toEntity)
+
+ }
+ return (await browser.tabs.query({ currentWindow: true }))
+ .map(TabRepositoryImpl.toEntity)
+ }
+
+ private static toEntity(tab: browser.tabs.Tab,): Tab {
+ return {
+ id: tab.id!!,
+ url: tab.url!!,
+ active: tab.active,
+ title: tab.title!!,
+ faviconUrl: tab.favIconUrl,
+ index: tab.index,
+ }
+ }
+}
diff --git a/test/background/completion/TabCompletionUseCase.test.ts b/test/background/completion/TabCompletionUseCase.test.ts
index 254bfe6..9df236d 100644
--- a/test/background/completion/TabCompletionUseCase.test.ts
+++ b/test/background/completion/TabCompletionUseCase.test.ts
@@ -11,6 +11,10 @@ class MockTabRepository implements TabRepositoryImpl {
async queryTabs(_query: string, _excludePinned: boolean): Promise<Tab[]> {
throw new Error("not implemented")
}
+
+ async getAllTabs(_excludePinned: boolean): Promise<Tab[]> {
+ throw new Error("not implemented")
+ }
}
class MockTabPresenter implements TabPresenter {
@@ -83,22 +87,57 @@ describe('TabCompletionUseCase', () => {
beforeEach(() => {
tabRepository = new MockTabRepository();
tabPresenter = new MockTabPresenter();
- sut = new TabCompletionUseCase(tabRepository, tabPresenter)
+ sut = new TabCompletionUseCase(tabRepository, tabPresenter);
+
+ sinon.stub(tabPresenter, 'getLastSelectedId').returns(Promise.resolve(12));
+ sinon.stub(tabRepository, 'getAllTabs').returns(Promise.resolve([
+ { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false },
+ { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true },
+ { id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false },
+ ]));
});
describe('#queryTabs', () => {
it("returns tab items", async () => {
- sinon.stub(tabRepository, 'queryTabs').returns(Promise.resolve([
- { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: true },
- { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: false },
+ sinon.stub(tabRepository, 'queryTabs').withArgs('', false).returns(Promise.resolve([
+ { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false },
+ { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true },
{ id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false },
+ ])).withArgs('oo', false).returns(Promise.resolve([
+ { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false },
+ { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true },
]));
- sinon.stub(tabPresenter, 'getLastSelectedId').returns(Promise.resolve(11));
- expect(await sut.queryTabs("", false)).to.deep.equal([
+ expect(await sut.queryTabs('', false)).to.deep.equal([
+ { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None },
+ { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab },
+ { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab },
+ ]);
+
+ expect(await sut.queryTabs('oo', false)).to.deep.equal([
+ { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None },
+ { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab },
+ ]);
+ });
+
+ it("returns a tab by the index", async () => {
+ expect(await sut.queryTabs('1', false)).to.deep.equal([
{ index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.CurrentTab },
+ ]);
+
+ expect(await sut.queryTabs('10', false)).to.be.empty;
+ expect(await sut.queryTabs('-1', false)).to.be.empty;
+ });
+
+ it("returns the current tab by % flag", async () => {
+ expect(await sut.queryTabs('%', false)).to.deep.equal([
{ index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.LastTab },
- { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.None },
+ ]);
+ });
+
+ it("returns the current tab by # flag", async () => {
+ expect(await sut.queryTabs('#', false)).to.deep.equal([
+ { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab },
]);
})
});