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/scoped_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;
29 void Increment(int* count) { (*count)++; }
30 void IncrementBy(int* count, int n) { (*count) += n; }
31 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
33 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
38 // - Callback can be run multiple times.
39 // - After Cancel(), Run() completes but has no effect.
40 TEST(CancelableCallbackTest, Cancel) {
42 CancelableClosure cancelable(
43 base::Bind(&Increment, base::Unretained(&count)));
45 base::Closure callback = cancelable.callback();
57 // Cancel() called multiple times.
58 // - Cancel() cancels all copies of the wrapped callback.
59 // - Calling Cancel() more than once has no effect.
60 // - After Cancel(), callback() returns a null callback.
61 TEST(CancelableCallbackTest, MultipleCancel) {
63 CancelableClosure cancelable(
64 base::Bind(&Increment, base::Unretained(&count)));
66 base::Closure callback1 = cancelable.callback();
67 base::Closure callback2 = cancelable.callback();
76 // Calling Cancel() again has no effect.
79 // callback() of a cancelled callback is null.
80 base::Closure callback3 = cancelable.callback();
81 EXPECT_TRUE(callback3.is_null());
84 // CancelableCallback destroyed before callback is run.
85 // - Destruction of CancelableCallback cancels outstanding callbacks.
86 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
88 base::Closure callback;
91 CancelableClosure cancelable(
92 base::Bind(&Increment, base::Unretained(&count)));
94 callback = cancelable.callback();
103 // Cancel() called on bound closure with a RefCounted parameter.
104 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
105 TEST(CancelableCallbackTest, CancelDropsCallback) {
106 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
107 EXPECT_TRUE(ref_counted->HasOneRef());
109 CancelableClosure cancelable(base::Bind(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 CancelableClosure cancelable(
127 base::Bind(&Increment, base::Unretained(&count)));
129 base::Closure callback = cancelable.callback();
137 base::Bind(&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::Closure callback2 = cancelable.callback();
148 ASSERT_FALSE(callback2.is_null());
155 // - Cancel() transforms the CancelableCallback into a cancelled state.
156 TEST(CancelableCallbackTest, IsNull) {
157 CancelableClosure cancelable;
158 EXPECT_TRUE(cancelable.IsCancelled());
161 cancelable.Reset(base::Bind(&Increment,
162 base::Unretained(&count)));
163 EXPECT_FALSE(cancelable.IsCancelled());
166 EXPECT_TRUE(cancelable.IsCancelled());
169 // CancelableCallback posted to a task environment with PostTask.
170 // - Posted callbacks can be cancelled.
171 TEST(CancelableCallbackTest, PostTask) {
172 test::ScopedTaskEnvironment scoped_task_environment;
175 CancelableClosure cancelable(base::Bind(&Increment,
176 base::Unretained(&count)));
178 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
179 RunLoop().RunUntilIdle();
183 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
185 // Cancel before running the tasks.
187 RunLoop().RunUntilIdle();
189 // Callback never ran due to cancellation; count is the same.
193 // CancelableCallback can be used with move-only types.
194 TEST(CancelableCallbackTest, MoveOnlyType) {
195 const int kExpectedResult = 42;
198 CancelableCallback<void(std::unique_ptr<int>)> cb(
199 base::Bind(&OnMoveOnlyReceived, base::Unretained(&result)));
200 cb.callback().Run(std::make_unique<int>(kExpectedResult));
202 EXPECT_EQ(kExpectedResult, result);