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

Side by Side Diff: base/callback_unittest.cc

Issue 8738001: Remove BindStateHolder and have Bind() return a Callback<> object directly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix callback_unittest.cc 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
« base/callback_internal.cc ('K') | « base/callback_internal.cc ('k') | no next file » | 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
12 namespace { 13 namespace {
13 14
14 class HelperObject { 15 class HelperObject {
15 public: 16 public:
16 HelperObject() : next_number_(0) { } 17 HelperObject() : next_number_(0) { }
17 int GetNextNumber() { return ++next_number_; } 18 int GetNextNumber() { return ++next_number_; }
18 void GetNextNumberArg(int* number) { *number = GetNextNumber(); } 19 void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
19 20
20 private: 21 private:
21 int next_number_; 22 int next_number_;
22 }; 23 };
23 24
24 struct FakeInvoker { 25 struct FakeInvoker {
25 typedef void(RunType)(internal::BindStateBase*); 26 typedef void(RunType)(internal::BindStateBase*);
26 static void Run(internal::BindStateBase*) { 27 static void Run(internal::BindStateBase*) {
27 } 28 }
28 }; 29 };
30 } // namespace
31
32 namespace internal {
33 template <typename Runnable, typename RunType, typename BoundArgsType>
34 struct BindState;
29 35
30 // White-box testpoints to inject into a Callback<> object for checking 36 // White-box testpoints to inject into a Callback<> object for checking
31 // comparators and emptiness APIs. 37 // comparators and emptiness APIs. Use a BindState that is specialized
32 class FakeBindState1 : public internal::BindStateBase { 38 // based on a type we declared in this anonymous namespace to remove any chance
akalin 2011/12/02 18:54:45 this anonymous namespace -> the anonymous namespac
awong 2011/12/06 00:21:12 Done.
39 // of colliding with another instantiation and breaking the one-definition-rule.
40 template <>
41 struct internal::BindState<void(void), void(void), void(FakeInvoker)>
42 : public internal::BindStateBase {
33 public: 43 public:
34 typedef FakeInvoker InvokerType; 44 typedef FakeInvoker InvokerType;
35 }; 45 };
36 46
37 class FakeBindState2 : public internal::BindStateBase { 47 template <>
48 struct internal::BindState<void(void), void(void),
49 void(FakeInvoker, FakeInvoker)>
50 : public internal::BindStateBase {
38 public: 51 public:
39 typedef FakeInvoker InvokerType; 52 typedef FakeInvoker InvokerType;
40 }; 53 };
54 } // namespace internal
55
56 namespace {
57
58 typedef internal::BindState<void(void), void(void), void(FakeInvoker)>
59 FakeBindState1;
60 typedef internal::BindState<void(void), void(void),
61 void(FakeInvoker, FakeInvoker)>
62 FakeBindState2;
41 63
42 TEST(CallbackOld, OneArg) { 64 TEST(CallbackOld, OneArg) {
43 HelperObject obj; 65 HelperObject obj;
44 scoped_ptr<Callback1<int*>::Type> callback( 66 scoped_ptr<Callback1<int*>::Type> callback(
45 NewCallback(&obj, &HelperObject::GetNextNumberArg)); 67 NewCallback(&obj, &HelperObject::GetNextNumberArg));
46 68
47 int number = 0; 69 int number = 0;
48 callback->Run(&number); 70 callback->Run(&number);
49 EXPECT_EQ(number, 1); 71 EXPECT_EQ(number, 1);
50 } 72 }
51 73
52 TEST(CallbackOld, ReturnValue) { 74 TEST(CallbackOld, ReturnValue) {
53 HelperObject obj; 75 HelperObject obj;
54 scoped_ptr<CallbackWithReturnValue<int>::Type> callback( 76 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
55 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); 77 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
56 78
57 EXPECT_EQ(callback->Run(), 1); 79 EXPECT_EQ(callback->Run(), 1);
58 } 80 }
59 81
60 class CallbackTest : public ::testing::Test { 82 class CallbackTest : public ::testing::Test {
61 public: 83 public:
62 CallbackTest() 84 CallbackTest()
63 : callback_a_(MakeBindStateHolder(new FakeBindState1())), 85 : callback_a_(new FakeBindState1()),
64 callback_b_(MakeBindStateHolder(new FakeBindState2())) { 86 callback_b_(new FakeBindState2()) {
65 } 87 }
66 88
67 virtual ~CallbackTest() { 89 virtual ~CallbackTest() {
68 } 90 }
69 91
70 protected: 92 protected:
71 Callback<void(void)> callback_a_; 93 Callback<void(void)> callback_a_;
72 const Callback<void(void)> callback_b_; // Ensure APIs work with const. 94 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
73 Callback<void(void)> null_callback_; 95 Callback<void(void)> null_callback_;
74 }; 96 };
(...skipping 23 matching lines...) Expand all
98 EXPECT_FALSE(callback_a_.is_null()); 120 EXPECT_FALSE(callback_a_.is_null());
99 EXPECT_FALSE(callback_b_.is_null()); 121 EXPECT_FALSE(callback_b_.is_null());
100 } 122 }
101 123
102 TEST_F(CallbackTest, Equals) { 124 TEST_F(CallbackTest, Equals) {
103 EXPECT_TRUE(callback_a_.Equals(callback_a_)); 125 EXPECT_TRUE(callback_a_.Equals(callback_a_));
104 EXPECT_FALSE(callback_a_.Equals(callback_b_)); 126 EXPECT_FALSE(callback_a_.Equals(callback_b_));
105 EXPECT_FALSE(callback_b_.Equals(callback_a_)); 127 EXPECT_FALSE(callback_b_.Equals(callback_a_));
106 128
107 // We should compare based on instance, not type. 129 // We should compare based on instance, not type.
108 Callback<void(void)> callback_c( 130 Callback<void(void)> callback_c(new FakeBindState1());
109 MakeBindStateHolder(new FakeBindState1()));
110 Callback<void(void)> callback_a2 = callback_a_; 131 Callback<void(void)> callback_a2 = callback_a_;
111 EXPECT_TRUE(callback_a_.Equals(callback_a2)); 132 EXPECT_TRUE(callback_a_.Equals(callback_a2));
112 EXPECT_FALSE(callback_a_.Equals(callback_c)); 133 EXPECT_FALSE(callback_a_.Equals(callback_c));
113 134
114 // Empty, however, is always equal to empty. 135 // Empty, however, is always equal to empty.
115 Callback<void(void)> empty2; 136 Callback<void(void)> empty2;
116 EXPECT_TRUE(null_callback_.Equals(empty2)); 137 EXPECT_TRUE(null_callback_.Equals(empty2));
117 } 138 }
118 139
119 TEST_F(CallbackTest, Reset) { 140 TEST_F(CallbackTest, Reset) {
120 // Resetting should bring us back to empty. 141 // Resetting should bring us back to empty.
121 ASSERT_FALSE(callback_a_.is_null()); 142 ASSERT_FALSE(callback_a_.is_null());
122 ASSERT_FALSE(callback_a_.Equals(null_callback_)); 143 ASSERT_FALSE(callback_a_.Equals(null_callback_));
123 144
124 callback_a_.Reset(); 145 callback_a_.Reset();
125 146
126 EXPECT_TRUE(callback_a_.is_null()); 147 EXPECT_TRUE(callback_a_.is_null());
127 EXPECT_TRUE(callback_a_.Equals(null_callback_)); 148 EXPECT_TRUE(callback_a_.Equals(null_callback_));
128 } 149 }
129 150
130 } // namespace 151 } // namespace
131 } // namespace base 152 } // namespace base
OLDNEW
« base/callback_internal.cc ('K') | « base/callback_internal.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698