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 "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 int assigns() const { | 127 int assigns() const { |
128 return *assigns_; | 128 return *assigns_; |
129 } | 129 } |
130 | 130 |
131 private: | 131 private: |
132 int* copies_; | 132 int* copies_; |
133 int* assigns_; | 133 int* assigns_; |
134 }; | 134 }; |
135 | 135 |
| 136 class DeleteCounter { |
| 137 public: |
| 138 explicit DeleteCounter(int* deletes) |
| 139 : deletes_(deletes) { |
| 140 } |
| 141 |
| 142 ~DeleteCounter() { |
| 143 (*deletes_)++; |
| 144 } |
| 145 |
| 146 void VoidMethod0() {} |
| 147 |
| 148 private: |
| 149 int* deletes_; |
| 150 }; |
| 151 |
136 // Some test functions that we can Bind to. | 152 // Some test functions that we can Bind to. |
137 template <typename T> | 153 template <typename T> |
138 T PolymorphicIdentity(T t) { | 154 T PolymorphicIdentity(T t) { |
139 return t; | 155 return t; |
140 } | 156 } |
141 | 157 |
142 template <typename T> | 158 template <typename T> |
143 void VoidPolymorphic1(T t) { | 159 void VoidPolymorphic1(T t) { |
144 } | 160 } |
145 | 161 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 196 } |
181 | 197 |
182 void RefArgSet(int &n) { | 198 void RefArgSet(int &n) { |
183 n = 2; | 199 n = 2; |
184 } | 200 } |
185 | 201 |
186 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { | 202 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
187 return n; | 203 return n; |
188 } | 204 } |
189 | 205 |
190 // Only useful in no-compile tests. | |
191 int UnwrapNoRefParentRef(Parent& p) { | |
192 return p.value; | |
193 } | |
194 | |
195 class BindTest : public ::testing::Test { | 206 class BindTest : public ::testing::Test { |
196 public: | 207 public: |
197 BindTest() { | 208 BindTest() { |
198 const_has_ref_ptr_ = &has_ref_; | 209 const_has_ref_ptr_ = &has_ref_; |
199 const_no_ref_ptr_ = &no_ref_; | 210 const_no_ref_ptr_ = &no_ref_; |
200 static_func_mock_ptr = &static_func_mock_; | 211 static_func_mock_ptr = &static_func_mock_; |
201 } | 212 } |
202 | 213 |
203 virtual ~BindTest() { | 214 virtual ~BindTest() { |
204 } | 215 } |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 int copies = 0; | 599 int copies = 0; |
589 int assigns = 0; | 600 int assigns = 0; |
590 CopyCounter counter(&copies, &assigns); | 601 CopyCounter counter(&copies, &assigns); |
591 Callback<int(void)> all_const_ref_cb = | 602 Callback<int(void)> all_const_ref_cb = |
592 Bind(&GetCopies, ConstRef(counter)); | 603 Bind(&GetCopies, ConstRef(counter)); |
593 EXPECT_EQ(0, all_const_ref_cb.Run()); | 604 EXPECT_EQ(0, all_const_ref_cb.Run()); |
594 EXPECT_EQ(0, copies); | 605 EXPECT_EQ(0, copies); |
595 EXPECT_EQ(0, assigns); | 606 EXPECT_EQ(0, assigns); |
596 } | 607 } |
597 | 608 |
| 609 // Test Owned() support. |
| 610 TEST_F(BindTest, Owned) { |
| 611 int deletes = 0; |
| 612 DeleteCounter* counter = new DeleteCounter(&deletes); |
| 613 |
| 614 // If we don't capture, delete happens on Callback destruction/reset. |
| 615 // return the same value. |
| 616 Callback<DeleteCounter*(void)> no_capture_cb = |
| 617 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
| 618 EXPECT_EQ(counter, no_capture_cb.Run()); |
| 619 EXPECT_EQ(counter, no_capture_cb.Run()); |
| 620 EXPECT_EQ(0, deletes); |
| 621 no_capture_cb.Reset(); // This should trigger a delete. |
| 622 EXPECT_EQ(1, deletes); |
| 623 |
| 624 deletes = 0; |
| 625 counter = new DeleteCounter(&deletes); |
| 626 base::Closure own_object_cb = |
| 627 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
| 628 own_object_cb.Run(); |
| 629 EXPECT_EQ(0, deletes); |
| 630 own_object_cb.Reset(); |
| 631 EXPECT_EQ(1, deletes); |
| 632 } |
| 633 |
598 // Argument Copy-constructor usage for non-reference parameters. | 634 // Argument Copy-constructor usage for non-reference parameters. |
599 // - Bound arguments are only copied once. | 635 // - Bound arguments are only copied once. |
600 // - Forwarded arguments are only copied once. | 636 // - Forwarded arguments are only copied once. |
601 // - Forwarded arguments with coerscions are only copied twice (once for the | 637 // - Forwarded arguments with coerscions are only copied twice (once for the |
602 // coerscion, and one for the final dispatch). | 638 // coerscion, and one for the final dispatch). |
603 TEST_F(BindTest, ArgumentCopies) { | 639 TEST_F(BindTest, ArgumentCopies) { |
604 int copies = 0; | 640 int copies = 0; |
605 int assigns = 0; | 641 int assigns = 0; |
606 | 642 |
607 CopyCounter counter(&copies, &assigns); | 643 CopyCounter counter(&copies, &assigns); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 687 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
652 EXPECT_EQ(1, fastcall_cb.Run()); | 688 EXPECT_EQ(1, fastcall_cb.Run()); |
653 | 689 |
654 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 690 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
655 EXPECT_EQ(2, stdcall_cb.Run()); | 691 EXPECT_EQ(2, stdcall_cb.Run()); |
656 } | 692 } |
657 #endif | 693 #endif |
658 | 694 |
659 } // namespace | 695 } // namespace |
660 } // namespace base | 696 } // namespace base |
OLD | NEW |