- add sources.
[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  * Returns the unique transition key identified by the innerText,
9  * alignment and keyset it is in.
10  * @param {string} alignment The alignment of the key. One of {left,right}.
11  * @param {string} initialKeyset The keyset the key is in.
12  * @param {string} text The inner text of the key we want.
13  * @return {Object} The unique key which satisfies these properties.
14  */
15 function getTransitionKey(alignment, initialKeyset, text) {
16   var keyset = keyboard.querySelector('#qwerty-' + initialKeyset);
17   assertTrue(!!keyset, "Keyset " + initialKeyset + " not found.");
18   var candidates = keyset.querySelectorAll('[align="' +
19       alignment +'"]').array();
20   assertTrue(candidates.length > 0, "No " +
21       alignment + " aligned keys found.");
22   var results = candidates.filter(function(key) {
23         return key.innerText == text;
24       });
25   assertEquals(1, results.length, 'Unexpected number of ' +
26       text + ' keys with alignment: ' + alignment);
27   return results[0];
28 }
29
30 // helper functions for getting different keys.
31 function getSymbolKey(alignment, initialKeyset) {
32   return getTransitionKey(alignment, initialKeyset, '#123');
33 }
34
35 function getAbcKey(alignment, initialKeyset) {
36   return getTransitionKey(alignment, initialKeyset, 'abc');
37 }
38
39 function getMoreKey(alignment) {
40   return getTransitionKey(alignment, 'symbol', 'more');
41 }
42
43 function getShiftKey(alignment, keyset) {
44   return getTransitionKey(alignment, keyset, 'shift');
45 }
46
47 /**
48  * Tests all the basic transitions between keysets using keys
49  * of a particular alignment.
50  * @param {string} alignment The alignment of the keys to test
51  */
52 function checkBasicTransitions(alignment) {
53   var mockEvent = {pointerId:1, isPrimary:true};
54   assertEquals('lower', keyboard.keyset,
55       "Unexpected initial keyset.");
56
57   // Test the path abc -> symbol -> more -> symbol -> abc.
58   var symbol = getSymbolKey(alignment, 'lower');
59   symbol.down(mockEvent);
60   symbol.up(mockEvent);
61   assertEquals('symbol', keyboard.keyset,
62       "Did not transition from lower to symbol keyset. ");
63
64   var more = getMoreKey(alignment);
65   more.down(mockEvent);
66   more.up(mockEvent);
67   assertEquals('more', keyboard.keyset,
68       "Did not transition from symbol to more keyset.");
69
70   symbol = getSymbolKey(alignment, 'more');
71   symbol.down(mockEvent);
72   symbol.up(mockEvent);
73   assertEquals('symbol', keyboard.keyset,
74       "Did not transition from  more to symbol. ");
75
76   var abc = getAbcKey(alignment, 'symbol');
77   abc.down(mockEvent);
78   abc.up(mockEvent);
79   assertEquals('lower', keyboard.keyset,
80       "Did not transition from symbol to lower keyset. ");
81
82   // Test the path abc -> symbol -> more -> abc.
83   symbol = getSymbolKey(alignment, 'lower');
84   symbol.down(mockEvent);
85   symbol.up(mockEvent);
86   assertEquals('symbol', keyboard.keyset,
87       "Did not transition from lower to symbol keyset in second path. ");
88
89   more = getMoreKey(alignment);
90   more.down(mockEvent);
91   more.up(mockEvent);
92   assertEquals('more', keyboard.keyset,
93       "Did not transition from symbol to more keyset in second path.");
94   abc = getAbcKey(alignment, 'more');
95   abc.down(mockEvent);
96   abc.up(mockEvent);
97   assertEquals('lower', keyboard.keyset,
98       "Did not transition from more to lower keyset. ");
99 }
100
101 /**
102  * Tests that capitalization persists among keyset transitions
103  * when using keys of a particular alignment.
104  * @param {string} alignment The alignment of the key to transition with.
105  */
106 function checkPersistantCapitalization(alignment) {
107   var mockEvent = {pointerId:1, isPrimary:true};
108   assertEquals('lower', keyboard.keyset,
109       "Unexpected initial keyset.");
110
111   var lowerShift = getShiftKey(alignment, 'lower');
112   lowerShift.down(mockEvent);
113   // Long press to capslock.
114   mockTimer.tick(1000);
115   assertEquals('upper', keyboard.keyset,
116       "Did not transition to locked keyset on long press");
117
118   var symbol = getSymbolKey(alignment, 'upper');
119   symbol.down(mockEvent);
120   symbol.up(mockEvent);
121   assertEquals('symbol', keyboard.keyset,
122       "Did not transition from upper to symbol keyset. ");
123   var more = getMoreKey(alignment);
124   more.down(mockEvent);
125   more.up(mockEvent);
126   assertEquals('more', keyboard.keyset,
127       "Did not transition from symbol to more keyset.");
128   var abc = getAbcKey(alignment, 'more');
129   abc.down(mockEvent);
130   abc.up(mockEvent);
131   assertEquals('upper', keyboard.keyset,
132       "Did not persist capitalization on keyset transition. ");
133
134   // Reset to lower
135   var upperShift = getShiftKey(alignment, 'upper');
136   upperShift.up(mockEvent);
137   lowerShift.down(mockEvent);
138   assertEquals('lower', keyboard.keyset,
139       "Unexpected final keyset.");
140 }
141
142 /**
143  * Tests that capitalizion persists on keyset transitions.
144  * The test is run asynchronously since the keyboard loads keysets dynamically.
145  * @param {function} testDoneCallback The function to be called on completion.
146  */
147 function testPersistantCapitalizationAsync(testDoneCallback) {
148   var runTest = function() {
149   var alignments = ['left', 'right'];
150     for (var i in alignments) {
151       checkPersistantCapitalization(alignments[i]);
152     }
153   };
154   onKeyboardReady('testPersistantCapitalizationAsync',
155       runTest, testDoneCallback);
156 }
157
158 /**
159  * Tests that changing the input type changes the layout. The test is run
160  * asynchronously since the keyboard loads keysets dynamically.
161  * @param {function} testDoneCallback The function to be called on completion.
162  */
163 function testInputTypeResponsivenessAsync(testDoneCallback) {
164   var testName = "testInputTypeResponsivenessAsync";
165   var transition = function (expectedKeyset, nextInputType, error) {
166     return function() {
167       assertEquals(expectedKeyset, keyboard.activeKeysetId, error);
168       keyboard.inputTypeValue = nextInputType;
169     }
170   }
171
172   var setupWork = function() {
173     // Check initial state.
174     assertEquals('qwerty-lower', keyboard.activeKeysetId,
175         "Unexpected initial active keyset");
176     // Check that capitalization is not persistant
177     var lowerShift = getShiftKey('left', 'lower');
178     var upperShift = getShiftKey('left', 'upper');
179     var mockEvent = {isPrimary: true, pointerId: 1};
180     lowerShift.down(mockEvent);
181     mockTimer.tick(1000);
182     upperShift.up(mockEvent);
183     assertEquals('qwerty-upper', keyboard.activeKeysetId,
184         "Unexpected transition on long press.");
185     keyboard.inputTypeValue = 'text';
186   }
187
188   var subtasks = [];
189   subtasks.push(transition('qwerty-lower', 'number',
190       "Did not reset keyboard on focus change"));
191   subtasks.push(transition('numeric-symbol', 'text',
192       "Did not switch to numeric keypad."));
193   // Clean up
194   subtasks.push(function() {
195    assertEquals('qwerty-lower', keyboard.activeKeysetId,
196         "Unexpected final active keyset");
197   });
198   onKeyboardReady(testName, setupWork, testDoneCallback, subtasks);
199 }
200
201  /**
202  * Tests that keyset transitions work as expected.
203  * The test is run asynchronously since the keyboard loads keysets dynamically.
204  * @param {function} testDoneCallback The function to be called on completion.
205  */
206 function testKeysetTransitionsAsync(testDoneCallback) {
207   var runTest = function() {
208     var alignments = ['left', 'right'];
209     for (var i in alignments) {
210       checkBasicTransitions(alignments[i]);
211     }
212   };
213   onKeyboardReady('testKeysetTransitionsAsync',
214      runTest, testDoneCallback);
215 }