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 | 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. | 12 // AtExit callbacks can ruin the global state that other tests may depend on. |
13 class ShadowingAtExitManager : public base::AtExitManager { | 13 class ShadowingAtExitManager : public base::AtExitManager { |
14 public: | 14 public: |
15 ShadowingAtExitManager() : AtExitManager(true) {} | 15 ShadowingAtExitManager() : AtExitManager(true) {} |
16 }; | 16 }; |
17 | 17 |
18 int g_test_counter_1 = 0; | 18 int g_test_counter_1 = 0; |
19 int g_test_counter_2 = 0; | 19 int g_test_counter_2 = 0; |
20 | 20 |
21 void IncrementTestCounter1() { | 21 void IncrementTestCounter1(void* unused) { |
22 ++g_test_counter_1; | 22 ++g_test_counter_1; |
23 } | 23 } |
24 | 24 |
25 void IncrementTestCounter2() { | 25 void IncrementTestCounter2(void* unused) { |
26 ++g_test_counter_2; | 26 ++g_test_counter_2; |
27 } | 27 } |
28 | 28 |
29 void ZeroTestCounters() { | 29 void ZeroTestCounters() { |
30 g_test_counter_1 = 0; | 30 g_test_counter_1 = 0; |
31 g_test_counter_2 = 0; | 31 g_test_counter_2 = 0; |
32 } | 32 } |
33 | 33 |
34 void ExpectCounter1IsZero() { | 34 void ExpectCounter1IsZero(void* unused) { |
35 EXPECT_EQ(0, g_test_counter_1); | 35 EXPECT_EQ(0, g_test_counter_1); |
36 } | 36 } |
37 | 37 |
| 38 void ExpectParamIsNull(void* param) { |
| 39 EXPECT_EQ(static_cast<void*>(NULL), param); |
| 40 } |
| 41 |
| 42 void ExpectParamIsCounter(void* param) { |
| 43 EXPECT_EQ(&g_test_counter_1, param); |
| 44 } |
| 45 |
38 } // namespace | 46 } // namespace |
39 | 47 |
40 TEST(AtExitTest, Basic) { | 48 TEST(AtExitTest, Basic) { |
41 ShadowingAtExitManager shadowing_at_exit_manager; | 49 ShadowingAtExitManager shadowing_at_exit_manager; |
42 | 50 |
43 ZeroTestCounters(); | 51 ZeroTestCounters(); |
44 base::AtExitManager::RegisterCallback(&IncrementTestCounter1); | 52 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
45 base::AtExitManager::RegisterCallback(&IncrementTestCounter2); | 53 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
46 base::AtExitManager::RegisterCallback(&IncrementTestCounter1); | 54 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
47 | 55 |
48 EXPECT_EQ(0, g_test_counter_1); | 56 EXPECT_EQ(0, g_test_counter_1); |
49 EXPECT_EQ(0, g_test_counter_2); | 57 EXPECT_EQ(0, g_test_counter_2); |
50 base::AtExitManager::ProcessCallbacksNow(); | 58 base::AtExitManager::ProcessCallbacksNow(); |
51 EXPECT_EQ(2, g_test_counter_1); | 59 EXPECT_EQ(2, g_test_counter_1); |
52 EXPECT_EQ(1, g_test_counter_2); | 60 EXPECT_EQ(1, g_test_counter_2); |
53 } | 61 } |
54 | 62 |
55 TEST(AtExitTest, LIFOOrder) { | 63 TEST(AtExitTest, LIFOOrder) { |
56 ShadowingAtExitManager shadowing_at_exit_manager; | 64 ShadowingAtExitManager shadowing_at_exit_manager; |
57 | 65 |
58 ZeroTestCounters(); | 66 ZeroTestCounters(); |
59 base::AtExitManager::RegisterCallback(&IncrementTestCounter1); | 67 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
60 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero); | 68 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); |
61 base::AtExitManager::RegisterCallback(&IncrementTestCounter2); | 69 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
62 | 70 |
63 EXPECT_EQ(0, g_test_counter_1); | 71 EXPECT_EQ(0, g_test_counter_1); |
64 EXPECT_EQ(0, g_test_counter_2); | 72 EXPECT_EQ(0, g_test_counter_2); |
65 base::AtExitManager::ProcessCallbacksNow(); | 73 base::AtExitManager::ProcessCallbacksNow(); |
66 EXPECT_EQ(1, g_test_counter_1); | 74 EXPECT_EQ(1, g_test_counter_1); |
67 EXPECT_EQ(1, g_test_counter_2); | 75 EXPECT_EQ(1, g_test_counter_2); |
68 } | 76 } |
69 | 77 |
| 78 TEST(AtExitTest, Param) { |
| 79 ShadowingAtExitManager shadowing_at_exit_manager; |
| 80 |
| 81 base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL); |
| 82 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, |
| 83 &g_test_counter_1); |
| 84 base::AtExitManager::ProcessCallbacksNow(); |
| 85 } |
OLD | NEW |