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/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"
35 static const char kMainWebrtcTestHtmlPage
[] =
36 "/webrtc/webrtc_jsep01_test.html";
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
{
43 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
{
44 PeerConnectionServerRunner::KillAllPeerConnectionServersOnCurrentSystem();
45 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
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
));
56 // Flag used by TestWebAudioMediaStream to force garbage collection.
57 command_line
->AppendSwitchASCII(switches::kJavaScriptFlags
, "--expose-gc");
60 void EstablishCall(content::WebContents
* from_tab
,
61 content::WebContents
* to_tab
) {
62 ConnectToPeerConnectionServer("peer 1", from_tab
);
63 ConnectToPeerConnectionServer("peer 2", to_tab
);
65 EXPECT_EQ("ok-peerconnection-created",
66 ExecuteJavascript("preparePeerConnection()", from_tab
));
68 ExecuteJavascript("addLocalStream()", from_tab
));
69 EXPECT_EQ("ok-negotiating",
70 ExecuteJavascript("negotiateCall()", from_tab
));
72 // Ensure the call gets up on both sides.
73 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
75 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
79 void StartDetectingVideo(content::WebContents
* tab_contents
,
80 const std::string
& video_element
) {
81 std::string javascript
= base::StringPrintf(
82 "startDetection('%s', 'frame-buffer', 320, 240)",
83 video_element
.c_str());
84 EXPECT_EQ("ok-started", ExecuteJavascript(javascript
, tab_contents
));
87 void WaitForVideoToPlay(content::WebContents
* tab_contents
) {
88 EXPECT_TRUE(PollingWaitUntil("isVideoPlaying()", "video-playing",
92 void HangUp(content::WebContents
* from_tab
) {
93 EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab
));
96 void WaitUntilHangupVerified(content::WebContents
* tab_contents
) {
97 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
98 "no-peer-connection", tab_contents
));
101 void PrintProcessMetrics(base::ProcessMetrics
* process_metrics
,
102 const std::string
& suffix
) {
103 perf_test::PrintResult("cpu", "", "cpu" + suffix
,
104 process_metrics
->GetCPUUsage(),
106 perf_test::PrintResult("memory", "", "ws_peak" + suffix
,
107 process_metrics
->GetPeakWorkingSetSize(),
109 perf_test::PrintResult("memory", "", "ws_final" + suffix
,
110 process_metrics
->GetWorkingSetSize(),
115 if (process_metrics
->GetMemoryBytes(&private_mem
, &shared_mem
)) {
116 perf_test::PrintResult("memory", "", "private_mem_final" + suffix
,
117 private_mem
, "bytes", false);
118 perf_test::PrintResult("memory", "", "shared_mem_final" + suffix
,
119 shared_mem
, "bytes", false);
123 // Tries to extract data from peerConnectionDataStore in the webrtc-internals
124 // tab. The caller owns the parsed data. Returns NULL on failure.
125 base::DictionaryValue
* GetWebrtcInternalsData(
126 content::WebContents
* webrtc_internals_tab
) {
127 std::string all_stats_json
= ExecuteJavascript(
128 "window.domAutomationController.send("
129 " JSON.stringify(peerConnectionDataStore));",
130 webrtc_internals_tab
);
132 base::Value
* parsed_json
= base::JSONReader::Read(all_stats_json
);
133 base::DictionaryValue
* result
;
134 if (parsed_json
&& parsed_json
->GetAsDictionary(&result
))
140 const base::DictionaryValue
* GetDataOnFirstPeerConnection(
141 const base::DictionaryValue
* all_data
) {
142 base::DictionaryValue::Iterator
iterator(*all_data
);
144 const base::DictionaryValue
* result
;
145 if (!iterator
.IsAtEnd() && iterator
.value().GetAsDictionary(&result
))
151 content::WebContents
* OpenTestPageAndGetUserMediaInNewTab() {
152 return OpenPageAndGetUserMediaInNewTab(
153 embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage
));
156 PeerConnectionServerRunner peerconnection_server_
;
159 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest
,
160 MANUAL_RunsAudioVideoWebRTCCallInTwoTabs
) {
161 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
162 ASSERT_TRUE(peerconnection_server_
.Start());
164 content::WebContents
* left_tab
= OpenTestPageAndGetUserMediaInNewTab();
165 content::WebContents
* right_tab
= OpenTestPageAndGetUserMediaInNewTab();
167 EstablishCall(left_tab
, right_tab
);
169 StartDetectingVideo(left_tab
, "remote-view");
170 StartDetectingVideo(right_tab
, "remote-view");
172 WaitForVideoToPlay(left_tab
);
173 WaitForVideoToPlay(right_tab
);
176 WaitUntilHangupVerified(left_tab
);
177 WaitUntilHangupVerified(right_tab
);
179 ASSERT_TRUE(peerconnection_server_
.Stop());
182 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest
, MANUAL_CpuUsage15Seconds
) {
183 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
184 ASSERT_TRUE(peerconnection_server_
.Start());
186 base::FilePath results_file
;
187 ASSERT_TRUE(base::CreateTemporaryFile(&results_file
));
189 content::WebContents
* left_tab
= OpenTestPageAndGetUserMediaInNewTab();
191 #if defined(OS_MACOSX)
192 // Don't measure renderer CPU on mac: requires a mach broker we don't have
193 // access to from the browser test.
194 scoped_ptr
<base::ProcessMetrics
> browser_process_metrics(
195 base::ProcessMetrics::CreateProcessMetrics(
196 base::Process::Current().handle(), NULL
));
197 browser_process_metrics
->GetCPUUsage();
199 // Measure rendering CPU on platforms that support it.
200 base::ProcessHandle renderer_pid
=
201 left_tab
->GetRenderProcessHost()->GetHandle();
202 scoped_ptr
<base::ProcessMetrics
> renderer_process_metrics(
203 base::ProcessMetrics::CreateProcessMetrics(renderer_pid
));
204 renderer_process_metrics
->GetCPUUsage();
206 scoped_ptr
<base::ProcessMetrics
> browser_process_metrics(
207 base::ProcessMetrics::CreateProcessMetrics(
208 base::Process::Current().handle()));
209 browser_process_metrics
->GetCPUUsage();
212 content::WebContents
* right_tab
= OpenTestPageAndGetUserMediaInNewTab();
214 EstablishCall(left_tab
, right_tab
);
216 SleepInJavascript(left_tab
, 15000);
219 WaitUntilHangupVerified(left_tab
);
220 WaitUntilHangupVerified(right_tab
);
222 #if !defined(OS_MACOSX)
223 PrintProcessMetrics(renderer_process_metrics
.get(), "_r");
225 PrintProcessMetrics(browser_process_metrics
.get(), "_b");
227 ASSERT_TRUE(peerconnection_server_
.Stop());
230 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest
,
231 MANUAL_RunsAudioVideoCall60SecsAndLogsInternalMetrics
) {
232 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
233 ASSERT_TRUE(peerconnection_server_
.Start());
235 ASSERT_GE(TestTimeouts::action_max_timeout().InSeconds(), 80) <<
236 "This is a long-running test; you must specify "
237 "--ui-test-action-max-timeout to have a value of at least 80000.";
239 content::WebContents
* left_tab
= OpenTestPageAndGetUserMediaInNewTab();
240 content::WebContents
* right_tab
= OpenTestPageAndGetUserMediaInNewTab();
242 EstablishCall(left_tab
, right_tab
);
244 StartDetectingVideo(left_tab
, "remote-view");
245 StartDetectingVideo(right_tab
, "remote-view");
247 WaitForVideoToPlay(left_tab
);
248 WaitForVideoToPlay(right_tab
);
250 // Let values stabilize, bandwidth ramp up, etc.
251 SleepInJavascript(left_tab
, 60000);
253 // Start measurements.
254 chrome::AddTabAt(browser(), GURL(), -1, true);
255 ui_test_utils::NavigateToURL(browser(), GURL("chrome://webrtc-internals"));
256 content::WebContents
* webrtc_internals_tab
=
257 browser()->tab_strip_model()->GetActiveWebContents();
259 SleepInJavascript(left_tab
, 10000);
261 scoped_ptr
<base::DictionaryValue
> all_data(
262 GetWebrtcInternalsData(webrtc_internals_tab
));
263 ASSERT_TRUE(all_data
.get() != NULL
);
265 const base::DictionaryValue
* first_pc_dict
=
266 GetDataOnFirstPeerConnection(all_data
.get());
267 ASSERT_TRUE(first_pc_dict
!= NULL
);
268 PrintBweForVideoMetrics(*first_pc_dict
);
269 PrintMetricsForAllStreams(*first_pc_dict
);
272 WaitUntilHangupVerified(left_tab
);
273 WaitUntilHangupVerified(right_tab
);
275 ASSERT_TRUE(peerconnection_server_
.Stop());
278 IN_PROC_BROWSER_TEST_F(WebRtcBrowserTest
, TestWebAudioMediaStream
) {
279 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
280 GURL
url(embedded_test_server()->GetURL("/webrtc/webaudio_crash.html"));
281 ui_test_utils::NavigateToURL(browser(), url
);
282 content::WebContents
* tab
=
283 browser()->tab_strip_model()->GetActiveWebContents();
284 // A sleep is necessary to be able to detect the crash.
285 SleepInJavascript(tab
, 1000);
287 ASSERT_FALSE(tab
->IsCrashed());