[M73 Dev][Tizen] Fix compilation errors for TV profile
[platform/framework/web/chromium-efl.git] / base / callback_unittest.cc
1 // Copyright (c) 2012 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/callback.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/callback_internal.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/test/test_timeouts.h"
15 #include "base/threading/thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace base {
19
20 void NopInvokeFunc() {}
21
22 // White-box testpoints to inject into a callback object for checking
23 // comparators and emptiness APIs. Use a BindState that is specialized based on
24 // a type we declared in the anonymous namespace above to remove any chance of
25 // colliding with another instantiation and breaking the one-definition-rule.
26 struct FakeBindState : internal::BindStateBase {
27   FakeBindState() : BindStateBase(&NopInvokeFunc, &Destroy, &IsCancelled) {}
28
29  private:
30   ~FakeBindState() = default;
31   static void Destroy(const internal::BindStateBase* self) {
32     delete static_cast<const FakeBindState*>(self);
33   }
34   static bool IsCancelled(const internal::BindStateBase*,
35                           internal::BindStateBase::CancellationQueryMode mode) {
36     switch (mode) {
37       case internal::BindStateBase::IS_CANCELLED:
38         return false;
39       case internal::BindStateBase::MAYBE_VALID:
40         return true;
41     }
42     NOTREACHED();
43   }
44 };
45
46 namespace {
47
48 class CallbackTest : public ::testing::Test {
49  public:
50   CallbackTest()
51       : callback_a_(new FakeBindState()), callback_b_(new FakeBindState()) {}
52
53   ~CallbackTest() override = default;
54
55  protected:
56   RepeatingCallback<void()> callback_a_;
57   const RepeatingCallback<void()> callback_b_;  // Ensure APIs work with const.
58   RepeatingCallback<void()> null_callback_;
59 };
60
61 // Ensure we can create unbound callbacks. We need this to be able to store
62 // them in class members that can be initialized later.
63 TEST_F(CallbackTest, DefaultConstruction) {
64   RepeatingCallback<void()> c0;
65   RepeatingCallback<void(int)> c1;
66   RepeatingCallback<void(int, int)> c2;
67   RepeatingCallback<void(int, int, int)> c3;
68   RepeatingCallback<void(int, int, int, int)> c4;
69   RepeatingCallback<void(int, int, int, int, int)> c5;
70   RepeatingCallback<void(int, int, int, int, int, int)> c6;
71
72   EXPECT_TRUE(c0.is_null());
73   EXPECT_TRUE(c1.is_null());
74   EXPECT_TRUE(c2.is_null());
75   EXPECT_TRUE(c3.is_null());
76   EXPECT_TRUE(c4.is_null());
77   EXPECT_TRUE(c5.is_null());
78   EXPECT_TRUE(c6.is_null());
79 }
80
81 TEST_F(CallbackTest, IsNull) {
82   EXPECT_TRUE(null_callback_.is_null());
83   EXPECT_FALSE(callback_a_.is_null());
84   EXPECT_FALSE(callback_b_.is_null());
85 }
86
87 TEST_F(CallbackTest, Equals) {
88   EXPECT_TRUE(callback_a_.Equals(callback_a_));
89   EXPECT_FALSE(callback_a_.Equals(callback_b_));
90   EXPECT_FALSE(callback_b_.Equals(callback_a_));
91
92   // We should compare based on instance, not type.
93   RepeatingCallback<void()> callback_c(new FakeBindState());
94   RepeatingCallback<void()> callback_a2 = callback_a_;
95   EXPECT_TRUE(callback_a_.Equals(callback_a2));
96   EXPECT_FALSE(callback_a_.Equals(callback_c));
97
98   // Empty, however, is always equal to empty.
99   RepeatingCallback<void()> empty2;
100   EXPECT_TRUE(null_callback_.Equals(empty2));
101 }
102
103 TEST_F(CallbackTest, Reset) {
104   // Resetting should bring us back to empty.
105   ASSERT_FALSE(callback_a_.is_null());
106   ASSERT_FALSE(callback_a_.Equals(null_callback_));
107
108   callback_a_.Reset();
109
110   EXPECT_TRUE(callback_a_.is_null());
111   EXPECT_TRUE(callback_a_.Equals(null_callback_));
112 }
113
114 TEST_F(CallbackTest, Move) {
115   // Moving should reset the callback.
116   ASSERT_FALSE(callback_a_.is_null());
117   ASSERT_FALSE(callback_a_.Equals(null_callback_));
118
119   auto tmp = std::move(callback_a_);
120
121   EXPECT_TRUE(callback_a_.is_null());
122   EXPECT_TRUE(callback_a_.Equals(null_callback_));
123 }
124
125 struct TestForReentrancy {
126   TestForReentrancy()
127       : cb_already_run(false),
128         cb(BindRepeating(&TestForReentrancy::AssertCBIsNull,
129                          Unretained(this))) {}
130   void AssertCBIsNull() {
131     ASSERT_TRUE(cb.is_null());
132     cb_already_run = true;
133   }
134   bool cb_already_run;
135   RepeatingClosure cb;
136 };
137
138 TEST_F(CallbackTest, ResetAndReturn) {
139   TestForReentrancy tfr;
140   ASSERT_FALSE(tfr.cb.is_null());
141   ASSERT_FALSE(tfr.cb_already_run);
142   ResetAndReturn(&tfr.cb).Run();
143   ASSERT_TRUE(tfr.cb.is_null());
144   ASSERT_TRUE(tfr.cb_already_run);
145 }
146
147 TEST_F(CallbackTest, NullAfterMoveRun) {
148   RepeatingClosure cb = BindRepeating([] {});
149   ASSERT_TRUE(cb);
150   std::move(cb).Run();
151   ASSERT_FALSE(cb);
152
153   const RepeatingClosure cb2 = BindRepeating([] {});
154   ASSERT_TRUE(cb2);
155   std::move(cb2).Run();
156   ASSERT_TRUE(cb2);
157
158   OnceClosure cb3 = BindOnce([] {});
159   ASSERT_TRUE(cb3);
160   std::move(cb3).Run();
161   ASSERT_FALSE(cb3);
162 }
163
164 TEST_F(CallbackTest, MaybeValidReturnsTrue) {
165   RepeatingCallback<void()> cb = BindRepeating([]() {});
166   // By default, MaybeValid() just returns true all the time.
167   EXPECT_TRUE(cb.MaybeValid());
168   cb.Run();
169   EXPECT_TRUE(cb.MaybeValid());
170 }
171
172 // WeakPtr detection in BindRepeating() requires a method, not just any
173 // function.
174 class ClassWithAMethod {
175  public:
176   void TheMethod() {}
177 };
178
179 TEST_F(CallbackTest, MaybeValidInvalidateWeakPtrsOnSameSequence) {
180   ClassWithAMethod obj;
181   WeakPtrFactory<ClassWithAMethod> factory(&obj);
182   WeakPtr<ClassWithAMethod> ptr = factory.GetWeakPtr();
183
184   RepeatingCallback<void()> cb =
185       BindRepeating(&ClassWithAMethod::TheMethod, ptr);
186   EXPECT_TRUE(cb.MaybeValid());
187
188   factory.InvalidateWeakPtrs();
189   // MaybeValid() should be false because InvalidateWeakPtrs() was called on
190   // the same thread.
191   EXPECT_FALSE(cb.MaybeValid());
192 }
193
194 TEST_F(CallbackTest, MaybeValidInvalidateWeakPtrsOnOtherSequence) {
195   ClassWithAMethod obj;
196   WeakPtrFactory<ClassWithAMethod> factory(&obj);
197   WeakPtr<ClassWithAMethod> ptr = factory.GetWeakPtr();
198
199   RepeatingCallback<void()> cb =
200       BindRepeating(&ClassWithAMethod::TheMethod, ptr);
201   EXPECT_TRUE(cb.MaybeValid());
202
203   Thread other_thread("other_thread");
204   other_thread.StartAndWaitForTesting();
205   other_thread.task_runner()->PostTask(
206       FROM_HERE,
207       BindOnce(
208           [](RepeatingCallback<void()> cb) {
209             // Check that MaybeValid() _eventually_ returns false.
210             const TimeDelta timeout = TestTimeouts::tiny_timeout();
211             const TimeTicks begin = TimeTicks::Now();
212             while (cb.MaybeValid() && (TimeTicks::Now() - begin) < timeout)
213               PlatformThread::YieldCurrentThread();
214             EXPECT_FALSE(cb.MaybeValid());
215           },
216           cb));
217   factory.InvalidateWeakPtrs();
218   // |other_thread|'s destructor will join, ensuring we wait for the task to be
219   // run.
220 }
221
222 class CallbackOwner : public base::RefCounted<CallbackOwner> {
223  public:
224   explicit CallbackOwner(bool* deleted) {
225     // WrapRefCounted() here is needed to avoid the check failure in the
226     // BindRepeating implementation, that refuses to create the first reference
227     // to ref-counted objects.
228     callback_ = BindRepeating(&CallbackOwner::Unused, WrapRefCounted(this));
229     deleted_ = deleted;
230   }
231   void Reset() {
232     callback_.Reset();
233     // We are deleted here if no-one else had a ref to us.
234   }
235
236  private:
237   friend class base::RefCounted<CallbackOwner>;
238   virtual ~CallbackOwner() {
239     *deleted_ = true;
240   }
241   void Unused() {
242     FAIL() << "Should never be called";
243   }
244
245   RepeatingClosure callback_;
246   bool* deleted_;
247 };
248
249 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
250   bool deleted = false;
251   CallbackOwner* owner = new CallbackOwner(&deleted);
252   owner->Reset();
253   ASSERT_TRUE(deleted);
254 }
255
256 }  // namespace
257 }  // namespace base