| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/callback_internal.h" | 8 #include "base/callback_internal.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 struct FakeInvoker { | 17 struct FakeInvoker { |
| 18 typedef void(RunType)(internal::BindStateBase*); | 18 typedef void(RunType)(internal::BindStateBase*); |
| 19 static void Run(internal::BindStateBase*) { | 19 static void Run(internal::BindStateBase*) { |
| 20 } | 20 } |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 namespace internal { | 25 namespace internal { |
| 26 template <typename Runnable, typename RunType, typename BoundArgsType> | |
| 27 struct BindState; | |
| 28 | 26 |
| 29 // White-box testpoints to inject into a Callback<> object for checking | 27 // White-box testpoints to inject into a Callback<> object for checking |
| 30 // comparators and emptiness APIs. Use a BindState that is specialized | 28 // comparators and emptiness APIs. Use a BindState that is specialized |
| 31 // based on a type we declared in the anonymous namespace above to remove any | 29 // based on a type we declared in the anonymous namespace above to remove any |
| 32 // chance of colliding with another instantiation and breaking the | 30 // chance of colliding with another instantiation and breaking the |
| 33 // one-definition-rule. | 31 // one-definition-rule. |
| 34 template <> | 32 template <> |
| 35 struct BindState<void(void), void(void), void(FakeInvoker)> | 33 struct BindState<void(void), void(void), FakeInvoker> |
| 36 : public BindStateBase { | 34 : public BindStateBase { |
| 37 public: | 35 public: |
| 38 BindState() : BindStateBase(&Destroy) {} | 36 BindState() : BindStateBase(&Destroy) {} |
| 39 typedef FakeInvoker InvokerType; | 37 typedef FakeInvoker InvokerType; |
| 40 private: | 38 private: |
| 41 ~BindState() {} | 39 ~BindState() {} |
| 42 static void Destroy(BindStateBase* self) { | 40 static void Destroy(BindStateBase* self) { |
| 43 delete static_cast<BindState*>(self); | 41 delete static_cast<BindState*>(self); |
| 44 } | 42 } |
| 45 }; | 43 }; |
| 46 | 44 |
| 47 template <> | 45 template <> |
| 48 struct BindState<void(void), void(void), | 46 struct BindState<void(void), void(void), FakeInvoker, FakeInvoker> |
| 49 void(FakeInvoker, FakeInvoker)> | |
| 50 : public BindStateBase { | 47 : public BindStateBase { |
| 51 public: | 48 public: |
| 52 BindState() : BindStateBase(&Destroy) {} | 49 BindState() : BindStateBase(&Destroy) {} |
| 53 typedef FakeInvoker InvokerType; | 50 typedef FakeInvoker InvokerType; |
| 54 private: | 51 private: |
| 55 ~BindState() {} | 52 ~BindState() {} |
| 56 static void Destroy(BindStateBase* self) { | 53 static void Destroy(BindStateBase* self) { |
| 57 delete static_cast<BindState*>(self); | 54 delete static_cast<BindState*>(self); |
| 58 } | 55 } |
| 59 }; | 56 }; |
| 60 } // namespace internal | 57 } // namespace internal |
| 61 | 58 |
| 62 namespace { | 59 namespace { |
| 63 | 60 |
| 64 typedef internal::BindState<void(void), void(void), void(FakeInvoker)> | 61 typedef internal::BindState<void(void), void(void), FakeInvoker> |
| 65 FakeBindState1; | 62 FakeBindState1; |
| 66 typedef internal::BindState<void(void), void(void), | 63 typedef internal::BindState<void(void), void(void), FakeInvoker, FakeInvoker> |
| 67 void(FakeInvoker, FakeInvoker)> | 64 FakeBindState2; |
| 68 FakeBindState2; | |
| 69 | 65 |
| 70 class CallbackTest : public ::testing::Test { | 66 class CallbackTest : public ::testing::Test { |
| 71 public: | 67 public: |
| 72 CallbackTest() | 68 CallbackTest() |
| 73 : callback_a_(new FakeBindState1()), | 69 : callback_a_(new FakeBindState1()), |
| 74 callback_b_(new FakeBindState2()) { | 70 callback_b_(new FakeBindState2()) { |
| 75 } | 71 } |
| 76 | 72 |
| 77 ~CallbackTest() override {} | 73 ~CallbackTest() override {} |
| 78 | 74 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 179 |
| 184 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { | 180 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
| 185 bool deleted = false; | 181 bool deleted = false; |
| 186 CallbackOwner* owner = new CallbackOwner(&deleted); | 182 CallbackOwner* owner = new CallbackOwner(&deleted); |
| 187 owner->Reset(); | 183 owner->Reset(); |
| 188 ASSERT_TRUE(deleted); | 184 ASSERT_TRUE(deleted); |
| 189 } | 185 } |
| 190 | 186 |
| 191 } // namespace | 187 } // namespace |
| 192 } // namespace base | 188 } // namespace base |
| OLD | NEW |