blob: 08ca824994cb7c5a7c34efde5f1771922a10fb5e [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]a8ba174a2013-09-11 14:28:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng36b6aec92015-12-26 06:16:365#include "content/public/browser/tracing_controller.h"
6
avib7348942015-12-25 20:57:107#include <stdint.h>
Arthur Sonzognic686e8f2024-01-11 08:36:378
9#include <optional>
dcheng36b6aec92015-12-26 06:16:3610#include <utility>
avib7348942015-12-25 20:57:1011
thestigb7aad54f2014-09-05 18:25:3912#include "base/files/file_util.h"
Avi Drissmanadac21992023-01-11 23:46:3913#include "base/functional/bind.h"
Etienne Bergeron9dcc6822018-11-27 21:41:0214#include "base/json/json_reader.h"
caseqb85bb8f2014-09-15 10:50:1015#include "base/memory/ref_counted_memory.h"
[email protected]a8ba174a2013-09-11 14:28:0216#include "base/run_loop.h"
zhenwecfd5342015-12-11 19:48:4017#include "base/strings/pattern.h"
Eric Secklera8baa542020-02-07 10:45:4718#include "base/task/task_traits.h"
lukasza7947ccd2016-07-28 21:56:2519#include "base/threading/thread_restrictions.h"
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:1020#include "base/values.h"
avib7348942015-12-25 20:57:1021#include "build/build_config.h"
Michael Spang8a063342019-12-06 22:54:3322#include "build/chromecast_buildflags.h"
caseq2fe929b2016-07-13 20:23:1223#include "content/browser/tracing/tracing_controller_impl.h"
Eric Seckler8652dcd52018-09-20 10:42:2824#include "content/public/browser/browser_task_traits.h"
simonhatch329f14f2015-04-24 13:42:2025#include "content/public/browser/browser_thread.h"
Peter Kasting919ce652020-05-07 10:22:3626#include "content/public/test/browser_test.h"
[email protected]a8ba174a2013-09-11 14:28:0227#include "content/public/test/browser_test_utils.h"
[email protected]6e9def12014-03-27 20:23:2828#include "content/public/test/content_browser_test.h"
29#include "content/public/test/content_browser_test_utils.h"
[email protected]a8ba174a2013-09-11 14:28:0230#include "content/shell/browser/shell.h"
caseqbffe4e62016-07-14 02:09:1431#include "content/test/test_content_browser_client.h"
Sergio Villar Seninad5740e2018-08-07 15:41:1232#include "services/network/public/cpp/shared_url_loader_factory.h"
Etienne Pierre-doray57db71a2024-12-10 23:50:4333#include "services/tracing/public/cpp/perfetto/trace_event_metadata_source.h"
Eric Seckler4d2bcd32018-11-01 09:51:0934#include "services/tracing/public/cpp/tracing_features.h"
[email protected]a8ba174a2013-09-11 14:28:0235
Georg Neis35ff854b2024-12-17 02:02:0836#if BUILDFLAG(IS_CHROMEOS)
Yeunjoo Choi5c4d74b2022-07-28 04:41:3637#include "chromeos/ash/components/dbus/debug_daemon/debug_daemon_client.h"
Yeunjoo Choia10bd86e2022-12-15 02:34:5138#include "chromeos/ash/components/system/fake_statistics_provider.h"
39#include "chromeos/ash/components/system/statistics_provider.h"
Jonghyun Ahnf0d70102019-08-21 22:02:1540#endif
41
ssidb2e3ece2015-02-09 16:02:2042using base::trace_event::RECORD_CONTINUOUSLY;
43using base::trace_event::RECORD_UNTIL_FULL;
zhenwd601ddc52015-06-02 21:46:3444using base::trace_event::TraceConfig;
[email protected]4753b9e2014-08-04 16:30:5245
[email protected]a8ba174a2013-09-11 14:28:0246namespace content {
47
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:1048class TracingControllerTestEndpoint
49 : public TracingController::TraceDataEndpoint {
simonhatch329f14f2015-04-24 13:42:2050 public:
51 TracingControllerTestEndpoint(
Oystein Eftevaag2386e052019-10-02 21:08:0952 TracingController::CompletionCallback done_callback)
53 : done_callback_(std::move(done_callback)) {}
simonhatch329f14f2015-04-24 13:42:2054
caseqe9307932016-07-16 00:55:3955 void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) override {
56 EXPECT_FALSE(chunk->empty());
57 trace_ += *chunk;
simonhatch329f14f2015-04-24 13:42:2058 }
59
Oystein Eftevaag2386e052019-10-02 21:08:0960 void ReceivedTraceFinalContents() override {
Gabriel Charettee7cdc5cd2020-05-27 23:35:0561 GetUIThreadTaskRunner({})->PostTask(
62 FROM_HERE,
Oystein Eftevaag2386e052019-10-02 21:08:0963 base::BindOnce(std::move(done_callback_),
64 std::make_unique<std::string>(std::move(trace_))));
simonhatch329f14f2015-04-24 13:42:2065 }
66
67 protected:
Oystein Eftevaag2386e052019-10-02 21:08:0968 ~TracingControllerTestEndpoint() override = default;
simonhatch329f14f2015-04-24 13:42:2069
70 std::string trace_;
Oystein Eftevaag2386e052019-10-02 21:08:0971 TracingController::CompletionCallback done_callback_;
simonhatch329f14f2015-04-24 13:42:2072};
73
[email protected]a8ba174a2013-09-11 14:28:0274class TracingControllerTest : public ContentBrowserTest {
75 public:
Avi Drissman89545b12023-02-02 05:13:0876 TracingControllerTest() = default;
[email protected]a8ba174a2013-09-11 14:28:0277
dchengfa85b152014-10-28 01:13:4278 void SetUp() override {
[email protected]a8ba174a2013-09-11 14:28:0279 get_categories_done_callback_count_ = 0;
80 enable_recording_done_callback_count_ = 0;
81 disable_recording_done_callback_count_ = 0;
Jonghyun Ahnf0d70102019-08-21 22:02:1582
Georg Neis35ff854b2024-12-17 02:02:0883#if BUILDFLAG(IS_CHROMEOS)
Yeunjoo Choi5c4d74b2022-07-28 04:41:3684 ash::DebugDaemonClient::InitializeFake();
Jonghyun Ahnf0d70102019-08-21 22:02:1585 // Set statistic provider for hardware class tests.
Yeunjoo Choi462896712022-12-20 01:15:4086 ash::system::StatisticsProvider::SetTestProvider(
Jonghyun Ahnf0d70102019-08-21 22:02:1587 &fake_statistics_provider_);
88 fake_statistics_provider_.SetMachineStatistic(
Yeunjoo Choi462896712022-12-20 01:15:4089 ash::system::kHardwareClassKey, "test-hardware-class");
Jonghyun Ahnf0d70102019-08-21 22:02:1590#endif
[email protected]a8ba174a2013-09-11 14:28:0291 ContentBrowserTest::SetUp();
92 }
93
James Cook9f3078852022-07-21 16:13:4094 void TearDown() override {
95 ContentBrowserTest::TearDown();
Georg Neis35ff854b2024-12-17 02:02:0896#if BUILDFLAG(IS_CHROMEOS)
Yeunjoo Choi5c4d74b2022-07-28 04:41:3697 ash::DebugDaemonClient::Shutdown();
James Cook9f3078852022-07-21 16:13:4098#endif
99 }
100
[email protected]a8ba174a2013-09-11 14:28:02101 void Navigate(Shell* shell) {
Alex Moshchuk4174c192019-08-20 16:58:09102 EXPECT_TRUE(NavigateToURL(shell, GetTestUrl("", "title1.html")));
[email protected]a8ba174a2013-09-11 14:28:02103 }
104
Arthur Sonzognic686e8f2024-01-11 08:36:37105 std::optional<base::Value::Dict> GenerateMetadataDict() {
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:10106 return std::move(metadata_);
107 }
108
danakj18743702019-11-29 21:33:24109 void GetCategoriesDoneCallbackTest(base::OnceClosure quit_callback,
[email protected]a8ba174a2013-09-11 14:28:02110 const std::set<std::string>& categories) {
111 get_categories_done_callback_count_++;
Etienne Bergeronff38be12018-11-26 20:00:35112 EXPECT_FALSE(categories.empty());
Tommy Nyquist4b749d02018-03-20 21:46:29113 std::move(quit_callback).Run();
[email protected]a8ba174a2013-09-11 14:28:02114 }
115
danakj18743702019-11-29 21:33:24116 void StartTracingDoneCallbackTest(base::OnceClosure quit_callback) {
[email protected]a8ba174a2013-09-11 14:28:02117 enable_recording_done_callback_count_++;
Tommy Nyquist4b749d02018-03-20 21:46:29118 std::move(quit_callback).Run();
[email protected]a8ba174a2013-09-11 14:28:02119 }
120
Oystein Eftevaag2386e052019-10-02 21:08:09121 void StopTracingStringDoneCallbackTest(base::OnceClosure quit_callback,
122 std::unique_ptr<std::string> data) {
caseqb85bb8f2014-09-15 10:50:10123 disable_recording_done_callback_count_++;
Oystein Eftevaag2386e052019-10-02 21:08:09124 last_data_ = std::move(data);
125 EXPECT_FALSE(last_data_->empty());
Tommy Nyquist4b749d02018-03-20 21:46:29126 std::move(quit_callback).Run();
caseqb85bb8f2014-09-15 10:50:10127 }
128
danakj18743702019-11-29 21:33:24129 void StopTracingFileDoneCallbackTest(base::OnceClosure quit_callback,
Oystein Eftevaag2386e052019-10-02 21:08:09130 const base::FilePath& file_path) {
[email protected]a8ba174a2013-09-11 14:28:02131 disable_recording_done_callback_count_++;
lukasza7947ccd2016-07-28 21:56:25132 {
Francois Doray21cd53192018-08-21 13:32:35133 base::ScopedAllowBlockingForTesting allow_blocking;
lukasza7947ccd2016-07-28 21:56:25134 EXPECT_TRUE(PathExists(file_path));
Helmut Januschkaa3cf92a42024-10-17 21:09:58135 std::optional<int64_t> file_size = base::GetFileSize(file_path);
136 ASSERT_TRUE(file_size.has_value());
137 EXPECT_GT(file_size.value(), 0);
lukasza7947ccd2016-07-28 21:56:25138 }
Tommy Nyquist4b749d02018-03-20 21:46:29139 std::move(quit_callback).Run();
[email protected]727e9d92013-11-20 02:13:51140 last_actual_recording_file_path_ = file_path;
[email protected]7ad4e2d2013-10-08 01:01:25141 }
142
Jonghyun Ahnf0d70102019-08-21 22:02:15143 int get_categories_done_callback_count() const {
[email protected]a8ba174a2013-09-11 14:28:02144 return get_categories_done_callback_count_;
145 }
146
147 int enable_recording_done_callback_count() const {
148 return enable_recording_done_callback_count_;
149 }
150
151 int disable_recording_done_callback_count() const {
152 return disable_recording_done_callback_count_;
153 }
154
[email protected]727e9d92013-11-20 02:13:51155 base::FilePath last_actual_recording_file_path() const {
156 return last_actual_recording_file_path_;
157 }
158
Oystein Eftevaag2386e052019-10-02 21:08:09159 const std::string& last_data() const { return *last_data_; }
zhenwecfd5342015-12-11 19:48:40160
Eric Seckler4d2bcd32018-11-01 09:51:09161 void TestStartAndStopTracingString(bool enable_systrace = false) {
[email protected]727e9d92013-11-20 02:13:51162 Navigate(shell());
163
164 TracingController* controller = TracingController::GetInstance();
165
166 {
167 base::RunLoop run_loop;
zhenw873bdff2015-11-11 22:16:55168 TracingController::StartTracingDoneCallback callback =
tzik37afd2a2018-10-17 06:40:18169 base::BindOnce(&TracingControllerTest::StartTracingDoneCallbackTest,
170 base::Unretained(this), run_loop.QuitClosure());
Eric Seckler4d2bcd32018-11-01 09:51:09171 TraceConfig config;
172 if (enable_systrace)
173 config.EnableSystrace();
174 bool result = controller->StartTracing(config, std::move(callback));
[email protected]1eb14612013-11-21 01:04:58175 ASSERT_TRUE(result);
[email protected]727e9d92013-11-20 02:13:51176 run_loop.Run();
177 EXPECT_EQ(enable_recording_done_callback_count(), 1);
178 }
179
180 {
181 base::RunLoop run_loop;
Jan Wilken Dörrie8c74db022020-04-20 09:05:00182 TracingController::CompletionCallback callback = base::BindOnce(
Oystein Eftevaag2386e052019-10-02 21:08:09183 &TracingControllerTest::StopTracingStringDoneCallbackTest,
184 base::Unretained(this), run_loop.QuitClosure());
zhenw873bdff2015-11-11 22:16:55185 bool result = controller->StopTracing(
Tommy Nyquist4b749d02018-03-20 21:46:29186 TracingController::CreateStringEndpoint(std::move(callback)));
caseqb85bb8f2014-09-15 10:50:10187 ASSERT_TRUE(result);
188 run_loop.Run();
189 EXPECT_EQ(disable_recording_done_callback_count(), 1);
190 }
191 }
192
zhenwecfd5342015-12-11 19:48:40193 void TestStartAndStopTracingStringWithFilter() {
Etienne Bergeron9dcc6822018-11-27 21:41:02194
zhenwecfd5342015-12-11 19:48:40195 Navigate(shell());
196
Oystein Eftevaage960b772018-04-26 18:29:18197 TracingControllerImpl* controller = TracingControllerImpl::GetInstance();
zhenwecfd5342015-12-11 19:48:40198
199 {
200 base::RunLoop run_loop;
201 TracingController::StartTracingDoneCallback callback =
tzik37afd2a2018-10-17 06:40:18202 base::BindOnce(&TracingControllerTest::StartTracingDoneCallbackTest,
203 base::Unretained(this), run_loop.QuitClosure());
zhenwecfd5342015-12-11 19:48:40204
Oystein Eftevaag718aa022019-10-11 18:49:02205 bool result =
206 controller->StartTracing(TraceConfig(), std::move(callback));
zhenwecfd5342015-12-11 19:48:40207 ASSERT_TRUE(result);
208 run_loop.Run();
209 EXPECT_EQ(enable_recording_done_callback_count(), 1);
210 }
211
212 {
213 base::RunLoop run_loop;
Jan Wilken Dörrie8c74db022020-04-20 09:05:00214 TracingController::CompletionCallback callback = base::BindOnce(
Oystein Eftevaag2386e052019-10-02 21:08:09215 &TracingControllerTest::StopTracingStringDoneCallbackTest,
216 base::Unretained(this), run_loop.QuitClosure());
zhenwecfd5342015-12-11 19:48:40217
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:10218 scoped_refptr<TracingController::TraceDataEndpoint> trace_data_endpoint =
Tommy Nyquist4b749d02018-03-20 21:46:29219 TracingController::CreateStringEndpoint(std::move(callback));
zhenwecfd5342015-12-11 19:48:40220
Sergii Bykov50e5b022023-10-10 13:46:47221 metadata_ = base::Value::Dict().Set("not-whitelisted", "this_not_found");
Etienne Pierre-dorayf353fdc52022-12-06 19:39:17222 tracing::TraceEventMetadataSource::GetInstance()->AddGeneratorFunction(
danakj18743702019-11-29 21:33:24223 base::BindRepeating(&TracingControllerTest::GenerateMetadataDict,
224 base::Unretained(this)));
zhenwecfd5342015-12-11 19:48:40225
Oystein Eftevaag718aa022019-10-11 18:49:02226 bool result =
227 controller->StopTracing(trace_data_endpoint, /*agent_label=*/"",
228 /*privacy_filtering_enabled=*/true);
zhenwecfd5342015-12-11 19:48:40229 ASSERT_TRUE(result);
230 run_loop.Run();
231 EXPECT_EQ(disable_recording_done_callback_count(), 1);
232 }
233 }
234
zhenw873bdff2015-11-11 22:16:55235 void TestStartAndStopTracingCompressed() {
simonhatch329f14f2015-04-24 13:42:20236 Navigate(shell());
237
238 TracingController* controller = TracingController::GetInstance();
239
240 {
241 base::RunLoop run_loop;
zhenw873bdff2015-11-11 22:16:55242 TracingController::StartTracingDoneCallback callback =
tzik37afd2a2018-10-17 06:40:18243 base::BindOnce(&TracingControllerTest::StartTracingDoneCallbackTest,
244 base::Unretained(this), run_loop.QuitClosure());
Tommy Nyquist4b749d02018-03-20 21:46:29245 bool result =
246 controller->StartTracing(TraceConfig(), std::move(callback));
simonhatch329f14f2015-04-24 13:42:20247 ASSERT_TRUE(result);
248 run_loop.Run();
249 EXPECT_EQ(enable_recording_done_callback_count(), 1);
250 }
251
252 {
253 base::RunLoop run_loop;
Oystein Eftevaag2386e052019-10-02 21:08:09254 TracingController::CompletionCallback callback = base::BindOnce(
255 &TracingControllerTest::StopTracingStringDoneCallbackTest,
256 base::Unretained(this), run_loop.QuitClosure());
zhenw873bdff2015-11-11 22:16:55257 bool result = controller->StopTracing(
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:10258 TracingControllerImpl::CreateCompressedStringEndpoint(
Tommy Nyquist4b749d02018-03-20 21:46:29259 new TracingControllerTestEndpoint(std::move(callback)),
Oystein Eftevaag358caab2017-09-25 03:25:38260 true /* compress_with_background_priority */));
simonhatch329f14f2015-04-24 13:42:20261 ASSERT_TRUE(result);
262 run_loop.Run();
263 EXPECT_EQ(disable_recording_done_callback_count(), 1);
264 }
265 }
266
zhenw873bdff2015-11-11 22:16:55267 void TestStartAndStopTracingFile(
caseqb85bb8f2014-09-15 10:50:10268 const base::FilePath& result_file_path) {
269 Navigate(shell());
270
271 TracingController* controller = TracingController::GetInstance();
272
273 {
274 base::RunLoop run_loop;
zhenw873bdff2015-11-11 22:16:55275 TracingController::StartTracingDoneCallback callback =
tzik37afd2a2018-10-17 06:40:18276 base::BindOnce(&TracingControllerTest::StartTracingDoneCallbackTest,
277 base::Unretained(this), run_loop.QuitClosure());
Tommy Nyquist4b749d02018-03-20 21:46:29278 bool result =
279 controller->StartTracing(TraceConfig(), std::move(callback));
caseqb85bb8f2014-09-15 10:50:10280 ASSERT_TRUE(result);
281 run_loop.Run();
282 EXPECT_EQ(enable_recording_done_callback_count(), 1);
283 }
284
285 {
286 base::RunLoop run_loop;
Oystein Eftevaag2386e052019-10-02 21:08:09287 base::RepeatingClosure callback = base::BindRepeating(
zhenw873bdff2015-11-11 22:16:55288 &TracingControllerTest::StopTracingFileDoneCallbackTest,
Oystein Eftevaag2386e052019-10-02 21:08:09289 base::Unretained(this), run_loop.QuitClosure(), result_file_path);
Tommy Nyquist4b749d02018-03-20 21:46:29290 bool result =
291 controller->StopTracing(TracingController::CreateFileEndpoint(
Eric Secklera8baa542020-02-07 10:45:47292 result_file_path, std::move(callback),
293 base::TaskPriority::USER_BLOCKING));
[email protected]1eb14612013-11-21 01:04:58294 ASSERT_TRUE(result);
[email protected]727e9d92013-11-20 02:13:51295 run_loop.Run();
296 EXPECT_EQ(disable_recording_done_callback_count(), 1);
297 }
298 }
299
Georg Neis35ff854b2024-12-17 02:02:08300#if BUILDFLAG(IS_CHROMEOS)
Jonghyun Ahnf0d70102019-08-21 22:02:15301 protected:
Yeunjoo Choi462896712022-12-20 01:15:40302 ash::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
Jonghyun Ahnf0d70102019-08-21 22:02:15303#endif
304
[email protected]a8ba174a2013-09-11 14:28:02305 private:
306 int get_categories_done_callback_count_;
307 int enable_recording_done_callback_count_;
308 int disable_recording_done_callback_count_;
[email protected]727e9d92013-11-20 02:13:51309 base::FilePath last_actual_recording_file_path_;
Arthur Sonzognic686e8f2024-01-11 08:36:37310 std::optional<base::Value::Dict> metadata_;
Oystein Eftevaag2386e052019-10-02 21:08:09311 std::unique_ptr<std::string> last_data_;
[email protected]a8ba174a2013-09-11 14:28:02312};
313
Francois Doray570cfda2020-01-24 19:59:16314// Consistent failures on Android Asan https://crbug.com/1045519
Xiaohan Wang3b52e79d02022-01-15 17:36:20315#if BUILDFLAG(IS_ANDROID) && defined(ADDRESS_SANITIZER)
Francois Doray570cfda2020-01-24 19:59:16316#define MAYBE_EnableAndStopTracing DISABLED_EnableAndStopTracing
317#define MAYBE_DisableRecordingStoresMetadata \
318 DISABLED_DisableRecordingStoresMetadata
Francois Doray570cfda2020-01-24 19:59:16319#define MAYBE_EnableAndStopTracingWithFilePath \
320 DISABLED_EnableAndStopTracingWithFilePath
321#define MAYBE_EnableAndStopTracingWithCompression \
322 DISABLED_EnableAndStopTracingWithCompression
Eric Secklercedf986b2020-03-04 13:40:14323#define MAYBE_EnableAndStopTracingWithEmptyFile \
324 DISABLED_EnableAndStopTracingWithEmptyFile
325#define MAYBE_DoubleStopTracing DISABLED_DoubleStopTracing
Mikhail Khokhlovc611ee082020-07-31 12:18:25326#define MAYBE_ProcessesPresentInTrace DISABLED_ProcessesPresentInTrace
Francois Doray570cfda2020-01-24 19:59:16327#else
328#define MAYBE_EnableAndStopTracing EnableAndStopTracing
329#define MAYBE_DisableRecordingStoresMetadata DisableRecordingStoresMetadata
Francois Doray570cfda2020-01-24 19:59:16330#define MAYBE_EnableAndStopTracingWithFilePath EnableAndStopTracingWithFilePath
331#define MAYBE_EnableAndStopTracingWithCompression \
332 EnableAndStopTracingWithCompression
Eric Secklercedf986b2020-03-04 13:40:14333#define MAYBE_EnableAndStopTracingWithEmptyFile \
334 EnableAndStopTracingWithEmptyFile
335#define MAYBE_DoubleStopTracing DoubleStopTracing
Mikhail Khokhlovc611ee082020-07-31 12:18:25336#define MAYBE_ProcessesPresentInTrace ProcessesPresentInTrace
Francois Doray570cfda2020-01-24 19:59:16337#endif
338
Etienne Bergeronf89bca72018-11-28 18:56:33339IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) {
[email protected]a8ba174a2013-09-11 14:28:02340 Navigate(shell());
341
342 TracingController* controller = TracingController::GetInstance();
343
344 base::RunLoop run_loop;
345 TracingController::GetCategoriesDoneCallback callback =
tzik37afd2a2018-10-17 06:40:18346 base::BindOnce(&TracingControllerTest::GetCategoriesDoneCallbackTest,
347 base::Unretained(this), run_loop.QuitClosure());
Tommy Nyquist4b749d02018-03-20 21:46:29348 ASSERT_TRUE(controller->GetCategories(std::move(callback)));
[email protected]a8ba174a2013-09-11 14:28:02349 run_loop.Run();
350 EXPECT_EQ(get_categories_done_callback_count(), 1);
351}
352
Francois Doray570cfda2020-01-24 19:59:16353IN_PROC_BROWSER_TEST_F(TracingControllerTest, MAYBE_EnableAndStopTracing) {
zhenw873bdff2015-11-11 22:16:55354 TestStartAndStopTracingString();
[email protected]727e9d92013-11-20 02:13:51355}
356
Avi Drissmanebcefcc12018-08-07 15:37:27357IN_PROC_BROWSER_TEST_F(TracingControllerTest,
Francois Doray570cfda2020-01-24 19:59:16358 MAYBE_DisableRecordingStoresMetadata) {
zhenw873bdff2015-11-11 22:16:55359 TestStartAndStopTracingString();
beaudoin34e0ac12015-11-09 20:51:02360 // Check that a number of important keys exist in the metadata dictionary. The
361 // values are not checked to ensure the test is robust.
Arthur Sonzognic686e8f2024-01-11 08:36:37362 std::optional<base::Value> trace_json = base::JSONReader::Read(last_data());
Oystein Eftevaag2386e052019-10-02 21:08:09363 ASSERT_TRUE(trace_json);
Luc Nguyen64e9875f2023-03-13 21:51:05364 ASSERT_TRUE(trace_json->is_dict());
365 auto* metadata_json = trace_json->GetDict().FindDict("metadata");
Oystein Eftevaag2386e052019-10-02 21:08:09366 ASSERT_TRUE(metadata_json);
367
Luc Nguyen64e9875f2023-03-13 21:51:05368 std::string* user_agent = metadata_json->FindString("user-agent");
Scott Haseley02ebe052021-09-16 01:43:00369 ASSERT_TRUE(user_agent);
370 EXPECT_FALSE(user_agent->empty());
371
Luc Nguyen64e9875f2023-03-13 21:51:05372 std::string* os_name = metadata_json->FindString("os-name");
Scott Haseley02ebe052021-09-16 01:43:00373 ASSERT_TRUE(os_name);
374 EXPECT_FALSE(os_name->empty());
375
Luc Nguyen64e9875f2023-03-13 21:51:05376 std::string* command_line = metadata_json->FindString("command_line");
Scott Haseley02ebe052021-09-16 01:43:00377 ASSERT_TRUE(command_line);
378 EXPECT_FALSE(command_line->empty());
zhenwecfd5342015-12-11 19:48:40379}
380
[email protected]727e9d92013-11-20 02:13:51381IN_PROC_BROWSER_TEST_F(TracingControllerTest,
Francois Doray570cfda2020-01-24 19:59:16382 MAYBE_EnableAndStopTracingWithFilePath) {
[email protected]727e9d92013-11-20 02:13:51383 base::FilePath file_path;
lukasza7947ccd2016-07-28 21:56:25384 {
Francois Doray21cd53192018-08-21 13:32:35385 base::ScopedAllowBlockingForTesting allow_blocking;
lukasza7947ccd2016-07-28 21:56:25386 base::CreateTemporaryFile(&file_path);
387 }
zhenw873bdff2015-11-11 22:16:55388 TestStartAndStopTracingFile(file_path);
[email protected]727e9d92013-11-20 02:13:51389 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value());
390}
391
392IN_PROC_BROWSER_TEST_F(TracingControllerTest,
Francois Doray570cfda2020-01-24 19:59:16393 MAYBE_EnableAndStopTracingWithCompression) {
zhenw873bdff2015-11-11 22:16:55394 TestStartAndStopTracingCompressed();
simonhatch329f14f2015-04-24 13:42:20395}
396
397IN_PROC_BROWSER_TEST_F(TracingControllerTest,
Eric Secklercedf986b2020-03-04 13:40:14398 MAYBE_EnableAndStopTracingWithEmptyFile) {
[email protected]a8ba174a2013-09-11 14:28:02399 Navigate(shell());
400
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:10401 base::RunLoop run_loop;
[email protected]a8ba174a2013-09-11 14:28:02402 TracingController* controller = TracingController::GetInstance();
zhenw873bdff2015-11-11 22:16:55403 EXPECT_TRUE(controller->StartTracing(
zhenwd601ddc52015-06-02 21:46:34404 TraceConfig(),
zhenw873bdff2015-11-11 22:16:55405 TracingController::StartTracingDoneCallback()));
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:10406 EXPECT_TRUE(controller->StopTracing(
Jan Wilken Dörrie8c74db022020-04-20 09:05:00407 TracingControllerImpl::CreateCallbackEndpoint(base::BindOnce(
Oystein Eftevaag2386e052019-10-02 21:08:09408 [](base::OnceClosure quit_closure,
409 std::unique_ptr<std::string> trace_str) {
Tommy Nyquist4b749d02018-03-20 21:46:29410 std::move(quit_closure).Run();
411 },
Ehsan Chiniforooshanc77b44e2017-10-10 18:12:10412 run_loop.QuitClosure()))));
413 run_loop.Run();
[email protected]a8ba174a2013-09-11 14:28:02414}
415
Eric Secklercedf986b2020-03-04 13:40:14416IN_PROC_BROWSER_TEST_F(TracingControllerTest, MAYBE_DoubleStopTracing) {
Vasiliy Tsukanovbdadfa7f2018-02-22 17:29:31417 Navigate(shell());
418
419 base::RunLoop run_loop;
420 TracingController* controller = TracingController::GetInstance();
421 EXPECT_TRUE(controller->StartTracing(
422 TraceConfig(), TracingController::StartTracingDoneCallback()));
423 EXPECT_TRUE(controller->StopTracing(
Oystein Eftevaag2386e052019-10-02 21:08:09424 TracingControllerImpl::CreateCallbackEndpoint(base::BindOnce(
425 [](base::OnceClosure quit_closure,
426 std::unique_ptr<std::string> trace_str) {
Tommy Nyquist4b749d02018-03-20 21:46:29427 std::move(quit_closure).Run();
428 },
Vasiliy Tsukanovbdadfa7f2018-02-22 17:29:31429 run_loop.QuitClosure()))));
430 EXPECT_FALSE(controller->StopTracing(nullptr));
431 run_loop.Run();
432}
433
Eric Seckler4b8ad572018-11-29 11:12:41434// Only CrOS and Cast support system tracing.
Georg Neis35ff854b2024-12-17 02:02:08435#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_CASTOS)
Eric Seckler4d2bcd32018-11-01 09:51:09436#define MAYBE_SystemTraceEvents SystemTraceEvents
437#else
438#define MAYBE_SystemTraceEvents DISABLED_SystemTraceEvents
439#endif
440IN_PROC_BROWSER_TEST_F(TracingControllerTest, MAYBE_SystemTraceEvents) {
Eric Seckler4d2bcd32018-11-01 09:51:09441 TestStartAndStopTracingString(true /* enable_systrace */);
Eric Seckler4d2bcd32018-11-01 09:51:09442 EXPECT_TRUE(last_data().find("systemTraceEvents") != std::string::npos);
443}
444
Mikhail Khokhlovc611ee082020-07-31 12:18:25445IN_PROC_BROWSER_TEST_F(TracingControllerTest, MAYBE_ProcessesPresentInTrace) {
Oystein Eftevaag69ec0a02019-05-22 23:35:22446 TestStartAndStopTracingString();
447 EXPECT_TRUE(last_data().find("CrBrowserMain") != std::string::npos);
448 EXPECT_TRUE(last_data().find("CrRendererMain") != std::string::npos);
449}
450
[email protected]a8ba174a2013-09-11 14:28:02451} // namespace content