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
3 // found in the LICENSE file.
4
5 #include "base/callback_list.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/macros.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16 namespace {
17
18 class Listener {
19 public:
Listener()20 Listener() : total_(0), scaler_(1) {}
Listener(int scaler)21 explicit Listener(int scaler) : total_(0), scaler_(scaler) {}
IncrementTotal()22 void IncrementTotal() { total_++; }
IncrementByMultipleOfScaler(int x)23 void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; }
24
total() const25 int total() const { return total_; }
26
27 private:
28 int total_;
29 int scaler_;
30 DISALLOW_COPY_AND_ASSIGN(Listener);
31 };
32
33 class Remover {
34 public:
Remover()35 Remover() : total_(0) {}
IncrementTotalAndRemove()36 void IncrementTotalAndRemove() {
37 total_++;
38 removal_subscription_.reset();
39 }
SetSubscriptionToRemove(std::unique_ptr<CallbackList<void (void)>::Subscription> sub)40 void SetSubscriptionToRemove(
41 std::unique_ptr<CallbackList<void(void)>::Subscription> sub) {
42 removal_subscription_ = std::move(sub);
43 }
44
total() const45 int total() const { return total_; }
46
47 private:
48 int total_;
49 std::unique_ptr<CallbackList<void(void)>::Subscription> removal_subscription_;
50 DISALLOW_COPY_AND_ASSIGN(Remover);
51 };
52
53 class Adder {
54 public:
Adder(CallbackList<void (void)> * cb_reg)55 explicit Adder(CallbackList<void(void)>* cb_reg)
56 : added_(false),
57 total_(0),
58 cb_reg_(cb_reg) {
59 }
AddCallback()60 void AddCallback() {
61 if (!added_) {
62 added_ = true;
63 subscription_ =
64 cb_reg_->Add(BindRepeating(&Adder::IncrementTotal, Unretained(this)));
65 }
66 }
IncrementTotal()67 void IncrementTotal() { total_++; }
68
added() const69 bool added() const { return added_; }
70
total() const71 int total() const { return total_; }
72
73 private:
74 bool added_;
75 int total_;
76 CallbackList<void(void)>* cb_reg_;
77 std::unique_ptr<CallbackList<void(void)>::Subscription> subscription_;
78 DISALLOW_COPY_AND_ASSIGN(Adder);
79 };
80
81 class Summer {
82 public:
Summer()83 Summer() : value_(0) {}
84
AddOneParam(int a)85 void AddOneParam(int a) { value_ = a; }
AddTwoParam(int a,int b)86 void AddTwoParam(int a, int b) { value_ = a + b; }
AddThreeParam(int a,int b,int c)87 void AddThreeParam(int a, int b, int c) { value_ = a + b + c; }
AddFourParam(int a,int b,int c,int d)88 void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; }
AddFiveParam(int a,int b,int c,int d,int e)89 void AddFiveParam(int a, int b, int c, int d, int e) {
90 value_ = a + b + c + d + e;
91 }
AddSixParam(int a,int b,int c,int d,int e,int f)92 void AddSixParam(int a, int b, int c, int d, int e , int f) {
93 value_ = a + b + c + d + e + f;
94 }
95
value() const96 int value() const { return value_; }
97
98 private:
99 int value_;
100 DISALLOW_COPY_AND_ASSIGN(Summer);
101 };
102
103 class Counter {
104 public:
Counter()105 Counter() : value_(0) {}
106
Increment()107 void Increment() { value_++; }
108
value() const109 int value() const { return value_; }
110
111 private:
112 int value_;
113 DISALLOW_COPY_AND_ASSIGN(Counter);
114 };
115
116 // Sanity check that we can instantiate a CallbackList for each arity.
TEST(CallbackListTest,ArityTest)117 TEST(CallbackListTest, ArityTest) {
118 Summer s;
119
120 CallbackList<void(int)> c1;
121 std::unique_ptr<CallbackList<void(int)>::Subscription> subscription1 =
122 c1.Add(BindRepeating(&Summer::AddOneParam, Unretained(&s)));
123
124 c1.Notify(1);
125 EXPECT_EQ(1, s.value());
126
127 CallbackList<void(int, int)> c2;
128 std::unique_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
129 c2.Add(BindRepeating(&Summer::AddTwoParam, Unretained(&s)));
130
131 c2.Notify(1, 2);
132 EXPECT_EQ(3, s.value());
133
134 CallbackList<void(int, int, int)> c3;
135 std::unique_ptr<CallbackList<void(int, int, int)>::Subscription>
136 subscription3 =
137 c3.Add(BindRepeating(&Summer::AddThreeParam, Unretained(&s)));
138
139 c3.Notify(1, 2, 3);
140 EXPECT_EQ(6, s.value());
141
142 CallbackList<void(int, int, int, int)> c4;
143 std::unique_ptr<CallbackList<void(int, int, int, int)>::Subscription>
144 subscription4 =
145 c4.Add(BindRepeating(&Summer::AddFourParam, Unretained(&s)));
146
147 c4.Notify(1, 2, 3, 4);
148 EXPECT_EQ(10, s.value());
149
150 CallbackList<void(int, int, int, int, int)> c5;
151 std::unique_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
152 subscription5 =
153 c5.Add(BindRepeating(&Summer::AddFiveParam, Unretained(&s)));
154
155 c5.Notify(1, 2, 3, 4, 5);
156 EXPECT_EQ(15, s.value());
157
158 CallbackList<void(int, int, int, int, int, int)> c6;
159 std::unique_ptr<
160 CallbackList<void(int, int, int, int, int, int)>::Subscription>
161 subscription6 =
162 c6.Add(BindRepeating(&Summer::AddSixParam, Unretained(&s)));
163
164 c6.Notify(1, 2, 3, 4, 5, 6);
165 EXPECT_EQ(21, s.value());
166 }
167
168 // Sanity check that closures added to the list will be run, and those removed
169 // from the list will not be run.
TEST(CallbackListTest,BasicTest)170 TEST(CallbackListTest, BasicTest) {
171 CallbackList<void(void)> cb_reg;
172 Listener a, b, c;
173
174 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
175 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a)));
176 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
177 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b)));
178
179 EXPECT_TRUE(a_subscription.get());
180 EXPECT_TRUE(b_subscription.get());
181
182 cb_reg.Notify();
183
184 EXPECT_EQ(1, a.total());
185 EXPECT_EQ(1, b.total());
186
187 b_subscription.reset();
188
189 std::unique_ptr<CallbackList<void(void)>::Subscription> c_subscription =
190 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&c)));
191
192 cb_reg.Notify();
193
194 EXPECT_EQ(2, a.total());
195 EXPECT_EQ(1, b.total());
196 EXPECT_EQ(1, c.total());
197
198 a_subscription.reset();
199 b_subscription.reset();
200 c_subscription.reset();
201 }
202
203 // Sanity check that callbacks with details added to the list will be run, with
204 // the correct details, and those removed from the list will not be run.
TEST(CallbackListTest,BasicTestWithParams)205 TEST(CallbackListTest, BasicTestWithParams) {
206 CallbackList<void(int)> cb_reg;
207 Listener a(1), b(-1), c(1);
208
209 std::unique_ptr<CallbackList<void(int)>::Subscription> a_subscription =
210 cb_reg.Add(BindRepeating(&Listener::IncrementByMultipleOfScaler,
211 Unretained(&a)));
212 std::unique_ptr<CallbackList<void(int)>::Subscription> b_subscription =
213 cb_reg.Add(BindRepeating(&Listener::IncrementByMultipleOfScaler,
214 Unretained(&b)));
215
216 EXPECT_TRUE(a_subscription.get());
217 EXPECT_TRUE(b_subscription.get());
218
219 cb_reg.Notify(10);
220
221 EXPECT_EQ(10, a.total());
222 EXPECT_EQ(-10, b.total());
223
224 b_subscription.reset();
225
226 std::unique_ptr<CallbackList<void(int)>::Subscription> c_subscription =
227 cb_reg.Add(BindRepeating(&Listener::IncrementByMultipleOfScaler,
228 Unretained(&c)));
229
230 cb_reg.Notify(10);
231
232 EXPECT_EQ(20, a.total());
233 EXPECT_EQ(-10, b.total());
234 EXPECT_EQ(10, c.total());
235
236 a_subscription.reset();
237 b_subscription.reset();
238 c_subscription.reset();
239 }
240
241 // Test the a callback can remove itself or a different callback from the list
242 // during iteration without invalidating the iterator.
TEST(CallbackListTest,RemoveCallbacksDuringIteration)243 TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
244 CallbackList<void(void)> cb_reg;
245 Listener a, b;
246 Remover remover_1, remover_2;
247
248 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
249 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
250 Unretained(&remover_1)));
251 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
252 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
253 Unretained(&remover_2)));
254 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
255 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a)));
256 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
257 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b)));
258
259 // |remover_1| will remove itself.
260 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
261 // |remover_2| will remove a.
262 remover_2.SetSubscriptionToRemove(std::move(a_subscription));
263
264 cb_reg.Notify();
265
266 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
267 // removes a), |a| never runs, and |b| runs once.
268 EXPECT_EQ(1, remover_1.total());
269 EXPECT_EQ(1, remover_2.total());
270 EXPECT_EQ(0, a.total());
271 EXPECT_EQ(1, b.total());
272
273 cb_reg.Notify();
274
275 // Only |remover_2| and |b| run this time.
276 EXPECT_EQ(1, remover_1.total());
277 EXPECT_EQ(2, remover_2.total());
278 EXPECT_EQ(0, a.total());
279 EXPECT_EQ(2, b.total());
280 }
281
282 // Test that a callback can add another callback to the list durning iteration
283 // without invalidating the iterator. The newly added callback should be run on
284 // the current iteration as will all other callbacks in the list.
TEST(CallbackListTest,AddCallbacksDuringIteration)285 TEST(CallbackListTest, AddCallbacksDuringIteration) {
286 CallbackList<void(void)> cb_reg;
287 Adder a(&cb_reg);
288 Listener b;
289 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
290 cb_reg.Add(BindRepeating(&Adder::AddCallback, Unretained(&a)));
291 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
292 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b)));
293
294 cb_reg.Notify();
295
296 EXPECT_EQ(1, a.total());
297 EXPECT_EQ(1, b.total());
298 EXPECT_TRUE(a.added());
299
300 cb_reg.Notify();
301
302 EXPECT_EQ(2, a.total());
303 EXPECT_EQ(2, b.total());
304 }
305
306 // Sanity check: notifying an empty list is a no-op.
TEST(CallbackListTest,EmptyList)307 TEST(CallbackListTest, EmptyList) {
308 CallbackList<void(void)> cb_reg;
309
310 cb_reg.Notify();
311 }
312
TEST(CallbackList,RemovalCallback)313 TEST(CallbackList, RemovalCallback) {
314 Counter remove_count;
315 CallbackList<void(void)> cb_reg;
316 cb_reg.set_removal_callback(
317 BindRepeating(&Counter::Increment, Unretained(&remove_count)));
318
319 std::unique_ptr<CallbackList<void(void)>::Subscription> subscription =
320 cb_reg.Add(DoNothing());
321
322 // Removing a subscription outside of iteration signals the callback.
323 EXPECT_EQ(0, remove_count.value());
324 subscription.reset();
325 EXPECT_EQ(1, remove_count.value());
326
327 // Configure two subscriptions to remove themselves.
328 Remover remover_1, remover_2;
329 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
330 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
331 Unretained(&remover_1)));
332 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
333 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
334 Unretained(&remover_2)));
335 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
336 remover_2.SetSubscriptionToRemove(std::move(remover_2_sub));
337
338 // The callback should be signaled exactly once.
339 EXPECT_EQ(1, remove_count.value());
340 cb_reg.Notify();
341 EXPECT_EQ(2, remove_count.value());
342 EXPECT_TRUE(cb_reg.empty());
343 }
344
TEST(CallbackList,AbandonSubscriptions)345 TEST(CallbackList, AbandonSubscriptions) {
346 Listener listener;
347 std::unique_ptr<CallbackList<void(void)>::Subscription> subscription;
348 {
349 CallbackList<void(void)> cb_reg;
350 subscription = cb_reg.Add(
351 BindRepeating(&Listener::IncrementTotal, Unretained(&listener)));
352 // Make sure the callback is signaled while cb_reg is in scope.
353 cb_reg.Notify();
354 // Exiting this scope and running the cb_reg destructor shouldn't fail.
355 }
356 EXPECT_EQ(1, listener.total());
357 // The subscription from the destroyed callback list should be cancelled now.
358 EXPECT_TRUE(subscription->IsCancelled());
359 }
360
361 } // namespace
362 } // namespace base
363