blob: 8586b4c7e2b4e978fe9600b5cb6849d3372509c9 [file] [log] [blame]
Ben Rohlfs966586e2021-10-06 16:15:24 +02001/**
2 * @license
3 * Copyright (C) 2021 The Android Open Source Project
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 */
17import '../../test/common-test-setup-karma';
18import {
19 ShortcutsService,
20 SPECIAL_SHORTCUT,
21} from '../../services/shortcuts/shortcuts-service';
22import {Shortcut, ShortcutSection} from './shortcuts-config';
23
24suite('shortcuts-service tests', () => {
25 test('bindings management', () => {
26 const mgr = new ShortcutsService();
27 const NEXT_FILE = Shortcut.NEXT_FILE;
28
29 assert.isUndefined(mgr.getBindingsForShortcut(NEXT_FILE));
30 mgr.bindShortcut(NEXT_FILE, ']', '}', 'right');
31 assert.deepEqual(mgr.getBindingsForShortcut(NEXT_FILE), [
32 ']',
33 '}',
34 'right',
35 ]);
36 });
37
38 test('getShortcut', () => {
39 const mgr = new ShortcutsService();
40 const NEXT_FILE = Shortcut.NEXT_FILE;
41
42 assert.isUndefined(mgr.getBindingsForShortcut(NEXT_FILE));
43 mgr.bindShortcut(NEXT_FILE, ']', '}', 'right');
44 assert.equal(mgr.getShortcut(NEXT_FILE), '],},→');
45 });
46
47 test('getShortcut with modifiers', () => {
48 const mgr = new ShortcutsService();
49 const NEXT_FILE = Shortcut.NEXT_FILE;
50
51 assert.isUndefined(mgr.getBindingsForShortcut(NEXT_FILE));
52 mgr.bindShortcut(NEXT_FILE, 'Shift+a:key');
53 assert.equal(mgr.getShortcut(NEXT_FILE), 'Shift+a');
54 });
55
56 suite('binding descriptions', () => {
57 function mapToObject<K, V>(m: Map<K, V>) {
58 const o: any = {};
59 m.forEach((v: V, k: K) => (o[k] = v));
60 return o;
61 }
62
63 test('single combo description', () => {
64 const mgr = new ShortcutsService();
65 assert.deepEqual(mgr.describeBinding('a'), ['a']);
66 assert.deepEqual(mgr.describeBinding('a:keyup'), ['a']);
67 assert.deepEqual(mgr.describeBinding('ctrl+a'), ['Ctrl', 'a']);
68 assert.deepEqual(mgr.describeBinding('ctrl+shift+up:keyup'), [
69 'Ctrl',
70 'Shift',
71 '↑',
72 ]);
73 });
74
75 test('combo set description', () => {
76 const mgr = new ShortcutsService();
77 assert.isNull(mgr.describeBindings(Shortcut.NEXT_FILE));
78
79 mgr.bindShortcut(
80 Shortcut.GO_TO_OPENED_CHANGES,
81 SPECIAL_SHORTCUT.GO_KEY,
82 'o'
83 );
84 assert.deepEqual(mgr.describeBindings(Shortcut.GO_TO_OPENED_CHANGES), [
85 ['g', 'o'],
86 ]);
87
88 mgr.bindShortcut(
89 Shortcut.NEXT_FILE,
90 SPECIAL_SHORTCUT.DOC_ONLY,
91 ']',
92 'ctrl+shift+right:keyup'
93 );
94 assert.deepEqual(mgr.describeBindings(Shortcut.NEXT_FILE), [
95 [']'],
96 ['Ctrl', 'Shift', '→'],
97 ]);
98
99 mgr.bindShortcut(Shortcut.PREV_FILE, '[');
100 assert.deepEqual(mgr.describeBindings(Shortcut.PREV_FILE), [['[']]);
101 });
102
103 test('combo set description width', () => {
104 const mgr = new ShortcutsService();
105 assert.strictEqual(mgr.comboSetDisplayWidth([['u']]), 1);
106 assert.strictEqual(mgr.comboSetDisplayWidth([['g', 'o']]), 2);
107 assert.strictEqual(mgr.comboSetDisplayWidth([['Shift', 'r']]), 6);
108 assert.strictEqual(mgr.comboSetDisplayWidth([['x'], ['y']]), 4);
109 assert.strictEqual(
110 mgr.comboSetDisplayWidth([['x'], ['y'], ['Shift', 'z']]),
111 12
112 );
113 });
114
115 test('distribute shortcut help', () => {
116 const mgr = new ShortcutsService();
117 assert.deepEqual(mgr.distributeBindingDesc([['o']]), [[['o']]]);
118 assert.deepEqual(mgr.distributeBindingDesc([['g', 'o']]), [[['g', 'o']]]);
119 assert.deepEqual(
120 mgr.distributeBindingDesc([['ctrl', 'shift', 'meta', 'enter']]),
121 [[['ctrl', 'shift', 'meta', 'enter']]]
122 );
123 assert.deepEqual(
124 mgr.distributeBindingDesc([['ctrl', 'shift', 'meta', 'enter'], ['o']]),
125 [[['ctrl', 'shift', 'meta', 'enter']], [['o']]]
126 );
127 assert.deepEqual(
128 mgr.distributeBindingDesc([
129 ['ctrl', 'enter'],
130 ['meta', 'enter'],
131 ['ctrl', 's'],
132 ['meta', 's'],
133 ]),
134 [
135 [
136 ['ctrl', 'enter'],
137 ['meta', 'enter'],
138 ],
139 [
140 ['ctrl', 's'],
141 ['meta', 's'],
142 ],
143 ]
144 );
145 });
146
147 test('active shortcuts by section', () => {
148 const mgr = new ShortcutsService();
149 mgr.bindShortcut(Shortcut.NEXT_FILE, ']');
150 mgr.bindShortcut(Shortcut.NEXT_LINE, 'j');
151 mgr.bindShortcut(Shortcut.GO_TO_OPENED_CHANGES, 'g+o');
152 mgr.bindShortcut(Shortcut.SEARCH, '/');
153
154 assert.deepEqual(mapToObject(mgr.activeShortcutsBySection()), {});
155
156 mgr.attachHost({}, new Map([[Shortcut.NEXT_FILE, 'null']]));
157 assert.deepEqual(mapToObject(mgr.activeShortcutsBySection()), {
158 [ShortcutSection.NAVIGATION]: [
159 {shortcut: Shortcut.NEXT_FILE, text: 'Go to next file'},
160 ],
161 });
162
163 mgr.attachHost({}, new Map([[Shortcut.NEXT_LINE, 'null']]));
164 assert.deepEqual(mapToObject(mgr.activeShortcutsBySection()), {
165 [ShortcutSection.DIFFS]: [
166 {shortcut: Shortcut.NEXT_LINE, text: 'Go to next line'},
167 ],
168 [ShortcutSection.NAVIGATION]: [
169 {shortcut: Shortcut.NEXT_FILE, text: 'Go to next file'},
170 ],
171 });
172
173 mgr.attachHost(
174 {},
175 new Map([
176 [Shortcut.SEARCH, 'null'],
177 [Shortcut.GO_TO_OPENED_CHANGES, 'null'],
178 ])
179 );
180 assert.deepEqual(mapToObject(mgr.activeShortcutsBySection()), {
181 [ShortcutSection.DIFFS]: [
182 {shortcut: Shortcut.NEXT_LINE, text: 'Go to next line'},
183 ],
184 [ShortcutSection.EVERYWHERE]: [
185 {shortcut: Shortcut.SEARCH, text: 'Search'},
186 {
187 shortcut: Shortcut.GO_TO_OPENED_CHANGES,
188 text: 'Go to Opened Changes',
189 },
190 ],
191 [ShortcutSection.NAVIGATION]: [
192 {shortcut: Shortcut.NEXT_FILE, text: 'Go to next file'},
193 ],
194 });
195 });
196
197 test('directory view', () => {
198 const mgr = new ShortcutsService();
199 mgr.bindShortcut(Shortcut.NEXT_FILE, ']');
200 mgr.bindShortcut(Shortcut.NEXT_LINE, 'j');
201 mgr.bindShortcut(
202 Shortcut.GO_TO_OPENED_CHANGES,
203 SPECIAL_SHORTCUT.GO_KEY,
204 'o'
205 );
206 mgr.bindShortcut(Shortcut.SEARCH, '/');
207 mgr.bindShortcut(
208 Shortcut.SAVE_COMMENT,
209 'ctrl+enter',
210 'meta+enter',
211 'ctrl+s',
212 'meta+s'
213 );
214
215 assert.deepEqual(mapToObject(mgr.directoryView()), {});
216
217 mgr.attachHost(
218 {},
219 new Map([
220 [Shortcut.GO_TO_OPENED_CHANGES, 'null'],
221 [Shortcut.NEXT_FILE, 'null'],
222 [Shortcut.NEXT_LINE, 'null'],
223 [Shortcut.SAVE_COMMENT, 'null'],
224 [Shortcut.SEARCH, 'null'],
225 ])
226 );
227 assert.deepEqual(mapToObject(mgr.directoryView()), {
228 [ShortcutSection.DIFFS]: [
229 {binding: [['j']], text: 'Go to next line'},
230 {
231 binding: [
232 ['Ctrl', 'Enter'],
233 ['Meta', 'Enter'],
234 ],
235 text: 'Save comment',
236 },
237 {
238 binding: [
239 ['Ctrl', 's'],
240 ['Meta', 's'],
241 ],
242 text: 'Save comment',
243 },
244 ],
245 [ShortcutSection.EVERYWHERE]: [
246 {binding: [['/']], text: 'Search'},
247 {binding: [['g', 'o']], text: 'Go to Opened Changes'},
248 ],
249 [ShortcutSection.NAVIGATION]: [
250 {binding: [[']']], text: 'Go to next file'},
251 ],
252 });
253 });
254 });
255});