blob: 71e781c40d4d03c07b50a33860c851630c50b996 [file] [log] [blame]
[email protected]893c8162013-09-11 15:16:331// 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
[email protected]2a7cac02013-09-26 19:20:185#include "base/callback_list.h"
[email protected]893c8162013-09-11 15:16:336
dcheng9dfa1232015-12-15 05:11:177#include <utility>
8
[email protected]893c8162013-09-11 15:16:339#include "base/basictypes.h"
10#include "base/bind.h"
11#include "base/bind_helpers.h"
12#include "base/memory/scoped_ptr.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace base {
16namespace {
17
18class Listener {
19 public:
20 Listener() : total_(0), scaler_(1) {}
21 explicit Listener(int scaler) : total_(0), scaler_(scaler) {}
22 void IncrementTotal() { total_++; }
[email protected]243576f2013-09-25 13:56:2323 void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; }
[email protected]893c8162013-09-11 15:16:3324
[email protected]abdd66e2013-10-09 23:28:2125 int total() const { return total_; }
[email protected]893c8162013-09-11 15:16:3326
27 private:
[email protected]abdd66e2013-10-09 23:28:2128 int total_;
[email protected]893c8162013-09-11 15:16:3329 int scaler_;
30 DISALLOW_COPY_AND_ASSIGN(Listener);
31};
32
33class Remover {
34 public:
35 Remover() : total_(0) {}
36 void IncrementTotalAndRemove() {
37 total_++;
38 removal_subscription_.reset();
39 }
40 void SetSubscriptionToRemove(
[email protected]2a7cac02013-09-26 19:20:1841 scoped_ptr<CallbackList<void(void)>::Subscription> sub) {
dcheng9dfa1232015-12-15 05:11:1742 removal_subscription_ = std::move(sub);
[email protected]893c8162013-09-11 15:16:3343 }
44
[email protected]abdd66e2013-10-09 23:28:2145 int total() const { return total_; }
[email protected]893c8162013-09-11 15:16:3346
47 private:
[email protected]abdd66e2013-10-09 23:28:2148 int total_;
[email protected]2a7cac02013-09-26 19:20:1849 scoped_ptr<CallbackList<void(void)>::Subscription> removal_subscription_;
[email protected]893c8162013-09-11 15:16:3350 DISALLOW_COPY_AND_ASSIGN(Remover);
51};
52
53class Adder {
54 public:
[email protected]2a7cac02013-09-26 19:20:1855 explicit Adder(CallbackList<void(void)>* cb_reg)
[email protected]893c8162013-09-11 15:16:3356 : added_(false),
57 total_(0),
[email protected]abdd66e2013-10-09 23:28:2158 cb_reg_(cb_reg) {
59 }
[email protected]893c8162013-09-11 15:16:3360 void AddCallback() {
61 if (!added_) {
62 added_ = true;
63 subscription_ =
64 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this)));
65 }
66 }
67 void IncrementTotal() { total_++; }
68
[email protected]abdd66e2013-10-09 23:28:2169 bool added() const { return added_; }
70
71 int total() const { return total_; }
[email protected]893c8162013-09-11 15:16:3372
73 private:
[email protected]abdd66e2013-10-09 23:28:2174 bool added_;
75 int total_;
[email protected]2a7cac02013-09-26 19:20:1876 CallbackList<void(void)>* cb_reg_;
77 scoped_ptr<CallbackList<void(void)>::Subscription> subscription_;
[email protected]893c8162013-09-11 15:16:3378 DISALLOW_COPY_AND_ASSIGN(Adder);
79};
80
[email protected]243576f2013-09-25 13:56:2381class Summer {
82 public:
83 Summer() : value_(0) {}
84
85 void AddOneParam(int a) { value_ = a; }
86 void AddTwoParam(int a, int b) { value_ = a + b; }
87 void AddThreeParam(int a, int b, int c) { value_ = a + b + c; }
88 void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; }
89 void AddFiveParam(int a, int b, int c, int d, int e) {
90 value_ = a + b + c + d + e;
91 }
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
[email protected]abdd66e2013-10-09 23:28:2196 int value() const { return value_; }
[email protected]243576f2013-09-25 13:56:2397
98 private:
[email protected]abdd66e2013-10-09 23:28:2199 int value_;
[email protected]243576f2013-09-25 13:56:23100 DISALLOW_COPY_AND_ASSIGN(Summer);
101};
102
davidbenf126cb22015-10-30 20:42:07103class Counter {
104 public:
105 Counter() : value_(0) {}
106
107 void Increment() { value_++; }
108
109 int value() const { return value_; }
110
111 private:
112 int value_;
113 DISALLOW_COPY_AND_ASSIGN(Counter);
114};
115
[email protected]2a7cac02013-09-26 19:20:18116// Sanity check that we can instantiate a CallbackList for each arity.
117TEST(CallbackListTest, ArityTest) {
[email protected]243576f2013-09-25 13:56:23118 Summer s;
119
[email protected]2a7cac02013-09-26 19:20:18120 CallbackList<void(int)> c1;
121 scoped_ptr<CallbackList<void(int)>::Subscription> subscription1 =
[email protected]243576f2013-09-25 13:56:23122 c1.Add(Bind(&Summer::AddOneParam, Unretained(&s)));
123
124 c1.Notify(1);
[email protected]abdd66e2013-10-09 23:28:21125 EXPECT_EQ(1, s.value());
[email protected]243576f2013-09-25 13:56:23126
[email protected]2a7cac02013-09-26 19:20:18127 CallbackList<void(int, int)> c2;
128 scoped_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
[email protected]243576f2013-09-25 13:56:23129 c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s)));
130
131 c2.Notify(1, 2);
[email protected]abdd66e2013-10-09 23:28:21132 EXPECT_EQ(3, s.value());
[email protected]243576f2013-09-25 13:56:23133
[email protected]2a7cac02013-09-26 19:20:18134 CallbackList<void(int, int, int)> c3;
135 scoped_ptr<CallbackList<void(int, int, int)>::Subscription>
[email protected]243576f2013-09-25 13:56:23136 subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s)));
137
138 c3.Notify(1, 2, 3);
[email protected]abdd66e2013-10-09 23:28:21139 EXPECT_EQ(6, s.value());
[email protected]243576f2013-09-25 13:56:23140
[email protected]2a7cac02013-09-26 19:20:18141 CallbackList<void(int, int, int, int)> c4;
142 scoped_ptr<CallbackList<void(int, int, int, int)>::Subscription>
[email protected]243576f2013-09-25 13:56:23143 subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s)));
144
145 c4.Notify(1, 2, 3, 4);
[email protected]abdd66e2013-10-09 23:28:21146 EXPECT_EQ(10, s.value());
[email protected]243576f2013-09-25 13:56:23147
[email protected]2a7cac02013-09-26 19:20:18148 CallbackList<void(int, int, int, int, int)> c5;
149 scoped_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
[email protected]243576f2013-09-25 13:56:23150 subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s)));
151
152 c5.Notify(1, 2, 3, 4, 5);
[email protected]abdd66e2013-10-09 23:28:21153 EXPECT_EQ(15, s.value());
[email protected]243576f2013-09-25 13:56:23154
[email protected]2a7cac02013-09-26 19:20:18155 CallbackList<void(int, int, int, int, int, int)> c6;
156 scoped_ptr<CallbackList<void(int, int, int, int, int, int)>::Subscription>
[email protected]243576f2013-09-25 13:56:23157 subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s)));
158
159 c6.Notify(1, 2, 3, 4, 5, 6);
[email protected]abdd66e2013-10-09 23:28:21160 EXPECT_EQ(21, s.value());
[email protected]243576f2013-09-25 13:56:23161}
162
[email protected]893c8162013-09-11 15:16:33163// Sanity check that closures added to the list will be run, and those removed
164// from the list will not be run.
[email protected]2a7cac02013-09-26 19:20:18165TEST(CallbackListTest, BasicTest) {
166 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33167 Listener a, b, c;
168
[email protected]2a7cac02013-09-26 19:20:18169 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
[email protected]893c8162013-09-11 15:16:33170 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
[email protected]2a7cac02013-09-26 19:20:18171 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
[email protected]893c8162013-09-11 15:16:33172 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
173
174 EXPECT_TRUE(a_subscription.get());
175 EXPECT_TRUE(b_subscription.get());
176
177 cb_reg.Notify();
178
[email protected]abdd66e2013-10-09 23:28:21179 EXPECT_EQ(1, a.total());
180 EXPECT_EQ(1, b.total());
[email protected]893c8162013-09-11 15:16:33181
182 b_subscription.reset();
183
[email protected]2a7cac02013-09-26 19:20:18184 scoped_ptr<CallbackList<void(void)>::Subscription> c_subscription =
[email protected]893c8162013-09-11 15:16:33185 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
186
187 cb_reg.Notify();
188
[email protected]abdd66e2013-10-09 23:28:21189 EXPECT_EQ(2, a.total());
190 EXPECT_EQ(1, b.total());
191 EXPECT_EQ(1, c.total());
[email protected]893c8162013-09-11 15:16:33192
193 a_subscription.reset();
194 b_subscription.reset();
195 c_subscription.reset();
196}
197
198// Sanity check that callbacks with details added to the list will be run, with
199// the correct details, and those removed from the list will not be run.
[email protected]2a7cac02013-09-26 19:20:18200TEST(CallbackListTest, BasicTestWithParams) {
201 CallbackList<void(int)> cb_reg;
[email protected]893c8162013-09-11 15:16:33202 Listener a(1), b(-1), c(1);
203
[email protected]2a7cac02013-09-26 19:20:18204 scoped_ptr<CallbackList<void(int)>::Subscription> a_subscription =
[email protected]893c8162013-09-11 15:16:33205 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a)));
[email protected]2a7cac02013-09-26 19:20:18206 scoped_ptr<CallbackList<void(int)>::Subscription> b_subscription =
[email protected]893c8162013-09-11 15:16:33207 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b)));
208
209 EXPECT_TRUE(a_subscription.get());
210 EXPECT_TRUE(b_subscription.get());
211
212 cb_reg.Notify(10);
213
[email protected]abdd66e2013-10-09 23:28:21214 EXPECT_EQ(10, a.total());
215 EXPECT_EQ(-10, b.total());
[email protected]893c8162013-09-11 15:16:33216
217 b_subscription.reset();
218
[email protected]2a7cac02013-09-26 19:20:18219 scoped_ptr<CallbackList<void(int)>::Subscription> c_subscription =
[email protected]893c8162013-09-11 15:16:33220 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c)));
221
222 cb_reg.Notify(10);
223
[email protected]abdd66e2013-10-09 23:28:21224 EXPECT_EQ(20, a.total());
225 EXPECT_EQ(-10, b.total());
226 EXPECT_EQ(10, c.total());
[email protected]893c8162013-09-11 15:16:33227
228 a_subscription.reset();
229 b_subscription.reset();
230 c_subscription.reset();
231}
232
233// Test the a callback can remove itself or a different callback from the list
234// during iteration without invalidating the iterator.
[email protected]2a7cac02013-09-26 19:20:18235TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
236 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33237 Listener a, b;
238 Remover remover_1, remover_2;
239
[email protected]2a7cac02013-09-26 19:20:18240 scoped_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
[email protected]893c8162013-09-11 15:16:33241 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
242 Unretained(&remover_1)));
[email protected]2a7cac02013-09-26 19:20:18243 scoped_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
[email protected]893c8162013-09-11 15:16:33244 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
245 Unretained(&remover_2)));
[email protected]2a7cac02013-09-26 19:20:18246 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
[email protected]893c8162013-09-11 15:16:33247 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
[email protected]2a7cac02013-09-26 19:20:18248 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
[email protected]893c8162013-09-11 15:16:33249 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
250
251 // |remover_1| will remove itself.
dcheng9dfa1232015-12-15 05:11:17252 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
[email protected]893c8162013-09-11 15:16:33253 // |remover_2| will remove a.
dcheng9dfa1232015-12-15 05:11:17254 remover_2.SetSubscriptionToRemove(std::move(a_subscription));
[email protected]893c8162013-09-11 15:16:33255
256 cb_reg.Notify();
257
258 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
259 // removes a), |a| never runs, and |b| runs once.
[email protected]abdd66e2013-10-09 23:28:21260 EXPECT_EQ(1, remover_1.total());
261 EXPECT_EQ(1, remover_2.total());
262 EXPECT_EQ(0, a.total());
263 EXPECT_EQ(1, b.total());
[email protected]893c8162013-09-11 15:16:33264
265 cb_reg.Notify();
266
267 // Only |remover_2| and |b| run this time.
[email protected]abdd66e2013-10-09 23:28:21268 EXPECT_EQ(1, remover_1.total());
269 EXPECT_EQ(2, remover_2.total());
270 EXPECT_EQ(0, a.total());
271 EXPECT_EQ(2, b.total());
[email protected]893c8162013-09-11 15:16:33272}
273
274// Test that a callback can add another callback to the list durning iteration
275// without invalidating the iterator. The newly added callback should be run on
276// the current iteration as will all other callbacks in the list.
[email protected]2a7cac02013-09-26 19:20:18277TEST(CallbackListTest, AddCallbacksDuringIteration) {
278 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33279 Adder a(&cb_reg);
280 Listener b;
[email protected]2a7cac02013-09-26 19:20:18281 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
[email protected]893c8162013-09-11 15:16:33282 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
[email protected]2a7cac02013-09-26 19:20:18283 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
[email protected]893c8162013-09-11 15:16:33284 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
285
286 cb_reg.Notify();
287
[email protected]abdd66e2013-10-09 23:28:21288 EXPECT_EQ(1, a.total());
289 EXPECT_EQ(1, b.total());
290 EXPECT_TRUE(a.added());
[email protected]893c8162013-09-11 15:16:33291
292 cb_reg.Notify();
293
[email protected]abdd66e2013-10-09 23:28:21294 EXPECT_EQ(2, a.total());
295 EXPECT_EQ(2, b.total());
[email protected]893c8162013-09-11 15:16:33296}
297
298// Sanity check: notifying an empty list is a no-op.
[email protected]2a7cac02013-09-26 19:20:18299TEST(CallbackListTest, EmptyList) {
300 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33301
302 cb_reg.Notify();
303}
304
davidbenf126cb22015-10-30 20:42:07305TEST(CallbackList, RemovalCallback) {
306 Counter remove_count;
307 CallbackList<void(void)> cb_reg;
308 cb_reg.set_removal_callback(
309 Bind(&Counter::Increment, Unretained(&remove_count)));
310
311 scoped_ptr<CallbackList<void(void)>::Subscription> subscription =
312 cb_reg.Add(Bind(&DoNothing));
313
314 // Removing a subscription outside of iteration signals the callback.
315 EXPECT_EQ(0, remove_count.value());
316 subscription.reset();
317 EXPECT_EQ(1, remove_count.value());
318
319 // Configure two subscriptions to remove themselves.
320 Remover remover_1, remover_2;
321 scoped_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
322 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
323 Unretained(&remover_1)));
324 scoped_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
325 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
326 Unretained(&remover_2)));
dcheng9dfa1232015-12-15 05:11:17327 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
328 remover_2.SetSubscriptionToRemove(std::move(remover_2_sub));
davidbenf126cb22015-10-30 20:42:07329
330 // The callback should be signaled exactly once.
331 EXPECT_EQ(1, remove_count.value());
332 cb_reg.Notify();
333 EXPECT_EQ(2, remove_count.value());
334 EXPECT_TRUE(cb_reg.empty());
335}
336
[email protected]893c8162013-09-11 15:16:33337} // namespace
338} // namespace base