Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [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" |
| 6 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 12 | int g_test_counter_1 = 0; |
| 13 | int g_test_counter_2 = 0; |
| 14 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 15 | void IncrementTestCounter1(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 16 | ++g_test_counter_1; |
| 17 | } |
| 18 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 19 | void IncrementTestCounter2(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [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 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 28 | void ExpectCounter1IsZero(void* unused) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 29 | EXPECT_EQ(0, g_test_counter_1); |
| 30 | } |
| 31 | |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 32 | void ExpectParamIsNull(void* param) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 33 | EXPECT_EQ(nullptr, param); |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void ExpectParamIsCounter(void* param) { |
| 37 | EXPECT_EQ(&g_test_counter_1, param); |
| 38 | } |
| 39 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 40 | } // namespace |
| 41 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [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 | }; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 48 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 49 | TEST_F(AtExitTest, Basic) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 50 | ZeroTestCounters(); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 51 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, nullptr); |
| 52 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, nullptr); |
| 53 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, nullptr); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 54 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [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 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 62 | TEST_F(AtExitTest, LIFOOrder) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 63 | ZeroTestCounters(); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 64 | base::AtExitManager::RegisterCallback(&IncrementTestCounter1, nullptr); |
| 65 | base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, nullptr); |
| 66 | base::AtExitManager::RegisterCallback(&IncrementTestCounter2, nullptr); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 67 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [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 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 75 | TEST_F(AtExitTest, Param) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 76 | base::AtExitManager::RegisterCallback(&ExpectParamIsNull, nullptr); |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 77 | base::AtExitManager::RegisterCallback(&ExpectParamIsCounter, |
| 78 | &g_test_counter_1); |
| 79 | base::AtExitManager::ProcessCallbacksNow(); |
| 80 | } |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 81 | |
| 82 | TEST_F(AtExitTest, Task) { |
| 83 | ZeroTestCounters(); |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 84 | base::AtExitManager::RegisterTask( |
| 85 | base::BindOnce(&ExpectParamIsCounter, &g_test_counter_1)); |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 86 | base::AtExitManager::ProcessCallbacksNow(); |
| 87 | } |