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 * Mocks using the longpress candidate window to enter an alternate character.
9 * @param {string} label The label on the key.
10 * @param {string} candidateLabel The alternate character being typed.
11 * @param {number} keyCode The keyCode for the alternate character, which may
12 * be zero for characters not found on a QWERTY keyboard.
13 * @param {number} modifiers Indicates the state of the shift, control and alt
14 * key when entering the character.
15 * @param {Object.<string, boolean>=} opt_variant Optional test variant.
17 function mockLongPressType(label,
22 // Verify that candidates popup window is initally hidden.
23 var keyset = keyboard.querySelector('#' + keyboard.activeKeysetId);
24 assertTrue(!!keyset, 'Unable to find active keyset.');
25 var mockEvent = { pointerId:1 };
26 var candidatesPopup = keyset.querySelector('kb-altkey-container');
27 assertTrue(!!candidatesPopup, 'Unable to find altkey container.');
28 assertTrue(candidatesPopup.hidden,
29 'Candidate popup should be hidden initially.');
31 // Show candidate window of alternate keys on a long press.
32 var key = findKey(label);
34 assertTrue(!!key, 'Unable to find key labelled "' + label + '".');
37 if (opt_variant && opt_variant.noCandidates) {
38 assertTrue(candidatesPopup.hidden, 'Candidate popup should remain hidden.');
40 assertFalse(candidatesPopup.hidden, 'Candidate popup should be visible.');
42 // Verify that the popup window contains the candidate key.
43 var candidates = candidatesPopup.querySelectorAll('kb-altkey');
44 for (var i = 0; i < candidates.length; i++) {
45 if (candidates[i].innerText == candidateLabel) {
46 altKey = candidates[i];
50 assertTrue(!!altKey, 'Unable to find key in candidate list.');
53 var abortSelection = opt_variant && opt_variant.abortSelection;
54 if (!abortSelection) {
55 // Verify that the candidate key is typed on release of the longpress.
56 var send = chrome.virtualKeyboardPrivate.sendKeyEvent;
57 var unicodeValue = candidateLabel.charCodeAt(0);
60 charValue: unicodeValue,
66 charValue: unicodeValue,
72 // A keyout event should be dispatched before a keyover event.
74 altKey.over({pointerId: 1, relatedTarget: altKey});
76 altKey.out({pointerId: 1, relatedTarget: altKey});
80 // Verify that the candidate list is hidden on a pointer up event.
81 candidatesPopup.up(mockEvent);
82 assertTrue(candidatesPopup.hidden,
83 'Candidate popup should be hidden after inserting a character.');
90 * Tests that typing characters on the default lowercase keyboard triggers the
91 * correct sequence of events. The test is run asynchronously since the
92 * keyboard loads keysets dynamically.
94 function testLowercaseKeysetAsync(testDoneCallback) {
95 var runTest = function() {
96 // Keyboard defaults to lowercase.
97 mockTypeCharacter('a', 0x41, Modifier.NONE);
98 mockTypeCharacter('s', 0x53, Modifier.NONE);
99 mockTypeCharacter('.', 0xBE, Modifier.NONE);
100 mockTypeCharacter('\b', 0x08, Modifier.NONE, 0x08);
101 mockTypeCharacter('\t', 0x09, Modifier.NONE, 0x09);
102 mockTypeCharacter('\n', 0x0D, Modifier.NONE, 0x0A);
103 mockTypeCharacter(' ', 0x20, Modifier.NONE);
105 onKeyboardReady('testLowercaseKeysetAsync', runTest, testDoneCallback);
109 * Tests long press on a key that has alternate sugestions. For example,
110 * longpressing the 'a' key displays 'a acute' 'a grave', etc. Longpressing
111 * characters on the top row of the keyboard displays numbers as alternatives.
113 function testLongPressTypeAccentedCharacterAsync(testDoneCallback) {
114 var runTest = function() {
115 // Test popup for letters with candidate lists that are derived from a
116 // single source (hintText or accents).
117 // Type lowercase A grave
118 mockLongPressType('a', '\u00E0', 0, Modifier.NONE);
119 // Type the digit '1' (hintText on 'q' key).
120 mockLongPressType('q', '1', 0x31, Modifier.NONE);
122 // Test popup for letter that has a candidate list combining hintText and
124 // Type lowercase E acute.
125 mockLongPressType('e', '\u00E9', 0, Modifier.NONE);
126 // Type the digit '3' (hintText on the 'e' key).
127 mockLongPressType('e', '3', 0x33, Modifier.NONE);
129 // Mock longpress typing a character that does not have alternate
131 mockLongPressType('z', 'z', 0x5A, Modifier.NONE, {noCandidates: true});
133 // Mock aborting a longpress selection.
134 mockLongPressType('e', '3', 0x33, Modifier.NONE, {abortSelection: true});
136 onKeyboardReady('testLongPressTypeAccentedCharacterAsync',
142 * When typing quickly, one can often press a second key before releasing the
143 * first. This test confirms that both keys are typed in the correct order.
145 function testAutoReleasePreviousKey(testDoneCallback) {
146 var runTest = function() {
147 var key = findKey('a');
148 assertTrue(!!key, 'Unable to find key labelled "a".');
149 var unicodeValue = 'a'.charCodeAt(0);
150 var send = chrome.virtualKeyboardPrivate.sendKeyEvent;
151 send.addExpectation({
153 charValue: unicodeValue,
155 modifiers: Modifier.NONE
157 send.addExpectation({
159 charValue: unicodeValue,
161 modifiers: Modifier.NONE
163 var mockEvent = { pointerId:2 };
165 mockTypeCharacter('s', 0x53, Modifier.NONE);
167 onKeyboardReady('testAutoReleasePreviousKey', runTest, testDoneCallback);
171 * When touch typing, one can often press a key and move slightly out of the key
172 * area before releasing the key. This test confirms that the key is not
175 function testFingerOutType(testDoneCallback) {
176 var runTest = function() {
177 var key = findKey('a');
178 assertTrue(!!key, 'Unable to find key labelled "a".');
179 var unicodeValue = 'a'.charCodeAt(0);
180 var send = chrome.virtualKeyboardPrivate.sendKeyEvent;
182 // Test finger moves out of typed key slightly before release. The key
183 // should not be dropped.
184 send.addExpectation({
186 charValue: unicodeValue,
188 modifiers: Modifier.NONE
190 send.addExpectation({
192 charValue: unicodeValue,
194 modifiers: Modifier.NONE
196 var mockEvent = { pointerId:2 };
199 // Mocks finger releases after moved out of the 'a' key.
200 keyboard.up(mockEvent);
202 // Test a second finger types on a different key before first finger
203 // releases (yet moves out of the typed key). The first typed key should not
205 send.addExpectation({
207 charValue: unicodeValue,
209 modifiers: Modifier.NONE
211 send.addExpectation({
213 charValue: unicodeValue,
215 modifiers: Modifier.NONE
219 mockTypeCharacter('s', 0x53, Modifier.NONE);
221 onKeyboardReady('testFingerOutType', runTest, testDoneCallback);