Chromium Code Reviews| Index: chrome/browser/resources/video_player/js/test_util.js |
| diff --git a/chrome/browser/resources/video_player/js/test_util.js b/chrome/browser/resources/video_player/js/test_util.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6be5533934336282f22c2ea5ececa00e8ed28c4c |
| --- /dev/null |
| +++ b/chrome/browser/resources/video_player/js/test_util.js |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
hirono
2014/03/25 07:48:16
The Files.app and the video player now share a few
yoshiki
2014/03/25 07:53:34
test.util.registerRemoteTestUtils is a bit differe
hirono
2014/03/25 08:02:19
SGTM!
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * Namespace for test related things. |
| + */ |
| +var test = test || {}; |
| + |
| +/** |
| + * Namespace for test utility functions. |
| + * |
| + * Public functions in the test.util.sync and the test.util.async namespaces are |
| + * published to test cases and can be called by using callRemoteTestUtil. The |
| + * arguments are serialized as JSON internally. If application ID is passed to |
| + * callRemoteTestUtil, the content window of the application is added as the |
| + * first argument. The functions in the test.util.async namespace are passed the |
| + * callback function as the last argument. |
| + */ |
| +test.util = {}; |
| + |
| +/** |
| + * Namespace for synchronous utility functions. |
| + */ |
| +test.util.sync = {}; |
| + |
| +/** |
| + * Namespace for asynchronous utility functions. |
| + */ |
| +test.util.async = {}; |
| + |
| +/** |
| + * Extension ID of the testing extension. |
| + * @type {string} |
| + * @const |
| + */ |
| +test.util.TESTING_EXTENSION_ID = 'oobinhbdbiehknkpbpejbbpdbkdjmoco'; |
| + |
| +/** |
| + * Opens the main Files.app's window and waits until it is ready. |
| + * |
| + * @param {Window} contentWindow Video player window to be chacked toOB. |
| + * @return {boolean} True if the video is playing, false otherwise. |
| + */ |
| +test.util.sync.isPlaying = function(contentWindow) { |
| + var selector = 'video[src]'; |
| + var element = contentWindow.document.querySelector(selector); |
| + if (!element) |
| + return false; |
| + |
| + return !element.paused; |
| +}; |
| + |
| +/** |
| + * Registers message listener, which runs test utility functions. |
| + */ |
| +test.util.registerRemoteTestUtils = function() { |
| + // Register the message listener. |
| + var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : |
| + chrome.extension.onMessageExternal; |
| + // Return true for asynchronous functions and false for synchronous. |
| + onMessage.addListener(function(request, sender, sendResponse) { |
| + // Check the sender. |
| + if (sender.id != test.util.TESTING_EXTENSION_ID) { |
| + console.error('The testing extension must be white-listed.'); |
| + sendResponse(false); |
| + return false; |
| + } |
| + if (!request.func || request.func[request.func.length - 1] == '_') { |
| + request.func = ''; |
| + } |
| + // Prepare arguments. |
| + var args = request.args.slice(); // shallow copy |
| + if (request.file) { |
| + if (!appWindowsForTest[request.file]) { |
| + console.error('Specified window not found: ' + request.file); |
| + sendResponse(false); |
| + return false; |
| + } |
| + args.unshift(appWindowsForTest[request.file].contentWindow); |
| + } |
| + // Call the test utility function and respond the result. |
| + if (test.util.async[request.func]) { |
| + args[test.util.async[request.func].length - 1] = function() { |
| + console.debug('Received the result of ' + request.func); |
| + sendResponse.apply(null, arguments); |
| + }; |
| + console.debug('Waiting for the result of ' + request.func); |
| + test.util.async[request.func].apply(null, args); |
| + return true; |
| + } else if (test.util.sync[request.func]) { |
| + sendResponse(test.util.sync[request.func].apply(null, args)); |
| + return false; |
| + } else { |
| + console.error('Invalid function name.'); |
| + sendResponse(false); |
| + return false; |
| + } |
| + }); |
| +}; |
| + |
| +// Register the test utils. |
| +test.util.registerRemoteTestUtils(); |