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 <memory>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace base {
21 namespace {
22
23 class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
24 private:
25 friend class RefCountedThreadSafe<TestRefCounted>;
26 ~TestRefCounted() = default;
27 ;
28 };
29
Increment(int * count)30 void Increment(int* count) { (*count)++; }
IncrementBy(int * count,int n)31 void IncrementBy(int* count, int n) { (*count) += n; }
RefCountedParam(const scoped_refptr<TestRefCounted> & ref_counted)32 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
33
OnMoveOnlyReceived(int * value,std::unique_ptr<int> result)34 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
35 *value = *result;
36 }
37
38 // Cancel().
39 // - Callback can be run multiple times.
40 // - After Cancel(), Run() completes but has no effect.
TEST(CancelableCallbackTest,Cancel)41 TEST(CancelableCallbackTest, Cancel) {
42 int count = 0;
43 CancelableClosure cancelable(
44 base::Bind(&Increment, base::Unretained(&count)));
45
46 base::Closure callback = cancelable.callback();
47 callback.Run();
48 EXPECT_EQ(1, count);
49
50 callback.Run();
51 EXPECT_EQ(2, count);
52
53 cancelable.Cancel();
54 callback.Run();
55 EXPECT_EQ(2, count);
56 }
57
58 // Cancel() called multiple times.
59 // - Cancel() cancels all copies of the wrapped callback.
60 // - Calling Cancel() more than once has no effect.
61 // - After Cancel(), callback() returns a null callback.
TEST(CancelableCallbackTest,MultipleCancel)62 TEST(CancelableCallbackTest, MultipleCancel) {
63 int count = 0;
64 CancelableClosure cancelable(
65 base::Bind(&Increment, base::Unretained(&count)));
66
67 base::Closure callback1 = cancelable.callback();
68 base::Closure callback2 = cancelable.callback();
69 cancelable.Cancel();
70
71 callback1.Run();
72 EXPECT_EQ(0, count);
73
74 callback2.Run();
75 EXPECT_EQ(0, count);
76
77 // Calling Cancel() again has no effect.
78 cancelable.Cancel();
79
80 // callback() of a cancelled callback is null.
81 base::Closure callback3 = cancelable.callback();
82 EXPECT_TRUE(callback3.is_null());
83 }
84
85 // CancelableCallback destroyed before callback is run.
86 // - Destruction of CancelableCallback cancels outstanding callbacks.
TEST(CancelableCallbackTest,CallbackCanceledOnDestruction)87 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
88 int count = 0;
89 base::Closure callback;
90
91 {
92 CancelableClosure cancelable(
93 base::Bind(&Increment, base::Unretained(&count)));
94
95 callback = cancelable.callback();
96 callback.Run();
97 EXPECT_EQ(1, count);
98 }
99
100 callback.Run();
101 EXPECT_EQ(1, count);
102 }
103
104 // Cancel() called on bound closure with a RefCounted parameter.
105 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
TEST(CancelableCallbackTest,CancelDropsCallback)106 TEST(CancelableCallbackTest, CancelDropsCallback) {
107 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
108 EXPECT_TRUE(ref_counted->HasOneRef());
109
110 CancelableClosure cancelable(base::Bind(RefCountedParam, ref_counted));
111 EXPECT_FALSE(cancelable.IsCancelled());
112 EXPECT_TRUE(ref_counted.get());
113 EXPECT_FALSE(ref_counted->HasOneRef());
114
115 // There is only one reference to |ref_counted| after the Cancel().
116 cancelable.Cancel();
117 EXPECT_TRUE(cancelable.IsCancelled());
118 EXPECT_TRUE(ref_counted.get());
119 EXPECT_TRUE(ref_counted->HasOneRef());
120 }
121
122 // Reset().
123 // - Reset() replaces the existing wrapped callback with a new callback.
124 // - Reset() deactivates outstanding callbacks.
TEST(CancelableCallbackTest,Reset)125 TEST(CancelableCallbackTest, Reset) {
126 int count = 0;
127 CancelableClosure cancelable(
128 base::Bind(&Increment, base::Unretained(&count)));
129
130 base::Closure callback = cancelable.callback();
131 callback.Run();
132 EXPECT_EQ(1, count);
133
134 callback.Run();
135 EXPECT_EQ(2, count);
136
137 cancelable.Reset(
138 base::Bind(&IncrementBy, base::Unretained(&count), 3));
139 EXPECT_FALSE(cancelable.IsCancelled());
140
141 // The stale copy of the cancelable callback is non-null.
142 ASSERT_FALSE(callback.is_null());
143
144 // The stale copy of the cancelable callback is no longer active.
145 callback.Run();
146 EXPECT_EQ(2, count);
147
148 base::Closure callback2 = cancelable.callback();
149 ASSERT_FALSE(callback2.is_null());
150
151 callback2.Run();
152 EXPECT_EQ(5, count);
153 }
154
155 // IsCanceled().
156 // - Cancel() transforms the CancelableCallback into a cancelled state.
TEST(CancelableCallbackTest,IsNull)157 TEST(CancelableCallbackTest, IsNull) {
158 CancelableClosure cancelable;
159 EXPECT_TRUE(cancelable.IsCancelled());
160
161 int count = 0;
162 cancelable.Reset(base::Bind(&Increment,
163 base::Unretained(&count)));
164 EXPECT_FALSE(cancelable.IsCancelled());
165
166 cancelable.Cancel();
167 EXPECT_TRUE(cancelable.IsCancelled());
168 }
169
170 // CancelableCallback posted to a MessageLoop with PostTask.
171 // - Callbacks posted to a MessageLoop can be cancelled.
TEST(CancelableCallbackTest,PostTask)172 TEST(CancelableCallbackTest, PostTask) {
173 MessageLoop loop;
174
175 int count = 0;
176 CancelableClosure cancelable(base::Bind(&Increment,
177 base::Unretained(&count)));
178
179 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
180 RunLoop().RunUntilIdle();
181
182 EXPECT_EQ(1, count);
183
184 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
185
186 // Cancel before running the message loop.
187 cancelable.Cancel();
188 RunLoop().RunUntilIdle();
189
190 // Callback never ran due to cancellation; count is the same.
191 EXPECT_EQ(1, count);
192 }
193
194 // CancelableCallback can be used with move-only types.
TEST(CancelableCallbackTest,MoveOnlyType)195 TEST(CancelableCallbackTest, MoveOnlyType) {
196 const int kExpectedResult = 42;
197
198 int result = 0;
199 CancelableCallback<void(std::unique_ptr<int>)> cb(
200 base::Bind(&OnMoveOnlyReceived, base::Unretained(&result)));
201 cb.callback().Run(base::WrapUnique(new int(kExpectedResult)));
202
203 EXPECT_EQ(kExpectedResult, result);
204 }
205
206 } // namespace
207 } // namespace base
208