[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / tracing / tracing_controller_browsertest.cc
blobca94e40f0c8a1aeb056773ded65bca327b6aae0d
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::RECORD_CONTINUOUSLY;
16 using base::trace_event::RECORD_UNTIL_FULL;
17 using base::trace_event::TraceConfig;
19 namespace content {
21 class TracingControllerTestEndpoint
22 : public TracingController::TraceDataEndpoint {
23 public:
24 TracingControllerTestEndpoint(
25 base::Callback<void(base::RefCountedString*)> done_callback)
26 : done_callback_(done_callback) {}
28 void ReceiveTraceChunk(const std::string& chunk) override {
29 EXPECT_FALSE(chunk.empty());
30 trace_ += chunk;
33 void ReceiveTraceFinalContents(const std::string& contents) override {
34 EXPECT_EQ(trace_, contents);
36 std::string tmp = contents;
37 scoped_refptr<base::RefCountedString> chunk_ptr =
38 base::RefCountedString::TakeString(&tmp);
40 BrowserThread::PostTask(
41 BrowserThread::UI, FROM_HERE,
42 base::Bind(done_callback_, chunk_ptr));
45 protected:
46 ~TracingControllerTestEndpoint() override {}
48 std::string trace_;
49 base::Callback<void(base::RefCountedString*)> done_callback_;
52 class TracingControllerTest : public ContentBrowserTest {
53 public:
54 TracingControllerTest() {}
56 void SetUp() override {
57 get_categories_done_callback_count_ = 0;
58 enable_recording_done_callback_count_ = 0;
59 disable_recording_done_callback_count_ = 0;
60 enable_monitoring_done_callback_count_ = 0;
61 disable_monitoring_done_callback_count_ = 0;
62 capture_monitoring_snapshot_done_callback_count_ = 0;
63 ContentBrowserTest::SetUp();
66 void TearDown() override { ContentBrowserTest::TearDown(); }
68 void Navigate(Shell* shell) {
69 NavigateToURL(shell, GetTestUrl("", "title.html"));
72 void GetCategoriesDoneCallbackTest(base::Closure quit_callback,
73 const std::set<std::string>& categories) {
74 get_categories_done_callback_count_++;
75 EXPECT_TRUE(categories.size() > 0);
76 quit_callback.Run();
79 void EnableRecordingDoneCallbackTest(base::Closure quit_callback) {
80 enable_recording_done_callback_count_++;
81 quit_callback.Run();
84 void DisableRecordingStringDoneCallbackTest(base::Closure quit_callback,
85 base::RefCountedString* data) {
86 disable_recording_done_callback_count_++;
87 EXPECT_TRUE(data->size() > 0);
88 quit_callback.Run();
91 void DisableRecordingFileDoneCallbackTest(base::Closure quit_callback,
92 const base::FilePath& file_path) {
93 disable_recording_done_callback_count_++;
94 EXPECT_TRUE(PathExists(file_path));
95 int64 file_size;
96 base::GetFileSize(file_path, &file_size);
97 EXPECT_TRUE(file_size > 0);
98 quit_callback.Run();
99 last_actual_recording_file_path_ = file_path;
102 void EnableMonitoringDoneCallbackTest(base::Closure quit_callback) {
103 enable_monitoring_done_callback_count_++;
104 quit_callback.Run();
107 void DisableMonitoringDoneCallbackTest(base::Closure quit_callback) {
108 disable_monitoring_done_callback_count_++;
109 quit_callback.Run();
112 void CaptureMonitoringSnapshotDoneCallbackTest(
113 base::Closure quit_callback, const base::FilePath& file_path) {
114 capture_monitoring_snapshot_done_callback_count_++;
115 EXPECT_TRUE(PathExists(file_path));
116 int64 file_size;
117 base::GetFileSize(file_path, &file_size);
118 EXPECT_TRUE(file_size > 0);
119 quit_callback.Run();
120 last_actual_monitoring_file_path_ = file_path;
123 int get_categories_done_callback_count() const {
124 return get_categories_done_callback_count_;
127 int enable_recording_done_callback_count() const {
128 return enable_recording_done_callback_count_;
131 int disable_recording_done_callback_count() const {
132 return disable_recording_done_callback_count_;
135 int enable_monitoring_done_callback_count() const {
136 return enable_monitoring_done_callback_count_;
139 int disable_monitoring_done_callback_count() const {
140 return disable_monitoring_done_callback_count_;
143 int capture_monitoring_snapshot_done_callback_count() const {
144 return capture_monitoring_snapshot_done_callback_count_;
147 base::FilePath last_actual_recording_file_path() const {
148 return last_actual_recording_file_path_;
151 base::FilePath last_actual_monitoring_file_path() const {
152 return last_actual_monitoring_file_path_;
155 void TestEnableAndDisableRecordingString() {
156 Navigate(shell());
158 TracingController* controller = TracingController::GetInstance();
161 base::RunLoop run_loop;
162 TracingController::EnableRecordingDoneCallback callback =
163 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest,
164 base::Unretained(this),
165 run_loop.QuitClosure());
166 bool result = controller->EnableRecording(
167 TraceConfig(), callback);
168 ASSERT_TRUE(result);
169 run_loop.Run();
170 EXPECT_EQ(enable_recording_done_callback_count(), 1);
174 base::RunLoop run_loop;
175 base::Callback<void(base::RefCountedString*)> callback = base::Bind(
176 &TracingControllerTest::DisableRecordingStringDoneCallbackTest,
177 base::Unretained(this),
178 run_loop.QuitClosure());
179 bool result = controller->DisableRecording(
180 TracingController::CreateStringSink(callback));
181 ASSERT_TRUE(result);
182 run_loop.Run();
183 EXPECT_EQ(disable_recording_done_callback_count(), 1);
187 void TestEnableAndDisableRecordingCompressed() {
188 Navigate(shell());
190 TracingController* controller = TracingController::GetInstance();
193 base::RunLoop run_loop;
194 TracingController::EnableRecordingDoneCallback callback =
195 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest,
196 base::Unretained(this), run_loop.QuitClosure());
197 bool result = controller->EnableRecording(TraceConfig(), callback);
198 ASSERT_TRUE(result);
199 run_loop.Run();
200 EXPECT_EQ(enable_recording_done_callback_count(), 1);
204 base::RunLoop run_loop;
205 base::Callback<void(base::RefCountedString*)> callback = base::Bind(
206 &TracingControllerTest::DisableRecordingStringDoneCallbackTest,
207 base::Unretained(this), run_loop.QuitClosure());
208 bool result = controller->DisableRecording(
209 TracingController::CreateCompressedStringSink(
210 new TracingControllerTestEndpoint(callback)));
211 ASSERT_TRUE(result);
212 run_loop.Run();
213 EXPECT_EQ(disable_recording_done_callback_count(), 1);
217 void TestEnableAndDisableRecordingCompressedFile(
218 const base::FilePath& result_file_path) {
219 Navigate(shell());
221 TracingController* controller = TracingController::GetInstance();
224 base::RunLoop run_loop;
225 TracingController::EnableRecordingDoneCallback callback =
226 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest,
227 base::Unretained(this), run_loop.QuitClosure());
228 bool result = controller->EnableRecording(TraceConfig(), callback);
229 ASSERT_TRUE(result);
230 run_loop.Run();
231 EXPECT_EQ(enable_recording_done_callback_count(), 1);
235 base::RunLoop run_loop;
236 base::Closure callback = base::Bind(
237 &TracingControllerTest::DisableRecordingFileDoneCallbackTest,
238 base::Unretained(this), run_loop.QuitClosure(), result_file_path);
239 bool result = controller->DisableRecording(
240 TracingController::CreateCompressedStringSink(
241 TracingController::CreateFileEndpoint(result_file_path,
242 callback)));
243 ASSERT_TRUE(result);
244 run_loop.Run();
245 EXPECT_EQ(disable_recording_done_callback_count(), 1);
249 void TestEnableAndDisableRecordingFile(
250 const base::FilePath& result_file_path) {
251 Navigate(shell());
253 TracingController* controller = TracingController::GetInstance();
256 base::RunLoop run_loop;
257 TracingController::EnableRecordingDoneCallback callback =
258 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest,
259 base::Unretained(this),
260 run_loop.QuitClosure());
261 bool result = controller->EnableRecording(TraceConfig(), callback);
262 ASSERT_TRUE(result);
263 run_loop.Run();
264 EXPECT_EQ(enable_recording_done_callback_count(), 1);
268 base::RunLoop run_loop;
269 base::Closure callback = base::Bind(
270 &TracingControllerTest::DisableRecordingFileDoneCallbackTest,
271 base::Unretained(this),
272 run_loop.QuitClosure(),
273 result_file_path);
274 bool result = controller->DisableRecording(
275 TracingController::CreateFileSink(result_file_path, callback));
276 ASSERT_TRUE(result);
277 run_loop.Run();
278 EXPECT_EQ(disable_recording_done_callback_count(), 1);
282 void TestEnableCaptureAndDisableMonitoring(
283 const base::FilePath& result_file_path) {
284 Navigate(shell());
286 TracingController* controller = TracingController::GetInstance();
289 bool is_monitoring;
290 TraceConfig trace_config("", "");
291 controller->GetMonitoringStatus(
292 &is_monitoring, &trace_config);
293 EXPECT_FALSE(is_monitoring);
294 EXPECT_EQ("-*Debug,-*Test", trace_config.ToCategoryFilterString());
295 EXPECT_FALSE(trace_config.GetTraceRecordMode() == RECORD_CONTINUOUSLY);
296 EXPECT_FALSE(trace_config.IsSamplingEnabled());
297 EXPECT_FALSE(trace_config.IsSystraceEnabled());
301 base::RunLoop run_loop;
302 TracingController::EnableMonitoringDoneCallback callback =
303 base::Bind(&TracingControllerTest::EnableMonitoringDoneCallbackTest,
304 base::Unretained(this),
305 run_loop.QuitClosure());
307 TraceConfig trace_config("*", "");
308 trace_config.EnableSampling();
309 bool result = controller->EnableMonitoring(trace_config, callback);
310 ASSERT_TRUE(result);
311 run_loop.Run();
312 EXPECT_EQ(enable_monitoring_done_callback_count(), 1);
316 bool is_monitoring;
317 TraceConfig trace_config("", "");
318 controller->GetMonitoringStatus(&is_monitoring, &trace_config);
319 EXPECT_TRUE(is_monitoring);
320 EXPECT_EQ("*", trace_config.ToCategoryFilterString());
321 EXPECT_FALSE(trace_config.GetTraceRecordMode() == RECORD_CONTINUOUSLY);
322 EXPECT_TRUE(trace_config.IsSamplingEnabled());
323 EXPECT_FALSE(trace_config.IsSystraceEnabled());
327 base::RunLoop run_loop;
328 base::Closure callback = base::Bind(
329 &TracingControllerTest::CaptureMonitoringSnapshotDoneCallbackTest,
330 base::Unretained(this),
331 run_loop.QuitClosure(),
332 result_file_path);
333 ASSERT_TRUE(controller->CaptureMonitoringSnapshot(
334 TracingController::CreateFileSink(result_file_path, callback)));
335 run_loop.Run();
336 EXPECT_EQ(capture_monitoring_snapshot_done_callback_count(), 1);
340 base::RunLoop run_loop;
341 TracingController::DisableMonitoringDoneCallback callback =
342 base::Bind(&TracingControllerTest::DisableMonitoringDoneCallbackTest,
343 base::Unretained(this),
344 run_loop.QuitClosure());
345 bool result = controller->DisableMonitoring(callback);
346 ASSERT_TRUE(result);
347 run_loop.Run();
348 EXPECT_EQ(disable_monitoring_done_callback_count(), 1);
352 bool is_monitoring;
353 TraceConfig trace_config("", "");
354 controller->GetMonitoringStatus(&is_monitoring, &trace_config);
355 EXPECT_FALSE(is_monitoring);
356 EXPECT_EQ("", trace_config.ToCategoryFilterString());
357 EXPECT_FALSE(trace_config.GetTraceRecordMode() == RECORD_CONTINUOUSLY);
358 EXPECT_FALSE(trace_config.IsSamplingEnabled());
359 EXPECT_FALSE(trace_config.IsSystraceEnabled());
363 private:
364 int get_categories_done_callback_count_;
365 int enable_recording_done_callback_count_;
366 int disable_recording_done_callback_count_;
367 int enable_monitoring_done_callback_count_;
368 int disable_monitoring_done_callback_count_;
369 int capture_monitoring_snapshot_done_callback_count_;
370 base::FilePath last_actual_recording_file_path_;
371 base::FilePath last_actual_monitoring_file_path_;
374 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) {
375 Navigate(shell());
377 TracingController* controller = TracingController::GetInstance();
379 base::RunLoop run_loop;
380 TracingController::GetCategoriesDoneCallback callback =
381 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest,
382 base::Unretained(this),
383 run_loop.QuitClosure());
384 ASSERT_TRUE(controller->GetCategories(callback));
385 run_loop.Run();
386 EXPECT_EQ(get_categories_done_callback_count(), 1);
389 IN_PROC_BROWSER_TEST_F(TracingControllerTest, EnableAndDisableRecording) {
390 TestEnableAndDisableRecordingString();
393 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
394 EnableAndDisableRecordingWithFilePath) {
395 base::FilePath file_path;
396 base::CreateTemporaryFile(&file_path);
397 TestEnableAndDisableRecordingFile(file_path);
398 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value());
401 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
402 EnableAndDisableRecordingWithCompression) {
403 TestEnableAndDisableRecordingCompressed();
406 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
407 EnableAndDisableRecordingToFileWithCompression) {
408 base::FilePath file_path;
409 base::CreateTemporaryFile(&file_path);
410 TestEnableAndDisableRecordingCompressedFile(file_path);
411 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value());
414 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
415 EnableAndDisableRecordingWithEmptyFileAndNullCallback) {
416 Navigate(shell());
418 TracingController* controller = TracingController::GetInstance();
419 EXPECT_TRUE(controller->EnableRecording(
420 TraceConfig(),
421 TracingController::EnableRecordingDoneCallback()));
422 EXPECT_TRUE(controller->DisableRecording(NULL));
423 base::RunLoop().RunUntilIdle();
426 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
427 EnableCaptureAndDisableMonitoring) {
428 base::FilePath file_path;
429 base::CreateTemporaryFile(&file_path);
430 TestEnableCaptureAndDisableMonitoring(file_path);
433 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
434 EnableCaptureAndDisableMonitoringWithFilePath) {
435 base::FilePath file_path;
436 base::CreateTemporaryFile(&file_path);
437 TestEnableCaptureAndDisableMonitoring(file_path);
438 EXPECT_EQ(file_path.value(), last_actual_monitoring_file_path().value());
441 // See http://crbug.com/392446
442 #if defined(OS_ANDROID)
443 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
444 DISABLED_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
445 #else
446 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
447 EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
448 #endif
449 IN_PROC_BROWSER_TEST_F(
450 TracingControllerTest,
451 MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback) {
452 Navigate(shell());
454 TracingController* controller = TracingController::GetInstance();
455 TraceConfig trace_config("*", "");
456 trace_config.EnableSampling();
457 EXPECT_TRUE(controller->EnableMonitoring(
458 trace_config,
459 TracingController::EnableMonitoringDoneCallback()));
460 controller->CaptureMonitoringSnapshot(NULL);
461 base::RunLoop().RunUntilIdle();
462 EXPECT_TRUE(controller->DisableMonitoring(
463 TracingController::DisableMonitoringDoneCallback()));
464 base::RunLoop().RunUntilIdle();
467 } // namespace content