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

Side by Side Diff: base/callback_registry_unittest.cc

Issue 22877038: Add a CallbackRegistry class to base/ to manage callbacks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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_registry.h ('K') | « base/callback_registry.h ('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
(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"
erikwright (departed) 2013/09/09 20:08:14 I think you meant to include basictypes.h for DISA
Cait (Slow) 2013/09/09 20:33:03 Done.
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
erikwright (departed) 2013/09/09 20:08:14 no longer needed?
Cait (Slow) 2013/09/09 20:33:03 Done.
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_subscription_.reset();
36 }
37 void SetSubscriptionToRemove(
38 scoped_ptr<CallbackRegistry<void>::Subscription> sub) {
39 removal_subscription_ = sub.Pass();
40 }
41
42 int total_;
erikwright (departed) 2013/09/09 20:08:14 blank line after.
Cait (Slow) 2013/09/09 20:33:03 Done.
43 private:
44 scoped_ptr<CallbackRegistry<void>::Subscription> removal_subscription_;
45 DISALLOW_COPY_AND_ASSIGN(Remover);
46 };
47
48 class Adder {
49 public:
50 explicit Adder(CallbackRegistry<void>* cb_reg)
51 : added_(false),
52 total_(0),
53 cb_reg_(cb_reg) {}
54 void AddCallback() {
55 if (!added_) {
56 added_ = true;
57 subscription_ =
58 cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this)));
59 }
60 }
61 void IncrementTotal() { total_++; }
62
63 bool added_;
64 int total_;
65 private:
66 CallbackRegistry<void>* cb_reg_;
67 scoped_ptr<CallbackRegistry<void>::Subscription> subscription_;
68 DISALLOW_COPY_AND_ASSIGN(Adder);
69 };
70
71 // Sanity check that closures added to the list will be run, and those removed
72 // from the list will not be run.
73 TEST(CallbackRegistryTest, BasicTest) {
74 CallbackRegistry<void> cb_reg;
75 Listener a, b, c;
76
77 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription =
78 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
79 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription =
80 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
81
82 EXPECT_TRUE(a_subscription.get());
83 EXPECT_TRUE(b_subscription.get());
84
85 cb_reg.Notify();
86
87 EXPECT_EQ(1, a.total_);
88 EXPECT_EQ(1, b.total_);
89
90 b_subscription.reset();
91
92 scoped_ptr<CallbackRegistry<void>::Subscription> c_subscription =
93 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
94
95 cb_reg.Notify();
96
97 EXPECT_EQ(2, a.total_);
98 EXPECT_EQ(1, b.total_);
99 EXPECT_EQ(1, c.total_);
100
101 a_subscription.reset();
102 b_subscription.reset();
103 c_subscription.reset();
104 }
105
106 // Sanity check that callbacks with details added to the list will be run, with
107 // the correct details, and those removed from the list will not be run.
108 TEST(CallbackRegistryTest, BasicTestWithParams) {
109 CallbackRegistry<int> cb_reg;
110 Listener a(1), b(-1), c(1);
111
112 scoped_ptr<CallbackRegistry<int>::Subscription> a_subscription =
113 cb_reg.Add(Bind(&Listener::MultiplyTotal, Unretained(&a)));
114 scoped_ptr<CallbackRegistry<int>::Subscription> b_subscription =
115 cb_reg.Add(Bind(&Listener::MultiplyTotal, Unretained(&b)));
116
117 EXPECT_TRUE(a_subscription.get());
118 EXPECT_TRUE(b_subscription.get());
119
120 cb_reg.Notify(10);
121
122 EXPECT_EQ(10, a.total_);
123 EXPECT_EQ(-10, b.total_);
124
125 b_subscription.reset();
126
127 scoped_ptr<CallbackRegistry<int>::Subscription> c_subscription =
128 cb_reg.Add(Bind(&Listener::MultiplyTotal, Unretained(&c)));
129
130 cb_reg.Notify(10);
131
132 EXPECT_EQ(20, a.total_);
erikwright (departed) 2013/09/09 20:08:14 nit: I expected this to be 100 (i.e., I thought by
Cait (Slow) 2013/09/09 20:33:03 IncrementByMultipleOfScaler?
133 EXPECT_EQ(-10, b.total_);
134 EXPECT_EQ(10, c.total_);
135
136 a_subscription.reset();
137 b_subscription.reset();
138 c_subscription.reset();
139 }
140
141 // Test the a callback can remove itself or a different callback from the list
142 // during iteration without invalidating the iterator.
143 TEST(CallbackRegistryTest, RemoveCallbacksDuringIteration) {
144 CallbackRegistry<void> cb_reg;
145 Listener a, b;
146 Remover remover_1, remover_2;
147
148 scoped_ptr<CallbackRegistry<void>::Subscription> remover_1_subscription =
149 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
150 Unretained(&remover_1)));
151 scoped_ptr<CallbackRegistry<void>::Subscription> remover_2_subscription =
152 cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
153 Unretained(&remover_2)));
154 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription =
155 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
156 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription =
157 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
158
159 // |remover_1| will remove itself.
160 remover_1.SetSubscriptionToRemove(remover_1_subscription.Pass());
161 // |remover_2| will remove a.
162 remover_2.SetSubscriptionToRemove(a_subscription.Pass());
163
164 cb_reg.Notify();
165
166 // |remover_1| runs once (and removes itself), |remover_2| runs once (and
167 // removes a), |a| never runs, and |b| runs once.
168 EXPECT_EQ(1, remover_1.total_);
169 EXPECT_EQ(1, remover_2.total_);
170 EXPECT_EQ(0, a.total_);
171 EXPECT_EQ(1, b.total_);
172
173 cb_reg.Notify();
174
175 // Only |remover_2| and |b| run this time.
176 EXPECT_EQ(1, remover_1.total_);
177 EXPECT_EQ(2, remover_2.total_);
178 EXPECT_EQ(0, a.total_);
179 EXPECT_EQ(2, b.total_);
180 }
181
182 // Test that a callback can add another callback to the list durning iteration
183 // without invalidating the iterator.
184 // - If the CallbackNotificationType is |CALLBACKS_NOTIFY_ALL|, the newly added
erikwright (departed) 2013/09/09 20:08:14 comment no longer relevant
Cait (Slow) 2013/09/09 20:33:03 Done.
185 // callback will be run on the current iteration.
186 // - All other callbacks in the list will be run as well.
187 TEST(CallbackRegistryTest, AddCallbacksDuringIteration) {
188 CallbackRegistry<void> cb_reg;
189 Adder a(&cb_reg);
190 Listener b;
191 scoped_ptr<CallbackRegistry<void>::Subscription> a_subscription =
192 cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
193 scoped_ptr<CallbackRegistry<void>::Subscription> b_subscription =
194 cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
195
196 cb_reg.Notify();
197
198 EXPECT_EQ(1, a.total_);
199 EXPECT_EQ(1, b.total_);
200 EXPECT_TRUE(a.added_);
201
202 cb_reg.Notify();
203
204 EXPECT_EQ(2, a.total_);
205 EXPECT_EQ(2, b.total_);
206 }
207
208 } // namespace
209 } // namespace base
erikwright (departed) 2013/09/09 20:08:14 Add test for Empty callback list.
Cait (Slow) 2013/09/09 20:33:03 Done.
OLDNEW
« base/callback_registry.h ('K') | « base/callback_registry.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698