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"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 class TestRefCounted
: public RefCountedThreadSafe
<TestRefCounted
> {
21 friend class RefCountedThreadSafe
<TestRefCounted
>;
25 void Increment(int* count
) { (*count
)++; }
26 void IncrementBy(int* count
, int n
) { (*count
) += n
; }
27 void RefCountedParam(const scoped_refptr
<TestRefCounted
>& ref_counted
) {}
30 // - Callback can be run multiple times.
31 // - After Cancel(), Run() completes but has no effect.
32 TEST(CancelableCallbackTest
, Cancel
) {
34 CancelableClosure
cancelable(
35 base::Bind(&Increment
, base::Unretained(&count
)));
37 base::Closure callback
= cancelable
.callback();
49 // Cancel() called multiple times.
50 // - Cancel() cancels all copies of the wrapped callback.
51 // - Calling Cancel() more than once has no effect.
52 // - After Cancel(), callback() returns a null callback.
53 TEST(CancelableCallbackTest
, MultipleCancel
) {
55 CancelableClosure
cancelable(
56 base::Bind(&Increment
, base::Unretained(&count
)));
58 base::Closure callback1
= cancelable
.callback();
59 base::Closure callback2
= cancelable
.callback();
68 // Calling Cancel() again has no effect.
71 // callback() of a cancelled callback is null.
72 base::Closure callback3
= cancelable
.callback();
73 EXPECT_TRUE(callback3
.is_null());
76 // CancelableCallback destroyed before callback is run.
77 // - Destruction of CancelableCallback cancels outstanding callbacks.
78 TEST(CancelableCallbackTest
, CallbackCanceledOnDestruction
) {
80 base::Closure callback
;
83 CancelableClosure
cancelable(
84 base::Bind(&Increment
, base::Unretained(&count
)));
86 callback
= cancelable
.callback();
95 // Cancel() called on bound closure with a RefCounted parameter.
96 // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
97 TEST(CancelableCallbackTest
, CancelDropsCallback
) {
98 scoped_refptr
<TestRefCounted
> ref_counted
= new TestRefCounted
;
99 EXPECT_TRUE(ref_counted
->HasOneRef());
101 CancelableClosure
cancelable(base::Bind(RefCountedParam
, ref_counted
));
102 EXPECT_FALSE(cancelable
.IsCancelled());
103 EXPECT_TRUE(ref_counted
.get());
104 EXPECT_FALSE(ref_counted
->HasOneRef());
106 // There is only one reference to |ref_counted| after the Cancel().
108 EXPECT_TRUE(cancelable
.IsCancelled());
109 EXPECT_TRUE(ref_counted
.get());
110 EXPECT_TRUE(ref_counted
->HasOneRef());
114 // - Reset() replaces the existing wrapped callback with a new callback.
115 // - Reset() deactivates outstanding callbacks.
116 TEST(CancelableCallbackTest
, Reset
) {
118 CancelableClosure
cancelable(
119 base::Bind(&Increment
, base::Unretained(&count
)));
121 base::Closure callback
= cancelable
.callback();
129 base::Bind(&IncrementBy
, base::Unretained(&count
), 3));
130 EXPECT_FALSE(cancelable
.IsCancelled());
132 // The stale copy of the cancelable callback is non-null.
133 ASSERT_FALSE(callback
.is_null());
135 // The stale copy of the cancelable callback is no longer active.
139 base::Closure callback2
= cancelable
.callback();
140 ASSERT_FALSE(callback2
.is_null());
147 // - Cancel() transforms the CancelableCallback into a cancelled state.
148 TEST(CancelableCallbackTest
, IsNull
) {
149 CancelableClosure cancelable
;
150 EXPECT_TRUE(cancelable
.IsCancelled());
153 cancelable
.Reset(base::Bind(&Increment
,
154 base::Unretained(&count
)));
155 EXPECT_FALSE(cancelable
.IsCancelled());
158 EXPECT_TRUE(cancelable
.IsCancelled());
161 // CancelableCallback posted to a MessageLoop with PostTask.
162 // - Callbacks posted to a MessageLoop can be cancelled.
163 TEST(CancelableCallbackTest
, PostTask
) {
167 CancelableClosure
cancelable(base::Bind(&Increment
,
168 base::Unretained(&count
)));
170 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, cancelable
.callback());
171 RunLoop().RunUntilIdle();
175 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, cancelable
.callback());
177 // Cancel before running the message loop.
179 RunLoop().RunUntilIdle();
181 // Callback never ran due to cancellation; count is the same.