1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122

base / test / test_suite.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_TEST_TEST_SUITE_H_
#define BASE_TEST_TEST_SUITE_H_

// Defines a basic test suite framework for running gtest based tests.  You can
// instantiate this class in your main function and call its Run method to run
// any gtest based tests that are linked into your executable.

#include <memory>
#include <string_view>

#include "base/at_exit.h"
#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/tracing_buildflags.h"
#include "build/build_config.h"

#if BUILDFLAG(ENABLE_BASE_TRACING)
#include "base/test/trace_to_file.h"
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)

#if BUILDFLAG(IS_WIN)
#include <vector>
#include "base/memory/raw_ptr_exclusion.h"
#endif

namespace logging {
class ScopedLogAssertHandler;
}

namespace testing {
class TestInfo;
}

namespace base {

class XmlUnitTestResultPrinter;

// Instantiates TestSuite, runs it and returns exit code.
int RunUnitTestsUsingBaseTestSuite(int argc, char** argv);

class TestSuite {
 public:
  // Match function used by the GetTestCount method.
  typedef bool (*TestMatch)(const testing::TestInfo&);

  TestSuite(int argc, char** argv);
#if BUILDFLAG(IS_WIN)
  TestSuite(int argc, wchar_t** argv);
#endif  // BUILDFLAG(IS_WIN)

  TestSuite(const TestSuite&) = delete;
  TestSuite& operator=(const TestSuite&) = delete;

  virtual ~TestSuite();

  int Run();

  // Disables checks for thread and process priority at the beginning and end of
  // each test. Most tests should not use this.
  void DisableCheckForThreadAndProcessPriority();

  // Disables checks for certain global objects being leaked across tests.
  void DisableCheckForLeakedGlobals();

 protected:
  // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
  // which gum up buildbots. Use a minimalistic assert handler which just
  // terminates the process.
  void UnitTestAssertHandler(const char* file,
                             int line,
                             std::string_view summary,
                             std::string_view stack_trace);

  // Disable crash dialogs so that it doesn't gum up the buildbot
  virtual void SuppressErrorDialogs();

  // Override these for custom test handling. Use these instead of putting
  // complex code in your constructor/destructor.
  virtual void Initialize();
  virtual void InitializeFromCommandLine(int* argc, char** argv);
  virtual int RunAllTests();
  virtual void Shutdown();

  // Make sure that we setup an AtExitManager so Singleton objects will be
  // destroyed.
  std::unique_ptr<base::AtExitManager> at_exit_manager_;

 private:
  // Basic initialization for the test suite happens here.
  void PreInitialize();

  void AddTestLauncherResultPrinter();

#if BUILDFLAG(ENABLE_BASE_TRACING)
  test::TraceToFile trace_to_file_;
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)

  raw_ptr<XmlUnitTestResultPrinter, DanglingUntriaged> printer_ = nullptr;

  std::unique_ptr<logging::ScopedLogAssertHandler> assert_handler_;

  bool initialized_command_line_ = false;
  bool check_for_leaked_globals_ = true;
  bool check_for_thread_and_process_priority_ = true;
  bool is_initialized_ = false;
  int argc_;
#if BUILDFLAG(IS_WIN)
  // We need argv_as_pointers_.data() to have type char**, so we can't use
  // raw_ptr here.
  RAW_PTR_EXCLUSION std::vector<char*> argv_as_pointers_;
  std::vector<std::string> argv_as_strings_;
#endif
  raw_ptr<char*> argv_;
};

}  // namespace base

#endif  // BASE_TEST_TEST_SUITE_H_