blob: cda73403fb04af6f08d2d1951614f1de6e1cd612 [file] [log] [blame]
apatrick@chromium.org035f4af2011-09-07 08:14:47 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
mmentovai@google.comaa13be62008-09-03 03:20:34 +09002// 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.org035f4af2011-09-07 08:14:47 +09006#include "base/bind.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +09007
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace {
11
mmentovai@google.comaa13be62008-09-03 03:20:34 +090012int g_test_counter_1 = 0;
13int g_test_counter_2 = 0;
14
deanm@google.comf6299e72008-09-08 18:06:51 +090015void IncrementTestCounter1(void* unused) {
mmentovai@google.comaa13be62008-09-03 03:20:34 +090016 ++g_test_counter_1;
17}
18
deanm@google.comf6299e72008-09-08 18:06:51 +090019void IncrementTestCounter2(void* unused) {
mmentovai@google.comaa13be62008-09-03 03:20:34 +090020 ++g_test_counter_2;
21}
22
23void ZeroTestCounters() {
24 g_test_counter_1 = 0;
25 g_test_counter_2 = 0;
26}
27
deanm@google.comf6299e72008-09-08 18:06:51 +090028void ExpectCounter1IsZero(void* unused) {
mmentovai@google.comaa13be62008-09-03 03:20:34 +090029 EXPECT_EQ(0, g_test_counter_1);
30}
31
deanm@google.comf6299e72008-09-08 18:06:51 +090032void ExpectParamIsNull(void* param) {
33 EXPECT_EQ(static_cast<void*>(NULL), param);
34}
35
36void ExpectParamIsCounter(void* param) {
37 EXPECT_EQ(&g_test_counter_1, param);
38}
39
mmentovai@google.comaa13be62008-09-03 03:20:34 +090040} // namespace
41
phajdan.jr@chromium.orge7f133a2009-11-19 18:11:39 +090042class 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.comaa13be62008-09-03 03:20:34 +090048
phajdan.jr@chromium.orge7f133a2009-11-19 18:11:39 +090049TEST_F(AtExitTest, Basic) {
mmentovai@google.comaa13be62008-09-03 03:20:34 +090050 ZeroTestCounters();
deanm@google.comf6299e72008-09-08 18:06:51 +090051 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL);
52 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL);
53 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL);
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +090054
mmentovai@google.comaa13be62008-09-03 03:20:34 +090055 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.orge7f133a2009-11-19 18:11:39 +090062TEST_F(AtExitTest, LIFOOrder) {
mmentovai@google.comaa13be62008-09-03 03:20:34 +090063 ZeroTestCounters();
deanm@google.comf6299e72008-09-08 18:06:51 +090064 base::AtExitManager::RegisterCallback(&IncrementTestCounter1, NULL);
65 base::AtExitManager::RegisterCallback(&ExpectCounter1IsZero, NULL);
66 base::AtExitManager::RegisterCallback(&IncrementTestCounter2, NULL);
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +090067
mmentovai@google.comaa13be62008-09-03 03:20:34 +090068 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.orge7f133a2009-11-19 18:11:39 +090075TEST_F(AtExitTest, Param) {
deanm@google.comf6299e72008-09-08 18:06:51 +090076 base::AtExitManager::RegisterCallback(&ExpectParamIsNull, NULL);
77 base::AtExitManager::RegisterCallback(&ExpectParamIsCounter,
78 &g_test_counter_1);
79 base::AtExitManager::ProcessCallbacksNow();
80}
apatrick@chromium.org035f4af2011-09-07 08:14:47 +090081
82TEST_F(AtExitTest, Task) {
83 ZeroTestCounters();
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090084 base::AtExitManager::RegisterTask(base::Bind(&ExpectParamIsCounter,
apatrick@chromium.org035f4af2011-09-07 08:14:47 +090085 &g_test_counter_1));
86 base::AtExitManager::ProcessCallbacksNow();
87}