blob: fa9bf414f6c73bc98c9f6d6e8f9648d36ad92b85 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandoloc8178492017-04-11 17:55:13 -070019import * as angular from 'angular';
20import 'angular-mocks';
21import {IXosKeyboardShortcutService, XosKeyboardShortcut, IXosKeyboardShortcutBinding} from './keyboard-shortcut';
22import {IXosSidePanelService} from '../side-panel/side-panel.service';
23
24let service: IXosKeyboardShortcutService;
25let $log: ng.ILogService;
26let $transitions: any;
27let XosSidePanel: IXosSidePanelService;
Matteo Scandolo9b460042017-04-14 16:24:45 -070028let logSpy: any;
Matteo Scandoloc8178492017-04-11 17:55:13 -070029
30const 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
47const baseLocalModifiers: IXosKeyboardShortcutBinding[] = [
48 {
49 key: 'b',
50 cb: 'cb'
51 },
52 {
53 key: 'b',
54 cb: 'modified',
55 modifiers: ['meta', 'alt']
56 }
57];
58
59describe('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 Scandolo9b460042017-04-14 16:24:45 -070077 logSpy = spyOn($log, 'warn');
Matteo Scandoloc8178492017-04-11 17:55:13 -070078 });
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 Scandoloc8a58c82017-08-17 17:14:38 -0700160 cb: 'cb',
161 modifiers: undefined
Matteo Scandoloc8178492017-04-11 17:55:13 -0700162 }
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 Scandolo9b460042017-04-14 16:24:45 -0700191 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 Scandoloc8178492017-04-11 17:55:13 -0700196 });
197 });
198});