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 namespace base {
17
18 namespace {
19
20 #if BUILDFLAG(IS_WIN)
21 class ComLeakCheck : public testing::EmptyTestEventListener {
22 public:
OnTestEnd(const testing::TestInfo & test)23 void OnTestEnd(const testing::TestInfo& test) override {
24 // Verify that COM has been reset to defaults by the test.
25 EXPECT_EQ(win::GetComApartmentTypeForThread(), win::ComApartmentType::NONE);
26 }
27 };
28
29 class HistogramAllocatorCheck : public testing::EmptyTestEventListener {
30 public:
OnTestEnd(const testing::TestInfo & test)31 void OnTestEnd(const testing::TestInfo& test) override {
32 // Verify that the histogram allocator was released by the test.
33 CHECK(!GlobalHistogramAllocator::Get());
34 }
35 };
36
37 class TimerCheck : public testing::EmptyTestEventListener {
38 public:
OnTestEnd(const testing::TestInfo & test_info)39 void OnTestEnd(const testing::TestInfo& test_info) override {
40 EXPECT_FALSE(Time::IsHighResolutionTimerInUse());
41 }
42 };
43 #endif // BUILDFLAG(IS_WIN)
44
45 class BaseUnittestSuite : public TestSuite {
46 public:
BaseUnittestSuite(int argc,char ** argv)47 BaseUnittestSuite(int argc, char** argv) : TestSuite(argc, argv) {}
48
49 protected:
Initialize()50 void Initialize() override {
51 TestSuite::Initialize();
52
53 #if BUILDFLAG(IS_WIN)
54 // Add TestEventListeners to enforce certain properties across tests.
55 testing::TestEventListeners& listeners =
56 testing::UnitTest::GetInstance()->listeners();
57 listeners.Append(new ComLeakCheck);
58 listeners.Append(new HistogramAllocatorCheck);
59 listeners.Append(new TimerCheck);
60 #endif // BUILDFLAG(IS_WIN)
61 }
62 };
63
64 } // namespace
65
66 } // namespace base
67
main(int argc,char ** argv)68 int main(int argc, char** argv) {
69 base::BaseUnittestSuite test_suite(argc, argv);
70 return base::LaunchUnitTests(
71 argc, argv,
72 base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
73 }
74