2 * Copyright 2014 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.
11 var DEFAULT_CONTEXT_ID = 1;
14 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API
15 * calls must set expectations for call signatures.
18 mockController = new MockController();
19 mockTimer = new MockTimer();
22 mockController.createFunctionMock(chrome.input.ime, 'commitText');
24 var validateCommit = function(index, expected, observed) {
25 // Only consider the first argument, the details object.
26 var expectedEvent = expected[0];
27 var observedEvent = observed[0];
28 assertEquals(expectedEvent.text,
30 'Mismatched commit text.');
32 chrome.input.ime.commitText.validateCall = validateCommit;
33 setComposition = chrome.input.ime.setComposition;
34 // Mocks setComposition manually to immediately callback. The mock controller
35 // does not support callback functions.
36 chrome.input.ime.setComposition = function(obj, callback) {
39 window.setContext({'contextID': DEFAULT_CONTEXT_ID, 'type': 'text'});
40 // TODO(rsadam): Mock additional extension API calls as required.
44 * Verify that API calls match expectations.
47 mockController.verifyMocks();
48 mockController.reset();
49 mockTimer.uninstall();
50 chrome.input.ime.setComposition = setComposition;
54 * Retrieves the key from the current keyset.
55 * @param {String} char The character of the key.
56 * @return {Object} The key.
58 function getKey(char) {
59 return document.querySelector('#Key' + char.toUpperCase())
63 * Generates a mouse event and dispatches it on the target.
64 * @param target {Object} The target of the event.
65 * @param type {String} The type of the mouse event.
67 function generateMouseEvent(target, type) {
68 var e = new MouseEvent(type, {bubbles:true, cancelable:true});
69 target.dispatchEvent(e);
73 * Mocks a character type using the mouse.
74 * @param {String} char The character to type.
76 function mockMouseType(char) {
77 var send = chrome.input.ime.commitText;
79 contextId: DEFAULT_CONTEXT_ID,
82 var key = getKey(char);
84 console.error("Cannot find key: " + char);
87 generateMouseEvent(key, 'mouseover');
88 generateMouseEvent(key, 'mousedown');
89 generateMouseEvent(key, 'mouseup');
90 generateMouseEvent(key, 'click');
91 generateMouseEvent(key, 'mouseover');
92 generateMouseEvent(key, 'mouseout');