OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
7 #include "base/eintr_wrapper.h" | 9 #include "base/eintr_wrapper.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
willchan no longer on Chromium
2011/02/15 01:41:11
This should go first.
| |
10 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
11 #include "base/task.h" | 13 #include "base/task.h" |
12 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
13 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
17 #include "base/message_pump_win.h" | 19 #include "base/message_pump_win.h" |
18 #include "base/win/scoped_handle.h" | 20 #include "base/win/scoped_handle.h" |
19 #endif | 21 #endif |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 void QuitNow() { | 87 void QuitNow() { |
86 MessageLoop::current()->Quit(); | 88 MessageLoop::current()->Quit(); |
87 } | 89 } |
88 | 90 |
89 private: | 91 private: |
90 friend class base::RefCounted<QuitMsgLoop>; | 92 friend class base::RefCounted<QuitMsgLoop>; |
91 | 93 |
92 ~QuitMsgLoop() {} | 94 ~QuitMsgLoop() {} |
93 }; | 95 }; |
94 | 96 |
97 void RunTest_PostClosure(MessageLoop::Type message_loop_type) { | |
98 MessageLoop loop(message_loop_type); | |
99 | |
100 // Add tests to message loop | |
101 scoped_refptr<Foo> foo(new Foo()); | |
102 std::string a("a"), b("b"), c("c"), d("d"); | |
103 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
104 &Foo::Test0, foo.get())); | |
105 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
106 &Foo::Test1ConstRef, foo.get(), a)); | |
107 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
108 &Foo::Test1Ptr, foo.get(), &b)); | |
109 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
110 &Foo::Test1Int, foo.get(), 100)); | |
111 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
112 &Foo::Test2Ptr, foo.get(), &a, &c)); | |
113 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
114 &Foo::Test2Mixed, foo.get(), a, &d)); | |
115 | |
116 // After all tests, post a message that will shut down the message loop | |
117 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
118 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); | |
119 | |
120 // Now kick things off | |
121 MessageLoop::current()->Run(); | |
122 | |
123 EXPECT_EQ(foo->test_count(), 105); | |
124 EXPECT_EQ(foo->result(), "abacad"); | |
125 } | |
126 | |
95 void RunTest_PostTask(MessageLoop::Type message_loop_type) { | 127 void RunTest_PostTask(MessageLoop::Type message_loop_type) { |
96 MessageLoop loop(message_loop_type); | 128 MessageLoop loop(message_loop_type); |
97 | 129 |
98 // Add tests to message loop | 130 // Add tests to message loop |
99 scoped_refptr<Foo> foo(new Foo()); | 131 scoped_refptr<Foo> foo(new Foo()); |
100 std::string a("a"), b("b"), c("c"), d("d"); | 132 std::string a("a"), b("b"), c("c"), d("d"); |
101 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 133 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
102 foo.get(), &Foo::Test0)); | 134 foo.get(), &Foo::Test0)); |
103 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 135 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
104 foo.get(), &Foo::Test1ConstRef, a)); | 136 foo.get(), &Foo::Test1ConstRef, a)); |
(...skipping 11 matching lines...) Expand all Loading... | |
116 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 148 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
117 quit.get(), &QuitMsgLoop::QuitNow)); | 149 quit.get(), &QuitMsgLoop::QuitNow)); |
118 | 150 |
119 // Now kick things off | 151 // Now kick things off |
120 MessageLoop::current()->Run(); | 152 MessageLoop::current()->Run(); |
121 | 153 |
122 EXPECT_EQ(foo->test_count(), 105); | 154 EXPECT_EQ(foo->test_count(), 105); |
123 EXPECT_EQ(foo->result(), "abacad"); | 155 EXPECT_EQ(foo->result(), "abacad"); |
124 } | 156 } |
125 | 157 |
158 void RunTest_PostClosure_SEH(MessageLoop::Type message_loop_type) { | |
159 MessageLoop loop(message_loop_type); | |
160 | |
161 // Add tests to message loop | |
162 scoped_refptr<Foo> foo(new Foo()); | |
163 std::string a("a"), b("b"), c("c"), d("d"); | |
164 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
165 &Foo::Test0, foo.get())); | |
166 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
167 &Foo::Test1ConstRef,foo.get(), a)); | |
168 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
169 &Foo::Test1Ptr,foo.get(), &b)); | |
170 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
171 &Foo::Test1Int,foo.get(), 100)); | |
172 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
173 &Foo::Test2Ptr,foo.get(), &a, &c)); | |
174 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
175 &Foo::Test2Mixed,foo.get(), a, &d)); | |
176 | |
177 // After all tests, post a message that will shut down the message loop | |
178 MessageLoop::current()->PostClosure(FROM_HERE, base::Bind( | |
179 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); | |
180 | |
181 // Now kick things off with the SEH block active. | |
182 MessageLoop::current()->set_exception_restoration(true); | |
183 MessageLoop::current()->Run(); | |
184 MessageLoop::current()->set_exception_restoration(false); | |
185 | |
186 EXPECT_EQ(foo->test_count(), 105); | |
187 EXPECT_EQ(foo->result(), "abacad"); | |
188 } | |
189 | |
126 void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) { | 190 void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) { |
127 MessageLoop loop(message_loop_type); | 191 MessageLoop loop(message_loop_type); |
128 | 192 |
129 // Add tests to message loop | 193 // Add tests to message loop |
130 scoped_refptr<Foo> foo(new Foo()); | 194 scoped_refptr<Foo> foo(new Foo()); |
131 std::string a("a"), b("b"), c("c"), d("d"); | 195 std::string a("a"), b("b"), c("c"), d("d"); |
132 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 196 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
133 foo.get(), &Foo::Test0)); | 197 foo.get(), &Foo::Test0)); |
134 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 198 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
135 foo.get(), &Foo::Test1ConstRef, a)); | 199 foo.get(), &Foo::Test1ConstRef, a)); |
(...skipping 13 matching lines...) Expand all Loading... | |
149 | 213 |
150 // Now kick things off with the SEH block active. | 214 // Now kick things off with the SEH block active. |
151 MessageLoop::current()->set_exception_restoration(true); | 215 MessageLoop::current()->set_exception_restoration(true); |
152 MessageLoop::current()->Run(); | 216 MessageLoop::current()->Run(); |
153 MessageLoop::current()->set_exception_restoration(false); | 217 MessageLoop::current()->set_exception_restoration(false); |
154 | 218 |
155 EXPECT_EQ(foo->test_count(), 105); | 219 EXPECT_EQ(foo->test_count(), 105); |
156 EXPECT_EQ(foo->result(), "abacad"); | 220 EXPECT_EQ(foo->result(), "abacad"); |
157 } | 221 } |
158 | 222 |
223 // This Func runs slowly to simulate a large amount of work being done. | |
224 static void SlowFunc(int pause_ms, int* quit_counter) { | |
225 PlatformThread::Sleep(pause_ms); | |
226 if (--(*quit_counter) == 0) | |
227 MessageLoop::current()->Quit(); | |
228 } | |
229 | |
159 // This class runs slowly to simulate a large amount of work being done. | 230 // This class runs slowly to simulate a large amount of work being done. |
160 class SlowTask : public Task { | 231 class SlowTask : public Task { |
161 public: | 232 public: |
162 SlowTask(int pause_ms, int* quit_counter) | 233 SlowTask(int pause_ms, int* quit_counter) |
163 : pause_ms_(pause_ms), quit_counter_(quit_counter) { | 234 : pause_ms_(pause_ms), quit_counter_(quit_counter) { |
164 } | 235 } |
165 virtual void Run() { | 236 virtual void Run() { |
166 PlatformThread::Sleep(pause_ms_); | 237 SlowFunc(pause_ms_, quit_counter_); |
167 if (--(*quit_counter_) == 0) | |
168 MessageLoop::current()->Quit(); | |
169 } | 238 } |
170 private: | 239 private: |
171 int pause_ms_; | 240 int pause_ms_; |
172 int* quit_counter_; | 241 int* quit_counter_; |
173 }; | 242 }; |
174 | 243 |
175 // This class records the time when Run was called in a Time object, which is | 244 // This class records the time when Run was called in a Time object, which is |
176 // useful for building a variety of MessageLoop tests. | 245 // useful for building a variety of MessageLoop tests. |
177 class RecordRunTimeTask : public SlowTask { | 246 static void RecordRunTimeFunc(Time* run_time, int* quit_counter) { |
247 *run_time = Time::Now(); | |
248 | |
249 // Cause our Run function to take some time to execute. As a result we can | |
250 // count on subsequent RecordRunTimeFunc()s running at a future time, | |
251 // without worry about the resolution of our system clock being an issue. | |
252 SlowFunc(10, quit_counter); | |
253 } | |
254 | |
255 // This class records the time when Run was called in a Time object, which is | |
256 // useful for building a variety of MessageLoop tests. | |
257 class RecordRunTimeTask : public Task { | |
178 public: | 258 public: |
179 RecordRunTimeTask(Time* run_time, int* quit_counter) | 259 RecordRunTimeTask(Time* run_time, int* quit_counter) |
180 : SlowTask(10, quit_counter), run_time_(run_time) { | 260 : run_time_(run_time), quit_counter_(quit_counter) { |
181 } | 261 } |
182 virtual void Run() { | 262 virtual void Run() { |
183 *run_time_ = Time::Now(); | 263 RecordRunTimeFunc(run_time_, quit_counter_); |
184 // Cause our Run function to take some time to execute. As a result we can | |
185 // count on subsequent RecordRunTimeTask objects running at a future time, | |
186 // without worry about the resolution of our system clock being an issue. | |
187 SlowTask::Run(); | |
188 } | 264 } |
189 private: | 265 private: |
190 Time* run_time_; | 266 Time* run_time_; |
267 int* quit_counter_; | |
191 }; | 268 }; |
192 | 269 |
270 void RunTest_PostDelayedClosure_Basic(MessageLoop::Type message_loop_type) { | |
271 MessageLoop loop(message_loop_type); | |
272 | |
273 // Test that PostDelayedClosureTask results in a delayed task. | |
274 | |
275 const int kDelayMS = 100; | |
276 | |
277 int num_tasks = 1; | |
278 Time run_time; | |
279 | |
280 loop.PostDelayedClosure( | |
281 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), | |
282 kDelayMS); | |
283 | |
284 Time time_before_run = Time::Now(); | |
285 loop.Run(); | |
286 Time time_after_run = Time::Now(); | |
287 | |
288 EXPECT_EQ(0, num_tasks); | |
289 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds()); | |
290 } | |
291 | |
193 void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) { | 292 void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) { |
194 MessageLoop loop(message_loop_type); | 293 MessageLoop loop(message_loop_type); |
195 | 294 |
196 // Test that PostDelayedTask results in a delayed task. | 295 // Test that PostDelayedTask results in a delayed task. |
197 | 296 |
198 const int kDelayMS = 100; | 297 const int kDelayMS = 100; |
199 | 298 |
200 int num_tasks = 1; | 299 int num_tasks = 1; |
201 Time run_time; | 300 Time run_time; |
202 | 301 |
203 loop.PostDelayedTask( | 302 loop.PostDelayedTask( |
204 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), kDelayMS); | 303 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), kDelayMS); |
205 | 304 |
206 Time time_before_run = Time::Now(); | 305 Time time_before_run = Time::Now(); |
207 loop.Run(); | 306 loop.Run(); |
208 Time time_after_run = Time::Now(); | 307 Time time_after_run = Time::Now(); |
209 | 308 |
210 EXPECT_EQ(0, num_tasks); | 309 EXPECT_EQ(0, num_tasks); |
211 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds()); | 310 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds()); |
212 } | 311 } |
213 | 312 |
313 void RunTest_PostDelayedClosure_InDelayOrder( | |
314 MessageLoop::Type message_loop_type) { | |
315 MessageLoop loop(message_loop_type); | |
316 | |
317 // Test that two tasks with different delays run in the right order. | |
318 | |
319 int num_tasks = 2; | |
320 Time run_time1, run_time2; | |
321 | |
322 loop.PostDelayedClosure( | |
323 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), 200); | |
324 // If we get a large pause in execution (due to a context switch) here, this | |
325 // test could fail. | |
326 loop.PostDelayedClosure( | |
327 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10); | |
328 | |
329 loop.Run(); | |
330 EXPECT_EQ(0, num_tasks); | |
331 | |
332 EXPECT_TRUE(run_time2 < run_time1); | |
333 } | |
334 | |
214 void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type) { | 335 void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type) { |
215 MessageLoop loop(message_loop_type); | 336 MessageLoop loop(message_loop_type); |
216 | 337 |
217 // Test that two tasks with different delays run in the right order. | 338 // Test that two tasks with different delays run in the right order. |
218 | 339 |
219 int num_tasks = 2; | 340 int num_tasks = 2; |
220 Time run_time1, run_time2; | 341 Time run_time1, run_time2; |
221 | 342 |
222 loop.PostDelayedTask( | 343 loop.PostDelayedTask( |
223 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), 200); | 344 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), 200); |
224 // If we get a large pause in execution (due to a context switch) here, this | 345 // If we get a large pause in execution (due to a context switch) here, this |
225 // test could fail. | 346 // test could fail. |
226 loop.PostDelayedTask( | 347 loop.PostDelayedTask( |
227 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10); | 348 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10); |
228 | 349 |
229 loop.Run(); | 350 loop.Run(); |
230 EXPECT_EQ(0, num_tasks); | 351 EXPECT_EQ(0, num_tasks); |
231 | 352 |
232 EXPECT_TRUE(run_time2 < run_time1); | 353 EXPECT_TRUE(run_time2 < run_time1); |
233 } | 354 } |
234 | 355 |
356 void RunTest_PostDelayedClosure_InPostOrder( | |
357 MessageLoop::Type message_loop_type) { | |
358 MessageLoop loop(message_loop_type); | |
359 | |
360 // Test that two tasks with the same delay run in the order in which they | |
361 // were posted. | |
362 // | |
363 // NOTE: This is actually an approximate test since the API only takes a | |
364 // "delay" parameter, so we are not exactly simulating two tasks that get | |
365 // posted at the exact same time. It would be nice if the API allowed us to | |
366 // specify the desired run time. | |
367 | |
368 const int kDelayMS = 100; | |
369 | |
370 int num_tasks = 2; | |
371 Time run_time1, run_time2; | |
372 | |
373 loop.PostDelayedClosure( | |
374 FROM_HERE, | |
375 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), | |
376 kDelayMS); | |
377 loop.PostDelayedClosure( | |
378 FROM_HERE, | |
379 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), | |
380 kDelayMS); | |
381 | |
382 loop.Run(); | |
383 EXPECT_EQ(0, num_tasks); | |
384 | |
385 EXPECT_TRUE(run_time1 < run_time2); | |
386 } | |
387 | |
235 void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type) { | 388 void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type) { |
236 MessageLoop loop(message_loop_type); | 389 MessageLoop loop(message_loop_type); |
237 | 390 |
238 // Test that two tasks with the same delay run in the order in which they | 391 // Test that two tasks with the same delay run in the order in which they |
239 // were posted. | 392 // were posted. |
240 // | 393 // |
241 // NOTE: This is actually an approximate test since the API only takes a | 394 // NOTE: This is actually an approximate test since the API only takes a |
242 // "delay" parameter, so we are not exactly simulating two tasks that get | 395 // "delay" parameter, so we are not exactly simulating two tasks that get |
243 // posted at the exact same time. It would be nice if the API allowed us to | 396 // posted at the exact same time. It would be nice if the API allowed us to |
244 // specify the desired run time. | 397 // specify the desired run time. |
245 | 398 |
246 const int kDelayMS = 100; | 399 const int kDelayMS = 100; |
247 | 400 |
248 int num_tasks = 2; | 401 int num_tasks = 2; |
249 Time run_time1, run_time2; | 402 Time run_time1, run_time2; |
250 | 403 |
251 loop.PostDelayedTask( | 404 loop.PostDelayedTask( |
252 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), kDelayMS); | 405 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), kDelayMS); |
253 loop.PostDelayedTask( | 406 loop.PostDelayedTask( |
254 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), kDelayMS); | 407 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), kDelayMS); |
255 | 408 |
256 loop.Run(); | 409 loop.Run(); |
257 EXPECT_EQ(0, num_tasks); | 410 EXPECT_EQ(0, num_tasks); |
258 | 411 |
259 EXPECT_TRUE(run_time1 < run_time2); | 412 EXPECT_TRUE(run_time1 < run_time2); |
260 } | 413 } |
261 | 414 |
415 void RunTest_PostDelayedClosure_InPostOrder_2( | |
416 MessageLoop::Type message_loop_type) { | |
417 MessageLoop loop(message_loop_type); | |
418 | |
419 // Test that a delayed task still runs after a normal tasks even if the | |
420 // normal tasks take a long time to run. | |
421 | |
422 const int kPauseMS = 50; | |
423 | |
424 int num_tasks = 2; | |
425 Time run_time; | |
426 | |
427 loop.PostClosure( | |
428 FROM_HERE, base::Bind(&SlowFunc, kPauseMS, &num_tasks)); | |
429 loop.PostDelayedClosure( | |
430 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), 10); | |
431 | |
432 Time time_before_run = Time::Now(); | |
433 loop.Run(); | |
434 Time time_after_run = Time::Now(); | |
435 | |
436 EXPECT_EQ(0, num_tasks); | |
437 | |
438 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds()); | |
439 } | |
440 | |
262 void RunTest_PostDelayedTask_InPostOrder_2( | 441 void RunTest_PostDelayedTask_InPostOrder_2( |
263 MessageLoop::Type message_loop_type) { | 442 MessageLoop::Type message_loop_type) { |
264 MessageLoop loop(message_loop_type); | 443 MessageLoop loop(message_loop_type); |
265 | 444 |
266 // Test that a delayed task still runs after a normal tasks even if the | 445 // Test that a delayed task still runs after a normal tasks even if the |
267 // normal tasks take a long time to run. | 446 // normal tasks take a long time to run. |
268 | 447 |
269 const int kPauseMS = 50; | 448 const int kPauseMS = 50; |
270 | 449 |
271 int num_tasks = 2; | 450 int num_tasks = 2; |
272 Time run_time; | 451 Time run_time; |
273 | 452 |
274 loop.PostTask( | 453 loop.PostTask( |
275 FROM_HERE, new SlowTask(kPauseMS, &num_tasks)); | 454 FROM_HERE, new SlowTask(kPauseMS, &num_tasks)); |
276 loop.PostDelayedTask( | 455 loop.PostDelayedTask( |
277 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 10); | 456 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 10); |
278 | 457 |
279 Time time_before_run = Time::Now(); | 458 Time time_before_run = Time::Now(); |
280 loop.Run(); | 459 loop.Run(); |
281 Time time_after_run = Time::Now(); | 460 Time time_after_run = Time::Now(); |
282 | 461 |
283 EXPECT_EQ(0, num_tasks); | 462 EXPECT_EQ(0, num_tasks); |
284 | 463 |
285 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds()); | 464 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds()); |
286 } | 465 } |
287 | 466 |
467 void RunTest_PostDelayedClosure_InPostOrder_3( | |
468 MessageLoop::Type message_loop_type) { | |
469 MessageLoop loop(message_loop_type); | |
470 | |
471 // Test that a delayed task still runs after a pile of normal tasks. The key | |
472 // difference between this test and the previous one is that here we return | |
473 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities | |
474 // to maybe run the delayed task. It should know not to do so until the | |
475 // delayed task's delay has passed. | |
476 | |
477 int num_tasks = 11; | |
478 Time run_time1, run_time2; | |
479 | |
480 // Clutter the ML with tasks. | |
481 for (int i = 1; i < num_tasks; ++i) | |
482 loop.PostClosure(FROM_HERE, | |
483 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks)); | |
484 | |
485 loop.PostDelayedClosure( | |
486 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 1); | |
487 | |
488 loop.Run(); | |
489 EXPECT_EQ(0, num_tasks); | |
490 | |
491 EXPECT_TRUE(run_time2 > run_time1); | |
492 } | |
493 | |
288 void RunTest_PostDelayedTask_InPostOrder_3( | 494 void RunTest_PostDelayedTask_InPostOrder_3( |
289 MessageLoop::Type message_loop_type) { | 495 MessageLoop::Type message_loop_type) { |
290 MessageLoop loop(message_loop_type); | 496 MessageLoop loop(message_loop_type); |
291 | 497 |
292 // Test that a delayed task still runs after a pile of normal tasks. The key | 498 // Test that a delayed task still runs after a pile of normal tasks. The key |
293 // difference between this test and the previous one is that here we return | 499 // difference between this test and the previous one is that here we return |
294 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities | 500 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities |
295 // to maybe run the delayed task. It should know not to do so until the | 501 // to maybe run the delayed task. It should know not to do so until the |
296 // delayed task's delay has passed. | 502 // delayed task's delay has passed. |
297 | 503 |
298 int num_tasks = 11; | 504 int num_tasks = 11; |
299 Time run_time1, run_time2; | 505 Time run_time1, run_time2; |
300 | 506 |
301 // Clutter the ML with tasks. | 507 // Clutter the ML with tasks. |
302 for (int i = 1; i < num_tasks; ++i) | 508 for (int i = 1; i < num_tasks; ++i) |
303 loop.PostTask(FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks)); | 509 loop.PostTask(FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks)); |
304 | 510 |
305 loop.PostDelayedTask( | 511 loop.PostDelayedTask( |
306 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); | 512 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); |
307 | 513 |
308 loop.Run(); | 514 loop.Run(); |
309 EXPECT_EQ(0, num_tasks); | 515 EXPECT_EQ(0, num_tasks); |
310 | 516 |
311 EXPECT_TRUE(run_time2 > run_time1); | 517 EXPECT_TRUE(run_time2 > run_time1); |
312 } | 518 } |
313 | 519 |
520 void RunTest_PostDelayedClosure_SharedTimer( | |
521 MessageLoop::Type message_loop_type) { | |
522 MessageLoop loop(message_loop_type); | |
523 | |
524 // Test that the interval of the timer, used to run the next delayed task, is | |
525 // set to a value corresponding to when the next delayed task should run. | |
526 | |
527 // By setting num_tasks to 1, we ensure that the first task to run causes the | |
528 // run loop to exit. | |
529 int num_tasks = 1; | |
530 Time run_time1, run_time2; | |
531 | |
532 loop.PostDelayedClosure( | |
533 FROM_HERE, | |
534 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), | |
535 1000000); | |
536 loop.PostDelayedClosure( | |
537 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10); | |
538 | |
539 Time start_time = Time::Now(); | |
540 | |
541 loop.Run(); | |
542 EXPECT_EQ(0, num_tasks); | |
543 | |
544 // Ensure that we ran in far less time than the slower timer. | |
545 TimeDelta total_time = Time::Now() - start_time; | |
546 EXPECT_GT(5000, total_time.InMilliseconds()); | |
547 | |
548 // In case both timers somehow run at nearly the same time, sleep a little | |
549 // and then run all pending to force them both to have run. This is just | |
550 // encouraging flakiness if there is any. | |
551 PlatformThread::Sleep(100); | |
552 loop.RunAllPending(); | |
553 | |
554 EXPECT_TRUE(run_time1.is_null()); | |
555 EXPECT_FALSE(run_time2.is_null()); | |
556 } | |
557 | |
314 void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) { | 558 void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) { |
315 MessageLoop loop(message_loop_type); | 559 MessageLoop loop(message_loop_type); |
316 | 560 |
317 // Test that the interval of the timer, used to run the next delayed task, is | 561 // Test that the interval of the timer, used to run the next delayed task, is |
318 // set to a value corresponding to when the next delayed task should run. | 562 // set to a value corresponding to when the next delayed task should run. |
319 | 563 |
320 // By setting num_tasks to 1, we ensure that the first task to run causes the | 564 // By setting num_tasks to 1, we ensure that the first task to run causes the |
321 // run loop to exit. | 565 // run loop to exit. |
322 int num_tasks = 1; | 566 int num_tasks = 1; |
323 Time run_time1, run_time2; | 567 Time run_time1, run_time2; |
(...skipping 17 matching lines...) Expand all Loading... | |
341 // encouraging flakiness if there is any. | 585 // encouraging flakiness if there is any. |
342 PlatformThread::Sleep(100); | 586 PlatformThread::Sleep(100); |
343 loop.RunAllPending(); | 587 loop.RunAllPending(); |
344 | 588 |
345 EXPECT_TRUE(run_time1.is_null()); | 589 EXPECT_TRUE(run_time1.is_null()); |
346 EXPECT_FALSE(run_time2.is_null()); | 590 EXPECT_FALSE(run_time2.is_null()); |
347 } | 591 } |
348 | 592 |
349 #if defined(OS_WIN) | 593 #if defined(OS_WIN) |
350 | 594 |
595 void SubPumpFunc() { | |
596 MessageLoop::current()->SetNestableTasksAllowed(true); | |
597 MSG msg; | |
598 while (GetMessage(&msg, NULL, 0, 0)) { | |
599 TranslateMessage(&msg); | |
600 DispatchMessage(&msg); | |
601 } | |
602 MessageLoop::current()->Quit(); | |
603 } | |
604 | |
351 class SubPumpTask : public Task { | 605 class SubPumpTask : public Task { |
352 public: | 606 public: |
353 virtual void Run() { | 607 virtual void Run() { |
354 MessageLoop::current()->SetNestableTasksAllowed(true); | 608 SubPumpFunc(); |
355 MSG msg; | |
356 while (GetMessage(&msg, NULL, 0, 0)) { | |
357 TranslateMessage(&msg); | |
358 DispatchMessage(&msg); | |
359 } | |
360 MessageLoop::current()->Quit(); | |
361 } | 609 } |
362 }; | 610 }; |
363 | 611 |
364 class SubPumpQuitTask : public Task { | 612 class SubPumpQuitTask : public Task { |
365 public: | 613 public: |
366 SubPumpQuitTask() { | 614 SubPumpQuitTask() { |
367 } | 615 } |
368 virtual void Run() { | 616 virtual void Run() { |
369 PostQuitMessage(0); | 617 PostQuitMessage(0); |
370 } | 618 } |
371 }; | 619 }; |
372 | 620 |
621 // Workaround base::Bind not support __stdcall. | |
622 // | |
623 // TODO(ajwong): Add support for platform specific function types into Bind. | |
624 void DoPostQuitMessage(int n) { | |
625 PostQuitMessage(n); | |
626 } | |
627 | |
628 void RunTest_PostDelayedClosure_SharedTimer_SubPump() { | |
629 MessageLoop loop(MessageLoop::TYPE_UI); | |
630 | |
631 // Test that the interval of the timer, used to run the next delayed task, is | |
632 // set to a value corresponding to when the next delayed task should run. | |
633 | |
634 // By setting num_tasks to 1, we ensure that the first task to run causes the | |
635 // run loop to exit. | |
636 int num_tasks = 1; | |
637 Time run_time; | |
638 | |
639 loop.PostClosure(FROM_HERE, base::Bind(&SubPumpFunc)); | |
640 | |
641 // This very delayed task should never run. | |
642 loop.PostDelayedClosure( | |
643 FROM_HERE, | |
644 base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), | |
645 1000000); | |
646 | |
647 // This slightly delayed task should run from within SubPumpTask::Run(). | |
648 loop.PostDelayedClosure(FROM_HERE, base::Bind(&DoPostQuitMessage, 0), 10); | |
649 | |
650 Time start_time = Time::Now(); | |
651 | |
652 loop.Run(); | |
653 EXPECT_EQ(1, num_tasks); | |
654 | |
655 // Ensure that we ran in far less time than the slower timer. | |
656 TimeDelta total_time = Time::Now() - start_time; | |
657 EXPECT_GT(5000, total_time.InMilliseconds()); | |
658 | |
659 // In case both timers somehow run at nearly the same time, sleep a little | |
660 // and then run all pending to force them both to have run. This is just | |
661 // encouraging flakiness if there is any. | |
662 PlatformThread::Sleep(100); | |
663 loop.RunAllPending(); | |
664 | |
665 EXPECT_TRUE(run_time.is_null()); | |
666 } | |
667 | |
373 void RunTest_PostDelayedTask_SharedTimer_SubPump() { | 668 void RunTest_PostDelayedTask_SharedTimer_SubPump() { |
374 MessageLoop loop(MessageLoop::TYPE_UI); | 669 MessageLoop loop(MessageLoop::TYPE_UI); |
375 | 670 |
376 // Test that the interval of the timer, used to run the next delayed task, is | 671 // Test that the interval of the timer, used to run the next delayed task, is |
377 // set to a value corresponding to when the next delayed task should run. | 672 // set to a value corresponding to when the next delayed task should run. |
378 | 673 |
379 // By setting num_tasks to 1, we ensure that the first task to run causes the | 674 // By setting num_tasks to 1, we ensure that the first task to run causes the |
380 // run loop to exit. | 675 // run loop to exit. |
381 int num_tasks = 1; | 676 int num_tasks = 1; |
382 Time run_time; | 677 Time run_time; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 RecordDeletionTask* a = new RecordDeletionTask(NULL, &a_was_deleted); | 745 RecordDeletionTask* a = new RecordDeletionTask(NULL, &a_was_deleted); |
451 RecordDeletionTask* b = new RecordDeletionTask(a, &b_was_deleted); | 746 RecordDeletionTask* b = new RecordDeletionTask(a, &b_was_deleted); |
452 RecordDeletionTask* c = new RecordDeletionTask(b, &c_was_deleted); | 747 RecordDeletionTask* c = new RecordDeletionTask(b, &c_was_deleted); |
453 loop.PostTask(FROM_HERE, c); | 748 loop.PostTask(FROM_HERE, c); |
454 } | 749 } |
455 EXPECT_TRUE(a_was_deleted); | 750 EXPECT_TRUE(a_was_deleted); |
456 EXPECT_TRUE(b_was_deleted); | 751 EXPECT_TRUE(b_was_deleted); |
457 EXPECT_TRUE(c_was_deleted); | 752 EXPECT_TRUE(c_was_deleted); |
458 } | 753 } |
459 | 754 |
755 void NestingFunc(int* depth) { | |
756 if (*depth > 0) { | |
757 *depth -= 1; | |
758 MessageLoop::current()->PostClosure(FROM_HERE, | |
759 base::Bind(&NestingFunc, depth)); | |
760 | |
761 MessageLoop::current()->SetNestableTasksAllowed(true); | |
762 MessageLoop::current()->Run(); | |
763 } | |
764 MessageLoop::current()->Quit(); | |
765 } | |
766 | |
460 class NestingTest : public Task { | 767 class NestingTest : public Task { |
461 public: | 768 public: |
462 explicit NestingTest(int* depth) : depth_(depth) { | 769 explicit NestingTest(int* depth) : depth_(depth) { |
463 } | 770 } |
464 void Run() { | 771 void Run() { |
465 if (*depth_ > 0) { | 772 if (*depth_ > 0) { |
466 *depth_ -= 1; | 773 *depth_ -= 1; |
467 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_)); | 774 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_)); |
468 | 775 |
469 MessageLoop::current()->SetNestableTasksAllowed(true); | 776 MessageLoop::current()->SetNestableTasksAllowed(true); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
581 MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(true)); | 888 MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(true)); |
582 MessageLoop::current()->set_exception_restoration(true); | 889 MessageLoop::current()->set_exception_restoration(true); |
583 MessageLoop::current()->Run(); | 890 MessageLoop::current()->Run(); |
584 MessageLoop::current()->set_exception_restoration(false); | 891 MessageLoop::current()->set_exception_restoration(false); |
585 | 892 |
586 ::SetUnhandledExceptionFilter(old_SEH_filter); | 893 ::SetUnhandledExceptionFilter(old_SEH_filter); |
587 } | 894 } |
588 | 895 |
589 #endif // defined(OS_WIN) | 896 #endif // defined(OS_WIN) |
590 | 897 |
898 void RunTest_NestingClosure(MessageLoop::Type message_loop_type) { | |
899 MessageLoop loop(message_loop_type); | |
900 | |
901 int depth = 100; | |
902 MessageLoop::current()->PostClosure(FROM_HERE, | |
903 base::Bind(&NestingFunc, &depth)); | |
904 MessageLoop::current()->Run(); | |
905 EXPECT_EQ(depth, 0); | |
906 } | |
907 | |
591 void RunTest_Nesting(MessageLoop::Type message_loop_type) { | 908 void RunTest_Nesting(MessageLoop::Type message_loop_type) { |
592 MessageLoop loop(message_loop_type); | 909 MessageLoop loop(message_loop_type); |
593 | 910 |
594 int depth = 100; | 911 int depth = 100; |
595 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(&depth)); | 912 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(&depth)); |
596 MessageLoop::current()->Run(); | 913 MessageLoop::current()->Run(); |
597 EXPECT_EQ(depth, 0); | 914 EXPECT_EQ(depth, 0); |
598 } | 915 } |
599 | 916 |
600 const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test"; | 917 const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test"; |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
855 private: | 1172 private: |
856 MessageLoop* target_; | 1173 MessageLoop* target_; |
857 HANDLE event_; | 1174 HANDLE event_; |
858 TaskList* order_; | 1175 TaskList* order_; |
859 bool expect_window_; | 1176 bool expect_window_; |
860 bool is_reentrant_; | 1177 bool is_reentrant_; |
861 }; | 1178 }; |
862 | 1179 |
863 #endif // defined(OS_WIN) | 1180 #endif // defined(OS_WIN) |
864 | 1181 |
1182 void RunTest_RecursiveDenialClosure1(MessageLoop::Type message_loop_type) { | |
1183 MessageLoop loop(message_loop_type); | |
1184 | |
1185 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); | |
1186 TaskList order; | |
1187 RecursiveTask recursive1(2, &order, 1, false); | |
1188 RecursiveTask recursive2(2, &order, 2, false); | |
1189 QuitTask quit_task(&order, 3); | |
1190 MessageLoop::current()->PostClosure( | |
1191 FROM_HERE, | |
1192 base::Bind(&RecursiveTask::Run, base::Unretained(&recursive1))); | |
1193 MessageLoop::current()->PostClosure( | |
1194 FROM_HERE, | |
1195 base::Bind(&RecursiveTask::Run, base::Unretained(&recursive2))); | |
1196 MessageLoop::current()->PostClosure( | |
1197 FROM_HERE, | |
1198 base::Bind(&QuitTask::Run, base::Unretained(&quit_task))); | |
1199 | |
1200 MessageLoop::current()->Run(); | |
1201 | |
1202 // FIFO order. | |
1203 ASSERT_EQ(14U, order.size()); | |
1204 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true)); | |
1205 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false)); | |
1206 EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true)); | |
1207 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false)); | |
1208 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); | |
1209 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); | |
1210 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true)); | |
1211 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false)); | |
1212 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true)); | |
1213 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false)); | |
1214 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); | |
1215 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); | |
1216 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true)); | |
1217 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false)); | |
1218 } | |
1219 | |
865 void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) { | 1220 void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) { |
866 MessageLoop loop(message_loop_type); | 1221 MessageLoop loop(message_loop_type); |
867 | 1222 |
868 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); | 1223 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); |
869 TaskList order; | 1224 TaskList order; |
870 MessageLoop::current()->PostTask(FROM_HERE, | 1225 MessageLoop::current()->PostTask(FROM_HERE, |
871 new RecursiveTask(2, &order, 1, false)); | 1226 new RecursiveTask(2, &order, 1, false)); |
872 MessageLoop::current()->PostTask(FROM_HERE, | 1227 MessageLoop::current()->PostTask(FROM_HERE, |
873 new RecursiveTask(2, &order, 2, false)); | 1228 new RecursiveTask(2, &order, 2, false)); |
874 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3)); | 1229 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3)); |
(...skipping 11 matching lines...) Expand all Loading... | |
886 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true)); | 1241 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true)); |
887 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false)); | 1242 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false)); |
888 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true)); | 1243 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true)); |
889 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false)); | 1244 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false)); |
890 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); | 1245 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); |
891 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); | 1246 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); |
892 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true)); | 1247 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true)); |
893 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false)); | 1248 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false)); |
894 } | 1249 } |
895 | 1250 |
1251 void RunTest_RecursiveSupportClosure1(MessageLoop::Type message_loop_type) { | |
1252 MessageLoop loop(message_loop_type); | |
1253 | |
1254 TaskList order; | |
1255 RecursiveTask recursive1(2, &order, 1, true); | |
1256 RecursiveTask recursive2(2, &order, 2, true); | |
1257 QuitTask quit_task(&order, 3); | |
1258 MessageLoop::current()->PostClosure( | |
1259 FROM_HERE, | |
1260 base::Bind(&RecursiveTask::Run, base::Unretained(&recursive1))); | |
1261 MessageLoop::current()->PostClosure( | |
1262 FROM_HERE, | |
1263 base::Bind(&RecursiveTask::Run, base::Unretained(&recursive2))); | |
1264 MessageLoop::current()->PostClosure( | |
1265 FROM_HERE, | |
1266 base::Bind(&QuitTask::Run, base::Unretained(&quit_task))); | |
1267 | |
1268 MessageLoop::current()->Run(); | |
1269 | |
1270 // FIFO order. | |
1271 ASSERT_EQ(14U, order.size()); | |
1272 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true)); | |
1273 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false)); | |
1274 EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true)); | |
1275 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false)); | |
1276 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); | |
1277 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); | |
1278 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true)); | |
1279 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false)); | |
1280 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true)); | |
1281 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false)); | |
1282 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); | |
1283 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); | |
1284 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true)); | |
1285 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false)); | |
1286 } | |
1287 | |
896 void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) { | 1288 void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) { |
897 MessageLoop loop(message_loop_type); | 1289 MessageLoop loop(message_loop_type); |
898 | 1290 |
899 TaskList order; | 1291 TaskList order; |
900 MessageLoop::current()->PostTask(FROM_HERE, | 1292 MessageLoop::current()->PostTask(FROM_HERE, |
901 new RecursiveTask(2, &order, 1, true)); | 1293 new RecursiveTask(2, &order, 1, true)); |
902 MessageLoop::current()->PostTask(FROM_HERE, | 1294 MessageLoop::current()->PostTask(FROM_HERE, |
903 new RecursiveTask(2, &order, 2, true)); | 1295 new RecursiveTask(2, &order, 2, true)); |
904 MessageLoop::current()->PostTask(FROM_HERE, | 1296 MessageLoop::current()->PostTask(FROM_HERE, |
905 new QuitTask(&order, 3)); | 1297 new QuitTask(&order, 3)); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1030 RunStart(); | 1422 RunStart(); |
1031 bool old_state = MessageLoop::current()->NestableTasksAllowed(); | 1423 bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
1032 MessageLoop::current()->SetNestableTasksAllowed(true); | 1424 MessageLoop::current()->SetNestableTasksAllowed(true); |
1033 MessageLoop::current()->RunAllPending(); | 1425 MessageLoop::current()->RunAllPending(); |
1034 MessageLoop::current()->SetNestableTasksAllowed(old_state); | 1426 MessageLoop::current()->SetNestableTasksAllowed(old_state); |
1035 RunEnd(); | 1427 RunEnd(); |
1036 } | 1428 } |
1037 }; | 1429 }; |
1038 | 1430 |
1039 // Tests that non nestable tasks run in FIFO if there are no nested loops. | 1431 // Tests that non nestable tasks run in FIFO if there are no nested loops. |
1432 void RunTest_NonNestableClosureWithNoNesting( | |
1433 MessageLoop::Type message_loop_type) { | |
1434 MessageLoop loop(message_loop_type); | |
1435 | |
1436 TaskList order; | |
1437 | |
1438 OrderedTasks order1(&order, 1); | |
1439 OrderedTasks order2(&order, 2); | |
1440 QuitTask quit_task(&order, 3); | |
1441 MessageLoop::current()->PostNonNestableClosure( | |
1442 FROM_HERE, | |
1443 base::Bind(&OrderedTasks::Run, base::Unretained(&order1))); | |
1444 MessageLoop::current()->PostClosure( | |
1445 FROM_HERE, | |
1446 base::Bind(&OrderedTasks::Run, base::Unretained(&order2))); | |
1447 MessageLoop::current()->PostClosure( | |
1448 FROM_HERE, | |
1449 base::Bind(&QuitTask::Run, base::Unretained(&quit_task))); | |
1450 MessageLoop::current()->Run(); | |
1451 | |
1452 // FIFO order. | |
1453 ASSERT_EQ(6U, order.size()); | |
1454 EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true)); | |
1455 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false)); | |
1456 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true)); | |
1457 EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false)); | |
1458 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); | |
1459 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); | |
1460 } | |
1461 | |
1462 // Tests that non nestable tasks run in FIFO if there are no nested loops. | |
1040 void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type) { | 1463 void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type) { |
1041 MessageLoop loop(message_loop_type); | 1464 MessageLoop loop(message_loop_type); |
1042 | 1465 |
1043 TaskList order; | 1466 TaskList order; |
1044 | 1467 |
1045 Task* task = new OrderedTasks(&order, 1); | 1468 Task* task = new OrderedTasks(&order, 1); |
1046 MessageLoop::current()->PostNonNestableTask(FROM_HERE, task); | 1469 MessageLoop::current()->PostNonNestableTask(FROM_HERE, task); |
1047 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 2)); | 1470 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 2)); |
1048 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3)); | 1471 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3)); |
1049 MessageLoop::current()->Run(); | 1472 MessageLoop::current()->Run(); |
1050 | 1473 |
1051 // FIFO order. | 1474 // FIFO order. |
1052 ASSERT_EQ(6U, order.size()); | 1475 ASSERT_EQ(6U, order.size()); |
1053 EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true)); | 1476 EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true)); |
1054 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false)); | 1477 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false)); |
1055 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true)); | 1478 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true)); |
1056 EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false)); | 1479 EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false)); |
1057 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); | 1480 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); |
1058 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); | 1481 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); |
1059 } | 1482 } |
1060 | 1483 |
1061 // Tests that non nestable tasks don't run when there's code in the call stack. | 1484 // Tests that non nestable tasks don't run when there's code in the call stack. |
1485 void RunTest_NonNestableClosureInNestedLoop( | |
1486 MessageLoop::Type message_loop_type, | |
1487 bool use_delayed) { | |
1488 MessageLoop loop(message_loop_type); | |
1489 | |
1490 TaskList order; | |
1491 | |
1492 TaskThatPumps task_that_pumps(&order, 1); | |
1493 MessageLoop::current()->PostClosure( | |
1494 FROM_HERE, | |
1495 base::Bind(&TaskThatPumps::Run, base::Unretained(&task_that_pumps))); | |
1496 OrderedTasks ordered1(&order, 2); | |
1497 if (use_delayed) { | |
1498 MessageLoop::current()->PostNonNestableDelayedClosure( | |
1499 FROM_HERE, | |
1500 base::Bind(&OrderedTasks::Run, base::Unretained(&ordered1)), | |
1501 1); | |
1502 } else { | |
1503 MessageLoop::current()->PostNonNestableClosure( | |
1504 FROM_HERE, | |
1505 base::Bind(&OrderedTasks::Run, base::Unretained(&ordered1))); | |
1506 } | |
1507 OrderedTasks ordered3(&order, 3); | |
1508 SleepTask sleep_task(&order, 4, 50); | |
1509 OrderedTasks ordered5(&order, 5); | |
1510 QuitTask quit_task(&order, 6); | |
1511 MessageLoop::current()->PostClosure( | |
1512 FROM_HERE, | |
1513 base::Bind(&OrderedTasks::Run, base::Unretained(&ordered3))); | |
1514 MessageLoop::current()->PostClosure( | |
1515 FROM_HERE, | |
1516 base::Bind(&SleepTask::Run, base::Unretained(&sleep_task))); | |
1517 MessageLoop::current()->PostClosure( | |
1518 FROM_HERE, | |
1519 base::Bind(&OrderedTasks::Run, base::Unretained(&ordered5))); | |
1520 if (use_delayed) { | |
1521 MessageLoop::current()->PostNonNestableDelayedClosure( | |
1522 FROM_HERE, | |
1523 base::Bind(&QuitTask::Run, base::Unretained(&quit_task)), | |
1524 2); | |
1525 } else { | |
1526 MessageLoop::current()->PostNonNestableClosure( | |
1527 FROM_HERE, | |
1528 base::Bind(&QuitTask::Run, base::Unretained(&quit_task))); | |
1529 } | |
1530 | |
1531 MessageLoop::current()->Run(); | |
1532 | |
1533 // FIFO order. | |
1534 ASSERT_EQ(12U, order.size()); | |
1535 EXPECT_EQ(order[ 0], TaskItem(PUMPS, 1, true)); | |
1536 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 3, true)); | |
1537 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 3, false)); | |
1538 EXPECT_EQ(order[ 3], TaskItem(SLEEP, 4, true)); | |
1539 EXPECT_EQ(order[ 4], TaskItem(SLEEP, 4, false)); | |
1540 EXPECT_EQ(order[ 5], TaskItem(ORDERERD, 5, true)); | |
1541 EXPECT_EQ(order[ 6], TaskItem(ORDERERD, 5, false)); | |
1542 EXPECT_EQ(order[ 7], TaskItem(PUMPS, 1, false)); | |
1543 EXPECT_EQ(order[ 8], TaskItem(ORDERERD, 2, true)); | |
1544 EXPECT_EQ(order[ 9], TaskItem(ORDERERD, 2, false)); | |
1545 EXPECT_EQ(order[10], TaskItem(QUITMESSAGELOOP, 6, true)); | |
1546 EXPECT_EQ(order[11], TaskItem(QUITMESSAGELOOP, 6, false)); | |
1547 } | |
1548 | |
1549 // Tests that non nestable tasks don't run when there's code in the call stack. | |
1062 void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type, | 1550 void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type, |
1063 bool use_delayed) { | 1551 bool use_delayed) { |
1064 MessageLoop loop(message_loop_type); | 1552 MessageLoop loop(message_loop_type); |
1065 | 1553 |
1066 TaskList order; | 1554 TaskList order; |
1067 | 1555 |
1068 MessageLoop::current()->PostTask(FROM_HERE, | 1556 MessageLoop::current()->PostTask(FROM_HERE, |
1069 new TaskThatPumps(&order, 1)); | 1557 new TaskThatPumps(&order, 1)); |
1070 Task* task = new OrderedTasks(&order, 2); | 1558 Task* task = new OrderedTasks(&order, 2); |
1071 if (use_delayed) { | 1559 if (use_delayed) { |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1321 | 1809 |
1322 #endif // defined(OS_WIN) | 1810 #endif // defined(OS_WIN) |
1323 | 1811 |
1324 } // namespace | 1812 } // namespace |
1325 | 1813 |
1326 //----------------------------------------------------------------------------- | 1814 //----------------------------------------------------------------------------- |
1327 // Each test is run against each type of MessageLoop. That way we are sure | 1815 // Each test is run against each type of MessageLoop. That way we are sure |
1328 // that message loops work properly in all configurations. Of course, in some | 1816 // that message loops work properly in all configurations. Of course, in some |
1329 // cases, a unit test may only be for a particular type of loop. | 1817 // cases, a unit test may only be for a particular type of loop. |
1330 | 1818 |
1819 TEST(MessageLoopTest, PostClosure) { | |
1820 RunTest_PostClosure(MessageLoop::TYPE_DEFAULT); | |
1821 RunTest_PostClosure(MessageLoop::TYPE_UI); | |
1822 RunTest_PostClosure(MessageLoop::TYPE_IO); | |
1823 } | |
1824 | |
1331 TEST(MessageLoopTest, PostTask) { | 1825 TEST(MessageLoopTest, PostTask) { |
1332 RunTest_PostTask(MessageLoop::TYPE_DEFAULT); | 1826 RunTest_PostTask(MessageLoop::TYPE_DEFAULT); |
1333 RunTest_PostTask(MessageLoop::TYPE_UI); | 1827 RunTest_PostTask(MessageLoop::TYPE_UI); |
1334 RunTest_PostTask(MessageLoop::TYPE_IO); | 1828 RunTest_PostTask(MessageLoop::TYPE_IO); |
1335 } | 1829 } |
1336 | 1830 |
1831 TEST(MessageLoopTest, PostClosure_SEH) { | |
1832 RunTest_PostClosure_SEH(MessageLoop::TYPE_DEFAULT); | |
1833 RunTest_PostClosure_SEH(MessageLoop::TYPE_UI); | |
1834 RunTest_PostClosure_SEH(MessageLoop::TYPE_IO); | |
1835 } | |
1836 | |
1337 TEST(MessageLoopTest, PostTask_SEH) { | 1837 TEST(MessageLoopTest, PostTask_SEH) { |
1338 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT); | 1838 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT); |
1339 RunTest_PostTask_SEH(MessageLoop::TYPE_UI); | 1839 RunTest_PostTask_SEH(MessageLoop::TYPE_UI); |
1340 RunTest_PostTask_SEH(MessageLoop::TYPE_IO); | 1840 RunTest_PostTask_SEH(MessageLoop::TYPE_IO); |
1341 } | 1841 } |
1342 | 1842 |
1843 TEST(MessageLoopTest, PostDelayedClosure_Basic) { | |
1844 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT); | |
1845 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI); | |
1846 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO); | |
1847 } | |
1848 | |
1343 TEST(MessageLoopTest, PostDelayedTask_Basic) { | 1849 TEST(MessageLoopTest, PostDelayedTask_Basic) { |
1344 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT); | 1850 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT); |
1345 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI); | 1851 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI); |
1346 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO); | 1852 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO); |
1347 } | 1853 } |
1348 | 1854 |
1855 TEST(MessageLoopTest, PostDelayedClosure_InDelayOrder) { | |
1856 RunTest_PostDelayedClosure_InDelayOrder(MessageLoop::TYPE_DEFAULT); | |
1857 RunTest_PostDelayedClosure_InDelayOrder(MessageLoop::TYPE_UI); | |
1858 RunTest_PostDelayedClosure_InDelayOrder(MessageLoop::TYPE_IO); | |
1859 } | |
1860 | |
1349 TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) { | 1861 TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) { |
1350 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT); | 1862 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT); |
1351 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI); | 1863 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI); |
1352 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO); | 1864 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO); |
1353 } | 1865 } |
1354 | 1866 |
1867 TEST(MessageLoopTest, PostDelayedClosure_InPostOrder) { | |
1868 RunTest_PostDelayedClosure_InPostOrder(MessageLoop::TYPE_DEFAULT); | |
1869 RunTest_PostDelayedClosure_InPostOrder(MessageLoop::TYPE_UI); | |
1870 RunTest_PostDelayedClosure_InPostOrder(MessageLoop::TYPE_IO); | |
1871 } | |
1872 | |
1355 TEST(MessageLoopTest, PostDelayedTask_InPostOrder) { | 1873 TEST(MessageLoopTest, PostDelayedTask_InPostOrder) { |
1356 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT); | 1874 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT); |
1357 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI); | 1875 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI); |
1358 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO); | 1876 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO); |
1359 } | 1877 } |
1360 | 1878 |
1879 TEST(MessageLoopTest, PostDelayedClosure_InPostOrder_2) { | |
1880 RunTest_PostDelayedClosure_InPostOrder_2(MessageLoop::TYPE_DEFAULT); | |
1881 RunTest_PostDelayedClosure_InPostOrder_2(MessageLoop::TYPE_UI); | |
1882 RunTest_PostDelayedClosure_InPostOrder_2(MessageLoop::TYPE_IO); | |
1883 } | |
1884 | |
1361 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) { | 1885 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) { |
1362 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT); | 1886 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT); |
1363 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); | 1887 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); |
1364 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); | 1888 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); |
1365 } | 1889 } |
1366 | 1890 |
1891 TEST(MessageLoopTest, PostDelayedClosure_InPostOrder_3) { | |
1892 RunTest_PostDelayedClosure_InPostOrder_3(MessageLoop::TYPE_DEFAULT); | |
1893 RunTest_PostDelayedClosure_InPostOrder_3(MessageLoop::TYPE_UI); | |
1894 RunTest_PostDelayedClosure_InPostOrder_3(MessageLoop::TYPE_IO); | |
1895 } | |
1896 | |
1367 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { | 1897 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { |
1368 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); | 1898 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); |
1369 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); | 1899 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); |
1370 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); | 1900 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); |
1371 } | 1901 } |
1372 | 1902 |
1903 TEST(MessageLoopTest, PostDelayedClosure_SharedTimer) { | |
1904 RunTest_PostDelayedClosure_SharedTimer(MessageLoop::TYPE_DEFAULT); | |
1905 RunTest_PostDelayedClosure_SharedTimer(MessageLoop::TYPE_UI); | |
1906 RunTest_PostDelayedClosure_SharedTimer(MessageLoop::TYPE_IO); | |
1907 } | |
1908 | |
1373 TEST(MessageLoopTest, PostDelayedTask_SharedTimer) { | 1909 TEST(MessageLoopTest, PostDelayedTask_SharedTimer) { |
1374 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT); | 1910 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT); |
1375 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI); | 1911 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI); |
1376 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO); | 1912 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO); |
1377 } | 1913 } |
1378 | 1914 |
1379 #if defined(OS_WIN) | 1915 #if defined(OS_WIN) |
1916 TEST(MessageLoopTest, PostDelayedClosure_SharedTimer_SubPump) { | |
1917 RunTest_PostDelayedClosure_SharedTimer_SubPump(); | |
1918 } | |
1919 | |
1380 TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) { | 1920 TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) { |
1381 RunTest_PostDelayedTask_SharedTimer_SubPump(); | 1921 RunTest_PostDelayedTask_SharedTimer_SubPump(); |
1382 } | 1922 } |
1383 #endif | 1923 #endif |
1384 | 1924 |
1385 // TODO(darin): MessageLoop does not support deleting all tasks in the | 1925 // TODO(darin): MessageLoop does not support deleting all tasks in the |
1386 // destructor. | 1926 // destructor. |
1387 // Fails, http://crbug.com/50272. | 1927 // Fails, http://crbug.com/50272. |
1388 TEST(MessageLoopTest, FAILS_EnsureTaskDeletion) { | 1928 TEST(MessageLoopTest, FAILS_EnsureTaskDeletion) { |
1389 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); | 1929 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); |
1390 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); | 1930 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); |
1391 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); | 1931 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); |
1392 } | 1932 } |
1393 | 1933 |
1394 // TODO(darin): MessageLoop does not support deleting all tasks in the | 1934 // TODO(darin): MessageLoop does not support deleting all tasks in the |
1395 // destructor. | 1935 // destructor. |
1396 // Fails, http://crbug.com/50272. | 1936 // Fails, http://crbug.com/50272. |
1397 TEST(MessageLoopTest, FAILS_EnsureTaskDeletion_Chain) { | 1937 TEST(MessageLoopTest, FAILS_EnsureTaskDeletion_Chain) { |
1398 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); | 1938 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); |
1399 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI); | 1939 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI); |
1400 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO); | 1940 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO); |
1401 } | 1941 } |
1402 | 1942 |
1403 #if defined(OS_WIN) | 1943 #if defined(OS_WIN) |
1944 // TODO(ajwong): Port the Crasher tests Closure. | |
1404 TEST(MessageLoopTest, Crasher) { | 1945 TEST(MessageLoopTest, Crasher) { |
1405 RunTest_Crasher(MessageLoop::TYPE_DEFAULT); | 1946 RunTest_Crasher(MessageLoop::TYPE_DEFAULT); |
1406 RunTest_Crasher(MessageLoop::TYPE_UI); | 1947 RunTest_Crasher(MessageLoop::TYPE_UI); |
1407 RunTest_Crasher(MessageLoop::TYPE_IO); | 1948 RunTest_Crasher(MessageLoop::TYPE_IO); |
1408 } | 1949 } |
1409 | 1950 |
1410 TEST(MessageLoopTest, CrasherNasty) { | 1951 TEST(MessageLoopTest, CrasherNasty) { |
1411 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT); | 1952 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT); |
1412 RunTest_CrasherNasty(MessageLoop::TYPE_UI); | 1953 RunTest_CrasherNasty(MessageLoop::TYPE_UI); |
1413 RunTest_CrasherNasty(MessageLoop::TYPE_IO); | 1954 RunTest_CrasherNasty(MessageLoop::TYPE_IO); |
1414 } | 1955 } |
1415 #endif // defined(OS_WIN) | 1956 #endif // defined(OS_WIN) |
1416 | 1957 |
1417 TEST(MessageLoopTest, Nesting) { | 1958 TEST(MessageLoopTest, Nesting) { |
1418 RunTest_Nesting(MessageLoop::TYPE_DEFAULT); | 1959 RunTest_Nesting(MessageLoop::TYPE_DEFAULT); |
1419 RunTest_Nesting(MessageLoop::TYPE_UI); | 1960 RunTest_Nesting(MessageLoop::TYPE_UI); |
1420 RunTest_Nesting(MessageLoop::TYPE_IO); | 1961 RunTest_Nesting(MessageLoop::TYPE_IO); |
1962 RunTest_NestingClosure(MessageLoop::TYPE_DEFAULT); | |
1963 RunTest_NestingClosure(MessageLoop::TYPE_UI); | |
1964 RunTest_NestingClosure(MessageLoop::TYPE_IO); | |
1421 } | 1965 } |
1422 | 1966 |
1423 TEST(MessageLoopTest, RecursiveDenial1) { | 1967 TEST(MessageLoopTest, RecursiveDenial1) { |
1424 RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT); | 1968 RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT); |
1425 RunTest_RecursiveDenial1(MessageLoop::TYPE_UI); | 1969 RunTest_RecursiveDenial1(MessageLoop::TYPE_UI); |
1426 RunTest_RecursiveDenial1(MessageLoop::TYPE_IO); | 1970 RunTest_RecursiveDenial1(MessageLoop::TYPE_IO); |
1971 RunTest_RecursiveDenialClosure1(MessageLoop::TYPE_DEFAULT); | |
1972 RunTest_RecursiveDenialClosure1(MessageLoop::TYPE_UI); | |
1973 RunTest_RecursiveDenialClosure1(MessageLoop::TYPE_IO); | |
1427 } | 1974 } |
1428 | 1975 |
1429 TEST(MessageLoopTest, RecursiveSupport1) { | 1976 TEST(MessageLoopTest, RecursiveSupport1) { |
1430 RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT); | 1977 RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT); |
1431 RunTest_RecursiveSupport1(MessageLoop::TYPE_UI); | 1978 RunTest_RecursiveSupport1(MessageLoop::TYPE_UI); |
1432 RunTest_RecursiveSupport1(MessageLoop::TYPE_IO); | 1979 RunTest_RecursiveSupport1(MessageLoop::TYPE_IO); |
1980 RunTest_RecursiveSupportClosure1(MessageLoop::TYPE_DEFAULT); | |
1981 RunTest_RecursiveSupportClosure1(MessageLoop::TYPE_UI); | |
1982 RunTest_RecursiveSupportClosure1(MessageLoop::TYPE_IO); | |
1433 } | 1983 } |
1434 | 1984 |
1435 #if defined(OS_WIN) | 1985 #if defined(OS_WIN) |
1986 // TODO(ajwong): Port these. | |
1436 // This test occasionally hangs http://crbug.com/44567 | 1987 // This test occasionally hangs http://crbug.com/44567 |
1437 TEST(MessageLoopTest, DISABLED_RecursiveDenial2) { | 1988 TEST(MessageLoopTest, DISABLED_RecursiveDenial2) { |
1438 RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT); | 1989 RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT); |
1439 RunTest_RecursiveDenial2(MessageLoop::TYPE_UI); | 1990 RunTest_RecursiveDenial2(MessageLoop::TYPE_UI); |
1440 RunTest_RecursiveDenial2(MessageLoop::TYPE_IO); | 1991 RunTest_RecursiveDenial2(MessageLoop::TYPE_IO); |
1441 } | 1992 } |
1442 | 1993 |
1443 TEST(MessageLoopTest, RecursiveSupport2) { | 1994 TEST(MessageLoopTest, RecursiveSupport2) { |
1444 // This test requires a UI loop | 1995 // This test requires a UI loop |
1445 RunTest_RecursiveSupport2(MessageLoop::TYPE_UI); | 1996 RunTest_RecursiveSupport2(MessageLoop::TYPE_UI); |
1446 } | 1997 } |
1447 #endif // defined(OS_WIN) | 1998 #endif // defined(OS_WIN) |
1448 | 1999 |
1449 TEST(MessageLoopTest, NonNestableWithNoNesting) { | 2000 TEST(MessageLoopTest, NonNestableWithNoNesting) { |
1450 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT); | 2001 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT); |
1451 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI); | 2002 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI); |
1452 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO); | 2003 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO); |
2004 RunTest_NonNestableClosureWithNoNesting(MessageLoop::TYPE_DEFAULT); | |
2005 RunTest_NonNestableClosureWithNoNesting(MessageLoop::TYPE_UI); | |
2006 RunTest_NonNestableClosureWithNoNesting(MessageLoop::TYPE_IO); | |
1453 } | 2007 } |
1454 | 2008 |
1455 TEST(MessageLoopTest, NonNestableInNestedLoop) { | 2009 TEST(MessageLoopTest, NonNestableInNestedLoop) { |
1456 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false); | 2010 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false); |
1457 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false); | 2011 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false); |
1458 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false); | 2012 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false); |
2013 RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_DEFAULT, false); | |
2014 RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_UI, false); | |
2015 RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_IO, false); | |
1459 } | 2016 } |
1460 | 2017 |
1461 TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { | 2018 TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { |
1462 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); | 2019 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); |
1463 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); | 2020 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); |
1464 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); | 2021 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); |
2022 RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_DEFAULT, true); | |
2023 RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_UI, true); | |
2024 RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_IO, true); | |
1465 } | 2025 } |
1466 | 2026 |
1467 class DummyTask : public Task { | 2027 class DummyTask : public Task { |
1468 public: | 2028 public: |
1469 explicit DummyTask(int num_tasks) : num_tasks_(num_tasks) {} | 2029 explicit DummyTask(int num_tasks) : num_tasks_(num_tasks) {} |
1470 | 2030 |
1471 virtual void Run() { | 2031 virtual void Run() { |
1472 if (num_tasks_ > 1) { | 2032 if (num_tasks_ > 1) { |
1473 MessageLoop::current()->PostTask( | 2033 MessageLoop::current()->PostTask( |
1474 FROM_HERE, | 2034 FROM_HERE, |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1707 loop->PostDelayedTask( | 2267 loop->PostDelayedTask( |
1708 FROM_HERE, | 2268 FROM_HERE, |
1709 new RunAtDestructionTask(&task_destroyed, &destruction_observer_called), | 2269 new RunAtDestructionTask(&task_destroyed, &destruction_observer_called), |
1710 kDelayMS); | 2270 kDelayMS); |
1711 delete loop; | 2271 delete loop; |
1712 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); | 2272 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); |
1713 // The task should have been destroyed when we deleted the loop. | 2273 // The task should have been destroyed when we deleted the loop. |
1714 EXPECT_TRUE(task_destroyed); | 2274 EXPECT_TRUE(task_destroyed); |
1715 EXPECT_TRUE(destruction_observer_called); | 2275 EXPECT_TRUE(destruction_observer_called); |
1716 } | 2276 } |
OLD | NEW |