| 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 {opt_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 ? true : false; |
| 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 ? false : true; |
| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 if (contentWindow.document.querySelector('.cr-dialog-container')) | 345 if (contentWindow.document.querySelector('.cr-dialog-container')) |
| 344 return false; | 346 return false; |
| 345 callback(); | 347 callback(); |
| 346 return true; | 348 return true; |
| 347 }); | 349 }); |
| 348 return true; | 350 return true; |
| 349 }); | 351 }); |
| 350 }; | 352 }; |
| 351 | 353 |
| 352 /** | 354 /** |
| 353 * Fakes pressing the down arrow until the given |filename| is selected. | 355 * Fakes pressing the down arrow until the given |filename| is selected. Zero |
| 356 * timer is used to allow to handle the selection events in Files.app, before |
| 357 * returning the result. |
| 354 * | 358 * |
| 355 * @param {Window} contentWindow Window to be tested. | 359 * @param {Window} contentWindow Window to be tested. |
| 356 * @param {string} filename Name of the file to be selected. | 360 * @param {string} filename Name of the file to be selected. |
| 357 * @return {boolean} True if file got selected, false otherwise. | 361 * @param {function(boolean)} callback Completion callback with true for success |
| 362 * and false for a failure. |
| 358 */ | 363 */ |
| 359 test.util.sync.selectFile = function(contentWindow, filename) { | 364 test.util.async.selectFile = function(contentWindow, filename, callback) { |
| 360 var table = contentWindow.document.querySelector('#detail-table'); | 365 var table = contentWindow.document.querySelector('#detail-table'); |
| 361 var rows = table.querySelectorAll('li'); | 366 var rows = table.querySelectorAll('li'); |
| 362 for (var index = 0; index < rows.length; ++index) { | 367 for (var index = 0; index < rows.length; ++index) { |
| 363 test.util.sync.fakeKeyDown(contentWindow, '#file-list', 'Down', false); | 368 test.util.sync.fakeKeyDown(contentWindow, '#file-list', 'Down', false); |
| 364 var selection = test.util.sync.getSelectedFiles(contentWindow); | 369 var selection = test.util.sync.getSelectedFiles(contentWindow); |
| 365 if (selection.length === 1 && selection[0] === filename) | 370 if (selection.length === 1 && selection[0] === filename) { |
| 366 return true; | 371 setTimeout(callback.bind(null, true), 0); |
| 372 return; |
| 373 } |
| 367 } | 374 } |
| 368 console.error('Failed to select file "' + filename + '"'); | 375 console.error('Failed to select file "' + filename + '"'); |
| 369 return false; | 376 setTimeout(callback.bind(null, false), 0); |
| 370 }; | 377 }; |
| 371 | 378 |
| 372 /** | 379 /** |
| 373 * Selects a volume specified by its icon name | 380 * Selects a volume specified by its icon name |
| 374 * | 381 * |
| 375 * @param {Window} contentWindow Window to be tested. | 382 * @param {Window} contentWindow Window to be tested. |
| 376 * @param {string} iconName Name of the volume icon. | 383 * @param {string} iconName Name of the volume icon. |
| 377 * @param {function(boolean)} callback Callback function to notify the caller | 384 * @param {function(boolean)} callback Callback function to notify the caller |
| 378 * whether the target is found and mousedown and click events are sent. | 385 * whether the target is found and mousedown and click events are sent. |
| 379 */ | 386 */ |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 files.sort(); | 441 files.sort(); |
| 435 if (chrome.test.checkDeepEq(expected, files)) { | 442 if (chrome.test.checkDeepEq(expected, files)) { |
| 436 callback(true); | 443 callback(true); |
| 437 return true; | 444 return true; |
| 438 } | 445 } |
| 439 return false; | 446 return false; |
| 440 }); | 447 }); |
| 441 }; | 448 }; |
| 442 | 449 |
| 443 /** | 450 /** |
| 451 * Executes Javascript code on a webview and returns the result. |
| 452 * |
| 453 * @param {Window} contentWindow Window to be tested. |
| 454 * @param {string} webViewQuery Selector for the web view. |
| 455 * @param {string} code Javascript code to be executed within the web view. |
| 456 * @param {function(*)} callback Callback function with results returned by the |
| 457 * script. |
| 458 */ |
| 459 test.util.async.executeScriptInWebView = function( |
| 460 contentWindow, webViewQuery, code, callback) { |
| 461 var webView = contentWindow.document.querySelector(webViewQuery); |
| 462 webView.executeScript({code: code}, callback); |
| 463 }; |
| 464 |
| 465 /** |
| 444 * Sends an event to the element specified by |targetQuery|. | 466 * Sends an event to the element specified by |targetQuery|. |
| 445 * | 467 * |
| 446 * @param {Window} contentWindow Window to be tested. | 468 * @param {Window} contentWindow Window to be tested. |
| 447 * @param {string} targetQuery Query to specify the element. | 469 * @param {string} targetQuery Query to specify the element. |
| 448 * @param {Event} event Event to be sent. | 470 * @param {Event} event Event to be sent. |
| 449 * @param {string=} opt_iframeQuery Optional iframe selector. | 471 * @param {string=} opt_iframeQuery Optional iframe selector. |
| 450 * @return {boolean} True if the event is sent to the target, false otherwise. | 472 * @return {boolean} True if the event is sent to the target, false otherwise. |
| 451 */ | 473 */ |
| 452 test.util.sync.sendEvent = function( | 474 test.util.sync.sendEvent = function( |
| 453 contentWindow, targetQuery, event, opt_iframeQuery) { | 475 contentWindow, targetQuery, event, opt_iframeQuery) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 // Register the message listenr. | 678 // Register the message listenr. |
| 657 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : | 679 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : |
| 658 chrome.extension.onMessageExternal; | 680 chrome.extension.onMessageExternal; |
| 659 // Return true for asynchronous functions and false for synchronous. | 681 // Return true for asynchronous functions and false for synchronous. |
| 660 onMessage.addListener(function(request, sender, sendResponse) { | 682 onMessage.addListener(function(request, sender, sendResponse) { |
| 661 // Check the sender. | 683 // Check the sender. |
| 662 if (sender.id != test.util.TESTING_EXTENSION_ID) { | 684 if (sender.id != test.util.TESTING_EXTENSION_ID) { |
| 663 console.error('The testing extension must be white-listed.'); | 685 console.error('The testing extension must be white-listed.'); |
| 664 return false; | 686 return false; |
| 665 } | 687 } |
| 688 // Set a global flag that we are in tests, so other components are aware |
| 689 // of it. |
| 690 window.IN_TEST = true; |
| 666 // Check the function name. | 691 // Check the function name. |
| 667 if (!request.func || request.func[request.func.length - 1] == '_') { | 692 if (!request.func || request.func[request.func.length - 1] == '_') { |
| 668 request.func = ''; | 693 request.func = ''; |
| 669 } | 694 } |
| 670 // Prepare arguments. | 695 // Prepare arguments. |
| 671 var args = request.args.slice(); // shallow copy | 696 var args = request.args.slice(); // shallow copy |
| 672 if (request.appId) { | 697 if (request.appId) { |
| 673 if (!appWindows[request.appId]) { | 698 if (!appWindows[request.appId]) { |
| 674 console.error('Specified window not found.'); | 699 console.error('Specified window not found.'); |
| 675 return false; | 700 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 690 return false; | 715 return false; |
| 691 } else { | 716 } else { |
| 692 console.error('Invalid function name.'); | 717 console.error('Invalid function name.'); |
| 693 return false; | 718 return false; |
| 694 } | 719 } |
| 695 }); | 720 }); |
| 696 }; | 721 }; |
| 697 | 722 |
| 698 // Register the test utils. | 723 // Register the test utils. |
| 699 test.util.registerRemoteTestUtils(); | 724 test.util.registerRemoteTestUtils(); |
| OLD | NEW |