OLD | NEW |
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 "media/base/bind_to_current_loop.h" | 5 #include "media/base/bind_to_current_loop.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/free_deleter.h" | 10 #include "base/memory/free_deleter.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace media { | 16 namespace media { |
16 | 17 |
17 void BoundBoolSet(bool* var, bool val) { | 18 void BoundBoolSet(bool* var, bool val) { |
18 *var = val; | 19 *var = val; |
19 } | 20 } |
20 | 21 |
21 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { | 22 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { |
(...skipping 27 matching lines...) Expand all Loading... |
49 }; | 50 }; |
50 | 51 |
51 TEST_F(BindToCurrentLoopTest, Closure) { | 52 TEST_F(BindToCurrentLoopTest, Closure) { |
52 // Test the closure is run inside the loop, not outside it. | 53 // Test the closure is run inside the loop, not outside it. |
53 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 54 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
54 base::WaitableEvent::InitialState::NOT_SIGNALED); | 55 base::WaitableEvent::InitialState::NOT_SIGNALED); |
55 base::Closure cb = BindToCurrentLoop(base::Bind( | 56 base::Closure cb = BindToCurrentLoop(base::Bind( |
56 &base::WaitableEvent::Signal, base::Unretained(&waiter))); | 57 &base::WaitableEvent::Signal, base::Unretained(&waiter))); |
57 cb.Run(); | 58 cb.Run(); |
58 EXPECT_FALSE(waiter.IsSignaled()); | 59 EXPECT_FALSE(waiter.IsSignaled()); |
59 loop_.RunUntilIdle(); | 60 base::RunLoop().RunUntilIdle(); |
60 EXPECT_TRUE(waiter.IsSignaled()); | 61 EXPECT_TRUE(waiter.IsSignaled()); |
61 } | 62 } |
62 | 63 |
63 TEST_F(BindToCurrentLoopTest, Bool) { | 64 TEST_F(BindToCurrentLoopTest, Bool) { |
64 bool bool_var = false; | 65 bool bool_var = false; |
65 base::Callback<void(bool)> cb = BindToCurrentLoop(base::Bind( | 66 base::Callback<void(bool)> cb = BindToCurrentLoop(base::Bind( |
66 &BoundBoolSet, &bool_var)); | 67 &BoundBoolSet, &bool_var)); |
67 cb.Run(true); | 68 cb.Run(true); |
68 EXPECT_FALSE(bool_var); | 69 EXPECT_FALSE(bool_var); |
69 loop_.RunUntilIdle(); | 70 base::RunLoop().RunUntilIdle(); |
70 EXPECT_TRUE(bool_var); | 71 EXPECT_TRUE(bool_var); |
71 } | 72 } |
72 | 73 |
73 TEST_F(BindToCurrentLoopTest, BoundScopedPtrBool) { | 74 TEST_F(BindToCurrentLoopTest, BoundScopedPtrBool) { |
74 bool bool_val = false; | 75 bool bool_val = false; |
75 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); | 76 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
76 base::Closure cb = BindToCurrentLoop(base::Bind( | 77 base::Closure cb = BindToCurrentLoop(base::Bind( |
77 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); | 78 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
78 cb.Run(); | 79 cb.Run(); |
79 EXPECT_FALSE(bool_val); | 80 EXPECT_FALSE(bool_val); |
80 loop_.RunUntilIdle(); | 81 base::RunLoop().RunUntilIdle(); |
81 EXPECT_TRUE(bool_val); | 82 EXPECT_TRUE(bool_val); |
82 } | 83 } |
83 | 84 |
84 TEST_F(BindToCurrentLoopTest, PassedScopedPtrBool) { | 85 TEST_F(BindToCurrentLoopTest, PassedScopedPtrBool) { |
85 bool bool_val = false; | 86 bool bool_val = false; |
86 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); | 87 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
87 base::Callback<void(std::unique_ptr<bool>)> cb = | 88 base::Callback<void(std::unique_ptr<bool>)> cb = |
88 BindToCurrentLoop(base::Bind(&BoundBoolSetFromScopedPtr, &bool_val)); | 89 BindToCurrentLoop(base::Bind(&BoundBoolSetFromScopedPtr, &bool_val)); |
89 cb.Run(std::move(scoped_ptr_bool)); | 90 cb.Run(std::move(scoped_ptr_bool)); |
90 EXPECT_FALSE(bool_val); | 91 EXPECT_FALSE(bool_val); |
91 loop_.RunUntilIdle(); | 92 base::RunLoop().RunUntilIdle(); |
92 EXPECT_TRUE(bool_val); | 93 EXPECT_TRUE(bool_val); |
93 } | 94 } |
94 | 95 |
95 TEST_F(BindToCurrentLoopTest, BoundScopedArrayBool) { | 96 TEST_F(BindToCurrentLoopTest, BoundScopedArrayBool) { |
96 bool bool_val = false; | 97 bool bool_val = false; |
97 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); | 98 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
98 scoped_array_bool[0] = true; | 99 scoped_array_bool[0] = true; |
99 base::Closure cb = BindToCurrentLoop(base::Bind( | 100 base::Closure cb = BindToCurrentLoop(base::Bind( |
100 &BoundBoolSetFromScopedArray, &bool_val, | 101 &BoundBoolSetFromScopedArray, &bool_val, |
101 base::Passed(&scoped_array_bool))); | 102 base::Passed(&scoped_array_bool))); |
102 cb.Run(); | 103 cb.Run(); |
103 EXPECT_FALSE(bool_val); | 104 EXPECT_FALSE(bool_val); |
104 loop_.RunUntilIdle(); | 105 base::RunLoop().RunUntilIdle(); |
105 EXPECT_TRUE(bool_val); | 106 EXPECT_TRUE(bool_val); |
106 } | 107 } |
107 | 108 |
108 TEST_F(BindToCurrentLoopTest, PassedScopedArrayBool) { | 109 TEST_F(BindToCurrentLoopTest, PassedScopedArrayBool) { |
109 bool bool_val = false; | 110 bool bool_val = false; |
110 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); | 111 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
111 scoped_array_bool[0] = true; | 112 scoped_array_bool[0] = true; |
112 base::Callback<void(std::unique_ptr<bool[]>)> cb = | 113 base::Callback<void(std::unique_ptr<bool[]>)> cb = |
113 BindToCurrentLoop(base::Bind(&BoundBoolSetFromScopedArray, &bool_val)); | 114 BindToCurrentLoop(base::Bind(&BoundBoolSetFromScopedArray, &bool_val)); |
114 cb.Run(std::move(scoped_array_bool)); | 115 cb.Run(std::move(scoped_array_bool)); |
115 EXPECT_FALSE(bool_val); | 116 EXPECT_FALSE(bool_val); |
116 loop_.RunUntilIdle(); | 117 base::RunLoop().RunUntilIdle(); |
117 EXPECT_TRUE(bool_val); | 118 EXPECT_TRUE(bool_val); |
118 } | 119 } |
119 | 120 |
120 TEST_F(BindToCurrentLoopTest, BoundScopedPtrFreeDeleterBool) { | 121 TEST_F(BindToCurrentLoopTest, BoundScopedPtrFreeDeleterBool) { |
121 bool bool_val = false; | 122 bool bool_val = false; |
122 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( | 123 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
123 static_cast<bool*>(malloc(sizeof(bool)))); | 124 static_cast<bool*>(malloc(sizeof(bool)))); |
124 *scoped_ptr_free_deleter_bool = true; | 125 *scoped_ptr_free_deleter_bool = true; |
125 base::Closure cb = BindToCurrentLoop(base::Bind( | 126 base::Closure cb = BindToCurrentLoop(base::Bind( |
126 &BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, | 127 &BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, |
127 base::Passed(&scoped_ptr_free_deleter_bool))); | 128 base::Passed(&scoped_ptr_free_deleter_bool))); |
128 cb.Run(); | 129 cb.Run(); |
129 EXPECT_FALSE(bool_val); | 130 EXPECT_FALSE(bool_val); |
130 loop_.RunUntilIdle(); | 131 base::RunLoop().RunUntilIdle(); |
131 EXPECT_TRUE(bool_val); | 132 EXPECT_TRUE(bool_val); |
132 } | 133 } |
133 | 134 |
134 TEST_F(BindToCurrentLoopTest, PassedScopedPtrFreeDeleterBool) { | 135 TEST_F(BindToCurrentLoopTest, PassedScopedPtrFreeDeleterBool) { |
135 bool bool_val = false; | 136 bool bool_val = false; |
136 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( | 137 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
137 static_cast<bool*>(malloc(sizeof(bool)))); | 138 static_cast<bool*>(malloc(sizeof(bool)))); |
138 *scoped_ptr_free_deleter_bool = true; | 139 *scoped_ptr_free_deleter_bool = true; |
139 base::Callback<void(std::unique_ptr<bool, base::FreeDeleter>)> cb = | 140 base::Callback<void(std::unique_ptr<bool, base::FreeDeleter>)> cb = |
140 BindToCurrentLoop( | 141 BindToCurrentLoop( |
141 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val)); | 142 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val)); |
142 cb.Run(std::move(scoped_ptr_free_deleter_bool)); | 143 cb.Run(std::move(scoped_ptr_free_deleter_bool)); |
143 EXPECT_FALSE(bool_val); | 144 EXPECT_FALSE(bool_val); |
144 loop_.RunUntilIdle(); | 145 base::RunLoop().RunUntilIdle(); |
145 EXPECT_TRUE(bool_val); | 146 EXPECT_TRUE(bool_val); |
146 } | 147 } |
147 | 148 |
148 TEST_F(BindToCurrentLoopTest, BoolConstRef) { | 149 TEST_F(BindToCurrentLoopTest, BoolConstRef) { |
149 bool bool_var = false; | 150 bool bool_var = false; |
150 bool true_var = true; | 151 bool true_var = true; |
151 const bool& true_ref = true_var; | 152 const bool& true_ref = true_var; |
152 base::Closure cb = BindToCurrentLoop(base::Bind( | 153 base::Closure cb = BindToCurrentLoop(base::Bind( |
153 &BoundBoolSetFromConstRef, &bool_var, true_ref)); | 154 &BoundBoolSetFromConstRef, &bool_var, true_ref)); |
154 cb.Run(); | 155 cb.Run(); |
155 EXPECT_FALSE(bool_var); | 156 EXPECT_FALSE(bool_var); |
156 loop_.RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
157 EXPECT_TRUE(bool_var); | 158 EXPECT_TRUE(bool_var); |
158 } | 159 } |
159 | 160 |
160 TEST_F(BindToCurrentLoopTest, Integers) { | 161 TEST_F(BindToCurrentLoopTest, Integers) { |
161 int a = 0; | 162 int a = 0; |
162 int b = 0; | 163 int b = 0; |
163 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( | 164 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( |
164 &BoundIntegersSet, &a, &b)); | 165 &BoundIntegersSet, &a, &b)); |
165 cb.Run(1, -1); | 166 cb.Run(1, -1); |
166 EXPECT_EQ(a, 0); | 167 EXPECT_EQ(a, 0); |
167 EXPECT_EQ(b, 0); | 168 EXPECT_EQ(b, 0); |
168 loop_.RunUntilIdle(); | 169 base::RunLoop().RunUntilIdle(); |
169 EXPECT_EQ(a, 1); | 170 EXPECT_EQ(a, 1); |
170 EXPECT_EQ(b, -1); | 171 EXPECT_EQ(b, -1); |
171 } | 172 } |
172 | 173 |
173 } // namespace media | 174 } // namespace media |
OLD | NEW |