Index: media/base/run_on_destruction_unittest.cc |
diff --git a/media/base/run_on_destruction_unittest.cc b/media/base/run_on_destruction_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c212bb3efac46aa0d58462f3b37032569d2017fe |
--- /dev/null |
+++ b/media/base/run_on_destruction_unittest.cc |
@@ -0,0 +1,203 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/run_on_destruction.h" |
+ |
+#include <memory> |
+#include <utility> |
+ |
+#include "base/memory/free_deleter.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/synchronization/waitable_event.h" |
+#include "base/threading/thread.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace media { |
+ |
+namespace { |
+ |
+void BoundBoolSet(bool* var, bool val) { |
+ *var = val; |
+} |
+ |
+void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { |
+ *a_var = a_val; |
+ *b_var = b_val; |
+} |
+ |
+/* |
+void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { |
+ *var = *val; |
+} |
+ |
+void BoundBoolSetFromScopedPtrFreeDeleter( |
+ bool* var, |
+ std::unique_ptr<bool, base::FreeDeleter> val) { |
+ *var = *val; |
+} |
+ |
+void BoundBoolSetFromScopedArray(bool* var, std::unique_ptr<bool[]> val) { |
+ *var = val[0]; |
+} |
+ |
+void BoundBoolSetFromConstRef(bool* var, const bool& val) { |
+ *var = val; |
+} |
+ |
+ |
+void RunAndClearReference(base::Closure cb) { |
+ cb.Run(); |
+} |
+ |
+void ClearReference(base::Closure cb) {} |
+ |
+*/ |
+ |
+} // namespace |
+ |
+TEST(RunOnDestructionTest, Closure_Run) { |
+ bool bool_var = false; |
+ base::OnceClosure cb = RunOnDestruction(base::Bind( |
+ &BoundBoolSet, &bool_var, true)); |
+ std::move(cb).Run(); |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+TEST(RunOnDestructionTest, Closure_Destruction) { |
+ bool bool_var = false; |
+ { |
+ base::OnceClosure cb = |
+ RunOnDestruction(base::Bind(&BoundBoolSet, &bool_var, true)); |
+ } |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+TEST(RunOnDestructionTest, Bool_Run) { |
+ bool bool_var = false; |
+ base::OnceCallback<void(bool)> cb = RunOnDestruction(base::Bind( |
+ &BoundBoolSet, &bool_var), false); |
+ std::move(cb).Run(true); |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+TEST(RunOnDestructionTest, Bool_Destruction) { |
+ bool bool_var = false; |
+ { |
+ base::OnceCallback<void(bool)> cb = |
+ RunOnDestruction(base::Bind(&BoundBoolSet, &bool_var), true); |
+ } |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+TEST(RunOnDestructionTest, Integers_Run) { |
+ int a = 0; |
+ int b = 0; |
+ base::OnceCallback<void(int, int)> cb = RunOnDestruction(base::Bind( |
+ &BoundIntegersSet, &a, &b), 3, 4); |
+ std::move(cb).Run(1, 2); |
+ EXPECT_EQ(a, 1); |
+ EXPECT_EQ(b, 2); |
+} |
+ |
+TEST(RunOnDestructionTest, Integers_Destruction) { |
+ int a = 0; |
+ int b = 0; |
+ { |
+ base::OnceCallback<void(int, int)> cb = |
+ RunOnDestruction(base::Bind(&BoundIntegersSet, &a, &b), 3, 4); |
+ } |
+ EXPECT_EQ(a, 3); |
+ EXPECT_EQ(b, 4); |
+} |
+ |
+/* |
+TEST(RunOnDestructionTest, BoundScopedPtrBool) { |
+ bool bool_val = false; |
+ std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
+ base::Closure cb = RunOnDestruction(base::Bind( |
+ &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
+ cb.Run(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST(RunOnDestructionTest, PassedScopedPtrBool) { |
+ bool bool_val = false; |
+ std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
+ base::Callback<void(std::unique_ptr<bool>)> cb = |
+ RunOnDestruction(base::Bind(&BoundBoolSetFromScopedPtr, &bool_val)); |
+ cb.Run(std::move(scoped_ptr_bool)); |
+ EXPECT_FALSE(bool_val); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST(RunOnDestructionTest, BoundScopedArrayBool) { |
+ bool bool_val = false; |
+ std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
+ scoped_array_bool[0] = true; |
+ base::Closure cb = RunOnDestruction(base::Bind( |
+ &BoundBoolSetFromScopedArray, &bool_val, |
+ base::Passed(&scoped_array_bool))); |
+ cb.Run(); |
+ EXPECT_FALSE(bool_val); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST(RunOnDestructionTest, PassedScopedArrayBool) { |
+ bool bool_val = false; |
+ std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
+ scoped_array_bool[0] = true; |
+ base::Callback<void(std::unique_ptr<bool[]>)> cb = |
+ RunOnDestruction(base::Bind(&BoundBoolSetFromScopedArray, &bool_val)); |
+ cb.Run(std::move(scoped_array_bool)); |
+ EXPECT_FALSE(bool_val); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST(RunOnDestructionTest, BoundScopedPtrFreeDeleterBool) { |
+ bool bool_val = false; |
+ std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
+ static_cast<bool*>(malloc(sizeof(bool)))); |
+ *scoped_ptr_free_deleter_bool = true; |
+ base::Closure cb = RunOnDestruction(base::Bind( |
+ &BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, |
+ base::Passed(&scoped_ptr_free_deleter_bool))); |
+ cb.Run(); |
+ EXPECT_FALSE(bool_val); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST(RunOnDestructionTest, PassedScopedPtrFreeDeleterBool) { |
+ bool bool_val = false; |
+ std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
+ static_cast<bool*>(malloc(sizeof(bool)))); |
+ *scoped_ptr_free_deleter_bool = true; |
+ base::Callback<void(std::unique_ptr<bool, base::FreeDeleter>)> cb = |
+ RunOnDestruction( |
+ base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val)); |
+ cb.Run(std::move(scoped_ptr_free_deleter_bool)); |
+ EXPECT_FALSE(bool_val); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST(RunOnDestructionTest, BoolConstRef) { |
+ bool bool_var = false; |
+ bool true_var = true; |
+ const bool& true_ref = true_var; |
+ base::Closure cb = RunOnDestruction(base::Bind( |
+ &BoundBoolSetFromConstRef, &bool_var, true_ref)); |
+ cb.Run(); |
+ EXPECT_FALSE(bool_var); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+*/ |
+ |
+} // namespace media |