Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / base / cancelable_callback_unittest.cc
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.
4
5 #include "base/cancelable_callback.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/callback_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"
18
19 namespace base {
20 namespace {
21
22 class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
23  private:
24   friend class RefCountedThreadSafe<TestRefCounted>;
25   ~TestRefCounted() = default;
26 };
27
28 void Increment(int* count) { (*count)++; }
29 void IncrementBy(int* count, int n) { (*count) += n; }
30 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
31
32 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
33   *value = *result;
34 }
35
36 // Cancel().
37 //  - Callback can be run multiple times.
38 //  - After Cancel(), Run() completes but has no effect.
39 TEST(CancelableCallbackTest, Cancel) {
40   int count = 0;
41   CancelableRepeatingClosure cancelable(
42       base::BindRepeating(&Increment, base::Unretained(&count)));
43
44   base::RepeatingClosure callback = cancelable.callback();
45   callback.Run();
46   EXPECT_EQ(1, count);
47
48   callback.Run();
49   EXPECT_EQ(2, count);
50
51   cancelable.Cancel();
52   callback.Run();
53   EXPECT_EQ(2, count);
54 }
55
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) {
61   int count = 0;
62   CancelableRepeatingClosure cancelable(
63       base::BindRepeating(&Increment, base::Unretained(&count)));
64
65   base::RepeatingClosure callback1 = cancelable.callback();
66   base::RepeatingClosure callback2 = cancelable.callback();
67   cancelable.Cancel();
68
69   callback1.Run();
70   EXPECT_EQ(0, count);
71
72   callback2.Run();
73   EXPECT_EQ(0, count);
74
75   // Calling Cancel() again has no effect.
76   cancelable.Cancel();
77
78   // callback() of a cancelled callback is null.
79   base::RepeatingClosure callback3 = cancelable.callback();
80   EXPECT_TRUE(callback3.is_null());
81 }
82
83 // CancelableRepeatingCallback destroyed before callback is run.
84 //  - Destruction of CancelableRepeatingCallback cancels outstanding callbacks.
85 TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
86   int count = 0;
87   base::RepeatingClosure callback;
88
89   {
90     CancelableRepeatingClosure cancelable(
91         base::BindRepeating(&Increment, base::Unretained(&count)));
92
93     callback = cancelable.callback();
94     callback.Run();
95     EXPECT_EQ(1, count);
96   }
97
98   callback.Run();
99   EXPECT_EQ(1, count);
100 }
101
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());
107
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());
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.
124 TEST(CancelableCallbackTest, Reset) {
125   int count = 0;
126   CancelableRepeatingClosure cancelable(
127       base::BindRepeating(&Increment, base::Unretained(&count)));
128
129   base::RepeatingClosure 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::BindRepeating(&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::RepeatingClosure 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 CancelableOnceCallback into a cancelled state.
156 TEST(CancelableCallbackTest, IsNull) {
157   CancelableOnceClosure cancelable;
158   EXPECT_TRUE(cancelable.IsCancelled());
159
160   int count = 0;
161   cancelable.Reset(base::BindOnce(&Increment, base::Unretained(&count)));
162   EXPECT_FALSE(cancelable.IsCancelled());
163
164   cancelable.Cancel();
165   EXPECT_TRUE(cancelable.IsCancelled());
166 }
167
168 // CancelableRepeatingCallback posted to a task environment with PostTask.
169 //  - Posted callbacks can be cancelled.
170 TEST(CancelableCallbackTest, PostTask) {
171   test::TaskEnvironment task_environment;
172
173   int count = 0;
174   CancelableRepeatingClosure cancelable(
175       base::BindRepeating(&Increment, base::Unretained(&count)));
176
177   ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
178   RunLoop().RunUntilIdle();
179
180   EXPECT_EQ(1, count);
181
182   ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
183
184   // Cancel before running the tasks.
185   cancelable.Cancel();
186   RunLoop().RunUntilIdle();
187
188   // Callback never ran due to cancellation; count is the same.
189   EXPECT_EQ(1, count);
190 }
191
192 // CancelableRepeatingCallback can be used with move-only types.
193 TEST(CancelableCallbackTest, MoveOnlyType) {
194   const int kExpectedResult = 42;
195
196   int result = 0;
197   CancelableRepeatingCallback<void(std::unique_ptr<int>)> cb(
198       base::BindRepeating(&OnMoveOnlyReceived, base::Unretained(&result)));
199   cb.callback().Run(std::make_unique<int>(kExpectedResult));
200
201   EXPECT_EQ(kExpectedResult, result);
202 }
203
204 }  // namespace
205 }  // namespace base