8ea22c184c0da8a9c4ff7718bf396d3da5f6d459
[platform/framework/web/crosswalk.git] / src / chrome / test / data / chromeos / virtual_keyboard / keyset_transition_test.js
1 /*
2  * Copyright 2013 The Chromium Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 /**
8  * Special keys for toggling keyset transitions.
9  * @enum {string}
10  */
11 var Key = {
12   MORE_SYMBOLS: '~[<',
13   SHIFT: 'shift',
14   SYMBOL: '?123',
15   TEXT: 'ABC'
16 };
17
18 /**
19  * Input types.
20  * @enum {string}
21  */
22 var InputType = {
23   TEXT: 'text',
24   NUMBER: 'number'
25 }
26
27 /**
28  * Tester for keyset transitions.
29  * @param {string=} opt_layout Optional layout. Used to generate the full
30  *     keyset ID from its shorthand name. If not specified, then the full
31  *     keyset ID is required for the test.
32  * @constructor
33  */
34 function KeysetTransitionTester(opt_layout) {
35   this.layout = opt_layout;
36   this.subtasks = [];
37 };
38
39 KeysetTransitionTester.prototype = {
40   /**
41    * Extends the subtask scheduler.
42    */
43   __proto__: SubtaskScheduler.prototype,
44
45   /**
46    * Adds a task for mocking a key event.
47    * @param {string} aligment Indicates if the left or right version of the
48    *     keyset transition key should be used.
49    * @param {string} key Text label on the key.
50    * @param {string} eventType Name of the event.
51    * @param {string} keysetId Name of the expected keyset at the start of the
52    *     task.
53    */
54   keyEvent: function(alignment, key, eventType, keysetId) {
55     var self = this;
56     var fn = function() {
57       Debug('Generating key event: alignment = ' + alignment + ', key = ' + key
58           + ', event type = ' + eventType);
59       self.verifyKeyset(keysetId, 'Unexpected keyset.');
60       var keyset = $('keyboard').activeKeyset;
61       assertTrue(!!keyset, 'Missing active keyset');
62       var search  = '[align="' + alignment + '"]';
63       var candidates = keyset.querySelectorAll(search).array();
64       for (var i = 0; i < candidates.length; i++) {
65         if (candidates[i].innerText == key) {
66           candidates[i][eventType]({pointerId: 1,  isPrimary:true});
67           return;
68         }
69       }
70       throw new Error('Unable to find \'' + key + '\' key in ' + keysetId);
71     };
72     this.addWaitCondition(fn, keysetId);
73     this.addSubtask(fn);
74   },
75
76   /**
77    * Adds a task for mocking a key typed event.
78    * @param {string} key Text label on the key.
79    * @param {number} keyCode The legacy key code for the character.
80    * @param {number} modifiers Indicates which if any of the shift, control and
81    *     alt keys are being virtually pressed.
82    * @param {string} keysetId Name of the expected keyset at the start of the
83    *     task.
84    */
85   typeKey: function(key, keyCode, modifiers, keysetId) {
86     var self = this;
87     var fn = function () {
88       Debug('Generating key typed event for key = ' + key + " and keycode = "
89           + keyCode);
90       self.verifyKeyset(keysetId, 'Unexpected keyset.');
91       mockTypeCharacter(key, keyCode, modifiers)
92     };
93     this.addWaitCondition(fn, keysetId);
94     this.addSubtask(fn);
95   },
96
97   /**
98    * Updates the input type.
99    * @param {string} inputType The new input type.
100    * @param {string} keysetId Expected keyset at the start of the task.
101    */
102   transition: function(inputType, keysetId) {
103     var self = this;
104     var fn = function() {
105       self.verifyKeyset(keysetId, 'Unexpected keyset');
106       Debug('changing input type to ' + inputType);
107       $('keyboard').inputTypeValue = inputType;
108     };
109     this.addWaitCondition(fn, keysetId);
110     this.addSubtask(fn);
111   }
112 };
113
114 /**
115  * Tests that capitalizion persists on keyset transitions.
116  * The test is run asynchronously since the keyboard loads keysets dynamically.
117  * @param {Function} testDoneCallback The function to be called on completion.
118  */
119 function testPersistantCapitalizationAsync(testDoneCallback) {
120   var tester = new KeysetTransitionTester(Layout.DEFAULT);
121
122   /**
123    * Checks persistant capitalization using the shift key with the given
124    * alignment.
125    * @param {string} alignment The alignment of the shift key.
126    */
127   var checkPersistantCapitalization = function(alignment) {
128     // Shift-lock
129     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.LOWER);
130     tester.wait(1000, Keyset.UPPER);
131     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.UPPER);
132
133      // text -> symbol -> more -> text
134     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
135         Keyset.UPPER);
136     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
137     tester.keyEvent(Alignment.LEFT, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
138         Keyset.SYMBOL);
139     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP,
140         Keyset.MORE_SYMBOLS);
141     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_DOWN,
142         Keyset.MORE_SYMBOLS);
143     // Symbol key only has left alignment.
144     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP, Keyset.UPPER);
145
146     // switch back to lower case
147     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN,
148         Keyset.UPPER);
149     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.LOWER);
150   };
151   // Run the test using the left shift key.
152   checkPersistantCapitalization(Alignment.LEFT);
153   // Run the test using the right shift key.
154   checkPersistantCapitalization(Alignment.RIGHT);
155
156   tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
157 }
158
159 /**
160  * Tests that changing the input type changes the layout. The test is run
161  * asynchronously since the keyboard loads keysets dynamically.
162  * @param {Function} testDoneCallback The function to be called on completion.
163  */
164 function testInputTypeResponsivenessAsync(testDoneCallback) {
165   var tester = new KeysetTransitionTester();
166
167   tester.init = function() {
168     $('keyboard').inputTypeValue = 'text';
169   };
170
171   // Shift-lock
172   tester.keyEvent(Alignment.LEFT, Key.SHIFT, EventType.KEY_DOWN,
173       Keyset.DEFAULT_LOWER);
174   tester.wait(1000, Keyset.DEFAULT_UPPER);
175   tester.keyEvent(Alignment.LEFT, Key.SHIFT, EventType.KEY_UP,
176       Keyset.DEFAULT_UPPER);
177
178   // Force keyset tranistions via input type changes. Should reset to lowercase
179   // once back to the text keyset.
180   tester.transition(InputType.NUMBER, Keyset.DEFAULT_UPPER);
181   tester.transition(InputType.TEXT, Keyset.KEYPAD);
182   tester.verifyReset(Keyset.DEFAULT_LOWER);
183
184   tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
185 }
186
187 /**
188  * Tests that keyset transitions work as expected.
189  * The test is run asynchronously since the keyboard loads keysets dynamically.
190  * @param {Function} testDoneCallback The function to be called on completion.
191  */
192 function testKeysetTransitionsAsync(testDoneCallback) {
193
194   var tester = new KeysetTransitionTester('qwerty');
195
196   /**
197    * Checks the transitions using the shift key with the given alignment.
198    * @param {string} alignment The alignment of the shift key.
199    */
200   var checkBasicTransitions = function(alignment) {
201     // Test the path abc -> symbol -> more -> symbol -> abc.
202     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
203         Keyset.LOWER);
204     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
205     tester.keyEvent(Alignment.LEFT, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
206         Keyset.SYMBOL);
207     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP,
208         Keyset.MORE_SYMBOLS);
209     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
210         Keyset.MORE_SYMBOLS);
211     tester.keyEvent(Alignment.LEFT, Key.MORE_SYMBOLS, EventType.KEY_UP,
212         Keyset.SYMBOL);
213     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_DOWN,
214         Keyset.SYMBOL);
215     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP,
216         Keyset.LOWER);
217
218     // Test the path abc -> symbol -> more -> abc.
219     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
220         Keyset.LOWER);
221     // Mock keyUp on the abc since it occupies the space where the
222     // symbol key used to be.
223     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
224     tester.keyEvent(Alignment.LEFT, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
225         Keyset.SYMBOL);
226     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP,
227         Keyset.MORE_SYMBOLS);
228     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_DOWN,
229         Keyset.MORE_SYMBOLS);
230     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP, Keyset.LOWER);
231
232     // Test the path abc ->  highlighted ABC -> symbol -> abc.
233     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.LOWER);
234     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.UPPER);
235     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
236         Keyset.UPPER);
237     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
238     tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_DOWN,
239         Keyset.SYMBOL);
240     tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP, Keyset.LOWER);
241   };
242   // Tests the transitions using the left shift key.
243   checkBasicTransitions(Alignment.LEFT);
244   // Tests the transitions using the right shift key.
245   checkBasicTransitions(Alignment.RIGHT);
246
247   tester.scheduleTest('testKeysetTransitionsAsync', testDoneCallback);
248 }
249
250 /**
251  * Tests that we transition to uppercase on punctuation followed by a space.
252  * The test is run asynchronously since the keyboard loads keysets dynamically.
253  * @param {Function} testDoneCallback The function to be called on completion.
254  */
255 function testUpperOnSpaceAfterPunctuation(testDoneCallback) {
256   var tester = new KeysetTransitionTester('qwerty');
257   tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
258   tester.typeKey('.', 0xBE, Modifier.NONE, Keyset.LOWER);
259   tester.typeKey(' ', 0x20, Modifier.NONE, Keyset.LOWER);
260   tester.typeKey('A', 0x41, Modifier.SHIFT, Keyset.UPPER);
261   tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
262   tester.scheduleTest('testUpperOnSpaceAfterPunctuation', testDoneCallback);
263 }