xref: /aosp_15_r20/external/cronet/base/test/trace_test_utils.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 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 #ifndef BASE_TEST_TRACE_TEST_UTILS_H_
6 #define BASE_TEST_TRACE_TEST_UTILS_H_
7 
8 #include "base/memory/raw_ptr.h"
9 #include "base/task/thread_pool.h"
10 #include "base/test/task_environment.h"
11 #include "base/trace_event/trace_log.h"
12 #include "third_party/perfetto/protos/perfetto/config/trace_config.gen.h"
13 
14 namespace base {
15 namespace tracing {
16 class PerfettoPlatform;
17 }
18 
19 namespace test {
20 
21 // A scoped class that sets up and tears down tracing support for unit tests.
22 // Note that only in-process tracing is supported by this harness. See
23 // //services/tracing for recording traces in multiprocess configurations.
24 class TracingEnvironment {
25  public:
26   // Construct a tracing environment using the default Perfetto tracing
27   // platform.
28   TracingEnvironment();
29 
30   // Constructs a tracing environment with the given task runner and Perfetto
31   // tracing platform.
32   explicit TracingEnvironment(TaskEnvironment&,
33                               scoped_refptr<SequencedTaskRunner> =
34                                   ThreadPool::CreateSequencedTaskRunner({}),
35                               base::tracing::PerfettoPlatform* = nullptr);
36   ~TracingEnvironment();
37 
38   // Builds a default Perfetto trace config with track events enabled.
39   static perfetto::protos::gen::TraceConfig GetDefaultTraceConfig();
40 
41  private:
42   raw_ptr<TaskEnvironment> task_environment_ = nullptr;
43 };
44 
45 }  // namespace test
46 }  // namespace base
47 
48 #endif  // BASE_TEST_TRACE_TEST_UTILS_H_
49