OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 using ::testing::Mock; | 20 using ::testing::Mock; |
21 using ::testing::Return; | 21 using ::testing::Return; |
22 using ::testing::StrictMock; | 22 using ::testing::StrictMock; |
23 | 23 |
24 namespace base { | 24 namespace base { |
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 // - Callback supports unbound arguments. | 810 // - Callback supports unbound arguments. |
811 template <typename T> | 811 template <typename T> |
812 class BindMoveOnlyTypeTest : public ::testing::Test { | 812 class BindMoveOnlyTypeTest : public ::testing::Test { |
813 }; | 813 }; |
814 | 814 |
815 struct CustomDeleter { | 815 struct CustomDeleter { |
816 void operator()(DeleteCounter* c) { delete c; } | 816 void operator()(DeleteCounter* c) { delete c; } |
817 }; | 817 }; |
818 | 818 |
819 using MoveOnlyTypesToTest = | 819 using MoveOnlyTypesToTest = |
820 ::testing::Types<scoped_ptr<DeleteCounter>, | 820 ::testing::Types<std::unique_ptr<DeleteCounter>, |
821 std::unique_ptr<DeleteCounter>, | 821 std::unique_ptr<DeleteCounter>, |
822 std::unique_ptr<DeleteCounter, CustomDeleter>>; | 822 std::unique_ptr<DeleteCounter, CustomDeleter>>; |
823 TYPED_TEST_CASE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest); | 823 TYPED_TEST_CASE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest); |
824 | 824 |
825 TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) { | 825 TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) { |
826 int deletes = 0; | 826 int deletes = 0; |
827 | 827 |
828 TypeParam ptr(new DeleteCounter(&deletes)); | 828 TypeParam ptr(new DeleteCounter(&deletes)); |
829 Callback<TypeParam()> callback = Bind(&PassThru<TypeParam>, Passed(&ptr)); | 829 Callback<TypeParam()> callback = Bind(&PassThru<TypeParam>, Passed(&ptr)); |
830 EXPECT_FALSE(ptr.get()); | 830 EXPECT_FALSE(ptr.get()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 | 867 |
868 TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) { | 868 TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) { |
869 int deletes = 0; | 869 int deletes = 0; |
870 TypeParam ptr(new DeleteCounter(&deletes)); | 870 TypeParam ptr(new DeleteCounter(&deletes)); |
871 // Test unbound argument forwarding. | 871 // Test unbound argument forwarding. |
872 Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>); | 872 Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>); |
873 cb_unbound.Run(std::move(ptr)); | 873 cb_unbound.Run(std::move(ptr)); |
874 EXPECT_EQ(1, deletes); | 874 EXPECT_EQ(1, deletes); |
875 } | 875 } |
876 | 876 |
877 void VerifyVector(const std::vector<scoped_ptr<int>>& v) { | 877 void VerifyVector(const std::vector<std::unique_ptr<int>>& v) { |
878 ASSERT_EQ(1u, v.size()); | 878 ASSERT_EQ(1u, v.size()); |
879 EXPECT_EQ(12345, *v[0]); | 879 EXPECT_EQ(12345, *v[0]); |
880 } | 880 } |
881 | 881 |
882 std::vector<scoped_ptr<int>> AcceptAndReturnMoveOnlyVector( | 882 std::vector<std::unique_ptr<int>> AcceptAndReturnMoveOnlyVector( |
883 std::vector<scoped_ptr<int>> v) { | 883 std::vector<std::unique_ptr<int>> v) { |
884 VerifyVector(v); | 884 VerifyVector(v); |
885 return v; | 885 return v; |
886 } | 886 } |
887 | 887 |
888 // Test that a vector containing move-only types can be used with Callback. | 888 // Test that a vector containing move-only types can be used with Callback. |
889 TEST_F(BindTest, BindMoveOnlyVector) { | 889 TEST_F(BindTest, BindMoveOnlyVector) { |
890 using MoveOnlyVector = std::vector<scoped_ptr<int>>; | 890 using MoveOnlyVector = std::vector<std::unique_ptr<int>>; |
891 | 891 |
892 MoveOnlyVector v; | 892 MoveOnlyVector v; |
893 v.push_back(make_scoped_ptr(new int(12345))); | 893 v.push_back(WrapUnique(new int(12345))); |
894 | 894 |
895 // Early binding should work: | 895 // Early binding should work: |
896 base::Callback<MoveOnlyVector()> bound_cb = | 896 base::Callback<MoveOnlyVector()> bound_cb = |
897 base::Bind(&AcceptAndReturnMoveOnlyVector, Passed(&v)); | 897 base::Bind(&AcceptAndReturnMoveOnlyVector, Passed(&v)); |
898 MoveOnlyVector intermediate_result = bound_cb.Run(); | 898 MoveOnlyVector intermediate_result = bound_cb.Run(); |
899 VerifyVector(intermediate_result); | 899 VerifyVector(intermediate_result); |
900 | 900 |
901 // As should passing it as an argument to Run(): | 901 // As should passing it as an argument to Run(): |
902 base::Callback<MoveOnlyVector(MoveOnlyVector)> unbound_cb = | 902 base::Callback<MoveOnlyVector(MoveOnlyVector)> unbound_cb = |
903 base::Bind(&AcceptAndReturnMoveOnlyVector); | 903 base::Bind(&AcceptAndReturnMoveOnlyVector); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1093 base::Callback<void(int)> null_cb; | 1093 base::Callback<void(int)> null_cb; |
1094 ASSERT_TRUE(null_cb.is_null()); | 1094 ASSERT_TRUE(null_cb.is_null()); |
1095 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 1095 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
1096 } | 1096 } |
1097 | 1097 |
1098 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 1098 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
1099 // GTEST_HAS_DEATH_TEST | 1099 // GTEST_HAS_DEATH_TEST |
1100 | 1100 |
1101 } // namespace | 1101 } // namespace |
1102 } // namespace base | 1102 } // namespace base |
OLD | NEW |