blob: ca84f60beb06d642e8676fb6919500bd74d63df6 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commitd7cae122008-07-26 21:49:384
5#include "base/logging.h"
6#include "base/message_loop.h"
[email protected]b16ef312008-08-19 18:36:237#include "base/platform_thread.h"
initial.commitd7cae122008-07-26 21:49:388#include "base/ref_counted.h"
[email protected]b16ef312008-08-19 18:36:239#include "base/thread.h"
initial.commitd7cae122008-07-26 21:49:3810#include "testing/gtest/include/gtest/gtest.h"
11
[email protected]295039bd2008-08-15 04:32:5712#if defined(OS_WIN)
13#include "base/message_pump_win.h"
[email protected]b16ef312008-08-19 18:36:2314#include "base/scoped_handle.h"
[email protected]295039bd2008-08-15 04:32:5715#endif
16
[email protected]b16ef312008-08-19 18:36:2317//
18// TODO(darin): This file needs to be re-organized into acceptance tests that
19// apply to a MessageLoop configured to use any MessagePump. Then, those tests
20// need to be run against all MessagePump types supported by a platform.
21//
22// Finally, platform-specific MessageLoop tests should be grouped together to
23// avoid the chopping this file up with so many #ifdefs.
24//
25
initial.commitd7cae122008-07-26 21:49:3826namespace {
27
28class MessageLoopTest : public testing::Test {
29 public:
30 virtual void SetUp() {
31 enable_recursive_task_ = MessageLoop::current()->NestableTasksAllowed();
32 }
33 virtual void TearDown() {
34 MessageLoop::current()->SetNestableTasksAllowed(enable_recursive_task_);
35 }
36 private:
37 bool enable_recursive_task_;
38};
39
40class Foo : public base::RefCounted<Foo> {
41 public:
42 Foo() : test_count_(0) {
43 }
44
45 void Test0() {
46 ++test_count_;
47 }
48
49 void Test1ConstRef(const std::string& a) {
50 ++test_count_;
51 result_.append(a);
52 }
53
54 void Test1Ptr(std::string* a) {
55 ++test_count_;
56 result_.append(*a);
57 }
58
59 void Test1Int(int a) {
60 test_count_ += a;
61 }
62
63 void Test2Ptr(std::string* a, std::string* b) {
64 ++test_count_;
65 result_.append(*a);
66 result_.append(*b);
67 }
68
69 void Test2Mixed(const std::string& a, std::string* b) {
70 ++test_count_;
71 result_.append(a);
72 result_.append(*b);
73 }
74
75 int test_count() const { return test_count_; }
76 const std::string& result() const { return result_; }
77
78 private:
79 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 }
88};
89
90} // namespace
91
92TEST(MessageLoopTest, PostTask) {
93 // Add tests to message loop
94 scoped_refptr<Foo> foo = new Foo();
95 std::string a("a"), b("b"), c("c"), d("d");
96 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
97 foo.get(), &Foo::Test0));
98 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
99 foo.get(), &Foo::Test1ConstRef, a));
100 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
101 foo.get(), &Foo::Test1Ptr, &b));
102 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
103 foo.get(), &Foo::Test1Int, 100));
104 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
105 foo.get(), &Foo::Test2Ptr, &a, &c));
106 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
107 foo.get(), &Foo::Test2Mixed, a, &d));
108
109 // After all tests, post a message that will shut down the message loop
110 scoped_refptr<QuitMsgLoop> quit = new QuitMsgLoop();
111 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
112 quit.get(), &QuitMsgLoop::QuitNow));
113
114 // Now kick things off
115 MessageLoop::current()->Run();
116
117 EXPECT_EQ(foo->test_count(), 105);
118 EXPECT_EQ(foo->result(), "abacad");
119}
120
121TEST(MessageLoopTest, InvokeLater_SEH) {
122 // Add tests to message loop
123 scoped_refptr<Foo> foo = new Foo();
124 std::string a("a"), b("b"), c("c"), d("d");
125 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
126 foo.get(), &Foo::Test0));
127 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
128 foo.get(), &Foo::Test1ConstRef, a));
129 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
130 foo.get(), &Foo::Test1Ptr, &b));
131 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
132 foo.get(), &Foo::Test1Int, 100));
133 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
134 foo.get(), &Foo::Test2Ptr, &a, &c));
135 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
136 foo.get(), &Foo::Test2Mixed, a, &d));
137
138 // After all tests, post a message that will shut down the message loop
139 scoped_refptr<QuitMsgLoop> quit = new QuitMsgLoop();
140 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
141 quit.get(), &QuitMsgLoop::QuitNow));
142
143 // Now kick things off with the SEH block active.
144 MessageLoop::current()->set_exception_restoration(true);
145 MessageLoop::current()->Run();
146 MessageLoop::current()->set_exception_restoration(false);
147
148 EXPECT_EQ(foo->test_count(), 105);
149 EXPECT_EQ(foo->result(), "abacad");
150}
151
152namespace {
153
154class NestingTest : public Task {
155 public:
156 explicit NestingTest(int* depth) : depth_(depth) {
157 }
158 void Run() {
159 if (*depth_ > 0) {
160 *depth_ -= 1;
161 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_));
162
163 MessageLoop::current()->SetNestableTasksAllowed(true);
164 MessageLoop::current()->Run();
165 }
166 MessageLoop::current()->Quit();
167 }
168 private:
169 int* depth_;
170};
171
[email protected]b16ef312008-08-19 18:36:23172#if defined(OS_WIN)
173
initial.commitd7cae122008-07-26 21:49:38174LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) {
175 ADD_FAILURE() << "bad exception handler";
176 ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode);
177 return EXCEPTION_EXECUTE_HANDLER;
178}
179
180// This task throws an SEH exception: initially write to an invalid address.
181// If the right SEH filter is installed, it will fix the error.
182class CrasherTask : public Task {
183 public:
184 // Ctor. If trash_SEH_handler is true, the task will override the unhandled
185 // exception handler with one sure to crash this test.
186 explicit CrasherTask(bool trash_SEH_handler)
187 : trash_SEH_handler_(trash_SEH_handler) {
188 }
189 void Run() {
[email protected]b16ef312008-08-19 18:36:23190 PlatformThread::Sleep(1);
initial.commitd7cae122008-07-26 21:49:38191 if (trash_SEH_handler_)
192 ::SetUnhandledExceptionFilter(&BadExceptionHandler);
193 // Generate a SEH fault. We do it in asm to make sure we know how to undo
194 // the damage.
[email protected]c88873922008-07-30 13:02:03195
196#if defined(_M_IX86)
197
initial.commitd7cae122008-07-26 21:49:38198 __asm {
199 mov eax, dword ptr [CrasherTask::bad_array_]
200 mov byte ptr [eax], 66
201 }
[email protected]c88873922008-07-30 13:02:03202
203#elif defined(_M_X64)
204
205 bad_array_[0] = 66;
206
[email protected]b16ef312008-08-19 18:36:23207#else
208#error "needs architecture support"
[email protected]c88873922008-07-30 13:02:03209#endif
210
initial.commitd7cae122008-07-26 21:49:38211 MessageLoop::current()->Quit();
212 }
213 // Points the bad array to a valid memory location.
214 static void FixError() {
215 bad_array_ = &valid_store_;
216 }
217
218 private:
219 bool trash_SEH_handler_;
220 static volatile char* bad_array_;
221 static char valid_store_;
222};
223
224volatile char* CrasherTask::bad_array_ = 0;
225char CrasherTask::valid_store_ = 0;
226
227// This SEH filter fixes the problem and retries execution. Fixing requires
228// that the last instruction: mov eax, [CrasherTask::bad_array_] to be retried
229// so we move the instruction pointer 5 bytes back.
230LONG WINAPI HandleCrasherTaskException(EXCEPTION_POINTERS *ex_info) {
231 if (ex_info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
232 return EXCEPTION_EXECUTE_HANDLER;
233
234 CrasherTask::FixError();
[email protected]c88873922008-07-30 13:02:03235
236#if defined(_M_IX86)
237
initial.commitd7cae122008-07-26 21:49:38238 ex_info->ContextRecord->Eip -= 5;
[email protected]c88873922008-07-30 13:02:03239
240#elif defined(_M_X64)
241
242 ex_info->ContextRecord->Rip -= 5;
243
244#endif
245
initial.commitd7cae122008-07-26 21:49:38246 return EXCEPTION_CONTINUE_EXECUTION;
247}
248
[email protected]b16ef312008-08-19 18:36:23249#endif // defined(OS_WIN)
250
initial.commitd7cae122008-07-26 21:49:38251} // namespace
252
253
[email protected]295039bd2008-08-15 04:32:57254#if defined(OS_WIN)
255
initial.commitd7cae122008-07-26 21:49:38256TEST(MessageLoopTest, Crasher) {
257 if (::IsDebuggerPresent())
258 return;
259
260 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
261 ::SetUnhandledExceptionFilter(&HandleCrasherTaskException);
262
263 MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(false));
264 MessageLoop::current()->set_exception_restoration(true);
265 MessageLoop::current()->Run();
266 MessageLoop::current()->set_exception_restoration(false);
267
268 ::SetUnhandledExceptionFilter(old_SEH_filter);
269}
270
initial.commitd7cae122008-07-26 21:49:38271TEST(MessageLoopTest, CrasherNasty) {
272 if (::IsDebuggerPresent())
273 return;
274
275 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
276 ::SetUnhandledExceptionFilter(&HandleCrasherTaskException);
277
278 MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(true));
279 MessageLoop::current()->set_exception_restoration(true);
280 MessageLoop::current()->Run();
281 MessageLoop::current()->set_exception_restoration(false);
282
283 ::SetUnhandledExceptionFilter(old_SEH_filter);
284}
285
[email protected]295039bd2008-08-15 04:32:57286#endif // defined(OS_WIN)
287
[email protected]c88873922008-07-30 13:02:03288
initial.commitd7cae122008-07-26 21:49:38289TEST(MessageLoopTest, Nesting) {
290 int depth = 100;
291 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(&depth));
292 MessageLoop::current()->Run();
293 EXPECT_EQ(depth, 0);
294}
295
296namespace {
297
298const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test";
299
300enum TaskType {
301 MESSAGEBOX,
302 ENDDIALOG,
303 RECURSIVE,
304 TIMEDMESSAGELOOP,
305 QUITMESSAGELOOP,
306 ORDERERD,
307 PUMPS,
308};
309
310// Saves the order in which the tasks executed.
311struct TaskItem {
312 TaskItem(TaskType t, int c, bool s)
313 : type(t),
314 cookie(c),
315 start(s) {
316 }
317
318 TaskType type;
319 int cookie;
320 bool start;
321
322 bool operator == (const TaskItem& other) const {
323 return type == other.type && cookie == other.cookie && start == other.start;
324 }
325};
326
327typedef std::vector<TaskItem> TaskList;
328
329std::ostream& operator <<(std::ostream& os, TaskType type) {
330 switch (type) {
331 case MESSAGEBOX: os << "MESSAGEBOX"; break;
332 case ENDDIALOG: os << "ENDDIALOG"; break;
333 case RECURSIVE: os << "RECURSIVE"; break;
334 case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break;
335 case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break;
336 case ORDERERD: os << "ORDERERD"; break;
337 case PUMPS: os << "PUMPS"; break;
338 default:
339 NOTREACHED();
340 os << "Unknown TaskType";
341 break;
342 }
343 return os;
344}
345
346std::ostream& operator <<(std::ostream& os, const TaskItem& item) {
347 if (item.start)
348 return os << item.type << " " << item.cookie << " starts";
349 else
350 return os << item.type << " " << item.cookie << " ends";
351}
352
353// Saves the order the tasks ran.
354class OrderedTasks : public Task {
355 public:
356 OrderedTasks(TaskList* order, int cookie)
357 : order_(order),
358 type_(ORDERERD),
359 cookie_(cookie) {
360 }
361 OrderedTasks(TaskList* order, TaskType type, int cookie)
362 : order_(order),
363 type_(type),
364 cookie_(cookie) {
365 }
366
367 void RunStart() {
368 TaskItem item(type_, cookie_, true);
369 DLOG(INFO) << item;
370 order_->push_back(item);
371 }
372 void RunEnd() {
373 TaskItem item(type_, cookie_, false);
374 DLOG(INFO) << item;
375 order_->push_back(item);
376 }
377
378 virtual void Run() {
379 RunStart();
380 RunEnd();
381 }
382
383 protected:
384 TaskList* order() const {
385 return order_;
386 }
387
388 int cookie() const {
389 return cookie_;
390 }
391
392 private:
393 TaskList* order_;
394 TaskType type_;
395 int cookie_;
396};
397
[email protected]b16ef312008-08-19 18:36:23398#if defined(OS_WIN)
399
initial.commitd7cae122008-07-26 21:49:38400// MessageLoop implicitly start a "modal message loop". Modal dialog boxes,
401// common controls (like OpenFile) and StartDoc printing function can cause
402// implicit message loops.
403class MessageBoxTask : public OrderedTasks {
404 public:
405 MessageBoxTask(TaskList* order, int cookie, bool is_reentrant)
406 : OrderedTasks(order, MESSAGEBOX, cookie),
407 is_reentrant_(is_reentrant) {
408 }
409
410 virtual void Run() {
411 RunStart();
412 if (is_reentrant_)
413 MessageLoop::current()->SetNestableTasksAllowed(true);
414 MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK);
415 RunEnd();
416 }
417
418 private:
419 bool is_reentrant_;
420};
421
422// Will end the MessageBox.
423class EndDialogTask : public OrderedTasks {
424 public:
425 EndDialogTask(TaskList* order, int cookie)
426 : OrderedTasks(order, ENDDIALOG, cookie) {
427 }
428
429 virtual void Run() {
430 RunStart();
431 HWND window = GetActiveWindow();
432 if (window != NULL) {
433 EXPECT_NE(EndDialog(window, IDCONTINUE), 0);
434 // Cheap way to signal that the window wasn't found if RunEnd() isn't
435 // called.
436 RunEnd();
437 }
438 }
439};
440
[email protected]b16ef312008-08-19 18:36:23441#endif // defined(OS_WIN)
442
initial.commitd7cae122008-07-26 21:49:38443class RecursiveTask : public OrderedTasks {
444 public:
445 RecursiveTask(int depth, TaskList* order, int cookie, bool is_reentrant)
446 : OrderedTasks(order, RECURSIVE, cookie),
447 depth_(depth),
448 is_reentrant_(is_reentrant) {
449 }
450
451 virtual void Run() {
452 RunStart();
453 if (depth_ > 0) {
454 if (is_reentrant_)
455 MessageLoop::current()->SetNestableTasksAllowed(true);
456 MessageLoop::current()->PostTask(FROM_HERE,
457 new RecursiveTask(depth_ - 1, order(), cookie(), is_reentrant_));
458 }
459 RunEnd();
460 }
461
462 private:
463 int depth_;
464 bool is_reentrant_;
465};
466
467class QuitTask : public OrderedTasks {
468 public:
469 QuitTask(TaskList* order, int cookie)
470 : OrderedTasks(order, QUITMESSAGELOOP, cookie) {
471 }
472
473 virtual void Run() {
474 RunStart();
475 MessageLoop::current()->Quit();
476 RunEnd();
477 }
478};
479
[email protected]295039bd2008-08-15 04:32:57480#if defined(OS_WIN)
481
initial.commitd7cae122008-07-26 21:49:38482class Recursive2Tasks : public Task {
483 public:
484 Recursive2Tasks(MessageLoop* target,
485 HANDLE event,
486 bool expect_window,
487 TaskList* order,
488 bool is_reentrant)
489 : target_(target),
490 event_(event),
491 expect_window_(expect_window),
492 order_(order),
493 is_reentrant_(is_reentrant) {
494 }
495
496 virtual void Run() {
497 target_->PostTask(FROM_HERE,
498 new RecursiveTask(2, order_, 1, is_reentrant_));
499 target_->PostTask(FROM_HERE,
500 new MessageBoxTask(order_, 2, is_reentrant_));
501 target_->PostTask(FROM_HERE,
502 new RecursiveTask(2, order_, 3, is_reentrant_));
503 // The trick here is that for recursive task processing, this task will be
504 // ran _inside_ the MessageBox message loop, dismissing the MessageBox
505 // without a chance.
506 // For non-recursive task processing, this will be executed _after_ the
507 // MessageBox will have been dismissed by the code below, where
508 // expect_window_ is true.
509 target_->PostTask(FROM_HERE, new EndDialogTask(order_, 4));
510 target_->PostTask(FROM_HERE, new QuitTask(order_, 5));
511
512 // Enforce that every tasks are sent before starting to run the main thread
513 // message loop.
514 ASSERT_TRUE(SetEvent(event_));
515
516 // Poll for the MessageBox. Don't do this at home! At the speed we do it,
517 // you will never realize one MessageBox was shown.
518 for (; expect_window_;) {
519 HWND window = FindWindow(L"#32770", kMessageBoxTitle);
520 if (window) {
521 // Dismiss it.
522 for (;;) {
523 HWND button = FindWindowEx(window, NULL, L"Button", NULL);
524 if (button != NULL) {
525 EXPECT_TRUE(0 == SendMessage(button, WM_LBUTTONDOWN, 0, 0));
526 EXPECT_TRUE(0 == SendMessage(button, WM_LBUTTONUP, 0, 0));
527 break;
528 }
529 }
530 break;
531 }
532 }
533 }
534
535 private:
536 MessageLoop* target_;
537 HANDLE event_;
538 TaskList* order_;
539 bool expect_window_;
540 bool is_reentrant_;
541};
542
[email protected]295039bd2008-08-15 04:32:57543#endif // defined(OS_WIN)
544
initial.commitd7cae122008-07-26 21:49:38545} // namespace
546
547TEST(MessageLoop, RecursiveDenial1) {
548 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
549 TaskList order;
550 MessageLoop::current()->PostTask(FROM_HERE,
551 new RecursiveTask(2, &order, 1, false));
552 MessageLoop::current()->PostTask(FROM_HERE,
553 new RecursiveTask(2, &order, 2, false));
554 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3));
555
556 MessageLoop::current()->Run();
557
558 // FIFO order.
[email protected]b16ef312008-08-19 18:36:23559 ASSERT_EQ(14U, order.size());
initial.commitd7cae122008-07-26 21:49:38560 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
561 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
562 EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true));
563 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false));
564 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
565 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
566 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true));
567 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false));
568 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true));
569 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false));
570 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
571 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
572 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true));
573 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
574}
575
576
577TEST(MessageLoop, RecursiveSupport1) {
578 TaskList order;
579 MessageLoop::current()->PostTask(FROM_HERE,
580 new RecursiveTask(2, &order, 1, true));
581 MessageLoop::current()->PostTask(FROM_HERE,
582 new RecursiveTask(2, &order, 2, true));
583 MessageLoop::current()->PostTask(FROM_HERE,
584 new QuitTask(&order, 3));
585
586 MessageLoop::current()->Run();
587
588 // FIFO order.
[email protected]b16ef312008-08-19 18:36:23589 ASSERT_EQ(14U, order.size());
initial.commitd7cae122008-07-26 21:49:38590 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
591 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
592 EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true));
593 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false));
594 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
595 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
596 EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true));
597 EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false));
598 EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true));
599 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false));
600 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
601 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
602 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true));
603 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
604}
605
[email protected]295039bd2008-08-15 04:32:57606#if defined(OS_WIN)
607// TODO(darin): These tests need to be ported since they test critical
608// message loop functionality.
609
initial.commitd7cae122008-07-26 21:49:38610// A side effect of this test is the generation a beep. Sorry.
611TEST(MessageLoop, RecursiveDenial2) {
612 Thread worker("RecursiveDenial2_worker");
613 ASSERT_EQ(true, worker.Start());
614 TaskList order;
615 ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
616 worker.message_loop()->PostTask(FROM_HERE,
617 new Recursive2Tasks(MessageLoop::current(),
618 event,
619 true,
620 &order,
621 false));
622 // Let the other thread execute.
623 WaitForSingleObject(event, INFINITE);
624 MessageLoop::current()->Run();
625
626 ASSERT_EQ(order.size(), 17);
627 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
628 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
629 EXPECT_EQ(order[ 2], TaskItem(MESSAGEBOX, 2, true));
630 EXPECT_EQ(order[ 3], TaskItem(MESSAGEBOX, 2, false));
631 EXPECT_EQ(order[ 4], TaskItem(RECURSIVE, 3, true));
632 EXPECT_EQ(order[ 5], TaskItem(RECURSIVE, 3, false));
633 // When EndDialogTask is processed, the window is already dismissed, hence no
634 // "end" entry.
635 EXPECT_EQ(order[ 6], TaskItem(ENDDIALOG, 4, true));
636 EXPECT_EQ(order[ 7], TaskItem(QUITMESSAGELOOP, 5, true));
637 EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, false));
638 EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 1, true));
639 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, false));
640 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 3, true));
641 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 3, false));
642 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 1, true));
643 EXPECT_EQ(order[14], TaskItem(RECURSIVE, 1, false));
644 EXPECT_EQ(order[15], TaskItem(RECURSIVE, 3, true));
645 EXPECT_EQ(order[16], TaskItem(RECURSIVE, 3, false));
646}
647
648// A side effect of this test is the generation a beep. Sorry.
649TEST(MessageLoop, RecursiveSupport2) {
650 Thread worker("RecursiveSupport2_worker");
651 ASSERT_EQ(true, worker.Start());
652 TaskList order;
653 ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
654 worker.message_loop()->PostTask(FROM_HERE,
655 new Recursive2Tasks(MessageLoop::current(),
656 event,
657 false,
658 &order,
659 true));
660 // Let the other thread execute.
661 WaitForSingleObject(event, INFINITE);
662 MessageLoop::current()->Run();
663
664 ASSERT_EQ(order.size(), 18);
665 EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
666 EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
667 EXPECT_EQ(order[ 2], TaskItem(MESSAGEBOX, 2, true));
668 // Note that this executes in the MessageBox modal loop.
669 EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 3, true));
670 EXPECT_EQ(order[ 4], TaskItem(RECURSIVE, 3, false));
671 EXPECT_EQ(order[ 5], TaskItem(ENDDIALOG, 4, true));
672 EXPECT_EQ(order[ 6], TaskItem(ENDDIALOG, 4, false));
673 EXPECT_EQ(order[ 7], TaskItem(MESSAGEBOX, 2, false));
674 /* The order can subtly change here. The reason is that when RecursiveTask(1)
675 is called in the main thread, if it is faster than getting to the
676 PostTask(FROM_HERE, QuitTask) execution, the order of task execution can
677 change. We don't care anyway that the order isn't correct.
678 EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, true));
679 EXPECT_EQ(order[ 9], TaskItem(QUITMESSAGELOOP, 5, false));
680 EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
681 EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
682 */
683 EXPECT_EQ(order[12], TaskItem(RECURSIVE, 3, true));
684 EXPECT_EQ(order[13], TaskItem(RECURSIVE, 3, false));
685 EXPECT_EQ(order[14], TaskItem(RECURSIVE, 1, true));
686 EXPECT_EQ(order[15], TaskItem(RECURSIVE, 1, false));
687 EXPECT_EQ(order[16], TaskItem(RECURSIVE, 3, true));
688 EXPECT_EQ(order[17], TaskItem(RECURSIVE, 3, false));
689}
690
[email protected]295039bd2008-08-15 04:32:57691#endif // defined(OS_WIN)
692
initial.commitd7cae122008-07-26 21:49:38693class TaskThatPumps : public OrderedTasks {
694 public:
695 TaskThatPumps(TaskList* order, int cookie)
696 : OrderedTasks(order, PUMPS, cookie) {
697 }
698
699 virtual void Run() {
700 RunStart();
701 bool old_state = MessageLoop::current()->NestableTasksAllowed();
initial.commitd7cae122008-07-26 21:49:38702 MessageLoop::current()->SetNestableTasksAllowed(true);
[email protected]295039bd2008-08-15 04:32:57703 MessageLoop::current()->RunAllPending();
initial.commitd7cae122008-07-26 21:49:38704 MessageLoop::current()->SetNestableTasksAllowed(old_state);
705 RunEnd();
706 }
707
708 private:
709};
710
711
712// Tests that non nestable tasks run in FIFO if there are no nested loops.
713TEST(MessageLoop, NonNestableWithNoNesting) {
714 TaskList order;
715
716 Task* task = new OrderedTasks(&order, 1);
717 task->set_nestable(false);
718 MessageLoop::current()->PostTask(FROM_HERE, task);
719 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 2));
720 MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3));
721 MessageLoop::current()->Run();
722
723 // FIFO order.
[email protected]b16ef312008-08-19 18:36:23724 ASSERT_EQ(6U, order.size());
initial.commitd7cae122008-07-26 21:49:38725 EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true));
726 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false));
727 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true));
728 EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false));
729 EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
730 EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
731}
732
733// Tests that non nestable tasks don't run when there's code in the call stack.
734TEST(MessageLoop, NonNestableInNestedLoop) {
735 TaskList order;
736
737 MessageLoop::current()->PostTask(FROM_HERE,
738 new TaskThatPumps(&order, 1));
739 Task* task = new OrderedTasks(&order, 2);
740 task->set_nestable(false);
741 MessageLoop::current()->PostTask(FROM_HERE, task);
742 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 3));
[email protected]5f800ed2008-08-14 01:09:18743 MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 4));
initial.commitd7cae122008-07-26 21:49:38744 Task* non_nestable_quit = new QuitTask(&order, 5);
745 non_nestable_quit->set_nestable(false);
746 MessageLoop::current()->PostTask(FROM_HERE, non_nestable_quit);
747
748
749 MessageLoop::current()->Run();
750
751 // FIFO order.
[email protected]b16ef312008-08-19 18:36:23752 ASSERT_EQ(10U, order.size());
initial.commitd7cae122008-07-26 21:49:38753 EXPECT_EQ(order[ 0], TaskItem(PUMPS, 1, true));
754 EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 3, true));
755 EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 3, false));
[email protected]5f800ed2008-08-14 01:09:18756 EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 4, true));
757 EXPECT_EQ(order[ 4], TaskItem(ORDERERD, 4, false));
initial.commitd7cae122008-07-26 21:49:38758 EXPECT_EQ(order[ 5], TaskItem(PUMPS, 1, false));
759 EXPECT_EQ(order[ 6], TaskItem(ORDERERD, 2, true));
760 EXPECT_EQ(order[ 7], TaskItem(ORDERERD, 2, false));
761 EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, true));
762 EXPECT_EQ(order[ 9], TaskItem(QUITMESSAGELOOP, 5, false));
763}
764
[email protected]295039bd2008-08-15 04:32:57765#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38766
767namespace {
768
769class AutoresetWatcher : public MessageLoop::Watcher {
770 public:
771 AutoresetWatcher(HANDLE signal, MessageLoop* message_loop)
[email protected]295039bd2008-08-15 04:32:57772 : signal_(signal),
773 message_loop_(message_loop) {
774 }
initial.commitd7cae122008-07-26 21:49:38775 virtual void OnObjectSignaled(HANDLE object);
776 private:
777 HANDLE signal_;
778 MessageLoop* message_loop_;
779};
780
781void AutoresetWatcher::OnObjectSignaled(HANDLE object) {
782 message_loop_->WatchObject(object, NULL);
783 ASSERT_TRUE(SetEvent(signal_));
784}
785
786class AutoresetTask : public Task {
787 public:
788 AutoresetTask(HANDLE object, MessageLoop::Watcher* watcher)
789 : object_(object), watcher_(watcher) {}
790 virtual void Run() {
791 MessageLoop::current()->WatchObject(object_, watcher_);
792 }
793
794 private:
795 HANDLE object_;
796 MessageLoop::Watcher* watcher_;
797};
798
799} // namespace
800
801TEST(MessageLoop, AutoresetEvents) {
802 SECURITY_ATTRIBUTES attributes;
803 attributes.nLength = sizeof(attributes);
804 attributes.bInheritHandle = false;
805 attributes.lpSecurityDescriptor = NULL;
806
807 // Init an autoreset and a manual reset events.
808 HANDLE autoreset = CreateEvent(&attributes, FALSE, FALSE, NULL);
809 HANDLE callback_called = CreateEvent(&attributes, TRUE, FALSE, NULL);
810 ASSERT_TRUE(NULL != autoreset);
811 ASSERT_TRUE(NULL != callback_called);
812
813 Thread thread("Autoreset test");
814 ASSERT_TRUE(thread.Start());
815
816 MessageLoop* message_loop = thread.message_loop();
817 ASSERT_TRUE(NULL != message_loop);
818
819 AutoresetWatcher watcher(callback_called, message_loop);
820 AutoresetTask* task = new AutoresetTask(autoreset, &watcher);
821 message_loop->PostTask(FROM_HERE, task);
822 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
823
824 ASSERT_TRUE(SetEvent(autoreset));
825
826 DWORD result = WaitForSingleObject(callback_called, 1000);
827 EXPECT_EQ(WAIT_OBJECT_0, result);
828
829 thread.Stop();
830}
831
832namespace {
833
834class DispatcherImpl : public MessageLoop::Dispatcher {
835 public:
836 DispatcherImpl() : dispatch_count_(0) {}
837
838 virtual bool Dispatch(const MSG& msg) {
839 ::TranslateMessage(&msg);
840 ::DispatchMessage(&msg);
841 return (++dispatch_count_ != 2);
842 }
843
844 int dispatch_count_;
845};
846
847} // namespace
848
849TEST(MessageLoop, Dispatcher) {
850 class MyTask : public Task {
851 public:
852 virtual void Run() {
853 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
854 PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
855 }
856 };
857 Task* task = new MyTask();
858 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100);
859 DispatcherImpl dispatcher;
860 MessageLoop::current()->Run(&dispatcher);
861 ASSERT_EQ(2, dispatcher.dispatch_count_);
862}
[email protected]295039bd2008-08-15 04:32:57863
864#endif
license.botbf09a502008-08-24 00:55:55865