Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: base/callback_list_unittest.cc

Issue 23645019: C++ Readability Review for caitkp (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« base/callback_list.h ('K') | « base/callback_list.h.pump ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_list.h" 5 #include "base/callback_list.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(int x) { total_ += x * scaler_; } 21 void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; }
22 22
23 int total_; 23 int total() { return total_; }
milos 2013/10/03 15:49:43 A nit - the function could be declared const, see
Cait (Slow) 2013/10/03 15:58:11 Done.
24 24
25 private: 25 private:
26 int total_;
26 int scaler_; 27 int scaler_;
27 DISALLOW_COPY_AND_ASSIGN(Listener); 28 DISALLOW_COPY_AND_ASSIGN(Listener);
28 }; 29 };
29 30
30 class Remover { 31 class Remover {
31 public: 32 public:
32 Remover() : total_(0) {} 33 Remover() : total_(0) {}
33 void IncrementTotalAndRemove() { 34 void IncrementTotalAndRemove() {
34 total_++; 35 total_++;
35 removal_subscription_.reset(); 36 removal_subscription_.reset();
36 } 37 }
37 void SetSubscriptionToRemove( 38 void SetSubscriptionToRemove(
38 scoped_ptr<CallbackList<void(void)>::Subscription> sub) { 39 scoped_ptr<CallbackList<void(void)>::Subscription> sub) {
39 removal_subscription_ = sub.Pass(); 40 removal_subscription_ = sub.Pass();
40 } 41 }
41 42
42 int total_; 43 int total() { return total_; }
43 44
44 private: 45 private:
46 int total_;
45 scoped_ptr<CallbackList<void(void)>::Subscription> removal_subscription_; 47 scoped_ptr<CallbackList<void(void)>::Subscription> removal_subscription_;
46 DISALLOW_COPY_AND_ASSIGN(Remover); 48 DISALLOW_COPY_AND_ASSIGN(Remover);
47 }; 49 };
48 50
49 class Adder { 51 class Adder {
50 public: 52 public:
51 explicit Adder(CallbackList<void(void)>* cb_reg) 53 explicit Adder(CallbackList<void(void)>* cb_reg)
52 : added_(false), 54 : added_(false),
53 total_(0), 55 total_(0),
54 cb_reg_(cb_reg) {} 56 cb_reg_(cb_reg) {}
milos 2013/10/03 15:49:43 } should be on the next line.
Cait (Slow) 2013/10/03 15:58:11 Done.
55 void AddCallback() { 57 void AddCallback() {
56 if (!added_) { 58 if (!added_) {
57 added_ = true; 59 added_ = true;
58 subscription_ = 60 subscription_ =
59 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this))); 61 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this)));
60 } 62 }
61 } 63 }
62 void IncrementTotal() { total_++; } 64 void IncrementTotal() { total_++; }
63 65
66 bool added() { return added_; }
67
68 int total() { return total_; }
69
70 private:
64 bool added_; 71 bool added_;
65 int total_; 72 int total_;
66
67 private:
68 CallbackList<void(void)>* cb_reg_; 73 CallbackList<void(void)>* cb_reg_;
69 scoped_ptr<CallbackList<void(void)>::Subscription> subscription_; 74 scoped_ptr<CallbackList<void(void)>::Subscription> subscription_;
70 DISALLOW_COPY_AND_ASSIGN(Adder); 75 DISALLOW_COPY_AND_ASSIGN(Adder);
71 }; 76 };
72 77
73 class Summer { 78 class Summer {
74 public: 79 public:
75 Summer() : value_(0) {} 80 Summer() : value_(0) {}
76 81
77 void AddOneParam(int a) { value_ = a; } 82 void AddOneParam(int a) { value_ = a; }
78 void AddTwoParam(int a, int b) { value_ = a + b; } 83 void AddTwoParam(int a, int b) { value_ = a + b; }
79 void AddThreeParam(int a, int b, int c) { value_ = a + b + c; } 84 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; } 85 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) { 86 void AddFiveParam(int a, int b, int c, int d, int e) {
82 value_ = a + b + c + d + e; 87 value_ = a + b + c + d + e;
83 } 88 }
84 void AddSixParam(int a, int b, int c, int d, int e , int f) { 89 void AddSixParam(int a, int b, int c, int d, int e , int f) {
85 value_ = a + b + c + d + e + f; 90 value_ = a + b + c + d + e + f;
86 } 91 }
87 92
88 int value_; 93 int value() { return value_; }
89 94
90 private: 95 private:
96 int value_;
91 DISALLOW_COPY_AND_ASSIGN(Summer); 97 DISALLOW_COPY_AND_ASSIGN(Summer);
92 }; 98 };
93 99
94 // Sanity check that we can instantiate a CallbackList for each arity. 100 // Sanity check that we can instantiate a CallbackList for each arity.
95 TEST(CallbackListTest, ArityTest) { 101 TEST(CallbackListTest, ArityTest) {
96 Summer s; 102 Summer s;
97 103
98 CallbackList<void(int)> c1; 104 CallbackList<void(int)> c1;
99 scoped_ptr<CallbackList<void(int)>::Subscription> subscription1 = 105 scoped_ptr<CallbackList<void(int)>::Subscription> subscription1 =
100 c1.Add(Bind(&Summer::AddOneParam, Unretained(&s))); 106 c1.Add(Bind(&Summer::AddOneParam, Unretained(&s)));
101 107
102 c1.Notify(1); 108 c1.Notify(1);
103 EXPECT_EQ(1, s.value_); 109 EXPECT_EQ(1, s.value());
104 110
105 CallbackList<void(int, int)> c2; 111 CallbackList<void(int, int)> c2;
106 scoped_ptr<CallbackList<void(int, int)>::Subscription> subscription2 = 112 scoped_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
107 c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s))); 113 c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s)));
108 114
109 c2.Notify(1, 2); 115 c2.Notify(1, 2);
110 EXPECT_EQ(3, s.value_); 116 EXPECT_EQ(3, s.value());
111 117
112 CallbackList<void(int, int, int)> c3; 118 CallbackList<void(int, int, int)> c3;
113 scoped_ptr<CallbackList<void(int, int, int)>::Subscription> 119 scoped_ptr<CallbackList<void(int, int, int)>::Subscription>
114 subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s))); 120 subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s)));
115 121
116 c3.Notify(1, 2, 3); 122 c3.Notify(1, 2, 3);
117 EXPECT_EQ(6, s.value_); 123 EXPECT_EQ(6, s.value());
118 124
119 CallbackList<void(int, int, int, int)> c4; 125 CallbackList<void(int, int, int, int)> c4;
120 scoped_ptr<CallbackList<void(int, int, int, int)>::Subscription> 126 scoped_ptr<CallbackList<void(int, int, int, int)>::Subscription>
121 subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s))); 127 subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s)));
122 128
123 c4.Notify(1, 2, 3, 4); 129 c4.Notify(1, 2, 3, 4);
124 EXPECT_EQ(10, s.value_); 130 EXPECT_EQ(10, s.value());
125 131
126 CallbackList<void(int, int, int, int, int)> c5; 132 CallbackList<void(int, int, int, int, int)> c5;
127 scoped_ptr<CallbackList<void(int, int, int, int, int)>::Subscription> 133 scoped_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
128 subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s))); 134 subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s)));
129 135
130 c5.Notify(1, 2, 3, 4, 5); 136 c5.Notify(1, 2, 3, 4, 5);
131 EXPECT_EQ(15, s.value_); 137 EXPECT_EQ(15, s.value());
132 138
133 CallbackList<void(int, int, int, int, int, int)> c6; 139 CallbackList<void(int, int, int, int, int, int)> c6;
134 scoped_ptr<CallbackList<void(int, int, int, int, int, int)>::Subscription> 140 scoped_ptr<CallbackList<void(int, int, int, int, int, int)>::Subscription>
135 subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s))); 141 subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s)));
136 142
137 c6.Notify(1, 2, 3, 4, 5, 6); 143 c6.Notify(1, 2, 3, 4, 5, 6);
138 EXPECT_EQ(21, s.value_); 144 EXPECT_EQ(21, s.value());
139 } 145 }
140 146
141 // Sanity check that closures added to the list will be run, and those removed 147 // Sanity check that closures added to the list will be run, and those removed
142 // from the list will not be run. 148 // from the list will not be run.
143 TEST(CallbackListTest, BasicTest) { 149 TEST(CallbackListTest, BasicTest) {
144 CallbackList<void(void)> cb_reg; 150 CallbackList<void(void)> cb_reg;
145 Listener a, b, c; 151 Listener a, b, c;
146 152
147 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription = 153 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
148 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a))); 154 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
149 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription = 155 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
150 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); 156 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
151 157
152 EXPECT_TRUE(a_subscription.get()); 158 EXPECT_TRUE(a_subscription.get());
153 EXPECT_TRUE(b_subscription.get()); 159 EXPECT_TRUE(b_subscription.get());
154 160
155 cb_reg.Notify(); 161 cb_reg.Notify();
156 162
157 EXPECT_EQ(1, a.total_); 163 EXPECT_EQ(1, a.total());
158 EXPECT_EQ(1, b.total_); 164 EXPECT_EQ(1, b.total());
159 165
160 b_subscription.reset(); 166 b_subscription.reset();
161 167
162 scoped_ptr<CallbackList<void(void)>::Subscription> c_subscription = 168 scoped_ptr<CallbackList<void(void)>::Subscription> c_subscription =
163 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c))); 169 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
164 170
165 cb_reg.Notify(); 171 cb_reg.Notify();
166 172
167 EXPECT_EQ(2, a.total_); 173 EXPECT_EQ(2, a.total());
168 EXPECT_EQ(1, b.total_); 174 EXPECT_EQ(1, b.total());
169 EXPECT_EQ(1, c.total_); 175 EXPECT_EQ(1, c.total());
170 176
171 a_subscription.reset(); 177 a_subscription.reset();
172 b_subscription.reset(); 178 b_subscription.reset();
173 c_subscription.reset(); 179 c_subscription.reset();
174 } 180 }
175 181
176 // Sanity check that callbacks with details added to the list will be run, with 182 // Sanity check that callbacks with details added to the list will be run, with
177 // the correct details, and those removed from the list will not be run. 183 // the correct details, and those removed from the list will not be run.
178 TEST(CallbackListTest, BasicTestWithParams) { 184 TEST(CallbackListTest, BasicTestWithParams) {
179 CallbackList<void(int)> cb_reg; 185 CallbackList<void(int)> cb_reg;
180 Listener a(1), b(-1), c(1); 186 Listener a(1), b(-1), c(1);
181 187
182 scoped_ptr<CallbackList<void(int)>::Subscription> a_subscription = 188 scoped_ptr<CallbackList<void(int)>::Subscription> a_subscription =
183 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a))); 189 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a)));
184 scoped_ptr<CallbackList<void(int)>::Subscription> b_subscription = 190 scoped_ptr<CallbackList<void(int)>::Subscription> b_subscription =
185 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b))); 191 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b)));
186 192
187 EXPECT_TRUE(a_subscription.get()); 193 EXPECT_TRUE(a_subscription.get());
188 EXPECT_TRUE(b_subscription.get()); 194 EXPECT_TRUE(b_subscription.get());
189 195
190 cb_reg.Notify(10); 196 cb_reg.Notify(10);
191 197
192 EXPECT_EQ(10, a.total_); 198 EXPECT_EQ(10, a.total());
193 EXPECT_EQ(-10, b.total_); 199 EXPECT_EQ(-10, b.total());
194 200
195 b_subscription.reset(); 201 b_subscription.reset();
196 202
197 scoped_ptr<CallbackList<void(int)>::Subscription> c_subscription = 203 scoped_ptr<CallbackList<void(int)>::Subscription> c_subscription =
198 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c))); 204 cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c)));
199 205
200 cb_reg.Notify(10); 206 cb_reg.Notify(10);
201 207
202 EXPECT_EQ(20, a.total_); 208 EXPECT_EQ(20, a.total());
203 EXPECT_EQ(-10, b.total_); 209 EXPECT_EQ(-10, b.total());
204 EXPECT_EQ(10, c.total_); 210 EXPECT_EQ(10, c.total());
205 211
206 a_subscription.reset(); 212 a_subscription.reset();
207 b_subscription.reset(); 213 b_subscription.reset();
208 c_subscription.reset(); 214 c_subscription.reset();
209 } 215 }
210 216
211 // Test the a callback can remove itself or a different callback from the list 217 // Test the a callback can remove itself or a different callback from the list
212 // during iteration without invalidating the iterator. 218 // during iteration without invalidating the iterator.
213 TEST(CallbackListTest, RemoveCallbacksDuringIteration) { 219 TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
214 CallbackList<void(void)> cb_reg; 220 CallbackList<void(void)> cb_reg;
(...skipping 13 matching lines...) Expand all
228 234
229 // |remover_1| will remove itself. 235 // |remover_1| will remove itself.
230 remover_1.SetSubscriptionToRemove(remover_1_sub.Pass()); 236 remover_1.SetSubscriptionToRemove(remover_1_sub.Pass());
231 // |remover_2| will remove a. 237 // |remover_2| will remove a.
232 remover_2.SetSubscriptionToRemove(a_subscription.Pass()); 238 remover_2.SetSubscriptionToRemove(a_subscription.Pass());
233 239
234 cb_reg.Notify(); 240 cb_reg.Notify();
235 241
236 // |remover_1| runs once (and removes itself), |remover_2| runs once (and 242 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
237 // removes a), |a| never runs, and |b| runs once. 243 // removes a), |a| never runs, and |b| runs once.
238 EXPECT_EQ(1, remover_1.total_); 244 EXPECT_EQ(1, remover_1.total());
239 EXPECT_EQ(1, remover_2.total_); 245 EXPECT_EQ(1, remover_2.total());
240 EXPECT_EQ(0, a.total_); 246 EXPECT_EQ(0, a.total());
241 EXPECT_EQ(1, b.total_); 247 EXPECT_EQ(1, b.total());
242 248
243 cb_reg.Notify(); 249 cb_reg.Notify();
244 250
245 // Only |remover_2| and |b| run this time. 251 // Only |remover_2| and |b| run this time.
246 EXPECT_EQ(1, remover_1.total_); 252 EXPECT_EQ(1, remover_1.total());
247 EXPECT_EQ(2, remover_2.total_); 253 EXPECT_EQ(2, remover_2.total());
248 EXPECT_EQ(0, a.total_); 254 EXPECT_EQ(0, a.total());
249 EXPECT_EQ(2, b.total_); 255 EXPECT_EQ(2, b.total());
250 } 256 }
251 257
252 // Test that a callback can add another callback to the list durning iteration 258 // Test that a callback can add another callback to the list durning iteration
253 // without invalidating the iterator. The newly added callback should be run on 259 // without invalidating the iterator. The newly added callback should be run on
254 // the current iteration as will all other callbacks in the list. 260 // the current iteration as will all other callbacks in the list.
255 TEST(CallbackListTest, AddCallbacksDuringIteration) { 261 TEST(CallbackListTest, AddCallbacksDuringIteration) {
256 CallbackList<void(void)> cb_reg; 262 CallbackList<void(void)> cb_reg;
257 Adder a(&cb_reg); 263 Adder a(&cb_reg);
258 Listener b; 264 Listener b;
259 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription = 265 scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
260 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a))); 266 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
261 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription = 267 scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
262 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b))); 268 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
263 269
264 cb_reg.Notify(); 270 cb_reg.Notify();
265 271
266 EXPECT_EQ(1, a.total_); 272 EXPECT_EQ(1, a.total());
267 EXPECT_EQ(1, b.total_); 273 EXPECT_EQ(1, b.total());
268 EXPECT_TRUE(a.added_); 274 EXPECT_TRUE(a.added());
269 275
270 cb_reg.Notify(); 276 cb_reg.Notify();
271 277
272 EXPECT_EQ(2, a.total_); 278 EXPECT_EQ(2, a.total());
273 EXPECT_EQ(2, b.total_); 279 EXPECT_EQ(2, b.total());
274 } 280 }
275 281
276 // Sanity check: notifying an empty list is a no-op. 282 // Sanity check: notifying an empty list is a no-op.
277 TEST(CallbackListTest, EmptyList) { 283 TEST(CallbackListTest, EmptyList) {
278 CallbackList<void(void)> cb_reg; 284 CallbackList<void(void)> cb_reg;
279 285
280 cb_reg.Notify(); 286 cb_reg.Notify();
281 } 287 }
282 288
283 } // namespace 289 } // namespace
284 } // namespace base 290 } // namespace base
OLDNEW
« base/callback_list.h ('K') | « base/callback_list.h.pump ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698