xref: /aosp_15_r20/external/cronet/base/test/test_suite.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_TEST_SUITE_H_
6 #define BASE_TEST_TEST_SUITE_H_
7 
8 // Defines a basic test suite framework for running gtest based tests.  You can
9 // instantiate this class in your main function and call its Run method to run
10 // any gtest based tests that are linked into your executable.
11 
12 #include <memory>
13 #include <string_view>
14 
15 #include "base/at_exit.h"
16 #include "base/check.h"
17 #include "base/memory/raw_ptr.h"
18 #include "base/tracing_buildflags.h"
19 #include "build/build_config.h"
20 
21 #if BUILDFLAG(ENABLE_BASE_TRACING)
22 #include "base/test/trace_to_file.h"
23 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
24 
25 #if BUILDFLAG(IS_WIN)
26 #include <vector>
27 #include "base/memory/raw_ptr_exclusion.h"
28 #endif
29 
30 namespace logging {
31 class ScopedLogAssertHandler;
32 }
33 
34 namespace testing {
35 class TestInfo;
36 }
37 
38 namespace base {
39 
40 class XmlUnitTestResultPrinter;
41 
42 // Instantiates TestSuite, runs it and returns exit code.
43 int RunUnitTestsUsingBaseTestSuite(int argc, char** argv);
44 
45 class TestSuite {
46  public:
47   // Match function used by the GetTestCount method.
48   typedef bool (*TestMatch)(const testing::TestInfo&);
49 
50   TestSuite(int argc, char** argv);
51 #if BUILDFLAG(IS_WIN)
52   TestSuite(int argc, wchar_t** argv);
53 #endif  // BUILDFLAG(IS_WIN)
54 
55   TestSuite(const TestSuite&) = delete;
56   TestSuite& operator=(const TestSuite&) = delete;
57 
58   virtual ~TestSuite();
59 
60   int Run();
61 
62   // Disables checks for thread and process priority at the beginning and end of
63   // each test. Most tests should not use this.
64   void DisableCheckForThreadAndProcessPriority();
65 
66   // Disables checks for certain global objects being leaked across tests.
67   void DisableCheckForLeakedGlobals();
68 
69  protected:
70   // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
71   // which gum up buildbots. Use a minimalistic assert handler which just
72   // terminates the process.
73   void UnitTestAssertHandler(const char* file,
74                              int line,
75                              const std::string_view summary,
76                              const std::string_view stack_trace);
77 
78   // Disable crash dialogs so that it doesn't gum up the buildbot
79   virtual void SuppressErrorDialogs();
80 
81   // Override these for custom test handling. Use these instead of putting
82   // complex code in your constructor/destructor.
83   virtual void Initialize();
84   virtual void InitializeFromCommandLine(int* argc, char** argv);
85   virtual int RunAllTests();
86   virtual void Shutdown();
87 
88   // Make sure that we setup an AtExitManager so Singleton objects will be
89   // destroyed.
90   std::unique_ptr<base::AtExitManager> at_exit_manager_;
91 
92  private:
93   // Basic initialization for the test suite happens here.
94   void PreInitialize();
95 
96   void AddTestLauncherResultPrinter();
97 
98 #if BUILDFLAG(ENABLE_BASE_TRACING)
99   test::TraceToFile trace_to_file_;
100 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
101 
102   raw_ptr<XmlUnitTestResultPrinter, DanglingUntriaged> printer_ = nullptr;
103 
104   std::unique_ptr<logging::ScopedLogAssertHandler> assert_handler_;
105 
106   bool initialized_command_line_ = false;
107   bool check_for_leaked_globals_ = true;
108   bool check_for_thread_and_process_priority_ = true;
109   bool is_initialized_ = false;
110   int argc_;
111 #if BUILDFLAG(IS_WIN)
112   // We need argv_as_pointers_.data() to have type char**, so we can't use
113   // raw_ptr here.
114   RAW_PTR_EXCLUSION std::vector<char*> argv_as_pointers_;
115   std::vector<std::string> argv_as_strings_;
116 #endif
117   raw_ptr<char*> argv_;
118 };
119 
120 }  // namespace base
121 
122 #endif  // BASE_TEST_TEST_SUITE_H_
123