apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 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" |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 6 | #include "base/bind.h" |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 7 | |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 12 | int g_test_counter_1 = 0; |
| 13 | int g_test_counter_2 = 0; |
| 14 | |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 15 | void IncrementTestCounter1(void* unused) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 16 | ++g_test_counter_1; |
| 17 | } |
| 18 | |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 19 | void IncrementTestCounter2(void* unused) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 20 | ++g_test_counter_2; |
| 21 | } |
| 22 | |
| 23 | void ZeroTestCounters() { |
| 24 | g_test_counter_1 = 0; |
| 25 | g_test_counter_2 = 0; |
| 26 | } |
| 27 | |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 28 | void ExpectCounter1IsZero(void* unused) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 29 | EXPECT_EQ(0, g_test_counter_1); |
| 30 | } |
| 31 | |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 32 | void ExpectParamIsNull(void* param) { |
| 33 | EXPECT_EQ(static_cast<void*>(NULL), param); |
| 34 | } |
| 35 | |
| 36 | void ExpectParamIsCounter(void* param) { |
| 37 | EXPECT_EQ(&g_test_counter_1, param); |
| 38 | } |
| 39 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 40 | } // namespace |
| 41 | |
phajdan.jr@chromium.org | e7f133a | 2009-11-19 18:11:39 +0900 | [diff] [blame] | 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_; |
| 47 | }; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 48 | |
phajdan.jr@chromium.org | e7f133a | 2009-11-19 18:11:39 +0900 | [diff] [blame] | 49 | TEST_F(AtExitTest, Basic) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 50 | ZeroTestCounters(); |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 51 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
| 52 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
| 53 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 54 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 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); |
| 60 | } |
| 61 | |
phajdan.jr@chromium.org | e7f133a | 2009-11-19 18:11:39 +0900 | [diff] [blame] | 62 | TEST_F(AtExitTest, LIFOOrder) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 63 | ZeroTestCounters(); |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 64 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL); |
| 65 | base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL); |
| 66 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL); |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 67 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 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); |
| 73 | } |
| 74 | |
phajdan.jr@chromium.org | e7f133a | 2009-11-19 18:11:39 +0900 | [diff] [blame] | 75 | TEST_F(AtExitTest, Param) { |
deanm@google.com | f6299e7 | 2008-09-08 18:06:51 +0900 | [diff] [blame] | 76 | base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL); |
| 77 | base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, |
| 78 | &g_test_counter_1); |
| 79 | base::AtExitManager::ProcessCallbacksNow(); |
| 80 | } |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 81 | |
| 82 | TEST_F(AtExitTest, Task) { |
| 83 | ZeroTestCounters(); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 84 | base::AtExitManager::RegisterTask(base::Bind(&ExpectParamIsCounter, |
apatrick@chromium.org | 035f4af | 2011-09-07 08:14:47 +0900 | [diff] [blame] | 85 | &g_test_counter_1)); |
| 86 | base::AtExitManager::ProcessCallbacksNow(); |
| 87 | } |