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

Unified Diff: base/bind_unittest.cc

Issue 2042223002: Introduce OnceClosure and BindOnce (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update docs Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: base/bind_unittest.cc
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index 1e3540bf7e7019623af97f931a8cc870891f725d..68a18996c00959ff80ac235523cfe1d6e4872351 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -1069,6 +1069,80 @@ TEST_F(BindTest, CapturelessLambda) {
EXPECT_EQ(42, x);
}
+TEST_F(BindTest, OneShotCallback) {
+ using internal::RepeatingClosure;
+ using internal::OneShotClosure;
+ using internal::BindOneShot;
+ using internal::BindRepeating;
+ using internal::OneShotCallback;
+
+ // Check if Callback variants have declarations of conversions as expected.
+ // Copy constructor and assignment of RepeatingCallback.
+ EXPECT_TRUE((std::is_constructible<
+ RepeatingClosure, const RepeatingClosure&>::value));
+ EXPECT_TRUE((is_assignable<
+ RepeatingClosure, const RepeatingClosure&>::value));
+
+ // Move constructor and assignment of RepeatingCallback.
+ EXPECT_TRUE((std::is_constructible<
+ RepeatingClosure, RepeatingClosure&&>::value));
+ EXPECT_TRUE((is_assignable<
+ RepeatingClosure, RepeatingClosure&&>::value));
+
+ // Conversions from OneShotCallback to RepeatingCallback.
+ EXPECT_FALSE((std::is_constructible<
+ RepeatingClosure, const OneShotClosure&>::value));
+ EXPECT_FALSE((is_assignable<
+ RepeatingClosure, const OneShotClosure&>::value));
+
+ // Destructive conversions from OneShotCallback to RepeatingCallback.
+ EXPECT_FALSE((std::is_constructible<
+ RepeatingClosure, OneShotClosure&&>::value));
+ EXPECT_FALSE((is_assignable<
+ RepeatingClosure, OneShotClosure&&>::value));
+
+ // Copy constructor and assignment of OneShotCallback.
+ EXPECT_FALSE((std::is_constructible<
+ OneShotClosure, const OneShotClosure&>::value));
+ EXPECT_FALSE((is_assignable<
+ OneShotClosure, const OneShotClosure&>::value));
+
+ // Move constructor and assignment of OneShotCallback.
+ EXPECT_TRUE((std::is_constructible<
+ OneShotClosure, OneShotClosure&&>::value));
+ EXPECT_TRUE((is_assignable<
+ OneShotClosure, OneShotClosure&&>::value));
+
+ // Conversions from RepeatingCallback to OneShotCallback.
+ EXPECT_TRUE((std::is_constructible<
+ OneShotClosure, const RepeatingClosure&>::value));
+ EXPECT_TRUE((is_assignable<
+ OneShotClosure, const RepeatingClosure&>::value));
+
+ // Destructive conversions from RepeatingCallback to OneShotCallback.
+ EXPECT_TRUE((std::is_constructible<
+ OneShotClosure, RepeatingClosure&&>::value));
+ EXPECT_TRUE((is_assignable<
+ OneShotClosure, RepeatingClosure&&>::value));
+
+ OneShotClosure cb = BindOneShot(&VoidPolymorphic<>::Run);
+ std::move(cb).Run();
+
+ // RepeatingCallback should be convertible to OneShotCallback.
+ OneShotClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run);
+ std::move(cb2).Run();
+
+ RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run);
+ cb = cb3;
+ std::move(cb).Run();
+
+ cb = std::move(cb2);
+
+ OneShotCallback<void(int)> cb4 = BindOneShot(
+ &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0));
+ BindOneShot(std::move(cb4), 1).Run();
+}
+
// Callback construction and assignment tests.
// - Construction from an InvokerStorageHolder should not cause ref/deref.
// - Assignment from other callback should only cause one ref

Powered by Google App Engine
This is Rietveld 408576698