1 // Copyright (c) 2006-2008 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"
7 #include "testing/gtest/include/gtest/gtest.h"
11 int g_test_counter_1
= 0;
12 int g_test_counter_2
= 0;
14 void IncrementTestCounter1(void* unused
) {
18 void IncrementTestCounter2(void* unused
) {
22 void ZeroTestCounters() {
27 void ExpectCounter1IsZero(void* unused
) {
28 EXPECT_EQ(0, g_test_counter_1
);
31 void ExpectParamIsNull(void* param
) {
32 EXPECT_EQ(static_cast<void*>(NULL
), param
);
35 void ExpectParamIsCounter(void* param
) {
36 EXPECT_EQ(&g_test_counter_1
, param
);
41 class AtExitTest
: public testing::Test
{
43 // Don't test the global AtExitManager, because asking it to process its
44 // AtExit callbacks can ruin the global state that other tests may depend on.
45 base::ShadowingAtExitManager exit_manager_
;
48 TEST_F(AtExitTest
, Basic
) {
50 base::AtExitManager::RegisterCallback(&IncrementTestCounter1
, NULL
);
51 base::AtExitManager::RegisterCallback(&IncrementTestCounter2
, NULL
);
52 base::AtExitManager::RegisterCallback(&IncrementTestCounter1
, NULL
);
54 EXPECT_EQ(0, g_test_counter_1
);
55 EXPECT_EQ(0, g_test_counter_2
);
56 base::AtExitManager::ProcessCallbacksNow();
57 EXPECT_EQ(2, g_test_counter_1
);
58 EXPECT_EQ(1, g_test_counter_2
);
61 TEST_F(AtExitTest
, LIFOOrder
) {
63 base::AtExitManager::RegisterCallback(&IncrementTestCounter1
, NULL
);
64 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero
, NULL
);
65 base::AtExitManager::RegisterCallback(&IncrementTestCounter2
, NULL
);
67 EXPECT_EQ(0, g_test_counter_1
);
68 EXPECT_EQ(0, g_test_counter_2
);
69 base::AtExitManager::ProcessCallbacksNow();
70 EXPECT_EQ(1, g_test_counter_1
);
71 EXPECT_EQ(1, g_test_counter_2
);
74 TEST_F(AtExitTest
, Param
) {
75 base::AtExitManager::RegisterCallback(&ExpectParamIsNull
, NULL
);
76 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter
,
78 base::AtExitManager::ProcessCallbacksNow();