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(internal::BindStateBase*) {} | 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 FakeBindState1 : internal::BindStateBase { |
25 FakeBindState1() : BindStateBase(&Destroy) {} | 25 FakeBindState1() : BindStateBase(&NopInvokeFunc, &Destroy) {} |
26 private: | 26 private: |
27 ~FakeBindState1() {} | 27 ~FakeBindState1() {} |
28 static void Destroy(internal::BindStateBase* self) { | 28 static void Destroy(internal::BindStateBase* self) { |
29 delete static_cast<FakeBindState1*>(self); | 29 delete static_cast<FakeBindState1*>(self); |
30 } | 30 } |
31 }; | 31 }; |
32 | 32 |
33 struct FakeBindState2 : internal::BindStateBase { | 33 struct FakeBindState2 : internal::BindStateBase { |
34 FakeBindState2() : BindStateBase(&Destroy) {} | 34 FakeBindState2() : BindStateBase(&NopInvokeFunc, &Destroy) {} |
35 private: | 35 private: |
36 ~FakeBindState2() {} | 36 ~FakeBindState2() {} |
37 static void Destroy(internal::BindStateBase* self) { | 37 static void Destroy(internal::BindStateBase* self) { |
38 delete static_cast<FakeBindState2*>(self); | 38 delete static_cast<FakeBindState2*>(self); |
39 } | 39 } |
40 }; | 40 }; |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 class CallbackTest : public ::testing::Test { | 44 class CallbackTest : public ::testing::Test { |
45 public: | 45 public: |
46 CallbackTest() | 46 CallbackTest() |
47 : callback_a_(new FakeBindState1(), &NopInvokeFunc), | 47 : callback_a_(new FakeBindState1()), |
48 callback_b_(new FakeBindState2(), &NopInvokeFunc) { | 48 callback_b_(new FakeBindState2()) { |
49 } | 49 } |
50 | 50 |
51 ~CallbackTest() override {} | 51 ~CallbackTest() override {} |
52 | 52 |
53 protected: | 53 protected: |
54 Callback<void()> callback_a_; | 54 Callback<void()> callback_a_; |
55 const Callback<void()> callback_b_; // Ensure APIs work with const. | 55 const Callback<void()> callback_b_; // Ensure APIs work with const. |
56 Callback<void()> null_callback_; | 56 Callback<void()> null_callback_; |
57 }; | 57 }; |
58 | 58 |
(...skipping 22 matching lines...) Expand all Loading... |
81 EXPECT_FALSE(callback_a_.is_null()); | 81 EXPECT_FALSE(callback_a_.is_null()); |
82 EXPECT_FALSE(callback_b_.is_null()); | 82 EXPECT_FALSE(callback_b_.is_null()); |
83 } | 83 } |
84 | 84 |
85 TEST_F(CallbackTest, Equals) { | 85 TEST_F(CallbackTest, Equals) { |
86 EXPECT_TRUE(callback_a_.Equals(callback_a_)); | 86 EXPECT_TRUE(callback_a_.Equals(callback_a_)); |
87 EXPECT_FALSE(callback_a_.Equals(callback_b_)); | 87 EXPECT_FALSE(callback_a_.Equals(callback_b_)); |
88 EXPECT_FALSE(callback_b_.Equals(callback_a_)); | 88 EXPECT_FALSE(callback_b_.Equals(callback_a_)); |
89 | 89 |
90 // We should compare based on instance, not type. | 90 // We should compare based on instance, not type. |
91 Callback<void()> callback_c(new FakeBindState1(), &NopInvokeFunc); | 91 Callback<void()> callback_c(new FakeBindState1()); |
92 Callback<void()> callback_a2 = callback_a_; | 92 Callback<void()> callback_a2 = callback_a_; |
93 EXPECT_TRUE(callback_a_.Equals(callback_a2)); | 93 EXPECT_TRUE(callback_a_.Equals(callback_a2)); |
94 EXPECT_FALSE(callback_a_.Equals(callback_c)); | 94 EXPECT_FALSE(callback_a_.Equals(callback_c)); |
95 | 95 |
96 // Empty, however, is always equal to empty. | 96 // Empty, however, is always equal to empty. |
97 Callback<void()> empty2; | 97 Callback<void()> empty2; |
98 EXPECT_TRUE(null_callback_.Equals(empty2)); | 98 EXPECT_TRUE(null_callback_.Equals(empty2)); |
99 } | 99 } |
100 | 100 |
101 TEST_F(CallbackTest, Reset) { | 101 TEST_F(CallbackTest, Reset) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 157 |
158 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { | 158 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
159 bool deleted = false; | 159 bool deleted = false; |
160 CallbackOwner* owner = new CallbackOwner(&deleted); | 160 CallbackOwner* owner = new CallbackOwner(&deleted); |
161 owner->Reset(); | 161 owner->Reset(); |
162 ASSERT_TRUE(deleted); | 162 ASSERT_TRUE(deleted); |
163 } | 163 } |
164 | 164 |
165 } // namespace | 165 } // namespace |
166 } // namespace base | 166 } // namespace base |
OLD | NEW |