Updating trunk VERSION from 833.0 to 834.0
[chromium-blink-merge.git] / base / callback_unittest.cc
blob713c9e48d5e2b70c593cc691ee0c8f4a4b205f72
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"
12 namespace base {
13 namespace {
15 class HelperObject {
16 public:
17 HelperObject() : next_number_(0) { }
18 int GetNextNumber() { return ++next_number_; }
19 void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
21 private:
22 int next_number_;
25 struct FakeInvoker {
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 {
34 public:
35 typedef FakeInvoker Invoker;
38 class FakeInvokerStorage2 : public internal::InvokerStorageBase {
39 public:
40 typedef FakeInvoker Invoker;
43 TEST(CallbackOld, OneArg) {
44 HelperObject obj;
45 scoped_ptr<Callback1<int*>::Type> callback(
46 NewCallback(&obj, &HelperObject::GetNextNumberArg));
48 int number = 0;
49 callback->Run(&number);
50 EXPECT_EQ(number, 1);
53 TEST(CallbackOld, ReturnValue) {
54 HelperObject obj;
55 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
56 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
58 EXPECT_EQ(callback->Run(), 1);
61 class CallbackTest : public ::testing::Test {
62 public:
63 CallbackTest()
64 : callback_a_(MakeInvokerStorageHolder(new FakeInvokerStorage1())),
65 callback_b_(MakeInvokerStorageHolder(new FakeInvokerStorage2())) {
68 virtual ~CallbackTest() {
71 protected:
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_));
125 callback_a_.Reset();
127 EXPECT_TRUE(callback_a_.is_null());
128 EXPECT_TRUE(callback_a_.Equals(null_callback_));
131 } // namespace
132 } // namespace base