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 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 204 } |
205 | 205 |
206 void PtrArgSet(int *n) { | 206 void PtrArgSet(int *n) { |
207 *n = 2; | 207 *n = 2; |
208 } | 208 } |
209 | 209 |
210 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { | 210 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
211 return n; | 211 return n; |
212 } | 212 } |
213 | 213 |
| 214 int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) { |
| 215 return n; |
| 216 } |
| 217 |
214 void TakesACallback(const Closure& callback) { | 218 void TakesACallback(const Closure& callback) { |
215 callback.Run(); | 219 callback.Run(); |
216 } | 220 } |
217 | 221 |
218 class BindTest : public ::testing::Test { | 222 class BindTest : public ::testing::Test { |
219 public: | 223 public: |
220 BindTest() { | 224 BindTest() { |
221 const_has_ref_ptr_ = &has_ref_; | 225 const_has_ref_ptr_ = &has_ref_; |
222 const_no_ref_ptr_ = &no_ref_; | 226 const_no_ref_ptr_ = &no_ref_; |
223 static_func_mock_ptr = &static_func_mock_; | 227 static_func_mock_ptr = &static_func_mock_; |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 int copies = 0; | 660 int copies = 0; |
657 int assigns = 0; | 661 int assigns = 0; |
658 CopyCounter counter(&copies, &assigns); | 662 CopyCounter counter(&copies, &assigns); |
659 Callback<int(void)> all_const_ref_cb = | 663 Callback<int(void)> all_const_ref_cb = |
660 Bind(&GetCopies, ConstRef(counter)); | 664 Bind(&GetCopies, ConstRef(counter)); |
661 EXPECT_EQ(0, all_const_ref_cb.Run()); | 665 EXPECT_EQ(0, all_const_ref_cb.Run()); |
662 EXPECT_EQ(0, copies); | 666 EXPECT_EQ(0, copies); |
663 EXPECT_EQ(0, assigns); | 667 EXPECT_EQ(0, assigns); |
664 } | 668 } |
665 | 669 |
| 670 TEST_F(BindTest, ScopedRefptr) { |
| 671 // BUG: The scoped_refptr should cause the only AddRef()/Release() pair. But |
| 672 // due to a bug in base::Bind(), there's an extra call when invoking the |
| 673 // callback. |
| 674 // https://code.google.com/p/chromium/issues/detail?id=251937 |
| 675 EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 676 EXPECT_CALL(has_ref_, Release()).Times(2); |
| 677 |
| 678 const scoped_refptr<StrictMock<HasRef> > refptr(&has_ref_); |
| 679 |
| 680 Callback<int(void)> scoped_refptr_const_ref_cb = |
| 681 Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1); |
| 682 EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run()); |
| 683 } |
| 684 |
666 // Test Owned() support. | 685 // Test Owned() support. |
667 TEST_F(BindTest, Owned) { | 686 TEST_F(BindTest, Owned) { |
668 int deletes = 0; | 687 int deletes = 0; |
669 DeleteCounter* counter = new DeleteCounter(&deletes); | 688 DeleteCounter* counter = new DeleteCounter(&deletes); |
670 | 689 |
671 // If we don't capture, delete happens on Callback destruction/reset. | 690 // If we don't capture, delete happens on Callback destruction/reset. |
672 // return the same value. | 691 // return the same value. |
673 Callback<DeleteCounter*(void)> no_capture_cb = | 692 Callback<DeleteCounter*(void)> no_capture_cb = |
674 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); | 693 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
675 ASSERT_EQ(counter, no_capture_cb.Run()); | 694 ASSERT_EQ(counter, no_capture_cb.Run()); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 base::Callback<void(int)> null_cb; | 824 base::Callback<void(int)> null_cb; |
806 ASSERT_TRUE(null_cb.is_null()); | 825 ASSERT_TRUE(null_cb.is_null()); |
807 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 826 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
808 } | 827 } |
809 | 828 |
810 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 829 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
811 // GTEST_HAS_DEATH_TEST | 830 // GTEST_HAS_DEATH_TEST |
812 | 831 |
813 } // namespace | 832 } // namespace |
814 } // namespace base | 833 } // namespace base |
OLD | NEW |