1 // Copyright (c) 2011 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/callback_old.h"
8 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
17 HelperObject() : next_number_(0) { }
18 int GetNextNumber() { return ++next_number_
; }
19 void GetNextNumberArg(int* number
) { *number
= GetNextNumber(); }
26 typedef void(*DoInvokeType
)(internal::InvokerStorageBase
*);
27 static void DoInvoke(internal::InvokerStorageBase
*) {
31 // White-box testpoints to inject into a Callback<> object for checking
32 // comparators and emptiness APIs.
33 class FakeInvokerStorage1
: public internal::InvokerStorageBase
{
35 typedef FakeInvoker Invoker
;
38 class FakeInvokerStorage2
: public internal::InvokerStorageBase
{
40 typedef FakeInvoker Invoker
;
43 TEST(CallbackOld
, OneArg
) {
45 scoped_ptr
<Callback1
<int*>::Type
> callback(
46 NewCallback(&obj
, &HelperObject::GetNextNumberArg
));
49 callback
->Run(&number
);
53 TEST(CallbackOld
, ReturnValue
) {
55 scoped_ptr
<CallbackWithReturnValue
<int>::Type
> callback(
56 NewCallbackWithReturnValue(&obj
, &HelperObject::GetNextNumber
));
58 EXPECT_EQ(callback
->Run(), 1);
61 class CallbackTest
: public ::testing::Test
{
64 : callback_a_(MakeInvokerStorageHolder(new FakeInvokerStorage1())),
65 callback_b_(MakeInvokerStorageHolder(new FakeInvokerStorage2())) {
68 virtual ~CallbackTest() {
72 Callback
<void(void)> callback_a_
;
73 const Callback
<void(void)> callback_b_
; // Ensure APIs work with const.
74 Callback
<void(void)> null_callback_
;
77 // Ensure we can create unbound callbacks. We need this to be able to store
78 // them in class members that can be initialized later.
79 TEST_F(CallbackTest
, DefaultConstruction
) {
80 Callback
<void(void)> c0
;
81 Callback
<void(int)> c1
;
82 Callback
<void(int,int)> c2
;
83 Callback
<void(int,int,int)> c3
;
84 Callback
<void(int,int,int,int)> c4
;
85 Callback
<void(int,int,int,int,int)> c5
;
86 Callback
<void(int,int,int,int,int,int)> c6
;
88 EXPECT_TRUE(c0
.is_null());
89 EXPECT_TRUE(c1
.is_null());
90 EXPECT_TRUE(c2
.is_null());
91 EXPECT_TRUE(c3
.is_null());
92 EXPECT_TRUE(c4
.is_null());
93 EXPECT_TRUE(c5
.is_null());
94 EXPECT_TRUE(c6
.is_null());
97 TEST_F(CallbackTest
, IsNull
) {
98 EXPECT_TRUE(null_callback_
.is_null());
99 EXPECT_FALSE(callback_a_
.is_null());
100 EXPECT_FALSE(callback_b_
.is_null());
103 TEST_F(CallbackTest
, Equals
) {
104 EXPECT_TRUE(callback_a_
.Equals(callback_a_
));
105 EXPECT_FALSE(callback_a_
.Equals(callback_b_
));
106 EXPECT_FALSE(callback_b_
.Equals(callback_a_
));
108 // We should compare based on instance, not type.
109 Callback
<void(void)> callback_c(
110 MakeInvokerStorageHolder(new FakeInvokerStorage1()));
111 Callback
<void(void)> callback_a2
= callback_a_
;
112 EXPECT_TRUE(callback_a_
.Equals(callback_a2
));
113 EXPECT_FALSE(callback_a_
.Equals(callback_c
));
115 // Empty, however, is always equal to empty.
116 Callback
<void(void)> empty2
;
117 EXPECT_TRUE(null_callback_
.Equals(empty2
));
120 TEST_F(CallbackTest
, Reset
) {
121 // Resetting should bring us back to empty.
122 ASSERT_FALSE(callback_a_
.is_null());
123 ASSERT_FALSE(callback_a_
.Equals(null_callback_
));
127 EXPECT_TRUE(callback_a_
.is_null());
128 EXPECT_TRUE(callback_a_
.Equals(null_callback_
));