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

Side by Side Diff: base/callback_unittest.cc

Issue 8915024: Retry 114494 - Remove BindStateHolder and have Bind() return a Callback<> object directly." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase 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
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 the anonymous namespace above to remove any
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 {
33 public: 44 public:
34 typedef FakeInvoker InvokerType; 45 typedef FakeInvoker InvokerType;
35 }; 46 };
36 47
37 class FakeBindState2 : public internal::BindStateBase { 48 template <>
49 struct BindState<void(void), void(void),
50 void(FakeInvoker, FakeInvoker)>
51 : public BindStateBase {
38 public: 52 public:
39 typedef FakeInvoker InvokerType; 53 typedef FakeInvoker InvokerType;
40 }; 54 };
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;
41 64
42 TEST(CallbackOld, OneArg) { 65 TEST(CallbackOld, OneArg) {
43 HelperObject obj; 66 HelperObject obj;
44 scoped_ptr<Callback1<int*>::Type> callback( 67 scoped_ptr<Callback1<int*>::Type> callback(
45 NewCallback(&obj, &HelperObject::GetNextNumberArg)); 68 NewCallback(&obj, &HelperObject::GetNextNumberArg));
46 69
47 int number = 0; 70 int number = 0;
48 callback->Run(&number); 71 callback->Run(&number);
49 EXPECT_EQ(number, 1); 72 EXPECT_EQ(number, 1);
50 } 73 }
51 74
52 TEST(CallbackOld, ReturnValue) { 75 TEST(CallbackOld, ReturnValue) {
53 HelperObject obj; 76 HelperObject obj;
54 scoped_ptr<CallbackWithReturnValue<int>::Type> callback( 77 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
55 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); 78 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
56 79
57 EXPECT_EQ(callback->Run(), 1); 80 EXPECT_EQ(callback->Run(), 1);
58 } 81 }
59 82
60 class CallbackTest : public ::testing::Test { 83 class CallbackTest : public ::testing::Test {
61 public: 84 public:
62 CallbackTest() 85 CallbackTest()
63 : callback_a_(MakeBindStateHolder(new FakeBindState1())), 86 : callback_a_(new FakeBindState1()),
64 callback_b_(MakeBindStateHolder(new FakeBindState2())) { 87 callback_b_(new FakeBindState2()) {
65 } 88 }
66 89
67 virtual ~CallbackTest() { 90 virtual ~CallbackTest() {
68 } 91 }
69 92
70 protected: 93 protected:
71 Callback<void(void)> callback_a_; 94 Callback<void(void)> callback_a_;
72 const Callback<void(void)> callback_b_; // Ensure APIs work with const. 95 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
73 Callback<void(void)> null_callback_; 96 Callback<void(void)> null_callback_;
74 }; 97 };
(...skipping 23 matching lines...) Expand all
98 EXPECT_FALSE(callback_a_.is_null()); 121 EXPECT_FALSE(callback_a_.is_null());
99 EXPECT_FALSE(callback_b_.is_null()); 122 EXPECT_FALSE(callback_b_.is_null());
100 } 123 }
101 124
102 TEST_F(CallbackTest, Equals) { 125 TEST_F(CallbackTest, Equals) {
103 EXPECT_TRUE(callback_a_.Equals(callback_a_)); 126 EXPECT_TRUE(callback_a_.Equals(callback_a_));
104 EXPECT_FALSE(callback_a_.Equals(callback_b_)); 127 EXPECT_FALSE(callback_a_.Equals(callback_b_));
105 EXPECT_FALSE(callback_b_.Equals(callback_a_)); 128 EXPECT_FALSE(callback_b_.Equals(callback_a_));
106 129
107 // We should compare based on instance, not type. 130 // We should compare based on instance, not type.
108 Callback<void(void)> callback_c( 131 Callback<void(void)> callback_c(new FakeBindState1());
109 MakeBindStateHolder(new FakeBindState1()));
110 Callback<void(void)> callback_a2 = callback_a_; 132 Callback<void(void)> callback_a2 = callback_a_;
111 EXPECT_TRUE(callback_a_.Equals(callback_a2)); 133 EXPECT_TRUE(callback_a_.Equals(callback_a2));
112 EXPECT_FALSE(callback_a_.Equals(callback_c)); 134 EXPECT_FALSE(callback_a_.Equals(callback_c));
113 135
114 // Empty, however, is always equal to empty. 136 // Empty, however, is always equal to empty.
115 Callback<void(void)> empty2; 137 Callback<void(void)> empty2;
116 EXPECT_TRUE(null_callback_.Equals(empty2)); 138 EXPECT_TRUE(null_callback_.Equals(empty2));
117 } 139 }
118 140
119 TEST_F(CallbackTest, Reset) { 141 TEST_F(CallbackTest, Reset) {
120 // Resetting should bring us back to empty. 142 // Resetting should bring us back to empty.
121 ASSERT_FALSE(callback_a_.is_null()); 143 ASSERT_FALSE(callback_a_.is_null());
122 ASSERT_FALSE(callback_a_.Equals(null_callback_)); 144 ASSERT_FALSE(callback_a_.Equals(null_callback_));
123 145
124 callback_a_.Reset(); 146 callback_a_.Reset();
125 147
126 EXPECT_TRUE(callback_a_.is_null()); 148 EXPECT_TRUE(callback_a_.is_null());
127 EXPECT_TRUE(callback_a_.Equals(null_callback_)); 149 EXPECT_TRUE(callback_a_.Equals(null_callback_));
128 } 150 }
129 151
130 } // namespace 152 } // namespace
131 } // namespace base 153 } // 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