blob: 6103b2e9675c318c3e95da9b3f6e49aa7c1e0491 [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:
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090038 typedef FakeInvoker InvokerType;
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090039};
40
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090041template <>
42struct BindState<void(void), void(void),
43 void(FakeInvoker, FakeInvoker)>
44 : public BindStateBase {
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090045 public:
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090046 typedef FakeInvoker InvokerType;
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090047};
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090048} // namespace internal
49
50namespace {
51
52typedef internal::BindState<void(void), void(void), void(FakeInvoker)>
53 FakeBindState1;
54typedef internal::BindState<void(void), void(void),
55 void(FakeInvoker, FakeInvoker)>
56 FakeBindState2;
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090057
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090058class CallbackTest : public ::testing::Test {
59 public:
60 CallbackTest()
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090061 : callback_a_(new FakeBindState1()),
62 callback_b_(new FakeBindState2()) {
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +090063 }
64
65 virtual ~CallbackTest() {
66 }
67
68 protected:
69 Callback<void(void)> callback_a_;
70 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
71 Callback<void(void)> null_callback_;
72};
73
74// Ensure we can create unbound callbacks. We need this to be able to store
75// them in class members that can be initialized later.
76TEST_F(CallbackTest, DefaultConstruction) {
77 Callback<void(void)> c0;
78 Callback<void(int)> c1;
79 Callback<void(int,int)> c2;
80 Callback<void(int,int,int)> c3;
81 Callback<void(int,int,int,int)> c4;
82 Callback<void(int,int,int,int,int)> c5;
83 Callback<void(int,int,int,int,int,int)> c6;
84
85 EXPECT_TRUE(c0.is_null());
86 EXPECT_TRUE(c1.is_null());
87 EXPECT_TRUE(c2.is_null());
88 EXPECT_TRUE(c3.is_null());
89 EXPECT_TRUE(c4.is_null());
90 EXPECT_TRUE(c5.is_null());
91 EXPECT_TRUE(c6.is_null());
92}
93
94TEST_F(CallbackTest, IsNull) {
95 EXPECT_TRUE(null_callback_.is_null());
96 EXPECT_FALSE(callback_a_.is_null());
97 EXPECT_FALSE(callback_b_.is_null());
98}
99
100TEST_F(CallbackTest, Equals) {
101 EXPECT_TRUE(callback_a_.Equals(callback_a_));
102 EXPECT_FALSE(callback_a_.Equals(callback_b_));
103 EXPECT_FALSE(callback_b_.Equals(callback_a_));
104
105 // We should compare based on instance, not type.
ajwong@chromium.orgabd70002011-12-20 09:10:04 +0900106 Callback<void(void)> callback_c(new FakeBindState1());
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +0900107 Callback<void(void)> callback_a2 = callback_a_;
108 EXPECT_TRUE(callback_a_.Equals(callback_a2));
109 EXPECT_FALSE(callback_a_.Equals(callback_c));
110
111 // Empty, however, is always equal to empty.
112 Callback<void(void)> empty2;
113 EXPECT_TRUE(null_callback_.Equals(empty2));
114}
115
116TEST_F(CallbackTest, Reset) {
117 // Resetting should bring us back to empty.
118 ASSERT_FALSE(callback_a_.is_null());
119 ASSERT_FALSE(callback_a_.Equals(null_callback_));
120
121 callback_a_.Reset();
122
123 EXPECT_TRUE(callback_a_.is_null());
124 EXPECT_TRUE(callback_a_.Equals(null_callback_));
125}
126
fischman@chromium.orgdc221a72012-03-25 05:37:27 +0900127struct TestForReentrancy {
128 TestForReentrancy()
129 : cb_already_run(false),
130 cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) {
131 }
132 void AssertCBIsNull() {
133 ASSERT_TRUE(cb.is_null());
134 cb_already_run = true;
135 }
136 bool cb_already_run;
137 Closure cb;
138};
139
140TEST_F(CallbackTest, ResetAndReturn) {
141 TestForReentrancy tfr;
142 ASSERT_FALSE(tfr.cb.is_null());
143 ASSERT_FALSE(tfr.cb_already_run);
144 ResetAndReturn(&tfr.cb).Run();
145 ASSERT_TRUE(tfr.cb.is_null());
146 ASSERT_TRUE(tfr.cb_already_run);
147}
148
mattm@chromium.orgbf27cb72012-06-14 08:15:16 +0900149class CallbackOwner : public base::RefCounted<CallbackOwner> {
150 public:
hans@chromium.org07615b42013-01-15 19:52:11 +0900151 explicit CallbackOwner(bool* deleted) {
mattm@chromium.orgbf27cb72012-06-14 08:15:16 +0900152 callback_ = Bind(&CallbackOwner::Unused, this);
153 deleted_ = deleted;
154 }
155 void Reset() {
156 callback_.Reset();
157 // We are deleted here if no-one else had a ref to us.
158 }
159
160 private:
161 friend class base::RefCounted<CallbackOwner>;
162 virtual ~CallbackOwner() {
163 *deleted_ = true;
164 }
165 void Unused() {
166 FAIL() << "Should never be called";
167 }
168
169 Closure callback_;
170 bool* deleted_;
171};
172
173TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
174 bool deleted = false;
175 CallbackOwner* owner = new CallbackOwner(&deleted);
176 owner->Reset();
177 ASSERT_TRUE(deleted);
178}
179
ajwong@chromium.org62fb0a02011-02-18 13:05:14 +0900180} // namespace
181} // namespace base