[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / media / chrome_webrtc_browsertest.cc
blob6fd8643b967b6c923970379c4c30caffa4203bff
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 "content/public/browser/notification_service.h"
28 #include "content/public/browser/render_process_host.h"
29 #include "content/public/test/browser_test_utils.h"
30 #include "media/base/media_switches.h"
31 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "testing/perf/perf_test.h"
34 // For fine-grained suppression.
35 #if defined(OS_WIN)
36 #include "base/win/windows_version.h"
37 #endif
39 static const char kMainWebrtcTestHtmlPage[] =
40 "/webrtc/webrtc_jsep01_test.html";
42 // Top-level integration test for WebRTC. The test methods here must run
43 // sequentially since they use a server binary on the system (hence they are
44 // tagged as MANUAL). In addition, they need the reference videos which require
45 // the webrtc.DEPS solution, which is not generally available on Chromium bots.
46 class WebRtcBrowserTest : public WebRtcTestBase,
47 public testing::WithParamInterface<bool> {
48 public:
49 WebRtcBrowserTest() {}
50 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
51 test::PeerConnectionServerRunner::KillAllPeerConnectionServers();
52 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
55 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
56 // Ensure the infobar is enabled, since we expect that in this test.
57 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream));
59 // Play a suitable, somewhat realistic video file.
60 base::FilePath input_video = test::GetReferenceFilesDir()
61 .Append(test::kReferenceFileName360p)
62 .AddExtension(test::kY4mFileExtension);
63 command_line->AppendSwitchPath(switches::kUseFileForFakeVideoCapture,
64 input_video);
65 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
67 // Flag used by TestWebAudioMediaStream to force garbage collection.
68 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
70 bool enable_audio_track_processing = GetParam();
71 if (enable_audio_track_processing)
72 command_line->AppendSwitch(switches::kEnableAudioTrackProcessing);
75 void PrintProcessMetrics(base::ProcessMetrics* process_metrics,
76 const std::string& suffix) {
77 perf_test::PrintResult("cpu", "", "cpu" + suffix,
78 process_metrics->GetCPUUsage(),
79 "%", true);
80 perf_test::PrintResult("memory", "", "ws_peak" + suffix,
81 process_metrics->GetPeakWorkingSetSize(),
82 "bytes", true);
83 perf_test::PrintResult("memory", "", "ws_final" + suffix,
84 process_metrics->GetWorkingSetSize(),
85 "bytes", false);
87 size_t private_mem;
88 size_t shared_mem;
89 if (process_metrics->GetMemoryBytes(&private_mem, &shared_mem)) {
90 perf_test::PrintResult("memory", "", "private_mem_final" + suffix,
91 private_mem, "bytes", false);
92 perf_test::PrintResult("memory", "", "shared_mem_final" + suffix,
93 shared_mem, "bytes", false);
97 // Tries to extract data from peerConnectionDataStore in the webrtc-internals
98 // tab. The caller owns the parsed data. Returns NULL on failure.
99 base::DictionaryValue* GetWebrtcInternalsData(
100 content::WebContents* webrtc_internals_tab) {
101 std::string all_stats_json = ExecuteJavascript(
102 "window.domAutomationController.send("
103 " JSON.stringify(peerConnectionDataStore));",
104 webrtc_internals_tab);
106 base::Value* parsed_json = base::JSONReader::Read(all_stats_json);
107 base::DictionaryValue* result;
108 if (parsed_json && parsed_json->GetAsDictionary(&result))
109 return result;
111 return NULL;
114 const base::DictionaryValue* GetDataOnFirstPeerConnection(
115 const base::DictionaryValue* all_data) {
116 base::DictionaryValue::Iterator iterator(*all_data);
118 const base::DictionaryValue* result;
119 if (!iterator.IsAtEnd() && iterator.value().GetAsDictionary(&result))
120 return result;
122 return NULL;
125 bool OnWinXp() {
126 #if defined(OS_WIN)
127 return base::win::GetVersion() <= base::win::VERSION_XP;
128 #else
129 return false;
130 #endif
133 test::PeerConnectionServerRunner peerconnection_server_;
136 static const bool kRunTestsWithFlag[] = { false, true };
137 INSTANTIATE_TEST_CASE_P(WebRtcBrowserTests,
138 WebRtcBrowserTest,
139 testing::ValuesIn(kRunTestsWithFlag));
141 IN_PROC_BROWSER_TEST_P(WebRtcBrowserTest,
142 MANUAL_RunsAudioVideoWebRTCCallInTwoTabs) {
143 if (OnWinXp()) return;
145 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
146 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
147 ASSERT_TRUE(peerconnection_server_.Start());
149 content::WebContents* left_tab =
150 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
151 content::WebContents* right_tab =
152 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
154 EstablishCall(left_tab, right_tab);
156 StartDetectingVideo(left_tab, "remote-view");
157 StartDetectingVideo(right_tab, "remote-view");
159 WaitForVideoToPlay(left_tab);
160 WaitForVideoToPlay(right_tab);
162 HangUp(left_tab);
163 WaitUntilHangupVerified(left_tab);
164 WaitUntilHangupVerified(right_tab);
166 ASSERT_TRUE(peerconnection_server_.Stop());
169 IN_PROC_BROWSER_TEST_P(WebRtcBrowserTest, MANUAL_CpuUsage15Seconds) {
170 if (OnWinXp()) return;
172 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
173 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
174 ASSERT_TRUE(peerconnection_server_.Start());
176 base::FilePath results_file;
177 ASSERT_TRUE(base::CreateTemporaryFile(&results_file));
179 content::WebContents* left_tab =
180 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
182 #if defined(OS_MACOSX)
183 // Don't measure renderer CPU on mac: requires a mach broker we don't have
184 // access to from the browser test.
185 scoped_ptr<base::ProcessMetrics> browser_process_metrics(
186 base::ProcessMetrics::CreateProcessMetrics(
187 base::Process::Current().handle(), NULL));
188 browser_process_metrics->GetCPUUsage();
189 #else
190 // Measure rendering CPU on platforms that support it.
191 base::ProcessHandle renderer_pid =
192 left_tab->GetRenderProcessHost()->GetHandle();
193 scoped_ptr<base::ProcessMetrics> renderer_process_metrics(
194 base::ProcessMetrics::CreateProcessMetrics(renderer_pid));
195 renderer_process_metrics->GetCPUUsage();
197 scoped_ptr<base::ProcessMetrics> browser_process_metrics(
198 base::ProcessMetrics::CreateProcessMetrics(
199 base::Process::Current().handle()));
200 browser_process_metrics->GetCPUUsage();
201 #endif
203 content::WebContents* right_tab =
204 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
206 EstablishCall(left_tab, right_tab);
208 test::SleepInJavascript(left_tab, 15000);
210 HangUp(left_tab);
211 WaitUntilHangupVerified(left_tab);
212 WaitUntilHangupVerified(right_tab);
214 #if !defined(OS_MACOSX)
215 PrintProcessMetrics(renderer_process_metrics.get(), "_r");
216 #endif
217 PrintProcessMetrics(browser_process_metrics.get(), "_b");
219 ASSERT_TRUE(peerconnection_server_.Stop());
222 // This is manual for its long execution time.
223 IN_PROC_BROWSER_TEST_P(WebRtcBrowserTest,
224 MANUAL_RunsAudioVideoCall60SecsAndLogsInternalMetrics) {
225 if (OnWinXp()) return;
227 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
228 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
229 ASSERT_TRUE(peerconnection_server_.Start());
231 ASSERT_GE(TestTimeouts::action_max_timeout().InSeconds(), 100) <<
232 "This is a long-running test; you must specify "
233 "--ui-test-action-max-timeout to have a value of at least 100000.";
235 content::WebContents* left_tab =
236 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
237 content::WebContents* right_tab =
238 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
240 EstablishCall(left_tab, right_tab);
242 StartDetectingVideo(left_tab, "remote-view");
243 StartDetectingVideo(right_tab, "remote-view");
245 WaitForVideoToPlay(left_tab);
246 WaitForVideoToPlay(right_tab);
248 // Let values stabilize, bandwidth ramp up, etc.
249 test::SleepInJavascript(left_tab, 60000);
251 // Start measurements.
252 chrome::AddTabAt(browser(), GURL(), -1, true);
253 ui_test_utils::NavigateToURL(browser(), GURL("chrome://webrtc-internals"));
254 content::WebContents* webrtc_internals_tab =
255 browser()->tab_strip_model()->GetActiveWebContents();
257 test::SleepInJavascript(left_tab, 10000);
259 scoped_ptr<base::DictionaryValue> all_data(
260 GetWebrtcInternalsData(webrtc_internals_tab));
261 ASSERT_TRUE(all_data.get() != NULL);
263 const base::DictionaryValue* first_pc_dict =
264 GetDataOnFirstPeerConnection(all_data.get());
265 ASSERT_TRUE(first_pc_dict != NULL);
266 test::PrintBweForVideoMetrics(*first_pc_dict);
267 test::PrintMetricsForAllStreams(*first_pc_dict);
269 HangUp(left_tab);
270 WaitUntilHangupVerified(left_tab);
271 WaitUntilHangupVerified(right_tab);
273 ASSERT_TRUE(peerconnection_server_.Stop());
276 IN_PROC_BROWSER_TEST_P(WebRtcBrowserTest, MANUAL_TestWebAudioMediaStream) {
277 if (OnWinXp()) return;
279 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
280 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
281 GURL url(embedded_test_server()->GetURL("/webrtc/webaudio_crash.html"));
282 ui_test_utils::NavigateToURL(browser(), url);
283 content::WebContents* tab =
284 browser()->tab_strip_model()->GetActiveWebContents();
286 // A sleep is necessary to be able to detect the crash.
287 test::SleepInJavascript(tab, 1000);
289 ASSERT_FALSE(tab->IsCrashed());