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.
5 #include "base/cancelable_callback.h"
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"
21 class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
23 friend class RefCountedThreadSafe<TestRefCounted>;
24 ~TestRefCounted() = default;
27 void Increment(int* count) { (*count)++; }
28 void IncrementBy(int* count, int n) { (*count) += n; }
29 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
31 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
36 // - Callback can be run multiple times.
37 // - After Cancel(), Run() completes but has no effect.
38 TEST(CancelableCallbackTest, Cancel) {
40 CancelableRepeatingClosure cancelable(
41 base::BindRepeating(&Increment, base::Unretained(&count)));
43 base::RepeatingClosure callback = cancelable.callback();
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.
59 TEST(CancelableCallbackTest, MultipleCancel) {
61 CancelableRepeatingClosure cancelable(
62 base::BindRepeating(&Increment, base::Unretained(&count)));
64 base::RepeatingClosure callback1 = cancelable.callback();
65 base::RepeatingClosure callback2 = cancelable.callback();
74 // Calling Cancel() again has no effect.
77 // callback() of a cancelled callback is null.
78 base::RepeatingClosure callback3 = cancelable.callback();
79 EXPECT_TRUE(callback3.is_null());
82 // CancelableRepeatingCallback destroyed before callback is run.
83 // - Destruction of CancelableRepeatingCallback cancels outstanding callbacks.
84 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
86 base::RepeatingClosure callback;
89 CancelableRepeatingClosure cancelable(
90 base::BindRepeating(&Increment, base::Unretained(&count)));
92 callback = cancelable.callback();
101 // Cancel() called on bound closure with a RefCounted parameter.
102 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
103 TEST(CancelableCallbackTest, CancelDropsCallback) {
104 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
105 EXPECT_TRUE(ref_counted->HasOneRef());
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());
113 // There is only one reference to |ref_counted| after the Cancel().
115 EXPECT_TRUE(cancelable.IsCancelled());
116 EXPECT_TRUE(ref_counted.get());
117 EXPECT_TRUE(ref_counted->HasOneRef());
121 // - Reset() replaces the existing wrapped callback with a new callback.
122 // - Reset() deactivates outstanding callbacks.
123 TEST(CancelableCallbackTest, Reset) {
125 CancelableRepeatingClosure cancelable(
126 base::BindRepeating(&Increment, base::Unretained(&count)));
128 base::RepeatingClosure callback = cancelable.callback();
136 base::BindRepeating(&IncrementBy, base::Unretained(&count), 3));
137 EXPECT_FALSE(cancelable.IsCancelled());
139 // The stale copy of the cancelable callback is non-null.
140 ASSERT_FALSE(callback.is_null());
142 // The stale copy of the cancelable callback is no longer active.
146 base::RepeatingClosure callback2 = cancelable.callback();
147 ASSERT_FALSE(callback2.is_null());
154 // - Cancel() transforms the CancelableOnceCallback into a cancelled state.
155 TEST(CancelableCallbackTest, IsNull) {
156 CancelableOnceClosure cancelable;
157 EXPECT_TRUE(cancelable.IsCancelled());
160 cancelable.Reset(base::BindOnce(&Increment, base::Unretained(&count)));
161 EXPECT_FALSE(cancelable.IsCancelled());
164 EXPECT_TRUE(cancelable.IsCancelled());
167 // CancelableRepeatingCallback posted to a task environment with PostTask.
168 // - Posted callbacks can be cancelled.
169 TEST(CancelableCallbackTest, PostTask) {
170 test::TaskEnvironment task_environment;
173 CancelableRepeatingClosure cancelable(
174 base::BindRepeating(&Increment, base::Unretained(&count)));
176 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
177 cancelable.callback());
178 RunLoop().RunUntilIdle();
182 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
183 cancelable.callback());
185 // Cancel before running the tasks.
187 RunLoop().RunUntilIdle();
189 // Callback never ran due to cancellation; count is the same.
193 // CancelableRepeatingCallback can be used with move-only types.
194 TEST(CancelableCallbackTest, MoveOnlyType) {
195 const int kExpectedResult = 42;
198 CancelableRepeatingCallback<void(std::unique_ptr<int>)> cb(
199 base::BindRepeating(&OnMoveOnlyReceived, base::Unretained(&result)));
200 cb.callback().Run(std::make_unique<int>(kExpectedResult));
202 EXPECT_EQ(kExpectedResult, result);