From b496cea5827165bd23a503231f94f708a976cad4 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Thu, 3 Oct 2019 12:32:32 +0000 Subject: Make KeySequence class --- src/content/repositories/KeymapRepository.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/content/repositories') diff --git a/src/content/repositories/KeymapRepository.ts b/src/content/repositories/KeymapRepository.ts index d7b5b7d..4463678 100644 --- a/src/content/repositories/KeymapRepository.ts +++ b/src/content/repositories/KeymapRepository.ts @@ -7,7 +7,7 @@ export default interface KeymapRepository { clear(): void; } -let current: KeySequence = KeySequence.from([]); +let current: KeySequence = new KeySequence([]); export class KeymapRepositoryImpl { @@ -17,6 +17,6 @@ export class KeymapRepositoryImpl { } clear(): void { - current = KeySequence.from([]); + current = new KeySequence([]); } } -- cgit v1.2.3 From 0dec6c641fc11348f89a12680a087ccda1181f66 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 07:19:48 +0000 Subject: Make Settings class --- src/background/controllers/SettingController.ts | 2 +- .../infrastructures/ContentMessageListener.ts | 3 +- src/background/repositories/SettingRepository.ts | 7 +- src/background/usecases/SettingUseCase.ts | 2 +- src/content/client/SettingClient.ts | 4 +- src/content/repositories/SettingRepository.ts | 2 +- src/content/usecases/SettingUseCase.ts | 2 +- src/shared/SettingData.ts | 6 +- src/shared/Settings.ts | 127 ----------------- src/shared/settings/Settings.ts | 154 +++++++++++++++++++++ .../content/repositories/SettingRepository.test.ts | 13 +- test/content/usecases/SettingUseCaase.test.ts | 2 +- test/shared/SettingData.test.ts | 39 +++--- test/shared/Settings.test.ts | 54 -------- test/shared/settings/Settings.test.ts | 54 ++++++++ 15 files changed, 246 insertions(+), 225 deletions(-) delete mode 100644 src/shared/Settings.ts create mode 100644 src/shared/settings/Settings.ts delete mode 100644 test/shared/Settings.test.ts create mode 100644 test/shared/settings/Settings.test.ts (limited to 'src/content/repositories') diff --git a/src/background/controllers/SettingController.ts b/src/background/controllers/SettingController.ts index 34951ff..8d05852 100644 --- a/src/background/controllers/SettingController.ts +++ b/src/background/controllers/SettingController.ts @@ -1,7 +1,7 @@ import { injectable } from 'tsyringe'; import SettingUseCase from '../usecases/SettingUseCase'; import ContentMessageClient from '../infrastructures/ContentMessageClient'; -import Settings from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; @injectable() export default class SettingController { diff --git a/src/background/infrastructures/ContentMessageListener.ts b/src/background/infrastructures/ContentMessageListener.ts index f348a74..f80d686 100644 --- a/src/background/infrastructures/ContentMessageListener.ts +++ b/src/background/infrastructures/ContentMessageListener.ts @@ -8,7 +8,6 @@ import AddonEnabledController from '../controllers/AddonEnabledController'; import LinkController from '../controllers/LinkController'; import OperationController from '../controllers/OperationController'; import MarkController from '../controllers/MarkController'; -import { toJSON } from '../../shared/Settings'; @injectable() export default class ContentMessageListener { @@ -103,7 +102,7 @@ export default class ContentMessageListener { } async onSettingsQuery(): Promise { - return toJSON(await this.settingController.getSetting()); + return (await this.settingController.getSetting()).toJSON(); } onFindGetKeyword(): Promise { diff --git a/src/background/repositories/SettingRepository.ts b/src/background/repositories/SettingRepository.ts index a11b65f..e775a32 100644 --- a/src/background/repositories/SettingRepository.ts +++ b/src/background/repositories/SettingRepository.ts @@ -1,6 +1,6 @@ import { injectable } from 'tsyringe'; import MemoryStorage from '../infrastructures/MemoryStorage'; -import Settings, { valueOf, toJSON } from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; import Properties from '../../shared/settings/Properties'; const CACHED_SETTING_KEY = 'setting'; @@ -15,12 +15,11 @@ export default class SettingRepository { get(): Promise { let data = this.cache.get(CACHED_SETTING_KEY); - return Promise.resolve(valueOf(data)); + return Promise.resolve(Settings.fromJSON(data)); } update(value: Settings): void { - let data = toJSON(value); - return this.cache.set(CACHED_SETTING_KEY, data); + return this.cache.set(CACHED_SETTING_KEY, value.toJSON()); } async setProperty( diff --git a/src/background/usecases/SettingUseCase.ts b/src/background/usecases/SettingUseCase.ts index d73521f..d78d440 100644 --- a/src/background/usecases/SettingUseCase.ts +++ b/src/background/usecases/SettingUseCase.ts @@ -3,7 +3,7 @@ import PersistentSettingRepository from '../repositories/PersistentSettingRepository'; import SettingRepository from '../repositories/SettingRepository'; import { DefaultSettingData } from '../../shared/SettingData'; -import Settings from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; import NotifyPresenter from '../presenters/NotifyPresenter'; @injectable() diff --git a/src/content/client/SettingClient.ts b/src/content/client/SettingClient.ts index a7cd1ee..fc62720 100644 --- a/src/content/client/SettingClient.ts +++ b/src/content/client/SettingClient.ts @@ -1,4 +1,4 @@ -import Settings, { valueOf } from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; import * as messages from '../../shared/messages'; export default interface SettingClient { @@ -10,6 +10,6 @@ export class SettingClientImpl { let settings = await browser.runtime.sendMessage({ type: messages.SETTINGS_QUERY, }); - return valueOf(settings); + return Settings.fromJSON(settings); } } diff --git a/src/content/repositories/SettingRepository.ts b/src/content/repositories/SettingRepository.ts index d718794..4ba26e0 100644 --- a/src/content/repositories/SettingRepository.ts +++ b/src/content/repositories/SettingRepository.ts @@ -1,4 +1,4 @@ -import Settings, { DefaultSetting } from '../../shared/Settings'; +import Settings, { DefaultSetting } from '../../shared/settings/Settings'; let current: Settings = DefaultSetting; diff --git a/src/content/usecases/SettingUseCase.ts b/src/content/usecases/SettingUseCase.ts index d5f66c6..4608039 100644 --- a/src/content/usecases/SettingUseCase.ts +++ b/src/content/usecases/SettingUseCase.ts @@ -1,7 +1,7 @@ import { injectable, inject } from 'tsyringe'; import SettingRepository from '../repositories/SettingRepository'; import SettingClient from '../client/SettingClient'; -import Settings from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; @injectable() export default class SettingUseCase { diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index 8ef8385..2dedfef 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -1,5 +1,5 @@ import * as operations from './operations'; -import Settings, * as settings from './Settings'; +import Settings from './settings/Settings'; import Keymaps from './settings/Keymaps'; import Search from './settings/Search'; import Properties from './settings/Properties'; @@ -118,7 +118,7 @@ export class JSONTextSettings { } toSettings(): Settings { - return settings.valueOf(JSON.parse(this.json)); + return Settings.fromJSON(JSON.parse(this.json)); } toJSONText(): string { @@ -198,7 +198,7 @@ export class FormSettings { } toSettings(): Settings { - return settings.valueOf({ + return Settings.fromJSON({ keymaps: this.keymaps.toKeymaps().toJSON(), search: this.search.toSearchSettings().toJSON(), properties: this.properties.toJSON(), diff --git a/src/shared/Settings.ts b/src/shared/Settings.ts deleted file mode 100644 index 2767820..0000000 --- a/src/shared/Settings.ts +++ /dev/null @@ -1,127 +0,0 @@ -import Keymaps from './settings/Keymaps'; -import Search from './settings/Search'; -import Properties from './settings/Properties'; -import Blacklist from './settings/Blacklist'; - -export default interface Settings { - keymaps: Keymaps; - search: Search; - properties: Properties; - blacklist: Blacklist; -} - -export const valueOf = (o: any): Settings => { - let settings = { ...DefaultSetting }; - for (let key of Object.keys(o)) { - switch (key) { - case 'keymaps': - settings.keymaps = Keymaps.fromJSON(o.keymaps); - break; - case 'search': - settings.search = Search.fromJSON(o.search); - break; - case 'properties': - settings.properties = Properties.fromJSON(o.properties); - break; - case 'blacklist': - settings.blacklist = Blacklist.fromJSON(o.blacklist); - break; - default: - throw new TypeError('unknown setting: ' + key); - } - } - return settings; -}; - -export const toJSON = (settings: Settings): any => { - return { - keymaps: settings.keymaps.toJSON(), - search: settings.search.toJSON(), - properties: settings.properties.toJSON(), - blacklist: settings.blacklist.toJSON(), - }; -}; - -export const DefaultSetting: Settings = { - keymaps: Keymaps.fromJSON({ - '0': { 'type': 'scroll.home' }, - ':': { 'type': 'command.show' }, - 'o': { 'type': 'command.show.open', 'alter': false }, - 'O': { 'type': 'command.show.open', 'alter': true }, - 't': { 'type': 'command.show.tabopen', 'alter': false }, - 'T': { 'type': 'command.show.tabopen', 'alter': true }, - 'w': { 'type': 'command.show.winopen', 'alter': false }, - 'W': { 'type': 'command.show.winopen', 'alter': true }, - 'b': { 'type': 'command.show.buffer' }, - 'a': { 'type': 'command.show.addbookmark', 'alter': true }, - 'k': { 'type': 'scroll.vertically', 'count': -1 }, - 'j': { 'type': 'scroll.vertically', 'count': 1 }, - 'h': { 'type': 'scroll.horizonally', 'count': -1 }, - 'l': { 'type': 'scroll.horizonally', 'count': 1 }, - '': { 'type': 'scroll.pages', 'count': -0.5 }, - '': { 'type': 'scroll.pages', 'count': 0.5 }, - '': { 'type': 'scroll.pages', 'count': -1 }, - '': { 'type': 'scroll.pages', 'count': 1 }, - 'gg': { 'type': 'scroll.top' }, - 'G': { 'type': 'scroll.bottom' }, - '$': { 'type': 'scroll.end' }, - 'd': { 'type': 'tabs.close' }, - 'D': { 'type': 'tabs.close', 'select': 'left' }, - 'x$': { 'type': 'tabs.close.right' }, - '!d': { 'type': 'tabs.close.force' }, - 'u': { 'type': 'tabs.reopen' }, - 'K': { 'type': 'tabs.prev' }, - 'J': { 'type': 'tabs.next' }, - 'gT': { 'type': 'tabs.prev' }, - 'gt': { 'type': 'tabs.next' }, - 'g0': { 'type': 'tabs.first' }, - 'g$': { 'type': 'tabs.last' }, - '': { 'type': 'tabs.prevsel' }, - 'r': { 'type': 'tabs.reload', 'cache': false }, - 'R': { 'type': 'tabs.reload', 'cache': true }, - 'zp': { 'type': 'tabs.pin.toggle' }, - 'zd': { 'type': 'tabs.duplicate' }, - 'zi': { 'type': 'zoom.in' }, - 'zo': { 'type': 'zoom.out' }, - 'zz': { 'type': 'zoom.neutral' }, - 'f': { 'type': 'follow.start', 'newTab': false, 'background': false }, - 'F': { 'type': 'follow.start', 'newTab': true, 'background': false }, - 'm': { 'type': 'mark.set.prefix' }, - '\'': { 'type': 'mark.jump.prefix' }, - 'H': { 'type': 'navigate.history.prev' }, - 'L': { 'type': 'navigate.history.next' }, - '[[': { 'type': 'navigate.link.prev' }, - ']]': { 'type': 'navigate.link.next' }, - 'gu': { 'type': 'navigate.parent' }, - 'gU': { 'type': 'navigate.root' }, - 'gi': { 'type': 'focus.input' }, - 'gf': { 'type': 'page.source' }, - 'gh': { 'type': 'page.home', 'newTab': false }, - 'gH': { 'type': 'page.home', 'newTab': true }, - 'y': { 'type': 'urls.yank' }, - 'p': { 'type': 'urls.paste', 'newTab': false }, - 'P': { 'type': 'urls.paste', 'newTab': true }, - '/': { 'type': 'find.start' }, - 'n': { 'type': 'find.next' }, - 'N': { 'type': 'find.prev' }, - '.': { 'type': 'repeat.last' }, - '': { 'type': 'addon.toggle.enabled' } - }), - search: Search.fromJSON({ - default: 'google', - engines: { - 'google': 'https://google.com/search?q={}', - 'yahoo': 'https://search.yahoo.com/search?p={}', - 'bing': 'https://www.bing.com/search?q={}', - 'duckduckgo': 'https://duckduckgo.com/?q={}', - 'twitter': 'https://twitter.com/search?q={}', - 'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}' - } - }), - properties: Properties.fromJSON({ - hintchars: 'abcdefghijklmnopqrstuvwxyz', - smoothscroll: false, - complete: 'sbh' - }), - blacklist: Blacklist.fromJSON([]), -}; diff --git a/src/shared/settings/Settings.ts b/src/shared/settings/Settings.ts new file mode 100644 index 0000000..116c7d7 --- /dev/null +++ b/src/shared/settings/Settings.ts @@ -0,0 +1,154 @@ +import Keymaps, { KeymapsJSON } from './Keymaps'; +import Search, { SearchJSON } from './Search'; +import Properties, { PropertiesJSON } from './Properties'; +import Blacklist, { BlacklistJSON } from './Blacklist'; + +export type SettingsJSON = { + keymaps: KeymapsJSON, + search: SearchJSON, + properties: PropertiesJSON, + blacklist: BlacklistJSON, +}; + +export default class Settings { + public keymaps: Keymaps; + + public search: Search; + + public properties: Properties; + + public blacklist: Blacklist; + + constructor({ + keymaps, + search, + properties, + blacklist, + }: { + keymaps: Keymaps; + search: Search; + properties: Properties; + blacklist: Blacklist; + }) { + this.keymaps = keymaps; + this.search = search; + this.properties = properties; + this.blacklist = blacklist; + } + + static fromJSON(json: any): Settings { + let settings = { ...DefaultSetting }; + for (let key of Object.keys(json)) { + switch (key) { + case 'keymaps': + settings.keymaps = Keymaps.fromJSON(json.keymaps); + break; + case 'search': + settings.search = Search.fromJSON(json.search); + break; + case 'properties': + settings.properties = Properties.fromJSON(json.properties); + break; + case 'blacklist': + settings.blacklist = Blacklist.fromJSON(json.blacklist); + break; + default: + throw new TypeError('unknown setting: ' + key); + } + } + return new Settings(settings); + } + + toJSON(): SettingsJSON { + return { + keymaps: this.keymaps.toJSON(), + search: this.search.toJSON(), + properties: this.properties.toJSON(), + blacklist: this.blacklist.toJSON(), + }; + } +} + +export const DefaultSetting: Settings = Settings.fromJSON({ + keymaps: { + '0': { 'type': 'scroll.home' }, + ':': { 'type': 'command.show' }, + 'o': { 'type': 'command.show.open', 'alter': false }, + 'O': { 'type': 'command.show.open', 'alter': true }, + 't': { 'type': 'command.show.tabopen', 'alter': false }, + 'T': { 'type': 'command.show.tabopen', 'alter': true }, + 'w': { 'type': 'command.show.winopen', 'alter': false }, + 'W': { 'type': 'command.show.winopen', 'alter': true }, + 'b': { 'type': 'command.show.buffer' }, + 'a': { 'type': 'command.show.addbookmark', 'alter': true }, + 'k': { 'type': 'scroll.vertically', 'count': -1 }, + 'j': { 'type': 'scroll.vertically', 'count': 1 }, + 'h': { 'type': 'scroll.horizonally', 'count': -1 }, + 'l': { 'type': 'scroll.horizonally', 'count': 1 }, + '': { 'type': 'scroll.pages', 'count': -0.5 }, + '': { 'type': 'scroll.pages', 'count': 0.5 }, + '': { 'type': 'scroll.pages', 'count': -1 }, + '': { 'type': 'scroll.pages', 'count': 1 }, + 'gg': { 'type': 'scroll.top' }, + 'G': { 'type': 'scroll.bottom' }, + '$': { 'type': 'scroll.end' }, + 'd': { 'type': 'tabs.close' }, + 'D': { 'type': 'tabs.close', 'select': 'left' }, + 'x$': { 'type': 'tabs.close.right' }, + '!d': { 'type': 'tabs.close.force' }, + 'u': { 'type': 'tabs.reopen' }, + 'K': { 'type': 'tabs.prev' }, + 'J': { 'type': 'tabs.next' }, + 'gT': { 'type': 'tabs.prev' }, + 'gt': { 'type': 'tabs.next' }, + 'g0': { 'type': 'tabs.first' }, + 'g$': { 'type': 'tabs.last' }, + '': { 'type': 'tabs.prevsel' }, + 'r': { 'type': 'tabs.reload', 'cache': false }, + 'R': { 'type': 'tabs.reload', 'cache': true }, + 'zp': { 'type': 'tabs.pin.toggle' }, + 'zd': { 'type': 'tabs.duplicate' }, + 'zi': { 'type': 'zoom.in' }, + 'zo': { 'type': 'zoom.out' }, + 'zz': { 'type': 'zoom.neutral' }, + 'f': { 'type': 'follow.start', 'newTab': false, 'background': false }, + 'F': { 'type': 'follow.start', 'newTab': true, 'background': false }, + 'm': { 'type': 'mark.set.prefix' }, + '\'': { 'type': 'mark.jump.prefix' }, + 'H': { 'type': 'navigate.history.prev' }, + 'L': { 'type': 'navigate.history.next' }, + '[[': { 'type': 'navigate.link.prev' }, + ']]': { 'type': 'navigate.link.next' }, + 'gu': { 'type': 'navigate.parent' }, + 'gU': { 'type': 'navigate.root' }, + 'gi': { 'type': 'focus.input' }, + 'gf': { 'type': 'page.source' }, + 'gh': { 'type': 'page.home', 'newTab': false }, + 'gH': { 'type': 'page.home', 'newTab': true }, + 'y': { 'type': 'urls.yank' }, + 'p': { 'type': 'urls.paste', 'newTab': false }, + 'P': { 'type': 'urls.paste', 'newTab': true }, + '/': { 'type': 'find.start' }, + 'n': { 'type': 'find.next' }, + 'N': { 'type': 'find.prev' }, + '.': { 'type': 'repeat.last' }, + '': { 'type': 'addon.toggle.enabled' } + }, + search: { + default: 'google', + engines: { + 'google': 'https://google.com/search?q={}', + 'yahoo': 'https://search.yahoo.com/search?p={}', + 'bing': 'https://www.bing.com/search?q={}', + 'duckduckgo': 'https://duckduckgo.com/?q={}', + 'twitter': 'https://twitter.com/search?q={}', + 'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}' + } + }, + properties: { + hintchars: 'abcdefghijklmnopqrstuvwxyz', + smoothscroll: false, + complete: 'sbh' + }, + blacklist: [], +}); diff --git a/test/content/repositories/SettingRepository.test.ts b/test/content/repositories/SettingRepository.test.ts index 363fcec..db4c528 100644 --- a/test/content/repositories/SettingRepository.test.ts +++ b/test/content/repositories/SettingRepository.test.ts @@ -1,27 +1,26 @@ import { SettingRepositoryImpl } from '../../../src/content/repositories/SettingRepository'; import { expect } from 'chai'; -import Keymaps from '../../../src/shared/settings/Keymaps'; -import Search from '../../../src/shared/settings/Search'; +import Settings from '../../../src/shared/settings/Settings'; describe('SettingRepositoryImpl', () => { it('updates and gets current value', () => { let sut = new SettingRepositoryImpl(); - let settings = { - keymaps: Keymaps.fromJSON({}), - search: Search.fromJSON({ + let settings = Settings.fromJSON({ + keymaps: {}, + search:{ default: 'google', engines: { google: 'https://google.com/?q={}', } - }), + }, properties: { hintchars: 'abcd1234', smoothscroll: false, complete: 'sbh', }, blacklist: [], - }; + }); sut.set(settings); diff --git a/test/content/usecases/SettingUseCaase.test.ts b/test/content/usecases/SettingUseCaase.test.ts index e9633f4..136c5af 100644 --- a/test/content/usecases/SettingUseCaase.test.ts +++ b/test/content/usecases/SettingUseCaase.test.ts @@ -1,7 +1,7 @@ import SettingRepository from '../../../src/content/repositories/SettingRepository'; import SettingClient from '../../../src/content/client/SettingClient'; import SettingUseCase from '../../../src/content/usecases/SettingUseCase'; -import Settings, { DefaultSetting } from '../../../src/shared/Settings'; +import Settings, { DefaultSetting } from '../../../src/shared/settings/Settings'; import { expect } from 'chai'; class MockSettingRepository implements SettingRepository { diff --git a/test/shared/SettingData.test.ts b/test/shared/SettingData.test.ts index 4391e73..138d768 100644 --- a/test/shared/SettingData.test.ts +++ b/test/shared/SettingData.test.ts @@ -1,12 +1,9 @@ import SettingData, { FormKeymaps, JSONTextSettings, FormSettings, } from '../../src/shared/SettingData'; -import Settings from '../../src/shared/Settings'; +import Settings from '../../src/shared/settings/Settings'; import { expect } from 'chai'; import Keymaps from '../../src/shared/settings/Keymaps'; -import Search from '../../src/shared/settings/Search'; -import Properties from '../../src/shared/settings/Properties'; -import Blacklist from '../../src/shared/settings/Blacklist' describe('shared/SettingData', () => { describe('FormKeymaps', () => { @@ -72,21 +69,21 @@ describe('shared/SettingData', () => { describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { - let o = { - keymaps: Keymaps.fromJSON({}), - search: Search.fromJSON({ + let o = Settings.fromJSON({ + keymaps: {}, + search: { default: "google", engines: { google: "https://google.com/search?q={}", }, - }), - properties: Properties.fromJSON({ + }, + properties: { hintchars: "abcdefghijklmnopqrstuvwxyz", smoothscroll: false, complete: "sbh" - }), - blacklist: Blacklist.fromJSON([]), - }; + }, + blacklist: [], + }); let json = JSONTextSettings.fromSettings(o).toJSONText(); expect(JSON.parse(json)).to.deep.equal({ @@ -150,24 +147,24 @@ describe('shared/SettingData', () => { describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { - let data: Settings = { - keymaps: Keymaps.fromJSON({ + let data: Settings = Settings.fromJSON({ + keymaps: { 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, - }), - search: Search.fromJSON({ + }, + search: { default: "google", engines: { "google": "https://google.com/search?q={}" } - }), - properties: Properties.fromJSON({ + }, + properties: { hintchars: "abcdefghijklmnopqrstuvwxyz", smoothscroll: false, complete: "sbh" - }), - blacklist: Blacklist.fromJSON([]), - }; + }, + blacklist: [], + }); let json = FormSettings.fromSettings(data).toJSON(); expect(json).to.deep.equal({ diff --git a/test/shared/Settings.test.ts b/test/shared/Settings.test.ts deleted file mode 100644 index 9688798..0000000 --- a/test/shared/Settings.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import * as settings from '../../src/shared/Settings'; -import {expect} from 'chai'; - -describe('Settings', () => { - describe('#valueOf', () => { - it('returns settings by valid settings', () => { - let x = settings.valueOf({ - keymaps: {}, - "search": { - "default": "google", - "engines": { - "google": "https://google.com/search?q={}", - } - }, - "properties": {}, - "blacklist": [] - }); - - expect({ - keymaps: x.keymaps.toJSON(), - search: x.search.toJSON(), - properties: x.properties.toJSON(), - blacklist: x.blacklist.toJSON(), - }).to.deep.equal({ - keymaps: {}, - search: { - default: "google", - engines: { - google: "https://google.com/search?q={}", - } - }, - properties: { - hintchars: "abcdefghijklmnopqrstuvwxyz", - smoothscroll: false, - complete: "sbh" - }, - blacklist: [] - }); - }); - - it('sets default settings', () => { - let value = settings.valueOf({}); - expect(value.keymaps.toJSON()).to.not.be.empty; - expect(value.properties.toJSON()).to.not.be.empty; - expect(value.search.defaultEngine).to.be.a('string'); - expect(value.search.engines).to.be.an('object'); - expect(value.blacklist.toJSON()).to.be.empty; - }); - - it('throws a TypeError with an unknown field', () => { - expect(() => settings.valueOf({ name: 'alice' })).to.throw(TypeError) - }); - }); -}); diff --git a/test/shared/settings/Settings.test.ts b/test/shared/settings/Settings.test.ts new file mode 100644 index 0000000..ab6af04 --- /dev/null +++ b/test/shared/settings/Settings.test.ts @@ -0,0 +1,54 @@ +import Settings from '../../../src/shared/settings/Settings'; +import { expect } from 'chai'; + +describe('Settings', () => { + describe('#valueOf', () => { + it('returns settings by valid settings', () => { + let x = Settings.fromJSON({ + keymaps: {}, + "search": { + "default": "google", + "engines": { + "google": "https://google.com/search?q={}", + } + }, + "properties": {}, + "blacklist": [] + }); + + expect({ + keymaps: x.keymaps.toJSON(), + search: x.search.toJSON(), + properties: x.properties.toJSON(), + blacklist: x.blacklist.toJSON(), + }).to.deep.equal({ + keymaps: {}, + search: { + default: "google", + engines: { + google: "https://google.com/search?q={}", + } + }, + properties: { + hintchars: "abcdefghijklmnopqrstuvwxyz", + smoothscroll: false, + complete: "sbh" + }, + blacklist: [] + }); + }); + + it('sets default settings', () => { + let value = Settings.fromJSON({}); + expect(value.keymaps.toJSON()).to.not.be.empty; + expect(value.properties.toJSON()).to.not.be.empty; + expect(value.search.defaultEngine).to.be.a('string'); + expect(value.search.engines).to.be.an('object'); + expect(value.blacklist.toJSON()).to.be.empty; + }); + + it('throws a TypeError with an unknown field', () => { + expect(() => Settings.fromJSON({ name: 'alice' })).to.throw(TypeError) + }); + }); +}); -- cgit v1.2.3 From da3ce77aa0a50568ee4ddb423d07bc50422bd79c Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sun, 6 Oct 2019 03:21:38 +0000 Subject: Move Key to settings --- src/content/InputDriver.ts | 36 +++++- src/content/client/FollowMasterClient.ts | 2 +- src/content/controllers/FollowKeyController.ts | 2 +- src/content/controllers/KeymapController.ts | 2 +- src/content/controllers/MarkKeyController.ts | 2 +- src/content/domains/Key.ts | 93 -------------- src/content/domains/KeySequence.ts | 54 -------- src/content/repositories/KeymapRepository.ts | 4 +- src/content/usecases/FollowSlaveUseCase.ts | 2 +- src/content/usecases/KeymapUseCase.ts | 11 +- src/shared/settings/Key.ts | 61 +++++++++ src/shared/settings/KeySequence.ts | 54 ++++++++ test/content/InputDriver.test.ts | 51 +++++++- test/content/domains/Key.test.ts | 139 --------------------- test/content/domains/KeySequence.test.ts | 72 ----------- test/content/repositories/KeymapRepository.test.ts | 2 +- test/shared/settings/Key.test.ts | 92 ++++++++++++++ test/shared/settings/KeySequence.test.ts | 72 +++++++++++ 18 files changed, 375 insertions(+), 376 deletions(-) delete mode 100644 src/content/domains/Key.ts delete mode 100644 src/content/domains/KeySequence.ts create mode 100644 src/shared/settings/Key.ts create mode 100644 src/shared/settings/KeySequence.ts delete mode 100644 test/content/domains/Key.test.ts delete mode 100644 test/content/domains/KeySequence.test.ts create mode 100644 test/shared/settings/Key.test.ts create mode 100644 test/shared/settings/KeySequence.test.ts (limited to 'src/content/repositories') diff --git a/src/content/InputDriver.ts b/src/content/InputDriver.ts index e77d857..bc184d2 100644 --- a/src/content/InputDriver.ts +++ b/src/content/InputDriver.ts @@ -1,5 +1,5 @@ import * as dom from '../shared/utils/dom'; -import Key from './domains/Key'; +import Key from '../shared/settings/Key'; const cancelKey = (e: KeyboardEvent): boolean => { if (e.key === 'Escape') { @@ -11,6 +11,38 @@ const cancelKey = (e: KeyboardEvent): boolean => { return false; }; +const modifiedKeyName = (name: string): string => { + if (name === ' ') { + return 'Space'; + } + if (name.length === 1) { + return name; + } else if (name === 'Escape') { + return 'Esc'; + } + return name; +}; + +// visible for testing +export const keyFromKeyboardEvent = (e: KeyboardEvent): Key => { + let key = modifiedKeyName(e.key); + let shift = e.shiftKey; + if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) { + // make shift false for symbols to enable key bindings by symbold keys. + // But this limits key bindings by symbol keys with Shift + // (such as Shift+$>. + shift = false; + } + + return new Key({ + key: modifiedKeyName(e.key), + shift: shift, + ctrl: e.ctrlKey, + alt: e.altKey, + meta: e.metaKey, + }); +}; + export default class InputDriver { private pressed: {[key: string]: string} = {}; @@ -66,7 +98,7 @@ export default class InputDriver { return; } - let key = Key.fromKeyboardEvent(e); + let key = keyFromKeyboardEvent(e); for (let listener of this.onKeyListeners) { let stop = listener(key); if (stop) { diff --git a/src/content/client/FollowMasterClient.ts b/src/content/client/FollowMasterClient.ts index f79c8b0..6681e8a 100644 --- a/src/content/client/FollowMasterClient.ts +++ b/src/content/client/FollowMasterClient.ts @@ -1,5 +1,5 @@ import * as messages from '../../shared/messages'; -import Key from '../domains/Key'; +import Key from '../../shared/settings/Key'; export default interface FollowMasterClient { startFollow(newTab: boolean, background: boolean): void; diff --git a/src/content/controllers/FollowKeyController.ts b/src/content/controllers/FollowKeyController.ts index 59d2271..0fd94ff 100644 --- a/src/content/controllers/FollowKeyController.ts +++ b/src/content/controllers/FollowKeyController.ts @@ -1,6 +1,6 @@ import { injectable } from 'tsyringe'; import FollowSlaveUseCase from '../usecases/FollowSlaveUseCase'; -import Key from '../domains/Key'; +import Key from '../../shared/settings/Key'; @injectable() export default class FollowKeyController { diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index fcfaff1..6157a71 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -9,7 +9,7 @@ import ClipboardUseCase from '../usecases/ClipboardUseCase'; import OperationClient from '../client/OperationClient'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; import FollowMasterClient from '../client/FollowMasterClient'; -import Key from '../domains/Key'; +import Key from '../../shared/settings/Key'; @injectable() export default class KeymapController { diff --git a/src/content/controllers/MarkKeyController.ts b/src/content/controllers/MarkKeyController.ts index 886e5ff..e7653ee 100644 --- a/src/content/controllers/MarkKeyController.ts +++ b/src/content/controllers/MarkKeyController.ts @@ -1,7 +1,7 @@ import { injectable } from 'tsyringe'; import MarkUseCase from '../usecases/MarkUseCase'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; -import Key from '../domains/Key'; +import Key from '../../shared/settings/Key'; @injectable() export default class MarkKeyController { diff --git a/src/content/domains/Key.ts b/src/content/domains/Key.ts deleted file mode 100644 index 669edfc..0000000 --- a/src/content/domains/Key.ts +++ /dev/null @@ -1,93 +0,0 @@ -const modifiedKeyName = (name: string): string => { - if (name === ' ') { - return 'Space'; - } - if (name.length === 1) { - return name; - } else if (name === 'Escape') { - return 'Esc'; - } - return name; -}; - -export default class Key { - public readonly key: string; - - public readonly shift: boolean; - - public readonly ctrl: boolean; - - public readonly alt: boolean; - - public readonly meta: boolean; - - constructor({ key, shift, ctrl, alt, meta }: { - key: string; - shift: boolean; - ctrl: boolean; - alt: boolean; - meta: boolean; - }) { - this.key = key; - this.shift = shift; - this.ctrl = ctrl; - this.alt = alt; - this.meta = meta; - } - - static fromMapKey(str: string): Key { - if (str.startsWith('<') && str.endsWith('>')) { - let inner = str.slice(1, -1); - let shift = inner.includes('S-'); - let base = inner.slice(inner.lastIndexOf('-') + 1); - if (shift && base.length === 1) { - base = base.toUpperCase(); - } else if (!shift && base.length === 1) { - base = base.toLowerCase(); - } - return new Key({ - key: base, - shift: shift, - ctrl: inner.includes('C-'), - alt: inner.includes('A-'), - meta: inner.includes('M-'), - }); - } - - return new Key({ - key: str, - shift: str.toLowerCase() !== str, - ctrl: false, - alt: false, - meta: false, - }); - } - - static fromKeyboardEvent(e: KeyboardEvent): Key { - let key = modifiedKeyName(e.key); - let shift = e.shiftKey; - if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) { - // make shift false for symbols to enable key bindings by symbold keys. - // But this limits key bindings by symbol keys with Shift - // (such as Shift+$>. - shift = false; - } - - return new Key({ - key: modifiedKeyName(e.key), - shift: shift, - ctrl: e.ctrlKey, - alt: e.altKey, - meta: e.metaKey, - }); - } - - equals(key: Key) { - return this.key === key.key && - this.ctrl === key.ctrl && - this.meta === key.meta && - this.alt === key.alt && - this.shift === key.shift; - } -} - diff --git a/src/content/domains/KeySequence.ts b/src/content/domains/KeySequence.ts deleted file mode 100644 index abae61a..0000000 --- a/src/content/domains/KeySequence.ts +++ /dev/null @@ -1,54 +0,0 @@ -import Key from './Key'; - -export default class KeySequence { - constructor( - public readonly keys: Key[], - ) { - } - - push(key: Key): number { - return this.keys.push(key); - } - - length(): number { - return this.keys.length; - } - - startsWith(o: KeySequence): boolean { - if (this.keys.length < o.keys.length) { - return false; - } - for (let i = 0; i < o.keys.length; ++i) { - if (!this.keys[i].equals(o.keys[i])) { - return false; - } - } - return true; - } - - static fromMapKeys(keys: string): KeySequence { - const fromMapKeysRecursive = ( - remaining: string, mappedKeys: Key[], - ): Key[] => { - if (remaining.length === 0) { - return mappedKeys; - } - - let nextPos = 1; - if (remaining.startsWith('<')) { - let ltPos = remaining.indexOf('>'); - if (ltPos > 0) { - nextPos = ltPos + 1; - } - } - - return fromMapKeysRecursive( - remaining.slice(nextPos), - mappedKeys.concat([Key.fromMapKey(remaining.slice(0, nextPos))]) - ); - }; - - let data = fromMapKeysRecursive(keys, []); - return new KeySequence(data); - } -} diff --git a/src/content/repositories/KeymapRepository.ts b/src/content/repositories/KeymapRepository.ts index 4463678..3391229 100644 --- a/src/content/repositories/KeymapRepository.ts +++ b/src/content/repositories/KeymapRepository.ts @@ -1,5 +1,5 @@ -import Key from '../domains/Key'; -import KeySequence from '../domains/KeySequence'; +import Key from '../../shared/settings/Key'; +import KeySequence from '../../shared/settings/KeySequence'; export default interface KeymapRepository { enqueueKey(key: Key): KeySequence; diff --git a/src/content/usecases/FollowSlaveUseCase.ts b/src/content/usecases/FollowSlaveUseCase.ts index 2bd16ee..d471adb 100644 --- a/src/content/usecases/FollowSlaveUseCase.ts +++ b/src/content/usecases/FollowSlaveUseCase.ts @@ -4,7 +4,7 @@ import FollowPresenter from '../presenters/FollowPresenter'; import TabsClient from '../client/TabsClient'; import FollowMasterClient from '../client/FollowMasterClient'; import { LinkHint, InputHint } from '../presenters/Hint'; -import Key from '../domains/Key'; +import Key from '../../shared/settings/Key'; interface Size { width: number; diff --git a/src/content/usecases/KeymapUseCase.ts b/src/content/usecases/KeymapUseCase.ts index 62cd04c..495f6d0 100644 --- a/src/content/usecases/KeymapUseCase.ts +++ b/src/content/usecases/KeymapUseCase.ts @@ -3,9 +3,9 @@ import KeymapRepository from '../repositories/KeymapRepository'; import SettingRepository from '../repositories/SettingRepository'; import AddonEnabledRepository from '../repositories/AddonEnabledRepository'; import * as operations from '../../shared/operations'; -import Key from '../domains/Key'; -import KeySequence from '../domains/KeySequence'; import Keymaps from '../../shared/settings/Keymaps'; +import Key from '../../shared/settings/Key'; +import KeySequence from '../../shared/settings/KeySequence'; type KeymapEntityMap = Map; @@ -66,10 +66,9 @@ export default class KeymapUseCase { private keymapEntityMap(): KeymapEntityMap { let keymaps = this.settingRepository.get().keymaps.combine(reservedKeymaps); - let entries = keymaps.entries().map(entry => [ - KeySequence.fromMapKeys(entry[0]), - entry[1], - ]) as [KeySequence, operations.Operation][]; + let entries = keymaps.entries().map( + ([keys, op]) => [KeySequence.fromMapKeys(keys), op] + ) as [KeySequence, operations.Operation][]; return new Map(entries); } } diff --git a/src/shared/settings/Key.ts b/src/shared/settings/Key.ts new file mode 100644 index 0000000..b11eeb2 --- /dev/null +++ b/src/shared/settings/Key.ts @@ -0,0 +1,61 @@ +export default class Key { + public readonly key: string; + + public readonly shift: boolean; + + public readonly ctrl: boolean; + + public readonly alt: boolean; + + public readonly meta: boolean; + + constructor({ key, shift, ctrl, alt, meta }: { + key: string; + shift: boolean; + ctrl: boolean; + alt: boolean; + meta: boolean; + }) { + this.key = key; + this.shift = shift; + this.ctrl = ctrl; + this.alt = alt; + this.meta = meta; + } + + static fromMapKey(str: string): Key { + if (str.startsWith('<') && str.endsWith('>')) { + let inner = str.slice(1, -1); + let shift = inner.includes('S-'); + let base = inner.slice(inner.lastIndexOf('-') + 1); + if (shift && base.length === 1) { + base = base.toUpperCase(); + } else if (!shift && base.length === 1) { + base = base.toLowerCase(); + } + return new Key({ + key: base, + shift: shift, + ctrl: inner.includes('C-'), + alt: inner.includes('A-'), + meta: inner.includes('M-'), + }); + } + + return new Key({ + key: str, + shift: str.toLowerCase() !== str, + ctrl: false, + alt: false, + meta: false, + }); + } + + equals(key: Key) { + return this.key === key.key && + this.ctrl === key.ctrl && + this.meta === key.meta && + this.alt === key.alt && + this.shift === key.shift; + } +} diff --git a/src/shared/settings/KeySequence.ts b/src/shared/settings/KeySequence.ts new file mode 100644 index 0000000..4955583 --- /dev/null +++ b/src/shared/settings/KeySequence.ts @@ -0,0 +1,54 @@ +import Key from '../../shared/settings/Key'; + +export default class KeySequence { + constructor( + public readonly keys: Key[], + ) { + } + + push(key: Key): number { + return this.keys.push(key); + } + + length(): number { + return this.keys.length; + } + + startsWith(o: KeySequence): boolean { + if (this.keys.length < o.keys.length) { + return false; + } + for (let i = 0; i < o.keys.length; ++i) { + if (!this.keys[i].equals(o.keys[i])) { + return false; + } + } + return true; + } + + static fromMapKeys(keys: string): KeySequence { + const fromMapKeysRecursive = ( + remaining: string, mappedKeys: Key[], + ): Key[] => { + if (remaining.length === 0) { + return mappedKeys; + } + + let nextPos = 1; + if (remaining.startsWith('<')) { + let ltPos = remaining.indexOf('>'); + if (ltPos > 0) { + nextPos = ltPos + 1; + } + } + + return fromMapKeysRecursive( + remaining.slice(nextPos), + mappedKeys.concat([Key.fromMapKey(remaining.slice(0, nextPos))]) + ); + }; + + let data = fromMapKeysRecursive(keys, []); + return new KeySequence(data); + } +} diff --git a/test/content/InputDriver.test.ts b/test/content/InputDriver.test.ts index b39312c..441d107 100644 --- a/test/content/InputDriver.test.ts +++ b/test/content/InputDriver.test.ts @@ -1,6 +1,6 @@ -import InputDriver from '../../src/content/InputDriver'; +import InputDriver, {keyFromKeyboardEvent} from '../../src/content/InputDriver'; import { expect } from 'chai'; -import Key from '../../src/content/domains/Key'; +import Key from '../../src/shared/settings/Key'; describe('InputDriver', () => { let target: HTMLElement; @@ -127,3 +127,50 @@ describe('InputDriver', () => { div.dispatchEvent(new KeyboardEvent('keydown', { key: 'x' })); }); }); + +describe("#keyFromKeyboardEvent", () => { + it('returns from keyboard input Ctrl+X', () => { + let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', { + key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true, + })); + expect(k.key).to.equal('x'); + expect(k.shift).to.be.false; + expect(k.ctrl).to.be.true; + expect(k.alt).to.be.false; + expect(k.meta).to.be.true; + }); + + it('returns from keyboard input Shift+Esc', () => { + let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', { + key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true + })); + expect(k.key).to.equal('Esc'); + expect(k.shift).to.be.true; + expect(k.ctrl).to.be.false; + expect(k.alt).to.be.false; + expect(k.meta).to.be.true; + }); + + it('returns from keyboard input Ctrl+$', () => { + // $ required shift pressing on most keyboards + let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', { + key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false + })); + expect(k.key).to.equal('$'); + expect(k.shift).to.be.false; + expect(k.ctrl).to.be.true; + expect(k.alt).to.be.false; + expect(k.meta).to.be.false; + }); + + it('returns from keyboard input Crtl+Space', () => { + let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', { + key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false + })); + expect(k.key).to.equal('Space'); + expect(k.shift).to.be.false; + expect(k.ctrl).to.be.true; + expect(k.alt).to.be.false; + expect(k.meta).to.be.false; + }); +}); diff --git a/test/content/domains/Key.test.ts b/test/content/domains/Key.test.ts deleted file mode 100644 index 8e62f80..0000000 --- a/test/content/domains/Key.test.ts +++ /dev/null @@ -1,139 +0,0 @@ -import Key from '../../../src/content/domains/Key'; -import { expect } from 'chai' - -describe("Key", () => { - describe('fromKeyboardEvent', () => { - it('returns from keyboard input Ctrl+X', () => { - let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { - key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true, - })); - expect(k.key).to.equal('x'); - expect(k.shift).to.be.false; - expect(k.ctrl).to.be.true; - expect(k.alt).to.be.false; - expect(k.meta).to.be.true; - }); - - it('returns from keyboard input Shift+Esc', () => { - let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { - key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true - })); - expect(k.key).to.equal('Esc'); - expect(k.shift).to.be.true; - expect(k.ctrl).to.be.false; - expect(k.alt).to.be.false; - expect(k.meta).to.be.true; - }); - - it('returns from keyboard input Ctrl+$', () => { - // $ required shift pressing on most keyboards - let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { - key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false - })); - expect(k.key).to.equal('$'); - expect(k.shift).to.be.false; - expect(k.ctrl).to.be.true; - expect(k.alt).to.be.false; - expect(k.meta).to.be.false; - }); - - it('returns from keyboard input Crtl+Space', () => { - let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { - key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false - })); - expect(k.key).to.equal('Space'); - expect(k.shift).to.be.false; - expect(k.ctrl).to.be.true; - expect(k.alt).to.be.false; - expect(k.meta).to.be.false; - }); - }); - - describe('fromMapKey', () => { - it('return for X', () => { - let key = Key.fromMapKey('x'); - expect(key.key).to.equal('x'); - expect(key.shift).to.be.false; - expect(key.ctrl).to.be.false; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - - it('return for Shift+X', () => { - let key = Key.fromMapKey('X'); - expect(key.key).to.equal('X'); - expect(key.shift).to.be.true; - expect(key.ctrl).to.be.false; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - - it('return for Ctrl+X', () => { - let key = Key.fromMapKey(''); - expect(key.key).to.equal('x'); - expect(key.shift).to.be.false; - expect(key.ctrl).to.be.true; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - - it('returns for Ctrl+Meta+X', () => { - let key = Key.fromMapKey(''); - expect(key.key).to.equal('x'); - expect(key.shift).to.be.false; - expect(key.ctrl).to.be.true; - expect(key.alt).to.be.false; - expect(key.meta).to.be.true; - }); - - it('returns for Ctrl+Shift+x', () => { - let key = Key.fromMapKey(''); - expect(key.key).to.equal('X'); - expect(key.shift).to.be.true; - expect(key.ctrl).to.be.true; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - - it('returns for Shift+Esc', () => { - let key = Key.fromMapKey(''); - expect(key.key).to.equal('Esc'); - expect(key.shift).to.be.true; - expect(key.ctrl).to.be.false; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - - it('returns for Ctrl+Esc', () => { - let key = Key.fromMapKey(''); - expect(key.key).to.equal('Esc'); - expect(key.shift).to.be.false; - expect(key.ctrl).to.be.true; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - - it('returns for Ctrl+Esc', () => { - let key = Key.fromMapKey(''); - expect(key.key).to.equal('Space'); - expect(key.shift).to.be.false; - expect(key.ctrl).to.be.true; - expect(key.alt).to.be.false; - expect(key.meta).to.be.false; - }); - }); - - describe('equals', () => { - expect(new Key({ - key: 'x', shift: false, ctrl: true, alt: false, meta: false, - }).equals(new Key({ - key: 'x', shift: false, ctrl: true, alt: false, meta: false, - }))).to.be.true; - - expect(new Key({ - key: 'x', shift: false, ctrl: false, alt: false, meta: false, - }).equals(new Key({ - key: 'X', shift: true, ctrl: false, alt: false, meta: false, - }))).to.be.false; - }); -}); diff --git a/test/content/domains/KeySequence.test.ts b/test/content/domains/KeySequence.test.ts deleted file mode 100644 index 62af165..0000000 --- a/test/content/domains/KeySequence.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import KeySequence from '../../../src/content/domains/KeySequence'; -import Key from '../../../src/content/domains/Key'; -import { expect } from 'chai' - -describe("KeySequence", () => { - describe('#push', () => { - it('append a key to the sequence', () => { - let seq = new KeySequence([]); - seq.push(Key.fromMapKey('g')); - seq.push(Key.fromMapKey('')); - - expect(seq.keys[0].key).to.equal('g'); - expect(seq.keys[1].key).to.equal('U'); - expect(seq.keys[1].shift).to.be.true; - }) - }); - - describe('#startsWith', () => { - it('returns true if the key sequence starts with param', () => { - let seq = new KeySequence([ - Key.fromMapKey('g'), - Key.fromMapKey(''), - ]); - - expect(seq.startsWith(new KeySequence([ - ]))).to.be.true; - expect(seq.startsWith(new KeySequence([ - Key.fromMapKey('g'), - ]))).to.be.true; - expect(seq.startsWith(new KeySequence([ - Key.fromMapKey('g'), Key.fromMapKey(''), - ]))).to.be.true; - expect(seq.startsWith(new KeySequence([ - Key.fromMapKey('g'), Key.fromMapKey(''), Key.fromMapKey('x'), - ]))).to.be.false; - expect(seq.startsWith(new KeySequence([ - Key.fromMapKey('h'), - ]))).to.be.false; - }); - - it('returns true if the empty sequence starts with an empty sequence', () => { - let seq = new KeySequence([]); - - expect(seq.startsWith(new KeySequence([]))).to.be.true; - expect(seq.startsWith(new KeySequence([ - Key.fromMapKey('h'), - ]))).to.be.false; - }) - }); - - describe('#fromMapKeys', () => { - it('returns mapped keys for Shift+Esc', () => { - let keys = KeySequence.fromMapKeys('').keys; - expect(keys).to.have.lengthOf(1); - expect(keys[0].key).to.equal('Esc'); - expect(keys[0].shift).to.be.true; - }); - - it('returns mapped keys for ad', () => { - let keys = KeySequence.fromMapKeys('ad').keys; - expect(keys).to.have.lengthOf(5); - expect(keys[0].key).to.equal('a'); - expect(keys[1].ctrl).to.be.true; - expect(keys[1].key).to.equal('b'); - expect(keys[2].alt).to.be.true; - expect(keys[2].key).to.equal('c'); - expect(keys[3].key).to.equal('d'); - expect(keys[4].meta).to.be.true; - expect(keys[4].key).to.equal('e'); - }); - }) -}); diff --git a/test/content/repositories/KeymapRepository.test.ts b/test/content/repositories/KeymapRepository.test.ts index da5624e..df013df 100644 --- a/test/content/repositories/KeymapRepository.test.ts +++ b/test/content/repositories/KeymapRepository.test.ts @@ -1,7 +1,7 @@ import KeymapRepository, { KeymapRepositoryImpl } from '../../../src/content/repositories/KeymapRepository'; -import Key from '../../../src/content/domains/Key' import { expect } from 'chai'; +import Key from "../../../src/shared/settings/Key"; describe('KeymapRepositoryImpl', () => { let sut: KeymapRepository; diff --git a/test/shared/settings/Key.test.ts b/test/shared/settings/Key.test.ts new file mode 100644 index 0000000..8222d5a --- /dev/null +++ b/test/shared/settings/Key.test.ts @@ -0,0 +1,92 @@ +import { expect } from 'chai' +import Key from '../../../src/shared/settings/Key'; + +describe("Key", () => { + describe('fromMapKey', () => { + it('return for X', () => { + let key = Key.fromMapKey('x'); + expect(key.key).to.equal('x'); + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.false; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + + it('return for Shift+X', () => { + let key = Key.fromMapKey('X'); + expect(key.key).to.equal('X'); + expect(key.shift).to.be.true; + expect(key.ctrl).to.be.false; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + + it('return for Ctrl+X', () => { + let key = Key.fromMapKey(''); + expect(key.key).to.equal('x'); + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + + it('returns for Ctrl+Meta+X', () => { + let key = Key.fromMapKey(''); + expect(key.key).to.equal('x'); + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.true; + }); + + it('returns for Ctrl+Shift+x', () => { + let key = Key.fromMapKey(''); + expect(key.key).to.equal('X'); + expect(key.shift).to.be.true; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + + it('returns for Shift+Esc', () => { + let key = Key.fromMapKey(''); + expect(key.key).to.equal('Esc'); + expect(key.shift).to.be.true; + expect(key.ctrl).to.be.false; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + + it('returns for Ctrl+Esc', () => { + let key = Key.fromMapKey(''); + expect(key.key).to.equal('Esc'); + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + + it('returns for Ctrl+Esc', () => { + let key = Key.fromMapKey(''); + expect(key.key).to.equal('Space'); + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; + }); + }); + + describe('equals', () => { + expect(new Key({ + key: 'x', shift: false, ctrl: true, alt: false, meta: false, + }).equals(new Key({ + key: 'x', shift: false, ctrl: true, alt: false, meta: false, + }))).to.be.true; + + expect(new Key({ + key: 'x', shift: false, ctrl: false, alt: false, meta: false, + }).equals(new Key({ + key: 'X', shift: true, ctrl: false, alt: false, meta: false, + }))).to.be.false; + }); +}); diff --git a/test/shared/settings/KeySequence.test.ts b/test/shared/settings/KeySequence.test.ts new file mode 100644 index 0000000..361cbd1 --- /dev/null +++ b/test/shared/settings/KeySequence.test.ts @@ -0,0 +1,72 @@ +import KeySequence from '../../../src/shared/settings/KeySequence'; +import { expect } from 'chai' +import Key from "../../../src/shared/settings/Key"; + +describe("KeySequence", () => { + describe('#push', () => { + it('append a key to the sequence', () => { + let seq = new KeySequence([]); + seq.push(Key.fromMapKey('g')); + seq.push(Key.fromMapKey('')); + + expect(seq.keys[0].key).to.equal('g'); + expect(seq.keys[1].key).to.equal('U'); + expect(seq.keys[1].shift).to.be.true; + }) + }); + + describe('#startsWith', () => { + it('returns true if the key sequence starts with param', () => { + let seq = new KeySequence([ + Key.fromMapKey('g'), + Key.fromMapKey(''), + ]); + + expect(seq.startsWith(new KeySequence([ + ]))).to.be.true; + expect(seq.startsWith(new KeySequence([ + Key.fromMapKey('g'), + ]))).to.be.true; + expect(seq.startsWith(new KeySequence([ + Key.fromMapKey('g'), Key.fromMapKey(''), + ]))).to.be.true; + expect(seq.startsWith(new KeySequence([ + Key.fromMapKey('g'), Key.fromMapKey(''), Key.fromMapKey('x'), + ]))).to.be.false; + expect(seq.startsWith(new KeySequence([ + Key.fromMapKey('h'), + ]))).to.be.false; + }); + + it('returns true if the empty sequence starts with an empty sequence', () => { + let seq = new KeySequence([]); + + expect(seq.startsWith(new KeySequence([]))).to.be.true; + expect(seq.startsWith(new KeySequence([ + Key.fromMapKey('h'), + ]))).to.be.false; + }) + }); + + describe('#fromMapKeys', () => { + it('returns mapped keys for Shift+Esc', () => { + let keys = KeySequence.fromMapKeys('').keys; + expect(keys).to.have.lengthOf(1); + expect(keys[0].key).to.equal('Esc'); + expect(keys[0].shift).to.be.true; + }); + + it('returns mapped keys for ad', () => { + let keys = KeySequence.fromMapKeys('ad').keys; + expect(keys).to.have.lengthOf(5); + expect(keys[0].key).to.equal('a'); + expect(keys[1].ctrl).to.be.true; + expect(keys[1].key).to.equal('b'); + expect(keys[2].alt).to.be.true; + expect(keys[2].key).to.equal('c'); + expect(keys[3].key).to.equal('d'); + expect(keys[4].meta).to.be.true; + expect(keys[4].key).to.equal('e'); + }); + }) +}); -- cgit v1.2.3