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/path_service.h"
7 #include "base/process/launch.h"
8 #include "base/rand_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/win/windows_version.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";
36 // WebRTC-AppRTC integration test. Requires a real webcam and microphone
37 // on the running system. This test is not meant to run in the main browser
38 // test suite since normal tester machines do not have webcams. Chrome will use
39 // its fake camera for both tests, but Firefox will use the real webcam in the
40 // Firefox interop test.
42 // This test will bring up a AppRTC instance on localhost and verify that the
43 // call gets up when connecting to the same room from two tabs in a browser.
44 class WebRtcApprtcBrowserTest
: public WebRtcTestBase
{
46 WebRtcApprtcBrowserTest()
47 : dev_appserver_(base::kNullProcessHandle
),
48 firefox_(base::kNullProcessHandle
) {
51 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
52 EXPECT_FALSE(command_line
->HasSwitch(switches::kUseFakeUIForMediaStream
));
54 // The video playback will not work without a GPU, so force its use here.
55 command_line
->AppendSwitch(switches::kUseGpuInTests
);
56 CommandLine::ForCurrentProcess()->AppendSwitch(
57 switches::kUseFakeDeviceForMediaStream
);
60 virtual void TearDown() OVERRIDE
{
61 // Kill any processes we may have brought up.
62 if (dev_appserver_
!= base::kNullProcessHandle
)
63 base::KillProcess(dev_appserver_
, 0, false);
64 // TODO(phoglund): Find some way to shut down Firefox cleanly on Windows.
65 if (firefox_
!= base::kNullProcessHandle
)
66 base::KillProcess(firefox_
, 0, false);
70 bool LaunchApprtcInstanceOnLocalhost() {
71 base::FilePath appengine_dev_appserver
=
72 GetSourceDir().Append(
73 FILE_PATH_LITERAL("../google_appengine/dev_appserver.py"));
74 if (!base::PathExists(appengine_dev_appserver
)) {
75 LOG(ERROR
) << "Missing appengine sdk at " <<
76 appengine_dev_appserver
.value() << ". " << kAdviseOnGclientSolution
;
80 base::FilePath apprtc_dir
=
81 GetSourceDir().Append(FILE_PATH_LITERAL("out/apprtc"));
82 if (!base::PathExists(apprtc_dir
)) {
83 LOG(ERROR
) << "Missing AppRTC code at " <<
84 apprtc_dir
.value() << ". " << kAdviseOnGclientSolution
;
88 CommandLine
command_line(CommandLine::NO_PROGRAM
);
89 EXPECT_TRUE(GetPythonCommand(&command_line
));
91 command_line
.AppendArgPath(appengine_dev_appserver
);
92 command_line
.AppendArgPath(apprtc_dir
);
93 command_line
.AppendArg("--port=9999");
94 command_line
.AppendArg("--admin_port=9998");
95 command_line
.AppendArg("--skip_sdk_update_check");
97 VLOG(1) << "Running " << command_line
.GetCommandLineString();
98 return base::LaunchProcess(command_line
, base::LaunchOptions(),
102 bool LocalApprtcInstanceIsUp() {
103 // Load the admin page and see if we manage to load it right.
104 ui_test_utils::NavigateToURL(browser(), GURL("localhost:9998"));
105 content::WebContents
* tab_contents
=
106 browser()->tab_strip_model()->GetActiveWebContents();
107 std::string javascript
=
108 "window.domAutomationController.send(document.title)";
110 if (!content::ExecuteScriptAndExtractString(tab_contents
, javascript
,
114 return result
== kTitlePageOfAppEngineAdminPage
;
117 bool WaitForCallToComeUp(content::WebContents
* tab_contents
) {
118 // Apprtc will set remoteVideo.style.opacity to 1 when the call comes up.
119 std::string javascript
=
120 "window.domAutomationController.send(remoteVideo.style.opacity)";
121 return test::PollingWaitUntil(javascript
, "1", tab_contents
);
124 bool EvalInJavascriptFile(content::WebContents
* tab_contents
,
125 const base::FilePath
& path
) {
126 std::string javascript
;
127 if (!ReadFileToString(path
, &javascript
)) {
128 LOG(ERROR
) << "Missing javascript code at " << path
.value() << ".";
132 if (!content::ExecuteScript(tab_contents
, javascript
)) {
133 LOG(ERROR
) << "Failed to execute the following javascript: " <<
140 bool DetectRemoteVideoPlaying(content::WebContents
* tab_contents
) {
141 if (!EvalInJavascriptFile(tab_contents
, GetSourceDir().Append(
142 FILE_PATH_LITERAL("chrome/test/data/webrtc/test_functions.js"))))
144 if (!EvalInJavascriptFile(tab_contents
, GetSourceDir().Append(
145 FILE_PATH_LITERAL("chrome/test/data/webrtc/video_detector.js"))))
148 // The remote video tag is called remoteVideo in the AppRTC code.
149 StartDetectingVideo(tab_contents
, "remoteVideo");
150 WaitForVideoToPlay(tab_contents
);
154 base::FilePath
GetSourceDir() {
155 base::FilePath source_dir
;
156 PathService::Get(base::DIR_SOURCE_ROOT
, &source_dir
);
160 bool LaunchFirefoxWithUrl(const GURL
& url
) {
161 base::FilePath firefox_binary
=
162 GetSourceDir().Append(
163 FILE_PATH_LITERAL("../firefox-nightly/firefox/firefox"));
164 if (!base::PathExists(firefox_binary
)) {
165 LOG(ERROR
) << "Missing firefox binary at " <<
166 firefox_binary
.value() << ". " << kAdviseOnGclientSolution
;
169 base::FilePath firefox_launcher
=
170 GetSourceDir().Append(
171 FILE_PATH_LITERAL("../webrtc.DEPS/run_firefox_webrtc.py"));
172 if (!base::PathExists(firefox_launcher
)) {
173 LOG(ERROR
) << "Missing firefox launcher at " <<
174 firefox_launcher
.value() << ". " << kAdviseOnGclientSolution
;
178 CommandLine
command_line(firefox_launcher
);
179 command_line
.AppendSwitchPath("--binary", firefox_binary
);
180 command_line
.AppendSwitchASCII("--webpage", url
.spec());
182 VLOG(1) << "Running " << command_line
.GetCommandLineString();
183 return base::LaunchProcess(command_line
, base::LaunchOptions(),
188 base::ProcessHandle dev_appserver_
;
189 base::ProcessHandle firefox_
;
192 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest
, MANUAL_WorksOnApprtc
) {
193 // TODO(mcasas): Remove Win version filtering when this bug gets fixed:
194 // http://code.google.com/p/webrtc/issues/detail?id=2703
196 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
199 DetectErrorsInJavaScript();
200 ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
201 while (!LocalApprtcInstanceIsUp())
202 VLOG(1) << "Waiting for AppRTC to come up...";
204 GURL room_url
= GURL(base::StringPrintf("localhost:9999?r=room_%d",
205 base::RandInt(0, 65536)));
207 chrome::AddTabAt(browser(), GURL(), -1, true);
208 content::WebContents
* left_tab
= OpenPageAndAcceptUserMedia(room_url
);
209 chrome::AddTabAt(browser(), GURL(), -1, true);
210 content::WebContents
* right_tab
= OpenPageAndAcceptUserMedia(room_url
);
212 ASSERT_TRUE(WaitForCallToComeUp(left_tab
));
213 ASSERT_TRUE(WaitForCallToComeUp(right_tab
));
215 ASSERT_TRUE(DetectRemoteVideoPlaying(left_tab
));
216 ASSERT_TRUE(DetectRemoteVideoPlaying(right_tab
));
219 #if defined(OS_LINUX)
220 #define MAYBE_MANUAL_FirefoxApprtcInteropTest DISABLED_MANUAL_FirefoxApprtcInteropTest
222 // Not implemented yet on Windows and Mac.
223 #define MAYBE_MANUAL_FirefoxApprtcInteropTest DISABLED_MANUAL_FirefoxApprtcInteropTest
226 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest
,
227 MAYBE_MANUAL_FirefoxApprtcInteropTest
) {
228 // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
230 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
234 DetectErrorsInJavaScript();
235 ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
236 while (!LocalApprtcInstanceIsUp())
237 VLOG(1) << "Waiting for AppRTC to come up...";
239 GURL room_url
= GURL(base::StringPrintf("http://localhost:9999?r=room_%d",
240 base::RandInt(0, 65536)));
241 content::WebContents
* chrome_tab
= OpenPageAndAcceptUserMedia(room_url
);
243 ASSERT_TRUE(LaunchFirefoxWithUrl(room_url
));
245 ASSERT_TRUE(WaitForCallToComeUp(chrome_tab
));
247 // Ensure Firefox manages to send video our way.
248 ASSERT_TRUE(DetectRemoteVideoPlaying(chrome_tab
));