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. 4 5 #include "base/at_exit.h" 6 #include "base/bind.h" 7 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace { 11 12 int g_test_counter_1 = 0; 13 int g_test_counter_2 = 0; 14 15 void IncrementTestCounter1(void* unused) { 16 ++g_test_counter_1; 17 } 18 19 void IncrementTestCounter2(void* unused) { 20 ++g_test_counter_2; 21 } 22 23 void ZeroTestCounters() { 24 g_test_counter_1 = 0; 25 g_test_counter_2 = 0; 26 } 27 28 void ExpectCounter1IsZero(void* unused) { 29 EXPECT_EQ(0, g_test_counter_1); 30 } 31 32 void ExpectParamIsNull(void* param) { 33 EXPECT_EQ(static_cast<void*>(NULL), param); 34 } 35 36 void ExpectParamIsCounter(void* param) { 37 EXPECT_EQ(&g_test_counter_1, param); 38 } 39 40 } // namespace 41 42 class AtExitTest : public testing::Test { 43 private: 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_; 47 }; 48 49 TEST_F(AtExitTest, Basic) { 50 ZeroTestCounters(); 51 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); 52 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); 53 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); 54 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); 60 } 61 62 TEST_F(AtExitTest, LIFOOrder) { 63 ZeroTestCounters(); 64 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); 65 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); 66 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); 67 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); 73 } 74 75 TEST_F(AtExitTest, Param) { 76 base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL); 77 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, 78 &g_test_counter_1); 79 base::AtExitManager::ProcessCallbacksNow(); 80 } 81 82 TEST_F(AtExitTest, Task) { 83 ZeroTestCounters(); 84 base::AtExitManager::RegisterTask(base::Bind(&ExpectParamIsCounter, 85 &g_test_counter_1)); 86 base::AtExitManager::ProcessCallbacksNow(); 87 } 88