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/file_util.h"
6 #include "base/run_loop.h"
7 #include "content/browser/tracing/tracing_controller_impl.h"
8 #include "content/public/test/browser_test_utils.h"
9 #include "content/public/test/content_browser_test.h"
10 #include "content/public/test/content_browser_test_utils.h"
11 #include "content/shell/browser/shell.h"
13 using base::debug::CategoryFilter
;
14 using base::debug::TraceOptions
;
15 using base::debug::RECORD_CONTINUOUSLY
;
16 using base::debug::RECORD_UNTIL_FULL
;
20 class TracingControllerTest
: public ContentBrowserTest
{
22 TracingControllerTest() {}
24 virtual void SetUp() OVERRIDE
{
25 get_categories_done_callback_count_
= 0;
26 enable_recording_done_callback_count_
= 0;
27 disable_recording_done_callback_count_
= 0;
28 enable_monitoring_done_callback_count_
= 0;
29 disable_monitoring_done_callback_count_
= 0;
30 capture_monitoring_snapshot_done_callback_count_
= 0;
31 ContentBrowserTest::SetUp();
34 virtual void TearDown() OVERRIDE
{
35 ContentBrowserTest::TearDown();
38 void Navigate(Shell
* shell
) {
39 NavigateToURL(shell
, GetTestUrl("", "title.html"));
42 void GetCategoriesDoneCallbackTest(base::Closure quit_callback
,
43 const std::set
<std::string
>& categories
) {
44 get_categories_done_callback_count_
++;
45 EXPECT_TRUE(categories
.size() > 0);
49 void EnableRecordingDoneCallbackTest(base::Closure quit_callback
) {
50 enable_recording_done_callback_count_
++;
54 void DisableRecordingDoneCallbackTest(base::Closure quit_callback
,
55 const base::FilePath
& file_path
) {
56 disable_recording_done_callback_count_
++;
57 EXPECT_TRUE(PathExists(file_path
));
59 base::GetFileSize(file_path
, &file_size
);
60 EXPECT_TRUE(file_size
> 0);
62 last_actual_recording_file_path_
= file_path
;
65 void EnableMonitoringDoneCallbackTest(base::Closure quit_callback
) {
66 enable_monitoring_done_callback_count_
++;
70 void DisableMonitoringDoneCallbackTest(base::Closure quit_callback
) {
71 disable_monitoring_done_callback_count_
++;
75 void CaptureMonitoringSnapshotDoneCallbackTest(
76 base::Closure quit_callback
, const base::FilePath
& file_path
) {
77 capture_monitoring_snapshot_done_callback_count_
++;
78 EXPECT_TRUE(PathExists(file_path
));
80 base::GetFileSize(file_path
, &file_size
);
81 EXPECT_TRUE(file_size
> 0);
83 last_actual_monitoring_file_path_
= file_path
;
86 int get_categories_done_callback_count() const {
87 return get_categories_done_callback_count_
;
90 int enable_recording_done_callback_count() const {
91 return enable_recording_done_callback_count_
;
94 int disable_recording_done_callback_count() const {
95 return disable_recording_done_callback_count_
;
98 int enable_monitoring_done_callback_count() const {
99 return enable_monitoring_done_callback_count_
;
102 int disable_monitoring_done_callback_count() const {
103 return disable_monitoring_done_callback_count_
;
106 int capture_monitoring_snapshot_done_callback_count() const {
107 return capture_monitoring_snapshot_done_callback_count_
;
110 base::FilePath
last_actual_recording_file_path() const {
111 return last_actual_recording_file_path_
;
114 base::FilePath
last_actual_monitoring_file_path() const {
115 return last_actual_monitoring_file_path_
;
118 void TestEnableAndDisableRecording(const base::FilePath
& result_file_path
) {
121 TracingController
* controller
= TracingController::GetInstance();
124 base::RunLoop run_loop
;
125 TracingController::EnableRecordingDoneCallback callback
=
126 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest
,
127 base::Unretained(this),
128 run_loop
.QuitClosure());
129 bool result
= controller
->EnableRecording(
130 CategoryFilter(), TraceOptions(), callback
);
133 EXPECT_EQ(enable_recording_done_callback_count(), 1);
137 base::RunLoop run_loop
;
138 TracingController::TracingFileResultCallback callback
=
139 base::Bind(&TracingControllerTest::DisableRecordingDoneCallbackTest
,
140 base::Unretained(this),
141 run_loop
.QuitClosure());
142 bool result
= controller
->DisableRecording(result_file_path
, callback
);
145 EXPECT_EQ(disable_recording_done_callback_count(), 1);
149 void TestEnableCaptureAndDisableMonitoring(
150 const base::FilePath
& result_file_path
) {
153 TracingController
* controller
= TracingController::GetInstance();
157 CategoryFilter
category_filter("");
158 TraceOptions options
;
159 controller
->GetMonitoringStatus(
160 &is_monitoring
, &category_filter
, &options
);
161 EXPECT_FALSE(is_monitoring
);
162 EXPECT_EQ("-*Debug,-*Test", category_filter
.ToString());
163 EXPECT_FALSE(options
.record_mode
== RECORD_CONTINUOUSLY
);
164 EXPECT_FALSE(options
.enable_sampling
);
165 EXPECT_FALSE(options
.enable_systrace
);
169 base::RunLoop run_loop
;
170 TracingController::EnableMonitoringDoneCallback callback
=
171 base::Bind(&TracingControllerTest::EnableMonitoringDoneCallbackTest
,
172 base::Unretained(this),
173 run_loop
.QuitClosure());
175 TraceOptions trace_options
;
176 trace_options
.enable_sampling
= true;
178 bool result
= controller
->EnableMonitoring(
184 EXPECT_EQ(enable_monitoring_done_callback_count(), 1);
189 CategoryFilter
category_filter("");
190 TraceOptions options
;
191 controller
->GetMonitoringStatus(
192 &is_monitoring
, &category_filter
, &options
);
193 EXPECT_TRUE(is_monitoring
);
194 EXPECT_EQ("*", category_filter
.ToString());
195 EXPECT_FALSE(options
.record_mode
== RECORD_CONTINUOUSLY
);
196 EXPECT_TRUE(options
.enable_sampling
);
197 EXPECT_FALSE(options
.enable_systrace
);
201 base::RunLoop run_loop
;
202 TracingController::TracingFileResultCallback callback
=
203 base::Bind(&TracingControllerTest::
204 CaptureMonitoringSnapshotDoneCallbackTest
,
205 base::Unretained(this),
206 run_loop
.QuitClosure());
207 ASSERT_TRUE(controller
->CaptureMonitoringSnapshot(result_file_path
,
210 EXPECT_EQ(capture_monitoring_snapshot_done_callback_count(), 1);
214 base::RunLoop run_loop
;
215 TracingController::DisableMonitoringDoneCallback callback
=
216 base::Bind(&TracingControllerTest::DisableMonitoringDoneCallbackTest
,
217 base::Unretained(this),
218 run_loop
.QuitClosure());
219 bool result
= controller
->DisableMonitoring(callback
);
222 EXPECT_EQ(disable_monitoring_done_callback_count(), 1);
227 CategoryFilter
category_filter("");
228 TraceOptions options
;
229 controller
->GetMonitoringStatus(&is_monitoring
,
232 EXPECT_FALSE(is_monitoring
);
233 EXPECT_EQ("", category_filter
.ToString());
234 EXPECT_FALSE(options
.record_mode
== RECORD_CONTINUOUSLY
);
235 EXPECT_FALSE(options
.enable_sampling
);
236 EXPECT_FALSE(options
.enable_systrace
);
241 int get_categories_done_callback_count_
;
242 int enable_recording_done_callback_count_
;
243 int disable_recording_done_callback_count_
;
244 int enable_monitoring_done_callback_count_
;
245 int disable_monitoring_done_callback_count_
;
246 int capture_monitoring_snapshot_done_callback_count_
;
247 base::FilePath last_actual_recording_file_path_
;
248 base::FilePath last_actual_monitoring_file_path_
;
251 IN_PROC_BROWSER_TEST_F(TracingControllerTest
, GetCategories
) {
254 TracingController
* controller
= TracingController::GetInstance();
256 base::RunLoop run_loop
;
257 TracingController::GetCategoriesDoneCallback callback
=
258 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest
,
259 base::Unretained(this),
260 run_loop
.QuitClosure());
261 ASSERT_TRUE(controller
->GetCategories(callback
));
263 EXPECT_EQ(get_categories_done_callback_count(), 1);
266 IN_PROC_BROWSER_TEST_F(TracingControllerTest
, EnableAndDisableRecording
) {
267 TestEnableAndDisableRecording(base::FilePath());
270 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
271 EnableAndDisableRecordingWithFilePath
) {
272 base::FilePath file_path
;
273 base::CreateTemporaryFile(&file_path
);
274 TestEnableAndDisableRecording(file_path
);
275 EXPECT_EQ(file_path
.value(), last_actual_recording_file_path().value());
278 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
279 EnableAndDisableRecordingWithEmptyFileAndNullCallback
) {
282 TracingController
* controller
= TracingController::GetInstance();
283 EXPECT_TRUE(controller
->EnableRecording(
286 TracingController::EnableRecordingDoneCallback()));
287 EXPECT_TRUE(controller
->DisableRecording(
288 base::FilePath(), TracingController::TracingFileResultCallback()));
289 base::RunLoop().RunUntilIdle();
292 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
293 EnableCaptureAndDisableMonitoring
) {
294 TestEnableCaptureAndDisableMonitoring(base::FilePath());
297 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
298 EnableCaptureAndDisableMonitoringWithFilePath
) {
299 base::FilePath file_path
;
300 base::CreateTemporaryFile(&file_path
);
301 TestEnableCaptureAndDisableMonitoring(file_path
);
302 EXPECT_EQ(file_path
.value(), last_actual_monitoring_file_path().value());
305 // See http://crbug.com/392446
306 #if defined(OS_ANDROID)
307 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
308 DISABLED_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
310 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
311 EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
313 IN_PROC_BROWSER_TEST_F(
314 TracingControllerTest
,
315 MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
) {
318 TracingController
* controller
= TracingController::GetInstance();
319 TraceOptions trace_options
;
320 trace_options
.enable_sampling
= true;
321 EXPECT_TRUE(controller
->EnableMonitoring(
324 TracingController::EnableMonitoringDoneCallback()));
325 controller
->CaptureMonitoringSnapshot(
326 base::FilePath(), TracingController::TracingFileResultCallback());
327 base::RunLoop().RunUntilIdle();
328 EXPECT_TRUE(controller
->DisableMonitoring(
329 TracingController::DisableMonitoringDoneCallback()));
330 base::RunLoop().RunUntilIdle();
333 } // namespace content