OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/platform_thread.h" | 7 #include "base/platform_thread.h" |
8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
9 #include "base/thread.h" | 9 #include "base/thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1100 | 1100 |
1101 #if defined(OS_WIN) | 1101 #if defined(OS_WIN) |
1102 | 1102 |
1103 class DispatcherImpl : public MessageLoopForUI::Dispatcher { | 1103 class DispatcherImpl : public MessageLoopForUI::Dispatcher { |
1104 public: | 1104 public: |
1105 DispatcherImpl() : dispatch_count_(0) {} | 1105 DispatcherImpl() : dispatch_count_(0) {} |
1106 | 1106 |
1107 virtual bool Dispatch(const MSG& msg) { | 1107 virtual bool Dispatch(const MSG& msg) { |
1108 ::TranslateMessage(&msg); | 1108 ::TranslateMessage(&msg); |
1109 ::DispatchMessage(&msg); | 1109 ::DispatchMessage(&msg); |
1110 return (++dispatch_count_ != 2); | 1110 // Do not count WM_TIMER since it is not what we post and it will cause |
| 1111 // flakiness. |
| 1112 if (msg.message != WM_TIMER) |
| 1113 ++dispatch_count_; |
| 1114 // We treat WM_LBUTTONUP as the last message. |
| 1115 return msg.message != WM_LBUTTONUP; |
1111 } | 1116 } |
1112 | 1117 |
1113 int dispatch_count_; | 1118 int dispatch_count_; |
1114 }; | 1119 }; |
1115 | 1120 |
1116 void RunTest_Dispatcher(MessageLoop::Type message_loop_type) { | 1121 void RunTest_Dispatcher(MessageLoop::Type message_loop_type) { |
1117 MessageLoop loop(message_loop_type); | 1122 MessageLoop loop(message_loop_type); |
1118 | 1123 |
1119 class MyTask : public Task { | 1124 class MyTask : public Task { |
1120 public: | 1125 public: |
1121 virtual void Run() { | 1126 virtual void Run() { |
1122 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); | 1127 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); |
1123 PostMessage(NULL, WM_LBUTTONUP, 'A', 0); | 1128 PostMessage(NULL, WM_LBUTTONUP, 'A', 0); |
1124 } | 1129 } |
1125 }; | 1130 }; |
1126 Task* task = new MyTask(); | 1131 Task* task = new MyTask(); |
1127 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100); | 1132 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100); |
1128 DispatcherImpl dispatcher; | 1133 DispatcherImpl dispatcher; |
1129 MessageLoopForUI::current()->Run(&dispatcher); | 1134 MessageLoopForUI::current()->Run(&dispatcher); |
1130 ASSERT_EQ(2, dispatcher.dispatch_count_); | 1135 ASSERT_EQ(2, dispatcher.dispatch_count_); |
1131 } | 1136 } |
1132 | 1137 |
| 1138 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { |
| 1139 if (code == base::MessagePumpForUI::kMessageFilterCode) { |
| 1140 MSG* msg = reinterpret_cast<MSG*>(lparam); |
| 1141 if (msg->message == WM_LBUTTONDOWN) |
| 1142 return TRUE; |
| 1143 } |
| 1144 return FALSE; |
| 1145 } |
| 1146 |
| 1147 void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) { |
| 1148 MessageLoop loop(message_loop_type); |
| 1149 |
| 1150 class MyTask : public Task { |
| 1151 public: |
| 1152 virtual void Run() { |
| 1153 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); |
| 1154 PostMessage(NULL, WM_LBUTTONUP, 'A', 0); |
| 1155 } |
| 1156 }; |
| 1157 Task* task = new MyTask(); |
| 1158 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100); |
| 1159 HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER, |
| 1160 MsgFilterProc, |
| 1161 NULL, |
| 1162 GetCurrentThreadId()); |
| 1163 DispatcherImpl dispatcher; |
| 1164 MessageLoopForUI::current()->Run(&dispatcher); |
| 1165 ASSERT_EQ(1, dispatcher.dispatch_count_); |
| 1166 UnhookWindowsHookEx(msg_hook); |
| 1167 } |
| 1168 |
1133 class TestIOHandler : public MessageLoopForIO::IOHandler { | 1169 class TestIOHandler : public MessageLoopForIO::IOHandler { |
1134 public: | 1170 public: |
1135 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); | 1171 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); |
1136 | 1172 |
1137 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 1173 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
1138 DWORD bytes_transfered, DWORD error); | 1174 DWORD bytes_transfered, DWORD error); |
1139 | 1175 |
1140 void Init(); | 1176 void Init(); |
1141 void WaitForIO(); | 1177 void WaitForIO(); |
1142 OVERLAPPED* context() { return &context_.overlapped; } | 1178 OVERLAPPED* context() { return &context_.overlapped; } |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1416 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); | 1452 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); |
1417 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); | 1453 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); |
1418 } | 1454 } |
1419 | 1455 |
1420 #if defined(OS_WIN) | 1456 #if defined(OS_WIN) |
1421 TEST(MessageLoopTest, Dispatcher) { | 1457 TEST(MessageLoopTest, Dispatcher) { |
1422 // This test requires a UI loop | 1458 // This test requires a UI loop |
1423 RunTest_Dispatcher(MessageLoop::TYPE_UI); | 1459 RunTest_Dispatcher(MessageLoop::TYPE_UI); |
1424 } | 1460 } |
1425 | 1461 |
| 1462 TEST(MessageLoopTest, DispatcherWithMessageHook) { |
| 1463 // This test requires a UI loop |
| 1464 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI); |
| 1465 } |
| 1466 |
1426 TEST(MessageLoopTest, IOHandler) { | 1467 TEST(MessageLoopTest, IOHandler) { |
1427 RunTest_IOHandler(); | 1468 RunTest_IOHandler(); |
1428 } | 1469 } |
1429 | 1470 |
1430 TEST(MessageLoopTest, WaitForIO) { | 1471 TEST(MessageLoopTest, WaitForIO) { |
1431 RunTest_WaitForIO(); | 1472 RunTest_WaitForIO(); |
1432 } | 1473 } |
1433 #endif // defined(OS_WIN) | 1474 #endif // defined(OS_WIN) |
1434 | 1475 |
1435 #if defined(OS_POSIX) | 1476 #if defined(OS_POSIX) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1496 message_loop.WatchFileDescriptor(fd, | 1537 message_loop.WatchFileDescriptor(fd, |
1497 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); | 1538 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
1498 controller.StopWatchingFileDescriptor(); | 1539 controller.StopWatchingFileDescriptor(); |
1499 } | 1540 } |
1500 } | 1541 } |
1501 close(pipefds[0]); | 1542 close(pipefds[0]); |
1502 close(pipefds[1]); | 1543 close(pipefds[1]); |
1503 } | 1544 } |
1504 | 1545 |
1505 #endif // defined(OS_POSIX) | 1546 #endif // defined(OS_POSIX) |
OLD | NEW |