1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Include test fixture.
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js']);
11 * @extends {ChromeVoxUnitTestBase}
13 function CvoxKeySequenceUnitTest() {}
15 CvoxKeySequenceUnitTest.prototype = {
16 __proto__: ChromeVoxUnitTestBase.prototype,
25 * Create mock event object.
26 * @param {number} keyCode The event key code (i.e. 13 for Enter).
27 * @param {{altGraphKey: boolean=,
31 * searchKeyHeld: boolean=,
33 * stickyMode: boolean=,
34 * prefixKey: boolean=}} eventParams The parameters on the event.
35 * altGraphKey: Whether or not the altGraph key was held down.
36 * altKey: Whether or not the alt key was held down.
37 * ctrlKey: Whether or not the ctrl key was held down.
38 * metaKey: Whether or not the meta key was held down.
39 * searchKeyHeld: Whether or not the search key was held down.
40 * shiftKey: Whether or not the shift key was held down.
41 * stickyMode: Whether or not sticky mode is enabled.
42 * prefixKey: Whether or not the prefix key was entered.
43 * @return {Object} The mock event.
45 createMockEvent: function(keyCode, eventParams) {
47 mockEvent.keyCode = keyCode;
49 if (eventParams == null) {
52 if (eventParams.hasOwnProperty('altGraphKey')) {
53 mockEvent.altGraphKey = eventParams.altGraphKey;
55 if (eventParams.hasOwnProperty('altKey')) {
56 mockEvent.altKey = eventParams.altKey;
58 if (eventParams.hasOwnProperty('ctrlKey')) {
59 mockEvent.ctrlKey = eventParams.ctrlKey;
61 if (eventParams.hasOwnProperty('metaKey')) {
62 mockEvent.metaKey = eventParams.metaKey;
64 if (eventParams.hasOwnProperty('shiftKey')) {
65 mockEvent.shiftKey = eventParams.shiftKey;
68 if (eventParams.hasOwnProperty('searchKeyHeld')) {
69 mockEvent.searchKeyHeld = eventParams.searchKeyHeld;
71 if (eventParams.hasOwnProperty('stickyMode')) {
72 mockEvent.stickyMode = eventParams.stickyMode;
74 if (eventParams.hasOwnProperty('prefixKey')) {
75 mockEvent.keyPrefix = eventParams.prefixKey;
83 // Set up mock ChromeVox modifier
84 cvox.ChromeVox.modKeyStr = 'Alt';
86 // Use these mock events in the tests:
88 // Down arrow, no modifiers
89 this.downArrowEvent = this.createMockEvent(40);
91 // Down arrow key with alt held down. We specified 'Alt' as the
92 // mock ChromeVox modifier string, so this means that KeySequence
93 // should interpret this as the ChromeVox modifier being active.
94 this.altDownArrowEvent = this.createMockEvent(40, {altKey: true});
96 // Right arrow, no modifiers
97 this.rightArrowEvent = this.createMockEvent(39);
99 // Ctrl key, no modifiers
100 this.ctrlEvent = this.createMockEvent(17);
102 // Ctrl key with sticky mode
103 this.ctrlStickyEvent = this.createMockEvent(17, {stickyMode: true});
105 // Ctrl key with prefix mode
106 this.ctrlPrefixEvent = this.createMockEvent(17, {prefixKey: true});
108 // 'a' key, no modifiers
109 this.aEvent = this.createMockEvent(65);
111 // 'a' key with ctrl held down
112 this.ctrlAEvent = this.createMockEvent(65, {ctrlKey: true});
114 // 'a' key with meta held down
115 this.metaAEvent = this.createMockEvent(65, {metaKey: true});
117 // 'a' key with shift held down
118 this.shiftAEvent = this.createMockEvent(65, {shiftKey: true});
120 // 'a' key with alt (which is the mock ChromeVox modifier) and shift held
122 this.altShiftAEvent = this.createMockEvent(65, {altKey: true,
125 // 'a' key with shift and prefix held down
126 this.shiftAPrefixEvent = this.createMockEvent(65, {shiftKey: true,
129 // 'a' key with shift and sticky mode
130 this.shiftAStickyEvent = this.createMockEvent(65, {shiftKey: true,
133 // 'a' key with sticky mode
134 this.aEventSticky = this.createMockEvent(65, {stickyMode: true});
136 // 'a' key with prefix key
137 this.aEventPrefix = this.createMockEvent(65, {prefixKey: true});
139 // 'a' key with alt (which is the mock ChromeVox modifier) held down
140 this.altAEvent = this.createMockEvent(65, {altKey: true});
142 // 'b' key, no modifiers
143 this.bEvent = this.createMockEvent(66);
145 // 'b' key, with ctrl held down
146 this.ctrlBEvent = this.createMockEvent(66, {ctrlKey: true});
148 // 'c' key, no modifiers
149 this.cEvent = this.createMockEvent(67);
151 // Shift key with ctrl held down
152 this.ctrlShiftEvent = this.createMockEvent(60, {ctrlKey: true});
154 // Ctrl key with shift held down
155 this.shiftCtrlEvent = this.createMockEvent(17, {shiftKey: true});
159 TEST_F('CvoxKeySequenceUnitTest', 'SimpleSequenceNoModifier', function() {
160 var downKey = new cvox.KeySequence(this.downArrowEvent, false);
162 assertEqualsJSON([40], downKey.keys.keyCode);
163 assertFalse(downKey.stickyMode);
164 assertFalse(downKey.prefixKey);
165 assertFalse(downKey.cvoxModifier);
167 assertEqualsJSON([false], downKey.keys.altGraphKey);
168 assertEqualsJSON([false], downKey.keys.altKey);
169 assertEqualsJSON([false], downKey.keys.ctrlKey);
170 assertEqualsJSON([false], downKey.keys.metaKey);
171 assertEqualsJSON([false], downKey.keys.searchKeyHeld);
172 assertEqualsJSON([false], downKey.keys.shiftKey);
174 assertEquals(1, downKey.length());
178 /** Test another key sequence, this time with the modifier */
179 TEST_F('CvoxKeySequenceUnitTest', 'SimpleSequenceWithModifier', function() {
180 var downKey = new cvox.KeySequence(this.downArrowEvent, true);
182 assertEqualsJSON([40], downKey.keys.keyCode);
183 assertFalse(downKey.stickyMode);
184 assertFalse(downKey.prefixKey);
185 assertTrue(downKey.cvoxModifier);
187 assertEqualsJSON([false], downKey.keys.altGraphKey);
188 assertEqualsJSON([false], downKey.keys.altKey);
189 assertEqualsJSON([false], downKey.keys.ctrlKey);
190 assertEqualsJSON([false], downKey.keys.metaKey);
191 assertEqualsJSON([false], downKey.keys.searchKeyHeld);
192 assertEqualsJSON([false], downKey.keys.shiftKey);
194 assertEquals(1, downKey.length());
198 /** Test a key sequence that includes the modifier */
199 TEST_F('CvoxKeySequenceUnitTest', 'ModifiedSequence', function() {
200 var cvoxDownKey = new cvox.KeySequence(this.altDownArrowEvent, true);
202 assertEqualsJSON([40], cvoxDownKey.keys.keyCode);
203 assertFalse(cvoxDownKey.stickyMode);
204 assertFalse(cvoxDownKey.prefixKey);
205 assertTrue(cvoxDownKey.cvoxModifier);
207 assertEqualsJSON([false], cvoxDownKey.keys.altGraphKey);
208 assertEqualsJSON([false], cvoxDownKey.keys.altKey);
209 assertEqualsJSON([false], cvoxDownKey.keys.ctrlKey);
210 assertEqualsJSON([false], cvoxDownKey.keys.metaKey);
211 assertEqualsJSON([false], cvoxDownKey.keys.searchKeyHeld);
212 assertEqualsJSON([false], cvoxDownKey.keys.shiftKey);
214 assertEquals(1, cvoxDownKey.length());
219 * Test equality - Ctrl key vs. Ctrl key with sticky mode on
220 * These should be equal because Ctrl should still function even with
223 TEST_F('CvoxKeySequenceUnitTest', 'StickyEquality', function() {
224 var ctrlKey = new cvox.KeySequence(this.ctrlEvent, false);
225 var ctrlSticky = new cvox.KeySequence(this.ctrlStickyEvent, false);
227 assertTrue(ctrlKey.equals(ctrlSticky));
232 * Test equality - 'a' key with Shift modifier vs. 'a' key without Shift
234 * These should not be equal because they do not have the same modifiers.
236 TEST_F('CvoxKeySequenceUnitTest', 'ShiftEquality', function() {
237 var aKey = new cvox.KeySequence(this.aEvent, false);
238 var shiftA = new cvox.KeySequence(this.shiftAEvent, false);
240 assertFalse(aKey.equals(shiftA));
245 * Test equality - 'a' with ChromeVox modifier specified, 'a' with sticky mode
246 * on, 'a' with prefix key, and 'a' with ChromeVox modifier held down. These
247 * should all be equal to each other.
249 TEST_F('CvoxKeySequenceUnitTest', 'FourWayEquality', function() {
250 var commandSequence = new cvox.KeySequence(this.aEvent, true);
251 var stickySequence = new cvox.KeySequence(this.aEventSticky, false);
252 var prefixSequence = new cvox.KeySequence(this.aEventPrefix, false);
253 var cvoxModifierSequence = new cvox.KeySequence(this.altAEvent);
255 assertTrue(commandSequence.equals(stickySequence));
256 assertTrue(commandSequence.equals(prefixSequence));
257 assertTrue(commandSequence.equals(cvoxModifierSequence));
259 assertTrue(stickySequence.equals(commandSequence));
260 assertTrue(stickySequence.equals(prefixSequence));
261 assertTrue(stickySequence.equals(cvoxModifierSequence));
263 assertTrue(prefixSequence.equals(commandSequence));
264 assertTrue(prefixSequence.equals(stickySequence));
265 assertTrue(prefixSequence.equals(cvoxModifierSequence));
267 assertTrue(cvoxModifierSequence.equals(commandSequence));
268 assertTrue(cvoxModifierSequence.equals(stickySequence));
269 assertTrue(cvoxModifierSequence.equals(prefixSequence));
274 * Test equality - 'a' key with Shift modifier and prefix vs. 'a' key with Shift
275 * modifier and sticky mode vs. 'a' key with Shift modifier and ChromeVox
276 * modifier specified vs. 'a' key with ChromeVox modifier held down.
277 * These should all be equal to each other..
279 TEST_F('CvoxKeySequenceUnitTest', 'ShiftPrefixEquality', function() {
280 var shiftAWithModifier = new cvox.KeySequence(this.shiftAEvent, true);
281 var shiftAWithPrefix = new cvox.KeySequence(this.shiftAPrefixEvent, false);
282 var shiftASticky = new cvox.KeySequence(this.shiftAStickyEvent, false);
283 var cvoxShiftA = new cvox.KeySequence(this.altShiftAEvent);
285 assertTrue(shiftAWithModifier.equals(shiftAWithPrefix));
286 assertTrue(shiftAWithModifier.equals(shiftASticky));
287 assertTrue(shiftAWithModifier.equals(cvoxShiftA));
289 assertTrue(shiftAWithPrefix.equals(shiftAWithModifier));
290 assertTrue(shiftAWithPrefix.equals(shiftASticky));
291 assertTrue(shiftAWithPrefix.equals(cvoxShiftA));
293 assertTrue(shiftASticky.equals(shiftAWithPrefix));
294 assertTrue(shiftASticky.equals(shiftAWithModifier));
295 assertTrue(shiftASticky.equals(cvoxShiftA));
297 assertTrue(cvoxShiftA.equals(shiftAWithModifier));
298 assertTrue(cvoxShiftA.equals(shiftAWithPrefix));
299 assertTrue(cvoxShiftA.equals(shiftASticky));
304 * Test inequality - 'a' with modifier key vs. 'a' without modifier key.
305 * These should not be equal.
307 TEST_F('CvoxKeySequenceUnitTest', 'Inequality', function() {
308 var aNoModifier = new cvox.KeySequence(this.aEvent, false);
309 var aWithModifier = new cvox.KeySequence(this.aEvent, true);
311 assertFalse(aNoModifier.equals(aWithModifier));
312 assertFalse(aWithModifier.equals(aNoModifier));
317 * Test equality - adding an additional key onto a sequence.
319 TEST_F('CvoxKeySequenceUnitTest', 'CvoxCtrl', function() {
320 var cvoxCtrlSequence = new cvox.KeySequence(this.ctrlEvent, true);
321 assertTrue(cvoxCtrlSequence.addKeyEvent(this.rightArrowEvent));
323 assertEquals(2, cvoxCtrlSequence.length());
325 // Can't add more than two key events.
326 assertFalse(cvoxCtrlSequence.addKeyEvent(this.rightArrowEvent));
328 var cvoxCtrlStickySequence = new cvox.KeySequence(this.ctrlStickyEvent,
330 assertTrue(cvoxCtrlStickySequence.addKeyEvent(this.rightArrowEvent));
332 var mockCtrlPrefixSequence = new cvox.KeySequence(this.ctrlPrefixEvent,
334 assertTrue(mockCtrlPrefixSequence.addKeyEvent(this.rightArrowEvent));
336 assertTrue(cvoxCtrlSequence.equals(cvoxCtrlStickySequence));
337 assertTrue(cvoxCtrlStickySequence.equals(cvoxCtrlSequence));
339 assertTrue(cvoxCtrlSequence.equals(mockCtrlPrefixSequence));
340 assertTrue(mockCtrlPrefixSequence.equals(cvoxCtrlSequence));
342 assertTrue(cvoxCtrlStickySequence.equals(mockCtrlPrefixSequence));
343 assertTrue(mockCtrlPrefixSequence.equals(cvoxCtrlStickySequence));
348 * Test for inequality - key sequences in different orders.
350 TEST_F('CvoxKeySequenceUnitTest', 'DifferentSequences', function() {
351 var cvoxBSequence = new cvox.KeySequence(this.bEvent, true);
352 assertTrue(cvoxBSequence.addKeyEvent(this.cEvent));
354 var cvoxCSequence = new cvox.KeySequence(this.cEvent, false);
355 assertTrue(cvoxCSequence.addKeyEvent(this.bEvent));
357 assertFalse(cvoxBSequence.equals(cvoxCSequence));
358 assertFalse(cvoxCSequence.equals(cvoxBSequence));
363 * Tests modifiers (ctrl, alt, etc) - if two sequences have different modifiers
364 * held down then they aren't equal.
366 TEST_F('CvoxKeySequenceUnitTest', 'MoreModifiers', function() {
367 var ctrlASequence = new cvox.KeySequence(this.ctrlAEvent, false);
368 var ctrlModifierKeyASequence = new cvox.KeySequence(this.ctrlAEvent, true);
370 var ctrlBSequence = new cvox.KeySequence(this.ctrlBEvent, false);
372 var metaASequence = new cvox.KeySequence(this.metaAEvent, false);
374 assertFalse(ctrlASequence.equals(metaASequence));
375 assertFalse(ctrlASequence.equals(ctrlModifierKeyASequence));
376 assertFalse(ctrlASequence.equals(ctrlBSequence));
381 * Tests modifier (ctrl, alt, etc) order - if two sequences have the same
382 * modifiers but held down in a different order then they aren't equal.
384 TEST_F('CvoxKeySequenceUnitTest', 'ModifierOrder', function() {
385 var ctrlShiftSequence = new cvox.KeySequence(this.ctrlShiftEvent, false);
386 var shiftCtrlSequence = new cvox.KeySequence(this.shiftCtrlEvent, true);
388 assertFalse(ctrlShiftSequence.equals(shiftCtrlSequence));
393 * Tests converting from a string to a KeySequence object.
395 TEST_F('CvoxKeySequenceUnitTest', 'FromStr', function() {
396 var ctrlString = cvox.KeySequence.fromStr('Ctrl');
397 assertEqualsJSON(ctrlString.keys.ctrlKey, [true]);
398 assertEqualsJSON(ctrlString.keys.keyCode, [17]);
400 var modifiedLetterString = cvox.KeySequence.fromStr('Ctrl+Z');
401 assertEqualsJSON(modifiedLetterString.keys.ctrlKey, [true]);
402 assertEqualsJSON(modifiedLetterString.keys.keyCode, [90]);
404 var keyCodeString = cvox.KeySequence.fromStr('#9');
405 assertEqualsJSON(keyCodeString.keys.keyCode, [9]);
407 var modifiedKeyCodeString = cvox.KeySequence.fromStr('Shift+#9');
408 assertEqualsJSON(modifiedKeyCodeString.keys.shiftKey, [true]);
409 assertEqualsJSON(modifiedKeyCodeString.keys.keyCode, [9]);
411 var cvoxLetterString = cvox.KeySequence.fromStr('Cvox+U');
412 assertTrue(cvoxLetterString.cvoxModifier);
413 assertEqualsJSON(cvoxLetterString.keys.keyCode, [85]);
415 var cvoxSequenceString = cvox.KeySequence.fromStr('Cvox+C>T');
416 assertTrue(cvoxSequenceString.cvoxModifier);
417 assertEqualsJSON(cvoxSequenceString.keys.keyCode, [67, 84]);
419 var cvoxSequenceKeyCodeString = cvox.KeySequence.fromStr('Cvox+L>#186');
420 assertTrue(cvoxSequenceKeyCodeString.cvoxModifier);
421 assertEqualsJSON(cvoxSequenceKeyCodeString.keys.keyCode, [76, 186]);
423 var stickyString = cvox.KeySequence.fromStr('Insert>Insert+');
424 assertEqualsJSON(stickyString.keys.keyCode, [45, 45]);
429 * Tests converting from a JSON string to a KeySequence object.
431 TEST_F('CvoxKeySequenceUnitTest', 'Deserialize', function() {
432 var forwardSequence = cvox.KeySequence.deserialize({'cvoxModifier': true,
433 'stickyMode': false, 'prefixKey': false, 'keys': {'ctrlKey': [false],
434 'searchKeyHeld': [false], 'altKey': [false], 'altGraphKey': [false],
435 'shiftKey': [false], 'metaKey': [false], 'keyCode': [40]}});
436 assertTrue(forwardSequence.cvoxModifier);
437 assertEqualsJSON(forwardSequence.keys.keyCode, [40]);
439 var ctrlSequence = cvox.KeySequence.deserialize({'cvoxModifier': false,
440 'stickyMode': true, 'prefixKey': false, 'keys': {'ctrlKey': [true],
441 'searchKeyHeld': [false], 'altKey': [false], 'altGraphKey': [false],
442 'shiftKey': [false], 'metaKey': [false], 'keyCode': [17]}});
443 assertEqualsJSON(ctrlSequence.keys.ctrlKey, [true]);
444 assertEqualsJSON(ctrlSequence.keys.keyCode, [17]);