Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / chrome_webrtc_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/file_util.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/path_service.h"
10 #include "base/process/launch.h"
11 #include "base/process/process_metrics.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/test/test_timeouts.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/media/webrtc_browsertest_base.h"
19 #include "chrome/browser/media/webrtc_browsertest_common.h"
20 #include "chrome/browser/media/webrtc_browsertest_perf.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_tabstrip.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/test/base/in_process_browser_test.h"
26 #include "chrome/test/base/ui_test_utils.h"
27 #include "chrome/test/ui/ui_test.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/render_process_host.h"
30 #include "content/public/test/browser_test_utils.h"
31 #include "media/base/media_switches.h"
32 #include "net/test/embedded_test_server/embedded_test_server.h"
33 #include "testing/perf/perf_test.h"
34
35 static const char kMainWebrtcTestHtmlPage[] =
36     "/webrtc/webrtc_jsep01_test.html";
37
38 // Top-level integration test for WebRTC. Requires a real webcam and microphone
39 // on the running system. This test is not meant to run in the main browser
40 // test suite since normal tester machines do not have webcams.
41 class WebRtcBrowserTest : public WebRtcTestBase {
42  public:
43   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
44     PeerConnectionServerRunner::KillAllPeerConnectionServersOnCurrentSystem();
45     DetectErrorsInJavaScript();  // Look for errors in our rather complex js.
46   }
47
48   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
49     // This test expects real device handling and requires a real webcam / audio
50     // device; it will not work with fake devices.
51     EXPECT_FALSE(command_line->HasSwitch(
52         switches::kUseFakeDeviceForMediaStream));
53     EXPECT_FALSE(command_line->HasSwitch(
54         switches::kUseFakeUIForMediaStream));
55
56 #if defined(OS_MACOSX)
57     // TODO(mcasas): Remove this switch when ManyCam virtual video capture
58     // device starts supporting AVFoundation, see http://crbug.com/327618.
59     command_line->AppendSwitch(switches::kDisableAVFoundation);
60 #endif
61
62     // Flag used by TestWebAudioMediaStream to force garbage collection.
63     command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
64   }
65
66   void EstablishCall(content::WebContents* from_tab,
67                      content::WebContents* to_tab) {
68     ConnectToPeerConnectionServer("peer 1", from_tab);
69     ConnectToPeerConnectionServer("peer 2", to_tab);
70
71     EXPECT_EQ("ok-peerconnection-created",
72               ExecuteJavascript("preparePeerConnection()", from_tab));
73     EXPECT_EQ("ok-added",
74               ExecuteJavascript("addLocalStream()", from_tab));
75     EXPECT_EQ("ok-negotiating",
76               ExecuteJavascript("negotiateCall()", from_tab));
77
78     // Ensure the call gets up on both sides.
79     EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
80                                  "active", from_tab));
81     EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
82                                  "active", to_tab));
83   }
84
85   void HangUp(content::WebContents* from_tab) {
86     EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab));
87   }
88
89   void WaitUntilHangupVerified(content::WebContents* tab_contents) {
90     EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
91                                  "no-peer-connection", tab_contents));
92   }
93
94   void PrintProcessMetrics(base::ProcessMetrics* process_metrics,
95                            const std::string& suffix) {
96     perf_test::PrintResult("cpu", "", "cpu" + suffix,
97                            process_metrics->GetCPUUsage(),
98                            "%", true);
99     perf_test::PrintResult("memory", "", "ws_peak" + suffix,
100                            process_metrics->GetPeakWorkingSetSize(),
101                            "bytes", true);
102     perf_test::PrintResult("memory", "", "ws_final" + suffix,
103                            process_metrics->GetWorkingSetSize(),
104                            "bytes", false);
105
106     size_t private_mem;
107     size_t shared_mem;
108     if (process_metrics->GetMemoryBytes(&private_mem, &shared_mem)) {
109       perf_test::PrintResult("memory", "", "private_mem_final" + suffix,
110                              private_mem, "bytes", false);
111       perf_test::PrintResult("memory", "", "shared_mem_final" + suffix,
112                              shared_mem, "bytes", false);
113     }
114   }
115
116   // Tries to extract data from peerConnectionDataStore in the webrtc-internals
117   // tab. The caller owns the parsed data. Returns NULL on failure.
118   base::DictionaryValue* GetWebrtcInternalsData(
119       content::WebContents* webrtc_internals_tab) {
120     std::string all_stats_json = ExecuteJavascript(
121         "window.domAutomationController.send("
122         "    JSON.stringify(peerConnectionDataStore));",
123         webrtc_internals_tab);
124
125     base::Value* parsed_json = base::JSONReader::Read(all_stats_json);
126     base::DictionaryValue* result;
127     if (parsed_json && parsed_json->GetAsDictionary(&result))
128       return result;
129
130     return NULL;
131   }
132
133   const base::DictionaryValue* GetDataOnFirstPeerConnection(
134       const base::DictionaryValue* all_data) {
135     base::DictionaryValue::Iterator iterator(*all_data);
136
137     const base::DictionaryValue* result;
138     if (!iterator.IsAtEnd() && iterator.value().GetAsDictionary(&result))
139       return result;
140
141     return NULL;
142   }
143
144   content::WebContents* OpenTestPageAndGetUserMediaInNewTab() {
145     return OpenPageAndGetUserMediaInNewTab(
146         embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
147   }
148
149   PeerConnectionServerRunner peerconnection_server_;
150 };
151
152 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest,
153                        MANUAL_RunsAudioVideoWebRTCCallInTwoTabs) {
154   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
155   ASSERT_TRUE(peerconnection_server_.Start());
156
157   content::WebContents* left_tab = OpenTestPageAndGetUserMediaInNewTab();
158   content::WebContents* right_tab = OpenTestPageAndGetUserMediaInNewTab();
159
160   EstablishCall(left_tab, right_tab);
161
162   StartDetectingVideo(left_tab, "remote-view");
163   StartDetectingVideo(right_tab, "remote-view");
164
165   WaitForVideoToPlay(left_tab);
166   WaitForVideoToPlay(right_tab);
167
168   HangUp(left_tab);
169   WaitUntilHangupVerified(left_tab);
170   WaitUntilHangupVerified(right_tab);
171
172   ASSERT_TRUE(peerconnection_server_.Stop());
173 }
174
175 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest, MANUAL_CpuUsage15Seconds) {
176   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
177   ASSERT_TRUE(peerconnection_server_.Start());
178
179   base::FilePath results_file;
180   ASSERT_TRUE(base::CreateTemporaryFile(&results_file));
181
182   content::WebContents* left_tab = OpenTestPageAndGetUserMediaInNewTab();
183
184 #if defined(OS_MACOSX)
185   // Don't measure renderer CPU on mac: requires a mach broker we don't have
186   // access to from the browser test.
187   scoped_ptr<base::ProcessMetrics> browser_process_metrics(
188       base::ProcessMetrics::CreateProcessMetrics(
189           base::Process::Current().handle(), NULL));
190   browser_process_metrics->GetCPUUsage();
191 #else
192   // Measure rendering CPU on platforms that support it.
193   base::ProcessHandle renderer_pid =
194       left_tab->GetRenderProcessHost()->GetHandle();
195   scoped_ptr<base::ProcessMetrics> renderer_process_metrics(
196       base::ProcessMetrics::CreateProcessMetrics(renderer_pid));
197   renderer_process_metrics->GetCPUUsage();
198
199   scoped_ptr<base::ProcessMetrics> browser_process_metrics(
200       base::ProcessMetrics::CreateProcessMetrics(
201           base::Process::Current().handle()));
202   browser_process_metrics->GetCPUUsage();
203 #endif
204
205   content::WebContents* right_tab = OpenTestPageAndGetUserMediaInNewTab();
206
207   EstablishCall(left_tab, right_tab);
208
209   SleepInJavascript(left_tab, 15000);
210
211   HangUp(left_tab);
212   WaitUntilHangupVerified(left_tab);
213   WaitUntilHangupVerified(right_tab);
214
215 #if !defined(OS_MACOSX)
216   PrintProcessMetrics(renderer_process_metrics.get(), "_r");
217 #endif
218   PrintProcessMetrics(browser_process_metrics.get(), "_b");
219
220   ASSERT_TRUE(peerconnection_server_.Stop());
221 }
222
223 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest,
224                        MANUAL_RunsAudioVideoCall60SecsAndLogsInternalMetrics) {
225   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
226   ASSERT_TRUE(peerconnection_server_.Start());
227
228   ASSERT_GE(TestTimeouts::action_max_timeout().InSeconds(), 80) <<
229       "This is a long-running test; you must specify "
230       "--ui-test-action-max-timeout to have a value of at least 80000.";
231
232   content::WebContents* left_tab = OpenTestPageAndGetUserMediaInNewTab();
233   content::WebContents* right_tab = OpenTestPageAndGetUserMediaInNewTab();
234
235   EstablishCall(left_tab, right_tab);
236
237   StartDetectingVideo(left_tab, "remote-view");
238   StartDetectingVideo(right_tab, "remote-view");
239
240   WaitForVideoToPlay(left_tab);
241   WaitForVideoToPlay(right_tab);
242
243   // Let values stabilize, bandwidth ramp up, etc.
244   SleepInJavascript(left_tab, 60000);
245
246   // Start measurements.
247   chrome::AddTabAt(browser(), GURL(), -1, true);
248   ui_test_utils::NavigateToURL(browser(), GURL("chrome://webrtc-internals"));
249   content::WebContents* webrtc_internals_tab =
250       browser()->tab_strip_model()->GetActiveWebContents();
251
252   SleepInJavascript(left_tab, 10000);
253
254   scoped_ptr<base::DictionaryValue> all_data(
255       GetWebrtcInternalsData(webrtc_internals_tab));
256   ASSERT_TRUE(all_data.get() != NULL);
257
258   const base::DictionaryValue* first_pc_dict =
259       GetDataOnFirstPeerConnection(all_data.get());
260   ASSERT_TRUE(first_pc_dict != NULL);
261   PrintBweForVideoMetrics(*first_pc_dict);
262   PrintMetricsForAllStreams(*first_pc_dict);
263
264   HangUp(left_tab);
265   WaitUntilHangupVerified(left_tab);
266   WaitUntilHangupVerified(right_tab);
267
268   ASSERT_TRUE(peerconnection_server_.Stop());
269 }
270
271 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest, TestWebAudioMediaStream) {
272   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
273   GURL url(embedded_test_server()->GetURL("/webrtc/webaudio_crash.html"));
274   ui_test_utils::NavigateToURL(browser(), url);
275   content::WebContents* tab =
276       browser()->tab_strip_model()->GetActiveWebContents();
277   // A sleep is necessary to be able to detect the crash.
278   SleepInJavascript(tab, 1000);
279
280   ASSERT_FALSE(tab->IsCrashed());
281 }