Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import {IXosKeyboardShortcutService, XosKeyboardShortcut, IXosKeyboardShortcutBinding} from './keyboard-shortcut'; |
| 22 | import {IXosSidePanelService} from '../side-panel/side-panel.service'; |
| 23 | |
| 24 | let service: IXosKeyboardShortcutService; |
| 25 | let $log: ng.ILogService; |
| 26 | let $transitions: any; |
| 27 | let XosSidePanel: IXosSidePanelService; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 28 | let logSpy: any; |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 29 | |
| 30 | const baseGlobalModifiers: IXosKeyboardShortcutBinding[] = [ |
| 31 | { |
| 32 | key: 'a', |
| 33 | cb: 'cb' |
| 34 | }, |
| 35 | { |
| 36 | key: 'a', |
| 37 | cb: 'modified', |
| 38 | modifiers: ['alt'] |
| 39 | }, |
| 40 | { |
| 41 | key: 'a', |
| 42 | cb: 'modified', |
| 43 | modifiers: ['meta'] |
| 44 | } |
| 45 | ]; |
| 46 | |
| 47 | const baseLocalModifiers: IXosKeyboardShortcutBinding[] = [ |
| 48 | { |
| 49 | key: 'b', |
| 50 | cb: 'cb' |
| 51 | }, |
| 52 | { |
| 53 | key: 'b', |
| 54 | cb: 'modified', |
| 55 | modifiers: ['meta', 'alt'] |
| 56 | } |
| 57 | ]; |
| 58 | |
| 59 | describe('The XosKeyboardShortcut service', () => { |
| 60 | |
| 61 | beforeEach(() => { |
| 62 | angular.module('leyBinding', ['ui.router']) |
| 63 | .service('XosKeyboardShortcut', XosKeyboardShortcut) |
| 64 | .value('XosSidePanel', { |
| 65 | |
| 66 | }); |
| 67 | angular.mock.module('leyBinding'); |
| 68 | |
| 69 | angular.mock.inject(( |
| 70 | _$log_: ng.ILogService, |
| 71 | _$transitions_: any, |
| 72 | _XosSidePanel_: IXosSidePanelService |
| 73 | ) => { |
| 74 | $log = _$log_; |
| 75 | $transitions = _$transitions_; |
| 76 | XosSidePanel = _XosSidePanel_; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 77 | logSpy = spyOn($log, 'warn'); |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 78 | }); |
| 79 | |
| 80 | service = new XosKeyboardShortcut($log, $transitions, XosSidePanel); |
| 81 | }); |
| 82 | |
| 83 | it('should have a setup method', () => { |
| 84 | expect(service.setup).toBeDefined(); |
| 85 | }); |
| 86 | |
| 87 | describe('the addActiveModifierKey method', () => { |
| 88 | beforeEach(() => { |
| 89 | service['activeModifiers'] = []; |
| 90 | }); |
| 91 | it('should add an active modifier', () => { |
| 92 | service['addActiveModifierKey']('shift'); |
| 93 | expect(service['activeModifiers']).toEqual(['shift']); |
| 94 | }); |
| 95 | |
| 96 | it('should not add a modifier twice', () => { |
| 97 | service['addActiveModifierKey']('shift'); |
| 98 | service['addActiveModifierKey']('shift'); |
| 99 | expect(service['activeModifiers']).toEqual(['shift']); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe('the removeActiveModifierKey method', () => { |
| 104 | beforeEach(() => { |
| 105 | service['activeModifiers'] = ['shift', 'meta']; |
| 106 | }); |
| 107 | it('should remove an active modifier', () => { |
| 108 | service['removeActiveModifierKey']('shift'); |
| 109 | expect(service['activeModifiers']).toEqual(['meta']); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe('the findBindedShortcut method', () => { |
| 114 | beforeEach(() => { |
| 115 | service['activeModifiers'] = []; |
| 116 | service['keyMapping']['global'] = baseGlobalModifiers; |
| 117 | service['keyMapping']['view'] = baseLocalModifiers; |
| 118 | }); |
| 119 | |
| 120 | it('should find a global keybinding', () => { |
| 121 | const binding = service['findBindedShortcut']('a'); |
| 122 | expect(binding).toEqual({key: 'a', cb: 'cb'}); |
| 123 | }); |
| 124 | |
| 125 | it('should find a global keybinding with modifiers', () => { |
| 126 | service['activeModifiers'] = ['meta']; |
| 127 | const binding = service['findBindedShortcut']('a'); |
| 128 | expect(binding).toEqual({key: 'a', cb: 'modified', modifiers: ['meta']}); |
| 129 | }); |
| 130 | |
| 131 | it('should find a view keybinding', () => { |
| 132 | const binding = service['findBindedShortcut']('b'); |
| 133 | expect(binding).toEqual({key: 'b', cb: 'cb'}); |
| 134 | }); |
| 135 | |
| 136 | it('should find a view keybinding with modifiers', () => { |
| 137 | service['activeModifiers'] = ['meta', 'alt']; |
| 138 | const binding = service['findBindedShortcut']('b'); |
| 139 | expect(binding).toEqual({key: 'b', cb: 'modified', modifiers: ['meta', 'alt']}); |
| 140 | }); |
| 141 | |
| 142 | it('should not care about binding key case', () => { |
| 143 | const binding = service['findBindedShortcut']('A'); |
| 144 | expect(binding).toEqual({key: 'a', cb: 'cb'}); |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | describe('the registerKeyBinding method', () => { |
| 149 | |
| 150 | const binding = { |
| 151 | key: 'B', |
| 152 | cb: 'callback' |
| 153 | }; |
| 154 | |
| 155 | beforeEach(() => { |
| 156 | service['keyMapping'] = { |
| 157 | global: [ |
| 158 | { |
| 159 | key: 'a', |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 160 | cb: 'cb', |
| 161 | modifiers: undefined |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 162 | } |
| 163 | ], |
| 164 | view: [] |
| 165 | }; |
| 166 | }); |
| 167 | |
| 168 | it('should add a new global keybinding', () => { |
| 169 | service['registerKeyBinding'](binding, 'global'); |
| 170 | expect(service['keyMapping']['global'].length).toBe(2); |
| 171 | expect(service['keyMapping']['global'][1].key).toBe('b'); |
| 172 | }); |
| 173 | |
| 174 | it('should add a new view keybinding', () => { |
| 175 | service['registerKeyBinding'](binding); |
| 176 | expect(service['keyMapping']['view'].length).toBe(1); |
| 177 | expect(service['keyMapping']['view'][0].key).toBe('b'); |
| 178 | }); |
| 179 | |
| 180 | it('should not add binding that is not registered as "global" or "view"', () => { |
| 181 | function errorFunctionWrapper() { |
| 182 | service['registerKeyBinding']({ |
| 183 | key: 'z', |
| 184 | cb: 'cb' |
| 185 | }, 'something'); |
| 186 | } |
| 187 | expect(errorFunctionWrapper).toThrow(new Error('[XosKeyboardShortcut] A shortcut can be registered with scope "global" or "view" only')); |
| 188 | }); |
| 189 | |
| 190 | it('should not add binding that has an already registered key', () => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 191 | service['registerKeyBinding']({ |
| 192 | key: 'A', |
| 193 | cb: 'cb' |
| 194 | }, 'global'); |
| 195 | expect(logSpy).toHaveBeenCalledWith('[XosKeyboardShortcut] A shortcut for key "a" has already been registered'); |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 196 | }); |
| 197 | }); |
| 198 | }); |