Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: base/bind_unittest.cc

Issue 2042223002: Introduce OnceClosure and BindOnce (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: swap Once & Repeating positions Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/bind_internal.h ('k') | base/callback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1038 matching lines...) Expand 10 before | Expand all | Expand 10 after
1049 1049
1050 weak_factory.InvalidateWeakPtrs(); 1050 weak_factory.InvalidateWeakPtrs();
1051 1051
1052 EXPECT_TRUE(cb.IsCancelled()); 1052 EXPECT_TRUE(cb.IsCancelled());
1053 EXPECT_TRUE(cb2.IsCancelled()); 1053 EXPECT_TRUE(cb2.IsCancelled());
1054 1054
1055 cb.Run(); 1055 cb.Run();
1056 cb2.Run(); 1056 cb2.Run();
1057 } 1057 }
1058 1058
1059 TEST_F(BindTest, OnceCallback) {
1060 using internal::OnceClosure;
1061 using internal::RepeatingClosure;
1062 using internal::BindOnce;
1063 using internal::BindRepeating;
1064 using internal::OnceCallback;
1065
1066 // Check if Callback variants have declarations of conversions as expected.
1067 // Copy constructor and assignment of RepeatingCallback.
1068 static_assert(std::is_constructible<
1069 RepeatingClosure, const RepeatingClosure&>::value,
1070 "RepeatingClosure should be copyable.");
1071 static_assert(is_assignable<
1072 RepeatingClosure, const RepeatingClosure&>::value,
1073 "RepeatingClosure should be copy-assignable.");
1074
1075 // Move constructor and assignment of RepeatingCallback.
1076 static_assert(std::is_constructible<
1077 RepeatingClosure, RepeatingClosure&&>::value,
1078 "RepeatingClosure should be movable.");
1079 static_assert(is_assignable<
1080 RepeatingClosure, RepeatingClosure&&>::value,
1081 "RepeatingClosure should be move-assignable");
1082
1083 // Conversions from OnceCallback to RepeatingCallback.
1084 static_assert(!std::is_constructible<
1085 RepeatingClosure, const OnceClosure&>::value,
1086 "OnceClosure should not be convertible to RepeatingClosure.");
1087 static_assert(!is_assignable<
1088 RepeatingClosure, const OnceClosure&>::value,
1089 "OnceClosure should not be convertible to RepeatingClosure.");
1090
1091 // Destructive conversions from OnceCallback to RepeatingCallback.
1092 static_assert(!std::is_constructible<
1093 RepeatingClosure, OnceClosure&&>::value,
1094 "OnceClosure should not be convertible to RepeatingClosure.");
1095 static_assert(!is_assignable<
1096 RepeatingClosure, OnceClosure&&>::value,
1097 "OnceClosure should not be convertible to RepeatingClosure.");
1098
1099 // Copy constructor and assignment of OnceCallback.
1100 static_assert(!std::is_constructible<
1101 OnceClosure, const OnceClosure&>::value,
1102 "OnceClosure should not be copyable.");
1103 static_assert(!is_assignable<
1104 OnceClosure, const OnceClosure&>::value,
1105 "OnceClosure should not be copy-assignable");
1106
1107 // Move constructor and assignment of OnceCallback.
1108 static_assert(std::is_constructible<
1109 OnceClosure, OnceClosure&&>::value,
1110 "OnceClosure should be movable.");
1111 static_assert(is_assignable<
1112 OnceClosure, OnceClosure&&>::value,
1113 "OnceClosure should be move-assignable.");
1114
1115 // Conversions from RepeatingCallback to OnceCallback.
1116 static_assert(std::is_constructible<
1117 OnceClosure, const RepeatingClosure&>::value,
1118 "RepeatingClosure should be convertible to OnceClosure.");
1119 static_assert(is_assignable<
1120 OnceClosure, const RepeatingClosure&>::value,
1121 "RepeatingClosure should be convertible to OnceClosure.");
1122
1123 // Destructive conversions from RepeatingCallback to OnceCallback.
1124 static_assert(std::is_constructible<
1125 OnceClosure, RepeatingClosure&&>::value,
1126 "RepeatingClosure should be convertible to OnceClosure.");
1127 static_assert(is_assignable<
1128 OnceClosure, RepeatingClosure&&>::value,
1129 "RepeatingClosure should be covretible to OnceClosure.");
1130
1131 OnceClosure cb = BindOnce(&VoidPolymorphic<>::Run);
1132 std::move(cb).Run();
1133
1134 // RepeatingCallback should be convertible to OnceCallback.
1135 OnceClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run);
1136 std::move(cb2).Run();
1137
1138 RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run);
1139 cb = cb3;
1140 std::move(cb).Run();
1141
1142 cb = std::move(cb2);
1143
1144 OnceCallback<void(int)> cb4 = BindOnce(
1145 &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0));
1146 BindOnce(std::move(cb4), 1).Run();
1147 }
1148
1059 // Callback construction and assignment tests. 1149 // Callback construction and assignment tests.
1060 // - Construction from an InvokerStorageHolder should not cause ref/deref. 1150 // - Construction from an InvokerStorageHolder should not cause ref/deref.
1061 // - Assignment from other callback should only cause one ref 1151 // - Assignment from other callback should only cause one ref
1062 // 1152 //
1063 // TODO(ajwong): Is there actually a way to test this? 1153 // TODO(ajwong): Is there actually a way to test this?
1064 1154
1065 #if defined(OS_WIN) 1155 #if defined(OS_WIN)
1066 int __fastcall FastCallFunc(int n) { 1156 int __fastcall FastCallFunc(int n) {
1067 return n; 1157 return n;
1068 } 1158 }
(...skipping 16 matching lines...) Expand all
1085 1175
1086 // Test null callbacks cause a DCHECK. 1176 // Test null callbacks cause a DCHECK.
1087 TEST(BindDeathTest, NullCallback) { 1177 TEST(BindDeathTest, NullCallback) {
1088 base::Callback<void(int)> null_cb; 1178 base::Callback<void(int)> null_cb;
1089 ASSERT_TRUE(null_cb.is_null()); 1179 ASSERT_TRUE(null_cb.is_null());
1090 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); 1180 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42));
1091 } 1181 }
1092 1182
1093 } // namespace 1183 } // namespace
1094 } // namespace base 1184 } // namespace base
OLDNEW
« no previous file with comments | « base/bind_internal.h ('k') | base/callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698