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(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 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
624 deletes = 0; | 639 deletes = 0; |
625 counter = new DeleteCounter(&deletes); | 640 counter = new DeleteCounter(&deletes); |
626 base::Closure own_object_cb = | 641 base::Closure own_object_cb = |
627 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); | 642 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
628 own_object_cb.Run(); | 643 own_object_cb.Run(); |
629 EXPECT_EQ(0, deletes); | 644 EXPECT_EQ(0, deletes); |
630 own_object_cb.Reset(); | 645 own_object_cb.Reset(); |
631 EXPECT_EQ(1, deletes); | 646 EXPECT_EQ(1, deletes); |
632 } | 647 } |
633 | 648 |
649 // PassScopedPtr() wrapper support. | |
650 // - Binding PassScopedPtr gives Callback Ownership. | |
651 // - Callback only transfers ownership of PassScopedPtr on explicit Pass() | |
652 // or ToScopedPtr() call. | |
653 TEST_F(BindTest, PassScopedPtr) { | |
654 int deletes = 0; | |
655 | |
656 // If we never invoke the Callback, it returns ownership and deletes. | |
657 Callback<DeleteCounter*(bool, bool)> unused_callback = | |
658 Bind(&MaybeCapture, | |
659 PassScopedPtr<DeleteCounter>(new DeleteCounter(&deletes)).Pass()); | |
660 EXPECT_EQ(0, deletes); | |
levin
2011/11/02 00:51:30
Shouldn't these EXPECT calls be ASSERT?
If someth
awong
2011/11/11 00:21:58
Good catch. Fixed.
| |
661 unused_callback.Reset(); | |
662 EXPECT_EQ(1, deletes); | |
663 | |
664 // TODO(ajwong): Also test that Pass() captures the value, and that | |
665 // after the value is captured, is_valid() is false. | |
666 | |
667 // Check that ownership is only transfered if ToScopedPtr() is called. | |
668 deletes = 0; | |
669 DeleteCounter* counter = new DeleteCounter(&deletes); | |
670 EXPECT_EQ(0, deletes); | |
671 Callback<DeleteCounter*(bool, bool)> callback = | |
672 Bind(&MaybeCapture, | |
673 PassScopedPtr<DeleteCounter>(counter).Pass()); | |
674 EXPECT_EQ(0, deletes); | |
675 | |
676 // Multiple calls do not capture. | |
677 // | |
678 // TODO(ajwong): Actually assert is_valid() inside the test. | |
679 EXPECT_TRUE(callback.Run(true, false) == NULL); | |
680 EXPECT_TRUE(callback.Run(true, false) == NULL); | |
681 EXPECT_EQ(0, deletes); | |
682 | |
683 // Transfer ownership out. | |
684 scoped_ptr<DeleteCounter> result(callback.Run(true, true)); | |
685 EXPECT_EQ(counter, result.get()); | |
686 EXPECT_EQ(0, deletes); | |
687 | |
688 // Next invocation will find the PassScopedPtr is invalid. | |
689 EXPECT_TRUE(callback.Run(false, false) == NULL); | |
690 EXPECT_EQ(0, deletes); | |
691 | |
692 // Resetting does not delete since ownership was transfered. | |
693 callback.Reset(); | |
694 EXPECT_EQ(0, deletes); | |
levin
2011/11/02 00:51:30
Why not "delete counter" here to avoid leaks in te
awong
2011/11/11 00:21:58
Don't need to. result owns it. There's no way to
| |
695 } | |
696 | |
634 // Argument Copy-constructor usage for non-reference parameters. | 697 // Argument Copy-constructor usage for non-reference parameters. |
635 // - Bound arguments are only copied once. | 698 // - Bound arguments are only copied once. |
636 // - Forwarded arguments are only copied once. | 699 // - Forwarded arguments are only copied once. |
637 // - Forwarded arguments with coerscions are only copied twice (once for the | 700 // - Forwarded arguments with coerscions are only copied twice (once for the |
638 // coerscion, and one for the final dispatch). | 701 // coerscion, and one for the final dispatch). |
639 TEST_F(BindTest, ArgumentCopies) { | 702 TEST_F(BindTest, ArgumentCopies) { |
640 int copies = 0; | 703 int copies = 0; |
641 int assigns = 0; | 704 int assigns = 0; |
642 | 705 |
643 CopyCounter counter(&copies, &assigns); | 706 CopyCounter counter(&copies, &assigns); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
687 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 750 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
688 EXPECT_EQ(1, fastcall_cb.Run()); | 751 EXPECT_EQ(1, fastcall_cb.Run()); |
689 | 752 |
690 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 753 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
691 EXPECT_EQ(2, stdcall_cb.Run()); | 754 EXPECT_EQ(2, stdcall_cb.Run()); |
692 } | 755 } |
693 #endif | 756 #endif |
694 | 757 |
695 } // namespace | 758 } // namespace |
696 } // namespace base | 759 } // namespace base |
OLD | NEW |