| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/pass_scoped_ptr.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 using ::testing::Mock; | 14 using ::testing::Mock; |
| 12 using ::testing::Return; | 15 using ::testing::Return; |
| 13 using ::testing::StrictMock; | 16 using ::testing::StrictMock; |
| 14 | 17 |
| 15 namespace base { | 18 namespace base { |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 ~DeleteCounter() { | 145 ~DeleteCounter() { |
| 143 (*deletes_)++; | 146 (*deletes_)++; |
| 144 } | 147 } |
| 145 | 148 |
| 146 void VoidMethod0() {} | 149 void VoidMethod0() {} |
| 147 | 150 |
| 148 private: | 151 private: |
| 149 int* deletes_; | 152 int* deletes_; |
| 150 }; | 153 }; |
| 151 | 154 |
| 155 DeleteCounter* MaybeCapture(const PassScopedPtr<DeleteCounter>& ptr, |
| 156 bool expect_valid, |
| 157 bool should_capture) { |
| 158 EXPECT_EQ(expect_valid, ptr.is_valid()); |
| 159 if (should_capture) { |
| 160 scoped_ptr<DeleteCounter> scoped; |
| 161 ptr.ToScopedPtr(&scoped); |
| 162 return scoped.release(); |
| 163 } |
| 164 return NULL; |
| 165 } |
| 166 |
| 152 // Some test functions that we can Bind to. | 167 // Some test functions that we can Bind to. |
| 153 template <typename T> | 168 template <typename T> |
| 154 T PolymorphicIdentity(T t) { | 169 T PolymorphicIdentity(T t) { |
| 155 return t; | 170 return t; |
| 156 } | 171 } |
| 157 | 172 |
| 158 template <typename T> | 173 template <typename T> |
| 159 void VoidPolymorphic1(T t) { | 174 void VoidPolymorphic1(T t) { |
| 160 } | 175 } |
| 161 | 176 |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 | 649 |
| 635 // Test Owned() support. | 650 // Test Owned() support. |
| 636 TEST_F(BindTest, Owned) { | 651 TEST_F(BindTest, Owned) { |
| 637 int deletes = 0; | 652 int deletes = 0; |
| 638 DeleteCounter* counter = new DeleteCounter(&deletes); | 653 DeleteCounter* counter = new DeleteCounter(&deletes); |
| 639 | 654 |
| 640 // If we don't capture, delete happens on Callback destruction/reset. | 655 // If we don't capture, delete happens on Callback destruction/reset. |
| 641 // return the same value. | 656 // return the same value. |
| 642 Callback<DeleteCounter*(void)> no_capture_cb = | 657 Callback<DeleteCounter*(void)> no_capture_cb = |
| 643 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); | 658 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
| 644 EXPECT_EQ(counter, no_capture_cb.Run()); | 659 ASSERT_EQ(counter, no_capture_cb.Run()); |
| 645 EXPECT_EQ(counter, no_capture_cb.Run()); | 660 ASSERT_EQ(counter, no_capture_cb.Run()); |
| 646 EXPECT_EQ(0, deletes); | 661 EXPECT_EQ(0, deletes); |
| 647 no_capture_cb.Reset(); // This should trigger a delete. | 662 no_capture_cb.Reset(); // This should trigger a delete. |
| 648 EXPECT_EQ(1, deletes); | 663 EXPECT_EQ(1, deletes); |
| 649 | 664 |
| 650 deletes = 0; | 665 deletes = 0; |
| 651 counter = new DeleteCounter(&deletes); | 666 counter = new DeleteCounter(&deletes); |
| 652 base::Closure own_object_cb = | 667 base::Closure own_object_cb = |
| 653 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); | 668 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
| 654 own_object_cb.Run(); | 669 own_object_cb.Run(); |
| 655 EXPECT_EQ(0, deletes); | 670 EXPECT_EQ(0, deletes); |
| 656 own_object_cb.Reset(); | 671 own_object_cb.Reset(); |
| 657 EXPECT_EQ(1, deletes); | 672 EXPECT_EQ(1, deletes); |
| 658 } | 673 } |
| 659 | 674 |
| 675 // PassScopedPtr() wrapper support. |
| 676 // - Binding PassScopedPtr gives Callback Ownership. |
| 677 // - Callback only transfers ownership of PassScopedPtr on explicit Pass() |
| 678 // or ToScopedPtr() call. |
| 679 TEST_F(BindTest, PassScopedPtr) { |
| 680 int deletes = 0; |
| 681 |
| 682 // If we never invoke the Callback, it returns ownership and deletes. |
| 683 Callback<DeleteCounter*(bool, bool)> unused_callback = |
| 684 Bind(&MaybeCapture, |
| 685 MakePassScopedPtr(new DeleteCounter(&deletes)).PassForBind()); |
| 686 EXPECT_EQ(0, deletes); |
| 687 unused_callback.Reset(); |
| 688 EXPECT_EQ(1, deletes); |
| 689 |
| 690 // Check that ownership is only transfered if ToScopedPtr() is called. |
| 691 deletes = 0; |
| 692 DeleteCounter* counter = new DeleteCounter(&deletes); |
| 693 EXPECT_EQ(0, deletes); |
| 694 Callback<DeleteCounter*(bool, bool)> callback = |
| 695 Bind(&MaybeCapture, MakePassScopedPtr(counter).PassForBind()); |
| 696 EXPECT_EQ(0, deletes); |
| 697 |
| 698 // Multiple calls do not capture. |
| 699 ASSERT_TRUE(callback.Run(true, false) == NULL); |
| 700 ASSERT_TRUE(callback.Run(true, false) == NULL); |
| 701 EXPECT_EQ(0, deletes); |
| 702 |
| 703 // Transfer ownership out. |
| 704 scoped_ptr<DeleteCounter> result(callback.Run(true, true)); |
| 705 ASSERT_EQ(counter, result.get()); |
| 706 EXPECT_EQ(0, deletes); |
| 707 |
| 708 // Next invocation will find the PassScopedPtr is invalid. |
| 709 ASSERT_TRUE(callback.Run(false, false) == NULL); |
| 710 EXPECT_EQ(0, deletes); |
| 711 |
| 712 // Resetting does not delete since ownership was transfered. |
| 713 callback.Reset(); |
| 714 EXPECT_EQ(0, deletes); |
| 715 } |
| 716 |
| 660 // Argument Copy-constructor usage for non-reference parameters. | 717 // Argument Copy-constructor usage for non-reference parameters. |
| 661 // - Bound arguments are only copied once. | 718 // - Bound arguments are only copied once. |
| 662 // - Forwarded arguments are only copied once. | 719 // - Forwarded arguments are only copied once. |
| 663 // - Forwarded arguments with coerscions are only copied twice (once for the | 720 // - Forwarded arguments with coerscions are only copied twice (once for the |
| 664 // coerscion, and one for the final dispatch). | 721 // coerscion, and one for the final dispatch). |
| 665 TEST_F(BindTest, ArgumentCopies) { | 722 TEST_F(BindTest, ArgumentCopies) { |
| 666 int copies = 0; | 723 int copies = 0; |
| 667 int assigns = 0; | 724 int assigns = 0; |
| 668 | 725 |
| 669 CopyCounter counter(&copies, &assigns); | 726 CopyCounter counter(&copies, &assigns); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 770 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
| 714 EXPECT_EQ(1, fastcall_cb.Run()); | 771 EXPECT_EQ(1, fastcall_cb.Run()); |
| 715 | 772 |
| 716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 773 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
| 717 EXPECT_EQ(2, stdcall_cb.Run()); | 774 EXPECT_EQ(2, stdcall_cb.Run()); |
| 718 } | 775 } |
| 719 #endif | 776 #endif |
| 720 | 777 |
| 721 } // namespace | 778 } // namespace |
| 722 } // namespace base | 779 } // namespace base |
| OLD | NEW |