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