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 |
(...skipping 1016 matching lines...) Loading... |
1027 | 1027 |
1028 int x = 1; | 1028 int x = 1; |
1029 base::Callback<void(int)> cb = | 1029 base::Callback<void(int)> cb = |
1030 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); | 1030 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); |
1031 cb.Run(6); | 1031 cb.Run(6); |
1032 EXPECT_EQ(6, x); | 1032 EXPECT_EQ(6, x); |
1033 cb.Run(7); | 1033 cb.Run(7); |
1034 EXPECT_EQ(42, x); | 1034 EXPECT_EQ(42, x); |
1035 } | 1035 } |
1036 | 1036 |
| 1037 TEST_F(BindTest, OnceCallback) { |
| 1038 using internal::RepeatingClosure; |
| 1039 using internal::OnceClosure; |
| 1040 using internal::BindOnce; |
| 1041 using internal::BindRepeating; |
| 1042 using internal::OnceCallback; |
| 1043 |
| 1044 // Check if Callback variants have declarations of conversions as expected. |
| 1045 // Copy constructor and assignment of RepeatingCallback. |
| 1046 static_assert(std::is_constructible< |
| 1047 RepeatingClosure, const RepeatingClosure&>::value, |
| 1048 "RepeatingClosure should be copyable."); |
| 1049 static_assert(is_assignable< |
| 1050 RepeatingClosure, const RepeatingClosure&>::value, |
| 1051 "RepeatingClosure should be copy-assignable."); |
| 1052 |
| 1053 // Move constructor and assignment of RepeatingCallback. |
| 1054 static_assert(std::is_constructible< |
| 1055 RepeatingClosure, RepeatingClosure&&>::value, |
| 1056 "RepeatingClosure should be movable."); |
| 1057 static_assert(is_assignable< |
| 1058 RepeatingClosure, RepeatingClosure&&>::value, |
| 1059 "RepeatingClosure should be move-assignable"); |
| 1060 |
| 1061 // Conversions from OnceCallback to RepeatingCallback. |
| 1062 static_assert(!std::is_constructible< |
| 1063 RepeatingClosure, const OnceClosure&>::value, |
| 1064 "OnceClosure should not be convertible to RepeatingClosure."); |
| 1065 static_assert(!is_assignable< |
| 1066 RepeatingClosure, const OnceClosure&>::value, |
| 1067 "OnceClosure should not be convertible to RepeatingClosure."); |
| 1068 |
| 1069 // Destructive conversions from OnceCallback to RepeatingCallback. |
| 1070 static_assert(!std::is_constructible< |
| 1071 RepeatingClosure, OnceClosure&&>::value, |
| 1072 "OnceClosure should not be convertible to RepeatingClosure."); |
| 1073 static_assert(!is_assignable< |
| 1074 RepeatingClosure, OnceClosure&&>::value, |
| 1075 "OnceClosure should not be convertible to RepeatingClosure."); |
| 1076 |
| 1077 // Copy constructor and assignment of OnceCallback. |
| 1078 static_assert(!std::is_constructible< |
| 1079 OnceClosure, const OnceClosure&>::value, |
| 1080 "OnceClosure should not be copyable."); |
| 1081 static_assert(!is_assignable< |
| 1082 OnceClosure, const OnceClosure&>::value, |
| 1083 "OnceClosure should not be copy-assignable"); |
| 1084 |
| 1085 // Move constructor and assignment of OnceCallback. |
| 1086 static_assert(std::is_constructible< |
| 1087 OnceClosure, OnceClosure&&>::value, |
| 1088 "OnceClosure should be movable."); |
| 1089 static_assert(is_assignable< |
| 1090 OnceClosure, OnceClosure&&>::value, |
| 1091 "OnceClosure should be move-assignable."); |
| 1092 |
| 1093 // Conversions from RepeatingCallback to OnceCallback. |
| 1094 static_assert(std::is_constructible< |
| 1095 OnceClosure, const RepeatingClosure&>::value, |
| 1096 "RepeatingClosure should be convertible to OnceClosure."); |
| 1097 static_assert(is_assignable< |
| 1098 OnceClosure, const RepeatingClosure&>::value, |
| 1099 "RepeatingClosure should be convertible to OnceClosure."); |
| 1100 |
| 1101 // Destructive conversions from RepeatingCallback to OnceCallback. |
| 1102 static_assert(std::is_constructible< |
| 1103 OnceClosure, RepeatingClosure&&>::value, |
| 1104 "RepeatingClosure should be convertible to OnceClosure."); |
| 1105 static_assert(is_assignable< |
| 1106 OnceClosure, RepeatingClosure&&>::value, |
| 1107 "RepeatingClosure should be covretible to OnceClosure."); |
| 1108 |
| 1109 OnceClosure cb = BindOnce(&VoidPolymorphic<>::Run); |
| 1110 std::move(cb).Run(); |
| 1111 |
| 1112 // RepeatingCallback should be convertible to OnceCallback. |
| 1113 OnceClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1114 std::move(cb2).Run(); |
| 1115 |
| 1116 RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1117 cb = cb3; |
| 1118 std::move(cb).Run(); |
| 1119 |
| 1120 cb = std::move(cb2); |
| 1121 |
| 1122 OnceCallback<void(int)> cb4 = BindOnce( |
| 1123 &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0)); |
| 1124 BindOnce(std::move(cb4), 1).Run(); |
| 1125 } |
| 1126 |
1037 // Callback construction and assignment tests. | 1127 // Callback construction and assignment tests. |
1038 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 1128 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
1039 // - Assignment from other callback should only cause one ref | 1129 // - Assignment from other callback should only cause one ref |
1040 // | 1130 // |
1041 // TODO(ajwong): Is there actually a way to test this? | 1131 // TODO(ajwong): Is there actually a way to test this? |
1042 | 1132 |
1043 #if defined(OS_WIN) | 1133 #if defined(OS_WIN) |
1044 int __fastcall FastCallFunc(int n) { | 1134 int __fastcall FastCallFunc(int n) { |
1045 return n; | 1135 return n; |
1046 } | 1136 } |
(...skipping 16 matching lines...) Loading... |
1063 | 1153 |
1064 // Test null callbacks cause a DCHECK. | 1154 // Test null callbacks cause a DCHECK. |
1065 TEST(BindDeathTest, NullCallback) { | 1155 TEST(BindDeathTest, NullCallback) { |
1066 base::Callback<void(int)> null_cb; | 1156 base::Callback<void(int)> null_cb; |
1067 ASSERT_TRUE(null_cb.is_null()); | 1157 ASSERT_TRUE(null_cb.is_null()); |
1068 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); | 1158 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); |
1069 } | 1159 } |
1070 | 1160 |
1071 } // namespace | 1161 } // namespace |
1072 } // namespace base | 1162 } // namespace base |
OLD | NEW |