OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 var mockController; | 7 var mockController; |
8 var mockTimer; | 8 var mockTimer; |
9 var setComposition; | 9 var setComposition; |
10 | 10 |
11 var DEFAULT_CONTEXT_ID = 1; | 11 var DEFAULT_CONTEXT_ID = 1; |
12 var CAPSLOCK_ID = "OsLeft"; | |
13 | |
14 /** | |
15 * Key alignments. | |
16 * @enum {string} | |
17 */ | |
18 var Alignment = { | |
19 LEFT: 'left', | |
20 RIGHT: 'right', | |
21 CENTER: 'center' | |
22 } | |
bshe
2014/05/09 17:32:31
nit: add ";" back.
rsadam
2014/05/09 18:17:39
Done.
| |
12 | 23 |
13 /** | 24 /** |
14 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API | 25 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API |
15 * calls must set expectations for call signatures. | 26 * calls must set expectations for call signatures. |
16 */ | 27 */ |
17 function setUp() { | 28 function setUp() { |
18 mockController = new MockController(); | 29 mockController = new MockController(); |
19 mockTimer = new MockTimer(); | 30 mockTimer = new MockTimer(); |
31 mockTimer.install(); | |
20 | 32 |
21 mockTimer.install(); | |
22 mockController.createFunctionMock(chrome.input.ime, 'commitText'); | 33 mockController.createFunctionMock(chrome.input.ime, 'commitText'); |
23 | |
24 var validateCommit = function(index, expected, observed) { | 34 var validateCommit = function(index, expected, observed) { |
25 // Only consider the first argument, the details object. | 35 // Only consider the first argument, the details object. |
26 var expectedEvent = expected[0]; | 36 var expectedEvent = expected[0]; |
27 var observedEvent = observed[0]; | 37 var observedEvent = observed[0]; |
28 assertEquals(expectedEvent.text, | 38 assertEquals(expectedEvent.text, |
29 observedEvent.text, | 39 observedEvent.text, |
30 'Mismatched commit text.'); | 40 'Mismatched commit text.'); |
31 }; | 41 }; |
32 chrome.input.ime.commitText.validateCall = validateCommit; | 42 chrome.input.ime.commitText.validateCall = validateCommit; |
43 | |
33 setComposition = chrome.input.ime.setComposition; | 44 setComposition = chrome.input.ime.setComposition; |
34 // Mocks setComposition manually to immediately callback. The mock controller | 45 // Mocks setComposition manually to immediately callback. The mock controller |
35 // does not support callback functions. | 46 // does not support callback functions. |
36 chrome.input.ime.setComposition = function(obj, callback) { | 47 chrome.input.ime.setComposition = function(obj, callback) { |
37 callback(); | 48 callback(); |
38 } | 49 } |
39 window.setContext({'contextID': DEFAULT_CONTEXT_ID, 'type': 'text'}); | 50 window.setContext({'contextID': DEFAULT_CONTEXT_ID, 'type': 'text'}); |
40 // TODO(rsadam): Mock additional extension API calls as required. | 51 // TODO(rsadam): Mock additional extension API calls as required. |
41 } | 52 } |
42 | 53 |
(...skipping 20 matching lines...) Expand all Loading... | |
63 * Generates a mouse event and dispatches it on the target. | 74 * Generates a mouse event and dispatches it on the target. |
64 * @param target {Object} The target of the event. | 75 * @param target {Object} The target of the event. |
65 * @param type {String} The type of the mouse event. | 76 * @param type {String} The type of the mouse event. |
66 */ | 77 */ |
67 function generateMouseEvent(target, type) { | 78 function generateMouseEvent(target, type) { |
68 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); | 79 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); |
69 target.dispatchEvent(e); | 80 target.dispatchEvent(e); |
70 } | 81 } |
71 | 82 |
72 /** | 83 /** |
73 * Mocks a character type using the mouse. | 84 * Mocks a key type using the mouse. |
85 * @param {Object} key The key to click on. | |
86 */ | |
87 function mockMouseTypeOnKey(key) { | |
88 generateMouseEvent(key, 'mouseover'); | |
89 generateMouseEvent(key, 'mousedown'); | |
90 generateMouseEvent(key, 'mouseup'); | |
91 generateMouseEvent(key, 'click'); | |
92 generateMouseEvent(key, 'mouseover'); | |
93 generateMouseEvent(key, 'mouseout'); | |
94 } | |
95 | |
96 /** | |
97 * Mocks a character type using the mouse. Expects the character will be | |
98 * committed. | |
74 * @param {String} char The character to type. | 99 * @param {String} char The character to type. |
75 */ | 100 */ |
76 function mockMouseType(char) { | 101 function mockMouseType(char) { |
77 var send = chrome.input.ime.commitText; | 102 var send = chrome.input.ime.commitText; |
78 send.addExpectation({ | 103 send.addExpectation({ |
79 contextId: DEFAULT_CONTEXT_ID, | 104 contextId: DEFAULT_CONTEXT_ID, |
80 text: char, | 105 text: char, |
81 }); | 106 }); |
82 var key = getKey(char); | 107 var key = getKey(char); |
83 if (!key) { | 108 if (!key) { |
84 console.error("Cannot find key: " + char); | 109 console.error("Cannot find key: " + char); |
bshe
2014/05/09 17:32:31
Do we have any case that key may be null? it looks
rsadam
2014/05/09 18:17:39
Nice catch! This was a refactoring mistake between
| |
85 return; | 110 return; |
86 } | 111 } |
87 generateMouseEvent(key, 'mouseover'); | 112 mockMouseTypeOnKey(key); |
88 generateMouseEvent(key, 'mousedown'); | |
89 generateMouseEvent(key, 'mouseup'); | |
90 generateMouseEvent(key, 'click'); | |
91 generateMouseEvent(key, 'mouseover'); | |
92 generateMouseEvent(key, 'mouseout'); | |
93 } | 113 } |
114 | |
115 /** | |
116 * Generates a touch event and dispatches it on the target. | |
117 * @param target {Object} The target of the event. | |
118 * @param type {String} The type of the touch event. | |
119 */ | |
120 function generateTouchEvent(target, type) { | |
121 var e = document.createEvent('UIEvents'); | |
122 e.initEvent(type, true, true); | |
123 target.dispatchEvent(e); | |
124 } | |
125 | |
126 /** | |
127 * Mocks a character type using touch. | |
128 * @param {String} char The character to type. | |
129 */ | |
130 function mockTouchType(char) { | |
131 var send = chrome.input.ime.commitText; | |
132 send.addExpectation({ | |
133 contextId: DEFAULT_CONTEXT_ID, | |
134 text: char, | |
135 }); | |
136 var key = getKey(char); | |
137 generateTouchEvent(key, 'touchstart'); | |
138 generateTouchEvent(key, 'touchend'); | |
139 } | |
140 | |
141 /** | |
142 * Retrieves the shift key from the current keyset. | |
143 * @param {Alignment} align The alignment of the shift key. | |
144 * @return {Object} The key. | |
145 */ | |
146 function getShiftKey(align) { | |
147 var id; | |
148 switch(align) { | |
149 case Alignment.LEFT: | |
150 id = 'ShiftLeft'; | |
151 break; | |
152 case Alignment.RIGHT: | |
153 id = 'ShiftRight'; | |
154 break; | |
155 default: | |
156 break; | |
157 } | |
158 assertTrue(!!id, "Invalid shift alignment option: " + align); | |
159 var shift = document.querySelector('#' + id); | |
160 assertTrue(!!shift, "Cannot find shift key with alignment: " + align); | |
161 return shift; | |
162 } | |
OLD | NEW |