Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/callback_registry.h" | 5 #include "base/callback_registry.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class Listener { | 16 class Listener { |
| 17 public: | 17 public: |
| 18 Listener() : total_(0), scaler_(1) {} | 18 Listener() : total_(0), scaler_(1) {} |
| 19 explicit Listener(int scaler) : total_(0), scaler_(scaler) {} | 19 explicit Listener(int scaler) : total_(0), scaler_(scaler) {} |
| 20 void IncrementTotal() { total_++; } | 20 void IncrementTotal() { total_++; } |
| 21 void IncrementByMultipleOfScaler(const int& x) { total_ += x * scaler_; } | 21 void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; } |
| 22 | 22 |
| 23 int total_; | 23 int total_; |
| 24 | 24 |
| 25 private: | 25 private: |
| 26 int scaler_; | 26 int scaler_; |
| 27 DISALLOW_COPY_AND_ASSIGN(Listener); | 27 DISALLOW_COPY_AND_ASSIGN(Listener); |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 class Remover { | 30 class Remover { |
| 31 public: | 31 public: |
| 32 Remover() : total_(0) {} | 32 Remover() : total_(0) {} |
| 33 void IncrementTotalAndRemove() { | 33 void IncrementTotalAndRemove() { |
| 34 total_++; | 34 total_++; |
| 35 removal_subscription_.reset(); | 35 removal_subscription_.reset(); |
| 36 } | 36 } |
| 37 void SetSubscriptionToRemove( | 37 void SetSubscriptionToRemove( |
| 38 scoped_ptr<CallbackRegistry<void>::Subscription> sub) { | 38 scoped_ptr<CallbackRegistry<void(void)>::Subscription> sub) { |
| 39 removal_subscription_ = sub.Pass(); | 39 removal_subscription_ = sub.Pass(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 int total_; | 42 int total_; |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 scoped_ptr<CallbackRegistry<void>::Subscription> removal_subscription_; | 45 scoped_ptr<CallbackRegistry<void(void)>::Subscription> removal_subscription_; |
| 46 DISALLOW_COPY_AND_ASSIGN(Remover); | 46 DISALLOW_COPY_AND_ASSIGN(Remover); |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 class Adder { | 49 class Adder { |
| 50 public: | 50 public: |
| 51 explicit Adder(CallbackRegistry<void>* cb_reg) | 51 explicit Adder(CallbackRegistry<void(void)>* cb_reg) |
| 52 : added_(false), | 52 : added_(false), |
| 53 total_(0), | 53 total_(0), |
| 54 cb_reg_(cb_reg) {} | 54 cb_reg_(cb_reg) {} |
| 55 void AddCallback() { | 55 void AddCallback() { |
| 56 if (!added_) { | 56 if (!added_) { |
| 57 added_ = true; | 57 added_ = true; |
| 58 subscription_ = | 58 subscription_ = |
| 59 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this))); | 59 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this))); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 void IncrementTotal() { total_++; } | 62 void IncrementTotal() { total_++; } |
| 63 | 63 |
| 64 bool added_; | 64 bool added_; |
| 65 int total_; | 65 int total_; |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 CallbackRegistry<void>* cb_reg_; | 68 CallbackRegistry<void(void)>* cb_reg_; |
| 69 scoped_ptr<CallbackRegistry<void>::Subscription> subscription_; | 69 scoped_ptr<CallbackRegistry<void(void)>::Subscription> subscription_; |
| 70 DISALLOW_COPY_AND_ASSIGN(Adder); | 70 DISALLOW_COPY_AND_ASSIGN(Adder); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 class Summer { | |
| 74 public: | |
| 75 Summer() : value_(0) {} | |
| 76 | |
| 77 void AddOneParam(int a) { value_ = a; } | |
| 78 void AddTwoParam(int a, int b) { value_ = a + b; } | |
| 79 void AddThreeParam(int a, int b, int c) { value_ = a + b + c; } | |
| 80 void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; } | |
| 81 void AddFiveParam(int a, int b, int c, int d, int e) { | |
| 82 value_ = a + b + c + d + e; | |
| 83 } | |
| 84 void AddSixParam(int a, int b, int c, int d, int e , int f) { | |
| 85 value_ = a + b + c + d + e + f; | |
| 86 } | |
| 87 | |
| 88 int value_; | |
| 89 | |
| 90 private: | |
| 91 DISALLOW_COPY_AND_ASSIGN(Summer); | |
| 92 }; | |
| 93 | |
| 94 | |
| 95 // TODO(caitkp): Learn how non-compile unittests work. Put the following code | |
| 96 // in there. | |
| 97 /*class Foo { | |
|
awong
2013/09/20 20:06:25
Look at base_unittests.nc and copy the pattern the
| |
| 98 public: | |
| 99 Foo() {} | |
| 100 ~Foo() {} | |
| 101 }; | |
| 102 | |
| 103 class ThisShouldNotWork { | |
| 104 public: | |
| 105 ThisShouldNotWork() {} | |
| 106 | |
| 107 void GotAScopedFoo(scoped_ptr<Foo> f) { foo_ = f.Pass(); } | |
| 108 | |
| 109 scoped_ptr<Foo> foo_; | |
| 110 | |
| 111 private: | |
| 112 DISALLOW_COPY_AND_ASSIGN(ThisShouldNotWork); | |
| 113 }; | |
| 114 | |
| 115 TEST(CallbackRegistryTest, ThisShouldNotWork) { | |
| 116 ThisShouldNotWork t; | |
| 117 CallbackRegistry<void(scoped_ptr<Foo>)> c1; | |
| 118 scoped_ptr<CallbackRegistry<void(scoped_ptr<Foo>)>::Subscription> sub = | |
| 119 c1.Add(Bind(&ThisShouldNotWork::GotAScopedFoo, Unretained(&t))); | |
| 120 c1.Notify(scoped_ptr<Foo>(new Foo())); | |
| 121 }*/ | |
| 122 | |
| 123 // Sanity check that we can instantiate a CallbackRegistry for each arity. | |
| 124 TEST(CallbackRegistryTest, ArityTest) { | |
| 125 Summer s; | |
| 126 | |
| 127 CallbackRegistry<void(int)> c1; | |
| 128 scoped_ptr<CallbackRegistry<void(int)>::Subscription> subscription1 = | |
| 129 c1.Add(Bind(&Summer::AddOneParam, Unretained(&s))); | |
| 130 | |
| 131 c1.Notify(1); | |
| 132 EXPECT_EQ(1, s.value_); | |
| 133 | |
| 134 CallbackRegistry<void(int, int)> c2; | |
| 135 scoped_ptr<CallbackRegistry<void(int, int)>::Subscription> subscription2 = | |
| 136 c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s))); | |
| 137 | |
| 138 c2.Notify(1, 2); | |
| 139 EXPECT_EQ(3, s.value_); | |
| 140 | |
| 141 CallbackRegistry<void(int, int, int)> c3; | |
| 142 scoped_ptr<CallbackRegistry<void(int, int, int)>::Subscription> | |
| 143 subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s))); | |
| 144 | |
| 145 c3.Notify(1, 2, 3); | |
| 146 EXPECT_EQ(6, s.value_); | |
| 147 | |
| 148 CallbackRegistry<void(int, int, int, int)> c4; | |
| 149 scoped_ptr<CallbackRegistry<void(int, int, int, int)>::Subscription> | |
| 150 subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s))); | |
| 151 | |
| 152 c4.Notify(1, 2, 3, 4); | |
| 153 EXPECT_EQ(10, s.value_); | |
| 154 | |
| 155 CallbackRegistry<void(int, int, int, int, int)> c5; | |
| 156 scoped_ptr<CallbackRegistry<void(int, int, int, int, int)>::Subscription> | |
| 157 subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s))); | |
| 158 | |
| 159 c5.Notify(1, 2, 3, 4, 5); | |
| 160 EXPECT_EQ(15, s.value_); | |
| 161 | |
| 162 CallbackRegistry<void(int, int, int, int, int, int)> c6; | |
| 163 scoped_ptr<CallbackRegistry<void(int, int, int, int, int, int)>::Subscription> | |
| 164 subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s))); | |
| 165 | |
| 166 c6.Notify(1, 2, 3, 4, 5, 6); | |
| 167 EXPECT_EQ(21, s.value_); | |
| 168 } | |
| 169 | |
| 73 // Sanity check that closures added to the list will be run, and those removed | 170 // Sanity check that closures added to the list will be run, and those removed |
| 74 // from the list will not be run. | 171 // from the list will not be run. |
| 75 TEST(CallbackRegistryTest, BasicTest) { | 172 TEST(CallbackRegistryTest, BasicTest) { |
| 76 CallbackRegistry<void> cb_reg; | 173 CallbackRegistry<void(void)> cb_reg; |
| 77 Listener a, b, c; | 174 Listener a, b, c; |
| 78 | 175 |
| 79 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription = | 176 scoped_ptr<CallbackRegistry<void(void)>::Subscription> a_subscription = |
| 80 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); | 177 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); |
| 81 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription = | 178 scoped_ptr<CallbackRegistry<void(void)>::Subscription> b_subscription = |
| 82 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); | 179 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); |
| 83 | 180 |
| 84 EXPECT_TRUE(a_subscription.get()); | 181 EXPECT_TRUE(a_subscription.get()); |
| 85 EXPECT_TRUE(b_subscription.get()); | 182 EXPECT_TRUE(b_subscription.get()); |
| 86 | 183 |
| 87 cb_reg.Notify(); | 184 cb_reg.Notify(); |
| 88 | 185 |
| 89 EXPECT_EQ(1, a.total_); | 186 EXPECT_EQ(1, a.total_); |
| 90 EXPECT_EQ(1, b.total_); | 187 EXPECT_EQ(1, b.total_); |
| 91 | 188 |
| 92 b_subscription.reset(); | 189 b_subscription.reset(); |
| 93 | 190 |
| 94 scoped_ptr<CallbackRegistry<void>::Subscription> c_subscription = | 191 scoped_ptr<CallbackRegistry<void(void)>::Subscription> c_subscription = |
| 95 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c))); | 192 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c))); |
| 96 | 193 |
| 97 cb_reg.Notify(); | 194 cb_reg.Notify(); |
| 98 | 195 |
| 99 EXPECT_EQ(2, a.total_); | 196 EXPECT_EQ(2, a.total_); |
| 100 EXPECT_EQ(1, b.total_); | 197 EXPECT_EQ(1, b.total_); |
| 101 EXPECT_EQ(1, c.total_); | 198 EXPECT_EQ(1, c.total_); |
| 102 | 199 |
| 103 a_subscription.reset(); | 200 a_subscription.reset(); |
| 104 b_subscription.reset(); | 201 b_subscription.reset(); |
| 105 c_subscription.reset(); | 202 c_subscription.reset(); |
| 106 } | 203 } |
| 107 | 204 |
| 108 // Sanity check that callbacks with details added to the list will be run, with | 205 // Sanity check that callbacks with details added to the list will be run, with |
| 109 // the correct details, and those removed from the list will not be run. | 206 // the correct details, and those removed from the list will not be run. |
| 110 TEST(CallbackRegistryTest, BasicTestWithParams) { | 207 TEST(CallbackRegistryTest, BasicTestWithParams) { |
| 111 CallbackRegistry<int> cb_reg; | 208 CallbackRegistry<void(int)> cb_reg; |
| 112 Listener a(1), b(-1), c(1); | 209 Listener a(1), b(-1), c(1); |
| 113 | 210 |
| 114 scoped_ptr<CallbackRegistry<int>::Subscription> a_subscription = | 211 scoped_ptr<CallbackRegistry<void(int)>::Subscription> a_subscription = |
| 115 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a))); | 212 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a))); |
| 116 scoped_ptr<CallbackRegistry<int>::Subscription> b_subscription = | 213 scoped_ptr<CallbackRegistry<void(int)>::Subscription> b_subscription = |
| 117 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b))); | 214 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b))); |
| 118 | 215 |
| 119 EXPECT_TRUE(a_subscription.get()); | 216 EXPECT_TRUE(a_subscription.get()); |
| 120 EXPECT_TRUE(b_subscription.get()); | 217 EXPECT_TRUE(b_subscription.get()); |
| 121 | 218 |
| 122 cb_reg.Notify(10); | 219 cb_reg.Notify(10); |
| 123 | 220 |
| 124 EXPECT_EQ(10, a.total_); | 221 EXPECT_EQ(10, a.total_); |
| 125 EXPECT_EQ(-10, b.total_); | 222 EXPECT_EQ(-10, b.total_); |
| 126 | 223 |
| 127 b_subscription.reset(); | 224 b_subscription.reset(); |
| 128 | 225 |
| 129 scoped_ptr<CallbackRegistry<int>::Subscription> c_subscription = | 226 scoped_ptr<CallbackRegistry<void(int)>::Subscription> c_subscription = |
| 130 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c))); | 227 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c))); |
| 131 | 228 |
| 132 cb_reg.Notify(10); | 229 cb_reg.Notify(10); |
| 133 | 230 |
| 134 EXPECT_EQ(20, a.total_); | 231 EXPECT_EQ(20, a.total_); |
| 135 EXPECT_EQ(-10, b.total_); | 232 EXPECT_EQ(-10, b.total_); |
| 136 EXPECT_EQ(10, c.total_); | 233 EXPECT_EQ(10, c.total_); |
| 137 | 234 |
| 138 a_subscription.reset(); | 235 a_subscription.reset(); |
| 139 b_subscription.reset(); | 236 b_subscription.reset(); |
| 140 c_subscription.reset(); | 237 c_subscription.reset(); |
| 141 } | 238 } |
| 142 | 239 |
| 143 // Test the a callback can remove itself or a different callback from the list | 240 // Test the a callback can remove itself or a different callback from the list |
| 144 // during iteration without invalidating the iterator. | 241 // during iteration without invalidating the iterator. |
| 145 TEST(CallbackRegistryTest, RemoveCallbacksDuringIteration) { | 242 TEST(CallbackRegistryTest, RemoveCallbacksDuringIteration) { |
| 146 CallbackRegistry<void> cb_reg; | 243 CallbackRegistry<void(void)> cb_reg; |
| 147 Listener a, b; | 244 Listener a, b; |
| 148 Remover remover_1, remover_2; | 245 Remover remover_1, remover_2; |
| 149 | 246 |
| 150 scoped_ptr<CallbackRegistry<void>::Subscription> remover_1_subscription = | 247 scoped_ptr<CallbackRegistry<void(void)>::Subscription> remover_1_sub = |
| 151 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove, | 248 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove, |
| 152 Unretained(&remover_1))); | 249 Unretained(&remover_1))); |
| 153 scoped_ptr<CallbackRegistry<void>::Subscription> remover_2_subscription = | 250 scoped_ptr<CallbackRegistry<void(void)>::Subscription> remover_2_sub = |
| 154 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove, | 251 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove, |
| 155 Unretained(&remover_2))); | 252 Unretained(&remover_2))); |
| 156 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription = | 253 scoped_ptr<CallbackRegistry<void(void)>::Subscription> a_subscription = |
| 157 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); | 254 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); |
| 158 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription = | 255 scoped_ptr<CallbackRegistry<void(void)>::Subscription> b_subscription = |
| 159 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); | 256 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); |
| 160 | 257 |
| 161 // |remover_1| will remove itself. | 258 // |remover_1| will remove itself. |
| 162 remover_1.SetSubscriptionToRemove(remover_1_subscription.Pass()); | 259 remover_1.SetSubscriptionToRemove(remover_1_sub.Pass()); |
| 163 // |remover_2| will remove a. | 260 // |remover_2| will remove a. |
| 164 remover_2.SetSubscriptionToRemove(a_subscription.Pass()); | 261 remover_2.SetSubscriptionToRemove(a_subscription.Pass()); |
| 165 | 262 |
| 166 cb_reg.Notify(); | 263 cb_reg.Notify(); |
| 167 | 264 |
| 168 // |remover_1| runs once (and removes itself), |remover_2| runs once (and | 265 // |remover_1| runs once (and removes itself), |remover_2| runs once (and |
| 169 // removes a), |a| never runs, and |b| runs once. | 266 // removes a), |a| never runs, and |b| runs once. |
| 170 EXPECT_EQ(1, remover_1.total_); | 267 EXPECT_EQ(1, remover_1.total_); |
| 171 EXPECT_EQ(1, remover_2.total_); | 268 EXPECT_EQ(1, remover_2.total_); |
| 172 EXPECT_EQ(0, a.total_); | 269 EXPECT_EQ(0, a.total_); |
| 173 EXPECT_EQ(1, b.total_); | 270 EXPECT_EQ(1, b.total_); |
| 174 | 271 |
| 175 cb_reg.Notify(); | 272 cb_reg.Notify(); |
| 176 | 273 |
| 177 // Only |remover_2| and |b| run this time. | 274 // Only |remover_2| and |b| run this time. |
| 178 EXPECT_EQ(1, remover_1.total_); | 275 EXPECT_EQ(1, remover_1.total_); |
| 179 EXPECT_EQ(2, remover_2.total_); | 276 EXPECT_EQ(2, remover_2.total_); |
| 180 EXPECT_EQ(0, a.total_); | 277 EXPECT_EQ(0, a.total_); |
| 181 EXPECT_EQ(2, b.total_); | 278 EXPECT_EQ(2, b.total_); |
| 182 } | 279 } |
| 183 | 280 |
| 184 // Test that a callback can add another callback to the list durning iteration | 281 // Test that a callback can add another callback to the list durning iteration |
| 185 // without invalidating the iterator. The newly added callback should be run on | 282 // without invalidating the iterator. The newly added callback should be run on |
| 186 // the current iteration as will all other callbacks in the list. | 283 // the current iteration as will all other callbacks in the list. |
| 187 TEST(CallbackRegistryTest, AddCallbacksDuringIteration) { | 284 TEST(CallbackRegistryTest, AddCallbacksDuringIteration) { |
| 188 CallbackRegistry<void> cb_reg; | 285 CallbackRegistry<void(void)> cb_reg; |
| 189 Adder a(&cb_reg); | 286 Adder a(&cb_reg); |
| 190 Listener b; | 287 Listener b; |
| 191 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription = | 288 scoped_ptr<CallbackRegistry<void(void)>::Subscription> a_subscription = |
| 192 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a))); | 289 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a))); |
| 193 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription = | 290 scoped_ptr<CallbackRegistry<void(void)>::Subscription> b_subscription = |
| 194 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); | 291 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); |
| 195 | 292 |
| 196 cb_reg.Notify(); | 293 cb_reg.Notify(); |
| 197 | 294 |
| 198 EXPECT_EQ(1, a.total_); | 295 EXPECT_EQ(1, a.total_); |
| 199 EXPECT_EQ(1, b.total_); | 296 EXPECT_EQ(1, b.total_); |
| 200 EXPECT_TRUE(a.added_); | 297 EXPECT_TRUE(a.added_); |
| 201 | 298 |
| 202 cb_reg.Notify(); | 299 cb_reg.Notify(); |
| 203 | 300 |
| 204 EXPECT_EQ(2, a.total_); | 301 EXPECT_EQ(2, a.total_); |
| 205 EXPECT_EQ(2, b.total_); | 302 EXPECT_EQ(2, b.total_); |
| 206 } | 303 } |
| 207 | 304 |
| 208 // Sanity check: notifying an empty list is a no-op. | 305 // Sanity check: notifying an empty list is a no-op. |
| 209 TEST(CallbackRegistryTest, EmptyList) { | 306 TEST(CallbackRegistryTest, EmptyList) { |
| 210 CallbackRegistry<void> cb_reg; | 307 CallbackRegistry<void(void)> cb_reg; |
| 211 | 308 |
| 212 cb_reg.Notify(); | 309 cb_reg.Notify(); |
| 213 } | 310 } |
| 214 | 311 |
| 215 } // namespace | 312 } // namespace |
| 216 } // namespace base | 313 } // namespace base |
| OLD | NEW |