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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" |
10 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
11 #include "jingle/glue/thread_wrapper.h" | 13 #include "jingle/glue/thread_wrapper.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 using ::testing::DoAll; | 17 using ::testing::DoAll; |
16 using ::testing::InSequence; | 18 using ::testing::InSequence; |
17 using ::testing::InvokeWithoutArgs; | 19 using ::testing::InvokeWithoutArgs; |
18 using ::testing::Mock; | 20 using ::testing::Mock; |
19 | 21 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 EXPECT_CALL(handler1_, OnMessage( | 115 EXPECT_CALL(handler1_, OnMessage( |
114 MatchMessage(&handler1_, kTestMessage2, data2))) | 116 MatchMessage(&handler1_, kTestMessage2, data2))) |
115 .WillOnce(DeleteMessageData()); | 117 .WillOnce(DeleteMessageData()); |
116 EXPECT_CALL(handler2_, OnMessage( | 118 EXPECT_CALL(handler2_, OnMessage( |
117 MatchMessage(&handler2_, kTestMessage1, data3))) | 119 MatchMessage(&handler2_, kTestMessage1, data3))) |
118 .WillOnce(DeleteMessageData()); | 120 .WillOnce(DeleteMessageData()); |
119 EXPECT_CALL(handler2_, OnMessage( | 121 EXPECT_CALL(handler2_, OnMessage( |
120 MatchMessage(&handler2_, kTestMessage1, data4))) | 122 MatchMessage(&handler2_, kTestMessage1, data4))) |
121 .WillOnce(DeleteMessageData()); | 123 .WillOnce(DeleteMessageData()); |
122 | 124 |
123 message_loop_.RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
124 } | 126 } |
125 | 127 |
126 TEST_F(ThreadWrapperTest, PostDelayed) { | 128 TEST_F(ThreadWrapperTest, PostDelayed) { |
127 rtc::MessageData* data1 = new rtc::MessageData(); | 129 rtc::MessageData* data1 = new rtc::MessageData(); |
128 rtc::MessageData* data2 = new rtc::MessageData(); | 130 rtc::MessageData* data2 = new rtc::MessageData(); |
129 rtc::MessageData* data3 = new rtc::MessageData(); | 131 rtc::MessageData* data3 = new rtc::MessageData(); |
130 rtc::MessageData* data4 = new rtc::MessageData(); | 132 rtc::MessageData* data4 = new rtc::MessageData(); |
131 | 133 |
132 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs1, &handler1_, kTestMessage1, | 134 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs1, &handler1_, kTestMessage1, |
133 data1); | 135 data1); |
(...skipping 12 matching lines...) Expand all Loading... |
146 EXPECT_CALL(handler1_, OnMessage( | 148 EXPECT_CALL(handler1_, OnMessage( |
147 MatchMessage(&handler1_, kTestMessage2, data2))) | 149 MatchMessage(&handler1_, kTestMessage2, data2))) |
148 .WillOnce(DeleteMessageData()); | 150 .WillOnce(DeleteMessageData()); |
149 EXPECT_CALL(handler2_, OnMessage( | 151 EXPECT_CALL(handler2_, OnMessage( |
150 MatchMessage(&handler2_, kTestMessage1, data3))) | 152 MatchMessage(&handler2_, kTestMessage1, data3))) |
151 .WillOnce(DeleteMessageData()); | 153 .WillOnce(DeleteMessageData()); |
152 EXPECT_CALL(handler2_, OnMessage( | 154 EXPECT_CALL(handler2_, OnMessage( |
153 MatchMessage(&handler2_, kTestMessage1, data4))) | 155 MatchMessage(&handler2_, kTestMessage1, data4))) |
154 .WillOnce(DeleteMessageData()); | 156 .WillOnce(DeleteMessageData()); |
155 | 157 |
156 message_loop_.PostDelayedTask( | 158 message_loop_.task_runner()->PostDelayedTask( |
157 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 159 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
158 base::TimeDelta::FromMilliseconds(kMaxTestDelay)); | 160 base::TimeDelta::FromMilliseconds(kMaxTestDelay)); |
159 message_loop_.Run(); | 161 base::RunLoop().Run(); |
160 } | 162 } |
161 | 163 |
162 TEST_F(ThreadWrapperTest, Clear) { | 164 TEST_F(ThreadWrapperTest, Clear) { |
163 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage1, NULL); | 165 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage1, NULL); |
164 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage2, NULL); | 166 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage2, NULL); |
165 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage1, NULL); | 167 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage1, NULL); |
166 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage2, NULL); | 168 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage2, NULL); |
167 | 169 |
168 thread_->Clear(&handler1_, kTestMessage2); | 170 thread_->Clear(&handler1_, kTestMessage2); |
169 | 171 |
170 InSequence in_seq; | 172 InSequence in_seq; |
171 | 173 |
172 rtc::MessageData* null_data = NULL; | 174 rtc::MessageData* null_data = NULL; |
173 EXPECT_CALL(handler1_, OnMessage( | 175 EXPECT_CALL(handler1_, OnMessage( |
174 MatchMessage(&handler1_, kTestMessage1, null_data))) | 176 MatchMessage(&handler1_, kTestMessage1, null_data))) |
175 .WillOnce(DeleteMessageData()); | 177 .WillOnce(DeleteMessageData()); |
176 EXPECT_CALL(handler2_, OnMessage( | 178 EXPECT_CALL(handler2_, OnMessage( |
177 MatchMessage(&handler2_, kTestMessage1, null_data))) | 179 MatchMessage(&handler2_, kTestMessage1, null_data))) |
178 .WillOnce(DeleteMessageData()); | 180 .WillOnce(DeleteMessageData()); |
179 EXPECT_CALL(handler2_, OnMessage( | 181 EXPECT_CALL(handler2_, OnMessage( |
180 MatchMessage(&handler2_, kTestMessage2, null_data))) | 182 MatchMessage(&handler2_, kTestMessage2, null_data))) |
181 .WillOnce(DeleteMessageData()); | 183 .WillOnce(DeleteMessageData()); |
182 | 184 |
183 message_loop_.RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
184 } | 186 } |
185 | 187 |
186 TEST_F(ThreadWrapperTest, ClearDelayed) { | 188 TEST_F(ThreadWrapperTest, ClearDelayed) { |
187 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs1, &handler1_, kTestMessage1, | 189 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs1, &handler1_, kTestMessage1, |
188 NULL); | 190 NULL); |
189 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs2, &handler1_, kTestMessage2, | 191 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs2, &handler1_, kTestMessage2, |
190 NULL); | 192 NULL); |
191 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs3, &handler2_, kTestMessage1, | 193 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs3, &handler2_, kTestMessage1, |
192 NULL); | 194 NULL); |
193 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs4, &handler2_, kTestMessage1, | 195 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs4, &handler2_, kTestMessage1, |
194 NULL); | 196 NULL); |
195 | 197 |
196 thread_->Clear(&handler1_, kTestMessage2); | 198 thread_->Clear(&handler1_, kTestMessage2); |
197 | 199 |
198 InSequence in_seq; | 200 InSequence in_seq; |
199 | 201 |
200 rtc::MessageData* null_data = NULL; | 202 rtc::MessageData* null_data = NULL; |
201 EXPECT_CALL(handler1_, OnMessage( | 203 EXPECT_CALL(handler1_, OnMessage( |
202 MatchMessage(&handler1_, kTestMessage1, null_data))) | 204 MatchMessage(&handler1_, kTestMessage1, null_data))) |
203 .WillOnce(DeleteMessageData()); | 205 .WillOnce(DeleteMessageData()); |
204 EXPECT_CALL(handler2_, OnMessage( | 206 EXPECT_CALL(handler2_, OnMessage( |
205 MatchMessage(&handler2_, kTestMessage1, null_data))) | 207 MatchMessage(&handler2_, kTestMessage1, null_data))) |
206 .WillOnce(DeleteMessageData()); | 208 .WillOnce(DeleteMessageData()); |
207 EXPECT_CALL(handler2_, OnMessage( | 209 EXPECT_CALL(handler2_, OnMessage( |
208 MatchMessage(&handler2_, kTestMessage1, null_data))) | 210 MatchMessage(&handler2_, kTestMessage1, null_data))) |
209 .WillOnce(DeleteMessageData()); | 211 .WillOnce(DeleteMessageData()); |
210 | 212 |
211 message_loop_.PostDelayedTask( | 213 message_loop_.task_runner()->PostDelayedTask( |
212 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 214 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
213 base::TimeDelta::FromMilliseconds(kMaxTestDelay)); | 215 base::TimeDelta::FromMilliseconds(kMaxTestDelay)); |
214 message_loop_.Run(); | 216 base::RunLoop().Run(); |
215 } | 217 } |
216 | 218 |
217 // Verify that the queue is cleared when a handler is destroyed. | 219 // Verify that the queue is cleared when a handler is destroyed. |
218 TEST_F(ThreadWrapperTest, ClearDestroyed) { | 220 TEST_F(ThreadWrapperTest, ClearDestroyed) { |
219 MockMessageHandler* handler_ptr; | 221 MockMessageHandler* handler_ptr; |
220 { | 222 { |
221 MockMessageHandler handler; | 223 MockMessageHandler handler; |
222 handler_ptr = &handler; | 224 handler_ptr = &handler; |
223 thread_->Post(RTC_FROM_HERE, &handler, kTestMessage1, NULL); | 225 thread_->Post(RTC_FROM_HERE, &handler, kTestMessage1, NULL); |
224 } | 226 } |
(...skipping 26 matching lines...) Expand all Loading... |
251 TEST_F(ThreadWrapperTest, SendToOtherThread) { | 253 TEST_F(ThreadWrapperTest, SendToOtherThread) { |
252 JingleThreadWrapper::current()->set_send_allowed(true); | 254 JingleThreadWrapper::current()->set_send_allowed(true); |
253 | 255 |
254 base::Thread second_thread("JingleThreadWrapperTest"); | 256 base::Thread second_thread("JingleThreadWrapperTest"); |
255 second_thread.Start(); | 257 second_thread.Start(); |
256 | 258 |
257 base::WaitableEvent initialized_event( | 259 base::WaitableEvent initialized_event( |
258 base::WaitableEvent::ResetPolicy::MANUAL, | 260 base::WaitableEvent::ResetPolicy::MANUAL, |
259 base::WaitableEvent::InitialState::NOT_SIGNALED); | 261 base::WaitableEvent::InitialState::NOT_SIGNALED); |
260 rtc::Thread* target; | 262 rtc::Thread* target; |
261 second_thread.message_loop()->PostTask( | 263 second_thread.task_runner()->PostTask( |
262 FROM_HERE, base::Bind(&InitializeWrapperForNewThread, | 264 FROM_HERE, |
263 &target, &initialized_event)); | 265 base::Bind(&InitializeWrapperForNewThread, &target, &initialized_event)); |
264 initialized_event.Wait(); | 266 initialized_event.Wait(); |
265 | 267 |
266 ASSERT_TRUE(target != NULL); | 268 ASSERT_TRUE(target != NULL); |
267 | 269 |
268 rtc::MessageData* data = new rtc::MessageData(); | 270 rtc::MessageData* data = new rtc::MessageData(); |
269 | 271 |
270 EXPECT_CALL(handler1_, OnMessage( | 272 EXPECT_CALL(handler1_, OnMessage( |
271 MatchMessage(&handler1_, kTestMessage1, data))) | 273 MatchMessage(&handler1_, kTestMessage1, data))) |
272 .WillOnce(DeleteMessageData()); | 274 .WillOnce(DeleteMessageData()); |
273 target->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data); | 275 target->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data); |
274 | 276 |
275 Mock::VerifyAndClearExpectations(&handler1_); | 277 Mock::VerifyAndClearExpectations(&handler1_); |
276 } | 278 } |
277 | 279 |
278 // Verify that thread handles Send() while another Send() is | 280 // Verify that thread handles Send() while another Send() is |
279 // pending. The test creates second thread and Send()s kTestMessage1 | 281 // pending. The test creates second thread and Send()s kTestMessage1 |
280 // to that thread. kTestMessage1 handler calls PingMainThread() which | 282 // to that thread. kTestMessage1 handler calls PingMainThread() which |
281 // tries to Send() kTestMessage2 to the main thread. | 283 // tries to Send() kTestMessage2 to the main thread. |
282 TEST_F(ThreadWrapperTest, SendDuringSend) { | 284 TEST_F(ThreadWrapperTest, SendDuringSend) { |
283 JingleThreadWrapper::current()->set_send_allowed(true); | 285 JingleThreadWrapper::current()->set_send_allowed(true); |
284 | 286 |
285 base::Thread second_thread("JingleThreadWrapperTest"); | 287 base::Thread second_thread("JingleThreadWrapperTest"); |
286 second_thread.Start(); | 288 second_thread.Start(); |
287 | 289 |
288 base::WaitableEvent initialized_event( | 290 base::WaitableEvent initialized_event( |
289 base::WaitableEvent::ResetPolicy::MANUAL, | 291 base::WaitableEvent::ResetPolicy::MANUAL, |
290 base::WaitableEvent::InitialState::NOT_SIGNALED); | 292 base::WaitableEvent::InitialState::NOT_SIGNALED); |
291 rtc::Thread* target; | 293 rtc::Thread* target; |
292 second_thread.message_loop()->PostTask( | 294 second_thread.task_runner()->PostTask( |
293 FROM_HERE, base::Bind(&InitializeWrapperForNewThread, | 295 FROM_HERE, |
294 &target, &initialized_event)); | 296 base::Bind(&InitializeWrapperForNewThread, &target, &initialized_event)); |
295 initialized_event.Wait(); | 297 initialized_event.Wait(); |
296 | 298 |
297 ASSERT_TRUE(target != NULL); | 299 ASSERT_TRUE(target != NULL); |
298 | 300 |
299 rtc::MessageData* data = new rtc::MessageData(); | 301 rtc::MessageData* data = new rtc::MessageData(); |
300 | 302 |
301 EXPECT_CALL(handler1_, OnMessage( | 303 EXPECT_CALL(handler1_, OnMessage( |
302 MatchMessage(&handler1_, kTestMessage1, data))) | 304 MatchMessage(&handler1_, kTestMessage1, data))) |
303 .WillOnce(DoAll( | 305 .WillOnce(DoAll( |
304 InvokeWithoutArgs( | 306 InvokeWithoutArgs( |
305 this, &ThreadWrapperTest::PingMainThread), | 307 this, &ThreadWrapperTest::PingMainThread), |
306 DeleteMessageData())); | 308 DeleteMessageData())); |
307 target->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data); | 309 target->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data); |
308 | 310 |
309 Mock::VerifyAndClearExpectations(&handler1_); | 311 Mock::VerifyAndClearExpectations(&handler1_); |
310 } | 312 } |
311 | 313 |
312 TEST_F(ThreadWrapperTest, Dispose) { | 314 TEST_F(ThreadWrapperTest, Dispose) { |
313 bool deleted_ = false; | 315 bool deleted_ = false; |
314 thread_->Dispose(new DeletableObject(&deleted_)); | 316 thread_->Dispose(new DeletableObject(&deleted_)); |
315 EXPECT_FALSE(deleted_); | 317 EXPECT_FALSE(deleted_); |
316 message_loop_.RunUntilIdle(); | 318 base::RunLoop().RunUntilIdle(); |
317 EXPECT_TRUE(deleted_); | 319 EXPECT_TRUE(deleted_); |
318 } | 320 } |
319 | 321 |
320 } // namespace jingle_glue | 322 } // namespace jingle_glue |
OLD | NEW |