OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "base/callback_registry.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace base { |
| 15 namespace { |
| 16 |
| 17 class Listener { |
| 18 public: |
| 19 Listener() : total_(0), scaler_(1) {} |
| 20 explicit Listener(int scaler) : total_(0), scaler_(scaler) {} |
| 21 void IncrementTotal() { total_++; } |
| 22 void MultiplyTotal(const int& x) { total_ += x * scaler_; } |
| 23 |
| 24 int total_; |
| 25 private: |
| 26 int scaler_; |
| 27 DISALLOW_COPY_AND_ASSIGN(Listener); |
| 28 }; |
| 29 |
| 30 class Remover { |
| 31 public: |
| 32 Remover() : total_(0) {} |
| 33 void IncrementTotalAndRemove() { |
| 34 total_++; |
| 35 removal_handle_.reset(); |
| 36 } |
| 37 void SetHandleToRemove(scoped_ptr<base::Subscription> handle) { |
| 38 removal_handle_ = handle.Pass(); |
| 39 } |
| 40 |
| 41 int total_; |
| 42 private: |
| 43 scoped_ptr<base::Subscription> removal_handle_; |
| 44 DISALLOW_COPY_AND_ASSIGN(Remover); |
| 45 }; |
| 46 |
| 47 class Adder { |
| 48 public: |
| 49 explicit Adder(CallbackRegistry<void>* cb_reg) |
| 50 : added_(false), |
| 51 total_(0), |
| 52 cb_reg_(cb_reg) {} |
| 53 void AddCallback() { |
| 54 if (!added_) { |
| 55 added_ = true; |
| 56 handle_ = cb_reg_->Add( |
| 57 base::Bind(&Adder::IncrementTotal, base::Unretained(this))); |
| 58 } |
| 59 } |
| 60 void IncrementTotal() { total_++; } |
| 61 |
| 62 bool added_; |
| 63 int total_; |
| 64 private: |
| 65 CallbackRegistry<void>* cb_reg_; |
| 66 scoped_ptr<base::Subscription> handle_; |
| 67 DISALLOW_COPY_AND_ASSIGN(Adder); |
| 68 }; |
| 69 |
| 70 // Sanity check that closures added to the list will be run, and those removed |
| 71 // from the list will not be run. |
| 72 TEST(CallbackRegistryTest, BasicTest) { |
| 73 CallbackRegistry<void> cb_reg; |
| 74 Listener a, b, c; |
| 75 |
| 76 scoped_ptr<base::Subscription> a_handle = cb_reg.Add( |
| 77 base::Bind(&Listener::IncrementTotal, base::Unretained(&a))); |
| 78 scoped_ptr<base::Subscription> b_handle = cb_reg.Add( |
| 79 base::Bind(&Listener::IncrementTotal, base::Unretained(&b))); |
| 80 |
| 81 EXPECT_TRUE(a_handle.get()); |
| 82 EXPECT_TRUE(b_handle.get()); |
| 83 |
| 84 cb_reg.Notify(); |
| 85 |
| 86 EXPECT_EQ(1, a.total_); |
| 87 EXPECT_EQ(1, b.total_); |
| 88 |
| 89 b_handle.reset(); |
| 90 |
| 91 scoped_ptr<base::Subscription> c_handle = cb_reg.Add( |
| 92 base::Bind(&Listener::IncrementTotal, base::Unretained(&c))); |
| 93 |
| 94 cb_reg.Notify(); |
| 95 |
| 96 EXPECT_EQ(2, a.total_); |
| 97 EXPECT_EQ(1, b.total_); |
| 98 EXPECT_EQ(1, c.total_); |
| 99 |
| 100 a_handle.reset(); |
| 101 b_handle.reset(); |
| 102 c_handle.reset(); |
| 103 } |
| 104 |
| 105 // Sanity check that callbacks with details added to the list will be run, with |
| 106 // the correct details, and those removed from the list will not be run. |
| 107 TEST(CallbackRegistryTest, BasicTestWithParams) { |
| 108 CallbackRegistry<int> cb_reg; |
| 109 Listener a(1), b(-1), c(1); |
| 110 |
| 111 scoped_ptr<base::Subscription> a_handle = cb_reg.Add( |
| 112 base::Bind(&Listener::MultiplyTotal, base::Unretained(&a))); |
| 113 scoped_ptr<base::Subscription> b_handle = cb_reg.Add( |
| 114 base::Bind(&Listener::MultiplyTotal, base::Unretained(&b))); |
| 115 |
| 116 EXPECT_TRUE(a_handle.get()); |
| 117 EXPECT_TRUE(b_handle.get()); |
| 118 |
| 119 cb_reg.Notify(10); |
| 120 |
| 121 EXPECT_EQ(10, a.total_); |
| 122 EXPECT_EQ(-10, b.total_); |
| 123 |
| 124 b_handle.reset(); |
| 125 |
| 126 scoped_ptr<base::Subscription> c_handle = cb_reg.Add( |
| 127 base::Bind(&Listener::MultiplyTotal, base::Unretained(&c))); |
| 128 |
| 129 cb_reg.Notify(10); |
| 130 |
| 131 EXPECT_EQ(20, a.total_); |
| 132 EXPECT_EQ(-10, b.total_); |
| 133 EXPECT_EQ(10, c.total_); |
| 134 |
| 135 a_handle.reset(); |
| 136 b_handle.reset(); |
| 137 c_handle.reset(); |
| 138 } |
| 139 |
| 140 // Test the a callback can remove itself or a different callback from the list |
| 141 // during iteration without invalidating the iterator. |
| 142 TEST(CallbackRegistryTest, RemoveCallbacksDuringIteration) { |
| 143 CallbackRegistry<void> cb_reg; |
| 144 Listener a, b; |
| 145 Remover remover_1, remover_2; |
| 146 |
| 147 scoped_ptr<base::Subscription> remover_1_handle = cb_reg.Add( |
| 148 base::Bind(&Remover::IncrementTotalAndRemove, |
| 149 base::Unretained(&remover_1))); |
| 150 scoped_ptr<base::Subscription> remover_2_handle = cb_reg.Add( |
| 151 base::Bind(&Remover::IncrementTotalAndRemove, |
| 152 base::Unretained(&remover_2))); |
| 153 scoped_ptr<base::Subscription> a_handle = cb_reg.Add( |
| 154 base::Bind(&Listener::IncrementTotal, base::Unretained(&a))); |
| 155 scoped_ptr<base::Subscription> b_handle = cb_reg.Add( |
| 156 base::Bind(&Listener::IncrementTotal, base::Unretained(&b))); |
| 157 |
| 158 // |remover_1| will remove itself. |
| 159 remover_1.SetHandleToRemove(remover_1_handle.Pass()); |
| 160 // |remover_2| will remove a. |
| 161 remover_2.SetHandleToRemove(a_handle.Pass()); |
| 162 |
| 163 cb_reg.Notify(); |
| 164 |
| 165 // |remover_1| runs once (and removes itself), |remover_2| runs once (and |
| 166 // removes a), |a| never runs, and |b| runs once. |
| 167 EXPECT_EQ(1, remover_1.total_); |
| 168 EXPECT_EQ(1, remover_2.total_); |
| 169 EXPECT_EQ(0, a.total_); |
| 170 EXPECT_EQ(1, b.total_); |
| 171 |
| 172 cb_reg.Notify(); |
| 173 |
| 174 // Only |remover_2| and |b| run this time. |
| 175 EXPECT_EQ(1, remover_1.total_); |
| 176 EXPECT_EQ(2, remover_2.total_); |
| 177 EXPECT_EQ(0, a.total_); |
| 178 EXPECT_EQ(2, b.total_); |
| 179 } |
| 180 |
| 181 // Test that a callback can add another callback to the list durning iteration |
| 182 // without invalidating the iterator. |
| 183 // - If the CallbackNotificationType is |CALLBACKS_NOTIFY_ALL|, the newly added |
| 184 // callback will be run on the current iteration. |
| 185 // - All other callbacks in the list will be run as well. |
| 186 TEST(CallbackRegistryTest, AddCallbacksDuringIteration) { |
| 187 CallbackRegistry<void> cb_reg; |
| 188 Adder a(&cb_reg); |
| 189 Listener b; |
| 190 scoped_ptr<base::Subscription> a_handle = cb_reg.Add( |
| 191 base::Bind(&Adder::AddCallback, base::Unretained(&a))); |
| 192 scoped_ptr<base::Subscription> b_handle = cb_reg.Add( |
| 193 base::Bind(&Listener::IncrementTotal, base::Unretained(&b))); |
| 194 |
| 195 cb_reg.Notify(); |
| 196 |
| 197 EXPECT_EQ(1, a.total_); |
| 198 EXPECT_EQ(1, b.total_); |
| 199 EXPECT_TRUE(a.added_); |
| 200 |
| 201 cb_reg.Notify(); |
| 202 |
| 203 EXPECT_EQ(2, a.total_); |
| 204 EXPECT_EQ(2, b.total_); |
| 205 } |
| 206 |
| 207 } // namespace |
| 208 } // namespace base |
OLD | NEW |