Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / common / key_sequence_test.js
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.
4
5 // Include test fixture.
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js']);
7
8 /**
9  * Test fixture.
10  * @constructor
11  * @extends {ChromeVoxUnitTestBase}
12  */
13 function CvoxKeySequenceUnitTest() {}
14
15 CvoxKeySequenceUnitTest.prototype = {
16   __proto__: ChromeVoxUnitTestBase.prototype,
17
18   /** @override */
19   closureModuleDeps: [
20     'cvox.ChromeVox',
21     'cvox.KeySequence',
22   ],
23
24   /**
25    * Create mock event object.
26    * @param {number} keyCode The event key code (i.e. 13 for Enter).
27    * @param {{altGraphKey: boolean=,
28    *         altKey: boolean=,
29    *         ctrlKey: boolean=,
30    *         metaKey: boolean=,
31    *         searchKeyHeld: boolean=,
32    *         shiftKey: 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.
44    */
45   createMockEvent: function(keyCode, eventParams) {
46     var mockEvent = {};
47     mockEvent.keyCode = keyCode;
48
49     if (eventParams == null) {
50       return mockEvent;
51     }
52     if (eventParams.hasOwnProperty('altGraphKey')) {
53       mockEvent.altGraphKey = eventParams.altGraphKey;
54     }
55     if (eventParams.hasOwnProperty('altKey')) {
56       mockEvent.altKey = eventParams.altKey;
57     }
58     if (eventParams.hasOwnProperty('ctrlKey')) {
59       mockEvent.ctrlKey = eventParams.ctrlKey;
60     }
61     if (eventParams.hasOwnProperty('metaKey')) {
62       mockEvent.metaKey = eventParams.metaKey;
63     }
64     if (eventParams.hasOwnProperty('shiftKey')) {
65       mockEvent.shiftKey = eventParams.shiftKey;
66     }
67
68     if (eventParams.hasOwnProperty('searchKeyHeld')) {
69       mockEvent.searchKeyHeld = eventParams.searchKeyHeld;
70     }
71     if (eventParams.hasOwnProperty('stickyMode')) {
72       mockEvent.stickyMode = eventParams.stickyMode;
73     }
74     if (eventParams.hasOwnProperty('prefixKey')) {
75       mockEvent.keyPrefix = eventParams.prefixKey;
76     }
77
78     return mockEvent;
79   },
80
81   /** @override */
82   setUp: function() {
83     // Set up mock ChromeVox modifier
84     cvox.ChromeVox.modKeyStr = 'Alt';
85
86     // Use these mock events in the tests:
87
88     // Down arrow, no modifiers
89     this.downArrowEvent = this.createMockEvent(40);
90
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});
95
96     // Right arrow, no modifiers
97     this.rightArrowEvent = this.createMockEvent(39);
98
99     // Ctrl key, no modifiers
100     this.ctrlEvent = this.createMockEvent(17);
101
102     // Ctrl key with sticky mode
103     this.ctrlStickyEvent = this.createMockEvent(17, {stickyMode: true});
104
105     // Ctrl key with prefix mode
106     this.ctrlPrefixEvent = this.createMockEvent(17, {prefixKey: true});
107
108     // 'a' key, no modifiers
109     this.aEvent = this.createMockEvent(65);
110
111     // 'a' key with ctrl held down
112     this.ctrlAEvent = this.createMockEvent(65, {ctrlKey: true});
113
114     // 'a' key with meta held down
115     this.metaAEvent = this.createMockEvent(65, {metaKey: true});
116
117     // 'a' key with shift held down
118     this.shiftAEvent = this.createMockEvent(65, {shiftKey: true});
119
120     // 'a' key with alt (which is the mock ChromeVox modifier) and shift held
121     // down.
122     this.altShiftAEvent = this.createMockEvent(65, {altKey: true,
123                                                     shiftKey: true});
124
125     // 'a' key with shift and prefix held down
126     this.shiftAPrefixEvent = this.createMockEvent(65, {shiftKey: true,
127                                                        prefixKey: true});
128
129     // 'a' key with shift and sticky mode
130     this.shiftAStickyEvent = this.createMockEvent(65, {shiftKey: true,
131                                                        stickyMode: true});
132
133     // 'a' key with sticky mode
134     this.aEventSticky = this.createMockEvent(65, {stickyMode: true});
135
136     // 'a' key with prefix key
137     this.aEventPrefix = this.createMockEvent(65, {prefixKey: true});
138
139     // 'a' key with alt (which is the mock ChromeVox modifier) held down
140     this.altAEvent = this.createMockEvent(65, {altKey: true});
141
142     // 'b' key, no modifiers
143     this.bEvent = this.createMockEvent(66);
144
145     // 'b' key, with ctrl held down
146     this.ctrlBEvent = this.createMockEvent(66, {ctrlKey: true});
147
148     // 'c' key, no modifiers
149     this.cEvent = this.createMockEvent(67);
150
151     // Shift key with ctrl held down
152     this.ctrlShiftEvent = this.createMockEvent(60, {ctrlKey: true});
153
154     // Ctrl key with shift held down
155     this.shiftCtrlEvent = this.createMockEvent(17, {shiftKey: true});
156   }
157 };
158
159 TEST_F('CvoxKeySequenceUnitTest', 'SimpleSequenceNoModifier', function() {
160   var downKey = new cvox.KeySequence(this.downArrowEvent, false);
161
162   assertEqualsJSON([40], downKey.keys.keyCode);
163   assertFalse(downKey.stickyMode);
164   assertFalse(downKey.prefixKey);
165   assertFalse(downKey.cvoxModifier);
166
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);
173
174   assertEquals(1, downKey.length());
175 });
176
177
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);
181
182   assertEqualsJSON([40], downKey.keys.keyCode);
183   assertFalse(downKey.stickyMode);
184   assertFalse(downKey.prefixKey);
185   assertTrue(downKey.cvoxModifier);
186
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);
193
194   assertEquals(1, downKey.length());
195 });
196
197
198 /** Test a key sequence that includes the modifier */
199 TEST_F('CvoxKeySequenceUnitTest', 'ModifiedSequence', function() {
200   var cvoxDownKey = new cvox.KeySequence(this.altDownArrowEvent, true);
201
202   assertEqualsJSON([40], cvoxDownKey.keys.keyCode);
203   assertFalse(cvoxDownKey.stickyMode);
204   assertFalse(cvoxDownKey.prefixKey);
205   assertTrue(cvoxDownKey.cvoxModifier);
206
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);
213
214   assertEquals(1, cvoxDownKey.length());
215 });
216
217
218 /**
219  * Test equality - Ctrl key vs. Ctrl key with sticky mode on
220  * These should be equal because Ctrl should still function even with
221  * sticky mode on.
222 */
223 TEST_F('CvoxKeySequenceUnitTest', 'StickyEquality', function() {
224   var ctrlKey = new cvox.KeySequence(this.ctrlEvent, false);
225   var ctrlSticky = new cvox.KeySequence(this.ctrlStickyEvent, false);
226
227   assertTrue(ctrlKey.equals(ctrlSticky));
228 });
229
230
231 /**
232  * Test equality - 'a' key with Shift modifier vs. 'a' key without Shift
233  * modifier.
234  * These should not be equal because they do not have the same modifiers.
235 */
236 TEST_F('CvoxKeySequenceUnitTest', 'ShiftEquality', function() {
237   var aKey = new cvox.KeySequence(this.aEvent, false);
238   var shiftA = new cvox.KeySequence(this.shiftAEvent, false);
239
240   assertFalse(aKey.equals(shiftA));
241 });
242
243
244 /**
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.
248  */
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);
254
255   assertTrue(commandSequence.equals(stickySequence));
256   assertTrue(commandSequence.equals(prefixSequence));
257   assertTrue(commandSequence.equals(cvoxModifierSequence));
258
259   assertTrue(stickySequence.equals(commandSequence));
260   assertTrue(stickySequence.equals(prefixSequence));
261   assertTrue(stickySequence.equals(cvoxModifierSequence));
262
263   assertTrue(prefixSequence.equals(commandSequence));
264   assertTrue(prefixSequence.equals(stickySequence));
265   assertTrue(prefixSequence.equals(cvoxModifierSequence));
266
267   assertTrue(cvoxModifierSequence.equals(commandSequence));
268   assertTrue(cvoxModifierSequence.equals(stickySequence));
269   assertTrue(cvoxModifierSequence.equals(prefixSequence));
270 });
271
272
273 /**
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..
278 */
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);
284
285   assertTrue(shiftAWithModifier.equals(shiftAWithPrefix));
286   assertTrue(shiftAWithModifier.equals(shiftASticky));
287   assertTrue(shiftAWithModifier.equals(cvoxShiftA));
288
289   assertTrue(shiftAWithPrefix.equals(shiftAWithModifier));
290   assertTrue(shiftAWithPrefix.equals(shiftASticky));
291   assertTrue(shiftAWithPrefix.equals(cvoxShiftA));
292
293   assertTrue(shiftASticky.equals(shiftAWithPrefix));
294   assertTrue(shiftASticky.equals(shiftAWithModifier));
295   assertTrue(shiftASticky.equals(cvoxShiftA));
296
297   assertTrue(cvoxShiftA.equals(shiftAWithModifier));
298   assertTrue(cvoxShiftA.equals(shiftAWithPrefix));
299   assertTrue(cvoxShiftA.equals(shiftASticky));
300 });
301
302
303 /**
304  * Test inequality - 'a' with modifier key vs. 'a' without modifier key.
305  * These should not be equal.
306 */
307 TEST_F('CvoxKeySequenceUnitTest', 'Inequality', function() {
308   var aNoModifier = new cvox.KeySequence(this.aEvent, false);
309   var aWithModifier = new cvox.KeySequence(this.aEvent, true);
310
311   assertFalse(aNoModifier.equals(aWithModifier));
312   assertFalse(aWithModifier.equals(aNoModifier));
313 });
314
315
316 /**
317  * Test equality - adding an additional key onto a sequence.
318  */
319 TEST_F('CvoxKeySequenceUnitTest', 'CvoxCtrl', function() {
320   var cvoxCtrlSequence = new cvox.KeySequence(this.ctrlEvent, true);
321   assertTrue(cvoxCtrlSequence.addKeyEvent(this.rightArrowEvent));
322
323   assertEquals(2, cvoxCtrlSequence.length());
324
325   // Can't add more than two key events.
326   assertFalse(cvoxCtrlSequence.addKeyEvent(this.rightArrowEvent));
327
328   var cvoxCtrlStickySequence = new cvox.KeySequence(this.ctrlStickyEvent,
329                                                     false);
330   assertTrue(cvoxCtrlStickySequence.addKeyEvent(this.rightArrowEvent));
331
332   var mockCtrlPrefixSequence = new cvox.KeySequence(this.ctrlPrefixEvent,
333                                                     false);
334   assertTrue(mockCtrlPrefixSequence.addKeyEvent(this.rightArrowEvent));
335
336   assertTrue(cvoxCtrlSequence.equals(cvoxCtrlStickySequence));
337   assertTrue(cvoxCtrlStickySequence.equals(cvoxCtrlSequence));
338
339   assertTrue(cvoxCtrlSequence.equals(mockCtrlPrefixSequence));
340   assertTrue(mockCtrlPrefixSequence.equals(cvoxCtrlSequence));
341
342   assertTrue(cvoxCtrlStickySequence.equals(mockCtrlPrefixSequence));
343   assertTrue(mockCtrlPrefixSequence.equals(cvoxCtrlStickySequence));
344 });
345
346
347 /**
348  * Test for inequality - key sequences in different orders.
349  */
350 TEST_F('CvoxKeySequenceUnitTest', 'DifferentSequences', function() {
351   var cvoxBSequence = new cvox.KeySequence(this.bEvent, true);
352   assertTrue(cvoxBSequence.addKeyEvent(this.cEvent));
353
354   var cvoxCSequence = new cvox.KeySequence(this.cEvent, false);
355   assertTrue(cvoxCSequence.addKeyEvent(this.bEvent));
356
357   assertFalse(cvoxBSequence.equals(cvoxCSequence));
358   assertFalse(cvoxCSequence.equals(cvoxBSequence));
359 });
360
361
362 /**
363  * Tests modifiers (ctrl, alt, etc) - if two sequences have different modifiers
364  * held down then they aren't equal.
365  */
366 TEST_F('CvoxKeySequenceUnitTest', 'MoreModifiers', function() {
367   var ctrlASequence = new cvox.KeySequence(this.ctrlAEvent, false);
368   var ctrlModifierKeyASequence = new cvox.KeySequence(this.ctrlAEvent, true);
369
370   var ctrlBSequence = new cvox.KeySequence(this.ctrlBEvent, false);
371
372   var metaASequence = new cvox.KeySequence(this.metaAEvent, false);
373
374   assertFalse(ctrlASequence.equals(metaASequence));
375   assertFalse(ctrlASequence.equals(ctrlModifierKeyASequence));
376   assertFalse(ctrlASequence.equals(ctrlBSequence));
377 });
378
379
380 /**
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.
383  */
384 TEST_F('CvoxKeySequenceUnitTest', 'ModifierOrder', function() {
385   var ctrlShiftSequence = new cvox.KeySequence(this.ctrlShiftEvent, false);
386   var shiftCtrlSequence = new cvox.KeySequence(this.shiftCtrlEvent, true);
387
388   assertFalse(ctrlShiftSequence.equals(shiftCtrlSequence));
389 });
390
391
392 /**
393  * Tests converting from a string to a KeySequence object.
394  */
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]);
399
400   var modifiedLetterString = cvox.KeySequence.fromStr('Ctrl+Z');
401   assertEqualsJSON(modifiedLetterString.keys.ctrlKey, [true]);
402   assertEqualsJSON(modifiedLetterString.keys.keyCode, [90]);
403
404   var keyCodeString = cvox.KeySequence.fromStr('#9');
405   assertEqualsJSON(keyCodeString.keys.keyCode, [9]);
406
407   var modifiedKeyCodeString = cvox.KeySequence.fromStr('Shift+#9');
408   assertEqualsJSON(modifiedKeyCodeString.keys.shiftKey, [true]);
409   assertEqualsJSON(modifiedKeyCodeString.keys.keyCode, [9]);
410
411   var cvoxLetterString = cvox.KeySequence.fromStr('Cvox+U');
412   assertTrue(cvoxLetterString.cvoxModifier);
413   assertEqualsJSON(cvoxLetterString.keys.keyCode, [85]);
414
415   var cvoxSequenceString = cvox.KeySequence.fromStr('Cvox+C>T');
416   assertTrue(cvoxSequenceString.cvoxModifier);
417   assertEqualsJSON(cvoxSequenceString.keys.keyCode, [67, 84]);
418
419   var cvoxSequenceKeyCodeString = cvox.KeySequence.fromStr('Cvox+L>#186');
420   assertTrue(cvoxSequenceKeyCodeString.cvoxModifier);
421   assertEqualsJSON(cvoxSequenceKeyCodeString.keys.keyCode, [76, 186]);
422
423   var stickyString = cvox.KeySequence.fromStr('Insert>Insert+');
424   assertEqualsJSON(stickyString.keys.keyCode, [45, 45]);
425 });
426
427
428 /**
429  * Tests converting from a JSON string to a KeySequence object.
430  */
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]);
438
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]);
445 });