[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 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 11 | int g_test_counter_1 = 0; |
| 12 | int g_test_counter_2 = 0; |
| 13 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 14 | void IncrementTestCounter1(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 15 | ++g_test_counter_1; |
| 16 | } |
| 17 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 18 | void IncrementTestCounter2(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 19 | ++g_test_counter_2; |
| 20 | } |
| 21 | |
| 22 | void ZeroTestCounters() { |
| 23 | g_test_counter_1 = 0; |
| 24 | g_test_counter_2 = 0; |
| 25 | } |
| 26 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 27 | void ExpectCounter1IsZero(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 28 | EXPECT_EQ(0, g_test_counter_1); |
| 29 | } |
| 30 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 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 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 39 | } // namespace |
| 40 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 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 | }; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 47 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 48 | TEST_F(AtExitTest, Basic) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 49 | ZeroTestCounters(); |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 50 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
| 51 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
| 52 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 53 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 61 | TEST_F(AtExitTest, LIFOOrder) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 62 | ZeroTestCounters(); |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 63 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
| 64 | base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); |
| 65 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 66 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 74 | TEST_F(AtExitTest, Param) { |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 75 | base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL); |
| 76 | base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, |
| 77 | &g_test_counter_1); |
| 78 | base::AtExitManager::ProcessCallbacksNow(); |
| 79 | } |