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 * 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.
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;
25 assertEquals(1, results.length, 'Unexpected number of ' +
26 text + ' keys with alignment: ' + alignment);
30 // helper functions for getting different keys.
31 function getSymbolKey(alignment, initialKeyset) {
32 return getTransitionKey(alignment, initialKeyset, '#123');
35 function getAbcKey(alignment, initialKeyset) {
36 return getTransitionKey(alignment, initialKeyset, 'abc');
39 function getMoreKey(alignment) {
40 return getTransitionKey(alignment, 'symbol', 'more');
43 function getShiftKey(alignment, keyset) {
44 return getTransitionKey(alignment, keyset, 'shift');
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
52 function checkBasicTransitions(alignment) {
53 var mockEvent = {pointerId:1, isPrimary:true};
54 assertEquals('lower', keyboard.keyset,
55 "Unexpected initial keyset.");
57 // Test the path abc -> symbol -> more -> symbol -> abc.
58 var symbol = getSymbolKey(alignment, 'lower');
59 symbol.down(mockEvent);
61 assertEquals('symbol', keyboard.keyset,
62 "Did not transition from lower to symbol keyset. ");
64 var more = getMoreKey(alignment);
67 assertEquals('more', keyboard.keyset,
68 "Did not transition from symbol to more keyset.");
70 symbol = getSymbolKey(alignment, 'more');
71 symbol.down(mockEvent);
73 assertEquals('symbol', keyboard.keyset,
74 "Did not transition from more to symbol. ");
76 var abc = getAbcKey(alignment, 'symbol');
79 assertEquals('lower', keyboard.keyset,
80 "Did not transition from symbol to lower keyset. ");
82 // Test the path abc -> symbol -> more -> abc.
83 symbol = getSymbolKey(alignment, 'lower');
84 symbol.down(mockEvent);
86 assertEquals('symbol', keyboard.keyset,
87 "Did not transition from lower to symbol keyset in second path. ");
89 more = getMoreKey(alignment);
92 assertEquals('more', keyboard.keyset,
93 "Did not transition from symbol to more keyset in second path.");
94 abc = getAbcKey(alignment, 'more');
97 assertEquals('lower', keyboard.keyset,
98 "Did not transition from more to lower keyset. ");
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.
106 function checkPersistantCapitalization(alignment) {
107 var mockEvent = {pointerId:1, isPrimary:true};
108 assertEquals('lower', keyboard.keyset,
109 "Unexpected initial keyset.");
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");
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);
126 assertEquals('more', keyboard.keyset,
127 "Did not transition from symbol to more keyset.");
128 var abc = getAbcKey(alignment, 'more');
131 assertEquals('upper', keyboard.keyset,
132 "Did not persist capitalization on keyset transition. ");
135 var upperShift = getShiftKey(alignment, 'upper');
136 upperShift.up(mockEvent);
137 lowerShift.down(mockEvent);
138 assertEquals('lower', keyboard.keyset,
139 "Unexpected final keyset.");
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.
147 function testPersistantCapitalizationAsync(testDoneCallback) {
148 var runTest = function() {
149 var alignments = ['left', 'right'];
150 for (var i in alignments) {
151 checkPersistantCapitalization(alignments[i]);
154 onKeyboardReady('testPersistantCapitalizationAsync',
155 runTest, testDoneCallback);
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.
163 function testInputTypeResponsivenessAsync(testDoneCallback) {
164 var testName = "testInputTypeResponsivenessAsync";
165 var transition = function (expectedKeyset, nextInputType, error) {
167 assertEquals(expectedKeyset, keyboard.activeKeysetId, error);
168 keyboard.inputTypeValue = nextInputType;
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';
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."));
194 subtasks.push(function() {
195 assertEquals('qwerty-lower', keyboard.activeKeysetId,
196 "Unexpected final active keyset");
198 onKeyboardReady(testName, setupWork, testDoneCallback, subtasks);
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.
206 function testKeysetTransitionsAsync(testDoneCallback) {
207 var runTest = function() {
208 var alignments = ['left', 'right'];
209 for (var i in alignments) {
210 checkBasicTransitions(alignments[i]);
213 onKeyboardReady('testKeysetTransitionsAsync',
214 runTest, testDoneCallback);