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 /** |
62 * Generates a mouse event and dispatches it on the target. | 74 * Generates a mouse event and dispatches it on the target. |
63 * @param target {Object} The target of the event. | 75 * @param target {Object} The target of the event. |
64 * @param type {String} The type of the mouse event. | 76 * @param type {String} The type of the mouse event. |
65 */ | 77 */ |
66 function generateMouseEvent(target, type) { | 78 function generateMouseEvent(target, type) { |
67 var e = document.createEvent('MouseEvents'); | 79 var e = document.createEvent('MouseEvents'); |
68 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); | 80 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); |
69 target.dispatchEvent(e); | 81 target.dispatchEvent(e); |
70 } | 82 } |
71 | 83 |
72 /** | 84 /** |
73 * Mocks a character type using the mouse. | 85 * Mocks a character type using the mouse. |
74 * @param {String} char The character to type. | 86 * @param {String} char The character to type. |
75 */ | 87 */ |
76 function mockMouseType(char) { | 88 function mockMouseType(char) { |
77 var send = chrome.input.ime.commitText; | 89 var send = chrome.input.ime.commitText; |
78 send.addExpectation({ | 90 send.addExpectation({ |
79 contextId: DEFAULT_CONTEXT_ID, | 91 contextId: DEFAULT_CONTEXT_ID, |
80 text: char, | 92 text: char, |
81 }); | 93 }); |
82 var key = getKey(char); | 94 var key = getKey(char); |
83 if (!key) { | |
84 console.error("Cannot find key: " + char); | |
85 return; | |
86 } | |
87 generateMouseEvent(key, 'mouseover', true, true); | 95 generateMouseEvent(key, 'mouseover', true, true); |
88 generateMouseEvent(key, 'mousedown', true, true); | 96 generateMouseEvent(key, 'mousedown', true, true); |
89 generateMouseEvent(key, 'mouseup', true, true); | 97 generateMouseEvent(key, 'mouseup', true, true); |
90 generateMouseEvent(key, 'click', true, true); | 98 generateMouseEvent(key, 'click', true, true); |
91 generateMouseEvent(key, 'mouseover', true, true); | 99 generateMouseEvent(key, 'mouseover', true, true); |
92 generateMouseEvent(key, 'mouseout', true, true); | 100 generateMouseEvent(key, 'mouseout', true, true); |
93 } | 101 } |
| 102 |
| 103 /** |
| 104 * Generates a touch event and dispatches it on the target. |
| 105 * @param target {Object} The target of the event. |
| 106 * @param type {String} The type of the touch event. |
| 107 */ |
| 108 function generateTouchEvent(target, type) { |
| 109 var e = document.createEvent('UIEvents'); |
| 110 e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); |
| 111 target.dispatchEvent(e); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Mocks a character type using touch. |
| 116 * @param {String} char The character to type. |
| 117 */ |
| 118 function mockTouchType(char) { |
| 119 var send = chrome.input.ime.commitText; |
| 120 send.addExpectation({ |
| 121 contextId: DEFAULT_CONTEXT_ID, |
| 122 text: char, |
| 123 }); |
| 124 var key = getKey(char); |
| 125 generateTouchEvent(key, 'touchstart', true, true); |
| 126 generateTouchEvent(key, 'touchend', true, true); |
| 127 } |
| 128 |
| 129 /** |
| 130 * Checks whether the element is currently being displayed on screen. |
| 131 * @param {Object} The object to check. |
| 132 * @return {boolean} |
| 133 */ |
| 134 function isActive(element) { |
| 135 return getComputedStyle(element).display != "none"; |
| 136 } |
| 137 |
| 138 /** |
| 139 * Returns, if present, the active alternate key container. |
| 140 * @return {?Object} |
| 141 */ |
| 142 function getActiveAltContainer() { |
| 143 // TODO(rsadam): Simplify once code refactor to remove unneeded containers is |
| 144 // complete. |
| 145 var all = document.querySelectorAll('.inputview-altdata-view'); |
| 146 var hit; |
| 147 for (var i = 0; i < all.length; i++) { |
| 148 if (isActive(all[i])) { |
| 149 assertFalse(!!hit, "More than one alt key container is active."); |
| 150 hit = all[i]; |
| 151 } |
| 152 } |
| 153 return hit; |
| 154 } |
| 155 |
| 156 /** |
| 157 * Mocks a character long press. |
| 158 * @param {String} char The character to longpress. |
| 159 * @param {Array<String>} altKeys The expected alt keys. |
| 160 * @param {String} selection The alt key to select. |
| 161 */ |
| 162 function mockLongpress(char, altKeys, selection) { |
| 163 var key = getKey(char); |
| 164 generateTouchEvent(key, 'touchstart', true, true); |
| 165 mockTimer.tick(LONGPRESS_DELAY); |
| 166 |
| 167 var container = getActiveAltContainer(); |
| 168 assertTrue(!!container, "Cannot find active alt container."); |
| 169 var options = container.querySelectorAll('.inputview-altdata-key'); |
| 170 assertEquals(altKeys.length, options.length, |
| 171 "Unexpected number of alt keys."); |
| 172 |
| 173 var candidate; |
| 174 // Check all altKeys present and in order specified. |
| 175 for (var i = 0; i < altKeys.length; i++) { |
| 176 assertEquals(altKeys[i],options[i].innerHTML); |
| 177 if (options[i].innerHTML == selection) |
| 178 candidate = options[i]; |
| 179 } |
| 180 // Check that selection key found. |
| 181 assertTrue(!!candidate, "Could not find alt key to select: " + candidate); |
| 182 // Expect selection to be typed |
| 183 var send = chrome.input.ime.commitText; |
| 184 send.addExpectation({ |
| 185 contextId: DEFAULT_CONTEXT_ID, |
| 186 text: selection, |
| 187 }); |
| 188 // TODO(rsadam:) Add support for touch move here once latest CRX uploaded to |
| 189 // chrome/test/data. |
| 190 generateTouchEvent(key, 'touchend', true, true) |
| 191 container = getActiveAltContainer(); |
| 192 assertFalse(!!container, "Alt key container was not hidden."); |
| 193 } |
OLD | NEW |