1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {checkHotkey, formatHotkey, Hotkey, parseHotkey} from './hotkeys'; 16 17test('parseHotkey', () => { 18 expect(parseHotkey('A')).toEqual({ 19 key: 'A', 20 allowInEditable: false, 21 modifier: '', 22 }); 23 expect(parseHotkey('!A')).toEqual({ 24 key: 'A', 25 allowInEditable: true, 26 modifier: '', 27 }); 28 expect(parseHotkey('Shift+A')).toEqual({ 29 key: 'A', 30 allowInEditable: false, 31 modifier: 'Shift+', 32 }); 33 expect(parseHotkey('Mod+Shift+A')).toEqual({ 34 key: 'A', 35 allowInEditable: false, 36 modifier: 'Mod+Shift+', 37 }); 38 expect(parseHotkey('!Mod+Shift+A')).toEqual({ 39 key: 'A', 40 allowInEditable: true, 41 modifier: 'Mod+Shift+', 42 }); 43}); 44 45describe('checkHotkey', () => { 46 test('A', () => { 47 expect(checkHotkey('A', {key: 'a'})).toBe(true); 48 expect(checkHotkey('A', {key: 'A', shiftKey: true})).toBe(false); 49 expect(checkHotkey('A', {key: 'a', ctrlKey: true})).toBe(false); 50 expect(checkHotkey('A', {key: 'a', altKey: true})).toBe(false); 51 }); 52 53 test('Special', () => { 54 expect(checkHotkey('Enter', {key: 'Enter'})).toBe(true); 55 expect(checkHotkey('Escape', {key: 'Escape'})).toBe(true); 56 }); 57 58 test('Shift+A', () => { 59 const hotkey: Hotkey = 'Shift+A'; 60 expect(checkHotkey(hotkey, {key: 'a'})).toBe(false); 61 expect(checkHotkey(hotkey, {key: 'A', shiftKey: true})).toBe(true); 62 expect(checkHotkey(hotkey, {key: 'a', ctrlKey: true})).toBe(false); 63 expect(checkHotkey(hotkey, {key: 'a', altKey: true})).toBe(false); 64 }); 65 66 test('Mod+A on PC', () => { 67 const hotkey: Hotkey = 'Mod+A'; 68 expect(checkHotkey(hotkey, {key: 'a'}, 'PC')).toBe(false); 69 expect(checkHotkey(hotkey, {key: 'A', shiftKey: true}, 'PC')).toBe(false); 70 expect(checkHotkey(hotkey, {key: 'a', ctrlKey: true}, 'PC')).toBe(true); 71 expect(checkHotkey(hotkey, {key: 'a', metaKey: true}, 'PC')).toBe(false); 72 }); 73 74 test('Mod+A on Mac', () => { 75 const hotkey: Hotkey = 'Mod+A'; 76 expect(checkHotkey(hotkey, {key: 'a'}, 'Mac')).toBe(false); 77 expect(checkHotkey(hotkey, {key: 'A', shiftKey: true}, 'Mac')).toBe(false); 78 expect(checkHotkey(hotkey, {key: 'a', ctrlKey: true}, 'Mac')).toBe(false); 79 expect(checkHotkey(hotkey, {key: 'a', metaKey: true}, 'Mac')).toBe(true); 80 }); 81 82 test('allow in editable', () => { 83 const el = document.createElement('input'); 84 el.setAttribute('type', 'text'); 85 86 expect(checkHotkey('X', {key: 'x'})).toBe(true); 87 expect(checkHotkey('!X', {key: 'x'})).toBe(true); 88 89 expect(checkHotkey('X', {key: 'x', target: el})).toBe(false); 90 expect(checkHotkey('!X', {key: 'x', target: el})).toBe(true); 91 }); 92}); 93 94test('formatHotkey', () => { 95 expect(formatHotkey('Mod+X', 'Mac')).toEqual('⌘X'); 96 expect(formatHotkey('Mod+Shift+X', 'Mac')).toEqual('⌘⇧X'); 97 98 expect(formatHotkey('Mod+X', 'PC')).toEqual('Ctrl+X'); 99 expect(formatHotkey('Mod+Shift+X', 'PC')).toEqual('Ctrl+Shift+X'); 100}); 101