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