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 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 return false; | 214 return false; |
215 }); | 215 }); |
216 }; | 216 }; |
217 | 217 |
218 /** | 218 /** |
219 * Waits for an element and returns it as an array of it's attributes. | 219 * Waits for an element and returns it as an array of it's attributes. |
220 * | 220 * |
221 * @param {Window} contentWindow Window to be tested. | 221 * @param {Window} contentWindow Window to be tested. |
222 * @param {string} targetQuery Query to specify the element. | 222 * @param {string} targetQuery Query to specify the element. |
223 * @param {?string} iframeQuery Iframe selector or null if no iframe. | 223 * @param {?string} iframeQuery Iframe selector or null if no iframe. |
| 224 * @param {boolean=} opt_inverse True if the function should return if the |
| 225 * element disappears, instead of appearing. |
224 * @param {function(Object)} callback Callback with a hash array of attributes | 226 * @param {function(Object)} callback Callback with a hash array of attributes |
225 * and contents as text. | 227 * and contents as text. |
226 */ | 228 */ |
227 test.util.async.waitForElement = function( | 229 test.util.async.waitForElement = function( |
228 contentWindow, targetQuery, iframeQuery, callback) { | 230 contentWindow, targetQuery, iframeQuery, opt_inverse, callback) { |
229 test.util.repeatUntilTrue_(function() { | 231 test.util.repeatUntilTrue_(function() { |
230 var doc = test.util.sync.getDocument_(contentWindow, iframeQuery); | 232 var doc = test.util.sync.getDocument_(contentWindow, iframeQuery); |
231 if (!doc) | 233 if (!doc) |
232 return false; | 234 return false; |
233 var element = doc.querySelector(targetQuery); | 235 var element = doc.querySelector(targetQuery); |
234 if (!element) | 236 if (!element) |
235 return false; | 237 return !!opt_inverse; |
236 var attributes = {}; | 238 var attributes = {}; |
237 for (var i = 0; i < element.attributes.length; i++) { | 239 for (var i = 0; i < element.attributes.length; i++) { |
238 attributes[element.attributes[i].nodeName] = | 240 attributes[element.attributes[i].nodeName] = |
239 element.attributes[i].nodeValue; | 241 element.attributes[i].nodeValue; |
240 } | 242 } |
241 var text = element.textContent; | 243 var text = element.textContent; |
242 callback({attributes: attributes, text: text}); | 244 callback({attributes: attributes, text: text}); |
243 return true; | 245 return !opt_inverse; |
244 }); | 246 }); |
245 }; | 247 }; |
246 | 248 |
247 /** | 249 /** |
248 * Calls getFileList until the number of displayed files is different from | 250 * Calls getFileList until the number of displayed files is different from |
249 * lengthBefore. | 251 * lengthBefore. |
250 * | 252 * |
251 * @param {Window} contentWindow Window to be tested. | 253 * @param {Window} contentWindow Window to be tested. |
252 * @param {number} lengthBefore Number of items visible before. | 254 * @param {number} lengthBefore Number of items visible before. |
253 * @param {function(Array.<Array.<string>>)} callback Change callback. | 255 * @param {function(Array.<Array.<string>>)} callback Change callback. |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 files.sort(); | 436 files.sort(); |
435 if (chrome.test.checkDeepEq(expected, files)) { | 437 if (chrome.test.checkDeepEq(expected, files)) { |
436 callback(true); | 438 callback(true); |
437 return true; | 439 return true; |
438 } | 440 } |
439 return false; | 441 return false; |
440 }); | 442 }); |
441 }; | 443 }; |
442 | 444 |
443 /** | 445 /** |
| 446 * Executes Javascript code on a webview and returns the result. |
| 447 * |
| 448 * @param {Window} contentWindow Window to be tested. |
| 449 * @param {string} webViewQuery Selector for the web view. |
| 450 * @param {string} code Javascript code to be executed within the web view. |
| 451 * @param {function(*)} callback Callback function with results returned by the |
| 452 * script. |
| 453 */ |
| 454 test.util.async.executeScriptInWebView = function( |
| 455 contentWindow, webViewQuery, code, callback) { |
| 456 var webView = contentWindow.document.querySelector(webViewQuery); |
| 457 webView.executeScript({code: code}, callback); |
| 458 }; |
| 459 |
| 460 /** |
444 * Sends an event to the element specified by |targetQuery|. | 461 * Sends an event to the element specified by |targetQuery|. |
445 * | 462 * |
446 * @param {Window} contentWindow Window to be tested. | 463 * @param {Window} contentWindow Window to be tested. |
447 * @param {string} targetQuery Query to specify the element. | 464 * @param {string} targetQuery Query to specify the element. |
448 * @param {Event} event Event to be sent. | 465 * @param {Event} event Event to be sent. |
449 * @param {string=} opt_iframeQuery Optional iframe selector. | 466 * @param {string=} opt_iframeQuery Optional iframe selector. |
450 * @return {boolean} True if the event is sent to the target, false otherwise. | 467 * @return {boolean} True if the event is sent to the target, false otherwise. |
451 */ | 468 */ |
452 test.util.sync.sendEvent = function( | 469 test.util.sync.sendEvent = function( |
453 contentWindow, targetQuery, event, opt_iframeQuery) { | 470 contentWindow, targetQuery, event, opt_iframeQuery) { |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 // Register the message listenr. | 673 // Register the message listenr. |
657 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : | 674 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : |
658 chrome.extension.onMessageExternal; | 675 chrome.extension.onMessageExternal; |
659 // Return true for asynchronous functions and false for synchronous. | 676 // Return true for asynchronous functions and false for synchronous. |
660 onMessage.addListener(function(request, sender, sendResponse) { | 677 onMessage.addListener(function(request, sender, sendResponse) { |
661 // Check the sender. | 678 // Check the sender. |
662 if (sender.id != test.util.TESTING_EXTENSION_ID) { | 679 if (sender.id != test.util.TESTING_EXTENSION_ID) { |
663 console.error('The testing extension must be white-listed.'); | 680 console.error('The testing extension must be white-listed.'); |
664 return false; | 681 return false; |
665 } | 682 } |
| 683 // Set a global flag that we are in tests, so other components are aware |
| 684 // of it. |
| 685 window.IN_TEST = true; |
666 // Check the function name. | 686 // Check the function name. |
667 if (!request.func || request.func[request.func.length - 1] == '_') { | 687 if (!request.func || request.func[request.func.length - 1] == '_') { |
668 request.func = ''; | 688 request.func = ''; |
669 } | 689 } |
670 // Prepare arguments. | 690 // Prepare arguments. |
671 var args = request.args.slice(); // shallow copy | 691 var args = request.args.slice(); // shallow copy |
672 if (request.appId) { | 692 if (request.appId) { |
673 if (!appWindows[request.appId]) { | 693 if (!appWindows[request.appId]) { |
674 console.error('Specified window not found.'); | 694 console.error('Specified window not found.'); |
675 return false; | 695 return false; |
(...skipping 14 matching lines...) Expand all Loading... |
690 return false; | 710 return false; |
691 } else { | 711 } else { |
692 console.error('Invalid function name.'); | 712 console.error('Invalid function name.'); |
693 return false; | 713 return false; |
694 } | 714 } |
695 }); | 715 }); |
696 }; | 716 }; |
697 | 717 |
698 // Register the test utils. | 718 // Register the test utils. |
699 test.util.registerRemoteTestUtils(); | 719 test.util.registerRemoteTestUtils(); |
OLD | NEW |