OLD | NEW |
---|---|
(Empty) | |
1 // 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!
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * Namespace for test related things. | |
7 */ | |
8 var test = test || {}; | |
9 | |
10 /** | |
11 * Namespace for test utility functions. | |
12 * | |
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. | |
19 */ | |
20 test.util = {}; | |
21 | |
22 /** | |
23 * Namespace for synchronous utility functions. | |
24 */ | |
25 test.util.sync = {}; | |
26 | |
27 /** | |
28 * Namespace for asynchronous utility functions. | |
29 */ | |
30 test.util.async = {}; | |
31 | |
32 /** | |
33 * Extension ID of the testing extension. | |
34 * @type {string} | |
35 * @const | |
36 */ | |
37 test.util.TESTING_EXTENSION_ID = 'oobinhbdbiehknkpbpejbbpdbkdjmoco'; | |
38 | |
39 /** | |
40 * Opens the main Files.app's window and waits until it is ready. | |
41 * | |
42 * @param {Window} contentWindow Video player window to be chacked toOB. | |
43 * @return {boolean} True if the video is playing, false otherwise. | |
44 */ | |
45 test.util.sync.isPlaying = function(contentWindow) { | |
46 var selector = 'video[src]'; | |
47 var element = contentWindow.document.querySelector(selector); | |
48 if (!element) | |
49 return false; | |
50 | |
51 return !element.paused; | |
52 }; | |
53 | |
54 /** | |
55 * Registers message listener, which runs test utility functions. | |
56 */ | |
57 test.util.registerRemoteTestUtils = function() { | |
58 // Register the message listener. | |
59 var onMessage = chrome.runtime ? chrome.runtime.onMessageExternal : | |
60 chrome.extension.onMessageExternal; | |
61 // Return true for asynchronous functions and false for synchronous. | |
62 onMessage.addListener(function(request, sender, sendResponse) { | |
63 // Check the sender. | |
64 if (sender.id != test.util.TESTING_EXTENSION_ID) { | |
65 console.error('The testing extension must be white-listed.'); | |
66 sendResponse(false); | |
67 return false; | |
68 } | |
69 if (!request.func || request.func[request.func.length - 1] == '_') { | |
70 request.func = ''; | |
71 } | |
72 // Prepare arguments. | |
73 var args = request.args.slice(); // shallow copy | |
74 if (request.file) { | |
75 if (!appWindowsForTest[request.file]) { | |
76 console.error('Specified window not found: ' + request.file); | |
77 sendResponse(false); | |
78 return false; | |
79 } | |
80 args.unshift(appWindowsForTest[request.file].contentWindow); | |
81 } | |
82 // Call the test utility function and respond the result. | |
83 if (test.util.async[request.func]) { | |
84 args[test.util.async[request.func].length - 1] = function() { | |
85 console.debug('Received the result of ' + request.func); | |
86 sendResponse.apply(null, arguments); | |
87 }; | |
88 console.debug('Waiting for the result of ' + request.func); | |
89 test.util.async[request.func].apply(null, args); | |
90 return true; | |
91 } else if (test.util.sync[request.func]) { | |
92 sendResponse(test.util.sync[request.func].apply(null, args)); | |
93 return false; | |
94 } else { | |
95 console.error('Invalid function name.'); | |
96 sendResponse(false); | |
97 return false; | |
98 } | |
99 }); | |
100 }; | |
101 | |
102 // Register the test utils. | |
103 test.util.registerRemoteTestUtils(); | |
OLD | NEW |