- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / chromeos / virtual_keyboard / typing_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  * 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.
16  */
17 function mockLongPressType(label,
18                            candidateLabel,
19                            keyCode,
20                            modifiers,
21                            opt_variant) {
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.');
30
31   // Show candidate window of alternate keys on a long press.
32   var key = findKey(label);
33   var altKey = null;
34   assertTrue(!!key, 'Unable to find key labelled "' + label + '".');
35   key.down(mockEvent);
36   mockTimer.tick(1000);
37   if (opt_variant && opt_variant.noCandidates) {
38     assertTrue(candidatesPopup.hidden, 'Candidate popup should remain hidden.');
39   } else {
40     assertFalse(candidatesPopup.hidden, 'Candidate popup should be visible.');
41
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];
47          break;
48        }
49     }
50     assertTrue(!!altKey, 'Unable to find key in candidate list.');
51   }
52
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);
58     send.addExpectation({
59       type: 'keydown',
60       charValue: unicodeValue,
61       keyCode: keyCode,
62       modifiers: modifiers
63     });
64     send.addExpectation({
65       type: 'keyup',
66       charValue: unicodeValue,
67       keyCode: keyCode,
68       modifiers: modifiers
69     });
70   }
71   if (altKey) {
72     // A keyout event should be dispatched before a keyover event.
73     key.out(mockEvent);
74     altKey.over({pointerId: 1, relatedTarget: altKey});
75     if (abortSelection)
76       altKey.out({pointerId: 1, relatedTarget: altKey});
77     else
78       altKey.up(mockEvent);
79
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.');
84   } else {
85     key.up(mockEvent);
86   }
87 }
88
89 /**
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.
93  */
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);
104   };
105   onKeyboardReady('testLowercaseKeysetAsync', runTest, testDoneCallback);
106 }
107
108 /**
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.
112  */
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);
121
122     // Test popup for letter that has a candidate list combining hintText and
123     // accented letters.
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);
128
129     // Mock longpress typing a character that does not have alternate
130     // candidates.
131     mockLongPressType('z', 'z', 0x5A, Modifier.NONE, {noCandidates: true});
132
133     // Mock aborting a longpress selection.
134     mockLongPressType('e', '3', 0x33, Modifier.NONE, {abortSelection: true});
135   };
136   onKeyboardReady('testLongPressTypeAccentedCharacterAsync',
137                   runTest,
138                   testDoneCallback);
139 }
140
141 /**
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.
144  */
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({
152       type: 'keydown',
153       charValue: unicodeValue,
154       keyCode: 0x41,
155       modifiers: Modifier.NONE
156     });
157     send.addExpectation({
158       type: 'keyup',
159       charValue: unicodeValue,
160       keyCode: 0x41,
161       modifiers: Modifier.NONE
162     });
163     var mockEvent = { pointerId:2 };
164     key.down(mockEvent);
165     mockTypeCharacter('s', 0x53, Modifier.NONE);
166   };
167   onKeyboardReady('testAutoReleasePreviousKey', runTest, testDoneCallback);
168 }
169
170 /**
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
173  * dropped.
174  */
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;
181
182     // Test finger moves out of typed key slightly before release. The key
183     // should not be dropped.
184     send.addExpectation({
185       type: 'keydown',
186       charValue: unicodeValue,
187       keyCode: 0x41,
188       modifiers: Modifier.NONE
189     });
190     send.addExpectation({
191       type: 'keyup',
192       charValue: unicodeValue,
193       keyCode: 0x41,
194       modifiers: Modifier.NONE
195     });
196     var mockEvent = { pointerId:2 };
197     key.down(mockEvent);
198     key.out(mockEvent);
199     // Mocks finger releases after moved out of the 'a' key.
200     keyboard.up(mockEvent);
201
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
204     // be dropped.
205     send.addExpectation({
206       type: 'keydown',
207       charValue: unicodeValue,
208       keyCode: 0x41,
209       modifiers: Modifier.NONE
210     });
211     send.addExpectation({
212       type: 'keyup',
213       charValue: unicodeValue,
214       keyCode: 0x41,
215       modifiers: Modifier.NONE
216     });
217     key.down(mockEvent);
218     key.out(mockEvent);
219     mockTypeCharacter('s', 0x53, Modifier.NONE);
220   };
221   onKeyboardReady('testFingerOutType', runTest, testDoneCallback);
222 }