Index: base/message_loop_unittest.cc |
=================================================================== |
--- base/message_loop_unittest.cc (revision 35459) |
+++ base/message_loop_unittest.cc (working copy) |
@@ -1107,7 +1107,12 @@ |
virtual bool Dispatch(const MSG& msg) { |
::TranslateMessage(&msg); |
::DispatchMessage(&msg); |
- return (++dispatch_count_ != 2); |
+ // Do not count WM_TIMER since it is not what we post and it will cause |
+ // flakiness. |
+ if (msg.message != WM_TIMER) |
+ ++dispatch_count_; |
+ // We treat WM_LBUTTONUP as the last message. |
+ return msg.message != WM_LBUTTONUP; |
} |
int dispatch_count_; |
@@ -1130,6 +1135,37 @@ |
ASSERT_EQ(2, dispatcher.dispatch_count_); |
} |
+LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { |
+ if (code == base::MessagePumpForUI::kMessageFilterCode) { |
+ MSG* msg = reinterpret_cast<MSG*>(lparam); |
+ if (msg->message == WM_LBUTTONDOWN) |
+ return TRUE; |
+ } |
+ return FALSE; |
+} |
+ |
+void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) { |
+ MessageLoop loop(message_loop_type); |
+ |
+ class MyTask : public Task { |
+ public: |
+ virtual void Run() { |
+ PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); |
+ PostMessage(NULL, WM_LBUTTONUP, 'A', 0); |
+ } |
+ }; |
+ Task* task = new MyTask(); |
+ MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100); |
+ HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER, |
+ MsgFilterProc, |
+ NULL, |
+ GetCurrentThreadId()); |
+ DispatcherImpl dispatcher; |
+ MessageLoopForUI::current()->Run(&dispatcher); |
+ ASSERT_EQ(1, dispatcher.dispatch_count_); |
+ UnhookWindowsHookEx(msg_hook); |
+} |
+ |
class TestIOHandler : public MessageLoopForIO::IOHandler { |
public: |
TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); |
@@ -1423,6 +1459,11 @@ |
RunTest_Dispatcher(MessageLoop::TYPE_UI); |
} |
+TEST(MessageLoopTest, DispatcherWithMessageHook) { |
+ // This test requires a UI loop |
+ RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI); |
+} |
+ |
TEST(MessageLoopTest, IOHandler) { |
RunTest_IOHandler(); |
} |