Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / base / at_exit_unittest.cc
blobcda73403fb04af6f08d2d1951614f1de6e1cd612
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"
6 #include "base/bind.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace {
12 int g_test_counter_1 = 0;
13 int g_test_counter_2 = 0;
15 void IncrementTestCounter1(void* unused) {
16 ++g_test_counter_1;
19 void IncrementTestCounter2(void* unused) {
20 ++g_test_counter_2;
23 void ZeroTestCounters() {
24 g_test_counter_1 = 0;
25 g_test_counter_2 = 0;
28 void ExpectCounter1IsZero(void* unused) {
29 EXPECT_EQ(0, g_test_counter_1);
32 void ExpectParamIsNull(void* param) {
33 EXPECT_EQ(static_cast<void*>(NULL), param);
36 void ExpectParamIsCounter(void* param) {
37 EXPECT_EQ(&g_test_counter_1, param);
40 } // namespace
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_;
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);
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) {
63 ZeroTestCounters();
64 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL);
65 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL);
66 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL);
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, NULL);
77 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter,
78 &g_test_counter_1);
79 base::AtExitManager::ProcessCallbacksNow();
82 TEST_F(AtExitTest, Task) {
83 ZeroTestCounters();
84 base::AtExitManager::RegisterTask(base::Bind(&ExpectParamIsCounter,
85 &g_test_counter_1));
86 base::AtExitManager::ProcessCallbacksNow();