blob: 42e753f85628671f93f618e8eedd8e5a44a9854a [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001// 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.
4
5#include "base/cancelable_callback.h"
6
Andrew Top0d1858f2019-05-15 22:01:47 -07007#include <memory>
8
David Ghandehari9e5b5872016-07-28 09:50:04 -07009#include "base/bind.h"
10#include "base/bind_helpers.h"
Andrew Top0d1858f2019-05-15 22:01:47 -070011#include "base/location.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070012#include "base/memory/ref_counted.h"
Andrew Top0d1858f2019-05-15 22:01:47 -070013#include "base/message_loop/message_loop.h"
14#include "base/run_loop.h"
15#include "base/single_thread_task_runner.h"
16#include "base/threading/thread_task_runner_handle.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070017#include "testing/gtest/include/gtest/gtest.h"
18
19namespace base {
20namespace {
21
22class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
23 private:
24 friend class RefCountedThreadSafe<TestRefCounted>;
Andrew Top0d1858f2019-05-15 22:01:47 -070025 ~TestRefCounted() = default;
26 ;
David Ghandehari9e5b5872016-07-28 09:50:04 -070027};
28
29void Increment(int* count) { (*count)++; }
30void IncrementBy(int* count, int n) { (*count) += n; }
31void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
32
Andrew Top0d1858f2019-05-15 22:01:47 -070033void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
34 *value = *result;
35}
36
David Ghandehari9e5b5872016-07-28 09:50:04 -070037// Cancel().
38// - Callback can be run multiple times.
39// - After Cancel(), Run() completes but has no effect.
40TEST(CancelableCallbackTest, Cancel) {
41 int count = 0;
42 CancelableClosure cancelable(
43 base::Bind(&Increment, base::Unretained(&count)));
44
45 base::Closure callback = cancelable.callback();
46 callback.Run();
47 EXPECT_EQ(1, count);
48
49 callback.Run();
50 EXPECT_EQ(2, count);
51
52 cancelable.Cancel();
53 callback.Run();
54 EXPECT_EQ(2, count);
55}
56
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.
61TEST(CancelableCallbackTest, MultipleCancel) {
62 int count = 0;
63 CancelableClosure cancelable(
64 base::Bind(&Increment, base::Unretained(&count)));
65
66 base::Closure callback1 = cancelable.callback();
67 base::Closure callback2 = cancelable.callback();
68 cancelable.Cancel();
69
70 callback1.Run();
71 EXPECT_EQ(0, count);
72
73 callback2.Run();
74 EXPECT_EQ(0, count);
75
76 // Calling Cancel() again has no effect.
77 cancelable.Cancel();
78
79 // callback() of a cancelled callback is null.
80 base::Closure callback3 = cancelable.callback();
81 EXPECT_TRUE(callback3.is_null());
82}
83
84// CancelableCallback destroyed before callback is run.
85// - Destruction of CancelableCallback cancels outstanding callbacks.
86TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
87 int count = 0;
88 base::Closure callback;
89
90 {
91 CancelableClosure cancelable(
92 base::Bind(&Increment, base::Unretained(&count)));
93
94 callback = cancelable.callback();
95 callback.Run();
96 EXPECT_EQ(1, count);
97 }
98
99 callback.Run();
100 EXPECT_EQ(1, count);
101}
102
103// Cancel() called on bound closure with a RefCounted parameter.
104// - Cancel drops wrapped callback (and, implicitly, its bound arguments).
105TEST(CancelableCallbackTest, CancelDropsCallback) {
106 scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
107 EXPECT_TRUE(ref_counted->HasOneRef());
108
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());
113
114 // There is only one reference to |ref_counted| after the Cancel().
115 cancelable.Cancel();
116 EXPECT_TRUE(cancelable.IsCancelled());
117 EXPECT_TRUE(ref_counted.get());
118 EXPECT_TRUE(ref_counted->HasOneRef());
119}
120
121// Reset().
122// - Reset() replaces the existing wrapped callback with a new callback.
123// - Reset() deactivates outstanding callbacks.
124TEST(CancelableCallbackTest, Reset) {
125 int count = 0;
126 CancelableClosure cancelable(
127 base::Bind(&Increment, base::Unretained(&count)));
128
129 base::Closure callback = cancelable.callback();
130 callback.Run();
131 EXPECT_EQ(1, count);
132
133 callback.Run();
134 EXPECT_EQ(2, count);
135
136 cancelable.Reset(
137 base::Bind(&IncrementBy, base::Unretained(&count), 3));
138 EXPECT_FALSE(cancelable.IsCancelled());
139
140 // The stale copy of the cancelable callback is non-null.
141 ASSERT_FALSE(callback.is_null());
142
143 // The stale copy of the cancelable callback is no longer active.
144 callback.Run();
145 EXPECT_EQ(2, count);
146
147 base::Closure callback2 = cancelable.callback();
148 ASSERT_FALSE(callback2.is_null());
149
150 callback2.Run();
151 EXPECT_EQ(5, count);
152}
153
154// IsCanceled().
155// - Cancel() transforms the CancelableCallback into a cancelled state.
156TEST(CancelableCallbackTest, IsNull) {
157 CancelableClosure cancelable;
158 EXPECT_TRUE(cancelable.IsCancelled());
159
160 int count = 0;
161 cancelable.Reset(base::Bind(&Increment,
162 base::Unretained(&count)));
163 EXPECT_FALSE(cancelable.IsCancelled());
164
165 cancelable.Cancel();
166 EXPECT_TRUE(cancelable.IsCancelled());
167}
168
169// CancelableCallback posted to a MessageLoop with PostTask.
170// - Callbacks posted to a MessageLoop can be cancelled.
171TEST(CancelableCallbackTest, PostTask) {
Andrew Top0d1858f2019-05-15 22:01:47 -0700172 MessageLoop loop;
David Ghandehari9e5b5872016-07-28 09:50:04 -0700173
174 int count = 0;
175 CancelableClosure cancelable(base::Bind(&Increment,
176 base::Unretained(&count)));
177
Andrew Top0d1858f2019-05-15 22:01:47 -0700178 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
179 RunLoop().RunUntilIdle();
David Ghandehari9e5b5872016-07-28 09:50:04 -0700180
181 EXPECT_EQ(1, count);
182
Andrew Top0d1858f2019-05-15 22:01:47 -0700183 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
David Ghandehari9e5b5872016-07-28 09:50:04 -0700184
185 // Cancel before running the message loop.
186 cancelable.Cancel();
Andrew Top0d1858f2019-05-15 22:01:47 -0700187 RunLoop().RunUntilIdle();
David Ghandehari9e5b5872016-07-28 09:50:04 -0700188
189 // Callback never ran due to cancellation; count is the same.
190 EXPECT_EQ(1, count);
191}
192
Andrew Top0d1858f2019-05-15 22:01:47 -0700193// CancelableCallback can be used with move-only types.
194TEST(CancelableCallbackTest, MoveOnlyType) {
195 const int kExpectedResult = 42;
196
197 int result = 0;
198 CancelableCallback<void(std::unique_ptr<int>)> cb(
199 base::Bind(&OnMoveOnlyReceived, base::Unretained(&result)));
Kaido Kertf9bc0132023-12-04 11:52:41 -0800200 cb.callback().Run(std::make_unique<int>(kExpectedResult));
Andrew Top0d1858f2019-05-15 22:01:47 -0700201
202 EXPECT_EQ(kExpectedResult, result);
203}
204
David Ghandehari9e5b5872016-07-28 09:50:04 -0700205} // namespace
206} // namespace base