[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" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 11 | #include "base/callback_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_; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 45 | removal_subscription_ = {}; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 46 | } |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 47 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 48 | void SetSubscriptionToRemove(CallbackListSubscription subscription) { |
| 49 | removal_subscription_ = std::move(subscription); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 52 | int total() const { return total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 53 | |
| 54 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 55 | int total_ = 0; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 56 | CallbackListSubscription removal_subscription_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | class Adder { |
| 60 | public: |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 61 | explicit Adder(RepeatingClosureList* cb_reg) : cb_reg_(cb_reg) {} |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 62 | Adder(const Adder&) = delete; |
| 63 | Adder& operator=(const Adder&) = delete; |
| 64 | ~Adder() = default; |
| 65 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 66 | void AddCallback() { |
| 67 | if (!added_) { |
| 68 | added_ = true; |
| 69 | subscription_ = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 70 | cb_reg_->Add(BindRepeating(&Adder::IncrementTotal, Unretained(this))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 71 | } |
| 72 | } |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 73 | |
| 74 | void IncrementTotal() { ++total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 75 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 76 | bool added() const { return added_; } |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 77 | int total() const { return total_; } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 78 | |
| 79 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 80 | bool added_ = false; |
| 81 | int total_ = 0; |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 82 | RepeatingClosureList* cb_reg_; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 83 | CallbackListSubscription subscription_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 86 | class Summer { |
| 87 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 88 | Summer() = default; |
| 89 | Summer(const Summer&) = delete; |
| 90 | Summer& operator=(const Summer&) = delete; |
| 91 | ~Summer() = default; |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 92 | |
| 93 | void AddOneParam(int a) { value_ = a; } |
| 94 | void AddTwoParam(int a, int b) { value_ = a + b; } |
| 95 | void AddThreeParam(int a, int b, int c) { value_ = a + b + c; } |
| 96 | void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; } |
| 97 | void AddFiveParam(int a, int b, int c, int d, int e) { |
| 98 | value_ = a + b + c + d + e; |
| 99 | } |
| 100 | void AddSixParam(int a, int b, int c, int d, int e , int f) { |
| 101 | value_ = a + b + c + d + e + f; |
| 102 | } |
| 103 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 104 | int value() const { return value_; } |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 105 | |
| 106 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 107 | int value_ = 0; |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 108 | }; |
| 109 | |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 110 | class Counter { |
| 111 | public: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 112 | Counter() = default; |
| 113 | Counter(const Counter&) = delete; |
| 114 | Counter& operator=(const Counter&) = delete; |
| 115 | ~Counter() = default; |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 116 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 117 | void Increment() { ++value_; } |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 118 | |
| 119 | int value() const { return value_; } |
| 120 | |
| 121 | private: |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 122 | int value_ = 0; |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 123 | }; |
| 124 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 125 | // Sanity check that we can instantiate a CallbackList for each arity. |
| 126 | TEST(CallbackListTest, ArityTest) { |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 127 | Summer s; |
| 128 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 129 | RepeatingCallbackList<void(int)> c1; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 130 | CallbackListSubscription subscription1 = |
| 131 | c1.Add(BindRepeating(&Summer::AddOneParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 132 | |
| 133 | c1.Notify(1); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 134 | EXPECT_EQ(1, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 135 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 136 | RepeatingCallbackList<void(int, int)> c2; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 137 | CallbackListSubscription subscription2 = |
| 138 | c2.Add(BindRepeating(&Summer::AddTwoParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 139 | |
| 140 | c2.Notify(1, 2); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 141 | EXPECT_EQ(3, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 142 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 143 | RepeatingCallbackList<void(int, int, int)> c3; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 144 | CallbackListSubscription subscription3 = |
| 145 | c3.Add(BindRepeating(&Summer::AddThreeParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 146 | |
| 147 | c3.Notify(1, 2, 3); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 148 | EXPECT_EQ(6, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 149 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 150 | RepeatingCallbackList<void(int, int, int, int)> c4; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 151 | CallbackListSubscription subscription4 = |
| 152 | c4.Add(BindRepeating(&Summer::AddFourParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 153 | |
| 154 | c4.Notify(1, 2, 3, 4); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 155 | EXPECT_EQ(10, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 156 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 157 | RepeatingCallbackList<void(int, int, int, int, int)> c5; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 158 | CallbackListSubscription subscription5 = |
| 159 | c5.Add(BindRepeating(&Summer::AddFiveParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 160 | |
| 161 | c5.Notify(1, 2, 3, 4, 5); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 162 | EXPECT_EQ(15, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 163 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 164 | RepeatingCallbackList<void(int, int, int, int, int, int)> c6; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 165 | CallbackListSubscription subscription6 = |
| 166 | c6.Add(BindRepeating(&Summer::AddSixParam, Unretained(&s))); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 167 | |
| 168 | c6.Notify(1, 2, 3, 4, 5, 6); |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 169 | EXPECT_EQ(21, s.value()); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 172 | // Sanity check that closures added to the list will be run, and those removed |
| 173 | // from the list will not be run. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 174 | TEST(CallbackListTest, BasicTest) { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 175 | Listener a, b, c; |
Peter Kasting | 0fdb328 | 2020-08-06 05:23:24 | [diff] [blame] | 176 | RepeatingClosureList cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 177 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 178 | CallbackListSubscription a_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 179 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 180 | CallbackListSubscription b_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 181 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
Peter Kasting | 0fdb328 | 2020-08-06 05:23:24 | [diff] [blame] | 182 | cb_reg.AddUnsafe(BindRepeating(&Listener::IncrementTotal, Unretained(&c))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 183 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 184 | EXPECT_TRUE(a_subscription); |
| 185 | EXPECT_TRUE(b_subscription); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 186 | |
| 187 | cb_reg.Notify(); |
| 188 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 189 | EXPECT_EQ(1, a.total()); |
| 190 | EXPECT_EQ(1, b.total()); |
Peter Kasting | 0fdb328 | 2020-08-06 05:23:24 | [diff] [blame] | 191 | EXPECT_EQ(1, c.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 192 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 193 | b_subscription = {}; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 194 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 195 | CallbackListSubscription c_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 196 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&c))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 197 | |
| 198 | cb_reg.Notify(); |
| 199 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 200 | EXPECT_EQ(2, a.total()); |
| 201 | EXPECT_EQ(1, b.total()); |
Peter Kasting | 0fdb328 | 2020-08-06 05:23:24 | [diff] [blame] | 202 | EXPECT_EQ(3, c.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 203 | } |
| 204 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 205 | // Similar to BasicTest but with OnceCallbacks instead of Repeating. |
| 206 | TEST(CallbackListTest, OnceCallbacks) { |
| 207 | OnceClosureList cb_reg; |
| 208 | Listener a, b, c; |
| 209 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 210 | CallbackListSubscription a_subscription = |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 211 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&a))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 212 | CallbackListSubscription b_subscription = |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 213 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&b))); |
| 214 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 215 | EXPECT_TRUE(a_subscription); |
| 216 | EXPECT_TRUE(b_subscription); |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 217 | |
| 218 | cb_reg.Notify(); |
| 219 | |
| 220 | EXPECT_EQ(1, a.total()); |
| 221 | EXPECT_EQ(1, b.total()); |
| 222 | |
| 223 | // OnceCallbacks should auto-remove themselves after calling Notify(). |
| 224 | EXPECT_TRUE(cb_reg.empty()); |
| 225 | |
| 226 | // Destroying a subscription after the callback is canceled should not cause |
| 227 | // any problems. |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 228 | b_subscription = {}; |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 229 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 230 | CallbackListSubscription c_subscription = |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 231 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&c))); |
| 232 | |
| 233 | cb_reg.Notify(); |
| 234 | |
| 235 | EXPECT_EQ(1, a.total()); |
| 236 | EXPECT_EQ(1, b.total()); |
| 237 | EXPECT_EQ(1, c.total()); |
| 238 | } |
| 239 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 240 | // Sanity check that callbacks with details added to the list will be run, with |
| 241 | // the correct details, and those removed from the list will not be run. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 242 | TEST(CallbackListTest, BasicTestWithParams) { |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 243 | using CallbackListType = RepeatingCallbackList<void(int)>; |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 244 | CallbackListType cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 245 | Listener a(1), b(-1), c(1); |
| 246 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 247 | CallbackListSubscription a_subscription = cb_reg.Add( |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 248 | BindRepeating(&Listener::IncrementByMultipleOfScaler, Unretained(&a))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 249 | CallbackListSubscription b_subscription = cb_reg.Add( |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 250 | BindRepeating(&Listener::IncrementByMultipleOfScaler, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 251 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 252 | EXPECT_TRUE(a_subscription); |
| 253 | EXPECT_TRUE(b_subscription); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 254 | |
| 255 | cb_reg.Notify(10); |
| 256 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 257 | EXPECT_EQ(10, a.total()); |
| 258 | EXPECT_EQ(-10, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 259 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 260 | b_subscription = {}; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 261 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 262 | CallbackListSubscription c_subscription = cb_reg.Add( |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 263 | BindRepeating(&Listener::IncrementByMultipleOfScaler, Unretained(&c))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 264 | |
| 265 | cb_reg.Notify(10); |
| 266 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 267 | EXPECT_EQ(20, a.total()); |
| 268 | EXPECT_EQ(-10, b.total()); |
| 269 | EXPECT_EQ(10, c.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Test the a callback can remove itself or a different callback from the list |
| 273 | // during iteration without invalidating the iterator. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 274 | TEST(CallbackListTest, RemoveCallbacksDuringIteration) { |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 275 | RepeatingClosureList cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 276 | Listener a, b; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 277 | Remover remover_1, remover_2; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 278 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 279 | CallbackListSubscription remover_1_sub = cb_reg.Add( |
| 280 | BindRepeating(&Remover::IncrementTotalAndRemove, Unretained(&remover_1))); |
| 281 | CallbackListSubscription remover_2_sub = cb_reg.Add( |
| 282 | BindRepeating(&Remover::IncrementTotalAndRemove, Unretained(&remover_2))); |
| 283 | CallbackListSubscription a_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 284 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&a))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 285 | CallbackListSubscription b_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 286 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 287 | |
| 288 | // |remover_1| will remove itself. |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 289 | remover_1.SetSubscriptionToRemove(std::move(remover_1_sub)); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 290 | // |remover_2| will remove a. |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 291 | remover_2.SetSubscriptionToRemove(std::move(a_subscription)); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 292 | |
| 293 | cb_reg.Notify(); |
| 294 | |
| 295 | // |remover_1| runs once (and removes itself), |remover_2| runs once (and |
| 296 | // removes a), |a| never runs, and |b| runs once. |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 297 | EXPECT_EQ(1, remover_1.total()); |
| 298 | EXPECT_EQ(1, remover_2.total()); |
| 299 | EXPECT_EQ(0, a.total()); |
| 300 | EXPECT_EQ(1, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 301 | |
| 302 | cb_reg.Notify(); |
| 303 | |
| 304 | // Only |remover_2| and |b| run this time. |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 305 | EXPECT_EQ(1, remover_1.total()); |
| 306 | EXPECT_EQ(2, remover_2.total()); |
| 307 | EXPECT_EQ(0, a.total()); |
| 308 | EXPECT_EQ(2, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 309 | } |
| 310 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 311 | // Similar to RemoveCallbacksDuringIteration but with OnceCallbacks instead of |
| 312 | // Repeating. |
| 313 | TEST(CallbackListTest, RemoveOnceCallbacksDuringIteration) { |
| 314 | OnceClosureList cb_reg; |
| 315 | Listener a, b; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 316 | Remover remover_1, remover_2; |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 317 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 318 | CallbackListSubscription remover_1_sub = cb_reg.Add( |
| 319 | BindOnce(&Remover::IncrementTotalAndRemove, Unretained(&remover_1))); |
| 320 | CallbackListSubscription remover_2_sub = cb_reg.Add( |
| 321 | BindOnce(&Remover::IncrementTotalAndRemove, Unretained(&remover_2))); |
| 322 | CallbackListSubscription a_subscription = |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 323 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&a))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 324 | CallbackListSubscription b_subscription = |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 325 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&b))); |
| 326 | |
| 327 | // |remover_1| will remove itself. |
| 328 | remover_1.SetSubscriptionToRemove(std::move(remover_1_sub)); |
| 329 | // |remover_2| will remove a. |
| 330 | remover_2.SetSubscriptionToRemove(std::move(a_subscription)); |
| 331 | |
| 332 | cb_reg.Notify(); |
| 333 | |
| 334 | // |remover_1| runs once (and removes itself), |remover_2| runs once (and |
| 335 | // removes a), |a| never runs, and |b| runs once. |
| 336 | EXPECT_EQ(1, remover_1.total()); |
| 337 | EXPECT_EQ(1, remover_2.total()); |
| 338 | EXPECT_EQ(0, a.total()); |
| 339 | EXPECT_EQ(1, b.total()); |
| 340 | |
| 341 | cb_reg.Notify(); |
| 342 | |
| 343 | // Nothing runs this time. |
| 344 | EXPECT_EQ(1, remover_1.total()); |
| 345 | EXPECT_EQ(1, remover_2.total()); |
| 346 | EXPECT_EQ(0, a.total()); |
| 347 | EXPECT_EQ(1, b.total()); |
| 348 | } |
| 349 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 350 | // Test that a callback can add another callback to the list durning iteration |
| 351 | // without invalidating the iterator. The newly added callback should be run on |
| 352 | // the current iteration as will all other callbacks in the list. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 353 | TEST(CallbackListTest, AddCallbacksDuringIteration) { |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 354 | RepeatingClosureList cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 355 | Adder a(&cb_reg); |
| 356 | Listener b; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 357 | CallbackListSubscription a_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 358 | cb_reg.Add(BindRepeating(&Adder::AddCallback, Unretained(&a))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 359 | CallbackListSubscription b_subscription = |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 360 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 361 | |
| 362 | cb_reg.Notify(); |
| 363 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 364 | EXPECT_EQ(1, a.total()); |
| 365 | EXPECT_EQ(1, b.total()); |
| 366 | EXPECT_TRUE(a.added()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 367 | |
| 368 | cb_reg.Notify(); |
| 369 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 370 | EXPECT_EQ(2, a.total()); |
| 371 | EXPECT_EQ(2, b.total()); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // Sanity check: notifying an empty list is a no-op. |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 375 | TEST(CallbackListTest, EmptyList) { |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 376 | RepeatingClosureList cb_reg; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 377 | |
| 378 | cb_reg.Notify(); |
| 379 | } |
| 380 | |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 381 | // empty() should be callable during iteration, and return false if not all the |
| 382 | // remaining callbacks in the list are null. |
| 383 | TEST(CallbackListTest, NonEmptyListDuringIteration) { |
| 384 | // Declare items such that |cb_reg| is torn down before the subscriptions. |
| 385 | // This ensures the removal callback's invariant that the callback list is |
| 386 | // nonempty will always hold. |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 387 | Remover remover; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 388 | Listener listener; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 389 | CallbackListSubscription remover_sub, listener_sub; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 390 | RepeatingClosureList cb_reg; |
| 391 | cb_reg.set_removal_callback(base::BindRepeating( |
| 392 | [](const RepeatingClosureList* callbacks) { |
| 393 | EXPECT_FALSE(callbacks->empty()); |
| 394 | }, |
| 395 | Unretained(&cb_reg))); |
| 396 | |
| 397 | remover_sub = cb_reg.Add( |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 398 | BindRepeating(&Remover::IncrementTotalAndRemove, Unretained(&remover))); |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 399 | listener_sub = cb_reg.Add( |
| 400 | BindRepeating(&Listener::IncrementTotal, Unretained(&listener))); |
| 401 | |
| 402 | // |remover| will remove |listener|. |
| 403 | remover.SetSubscriptionToRemove(std::move(listener_sub)); |
| 404 | |
| 405 | cb_reg.Notify(); |
| 406 | |
| 407 | EXPECT_EQ(1, remover.total()); |
| 408 | EXPECT_EQ(0, listener.total()); |
| 409 | } |
| 410 | |
| 411 | // empty() should be callable during iteration, and return true if all the |
| 412 | // remaining callbacks in the list are null. |
| 413 | TEST(CallbackListTest, EmptyListDuringIteration) { |
| 414 | OnceClosureList cb_reg; |
| 415 | cb_reg.set_removal_callback(base::BindRepeating( |
| 416 | [](const OnceClosureList* callbacks) { EXPECT_TRUE(callbacks->empty()); }, |
| 417 | Unretained(&cb_reg))); |
| 418 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 419 | Remover remover; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 420 | Listener listener; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 421 | CallbackListSubscription remover_sub = cb_reg.Add( |
| 422 | BindOnce(&Remover::IncrementTotalAndRemove, Unretained(&remover))); |
| 423 | CallbackListSubscription listener_sub = |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 424 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&listener))); |
| 425 | |
| 426 | // |remover| will remove |listener|. |
| 427 | remover.SetSubscriptionToRemove(std::move(listener_sub)); |
| 428 | |
| 429 | cb_reg.Notify(); |
| 430 | |
| 431 | EXPECT_EQ(1, remover.total()); |
| 432 | EXPECT_EQ(0, listener.total()); |
| 433 | } |
| 434 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 435 | TEST(CallbackListTest, RemovalCallback) { |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 436 | Counter remove_count; |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 437 | RepeatingClosureList cb_reg; |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 438 | cb_reg.set_removal_callback( |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 439 | BindRepeating(&Counter::Increment, Unretained(&remove_count))); |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 440 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 441 | CallbackListSubscription subscription = cb_reg.Add(DoNothing()); |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 442 | |
| 443 | // Removing a subscription outside of iteration signals the callback. |
| 444 | EXPECT_EQ(0, remove_count.value()); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 445 | subscription = {}; |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 446 | EXPECT_EQ(1, remove_count.value()); |
| 447 | |
| 448 | // Configure two subscriptions to remove themselves. |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 449 | Remover remover_1, remover_2; |
| 450 | CallbackListSubscription remover_1_sub = cb_reg.Add( |
| 451 | BindRepeating(&Remover::IncrementTotalAndRemove, Unretained(&remover_1))); |
| 452 | CallbackListSubscription remover_2_sub = cb_reg.Add( |
| 453 | BindRepeating(&Remover::IncrementTotalAndRemove, Unretained(&remover_2))); |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 454 | remover_1.SetSubscriptionToRemove(std::move(remover_1_sub)); |
| 455 | remover_2.SetSubscriptionToRemove(std::move(remover_2_sub)); |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 456 | |
| 457 | // The callback should be signaled exactly once. |
| 458 | EXPECT_EQ(1, remove_count.value()); |
| 459 | cb_reg.Notify(); |
| 460 | EXPECT_EQ(2, remove_count.value()); |
| 461 | EXPECT_TRUE(cb_reg.empty()); |
| 462 | } |
| 463 | |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 464 | TEST(CallbackListTest, AbandonSubscriptions) { |
Allen Bauer | 8268630 | 2019-05-14 22:37:47 | [diff] [blame] | 465 | Listener listener; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 466 | CallbackListSubscription subscription; |
Allen Bauer | 8268630 | 2019-05-14 22:37:47 | [diff] [blame] | 467 | { |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 468 | RepeatingClosureList cb_reg; |
Allen Bauer | 8268630 | 2019-05-14 22:37:47 | [diff] [blame] | 469 | subscription = cb_reg.Add( |
| 470 | BindRepeating(&Listener::IncrementTotal, Unretained(&listener))); |
| 471 | // Make sure the callback is signaled while cb_reg is in scope. |
| 472 | cb_reg.Notify(); |
| 473 | // Exiting this scope and running the cb_reg destructor shouldn't fail. |
| 474 | } |
| 475 | EXPECT_EQ(1, listener.total()); |
Peter Kasting | 182606c5 | 2020-05-04 23:13:51 | [diff] [blame] | 476 | |
| 477 | // Destroying the subscription after the list should not cause any problems. |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 478 | subscription = {}; |
Allen Bauer | 8268630 | 2019-05-14 22:37:47 | [diff] [blame] | 479 | } |
| 480 | |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 481 | // Subscriptions should be movable. |
| 482 | TEST(CallbackListTest, MoveSubscription) { |
| 483 | RepeatingClosureList cb_reg; |
| 484 | Listener listener; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 485 | CallbackListSubscription subscription1 = cb_reg.Add( |
| 486 | BindRepeating(&Listener::IncrementTotal, Unretained(&listener))); |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 487 | cb_reg.Notify(); |
| 488 | EXPECT_EQ(1, listener.total()); |
| 489 | |
| 490 | auto subscription2 = std::move(subscription1); |
| 491 | cb_reg.Notify(); |
| 492 | EXPECT_EQ(2, listener.total()); |
| 493 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 494 | subscription2 = {}; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 495 | cb_reg.Notify(); |
| 496 | EXPECT_EQ(2, listener.total()); |
| 497 | } |
| 498 | |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 499 | TEST(CallbackListTest, CancelBeforeRunning) { |
| 500 | OnceClosureList cb_reg; |
| 501 | Listener a; |
| 502 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 503 | CallbackListSubscription a_subscription = |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 504 | cb_reg.Add(BindOnce(&Listener::IncrementTotal, Unretained(&a))); |
| 505 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 506 | EXPECT_TRUE(a_subscription); |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 507 | |
| 508 | // Canceling a OnceCallback before running it should not cause problems. |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 509 | a_subscription = {}; |
Peter Kasting | 670a86b | 2020-05-06 22:50:02 | [diff] [blame] | 510 | cb_reg.Notify(); |
| 511 | |
| 512 | // |a| should not have received any callbacks. |
| 513 | EXPECT_EQ(0, a.total()); |
| 514 | } |
| 515 | |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 516 | // Verifies Notify() can be called reentrantly and what its expected effects |
| 517 | // are. |
| 518 | TEST(CallbackListTest, ReentrantNotify) { |
| 519 | RepeatingClosureList cb_reg; |
| 520 | Listener a, b, c, d; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 521 | CallbackListSubscription a_subscription, c_subscription; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 522 | |
| 523 | // A callback to run for |a|. |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 524 | const auto a_callback = [](RepeatingClosureList* callbacks, Listener* a, |
| 525 | CallbackListSubscription* a_subscription, |
| 526 | const Listener* b, Listener* c, |
| 527 | CallbackListSubscription* c_subscription, |
| 528 | Listener* d) { |
| 529 | // This should be the first callback. |
| 530 | EXPECT_EQ(0, a->total()); |
| 531 | EXPECT_EQ(0, b->total()); |
| 532 | EXPECT_EQ(0, c->total()); |
| 533 | EXPECT_EQ(0, d->total()); |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 534 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 535 | // Increment |a| once. |
| 536 | a->IncrementTotal(); |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 537 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 538 | // Prevent |a| from being incremented again during the reentrant Notify(). |
| 539 | // Since this is the first callback, this also verifies the inner Notify() |
| 540 | // doesn't assume the first callback (or all callbacks) are valid. |
| 541 | *a_subscription = {}; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 542 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 543 | // Add |c| and |d| to be incremented by the reentrant Notify(). |
| 544 | *c_subscription = |
| 545 | callbacks->Add(BindRepeating(&Listener::IncrementTotal, Unretained(c))); |
| 546 | CallbackListSubscription d_subscription = |
| 547 | callbacks->Add(BindRepeating(&Listener::IncrementTotal, Unretained(d))); |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 548 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 549 | // Notify reentrantly. This should not increment |a|, but all the others |
| 550 | // should be incremented. |
| 551 | callbacks->Notify(); |
| 552 | EXPECT_EQ(1, b->total()); |
| 553 | EXPECT_EQ(1, c->total()); |
| 554 | EXPECT_EQ(1, d->total()); |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 555 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 556 | // Since |d_subscription| is locally scoped, it should be canceled before |
| 557 | // the outer Notify() increments |d|. |c_subscription| already exists and |
| 558 | // thus |c| should get incremented again by the outer Notify() even though |
| 559 | // it wasn't scoped when that was called. |
| 560 | }; |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 561 | |
| 562 | // Add |a| and |b| to the list to be notified, and notify. |
| 563 | a_subscription = cb_reg.Add( |
| 564 | BindRepeating(a_callback, Unretained(&cb_reg), Unretained(&a), |
| 565 | Unretained(&a_subscription), Unretained(&b), Unretained(&c), |
| 566 | Unretained(&c_subscription), Unretained(&d))); |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 567 | CallbackListSubscription b_subscription = |
Peter Kasting | c8b3cc94 | 2020-08-11 03:03:54 | [diff] [blame] | 568 | cb_reg.Add(BindRepeating(&Listener::IncrementTotal, Unretained(&b))); |
| 569 | |
| 570 | // Execute both notifications and check the cumulative effect. |
| 571 | cb_reg.Notify(); |
| 572 | EXPECT_EQ(1, a.total()); |
| 573 | EXPECT_EQ(2, b.total()); |
| 574 | EXPECT_EQ(2, c.total()); |
| 575 | EXPECT_EQ(1, d.total()); |
| 576 | } |
| 577 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 578 | } // namespace |
| 579 | } // namespace base |