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