xref: /aosp_15_r20/external/libchrome/base/test/test_suite.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TEST_TEST_SUITE_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_TEST_TEST_SUITE_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker // Defines a basic test suite framework for running gtest based tests.  You can
9*635a8641SAndroid Build Coastguard Worker // instantiate this class in your main function and call its Run method to run
10*635a8641SAndroid Build Coastguard Worker // any gtest based tests that are linked into your executable.
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include <memory>
13*635a8641SAndroid Build Coastguard Worker #include <string>
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker #include "base/at_exit.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/test/scoped_feature_list.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/test/trace_to_file.h"
20*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace testing {
23*635a8641SAndroid Build Coastguard Worker class TestInfo;
24*635a8641SAndroid Build Coastguard Worker }
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker namespace base {
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker class XmlUnitTestResultPrinter;
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker // Instantiates TestSuite, runs it and returns exit code.
31*635a8641SAndroid Build Coastguard Worker int RunUnitTestsUsingBaseTestSuite(int argc, char **argv);
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker class TestSuite {
34*635a8641SAndroid Build Coastguard Worker  public:
35*635a8641SAndroid Build Coastguard Worker   // Match function used by the GetTestCount method.
36*635a8641SAndroid Build Coastguard Worker   typedef bool (*TestMatch)(const testing::TestInfo&);
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker   TestSuite(int argc, char** argv);
39*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
40*635a8641SAndroid Build Coastguard Worker   TestSuite(int argc, wchar_t** argv);
41*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
42*635a8641SAndroid Build Coastguard Worker   virtual ~TestSuite();
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker   // Returns true if the test is marked as "MAYBE_".
45*635a8641SAndroid Build Coastguard Worker   // When using different prefixes depending on platform, we use MAYBE_ and
46*635a8641SAndroid Build Coastguard Worker   // preprocessor directives to replace MAYBE_ with the target prefix.
47*635a8641SAndroid Build Coastguard Worker   static bool IsMarkedMaybe(const testing::TestInfo& test);
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker   void CatchMaybeTests();
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker   void ResetCommandLine();
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker   void AddTestLauncherResultPrinter();
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker   int Run();
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker  protected:
58*635a8641SAndroid Build Coastguard Worker   // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
59*635a8641SAndroid Build Coastguard Worker   // which gum up buildbots. Use a minimalistic assert handler which just
60*635a8641SAndroid Build Coastguard Worker   // terminates the process.
61*635a8641SAndroid Build Coastguard Worker   void UnitTestAssertHandler(const char* file,
62*635a8641SAndroid Build Coastguard Worker                              int line,
63*635a8641SAndroid Build Coastguard Worker                              const base::StringPiece summary,
64*635a8641SAndroid Build Coastguard Worker                              const base::StringPiece stack_trace);
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker   // Disable crash dialogs so that it doesn't gum up the buildbot
67*635a8641SAndroid Build Coastguard Worker   virtual void SuppressErrorDialogs();
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker   // Override these for custom initialization and shutdown handling.  Use these
70*635a8641SAndroid Build Coastguard Worker   // instead of putting complex code in your constructor/destructor.
71*635a8641SAndroid Build Coastguard Worker 
72*635a8641SAndroid Build Coastguard Worker   virtual void Initialize();
73*635a8641SAndroid Build Coastguard Worker   virtual void Shutdown();
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker   // Make sure that we setup an AtExitManager so Singleton objects will be
76*635a8641SAndroid Build Coastguard Worker   // destroyed.
77*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<base::AtExitManager> at_exit_manager_;
78*635a8641SAndroid Build Coastguard Worker 
79*635a8641SAndroid Build Coastguard Worker  private:
80*635a8641SAndroid Build Coastguard Worker   void InitializeFromCommandLine(int argc, char** argv);
81*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
82*635a8641SAndroid Build Coastguard Worker   void InitializeFromCommandLine(int argc, wchar_t** argv);
83*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker   // Basic initialization for the test suite happens here.
86*635a8641SAndroid Build Coastguard Worker   void PreInitialize();
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker   test::TraceToFile trace_to_file_;
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker   bool initialized_command_line_;
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker   test::ScopedFeatureList scoped_feature_list_;
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker   XmlUnitTestResultPrinter* printer_ = nullptr;
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<logging::ScopedLogAssertHandler> assert_handler_;
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(TestSuite);
99*635a8641SAndroid Build Coastguard Worker };
100*635a8641SAndroid Build Coastguard Worker 
101*635a8641SAndroid Build Coastguard Worker }  // namespace base
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker #endif  // BASE_TEST_TEST_SUITE_H_
104