1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/callback.h"
7 #include "base/callback_helpers.h"
8 #include "base/callback_internal.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
17 typedef void(RunType
)(internal::BindStateBase
*);
18 static void Run(internal::BindStateBase
*) {
25 template <typename Runnable
, typename RunType
, typename BoundArgsType
>
28 // White-box testpoints to inject into a Callback<> object for checking
29 // comparators and emptiness APIs. Use a BindState that is specialized
30 // based on a type we declared in the anonymous namespace above to remove any
31 // chance of colliding with another instantiation and breaking the
32 // one-definition-rule.
34 struct BindState
<void(void), void(void), void(FakeInvoker
)>
35 : public BindStateBase
{
37 typedef FakeInvoker InvokerType
;
41 struct BindState
<void(void), void(void),
42 void(FakeInvoker
, FakeInvoker
)>
43 : public BindStateBase
{
45 typedef FakeInvoker InvokerType
;
47 } // namespace internal
51 typedef internal::BindState
<void(void), void(void), void(FakeInvoker
)>
53 typedef internal::BindState
<void(void), void(void),
54 void(FakeInvoker
, FakeInvoker
)>
57 class CallbackTest
: public ::testing::Test
{
60 : callback_a_(new FakeBindState1()),
61 callback_b_(new FakeBindState2()) {
64 virtual ~CallbackTest() {
68 Callback
<void(void)> callback_a_
;
69 const Callback
<void(void)> callback_b_
; // Ensure APIs work with const.
70 Callback
<void(void)> null_callback_
;
73 // Ensure we can create unbound callbacks. We need this to be able to store
74 // them in class members that can be initialized later.
75 TEST_F(CallbackTest
, DefaultConstruction
) {
76 Callback
<void(void)> c0
;
77 Callback
<void(int)> c1
;
78 Callback
<void(int,int)> c2
;
79 Callback
<void(int,int,int)> c3
;
80 Callback
<void(int,int,int,int)> c4
;
81 Callback
<void(int,int,int,int,int)> c5
;
82 Callback
<void(int,int,int,int,int,int)> c6
;
84 EXPECT_TRUE(c0
.is_null());
85 EXPECT_TRUE(c1
.is_null());
86 EXPECT_TRUE(c2
.is_null());
87 EXPECT_TRUE(c3
.is_null());
88 EXPECT_TRUE(c4
.is_null());
89 EXPECT_TRUE(c5
.is_null());
90 EXPECT_TRUE(c6
.is_null());
93 TEST_F(CallbackTest
, IsNull
) {
94 EXPECT_TRUE(null_callback_
.is_null());
95 EXPECT_FALSE(callback_a_
.is_null());
96 EXPECT_FALSE(callback_b_
.is_null());
99 TEST_F(CallbackTest
, Equals
) {
100 EXPECT_TRUE(callback_a_
.Equals(callback_a_
));
101 EXPECT_FALSE(callback_a_
.Equals(callback_b_
));
102 EXPECT_FALSE(callback_b_
.Equals(callback_a_
));
104 // We should compare based on instance, not type.
105 Callback
<void(void)> callback_c(new FakeBindState1());
106 Callback
<void(void)> callback_a2
= callback_a_
;
107 EXPECT_TRUE(callback_a_
.Equals(callback_a2
));
108 EXPECT_FALSE(callback_a_
.Equals(callback_c
));
110 // Empty, however, is always equal to empty.
111 Callback
<void(void)> empty2
;
112 EXPECT_TRUE(null_callback_
.Equals(empty2
));
115 TEST_F(CallbackTest
, Reset
) {
116 // Resetting should bring us back to empty.
117 ASSERT_FALSE(callback_a_
.is_null());
118 ASSERT_FALSE(callback_a_
.Equals(null_callback_
));
122 EXPECT_TRUE(callback_a_
.is_null());
123 EXPECT_TRUE(callback_a_
.Equals(null_callback_
));
126 struct TestForReentrancy
{
128 : cb_already_run(false),
129 cb(Bind(&TestForReentrancy::AssertCBIsNull
, Unretained(this))) {
131 void AssertCBIsNull() {
132 ASSERT_TRUE(cb
.is_null());
133 cb_already_run
= true;
139 TEST_F(CallbackTest
, ResetAndReturn
) {
140 TestForReentrancy tfr
;
141 ASSERT_FALSE(tfr
.cb
.is_null());
142 ASSERT_FALSE(tfr
.cb_already_run
);
143 ResetAndReturn(&tfr
.cb
).Run();
144 ASSERT_TRUE(tfr
.cb
.is_null());
145 ASSERT_TRUE(tfr
.cb_already_run
);