OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/callback.h" | 5 #include "base/callback.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/callback_internal.h" | 11 #include "base/callback_internal.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 void NopInvokeFunc() {} | 17 void NopInvokeFunc() {} |
18 | 18 |
19 // White-box testpoints to inject into a Callback<> object for checking | 19 // White-box testpoints to inject into a Callback<> object for checking |
20 // comparators and emptiness APIs. Use a BindState that is specialized | 20 // comparators and emptiness APIs. Use a BindState that is specialized |
21 // based on a type we declared in the anonymous namespace above to remove any | 21 // based on a type we declared in the anonymous namespace above to remove any |
22 // chance of colliding with another instantiation and breaking the | 22 // chance of colliding with another instantiation and breaking the |
23 // one-definition-rule. | 23 // one-definition-rule. |
24 struct FakeBindState1 : internal::BindStateBase { | 24 struct FakeBindState : internal::BindStateBase { |
25 FakeBindState1() : BindStateBase(&NopInvokeFunc, &Destroy, &IsCancelled) {} | 25 FakeBindState() : BindStateBase(&NopInvokeFunc, &Destroy, &IsCancelled) {} |
| 26 |
26 private: | 27 private: |
27 ~FakeBindState1() {} | 28 ~FakeBindState() {} |
28 static void Destroy(const internal::BindStateBase* self) { | 29 static void Destroy(const internal::BindStateBase* self) { |
29 delete static_cast<const FakeBindState1*>(self); | 30 delete static_cast<const FakeBindState*>(self); |
30 } | 31 } |
31 static bool IsCancelled(const internal::BindStateBase*) { | 32 static bool IsCancelled(const internal::BindStateBase*) { |
32 return false; | 33 return false; |
33 } | |
34 }; | |
35 | |
36 struct FakeBindState2 : internal::BindStateBase { | |
37 FakeBindState2() : BindStateBase(&NopInvokeFunc, &Destroy, &IsCancelled) {} | |
38 private: | |
39 ~FakeBindState2() {} | |
40 static void Destroy(const internal::BindStateBase* self) { | |
41 delete static_cast<const FakeBindState2*>(self); | |
42 } | |
43 static bool IsCancelled(const internal::BindStateBase*) { | |
44 return false; | |
45 } | 34 } |
46 }; | 35 }; |
47 | 36 |
48 namespace { | 37 namespace { |
49 | 38 |
50 class CallbackTest : public ::testing::Test { | 39 class CallbackTest : public ::testing::Test { |
51 public: | 40 public: |
52 CallbackTest() | 41 CallbackTest() |
53 : callback_a_(new FakeBindState1()), | 42 : callback_a_(new FakeBindState()), callback_b_(new FakeBindState()) {} |
54 callback_b_(new FakeBindState2()) { | |
55 } | |
56 | 43 |
57 ~CallbackTest() override {} | 44 ~CallbackTest() override {} |
58 | 45 |
59 protected: | 46 protected: |
60 Callback<void()> callback_a_; | 47 Callback<void()> callback_a_; |
61 const Callback<void()> callback_b_; // Ensure APIs work with const. | 48 const Callback<void()> callback_b_; // Ensure APIs work with const. |
62 Callback<void()> null_callback_; | 49 Callback<void()> null_callback_; |
63 }; | 50 }; |
64 | 51 |
65 // Ensure we can create unbound callbacks. We need this to be able to store | 52 // Ensure we can create unbound callbacks. We need this to be able to store |
(...skipping 21 matching lines...) Expand all Loading... |
87 EXPECT_FALSE(callback_a_.is_null()); | 74 EXPECT_FALSE(callback_a_.is_null()); |
88 EXPECT_FALSE(callback_b_.is_null()); | 75 EXPECT_FALSE(callback_b_.is_null()); |
89 } | 76 } |
90 | 77 |
91 TEST_F(CallbackTest, Equals) { | 78 TEST_F(CallbackTest, Equals) { |
92 EXPECT_TRUE(callback_a_.Equals(callback_a_)); | 79 EXPECT_TRUE(callback_a_.Equals(callback_a_)); |
93 EXPECT_FALSE(callback_a_.Equals(callback_b_)); | 80 EXPECT_FALSE(callback_a_.Equals(callback_b_)); |
94 EXPECT_FALSE(callback_b_.Equals(callback_a_)); | 81 EXPECT_FALSE(callback_b_.Equals(callback_a_)); |
95 | 82 |
96 // We should compare based on instance, not type. | 83 // We should compare based on instance, not type. |
97 Callback<void()> callback_c(new FakeBindState1()); | 84 Callback<void()> callback_c(new FakeBindState()); |
98 Callback<void()> callback_a2 = callback_a_; | 85 Callback<void()> callback_a2 = callback_a_; |
99 EXPECT_TRUE(callback_a_.Equals(callback_a2)); | 86 EXPECT_TRUE(callback_a_.Equals(callback_a2)); |
100 EXPECT_FALSE(callback_a_.Equals(callback_c)); | 87 EXPECT_FALSE(callback_a_.Equals(callback_c)); |
101 | 88 |
102 // Empty, however, is always equal to empty. | 89 // Empty, however, is always equal to empty. |
103 Callback<void()> empty2; | 90 Callback<void()> empty2; |
104 EXPECT_TRUE(null_callback_.Equals(empty2)); | 91 EXPECT_TRUE(null_callback_.Equals(empty2)); |
105 } | 92 } |
106 | 93 |
107 TEST_F(CallbackTest, Reset) { | 94 TEST_F(CallbackTest, Reset) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 178 |
192 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { | 179 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
193 bool deleted = false; | 180 bool deleted = false; |
194 CallbackOwner* owner = new CallbackOwner(&deleted); | 181 CallbackOwner* owner = new CallbackOwner(&deleted); |
195 owner->Reset(); | 182 owner->Reset(); |
196 ASSERT_TRUE(deleted); | 183 ASSERT_TRUE(deleted); |
197 } | 184 } |
198 | 185 |
199 } // namespace | 186 } // namespace |
200 } // namespace base | 187 } // namespace base |
OLD | NEW |