OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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. | |
5 */ | |
6 | |
7 var mockController; | |
8 var mockTimer; | |
9 var setComposition; | |
10 | |
11 var DEFAULT_CONTEXT_ID = 0; | |
12 | |
13 /** | |
14 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API | |
15 * calls must set expectations for call signatures. | |
16 */ | |
17 function setUp() { | |
18 mockController = new MockController(); | |
19 mockTimer = new MockTimer(); | |
20 | |
21 mockTimer.install(); | |
22 mockController.createFunctionMock(chrome.input.ime, 'setComposition'); | |
23 mockController.createFunctionMock(chrome.input.ime, 'commitText'); | |
24 | |
25 var validateCommit = function(index, expected, observed) { | |
26 // Only consider the first argument, the details object. | |
27 var expectedEvent = expected[0]; | |
28 var observedEvent = observed[0]; | |
29 assertEquals(expectedEvent.text, | |
30 observedEvent.text, | |
31 'Mismatched commit text.'); | |
32 }; | |
33 chrome.input.ime.commitText.validateCall = validateCommit; | |
34 setComposition = chrome.input.ime.setComposition; | |
35 // Mocks setComposition manually to immediately callback. | |
36 chrome.input.ime.setComposition = function(obj, callback) { | |
37 callback(); | |
38 } | |
39 // TODO(rsadam): Mock additional extension API calls as required. | |
40 } | |
41 | |
42 /** | |
43 * Verify that API calls match expectations. | |
44 */ | |
45 function tearDown() { | |
46 mockController.verifyMocks(); | |
47 mockController.reset(); | |
48 mockTimer.uninstall(); | |
49 chrome.input.ime.setComposition = setComposition; | |
50 } | |
51 | |
52 /** | |
53 * Retrieves the key from the current keyset. | |
54 * @param {String} char The character of the key. | |
55 * @return {Object} The key. | |
56 */ | |
57 function getKey(char) { | |
58 return document.querySelector('#Key' + char.toUpperCase()) | |
59 } | |
60 | |
61 /** | |
62 * Generates a mouse event and dispatches it on the target. | |
63 * @param target {Object} The target of the event. | |
64 * @param type {String} The type of the mouse event. | |
65 */ | |
66 function generateMouseEvent(target, type) { | |
67 var e = document.createEvent('MouseEvents'); | |
68 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); | |
69 target.dispatchEvent(e); | |
70 } | |
71 | |
72 /** | |
73 * Mocks a character type using the mouse. | |
74 * @param {String} char The character to type. | |
75 */ | |
76 function mockMouseType(char) { | |
77 var send = chrome.input.ime.commitText; | |
78 send.addExpectation({ | |
79 contextId: DEFAULT_CONTEXT_ID, | |
80 text: char, | |
81 }); | |
82 var key = getKey(char); | |
83 if (!key) { | |
84 console.error("Cannot find key: " + char); | |
85 return; | |
86 } | |
87 generateMouseEvent(key, 'mouseover', true, true); | |
88 generateMouseEvent(key, 'mousedown', true, true); | |
89 generateMouseEvent(key, 'mouseup', true, true); | |
90 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.
| |
91 } | |
OLD | NEW |