Chromium Code Reviews| Index: chrome/test/data/chromeos/virtual_keyboard/inputview/test_base.js |
| diff --git a/chrome/test/data/chromeos/virtual_keyboard/inputview/test_base.js b/chrome/test/data/chromeos/virtual_keyboard/inputview/test_base.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fb5b1b956fe265d58105dd38cd4301c54b6032e4 |
| --- /dev/null |
| +++ b/chrome/test/data/chromeos/virtual_keyboard/inputview/test_base.js |
| @@ -0,0 +1,91 @@ |
| +/* |
| + * Copyright 2014 The Chromium Authors. All rights reserved. |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +var mockController; |
| +var mockTimer; |
| +var setComposition; |
| + |
| +var DEFAULT_CONTEXT_ID = 0; |
| + |
| +/** |
| + * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API |
| + * calls must set expectations for call signatures. |
| + */ |
| +function setUp() { |
| + mockController = new MockController(); |
| + mockTimer = new MockTimer(); |
| + |
| + mockTimer.install(); |
| + mockController.createFunctionMock(chrome.input.ime, 'setComposition'); |
| + mockController.createFunctionMock(chrome.input.ime, 'commitText'); |
| + |
| + var validateCommit = function(index, expected, observed) { |
| + // Only consider the first argument, the details object. |
| + var expectedEvent = expected[0]; |
| + var observedEvent = observed[0]; |
| + assertEquals(expectedEvent.text, |
| + observedEvent.text, |
| + 'Mismatched commit text.'); |
| + }; |
| + chrome.input.ime.commitText.validateCall = validateCommit; |
| + setComposition = chrome.input.ime.setComposition; |
| + // Mocks setComposition manually to immediately callback. |
| + chrome.input.ime.setComposition = function(obj, callback) { |
| + callback(); |
| + } |
| + // TODO(rsadam): Mock additional extension API calls as required. |
| +} |
| + |
| +/** |
| + * Verify that API calls match expectations. |
| + */ |
| +function tearDown() { |
| + mockController.verifyMocks(); |
| + mockController.reset(); |
| + mockTimer.uninstall(); |
| + chrome.input.ime.setComposition = setComposition; |
| +} |
| + |
| +/** |
| + * Retrieves the key from the current keyset. |
| + * @param {String} char The character of the key. |
| + * @return {Object} The key. |
| + */ |
| +function getKey(char) { |
| + return document.querySelector('#Key' + char.toUpperCase()) |
| +} |
| + |
| +/** |
| + * Generates a mouse event and dispatches it on the target. |
| + * @param target {Object} The target of the event. |
| + * @param type {String} The type of the mouse event. |
| + */ |
| +function generateMouseEvent(target, type) { |
| + var e = document.createEvent('MouseEvents'); |
| + e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); |
| + target.dispatchEvent(e); |
| +} |
| + |
| +/** |
| + * Mocks a character type using the mouse. |
| + * @param {String} char The character to type. |
| + */ |
| +function mockMouseType(char) { |
| + var send = chrome.input.ime.commitText; |
| + send.addExpectation({ |
| + contextId: DEFAULT_CONTEXT_ID, |
| + text: char, |
| + }); |
| + var key = getKey(char); |
| + if (!key) { |
| + console.error("Cannot find key: " + char); |
| + return; |
| + } |
| + generateMouseEvent(key, 'mouseover', true, true); |
| + generateMouseEvent(key, 'mousedown', true, true); |
| + generateMouseEvent(key, 'mouseup', true, true); |
| + generateMouseEvent(key, 'click', true, true); |
|
Shu Chen
2014/04/22 19:14:43
You probably want to add:
generateMouseEvent(key
rsadam
2014/04/22 20:28:20
Done.
|
| +} |