1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Namespace for test related things.
11 * Namespace for test utility functions.
13 * Public functions in the test.util.sync and the test.util.async namespaces are
14 * published to test cases and can be called by using callRemoteTestUtil. The
15 * arguments are serialized as JSON internally. If application ID is passed to
16 * callRemoteTestUtil, the content window of the application is added as the
17 * first argument. The functions in the test.util.async namespace are passed the
18 * callback function as the last argument.
23 * Namespace for synchronous utility functions.
28 * Namespace for asynchronous utility functions.
33 * Extension ID of the testing extension.
37 test.util.TESTING_EXTENSION_ID = 'oobinhbdbiehknkpbpejbbpdbkdjmoco';
40 * Opens the main Files.app's window and waits until it is ready.
42 * @param {Window} contentWindow Video player window to be chacked toOB.
43 * @return {boolean} True if the video is playing, false otherwise.
45 test.util.sync.isPlaying = function(contentWindow) {
46 var selector = 'video[src]';
47 var element = contentWindow.document.querySelector(selector);
51 return !element.paused;
55 * Gets total Javascript error count from each app window.
56 * @return {number} Error count.
58 test.util.sync.getErrorCount = function() {
59 var totalCount = window.JSErrorCount;
60 for (var appId in appWindowsForTest) {
61 var contentWindow = appWindowsForTest[appId].contentWindow;
62 if (contentWindow.JSErrorCount)
63 totalCount += contentWindow.JSErrorCount;
66 // Errors in the background page.
67 totalCount += window.JSErrorCount;
73 * Registers message listener, which runs test utility functions.
75 test.util.registerRemoteTestUtils = function() {
76 // Register the message listener.
77 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal :
78 chrome.extension.onMessageExternal;
79 // Return true for asynchronous functions and false for synchronous.
80 onMessage.addListener(function(request, sender, sendResponse) {
82 if (sender.id != test.util.TESTING_EXTENSION_ID) {
83 console.error('The testing extension must be white-listed.');
87 if (!request.func || request.func[request.func.length - 1] == '_') {
91 var args = request.args.slice(); // shallow copy
93 if (!appWindowsForTest[request.appId]) {
94 console.error('Specified window not found: ' + request.appId);
98 args.unshift(appWindowsForTest[request.appId].contentWindow);
100 // Call the test utility function and respond the result.
101 if (test.util.async[request.func]) {
102 args[test.util.async[request.func].length - 1] = function() {
103 console.debug('Received the result of ' + request.func);
104 sendResponse.apply(null, arguments);
106 console.debug('Waiting for the result of ' + request.func);
107 test.util.async[request.func].apply(null, args);
109 } else if (test.util.sync[request.func]) {
110 sendResponse(test.util.sync[request.func].apply(null, args));
113 console.error('Invalid function name.');
120 // Register the test utils.
121 test.util.registerRemoteTestUtils();