blob: 6eb5ff7f855f2497e7d82e1c06fe2b8f45f62555 [file] [log] [blame]
caitkp@chromium.org1268af12013-09-12 00:16:33 +09001// 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
caitkp@chromium.org49647322013-09-27 04:20:18 +09005#include "base/callback_list.h"
caitkp@chromium.org1268af12013-09-12 00:16:33 +09006
dchengcc8e4d82016-04-05 06:25:51 +09007#include <memory>
dcheng961ff4a2015-12-15 14:11:17 +09008#include <utility>
9
caitkp@chromium.org1268af12013-09-12 00:16:33 +090010#include "base/bind.h"
11#include "base/bind_helpers.h"
avia6a6a682015-12-27 07:15:14 +090012#include "base/macros.h"
caitkp@chromium.org1268af12013-09-12 00:16:33 +090013#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_++; }
caitkp@google.com2b10b742013-09-25 22:56:23 +090023 void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090024
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090025 int total() const { return total_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090026
27 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090028 int total_;
caitkp@chromium.org1268af12013-09-12 00:16:33 +090029 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(
dchengcc8e4d82016-04-05 06:25:51 +090041 std::unique_ptr<CallbackList<void(void)>::Subscription> sub) {
dcheng961ff4a2015-12-15 14:11:17 +090042 removal_subscription_ = std::move(sub);
caitkp@chromium.org1268af12013-09-12 00:16:33 +090043 }
44
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090045 int total() const { return total_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090046
47 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090048 int total_;
dchengcc8e4d82016-04-05 06:25:51 +090049 std::unique_ptr<CallbackList<void(void)>::Subscription> removal_subscription_;
caitkp@chromium.org1268af12013-09-12 00:16:33 +090050 DISALLOW_COPY_AND_ASSIGN(Remover);
51};
52
53class Adder {
54 public:
caitkp@chromium.org49647322013-09-27 04:20:18 +090055 explicit Adder(CallbackList<void(void)>* cb_reg)
caitkp@chromium.org1268af12013-09-12 00:16:33 +090056 : added_(false),
57 total_(0),
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090058 cb_reg_(cb_reg) {
59 }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090060 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
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090069 bool added() const { return added_; }
70
71 int total() const { return total_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090072
73 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090074 bool added_;
75 int total_;
caitkp@chromium.org49647322013-09-27 04:20:18 +090076 CallbackList<void(void)>* cb_reg_;
dchengcc8e4d82016-04-05 06:25:51 +090077 std::unique_ptr<CallbackList<void(void)>::Subscription> subscription_;
caitkp@chromium.org1268af12013-09-12 00:16:33 +090078 DISALLOW_COPY_AND_ASSIGN(Adder);
79};
80
caitkp@google.com2b10b742013-09-25 22:56:23 +090081class 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
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090096 int value() const { return value_; }
caitkp@google.com2b10b742013-09-25 22:56:23 +090097
98 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090099 int value_;
caitkp@google.com2b10b742013-09-25 22:56:23 +0900100 DISALLOW_COPY_AND_ASSIGN(Summer);
101};
102
davidben4f5f0432015-10-31 05:42:07 +0900103class 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
caitkp@chromium.org49647322013-09-27 04:20:18 +0900116// Sanity check that we can instantiate a CallbackList for each arity.
117TEST(CallbackListTest, ArityTest) {
caitkp@google.com2b10b742013-09-25 22:56:23 +0900118 Summer s;
119
caitkp@chromium.org49647322013-09-27 04:20:18 +0900120 CallbackList<void(int)> c1;
dchengcc8e4d82016-04-05 06:25:51 +0900121 std::unique_ptr<CallbackList<void(int)>::Subscription> subscription1 =
caitkp@google.com2b10b742013-09-25 22:56:23 +0900122 c1.Add(Bind(&Summer::AddOneParam, Unretained(&s)));
123
124 c1.Notify(1);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900125 EXPECT_EQ(1, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900126
caitkp@chromium.org49647322013-09-27 04:20:18 +0900127 CallbackList<void(int, int)> c2;
dchengcc8e4d82016-04-05 06:25:51 +0900128 std::unique_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
caitkp@google.com2b10b742013-09-25 22:56:23 +0900129 c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s)));
130
131 c2.Notify(1, 2);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900132 EXPECT_EQ(3, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900133
caitkp@chromium.org49647322013-09-27 04:20:18 +0900134 CallbackList<void(int, int, int)> c3;
dchengcc8e4d82016-04-05 06:25:51 +0900135 std::unique_ptr<CallbackList<void(int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900136 subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s)));
137
138 c3.Notify(1, 2, 3);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900139 EXPECT_EQ(6, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900140
caitkp@chromium.org49647322013-09-27 04:20:18 +0900141 CallbackList<void(int, int, int, int)> c4;
dchengcc8e4d82016-04-05 06:25:51 +0900142 std::unique_ptr<CallbackList<void(int, int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900143 subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s)));
144
145 c4.Notify(1, 2, 3, 4);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900146 EXPECT_EQ(10, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900147
caitkp@chromium.org49647322013-09-27 04:20:18 +0900148 CallbackList<void(int, int, int, int, int)> c5;
dchengcc8e4d82016-04-05 06:25:51 +0900149 std::unique_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900150 subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s)));
151
152 c5.Notify(1, 2, 3, 4, 5);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900153 EXPECT_EQ(15, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900154
caitkp@chromium.org49647322013-09-27 04:20:18 +0900155 CallbackList<void(int, int, int, int, int, int)> c6;
dchengcc8e4d82016-04-05 06:25:51 +0900156 std::unique_ptr<
157 CallbackList<void(int, int, int, int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900158 subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s)));
159
160 c6.Notify(1, 2, 3, 4, 5, 6);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900161 EXPECT_EQ(21, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900162}
163
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900164// Sanity check that closures added to the list will be run, and those removed
165// from the list will not be run.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900166TEST(CallbackListTest, BasicTest) {
167 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900168 Listener a, b, c;
169
dchengcc8e4d82016-04-05 06:25:51 +0900170 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900171 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
dchengcc8e4d82016-04-05 06:25:51 +0900172 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900173 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
174
175 EXPECT_TRUE(a_subscription.get());
176 EXPECT_TRUE(b_subscription.get());
177
178 cb_reg.Notify();
179
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900180 EXPECT_EQ(1, a.total());
181 EXPECT_EQ(1, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900182
183 b_subscription.reset();
184
dchengcc8e4d82016-04-05 06:25:51 +0900185 std::unique_ptr<CallbackList<void(void)>::Subscription> c_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900186 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
187
188 cb_reg.Notify();
189
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900190 EXPECT_EQ(2, a.total());
191 EXPECT_EQ(1, b.total());
192 EXPECT_EQ(1, c.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900193
194 a_subscription.reset();
195 b_subscription.reset();
196 c_subscription.reset();
197}
198
199// Sanity check that callbacks with details added to the list will be run, with
200// the correct details, and those removed from the list will not be run.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900201TEST(CallbackListTest, BasicTestWithParams) {
202 CallbackList<void(int)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900203 Listener a(1), b(-1), c(1);
204
dchengcc8e4d82016-04-05 06:25:51 +0900205 std::unique_ptr<CallbackList<void(int)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900206 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a)));
dchengcc8e4d82016-04-05 06:25:51 +0900207 std::unique_ptr<CallbackList<void(int)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900208 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b)));
209
210 EXPECT_TRUE(a_subscription.get());
211 EXPECT_TRUE(b_subscription.get());
212
213 cb_reg.Notify(10);
214
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900215 EXPECT_EQ(10, a.total());
216 EXPECT_EQ(-10, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900217
218 b_subscription.reset();
219
dchengcc8e4d82016-04-05 06:25:51 +0900220 std::unique_ptr<CallbackList<void(int)>::Subscription> c_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900221 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c)));
222
223 cb_reg.Notify(10);
224
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900225 EXPECT_EQ(20, a.total());
226 EXPECT_EQ(-10, b.total());
227 EXPECT_EQ(10, c.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900228
229 a_subscription.reset();
230 b_subscription.reset();
231 c_subscription.reset();
232}
233
234// Test the a callback can remove itself or a different callback from the list
235// during iteration without invalidating the iterator.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900236TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
237 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900238 Listener a, b;
239 Remover remover_1, remover_2;
240
dchengcc8e4d82016-04-05 06:25:51 +0900241 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
242 cb_reg.Add(
243 Bind(&Remover::IncrementTotalAndRemove, Unretained(&remover_1)));
244 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
245 cb_reg.Add(
246 Bind(&Remover::IncrementTotalAndRemove, Unretained(&remover_2)));
247 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900248 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
dchengcc8e4d82016-04-05 06:25:51 +0900249 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900250 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
251
252 // |remover_1| will remove itself.
dcheng961ff4a2015-12-15 14:11:17 +0900253 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900254 // |remover_2| will remove a.
dcheng961ff4a2015-12-15 14:11:17 +0900255 remover_2.SetSubscriptionToRemove(std::move(a_subscription));
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900256
257 cb_reg.Notify();
258
259 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
260 // removes a), |a| never runs, and |b| runs once.
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900261 EXPECT_EQ(1, remover_1.total());
262 EXPECT_EQ(1, remover_2.total());
263 EXPECT_EQ(0, a.total());
264 EXPECT_EQ(1, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900265
266 cb_reg.Notify();
267
268 // Only |remover_2| and |b| run this time.
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900269 EXPECT_EQ(1, remover_1.total());
270 EXPECT_EQ(2, remover_2.total());
271 EXPECT_EQ(0, a.total());
272 EXPECT_EQ(2, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900273}
274
275// Test that a callback can add another callback to the list durning iteration
276// without invalidating the iterator. The newly added callback should be run on
277// the current iteration as will all other callbacks in the list.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900278TEST(CallbackListTest, AddCallbacksDuringIteration) {
279 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900280 Adder a(&cb_reg);
281 Listener b;
dchengcc8e4d82016-04-05 06:25:51 +0900282 std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900283 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
dchengcc8e4d82016-04-05 06:25:51 +0900284 std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900285 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
286
287 cb_reg.Notify();
288
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900289 EXPECT_EQ(1, a.total());
290 EXPECT_EQ(1, b.total());
291 EXPECT_TRUE(a.added());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900292
293 cb_reg.Notify();
294
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900295 EXPECT_EQ(2, a.total());
296 EXPECT_EQ(2, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900297}
298
299// Sanity check: notifying an empty list is a no-op.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900300TEST(CallbackListTest, EmptyList) {
301 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900302
303 cb_reg.Notify();
304}
305
davidben4f5f0432015-10-31 05:42:07 +0900306TEST(CallbackList, RemovalCallback) {
307 Counter remove_count;
308 CallbackList<void(void)> cb_reg;
309 cb_reg.set_removal_callback(
310 Bind(&Counter::Increment, Unretained(&remove_count)));
311
dchengcc8e4d82016-04-05 06:25:51 +0900312 std::unique_ptr<CallbackList<void(void)>::Subscription> subscription =
Peter Kasting24efe5e2018-02-24 09:03:01 +0900313 cb_reg.Add(DoNothing());
davidben4f5f0432015-10-31 05:42:07 +0900314
315 // Removing a subscription outside of iteration signals the callback.
316 EXPECT_EQ(0, remove_count.value());
317 subscription.reset();
318 EXPECT_EQ(1, remove_count.value());
319
320 // Configure two subscriptions to remove themselves.
321 Remover remover_1, remover_2;
dchengcc8e4d82016-04-05 06:25:51 +0900322 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
323 cb_reg.Add(
324 Bind(&Remover::IncrementTotalAndRemove, Unretained(&remover_1)));
325 std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
326 cb_reg.Add(
327 Bind(&Remover::IncrementTotalAndRemove, Unretained(&remover_2)));
dcheng961ff4a2015-12-15 14:11:17 +0900328 remover_1.SetSubscriptionToRemove(std::move(remover_1_sub));
329 remover_2.SetSubscriptionToRemove(std::move(remover_2_sub));
davidben4f5f0432015-10-31 05:42:07 +0900330
331 // The callback should be signaled exactly once.
332 EXPECT_EQ(1, remove_count.value());
333 cb_reg.Notify();
334 EXPECT_EQ(2, remove_count.value());
335 EXPECT_TRUE(cb_reg.empty());
336}
337
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900338} // namespace
339} // namespace base