| 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/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/callback_helpers.h" |
| 6 #include "base/callback_internal.h" | 8 #include "base/callback_internal.h" |
| 7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 11 |
| 10 namespace base { | 12 namespace base { |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 class HelperObject { | |
| 15 public: | |
| 16 HelperObject() : next_number_(0) { } | |
| 17 int GetNextNumber() { return ++next_number_; } | |
| 18 void GetNextNumberArg(int* number) { *number = GetNextNumber(); } | |
| 19 | |
| 20 private: | |
| 21 int next_number_; | |
| 22 }; | |
| 23 | |
| 24 struct FakeInvoker { | 16 struct FakeInvoker { |
| 25 typedef void(RunType)(internal::BindStateBase*); | 17 typedef void(RunType)(internal::BindStateBase*); |
| 26 static void Run(internal::BindStateBase*) { | 18 static void Run(internal::BindStateBase*) { |
| 27 } | 19 } |
| 28 }; | 20 }; |
| 21 |
| 29 } // namespace | 22 } // namespace |
| 30 | 23 |
| 31 namespace internal { | 24 namespace internal { |
| 32 template <typename Runnable, typename RunType, typename BoundArgsType> | 25 template <typename Runnable, typename RunType, typename BoundArgsType> |
| 33 struct BindState; | 26 struct BindState; |
| 34 | 27 |
| 35 // White-box testpoints to inject into a Callback<> object for checking | 28 // White-box testpoints to inject into a Callback<> object for checking |
| 36 // comparators and emptiness APIs. Use a BindState that is specialized | 29 // comparators and emptiness APIs. Use a BindState that is specialized |
| 37 // based on a type we declared in the anonymous namespace above to remove any | 30 // based on a type we declared in the anonymous namespace above to remove any |
| 38 // chance of colliding with another instantiation and breaking the | 31 // chance of colliding with another instantiation and breaking the |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // Resetting should bring us back to empty. | 116 // Resetting should bring us back to empty. |
| 124 ASSERT_FALSE(callback_a_.is_null()); | 117 ASSERT_FALSE(callback_a_.is_null()); |
| 125 ASSERT_FALSE(callback_a_.Equals(null_callback_)); | 118 ASSERT_FALSE(callback_a_.Equals(null_callback_)); |
| 126 | 119 |
| 127 callback_a_.Reset(); | 120 callback_a_.Reset(); |
| 128 | 121 |
| 129 EXPECT_TRUE(callback_a_.is_null()); | 122 EXPECT_TRUE(callback_a_.is_null()); |
| 130 EXPECT_TRUE(callback_a_.Equals(null_callback_)); | 123 EXPECT_TRUE(callback_a_.Equals(null_callback_)); |
| 131 } | 124 } |
| 132 | 125 |
| 126 struct TestForReentrancy { |
| 127 TestForReentrancy() |
| 128 : cb_already_run(false), |
| 129 cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) { |
| 130 } |
| 131 void AssertCBIsNull() { |
| 132 ASSERT_TRUE(cb.is_null()); |
| 133 cb_already_run = true; |
| 134 } |
| 135 bool cb_already_run; |
| 136 Closure cb; |
| 137 }; |
| 138 |
| 139 TEST_F(CallbackTest, ResetAndReturn) { |
| 140 TestForReentrancy tfr; |
| 141 ASSERT_FALSE(tfr.cb.is_null()); |
| 142 ASSERT_FALSE(tfr.cb_already_run); |
| 143 ResetAndReturn(&tfr.cb).Run(); |
| 144 ASSERT_TRUE(tfr.cb.is_null()); |
| 145 ASSERT_TRUE(tfr.cb_already_run); |
| 146 } |
| 147 |
| 133 } // namespace | 148 } // namespace |
| 134 } // namespace base | 149 } // namespace base |
| OLD | NEW |