OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/message_loop/message_loop_proxy_impl.h" | 13 #include "base/message_loop/message_loop_proxy_impl.h" |
14 #include "base/message_loop/message_loop_test.h" | 14 #include "base/message_loop/message_loop_test.h" |
15 #include "base/pending_task.h" | 15 #include "base/pending_task.h" |
16 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
19 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
25 #include "base/message_loop/message_pump_win.h" | 25 #include "base/message_loop/message_pump_win.h" |
26 #include "base/process/memory.h" | |
27 #include "base/strings/string16.h" | |
26 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
27 #endif | 29 #endif |
28 | 30 |
29 namespace base { | 31 namespace base { |
30 | 32 |
31 // TODO(darin): Platform-specific MessageLoop tests should be grouped together | 33 // TODO(darin): Platform-specific MessageLoop tests should be grouped together |
32 // to avoid chopping this file up with so many #ifdefs. | 34 // to avoid chopping this file up with so many #ifdefs. |
33 | 35 |
34 namespace { | 36 namespace { |
35 | 37 |
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1073 EXPECT_EQ(foo->result(), "a"); | 1075 EXPECT_EQ(foo->result(), "a"); |
1074 } | 1076 } |
1075 | 1077 |
1076 TEST(MessageLoopTest, IsType) { | 1078 TEST(MessageLoopTest, IsType) { |
1077 MessageLoop loop(MessageLoop::TYPE_UI); | 1079 MessageLoop loop(MessageLoop::TYPE_UI); |
1078 EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI)); | 1080 EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI)); |
1079 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO)); | 1081 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO)); |
1080 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT)); | 1082 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT)); |
1081 } | 1083 } |
1082 | 1084 |
1085 #if defined(OS_WIN) | |
1086 void EmptyFunction() {} | |
1087 | |
1088 void PostMultipleTasks() { | |
1089 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&EmptyFunction)); | |
1090 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&EmptyFunction)); | |
1091 } | |
1092 | |
1093 static const int kSignalMsg = WM_USER + 2; | |
1094 | |
1095 void PostWindowsMessage(HWND message_hwnd) { | |
1096 PostMessage(message_hwnd, kSignalMsg, 0, 2); | |
1097 } | |
1098 | |
1099 void DidRun(bool* did_run) { *did_run = true; } | |
1100 | |
1101 UINT_PTR timer_id = 0; | |
1102 | |
1103 LRESULT CALLBACK TestWndProcThunk(HWND hwnd, UINT message, | |
1104 WPARAM wparam, LPARAM lparam) { | |
1105 if (message == WM_TIMER) { | |
1106 KillTimer(hwnd, timer_id); | |
1107 PostQuitMessage(0); | |
1108 return 0; | |
1109 } | |
cpu_(ooo_6.6-7.5)
2013/11/20 21:31:25
check message
| |
1110 switch (lparam) { | |
1111 case 1: | |
1112 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&PostMultipleTasks)); | |
1113 MessageLoop::current()->PostTask(FROM_HERE, | |
1114 base::Bind(&PostWindowsMessage, hwnd)); | |
1115 return 0; | |
1116 case 2: | |
1117 MessageLoop::current()->SetNestableTasksAllowed(true); | |
1118 // Make sure we have a kMsgHaveWork "tickler" in the windows message queue. | |
1119 // If we don't, and this enters a synchronous windows message loop, we won't | |
1120 // get to run any posted tasks. | |
cpu_(ooo_6.6-7.5)
2013/11/20 21:31:25
comment is stale?
| |
1121 // Run a nested windows-style message loop and verify that our tasks run. | |
1122 bool did_run = false; | |
1123 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DidRun, &did_run)); | |
1124 unsigned timer = SetTimer(hwnd, 1, 10, NULL); | |
1125 MSG msg; | |
1126 while (GetMessage(&msg, 0, 0, 0)) { | |
1127 DispatchMessage(&msg); | |
1128 } | |
1129 EXPECT_TRUE(did_run); | |
jamesr
2013/11/20 21:00:54
this check fails without the message_loop.cc patch
| |
1130 MessageLoop::current()->Quit(); | |
1131 return 0; | |
1132 } | |
1133 return DefWindowProc(hwnd, message, wparam, lparam); | |
1134 } | |
1135 | |
1136 TEST(MessageLoopTest, AlwaysHaveUserMessageWhenNesting) { | |
1137 MessageLoop loop(MessageLoop::TYPE_UI); | |
1138 ATOM atom; | |
1139 string16 class_name = L"MessageLoopTest_HWND"; | |
cpu_(ooo_6.6-7.5)
2013/11/20 21:31:25
unneeded class_name
| |
1140 HINSTANCE instance = GetModuleFromAddress(&TestWndProcThunk); | |
1141 WNDCLASSEX wc = {0}; | |
1142 wc.cbSize = sizeof(wc); | |
1143 wc.lpfnWndProc = TestWndProcThunk; | |
1144 wc.hInstance = instance; | |
1145 wc.lpszClassName = class_name.c_str(); | |
1146 atom = RegisterClassEx(&wc); | |
cpu_(ooo_6.6-7.5)
2013/11/20 21:31:25
ATOM atom =
| |
1147 ASSERT_TRUE(atom); | |
1148 | |
1149 HWND message_hwnd = CreateWindow(MAKEINTATOM(atom), 0, 0, 0, 0, 0, 0, | |
1150 HWND_MESSAGE, 0, instance, 0); | |
1151 int error = GetLastError(); | |
1152 ASSERT_TRUE(message_hwnd) << error; | |
1153 | |
1154 ASSERT_TRUE(PostMessage(message_hwnd, kSignalMsg, 0, 1)); | |
1155 | |
1156 loop.Run(); | |
1157 | |
1158 ASSERT_TRUE(DestroyWindow(message_hwnd)); | |
cpu_(ooo_6.6-7.5)
2013/11/20 21:31:25
UnregisterClass(atom)
| |
1159 } | |
1160 #endif // defined(OS_WIN) | |
1161 | |
1083 } // namespace base | 1162 } // namespace base |
OLD | NEW |