Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / chrome_webrtc_apprtc_browsertest.cc
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.
4
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 "chrome/test/ui/ui_test.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"
24
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"
29     "{\n"
30     "  \"name\"        : \"webrtc.DEPS\",\n"
31     "  \"url\"         : \"svn://svn.chromium.org/chrome/trunk/deps/"
32     "third_party/webrtc/webrtc.DEPS\",\n"
33     "}";
34 const char kTitlePageOfAppEngineAdminPage[] = "Instances";
35
36
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.
42 //
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 {
46  public:
47   WebRtcApprtcBrowserTest()
48       : dev_appserver_(base::kNullProcessHandle),
49         firefox_(base::kNullProcessHandle) {
50   }
51
52   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
53     EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream));
54
55     // The video playback will not work without a GPU, so force its use here.
56     command_line->AppendSwitch(switches::kUseGpuInTests);
57
58 #if defined(OS_MACOSX)
59     // TODO(mcasas): Remove this switch when ManyCam virtual video capture
60     // device starts supporting AVFoundation, see http://crbug.com/327618.
61     command_line->AppendSwitch(switches::kDisableAVFoundation);
62 #endif
63
64     CommandLine::ForCurrentProcess()->AppendSwitch(
65         switches::kUseFakeDeviceForMediaStream);
66   }
67
68   virtual void TearDown() OVERRIDE {
69     // Kill any processes we may have brought up.
70     if (dev_appserver_ != base::kNullProcessHandle)
71       base::KillProcess(dev_appserver_, 0, false);
72     // TODO(phoglund): Find some way to shut down Firefox cleanly on Windows.
73     if (firefox_ != base::kNullProcessHandle)
74       base::KillProcess(firefox_, 0, false);
75   }
76
77  protected:
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;
85       return false;
86     }
87
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;
93       return false;
94     }
95
96     CommandLine command_line(CommandLine::NO_PROGRAM);
97     EXPECT_TRUE(GetPythonCommand(&command_line));
98
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
105     VLOG(1) << "Running " << command_line.GetCommandLineString();
106     return base::LaunchProcess(command_line, base::LaunchOptions(),
107                                &dev_appserver_);
108   }
109
110   bool LocalApprtcInstanceIsUp() {
111     // Load the admin page and see if we manage to load it right.
112     ui_test_utils::NavigateToURL(browser(), GURL("localhost:9998"));
113     content::WebContents* tab_contents =
114         browser()->tab_strip_model()->GetActiveWebContents();
115     std::string javascript =
116         "window.domAutomationController.send(document.title)";
117     std::string result;
118     if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
119                                                 &result))
120       return false;
121
122     return result == kTitlePageOfAppEngineAdminPage;
123   }
124
125   bool WaitForCallToComeUp(content::WebContents* tab_contents) {
126     // Apprtc will set remoteVideo.style.opacity to 1 when the call comes up.
127     std::string javascript =
128         "window.domAutomationController.send(remoteVideo.style.opacity)";
129     return PollingWaitUntil(javascript, "1", tab_contents);
130   }
131
132   bool EvalInJavascriptFile(content::WebContents* tab_contents,
133                             const base::FilePath& path) {
134     std::string javascript;
135     if (!ReadFileToString(path, &javascript)) {
136       LOG(ERROR) << "Missing javascript code at " << path.value() << ".";
137       return false;
138     }
139
140     if (!content::ExecuteScript(tab_contents, javascript)) {
141       LOG(ERROR) << "Failed to execute the following javascript: " <<
142           javascript;
143       return false;
144     }
145     return true;
146   }
147
148   bool DetectRemoteVideoPlaying(content::WebContents* tab_contents) {
149     if (!EvalInJavascriptFile(tab_contents, GetSourceDir().Append(
150         FILE_PATH_LITERAL("chrome/test/data/webrtc/test_functions.js"))))
151       return false;
152     if (!EvalInJavascriptFile(tab_contents, GetSourceDir().Append(
153         FILE_PATH_LITERAL("chrome/test/data/webrtc/video_detector.js"))))
154       return false;
155
156     // The remote video tag is called remoteVideo in the AppRTC code.
157     StartDetectingVideo(tab_contents, "remoteVideo");
158     WaitForVideoToPlay(tab_contents);
159     return true;
160   }
161
162   base::FilePath GetSourceDir() {
163     base::FilePath source_dir;
164     PathService::Get(base::DIR_SOURCE_ROOT, &source_dir);
165     return source_dir;
166   }
167
168   bool LaunchFirefoxWithUrl(const GURL& url) {
169     base::FilePath firefox_binary =
170         GetSourceDir().Append(
171             FILE_PATH_LITERAL("../firefox-nightly/firefox/firefox"));
172     if (!base::PathExists(firefox_binary)) {
173       LOG(ERROR) << "Missing firefox binary at " <<
174           firefox_binary.value() << ". " << kAdviseOnGclientSolution;
175       return false;
176     }
177     base::FilePath firefox_launcher =
178         GetSourceDir().Append(
179             FILE_PATH_LITERAL("../webrtc.DEPS/run_firefox_webrtc.py"));
180     if (!base::PathExists(firefox_launcher)) {
181       LOG(ERROR) << "Missing firefox launcher at " <<
182           firefox_launcher.value() << ". " << kAdviseOnGclientSolution;
183       return false;
184     }
185
186     CommandLine command_line(firefox_launcher);
187     command_line.AppendSwitchPath("--binary", firefox_binary);
188     command_line.AppendSwitchASCII("--webpage", url.spec());
189
190     VLOG(1) << "Running " << command_line.GetCommandLineString();
191     return base::LaunchProcess(command_line, base::LaunchOptions(),
192                                &firefox_);
193
194     return true;
195   }
196
197  private:
198   base::ProcessHandle dev_appserver_;
199   base::ProcessHandle firefox_;
200 };
201
202 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest, MANUAL_WorksOnApprtc) {
203   // TODO(mcasas): Remove Win version filtering when this bug gets fixed:
204   // http://code.google.com/p/webrtc/issues/detail?id=2703
205 #if defined(OS_WIN)
206   if (base::win::GetVersion() < base::win::VERSION_VISTA)
207     return;
208 #endif
209   DetectErrorsInJavaScript();
210   ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
211   while (!LocalApprtcInstanceIsUp())
212     VLOG(1) << "Waiting for AppRTC to come up...";
213
214   GURL room_url = GURL(base::StringPrintf("localhost:9999?r=room_%d",
215                                           base::RandInt(0, 65536)));
216
217   chrome::AddTabAt(browser(), GURL(), -1, true);
218   content::WebContents* left_tab = OpenPageAndAcceptUserMedia(room_url);
219   chrome::AddTabAt(browser(), GURL(), -1, true);
220   content::WebContents* right_tab = OpenPageAndAcceptUserMedia(room_url);
221
222   ASSERT_TRUE(WaitForCallToComeUp(left_tab));
223   ASSERT_TRUE(WaitForCallToComeUp(right_tab));
224
225   ASSERT_TRUE(DetectRemoteVideoPlaying(left_tab));
226   ASSERT_TRUE(DetectRemoteVideoPlaying(right_tab));
227 }
228
229 #if defined(OS_LINUX)
230 #define MAYBE_MANUAL_FirefoxApprtcInteropTest MANUAL_FirefoxApprtcInteropTest
231 #else
232 // Not implemented yet on Windows and Mac.
233 #define MAYBE_MANUAL_FirefoxApprtcInteropTest DISABLED_MANUAL_FirefoxApprtcInteropTest
234 #endif
235
236 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest,
237                        MAYBE_MANUAL_FirefoxApprtcInteropTest) {
238   // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
239 #if defined(OS_WIN)
240   if (base::win::GetVersion() < base::win::VERSION_VISTA)
241     return;
242 #endif
243
244   DetectErrorsInJavaScript();
245   ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
246   while (!LocalApprtcInstanceIsUp())
247     VLOG(1) << "Waiting for AppRTC to come up...";
248
249   GURL room_url = GURL(base::StringPrintf("http://localhost:9999?r=room_%d",
250                                           base::RandInt(0, 65536)));
251   content::WebContents* chrome_tab = OpenPageAndAcceptUserMedia(room_url);
252
253   ASSERT_TRUE(LaunchFirefoxWithUrl(room_url));
254
255   ASSERT_TRUE(WaitForCallToComeUp(chrome_tab));
256
257   // Ensure Firefox manages to send video our way.
258   ASSERT_TRUE(DetectRemoteVideoPlaying(chrome_tab));
259 }