Chromium Code Reviews| 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 = 0; | 11 var DEFAULT_CONTEXT_ID = 0; |
| 12 var LONGPRESS_DELAY = 1100; | |
| 13 | |
| 14 /** | |
| 15 * Key alignments. | |
| 16 * @enum {string} | |
| 17 */ | |
| 18 var Alignment = { | |
| 19 LEFT: 'left', | |
| 20 RIGHT: 'right', | |
| 21 CENTER: 'center' | |
| 22 }; | |
| 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(); |
| 20 | 31 |
| 21 mockTimer.install(); | 32 mockTimer.install(); |
| 22 mockController.createFunctionMock(chrome.input.ime, 'setComposition'); | |
| 23 mockController.createFunctionMock(chrome.input.ime, 'commitText'); | 33 mockController.createFunctionMock(chrome.input.ime, 'commitText'); |
| 24 | 34 |
| 25 var validateCommit = function(index, expected, observed) { | 35 var validateCommit = function(index, expected, observed) { |
| 26 // Only consider the first argument, the details object. | 36 // Only consider the first argument, the details object. |
| 27 var expectedEvent = expected[0]; | 37 var expectedEvent = expected[0]; |
| 28 var observedEvent = observed[0]; | 38 var observedEvent = observed[0]; |
| 29 assertEquals(expectedEvent.text, | 39 assertEquals(expectedEvent.text, |
| 30 observedEvent.text, | 40 observedEvent.text, |
| 31 'Mismatched commit text.'); | 41 'Mismatched commit text.'); |
| 32 }; | 42 }; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 48 mockTimer.uninstall(); | 58 mockTimer.uninstall(); |
| 49 chrome.input.ime.setComposition = setComposition; | 59 chrome.input.ime.setComposition = setComposition; |
| 50 } | 60 } |
| 51 | 61 |
| 52 /** | 62 /** |
| 53 * Retrieves the key from the current keyset. | 63 * Retrieves the key from the current keyset. |
| 54 * @param {String} char The character of the key. | 64 * @param {String} char The character of the key. |
| 55 * @return {Object} The key. | 65 * @return {Object} The key. |
| 56 */ | 66 */ |
| 57 function getKey(char) { | 67 function getKey(char) { |
| 58 return document.querySelector('#Key' + char.toUpperCase()) | 68 var key = document.querySelector('#Key' + char.toUpperCase()) |
| 69 assertTrue(!!key, "Cannot find key: " + char); | |
| 70 return key; | |
| 59 } | 71 } |
| 60 | 72 |
| 61 /** | 73 /** |
| 74 * Retrieves the shift key from the current keyset. | |
| 75 * @param {Alignment} align The alignment of the shift key. | |
| 76 * @return {Object} The key. | |
| 77 */ | |
|
bshe
2014/04/24 18:36:25
Assume this will be used when add upper case tests
rsadam
2014/04/24 21:23:25
Done.
| |
| 78 function getShiftKey(align) { | |
| 79 var id; | |
| 80 switch(align) { | |
| 81 case Alignment.LEFT: | |
| 82 id = 'ShiftLeft'; | |
| 83 break; | |
| 84 case Alignment.RIGHT: | |
| 85 id = 'ShiftRight'; | |
| 86 break; | |
| 87 default: | |
| 88 break; | |
| 89 } | |
| 90 assertTrue(!!id, "Invalid shift alignment option: " + align); | |
| 91 var shift = document.querySelector('#' + id); | |
| 92 assertTrue(!!shift, "Cannot find shift key with alignment: " + align); | |
| 93 return shift; | |
| 94 } | |
| 95 | |
| 96 /** | |
| 62 * Generates a mouse event and dispatches it on the target. | 97 * Generates a mouse event and dispatches it on the target. |
| 63 * @param target {Object} The target of the event. | 98 * @param target {Object} The target of the event. |
| 64 * @param type {String} The type of the mouse event. | 99 * @param type {String} The type of the mouse event. |
| 65 */ | 100 */ |
| 66 function generateMouseEvent(target, type) { | 101 function generateMouseEvent(target, type) { |
| 67 var e = document.createEvent('MouseEvents'); | 102 var e = document.createEvent('MouseEvents'); |
| 68 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); | 103 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); |
| 69 target.dispatchEvent(e); | 104 target.dispatchEvent(e); |
| 70 } | 105 } |
| 71 | 106 |
| 72 /** | 107 /** |
| 73 * Mocks a character type using the mouse. | 108 * Mocks a character type using the mouse. |
| 74 * @param {String} char The character to type. | 109 * @param {String} char The character to type. |
| 75 */ | 110 */ |
| 76 function mockMouseType(char) { | 111 function mockMouseType(char) { |
| 77 var send = chrome.input.ime.commitText; | 112 var send = chrome.input.ime.commitText; |
| 78 send.addExpectation({ | 113 send.addExpectation({ |
| 79 contextId: DEFAULT_CONTEXT_ID, | 114 contextId: DEFAULT_CONTEXT_ID, |
| 80 text: char, | 115 text: char, |
| 81 }); | 116 }); |
| 82 var key = getKey(char); | 117 var key = getKey(char); |
| 83 if (!key) { | |
| 84 console.error("Cannot find key: " + char); | |
| 85 return; | |
| 86 } | |
| 87 generateMouseEvent(key, 'mouseover', true, true); | 118 generateMouseEvent(key, 'mouseover', true, true); |
| 88 generateMouseEvent(key, 'mousedown', true, true); | 119 generateMouseEvent(key, 'mousedown', true, true); |
| 89 generateMouseEvent(key, 'mouseup', true, true); | 120 generateMouseEvent(key, 'mouseup', true, true); |
| 90 generateMouseEvent(key, 'click', true, true); | 121 generateMouseEvent(key, 'click', true, true); |
| 91 generateMouseEvent(key, 'mouseover', true, true); | 122 generateMouseEvent(key, 'mouseover', true, true); |
| 92 generateMouseEvent(key, 'mouseout', true, true); | 123 generateMouseEvent(key, 'mouseout', true, true); |
| 93 } | 124 } |
| 125 | |
| 126 /** | |
| 127 * Generates a touch event and dispatches it on the target. | |
| 128 * @param target {Object} The target of the event. | |
| 129 * @param type {String} The type of the touch event. | |
| 130 */ | |
| 131 function generateTouchEvent(target, type) { | |
| 132 var e = document.createEvent('UIEvents'); | |
| 133 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); | |
| 134 target.dispatchEvent(e); | |
| 135 } | |
| 136 | |
| 137 /** | |
| 138 * Mocks a character type using touch. | |
| 139 * @param {String} char The character to type. | |
| 140 */ | |
| 141 function mockTouchType(char) { | |
| 142 var send = chrome.input.ime.commitText; | |
| 143 send.addExpectation({ | |
| 144 contextId: DEFAULT_CONTEXT_ID, | |
| 145 text: char, | |
| 146 }); | |
| 147 var key = getKey(char); | |
| 148 generateTouchEvent(key, 'touchstart', true, true); | |
| 149 generateTouchEvent(key, 'touchend', true, true); | |
| 150 } | |
| 151 | |
| 152 /** | |
| 153 * Checks whether the element is currently being displayed on screen. | |
| 154 * @param {Object} The object to check. | |
|
bshe
2014/04/24 18:36:25
nit: document return value
rsadam
2014/04/24 21:23:25
Done.
| |
| 155 */ | |
| 156 function isActive(element) { | |
| 157 return getComputedStyle(element).display != "none"; | |
| 158 } | |
| 159 | |
| 160 /** | |
| 161 * Returns, if present, the active alternate key container. | |
| 162 * @return {?Object} | |
| 163 */ | |
| 164 function getActiveAltContainer() { | |
| 165 // TODO(rsadam): Simplify once code refactor to remove unneeded containers is | |
| 166 // complete. | |
| 167 var all = document.querySelectorAll('.inputview-altdata-view'); | |
| 168 var hit; | |
| 169 for (var i = 0; i < all.length; i++) { | |
| 170 if (isActive(all[i])) { | |
| 171 assertFalse(!!hit, "More than one alt key container is active."); | |
| 172 hit = all[i]; | |
| 173 } | |
| 174 } | |
| 175 return hit; | |
| 176 } | |
| 177 | |
| 178 /** | |
| 179 * Mocks a character long press. | |
| 180 * @param {String} char The character to longpress. | |
| 181 * @param {Array<String>} altKeys The expected alt keys. | |
| 182 * @param {String} selection The alt key to select. | |
| 183 */ | |
| 184 function mockLongpress(char, altKeys, selection) { | |
| 185 var key = getKey(char); | |
| 186 generateTouchEvent(key, 'touchstart', true, true); | |
| 187 mockTimer.tick(LONGPRESS_DELAY); | |
| 188 | |
| 189 var container = getActiveAltContainer(); | |
| 190 assertTrue(!!container, "Cannot find active alt container."); | |
| 191 var options = container.querySelectorAll('.inputview-altdata-key'); | |
| 192 assertEquals(altKeys.length, options.length, | |
| 193 "Unexpected number of alt keys."); | |
| 194 | |
| 195 var candidate; | |
| 196 // Check all altKeys present and in order specified. | |
| 197 for (var i = 0; i < altKeys.length; i++) { | |
| 198 assertEquals(altKeys[i],options[i].innerHTML); | |
| 199 if (options[i].innerHTML == selection) | |
| 200 candidate = options[i]; | |
| 201 } | |
| 202 // Check that selection key found. | |
| 203 assertTrue(!!candidate, "Could not find alt key to select: " + candidate); | |
| 204 // Expect selection to be typed | |
| 205 var send = chrome.input.ime.commitText; | |
| 206 send.addExpectation({ | |
| 207 contextId: DEFAULT_CONTEXT_ID, | |
| 208 text: selection, | |
| 209 }); | |
| 210 // TODO(rsadam:) Add support for touch move here once latest CRX uploaded to | |
|
rsadam
2014/04/24 17:45:04
The current CRX uses the delta in screen position
| |
| 211 // chrome/test/data. | |
| 212 generateTouchEvent(key, 'touchend', true, true) | |
|
bshe
2014/04/24 18:36:25
should the touchend dispatched by candidate?
rsadam
2014/04/24 21:23:25
I believe the W3 spec is for the target to be same
| |
| 213 container = getActiveAltContainer(); | |
| 214 assertFalse(!!container, "Alt key container was not hidden."); | |
| 215 } | |
| OLD | NEW |