Merge pull request #592 from ueokande/repeat-last-operation
Add "repeat last operation" commandjh-changes
commit
cd584c8e24
28 changed files with 606 additions and 142 deletions
@ -0,0 +1,92 @@ |
|||||||
|
const express = require('express'); |
||||||
|
const lanthan = require('lanthan'); |
||||||
|
const path = require('path'); |
||||||
|
const assert = require('assert'); |
||||||
|
const eventually = require('./eventually'); |
||||||
|
|
||||||
|
const Key = lanthan.Key; |
||||||
|
|
||||||
|
const newApp = () => { |
||||||
|
let app = express(); |
||||||
|
app.get('/', (req, res) => { |
||||||
|
res.send('ok'); |
||||||
|
}); |
||||||
|
return app; |
||||||
|
}; |
||||||
|
|
||||||
|
describe("tab test", () => { |
||||||
|
|
||||||
|
const port = 12321; |
||||||
|
const url = `http://127.0.0.1:${port}/`; |
||||||
|
|
||||||
|
let http; |
||||||
|
let firefox; |
||||||
|
let session; |
||||||
|
let browser; |
||||||
|
let tabs; |
||||||
|
|
||||||
|
before(async() => { |
||||||
|
firefox = await lanthan.firefox(); |
||||||
|
await firefox.session.installAddonFromPath(path.join(__dirname, '..')); |
||||||
|
session = firefox.session; |
||||||
|
browser = firefox.browser; |
||||||
|
http = newApp().listen(port); |
||||||
|
|
||||||
|
await session.navigateTo(`${url}`); |
||||||
|
}); |
||||||
|
|
||||||
|
after(async() => { |
||||||
|
http.close(); |
||||||
|
if (firefox) { |
||||||
|
await firefox.close(); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
it('repeats last operation', async () => { |
||||||
|
let before = await browser.tabs.query({}); |
||||||
|
|
||||||
|
let body = await session.findElementByCSS('body'); |
||||||
|
await body.sendKeys(':'); |
||||||
|
|
||||||
|
await session.switchToFrame(0); |
||||||
|
let input = await session.findElementByCSS('input'); |
||||||
|
input.sendKeys(`tabopen ${url}newtab`, Key.Enter); |
||||||
|
|
||||||
|
await eventually(async() => { |
||||||
|
let current = await browser.tabs.query({ url: `*://*/newtab` }); |
||||||
|
assert.equal(current.length, 1); |
||||||
|
}); |
||||||
|
|
||||||
|
body = await session.findElementByCSS('body'); |
||||||
|
await body.sendKeys('.'); |
||||||
|
|
||||||
|
await eventually(async() => { |
||||||
|
let current = await browser.tabs.query({ url: `*://*/newtab` }); |
||||||
|
assert.equal(current.length, 2); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
it('repeats last operation', async () => { |
||||||
|
for (let i = 1; i < 5; ++i) { |
||||||
|
await browser.tabs.create({ url: `${url}#${i}` }); |
||||||
|
} |
||||||
|
let before = await browser.tabs.query({}); |
||||||
|
|
||||||
|
let body = await session.findElementByCSS('body'); |
||||||
|
await body.sendKeys('d'); |
||||||
|
|
||||||
|
await eventually(async() => { |
||||||
|
let current = await browser.tabs.query({}); |
||||||
|
assert.equal(current.length, before.length - 1); |
||||||
|
}); |
||||||
|
|
||||||
|
await browser.tabs.update(before[2].id, { active: true }); |
||||||
|
body = await session.findElementByCSS('body'); |
||||||
|
await body.sendKeys('.'); |
||||||
|
|
||||||
|
await eventually(async() => { |
||||||
|
let current = await browser.tabs.query({}); |
||||||
|
assert.equal(current.length, before.length - 2); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,29 @@ |
|||||||
|
import { injectable } from 'tsyringe'; |
||||||
|
import * as messages from '../../shared/messages'; |
||||||
|
|
||||||
|
@injectable() |
||||||
|
export default class NavigateClient { |
||||||
|
async historyNext(tabId: number): Promise<void> { |
||||||
|
await browser.tabs.sendMessage(tabId, { |
||||||
|
type: messages.NAVIGATE_HISTORY_NEXT, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
async historyPrev(tabId: number): Promise<void> { |
||||||
|
await browser.tabs.sendMessage(tabId, { |
||||||
|
type: messages.NAVIGATE_HISTORY_PREV, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
async linkNext(tabId: number): Promise<void> { |
||||||
|
await browser.tabs.sendMessage(tabId, { |
||||||
|
type: messages.NAVIGATE_LINK_NEXT, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
async linkPrev(tabId: number): Promise<void> { |
||||||
|
await browser.tabs.sendMessage(tabId, { |
||||||
|
type: messages.NAVIGATE_LINK_PREV, |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
import { injectable } from 'tsyringe'; |
||||||
|
import { Operation } from '../../shared/operations'; |
||||||
|
import MemoryStorage from '../infrastructures/MemoryStorage'; |
||||||
|
|
||||||
|
const REPEAT_KEY = 'repeat'; |
||||||
|
|
||||||
|
@injectable() |
||||||
|
export default class RepeatRepository { |
||||||
|
private cache: MemoryStorage; |
||||||
|
|
||||||
|
constructor() { |
||||||
|
this.cache = new MemoryStorage(); |
||||||
|
} |
||||||
|
|
||||||
|
getLastOperation(): Operation | undefined { |
||||||
|
return this.cache.get(REPEAT_KEY); |
||||||
|
} |
||||||
|
|
||||||
|
setLastOperation(op: Operation): void { |
||||||
|
this.cache.set(REPEAT_KEY, op); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
import { injectable } from 'tsyringe'; |
||||||
|
import NavigateClient from '../clients/NavigateClient'; |
||||||
|
import TabPresenter from '../presenters/TabPresenter'; |
||||||
|
|
||||||
|
@injectable() |
||||||
|
export default class NavigateUseCase { |
||||||
|
constructor( |
||||||
|
private tabPresenter: TabPresenter, |
||||||
|
private navigateClient: NavigateClient, |
||||||
|
) { |
||||||
|
} |
||||||
|
|
||||||
|
async openHistoryNext(): Promise<void> { |
||||||
|
let tab = await this.tabPresenter.getCurrent(); |
||||||
|
await this.navigateClient.historyNext(tab.id!!); |
||||||
|
} |
||||||
|
|
||||||
|
async openHistoryPrev(): Promise<void> { |
||||||
|
let tab = await this.tabPresenter.getCurrent(); |
||||||
|
await this.navigateClient.historyPrev(tab.id!!); |
||||||
|
} |
||||||
|
|
||||||
|
async openLinkNext(): Promise<void> { |
||||||
|
let tab = await this.tabPresenter.getCurrent(); |
||||||
|
await this.navigateClient.linkNext(tab.id!!); |
||||||
|
} |
||||||
|
|
||||||
|
async openLinkPrev(): Promise<void> { |
||||||
|
let tab = await this.tabPresenter.getCurrent(); |
||||||
|
await this.navigateClient.linkPrev(tab.id!!); |
||||||
|
} |
||||||
|
|
||||||
|
async openParent(): Promise<void> { |
||||||
|
let tab = await this.tabPresenter.getCurrent(); |
||||||
|
let 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> { |
||||||
|
let tab = await this.tabPresenter.getCurrent(); |
||||||
|
let url = new URL(tab.url!!); |
||||||
|
await this.tabPresenter.open(url.origin); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
import { injectable } from 'tsyringe'; |
||||||
|
import * as operations from '../../shared/operations'; |
||||||
|
import RepeatRepository from '../repositories/RepeatRepository'; |
||||||
|
|
||||||
|
type Operation = operations.Operation; |
||||||
|
|
||||||
|
@injectable() |
||||||
|
export default class RepeatUseCase { |
||||||
|
constructor( |
||||||
|
private repeatRepository: RepeatRepository, |
||||||
|
) { |
||||||
|
} |
||||||
|
|
||||||
|
storeLastOperation(op: Operation): void { |
||||||
|
this.repeatRepository.setLastOperation(op); |
||||||
|
} |
||||||
|
|
||||||
|
getLastOperation(): operations.Operation | undefined { |
||||||
|
return this.repeatRepository.getLastOperation(); |
||||||
|
} |
||||||
|
|
||||||
|
// eslint-disable-next-line complexity
|
||||||
|
isRepeatable(op: Operation): boolean { |
||||||
|
switch (op.type) { |
||||||
|
case operations.NAVIGATE_HISTORY_PREV: |
||||||
|
case operations.NAVIGATE_HISTORY_NEXT: |
||||||
|
case operations.NAVIGATE_LINK_PREV: |
||||||
|
case operations.NAVIGATE_LINK_NEXT: |
||||||
|
case operations.NAVIGATE_PARENT: |
||||||
|
case operations.NAVIGATE_ROOT: |
||||||
|
case operations.PAGE_SOURCE: |
||||||
|
case operations.PAGE_HOME: |
||||||
|
case operations.TAB_CLOSE: |
||||||
|
case operations.TAB_CLOSE_FORCE: |
||||||
|
case operations.TAB_CLOSE_RIGHT: |
||||||
|
case operations.TAB_REOPEN: |
||||||
|
case operations.TAB_RELOAD: |
||||||
|
case operations.TAB_PIN: |
||||||
|
case operations.TAB_UNPIN: |
||||||
|
case operations.TAB_TOGGLE_PINNED: |
||||||
|
case operations.TAB_DUPLICATE: |
||||||
|
case operations.ZOOM_IN: |
||||||
|
case operations.ZOOM_OUT: |
||||||
|
case operations.ZOOM_NEUTRAL: |
||||||
|
case operations.INTERNAL_OPEN_URL: |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
@ -1,13 +0,0 @@ |
|||||||
import { injectable } from 'tsyringe'; |
|
||||||
import * as operations from '../../shared/operations'; |
|
||||||
import * as messages from '../../shared/messages'; |
|
||||||
|
|
||||||
@injectable() |
|
||||||
export default class BackgroundClient { |
|
||||||
execBackgroundOp(op: operations.Operation): Promise<void> { |
|
||||||
return browser.runtime.sendMessage({ |
|
||||||
type: messages.BACKGROUND_OPERATION, |
|
||||||
operation: op, |
|
||||||
}); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,33 @@ |
|||||||
|
import * as operations from '../../shared/operations'; |
||||||
|
import * as messages from '../../shared/messages'; |
||||||
|
|
||||||
|
export default interface OperationClient { |
||||||
|
execBackgroundOp(op: operations.Operation): Promise<void>; |
||||||
|
|
||||||
|
internalOpenUrl( |
||||||
|
url: string, newTab?: boolean, background?: boolean, |
||||||
|
): Promise<void>; |
||||||
|
} |
||||||
|
|
||||||
|
export class OperationClientImpl implements OperationClient { |
||||||
|
execBackgroundOp(op: operations.Operation): Promise<void> { |
||||||
|
return browser.runtime.sendMessage({ |
||||||
|
type: messages.BACKGROUND_OPERATION, |
||||||
|
operation: op, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
internalOpenUrl( |
||||||
|
url: string, newTab?: boolean, background?: boolean, |
||||||
|
): Promise<void> { |
||||||
|
return browser.runtime.sendMessage({ |
||||||
|
type: messages.BACKGROUND_OPERATION, |
||||||
|
operation: { |
||||||
|
type: operations.INTERNAL_OPEN_URL, |
||||||
|
url, |
||||||
|
newTab, |
||||||
|
background, |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
import { injectable } from 'tsyringe'; |
||||||
|
import { Message } from '../../shared/messages'; |
||||||
|
import NavigateUseCase from '../usecases/NavigateUseCase'; |
||||||
|
|
||||||
|
@injectable() |
||||||
|
export default class NavigateController { |
||||||
|
constructor( |
||||||
|
private navigateUseCase: NavigateUseCase, |
||||||
|
) { |
||||||
|
} |
||||||
|
|
||||||
|
openHistoryNext(_m: Message): Promise<void> { |
||||||
|
this.navigateUseCase.openHistoryNext(); |
||||||
|
return Promise.resolve(); |
||||||
|
} |
||||||
|
|
||||||
|
openHistoryPrev(_m: Message): Promise<void> { |
||||||
|
this.navigateUseCase.openHistoryPrev(); |
||||||
|
return Promise.resolve(); |
||||||
|
} |
||||||
|
|
||||||
|
openLinkNext(_m: Message): Promise<void> { |
||||||
|
this.navigateUseCase.openLinkNext(); |
||||||
|
return Promise.resolve(); |
||||||
|
} |
||||||
|
|
||||||
|
openLinkPrev(_m: Message): Promise<void> { |
||||||
|
this.navigateUseCase.openLinkPrev(); |
||||||
|
return Promise.resolve(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,82 @@ |
|||||||
|
import TabPresenter from '../../../src/background/presenters/TabPresenter'; |
||||||
|
import NavigateUseCase from '../../../src/background/usecases/NavigateUseCase'; |
||||||
|
import NavigateClient from '../../../src/background/clients/NavigateClient'; |
||||||
|
// import { expect } from 'chai';
|
||||||
|
import * as sinon from 'sinon'; |
||||||
|
|
||||||
|
describe('NavigateUseCase', () => { |
||||||
|
let sut: NavigateUseCase; |
||||||
|
let tabPresenter: TabPresenter; |
||||||
|
let navigateClient: NavigateClient; |
||||||
|
beforeEach(() => { |
||||||
|
tabPresenter = new TabPresenter(); |
||||||
|
navigateClient = new NavigateClient(); |
||||||
|
sut = new NavigateUseCase(tabPresenter, navigateClient); |
||||||
|
}); |
||||||
|
|
||||||
|
describe('#openParent()', async () => { |
||||||
|
it('opens parent directory of file', async() => { |
||||||
|
var stub = sinon.stub(tabPresenter, 'getCurrent'); |
||||||
|
stub.returns(Promise.resolve({ url: 'https://google.com/fruits/yellow/banana' })) |
||||||
|
|
||||||
|
var mock = sinon.mock(tabPresenter); |
||||||
|
mock.expects('open').withArgs('https://google.com/fruits/yellow/'); |
||||||
|
|
||||||
|
await sut.openParent(); |
||||||
|
|
||||||
|
mock.verify(); |
||||||
|
}); |
||||||
|
|
||||||
|
it('opens parent directory of directory', async() => { |
||||||
|
var stub = sinon.stub(tabPresenter, 'getCurrent'); |
||||||
|
stub.returns(Promise.resolve({ url: 'https://google.com/fruits/yellow/' })) |
||||||
|
|
||||||
|
var mock = sinon.mock(tabPresenter); |
||||||
|
mock.expects('open').withArgs('https://google.com/fruits/'); |
||||||
|
|
||||||
|
await sut.openParent(); |
||||||
|
|
||||||
|
mock.verify(); |
||||||
|
}); |
||||||
|
|
||||||
|
it('removes hash', async() => { |
||||||
|
var stub = sinon.stub(tabPresenter, 'getCurrent'); |
||||||
|
stub.returns(Promise.resolve({ url: 'https://google.com/#top' })) |
||||||
|
|
||||||
|
var mock = sinon.mock(tabPresenter); |
||||||
|
mock.expects('open').withArgs('https://google.com/'); |
||||||
|
|
||||||
|
await sut.openParent(); |
||||||
|
|
||||||
|
mock.verify(); |
||||||
|
}); |
||||||
|
|
||||||
|
it('removes search query', async() => { |
||||||
|
var stub = sinon.stub(tabPresenter, 'getCurrent'); |
||||||
|
stub.returns(Promise.resolve({ url: 'https://google.com/search?q=apple' })) |
||||||
|
|
||||||
|
var mock = sinon.mock(tabPresenter); |
||||||
|
mock.expects('open').withArgs('https://google.com/search'); |
||||||
|
|
||||||
|
await sut.openParent(); |
||||||
|
|
||||||
|
mock.verify(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe('#openRoot()', () => { |
||||||
|
it('opens root direectory', async() => { |
||||||
|
var stub = sinon.stub(tabPresenter, 'getCurrent'); |
||||||
|
stub.returns(Promise.resolve({ |
||||||
|
url: 'https://google.com/seach?q=apple', |
||||||
|
})) |
||||||
|
|
||||||
|
var mock = sinon.mock(tabPresenter); |
||||||
|
mock.expects('open').withArgs('https://google.com'); |
||||||
|
|
||||||
|
await sut.openRoot(); |
||||||
|
|
||||||
|
mock.verify(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
Reference in new issue