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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 }); | 101 }); |
102 if (notReadyRows.length === 0 && files.length !== lengthBefore) | 102 if (notReadyRows.length === 0 && files.length !== lengthBefore) |
103 callback(files); | 103 callback(files); |
104 else | 104 else |
105 window.setTimeout(helper, 50); | 105 window.setTimeout(helper, 50); |
106 } | 106 } |
107 helper(); | 107 helper(); |
108 }; | 108 }; |
109 | 109 |
110 /** | 110 /** |
| 111 * Returns an array of items on the file manager's autocomplete list. |
| 112 * |
| 113 * @param {Window} contentWindow Window to be tested. |
| 114 * @return {Array.<string>} Array of items. |
| 115 */ |
| 116 test.util.getAutocompleteList = function(contentWindow) { |
| 117 var list = contentWindow.document.querySelector('#autocomplete-list'); |
| 118 var lines = list.querySelectorAll('li'); |
| 119 var items = []; |
| 120 for (var j = 0; j < lines.length; ++j) { |
| 121 var line = lines[j]; |
| 122 items.push(line.innerText); |
| 123 } |
| 124 return items; |
| 125 }; |
| 126 |
| 127 /** |
| 128 * Performs autocomplete with the given query and waits until at least |
| 129 * |numExpectedItems| items are shown, including the first item which |
| 130 * always looks like "'<query>' - search Drive". |
| 131 * |
| 132 * @param {Window} contentWindow Window to be tested. |
| 133 * @param {string} query Query used for autocomplete. |
| 134 * @param {number} numExpectedItems number of items to be shown. |
| 135 * @param {function(Array.<string>)} callback Change callback. |
| 136 */ |
| 137 test.util.performAutocompleteAndWait = function( |
| 138 contentWindow, query, numExpectedItems, callback) { |
| 139 // Dispatch a 'focus' event to the search box so that the autocomplete list |
| 140 // is attached to the search box. Note that calling searchBox.focus() won't |
| 141 // dispatch a 'focus' event. |
| 142 var searchBox = contentWindow.document.querySelector('#search-box'); |
| 143 var focusEvent = contentWindow.document.createEvent('Event'); |
| 144 focusEvent.initEvent('focus', true /* bubbles */, true /* cancelable */); |
| 145 searchBox.dispatchEvent(focusEvent); |
| 146 |
| 147 // Change the value of the search box and dispatch an 'input' event so that |
| 148 // the autocomplete query is processed. |
| 149 searchBox.value = query; |
| 150 var inputEvent = contentWindow.document.createEvent('Event'); |
| 151 inputEvent.initEvent('input', true /* bubbles */, true /* cancelable */); |
| 152 searchBox.dispatchEvent(inputEvent); |
| 153 |
| 154 function helper() { |
| 155 var items = test.util.getAutocompleteList(contentWindow); |
| 156 if (items.length >= numExpectedItems) |
| 157 callback(items); |
| 158 else |
| 159 window.setTimeout(helper, 50); |
| 160 } |
| 161 helper(); |
| 162 }; |
| 163 |
| 164 /** |
111 * Waits until a dialog with an OK button is shown and accepts it. | 165 * Waits until a dialog with an OK button is shown and accepts it. |
112 * | 166 * |
113 * @param {Window} contentWindow Window to be tested. | 167 * @param {Window} contentWindow Window to be tested. |
114 * @param {function()} callback Success callback. | 168 * @param {function()} callback Success callback. |
115 */ | 169 */ |
116 test.util.waitAndAcceptDialog = function(contentWindow, callback) { | 170 test.util.waitAndAcceptDialog = function(contentWindow, callback) { |
117 var tryAccept = function() { | 171 var tryAccept = function() { |
118 var button = contentWindow.document.querySelector('.cr-dialog-ok'); | 172 var button = contentWindow.document.querySelector('.cr-dialog-ok'); |
119 if (button) { | 173 if (button) { |
120 button.click(); | 174 button.click(); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 } | 296 } |
243 } else { | 297 } else { |
244 // Functions working on a window. | 298 // Functions working on a window. |
245 switch (request.func) { | 299 switch (request.func) { |
246 case 'getSelectedFiles': | 300 case 'getSelectedFiles': |
247 sendResponse(test.util.getSelectedFiles(contentWindow)); | 301 sendResponse(test.util.getSelectedFiles(contentWindow)); |
248 return false; | 302 return false; |
249 case 'getFileList': | 303 case 'getFileList': |
250 sendResponse(test.util.getFileList(contentWindow)); | 304 sendResponse(test.util.getFileList(contentWindow)); |
251 return false; | 305 return false; |
| 306 case 'performAutocompleteAndWait': |
| 307 test.util.performAutocompleteAndWait( |
| 308 contentWindow, request.args[0], request.args[1], sendResponse); |
| 309 return true; |
252 case 'waitForFileListChange': | 310 case 'waitForFileListChange': |
253 test.util.waitForFileListChange( | 311 test.util.waitForFileListChange( |
254 contentWindow, request.args[0], sendResponse); | 312 contentWindow, request.args[0], sendResponse); |
255 return true; | 313 return true; |
256 case 'waitAndAcceptDialog': | 314 case 'waitAndAcceptDialog': |
257 test.util.waitAndAcceptDialog(contentWindow, sendResponse); | 315 test.util.waitAndAcceptDialog(contentWindow, sendResponse); |
258 return true; | 316 return true; |
259 case 'selectFile': | 317 case 'selectFile': |
260 test.util.sendResponse(selectFile(contentWindow, request.args[0])); | 318 test.util.sendResponse(selectFile(contentWindow, request.args[0])); |
261 return false; | 319 return false; |
(...skipping 14 matching lines...) Expand all Loading... |
276 default: | 334 default: |
277 console.error('Window function ' + request.func + ' not found.'); | 335 console.error('Window function ' + request.func + ' not found.'); |
278 } | 336 } |
279 } | 337 } |
280 return false; | 338 return false; |
281 }); | 339 }); |
282 }; | 340 }; |
283 | 341 |
284 // Register the test utils. | 342 // Register the test utils. |
285 test.util.registerRemoteTestUtils(); | 343 test.util.registerRemoteTestUtils(); |
OLD | NEW |