1 // Copyright (c) 2011 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.
5 #include "base/at_exit.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 int g_test_counter_1 = 0;
13 int g_test_counter_2 = 0;
15 void IncrementTestCounter1(void* unused) {
19 void IncrementTestCounter2(void* unused) {
23 void ZeroTestCounters() {
28 void ExpectCounter1IsZero(void* unused) {
29 EXPECT_EQ(0, g_test_counter_1);
32 void ExpectParamIsNull(void* param) {
33 EXPECT_EQ(nullptr, param);
36 void ExpectParamIsCounter(void* param) {
37 EXPECT_EQ(&g_test_counter_1, param);
42 class AtExitTest : public testing::Test {
44 // Don't test the global AtExitManager, because asking it to process its
45 // AtExit callbacks can ruin the global state that other tests may depend on.
46 base::ShadowingAtExitManager exit_manager_;
49 TEST_F(AtExitTest, Basic) {
51 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, nullptr);
52 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, nullptr);
53 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, nullptr);
55 EXPECT_EQ(0, g_test_counter_1);
56 EXPECT_EQ(0, g_test_counter_2);
57 base::AtExitManager::ProcessCallbacksNow();
58 EXPECT_EQ(2, g_test_counter_1);
59 EXPECT_EQ(1, g_test_counter_2);
62 TEST_F(AtExitTest, LIFOOrder) {
64 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, nullptr);
65 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, nullptr);
66 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, nullptr);
68 EXPECT_EQ(0, g_test_counter_1);
69 EXPECT_EQ(0, g_test_counter_2);
70 base::AtExitManager::ProcessCallbacksNow();
71 EXPECT_EQ(1, g_test_counter_1);
72 EXPECT_EQ(1, g_test_counter_2);
75 TEST_F(AtExitTest, Param) {
76 base::AtExitManager::RegisterCallback(&ExpectParamIsNull, nullptr);
77 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter,
79 base::AtExitManager::ProcessCallbacksNow();
82 TEST_F(AtExitTest, Task) {
84 base::AtExitManager::RegisterTask(base::Bind(&ExpectParamIsCounter,
86 base::AtExitManager::ProcessCallbacksNow();