blob: 2844aa98a29317149eaaf493b3d9f92b3f7fff0b [file] [log] [blame]
jhawkins@chromium.org4a310212012-01-04 04:36:57 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
jam@chromium.org2750bf82009-07-18 03:14:47 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
fischman@chromium.orgdc221a72012-03-25 05:37:27 +09005#include "base/bind.h"
akalin@chromium.org6ed4f882010-02-19 12:15:59 +09006#include "base/callback.h"
fischman@chromium.orgdc221a72012-03-25 05:37:27 +09007#include "base/callback_helpers.h"
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +09008#include "base/callback_internal.h"
mattm@chromium.orgbf27cb72012-06-14 08:15:16 +09009#include "base/memory/ref_counted.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090010#include "base/memory/scoped_ptr.h"
jam@chromium.org2750bf82009-07-18 03:14:47 +090011#include "testing/gtest/include/gtest/gtest.h"
12
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090013namespace base {
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090014
jam@chromium.org2750bf82009-07-18 03:14:47 +090015namespace {
16
ajwong@chromium.orge0648232011-02-19 09:52:15 +090017struct FakeInvoker {
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090018 typedef void(RunType)(internal::BindStateBase*);
19 static void Run(internal::BindStateBase*) {
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090020 }
21};
fischman@chromium.orgdc221a72012-03-25 05:37:27 +090022
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090023} // namespace
24
25namespace internal {
26template <typename Runnable, typename RunType, typename BoundArgsType>
27struct BindState;
jam@chromium.org2750bf82009-07-18 03:14:47 +090028
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090029// White-box testpoints to inject into a Callback<> object for checking
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090030// comparators and emptiness APIs. Use a BindState that is specialized
31// based on a type we declared in the anonymous namespace above to remove any
32// chance of colliding with another instantiation and breaking the
33// one-definition-rule.
34template <>
35struct BindState<void(void), void(void), void(FakeInvoker)>
36 : public BindStateBase {
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090037 public:
tapted9cef4212015-05-14 17:03:32 +090038 BindState() : BindStateBase(&Destroy) {}
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090039 typedef FakeInvoker InvokerType;
dmichael2f7bc202014-12-19 07:30:11 +090040 private:
tapted9cef4212015-05-14 17:03:32 +090041 ~BindState() {}
42 static void Destroy(BindStateBase* self) {
43 delete static_cast<BindState*>(self);
44 }
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090045};
46
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090047template <>
48struct BindState<void(void), void(void),
49 void(FakeInvoker, FakeInvoker)>
50 : public BindStateBase {
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090051 public:
tapted9cef4212015-05-14 17:03:32 +090052 BindState() : BindStateBase(&Destroy) {}
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090053 typedef FakeInvoker InvokerType;
dmichael2f7bc202014-12-19 07:30:11 +090054 private:
tapted9cef4212015-05-14 17:03:32 +090055 ~BindState() {}
56 static void Destroy(BindStateBase* self) {
57 delete static_cast<BindState*>(self);
58 }
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090059};
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090060} // namespace internal
61
62namespace {
63
64typedef internal::BindState<void(void), void(void), void(FakeInvoker)>
65 FakeBindState1;
66typedef internal::BindState<void(void), void(void),
67 void(FakeInvoker, FakeInvoker)>
68 FakeBindState2;
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090069
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090070class CallbackTest : public ::testing::Test {
71 public:
72 CallbackTest()
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090073 : callback_a_(new FakeBindState1()),
74 callback_b_(new FakeBindState2()) {
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090075 }
76
dchengca87abb2014-12-23 11:56:47 +090077 ~CallbackTest() override {}
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090078
79 protected:
80 Callback<void(void)> callback_a_;
81 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
82 Callback<void(void)> null_callback_;
83};
84
85// Ensure we can create unbound callbacks. We need this to be able to store
86// them in class members that can be initialized later.
87TEST_F(CallbackTest, DefaultConstruction) {
88 Callback<void(void)> c0;
89 Callback<void(int)> c1;
90 Callback<void(int,int)> c2;
91 Callback<void(int,int,int)> c3;
92 Callback<void(int,int,int,int)> c4;
93 Callback<void(int,int,int,int,int)> c5;
94 Callback<void(int,int,int,int,int,int)> c6;
95
96 EXPECT_TRUE(c0.is_null());
97 EXPECT_TRUE(c1.is_null());
98 EXPECT_TRUE(c2.is_null());
99 EXPECT_TRUE(c3.is_null());
100 EXPECT_TRUE(c4.is_null());
101 EXPECT_TRUE(c5.is_null());
102 EXPECT_TRUE(c6.is_null());
103}
104
105TEST_F(CallbackTest, IsNull) {
106 EXPECT_TRUE(null_callback_.is_null());
107 EXPECT_FALSE(callback_a_.is_null());
108 EXPECT_FALSE(callback_b_.is_null());
109}
110
111TEST_F(CallbackTest, Equals) {
112 EXPECT_TRUE(callback_a_.Equals(callback_a_));
113 EXPECT_FALSE(callback_a_.Equals(callback_b_));
114 EXPECT_FALSE(callback_b_.Equals(callback_a_));
115
116 // We should compare based on instance, not type.
ajwong@chromium.orgabd70002011-12-20 09:10:04 +0900117 Callback<void(void)> callback_c(new FakeBindState1());
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +0900118 Callback<void(void)> callback_a2 = callback_a_;
119 EXPECT_TRUE(callback_a_.Equals(callback_a2));
120 EXPECT_FALSE(callback_a_.Equals(callback_c));
121
122 // Empty, however, is always equal to empty.
123 Callback<void(void)> empty2;
124 EXPECT_TRUE(null_callback_.Equals(empty2));
125}
126
127TEST_F(CallbackTest, Reset) {
128 // Resetting should bring us back to empty.
129 ASSERT_FALSE(callback_a_.is_null());
130 ASSERT_FALSE(callback_a_.Equals(null_callback_));
131
132 callback_a_.Reset();
133
134 EXPECT_TRUE(callback_a_.is_null());
135 EXPECT_TRUE(callback_a_.Equals(null_callback_));
136}
137
fischman@chromium.orgdc221a72012-03-25 05:37:27 +0900138struct TestForReentrancy {
139 TestForReentrancy()
140 : cb_already_run(false),
141 cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) {
142 }
143 void AssertCBIsNull() {
144 ASSERT_TRUE(cb.is_null());
145 cb_already_run = true;
146 }
147 bool cb_already_run;
148 Closure cb;
149};
150
151TEST_F(CallbackTest, ResetAndReturn) {
152 TestForReentrancy tfr;
153 ASSERT_FALSE(tfr.cb.is_null());
154 ASSERT_FALSE(tfr.cb_already_run);
155 ResetAndReturn(&tfr.cb).Run();
156 ASSERT_TRUE(tfr.cb.is_null());
157 ASSERT_TRUE(tfr.cb_already_run);
158}
159
mattm@chromium.orgbf27cb72012-06-14 08:15:16 +0900160class CallbackOwner : public base::RefCounted<CallbackOwner> {
161 public:
hans@chromium.org07615b42013-01-15 19:52:11 +0900162 explicit CallbackOwner(bool* deleted) {
mattm@chromium.orgbf27cb72012-06-14 08:15:16 +0900163 callback_ = Bind(&CallbackOwner::Unused, this);
164 deleted_ = deleted;
165 }
166 void Reset() {
167 callback_.Reset();
168 // We are deleted here if no-one else had a ref to us.
169 }
170
171 private:
172 friend class base::RefCounted<CallbackOwner>;
173 virtual ~CallbackOwner() {
174 *deleted_ = true;
175 }
176 void Unused() {
177 FAIL() << "Should never be called";
178 }
179
180 Closure callback_;
181 bool* deleted_;
182};
183
184TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
185 bool deleted = false;
186 CallbackOwner* owner = new CallbackOwner(&deleted);
187 owner->Reset();
188 ASSERT_TRUE(deleted);
189}
190
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +0900191} // namespace
192} // namespace base