OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/run_on_destruction.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/memory/free_deleter.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace media { |
| 18 |
| 19 namespace { |
| 20 |
| 21 void BoundBoolSet(bool* var, bool val) { |
| 22 *var = val; |
| 23 } |
| 24 |
| 25 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { |
| 26 *a_var = a_val; |
| 27 *b_var = b_val; |
| 28 } |
| 29 |
| 30 /* |
| 31 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { |
| 32 *var = *val; |
| 33 } |
| 34 |
| 35 void BoundBoolSetFromScopedPtrFreeDeleter( |
| 36 bool* var, |
| 37 std::unique_ptr<bool, base::FreeDeleter> val) { |
| 38 *var = *val; |
| 39 } |
| 40 |
| 41 void BoundBoolSetFromScopedArray(bool* var, std::unique_ptr<bool[]> val) { |
| 42 *var = val[0]; |
| 43 } |
| 44 |
| 45 void BoundBoolSetFromConstRef(bool* var, const bool& val) { |
| 46 *var = val; |
| 47 } |
| 48 |
| 49 |
| 50 void RunAndClearReference(base::Closure cb) { |
| 51 cb.Run(); |
| 52 } |
| 53 |
| 54 void ClearReference(base::Closure cb) {} |
| 55 |
| 56 */ |
| 57 |
| 58 } // namespace |
| 59 |
| 60 TEST(RunOnDestructionTest, Closure_Run) { |
| 61 bool bool_var = false; |
| 62 base::OnceClosure cb = RunOnDestruction(base::Bind( |
| 63 &BoundBoolSet, &bool_var, true)); |
| 64 std::move(cb).Run(); |
| 65 EXPECT_TRUE(bool_var); |
| 66 } |
| 67 |
| 68 TEST(RunOnDestructionTest, Closure_Destruction) { |
| 69 bool bool_var = false; |
| 70 { |
| 71 base::OnceClosure cb = |
| 72 RunOnDestruction(base::Bind(&BoundBoolSet, &bool_var, true)); |
| 73 } |
| 74 EXPECT_TRUE(bool_var); |
| 75 } |
| 76 |
| 77 TEST(RunOnDestructionTest, Bool_Run) { |
| 78 bool bool_var = false; |
| 79 base::OnceCallback<void(bool)> cb = RunOnDestruction(base::Bind( |
| 80 &BoundBoolSet, &bool_var), false); |
| 81 std::move(cb).Run(true); |
| 82 EXPECT_TRUE(bool_var); |
| 83 } |
| 84 |
| 85 TEST(RunOnDestructionTest, Bool_Destruction) { |
| 86 bool bool_var = false; |
| 87 { |
| 88 base::OnceCallback<void(bool)> cb = |
| 89 RunOnDestruction(base::Bind(&BoundBoolSet, &bool_var), true); |
| 90 } |
| 91 EXPECT_TRUE(bool_var); |
| 92 } |
| 93 |
| 94 TEST(RunOnDestructionTest, Integers_Run) { |
| 95 int a = 0; |
| 96 int b = 0; |
| 97 base::OnceCallback<void(int, int)> cb = RunOnDestruction(base::Bind( |
| 98 &BoundIntegersSet, &a, &b), 3, 4); |
| 99 std::move(cb).Run(1, 2); |
| 100 EXPECT_EQ(a, 1); |
| 101 EXPECT_EQ(b, 2); |
| 102 } |
| 103 |
| 104 TEST(RunOnDestructionTest, Integers_Destruction) { |
| 105 int a = 0; |
| 106 int b = 0; |
| 107 { |
| 108 base::OnceCallback<void(int, int)> cb = |
| 109 RunOnDestruction(base::Bind(&BoundIntegersSet, &a, &b), 3, 4); |
| 110 } |
| 111 EXPECT_EQ(a, 3); |
| 112 EXPECT_EQ(b, 4); |
| 113 } |
| 114 |
| 115 /* |
| 116 TEST(RunOnDestructionTest, BoundScopedPtrBool) { |
| 117 bool bool_val = false; |
| 118 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
| 119 base::Closure cb = RunOnDestruction(base::Bind( |
| 120 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
| 121 cb.Run(); |
| 122 EXPECT_TRUE(bool_val); |
| 123 } |
| 124 |
| 125 TEST(RunOnDestructionTest, PassedScopedPtrBool) { |
| 126 bool bool_val = false; |
| 127 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
| 128 base::Callback<void(std::unique_ptr<bool>)> cb = |
| 129 RunOnDestruction(base::Bind(&BoundBoolSetFromScopedPtr, &bool_val)); |
| 130 cb.Run(std::move(scoped_ptr_bool)); |
| 131 EXPECT_FALSE(bool_val); |
| 132 base::RunLoop().RunUntilIdle(); |
| 133 EXPECT_TRUE(bool_val); |
| 134 } |
| 135 |
| 136 TEST(RunOnDestructionTest, BoundScopedArrayBool) { |
| 137 bool bool_val = false; |
| 138 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
| 139 scoped_array_bool[0] = true; |
| 140 base::Closure cb = RunOnDestruction(base::Bind( |
| 141 &BoundBoolSetFromScopedArray, &bool_val, |
| 142 base::Passed(&scoped_array_bool))); |
| 143 cb.Run(); |
| 144 EXPECT_FALSE(bool_val); |
| 145 base::RunLoop().RunUntilIdle(); |
| 146 EXPECT_TRUE(bool_val); |
| 147 } |
| 148 |
| 149 TEST(RunOnDestructionTest, PassedScopedArrayBool) { |
| 150 bool bool_val = false; |
| 151 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
| 152 scoped_array_bool[0] = true; |
| 153 base::Callback<void(std::unique_ptr<bool[]>)> cb = |
| 154 RunOnDestruction(base::Bind(&BoundBoolSetFromScopedArray, &bool_val)); |
| 155 cb.Run(std::move(scoped_array_bool)); |
| 156 EXPECT_FALSE(bool_val); |
| 157 base::RunLoop().RunUntilIdle(); |
| 158 EXPECT_TRUE(bool_val); |
| 159 } |
| 160 |
| 161 TEST(RunOnDestructionTest, BoundScopedPtrFreeDeleterBool) { |
| 162 bool bool_val = false; |
| 163 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
| 164 static_cast<bool*>(malloc(sizeof(bool)))); |
| 165 *scoped_ptr_free_deleter_bool = true; |
| 166 base::Closure cb = RunOnDestruction(base::Bind( |
| 167 &BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, |
| 168 base::Passed(&scoped_ptr_free_deleter_bool))); |
| 169 cb.Run(); |
| 170 EXPECT_FALSE(bool_val); |
| 171 base::RunLoop().RunUntilIdle(); |
| 172 EXPECT_TRUE(bool_val); |
| 173 } |
| 174 |
| 175 TEST(RunOnDestructionTest, PassedScopedPtrFreeDeleterBool) { |
| 176 bool bool_val = false; |
| 177 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
| 178 static_cast<bool*>(malloc(sizeof(bool)))); |
| 179 *scoped_ptr_free_deleter_bool = true; |
| 180 base::Callback<void(std::unique_ptr<bool, base::FreeDeleter>)> cb = |
| 181 RunOnDestruction( |
| 182 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val)); |
| 183 cb.Run(std::move(scoped_ptr_free_deleter_bool)); |
| 184 EXPECT_FALSE(bool_val); |
| 185 base::RunLoop().RunUntilIdle(); |
| 186 EXPECT_TRUE(bool_val); |
| 187 } |
| 188 |
| 189 TEST(RunOnDestructionTest, BoolConstRef) { |
| 190 bool bool_var = false; |
| 191 bool true_var = true; |
| 192 const bool& true_ref = true_var; |
| 193 base::Closure cb = RunOnDestruction(base::Bind( |
| 194 &BoundBoolSetFromConstRef, &bool_var, true_ref)); |
| 195 cb.Run(); |
| 196 EXPECT_FALSE(bool_var); |
| 197 base::RunLoop().RunUntilIdle(); |
| 198 EXPECT_TRUE(bool_var); |
| 199 } |
| 200 |
| 201 */ |
| 202 |
| 203 } // namespace media |
OLD | NEW |