Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / tracing / tracing_controller_browsertest.cc
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.
4
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/tracing_controller.h"
9 #include "content/public/test/browser_test_utils.h"
10 #include "content/public/test/content_browser_test.h"
11 #include "content/public/test/content_browser_test_utils.h"
12 #include "content/shell/browser/shell.h"
13
14 using base::debug::CategoryFilter;
15 using base::debug::TraceOptions;
16 using base::debug::RECORD_CONTINUOUSLY;
17 using base::debug::RECORD_UNTIL_FULL;
18
19 namespace content {
20
21 class TracingControllerTest : public ContentBrowserTest {
22  public:
23   TracingControllerTest() {}
24
25   void SetUp() override {
26     get_categories_done_callback_count_ = 0;
27     enable_recording_done_callback_count_ = 0;
28     disable_recording_done_callback_count_ = 0;
29     enable_monitoring_done_callback_count_ = 0;
30     disable_monitoring_done_callback_count_ = 0;
31     capture_monitoring_snapshot_done_callback_count_ = 0;
32     ContentBrowserTest::SetUp();
33   }
34
35   void TearDown() override { ContentBrowserTest::TearDown(); }
36
37   void Navigate(Shell* shell) {
38     NavigateToURL(shell, GetTestUrl("", "title.html"));
39   }
40
41   void GetCategoriesDoneCallbackTest(base::Closure quit_callback,
42                                      const std::set<std::string>& categories) {
43     get_categories_done_callback_count_++;
44     EXPECT_TRUE(categories.size() > 0);
45     quit_callback.Run();
46   }
47
48   void EnableRecordingDoneCallbackTest(base::Closure quit_callback) {
49     enable_recording_done_callback_count_++;
50     quit_callback.Run();
51   }
52
53   void DisableRecordingStringDoneCallbackTest(base::Closure quit_callback,
54                                               base::RefCountedString* data) {
55     disable_recording_done_callback_count_++;
56     EXPECT_TRUE(data->size() > 0);
57     quit_callback.Run();
58   }
59
60   void DisableRecordingFileDoneCallbackTest(base::Closure quit_callback,
61                                             const base::FilePath& file_path) {
62     disable_recording_done_callback_count_++;
63     EXPECT_TRUE(PathExists(file_path));
64     int64 file_size;
65     base::GetFileSize(file_path, &file_size);
66     EXPECT_TRUE(file_size > 0);
67     quit_callback.Run();
68     last_actual_recording_file_path_ = file_path;
69   }
70
71   void EnableMonitoringDoneCallbackTest(base::Closure quit_callback) {
72     enable_monitoring_done_callback_count_++;
73     quit_callback.Run();
74   }
75
76   void DisableMonitoringDoneCallbackTest(base::Closure quit_callback) {
77     disable_monitoring_done_callback_count_++;
78     quit_callback.Run();
79   }
80
81   void CaptureMonitoringSnapshotDoneCallbackTest(
82       base::Closure quit_callback, const base::FilePath& file_path) {
83     capture_monitoring_snapshot_done_callback_count_++;
84     EXPECT_TRUE(PathExists(file_path));
85     int64 file_size;
86     base::GetFileSize(file_path, &file_size);
87     EXPECT_TRUE(file_size > 0);
88     quit_callback.Run();
89     last_actual_monitoring_file_path_ = file_path;
90   }
91
92   int get_categories_done_callback_count() const {
93     return get_categories_done_callback_count_;
94   }
95
96   int enable_recording_done_callback_count() const {
97     return enable_recording_done_callback_count_;
98   }
99
100   int disable_recording_done_callback_count() const {
101     return disable_recording_done_callback_count_;
102   }
103
104   int enable_monitoring_done_callback_count() const {
105     return enable_monitoring_done_callback_count_;
106   }
107
108   int disable_monitoring_done_callback_count() const {
109     return disable_monitoring_done_callback_count_;
110   }
111
112   int capture_monitoring_snapshot_done_callback_count() const {
113     return capture_monitoring_snapshot_done_callback_count_;
114   }
115
116   base::FilePath last_actual_recording_file_path() const {
117     return last_actual_recording_file_path_;
118   }
119
120   base::FilePath last_actual_monitoring_file_path() const {
121     return last_actual_monitoring_file_path_;
122   }
123
124   void TestEnableAndDisableRecordingString() {
125     Navigate(shell());
126
127     TracingController* controller = TracingController::GetInstance();
128
129     {
130       base::RunLoop run_loop;
131       TracingController::EnableRecordingDoneCallback callback =
132           base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest,
133                      base::Unretained(this),
134                      run_loop.QuitClosure());
135       bool result = controller->EnableRecording(
136           CategoryFilter(), TraceOptions(), callback);
137       ASSERT_TRUE(result);
138       run_loop.Run();
139       EXPECT_EQ(enable_recording_done_callback_count(), 1);
140     }
141
142     {
143       base::RunLoop run_loop;
144       base::Callback<void(base::RefCountedString*)> callback = base::Bind(
145           &TracingControllerTest::DisableRecordingStringDoneCallbackTest,
146           base::Unretained(this),
147           run_loop.QuitClosure());
148       bool result = controller->DisableRecording(
149           TracingController::CreateStringSink(callback));
150       ASSERT_TRUE(result);
151       run_loop.Run();
152       EXPECT_EQ(disable_recording_done_callback_count(), 1);
153     }
154   }
155
156   void TestEnableAndDisableRecordingFile(
157       const base::FilePath& result_file_path) {
158     Navigate(shell());
159
160     TracingController* controller = TracingController::GetInstance();
161
162     {
163       base::RunLoop run_loop;
164       TracingController::EnableRecordingDoneCallback callback =
165           base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest,
166                      base::Unretained(this),
167                      run_loop.QuitClosure());
168       bool result = controller->EnableRecording(
169           CategoryFilter(), TraceOptions(), callback);
170       ASSERT_TRUE(result);
171       run_loop.Run();
172       EXPECT_EQ(enable_recording_done_callback_count(), 1);
173     }
174
175     {
176       base::RunLoop run_loop;
177       base::Closure callback = base::Bind(
178           &TracingControllerTest::DisableRecordingFileDoneCallbackTest,
179           base::Unretained(this),
180           run_loop.QuitClosure(),
181           result_file_path);
182       bool result = controller->DisableRecording(
183           TracingController::CreateFileSink(result_file_path, callback));
184       ASSERT_TRUE(result);
185       run_loop.Run();
186       EXPECT_EQ(disable_recording_done_callback_count(), 1);
187     }
188   }
189
190   void TestEnableCaptureAndDisableMonitoring(
191       const base::FilePath& result_file_path) {
192     Navigate(shell());
193
194     TracingController* controller = TracingController::GetInstance();
195
196     {
197       bool is_monitoring;
198       CategoryFilter category_filter("");
199       TraceOptions options;
200       controller->GetMonitoringStatus(
201           &is_monitoring, &category_filter, &options);
202       EXPECT_FALSE(is_monitoring);
203       EXPECT_EQ("-*Debug,-*Test", category_filter.ToString());
204       EXPECT_FALSE(options.record_mode == RECORD_CONTINUOUSLY);
205       EXPECT_FALSE(options.enable_sampling);
206       EXPECT_FALSE(options.enable_systrace);
207     }
208
209     {
210       base::RunLoop run_loop;
211       TracingController::EnableMonitoringDoneCallback callback =
212           base::Bind(&TracingControllerTest::EnableMonitoringDoneCallbackTest,
213                      base::Unretained(this),
214                      run_loop.QuitClosure());
215
216       TraceOptions trace_options;
217       trace_options.enable_sampling = true;
218
219       bool result = controller->EnableMonitoring(
220           CategoryFilter("*"),
221           trace_options,
222           callback);
223       ASSERT_TRUE(result);
224       run_loop.Run();
225       EXPECT_EQ(enable_monitoring_done_callback_count(), 1);
226     }
227
228     {
229       bool is_monitoring;
230       CategoryFilter category_filter("");
231       TraceOptions options;
232       controller->GetMonitoringStatus(
233           &is_monitoring, &category_filter, &options);
234       EXPECT_TRUE(is_monitoring);
235       EXPECT_EQ("*", category_filter.ToString());
236       EXPECT_FALSE(options.record_mode == RECORD_CONTINUOUSLY);
237       EXPECT_TRUE(options.enable_sampling);
238       EXPECT_FALSE(options.enable_systrace);
239     }
240
241     {
242       base::RunLoop run_loop;
243       base::Closure callback = base::Bind(
244           &TracingControllerTest::CaptureMonitoringSnapshotDoneCallbackTest,
245           base::Unretained(this),
246           run_loop.QuitClosure(),
247           result_file_path);
248       ASSERT_TRUE(controller->CaptureMonitoringSnapshot(
249           TracingController::CreateFileSink(result_file_path, callback)));
250       run_loop.Run();
251       EXPECT_EQ(capture_monitoring_snapshot_done_callback_count(), 1);
252     }
253
254     {
255       base::RunLoop run_loop;
256       TracingController::DisableMonitoringDoneCallback callback =
257           base::Bind(&TracingControllerTest::DisableMonitoringDoneCallbackTest,
258                      base::Unretained(this),
259                      run_loop.QuitClosure());
260       bool result = controller->DisableMonitoring(callback);
261       ASSERT_TRUE(result);
262       run_loop.Run();
263       EXPECT_EQ(disable_monitoring_done_callback_count(), 1);
264     }
265
266     {
267       bool is_monitoring;
268       CategoryFilter category_filter("");
269       TraceOptions options;
270       controller->GetMonitoringStatus(&is_monitoring,
271                                       &category_filter,
272                                       &options);
273       EXPECT_FALSE(is_monitoring);
274       EXPECT_EQ("", category_filter.ToString());
275       EXPECT_FALSE(options.record_mode == RECORD_CONTINUOUSLY);
276       EXPECT_FALSE(options.enable_sampling);
277       EXPECT_FALSE(options.enable_systrace);
278     }
279   }
280
281  private:
282   int get_categories_done_callback_count_;
283   int enable_recording_done_callback_count_;
284   int disable_recording_done_callback_count_;
285   int enable_monitoring_done_callback_count_;
286   int disable_monitoring_done_callback_count_;
287   int capture_monitoring_snapshot_done_callback_count_;
288   base::FilePath last_actual_recording_file_path_;
289   base::FilePath last_actual_monitoring_file_path_;
290 };
291
292 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) {
293   Navigate(shell());
294
295   TracingController* controller = TracingController::GetInstance();
296
297   base::RunLoop run_loop;
298   TracingController::GetCategoriesDoneCallback callback =
299       base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest,
300                  base::Unretained(this),
301                  run_loop.QuitClosure());
302   ASSERT_TRUE(controller->GetCategories(callback));
303   run_loop.Run();
304   EXPECT_EQ(get_categories_done_callback_count(), 1);
305 }
306
307 IN_PROC_BROWSER_TEST_F(TracingControllerTest, EnableAndDisableRecording) {
308   TestEnableAndDisableRecordingString();
309 }
310
311 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
312                        EnableAndDisableRecordingWithFilePath) {
313   base::FilePath file_path;
314   base::CreateTemporaryFile(&file_path);
315   TestEnableAndDisableRecordingFile(file_path);
316   EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value());
317 }
318
319 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
320                        EnableAndDisableRecordingWithEmptyFileAndNullCallback) {
321   Navigate(shell());
322
323   TracingController* controller = TracingController::GetInstance();
324   EXPECT_TRUE(controller->EnableRecording(
325       CategoryFilter(),
326       TraceOptions(),
327       TracingController::EnableRecordingDoneCallback()));
328   EXPECT_TRUE(controller->DisableRecording(NULL));
329   base::RunLoop().RunUntilIdle();
330 }
331
332 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
333                        EnableCaptureAndDisableMonitoring) {
334   base::FilePath file_path;
335   base::CreateTemporaryFile(&file_path);
336   TestEnableCaptureAndDisableMonitoring(file_path);
337 }
338
339 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
340                        EnableCaptureAndDisableMonitoringWithFilePath) {
341   base::FilePath file_path;
342   base::CreateTemporaryFile(&file_path);
343   TestEnableCaptureAndDisableMonitoring(file_path);
344   EXPECT_EQ(file_path.value(), last_actual_monitoring_file_path().value());
345 }
346
347 // See http://crbug.com/392446
348 #if defined(OS_ANDROID)
349 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
350     DISABLED_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
351 #else
352 #define MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback \
353     EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback
354 #endif
355 IN_PROC_BROWSER_TEST_F(
356     TracingControllerTest,
357     MAYBE_EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback) {
358   Navigate(shell());
359
360   TracingController* controller = TracingController::GetInstance();
361   TraceOptions trace_options;
362   trace_options.enable_sampling = true;
363   EXPECT_TRUE(controller->EnableMonitoring(
364       CategoryFilter("*"),
365       trace_options,
366       TracingController::EnableMonitoringDoneCallback()));
367   controller->CaptureMonitoringSnapshot(NULL);
368   base::RunLoop().RunUntilIdle();
369   EXPECT_TRUE(controller->DisableMonitoring(
370       TracingController::DisableMonitoringDoneCallback()));
371   base::RunLoop().RunUntilIdle();
372 }
373
374 }  // namespace content