Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(876)

Side by Side Diff: base/callback_unittest.cc

Issue 8914022: Revert 114494 - Remove BindStateHolder and have Bind() return a Callback<> object directly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/callback_internal.cc ('k') | base/cancelable_callback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/callback.h" 5 #include "base/callback.h"
6 #include "base/callback_internal.h" 6 #include "base/callback_internal.h"
7 #include "base/callback_old.h" 7 #include "base/callback_old.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace base { 11 namespace base {
12
13 namespace { 12 namespace {
14 13
15 class HelperObject { 14 class HelperObject {
16 public: 15 public:
17 HelperObject() : next_number_(0) { } 16 HelperObject() : next_number_(0) { }
18 int GetNextNumber() { return ++next_number_; } 17 int GetNextNumber() { return ++next_number_; }
19 void GetNextNumberArg(int* number) { *number = GetNextNumber(); } 18 void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
20 19
21 private: 20 private:
22 int next_number_; 21 int next_number_;
23 }; 22 };
24 23
25 struct FakeInvoker { 24 struct FakeInvoker {
26 typedef void(RunType)(internal::BindStateBase*); 25 typedef void(RunType)(internal::BindStateBase*);
27 static void Run(internal::BindStateBase*) { 26 static void Run(internal::BindStateBase*) {
28 } 27 }
29 }; 28 };
30 } // namespace
31
32 namespace internal {
33 template <typename Runnable, typename RunType, typename BoundArgsType>
34 struct BindState;
35 29
36 // White-box testpoints to inject into a Callback<> object for checking 30 // White-box testpoints to inject into a Callback<> object for checking
37 // comparators and emptiness APIs. Use a BindState that is specialized 31 // comparators and emptiness APIs.
38 // based on a type we declared in the anonymous namespace above to remove any 32 class FakeBindState1 : public internal::BindStateBase {
39 // chance of colliding with another instantiation and breaking the
40 // one-definition-rule.
41 template <>
42 struct BindState<void(void), void(void), void(FakeInvoker)>
43 : public BindStateBase {
44 public: 33 public:
45 typedef FakeInvoker InvokerType; 34 typedef FakeInvoker InvokerType;
46 }; 35 };
47 36
48 template <> 37 class FakeBindState2 : public internal::BindStateBase {
49 struct BindState<void(void), void(void),
50 void(FakeInvoker, FakeInvoker)>
51 : public BindStateBase {
52 public: 38 public:
53 typedef FakeInvoker InvokerType; 39 typedef FakeInvoker InvokerType;
54 }; 40 };
55 } // namespace internal
56
57 namespace {
58
59 typedef internal::BindState<void(void), void(void), void(FakeInvoker)>
60 FakeBindState1;
61 typedef internal::BindState<void(void), void(void),
62 void(FakeInvoker, FakeInvoker)>
63 FakeBindState2;
64 41
65 TEST(CallbackOld, OneArg) { 42 TEST(CallbackOld, OneArg) {
66 HelperObject obj; 43 HelperObject obj;
67 scoped_ptr<Callback1<int*>::Type> callback( 44 scoped_ptr<Callback1<int*>::Type> callback(
68 NewCallback(&obj, &HelperObject::GetNextNumberArg)); 45 NewCallback(&obj, &HelperObject::GetNextNumberArg));
69 46
70 int number = 0; 47 int number = 0;
71 callback->Run(&number); 48 callback->Run(&number);
72 EXPECT_EQ(number, 1); 49 EXPECT_EQ(number, 1);
73 } 50 }
74 51
75 TEST(CallbackOld, ReturnValue) { 52 TEST(CallbackOld, ReturnValue) {
76 HelperObject obj; 53 HelperObject obj;
77 scoped_ptr<CallbackWithReturnValue<int>::Type> callback( 54 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
78 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); 55 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
79 56
80 EXPECT_EQ(callback->Run(), 1); 57 EXPECT_EQ(callback->Run(), 1);
81 } 58 }
82 59
83 class CallbackTest : public ::testing::Test { 60 class CallbackTest : public ::testing::Test {
84 public: 61 public:
85 CallbackTest() 62 CallbackTest()
86 : callback_a_(new FakeBindState1()), 63 : callback_a_(MakeBindStateHolder(new FakeBindState1())),
87 callback_b_(new FakeBindState2()) { 64 callback_b_(MakeBindStateHolder(new FakeBindState2())) {
88 } 65 }
89 66
90 virtual ~CallbackTest() { 67 virtual ~CallbackTest() {
91 } 68 }
92 69
93 protected: 70 protected:
94 Callback<void(void)> callback_a_; 71 Callback<void(void)> callback_a_;
95 const Callback<void(void)> callback_b_; // Ensure APIs work with const. 72 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
96 Callback<void(void)> null_callback_; 73 Callback<void(void)> null_callback_;
97 }; 74 };
(...skipping 23 matching lines...) Expand all
121 EXPECT_FALSE(callback_a_.is_null()); 98 EXPECT_FALSE(callback_a_.is_null());
122 EXPECT_FALSE(callback_b_.is_null()); 99 EXPECT_FALSE(callback_b_.is_null());
123 } 100 }
124 101
125 TEST_F(CallbackTest, Equals) { 102 TEST_F(CallbackTest, Equals) {
126 EXPECT_TRUE(callback_a_.Equals(callback_a_)); 103 EXPECT_TRUE(callback_a_.Equals(callback_a_));
127 EXPECT_FALSE(callback_a_.Equals(callback_b_)); 104 EXPECT_FALSE(callback_a_.Equals(callback_b_));
128 EXPECT_FALSE(callback_b_.Equals(callback_a_)); 105 EXPECT_FALSE(callback_b_.Equals(callback_a_));
129 106
130 // We should compare based on instance, not type. 107 // We should compare based on instance, not type.
131 Callback<void(void)> callback_c(new FakeBindState1()); 108 Callback<void(void)> callback_c(
109 MakeBindStateHolder(new FakeBindState1()));
132 Callback<void(void)> callback_a2 = callback_a_; 110 Callback<void(void)> callback_a2 = callback_a_;
133 EXPECT_TRUE(callback_a_.Equals(callback_a2)); 111 EXPECT_TRUE(callback_a_.Equals(callback_a2));
134 EXPECT_FALSE(callback_a_.Equals(callback_c)); 112 EXPECT_FALSE(callback_a_.Equals(callback_c));
135 113
136 // Empty, however, is always equal to empty. 114 // Empty, however, is always equal to empty.
137 Callback<void(void)> empty2; 115 Callback<void(void)> empty2;
138 EXPECT_TRUE(null_callback_.Equals(empty2)); 116 EXPECT_TRUE(null_callback_.Equals(empty2));
139 } 117 }
140 118
141 TEST_F(CallbackTest, Reset) { 119 TEST_F(CallbackTest, Reset) {
142 // Resetting should bring us back to empty. 120 // Resetting should bring us back to empty.
143 ASSERT_FALSE(callback_a_.is_null()); 121 ASSERT_FALSE(callback_a_.is_null());
144 ASSERT_FALSE(callback_a_.Equals(null_callback_)); 122 ASSERT_FALSE(callback_a_.Equals(null_callback_));
145 123
146 callback_a_.Reset(); 124 callback_a_.Reset();
147 125
148 EXPECT_TRUE(callback_a_.is_null()); 126 EXPECT_TRUE(callback_a_.is_null());
149 EXPECT_TRUE(callback_a_.Equals(null_callback_)); 127 EXPECT_TRUE(callback_a_.Equals(null_callback_));
150 } 128 }
151 129
152 } // namespace 130 } // namespace
153 } // namespace base 131 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_internal.cc ('k') | base/cancelable_callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698