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/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.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 template <typename T> |
| 156 T PassThru(T scoper) { |
| 157 return scoper.Pass(); |
| 158 } |
| 159 |
152 // Some test functions that we can Bind to. | 160 // Some test functions that we can Bind to. |
153 template <typename T> | 161 template <typename T> |
154 T PolymorphicIdentity(T t) { | 162 T PolymorphicIdentity(T t) { |
155 return t; | 163 return t; |
156 } | 164 } |
157 | 165 |
158 template <typename T> | 166 template <typename T> |
159 void VoidPolymorphic1(T t) { | 167 void VoidPolymorphic1(T t) { |
160 } | 168 } |
161 | 169 |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 | 642 |
635 // Test Owned() support. | 643 // Test Owned() support. |
636 TEST_F(BindTest, Owned) { | 644 TEST_F(BindTest, Owned) { |
637 int deletes = 0; | 645 int deletes = 0; |
638 DeleteCounter* counter = new DeleteCounter(&deletes); | 646 DeleteCounter* counter = new DeleteCounter(&deletes); |
639 | 647 |
640 // If we don't capture, delete happens on Callback destruction/reset. | 648 // If we don't capture, delete happens on Callback destruction/reset. |
641 // return the same value. | 649 // return the same value. |
642 Callback<DeleteCounter*(void)> no_capture_cb = | 650 Callback<DeleteCounter*(void)> no_capture_cb = |
643 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); | 651 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
644 EXPECT_EQ(counter, no_capture_cb.Run()); | 652 ASSERT_EQ(counter, no_capture_cb.Run()); |
645 EXPECT_EQ(counter, no_capture_cb.Run()); | 653 ASSERT_EQ(counter, no_capture_cb.Run()); |
646 EXPECT_EQ(0, deletes); | 654 EXPECT_EQ(0, deletes); |
647 no_capture_cb.Reset(); // This should trigger a delete. | 655 no_capture_cb.Reset(); // This should trigger a delete. |
648 EXPECT_EQ(1, deletes); | 656 EXPECT_EQ(1, deletes); |
649 | 657 |
650 deletes = 0; | 658 deletes = 0; |
651 counter = new DeleteCounter(&deletes); | 659 counter = new DeleteCounter(&deletes); |
652 base::Closure own_object_cb = | 660 base::Closure own_object_cb = |
653 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); | 661 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
654 own_object_cb.Run(); | 662 own_object_cb.Run(); |
655 EXPECT_EQ(0, deletes); | 663 EXPECT_EQ(0, deletes); |
656 own_object_cb.Reset(); | 664 own_object_cb.Reset(); |
657 EXPECT_EQ(1, deletes); | 665 EXPECT_EQ(1, deletes); |
658 } | 666 } |
659 | 667 |
| 668 // PassScoped() wrapper support. |
| 669 // - Using PassScoped() gives Callback Ownership. |
| 670 // - Ownership is transfered from Callback to callee on the first Run(). |
| 671 TEST_F(BindTest, ScopedPtr) { |
| 672 int deletes = 0; |
| 673 |
| 674 // If we never invoke the Callback, it returns ownership and deletes. |
| 675 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes)); |
| 676 Callback<scoped_ptr<DeleteCounter>(void)> unused_callback = |
| 677 Bind(&PassThru<scoped_ptr<DeleteCounter> >, PassScoped(&ptr)); |
| 678 EXPECT_EQ(0, deletes); |
| 679 unused_callback.Reset(); |
| 680 EXPECT_EQ(1, deletes); |
| 681 |
| 682 // Check that ownership can be transfered back out. |
| 683 deletes = 0; |
| 684 DeleteCounter* counter = new DeleteCounter(&deletes); |
| 685 ptr.reset(counter); |
| 686 Callback<scoped_ptr<DeleteCounter>(void)> callback = |
| 687 Bind(&PassThru<scoped_ptr<DeleteCounter> >, PassScoped(&ptr)); |
| 688 EXPECT_EQ(0, deletes); |
| 689 |
| 690 scoped_ptr<DeleteCounter> result = callback.Run(); |
| 691 ASSERT_EQ(counter, result.get()); |
| 692 EXPECT_EQ(0, deletes); |
| 693 |
| 694 // Resetting does not delete since ownership was transfered. |
| 695 callback.Reset(); |
| 696 EXPECT_EQ(0, deletes); |
| 697 |
| 698 // Ensure that we actually did get ownership. |
| 699 result.reset(); |
| 700 EXPECT_EQ(1, deletes); |
| 701 } |
| 702 |
660 // Argument Copy-constructor usage for non-reference parameters. | 703 // Argument Copy-constructor usage for non-reference parameters. |
661 // - Bound arguments are only copied once. | 704 // - Bound arguments are only copied once. |
662 // - Forwarded arguments are only copied once. | 705 // - Forwarded arguments are only copied once. |
663 // - Forwarded arguments with coerscions are only copied twice (once for the | 706 // - Forwarded arguments with coerscions are only copied twice (once for the |
664 // coerscion, and one for the final dispatch). | 707 // coerscion, and one for the final dispatch). |
665 TEST_F(BindTest, ArgumentCopies) { | 708 TEST_F(BindTest, ArgumentCopies) { |
666 int copies = 0; | 709 int copies = 0; |
667 int assigns = 0; | 710 int assigns = 0; |
668 | 711 |
669 CopyCounter counter(&copies, &assigns); | 712 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); | 756 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
714 EXPECT_EQ(1, fastcall_cb.Run()); | 757 EXPECT_EQ(1, fastcall_cb.Run()); |
715 | 758 |
716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 759 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
717 EXPECT_EQ(2, stdcall_cb.Run()); | 760 EXPECT_EQ(2, stdcall_cb.Run()); |
718 } | 761 } |
719 #endif | 762 #endif |
720 | 763 |
721 } // namespace | 764 } // namespace |
722 } // namespace base | 765 } // namespace base |
OLD | NEW |