| 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 namespace { | 17 void NopInvokeFunc(internal::BindStateBase*) {} |
| 18 | |
| 19 struct FakeInvoker { | |
| 20 // MSVC 2013 doesn't support Type Alias of function types. | |
| 21 // Revisit this after we update it to newer version. | |
| 22 typedef void RunType(internal::BindStateBase*); | |
| 23 static void Run(internal::BindStateBase*) { | |
| 24 } | |
| 25 }; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 namespace internal { | |
| 30 | 18 |
| 31 // White-box testpoints to inject into a Callback<> object for checking | 19 // White-box testpoints to inject into a Callback<> object for checking |
| 32 // comparators and emptiness APIs. Use a BindState that is specialized | 20 // comparators and emptiness APIs. Use a BindState that is specialized |
| 33 // 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 |
| 34 // chance of colliding with another instantiation and breaking the | 22 // chance of colliding with another instantiation and breaking the |
| 35 // one-definition-rule. | 23 // one-definition-rule. |
| 36 template <> | 24 struct FakeBindState1 : internal::BindStateBase { |
| 37 struct BindState<void(), void(), FakeInvoker> | 25 FakeBindState1() : BindStateBase(&Destroy) {} |
| 38 : public BindStateBase { | |
| 39 public: | |
| 40 BindState() : BindStateBase(&Destroy) {} | |
| 41 using InvokerType = FakeInvoker; | |
| 42 private: | 26 private: |
| 43 ~BindState() {} | 27 ~FakeBindState1() {} |
| 44 static void Destroy(BindStateBase* self) { | 28 static void Destroy(internal::BindStateBase* self) { |
| 45 delete static_cast<BindState*>(self); | 29 delete static_cast<FakeBindState1*>(self); |
| 46 } | 30 } |
| 47 }; | 31 }; |
| 48 | 32 |
| 49 template <> | 33 struct FakeBindState2 : internal::BindStateBase { |
| 50 struct BindState<void(), void(), FakeInvoker, FakeInvoker> | 34 FakeBindState2() : BindStateBase(&Destroy) {} |
| 51 : public BindStateBase { | |
| 52 public: | |
| 53 BindState() : BindStateBase(&Destroy) {} | |
| 54 using InvokerType = FakeInvoker; | |
| 55 private: | 35 private: |
| 56 ~BindState() {} | 36 ~FakeBindState2() {} |
| 57 static void Destroy(BindStateBase* self) { | 37 static void Destroy(internal::BindStateBase* self) { |
| 58 delete static_cast<BindState*>(self); | 38 delete static_cast<FakeBindState2*>(self); |
| 59 } | 39 } |
| 60 }; | 40 }; |
| 61 } // namespace internal | |
| 62 | 41 |
| 63 namespace { | 42 namespace { |
| 64 | 43 |
| 65 using FakeBindState1 = internal::BindState<void(), void(), FakeInvoker>; | |
| 66 using FakeBindState2 = | |
| 67 internal::BindState<void(), void(), FakeInvoker, FakeInvoker>; | |
| 68 | |
| 69 class CallbackTest : public ::testing::Test { | 44 class CallbackTest : public ::testing::Test { |
| 70 public: | 45 public: |
| 71 CallbackTest() | 46 CallbackTest() |
| 72 : callback_a_(new FakeBindState1()), | 47 : callback_a_(new FakeBindState1(), &NopInvokeFunc), |
| 73 callback_b_(new FakeBindState2()) { | 48 callback_b_(new FakeBindState2(), &NopInvokeFunc) { |
| 74 } | 49 } |
| 75 | 50 |
| 76 ~CallbackTest() override {} | 51 ~CallbackTest() override {} |
| 77 | 52 |
| 78 protected: | 53 protected: |
| 79 Callback<void()> callback_a_; | 54 Callback<void()> callback_a_; |
| 80 const Callback<void()> callback_b_; // Ensure APIs work with const. | 55 const Callback<void()> callback_b_; // Ensure APIs work with const. |
| 81 Callback<void()> null_callback_; | 56 Callback<void()> null_callback_; |
| 82 }; | 57 }; |
| 83 | 58 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 106 EXPECT_FALSE(callback_a_.is_null()); | 81 EXPECT_FALSE(callback_a_.is_null()); |
| 107 EXPECT_FALSE(callback_b_.is_null()); | 82 EXPECT_FALSE(callback_b_.is_null()); |
| 108 } | 83 } |
| 109 | 84 |
| 110 TEST_F(CallbackTest, Equals) { | 85 TEST_F(CallbackTest, Equals) { |
| 111 EXPECT_TRUE(callback_a_.Equals(callback_a_)); | 86 EXPECT_TRUE(callback_a_.Equals(callback_a_)); |
| 112 EXPECT_FALSE(callback_a_.Equals(callback_b_)); | 87 EXPECT_FALSE(callback_a_.Equals(callback_b_)); |
| 113 EXPECT_FALSE(callback_b_.Equals(callback_a_)); | 88 EXPECT_FALSE(callback_b_.Equals(callback_a_)); |
| 114 | 89 |
| 115 // We should compare based on instance, not type. | 90 // We should compare based on instance, not type. |
| 116 Callback<void()> callback_c(new FakeBindState1()); | 91 Callback<void()> callback_c(new FakeBindState1(), &NopInvokeFunc); |
| 117 Callback<void()> callback_a2 = callback_a_; | 92 Callback<void()> callback_a2 = callback_a_; |
| 118 EXPECT_TRUE(callback_a_.Equals(callback_a2)); | 93 EXPECT_TRUE(callback_a_.Equals(callback_a2)); |
| 119 EXPECT_FALSE(callback_a_.Equals(callback_c)); | 94 EXPECT_FALSE(callback_a_.Equals(callback_c)); |
| 120 | 95 |
| 121 // Empty, however, is always equal to empty. | 96 // Empty, however, is always equal to empty. |
| 122 Callback<void()> empty2; | 97 Callback<void()> empty2; |
| 123 EXPECT_TRUE(null_callback_.Equals(empty2)); | 98 EXPECT_TRUE(null_callback_.Equals(empty2)); |
| 124 } | 99 } |
| 125 | 100 |
| 126 TEST_F(CallbackTest, Reset) { | 101 TEST_F(CallbackTest, Reset) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 157 |
| 183 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { | 158 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
| 184 bool deleted = false; | 159 bool deleted = false; |
| 185 CallbackOwner* owner = new CallbackOwner(&deleted); | 160 CallbackOwner* owner = new CallbackOwner(&deleted); |
| 186 owner->Reset(); | 161 owner->Reset(); |
| 187 ASSERT_TRUE(deleted); | 162 ASSERT_TRUE(deleted); |
| 188 } | 163 } |
| 189 | 164 |
| 190 } // namespace | 165 } // namespace |
| 191 } // namespace base | 166 } // namespace base |
| OLD | NEW |