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