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.
5 #include "base/callback.h"
6 #include "base/callback_internal.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
16 HelperObject() : next_number_(0) { }
17 int GetNextNumber() { return ++next_number_
; }
18 void GetNextNumberArg(int* number
) { *number
= GetNextNumber(); }
25 typedef void(RunType
)(internal::BindStateBase
*);
26 static void Run(internal::BindStateBase
*) {
32 template <typename Runnable
, typename RunType
, typename BoundArgsType
>
35 // White-box testpoints to inject into a Callback<> object for checking
36 // comparators and emptiness APIs. Use a BindState that is specialized
37 // based on a type we declared in the anonymous namespace above to remove any
38 // chance of colliding with another instantiation and breaking the
39 // one-definition-rule.
41 struct BindState
<void(void), void(void), void(FakeInvoker
)>
42 : public BindStateBase
{
44 typedef FakeInvoker InvokerType
;
48 struct BindState
<void(void), void(void),
49 void(FakeInvoker
, FakeInvoker
)>
50 : public BindStateBase
{
52 typedef FakeInvoker InvokerType
;
54 } // namespace internal
58 typedef internal::BindState
<void(void), void(void), void(FakeInvoker
)>
60 typedef internal::BindState
<void(void), void(void),
61 void(FakeInvoker
, FakeInvoker
)>
64 class CallbackTest
: public ::testing::Test
{
67 : callback_a_(new FakeBindState1()),
68 callback_b_(new FakeBindState2()) {
71 virtual ~CallbackTest() {
75 Callback
<void(void)> callback_a_
;
76 const Callback
<void(void)> callback_b_
; // Ensure APIs work with const.
77 Callback
<void(void)> null_callback_
;
80 // Ensure we can create unbound callbacks. We need this to be able to store
81 // them in class members that can be initialized later.
82 TEST_F(CallbackTest
, DefaultConstruction
) {
83 Callback
<void(void)> c0
;
84 Callback
<void(int)> c1
;
85 Callback
<void(int,int)> c2
;
86 Callback
<void(int,int,int)> c3
;
87 Callback
<void(int,int,int,int)> c4
;
88 Callback
<void(int,int,int,int,int)> c5
;
89 Callback
<void(int,int,int,int,int,int)> c6
;
91 EXPECT_TRUE(c0
.is_null());
92 EXPECT_TRUE(c1
.is_null());
93 EXPECT_TRUE(c2
.is_null());
94 EXPECT_TRUE(c3
.is_null());
95 EXPECT_TRUE(c4
.is_null());
96 EXPECT_TRUE(c5
.is_null());
97 EXPECT_TRUE(c6
.is_null());
100 TEST_F(CallbackTest
, IsNull
) {
101 EXPECT_TRUE(null_callback_
.is_null());
102 EXPECT_FALSE(callback_a_
.is_null());
103 EXPECT_FALSE(callback_b_
.is_null());
106 TEST_F(CallbackTest
, Equals
) {
107 EXPECT_TRUE(callback_a_
.Equals(callback_a_
));
108 EXPECT_FALSE(callback_a_
.Equals(callback_b_
));
109 EXPECT_FALSE(callback_b_
.Equals(callback_a_
));
111 // We should compare based on instance, not type.
112 Callback
<void(void)> callback_c(new FakeBindState1());
113 Callback
<void(void)> callback_a2
= callback_a_
;
114 EXPECT_TRUE(callback_a_
.Equals(callback_a2
));
115 EXPECT_FALSE(callback_a_
.Equals(callback_c
));
117 // Empty, however, is always equal to empty.
118 Callback
<void(void)> empty2
;
119 EXPECT_TRUE(null_callback_
.Equals(empty2
));
122 TEST_F(CallbackTest
, Reset
) {
123 // Resetting should bring us back to empty.
124 ASSERT_FALSE(callback_a_
.is_null());
125 ASSERT_FALSE(callback_a_
.Equals(null_callback_
));
129 EXPECT_TRUE(callback_a_
.is_null());
130 EXPECT_TRUE(callback_a_
.Equals(null_callback_
));