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 "base/win/windows_version.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/media/webrtc_browsertest_base.h"
14 #include "chrome/browser/media/webrtc_browsertest_common.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_tabstrip.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "media/base/media_switches.h"
22 #include "net/test/python_utils.h"
23 #include "ui/gl/gl_switches.h"
25 // You need this solution to run this test. The solution will download appengine
26 // and the apprtc code for you.
27 const char kAdviseOnGclientSolution[] =
28 "You need to add this solution to your .gclient to run this test:\n"
30 " \"name\" : \"webrtc.DEPS\",\n"
31 " \"url\" : \"svn://svn.chromium.org/chrome/trunk/deps/"
32 "third_party/webrtc/webrtc.DEPS\",\n"
34 const char kTitlePageOfAppEngineAdminPage[] = "Instances";
37 // WebRTC-AppRTC integration test. Requires a real webcam and microphone
38 // on the running system. This test is not meant to run in the main browser
39 // test suite since normal tester machines do not have webcams. Chrome will use
40 // its fake camera for both tests, but Firefox will use the real webcam in the
41 // Firefox interop test. Thus, this test must on a machine with a real webcam.
43 // This test will bring up a AppRTC instance on localhost and verify that the
44 // call gets up when connecting to the same room from two tabs in a browser.
45 class WebRtcApprtcBrowserTest : public WebRtcTestBase {
47 WebRtcApprtcBrowserTest()
48 : dev_appserver_(base::kNullProcessHandle),
49 firefox_(base::kNullProcessHandle) {
52 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
53 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream));
55 // The video playback will not work without a GPU, so force its use here.
56 command_line->AppendSwitch(switches::kUseGpuInTests);
57 CommandLine::ForCurrentProcess()->AppendSwitch(
58 switches::kUseFakeDeviceForMediaStream);
61 virtual void TearDown() OVERRIDE {
62 // Kill any processes we may have brought up.
63 if (dev_appserver_ != base::kNullProcessHandle)
64 base::KillProcess(dev_appserver_, 0, false);
65 // TODO(phoglund): Find some way to shut down Firefox cleanly on Windows.
66 if (firefox_ != base::kNullProcessHandle)
67 base::KillProcess(firefox_, 0, false);
71 bool LaunchApprtcInstanceOnLocalhost() {
72 base::FilePath appengine_dev_appserver =
73 GetSourceDir().Append(
74 FILE_PATH_LITERAL("../google_appengine/dev_appserver.py"));
75 if (!base::PathExists(appengine_dev_appserver)) {
76 LOG(ERROR) << "Missing appengine sdk at " <<
77 appengine_dev_appserver.value() << ". " << kAdviseOnGclientSolution;
81 base::FilePath apprtc_dir =
82 GetSourceDir().Append(FILE_PATH_LITERAL("out/apprtc"));
83 if (!base::PathExists(apprtc_dir)) {
84 LOG(ERROR) << "Missing AppRTC code at " <<
85 apprtc_dir.value() << ". " << kAdviseOnGclientSolution;
89 CommandLine command_line(CommandLine::NO_PROGRAM);
90 EXPECT_TRUE(GetPythonCommand(&command_line));
92 command_line.AppendArgPath(appengine_dev_appserver);
93 command_line.AppendArgPath(apprtc_dir);
94 command_line.AppendArg("--port=9999");
95 command_line.AppendArg("--admin_port=9998");
96 command_line.AppendArg("--skip_sdk_update_check");
98 VLOG(1) << "Running " << command_line.GetCommandLineString();
99 return base::LaunchProcess(command_line, base::LaunchOptions(),
103 bool LocalApprtcInstanceIsUp() {
104 // Load the admin page and see if we manage to load it right.
105 ui_test_utils::NavigateToURL(browser(), GURL("localhost:9998"));
106 content::WebContents* tab_contents =
107 browser()->tab_strip_model()->GetActiveWebContents();
108 std::string javascript =
109 "window.domAutomationController.send(document.title)";
111 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
115 return result == kTitlePageOfAppEngineAdminPage;
118 bool WaitForCallToComeUp(content::WebContents* tab_contents) {
119 // Apprtc will set remoteVideo.style.opacity to 1 when the call comes up.
120 std::string javascript =
121 "window.domAutomationController.send(remoteVideo.style.opacity)";
122 return test::PollingWaitUntil(javascript, "1", tab_contents);
125 bool EvalInJavascriptFile(content::WebContents* tab_contents,
126 const base::FilePath& path) {
127 std::string javascript;
128 if (!ReadFileToString(path, &javascript)) {
129 LOG(ERROR) << "Missing javascript code at " << path.value() << ".";
133 if (!content::ExecuteScript(tab_contents, javascript)) {
134 LOG(ERROR) << "Failed to execute the following javascript: " <<
141 bool DetectRemoteVideoPlaying(content::WebContents* tab_contents) {
142 if (!EvalInJavascriptFile(tab_contents, GetSourceDir().Append(
143 FILE_PATH_LITERAL("chrome/test/data/webrtc/test_functions.js"))))
145 if (!EvalInJavascriptFile(tab_contents, GetSourceDir().Append(
146 FILE_PATH_LITERAL("chrome/test/data/webrtc/video_detector.js"))))
149 // The remote video tag is called remoteVideo in the AppRTC code.
150 StartDetectingVideo(tab_contents, "remoteVideo");
151 WaitForVideoToPlay(tab_contents);
155 base::FilePath GetSourceDir() {
156 base::FilePath source_dir;
157 PathService::Get(base::DIR_SOURCE_ROOT, &source_dir);
161 bool LaunchFirefoxWithUrl(const GURL& url) {
162 base::FilePath firefox_binary =
163 GetSourceDir().Append(
164 FILE_PATH_LITERAL("../firefox-nightly/firefox/firefox"));
165 if (!base::PathExists(firefox_binary)) {
166 LOG(ERROR) << "Missing firefox binary at " <<
167 firefox_binary.value() << ". " << kAdviseOnGclientSolution;
170 base::FilePath firefox_launcher =
171 GetSourceDir().Append(
172 FILE_PATH_LITERAL("../webrtc.DEPS/run_firefox_webrtc.py"));
173 if (!base::PathExists(firefox_launcher)) {
174 LOG(ERROR) << "Missing firefox launcher at " <<
175 firefox_launcher.value() << ". " << kAdviseOnGclientSolution;
179 CommandLine command_line(firefox_launcher);
180 command_line.AppendSwitchPath("--binary", firefox_binary);
181 command_line.AppendSwitchASCII("--webpage", url.spec());
183 VLOG(1) << "Running " << command_line.GetCommandLineString();
184 return base::LaunchProcess(command_line, base::LaunchOptions(),
188 bool HasWebcamOnSystem() {
189 #if defined(OS_LINUX)
190 // Implementation note: normally we would be able to figure this out with
191 // MediaStreamTrack.getSources, but we can't ask Chrome since it runs in
192 // fake device mode where it will not enumerate webcams on the system.
193 // Therefore, look for /dev/video* entries directly since this test only
194 // runs on Linux for now anyway.
195 base::FileEnumerator dev_video(base::FilePath(FILE_PATH_LITERAL("/dev")),
196 false, base::FileEnumerator::FILES,
197 FILE_PATH_LITERAL("video*"));
198 return !dev_video.Next().empty();
205 base::ProcessHandle dev_appserver_;
206 base::ProcessHandle firefox_;
209 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest, MANUAL_WorksOnApprtc) {
210 // TODO(mcasas): Remove Win version filtering when this bug gets fixed:
211 // http://code.google.com/p/webrtc/issues/detail?id=2703
213 if (base::win::GetVersion() < base::win::VERSION_VISTA)
216 DetectErrorsInJavaScript();
217 ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
218 while (!LocalApprtcInstanceIsUp())
219 VLOG(1) << "Waiting for AppRTC to come up...";
221 GURL room_url = GURL(base::StringPrintf("localhost:9999?r=room_%d",
222 base::RandInt(0, 65536)));
224 chrome::AddTabAt(browser(), GURL(), -1, true);
225 content::WebContents* left_tab = OpenPageAndAcceptUserMedia(room_url);
226 chrome::AddTabAt(browser(), GURL(), -1, true);
227 content::WebContents* right_tab = OpenPageAndAcceptUserMedia(room_url);
229 ASSERT_TRUE(WaitForCallToComeUp(left_tab));
230 ASSERT_TRUE(WaitForCallToComeUp(right_tab));
232 ASSERT_TRUE(DetectRemoteVideoPlaying(left_tab));
233 ASSERT_TRUE(DetectRemoteVideoPlaying(right_tab));
236 #if defined(OS_LINUX)
237 #define MAYBE_MANUAL_FirefoxApprtcInteropTest MANUAL_FirefoxApprtcInteropTest
239 // Not implemented yet on Windows and Mac.
240 #define MAYBE_MANUAL_FirefoxApprtcInteropTest DISABLED_MANUAL_FirefoxApprtcInteropTest
243 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest,
244 MAYBE_MANUAL_FirefoxApprtcInteropTest) {
245 // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
247 if (base::win::GetVersion() < base::win::VERSION_VISTA)
250 if (!HasWebcamOnSystem()) {
252 << "Didn't find a webcam on the system; skipping test since Firefox "
253 << "needs to be able to acquire a webcam.";
257 DetectErrorsInJavaScript();
258 ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
259 while (!LocalApprtcInstanceIsUp())
260 VLOG(1) << "Waiting for AppRTC to come up...";
262 GURL room_url = GURL(base::StringPrintf("http://localhost:9999?r=room_%d",
263 base::RandInt(0, 65536)));
264 content::WebContents* chrome_tab = OpenPageAndAcceptUserMedia(room_url);
266 ASSERT_TRUE(LaunchFirefoxWithUrl(room_url));
268 ASSERT_TRUE(WaitForCallToComeUp(chrome_tab));
270 // Ensure Firefox manages to send video our way.
271 ASSERT_TRUE(DetectRemoteVideoPlaying(chrome_tab));