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 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 | 634 |
643 // Test Owned() support. | 635 // Test Owned() support. |
644 TEST_F(BindTest, Owned) { | 636 TEST_F(BindTest, Owned) { |
645 int deletes = 0; | 637 int deletes = 0; |
646 DeleteCounter* counter = new DeleteCounter(&deletes); | 638 DeleteCounter* counter = new DeleteCounter(&deletes); |
647 | 639 |
648 // If we don't capture, delete happens on Callback destruction/reset. | 640 // If we don't capture, delete happens on Callback destruction/reset. |
649 // return the same value. | 641 // return the same value. |
650 Callback<DeleteCounter*(void)> no_capture_cb = | 642 Callback<DeleteCounter*(void)> no_capture_cb = |
651 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); | 643 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
652 ASSERT_EQ(counter, no_capture_cb.Run()); | 644 EXPECT_EQ(counter, no_capture_cb.Run()); |
653 ASSERT_EQ(counter, no_capture_cb.Run()); | 645 EXPECT_EQ(counter, no_capture_cb.Run()); |
654 EXPECT_EQ(0, deletes); | 646 EXPECT_EQ(0, deletes); |
655 no_capture_cb.Reset(); // This should trigger a delete. | 647 no_capture_cb.Reset(); // This should trigger a delete. |
656 EXPECT_EQ(1, deletes); | 648 EXPECT_EQ(1, deletes); |
657 | 649 |
658 deletes = 0; | 650 deletes = 0; |
659 counter = new DeleteCounter(&deletes); | 651 counter = new DeleteCounter(&deletes); |
660 base::Closure own_object_cb = | 652 base::Closure own_object_cb = |
661 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); | 653 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
662 own_object_cb.Run(); | 654 own_object_cb.Run(); |
663 EXPECT_EQ(0, deletes); | 655 EXPECT_EQ(0, deletes); |
664 own_object_cb.Reset(); | 656 own_object_cb.Reset(); |
665 EXPECT_EQ(1, deletes); | 657 EXPECT_EQ(1, deletes); |
666 } | 658 } |
667 | 659 |
668 // Passed() wrapper support. | |
669 // - Passed() can be constructed from a pointer to scoper. | |
670 // - Passed() can be constructed from a scoper rvalue. | |
671 // - Using Passed() gives Callback Ownership. | |
672 // - Ownership is transferred from Callback to callee on the first Run(). | |
673 // - Callback supports unbound arguments. | |
674 TEST_F(BindTest, ScopedPtr) { | |
675 int deletes = 0; | |
676 | |
677 // Tests the Passed() function's support for pointers. | |
678 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes)); | |
679 Callback<scoped_ptr<DeleteCounter>(void)> unused_callback = | |
680 Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr)); | |
681 EXPECT_FALSE(ptr.get()); | |
682 EXPECT_EQ(0, deletes); | |
683 | |
684 // If we never invoke the Callback, it retains ownership and deletes. | |
685 unused_callback.Reset(); | |
686 EXPECT_EQ(1, deletes); | |
687 | |
688 // Tests the Passed() function's support for rvalues. | |
689 deletes = 0; | |
690 DeleteCounter* counter = new DeleteCounter(&deletes); | |
691 Callback<scoped_ptr<DeleteCounter>(void)> callback = | |
692 Bind(&PassThru<scoped_ptr<DeleteCounter> >, | |
693 Passed(scoped_ptr<DeleteCounter>(counter))); | |
694 EXPECT_FALSE(ptr.get()); | |
695 EXPECT_EQ(0, deletes); | |
696 | |
697 // Check that ownership can be transferred back out. | |
698 scoped_ptr<DeleteCounter> result = callback.Run(); | |
699 ASSERT_EQ(counter, result.get()); | |
700 EXPECT_EQ(0, deletes); | |
701 | |
702 // Resetting does not delete since ownership was transferred. | |
703 callback.Reset(); | |
704 EXPECT_EQ(0, deletes); | |
705 | |
706 // Ensure that we actually did get ownership. | |
707 result.reset(); | |
708 EXPECT_EQ(1, deletes); | |
709 | |
710 // Test unbound argument forwarding. | |
711 Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound = | |
712 Bind(&PassThru<scoped_ptr<DeleteCounter> >); | |
713 ptr.reset(new DeleteCounter(&deletes)); | |
714 cb_unbound.Run(ptr.Pass()); | |
715 } | |
716 | |
717 // Argument Copy-constructor usage for non-reference parameters. | 660 // Argument Copy-constructor usage for non-reference parameters. |
718 // - Bound arguments are only copied once. | 661 // - Bound arguments are only copied once. |
719 // - Forwarded arguments are only copied once. | 662 // - Forwarded arguments are only copied once. |
720 // - Forwarded arguments with coercions are only copied twice (once for the | 663 // - Forwarded arguments with coerscions are only copied twice (once for the |
721 // coercion, and one for the final dispatch). | 664 // coerscion, and one for the final dispatch). |
722 TEST_F(BindTest, ArgumentCopies) { | 665 TEST_F(BindTest, ArgumentCopies) { |
723 int copies = 0; | 666 int copies = 0; |
724 int assigns = 0; | 667 int assigns = 0; |
725 | 668 |
726 CopyCounter counter(&copies, &assigns); | 669 CopyCounter counter(&copies, &assigns); |
727 | 670 |
728 Callback<void(void)> copy_cb = | 671 Callback<void(void)> copy_cb = |
729 Bind(&VoidPolymorphic1<CopyCounter>, counter); | 672 Bind(&VoidPolymorphic1<CopyCounter>, counter); |
730 EXPECT_GE(1, copies); | 673 EXPECT_GE(1, copies); |
731 EXPECT_EQ(0, assigns); | 674 EXPECT_EQ(0, assigns); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 713 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
771 EXPECT_EQ(1, fastcall_cb.Run()); | 714 EXPECT_EQ(1, fastcall_cb.Run()); |
772 | 715 |
773 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
774 EXPECT_EQ(2, stdcall_cb.Run()); | 717 EXPECT_EQ(2, stdcall_cb.Run()); |
775 } | 718 } |
776 #endif | 719 #endif |
777 | 720 |
778 } // namespace | 721 } // namespace |
779 } // namespace base | 722 } // namespace base |
OLD | NEW |