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.
5 #include "base/cancelable_callback.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/test/task_environment.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
24 friend class RefCountedThreadSafe<TestRefCounted>;
25 ~TestRefCounted() = default;
28 void Increment(int* count) { (*count)++; }
29 void IncrementBy(int* count, int n) { (*count) += n; }
30 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
32 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
37 // - Callback can be run multiple times.
38 // - After Cancel(), Run() completes but has no effect.
39 TEST(CancelableCallbackTest, Cancel) {
41 CancelableRepeatingClosure cancelable(
42 base::BindRepeating(&Increment, base::Unretained(&count)));
44 base::RepeatingClosure callback = cancelable.callback();
56 // Cancel() called multiple times.
57 // - Cancel() cancels all copies of the wrapped callback.
58 // - Calling Cancel() more than once has no effect.
59 // - After Cancel(), callback() returns a null callback.
60 TEST(CancelableCallbackTest, MultipleCancel) {
62 CancelableRepeatingClosure cancelable(
63 base::BindRepeating(&Increment, base::Unretained(&count)));
65 base::RepeatingClosure callback1 = cancelable.callback();
66 base::RepeatingClosure callback2 = cancelable.callback();
75 // Calling Cancel() again has no effect.
78 // callback() of a cancelled callback is null.
79 base::RepeatingClosure callback3 = cancelable.callback();
80 EXPECT_TRUE(callback3.is_null());
83 // CancelableCallback destroyed before callback is run.
84 // - Destruction of CancelableCallback cancels outstanding callbacks.
85 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
87 base::RepeatingClosure callback;
90 CancelableRepeatingClosure cancelable(
91 base::BindRepeating(&Increment, base::Unretained(&count)));
93 callback = cancelable.callback();
102 // Cancel() called on bound closure with a RefCounted parameter.
103 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
104 TEST(CancelableCallbackTest, CancelDropsCallback) {
105 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
106 EXPECT_TRUE(ref_counted->HasOneRef());
108 CancelableOnceClosure cancelable(
109 base::BindOnce(RefCountedParam, ref_counted));
110 EXPECT_FALSE(cancelable.IsCancelled());
111 EXPECT_TRUE(ref_counted.get());
112 EXPECT_FALSE(ref_counted->HasOneRef());
114 // There is only one reference to |ref_counted| after the Cancel().
116 EXPECT_TRUE(cancelable.IsCancelled());
117 EXPECT_TRUE(ref_counted.get());
118 EXPECT_TRUE(ref_counted->HasOneRef());
122 // - Reset() replaces the existing wrapped callback with a new callback.
123 // - Reset() deactivates outstanding callbacks.
124 TEST(CancelableCallbackTest, Reset) {
126 CancelableRepeatingClosure cancelable(
127 base::BindRepeating(&Increment, base::Unretained(&count)));
129 base::RepeatingClosure callback = cancelable.callback();
137 base::BindRepeating(&IncrementBy, base::Unretained(&count), 3));
138 EXPECT_FALSE(cancelable.IsCancelled());
140 // The stale copy of the cancelable callback is non-null.
141 ASSERT_FALSE(callback.is_null());
143 // The stale copy of the cancelable callback is no longer active.
147 base::RepeatingClosure callback2 = cancelable.callback();
148 ASSERT_FALSE(callback2.is_null());
155 // - Cancel() transforms the CancelableCallback into a cancelled state.
156 TEST(CancelableCallbackTest, IsNull) {
157 CancelableOnceClosure cancelable;
158 EXPECT_TRUE(cancelable.IsCancelled());
161 cancelable.Reset(base::BindOnce(&Increment, base::Unretained(&count)));
162 EXPECT_FALSE(cancelable.IsCancelled());
165 EXPECT_TRUE(cancelable.IsCancelled());
168 // CancelableCallback posted to a task environment with PostTask.
169 // - Posted callbacks can be cancelled.
170 TEST(CancelableCallbackTest, PostTask) {
171 test::TaskEnvironment task_environment;
174 CancelableRepeatingClosure cancelable(
175 base::BindRepeating(&Increment, base::Unretained(&count)));
177 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
178 RunLoop().RunUntilIdle();
182 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
184 // Cancel before running the tasks.
186 RunLoop().RunUntilIdle();
188 // Callback never ran due to cancellation; count is the same.
192 // CancelableCallback can be used with move-only types.
193 TEST(CancelableCallbackTest, MoveOnlyType) {
194 const int kExpectedResult = 42;
197 CancelableRepeatingCallback<void(std::unique_ptr<int>)> cb(
198 base::BindRepeating(&OnMoveOnlyReceived, base::Unretained(&result)));
199 cb.callback().Run(std::make_unique<int>(kExpectedResult));
201 EXPECT_EQ(kExpectedResult, result);