run_all_unittests.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/bind.h"
  5. #include "base/metrics/persistent_histogram_allocator.h"
  6. #include "base/test/launcher/unit_test_launcher.h"
  7. #include "base/test/test_suite.h"
  8. #include "base/time/time.h"
  9. #include "build/build_config.h"
  10. #if BUILDFLAG(IS_WIN)
  11. #include "base/win/com_init_util.h"
  12. #endif // BUILDFLAG(IS_WIN)
  13. namespace base {
  14. namespace {
  15. #if BUILDFLAG(IS_WIN)
  16. class ComLeakCheck : public testing::EmptyTestEventListener {
  17. public:
  18. void OnTestEnd(const testing::TestInfo& test) override {
  19. // Verify that COM has been reset to defaults by the test.
  20. EXPECT_EQ(win::GetComApartmentTypeForThread(), win::ComApartmentType::NONE);
  21. }
  22. };
  23. class HistogramAllocatorCheck : public testing::EmptyTestEventListener {
  24. public:
  25. void OnTestEnd(const testing::TestInfo& test) override {
  26. // Verify that the histogram allocator was released by the test.
  27. CHECK(!GlobalHistogramAllocator::Get());
  28. }
  29. };
  30. class TimerCheck : public testing::EmptyTestEventListener {
  31. public:
  32. void OnTestEnd(const testing::TestInfo& test_info) override {
  33. EXPECT_FALSE(Time::IsHighResolutionTimerInUse());
  34. }
  35. };
  36. #endif // BUILDFLAG(IS_WIN)
  37. class BaseUnittestSuite : public TestSuite {
  38. public:
  39. BaseUnittestSuite(int argc, char** argv) : TestSuite(argc, argv) {}
  40. protected:
  41. void Initialize() override {
  42. TestSuite::Initialize();
  43. #if BUILDFLAG(IS_WIN)
  44. // Add TestEventListeners to enforce certain properties across tests.
  45. testing::TestEventListeners& listeners =
  46. testing::UnitTest::GetInstance()->listeners();
  47. listeners.Append(new ComLeakCheck);
  48. listeners.Append(new HistogramAllocatorCheck);
  49. listeners.Append(new TimerCheck);
  50. #endif // BUILDFLAG(IS_WIN)
  51. }
  52. };
  53. } // namespace
  54. } // namespace base
  55. int main(int argc, char** argv) {
  56. base::BaseUnittestSuite test_suite(argc, argv);
  57. return base::LaunchUnitTests(
  58. argc, argv,
  59. base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
  60. }