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" | |
11 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
13 | 10 |
14 using ::testing::Mock; | 11 using ::testing::Mock; |
15 using ::testing::Return; | 12 using ::testing::Return; |
16 using ::testing::StrictMock; | 13 using ::testing::StrictMock; |
17 | 14 |
18 namespace base { | 15 namespace base { |
19 namespace { | 16 namespace { |
20 | 17 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 CopyCounter(int* copies, int* assigns) | 95 CopyCounter(int* copies, int* assigns) |
99 : copies_(copies), assigns_(assigns) { | 96 : copies_(copies), assigns_(assigns) { |
100 } | 97 } |
101 | 98 |
102 CopyCounter(const CopyCounter& other) | 99 CopyCounter(const CopyCounter& other) |
103 : copies_(other.copies_), | 100 : copies_(other.copies_), |
104 assigns_(other.assigns_) { | 101 assigns_(other.assigns_) { |
105 (*copies_)++; | 102 (*copies_)++; |
106 } | 103 } |
107 | 104 |
108 // Probing for copies from coercion. | 105 // Probing for copies from coerscion. |
109 CopyCounter(const DerivedCopyCounter& other) | 106 CopyCounter(const DerivedCopyCounter& other) |
110 : copies_(other.copies_), | 107 : copies_(other.copies_), |
111 assigns_(other.assigns_) { | 108 assigns_(other.assigns_) { |
112 (*copies_)++; | 109 (*copies_)++; |
113 } | 110 } |
114 | 111 |
115 const CopyCounter& operator=(const CopyCounter& rhs) { | 112 const CopyCounter& operator=(const CopyCounter& rhs) { |
116 copies_ = rhs.copies_; | 113 copies_ = rhs.copies_; |
117 assigns_ = rhs.assigns_; | 114 assigns_ = rhs.assigns_; |
118 | 115 |
(...skipping 26 matching lines...) Expand all Loading... |
145 ~DeleteCounter() { | 142 ~DeleteCounter() { |
146 (*deletes_)++; | 143 (*deletes_)++; |
147 } | 144 } |
148 | 145 |
149 void VoidMethod0() {} | 146 void VoidMethod0() {} |
150 | 147 |
151 private: | 148 private: |
152 int* deletes_; | 149 int* deletes_; |
153 }; | 150 }; |
154 | 151 |
155 template <typename T> | |
156 T PassThru(T scoper) { | |
157 return scoper.Pass(); | |
158 } | |
159 | |
160 // Some test functions that we can Bind to. | 152 // Some test functions that we can Bind to. |
161 template <typename T> | 153 template <typename T> |
162 T PolymorphicIdentity(T t) { | 154 T PolymorphicIdentity(T t) { |
163 return t; | 155 return t; |
164 } | 156 } |
165 | 157 |
166 template <typename T> | 158 template <typename T> |
167 void VoidPolymorphic1(T t) { | 159 void VoidPolymorphic1(T t) { |
168 } | 160 } |
169 | 161 |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 | 661 |
670 // Test Owned() support. | 662 // Test Owned() support. |
671 TEST_F(BindTest, Owned) { | 663 TEST_F(BindTest, Owned) { |
672 int deletes = 0; | 664 int deletes = 0; |
673 DeleteCounter* counter = new DeleteCounter(&deletes); | 665 DeleteCounter* counter = new DeleteCounter(&deletes); |
674 | 666 |
675 // If we don't capture, delete happens on Callback destruction/reset. | 667 // If we don't capture, delete happens on Callback destruction/reset. |
676 // return the same value. | 668 // return the same value. |
677 Callback<DeleteCounter*(void)> no_capture_cb = | 669 Callback<DeleteCounter*(void)> no_capture_cb = |
678 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); | 670 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
679 ASSERT_EQ(counter, no_capture_cb.Run()); | 671 EXPECT_EQ(counter, no_capture_cb.Run()); |
680 ASSERT_EQ(counter, no_capture_cb.Run()); | 672 EXPECT_EQ(counter, no_capture_cb.Run()); |
681 EXPECT_EQ(0, deletes); | 673 EXPECT_EQ(0, deletes); |
682 no_capture_cb.Reset(); // This should trigger a delete. | 674 no_capture_cb.Reset(); // This should trigger a delete. |
683 EXPECT_EQ(1, deletes); | 675 EXPECT_EQ(1, deletes); |
684 | 676 |
685 deletes = 0; | 677 deletes = 0; |
686 counter = new DeleteCounter(&deletes); | 678 counter = new DeleteCounter(&deletes); |
687 base::Closure own_object_cb = | 679 base::Closure own_object_cb = |
688 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); | 680 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
689 own_object_cb.Run(); | 681 own_object_cb.Run(); |
690 EXPECT_EQ(0, deletes); | 682 EXPECT_EQ(0, deletes); |
691 own_object_cb.Reset(); | 683 own_object_cb.Reset(); |
692 EXPECT_EQ(1, deletes); | 684 EXPECT_EQ(1, deletes); |
693 } | 685 } |
694 | 686 |
695 // Passed() wrapper support. | |
696 // - Passed() can be constructed from a pointer to scoper. | |
697 // - Passed() can be constructed from a scoper rvalue. | |
698 // - Using Passed() gives Callback Ownership. | |
699 // - Ownership is transferred from Callback to callee on the first Run(). | |
700 // - Callback supports unbound arguments. | |
701 TEST_F(BindTest, ScopedPtr) { | |
702 int deletes = 0; | |
703 | |
704 // Tests the Passed() function's support for pointers. | |
705 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes)); | |
706 Callback<scoped_ptr<DeleteCounter>(void)> unused_callback = | |
707 Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr)); | |
708 EXPECT_FALSE(ptr.get()); | |
709 EXPECT_EQ(0, deletes); | |
710 | |
711 // If we never invoke the Callback, it retains ownership and deletes. | |
712 unused_callback.Reset(); | |
713 EXPECT_EQ(1, deletes); | |
714 | |
715 // Tests the Passed() function's support for rvalues. | |
716 deletes = 0; | |
717 DeleteCounter* counter = new DeleteCounter(&deletes); | |
718 Callback<scoped_ptr<DeleteCounter>(void)> callback = | |
719 Bind(&PassThru<scoped_ptr<DeleteCounter> >, | |
720 Passed(scoped_ptr<DeleteCounter>(counter))); | |
721 EXPECT_FALSE(ptr.get()); | |
722 EXPECT_EQ(0, deletes); | |
723 | |
724 // Check that ownership can be transferred back out. | |
725 scoped_ptr<DeleteCounter> result = callback.Run(); | |
726 ASSERT_EQ(counter, result.get()); | |
727 EXPECT_EQ(0, deletes); | |
728 | |
729 // Resetting does not delete since ownership was transferred. | |
730 callback.Reset(); | |
731 EXPECT_EQ(0, deletes); | |
732 | |
733 // Ensure that we actually did get ownership. | |
734 result.reset(); | |
735 EXPECT_EQ(1, deletes); | |
736 | |
737 // Test unbound argument forwarding. | |
738 Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound = | |
739 Bind(&PassThru<scoped_ptr<DeleteCounter> >); | |
740 ptr.reset(new DeleteCounter(&deletes)); | |
741 cb_unbound.Run(ptr.Pass()); | |
742 } | |
743 | |
744 // Argument Copy-constructor usage for non-reference parameters. | 687 // Argument Copy-constructor usage for non-reference parameters. |
745 // - Bound arguments are only copied once. | 688 // - Bound arguments are only copied once. |
746 // - Forwarded arguments are only copied once. | 689 // - Forwarded arguments are only copied once. |
747 // - Forwarded arguments with coercions are only copied twice (once for the | 690 // - Forwarded arguments with coerscions are only copied twice (once for the |
748 // coercion, and one for the final dispatch). | 691 // coerscion, and one for the final dispatch). |
749 TEST_F(BindTest, ArgumentCopies) { | 692 TEST_F(BindTest, ArgumentCopies) { |
750 int copies = 0; | 693 int copies = 0; |
751 int assigns = 0; | 694 int assigns = 0; |
752 | 695 |
753 CopyCounter counter(&copies, &assigns); | 696 CopyCounter counter(&copies, &assigns); |
754 | 697 |
755 Callback<void(void)> copy_cb = | 698 Callback<void(void)> copy_cb = |
756 Bind(&VoidPolymorphic1<CopyCounter>, counter); | 699 Bind(&VoidPolymorphic1<CopyCounter>, counter); |
757 EXPECT_GE(1, copies); | 700 EXPECT_GE(1, copies); |
758 EXPECT_EQ(0, assigns); | 701 EXPECT_EQ(0, assigns); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 740 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
798 EXPECT_EQ(1, fastcall_cb.Run()); | 741 EXPECT_EQ(1, fastcall_cb.Run()); |
799 | 742 |
800 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 743 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
801 EXPECT_EQ(2, stdcall_cb.Run()); | 744 EXPECT_EQ(2, stdcall_cb.Run()); |
802 } | 745 } |
803 #endif | 746 #endif |
804 | 747 |
805 } // namespace | 748 } // namespace |
806 } // namespace base | 749 } // namespace base |
OLD | NEW |