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

Side by Side Diff: base/callback_registry_unittest.cc

Issue 23514056: Function-type templated CallbackRegistry (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months 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_registry.h ('K') | « base/callback_registry.h.pump ('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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_registry.h" 5 #include "base/callback_registry.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 17 matching lines...) Expand all
28 }; 28 };
29 29
30 class Remover { 30 class Remover {
31 public: 31 public:
32 Remover() : total_(0) {} 32 Remover() : total_(0) {}
33 void IncrementTotalAndRemove() { 33 void IncrementTotalAndRemove() {
34 total_++; 34 total_++;
35 removal_subscription_.reset(); 35 removal_subscription_.reset();
36 } 36 }
37 void SetSubscriptionToRemove( 37 void SetSubscriptionToRemove(
38 scoped_ptr<CallbackRegistry<void>::Subscription> sub) { 38 scoped_ptr<CallbackRegistry<void(void)>::Subscription> sub) {
39 removal_subscription_ = sub.Pass(); 39 removal_subscription_ = sub.Pass();
40 } 40 }
41 41
42 int total_; 42 int total_;
43 43
44 private: 44 private:
45 scoped_ptr<CallbackRegistry<void>::Subscription> removal_subscription_; 45 scoped_ptr<CallbackRegistry<void(void)>::Subscription> removal_subscription_;
46 DISALLOW_COPY_AND_ASSIGN(Remover); 46 DISALLOW_COPY_AND_ASSIGN(Remover);
47 }; 47 };
48 48
49 class Adder { 49 class Adder {
50 public: 50 public:
51 explicit Adder(CallbackRegistry<void>* cb_reg) 51 explicit Adder(CallbackRegistry<void(void)>* cb_reg)
52 : added_(false), 52 : added_(false),
53 total_(0), 53 total_(0),
54 cb_reg_(cb_reg) {} 54 cb_reg_(cb_reg) {}
55 void AddCallback() { 55 void AddCallback() {
56 if (!added_) { 56 if (!added_) {
57 added_ = true; 57 added_ = true;
58 subscription_ = 58 subscription_ =
59 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this))); 59 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this)));
60 } 60 }
61 } 61 }
62 void IncrementTotal() { total_++; } 62 void IncrementTotal() { total_++; }
63 63
64 bool added_; 64 bool added_;
65 int total_; 65 int total_;
66 66
67 private: 67 private:
68 CallbackRegistry<void>* cb_reg_; 68 CallbackRegistry<void(void)>* cb_reg_;
69 scoped_ptr<CallbackRegistry<void>::Subscription> subscription_; 69 scoped_ptr<CallbackRegistry<void(void)>::Subscription> subscription_;
70 DISALLOW_COPY_AND_ASSIGN(Adder); 70 DISALLOW_COPY_AND_ASSIGN(Adder);
71 }; 71 };
72 72
73 // Sanity check that closures added to the list will be run, and those removed 73 // Sanity check that closures added to the list will be run, and those removed
74 // from the list will not be run. 74 // from the list will not be run.
75 TEST(CallbackRegistryTest, BasicTest) { 75 TEST(CallbackRegistryTest, BasicTest) {
76 CallbackRegistry<void> cb_reg; 76 CallbackRegistry<void(void)> cb_reg;
77 Listener a, b, c; 77 Listener a, b, c;
78 78
79 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription = 79 scoped_ptr<CallbackRegistry<void(void)>::Subscription> a_subscription =
80 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); 80 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
81 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription = 81 scoped_ptr<CallbackRegistry<void(void)>::Subscription> b_subscription =
82 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); 82 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
83 83
84 EXPECT_TRUE(a_subscription.get()); 84 EXPECT_TRUE(a_subscription.get());
85 EXPECT_TRUE(b_subscription.get()); 85 EXPECT_TRUE(b_subscription.get());
86 86
87 cb_reg.Notify(); 87 cb_reg.Notify();
88 88
89 EXPECT_EQ(1, a.total_); 89 EXPECT_EQ(1, a.total_);
90 EXPECT_EQ(1, b.total_); 90 EXPECT_EQ(1, b.total_);
91 91
92 b_subscription.reset(); 92 b_subscription.reset();
93 93
94 scoped_ptr<CallbackRegistry<void>::Subscription> c_subscription = 94 scoped_ptr<CallbackRegistry<void(void)>::Subscription> c_subscription =
95 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c))); 95 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
96 96
97 cb_reg.Notify(); 97 cb_reg.Notify();
98 98
99 EXPECT_EQ(2, a.total_); 99 EXPECT_EQ(2, a.total_);
100 EXPECT_EQ(1, b.total_); 100 EXPECT_EQ(1, b.total_);
101 EXPECT_EQ(1, c.total_); 101 EXPECT_EQ(1, c.total_);
102 102
103 a_subscription.reset(); 103 a_subscription.reset();
104 b_subscription.reset(); 104 b_subscription.reset();
105 c_subscription.reset(); 105 c_subscription.reset();
106 } 106 }
107 107
108 // Sanity check that callbacks with details added to the list will be run, with 108 // Sanity check that callbacks with details added to the list will be run, with
109 // the correct details, and those removed from the list will not be run. 109 // the correct details, and those removed from the list will not be run.
110 TEST(CallbackRegistryTest, BasicTestWithParams) { 110 TEST(CallbackRegistryTest, BasicTestWithParams) {
111 CallbackRegistry<int> cb_reg; 111 CallbackRegistry<void(const int&)> cb_reg;
112 Listener a(1), b(-1), c(1); 112 Listener a(1), b(-1), c(1);
113 113
114 scoped_ptr<CallbackRegistry<int>::Subscription> a_subscription = 114 scoped_ptr<CallbackRegistry<void(const int&)>::Subscription> a_subscription =
115 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a))); 115 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a)));
116 scoped_ptr<CallbackRegistry<int>::Subscription> b_subscription = 116 scoped_ptr<CallbackRegistry<void(const int&)>::Subscription> b_subscription =
117 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b))); 117 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b)));
118 118
119 EXPECT_TRUE(a_subscription.get()); 119 EXPECT_TRUE(a_subscription.get());
120 EXPECT_TRUE(b_subscription.get()); 120 EXPECT_TRUE(b_subscription.get());
121 121
122 cb_reg.Notify(10); 122 cb_reg.Notify(10);
123 123
124 EXPECT_EQ(10, a.total_); 124 EXPECT_EQ(10, a.total_);
125 EXPECT_EQ(-10, b.total_); 125 EXPECT_EQ(-10, b.total_);
126 126
127 b_subscription.reset(); 127 b_subscription.reset();
128 128
129 scoped_ptr<CallbackRegistry<int>::Subscription> c_subscription = 129 scoped_ptr<CallbackRegistry<void(const int&)>::Subscription> c_subscription =
130 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c))); 130 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c)));
131 131
132 cb_reg.Notify(10); 132 cb_reg.Notify(10);
133 133
134 EXPECT_EQ(20, a.total_); 134 EXPECT_EQ(20, a.total_);
135 EXPECT_EQ(-10, b.total_); 135 EXPECT_EQ(-10, b.total_);
136 EXPECT_EQ(10, c.total_); 136 EXPECT_EQ(10, c.total_);
137 137
138 a_subscription.reset(); 138 a_subscription.reset();
139 b_subscription.reset(); 139 b_subscription.reset();
140 c_subscription.reset(); 140 c_subscription.reset();
141 } 141 }
142 142
143 // Test the a callback can remove itself or a different callback from the list 143 // Test the a callback can remove itself or a different callback from the list
144 // during iteration without invalidating the iterator. 144 // during iteration without invalidating the iterator.
145 TEST(CallbackRegistryTest, RemoveCallbacksDuringIteration) { 145 TEST(CallbackRegistryTest, RemoveCallbacksDuringIteration) {
146 CallbackRegistry<void> cb_reg; 146 CallbackRegistry<void(void)> cb_reg;
147 Listener a, b; 147 Listener a, b;
148 Remover remover_1, remover_2; 148 Remover remover_1, remover_2;
149 149
150 scoped_ptr<CallbackRegistry<void>::Subscription> remover_1_subscription = 150 scoped_ptr<CallbackRegistry<void(void)>::Subscription> remover_1_sub =
151 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove, 151 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
152 Unretained(&remover_1))); 152 Unretained(&remover_1)));
153 scoped_ptr<CallbackRegistry<void>::Subscription> remover_2_subscription = 153 scoped_ptr<CallbackRegistry<void(void)>::Subscription> remover_2_sub =
154 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove, 154 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
155 Unretained(&remover_2))); 155 Unretained(&remover_2)));
156 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription = 156 scoped_ptr<CallbackRegistry<void(void)>::Subscription> a_subscription =
157 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); 157 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
158 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription = 158 scoped_ptr<CallbackRegistry<void(void)>::Subscription> b_subscription =
159 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); 159 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
160 160
161 // |remover_1| will remove itself. 161 // |remover_1| will remove itself.
162 remover_1.SetSubscriptionToRemove(remover_1_subscription.Pass()); 162 remover_1.SetSubscriptionToRemove(remover_1_sub.Pass());
163 // |remover_2| will remove a. 163 // |remover_2| will remove a.
164 remover_2.SetSubscriptionToRemove(a_subscription.Pass()); 164 remover_2.SetSubscriptionToRemove(a_subscription.Pass());
165 165
166 cb_reg.Notify(); 166 cb_reg.Notify();
167 167
168 // |remover_1| runs once (and removes itself), |remover_2| runs once (and 168 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
169 // removes a), |a| never runs, and |b| runs once. 169 // removes a), |a| never runs, and |b| runs once.
170 EXPECT_EQ(1, remover_1.total_); 170 EXPECT_EQ(1, remover_1.total_);
171 EXPECT_EQ(1, remover_2.total_); 171 EXPECT_EQ(1, remover_2.total_);
172 EXPECT_EQ(0, a.total_); 172 EXPECT_EQ(0, a.total_);
173 EXPECT_EQ(1, b.total_); 173 EXPECT_EQ(1, b.total_);
174 174
175 cb_reg.Notify(); 175 cb_reg.Notify();
176 176
177 // Only |remover_2| and |b| run this time. 177 // Only |remover_2| and |b| run this time.
178 EXPECT_EQ(1, remover_1.total_); 178 EXPECT_EQ(1, remover_1.total_);
179 EXPECT_EQ(2, remover_2.total_); 179 EXPECT_EQ(2, remover_2.total_);
180 EXPECT_EQ(0, a.total_); 180 EXPECT_EQ(0, a.total_);
181 EXPECT_EQ(2, b.total_); 181 EXPECT_EQ(2, b.total_);
182 } 182 }
183 183
184 // Test that a callback can add another callback to the list durning iteration 184 // Test that a callback can add another callback to the list durning iteration
185 // without invalidating the iterator. The newly added callback should be run on 185 // without invalidating the iterator. The newly added callback should be run on
186 // the current iteration as will all other callbacks in the list. 186 // the current iteration as will all other callbacks in the list.
187 TEST(CallbackRegistryTest, AddCallbacksDuringIteration) { 187 TEST(CallbackRegistryTest, AddCallbacksDuringIteration) {
188 CallbackRegistry<void> cb_reg; 188 CallbackRegistry<void(void)> cb_reg;
189 Adder a(&cb_reg); 189 Adder a(&cb_reg);
190 Listener b; 190 Listener b;
191 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription = 191 scoped_ptr<CallbackRegistry<void(void)>::Subscription> a_subscription =
192 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a))); 192 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
193 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription = 193 scoped_ptr<CallbackRegistry<void(void)>::Subscription> b_subscription =
194 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); 194 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
195 195
196 cb_reg.Notify(); 196 cb_reg.Notify();
197 197
198 EXPECT_EQ(1, a.total_); 198 EXPECT_EQ(1, a.total_);
199 EXPECT_EQ(1, b.total_); 199 EXPECT_EQ(1, b.total_);
200 EXPECT_TRUE(a.added_); 200 EXPECT_TRUE(a.added_);
201 201
202 cb_reg.Notify(); 202 cb_reg.Notify();
203 203
204 EXPECT_EQ(2, a.total_); 204 EXPECT_EQ(2, a.total_);
205 EXPECT_EQ(2, b.total_); 205 EXPECT_EQ(2, b.total_);
206 } 206 }
207 207
208 // Sanity check: notifying an empty list is a no-op. 208 // Sanity check: notifying an empty list is a no-op.
209 TEST(CallbackRegistryTest, EmptyList) { 209 TEST(CallbackRegistryTest, EmptyList) {
210 CallbackRegistry<void> cb_reg; 210 CallbackRegistry<void(void)> cb_reg;
211 211
212 cb_reg.Notify(); 212 cb_reg.Notify();
213 } 213 }
214 214
215 } // namespace 215 } // namespace
216 } // namespace base 216 } // namespace base
OLDNEW
« base/callback_registry.h ('K') | « base/callback_registry.h.pump ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698