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 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 | 1062 |
1063 int x = 1; | 1063 int x = 1; |
1064 base::Callback<void(int)> cb = | 1064 base::Callback<void(int)> cb = |
1065 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); | 1065 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); |
1066 cb.Run(6); | 1066 cb.Run(6); |
1067 EXPECT_EQ(6, x); | 1067 EXPECT_EQ(6, x); |
1068 cb.Run(7); | 1068 cb.Run(7); |
1069 EXPECT_EQ(42, x); | 1069 EXPECT_EQ(42, x); |
1070 } | 1070 } |
1071 | 1071 |
| 1072 TEST_F(BindTest, OneShotCallback) { |
| 1073 using internal::RepeatingClosure; |
| 1074 using internal::OneShotClosure; |
| 1075 using internal::BindOneShot; |
| 1076 using internal::BindRepeating; |
| 1077 using internal::OneShotCallback; |
| 1078 |
| 1079 // Check if Callback variants have declarations of conversions as expected. |
| 1080 // Copy constructor and assignment of RepeatingCallback. |
| 1081 EXPECT_TRUE((std::is_constructible< |
| 1082 RepeatingClosure, const RepeatingClosure&>::value)); |
| 1083 EXPECT_TRUE((is_assignable< |
| 1084 RepeatingClosure, const RepeatingClosure&>::value)); |
| 1085 |
| 1086 // Move constructor and assignment of RepeatingCallback. |
| 1087 EXPECT_TRUE((std::is_constructible< |
| 1088 RepeatingClosure, RepeatingClosure&&>::value)); |
| 1089 EXPECT_TRUE((is_assignable< |
| 1090 RepeatingClosure, RepeatingClosure&&>::value)); |
| 1091 |
| 1092 // Conversions from OneShotCallback to RepeatingCallback. |
| 1093 EXPECT_FALSE((std::is_constructible< |
| 1094 RepeatingClosure, const OneShotClosure&>::value)); |
| 1095 EXPECT_FALSE((is_assignable< |
| 1096 RepeatingClosure, const OneShotClosure&>::value)); |
| 1097 |
| 1098 // Destructive conversions from OneShotCallback to RepeatingCallback. |
| 1099 EXPECT_FALSE((std::is_constructible< |
| 1100 RepeatingClosure, OneShotClosure&&>::value)); |
| 1101 EXPECT_FALSE((is_assignable< |
| 1102 RepeatingClosure, OneShotClosure&&>::value)); |
| 1103 |
| 1104 // Copy constructor and assignment of OneShotCallback. |
| 1105 EXPECT_FALSE((std::is_constructible< |
| 1106 OneShotClosure, const OneShotClosure&>::value)); |
| 1107 EXPECT_FALSE((is_assignable< |
| 1108 OneShotClosure, const OneShotClosure&>::value)); |
| 1109 |
| 1110 // Move constructor and assignment of OneShotCallback. |
| 1111 EXPECT_TRUE((std::is_constructible< |
| 1112 OneShotClosure, OneShotClosure&&>::value)); |
| 1113 EXPECT_TRUE((is_assignable< |
| 1114 OneShotClosure, OneShotClosure&&>::value)); |
| 1115 |
| 1116 // Conversions from RepeatingCallback to OneShotCallback. |
| 1117 EXPECT_TRUE((std::is_constructible< |
| 1118 OneShotClosure, const RepeatingClosure&>::value)); |
| 1119 EXPECT_TRUE((is_assignable< |
| 1120 OneShotClosure, const RepeatingClosure&>::value)); |
| 1121 |
| 1122 // Destructive conversions from RepeatingCallback to OneShotCallback. |
| 1123 EXPECT_TRUE((std::is_constructible< |
| 1124 OneShotClosure, RepeatingClosure&&>::value)); |
| 1125 EXPECT_TRUE((is_assignable< |
| 1126 OneShotClosure, RepeatingClosure&&>::value)); |
| 1127 |
| 1128 OneShotClosure cb = BindOneShot(&VoidPolymorphic<>::Run); |
| 1129 std::move(cb).Run(); |
| 1130 |
| 1131 // RepeatingCallback should be convertible to OneShotCallback. |
| 1132 OneShotClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1133 std::move(cb2).Run(); |
| 1134 |
| 1135 RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1136 cb = cb3; |
| 1137 std::move(cb).Run(); |
| 1138 |
| 1139 cb = std::move(cb2); |
| 1140 |
| 1141 OneShotCallback<void(int)> cb4 = BindOneShot( |
| 1142 &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0)); |
| 1143 BindOneShot(std::move(cb4), 1).Run(); |
| 1144 } |
| 1145 |
1072 // Callback construction and assignment tests. | 1146 // Callback construction and assignment tests. |
1073 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 1147 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
1074 // - Assignment from other callback should only cause one ref | 1148 // - Assignment from other callback should only cause one ref |
1075 // | 1149 // |
1076 // TODO(ajwong): Is there actually a way to test this? | 1150 // TODO(ajwong): Is there actually a way to test this? |
1077 | 1151 |
1078 #if defined(OS_WIN) | 1152 #if defined(OS_WIN) |
1079 int __fastcall FastCallFunc(int n) { | 1153 int __fastcall FastCallFunc(int n) { |
1080 return n; | 1154 return n; |
1081 } | 1155 } |
(...skipping 16 matching lines...) Expand all Loading... |
1098 | 1172 |
1099 // Test null callbacks cause a DCHECK. | 1173 // Test null callbacks cause a DCHECK. |
1100 TEST(BindDeathTest, NullCallback) { | 1174 TEST(BindDeathTest, NullCallback) { |
1101 base::Callback<void(int)> null_cb; | 1175 base::Callback<void(int)> null_cb; |
1102 ASSERT_TRUE(null_cb.is_null()); | 1176 ASSERT_TRUE(null_cb.is_null()); |
1103 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); | 1177 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); |
1104 } | 1178 } |
1105 | 1179 |
1106 } // namespace | 1180 } // namespace |
1107 } // namespace base | 1181 } // namespace base |
OLD | NEW |