1 // Copyright 2013 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.
5 #include "base/command_line.h"
6 #include "base/files/file_enumerator.h"
7 #include "base/path_service.h"
8 #include "base/process/launch.h"
9 #include "base/rand_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/media/webrtc_browsertest_base.h"
13 #include "chrome/browser/media/webrtc_browsertest_common.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "media/base/media_switches.h"
21 #include "net/test/python_utils.h"
22 #include "ui/gl/gl_switches.h"
24 // You need this solution to run this test. The solution will download appengine
25 // and the apprtc code for you.
26 const char kAdviseOnGclientSolution
[] =
27 "You need to add this solution to your .gclient to run this test:\n"
29 " \"name\" : \"webrtc.DEPS\",\n"
30 " \"url\" : \"svn://svn.chromium.org/chrome/trunk/deps/"
31 "third_party/webrtc/webrtc.DEPS\",\n"
33 const char kTitlePageOfAppEngineAdminPage
[] = "Instances";
35 const char kIsApprtcCallUpJavascript
[] =
36 "var remoteVideoActive ="
37 " typeof remoteVideo != undefined &&"
38 " remoteVideo.classList.contains('active');"
39 "window.domAutomationController.send(remoteVideoActive.toString());";
42 // WebRTC-AppRTC integration test. Requires a real webcam and microphone
43 // on the running system. This test is not meant to run in the main browser
44 // test suite since normal tester machines do not have webcams. Chrome will use
45 // its fake camera for both tests, but Firefox will use the real webcam in the
46 // Firefox interop test. Thus, this test must on a machine with a real webcam.
48 // This test will bring up a AppRTC instance on localhost and verify that the
49 // call gets up when connecting to the same room from two tabs in a browser.
50 class WebRtcApprtcBrowserTest
: public WebRtcTestBase
{
52 WebRtcApprtcBrowserTest()
53 : dev_appserver_(base::kNullProcessHandle
),
54 firefox_(base::kNullProcessHandle
) {
57 void SetUpCommandLine(CommandLine
* command_line
) override
{
58 EXPECT_FALSE(command_line
->HasSwitch(switches::kUseFakeUIForMediaStream
));
60 // The video playback will not work without a GPU, so force its use here.
61 command_line
->AppendSwitch(switches::kUseGpuInTests
);
62 CommandLine::ForCurrentProcess()->AppendSwitch(
63 switches::kUseFakeDeviceForMediaStream
);
66 void TearDown() override
{
67 // Kill any processes we may have brought up.
68 LOG(INFO
) << "Entering TearDown";
69 if (dev_appserver_
!= base::kNullProcessHandle
)
70 base::KillProcess(dev_appserver_
, 0, false);
71 // TODO(phoglund): Find some way to shut down Firefox cleanly on Windows.
72 if (firefox_
!= base::kNullProcessHandle
)
73 base::KillProcess(firefox_
, 0, false);
74 LOG(INFO
) << "Exiting TearDown";
78 bool LaunchApprtcInstanceOnLocalhost() {
79 base::FilePath appengine_dev_appserver
=
80 GetSourceDir().Append(
81 FILE_PATH_LITERAL("../google_appengine/dev_appserver.py"));
82 if (!base::PathExists(appengine_dev_appserver
)) {
83 LOG(ERROR
) << "Missing appengine sdk at " <<
84 appengine_dev_appserver
.value() << ". " << kAdviseOnGclientSolution
;
88 base::FilePath apprtc_dir
=
89 GetSourceDir().Append(FILE_PATH_LITERAL("out/apprtc"));
90 if (!base::PathExists(apprtc_dir
)) {
91 LOG(ERROR
) << "Missing AppRTC code at " <<
92 apprtc_dir
.value() << ". " << kAdviseOnGclientSolution
;
96 CommandLine
command_line(CommandLine::NO_PROGRAM
);
97 EXPECT_TRUE(GetPythonCommand(&command_line
));
99 command_line
.AppendArgPath(appengine_dev_appserver
);
100 command_line
.AppendArgPath(apprtc_dir
);
101 command_line
.AppendArg("--port=9999");
102 command_line
.AppendArg("--admin_port=9998");
103 command_line
.AppendArg("--skip_sdk_update_check");
104 command_line
.AppendArg("--clear_datastore=yes");
106 DVLOG(1) << "Running " << command_line
.GetCommandLineString();
107 return base::LaunchProcess(command_line
, base::LaunchOptions(),
111 bool LocalApprtcInstanceIsUp() {
112 // Load the admin page and see if we manage to load it right.
113 ui_test_utils::NavigateToURL(browser(), GURL("localhost:9998"));
114 content::WebContents
* tab_contents
=
115 browser()->tab_strip_model()->GetActiveWebContents();
116 std::string javascript
=
117 "window.domAutomationController.send(document.title)";
119 if (!content::ExecuteScriptAndExtractString(tab_contents
, javascript
,
123 return result
== kTitlePageOfAppEngineAdminPage
;
126 bool WaitForCallToComeUp(content::WebContents
* tab_contents
) {
127 return test::PollingWaitUntil(kIsApprtcCallUpJavascript
, "true",
131 bool WaitForCallToHangUp(content::WebContents
* tab_contents
) {
132 return test::PollingWaitUntil(kIsApprtcCallUpJavascript
, "false",
136 bool EvalInJavascriptFile(content::WebContents
* tab_contents
,
137 const base::FilePath
& path
) {
138 std::string javascript
;
139 if (!ReadFileToString(path
, &javascript
)) {
140 LOG(ERROR
) << "Missing javascript code at " << path
.value() << ".";
144 if (!content::ExecuteScript(tab_contents
, javascript
)) {
145 LOG(ERROR
) << "Failed to execute the following javascript: " <<
152 bool DetectRemoteVideoPlaying(content::WebContents
* tab_contents
) {
153 if (!EvalInJavascriptFile(tab_contents
, GetSourceDir().Append(
154 FILE_PATH_LITERAL("chrome/test/data/webrtc/test_functions.js"))))
156 if (!EvalInJavascriptFile(tab_contents
, GetSourceDir().Append(
157 FILE_PATH_LITERAL("chrome/test/data/webrtc/video_detector.js"))))
160 // The remote video tag is called remoteVideo in the AppRTC code.
161 StartDetectingVideo(tab_contents
, "remote-video");
162 WaitForVideoToPlay(tab_contents
);
166 bool HangUpApprtcCall(content::WebContents
* tab_contents
) {
167 // This is the same as clicking the Hangup button in the AppRTC call.
168 return content::ExecuteScript(tab_contents
, "hangup()");
171 base::FilePath
GetSourceDir() {
172 base::FilePath source_dir
;
173 PathService::Get(base::DIR_SOURCE_ROOT
, &source_dir
);
177 bool LaunchFirefoxWithUrl(const GURL
& url
) {
178 base::FilePath firefox_binary
=
179 GetSourceDir().Append(
180 FILE_PATH_LITERAL("../firefox-nightly/firefox/firefox"));
181 if (!base::PathExists(firefox_binary
)) {
182 LOG(ERROR
) << "Missing firefox binary at " <<
183 firefox_binary
.value() << ". " << kAdviseOnGclientSolution
;
186 base::FilePath firefox_launcher
=
187 GetSourceDir().Append(
188 FILE_PATH_LITERAL("../webrtc.DEPS/run_firefox_webrtc.py"));
189 if (!base::PathExists(firefox_launcher
)) {
190 LOG(ERROR
) << "Missing firefox launcher at " <<
191 firefox_launcher
.value() << ". " << kAdviseOnGclientSolution
;
195 CommandLine
command_line(firefox_launcher
);
196 command_line
.AppendSwitchPath("--binary", firefox_binary
);
197 command_line
.AppendSwitchASCII("--webpage", url
.spec());
199 DVLOG(1) << "Running " << command_line
.GetCommandLineString();
200 return base::LaunchProcess(command_line
, base::LaunchOptions(),
204 bool HasWebcamOnSystem() {
205 #if defined(OS_LINUX)
206 // Implementation note: normally we would be able to figure this out with
207 // MediaStreamTrack.getSources, but we can't ask Chrome since it runs in
208 // fake device mode where it will not enumerate webcams on the system.
209 // Therefore, look for /dev/video* entries directly since this test only
210 // runs on Linux for now anyway.
211 base::FileEnumerator
dev_video(base::FilePath(FILE_PATH_LITERAL("/dev")),
212 false, base::FileEnumerator::FILES
,
213 FILE_PATH_LITERAL("video*"));
214 return !dev_video
.Next().empty();
221 base::ProcessHandle dev_appserver_
;
222 base::ProcessHandle firefox_
;
225 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest
, MANUAL_WorksOnApprtc
) {
226 // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
230 DetectErrorsInJavaScript();
231 ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
232 while (!LocalApprtcInstanceIsUp())
233 DVLOG(1) << "Waiting for AppRTC to come up...";
235 GURL room_url
= GURL(base::StringPrintf("localhost:9999?r=room_%d",
236 base::RandInt(0, 65536)));
238 chrome::AddTabAt(browser(), GURL(), -1, true);
239 content::WebContents
* left_tab
= OpenPageAndAcceptUserMedia(room_url
);
241 chrome::AddTabAt(browser(), GURL(), -1, true);
242 content::WebContents
* right_tab
= OpenPageAndAcceptUserMedia(room_url
);
244 ASSERT_TRUE(WaitForCallToComeUp(left_tab
));
245 ASSERT_TRUE(WaitForCallToComeUp(right_tab
));
247 ASSERT_TRUE(DetectRemoteVideoPlaying(left_tab
));
248 ASSERT_TRUE(DetectRemoteVideoPlaying(right_tab
));
250 ASSERT_TRUE(HangUpApprtcCall(left_tab
));
252 ASSERT_TRUE(WaitForCallToHangUp(left_tab
));
253 ASSERT_TRUE(WaitForCallToHangUp(right_tab
));
255 chrome::CloseWebContents(browser(), left_tab
, false);
256 chrome::CloseWebContents(browser(), right_tab
, false);
259 #if defined(OS_LINUX)
260 #define MAYBE_MANUAL_FirefoxApprtcInteropTest MANUAL_FirefoxApprtcInteropTest
262 // Not implemented yet on Windows and Mac.
263 #define MAYBE_MANUAL_FirefoxApprtcInteropTest DISABLED_MANUAL_FirefoxApprtcInteropTest
266 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest
,
267 MAYBE_MANUAL_FirefoxApprtcInteropTest
) {
268 // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
272 DetectErrorsInJavaScript();
273 ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
274 while (!LocalApprtcInstanceIsUp())
275 DVLOG(1) << "Waiting for AppRTC to come up...";
277 GURL room_url
= GURL(
278 "http://localhost:9999?r=some_room_id&firefox_fake_device=1");
279 content::WebContents
* chrome_tab
= OpenPageAndAcceptUserMedia(room_url
);
281 ASSERT_TRUE(LaunchFirefoxWithUrl(room_url
));
283 ASSERT_TRUE(WaitForCallToComeUp(chrome_tab
));
285 // Ensure Firefox manages to send video our way.
286 ASSERT_TRUE(DetectRemoteVideoPlaying(chrome_tab
));