blob: e7a97afba49a4571d935551a8641131836129c65 [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
dcheng093de9b2016-04-04 21:25:517#include <memory>
dcheng9dfa1232015-12-15 05:11:178#include <utility>
9
[email protected]893c8162013-09-11 15:16:3310#include "base/bind.h"
11#include "base/bind_helpers.h"
avi9b6f42932015-12-26 22:15:1412#include "base/macros.h"
[email protected]893c8162013-09-11 15:16:3313#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(
dcheng093de9b2016-04-04 21:25:5141 std::unique_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_;
dcheng093de9b2016-04-04 21:25:5149 std::unique_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_ =
kylecharb2695fc2019-04-24 14:51:2064 cb_reg_->Add(BindRepeating(&Adder::IncrementTotal, Unretained(this)));
[email protected]893c8162013-09-11 15:16:3365 }
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_;
dcheng093de9b2016-04-04 21:25:5177 std::unique_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;
dcheng093de9b2016-04-04 21:25:51121 std::unique_ptr<CallbackList<void(int)>::Subscription> subscription1 =
kylecharb2695fc2019-04-24 14:51:20122 c1.Add(BindRepeating(&Summer::AddOneParam, Unretained(&s)));
[email protected]243576f2013-09-25 13:56:23123
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;
dcheng093de9b2016-04-04 21:25:51128 std::unique_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
kylecharb2695fc2019-04-24 14:51:20129 c2.Add(BindRepeating(&Summer::AddTwoParam, Unretained(&s)));
[email protected]243576f2013-09-25 13:56:23130
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;
dcheng093de9b2016-04-04 21:25:51135 std::unique_ptr<CallbackList<void(int, int, int)>::Subscription>
kylecharb2695fc2019-04-24 14:51:20136 subscription3 =
137 c3.Add(BindRepeating(&Summer::AddThreeParam, Unretained(&s)));
[email protected]243576f2013-09-25 13:56:23138
139 c3.Notify(1, 2, 3);
[email protected]abdd66e2013-10-09 23:28:21140 EXPECT_EQ(6, s.value());
[email protected]243576f2013-09-25 13:56:23141
[email protected]2a7cac02013-09-26 19:20:18142 CallbackList<void(int, int, int, int)> c4;
dcheng093de9b2016-04-04 21:25:51143 std::unique_ptr<CallbackList<void(int, int, int, int)>::Subscription>
kylecharb2695fc2019-04-24 14:51:20144 subscription4 =
145 c4.Add(BindRepeating(&Summer::AddFourParam, Unretained(&s)));
[email protected]243576f2013-09-25 13:56:23146
147 c4.Notify(1, 2, 3, 4);
[email protected]abdd66e2013-10-09 23:28:21148 EXPECT_EQ(10, s.value());
[email protected]243576f2013-09-25 13:56:23149
[email protected]2a7cac02013-09-26 19:20:18150 CallbackList<void(int, int, int, int, int)> c5;
dcheng093de9b2016-04-04 21:25:51151 std::unique_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
kylecharb2695fc2019-04-24 14:51:20152 subscription5 =
153 c5.Add(BindRepeating(&Summer::AddFiveParam, Unretained(&s)));
[email protected]243576f2013-09-25 13:56:23154
155 c5.Notify(1, 2, 3, 4, 5);
[email protected]abdd66e2013-10-09 23:28:21156 EXPECT_EQ(15, s.value());
[email protected]243576f2013-09-25 13:56:23157
[email protected]2a7cac02013-09-26 19:20:18158 CallbackList<void(int, int, int, int, int, int)> c6;
dcheng093de9b2016-04-04 21:25:51159 std::unique_ptr<
160 CallbackList<void(int, int, int, int, int, int)>::Subscription>
kylecharb2695fc2019-04-24 14:51:20161 subscription6 =
162 c6.Add(BindRepeating(&Summer::AddSixParam, Unretained(&s)));
[email protected]243576f2013-09-25 13:56:23163
164 c6.Notify(1, 2, 3, 4, 5, 6);
[email protected]abdd66e2013-10-09 23:28:21165 EXPECT_EQ(21, s.value());
[email protected]243576f2013-09-25 13:56:23166}
167
[email protected]893c8162013-09-11 15:16:33168// Sanity check that closures added to the list will be run, and those removed
169// from the list will not be run.
[email protected]2a7cac02013-09-26 19:20:18170TEST(CallbackListTest, BasicTest) {
171 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33172 Listener a, b, c;
173
dcheng093de9b2016-04-04 21:25:51174 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
kylecharb2695fc2019-04-24 14:51:20175 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a)));
dcheng093de9b2016-04-04 21:25:51176 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
kylecharb2695fc2019-04-24 14:51:20177 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b)));
[email protected]893c8162013-09-11 15:16:33178
179 EXPECT_TRUE(a_subscription.get());
180 EXPECT_TRUE(b_subscription.get());
181
182 cb_reg.Notify();
183
[email protected]abdd66e2013-10-09 23:28:21184 EXPECT_EQ(1, a.total());
185 EXPECT_EQ(1, b.total());
[email protected]893c8162013-09-11 15:16:33186
187 b_subscription.reset();
188
dcheng093de9b2016-04-04 21:25:51189 std::unique_ptr<CallbackList<void(void)>::Subscription> c_subscription =
kylecharb2695fc2019-04-24 14:51:20190 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&c)));
[email protected]893c8162013-09-11 15:16:33191
192 cb_reg.Notify();
193
[email protected]abdd66e2013-10-09 23:28:21194 EXPECT_EQ(2, a.total());
195 EXPECT_EQ(1, b.total());
196 EXPECT_EQ(1, c.total());
[email protected]893c8162013-09-11 15:16:33197
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.
[email protected]2a7cac02013-09-26 19:20:18205TEST(CallbackListTest, BasicTestWithParams) {
206 CallbackList<void(int)> cb_reg;
[email protected]893c8162013-09-11 15:16:33207 Listener a(1), b(-1), c(1);
208
dcheng093de9b2016-04-04 21:25:51209 std::unique_ptr<CallbackList<void(int)>::Subscription> a_subscription =
kylecharb2695fc2019-04-24 14:51:20210 cb_reg.Add(BindRepeating(&Listener::IncrementByMultipleOfScaler,
211 Unretained(&a)));
dcheng093de9b2016-04-04 21:25:51212 std::unique_ptr<CallbackList<void(int)>::Subscription> b_subscription =
kylecharb2695fc2019-04-24 14:51:20213 cb_reg.Add(BindRepeating(&Listener::IncrementByMultipleOfScaler,
214 Unretained(&b)));
[email protected]893c8162013-09-11 15:16:33215
216 EXPECT_TRUE(a_subscription.get());
217 EXPECT_TRUE(b_subscription.get());
218
219 cb_reg.Notify(10);
220
[email protected]abdd66e2013-10-09 23:28:21221 EXPECT_EQ(10, a.total());
222 EXPECT_EQ(-10, b.total());
[email protected]893c8162013-09-11 15:16:33223
224 b_subscription.reset();
225
dcheng093de9b2016-04-04 21:25:51226 std::unique_ptr<CallbackList<void(int)>::Subscription> c_subscription =
kylecharb2695fc2019-04-24 14:51:20227 cb_reg.Add(BindRepeating(&Listener::IncrementByMultipleOfScaler,
228 Unretained(&c)));
[email protected]893c8162013-09-11 15:16:33229
230 cb_reg.Notify(10);
231
[email protected]abdd66e2013-10-09 23:28:21232 EXPECT_EQ(20, a.total());
233 EXPECT_EQ(-10, b.total());
234 EXPECT_EQ(10, c.total());
[email protected]893c8162013-09-11 15:16:33235
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.
[email protected]2a7cac02013-09-26 19:20:18243TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
244 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33245 Listener a, b;
246 Remover remover_1, remover_2;
247
dcheng093de9b2016-04-04 21:25:51248 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
kylecharb2695fc2019-04-24 14:51:20249 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
250 Unretained(&remover_1)));
dcheng093de9b2016-04-04 21:25:51251 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
kylecharb2695fc2019-04-24 14:51:20252 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
253 Unretained(&remover_2)));
dcheng093de9b2016-04-04 21:25:51254 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
kylecharb2695fc2019-04-24 14:51:20255 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a)));
dcheng093de9b2016-04-04 21:25:51256 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
kylecharb2695fc2019-04-24 14:51:20257 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b)));
[email protected]893c8162013-09-11 15:16:33258
259 // |remover_1| will remove itself.
dcheng9dfa1232015-12-15 05:11:17260 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
[email protected]893c8162013-09-11 15:16:33261 // |remover_2| will remove a.
dcheng9dfa1232015-12-15 05:11:17262 remover_2.SetSubscriptionToRemove(std::move(a_subscription));
[email protected]893c8162013-09-11 15:16:33263
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.
[email protected]abdd66e2013-10-09 23:28:21268 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());
[email protected]893c8162013-09-11 15:16:33272
273 cb_reg.Notify();
274
275 // Only |remover_2| and |b| run this time.
[email protected]abdd66e2013-10-09 23:28:21276 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());
[email protected]893c8162013-09-11 15:16:33280}
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.
[email protected]2a7cac02013-09-26 19:20:18285TEST(CallbackListTest, AddCallbacksDuringIteration) {
286 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33287 Adder a(&cb_reg);
288 Listener b;
dcheng093de9b2016-04-04 21:25:51289 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
kylecharb2695fc2019-04-24 14:51:20290 cb_reg.Add(BindRepeating(&Adder::AddCallback, Unretained(&a)));
dcheng093de9b2016-04-04 21:25:51291 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
kylecharb2695fc2019-04-24 14:51:20292 cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b)));
[email protected]893c8162013-09-11 15:16:33293
294 cb_reg.Notify();
295
[email protected]abdd66e2013-10-09 23:28:21296 EXPECT_EQ(1, a.total());
297 EXPECT_EQ(1, b.total());
298 EXPECT_TRUE(a.added());
[email protected]893c8162013-09-11 15:16:33299
300 cb_reg.Notify();
301
[email protected]abdd66e2013-10-09 23:28:21302 EXPECT_EQ(2, a.total());
303 EXPECT_EQ(2, b.total());
[email protected]893c8162013-09-11 15:16:33304}
305
306// Sanity check: notifying an empty list is a no-op.
[email protected]2a7cac02013-09-26 19:20:18307TEST(CallbackListTest, EmptyList) {
308 CallbackList<void(void)> cb_reg;
[email protected]893c8162013-09-11 15:16:33309
310 cb_reg.Notify();
311}
312
davidbenf126cb22015-10-30 20:42:07313TEST(CallbackList, RemovalCallback) {
314 Counter remove_count;
315 CallbackList<void(void)> cb_reg;
316 cb_reg.set_removal_callback(
kylecharb2695fc2019-04-24 14:51:20317 BindRepeating(&Counter::Increment, Unretained(&remove_count)));
davidbenf126cb22015-10-30 20:42:07318
dcheng093de9b2016-04-04 21:25:51319 std::unique_ptr<CallbackList<void(void)>::Subscription> subscription =
Peter Kasting341e1fb2018-02-24 00:03:01320 cb_reg.Add(DoNothing());
davidbenf126cb22015-10-30 20:42:07321
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;
dcheng093de9b2016-04-04 21:25:51329 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
kylecharb2695fc2019-04-24 14:51:20330 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
331 Unretained(&remover_1)));
dcheng093de9b2016-04-04 21:25:51332 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
kylecharb2695fc2019-04-24 14:51:20333 cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove,
334 Unretained(&remover_2)));
dcheng9dfa1232015-12-15 05:11:17335 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
336 remover_2.SetSubscriptionToRemove(std::move(remover_2_sub));
davidbenf126cb22015-10-30 20:42:07337
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
Allen Bauer82686302019-05-14 22:37:47345TEST(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
[email protected]893c8162013-09-11 15:16:33361} // namespace
362} // namespace base