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

Side by Side Diff: base/cancelable_callback_unittest.cc

Issue 8673008: base::Bind: Implement CancelableCallback to replace CancelableTaske. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test build fix. Created 9 years, 1 month 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
« no previous file with comments | « base/cancelable_callback.cc ('k') | webkit/appcache/appcache_group.h » ('j') | 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 (c) 2011 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/cancelable_callback.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base {
14 namespace {
15
16 class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
17 private:
18 friend class RefCountedThreadSafe<TestRefCounted>;
19 ~TestRefCounted() {};
20 };
21
22 void Increment(int* count) { (*count)++; }
23 void IncrementBy(int* count, int n) { (*count) += n; }
24 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
25
26 // Cancel().
27 // - Callback can be run multiple times.
28 // - After Cancel(), Run() completes but has no effect.
29 TEST(CancelableCallbackTest, Cancel) {
30 int count = 0;
31 CancelableCallback cancelable(
32 base::Bind(&Increment, base::Unretained(&count)));
33
34 base::Closure callback = cancelable.callback();
35 callback.Run();
36 EXPECT_EQ(1, count);
37
38 callback.Run();
39 EXPECT_EQ(2, count);
40
41 cancelable.Cancel();
42 callback.Run();
43 EXPECT_EQ(2, count);
44 }
45
46 // Cancel() called multiple times.
47 // - Cancel() cancels all copies of the wrapped callback.
48 TEST(CancelableCallbackTest, MultipleCancel) {
49 int count = 0;
50 CancelableCallback cancelable(
51 base::Bind(&Increment, base::Unretained(&count)));
52
53 base::Closure callback1 = cancelable.callback();
54 cancelable.Cancel();
55
56 callback1.Run();
57 EXPECT_EQ(0, count);
58
59 base::Closure callback2 = cancelable.callback();
60 callback2.Run();
61 EXPECT_EQ(0, count);
62
63 // Cancel() should have no effect, but callback() is still runnable.
64 base::Closure callback3 = cancelable.callback();
65 cancelable.Cancel();
66 callback3.Run();
67 EXPECT_EQ(0, count);
68 }
69
70 // CancelableCallback destroyed before callback is run.
71 // - Destruction of CancelableCallback cancels outstanding callbacks.
72 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
73 int count = 0;
74 base::Closure callback;
75
76 {
77 CancelableCallback cancelable(
78 base::Bind(&Increment, base::Unretained(&count)));
79
80 callback = cancelable.callback();
81 callback.Run();
82 EXPECT_EQ(1, count);
83 }
84
85 callback.Run();
86 EXPECT_EQ(1, count);
87 }
88
89 // Cancel() called on bound closure with a RefCounted parameter.
90 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
91 TEST(CancelableCallbackTest, CancelDropsCallback) {
92 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
93 EXPECT_TRUE(ref_counted->HasOneRef());
94
95 CancelableCallback cancelable(base::Bind(RefCountedParam, ref_counted));
96 EXPECT_FALSE(cancelable.IsCancelled());
97 EXPECT_TRUE(ref_counted.get());
98 EXPECT_FALSE(ref_counted->HasOneRef());
99
100 // There is only one reference to |ref_counted| after the Cancel().
101 cancelable.Cancel();
102 EXPECT_TRUE(cancelable.IsCancelled());
103 EXPECT_TRUE(ref_counted.get());
104 EXPECT_TRUE(ref_counted->HasOneRef());
105 }
106
107 // Reset().
108 // - Reset() replaces the existing wrapped callback with a new callback.
109 // - Reset() deactivates outstanding callbacks.
110 TEST(CancelableCallbackTest, Reset) {
111 int count = 0;
112 CancelableCallback cancelable(
113 base::Bind(&Increment, base::Unretained(&count)));
114
115 base::Closure callback = cancelable.callback();
116 callback.Run();
117 EXPECT_EQ(1, count);
118
119 callback.Run();
120 EXPECT_EQ(2, count);
121
122 cancelable.Reset(
123 base::Bind(&IncrementBy, base::Unretained(&count), 3));
124 EXPECT_FALSE(cancelable.IsCancelled());
125
126 // The stale copy of the cancelable callback is non-null.
127 ASSERT_FALSE(callback.is_null());
128
129 // The stale copy of the cancelable callback is no longer active.
130 callback.Run();
131 EXPECT_EQ(2, count);
132
133 base::Closure callback2 = cancelable.callback();
134 ASSERT_FALSE(callback2.is_null());
135
136 callback2.Run();
137 EXPECT_EQ(5, count);
138 }
139
140 // IsCanceled().
141 // - Cancel() transforms the CancelableCallback into a cancelled state.
142 TEST(CancelableCallbackTest, IsNull) {
143 CancelableCallback cancelable;
144 EXPECT_TRUE(cancelable.IsCancelled());
145
146 int count = 0;
147 cancelable.Reset(base::Bind(&Increment,
148 base::Unretained(&count)));
149 EXPECT_FALSE(cancelable.IsCancelled());
150
151 cancelable.Cancel();
152 EXPECT_TRUE(cancelable.IsCancelled());
153 }
154
155 // CancelableCallback posted to a MessageLoop with PostTask.
156 // - Callbacks posted to a MessageLoop can be cancelled.
157 TEST(CancelableCallbackTest, PostTask) {
158 MessageLoop loop(MessageLoop::TYPE_DEFAULT);
159
160 int count = 0;
161 CancelableCallback cancelable(base::Bind(&Increment,
162 base::Unretained(&count)));
163
164 MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback());
165 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
166 MessageLoop::current()->Run();
167
168 EXPECT_EQ(1, count);
169
170 MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback());
171 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
172
173 // Cancel before running the message loop.
174 cancelable.Cancel();
175 MessageLoop::current()->Run();
176
177 // Callback never ran due to cancellation; count is the same.
178 EXPECT_EQ(1, count);
179 }
180
181 } // namespace
182 } // namespace base
OLDNEW
« no previous file with comments | « base/cancelable_callback.cc ('k') | webkit/appcache/appcache_group.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698