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