OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/at_exit.h" | 5 #include "base/at_exit.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
11 // Don't test the global AtExitManager, because asking it to process its | |
12 // AtExit callbacks can ruin the global state that other tests may depend on. | |
13 class ShadowingAtExitManager : public base::AtExitManager { | |
14 public: | |
15 ShadowingAtExitManager() : AtExitManager(true) {} | |
16 }; | |
17 | |
18 int g_test_counter_1 = 0; | 11 int g_test_counter_1 = 0; |
19 int g_test_counter_2 = 0; | 12 int g_test_counter_2 = 0; |
20 | 13 |
21 void IncrementTestCounter1(void* unused) { | 14 void IncrementTestCounter1(void* unused) { |
22 ++g_test_counter_1; | 15 ++g_test_counter_1; |
23 } | 16 } |
24 | 17 |
25 void IncrementTestCounter2(void* unused) { | 18 void IncrementTestCounter2(void* unused) { |
26 ++g_test_counter_2; | 19 ++g_test_counter_2; |
27 } | 20 } |
(...skipping 10 matching lines...) Expand all Loading... |
38 void ExpectParamIsNull(void* param) { | 31 void ExpectParamIsNull(void* param) { |
39 EXPECT_EQ(static_cast<void*>(NULL), param); | 32 EXPECT_EQ(static_cast<void*>(NULL), param); |
40 } | 33 } |
41 | 34 |
42 void ExpectParamIsCounter(void* param) { | 35 void ExpectParamIsCounter(void* param) { |
43 EXPECT_EQ(&g_test_counter_1, param); | 36 EXPECT_EQ(&g_test_counter_1, param); |
44 } | 37 } |
45 | 38 |
46 } // namespace | 39 } // namespace |
47 | 40 |
48 TEST(AtExitTest, Basic) { | 41 class AtExitTest : public testing::Test { |
49 ShadowingAtExitManager shadowing_at_exit_manager; | 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 }; |
50 | 47 |
| 48 TEST_F(AtExitTest, Basic) { |
51 ZeroTestCounters(); | 49 ZeroTestCounters(); |
52 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); | 50 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
53 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); | 51 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
54 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); | 52 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
55 | 53 |
56 EXPECT_EQ(0, g_test_counter_1); | 54 EXPECT_EQ(0, g_test_counter_1); |
57 EXPECT_EQ(0, g_test_counter_2); | 55 EXPECT_EQ(0, g_test_counter_2); |
58 base::AtExitManager::ProcessCallbacksNow(); | 56 base::AtExitManager::ProcessCallbacksNow(); |
59 EXPECT_EQ(2, g_test_counter_1); | 57 EXPECT_EQ(2, g_test_counter_1); |
60 EXPECT_EQ(1, g_test_counter_2); | 58 EXPECT_EQ(1, g_test_counter_2); |
61 } | 59 } |
62 | 60 |
63 TEST(AtExitTest, LIFOOrder) { | 61 TEST_F(AtExitTest, LIFOOrder) { |
64 ShadowingAtExitManager shadowing_at_exit_manager; | |
65 | |
66 ZeroTestCounters(); | 62 ZeroTestCounters(); |
67 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); | 63 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
68 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); | 64 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); |
69 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); | 65 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
70 | 66 |
71 EXPECT_EQ(0, g_test_counter_1); | 67 EXPECT_EQ(0, g_test_counter_1); |
72 EXPECT_EQ(0, g_test_counter_2); | 68 EXPECT_EQ(0, g_test_counter_2); |
73 base::AtExitManager::ProcessCallbacksNow(); | 69 base::AtExitManager::ProcessCallbacksNow(); |
74 EXPECT_EQ(1, g_test_counter_1); | 70 EXPECT_EQ(1, g_test_counter_1); |
75 EXPECT_EQ(1, g_test_counter_2); | 71 EXPECT_EQ(1, g_test_counter_2); |
76 } | 72 } |
77 | 73 |
78 TEST(AtExitTest, Param) { | 74 TEST_F(AtExitTest, Param) { |
79 ShadowingAtExitManager shadowing_at_exit_manager; | |
80 | |
81 base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL); | 75 base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL); |
82 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, | 76 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, |
83 &g_test_counter_1); | 77 &g_test_counter_1); |
84 base::AtExitManager::ProcessCallbacksNow(); | 78 base::AtExitManager::ProcessCallbacksNow(); |
85 } | 79 } |
OLD | NEW |