yomichan/test/test-hotkey-util.js
toasted-nutbread 6a271e067f
Move mixed/js (#1383)
* Move mixed/js/core.js to js/core.js

* Move mixed/js/yomichan.js to js/yomichan.js

* Move mixed/js/timer.js to js/debug/timer.js

* Move mixed/js/hotkey-handler.js to js/input/hotkey-handler.js

* Move mixed/js/hotkey-help-controller.js to js/input/hotkey-help-controller.js

* Move mixed/js/hotkey-util.js to js/input/hotkey-util.js

* Move mixed/js/audio-system.js to js/input/audio-system.js

* Move mixed/js/media-loader.js to js/input/media-loader.js

* Move mixed/js/text-to-speech-audio.js to js/input/text-to-speech-audio.js

* Move mixed/js/comm.js to js/comm/cross-frame-api.js

* Move mixed/js/api.js to js/comm/api.js

* Move mixed/js/frame-client.js to js/comm/frame-client.js

* Move mixed/js/frame-endpoint.js to js/comm/frame-endpoint.js

* Move mixed/js/display.js to js/display/display.js

* Move mixed/js/display-audio.js to js/display/display-audio.js

* Move mixed/js/display-generator.js to js/display/display-generator.js

* Move mixed/js/display-history.js to js/display/display-history.js

* Move mixed/js/display-notification.js to js/display/display-notification.js

* Move mixed/js/display-profile-selection.js to js/display/display-profile-selection.js

* Move mixed/js/japanese.js to js/language/japanese-util.js

* Move mixed/js/dictionary-data-util.js to js/language/dictionary-data-util.js

* Move mixed/js/document-focus-controller.js to js/dom/document-focus-controller.js

* Move mixed/js/document-util.js to js/dom/document-util.js

* Move mixed/js/dom-data-binder.js to js/dom/dom-data-binder.js

* Move mixed/js/html-template-collection.js to js/dom/html-template-collection.js

* Move mixed/js/panel-element.js to js/dom/panel-element.js

* Move mixed/js/popup-menu.js to js/dom/popup-menu.js

* Move mixed/js/selector-observer.js to js/dom/selector-observer.js

* Move mixed/js/scroll.js to js/dom/window-scroll.js

* Move mixed/js/text-scanner.js to js/language/text-scanner.js

* Move mixed/js/cache-map.js to js/general/cache-map.js

* Move mixed/js/object-property-accessor.js to js/general/object-property-accessor.js

* Move mixed/js/task-accumulator.js to js/general/task-accumulator.js

* Move mixed/js/environment.js to js/background/environment.js

* Move mixed/js/dynamic-loader.js to js/scripting/dynamic-loader.js

* Move mixed/js/dynamic-loader-sentinel.js to js/scripting/dynamic-loader-sentinel.js
2021-02-13 22:52:28 -05:00

173 lines
9.5 KiB
JavaScript

/*
* Copyright (C) 2020-2021 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const assert = require('assert');
const {testMain} = require('../dev/util');
const {VM} = require('../dev/vm');
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
function createHotkeyUtil() {
const vm = new VM();
vm.execute(['js/input/hotkey-util.js']);
const [HotkeyUtil] = vm.get(['HotkeyUtil']);
return new HotkeyUtil();
}
function testCommandConversions() {
const data = [
{os: 'win', command: 'Alt+F', expectedCommand: 'Alt+F', expectedInput: {key: 'KeyF', modifiers: ['alt']}},
{os: 'win', command: 'F1', expectedCommand: 'F1', expectedInput: {key: 'F1', modifiers: []}},
{os: 'win', command: 'Ctrl+Alt+Shift+F1', expectedCommand: 'Ctrl+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['ctrl', 'alt', 'shift']}},
{os: 'win', command: 'MacCtrl+Alt+Shift+F1', expectedCommand: 'Ctrl+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['ctrl', 'alt', 'shift']}},
{os: 'win', command: 'Command+Alt+Shift+F1', expectedCommand: 'Command+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['meta', 'alt', 'shift']}},
{os: 'mac', command: 'Ctrl+Alt+Shift+F1', expectedCommand: 'Command+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['meta', 'alt', 'shift']}},
{os: 'mac', command: 'MacCtrl+Alt+Shift+F1', expectedCommand: 'MacCtrl+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['ctrl', 'alt', 'shift']}},
{os: 'mac', command: 'Command+Alt+Shift+F1', expectedCommand: 'Command+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['meta', 'alt', 'shift']}},
{os: 'linux', command: 'Ctrl+Alt+Shift+F1', expectedCommand: 'Ctrl+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['ctrl', 'alt', 'shift']}},
{os: 'linux', command: 'MacCtrl+Alt+Shift+F1', expectedCommand: 'Ctrl+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['ctrl', 'alt', 'shift']}},
{os: 'linux', command: 'Command+Alt+Shift+F1', expectedCommand: 'Command+Alt+Shift+F1', expectedInput: {key: 'F1', modifiers: ['meta', 'alt', 'shift']}}
];
const hotkeyUtil = createHotkeyUtil();
for (const {command, os, expectedInput, expectedCommand} of data) {
hotkeyUtil.os = os;
const input = clone(hotkeyUtil.convertCommandToInput(command));
assert.deepStrictEqual(input, expectedInput);
const command2 = hotkeyUtil.convertInputToCommand(input.key, input.modifiers);
assert.deepStrictEqual(command2, expectedCommand);
}
}
function testDisplayNames() {
const data = [
{os: 'win', key: null, modifiers: [], expected: ''},
{os: 'win', key: 'KeyF', modifiers: [], expected: 'F'},
{os: 'win', key: 'F1', modifiers: [], expected: 'F1'},
{os: 'win', key: null, modifiers: ['ctrl'], expected: 'Ctrl'},
{os: 'win', key: 'KeyF', modifiers: ['ctrl'], expected: 'Ctrl + F'},
{os: 'win', key: 'F1', modifiers: ['ctrl'], expected: 'Ctrl + F1'},
{os: 'win', key: null, modifiers: ['alt'], expected: 'Alt'},
{os: 'win', key: 'KeyF', modifiers: ['alt'], expected: 'Alt + F'},
{os: 'win', key: 'F1', modifiers: ['alt'], expected: 'Alt + F1'},
{os: 'win', key: null, modifiers: ['shift'], expected: 'Shift'},
{os: 'win', key: 'KeyF', modifiers: ['shift'], expected: 'Shift + F'},
{os: 'win', key: 'F1', modifiers: ['shift'], expected: 'Shift + F1'},
{os: 'win', key: null, modifiers: ['meta'], expected: 'Windows'},
{os: 'win', key: 'KeyF', modifiers: ['meta'], expected: 'Windows + F'},
{os: 'win', key: 'F1', modifiers: ['meta'], expected: 'Windows + F1'},
{os: 'win', key: null, modifiers: ['mouse1'], expected: 'Mouse 1'},
{os: 'win', key: 'KeyF', modifiers: ['mouse1'], expected: 'Mouse 1 + F'},
{os: 'win', key: 'F1', modifiers: ['mouse1'], expected: 'Mouse 1 + F1'},
{os: 'mac', key: null, modifiers: [], expected: ''},
{os: 'mac', key: 'KeyF', modifiers: [], expected: 'F'},
{os: 'mac', key: 'F1', modifiers: [], expected: 'F1'},
{os: 'mac', key: null, modifiers: ['ctrl'], expected: 'Ctrl'},
{os: 'mac', key: 'KeyF', modifiers: ['ctrl'], expected: 'Ctrl + F'},
{os: 'mac', key: 'F1', modifiers: ['ctrl'], expected: 'Ctrl + F1'},
{os: 'mac', key: null, modifiers: ['alt'], expected: 'Opt'},
{os: 'mac', key: 'KeyF', modifiers: ['alt'], expected: 'Opt + F'},
{os: 'mac', key: 'F1', modifiers: ['alt'], expected: 'Opt + F1'},
{os: 'mac', key: null, modifiers: ['shift'], expected: 'Shift'},
{os: 'mac', key: 'KeyF', modifiers: ['shift'], expected: 'Shift + F'},
{os: 'mac', key: 'F1', modifiers: ['shift'], expected: 'Shift + F1'},
{os: 'mac', key: null, modifiers: ['meta'], expected: 'Cmd'},
{os: 'mac', key: 'KeyF', modifiers: ['meta'], expected: 'Cmd + F'},
{os: 'mac', key: 'F1', modifiers: ['meta'], expected: 'Cmd + F1'},
{os: 'mac', key: null, modifiers: ['mouse1'], expected: 'Mouse 1'},
{os: 'mac', key: 'KeyF', modifiers: ['mouse1'], expected: 'Mouse 1 + F'},
{os: 'mac', key: 'F1', modifiers: ['mouse1'], expected: 'Mouse 1 + F1'},
{os: 'linux', key: null, modifiers: [], expected: ''},
{os: 'linux', key: 'KeyF', modifiers: [], expected: 'F'},
{os: 'linux', key: 'F1', modifiers: [], expected: 'F1'},
{os: 'linux', key: null, modifiers: ['ctrl'], expected: 'Ctrl'},
{os: 'linux', key: 'KeyF', modifiers: ['ctrl'], expected: 'Ctrl + F'},
{os: 'linux', key: 'F1', modifiers: ['ctrl'], expected: 'Ctrl + F1'},
{os: 'linux', key: null, modifiers: ['alt'], expected: 'Alt'},
{os: 'linux', key: 'KeyF', modifiers: ['alt'], expected: 'Alt + F'},
{os: 'linux', key: 'F1', modifiers: ['alt'], expected: 'Alt + F1'},
{os: 'linux', key: null, modifiers: ['shift'], expected: 'Shift'},
{os: 'linux', key: 'KeyF', modifiers: ['shift'], expected: 'Shift + F'},
{os: 'linux', key: 'F1', modifiers: ['shift'], expected: 'Shift + F1'},
{os: 'linux', key: null, modifiers: ['meta'], expected: 'Super'},
{os: 'linux', key: 'KeyF', modifiers: ['meta'], expected: 'Super + F'},
{os: 'linux', key: 'F1', modifiers: ['meta'], expected: 'Super + F1'},
{os: 'linux', key: null, modifiers: ['mouse1'], expected: 'Mouse 1'},
{os: 'linux', key: 'KeyF', modifiers: ['mouse1'], expected: 'Mouse 1 + F'},
{os: 'linux', key: 'F1', modifiers: ['mouse1'], expected: 'Mouse 1 + F1'},
{os: 'unknown', key: null, modifiers: [], expected: ''},
{os: 'unknown', key: 'KeyF', modifiers: [], expected: 'F'},
{os: 'unknown', key: 'F1', modifiers: [], expected: 'F1'},
{os: 'unknown', key: null, modifiers: ['ctrl'], expected: 'Ctrl'},
{os: 'unknown', key: 'KeyF', modifiers: ['ctrl'], expected: 'Ctrl + F'},
{os: 'unknown', key: 'F1', modifiers: ['ctrl'], expected: 'Ctrl + F1'},
{os: 'unknown', key: null, modifiers: ['alt'], expected: 'Alt'},
{os: 'unknown', key: 'KeyF', modifiers: ['alt'], expected: 'Alt + F'},
{os: 'unknown', key: 'F1', modifiers: ['alt'], expected: 'Alt + F1'},
{os: 'unknown', key: null, modifiers: ['shift'], expected: 'Shift'},
{os: 'unknown', key: 'KeyF', modifiers: ['shift'], expected: 'Shift + F'},
{os: 'unknown', key: 'F1', modifiers: ['shift'], expected: 'Shift + F1'},
{os: 'unknown', key: null, modifiers: ['meta'], expected: 'Meta'},
{os: 'unknown', key: 'KeyF', modifiers: ['meta'], expected: 'Meta + F'},
{os: 'unknown', key: 'F1', modifiers: ['meta'], expected: 'Meta + F1'},
{os: 'unknown', key: null, modifiers: ['mouse1'], expected: 'Mouse 1'},
{os: 'unknown', key: 'KeyF', modifiers: ['mouse1'], expected: 'Mouse 1 + F'},
{os: 'unknown', key: 'F1', modifiers: ['mouse1'], expected: 'Mouse 1 + F1'}
];
const hotkeyUtil = createHotkeyUtil();
for (const {os, key, modifiers, expected} of data) {
hotkeyUtil.os = os;
const displayName = hotkeyUtil.getInputDisplayValue(key, modifiers);
assert.deepStrictEqual(displayName, expected);
}
}
function testSortModifiers() {
const data = [
{modifiers: [], expected: []},
{modifiers: ['shift', 'alt', 'ctrl', 'mouse4', 'meta', 'mouse1', 'mouse0'], expected: ['meta', 'ctrl', 'alt', 'shift', 'mouse0', 'mouse1', 'mouse4']}
];
const hotkeyUtil = createHotkeyUtil();
for (const {modifiers, expected} of data) {
const modifiers2 = hotkeyUtil.sortModifiers(modifiers);
assert.strictEqual(modifiers2, modifiers);
assert.deepStrictEqual(modifiers2, expected);
}
}
function main() {
testCommandConversions();
testDisplayNames();
testSortModifiers();
}
if (require.main === module) { testMain(main); }