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/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"
23 class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
25 friend class RefCountedThreadSafe<TestRefCounted>;
26 ~TestRefCounted() = default;
30 void Increment(int* count) { (*count)++; }
31 void IncrementBy(int* count, int n) { (*count) += n; }
32 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
34 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
39 // - Callback can be run multiple times.
40 // - After Cancel(), Run() completes but has no effect.
41 TEST(CancelableCallbackTest, Cancel) {
43 CancelableClosure cancelable(
44 base::Bind(&Increment, base::Unretained(&count)));
46 base::Closure callback = cancelable.callback();
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.
62 TEST(CancelableCallbackTest, MultipleCancel) {
64 CancelableClosure cancelable(
65 base::Bind(&Increment, base::Unretained(&count)));
67 base::Closure callback1 = cancelable.callback();
68 base::Closure callback2 = cancelable.callback();
77 // Calling Cancel() again has no effect.
80 // callback() of a cancelled callback is null.
81 base::Closure callback3 = cancelable.callback();
82 EXPECT_TRUE(callback3.is_null());
85 // CancelableCallback destroyed before callback is run.
86 // - Destruction of CancelableCallback cancels outstanding callbacks.
87 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
89 base::Closure callback;
92 CancelableClosure cancelable(
93 base::Bind(&Increment, base::Unretained(&count)));
95 callback = cancelable.callback();
104 // Cancel() called on bound closure with a RefCounted parameter.
105 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
106 TEST(CancelableCallbackTest, CancelDropsCallback) {
107 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
108 EXPECT_TRUE(ref_counted->HasOneRef());
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());
115 // There is only one reference to |ref_counted| after the Cancel().
117 EXPECT_TRUE(cancelable.IsCancelled());
118 EXPECT_TRUE(ref_counted.get());
119 EXPECT_TRUE(ref_counted->HasOneRef());
123 // - Reset() replaces the existing wrapped callback with a new callback.
124 // - Reset() deactivates outstanding callbacks.
125 TEST(CancelableCallbackTest, Reset) {
127 CancelableClosure cancelable(
128 base::Bind(&Increment, base::Unretained(&count)));
130 base::Closure callback = cancelable.callback();
138 base::Bind(&IncrementBy, base::Unretained(&count), 3));
139 EXPECT_FALSE(cancelable.IsCancelled());
141 // The stale copy of the cancelable callback is non-null.
142 ASSERT_FALSE(callback.is_null());
144 // The stale copy of the cancelable callback is no longer active.
148 base::Closure callback2 = cancelable.callback();
149 ASSERT_FALSE(callback2.is_null());
156 // - Cancel() transforms the CancelableCallback into a cancelled state.
157 TEST(CancelableCallbackTest, IsNull) {
158 CancelableClosure cancelable;
159 EXPECT_TRUE(cancelable.IsCancelled());
162 cancelable.Reset(base::Bind(&Increment,
163 base::Unretained(&count)));
164 EXPECT_FALSE(cancelable.IsCancelled());
167 EXPECT_TRUE(cancelable.IsCancelled());
170 // CancelableCallback posted to a MessageLoop with PostTask.
171 // - Callbacks posted to a MessageLoop can be cancelled.
172 TEST(CancelableCallbackTest, PostTask) {
176 CancelableClosure cancelable(base::Bind(&Increment,
177 base::Unretained(&count)));
179 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
180 RunLoop().RunUntilIdle();
184 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
186 // Cancel before running the message loop.
188 RunLoop().RunUntilIdle();
190 // Callback never ran due to cancellation; count is the same.
194 // CancelableCallback can be used with move-only types.
195 TEST(CancelableCallbackTest, MoveOnlyType) {
196 const int kExpectedResult = 42;
199 CancelableCallback<void(std::unique_ptr<int>)> cb(
200 base::Bind(&OnMoveOnlyReceived, base::Unretained(&result)));
201 cb.callback().Run(base::WrapUnique(new int(kExpectedResult)));
203 EXPECT_EQ(kExpectedResult, result);