blob: 22177370ae5a34479eed01c7644a3a286961df9e [file] [log] [blame]
[email protected]b026e35d2010-10-19 02:31:031// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]b7243c42010-07-23 05:23:135#include <vector>
6
[email protected]9cfb89a2010-06-09 21:20:417#include "base/eintr_wrapper.h"
initial.commitd7cae122008-07-26 21:49:388#include "base/logging.h"
9#include "base/message_loop.h"
initial.commitd7cae122008-07-26 21:49:3810#include "base/ref_counted.h"
[email protected]9cfb89a2010-06-09 21:20:4111#include "base/task.h"
[email protected]ce072a72010-12-31 20:02:1612#include "base/threading/platform_thread.h"
[email protected]b16ef312008-08-19 18:36:2313#include "base/thread.h"
initial.commitd7cae122008-07-26 21:49:3814#include "testing/gtest/include/gtest/gtest.h"
15
[email protected]295039bd2008-08-15 04:32:5716#if defined(OS_WIN)
17#include "base/message_pump_win.h"
[email protected]b16ef312008-08-19 18:36:2318#include "base/scoped_handle.h"
[email protected]295039bd2008-08-15 04:32:5719#endif
[email protected]f74c8962009-04-22 20:01:3620#if defined(OS_POSIX)
21#include "base/message_pump_libevent.h"
22#endif
[email protected]295039bd2008-08-15 04:32:5723
[email protected]ce072a72010-12-31 20:02:1624using base::PlatformThread;
[email protected]4d9bdfaf2008-08-26 05:53:5725using base::Thread;
[email protected]e1acf6f2008-10-27 20:43:3326using base::Time;
27using base::TimeDelta;
[email protected]4d9bdfaf2008-08-26 05:53:5728
29// TODO(darin): Platform-specific MessageLoop tests should be grouped together
30// to avoid chopping this file up with so many #ifdefs.
[email protected]b16ef312008-08-19 18:36:2331
initial.commitd7cae122008-07-26 21:49:3832namespace {
33
[email protected]4d9bdfaf2008-08-26 05:53:5734class MessageLoopTest : public testing::Test {};
initial.commitd7cae122008-07-26 21:49:3835
36class Foo : public base::RefCounted<Foo> {
37 public:
38 Foo() : test_count_(0) {
39 }
40
41 void Test0() {
42 ++test_count_;
43 }
44
45 void Test1ConstRef(const std::string& a) {
46 ++test_count_;
47 result_.append(a);
48 }
49
50 void Test1Ptr(std::string* a) {
51 ++test_count_;
52 result_.append(*a);
53 }
54
55 void Test1Int(int a) {
56 test_count_ += a;
57 }
58
59 void Test2Ptr(std::string* a, std::string* b) {
60 ++test_count_;
61 result_.append(*a);
62 result_.append(*b);
63 }
64
65 void Test2Mixed(const std::string& a, std::string* b) {
66 ++test_count_;
67 result_.append(a);
68 result_.append(*b);
69 }
70
71 int test_count() const { return test_count_; }
72 const std::string& result() const { return result_; }
73
74 private:
[email protected]877d55d2009-11-05 21:53:0875 friend class base::RefCounted<Foo>;
76
77 ~Foo() {}
78
initial.commitd7cae122008-07-26 21:49:3879 int test_count_;
80 std::string result_;
81};
82
83class QuitMsgLoop : public base::RefCounted<QuitMsgLoop> {
84 public:
85 void QuitNow() {
86 MessageLoop::current()->Quit();
87 }
[email protected]877d55d2009-11-05 21:53:0888
89 private:
90 friend class base::RefCounted<QuitMsgLoop>;
91
92 ~QuitMsgLoop() {}
initial.commitd7cae122008-07-26 21:49:3893};
94
[email protected]4d9bdfaf2008-08-26 05:53:5795void RunTest_PostTask(MessageLoop::Type message_loop_type) {
96 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:3897
initial.commitd7cae122008-07-26 21:49:3898 // Add tests to message loop
[email protected]ad8e04a2010-11-01 04:16:2799 scoped_refptr<Foo> foo(new Foo());
initial.commitd7cae122008-07-26 21:49:38100 std::string a("a"), b("b"), c("c"), d("d");
101 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
102 foo.get(), &Foo::Test0));
103 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
104 foo.get(), &Foo::Test1ConstRef, a));
105 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
106 foo.get(), &Foo::Test1Ptr, &b));
107 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
108 foo.get(), &Foo::Test1Int, 100));
109 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
110 foo.get(), &Foo::Test2Ptr, &a, &c));
111 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
112 foo.get(), &Foo::Test2Mixed, a, &d));
113
114 // After all tests, post a message that will shut down the message loop
[email protected]ad8e04a2010-11-01 04:16:27115 scoped_refptr<QuitMsgLoop> quit(new QuitMsgLoop());
initial.commitd7cae122008-07-26 21:49:38116 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
117 quit.get(), &QuitMsgLoop::QuitNow));
118
119 // Now kick things off
120 MessageLoop::current()->Run();
121
122 EXPECT_EQ(foo->test_count(), 105);
123 EXPECT_EQ(foo->result(), "abacad");
124}
125
[email protected]4d9bdfaf2008-08-26 05:53:57126void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) {
127 MessageLoop loop(message_loop_type);
128
initial.commitd7cae122008-07-26 21:49:38129 // Add tests to message loop
[email protected]ad8e04a2010-11-01 04:16:27130 scoped_refptr<Foo> foo(new Foo());
initial.commitd7cae122008-07-26 21:49:38131 std::string a("a"), b("b"), c("c"), d("d");
132 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
133 foo.get(), &Foo::Test0));
134 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
135 foo.get(), &Foo::Test1ConstRef, a));
136 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
137 foo.get(), &Foo::Test1Ptr, &b));
138 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
139 foo.get(), &Foo::Test1Int, 100));
140 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
141 foo.get(), &Foo::Test2Ptr, &a, &c));
142 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
143 foo.get(), &Foo::Test2Mixed, a, &d));
144
145 // After all tests, post a message that will shut down the message loop
[email protected]ad8e04a2010-11-01 04:16:27146 scoped_refptr<QuitMsgLoop> quit(new QuitMsgLoop());
initial.commitd7cae122008-07-26 21:49:38147 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
148 quit.get(), &QuitMsgLoop::QuitNow));
149
150 // Now kick things off with the SEH block active.
151 MessageLoop::current()->set_exception_restoration(true);
152 MessageLoop::current()->Run();
153 MessageLoop::current()->set_exception_restoration(false);
154
155 EXPECT_EQ(foo->test_count(), 105);
156 EXPECT_EQ(foo->result(), "abacad");
157}
158
[email protected]752578562008-09-07 08:08:29159// This class runs slowly to simulate a large amount of work being done.
160class SlowTask : public Task {
161 public:
162 SlowTask(int pause_ms, int* quit_counter)
163 : pause_ms_(pause_ms), quit_counter_(quit_counter) {
164 }
165 virtual void Run() {
166 PlatformThread::Sleep(pause_ms_);
167 if (--(*quit_counter_) == 0)
168 MessageLoop::current()->Quit();
169 }
170 private:
171 int pause_ms_;
172 int* quit_counter_;
173};
174
175// This class records the time when Run was called in a Time object, which is
176// useful for building a variety of MessageLoop tests.
177class RecordRunTimeTask : public SlowTask {
178 public:
179 RecordRunTimeTask(Time* run_time, int* quit_counter)
180 : SlowTask(10, quit_counter), run_time_(run_time) {
181 }
182 virtual void Run() {
183 *run_time_ = Time::Now();
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 }
189 private:
190 Time* run_time_;
191};
192
193void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) {
194 MessageLoop loop(message_loop_type);
195
196 // Test that PostDelayedTask results in a delayed task.
197
198 const int kDelayMS = 100;
199
200 int num_tasks = 1;
201 Time run_time;
202
203 loop.PostDelayedTask(
204 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), kDelayMS);
205
206 Time time_before_run = Time::Now();
207 loop.Run();
208 Time time_after_run = Time::Now();
209
210 EXPECT_EQ(0, num_tasks);
211 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds());
212}
213
214void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type) {
215 MessageLoop loop(message_loop_type);
216
217 // Test that two tasks with different delays run in the right order.
218
219 int num_tasks = 2;
220 Time run_time1, run_time2;
221
222 loop.PostDelayedTask(
223 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
225 // test could fail.
226 loop.PostDelayedTask(
227 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10);
228
229 loop.Run();
230 EXPECT_EQ(0, num_tasks);
231
232 EXPECT_TRUE(run_time2 < run_time1);
233}
234
235void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type) {
236 MessageLoop loop(message_loop_type);
237
238 // Test that two tasks with the same delay run in the order in which they
239 // were posted.
240 //
241 // 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
243 // posted at the exact same time. It would be nice if the API allowed us to
244 // specify the desired run time.
245
246 const int kDelayMS = 100;
247
248 int num_tasks = 2;
249 Time run_time1, run_time2;
250
251 loop.PostDelayedTask(
252 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), kDelayMS);
253 loop.PostDelayedTask(
254 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), kDelayMS);
255
256 loop.Run();
257 EXPECT_EQ(0, num_tasks);
258
259 EXPECT_TRUE(run_time1 < run_time2);
260}
261
[email protected]32cda29d2008-10-09 23:58:43262void RunTest_PostDelayedTask_InPostOrder_2(
263 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29264 MessageLoop loop(message_loop_type);
265
266 // Test that a delayed task still runs after a normal tasks even if the
267 // normal tasks take a long time to run.
268
269 const int kPauseMS = 50;
270
271 int num_tasks = 2;
272 Time run_time;
273
274 loop.PostTask(
275 FROM_HERE, new SlowTask(kPauseMS, &num_tasks));
276 loop.PostDelayedTask(
277 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 10);
278
279 Time time_before_run = Time::Now();
280 loop.Run();
281 Time time_after_run = Time::Now();
282
283 EXPECT_EQ(0, num_tasks);
284
285 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds());
286}
287
[email protected]32cda29d2008-10-09 23:58:43288void RunTest_PostDelayedTask_InPostOrder_3(
289 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29290 MessageLoop loop(message_loop_type);
291
292 // 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
294 // 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
296 // delayed task's delay has passed.
297
298 int num_tasks = 11;
299 Time run_time1, run_time2;
300
301 // Clutter the ML with tasks.
302 for (int i = 1; i < num_tasks; ++i)
303 loop.PostTask(FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks));
304
305 loop.PostDelayedTask(
306 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1);
307
308 loop.Run();
309 EXPECT_EQ(0, num_tasks);
310
311 EXPECT_TRUE(run_time2 > run_time1);
312}
313
[email protected]72deacd2008-09-23 19:19:20314void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) {
315 MessageLoop loop(message_loop_type);
316
317 // 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.
319
320 // By setting num_tasks to 1, we ensure that the first task to run causes the
321 // run loop to exit.
322 int num_tasks = 1;
323 Time run_time1, run_time2;
324
325 loop.PostDelayedTask(
326 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), 1000000);
327 loop.PostDelayedTask(
328 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10);
329
330 Time start_time = Time::Now();
331
332 loop.Run();
333 EXPECT_EQ(0, num_tasks);
334
335 // Ensure that we ran in far less time than the slower timer.
[email protected]4615f1982008-09-23 19:22:33336 TimeDelta total_time = Time::Now() - start_time;
337 EXPECT_GT(5000, total_time.InMilliseconds());
[email protected]32cda29d2008-10-09 23:58:43338
[email protected]72deacd2008-09-23 19:19:20339 // In case both timers somehow run at nearly the same time, sleep a little
340 // and then run all pending to force them both to have run. This is just
341 // encouraging flakiness if there is any.
342 PlatformThread::Sleep(100);
343 loop.RunAllPending();
344
345 EXPECT_TRUE(run_time1.is_null());
346 EXPECT_FALSE(run_time2.is_null());
347}
348
349#if defined(OS_WIN)
350
351class SubPumpTask : public Task {
352 public:
353 virtual void Run() {
354 MessageLoop::current()->SetNestableTasksAllowed(true);
355 MSG msg;
[email protected]32cda29d2008-10-09 23:58:43356 while (GetMessage(&msg, NULL, 0, 0)) {
[email protected]72deacd2008-09-23 19:19:20357 TranslateMessage(&msg);
358 DispatchMessage(&msg);
359 }
360 MessageLoop::current()->Quit();
361 }
362};
363
364class SubPumpQuitTask : public Task {
365 public:
366 SubPumpQuitTask() {
367 }
368 virtual void Run() {
369 PostQuitMessage(0);
370 }
371};
372
373void RunTest_PostDelayedTask_SharedTimer_SubPump() {
374 MessageLoop loop(MessageLoop::TYPE_UI);
375
376 // 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.
378
379 // By setting num_tasks to 1, we ensure that the first task to run causes the
380 // run loop to exit.
381 int num_tasks = 1;
382 Time run_time;
383
384 loop.PostTask(FROM_HERE, new SubPumpTask());
385
386 // This very delayed task should never run.
387 loop.PostDelayedTask(
388 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 1000000);
389
390 // This slightly delayed task should run from within SubPumpTask::Run().
391 loop.PostDelayedTask(
392 FROM_HERE, new SubPumpQuitTask(), 10);
393
394 Time start_time = Time::Now();
395
396 loop.Run();
397 EXPECT_EQ(1, num_tasks);
398
399 // Ensure that we ran in far less time than the slower timer.
[email protected]4615f1982008-09-23 19:22:33400 TimeDelta total_time = Time::Now() - start_time;
401 EXPECT_GT(5000, total_time.InMilliseconds());
[email protected]72deacd2008-09-23 19:19:20402
403 // In case both timers somehow run at nearly the same time, sleep a little
404 // and then run all pending to force them both to have run. This is just
405 // encouraging flakiness if there is any.
406 PlatformThread::Sleep(100);
407 loop.RunAllPending();
408
409 EXPECT_TRUE(run_time.is_null());
410}
411
412#endif // defined(OS_WIN)
413
[email protected]001747c2008-09-10 00:37:07414class RecordDeletionTask : public Task {
415 public:
416 RecordDeletionTask(Task* post_on_delete, bool* was_deleted)
417 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) {
418 }
419 ~RecordDeletionTask() {
420 *was_deleted_ = true;
421 if (post_on_delete_)
422 MessageLoop::current()->PostTask(FROM_HERE, post_on_delete_);
423 }
424 virtual void Run() {}
425 private:
426 Task* post_on_delete_;
427 bool* was_deleted_;
428};
429
430void RunTest_EnsureTaskDeletion(MessageLoop::Type message_loop_type) {
431 bool a_was_deleted = false;
432 bool b_was_deleted = false;
433 {
434 MessageLoop loop(message_loop_type);
435 loop.PostTask(
436 FROM_HERE, new RecordDeletionTask(NULL, &a_was_deleted));
437 loop.PostDelayedTask(
438 FROM_HERE, new RecordDeletionTask(NULL, &b_was_deleted), 1000);
439 }
440 EXPECT_TRUE(a_was_deleted);
441 EXPECT_TRUE(b_was_deleted);
442}
443
444void RunTest_EnsureTaskDeletion_Chain(MessageLoop::Type message_loop_type) {
445 bool a_was_deleted = false;
446 bool b_was_deleted = false;
447 bool c_was_deleted = false;
448 {
449 MessageLoop loop(message_loop_type);
450 RecordDeletionTask* a = new RecordDeletionTask(NULL, &a_was_deleted);
451 RecordDeletionTask* b = new RecordDeletionTask(a, &b_was_deleted);
452 RecordDeletionTask* c = new RecordDeletionTask(b, &c_was_deleted);
453 loop.PostTask(FROM_HERE, c);
454 }
455 EXPECT_TRUE(a_was_deleted);
456 EXPECT_TRUE(b_was_deleted);
457 EXPECT_TRUE(c_was_deleted);
458}
459
initial.commitd7cae122008-07-26 21:49:38460class NestingTest : public Task {
461 public:
462 explicit NestingTest(int* depth) : depth_(depth) {
463 }
464 void Run() {
465 if (*depth_ > 0) {
466 *depth_ -= 1;
467 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_));
468
469 MessageLoop::current()->SetNestableTasksAllowed(true);
470 MessageLoop::current()->Run();
471 }
472 MessageLoop::current()->Quit();
473 }
474 private:
475 int* depth_;
476};
477
[email protected]b16ef312008-08-19 18:36:23478#if defined(OS_WIN)
479
initial.commitd7cae122008-07-26 21:49:38480LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) {
481 ADD_FAILURE() << "bad exception handler";
482 ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode);
483 return EXCEPTION_EXECUTE_HANDLER;
484}
485
486// This task throws an SEH exception: initially write to an invalid address.
487// If the right SEH filter is installed, it will fix the error.
488class CrasherTask : public Task {
489 public:
490 // Ctor. If trash_SEH_handler is true, the task will override the unhandled
491 // exception handler with one sure to crash this test.
492 explicit CrasherTask(bool trash_SEH_handler)
493 : trash_SEH_handler_(trash_SEH_handler) {
494 }
495 void Run() {
[email protected]b16ef312008-08-19 18:36:23496 PlatformThread::Sleep(1);
initial.commitd7cae122008-07-26 21:49:38497 if (trash_SEH_handler_)
498 ::SetUnhandledExceptionFilter(&BadExceptionHandler);
499 // Generate a SEH fault. We do it in asm to make sure we know how to undo
500 // the damage.
[email protected]c88873922008-07-30 13:02:03501
502#if defined(_M_IX86)
503
initial.commitd7cae122008-07-26 21:49:38504 __asm {
505 mov eax, dword ptr [CrasherTask::bad_array_]
506 mov byte ptr [eax], 66
507 }
[email protected]c88873922008-07-30 13:02:03508
509#elif defined(_M_X64)
510
511 bad_array_[0] = 66;
512
[email protected]b16ef312008-08-19 18:36:23513#else
514#error "needs architecture support"
[email protected]c88873922008-07-30 13:02:03515#endif
516
initial.commitd7cae122008-07-26 21:49:38517 MessageLoop::current()->Quit();
518 }
519 // Points the bad array to a valid memory location.
520 static void FixError() {
521 bad_array_ = &valid_store_;
522 }
523
524 private:
525 bool trash_SEH_handler_;
526 static volatile char* bad_array_;
527 static char valid_store_;
528};
529
530volatile char* CrasherTask::bad_array_ = 0;
531char CrasherTask::valid_store_ = 0;
532
533// This SEH filter fixes the problem and retries execution. Fixing requires
534// that the last instruction: mov eax, [CrasherTask::bad_array_] to be retried
535// so we move the instruction pointer 5 bytes back.
536LONG WINAPI HandleCrasherTaskException(EXCEPTION_POINTERS *ex_info) {
537 if (ex_info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
538 return EXCEPTION_EXECUTE_HANDLER;
539
540 CrasherTask::FixError();
[email protected]c88873922008-07-30 13:02:03541
542#if defined(_M_IX86)
543
initial.commitd7cae122008-07-26 21:49:38544 ex_info->ContextRecord->Eip -= 5;
[email protected]c88873922008-07-30 13:02:03545
546#elif defined(_M_X64)
547
548 ex_info->ContextRecord->Rip -= 5;
549
550#endif
551
initial.commitd7cae122008-07-26 21:49:38552 return EXCEPTION_CONTINUE_EXECUTION;
553}
554
[email protected]4d9bdfaf2008-08-26 05:53:57555void RunTest_Crasher(MessageLoop::Type message_loop_type) {
556 MessageLoop loop(message_loop_type);
[email protected]b16ef312008-08-19 18:36:23557
initial.commitd7cae122008-07-26 21:49:38558 if (::IsDebuggerPresent())
559 return;
560
561 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
562 ::SetUnhandledExceptionFilter(&HandleCrasherTaskException);
563
564 MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(false));
565 MessageLoop::current()->set_exception_restoration(true);
566 MessageLoop::current()->Run();
567 MessageLoop::current()->set_exception_restoration(false);
568
569 ::SetUnhandledExceptionFilter(old_SEH_filter);
570}
571
[email protected]4d9bdfaf2008-08-26 05:53:57572void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) {
573 MessageLoop loop(message_loop_type);
[email protected]32cda29d2008-10-09 23:58:43574
initial.commitd7cae122008-07-26 21:49:38575 if (::IsDebuggerPresent())
576 return;
577
578 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
579 ::SetUnhandledExceptionFilter(&HandleCrasherTaskException);
580
581 MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(true));
582 MessageLoop::current()->set_exception_restoration(true);
583 MessageLoop::current()->Run();
584 MessageLoop::current()->set_exception_restoration(false);
585
586 ::SetUnhandledExceptionFilter(old_SEH_filter);
587}
588
[email protected]295039bd2008-08-15 04:32:57589#endif // defined(OS_WIN)
590
[email protected]4d9bdfaf2008-08-26 05:53:57591void RunTest_Nesting(MessageLoop::Type message_loop_type) {
592 MessageLoop loop(message_loop_type);
[email protected]c88873922008-07-30 13:02:03593
initial.commitd7cae122008-07-26 21:49:38594 int depth = 100;
595 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(&depth));
596 MessageLoop::current()->Run();
597 EXPECT_EQ(depth, 0);
598}
599
initial.commitd7cae122008-07-26 21:49:38600const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test";
601
602enum TaskType {
603 MESSAGEBOX,
604 ENDDIALOG,
605 RECURSIVE,
606 TIMEDMESSAGELOOP,
607 QUITMESSAGELOOP,
608 ORDERERD,
609 PUMPS,
[email protected]c4280a92009-06-25 19:23:11610 SLEEP,
initial.commitd7cae122008-07-26 21:49:38611};
612
613// Saves the order in which the tasks executed.
614struct TaskItem {
615 TaskItem(TaskType t, int c, bool s)
616 : type(t),
617 cookie(c),
618 start(s) {
619 }
620
621 TaskType type;
622 int cookie;
623 bool start;
624
625 bool operator == (const TaskItem& other) const {
626 return type == other.type && cookie == other.cookie && start == other.start;
627 }
628};
629
630typedef std::vector<TaskItem> TaskList;
631
632std::ostream& operator <<(std::ostream& os, TaskType type) {
633 switch (type) {
634 case MESSAGEBOX: os << "MESSAGEBOX"; break;
635 case ENDDIALOG: os << "ENDDIALOG"; break;
636 case RECURSIVE: os << "RECURSIVE"; break;
637 case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break;
638 case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break;
639 case ORDERERD: os << "ORDERERD"; break;
640 case PUMPS: os << "PUMPS"; break;
[email protected]c4280a92009-06-25 19:23:11641 case SLEEP: os << "SLEEP"; break;
initial.commitd7cae122008-07-26 21:49:38642 default:
643 NOTREACHED();
644 os << "Unknown TaskType";
645 break;
646 }
647 return os;
648}
649
650std::ostream& operator <<(std::ostream& os, const TaskItem& item) {
651 if (item.start)
652 return os << item.type << " " << item.cookie << " starts";
653 else
654 return os << item.type << " " << item.cookie << " ends";
655}
656
657// Saves the order the tasks ran.
658class OrderedTasks : public Task {
659 public:
660 OrderedTasks(TaskList* order, int cookie)
661 : order_(order),
662 type_(ORDERERD),
663 cookie_(cookie) {
664 }
665 OrderedTasks(TaskList* order, TaskType type, int cookie)
666 : order_(order),
667 type_(type),
668 cookie_(cookie) {
669 }
670
671 void RunStart() {
672 TaskItem item(type_, cookie_, true);
[email protected]b026e35d2010-10-19 02:31:03673 DVLOG(1) << item;
initial.commitd7cae122008-07-26 21:49:38674 order_->push_back(item);
675 }
676 void RunEnd() {
677 TaskItem item(type_, cookie_, false);
[email protected]b026e35d2010-10-19 02:31:03678 DVLOG(1) << item;
initial.commitd7cae122008-07-26 21:49:38679 order_->push_back(item);
680 }
681
682 virtual void Run() {
683 RunStart();
684 RunEnd();
685 }
686
687 protected:
688 TaskList* order() const {
689 return order_;
690 }
691
692 int cookie() const {
693 return cookie_;
694 }
695
696 private:
697 TaskList* order_;
698 TaskType type_;
699 int cookie_;
700};
701
[email protected]b16ef312008-08-19 18:36:23702#if defined(OS_WIN)
703
initial.commitd7cae122008-07-26 21:49:38704// MessageLoop implicitly start a "modal message loop". Modal dialog boxes,
705// common controls (like OpenFile) and StartDoc printing function can cause
706// implicit message loops.
707class MessageBoxTask : public OrderedTasks {
708 public:
709 MessageBoxTask(TaskList* order, int cookie, bool is_reentrant)
710 : OrderedTasks(order, MESSAGEBOX, cookie),
711 is_reentrant_(is_reentrant) {
712 }
713
714 virtual void Run() {
715 RunStart();
716 if (is_reentrant_)
717 MessageLoop::current()->SetNestableTasksAllowed(true);
718 MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK);
719 RunEnd();
720 }
721
722 private:
723 bool is_reentrant_;
724};
725
726// Will end the MessageBox.
727class EndDialogTask : public OrderedTasks {
728 public:
729 EndDialogTask(TaskList* order, int cookie)
730 : OrderedTasks(order, ENDDIALOG, cookie) {
731 }
732
733 virtual void Run() {
734 RunStart();
735 HWND window = GetActiveWindow();
736 if (window != NULL) {
737 EXPECT_NE(EndDialog(window, IDCONTINUE), 0);
738 // Cheap way to signal that the window wasn't found if RunEnd() isn't
739 // called.
740 RunEnd();
741 }
742 }
743};
744
[email protected]b16ef312008-08-19 18:36:23745#endif // defined(OS_WIN)
746
initial.commitd7cae122008-07-26 21:49:38747class RecursiveTask : public OrderedTasks {
748 public:
749 RecursiveTask(int depth, TaskList* order, int cookie, bool is_reentrant)
750 : OrderedTasks(order, RECURSIVE, cookie),
751 depth_(depth),
752 is_reentrant_(is_reentrant) {
753 }
754
755 virtual void Run() {
756 RunStart();
757 if (depth_ > 0) {
758 if (is_reentrant_)
759 MessageLoop::current()->SetNestableTasksAllowed(true);
760 MessageLoop::current()->PostTask(FROM_HERE,
761 new RecursiveTask(depth_ - 1, order(), cookie(), is_reentrant_));
762 }
763 RunEnd();
764 }
765
766 private:
767 int depth_;
768 bool is_reentrant_;
769};
770
771class QuitTask : public OrderedTasks {
772 public:
773 QuitTask(TaskList* order, int cookie)
774 : OrderedTasks(order, QUITMESSAGELOOP, cookie) {
775 }
776
777 virtual void Run() {
778 RunStart();
779 MessageLoop::current()->Quit();
780 RunEnd();
781 }
782};
783
[email protected]c4280a92009-06-25 19:23:11784class SleepTask : public OrderedTasks {
785 public:
786 SleepTask(TaskList* order, int cookie, int ms)
787 : OrderedTasks(order, SLEEP, cookie), ms_(ms) {
788 }
789
790 virtual void Run() {
791 RunStart();
792 PlatformThread::Sleep(ms_);
793 RunEnd();
794 }
795
796 private:
797 int ms_;
798};
799
[email protected]295039bd2008-08-15 04:32:57800#if defined(OS_WIN)
801
initial.commitd7cae122008-07-26 21:49:38802class Recursive2Tasks : public Task {
803 public:
804 Recursive2Tasks(MessageLoop* target,
805 HANDLE event,
806 bool expect_window,
807 TaskList* order,
808 bool is_reentrant)
809 : target_(target),
810 event_(event),
811 expect_window_(expect_window),
812 order_(order),
813 is_reentrant_(is_reentrant) {
814 }
815
816 virtual void Run() {
817 target_->PostTask(FROM_HERE,
818 new RecursiveTask(2, order_, 1, is_reentrant_));
819 target_->PostTask(FROM_HERE,
820 new MessageBoxTask(order_, 2, is_reentrant_));
821 target_->PostTask(FROM_HERE,
822 new RecursiveTask(2, order_, 3, is_reentrant_));
823 // The trick here is that for recursive task processing, this task will be
824 // ran _inside_ the MessageBox message loop, dismissing the MessageBox
825 // without a chance.
826 // For non-recursive task processing, this will be executed _after_ the
827 // MessageBox will have been dismissed by the code below, where
828 // expect_window_ is true.
829 target_->PostTask(FROM_HERE, new EndDialogTask(order_, 4));
830 target_->PostTask(FROM_HERE, new QuitTask(order_, 5));
831
832 // Enforce that every tasks are sent before starting to run the main thread
833 // message loop.
834 ASSERT_TRUE(SetEvent(event_));
835
836 // Poll for the MessageBox. Don't do this at home! At the speed we do it,
837 // you will never realize one MessageBox was shown.
838 for (; expect_window_;) {
839 HWND window = FindWindow(L"#32770", kMessageBoxTitle);
840 if (window) {
841 // Dismiss it.
842 for (;;) {
843 HWND button = FindWindowEx(window, NULL, L"Button", NULL);
844 if (button != NULL) {
[email protected]b7243c42010-07-23 05:23:13845 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONDOWN, 0, 0));
846 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONUP, 0, 0));
initial.commitd7cae122008-07-26 21:49:38847 break;
848 }
849 }
850 break;
851 }
852 }
853 }
854
855 private:
856 MessageLoop* target_;
857 HANDLE event_;
858 TaskList* order_;
859 bool expect_window_;
860 bool is_reentrant_;
861};
862
[email protected]295039bd2008-08-15 04:32:57863#endif // defined(OS_WIN)
864
[email protected]4d9bdfaf2008-08-26 05:53:57865void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) {
866 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:38867
initial.commitd7cae122008-07-26 21:49:38868 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
869 TaskList order;
870 MessageLoop::current()->PostTask(FROM_HERE,
871 new RecursiveTask(2, &order, 1, false));
872 MessageLoop::current()->PostTask(FROM_HERE,
873 new RecursiveTask(2, &order, 2, false));
874 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3));
875
876 MessageLoop::current()->Run();
877
878 // FIFO order.
[email protected]b16ef312008-08-19 18:36:23879 ASSERT_EQ(14U, order.size());
initial.commitd7cae122008-07-26 21:49:38880 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
881 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
882 EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true));
883 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false));
884 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
885 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
886 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true));
887 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false));
888 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true));
889 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false));
890 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
891 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
892 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true));
893 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
894}
895
[email protected]4d9bdfaf2008-08-26 05:53:57896void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) {
897 MessageLoop loop(message_loop_type);
[email protected]32cda29d2008-10-09 23:58:43898
initial.commitd7cae122008-07-26 21:49:38899 TaskList order;
900 MessageLoop::current()->PostTask(FROM_HERE,
901 new RecursiveTask(2, &order, 1, true));
902 MessageLoop::current()->PostTask(FROM_HERE,
903 new RecursiveTask(2, &order, 2, true));
904 MessageLoop::current()->PostTask(FROM_HERE,
905 new QuitTask(&order, 3));
906
907 MessageLoop::current()->Run();
908
909 // FIFO order.
[email protected]b16ef312008-08-19 18:36:23910 ASSERT_EQ(14U, order.size());
initial.commitd7cae122008-07-26 21:49:38911 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
912 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
913 EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true));
914 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false));
915 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
916 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
917 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true));
918 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false));
919 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true));
920 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false));
921 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
922 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
923 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true));
924 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
925}
926
[email protected]295039bd2008-08-15 04:32:57927#if defined(OS_WIN)
928// TODO(darin): These tests need to be ported since they test critical
929// message loop functionality.
930
initial.commitd7cae122008-07-26 21:49:38931// A side effect of this test is the generation a beep. Sorry.
[email protected]4d9bdfaf2008-08-26 05:53:57932void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) {
933 MessageLoop loop(message_loop_type);
934
initial.commitd7cae122008-07-26 21:49:38935 Thread worker("RecursiveDenial2_worker");
[email protected]4d9bdfaf2008-08-26 05:53:57936 Thread::Options options;
937 options.message_loop_type = message_loop_type;
938 ASSERT_EQ(true, worker.StartWithOptions(options));
initial.commitd7cae122008-07-26 21:49:38939 TaskList order;
940 ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
941 worker.message_loop()->PostTask(FROM_HERE,
942 new Recursive2Tasks(MessageLoop::current(),
943 event,
944 true,
945 &order,
946 false));
947 // Let the other thread execute.
948 WaitForSingleObject(event, INFINITE);
949 MessageLoop::current()->Run();
950
951 ASSERT_EQ(order.size(), 17);
952 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
953 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
954 EXPECT_EQ(order[ 2], TaskItem(MESSAGEBOX, 2, true));
955 EXPECT_EQ(order[ 3], TaskItem(MESSAGEBOX, 2, false));
956 EXPECT_EQ(order[ 4], TaskItem(RECURSIVE, 3, true));
957 EXPECT_EQ(order[ 5], TaskItem(RECURSIVE, 3, false));
958 // When EndDialogTask is processed, the window is already dismissed, hence no
959 // "end" entry.
960 EXPECT_EQ(order[ 6], TaskItem(ENDDIALOG, 4, true));
961 EXPECT_EQ(order[ 7], TaskItem(QUITMESSAGELOOP, 5, true));
962 EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, false));
963 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 1, true));
964 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, false));
965 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 3, true));
966 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 3, false));
967 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 1, true));
968 EXPECT_EQ(order[14], TaskItem(RECURSIVE, 1, false));
969 EXPECT_EQ(order[15], TaskItem(RECURSIVE, 3, true));
970 EXPECT_EQ(order[16], TaskItem(RECURSIVE, 3, false));
971}
972
[email protected]4d9bdfaf2008-08-26 05:53:57973// A side effect of this test is the generation a beep. Sorry. This test also
974// needs to process windows messages on the current thread.
975void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) {
976 MessageLoop loop(message_loop_type);
977
initial.commitd7cae122008-07-26 21:49:38978 Thread worker("RecursiveSupport2_worker");
[email protected]4d9bdfaf2008-08-26 05:53:57979 Thread::Options options;
980 options.message_loop_type = message_loop_type;
981 ASSERT_EQ(true, worker.StartWithOptions(options));
initial.commitd7cae122008-07-26 21:49:38982 TaskList order;
983 ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
984 worker.message_loop()->PostTask(FROM_HERE,
985 new Recursive2Tasks(MessageLoop::current(),
986 event,
987 false,
988 &order,
989 true));
990 // Let the other thread execute.
991 WaitForSingleObject(event, INFINITE);
992 MessageLoop::current()->Run();
993
994 ASSERT_EQ(order.size(), 18);
995 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
996 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
997 EXPECT_EQ(order[ 2], TaskItem(MESSAGEBOX, 2, true));
998 // Note that this executes in the MessageBox modal loop.
999 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 3, true));
1000 EXPECT_EQ(order[ 4], TaskItem(RECURSIVE, 3, false));
1001 EXPECT_EQ(order[ 5], TaskItem(ENDDIALOG, 4, true));
1002 EXPECT_EQ(order[ 6], TaskItem(ENDDIALOG, 4, false));
1003 EXPECT_EQ(order[ 7], TaskItem(MESSAGEBOX, 2, false));
1004 /* The order can subtly change here. The reason is that when RecursiveTask(1)
1005 is called in the main thread, if it is faster than getting to the
1006 PostTask(FROM_HERE, QuitTask) execution, the order of task execution can
1007 change. We don't care anyway that the order isn't correct.
1008 EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, true));
1009 EXPECT_EQ(order[ 9], TaskItem(QUITMESSAGELOOP, 5, false));
1010 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
1011 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
1012 */
1013 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 3, true));
1014 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 3, false));
1015 EXPECT_EQ(order[14], TaskItem(RECURSIVE, 1, true));
1016 EXPECT_EQ(order[15], TaskItem(RECURSIVE, 1, false));
1017 EXPECT_EQ(order[16], TaskItem(RECURSIVE, 3, true));
1018 EXPECT_EQ(order[17], TaskItem(RECURSIVE, 3, false));
1019}
1020
[email protected]295039bd2008-08-15 04:32:571021#endif // defined(OS_WIN)
1022
initial.commitd7cae122008-07-26 21:49:381023class TaskThatPumps : public OrderedTasks {
1024 public:
1025 TaskThatPumps(TaskList* order, int cookie)
1026 : OrderedTasks(order, PUMPS, cookie) {
1027 }
1028
1029 virtual void Run() {
1030 RunStart();
1031 bool old_state = MessageLoop::current()->NestableTasksAllowed();
initial.commitd7cae122008-07-26 21:49:381032 MessageLoop::current()->SetNestableTasksAllowed(true);
[email protected]295039bd2008-08-15 04:32:571033 MessageLoop::current()->RunAllPending();
initial.commitd7cae122008-07-26 21:49:381034 MessageLoop::current()->SetNestableTasksAllowed(old_state);
1035 RunEnd();
1036 }
initial.commitd7cae122008-07-26 21:49:381037};
1038
initial.commitd7cae122008-07-26 21:49:381039// Tests that non nestable tasks run in FIFO if there are no nested loops.
[email protected]4d9bdfaf2008-08-26 05:53:571040void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type) {
1041 MessageLoop loop(message_loop_type);
1042
initial.commitd7cae122008-07-26 21:49:381043 TaskList order;
1044
1045 Task* task = new OrderedTasks(&order, 1);
[email protected]752578562008-09-07 08:08:291046 MessageLoop::current()->PostNonNestableTask(FROM_HERE, task);
initial.commitd7cae122008-07-26 21:49:381047 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 2));
1048 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3));
1049 MessageLoop::current()->Run();
1050
1051 // FIFO order.
[email protected]b16ef312008-08-19 18:36:231052 ASSERT_EQ(6U, order.size());
initial.commitd7cae122008-07-26 21:49:381053 EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true));
1054 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false));
1055 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true));
1056 EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false));
1057 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
1058 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
1059}
1060
1061// Tests that non nestable tasks don't run when there's code in the call stack.
[email protected]c4280a92009-06-25 19:23:111062void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type,
1063 bool use_delayed) {
[email protected]4d9bdfaf2008-08-26 05:53:571064 MessageLoop loop(message_loop_type);
1065
initial.commitd7cae122008-07-26 21:49:381066 TaskList order;
1067
1068 MessageLoop::current()->PostTask(FROM_HERE,
1069 new TaskThatPumps(&order, 1));
1070 Task* task = new OrderedTasks(&order, 2);
[email protected]c4280a92009-06-25 19:23:111071 if (use_delayed) {
1072 MessageLoop::current()->PostNonNestableDelayedTask(FROM_HERE, task, 1);
1073 } else {
1074 MessageLoop::current()->PostNonNestableTask(FROM_HERE, task);
1075 }
initial.commitd7cae122008-07-26 21:49:381076 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 3));
[email protected]c4280a92009-06-25 19:23:111077 MessageLoop::current()->PostTask(FROM_HERE, new SleepTask(&order, 4, 50));
1078 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 5));
1079 Task* non_nestable_quit = new QuitTask(&order, 6);
1080 if (use_delayed) {
1081 MessageLoop::current()->PostNonNestableDelayedTask(FROM_HERE,
1082 non_nestable_quit,
1083 2);
1084 } else {
1085 MessageLoop::current()->PostNonNestableTask(FROM_HERE, non_nestable_quit);
1086 }
initial.commitd7cae122008-07-26 21:49:381087
initial.commitd7cae122008-07-26 21:49:381088 MessageLoop::current()->Run();
1089
1090 // FIFO order.
[email protected]c4280a92009-06-25 19:23:111091 ASSERT_EQ(12U, order.size());
initial.commitd7cae122008-07-26 21:49:381092 EXPECT_EQ(order[ 0], TaskItem(PUMPS, 1, true));
1093 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 3, true));
1094 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 3, false));
[email protected]c4280a92009-06-25 19:23:111095 EXPECT_EQ(order[ 3], TaskItem(SLEEP, 4, true));
1096 EXPECT_EQ(order[ 4], TaskItem(SLEEP, 4, false));
1097 EXPECT_EQ(order[ 5], TaskItem(ORDERERD, 5, true));
1098 EXPECT_EQ(order[ 6], TaskItem(ORDERERD, 5, false));
1099 EXPECT_EQ(order[ 7], TaskItem(PUMPS, 1, false));
1100 EXPECT_EQ(order[ 8], TaskItem(ORDERERD, 2, true));
1101 EXPECT_EQ(order[ 9], TaskItem(ORDERERD, 2, false));
1102 EXPECT_EQ(order[10], TaskItem(QUITMESSAGELOOP, 6, true));
1103 EXPECT_EQ(order[11], TaskItem(QUITMESSAGELOOP, 6, false));
initial.commitd7cae122008-07-26 21:49:381104}
1105
[email protected]295039bd2008-08-15 04:32:571106#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381107
[email protected]4d9bdfaf2008-08-26 05:53:571108class DispatcherImpl : public MessageLoopForUI::Dispatcher {
initial.commitd7cae122008-07-26 21:49:381109 public:
1110 DispatcherImpl() : dispatch_count_(0) {}
1111
1112 virtual bool Dispatch(const MSG& msg) {
1113 ::TranslateMessage(&msg);
1114 ::DispatchMessage(&msg);
[email protected]6aa4a1c02010-01-15 18:49:581115 // Do not count WM_TIMER since it is not what we post and it will cause
1116 // flakiness.
1117 if (msg.message != WM_TIMER)
1118 ++dispatch_count_;
1119 // We treat WM_LBUTTONUP as the last message.
1120 return msg.message != WM_LBUTTONUP;
initial.commitd7cae122008-07-26 21:49:381121 }
1122
1123 int dispatch_count_;
1124};
1125
[email protected]4d9bdfaf2008-08-26 05:53:571126void RunTest_Dispatcher(MessageLoop::Type message_loop_type) {
1127 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:381128
initial.commitd7cae122008-07-26 21:49:381129 class MyTask : public Task {
1130 public:
1131 virtual void Run() {
1132 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
1133 PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
1134 }
1135 };
1136 Task* task = new MyTask();
1137 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100);
1138 DispatcherImpl dispatcher;
[email protected]4d9bdfaf2008-08-26 05:53:571139 MessageLoopForUI::current()->Run(&dispatcher);
initial.commitd7cae122008-07-26 21:49:381140 ASSERT_EQ(2, dispatcher.dispatch_count_);
1141}
[email protected]295039bd2008-08-15 04:32:571142
[email protected]6aa4a1c02010-01-15 18:49:581143LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) {
1144 if (code == base::MessagePumpForUI::kMessageFilterCode) {
1145 MSG* msg = reinterpret_cast<MSG*>(lparam);
1146 if (msg->message == WM_LBUTTONDOWN)
1147 return TRUE;
1148 }
1149 return FALSE;
1150}
1151
1152void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) {
1153 MessageLoop loop(message_loop_type);
1154
1155 class MyTask : public Task {
1156 public:
1157 virtual void Run() {
1158 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
1159 PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
1160 }
1161 };
1162 Task* task = new MyTask();
1163 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100);
1164 HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER,
1165 MsgFilterProc,
1166 NULL,
1167 GetCurrentThreadId());
1168 DispatcherImpl dispatcher;
1169 MessageLoopForUI::current()->Run(&dispatcher);
1170 ASSERT_EQ(1, dispatcher.dispatch_count_);
1171 UnhookWindowsHookEx(msg_hook);
1172}
1173
[email protected]32cda29d2008-10-09 23:58:431174class TestIOHandler : public MessageLoopForIO::IOHandler {
1175 public:
[email protected]17b89142008-11-07 21:52:151176 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait);
[email protected]32cda29d2008-10-09 23:58:431177
[email protected]17b89142008-11-07 21:52:151178 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
1179 DWORD bytes_transfered, DWORD error);
[email protected]32cda29d2008-10-09 23:58:431180
[email protected]17b89142008-11-07 21:52:151181 void Init();
1182 void WaitForIO();
1183 OVERLAPPED* context() { return &context_.overlapped; }
[email protected]32cda29d2008-10-09 23:58:431184 DWORD size() { return sizeof(buffer_); }
1185
1186 private:
1187 char buffer_[48];
[email protected]17b89142008-11-07 21:52:151188 MessageLoopForIO::IOContext context_;
[email protected]32cda29d2008-10-09 23:58:431189 HANDLE signal_;
1190 ScopedHandle file_;
[email protected]17b89142008-11-07 21:52:151191 bool wait_;
[email protected]32cda29d2008-10-09 23:58:431192};
1193
[email protected]17b89142008-11-07 21:52:151194TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait)
1195 : signal_(signal), wait_(wait) {
[email protected]32cda29d2008-10-09 23:58:431196 memset(buffer_, 0, sizeof(buffer_));
1197 memset(&context_, 0, sizeof(context_));
[email protected]17b89142008-11-07 21:52:151198 context_.handler = this;
[email protected]32cda29d2008-10-09 23:58:431199
1200 file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1201 FILE_FLAG_OVERLAPPED, NULL));
1202 EXPECT_TRUE(file_.IsValid());
1203}
1204
[email protected]17b89142008-11-07 21:52:151205void TestIOHandler::Init() {
1206 MessageLoopForIO::current()->RegisterIOHandler(file_, this);
1207
1208 DWORD read;
1209 EXPECT_FALSE(ReadFile(file_, buffer_, size(), &read, context()));
1210 EXPECT_EQ(ERROR_IO_PENDING, GetLastError());
1211 if (wait_)
1212 WaitForIO();
1213}
1214
1215void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext* context,
1216 DWORD bytes_transfered, DWORD error) {
[email protected]32cda29d2008-10-09 23:58:431217 ASSERT_TRUE(context == &context_);
[email protected]32cda29d2008-10-09 23:58:431218 ASSERT_TRUE(SetEvent(signal_));
1219}
1220
[email protected]17b89142008-11-07 21:52:151221void TestIOHandler::WaitForIO() {
1222 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this));
1223 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this));
1224}
1225
[email protected]32cda29d2008-10-09 23:58:431226class IOHandlerTask : public Task {
1227 public:
1228 explicit IOHandlerTask(TestIOHandler* handler) : handler_(handler) {}
[email protected]17b89142008-11-07 21:52:151229 virtual void Run() {
1230 handler_->Init();
1231 }
[email protected]32cda29d2008-10-09 23:58:431232
1233 private:
1234 TestIOHandler* handler_;
1235};
1236
[email protected]32cda29d2008-10-09 23:58:431237void RunTest_IOHandler() {
[email protected]32cda29d2008-10-09 23:58:431238 ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL));
1239 ASSERT_TRUE(callback_called.IsValid());
1240
1241 const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe";
1242 ScopedHandle server(CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1,
1243 0, 0, 0, NULL));
1244 ASSERT_TRUE(server.IsValid());
1245
1246 Thread thread("IOHandler test");
1247 Thread::Options options;
1248 options.message_loop_type = MessageLoop::TYPE_IO;
1249 ASSERT_TRUE(thread.StartWithOptions(options));
1250
1251 MessageLoop* thread_loop = thread.message_loop();
1252 ASSERT_TRUE(NULL != thread_loop);
1253
[email protected]17b89142008-11-07 21:52:151254 TestIOHandler handler(kPipeName, callback_called, false);
[email protected]32cda29d2008-10-09 23:58:431255 IOHandlerTask* task = new IOHandlerTask(&handler);
1256 thread_loop->PostTask(FROM_HERE, task);
1257 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
1258
1259 const char buffer[] = "Hello there!";
1260 DWORD written;
1261 EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL));
1262
1263 DWORD result = WaitForSingleObject(callback_called, 1000);
1264 EXPECT_EQ(WAIT_OBJECT_0, result);
1265
1266 thread.Stop();
1267}
1268
[email protected]17b89142008-11-07 21:52:151269void RunTest_WaitForIO() {
1270 ScopedHandle callback1_called(CreateEvent(NULL, TRUE, FALSE, NULL));
1271 ScopedHandle callback2_called(CreateEvent(NULL, TRUE, FALSE, NULL));
1272 ASSERT_TRUE(callback1_called.IsValid());
1273 ASSERT_TRUE(callback2_called.IsValid());
1274
1275 const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1";
1276 const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2";
1277 ScopedHandle server1(CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1,
1278 0, 0, 0, NULL));
1279 ScopedHandle server2(CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1,
1280 0, 0, 0, NULL));
1281 ASSERT_TRUE(server1.IsValid());
1282 ASSERT_TRUE(server2.IsValid());
1283
1284 Thread thread("IOHandler test");
1285 Thread::Options options;
1286 options.message_loop_type = MessageLoop::TYPE_IO;
1287 ASSERT_TRUE(thread.StartWithOptions(options));
1288
1289 MessageLoop* thread_loop = thread.message_loop();
1290 ASSERT_TRUE(NULL != thread_loop);
1291
1292 TestIOHandler handler1(kPipeName1, callback1_called, false);
1293 TestIOHandler handler2(kPipeName2, callback2_called, true);
1294 IOHandlerTask* task1 = new IOHandlerTask(&handler1);
1295 IOHandlerTask* task2 = new IOHandlerTask(&handler2);
1296 thread_loop->PostTask(FROM_HERE, task1);
1297 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
1298 thread_loop->PostTask(FROM_HERE, task2);
1299 Sleep(100);
1300
1301 // At this time handler1 is waiting to be called, and the thread is waiting
1302 // on the Init method of handler2, filtering only handler2 callbacks.
1303
1304 const char buffer[] = "Hello there!";
1305 DWORD written;
1306 EXPECT_TRUE(WriteFile(server1, buffer, sizeof(buffer), &written, NULL));
1307 Sleep(200);
1308 EXPECT_EQ(WAIT_TIMEOUT, WaitForSingleObject(callback1_called, 0)) <<
1309 "handler1 has not been called";
1310
1311 EXPECT_TRUE(WriteFile(server2, buffer, sizeof(buffer), &written, NULL));
1312
1313 HANDLE objects[2] = { callback1_called.Get(), callback2_called.Get() };
1314 DWORD result = WaitForMultipleObjects(2, objects, TRUE, 1000);
1315 EXPECT_EQ(WAIT_OBJECT_0, result);
1316
1317 thread.Stop();
1318}
1319
[email protected]4d9bdfaf2008-08-26 05:53:571320#endif // defined(OS_WIN)
license.botbf09a502008-08-24 00:55:551321
[email protected]4d9bdfaf2008-08-26 05:53:571322} // namespace
1323
1324//-----------------------------------------------------------------------------
1325// Each test is run against each type of MessageLoop. That way we are sure
1326// that message loops work properly in all configurations. Of course, in some
1327// cases, a unit test may only be for a particular type of loop.
1328
1329TEST(MessageLoopTest, PostTask) {
1330 RunTest_PostTask(MessageLoop::TYPE_DEFAULT);
1331 RunTest_PostTask(MessageLoop::TYPE_UI);
1332 RunTest_PostTask(MessageLoop::TYPE_IO);
1333}
1334
1335TEST(MessageLoopTest, PostTask_SEH) {
1336 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT);
1337 RunTest_PostTask_SEH(MessageLoop::TYPE_UI);
1338 RunTest_PostTask_SEH(MessageLoop::TYPE_IO);
1339}
1340
[email protected]752578562008-09-07 08:08:291341TEST(MessageLoopTest, PostDelayedTask_Basic) {
1342 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT);
1343 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI);
1344 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO);
1345}
1346
1347TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) {
1348 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT);
1349 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI);
1350 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO);
1351}
1352
1353TEST(MessageLoopTest, PostDelayedTask_InPostOrder) {
1354 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT);
1355 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI);
1356 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO);
1357}
1358
1359TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) {
1360 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT);
1361 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI);
1362 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO);
1363}
1364
1365TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) {
1366 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT);
1367 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI);
1368 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO);
1369}
1370
[email protected]72deacd2008-09-23 19:19:201371TEST(MessageLoopTest, PostDelayedTask_SharedTimer) {
1372 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT);
1373 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI);
1374 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO);
1375}
1376
1377#if defined(OS_WIN)
1378TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) {
1379 RunTest_PostDelayedTask_SharedTimer_SubPump();
1380}
1381#endif
1382
[email protected]5affb7e2010-05-25 20:13:361383// TODO(darin): MessageLoop does not support deleting all tasks in the
1384// destructor.
[email protected]2f861b32010-07-26 21:23:181385// Fails, http://crbug.com/50272.
[email protected]5affb7e2010-05-25 20:13:361386TEST(MessageLoopTest, FAILS_EnsureTaskDeletion) {
[email protected]001747c2008-09-10 00:37:071387 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT);
1388 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI);
1389 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO);
1390}
1391
[email protected]5affb7e2010-05-25 20:13:361392// TODO(darin): MessageLoop does not support deleting all tasks in the
1393// destructor.
[email protected]2f861b32010-07-26 21:23:181394// Fails, http://crbug.com/50272.
[email protected]5affb7e2010-05-25 20:13:361395TEST(MessageLoopTest, FAILS_EnsureTaskDeletion_Chain) {
[email protected]001747c2008-09-10 00:37:071396 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT);
1397 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI);
1398 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO);
1399}
1400
[email protected]4d9bdfaf2008-08-26 05:53:571401#if defined(OS_WIN)
1402TEST(MessageLoopTest, Crasher) {
1403 RunTest_Crasher(MessageLoop::TYPE_DEFAULT);
1404 RunTest_Crasher(MessageLoop::TYPE_UI);
1405 RunTest_Crasher(MessageLoop::TYPE_IO);
1406}
1407
1408TEST(MessageLoopTest, CrasherNasty) {
1409 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT);
1410 RunTest_CrasherNasty(MessageLoop::TYPE_UI);
1411 RunTest_CrasherNasty(MessageLoop::TYPE_IO);
1412}
1413#endif // defined(OS_WIN)
1414
1415TEST(MessageLoopTest, Nesting) {
1416 RunTest_Nesting(MessageLoop::TYPE_DEFAULT);
1417 RunTest_Nesting(MessageLoop::TYPE_UI);
1418 RunTest_Nesting(MessageLoop::TYPE_IO);
1419}
1420
1421TEST(MessageLoopTest, RecursiveDenial1) {
1422 RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT);
1423 RunTest_RecursiveDenial1(MessageLoop::TYPE_UI);
1424 RunTest_RecursiveDenial1(MessageLoop::TYPE_IO);
1425}
1426
1427TEST(MessageLoopTest, RecursiveSupport1) {
1428 RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT);
1429 RunTest_RecursiveSupport1(MessageLoop::TYPE_UI);
1430 RunTest_RecursiveSupport1(MessageLoop::TYPE_IO);
1431}
1432
1433#if defined(OS_WIN)
[email protected]85f39f82010-05-19 14:33:441434// This test occasionally hangs http://crbug.com/44567
1435TEST(MessageLoopTest, DISABLED_RecursiveDenial2) {
[email protected]4d9bdfaf2008-08-26 05:53:571436 RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT);
1437 RunTest_RecursiveDenial2(MessageLoop::TYPE_UI);
1438 RunTest_RecursiveDenial2(MessageLoop::TYPE_IO);
1439}
1440
1441TEST(MessageLoopTest, RecursiveSupport2) {
1442 // This test requires a UI loop
1443 RunTest_RecursiveSupport2(MessageLoop::TYPE_UI);
1444}
1445#endif // defined(OS_WIN)
1446
1447TEST(MessageLoopTest, NonNestableWithNoNesting) {
1448 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT);
1449 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI);
1450 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO);
1451}
1452
1453TEST(MessageLoopTest, NonNestableInNestedLoop) {
[email protected]c4280a92009-06-25 19:23:111454 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false);
1455 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false);
1456 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false);
1457}
1458
1459TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) {
1460 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true);
1461 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true);
1462 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true);
[email protected]4d9bdfaf2008-08-26 05:53:571463}
1464
[email protected]9cfb89a2010-06-09 21:20:411465class DummyTask : public Task {
1466 public:
[email protected]b7243c42010-07-23 05:23:131467 explicit DummyTask(int num_tasks) : num_tasks_(num_tasks) {}
[email protected]9cfb89a2010-06-09 21:20:411468
1469 virtual void Run() {
1470 if (num_tasks_ > 1) {
1471 MessageLoop::current()->PostTask(
1472 FROM_HERE,
1473 new DummyTask(num_tasks_ - 1));
1474 } else {
1475 MessageLoop::current()->Quit();
1476 }
1477 }
1478
1479 private:
1480 const int num_tasks_;
1481};
1482
1483class DummyTaskObserver : public MessageLoop::TaskObserver {
1484 public:
[email protected]b7243c42010-07-23 05:23:131485 explicit DummyTaskObserver(int num_tasks)
[email protected]9cfb89a2010-06-09 21:20:411486 : num_tasks_started_(0),
1487 num_tasks_processed_(0),
1488 num_tasks_(num_tasks) {}
1489
1490 virtual ~DummyTaskObserver() {}
1491
[email protected]024686682010-10-26 23:40:481492 virtual void WillProcessTask(const Task* task) {
[email protected]9cfb89a2010-06-09 21:20:411493 num_tasks_started_++;
[email protected]024686682010-10-26 23:40:481494 EXPECT_TRUE(task != NULL);
[email protected]9cfb89a2010-06-09 21:20:411495 EXPECT_LE(num_tasks_started_, num_tasks_);
1496 EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1);
1497 }
1498
[email protected]024686682010-10-26 23:40:481499 virtual void DidProcessTask(const Task* task) {
[email protected]9cfb89a2010-06-09 21:20:411500 num_tasks_processed_++;
[email protected]024686682010-10-26 23:40:481501 EXPECT_TRUE(task != NULL);
[email protected]9cfb89a2010-06-09 21:20:411502 EXPECT_LE(num_tasks_started_, num_tasks_);
1503 EXPECT_EQ(num_tasks_started_, num_tasks_processed_);
1504 }
1505
1506 int num_tasks_started() const { return num_tasks_started_; }
1507 int num_tasks_processed() const { return num_tasks_processed_; }
1508
1509 private:
1510 int num_tasks_started_;
1511 int num_tasks_processed_;
1512 const int num_tasks_;
1513
1514 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver);
1515};
1516
1517TEST(MessageLoopTest, TaskObserver) {
1518 const int kNumTasks = 6;
1519 DummyTaskObserver observer(kNumTasks);
1520
1521 MessageLoop loop;
1522 loop.AddTaskObserver(&observer);
1523 loop.PostTask(FROM_HERE, new DummyTask(kNumTasks));
1524 loop.Run();
1525 loop.RemoveTaskObserver(&observer);
1526
1527 EXPECT_EQ(kNumTasks, observer.num_tasks_started());
1528 EXPECT_EQ(kNumTasks, observer.num_tasks_processed());
1529}
1530
[email protected]4d9bdfaf2008-08-26 05:53:571531#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:571532TEST(MessageLoopTest, Dispatcher) {
1533 // This test requires a UI loop
1534 RunTest_Dispatcher(MessageLoop::TYPE_UI);
1535}
[email protected]32cda29d2008-10-09 23:58:431536
[email protected]6aa4a1c02010-01-15 18:49:581537TEST(MessageLoopTest, DispatcherWithMessageHook) {
1538 // This test requires a UI loop
1539 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI);
1540}
1541
[email protected]32cda29d2008-10-09 23:58:431542TEST(MessageLoopTest, IOHandler) {
1543 RunTest_IOHandler();
1544}
[email protected]17b89142008-11-07 21:52:151545
1546TEST(MessageLoopTest, WaitForIO) {
1547 RunTest_WaitForIO();
1548}
[email protected]57f030a2010-06-29 04:58:151549
1550TEST(MessageLoopTest, HighResolutionTimer) {
1551 MessageLoop loop;
1552
1553 const int kFastTimerMs = 5;
1554 const int kSlowTimerMs = 100;
1555
1556 EXPECT_EQ(false, loop.high_resolution_timers_enabled());
1557
1558 // Post a fast task to enable the high resolution timers.
1559 loop.PostDelayedTask(FROM_HERE, new DummyTask(1), kFastTimerMs);
1560 loop.Run();
1561 EXPECT_EQ(true, loop.high_resolution_timers_enabled());
1562
1563 // Post a slow task and verify high resolution timers
1564 // are still enabled.
1565 loop.PostDelayedTask(FROM_HERE, new DummyTask(1), kSlowTimerMs);
1566 loop.Run();
1567 EXPECT_EQ(true, loop.high_resolution_timers_enabled());
1568
1569 // Wait for a while so that high-resolution mode elapses.
1570 Sleep(MessageLoop::kHighResolutionTimerModeLeaseTimeMs);
1571
1572 // Post a slow task to disable the high resolution timers.
1573 loop.PostDelayedTask(FROM_HERE, new DummyTask(1), kSlowTimerMs);
1574 loop.Run();
1575 EXPECT_EQ(false, loop.high_resolution_timers_enabled());
1576}
1577
[email protected]4d9bdfaf2008-08-26 05:53:571578#endif // defined(OS_WIN)
[email protected]f74c8962009-04-22 20:01:361579
[email protected]5cffdfd2010-12-01 08:45:511580#if defined(OS_POSIX) && !defined(OS_NACL)
[email protected]f74c8962009-04-22 20:01:361581
1582namespace {
1583
[email protected]9cfb89a2010-06-09 21:20:411584class QuitDelegate : public base::MessagePumpLibevent::Watcher {
[email protected]f74c8962009-04-22 20:01:361585 public:
1586 virtual void OnFileCanWriteWithoutBlocking(int fd) {
1587 MessageLoop::current()->Quit();
1588 }
1589 virtual void OnFileCanReadWithoutBlocking(int fd) {
1590 MessageLoop::current()->Quit();
1591 }
1592};
1593
[email protected]f45213772010-06-11 00:06:191594TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) {
[email protected]f74c8962009-04-22 20:01:361595 // Simulate a MessageLoop that dies before an FileDescriptorWatcher.
1596 // This could happen when people use the Singleton pattern or atexit.
[email protected]f74c8962009-04-22 20:01:361597
1598 // Create a file descriptor. Doesn't need to be readable or writable,
1599 // as we don't need to actually get any notifications.
1600 // pipe() is just the easiest way to do it.
1601 int pipefds[2];
1602 int err = pipe(pipefds);
[email protected]b7243c42010-07-23 05:23:131603 ASSERT_EQ(0, err);
[email protected]f74c8962009-04-22 20:01:361604 int fd = pipefds[1];
1605 {
1606 // Arrange for controller to live longer than message loop.
1607 base::MessagePumpLibevent::FileDescriptorWatcher controller;
1608 {
1609 MessageLoopForIO message_loop;
1610
1611 QuitDelegate delegate;
1612 message_loop.WatchFileDescriptor(fd,
1613 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1614 // and don't run the message loop, just destroy it.
1615 }
1616 }
[email protected]70eb6572010-06-23 00:37:461617 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1618 PLOG(ERROR) << "close";
1619 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1620 PLOG(ERROR) << "close";
[email protected]f74c8962009-04-22 20:01:361621}
1622
1623TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) {
1624 // Verify that it's ok to call StopWatchingFileDescriptor().
1625 // (Errors only showed up in valgrind.)
1626 int pipefds[2];
1627 int err = pipe(pipefds);
[email protected]b7243c42010-07-23 05:23:131628 ASSERT_EQ(0, err);
[email protected]f74c8962009-04-22 20:01:361629 int fd = pipefds[1];
1630 {
1631 // Arrange for message loop to live longer than controller.
1632 MessageLoopForIO message_loop;
1633 {
1634 base::MessagePumpLibevent::FileDescriptorWatcher controller;
1635
1636 QuitDelegate delegate;
1637 message_loop.WatchFileDescriptor(fd,
1638 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1639 controller.StopWatchingFileDescriptor();
1640 }
1641 }
[email protected]70eb6572010-06-23 00:37:461642 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1643 PLOG(ERROR) << "close";
1644 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1645 PLOG(ERROR) << "close";
[email protected]f74c8962009-04-22 20:01:361646}
1647
[email protected]9cfb89a2010-06-09 21:20:411648} // namespace
1649
[email protected]5cffdfd2010-12-01 08:45:511650#endif // defined(OS_POSIX) && !defined(OS_NACL)
[email protected]582384772010-11-30 00:25:291651
1652namespace {
1653class RunAtDestructionTask : public Task {
1654 public:
1655 RunAtDestructionTask(bool* task_destroyed, bool* destruction_observer_called)
1656 : task_destroyed_(task_destroyed),
1657 destruction_observer_called_(destruction_observer_called) {
1658 }
1659 ~RunAtDestructionTask() {
1660 EXPECT_FALSE(*destruction_observer_called_);
1661 *task_destroyed_ = true;
1662 }
1663 virtual void Run() {
1664 // This task should never run.
1665 ADD_FAILURE();
1666 }
1667 private:
1668 bool* task_destroyed_;
1669 bool* destruction_observer_called_;
1670};
1671
1672class MLDestructionObserver : public MessageLoop::DestructionObserver {
1673 public:
1674 MLDestructionObserver(bool* task_destroyed, bool* destruction_observer_called)
1675 : task_destroyed_(task_destroyed),
1676 destruction_observer_called_(destruction_observer_called),
1677 task_destroyed_before_message_loop_(false) {
1678 }
1679 virtual void WillDestroyCurrentMessageLoop() {
1680 task_destroyed_before_message_loop_ = *task_destroyed_;
1681 *destruction_observer_called_ = true;
1682 }
1683 bool task_destroyed_before_message_loop() const {
1684 return task_destroyed_before_message_loop_;
1685 }
1686 private:
1687 bool* task_destroyed_;
1688 bool* destruction_observer_called_;
1689 bool task_destroyed_before_message_loop_;
1690};
1691
1692} // namespace
1693
1694TEST(MessageLoopTest, DestructionObserverTest) {
1695 // Verify that the destruction observer gets called at the very end (after
1696 // all the pending tasks have been destroyed).
1697 MessageLoop* loop = new MessageLoop;
1698 const int kDelayMS = 100;
1699
1700 bool task_destroyed = false;
1701 bool destruction_observer_called = false;
1702
1703 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called);
1704 loop->AddDestructionObserver(&observer);
1705 loop->PostDelayedTask(
1706 FROM_HERE,
1707 new RunAtDestructionTask(&task_destroyed, &destruction_observer_called),
1708 kDelayMS);
1709 delete loop;
1710 EXPECT_TRUE(observer.task_destroyed_before_message_loop());
1711 // The task should have been destroyed when we deleted the loop.
1712 EXPECT_TRUE(task_destroyed);
1713 EXPECT_TRUE(destruction_observer_called);
1714}