Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: base/message_loop/message_loop_unittest.cc

Issue 65173003: Kick the pump when allowing nestable tasks on a message loop (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: better test Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/message_loop/message_loop.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 EndTest(bool* did_run, HWND hwnd) {
1100 *did_run = true;
1101 PostMessage(hwnd, WM_CLOSE, 0, 0);
1102 }
1103
1104 int kMyMessageFilterCode = 0x5002;
1105
1106 LRESULT CALLBACK TestWndProcThunk(HWND hwnd, UINT message,
1107 WPARAM wparam, LPARAM lparam) {
1108 if (message == WM_CLOSE)
1109 EXPECT_TRUE(DestroyWindow(hwnd));
1110 if (message != kSignalMsg)
1111 return DefWindowProc(hwnd, message, wparam, lparam);
1112
1113 switch (lparam) {
1114 case 1:
1115 // First, we post a task that will post multiple no-op tasks to make sure
1116 // that the pump's incoming task queue does not become empty during the
1117 // test.
1118 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&PostMultipleTasks));
1119 // Next, we post a task that posts a windows message to trigger the second
1120 // stage of the test.
1121 MessageLoop::current()->PostTask(FROM_HERE,
1122 base::Bind(&PostWindowsMessage, hwnd));
1123 break;
1124 case 2:
1125 // Since we're about to enter a modal loop, tell the message loop that we
1126 // intend to nest tasks.
1127 MessageLoop::current()->SetNestableTasksAllowed(true);
1128 bool did_run = false;
1129 MessageLoop::current()->PostTask(FROM_HERE,
1130 base::Bind(&EndTest, &did_run, hwnd));
1131 // Run a nested windows-style message loop and verify that our task runs. If
1132 // it doesn't, then we'll loop here until the test times out.
1133 MSG msg;
1134 while (GetMessage(&msg, 0, 0, 0)) {
1135 if (!CallMsgFilter(&msg, kMyMessageFilterCode))
1136 DispatchMessage(&msg);
1137 // If this message is a WM_CLOSE, explicitly exit the modal loop. Posting
1138 // a WM_QUIT should handle this, but unfortunately MessagePumpWin eats
1139 // WM_QUIT messages even when running inside a modal loop.
1140 if (msg.message == WM_CLOSE)
1141 break;
1142 }
1143 EXPECT_TRUE(did_run);
1144 MessageLoop::current()->Quit();
1145 break;
1146 }
1147 return 0;
1148 }
1149
1150 TEST(MessageLoopTest, AlwaysHaveUserMessageWhenNesting) {
1151 MessageLoop loop(MessageLoop::TYPE_UI);
1152 HINSTANCE instance = GetModuleFromAddress(&TestWndProcThunk);
1153 WNDCLASSEX wc = {0};
1154 wc.cbSize = sizeof(wc);
1155 wc.lpfnWndProc = TestWndProcThunk;
1156 wc.hInstance = instance;
1157 wc.lpszClassName = L"MessageLoopTest_HWND";
1158 ATOM atom = RegisterClassEx(&wc);
1159 ASSERT_TRUE(atom);
1160
1161 HWND message_hwnd = CreateWindow(MAKEINTATOM(atom), 0, 0, 0, 0, 0, 0,
1162 HWND_MESSAGE, 0, instance, 0);
1163 ASSERT_TRUE(message_hwnd) << GetLastError();
1164
1165 ASSERT_TRUE(PostMessage(message_hwnd, kSignalMsg, 0, 1));
1166
1167 loop.Run();
1168
1169 ASSERT_TRUE(UnregisterClass(MAKEINTATOM(atom), instance));
1170 }
1171 #endif // defined(OS_WIN)
1172
1083 } // namespace base 1173 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop/message_loop.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698