blob: 9adbabb0931cf8573615e6fd1ab229c9de030d1f [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
7#include "base/basictypes.h"
8#include "base/bind.h"
9#include "base/bind_helpers.h"
10#include "base/memory/scoped_ptr.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace base {
14namespace {
15
16class Listener {
17 public:
18 Listener() : total_(0), scaler_(1) {}
19 explicit Listener(int scaler) : total_(0), scaler_(scaler) {}
20 void IncrementTotal() { total_++; }
caitkp@google.com2b10b742013-09-25 22:56:23 +090021 void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090022
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090023 int total() const { return total_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090024
25 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090026 int total_;
caitkp@chromium.org1268af12013-09-12 00:16:33 +090027 int scaler_;
28 DISALLOW_COPY_AND_ASSIGN(Listener);
29};
30
31class Remover {
32 public:
33 Remover() : total_(0) {}
34 void IncrementTotalAndRemove() {
35 total_++;
36 removal_subscription_.reset();
37 }
38 void SetSubscriptionToRemove(
caitkp@chromium.org49647322013-09-27 04:20:18 +090039 scoped_ptr<CallbackList<void(void)>::Subscription> sub) {
caitkp@chromium.org1268af12013-09-12 00:16:33 +090040 removal_subscription_ = sub.Pass();
41 }
42
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090043 int total() const { return total_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090044
45 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090046 int total_;
caitkp@chromium.org49647322013-09-27 04:20:18 +090047 scoped_ptr<CallbackList<void(void)>::Subscription> removal_subscription_;
caitkp@chromium.org1268af12013-09-12 00:16:33 +090048 DISALLOW_COPY_AND_ASSIGN(Remover);
49};
50
51class Adder {
52 public:
caitkp@chromium.org49647322013-09-27 04:20:18 +090053 explicit Adder(CallbackList<void(void)>* cb_reg)
caitkp@chromium.org1268af12013-09-12 00:16:33 +090054 : added_(false),
55 total_(0),
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090056 cb_reg_(cb_reg) {
57 }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090058 void AddCallback() {
59 if (!added_) {
60 added_ = true;
61 subscription_ =
62 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this)));
63 }
64 }
65 void IncrementTotal() { total_++; }
66
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090067 bool added() const { return added_; }
68
69 int total() const { return total_; }
caitkp@chromium.org1268af12013-09-12 00:16:33 +090070
71 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090072 bool added_;
73 int total_;
caitkp@chromium.org49647322013-09-27 04:20:18 +090074 CallbackList<void(void)>* cb_reg_;
75 scoped_ptr<CallbackList<void(void)>::Subscription> subscription_;
caitkp@chromium.org1268af12013-09-12 00:16:33 +090076 DISALLOW_COPY_AND_ASSIGN(Adder);
77};
78
caitkp@google.com2b10b742013-09-25 22:56:23 +090079class Summer {
80 public:
81 Summer() : value_(0) {}
82
83 void AddOneParam(int a) { value_ = a; }
84 void AddTwoParam(int a, int b) { value_ = a + b; }
85 void AddThreeParam(int a, int b, int c) { value_ = a + b + c; }
86 void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; }
87 void AddFiveParam(int a, int b, int c, int d, int e) {
88 value_ = a + b + c + d + e;
89 }
90 void AddSixParam(int a, int b, int c, int d, int e , int f) {
91 value_ = a + b + c + d + e + f;
92 }
93
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090094 int value() const { return value_; }
caitkp@google.com2b10b742013-09-25 22:56:23 +090095
96 private:
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +090097 int value_;
caitkp@google.com2b10b742013-09-25 22:56:23 +090098 DISALLOW_COPY_AND_ASSIGN(Summer);
99};
100
caitkp@chromium.org49647322013-09-27 04:20:18 +0900101// Sanity check that we can instantiate a CallbackList for each arity.
102TEST(CallbackListTest, ArityTest) {
caitkp@google.com2b10b742013-09-25 22:56:23 +0900103 Summer s;
104
caitkp@chromium.org49647322013-09-27 04:20:18 +0900105 CallbackList<void(int)> c1;
106 scoped_ptr<CallbackList<void(int)>::Subscription> subscription1 =
caitkp@google.com2b10b742013-09-25 22:56:23 +0900107 c1.Add(Bind(&Summer::AddOneParam, Unretained(&s)));
108
109 c1.Notify(1);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900110 EXPECT_EQ(1, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900111
caitkp@chromium.org49647322013-09-27 04:20:18 +0900112 CallbackList<void(int, int)> c2;
113 scoped_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
caitkp@google.com2b10b742013-09-25 22:56:23 +0900114 c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s)));
115
116 c2.Notify(1, 2);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900117 EXPECT_EQ(3, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900118
caitkp@chromium.org49647322013-09-27 04:20:18 +0900119 CallbackList<void(int, int, int)> c3;
120 scoped_ptr<CallbackList<void(int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900121 subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s)));
122
123 c3.Notify(1, 2, 3);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900124 EXPECT_EQ(6, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900125
caitkp@chromium.org49647322013-09-27 04:20:18 +0900126 CallbackList<void(int, int, int, int)> c4;
127 scoped_ptr<CallbackList<void(int, int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900128 subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s)));
129
130 c4.Notify(1, 2, 3, 4);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900131 EXPECT_EQ(10, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900132
caitkp@chromium.org49647322013-09-27 04:20:18 +0900133 CallbackList<void(int, int, int, int, int)> c5;
134 scoped_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900135 subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s)));
136
137 c5.Notify(1, 2, 3, 4, 5);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900138 EXPECT_EQ(15, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900139
caitkp@chromium.org49647322013-09-27 04:20:18 +0900140 CallbackList<void(int, int, int, int, int, int)> c6;
141 scoped_ptr<CallbackList<void(int, int, int, int, int, int)>::Subscription>
caitkp@google.com2b10b742013-09-25 22:56:23 +0900142 subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s)));
143
144 c6.Notify(1, 2, 3, 4, 5, 6);
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900145 EXPECT_EQ(21, s.value());
caitkp@google.com2b10b742013-09-25 22:56:23 +0900146}
147
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900148// Sanity check that closures added to the list will be run, and those removed
149// from the list will not be run.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900150TEST(CallbackListTest, BasicTest) {
151 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900152 Listener a, b, c;
153
caitkp@chromium.org49647322013-09-27 04:20:18 +0900154 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900155 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
caitkp@chromium.org49647322013-09-27 04:20:18 +0900156 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900157 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
158
159 EXPECT_TRUE(a_subscription.get());
160 EXPECT_TRUE(b_subscription.get());
161
162 cb_reg.Notify();
163
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900164 EXPECT_EQ(1, a.total());
165 EXPECT_EQ(1, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900166
167 b_subscription.reset();
168
caitkp@chromium.org49647322013-09-27 04:20:18 +0900169 scoped_ptr<CallbackList<void(void)>::Subscription> c_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900170 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
171
172 cb_reg.Notify();
173
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900174 EXPECT_EQ(2, a.total());
175 EXPECT_EQ(1, b.total());
176 EXPECT_EQ(1, c.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900177
178 a_subscription.reset();
179 b_subscription.reset();
180 c_subscription.reset();
181}
182
183// Sanity check that callbacks with details added to the list will be run, with
184// the correct details, and those removed from the list will not be run.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900185TEST(CallbackListTest, BasicTestWithParams) {
186 CallbackList<void(int)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900187 Listener a(1), b(-1), c(1);
188
caitkp@chromium.org49647322013-09-27 04:20:18 +0900189 scoped_ptr<CallbackList<void(int)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900190 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a)));
caitkp@chromium.org49647322013-09-27 04:20:18 +0900191 scoped_ptr<CallbackList<void(int)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900192 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b)));
193
194 EXPECT_TRUE(a_subscription.get());
195 EXPECT_TRUE(b_subscription.get());
196
197 cb_reg.Notify(10);
198
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900199 EXPECT_EQ(10, a.total());
200 EXPECT_EQ(-10, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900201
202 b_subscription.reset();
203
caitkp@chromium.org49647322013-09-27 04:20:18 +0900204 scoped_ptr<CallbackList<void(int)>::Subscription> c_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900205 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c)));
206
207 cb_reg.Notify(10);
208
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900209 EXPECT_EQ(20, a.total());
210 EXPECT_EQ(-10, b.total());
211 EXPECT_EQ(10, c.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900212
213 a_subscription.reset();
214 b_subscription.reset();
215 c_subscription.reset();
216}
217
218// Test the a callback can remove itself or a different callback from the list
219// during iteration without invalidating the iterator.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900220TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
221 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900222 Listener a, b;
223 Remover remover_1, remover_2;
224
caitkp@chromium.org49647322013-09-27 04:20:18 +0900225 scoped_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900226 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
227 Unretained(&remover_1)));
caitkp@chromium.org49647322013-09-27 04:20:18 +0900228 scoped_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900229 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
230 Unretained(&remover_2)));
caitkp@chromium.org49647322013-09-27 04:20:18 +0900231 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900232 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
caitkp@chromium.org49647322013-09-27 04:20:18 +0900233 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900234 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
235
236 // |remover_1| will remove itself.
caitkp@google.com2b10b742013-09-25 22:56:23 +0900237 remover_1.SetSubscriptionToRemove(remover_1_sub.Pass());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900238 // |remover_2| will remove a.
239 remover_2.SetSubscriptionToRemove(a_subscription.Pass());
240
241 cb_reg.Notify();
242
243 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
244 // removes a), |a| never runs, and |b| runs once.
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900245 EXPECT_EQ(1, remover_1.total());
246 EXPECT_EQ(1, remover_2.total());
247 EXPECT_EQ(0, a.total());
248 EXPECT_EQ(1, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900249
250 cb_reg.Notify();
251
252 // Only |remover_2| and |b| run this time.
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900253 EXPECT_EQ(1, remover_1.total());
254 EXPECT_EQ(2, remover_2.total());
255 EXPECT_EQ(0, a.total());
256 EXPECT_EQ(2, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900257}
258
259// Test that a callback can add another callback to the list durning iteration
260// without invalidating the iterator. The newly added callback should be run on
261// the current iteration as will all other callbacks in the list.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900262TEST(CallbackListTest, AddCallbacksDuringIteration) {
263 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900264 Adder a(&cb_reg);
265 Listener b;
caitkp@chromium.org49647322013-09-27 04:20:18 +0900266 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900267 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
caitkp@chromium.org49647322013-09-27 04:20:18 +0900268 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900269 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
270
271 cb_reg.Notify();
272
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900273 EXPECT_EQ(1, a.total());
274 EXPECT_EQ(1, b.total());
275 EXPECT_TRUE(a.added());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900276
277 cb_reg.Notify();
278
caitkp@chromium.org0b4414d2013-10-10 08:28:21 +0900279 EXPECT_EQ(2, a.total());
280 EXPECT_EQ(2, b.total());
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900281}
282
283// Sanity check: notifying an empty list is a no-op.
caitkp@chromium.org49647322013-09-27 04:20:18 +0900284TEST(CallbackListTest, EmptyList) {
285 CallbackList<void(void)> cb_reg;
caitkp@chromium.org1268af12013-09-12 00:16:33 +0900286
287 cb_reg.Notify();
288}
289
290} // namespace
291} // namespace base