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.
8 * Special keys for toggling keyset transitions.
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.
34 function KeysetTransitionTester(opt_layout) {
35 this.layout = opt_layout;
39 KeysetTransitionTester.prototype = {
41 * Extends the subtask scheduler.
43 __proto__: SubtaskScheduler.prototype,
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
54 keyEvent: function(alignment, key, eventType, keysetId) {
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].classList.contains(key)) {
67 candidates[i][eventType]({pointerId: 1, isPrimary:true});
71 throw new Error('Unable to find \'' + key + '\' key in ' + keysetId);
73 this.addWaitCondition(fn, keysetId);
78 * Adds a task for mocking a key typed event.
79 * @param {string} key Text label on the key.
80 * @param {number} keyCode The legacy key code for the character.
81 * @param {number} modifiers Indicates which if any of the shift, control and
82 * alt keys are being virtually pressed.
83 * @param {string} keysetId Name of the expected keyset at the start of the
86 typeKey: function(key, keyCode, modifiers, keysetId) {
88 var fn = function () {
89 Debug('Generating key typed event for key = ' + key + " and keycode = "
91 self.verifyKeyset(keysetId, 'Unexpected keyset.');
92 mockTypeCharacter(key, keyCode, modifiers)
94 this.addWaitCondition(fn, keysetId);
99 * Updates the input type.
100 * @param {string} inputType The new input type.
101 * @param {string} keysetId Expected keyset at the start of the task.
103 transition: function(inputType, keysetId) {
105 var fn = function() {
106 self.verifyKeyset(keysetId, 'Unexpected keyset');
107 Debug('changing input type to ' + inputType);
108 $('keyboard').inputTypeValue = inputType;
110 this.addWaitCondition(fn, keysetId);
116 * Tests that capitalizion persists on keyset transitions.
117 * The test is run asynchronously since the keyboard loads keysets dynamically.
118 * @param {Function} testDoneCallback The function to be called on completion.
120 function testPersistantCapitalizationAsync(testDoneCallback) {
121 var tester = new KeysetTransitionTester(Layout.DEFAULT);
124 * Checks persistant capitalization using the shift key with the given
126 * @param {string} alignment The alignment of the shift key.
128 var checkPersistantCapitalization = function(alignment) {
130 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.LOWER);
131 tester.wait(1000, Keyset.UPPER);
132 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.UPPER);
134 // text -> symbol -> more -> text
135 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_DOWN,
137 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_UP,
139 tester.keyEvent(Alignment.CENTER, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
141 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
142 Keyset.MORE_SYMBOLS);
143 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_DOWN,
144 Keyset.MORE_SYMBOLS);
145 // Symbol key only has center alignment.
146 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
149 // switch back to lower case
150 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN,
152 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.LOWER);
154 // Run the test using the left shift key.
155 checkPersistantCapitalization(Alignment.LEFT);
156 // Run the test using the right shift key.
157 checkPersistantCapitalization(Alignment.RIGHT);
159 tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
163 * Tests that changing the input type changes the layout. The test is run
164 * asynchronously since the keyboard loads keysets dynamically.
165 * @param {Function} testDoneCallback The function to be called on completion.
167 function testInputTypeResponsivenessAsync(testDoneCallback) {
168 var tester = new KeysetTransitionTester();
170 tester.init = function() {
171 $('keyboard').inputTypeValue = 'text';
175 tester.keyEvent(Alignment.LEFT, Key.SHIFT, EventType.KEY_DOWN,
176 Keyset.DEFAULT_LOWER);
177 tester.wait(1000, Keyset.DEFAULT_UPPER);
178 tester.keyEvent(Alignment.LEFT, Key.SHIFT, EventType.KEY_UP,
179 Keyset.DEFAULT_UPPER);
181 // Force keyset tranistions via input type changes. Should reset to lowercase
182 // once back to the text keyset.
183 tester.transition(InputType.NUMBER, Keyset.DEFAULT_UPPER);
184 tester.transition(InputType.TEXT, Keyset.KEYPAD);
185 tester.verifyReset(Keyset.DEFAULT_LOWER);
187 tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
191 * Tests that keyset transitions work as expected.
192 * The test is run asynchronously since the keyboard loads keysets dynamically.
193 * @param {Function} testDoneCallback The function to be called on completion.
195 function testKeysetTransitionsAsync(testDoneCallback) {
197 var tester = new KeysetTransitionTester('qwerty');
200 * Checks the transitions using the shift key with the given alignment.
201 * @param {string} alignment The alignment of the shift key.
203 var checkBasicTransitions = function(alignment) {
204 // Test the path abc -> symbol -> more -> symbol -> abc.
205 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_DOWN,
207 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_UP,
209 tester.keyEvent(Alignment.CENTER, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
211 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
212 Keyset.MORE_SYMBOLS);
213 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_DOWN,
214 Keyset.MORE_SYMBOLS);
215 tester.keyEvent(Alignment.CENTER, Key.MORE_SYMBOLS, EventType.KEY_UP,
217 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_DOWN,
219 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
222 // Test the path abc -> symbol -> more -> abc.
223 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_DOWN,
225 // Mock keyUp on the abc since it occupies the space where the
226 // symbol key used to be.
227 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_UP,
229 tester.keyEvent(Alignment.CENTER, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
231 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
232 Keyset.MORE_SYMBOLS);
233 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_DOWN,
234 Keyset.MORE_SYMBOLS);
235 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
238 // Test the path abc -> highlighted ABC -> symbol -> abc.
239 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.LOWER);
240 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.UPPER);
241 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_DOWN,
243 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_UP,
245 tester.keyEvent(Alignment.CENTER, Key.TEXT, EventType.KEY_DOWN,
247 tester.keyEvent(Alignment.CENTER, Key.SYMBOL, EventType.KEY_UP,
250 // Tests the transitions using the left shift key.
251 checkBasicTransitions(Alignment.LEFT);
252 // Tests the transitions using the right shift key.
253 checkBasicTransitions(Alignment.RIGHT);
255 tester.scheduleTest('testKeysetTransitionsAsync', testDoneCallback);
259 * Tests that we transition to uppercase on punctuation followed by a space.
260 * The test is run asynchronously since the keyboard loads keysets dynamically.
261 * @param {Function} testDoneCallback The function to be called on completion.
263 function testUpperOnSpaceAfterPunctuation(testDoneCallback) {
264 var tester = new KeysetTransitionTester('qwerty');
265 tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
266 tester.typeKey('.', 0xBE, Modifier.NONE, Keyset.LOWER);
267 tester.typeKey(' ', 0x20, Modifier.NONE, Keyset.LOWER);
268 tester.typeKey('A', 0x41, Modifier.SHIFT, Keyset.UPPER);
269 tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
270 // Test that we do not enter upper if there is a character between the . and
272 tester.typeKey('.', 0xBE, Modifier.NONE, Keyset.LOWER);
273 tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
274 tester.typeKey(' ', 0x20, Modifier.NONE, Keyset.LOWER);
275 tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
276 // Test the newline also causes a transition to upper.
277 tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
278 tester.typeKey('.', 0xBE, Modifier.NONE, Keyset.LOWER);
279 tester.typeKey('\n', 0x0D, Modifier.NONE, Keyset.LOWER);
280 tester.typeKey('A', 0x41, Modifier.SHIFT, Keyset.UPPER);
281 tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
282 tester.scheduleTest('testUpperOnSpaceAfterPunctuation', testDoneCallback);