[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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 | // 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; |
| 19 | int g_test_counter_2 = 0; |
| 20 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 21 | void IncrementTestCounter1(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 22 | ++g_test_counter_1; |
| 23 | } |
| 24 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 25 | void IncrementTestCounter2(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 26 | ++g_test_counter_2; |
| 27 | } |
| 28 | |
| 29 | void ZeroTestCounters() { |
| 30 | g_test_counter_1 = 0; |
| 31 | g_test_counter_2 = 0; |
| 32 | } |
| 33 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 34 | void ExpectCounter1IsZero(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 35 | EXPECT_EQ(0, g_test_counter_1); |
| 36 | } |
| 37 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 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 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 46 | } // namespace |
| 47 | |
| 48 | TEST(AtExitTest, Basic) { |
| 49 | ShadowingAtExitManager shadowing_at_exit_manager; |
| 50 | |
| 51 | ZeroTestCounters(); |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 52 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
| 53 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
| 54 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame^] | 55 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 56 | EXPECT_EQ(0, g_test_counter_1); |
| 57 | EXPECT_EQ(0, g_test_counter_2); |
| 58 | base::AtExitManager::ProcessCallbacksNow(); |
| 59 | EXPECT_EQ(2, g_test_counter_1); |
| 60 | EXPECT_EQ(1, g_test_counter_2); |
| 61 | } |
| 62 | |
| 63 | TEST(AtExitTest, LIFOOrder) { |
| 64 | ShadowingAtExitManager shadowing_at_exit_manager; |
| 65 | |
| 66 | ZeroTestCounters(); |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 67 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
| 68 | base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); |
| 69 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame^] | 70 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 71 | EXPECT_EQ(0, g_test_counter_1); |
| 72 | EXPECT_EQ(0, g_test_counter_2); |
| 73 | base::AtExitManager::ProcessCallbacksNow(); |
| 74 | EXPECT_EQ(1, g_test_counter_1); |
| 75 | EXPECT_EQ(1, g_test_counter_2); |
| 76 | } |
| 77 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 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 | } |