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/files/file_util.h"
6 #include "base/memory/ref_counted_memory.h"
7 #include "base/run_loop.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/tracing_controller.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/shell/browser/shell.h"
15 using base::trace_event::CategoryFilter
;
16 using base::trace_event::TraceOptions
;
17 using base::trace_event::RECORD_CONTINUOUSLY
;
18 using base::trace_event::RECORD_UNTIL_FULL
;
22 class TracingControllerTestEndpoint
23 : public TracingController::TraceDataEndpoint
{
25 TracingControllerTestEndpoint(
26 base::Callback
<void(base::RefCountedString
*)> done_callback
)
27 : done_callback_(done_callback
) {}
29 void ReceiveTraceChunk(const std::string
& chunk
) override
{
30 EXPECT_FALSE(chunk
.empty());
34 void ReceiveTraceFinalContents(const std::string
& contents
) override
{
35 EXPECT_EQ(trace_
, contents
);
37 std::string tmp
= contents
;
38 scoped_refptr
<base::RefCountedString
> chunk_ptr
=
39 base::RefCountedString::TakeString(&tmp
);
41 BrowserThread::PostTask(
42 BrowserThread::UI
, FROM_HERE
,
43 base::Bind(done_callback_
, chunk_ptr
));
47 ~TracingControllerTestEndpoint() override
{}
50 base::Callback
<void(base::RefCountedString
*)> done_callback_
;
53 class TracingControllerTest
: public ContentBrowserTest
{
55 TracingControllerTest() {}
57 void SetUp() override
{
58 get_categories_done_callback_count_
= 0;
59 enable_recording_done_callback_count_
= 0;
60 disable_recording_done_callback_count_
= 0;
61 enable_monitoring_done_callback_count_
= 0;
62 disable_monitoring_done_callback_count_
= 0;
63 capture_monitoring_snapshot_done_callback_count_
= 0;
64 ContentBrowserTest::SetUp();
67 void TearDown() override
{ ContentBrowserTest::TearDown(); }
69 void Navigate(Shell
* shell
) {
70 NavigateToURL(shell
, GetTestUrl("", "title.html"));
73 void GetCategoriesDoneCallbackTest(base::Closure quit_callback
,
74 const std::set
<std::string
>& categories
) {
75 get_categories_done_callback_count_
++;
76 EXPECT_TRUE(categories
.size() > 0);
80 void EnableRecordingDoneCallbackTest(base::Closure quit_callback
) {
81 enable_recording_done_callback_count_
++;
85 void DisableRecordingStringDoneCallbackTest(base::Closure quit_callback
,
86 base::RefCountedString
* data
) {
87 disable_recording_done_callback_count_
++;
88 EXPECT_TRUE(data
->size() > 0);
92 void DisableRecordingFileDoneCallbackTest(base::Closure quit_callback
,
93 const base::FilePath
& file_path
) {
94 disable_recording_done_callback_count_
++;
95 EXPECT_TRUE(PathExists(file_path
));
97 base::GetFileSize(file_path
, &file_size
);
98 EXPECT_TRUE(file_size
> 0);
100 last_actual_recording_file_path_
= file_path
;
103 void EnableMonitoringDoneCallbackTest(base::Closure quit_callback
) {
104 enable_monitoring_done_callback_count_
++;
108 void DisableMonitoringDoneCallbackTest(base::Closure quit_callback
) {
109 disable_monitoring_done_callback_count_
++;
113 void CaptureMonitoringSnapshotDoneCallbackTest(
114 base::Closure quit_callback
, const base::FilePath
& file_path
) {
115 capture_monitoring_snapshot_done_callback_count_
++;
116 EXPECT_TRUE(PathExists(file_path
));
118 base::GetFileSize(file_path
, &file_size
);
119 EXPECT_TRUE(file_size
> 0);
121 last_actual_monitoring_file_path_
= file_path
;
124 int get_categories_done_callback_count() const {
125 return get_categories_done_callback_count_
;
128 int enable_recording_done_callback_count() const {
129 return enable_recording_done_callback_count_
;
132 int disable_recording_done_callback_count() const {
133 return disable_recording_done_callback_count_
;
136 int enable_monitoring_done_callback_count() const {
137 return enable_monitoring_done_callback_count_
;
140 int disable_monitoring_done_callback_count() const {
141 return disable_monitoring_done_callback_count_
;
144 int capture_monitoring_snapshot_done_callback_count() const {
145 return capture_monitoring_snapshot_done_callback_count_
;
148 base::FilePath
last_actual_recording_file_path() const {
149 return last_actual_recording_file_path_
;
152 base::FilePath
last_actual_monitoring_file_path() const {
153 return last_actual_monitoring_file_path_
;
156 void TestEnableAndDisableRecordingString() {
159 TracingController
* controller
= TracingController::GetInstance();
162 base::RunLoop run_loop
;
163 TracingController::EnableRecordingDoneCallback callback
=
164 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest
,
165 base::Unretained(this),
166 run_loop
.QuitClosure());
167 bool result
= controller
->EnableRecording(
168 CategoryFilter(), TraceOptions(), callback
);
171 EXPECT_EQ(enable_recording_done_callback_count(), 1);
175 base::RunLoop run_loop
;
176 base::Callback
<void(base::RefCountedString
*)> callback
= base::Bind(
177 &TracingControllerTest::DisableRecordingStringDoneCallbackTest
,
178 base::Unretained(this),
179 run_loop
.QuitClosure());
180 bool result
= controller
->DisableRecording(
181 TracingController::CreateStringSink(callback
));
184 EXPECT_EQ(disable_recording_done_callback_count(), 1);
188 void TestEnableAndDisableRecordingCompressed() {
191 TracingController
* controller
= TracingController::GetInstance();
194 base::RunLoop run_loop
;
195 TracingController::EnableRecordingDoneCallback callback
=
196 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest
,
197 base::Unretained(this), run_loop
.QuitClosure());
198 bool result
= controller
->EnableRecording(CategoryFilter(),
199 TraceOptions(), callback
);
202 EXPECT_EQ(enable_recording_done_callback_count(), 1);
206 base::RunLoop run_loop
;
207 base::Callback
<void(base::RefCountedString
*)> callback
= base::Bind(
208 &TracingControllerTest::DisableRecordingStringDoneCallbackTest
,
209 base::Unretained(this), run_loop
.QuitClosure());
210 bool result
= controller
->DisableRecording(
211 TracingController::CreateCompressedStringSink(
212 new TracingControllerTestEndpoint(callback
)));
215 EXPECT_EQ(disable_recording_done_callback_count(), 1);
219 void TestEnableAndDisableRecordingFile(
220 const base::FilePath
& result_file_path
) {
223 TracingController
* controller
= TracingController::GetInstance();
226 base::RunLoop run_loop
;
227 TracingController::EnableRecordingDoneCallback callback
=
228 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest
,
229 base::Unretained(this),
230 run_loop
.QuitClosure());
231 bool result
= controller
->EnableRecording(
232 CategoryFilter(), TraceOptions(), callback
);
235 EXPECT_EQ(enable_recording_done_callback_count(), 1);
239 base::RunLoop run_loop
;
240 base::Closure callback
= base::Bind(
241 &TracingControllerTest::DisableRecordingFileDoneCallbackTest
,
242 base::Unretained(this),
243 run_loop
.QuitClosure(),
245 bool result
= controller
->DisableRecording(
246 TracingController::CreateFileSink(result_file_path
, callback
));
249 EXPECT_EQ(disable_recording_done_callback_count(), 1);
253 void TestEnableCaptureAndDisableMonitoring(
254 const base::FilePath
& result_file_path
) {
257 TracingController
* controller
= TracingController::GetInstance();
261 CategoryFilter
category_filter("");
262 TraceOptions options
;
263 controller
->GetMonitoringStatus(
264 &is_monitoring
, &category_filter
, &options
);
265 EXPECT_FALSE(is_monitoring
);
266 EXPECT_EQ("-*Debug,-*Test", category_filter
.ToString());
267 EXPECT_FALSE(options
.record_mode
== RECORD_CONTINUOUSLY
);
268 EXPECT_FALSE(options
.enable_sampling
);
269 EXPECT_FALSE(options
.enable_systrace
);
273 base::RunLoop run_loop
;
274 TracingController::EnableMonitoringDoneCallback callback
=
275 base::Bind(&TracingControllerTest::EnableMonitoringDoneCallbackTest
,
276 base::Unretained(this),
277 run_loop
.QuitClosure());
279 TraceOptions trace_options
;
280 trace_options
.enable_sampling
= true;
282 bool result
= controller
->EnableMonitoring(
288 EXPECT_EQ(enable_monitoring_done_callback_count(), 1);
293 CategoryFilter
category_filter("");
294 TraceOptions options
;
295 controller
->GetMonitoringStatus(
296 &is_monitoring
, &category_filter
, &options
);
297 EXPECT_TRUE(is_monitoring
);
298 EXPECT_EQ("*", category_filter
.ToString());
299 EXPECT_FALSE(options
.record_mode
== RECORD_CONTINUOUSLY
);
300 EXPECT_TRUE(options
.enable_sampling
);
301 EXPECT_FALSE(options
.enable_systrace
);
305 base::RunLoop run_loop
;
306 base::Closure callback
= base::Bind(
307 &TracingControllerTest::CaptureMonitoringSnapshotDoneCallbackTest
,
308 base::Unretained(this),
309 run_loop
.QuitClosure(),
311 ASSERT_TRUE(controller
->CaptureMonitoringSnapshot(
312 TracingController::CreateFileSink(result_file_path
, callback
)));
314 EXPECT_EQ(capture_monitoring_snapshot_done_callback_count(), 1);
318 base::RunLoop run_loop
;
319 TracingController::DisableMonitoringDoneCallback callback
=
320 base::Bind(&TracingControllerTest::DisableMonitoringDoneCallbackTest
,
321 base::Unretained(this),
322 run_loop
.QuitClosure());
323 bool result
= controller
->DisableMonitoring(callback
);
326 EXPECT_EQ(disable_monitoring_done_callback_count(), 1);
331 CategoryFilter
category_filter("");
332 TraceOptions options
;
333 controller
->GetMonitoringStatus(&is_monitoring
,
336 EXPECT_FALSE(is_monitoring
);
337 EXPECT_EQ("", category_filter
.ToString());
338 EXPECT_FALSE(options
.record_mode
== RECORD_CONTINUOUSLY
);
339 EXPECT_FALSE(options
.enable_sampling
);
340 EXPECT_FALSE(options
.enable_systrace
);
345 int get_categories_done_callback_count_
;
346 int enable_recording_done_callback_count_
;
347 int disable_recording_done_callback_count_
;
348 int enable_monitoring_done_callback_count_
;
349 int disable_monitoring_done_callback_count_
;
350 int capture_monitoring_snapshot_done_callback_count_
;
351 base::FilePath last_actual_recording_file_path_
;
352 base::FilePath last_actual_monitoring_file_path_
;
355 IN_PROC_BROWSER_TEST_F(TracingControllerTest
, GetCategories
) {
358 TracingController
* controller
= TracingController::GetInstance();
360 base::RunLoop run_loop
;
361 TracingController::GetCategoriesDoneCallback callback
=
362 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest
,
363 base::Unretained(this),
364 run_loop
.QuitClosure());
365 ASSERT_TRUE(controller
->GetCategories(callback
));
367 EXPECT_EQ(get_categories_done_callback_count(), 1);
370 IN_PROC_BROWSER_TEST_F(TracingControllerTest
, EnableAndDisableRecording
) {
371 TestEnableAndDisableRecordingString();
374 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
375 EnableAndDisableRecordingWithFilePath
) {
376 base::FilePath file_path
;
377 base::CreateTemporaryFile(&file_path
);
378 TestEnableAndDisableRecordingFile(file_path
);
379 EXPECT_EQ(file_path
.value(), last_actual_recording_file_path().value());
382 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
383 EnableAndDisableRecordingWithCompression
) {
384 TestEnableAndDisableRecordingCompressed();
387 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
388 EnableAndDisableRecordingWithEmptyFileAndNullCallback
) {
391 TracingController
* controller
= TracingController::GetInstance();
392 EXPECT_TRUE(controller
->EnableRecording(
395 TracingController::EnableRecordingDoneCallback()));
396 EXPECT_TRUE(controller
->DisableRecording(NULL
));
397 base::RunLoop().RunUntilIdle();
400 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
401 EnableCaptureAndDisableMonitoring
) {
402 base::FilePath file_path
;
403 base::CreateTemporaryFile(&file_path
);
404 TestEnableCaptureAndDisableMonitoring(file_path
);
407 IN_PROC_BROWSER_TEST_F(TracingControllerTest
,
408 EnableCaptureAndDisableMonitoringWithFilePath
) {
409 base::FilePath file_path
;
410 base::CreateTemporaryFile(&file_path
);
411 TestEnableCaptureAndDisableMonitoring(file_path
);
412 EXPECT_EQ(file_path
.value(), last_actual_monitoring_file_path().value());
415 // See http://crbug.com/392446
416 #if defined(OS_ANDROID)
417 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
418 DISABLED_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
420 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
421 EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
423 IN_PROC_BROWSER_TEST_F(
424 TracingControllerTest
,
425 MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
) {
428 TracingController
* controller
= TracingController::GetInstance();
429 TraceOptions trace_options
;
430 trace_options
.enable_sampling
= true;
431 EXPECT_TRUE(controller
->EnableMonitoring(
434 TracingController::EnableMonitoringDoneCallback()));
435 controller
->CaptureMonitoringSnapshot(NULL
);
436 base::RunLoop().RunUntilIdle();
437 EXPECT_TRUE(controller
->DisableMonitoring(
438 TracingController::DisableMonitoringDoneCallback()));
439 base::RunLoop().RunUntilIdle();
442 } // namespace content