[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 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 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 5 | #include "base/callback_list.h" |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 6 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 7 | #include <memory> |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace base { |
| 15 | namespace { |
| 16 | |
| 17 | class Listener { |
| 18 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 19 | Listener() = default; |
| 20 | explicit Listener(int scaler) : scaler_(scaler) {} |
| 21 | Listener(const Listener&) = delete; |
| 22 | Listener& operator=(const Listener&) = delete; |
| 23 | ~Listener() = default; |
| 24 | |
| 25 | void IncrementTotal() { ++total_; } |
| 26 | |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 27 | void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 28 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 29 | int total() const { return total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 30 | |
| 31 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 32 | int total_ = 0; |
| 33 | int scaler_ = 1; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | class Remover { |
| 37 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 38 | Remover() = default; |
| 39 | Remover(const Remover&) = delete; |
| 40 | Remover& operator=(const Remover&) = delete; |
| 41 | ~Remover() = default; |
| 42 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 43 | void IncrementTotalAndRemove() { |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 44 | ++total_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 45 | removal_subscription_.reset(); |
| 46 | } |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 47 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 48 | void SetSubscriptionToRemove( |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 49 | std::unique_ptr<CallbackList<void(void)>::Subscription> sub) { |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 50 | removal_subscription_ = std::move(sub); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 53 | int total() const { return total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 54 | |
| 55 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 56 | int total_ = 0; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 57 | std::unique_ptr<CallbackList<void(void)>::Subscription> removal_subscription_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | class Adder { |
| 61 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 62 | explicit Adder(CallbackList<void(void)>* cb_reg) : cb_reg_(cb_reg) {} |
| 63 | Adder(const Adder&) = delete; |
| 64 | Adder& operator=(const Adder&) = delete; |
| 65 | ~Adder() = default; |
| 66 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 67 | void AddCallback() { |
| 68 | if (!added_) { |
| 69 | added_ = true; |
| 70 | subscription_ = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 71 | cb_reg_->Add(BindRepeating(&Adder::IncrementTotal, Unretained(this))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 72 | } |
| 73 | } |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 74 | |
| 75 | void IncrementTotal() { ++total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 76 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 77 | bool added() const { return added_; } |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 78 | int total() const { return total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 79 | |
| 80 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 81 | bool added_ = false; |
| 82 | int total_ = 0; |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 83 | CallbackList<void(void)>* cb_reg_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 84 | std::unique_ptr<CallbackList<void(void)>::Subscription> subscription_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 85 | }; |
| 86 | |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 87 | class Summer { |
| 88 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 89 | Summer() = default; |
| 90 | Summer(const Summer&) = delete; |
| 91 | Summer& operator=(const Summer&) = delete; |
| 92 | ~Summer() = default; |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 93 | |
| 94 | void AddOneParam(int a) { value_ = a; } |
| 95 | void AddTwoParam(int a, int b) { value_ = a + b; } |
| 96 | void AddThreeParam(int a, int b, int c) { value_ = a + b + c; } |
| 97 | void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; } |
| 98 | void AddFiveParam(int a, int b, int c, int d, int e) { |
| 99 | value_ = a + b + c + d + e; |
| 100 | } |
| 101 | void AddSixParam(int a, int b, int c, int d, int e , int f) { |
| 102 | value_ = a + b + c + d + e + f; |
| 103 | } |
| 104 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 105 | int value() const { return value_; } |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 106 | |
| 107 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 108 | int value_ = 0; |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 109 | }; |
| 110 | |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 111 | class Counter { |
| 112 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 113 | Counter() = default; |
| 114 | Counter(const Counter&) = delete; |
| 115 | Counter& operator=(const Counter&) = delete; |
| 116 | ~Counter() = default; |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 117 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 118 | void Increment() { ++value_; } |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 119 | |
| 120 | int value() const { return value_; } |
| 121 | |
| 122 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 123 | int value_ = 0; |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 124 | }; |
| 125 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 126 | // Sanity check that we can instantiate a CallbackList for each arity. |
| 127 | TEST(CallbackListTest, ArityTest) { |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 128 | Summer s; |
| 129 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 130 | CallbackList<void(int)> c1; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 131 | std::unique_ptr<CallbackList<void(int)>::Subscription> subscription1 = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 132 | c1.Add(BindRepeating(&Summer::AddOneParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 133 | |
| 134 | c1.Notify(1); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 135 | EXPECT_EQ(1, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 136 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 137 | CallbackList<void(int, int)> c2; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 138 | std::unique_ptr<CallbackList<void(int, int)>::Subscription> subscription2 = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 139 | c2.Add(BindRepeating(&Summer::AddTwoParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 140 | |
| 141 | c2.Notify(1, 2); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 142 | EXPECT_EQ(3, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 143 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 144 | CallbackList<void(int, int, int)> c3; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 145 | std::unique_ptr<CallbackList<void(int, int, int)>::Subscription> |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 146 | subscription3 = |
| 147 | c3.Add(BindRepeating(&Summer::AddThreeParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 148 | |
| 149 | c3.Notify(1, 2, 3); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 150 | EXPECT_EQ(6, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 151 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 152 | CallbackList<void(int, int, int, int)> c4; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 153 | std::unique_ptr<CallbackList<void(int, int, int, int)>::Subscription> |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 154 | subscription4 = |
| 155 | c4.Add(BindRepeating(&Summer::AddFourParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 156 | |
| 157 | c4.Notify(1, 2, 3, 4); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 158 | EXPECT_EQ(10, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 159 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 160 | CallbackList<void(int, int, int, int, int)> c5; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 161 | std::unique_ptr<CallbackList<void(int, int, int, int, int)>::Subscription> |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 162 | subscription5 = |
| 163 | c5.Add(BindRepeating(&Summer::AddFiveParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 164 | |
| 165 | c5.Notify(1, 2, 3, 4, 5); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 166 | EXPECT_EQ(15, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 167 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 168 | CallbackList<void(int, int, int, int, int, int)> c6; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 169 | std::unique_ptr< |
| 170 | CallbackList<void(int, int, int, int, int, int)>::Subscription> |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 171 | subscription6 = |
| 172 | c6.Add(BindRepeating(&Summer::AddSixParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 173 | |
| 174 | c6.Notify(1, 2, 3, 4, 5, 6); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 175 | EXPECT_EQ(21, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 178 | // Sanity check that closures added to the list will be run, and those removed |
| 179 | // from the list will not be run. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 180 | TEST(CallbackListTest, BasicTest) { |
| 181 | CallbackList<void(void)> cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 182 | Listener a, b, c; |
| 183 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 184 | std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 185 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a))); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 186 | std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 187 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 188 | |
| 189 | EXPECT_TRUE(a_subscription.get()); |
| 190 | EXPECT_TRUE(b_subscription.get()); |
| 191 | |
| 192 | cb_reg.Notify(); |
| 193 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 194 | EXPECT_EQ(1, a.total()); |
| 195 | EXPECT_EQ(1, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 196 | |
| 197 | b_subscription.reset(); |
| 198 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 199 | std::unique_ptr<CallbackList<void(void)>::Subscription> c_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 200 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&c))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 201 | |
| 202 | cb_reg.Notify(); |
| 203 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 204 | EXPECT_EQ(2, a.total()); |
| 205 | EXPECT_EQ(1, b.total()); |
| 206 | EXPECT_EQ(1, c.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Sanity check that callbacks with details added to the list will be run, with |
| 210 | // the correct details, and those removed from the list will not be run. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 211 | TEST(CallbackListTest, BasicTestWithParams) { |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 212 | using CallbackListType = CallbackList<void(int)>; |
| 213 | CallbackListType cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 214 | Listener a(1), b(-1), c(1); |
| 215 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 216 | std::unique_ptr<CallbackListType::Subscription> a_subscription = cb_reg.Add( |
| 217 | BindRepeating(&Listener::IncrementByMultipleOfScaler, Unretained(&a))); |
| 218 | std::unique_ptr<CallbackListType::Subscription> b_subscription = cb_reg.Add( |
| 219 | BindRepeating(&Listener::IncrementByMultipleOfScaler, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 220 | |
| 221 | EXPECT_TRUE(a_subscription.get()); |
| 222 | EXPECT_TRUE(b_subscription.get()); |
| 223 | |
| 224 | cb_reg.Notify(10); |
| 225 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 226 | EXPECT_EQ(10, a.total()); |
| 227 | EXPECT_EQ(-10, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 228 | |
| 229 | b_subscription.reset(); |
| 230 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 231 | std::unique_ptr<CallbackListType::Subscription> c_subscription = cb_reg.Add( |
| 232 | BindRepeating(&Listener::IncrementByMultipleOfScaler, Unretained(&c))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 233 | |
| 234 | cb_reg.Notify(10); |
| 235 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 236 | EXPECT_EQ(20, a.total()); |
| 237 | EXPECT_EQ(-10, b.total()); |
| 238 | EXPECT_EQ(10, c.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 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] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 243 | TEST(CallbackListTest, RemoveCallbacksDuringIteration) { |
| 244 | CallbackList<void(void)> cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 245 | Listener a, b; |
| 246 | Remover remover_1, remover_2; |
| 247 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 248 | std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 249 | cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove, |
| 250 | Unretained(&remover_1))); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 251 | std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 252 | cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove, |
| 253 | Unretained(&remover_2))); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 254 | std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 255 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a))); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 256 | std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 257 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 258 | |
| 259 | // |remover_1| will remove itself. |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 260 | remover_1.SetSubscriptionToRemove(std::move(remover_1_sub)); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 261 | // |remover_2| will remove a. |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 262 | remover_2.SetSubscriptionToRemove(std::move(a_subscription)); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 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. |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 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()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 272 | |
| 273 | cb_reg.Notify(); |
| 274 | |
| 275 | // Only |remover_2| and |b| run this time. |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 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()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 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. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 285 | TEST(CallbackListTest, AddCallbacksDuringIteration) { |
| 286 | CallbackList<void(void)> cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 287 | Adder a(&cb_reg); |
| 288 | Listener b; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 289 | std::unique_ptr<CallbackList<void(void)>::Subscription> a_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 290 | cb_reg.Add(BindRepeating(&Adder::AddCallback, Unretained(&a))); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 291 | std::unique_ptr<CallbackList<void(void)>::Subscription> b_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 292 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 293 | |
| 294 | cb_reg.Notify(); |
| 295 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 296 | EXPECT_EQ(1, a.total()); |
| 297 | EXPECT_EQ(1, b.total()); |
| 298 | EXPECT_TRUE(a.added()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 299 | |
| 300 | cb_reg.Notify(); |
| 301 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 302 | EXPECT_EQ(2, a.total()); |
| 303 | EXPECT_EQ(2, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Sanity check: notifying an empty list is a no-op. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 307 | TEST(CallbackListTest, EmptyList) { |
| 308 | CallbackList<void(void)> cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 309 | |
| 310 | cb_reg.Notify(); |
| 311 | } |
| 312 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 313 | TEST(CallbackListTest, RemovalCallback) { |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 314 | Counter remove_count; |
| 315 | CallbackList<void(void)> cb_reg; |
| 316 | cb_reg.set_removal_callback( |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 317 | BindRepeating(&Counter::Increment, Unretained(&remove_count))); |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 318 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 319 | std::unique_ptr<CallbackList<void(void)>::Subscription> subscription = |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 320 | cb_reg.Add(DoNothing()); |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 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; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 329 | std::unique_ptr<CallbackList<void(void)>::Subscription> remover_1_sub = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 330 | cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove, |
| 331 | Unretained(&remover_1))); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 332 | std::unique_ptr<CallbackList<void(void)>::Subscription> remover_2_sub = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 333 | cb_reg.Add(BindRepeating(&Remover::IncrementTotalAndRemove, |
| 334 | Unretained(&remover_2))); |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 335 | remover_1.SetSubscriptionToRemove(std::move(remover_1_sub)); |
| 336 | remover_2.SetSubscriptionToRemove(std::move(remover_2_sub)); |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 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 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 345 | TEST(CallbackListTest, AbandonSubscriptions) { |
Allen Bauer | 8268630 | 2019-05-14 22:37:47 | [diff] [blame] | 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()); |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame^] | 357 | |
| 358 | // Destroying the subscription after the list should not cause any problems. |
| 359 | subscription.reset(); |
Allen Bauer | 8268630 | 2019-05-14 22:37:47 | [diff] [blame] | 360 | } |
| 361 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 362 | } // namespace |
| 363 | } // namespace base |