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
3 // found in the LICENSE file.
7 #include "base/eintr_wrapper.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop.h"
11 #include "base/task.h"
12 #include "base/threading/platform_thread.h"
13 #include "base/threading/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 #include "base/message_pump_win.h"
18 #include "base/win/scoped_handle.h"
21 #include "base/message_pump_libevent.h"
24 using base::PlatformThread
;
27 using base::TimeDelta
;
29 // TODO(darin): Platform-specific MessageLoop tests should be grouped together
30 // to avoid chopping this file up with so many #ifdefs.
34 class MessageLoopTest
: public testing::Test
{};
36 class Foo
: public base::RefCounted
<Foo
> {
38 Foo() : test_count_(0) {
45 void Test1ConstRef(const std::string
& a
) {
50 void Test1Ptr(std::string
* a
) {
55 void Test1Int(int a
) {
59 void Test2Ptr(std::string
* a
, std::string
* b
) {
65 void Test2Mixed(const std::string
& a
, std::string
* b
) {
71 int test_count() const { return test_count_
; }
72 const std::string
& result() const { return result_
; }
75 friend class base::RefCounted
<Foo
>;
83 class QuitMsgLoop
: public base::RefCounted
<QuitMsgLoop
> {
86 MessageLoop::current()->Quit();
90 friend class base::RefCounted
<QuitMsgLoop
>;
95 void RunTest_PostTask(MessageLoop::Type message_loop_type
) {
96 MessageLoop
loop(message_loop_type
);
98 // Add tests to message loop
99 scoped_refptr
<Foo
> foo(new Foo());
100 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
));
114 // After all tests, post a message that will shut down the message loop
115 scoped_refptr
<QuitMsgLoop
> quit(new QuitMsgLoop());
116 MessageLoop::current()->PostTask(FROM_HERE
, NewRunnableMethod(
117 quit
.get(), &QuitMsgLoop::QuitNow
));
119 // Now kick things off
120 MessageLoop::current()->Run();
122 EXPECT_EQ(foo
->test_count(), 105);
123 EXPECT_EQ(foo
->result(), "abacad");
126 void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type
) {
127 MessageLoop
loop(message_loop_type
);
129 // Add tests to message loop
130 scoped_refptr
<Foo
> foo(new Foo());
131 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
));
145 // After all tests, post a message that will shut down the message loop
146 scoped_refptr
<QuitMsgLoop
> quit(new QuitMsgLoop());
147 MessageLoop::current()->PostTask(FROM_HERE
, NewRunnableMethod(
148 quit
.get(), &QuitMsgLoop::QuitNow
));
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);
155 EXPECT_EQ(foo
->test_count(), 105);
156 EXPECT_EQ(foo
->result(), "abacad");
159 // This class runs slowly to simulate a large amount of work being done.
160 class SlowTask
: public Task
{
162 SlowTask(int pause_ms
, int* quit_counter
)
163 : pause_ms_(pause_ms
), quit_counter_(quit_counter
) {
166 PlatformThread::Sleep(pause_ms_
);
167 if (--(*quit_counter_
) == 0)
168 MessageLoop::current()->Quit();
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.
177 class RecordRunTimeTask
: public SlowTask
{
179 RecordRunTimeTask(Time
* run_time
, int* quit_counter
)
180 : SlowTask(10, quit_counter
), run_time_(run_time
) {
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.
193 void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type
) {
194 MessageLoop
loop(message_loop_type
);
196 // Test that PostDelayedTask results in a delayed task.
198 const int kDelayMS
= 100;
203 loop
.PostDelayedTask(
204 FROM_HERE
, new RecordRunTimeTask(&run_time
, &num_tasks
), kDelayMS
);
206 Time time_before_run
= Time::Now();
208 Time time_after_run
= Time::Now();
210 EXPECT_EQ(0, num_tasks
);
211 EXPECT_LT(kDelayMS
, (time_after_run
- time_before_run
).InMilliseconds());
214 void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type
) {
215 MessageLoop
loop(message_loop_type
);
217 // Test that two tasks with different delays run in the right order.
220 Time run_time1
, run_time2
;
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
226 loop
.PostDelayedTask(
227 FROM_HERE
, new RecordRunTimeTask(&run_time2
, &num_tasks
), 10);
230 EXPECT_EQ(0, num_tasks
);
232 EXPECT_TRUE(run_time2
< run_time1
);
235 void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type
) {
236 MessageLoop
loop(message_loop_type
);
238 // Test that two tasks with the same delay run in the order in which they
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.
246 const int kDelayMS
= 100;
249 Time run_time1
, run_time2
;
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
);
257 EXPECT_EQ(0, num_tasks
);
259 EXPECT_TRUE(run_time1
< run_time2
);
262 void RunTest_PostDelayedTask_InPostOrder_2(
263 MessageLoop::Type message_loop_type
) {
264 MessageLoop
loop(message_loop_type
);
266 // Test that a delayed task still runs after a normal tasks even if the
267 // normal tasks take a long time to run.
269 const int kPauseMS
= 50;
275 FROM_HERE
, new SlowTask(kPauseMS
, &num_tasks
));
276 loop
.PostDelayedTask(
277 FROM_HERE
, new RecordRunTimeTask(&run_time
, &num_tasks
), 10);
279 Time time_before_run
= Time::Now();
281 Time time_after_run
= Time::Now();
283 EXPECT_EQ(0, num_tasks
);
285 EXPECT_LT(kPauseMS
, (time_after_run
- time_before_run
).InMilliseconds());
288 void RunTest_PostDelayedTask_InPostOrder_3(
289 MessageLoop::Type message_loop_type
) {
290 MessageLoop
loop(message_loop_type
);
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.
299 Time run_time1
, run_time2
;
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
));
305 loop
.PostDelayedTask(
306 FROM_HERE
, new RecordRunTimeTask(&run_time2
, &num_tasks
), 1);
309 EXPECT_EQ(0, num_tasks
);
311 EXPECT_TRUE(run_time2
> run_time1
);
314 void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type
) {
315 MessageLoop
loop(message_loop_type
);
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.
320 // By setting num_tasks to 1, we ensure that the first task to run causes the
323 Time run_time1
, run_time2
;
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);
330 Time start_time
= Time::Now();
333 EXPECT_EQ(0, num_tasks
);
335 // Ensure that we ran in far less time than the slower timer.
336 TimeDelta total_time
= Time::Now() - start_time
;
337 EXPECT_GT(5000, total_time
.InMilliseconds());
339 // 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();
345 EXPECT_TRUE(run_time1
.is_null());
346 EXPECT_FALSE(run_time2
.is_null());
351 class SubPumpTask
: public Task
{
354 MessageLoop::current()->SetNestableTasksAllowed(true);
356 while (GetMessage(&msg
, NULL
, 0, 0)) {
357 TranslateMessage(&msg
);
358 DispatchMessage(&msg
);
360 MessageLoop::current()->Quit();
364 class SubPumpQuitTask
: public Task
{
373 void RunTest_PostDelayedTask_SharedTimer_SubPump() {
374 MessageLoop
loop(MessageLoop::TYPE_UI
);
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.
379 // By setting num_tasks to 1, we ensure that the first task to run causes the
384 loop
.PostTask(FROM_HERE
, new SubPumpTask());
386 // This very delayed task should never run.
387 loop
.PostDelayedTask(
388 FROM_HERE
, new RecordRunTimeTask(&run_time
, &num_tasks
), 1000000);
390 // This slightly delayed task should run from within SubPumpTask::Run().
391 loop
.PostDelayedTask(
392 FROM_HERE
, new SubPumpQuitTask(), 10);
394 Time start_time
= Time::Now();
397 EXPECT_EQ(1, num_tasks
);
399 // Ensure that we ran in far less time than the slower timer.
400 TimeDelta total_time
= Time::Now() - start_time
;
401 EXPECT_GT(5000, total_time
.InMilliseconds());
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();
409 EXPECT_TRUE(run_time
.is_null());
412 #endif // defined(OS_WIN)
414 class RecordDeletionTask
: public Task
{
416 RecordDeletionTask(Task
* post_on_delete
, bool* was_deleted
)
417 : post_on_delete_(post_on_delete
), was_deleted_(was_deleted
) {
419 ~RecordDeletionTask() {
420 *was_deleted_
= true;
422 MessageLoop::current()->PostTask(FROM_HERE
, post_on_delete_
);
424 virtual void Run() {}
426 Task
* post_on_delete_
;
430 void RunTest_EnsureTaskDeletion(MessageLoop::Type message_loop_type
) {
431 bool a_was_deleted
= false;
432 bool b_was_deleted
= false;
434 MessageLoop
loop(message_loop_type
);
436 FROM_HERE
, new RecordDeletionTask(NULL
, &a_was_deleted
));
437 loop
.PostDelayedTask(
438 FROM_HERE
, new RecordDeletionTask(NULL
, &b_was_deleted
), 1000);
440 EXPECT_TRUE(a_was_deleted
);
441 EXPECT_TRUE(b_was_deleted
);
444 void 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;
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
);
455 EXPECT_TRUE(a_was_deleted
);
456 EXPECT_TRUE(b_was_deleted
);
457 EXPECT_TRUE(c_was_deleted
);
460 class NestingTest
: public Task
{
462 explicit NestingTest(int* depth
) : depth_(depth
) {
467 MessageLoop::current()->PostTask(FROM_HERE
, new NestingTest(depth_
));
469 MessageLoop::current()->SetNestableTasksAllowed(true);
470 MessageLoop::current()->Run();
472 MessageLoop::current()->Quit();
480 LONG WINAPI
BadExceptionHandler(EXCEPTION_POINTERS
*ex_info
) {
481 ADD_FAILURE() << "bad exception handler";
482 ::ExitProcess(ex_info
->ExceptionRecord
->ExceptionCode
);
483 return EXCEPTION_EXECUTE_HANDLER
;
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.
488 class CrasherTask
: public Task
{
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
) {
496 PlatformThread::Sleep(1);
497 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
505 mov eax
, dword ptr
[CrasherTask::bad_array_
]
506 mov byte ptr
[eax
], 66
509 #elif defined(_M_X64)
514 #error "needs architecture support"
517 MessageLoop::current()->Quit();
519 // Points the bad array to a valid memory location.
520 static void FixError() {
521 bad_array_
= &valid_store_
;
525 bool trash_SEH_handler_
;
526 static volatile char* bad_array_
;
527 static char valid_store_
;
530 volatile char* CrasherTask::bad_array_
= 0;
531 char CrasherTask::valid_store_
= 0;
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.
536 LONG WINAPI
HandleCrasherTaskException(EXCEPTION_POINTERS
*ex_info
) {
537 if (ex_info
->ExceptionRecord
->ExceptionCode
!= EXCEPTION_ACCESS_VIOLATION
)
538 return EXCEPTION_EXECUTE_HANDLER
;
540 CrasherTask::FixError();
544 ex_info
->ContextRecord
->Eip
-= 5;
546 #elif defined(_M_X64)
548 ex_info
->ContextRecord
->Rip
-= 5;
552 return EXCEPTION_CONTINUE_EXECUTION
;
555 void RunTest_Crasher(MessageLoop::Type message_loop_type
) {
556 MessageLoop
loop(message_loop_type
);
558 if (::IsDebuggerPresent())
561 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter
=
562 ::SetUnhandledExceptionFilter(&HandleCrasherTaskException
);
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);
569 ::SetUnhandledExceptionFilter(old_SEH_filter
);
572 void RunTest_CrasherNasty(MessageLoop::Type message_loop_type
) {
573 MessageLoop
loop(message_loop_type
);
575 if (::IsDebuggerPresent())
578 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter
=
579 ::SetUnhandledExceptionFilter(&HandleCrasherTaskException
);
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);
586 ::SetUnhandledExceptionFilter(old_SEH_filter
);
589 #endif // defined(OS_WIN)
591 void RunTest_Nesting(MessageLoop::Type message_loop_type
) {
592 MessageLoop
loop(message_loop_type
);
595 MessageLoop::current()->PostTask(FROM_HERE
, new NestingTest(&depth
));
596 MessageLoop::current()->Run();
600 const wchar_t* const kMessageBoxTitle
= L
"MessageLoop Unit Test";
613 // Saves the order in which the tasks executed.
615 TaskItem(TaskType t
, int c
, bool s
)
625 bool operator == (const TaskItem
& other
) const {
626 return type
== other
.type
&& cookie
== other
.cookie
&& start
== other
.start
;
630 typedef std::vector
<TaskItem
> TaskList
;
632 std::ostream
& operator <<(std::ostream
& os
, TaskType 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;
641 case SLEEP
: os
<< "SLEEP"; break;
644 os
<< "Unknown TaskType";
650 std::ostream
& operator <<(std::ostream
& os
, const TaskItem
& item
) {
652 return os
<< item
.type
<< " " << item
.cookie
<< " starts";
654 return os
<< item
.type
<< " " << item
.cookie
<< " ends";
657 // Saves the order the tasks ran.
658 class OrderedTasks
: public Task
{
660 OrderedTasks(TaskList
* order
, int cookie
)
665 OrderedTasks(TaskList
* order
, TaskType type
, int cookie
)
672 TaskItem
item(type_
, cookie_
, true);
674 order_
->push_back(item
);
677 TaskItem
item(type_
, cookie_
, false);
679 order_
->push_back(item
);
688 TaskList
* order() const {
704 // 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.
707 class MessageBoxTask
: public OrderedTasks
{
709 MessageBoxTask(TaskList
* order
, int cookie
, bool is_reentrant
)
710 : OrderedTasks(order
, MESSAGEBOX
, cookie
),
711 is_reentrant_(is_reentrant
) {
717 MessageLoop::current()->SetNestableTasksAllowed(true);
718 MessageBox(NULL
, L
"Please wait...", kMessageBoxTitle
, MB_OK
);
726 // Will end the MessageBox.
727 class EndDialogTask
: public OrderedTasks
{
729 EndDialogTask(TaskList
* order
, int cookie
)
730 : OrderedTasks(order
, ENDDIALOG
, cookie
) {
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
745 #endif // defined(OS_WIN)
747 class RecursiveTask
: public OrderedTasks
{
749 RecursiveTask(int depth
, TaskList
* order
, int cookie
, bool is_reentrant
)
750 : OrderedTasks(order
, RECURSIVE
, cookie
),
752 is_reentrant_(is_reentrant
) {
759 MessageLoop::current()->SetNestableTasksAllowed(true);
760 MessageLoop::current()->PostTask(FROM_HERE
,
761 new RecursiveTask(depth_
- 1, order(), cookie(), is_reentrant_
));
771 class RecursiveSlowTask
: public RecursiveTask
{
773 RecursiveSlowTask(int depth
, TaskList
* order
, int cookie
, bool is_reentrant
)
774 : RecursiveTask(depth
, order
, cookie
, is_reentrant
) {
778 RecursiveTask::Run();
779 PlatformThread::Sleep(10); // milliseconds
783 class QuitTask
: public OrderedTasks
{
785 QuitTask(TaskList
* order
, int cookie
)
786 : OrderedTasks(order
, QUITMESSAGELOOP
, cookie
) {
791 MessageLoop::current()->Quit();
796 class SleepTask
: public OrderedTasks
{
798 SleepTask(TaskList
* order
, int cookie
, int ms
)
799 : OrderedTasks(order
, SLEEP
, cookie
), ms_(ms
) {
804 PlatformThread::Sleep(ms_
);
814 class Recursive2Tasks
: public Task
{
816 Recursive2Tasks(MessageLoop
* target
,
823 expect_window_(expect_window
),
825 is_reentrant_(is_reentrant
) {
829 target_
->PostTask(FROM_HERE
,
830 new RecursiveTask(2, order_
, 1, is_reentrant_
));
831 target_
->PostTask(FROM_HERE
,
832 new MessageBoxTask(order_
, 2, is_reentrant_
));
833 target_
->PostTask(FROM_HERE
,
834 new RecursiveTask(2, order_
, 3, is_reentrant_
));
835 // The trick here is that for recursive task processing, this task will be
836 // ran _inside_ the MessageBox message loop, dismissing the MessageBox
838 // For non-recursive task processing, this will be executed _after_ the
839 // MessageBox will have been dismissed by the code below, where
840 // expect_window_ is true.
841 target_
->PostTask(FROM_HERE
, new EndDialogTask(order_
, 4));
842 target_
->PostTask(FROM_HERE
, new QuitTask(order_
, 5));
844 // Enforce that every tasks are sent before starting to run the main thread
846 ASSERT_TRUE(SetEvent(event_
));
848 // Poll for the MessageBox. Don't do this at home! At the speed we do it,
849 // you will never realize one MessageBox was shown.
850 for (; expect_window_
;) {
851 HWND window
= FindWindow(L
"#32770", kMessageBoxTitle
);
855 HWND button
= FindWindowEx(window
, NULL
, L
"Button", NULL
);
856 if (button
!= NULL
) {
857 EXPECT_EQ(0, SendMessage(button
, WM_LBUTTONDOWN
, 0, 0));
858 EXPECT_EQ(0, SendMessage(button
, WM_LBUTTONUP
, 0, 0));
868 MessageLoop
* target_
;
875 #endif // defined(OS_WIN)
877 void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type
) {
878 MessageLoop
loop(message_loop_type
);
880 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
882 MessageLoop::current()->PostTask(FROM_HERE
,
883 new RecursiveTask(2, &order
, 1, false));
884 MessageLoop::current()->PostTask(FROM_HERE
,
885 new RecursiveTask(2, &order
, 2, false));
886 MessageLoop::current()->PostTask(FROM_HERE
, new QuitTask(&order
, 3));
888 MessageLoop::current()->Run();
891 ASSERT_EQ(14U, order
.size());
892 EXPECT_EQ(order
[ 0], TaskItem(RECURSIVE
, 1, true));
893 EXPECT_EQ(order
[ 1], TaskItem(RECURSIVE
, 1, false));
894 EXPECT_EQ(order
[ 2], TaskItem(RECURSIVE
, 2, true));
895 EXPECT_EQ(order
[ 3], TaskItem(RECURSIVE
, 2, false));
896 EXPECT_EQ(order
[ 4], TaskItem(QUITMESSAGELOOP
, 3, true));
897 EXPECT_EQ(order
[ 5], TaskItem(QUITMESSAGELOOP
, 3, false));
898 EXPECT_EQ(order
[ 6], TaskItem(RECURSIVE
, 1, true));
899 EXPECT_EQ(order
[ 7], TaskItem(RECURSIVE
, 1, false));
900 EXPECT_EQ(order
[ 8], TaskItem(RECURSIVE
, 2, true));
901 EXPECT_EQ(order
[ 9], TaskItem(RECURSIVE
, 2, false));
902 EXPECT_EQ(order
[10], TaskItem(RECURSIVE
, 1, true));
903 EXPECT_EQ(order
[11], TaskItem(RECURSIVE
, 1, false));
904 EXPECT_EQ(order
[12], TaskItem(RECURSIVE
, 2, true));
905 EXPECT_EQ(order
[13], TaskItem(RECURSIVE
, 2, false));
908 void RunTest_RecursiveDenial3(MessageLoop::Type message_loop_type
) {
909 MessageLoop
loop(message_loop_type
);
911 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
913 MessageLoop::current()->PostTask(FROM_HERE
,
914 new RecursiveSlowTask(2, &order
, 1, false));
915 MessageLoop::current()->PostTask(FROM_HERE
,
916 new RecursiveSlowTask(2, &order
, 2, false));
917 MessageLoop::current()->PostDelayedTask(FROM_HERE
,
918 new OrderedTasks(&order
, 3), 5);
919 MessageLoop::current()->PostDelayedTask(FROM_HERE
,
920 new QuitTask(&order
, 4), 5);
922 MessageLoop::current()->Run();
925 ASSERT_EQ(16U, order
.size());
926 EXPECT_EQ(order
[ 0], TaskItem(RECURSIVE
, 1, true));
927 EXPECT_EQ(order
[ 1], TaskItem(RECURSIVE
, 1, false));
928 EXPECT_EQ(order
[ 2], TaskItem(RECURSIVE
, 2, true));
929 EXPECT_EQ(order
[ 3], TaskItem(RECURSIVE
, 2, false));
930 EXPECT_EQ(order
[ 4], TaskItem(RECURSIVE
, 1, true));
931 EXPECT_EQ(order
[ 5], TaskItem(RECURSIVE
, 1, false));
932 EXPECT_EQ(order
[ 6], TaskItem(ORDERERD
, 3, true));
933 EXPECT_EQ(order
[ 7], TaskItem(ORDERERD
, 3, false));
934 EXPECT_EQ(order
[ 8], TaskItem(RECURSIVE
, 2, true));
935 EXPECT_EQ(order
[ 9], TaskItem(RECURSIVE
, 2, false));
936 EXPECT_EQ(order
[10], TaskItem(QUITMESSAGELOOP
, 4, true));
937 EXPECT_EQ(order
[11], TaskItem(QUITMESSAGELOOP
, 4, false));
938 EXPECT_EQ(order
[12], TaskItem(RECURSIVE
, 1, true));
939 EXPECT_EQ(order
[13], TaskItem(RECURSIVE
, 1, false));
940 EXPECT_EQ(order
[14], TaskItem(RECURSIVE
, 2, true));
941 EXPECT_EQ(order
[15], TaskItem(RECURSIVE
, 2, false));
944 void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type
) {
945 MessageLoop
loop(message_loop_type
);
948 MessageLoop::current()->PostTask(FROM_HERE
,
949 new RecursiveTask(2, &order
, 1, true));
950 MessageLoop::current()->PostTask(FROM_HERE
,
951 new RecursiveTask(2, &order
, 2, true));
952 MessageLoop::current()->PostTask(FROM_HERE
,
953 new QuitTask(&order
, 3));
955 MessageLoop::current()->Run();
958 ASSERT_EQ(14U, order
.size());
959 EXPECT_EQ(order
[ 0], TaskItem(RECURSIVE
, 1, true));
960 EXPECT_EQ(order
[ 1], TaskItem(RECURSIVE
, 1, false));
961 EXPECT_EQ(order
[ 2], TaskItem(RECURSIVE
, 2, true));
962 EXPECT_EQ(order
[ 3], TaskItem(RECURSIVE
, 2, false));
963 EXPECT_EQ(order
[ 4], TaskItem(QUITMESSAGELOOP
, 3, true));
964 EXPECT_EQ(order
[ 5], TaskItem(QUITMESSAGELOOP
, 3, false));
965 EXPECT_EQ(order
[ 6], TaskItem(RECURSIVE
, 1, true));
966 EXPECT_EQ(order
[ 7], TaskItem(RECURSIVE
, 1, false));
967 EXPECT_EQ(order
[ 8], TaskItem(RECURSIVE
, 2, true));
968 EXPECT_EQ(order
[ 9], TaskItem(RECURSIVE
, 2, false));
969 EXPECT_EQ(order
[10], TaskItem(RECURSIVE
, 1, true));
970 EXPECT_EQ(order
[11], TaskItem(RECURSIVE
, 1, false));
971 EXPECT_EQ(order
[12], TaskItem(RECURSIVE
, 2, true));
972 EXPECT_EQ(order
[13], TaskItem(RECURSIVE
, 2, false));
976 // TODO(darin): These tests need to be ported since they test critical
977 // message loop functionality.
979 // A side effect of this test is the generation a beep. Sorry.
980 void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type
) {
981 MessageLoop
loop(message_loop_type
);
983 Thread
worker("RecursiveDenial2_worker");
984 Thread::Options options
;
985 options
.message_loop_type
= message_loop_type
;
986 ASSERT_EQ(true, worker
.StartWithOptions(options
));
988 base::win::ScopedHandle
event(CreateEvent(NULL
, FALSE
, FALSE
, NULL
));
989 worker
.message_loop()->PostTask(FROM_HERE
,
990 new Recursive2Tasks(MessageLoop::current(),
995 // Let the other thread execute.
996 WaitForSingleObject(event
, INFINITE
);
997 MessageLoop::current()->Run();
999 ASSERT_EQ(order
.size(), 17);
1000 EXPECT_EQ(order
[ 0], TaskItem(RECURSIVE
, 1, true));
1001 EXPECT_EQ(order
[ 1], TaskItem(RECURSIVE
, 1, false));
1002 EXPECT_EQ(order
[ 2], TaskItem(MESSAGEBOX
, 2, true));
1003 EXPECT_EQ(order
[ 3], TaskItem(MESSAGEBOX
, 2, false));
1004 EXPECT_EQ(order
[ 4], TaskItem(RECURSIVE
, 3, true));
1005 EXPECT_EQ(order
[ 5], TaskItem(RECURSIVE
, 3, false));
1006 // When EndDialogTask is processed, the window is already dismissed, hence no
1008 EXPECT_EQ(order
[ 6], TaskItem(ENDDIALOG
, 4, true));
1009 EXPECT_EQ(order
[ 7], TaskItem(QUITMESSAGELOOP
, 5, true));
1010 EXPECT_EQ(order
[ 8], TaskItem(QUITMESSAGELOOP
, 5, false));
1011 EXPECT_EQ(order
[ 9], TaskItem(RECURSIVE
, 1, true));
1012 EXPECT_EQ(order
[10], TaskItem(RECURSIVE
, 1, false));
1013 EXPECT_EQ(order
[11], TaskItem(RECURSIVE
, 3, true));
1014 EXPECT_EQ(order
[12], TaskItem(RECURSIVE
, 3, false));
1015 EXPECT_EQ(order
[13], TaskItem(RECURSIVE
, 1, true));
1016 EXPECT_EQ(order
[14], TaskItem(RECURSIVE
, 1, false));
1017 EXPECT_EQ(order
[15], TaskItem(RECURSIVE
, 3, true));
1018 EXPECT_EQ(order
[16], TaskItem(RECURSIVE
, 3, false));
1021 // A side effect of this test is the generation a beep. Sorry. This test also
1022 // needs to process windows messages on the current thread.
1023 void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type
) {
1024 MessageLoop
loop(message_loop_type
);
1026 Thread
worker("RecursiveSupport2_worker");
1027 Thread::Options options
;
1028 options
.message_loop_type
= message_loop_type
;
1029 ASSERT_EQ(true, worker
.StartWithOptions(options
));
1031 base::win::ScopedHandle
event(CreateEvent(NULL
, FALSE
, FALSE
, NULL
));
1032 worker
.message_loop()->PostTask(FROM_HERE
,
1033 new Recursive2Tasks(MessageLoop::current(),
1038 // Let the other thread execute.
1039 WaitForSingleObject(event
, INFINITE
);
1040 MessageLoop::current()->Run();
1042 ASSERT_EQ(order
.size(), 18);
1043 EXPECT_EQ(order
[ 0], TaskItem(RECURSIVE
, 1, true));
1044 EXPECT_EQ(order
[ 1], TaskItem(RECURSIVE
, 1, false));
1045 EXPECT_EQ(order
[ 2], TaskItem(MESSAGEBOX
, 2, true));
1046 // Note that this executes in the MessageBox modal loop.
1047 EXPECT_EQ(order
[ 3], TaskItem(RECURSIVE
, 3, true));
1048 EXPECT_EQ(order
[ 4], TaskItem(RECURSIVE
, 3, false));
1049 EXPECT_EQ(order
[ 5], TaskItem(ENDDIALOG
, 4, true));
1050 EXPECT_EQ(order
[ 6], TaskItem(ENDDIALOG
, 4, false));
1051 EXPECT_EQ(order
[ 7], TaskItem(MESSAGEBOX
, 2, false));
1052 /* The order can subtly change here. The reason is that when RecursiveTask(1)
1053 is called in the main thread, if it is faster than getting to the
1054 PostTask(FROM_HERE, QuitTask) execution, the order of task execution can
1055 change. We don't care anyway that the order isn't correct.
1056 EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, true));
1057 EXPECT_EQ(order[ 9], TaskItem(QUITMESSAGELOOP, 5, false));
1058 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
1059 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
1061 EXPECT_EQ(order
[12], TaskItem(RECURSIVE
, 3, true));
1062 EXPECT_EQ(order
[13], TaskItem(RECURSIVE
, 3, false));
1063 EXPECT_EQ(order
[14], TaskItem(RECURSIVE
, 1, true));
1064 EXPECT_EQ(order
[15], TaskItem(RECURSIVE
, 1, false));
1065 EXPECT_EQ(order
[16], TaskItem(RECURSIVE
, 3, true));
1066 EXPECT_EQ(order
[17], TaskItem(RECURSIVE
, 3, false));
1069 #endif // defined(OS_WIN)
1071 class TaskThatPumps
: public OrderedTasks
{
1073 TaskThatPumps(TaskList
* order
, int cookie
)
1074 : OrderedTasks(order
, PUMPS
, cookie
) {
1077 virtual void Run() {
1079 bool old_state
= MessageLoop::current()->NestableTasksAllowed();
1080 MessageLoop::current()->SetNestableTasksAllowed(true);
1081 MessageLoop::current()->RunAllPending();
1082 MessageLoop::current()->SetNestableTasksAllowed(old_state
);
1087 // Tests that non nestable tasks run in FIFO if there are no nested loops.
1088 void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type
) {
1089 MessageLoop
loop(message_loop_type
);
1093 Task
* task
= new OrderedTasks(&order
, 1);
1094 MessageLoop::current()->PostNonNestableTask(FROM_HERE
, task
);
1095 MessageLoop::current()->PostTask(FROM_HERE
, new OrderedTasks(&order
, 2));
1096 MessageLoop::current()->PostTask(FROM_HERE
, new QuitTask(&order
, 3));
1097 MessageLoop::current()->Run();
1100 ASSERT_EQ(6U, order
.size());
1101 EXPECT_EQ(order
[ 0], TaskItem(ORDERERD
, 1, true));
1102 EXPECT_EQ(order
[ 1], TaskItem(ORDERERD
, 1, false));
1103 EXPECT_EQ(order
[ 2], TaskItem(ORDERERD
, 2, true));
1104 EXPECT_EQ(order
[ 3], TaskItem(ORDERERD
, 2, false));
1105 EXPECT_EQ(order
[ 4], TaskItem(QUITMESSAGELOOP
, 3, true));
1106 EXPECT_EQ(order
[ 5], TaskItem(QUITMESSAGELOOP
, 3, false));
1109 // Tests that non nestable tasks don't run when there's code in the call stack.
1110 void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type
,
1112 MessageLoop
loop(message_loop_type
);
1116 MessageLoop::current()->PostTask(FROM_HERE
,
1117 new TaskThatPumps(&order
, 1));
1118 Task
* task
= new OrderedTasks(&order
, 2);
1120 MessageLoop::current()->PostNonNestableDelayedTask(FROM_HERE
, task
, 1);
1122 MessageLoop::current()->PostNonNestableTask(FROM_HERE
, task
);
1124 MessageLoop::current()->PostTask(FROM_HERE
, new OrderedTasks(&order
, 3));
1125 MessageLoop::current()->PostTask(FROM_HERE
, new SleepTask(&order
, 4, 50));
1126 MessageLoop::current()->PostTask(FROM_HERE
, new OrderedTasks(&order
, 5));
1127 Task
* non_nestable_quit
= new QuitTask(&order
, 6);
1129 MessageLoop::current()->PostNonNestableDelayedTask(FROM_HERE
,
1133 MessageLoop::current()->PostNonNestableTask(FROM_HERE
, non_nestable_quit
);
1136 MessageLoop::current()->Run();
1139 ASSERT_EQ(12U, order
.size());
1140 EXPECT_EQ(order
[ 0], TaskItem(PUMPS
, 1, true));
1141 EXPECT_EQ(order
[ 1], TaskItem(ORDERERD
, 3, true));
1142 EXPECT_EQ(order
[ 2], TaskItem(ORDERERD
, 3, false));
1143 EXPECT_EQ(order
[ 3], TaskItem(SLEEP
, 4, true));
1144 EXPECT_EQ(order
[ 4], TaskItem(SLEEP
, 4, false));
1145 EXPECT_EQ(order
[ 5], TaskItem(ORDERERD
, 5, true));
1146 EXPECT_EQ(order
[ 6], TaskItem(ORDERERD
, 5, false));
1147 EXPECT_EQ(order
[ 7], TaskItem(PUMPS
, 1, false));
1148 EXPECT_EQ(order
[ 8], TaskItem(ORDERERD
, 2, true));
1149 EXPECT_EQ(order
[ 9], TaskItem(ORDERERD
, 2, false));
1150 EXPECT_EQ(order
[10], TaskItem(QUITMESSAGELOOP
, 6, true));
1151 EXPECT_EQ(order
[11], TaskItem(QUITMESSAGELOOP
, 6, false));
1156 class DispatcherImpl
: public MessageLoopForUI::Dispatcher
{
1158 DispatcherImpl() : dispatch_count_(0) {}
1160 virtual bool Dispatch(const MSG
& msg
) {
1161 ::TranslateMessage(&msg
);
1162 ::DispatchMessage(&msg
);
1163 // Do not count WM_TIMER since it is not what we post and it will cause
1165 if (msg
.message
!= WM_TIMER
)
1167 // We treat WM_LBUTTONUP as the last message.
1168 return msg
.message
!= WM_LBUTTONUP
;
1171 int dispatch_count_
;
1174 void RunTest_Dispatcher(MessageLoop::Type message_loop_type
) {
1175 MessageLoop
loop(message_loop_type
);
1177 class MyTask
: public Task
{
1179 virtual void Run() {
1180 PostMessage(NULL
, WM_LBUTTONDOWN
, 0, 0);
1181 PostMessage(NULL
, WM_LBUTTONUP
, 'A', 0);
1184 Task
* task
= new MyTask();
1185 MessageLoop::current()->PostDelayedTask(FROM_HERE
, task
, 100);
1186 DispatcherImpl dispatcher
;
1187 MessageLoopForUI::current()->Run(&dispatcher
);
1188 ASSERT_EQ(2, dispatcher
.dispatch_count_
);
1191 LRESULT CALLBACK
MsgFilterProc(int code
, WPARAM wparam
, LPARAM lparam
) {
1192 if (code
== base::MessagePumpForUI::kMessageFilterCode
) {
1193 MSG
* msg
= reinterpret_cast<MSG
*>(lparam
);
1194 if (msg
->message
== WM_LBUTTONDOWN
)
1200 void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type
) {
1201 MessageLoop
loop(message_loop_type
);
1203 class MyTask
: public Task
{
1205 virtual void Run() {
1206 PostMessage(NULL
, WM_LBUTTONDOWN
, 0, 0);
1207 PostMessage(NULL
, WM_LBUTTONUP
, 'A', 0);
1210 Task
* task
= new MyTask();
1211 MessageLoop::current()->PostDelayedTask(FROM_HERE
, task
, 100);
1212 HHOOK msg_hook
= SetWindowsHookEx(WH_MSGFILTER
,
1215 GetCurrentThreadId());
1216 DispatcherImpl dispatcher
;
1217 MessageLoopForUI::current()->Run(&dispatcher
);
1218 ASSERT_EQ(1, dispatcher
.dispatch_count_
);
1219 UnhookWindowsHookEx(msg_hook
);
1222 class TestIOHandler
: public MessageLoopForIO::IOHandler
{
1224 TestIOHandler(const wchar_t* name
, HANDLE signal
, bool wait
);
1226 virtual void OnIOCompleted(MessageLoopForIO::IOContext
* context
,
1227 DWORD bytes_transfered
, DWORD error
);
1231 OVERLAPPED
* context() { return &context_
.overlapped
; }
1232 DWORD
size() { return sizeof(buffer_
); }
1236 MessageLoopForIO::IOContext context_
;
1238 base::win::ScopedHandle file_
;
1242 TestIOHandler::TestIOHandler(const wchar_t* name
, HANDLE signal
, bool wait
)
1243 : signal_(signal
), wait_(wait
) {
1244 memset(buffer_
, 0, sizeof(buffer_
));
1245 memset(&context_
, 0, sizeof(context_
));
1246 context_
.handler
= this;
1248 file_
.Set(CreateFile(name
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
1249 FILE_FLAG_OVERLAPPED
, NULL
));
1250 EXPECT_TRUE(file_
.IsValid());
1253 void TestIOHandler::Init() {
1254 MessageLoopForIO::current()->RegisterIOHandler(file_
, this);
1257 EXPECT_FALSE(ReadFile(file_
, buffer_
, size(), &read
, context()));
1258 EXPECT_EQ(ERROR_IO_PENDING
, GetLastError());
1263 void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext
* context
,
1264 DWORD bytes_transfered
, DWORD error
) {
1265 ASSERT_TRUE(context
== &context_
);
1266 ASSERT_TRUE(SetEvent(signal_
));
1269 void TestIOHandler::WaitForIO() {
1270 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this));
1271 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this));
1274 class IOHandlerTask
: public Task
{
1276 explicit IOHandlerTask(TestIOHandler
* handler
) : handler_(handler
) {}
1277 virtual void Run() {
1282 TestIOHandler
* handler_
;
1285 void RunTest_IOHandler() {
1286 base::win::ScopedHandle
callback_called(CreateEvent(NULL
, TRUE
, FALSE
, NULL
));
1287 ASSERT_TRUE(callback_called
.IsValid());
1289 const wchar_t* kPipeName
= L
"\\\\.\\pipe\\iohandler_pipe";
1290 base::win::ScopedHandle
server(
1291 CreateNamedPipe(kPipeName
, PIPE_ACCESS_OUTBOUND
, 0, 1, 0, 0, 0, NULL
));
1292 ASSERT_TRUE(server
.IsValid());
1294 Thread
thread("IOHandler test");
1295 Thread::Options options
;
1296 options
.message_loop_type
= MessageLoop::TYPE_IO
;
1297 ASSERT_TRUE(thread
.StartWithOptions(options
));
1299 MessageLoop
* thread_loop
= thread
.message_loop();
1300 ASSERT_TRUE(NULL
!= thread_loop
);
1302 TestIOHandler
handler(kPipeName
, callback_called
, false);
1303 IOHandlerTask
* task
= new IOHandlerTask(&handler
);
1304 thread_loop
->PostTask(FROM_HERE
, task
);
1305 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
1307 const char buffer
[] = "Hello there!";
1309 EXPECT_TRUE(WriteFile(server
, buffer
, sizeof(buffer
), &written
, NULL
));
1311 DWORD result
= WaitForSingleObject(callback_called
, 1000);
1312 EXPECT_EQ(WAIT_OBJECT_0
, result
);
1317 void RunTest_WaitForIO() {
1318 base::win::ScopedHandle
callback1_called(
1319 CreateEvent(NULL
, TRUE
, FALSE
, NULL
));
1320 base::win::ScopedHandle
callback2_called(
1321 CreateEvent(NULL
, TRUE
, FALSE
, NULL
));
1322 ASSERT_TRUE(callback1_called
.IsValid());
1323 ASSERT_TRUE(callback2_called
.IsValid());
1325 const wchar_t* kPipeName1
= L
"\\\\.\\pipe\\iohandler_pipe1";
1326 const wchar_t* kPipeName2
= L
"\\\\.\\pipe\\iohandler_pipe2";
1327 base::win::ScopedHandle
server1(
1328 CreateNamedPipe(kPipeName1
, PIPE_ACCESS_OUTBOUND
, 0, 1, 0, 0, 0, NULL
));
1329 base::win::ScopedHandle
server2(
1330 CreateNamedPipe(kPipeName2
, PIPE_ACCESS_OUTBOUND
, 0, 1, 0, 0, 0, NULL
));
1331 ASSERT_TRUE(server1
.IsValid());
1332 ASSERT_TRUE(server2
.IsValid());
1334 Thread
thread("IOHandler test");
1335 Thread::Options options
;
1336 options
.message_loop_type
= MessageLoop::TYPE_IO
;
1337 ASSERT_TRUE(thread
.StartWithOptions(options
));
1339 MessageLoop
* thread_loop
= thread
.message_loop();
1340 ASSERT_TRUE(NULL
!= thread_loop
);
1342 TestIOHandler
handler1(kPipeName1
, callback1_called
, false);
1343 TestIOHandler
handler2(kPipeName2
, callback2_called
, true);
1344 IOHandlerTask
* task1
= new IOHandlerTask(&handler1
);
1345 IOHandlerTask
* task2
= new IOHandlerTask(&handler2
);
1346 thread_loop
->PostTask(FROM_HERE
, task1
);
1347 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
1348 thread_loop
->PostTask(FROM_HERE
, task2
);
1351 // At this time handler1 is waiting to be called, and the thread is waiting
1352 // on the Init method of handler2, filtering only handler2 callbacks.
1354 const char buffer
[] = "Hello there!";
1356 EXPECT_TRUE(WriteFile(server1
, buffer
, sizeof(buffer
), &written
, NULL
));
1358 EXPECT_EQ(WAIT_TIMEOUT
, WaitForSingleObject(callback1_called
, 0)) <<
1359 "handler1 has not been called";
1361 EXPECT_TRUE(WriteFile(server2
, buffer
, sizeof(buffer
), &written
, NULL
));
1363 HANDLE objects
[2] = { callback1_called
.Get(), callback2_called
.Get() };
1364 DWORD result
= WaitForMultipleObjects(2, objects
, TRUE
, 1000);
1365 EXPECT_EQ(WAIT_OBJECT_0
, result
);
1370 #endif // defined(OS_WIN)
1374 //-----------------------------------------------------------------------------
1375 // Each test is run against each type of MessageLoop. That way we are sure
1376 // that message loops work properly in all configurations. Of course, in some
1377 // cases, a unit test may only be for a particular type of loop.
1379 TEST(MessageLoopTest
, PostTask
) {
1380 RunTest_PostTask(MessageLoop::TYPE_DEFAULT
);
1381 RunTest_PostTask(MessageLoop::TYPE_UI
);
1382 RunTest_PostTask(MessageLoop::TYPE_IO
);
1385 TEST(MessageLoopTest
, PostTask_SEH
) {
1386 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT
);
1387 RunTest_PostTask_SEH(MessageLoop::TYPE_UI
);
1388 RunTest_PostTask_SEH(MessageLoop::TYPE_IO
);
1391 TEST(MessageLoopTest
, PostDelayedTask_Basic
) {
1392 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT
);
1393 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI
);
1394 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO
);
1397 TEST(MessageLoopTest
, PostDelayedTask_InDelayOrder
) {
1398 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT
);
1399 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI
);
1400 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO
);
1403 TEST(MessageLoopTest
, PostDelayedTask_InPostOrder
) {
1404 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT
);
1405 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI
);
1406 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO
);
1409 TEST(MessageLoopTest
, PostDelayedTask_InPostOrder_2
) {
1410 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT
);
1411 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI
);
1412 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO
);
1415 TEST(MessageLoopTest
, PostDelayedTask_InPostOrder_3
) {
1416 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT
);
1417 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI
);
1418 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO
);
1421 TEST(MessageLoopTest
, PostDelayedTask_SharedTimer
) {
1422 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT
);
1423 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI
);
1424 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO
);
1428 TEST(MessageLoopTest
, PostDelayedTask_SharedTimer_SubPump
) {
1429 RunTest_PostDelayedTask_SharedTimer_SubPump();
1433 // TODO(darin): MessageLoop does not support deleting all tasks in the
1435 // Fails, http://crbug.com/50272.
1436 TEST(MessageLoopTest
, FAILS_EnsureTaskDeletion
) {
1437 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT
);
1438 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI
);
1439 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO
);
1442 // TODO(darin): MessageLoop does not support deleting all tasks in the
1444 // Fails, http://crbug.com/50272.
1445 TEST(MessageLoopTest
, FAILS_EnsureTaskDeletion_Chain
) {
1446 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT
);
1447 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI
);
1448 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO
);
1452 TEST(MessageLoopTest
, Crasher
) {
1453 RunTest_Crasher(MessageLoop::TYPE_DEFAULT
);
1454 RunTest_Crasher(MessageLoop::TYPE_UI
);
1455 RunTest_Crasher(MessageLoop::TYPE_IO
);
1458 TEST(MessageLoopTest
, CrasherNasty
) {
1459 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT
);
1460 RunTest_CrasherNasty(MessageLoop::TYPE_UI
);
1461 RunTest_CrasherNasty(MessageLoop::TYPE_IO
);
1463 #endif // defined(OS_WIN)
1465 TEST(MessageLoopTest
, Nesting
) {
1466 RunTest_Nesting(MessageLoop::TYPE_DEFAULT
);
1467 RunTest_Nesting(MessageLoop::TYPE_UI
);
1468 RunTest_Nesting(MessageLoop::TYPE_IO
);
1471 TEST(MessageLoopTest
, RecursiveDenial1
) {
1472 RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT
);
1473 RunTest_RecursiveDenial1(MessageLoop::TYPE_UI
);
1474 RunTest_RecursiveDenial1(MessageLoop::TYPE_IO
);
1477 TEST(MessageLoopTest
, RecursiveDenial3
) {
1478 RunTest_RecursiveDenial3(MessageLoop::TYPE_DEFAULT
);
1479 RunTest_RecursiveDenial3(MessageLoop::TYPE_UI
);
1480 RunTest_RecursiveDenial3(MessageLoop::TYPE_IO
);
1483 TEST(MessageLoopTest
, RecursiveSupport1
) {
1484 RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT
);
1485 RunTest_RecursiveSupport1(MessageLoop::TYPE_UI
);
1486 RunTest_RecursiveSupport1(MessageLoop::TYPE_IO
);
1490 // This test occasionally hangs http://crbug.com/44567
1491 TEST(MessageLoopTest
, DISABLED_RecursiveDenial2
) {
1492 RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT
);
1493 RunTest_RecursiveDenial2(MessageLoop::TYPE_UI
);
1494 RunTest_RecursiveDenial2(MessageLoop::TYPE_IO
);
1497 TEST(MessageLoopTest
, RecursiveSupport2
) {
1498 // This test requires a UI loop
1499 RunTest_RecursiveSupport2(MessageLoop::TYPE_UI
);
1501 #endif // defined(OS_WIN)
1503 TEST(MessageLoopTest
, NonNestableWithNoNesting
) {
1504 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT
);
1505 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI
);
1506 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO
);
1509 TEST(MessageLoopTest
, NonNestableInNestedLoop
) {
1510 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT
, false);
1511 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI
, false);
1512 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO
, false);
1515 TEST(MessageLoopTest
, NonNestableDelayedInNestedLoop
) {
1516 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT
, true);
1517 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI
, true);
1518 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO
, true);
1521 class DummyTask
: public Task
{
1523 explicit DummyTask(int num_tasks
) : num_tasks_(num_tasks
) {}
1525 virtual void Run() {
1526 if (num_tasks_
> 1) {
1527 MessageLoop::current()->PostTask(
1529 new DummyTask(num_tasks_
- 1));
1531 MessageLoop::current()->Quit();
1536 const int num_tasks_
;
1539 class DummyTaskObserver
: public MessageLoop::TaskObserver
{
1541 explicit DummyTaskObserver(int num_tasks
)
1542 : num_tasks_started_(0),
1543 num_tasks_processed_(0),
1544 num_tasks_(num_tasks
) {}
1546 virtual ~DummyTaskObserver() {}
1548 virtual void WillProcessTask(const Task
* task
) {
1549 num_tasks_started_
++;
1550 EXPECT_TRUE(task
!= NULL
);
1551 EXPECT_LE(num_tasks_started_
, num_tasks_
);
1552 EXPECT_EQ(num_tasks_started_
, num_tasks_processed_
+ 1);
1555 virtual void DidProcessTask(const Task
* task
) {
1556 num_tasks_processed_
++;
1557 EXPECT_TRUE(task
!= NULL
);
1558 EXPECT_LE(num_tasks_started_
, num_tasks_
);
1559 EXPECT_EQ(num_tasks_started_
, num_tasks_processed_
);
1562 int num_tasks_started() const { return num_tasks_started_
; }
1563 int num_tasks_processed() const { return num_tasks_processed_
; }
1566 int num_tasks_started_
;
1567 int num_tasks_processed_
;
1568 const int num_tasks_
;
1570 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver
);
1573 TEST(MessageLoopTest
, TaskObserver
) {
1574 const int kNumTasks
= 6;
1575 DummyTaskObserver
observer(kNumTasks
);
1578 loop
.AddTaskObserver(&observer
);
1579 loop
.PostTask(FROM_HERE
, new DummyTask(kNumTasks
));
1581 loop
.RemoveTaskObserver(&observer
);
1583 EXPECT_EQ(kNumTasks
, observer
.num_tasks_started());
1584 EXPECT_EQ(kNumTasks
, observer
.num_tasks_processed());
1588 TEST(MessageLoopTest
, Dispatcher
) {
1589 // This test requires a UI loop
1590 RunTest_Dispatcher(MessageLoop::TYPE_UI
);
1593 TEST(MessageLoopTest
, DispatcherWithMessageHook
) {
1594 // This test requires a UI loop
1595 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI
);
1598 TEST(MessageLoopTest
, IOHandler
) {
1599 RunTest_IOHandler();
1602 TEST(MessageLoopTest
, WaitForIO
) {
1603 RunTest_WaitForIO();
1606 TEST(MessageLoopTest
, HighResolutionTimer
) {
1609 const int kFastTimerMs
= 5;
1610 const int kSlowTimerMs
= 100;
1612 EXPECT_FALSE(loop
.high_resolution_timers_enabled());
1614 // Post a fast task to enable the high resolution timers.
1615 loop
.PostDelayedTask(FROM_HERE
, new DummyTask(1), kFastTimerMs
);
1617 EXPECT_TRUE(loop
.high_resolution_timers_enabled());
1619 // Post a slow task and verify high resolution timers
1620 // are still enabled.
1621 loop
.PostDelayedTask(FROM_HERE
, new DummyTask(1), kSlowTimerMs
);
1623 EXPECT_TRUE(loop
.high_resolution_timers_enabled());
1625 // Wait for a while so that high-resolution mode elapses.
1626 Sleep(MessageLoop::kHighResolutionTimerModeLeaseTimeMs
);
1628 // Post a slow task to disable the high resolution timers.
1629 loop
.PostDelayedTask(FROM_HERE
, new DummyTask(1), kSlowTimerMs
);
1631 EXPECT_FALSE(loop
.high_resolution_timers_enabled());
1634 #endif // defined(OS_WIN)
1636 #if defined(OS_POSIX) && !defined(OS_NACL)
1640 class QuitDelegate
: public base::MessagePumpLibevent::Watcher
{
1642 virtual void OnFileCanWriteWithoutBlocking(int fd
) {
1643 MessageLoop::current()->Quit();
1645 virtual void OnFileCanReadWithoutBlocking(int fd
) {
1646 MessageLoop::current()->Quit();
1650 TEST(MessageLoopTest
, FileDescriptorWatcherOutlivesMessageLoop
) {
1651 // Simulate a MessageLoop that dies before an FileDescriptorWatcher.
1652 // This could happen when people use the Singleton pattern or atexit.
1654 // Create a file descriptor. Doesn't need to be readable or writable,
1655 // as we don't need to actually get any notifications.
1656 // pipe() is just the easiest way to do it.
1658 int err
= pipe(pipefds
);
1660 int fd
= pipefds
[1];
1662 // Arrange for controller to live longer than message loop.
1663 base::MessagePumpLibevent::FileDescriptorWatcher controller
;
1665 MessageLoopForIO message_loop
;
1667 QuitDelegate delegate
;
1668 message_loop
.WatchFileDescriptor(fd
,
1669 true, MessageLoopForIO::WATCH_WRITE
, &controller
, &delegate
);
1670 // and don't run the message loop, just destroy it.
1673 if (HANDLE_EINTR(close(pipefds
[0])) < 0)
1674 PLOG(ERROR
) << "close";
1675 if (HANDLE_EINTR(close(pipefds
[1])) < 0)
1676 PLOG(ERROR
) << "close";
1679 TEST(MessageLoopTest
, FileDescriptorWatcherDoubleStop
) {
1680 // Verify that it's ok to call StopWatchingFileDescriptor().
1681 // (Errors only showed up in valgrind.)
1683 int err
= pipe(pipefds
);
1685 int fd
= pipefds
[1];
1687 // Arrange for message loop to live longer than controller.
1688 MessageLoopForIO message_loop
;
1690 base::MessagePumpLibevent::FileDescriptorWatcher controller
;
1692 QuitDelegate delegate
;
1693 message_loop
.WatchFileDescriptor(fd
,
1694 true, MessageLoopForIO::WATCH_WRITE
, &controller
, &delegate
);
1695 controller
.StopWatchingFileDescriptor();
1698 if (HANDLE_EINTR(close(pipefds
[0])) < 0)
1699 PLOG(ERROR
) << "close";
1700 if (HANDLE_EINTR(close(pipefds
[1])) < 0)
1701 PLOG(ERROR
) << "close";
1706 #endif // defined(OS_POSIX) && !defined(OS_NACL)
1709 class RunAtDestructionTask
: public Task
{
1711 RunAtDestructionTask(bool* task_destroyed
, bool* destruction_observer_called
)
1712 : task_destroyed_(task_destroyed
),
1713 destruction_observer_called_(destruction_observer_called
) {
1715 ~RunAtDestructionTask() {
1716 EXPECT_FALSE(*destruction_observer_called_
);
1717 *task_destroyed_
= true;
1719 virtual void Run() {
1720 // This task should never run.
1724 bool* task_destroyed_
;
1725 bool* destruction_observer_called_
;
1728 class MLDestructionObserver
: public MessageLoop::DestructionObserver
{
1730 MLDestructionObserver(bool* task_destroyed
, bool* destruction_observer_called
)
1731 : task_destroyed_(task_destroyed
),
1732 destruction_observer_called_(destruction_observer_called
),
1733 task_destroyed_before_message_loop_(false) {
1735 virtual void WillDestroyCurrentMessageLoop() {
1736 task_destroyed_before_message_loop_
= *task_destroyed_
;
1737 *destruction_observer_called_
= true;
1739 bool task_destroyed_before_message_loop() const {
1740 return task_destroyed_before_message_loop_
;
1743 bool* task_destroyed_
;
1744 bool* destruction_observer_called_
;
1745 bool task_destroyed_before_message_loop_
;
1750 TEST(MessageLoopTest
, DestructionObserverTest
) {
1751 // Verify that the destruction observer gets called at the very end (after
1752 // all the pending tasks have been destroyed).
1753 MessageLoop
* loop
= new MessageLoop
;
1754 const int kDelayMS
= 100;
1756 bool task_destroyed
= false;
1757 bool destruction_observer_called
= false;
1759 MLDestructionObserver
observer(&task_destroyed
, &destruction_observer_called
);
1760 loop
->AddDestructionObserver(&observer
);
1761 loop
->PostDelayedTask(
1763 new RunAtDestructionTask(&task_destroyed
, &destruction_observer_called
),
1766 EXPECT_TRUE(observer
.task_destroyed_before_message_loop());
1767 // The task should have been destroyed when we deleted the loop.
1768 EXPECT_TRUE(task_destroyed
);
1769 EXPECT_TRUE(destruction_observer_called
);