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

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: s/EXPECT_{TRUE,FALSE}/static_assert/. +comments. simplify IsConvertibleCallbacks 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
« no previous file with comments | « base/bind_internal.h ('k') | base/callback.h » ('j') | base/callback.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/bind_unittest.cc
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index ba0358e5699cba33fdb202551e4f1204d95042c5..3b9afa1a42f49439f1c6e55fa94e45d72e9194f9 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -1034,6 +1034,96 @@ TEST_F(BindTest, CapturelessLambda) {
EXPECT_EQ(42, x);
}
+TEST_F(BindTest, OnceCallback) {
+ using internal::RepeatingClosure;
+ using internal::OnceClosure;
+ using internal::BindOnce;
+ using internal::BindRepeating;
+ using internal::OnceCallback;
+
+ // Check if Callback variants have declarations of conversions as expected.
+ // Copy constructor and assignment of RepeatingCallback.
+ static_assert(std::is_constructible<
+ RepeatingClosure, const RepeatingClosure&>::value,
+ "RepeatingClosure should be copyable.");
+ static_assert(is_assignable<
+ RepeatingClosure, const RepeatingClosure&>::value,
+ "RepeatingClosure should be copy-assignable.");
+
+ // Move constructor and assignment of RepeatingCallback.
+ static_assert(std::is_constructible<
+ RepeatingClosure, RepeatingClosure&&>::value,
+ "RepeatingClosure should be movable.");
+ static_assert(is_assignable<
+ RepeatingClosure, RepeatingClosure&&>::value,
+ "RepeatingClosure should be move-assignable");
+
+ // Conversions from OnceCallback to RepeatingCallback.
+ static_assert(!std::is_constructible<
+ RepeatingClosure, const OnceClosure&>::value,
+ "OnceClosure should not be convertible to RepeatingClosure.");
+ static_assert(!is_assignable<
+ RepeatingClosure, const OnceClosure&>::value,
+ "OnceClosure should not be convertible to RepeatingClosure.");
+
+ // Destructive conversions from OnceCallback to RepeatingCallback.
+ static_assert(!std::is_constructible<
+ RepeatingClosure, OnceClosure&&>::value,
+ "OnceClosure should not be convertible to RepeatingClosure.");
+ static_assert(!is_assignable<
+ RepeatingClosure, OnceClosure&&>::value,
+ "OnceClosure should not be convertible to RepeatingClosure.");
+
+ // Copy constructor and assignment of OnceCallback.
+ static_assert(!std::is_constructible<
+ OnceClosure, const OnceClosure&>::value,
+ "OnceClosure should not be copyable.");
+ static_assert(!is_assignable<
+ OnceClosure, const OnceClosure&>::value,
+ "OnceClosure should not be copy-assignable");
+
+ // Move constructor and assignment of OnceCallback.
+ static_assert(std::is_constructible<
+ OnceClosure, OnceClosure&&>::value,
+ "OnceClosure should be movable.");
+ static_assert(is_assignable<
+ OnceClosure, OnceClosure&&>::value,
+ "OnceClosure should be move-assignable.");
+
+ // Conversions from RepeatingCallback to OnceCallback.
+ static_assert(std::is_constructible<
+ OnceClosure, const RepeatingClosure&>::value,
+ "RepeatingClosure should be convertible to OnceClosure.");
+ static_assert(is_assignable<
+ OnceClosure, const RepeatingClosure&>::value,
+ "RepeatingClosure should be convertible to OnceClosure.");
+
+ // Destructive conversions from RepeatingCallback to OnceCallback.
+ static_assert(std::is_constructible<
+ OnceClosure, RepeatingClosure&&>::value,
+ "RepeatingClosure should be convertible to OnceClosure.");
+ static_assert(is_assignable<
+ OnceClosure, RepeatingClosure&&>::value,
+ "RepeatingClosure should be covretible to OnceClosure.");
+
+ OnceClosure cb = BindOnce(&VoidPolymorphic<>::Run);
+ std::move(cb).Run();
+
+ // RepeatingCallback should be convertible to OnceCallback.
+ OnceClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run);
+ std::move(cb2).Run();
+
+ RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run);
+ cb = cb3;
+ std::move(cb).Run();
+
+ cb = std::move(cb2);
+
+ OnceCallback<void(int)> cb4 = BindOnce(
+ &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0));
+ BindOnce(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
« no previous file with comments | « base/bind_internal.h ('k') | base/callback.h » ('j') | base/callback.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698