1 // Copyright 2024 The Chromium Authors
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/test/test_trace_processor.h"
6
7 #include "base/test/task_environment.h"
8 #include "base/test/trace_test_utils.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/perfetto/include/perfetto/tracing/tracing.h"
12
13 namespace base::test {
14
15
16 class TestTraceProcessorExample : public ::testing::Test {
17 private:
18 base::test::TracingEnvironment tracing_environment_;
19 base::test::TaskEnvironment task_environment_;
20 };
21
TEST_F(TestTraceProcessorExample,Basic)22 TEST_F(TestTraceProcessorExample, Basic) {
23 TestTraceProcessor test_trace_processor;
24 test_trace_processor.StartTrace("");
25
26 {
27 // A simple trace event inside of a scope so it gets flushed properly.
28 TRACE_EVENT("test_category", "test_event");
29 }
30
31 auto status = test_trace_processor.StopAndParseTrace();
32 ASSERT_TRUE(status.ok()) << status.message();
33
34 auto result = test_trace_processor.RunQuery(R"(
35 SELECT
36 name
37 FROM slice
38 WHERE category = 'test_category'
39 )");
40 ASSERT_TRUE(result.has_value()) << result.error();
41
42 EXPECT_THAT(result.value(),
43 ::testing::ElementsAre(std::vector<std::string>{"name"},
44 std::vector<std::string>{"test_event"}));
45 }
46
TEST_F(TestTraceProcessorExample,BasicTraceConfig)47 TEST_F(TestTraceProcessorExample, BasicTraceConfig) {
48 TestTraceProcessor test_trace_processor;
49
50 // Start tracing with a category filter string set in the trace config.
51 test_trace_processor.StartTrace(base::test::DefaultTraceConfig(
52 "test_category", /*privacy_filtering=*/false));
53
54 {
55 // A simple trace event inside of a scope so it gets flushed properly.
56 TRACE_EVENT("test_category", "test_event");
57 }
58
59 auto status = test_trace_processor.StopAndParseTrace();
60 ASSERT_TRUE(status.ok()) << status.message();
61
62 auto result = test_trace_processor.RunQuery(R"(
63 SELECT
64 name
65 FROM slice
66 )");
67 ASSERT_TRUE(result.has_value()) << result.error();
68
69 EXPECT_THAT(result.value(),
70 ::testing::ElementsAre(std::vector<std::string>{"name"},
71 std::vector<std::string>{"test_event"}));
72 }
73
74
75 } // namespace base::test
76