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

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

Powered by Google App Engine
This is Rietveld 408576698