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/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/shell/browser/shell.h" 10 #include "content/test/content_browser_test.h" 11 #include "content/test/content_browser_test_utils.h" 12 13 namespace content { 14 15 class TracingControllerTest : public ContentBrowserTest { 16 public: 17 TracingControllerTest() {} 18 19 virtual void SetUp() OVERRIDE { 20 get_categories_done_callback_count_ = 0; 21 enable_recording_done_callback_count_ = 0; 22 disable_recording_done_callback_count_ = 0; 23 enable_monitoring_done_callback_count_ = 0; 24 disable_monitoring_done_callback_count_ = 0; 25 capture_monitoring_snapshot_done_callback_count_ = 0; 26 ContentBrowserTest::SetUp(); 27 } 28 29 virtual void TearDown() OVERRIDE { 30 ContentBrowserTest::TearDown(); 31 } 32 33 void Navigate(Shell* shell) { 34 NavigateToURL(shell, GetTestUrl("", "title.html")); 35 } 36 37 void GetCategoriesDoneCallbackTest(base::Closure quit_callback, 38 const std::set<std::string>& categories) { 39 get_categories_done_callback_count_++; 40 EXPECT_TRUE(categories.size() > 0); 41 quit_callback.Run(); 42 } 43 44 void EnableRecordingDoneCallbackTest(base::Closure quit_callback) { 45 enable_recording_done_callback_count_++; 46 quit_callback.Run(); 47 } 48 49 void DisableRecordingDoneCallbackTest(base::Closure quit_callback, 50 const base::FilePath& file_path) { 51 disable_recording_done_callback_count_++; 52 EXPECT_TRUE(PathExists(file_path)); 53 int64 file_size; 54 base::GetFileSize(file_path, &file_size); 55 EXPECT_TRUE(file_size > 0); 56 quit_callback.Run(); 57 last_actual_recording_file_path_ = file_path; 58 } 59 60 void EnableMonitoringDoneCallbackTest(base::Closure quit_callback) { 61 enable_monitoring_done_callback_count_++; 62 quit_callback.Run(); 63 } 64 65 void DisableMonitoringDoneCallbackTest(base::Closure quit_callback) { 66 disable_monitoring_done_callback_count_++; 67 quit_callback.Run(); 68 } 69 70 void CaptureMonitoringSnapshotDoneCallbackTest( 71 base::Closure quit_callback, const base::FilePath& file_path) { 72 capture_monitoring_snapshot_done_callback_count_++; 73 EXPECT_TRUE(PathExists(file_path)); 74 int64 file_size; 75 base::GetFileSize(file_path, &file_size); 76 EXPECT_TRUE(file_size > 0); 77 quit_callback.Run(); 78 last_actual_monitoring_file_path_ = file_path; 79 } 80 81 int get_categories_done_callback_count() const { 82 return get_categories_done_callback_count_; 83 } 84 85 int enable_recording_done_callback_count() const { 86 return enable_recording_done_callback_count_; 87 } 88 89 int disable_recording_done_callback_count() const { 90 return disable_recording_done_callback_count_; 91 } 92 93 int enable_monitoring_done_callback_count() const { 94 return enable_monitoring_done_callback_count_; 95 } 96 97 int disable_monitoring_done_callback_count() const { 98 return disable_monitoring_done_callback_count_; 99 } 100 101 int capture_monitoring_snapshot_done_callback_count() const { 102 return capture_monitoring_snapshot_done_callback_count_; 103 } 104 105 base::FilePath last_actual_recording_file_path() const { 106 return last_actual_recording_file_path_; 107 } 108 109 base::FilePath last_actual_monitoring_file_path() const { 110 return last_actual_monitoring_file_path_; 111 } 112 113 void TestEnableAndDisableRecording(const base::FilePath& result_file_path) { 114 Navigate(shell()); 115 116 TracingController* controller = TracingController::GetInstance(); 117 118 { 119 base::RunLoop run_loop; 120 TracingController::EnableRecordingDoneCallback callback = 121 base::Bind(&TracingControllerTest::EnableRecordingDoneCallbackTest, 122 base::Unretained(this), 123 run_loop.QuitClosure()); 124 bool result = controller->EnableRecording( 125 "", TracingController::DEFAULT_OPTIONS, callback); 126 ASSERT_TRUE(result); 127 run_loop.Run(); 128 EXPECT_EQ(enable_recording_done_callback_count(), 1); 129 } 130 131 { 132 base::RunLoop run_loop; 133 TracingController::TracingFileResultCallback callback = 134 base::Bind(&TracingControllerTest::DisableRecordingDoneCallbackTest, 135 base::Unretained(this), 136 run_loop.QuitClosure()); 137 bool result = controller->DisableRecording(result_file_path, callback); 138 ASSERT_TRUE(result); 139 run_loop.Run(); 140 EXPECT_EQ(disable_recording_done_callback_count(), 1); 141 } 142 } 143 144 void TestEnableCaptureAndDisableMonitoring( 145 const base::FilePath& result_file_path) { 146 Navigate(shell()); 147 148 TracingController* controller = TracingController::GetInstance(); 149 150 { 151 base::RunLoop run_loop; 152 TracingController::EnableMonitoringDoneCallback callback = 153 base::Bind(&TracingControllerTest::EnableMonitoringDoneCallbackTest, 154 base::Unretained(this), 155 run_loop.QuitClosure()); 156 bool result = controller->EnableMonitoring( 157 "", TracingController::ENABLE_SAMPLING, callback); 158 ASSERT_TRUE(result); 159 run_loop.Run(); 160 EXPECT_EQ(enable_monitoring_done_callback_count(), 1); 161 } 162 163 { 164 base::RunLoop run_loop; 165 TracingController::TracingFileResultCallback callback = 166 base::Bind(&TracingControllerTest:: 167 CaptureMonitoringSnapshotDoneCallbackTest, 168 base::Unretained(this), 169 run_loop.QuitClosure()); 170 ASSERT_TRUE(controller->CaptureMonitoringSnapshot(result_file_path, 171 callback)); 172 run_loop.Run(); 173 EXPECT_EQ(capture_monitoring_snapshot_done_callback_count(), 1); 174 } 175 176 { 177 base::RunLoop run_loop; 178 TracingController::DisableMonitoringDoneCallback callback = 179 base::Bind(&TracingControllerTest::DisableMonitoringDoneCallbackTest, 180 base::Unretained(this), 181 run_loop.QuitClosure()); 182 bool result = controller->DisableMonitoring(callback); 183 ASSERT_TRUE(result); 184 run_loop.Run(); 185 EXPECT_EQ(disable_monitoring_done_callback_count(), 1); 186 } 187 } 188 189 private: 190 int get_categories_done_callback_count_; 191 int enable_recording_done_callback_count_; 192 int disable_recording_done_callback_count_; 193 int enable_monitoring_done_callback_count_; 194 int disable_monitoring_done_callback_count_; 195 int capture_monitoring_snapshot_done_callback_count_; 196 base::FilePath last_actual_recording_file_path_; 197 base::FilePath last_actual_monitoring_file_path_; 198 }; 199 200 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) { 201 Navigate(shell()); 202 203 TracingController* controller = TracingController::GetInstance(); 204 205 base::RunLoop run_loop; 206 TracingController::GetCategoriesDoneCallback callback = 207 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest, 208 base::Unretained(this), 209 run_loop.QuitClosure()); 210 ASSERT_TRUE(controller->GetCategories(callback)); 211 run_loop.Run(); 212 EXPECT_EQ(get_categories_done_callback_count(), 1); 213 } 214 215 IN_PROC_BROWSER_TEST_F(TracingControllerTest, EnableAndDisableRecording) { 216 TestEnableAndDisableRecording(base::FilePath()); 217 } 218 219 IN_PROC_BROWSER_TEST_F(TracingControllerTest, 220 EnableAndDisableRecordingWithFilePath) { 221 base::FilePath file_path; 222 base::CreateTemporaryFile(&file_path); 223 TestEnableAndDisableRecording(file_path); 224 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value()); 225 } 226 227 IN_PROC_BROWSER_TEST_F(TracingControllerTest, 228 EnableAndDisableRecordingWithEmptyFileAndNullCallback) { 229 Navigate(shell()); 230 231 TracingController* controller = TracingController::GetInstance(); 232 EXPECT_TRUE(controller->EnableRecording( 233 "", TracingController::DEFAULT_OPTIONS, 234 TracingController::EnableRecordingDoneCallback())); 235 EXPECT_TRUE(controller->DisableRecording( 236 base::FilePath(), TracingController::TracingFileResultCallback())); 237 base::RunLoop().RunUntilIdle(); 238 } 239 240 IN_PROC_BROWSER_TEST_F(TracingControllerTest, 241 EnableCaptureAndDisableMonitoring) { 242 TestEnableCaptureAndDisableMonitoring(base::FilePath()); 243 } 244 245 IN_PROC_BROWSER_TEST_F(TracingControllerTest, 246 EnableCaptureAndDisableMonitoringWithFilePath) { 247 base::FilePath file_path; 248 base::CreateTemporaryFile(&file_path); 249 TestEnableCaptureAndDisableMonitoring(file_path); 250 EXPECT_EQ(file_path.value(), last_actual_monitoring_file_path().value()); 251 } 252 253 IN_PROC_BROWSER_TEST_F( 254 TracingControllerTest, 255 EnableCaptureAndDisableMonitoringWithEmptyFileAndNullCallback) { 256 Navigate(shell()); 257 258 TracingController* controller = TracingController::GetInstance(); 259 EXPECT_TRUE(controller->EnableMonitoring( 260 "", TracingController::ENABLE_SAMPLING, 261 TracingController::EnableMonitoringDoneCallback())); 262 controller->CaptureMonitoringSnapshot( 263 base::FilePath(), TracingController::TracingFileResultCallback()); 264 base::RunLoop().RunUntilIdle(); 265 EXPECT_TRUE(controller->DisableMonitoring( 266 TracingController::DisableMonitoringDoneCallback())); 267 base::RunLoop().RunUntilIdle(); 268 } 269 270 } // namespace content 271