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 * Defers continuation of a test until a keyset is loaded.
9 * @param {string} keyset Name of the target keyset.
10 * @param {Function} continueTestCallback Callback function to invoke in order
13 function onKeysetReady(keyset
, continueTestCallback
) {
14 var container
= document
.querySelector('.inputview-container');
15 var bounds
= container
.getBoundingClientRect();
16 if (bounds
.bottom
> 0 && keyset
in controller
.container_
.keysetViewMap
&&
17 keyset
== controller
.currentKeyset_
) {
18 continueTestCallback();
21 setTimeout(function() {
22 onKeysetReady(keyset
, continueTestCallback
);
28 * Display an error message and abort the test.
29 * @param {string} message The error message.
31 function fail(message
) {
32 console
.error(message
);
33 window
.domAutomationController
.send(false);
38 * Mocks a touch event targeted on a key.
39 * @param {!Element} key .
40 * @param {string} eventType .
42 function mockTouchEvent(key
, eventType
) {
43 var rect
= key
.getBoundingClientRect();
44 var x
= rect
.left
+ rect
.width
/2;
45 var y
= rect
.top
+ rect
.height
/2;
46 var e
= document
.createEvent('UIEvent');
47 e
.initUIEvent(eventType
, true, true);
48 e
.touches
= [{pageX
: x
, pageY
: y
}];
55 * Simulates tapping on a key.
56 * @param {!Element} key .
58 function mockTap(key
) {
59 mockTouchEvent(key
, 'touchstart');
60 mockTouchEvent(key
, 'touchend');
65 * Returns the active keyboard view.
66 * @return {!HTMLElement}
68 function getActiveView() {
69 var container
= document
.querySelector('.inputview-container');
70 var views
= container
.querySelectorAll('.inputview-view');
71 for (var i
= 0; i
< views
.length
; i
++) {
72 var display
= views
[i
].style
.display
;
73 if (!display
|| display
!= 'none')
76 fail('Unable to find active keyboard view');
81 * Locates a key by label.
82 * @param {string} label The label on the key. If the key has multiple labels,
83 * |label| can match any of them.
84 * @returns {?Element} .
86 function findKey(label
) {
87 var view
= getActiveView();
88 candidates
= view
.querySelectorAll('.inputview-special-key-name');
89 for (var i
= 0; i
< candidates
.length
; i
++) {
90 if (candidates
[i
].textContent
== label
)
93 fail('Cannot find key labeled \'' + label
+ '\'');
97 // Wait for keyboard to finish loading asynchronously before tapping key.
98 onKeysetReady('us.compact.qwerty', function() {
99 mockTap(findKey('a'));