OLD | NEW |
| (Empty) |
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. | |
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 * Gets total Javascript error count from each app window. | |
56 * @return {number} Error count. | |
57 */ | |
58 test.util.sync.getErrorCount = function() { | |
59 var totalCount = JSErrorCount; | |
60 for (var appId in appWindowsForTest) { | |
61 var contentWindow = appWindowsForTest[appId].contentWindow; | |
62 if (contentWindow.JSErrorCount) | |
63 totalCount += contentWindow.JSErrorCount; | |
64 } | |
65 | |
66 // Errors in the background page. | |
67 totalCount += window.JSErrorCount; | |
68 | |
69 return totalCount; | |
70 }; | |
71 | |
72 /** | |
73 * Registers message listener, which runs test utility functions. | |
74 */ | |
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) { | |
81 // Check the sender. | |
82 if (sender.id != test.util.TESTING_EXTENSION_ID) { | |
83 console.error('The testing extension must be white-listed.'); | |
84 sendResponse(false); | |
85 return false; | |
86 } | |
87 if (!request.func || request.func[request.func.length - 1] == '_') { | |
88 request.func = ''; | |
89 } | |
90 // Prepare arguments. | |
91 var args = request.args.slice(); // shallow copy | |
92 if (request.file) { | |
93 if (!appWindowsForTest[request.file]) { | |
94 console.error('Specified window not found: ' + request.file); | |
95 sendResponse(false); | |
96 return false; | |
97 } | |
98 args.unshift(appWindowsForTest[request.file].contentWindow); | |
99 } | |
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); | |
105 }; | |
106 console.debug('Waiting for the result of ' + request.func); | |
107 test.util.async[request.func].apply(null, args); | |
108 return true; | |
109 } else if (test.util.sync[request.func]) { | |
110 sendResponse(test.util.sync[request.func].apply(null, args)); | |
111 return false; | |
112 } else { | |
113 console.error('Invalid function name.'); | |
114 sendResponse(false); | |
115 return false; | |
116 } | |
117 }.wrap()); | |
118 }; | |
119 | |
120 // Register the test utils. | |
121 test.util.registerRemoteTestUtils(); | |
OLD | NEW |