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][eventType]({pointerId: 1, isPrimary:true});
70 throw new Error('Unable to find \'' + key + '\' key in ' + keysetId);
72 this.addWaitCondition(fn, keysetId);
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
85 typeKey: function(key, keyCode, modifiers, keysetId) {
87 var fn = function () {
88 Debug('Generating key typed event for key = ' + key + " and keycode = "
90 self.verifyKeyset(keysetId, 'Unexpected keyset.');
91 mockTypeCharacter(key, keyCode, modifiers)
93 this.addWaitCondition(fn, keysetId);
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.
102 transition: function(inputType, keysetId) {
104 var fn = function() {
105 self.verifyKeyset(keysetId, 'Unexpected keyset');
106 Debug('changing input type to ' + inputType);
107 $('keyboard').inputTypeValue = inputType;
109 this.addWaitCondition(fn, keysetId);
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.
119 function testPersistantCapitalizationAsync(testDoneCallback) {
120 var tester = new KeysetTransitionTester(Layout.DEFAULT);
123 * Checks persistant capitalization using the shift key with the given
125 * @param {string} alignment The alignment of the shift key.
127 var checkPersistantCapitalization = function(alignment) {
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);
133 // text -> symbol -> more -> text
134 tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
136 tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
137 tester.keyEvent(Alignment.LEFT, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
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);
146 // switch back to lower case
147 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN,
149 tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.LOWER);
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);
156 tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
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.
164 function testInputTypeResponsivenessAsync(testDoneCallback) {
165 var tester = new KeysetTransitionTester();
167 tester.init = function() {
168 $('keyboard').inputTypeValue = 'text';
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);
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);
184 tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
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.
192 function testKeysetTransitionsAsync(testDoneCallback) {
194 var tester = new KeysetTransitionTester('qwerty');
197 * Checks the transitions using the shift key with the given alignment.
198 * @param {string} alignment The alignment of the shift key.
200 var checkBasicTransitions = function(alignment) {
201 // Test the path abc -> symbol -> more -> symbol -> abc.
202 tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
204 tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
205 tester.keyEvent(Alignment.LEFT, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
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,
213 tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_DOWN,
215 tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP,
218 // Test the path abc -> symbol -> more -> abc.
219 tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_DOWN,
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,
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);
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,
237 tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
238 tester.keyEvent(Alignment.LEFT, Key.TEXT, EventType.KEY_DOWN,
240 tester.keyEvent(Alignment.LEFT, Key.SYMBOL, EventType.KEY_UP, Keyset.LOWER);
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);
247 tester.scheduleTest('testKeysetTransitionsAsync', testDoneCallback);
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.
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);