ba7f67fddf112425235035713a04b760c90fed0b
[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: 'more',
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   var checkPersistantCapitalization = function(alignment) {
123     // Shift-lock
124     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.LOWER);
125     tester.wait(1000, Keyset.UPPER);
126     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.UPPER);
127
128      // text -> symbol -> more -> text
129     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_DOWN, Keyset.UPPER);
130     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
131     tester.keyEvent(alignment, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
132         Keyset.SYMBOL);
133     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP,
134         Keyset.MORE_SYMBOLS);
135     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_DOWN,
136         Keyset.MORE_SYMBOLS);
137     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP, Keyset.UPPER);
138
139     // switch back to lower case
140     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.UPPER);
141     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.LOWER);
142   };
143   checkPersistantCapitalization(Alignment.LEFT);
144   checkPersistantCapitalization(Alignment.RIGHT);
145
146   tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
147 }
148
149 /**
150  * Tests that changing the input type changes the layout. The test is run
151  * asynchronously since the keyboard loads keysets dynamically.
152  * @param {Function} testDoneCallback The function to be called on completion.
153  */
154 function testInputTypeResponsivenessAsync(testDoneCallback) {
155   var tester = new KeysetTransitionTester();
156
157   tester.init = function() {
158     $('keyboard').inputTypeValue = 'text';
159   };
160
161   // Shift-lock
162   tester.keyEvent(Alignment.LEFT, Key.SHIFT, EventType.KEY_DOWN,
163       Keyset.DEFAULT_LOWER);
164   tester.wait(1000, Keyset.DEFAULT_UPPER);
165   tester.keyEvent(Alignment.LEFT, Key.SHIFT, EventType.KEY_UP,
166       Keyset.DEFAULT_UPPER);
167
168   // Force keyset tranistions via input type changes. Should reset to lowercase
169   // once back to the text keyset.
170   tester.transition(InputType.NUMBER, Keyset.DEFAULT_UPPER);
171   tester.transition(InputType.TEXT, Keyset.KEYPAD);
172   tester.verifyReset(Keyset.DEFAULT_LOWER);
173
174   tester.scheduleTest('testInputTypeResponsivenessAsync', testDoneCallback);
175 }
176
177 /**
178  * Tests that keyset transitions work as expected.
179  * The test is run asynchronously since the keyboard loads keysets dynamically.
180  * @param {Function} testDoneCallback The function to be called on completion.
181  */
182 function testKeysetTransitionsAsync(testDoneCallback) {
183
184   var tester = new KeysetTransitionTester('qwerty');
185
186   var checkBasicTransitions = function(alignment) {
187     // Test the path abc -> symbol -> more -> symbol -> abc.
188     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_DOWN, Keyset.LOWER);
189     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
190     tester.keyEvent(alignment, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
191         Keyset.SYMBOL);
192     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP,
193         Keyset.MORE_SYMBOLS);
194     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_DOWN,
195         Keyset.MORE_SYMBOLS);
196     tester.keyEvent(alignment, Key.MORE_SYMBOLS, EventType.KEY_UP,
197         Keyset.SYMBOL);
198     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_DOWN, Keyset.SYMBOL);
199     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP, Keyset.LOWER);
200
201     // Test the path abc -> symbol -> more -> abc.
202     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_DOWN, Keyset.LOWER);
203     // Mock keyUp on the abc since it occupies the space where the
204     // symbol key used to be.
205     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
206     tester.keyEvent(alignment, Key.MORE_SYMBOLS, EventType.KEY_DOWN,
207         Keyset.SYMBOL);
208     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP,
209         Keyset.MORE_SYMBOLS);
210     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_DOWN,
211         Keyset.MORE_SYMBOLS);
212     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP, Keyset.LOWER);
213
214     // Test the path abc ->  highlighted ABC -> symbol -> abc.
215     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_DOWN, Keyset.LOWER);
216     tester.keyEvent(alignment, Key.SHIFT, EventType.KEY_UP, Keyset.UPPER);
217     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_DOWN, Keyset.UPPER);
218     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_UP, Keyset.SYMBOL);
219     tester.keyEvent(alignment, Key.TEXT, EventType.KEY_DOWN, Keyset.SYMBOL);
220     tester.keyEvent(alignment, Key.SYMBOL, EventType.KEY_UP, Keyset.LOWER);
221   };
222
223   checkBasicTransitions(Alignment.LEFT);
224   checkBasicTransitions(Alignment.RIGHT);
225
226   tester.scheduleTest('testKeysetTransitionsAsync', testDoneCallback);
227 }
228
229 /**
230  * Tests that we transition to uppercase on punctuation followed by a space.
231  * The test is run asynchronously since the keyboard loads keysets dynamically.
232  * @param {Function} testDoneCallback The function to be called on completion.
233  */
234 function testUpperOnSpaceAfterPunctuation(testDoneCallback) {
235   var tester = new KeysetTransitionTester('qwerty');
236   tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
237   tester.typeKey('.', 0xBE, Modifier.NONE, Keyset.LOWER);
238   tester.typeKey(' ', 0x20, Modifier.NONE, Keyset.LOWER);
239   tester.typeKey('A', 0x41, Modifier.SHIFT, Keyset.UPPER);
240   tester.typeKey('a', 0x41, Modifier.NONE, Keyset.LOWER);
241   tester.scheduleTest('testUpperOnSpaceAfterPunctuation', testDoneCallback);
242 }