OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Namespace for test related things. | 6 * Namespace for test related things. |
7 */ | 7 */ |
8 var test = test || {}; | 8 var test = test || {}; |
9 | 9 |
10 /** | 10 /** |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 * Fakes pressing the down arrow until the given |filename| is selected. | 130 * Fakes pressing the down arrow until the given |filename| is selected. |
131 * | 131 * |
132 * @param {Window} contentWindow Window to be tested. | 132 * @param {Window} contentWindow Window to be tested. |
133 * @param {string} filename Name of the file to be selected. | 133 * @param {string} filename Name of the file to be selected. |
134 * @return {boolean} True if file got selected, false otherwise. | 134 * @return {boolean} True if file got selected, false otherwise. |
135 */ | 135 */ |
136 test.util.selectFile = function(contentWindow, filename) { | 136 test.util.selectFile = function(contentWindow, filename) { |
137 var table = contentWindow.document.querySelector('#detail-table'); | 137 var table = contentWindow.document.querySelector('#detail-table'); |
138 var rows = table.querySelectorAll('li'); | 138 var rows = table.querySelectorAll('li'); |
139 for (var index = 0; index < rows.length; ++index) { | 139 for (var index = 0; index < rows.length; ++index) { |
140 test.util.fakeKeyDown(contentWindow, 'Down', false); | 140 test.util.fakeKeyDown(contentWindow, '#file-list', 'Down', false); |
141 var selection = test.util.getSelectedFiles(contentWindow); | 141 var selection = test.util.getSelectedFiles(contentWindow); |
142 if (selection.length === 1 && selection[0] === filename) | 142 if (selection.length === 1 && selection[0] === filename) |
143 return true; | 143 return true; |
144 } | 144 } |
145 console.error('Failed to select file "' + filename + '"'); | 145 console.error('Failed to select file "' + filename + '"'); |
146 return false; | 146 return false; |
147 }; | 147 }; |
148 | 148 |
149 /** | 149 /** |
150 * Sends a fake key event with the given |keyIdentifier| and optional |ctrl| | 150 * Sends an event to the element specified by |targetQuery|. |
151 * modifier to the file manager. | |
152 * | 151 * |
153 * @param {Window} contentWindow Window to be tested. | 152 * @param {Window} contentWindow Window to be tested. |
| 153 * @param {string} targetQuery Query to specify the element. |
| 154 * @param {Event} event Event to be sent. |
| 155 * @return {boolean} True if the event is sent to the target, false otherwise. |
| 156 */ |
| 157 test.util.sendEvent = function(contentWindow, targetQuery, event) { |
| 158 var target = contentWindow.document.querySelector(targetQuery); |
| 159 if (target) { |
| 160 target.dispatchEvent(event); |
| 161 return true; |
| 162 } else { |
| 163 console.error('Target element for ' + targetQuery + ' not found.'); |
| 164 return false; |
| 165 } |
| 166 }; |
| 167 |
| 168 /** |
| 169 * Sends a fake key event to the element specified by |targetQuery| with the |
| 170 * given |keyIdentifier| and optional |ctrl| modifier to the file manager. |
| 171 * |
| 172 * @param {Window} contentWindow Window to be tested. |
| 173 * @param {string} targetQuery Query to specify the element. |
154 * @param {string} keyIdentifier Identifier of the emulated key. | 174 * @param {string} keyIdentifier Identifier of the emulated key. |
155 * @param {boolean} ctrl Whether CTRL should be pressed, or not. | 175 * @param {boolean} ctrl Whether CTRL should be pressed, or not. |
| 176 * @return {boolean} True if the event is sent to the target, false otherwise. |
156 */ | 177 */ |
157 test.util.fakeKeyDown = function(contentWindow, keyIdentifier, ctrl) { | 178 test.util.fakeKeyDown = function( |
| 179 contentWindow, targetQuery, keyIdentifier, ctrl) { |
158 var event = new KeyboardEvent( | 180 var event = new KeyboardEvent( |
159 'keydown', | 181 'keydown', |
160 { bubbles: true, keyIdentifier: keyIdentifier, ctrlKey: ctrl }); | 182 { bubbles: true, keyIdentifier: keyIdentifier, ctrlKey: ctrl }); |
161 var detailTable = contentWindow.document.querySelector('#detail-table'); | 183 return test.util.sendEvent(contentWindow, targetQuery, event); |
162 detailTable.querySelector('list').dispatchEvent(event); | |
163 }; | 184 }; |
164 | 185 |
165 /** | 186 /** |
166 * Sends a fake mouse click event to the element specified by |targetQuery|. | 187 * Sends a fake mouse click event to the element specified by |targetQuery|. |
167 * | 188 * |
168 * @param {Window} contentWindow Window to be tested. | 189 * @param {Window} contentWindow Window to be tested. |
169 * @param {string} targetQuery Query to specify the element. | 190 * @param {string} targetQuery Query to specify the element. |
170 * @return {boolean} True if file got selected, false otherwise. | 191 * @return {boolean} True if the event is sent to the target, false otherwise. |
171 */ | 192 */ |
172 test.util.fakeMouseClick = function(contentWindow, targetQuery) { | 193 test.util.fakeMouseClick = function(contentWindow, targetQuery) { |
173 var event = new MouseEvent('click', { bubbles: true }); | 194 var event = new MouseEvent('click', { bubbles: true }); |
174 var target = contentWindow.document.querySelector(targetQuery); | 195 return test.util.sendEvent(contentWindow, targetQuery, event); |
175 if (target) { | |
176 target.dispatchEvent(event); | |
177 return true; | |
178 } else { | |
179 console.error('Target element for ' + targetQuery + ' not found.'); | |
180 return false; | |
181 } | |
182 }; | 196 }; |
183 | 197 |
184 /** | 198 /** |
185 * Selects |filename| and fakes pressing Ctrl+C, Ctrl+V (copy, paste). | 199 * Selects |filename| and fakes pressing Ctrl+C, Ctrl+V (copy, paste). |
186 * | 200 * |
187 * @param {Window} contentWindow Window to be tested. | 201 * @param {Window} contentWindow Window to be tested. |
188 * @param {string} filename Name of the file to be copied. | 202 * @param {string} filename Name of the file to be copied. |
189 * @return {boolean} True if copying got simulated successfully. It does not | 203 * @return {boolean} True if copying got simulated successfully. It does not |
190 * say if the file got copied, or not. | 204 * say if the file got copied, or not. |
191 */ | 205 */ |
192 test.util.copyFile = function(contentWindow, filename) { | 206 test.util.copyFile = function(contentWindow, filename) { |
193 if (!test.util.selectFile(contentWindow, filename)) | 207 if (!test.util.selectFile(contentWindow, filename)) |
194 return false; | 208 return false; |
195 test.util.fakeKeyDown(contentWindow, 'U+0043', true); // Ctrl+C | 209 // Ctrl+C and Ctrl+V |
196 test.util.fakeKeyDown(contentWindow, 'U+0056', true); // Ctrl+V | 210 test.util.fakeKeyDown(contentWindow, '#file-list', 'U+0043', true); |
| 211 test.util.fakeKeyDown(contentWindow, '#file-list', 'U+0056', true); |
197 return true; | 212 return true; |
198 }; | 213 }; |
199 | 214 |
200 /** | 215 /** |
201 * Selects |filename| and fakes pressing the Delete key. | 216 * Selects |filename| and fakes pressing the Delete key. |
202 * | 217 * |
203 * @param {Window} contentWindow Window to be tested. | 218 * @param {Window} contentWindow Window to be tested. |
204 * @param {string} filename Name of the file to be deleted. | 219 * @param {string} filename Name of the file to be deleted. |
205 * @return {boolean} True if deleting got simulated successfully. It does not | 220 * @return {boolean} True if deleting got simulated successfully. It does not |
206 * say if the file got deleted, or not. | 221 * say if the file got deleted, or not. |
207 */ | 222 */ |
208 test.util.deleteFile = function(contentWindow, filename) { | 223 test.util.deleteFile = function(contentWindow, filename) { |
209 if (!test.util.selectFile(contentWindow, filename)) | 224 if (!test.util.selectFile(contentWindow, filename)) |
210 return false; | 225 return false; |
211 test.util.fakeKeyDown(contentWindow, 'U+007F', false); // Delete | 226 // Delete |
| 227 test.util.fakeKeyDown(contentWindow, '#file-list', 'U+007F', false); |
212 return true; | 228 return true; |
213 }; | 229 }; |
214 | 230 |
215 /** | 231 /** |
216 * Registers message listener, which runs test utility functions. | 232 * Registers message listener, which runs test utility functions. |
217 */ | 233 */ |
218 test.util.registerRemoteTestUtils = function() { | 234 test.util.registerRemoteTestUtils = function() { |
219 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : | 235 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : |
220 chrome.extension.onMessageExternal; | 236 chrome.extension.onMessageExternal; |
221 onMessage.addListener(function(request, sender, sendResponse) { | 237 onMessage.addListener(function(request, sender, sendResponse) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 test.util.waitForFileListChange( | 269 test.util.waitForFileListChange( |
254 contentWindow, request.args[0], sendResponse); | 270 contentWindow, request.args[0], sendResponse); |
255 return true; | 271 return true; |
256 case 'waitAndAcceptDialog': | 272 case 'waitAndAcceptDialog': |
257 test.util.waitAndAcceptDialog(contentWindow, sendResponse); | 273 test.util.waitAndAcceptDialog(contentWindow, sendResponse); |
258 return true; | 274 return true; |
259 case 'selectFile': | 275 case 'selectFile': |
260 test.util.sendResponse(selectFile(contentWindow, request.args[0])); | 276 test.util.sendResponse(selectFile(contentWindow, request.args[0])); |
261 return false; | 277 return false; |
262 case 'fakeKeyDown': | 278 case 'fakeKeyDown': |
263 test.util.fakeKeyDown( | 279 sendResponse(test.util.fakeKeyDown(contentWindow, |
264 contentWindow, request.args[0], request.request[1]); | 280 request.args[0], |
| 281 request.args[1], |
| 282 request.args[2])); |
265 return false; | 283 return false; |
266 case 'fakeMouseClick': | 284 case 'fakeMouseClick': |
267 sendResponse(test.util.fakeMouseClick( | 285 sendResponse(test.util.fakeMouseClick( |
268 contentWindow, request.args[0])); | 286 contentWindow, request.args[0])); |
269 return false; | 287 return false; |
270 case 'copyFile': | 288 case 'copyFile': |
271 sendResponse(test.util.copyFile(contentWindow, request.args[0])); | 289 sendResponse(test.util.copyFile(contentWindow, request.args[0])); |
272 return false; | 290 return false; |
273 case 'deleteFile': | 291 case 'deleteFile': |
274 sendResponse(test.util.deleteFile(contentWindow, request.args[0])); | 292 sendResponse(test.util.deleteFile(contentWindow, request.args[0])); |
275 return false; | 293 return false; |
276 default: | 294 default: |
277 console.error('Window function ' + request.func + ' not found.'); | 295 console.error('Window function ' + request.func + ' not found.'); |
278 } | 296 } |
279 } | 297 } |
280 return false; | 298 return false; |
281 }); | 299 }); |
282 }; | 300 }; |
283 | 301 |
284 // Register the test utils. | 302 // Register the test utils. |
285 test.util.registerRemoteTestUtils(); | 303 test.util.registerRemoteTestUtils(); |
OLD | NEW |