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 #include "base/functional/bind.h"
6 #include "base/metrics/persistent_histogram_allocator.h"
7 #include "base/test/launcher/unit_test_launcher.h"
8 #include "base/test/test_suite.h"
9 #include "base/time/time.h"
10 #include "build/build_config.h"
11
12 #if BUILDFLAG(IS_WIN)
13 #include "base/win/com_init_util.h"
14 #endif // BUILDFLAG(IS_WIN)
15
16 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
17 #include "base/process/set_process_title_linux.h"
18 #endif
19
20 namespace base {
21
22 namespace {
23
24 #if BUILDFLAG(IS_WIN)
25 class ComLeakCheck : public testing::EmptyTestEventListener {
26 public:
OnTestEnd(const testing::TestInfo & test)27 void OnTestEnd(const testing::TestInfo& test) override {
28 // Verify that COM has been reset to defaults by the test.
29 EXPECT_EQ(win::GetComApartmentTypeForThread(), win::ComApartmentType::NONE);
30 }
31 };
32
33 class HistogramAllocatorCheck : public testing::EmptyTestEventListener {
34 public:
OnTestEnd(const testing::TestInfo & test)35 void OnTestEnd(const testing::TestInfo& test) override {
36 // Verify that the histogram allocator was released by the test.
37 CHECK(!GlobalHistogramAllocator::Get());
38 }
39 };
40
41 class TimerCheck : public testing::EmptyTestEventListener {
42 public:
OnTestEnd(const testing::TestInfo & test_info)43 void OnTestEnd(const testing::TestInfo& test_info) override {
44 EXPECT_FALSE(Time::IsHighResolutionTimerInUse());
45 }
46 };
47 #endif // BUILDFLAG(IS_WIN)
48
49 class BaseUnittestSuite : public TestSuite {
50 public:
BaseUnittestSuite(int argc,char ** argv)51 BaseUnittestSuite(int argc, char** argv) : TestSuite(argc, argv) {}
52
53 protected:
Initialize()54 void Initialize() override {
55 TestSuite::Initialize();
56
57 #if BUILDFLAG(IS_WIN)
58 // Add TestEventListeners to enforce certain properties across tests.
59 testing::TestEventListeners& listeners =
60 testing::UnitTest::GetInstance()->listeners();
61 listeners.Append(new ComLeakCheck);
62 listeners.Append(new HistogramAllocatorCheck);
63 listeners.Append(new TimerCheck);
64 #endif // BUILDFLAG(IS_WIN)
65 }
66 };
67
68 } // namespace
69
70 } // namespace base
71
main(int argc,char ** argv)72 int main(int argc, char** argv) {
73 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
74 // For setproctitle unit tests.
75 setproctitle_init(const_cast<const char**>(argv));
76 #endif
77
78 base::BaseUnittestSuite test_suite(argc, argv);
79 return base::LaunchUnitTests(
80 argc, argv,
81 base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
82 }
83