[M73 Dev][Tizen] Fix compilation errors for TV profile
[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/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"
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
29 void Increment(int* count) { (*count)++; }
30 void IncrementBy(int* count, int n) { (*count) += n; }
31 void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
32
33 void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
34   *value = *result;
35 }
36
37 // Cancel().
38 //  - Callback can be run multiple times.
39 //  - After Cancel(), Run() completes but has no effect.
40 TEST(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.
61 TEST(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.
86 TEST(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).
105 TEST(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.
124 TEST(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.
156 TEST(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 task environment with PostTask.
170 //  - Posted callbacks can be cancelled.
171 TEST(CancelableCallbackTest, PostTask) {
172   test::ScopedTaskEnvironment scoped_task_environment;
173
174   int count = 0;
175   CancelableClosure cancelable(base::Bind(&Increment,
176                                            base::Unretained(&count)));
177
178   ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
179   RunLoop().RunUntilIdle();
180
181   EXPECT_EQ(1, count);
182
183   ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
184
185   // Cancel before running the tasks.
186   cancelable.Cancel();
187   RunLoop().RunUntilIdle();
188
189   // Callback never ran due to cancellation; count is the same.
190   EXPECT_EQ(1, count);
191 }
192
193 // CancelableCallback can be used with move-only types.
194 TEST(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)));
200   cb.callback().Run(std::make_unique<int>(kExpectedResult));
201
202   EXPECT_EQ(kExpectedResult, result);
203 }
204
205 }  // namespace
206 }  // namespace base