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

Side by Side Diff: base/message_loop_unittest.cc

Issue 7529003: Make base::MessageLoops have names at construction time (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add name argument to MessageLoop Created 9 years, 4 months 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/eintr_wrapper.h" 10 #include "base/eintr_wrapper.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 77
78 ~Foo() {} 78 ~Foo() {}
79 79
80 int test_count_; 80 int test_count_;
81 std::string result_; 81 std::string result_;
82 }; 82 };
83 83
84 // TODO(ajwong): Remove this once we've finished getting rid of the PostTask() 84 // TODO(ajwong): Remove this once we've finished getting rid of the PostTask()
85 // compatibility methods. 85 // compatibility methods.
86 void RunTest_PostLegacyTask(MessageLoop::Type message_loop_type) { 86 void RunTest_PostLegacyTask(MessageLoop::Type message_loop_type) {
87 MessageLoop loop(message_loop_type); 87 MessageLoop loop("MessageLoopTest", message_loop_type);
88 88
89 // Add tests to message loop 89 // Add tests to message loop
90 scoped_refptr<Foo> foo(new Foo()); 90 scoped_refptr<Foo> foo(new Foo());
91 std::string a("a"), b("b"), c("c"), d("d"); 91 std::string a("a"), b("b"), c("c"), d("d");
92 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( 92 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
93 foo.get(), &Foo::Test0)); 93 foo.get(), &Foo::Test0));
94 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( 94 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
95 foo.get(), &Foo::Test1ConstRef, a)); 95 foo.get(), &Foo::Test1ConstRef, a));
96 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( 96 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
97 foo.get(), &Foo::Test1Ptr, &b)); 97 foo.get(), &Foo::Test1Ptr, &b));
98 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( 98 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
99 foo.get(), &Foo::Test1Int, 100)); 99 foo.get(), &Foo::Test1Int, 100));
100 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( 100 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
101 foo.get(), &Foo::Test2Ptr, &a, &c)); 101 foo.get(), &Foo::Test2Ptr, &a, &c));
102 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( 102 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
103 foo.get(), &Foo::Test2Mixed, a, &d)); 103 foo.get(), &Foo::Test2Mixed, a, &d));
104 104
105 // After all tests, post a message that will shut down the message loop 105 // After all tests, post a message that will shut down the message loop
106 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 106 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
107 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); 107 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
108 108
109 // Now kick things off 109 // Now kick things off
110 MessageLoop::current()->Run(); 110 MessageLoop::current()->Run();
111 111
112 EXPECT_EQ(foo->test_count(), 105); 112 EXPECT_EQ(foo->test_count(), 105);
113 EXPECT_EQ(foo->result(), "abacad"); 113 EXPECT_EQ(foo->result(), "abacad");
114 } 114 }
115 115
116 void RunTest_PostTask(MessageLoop::Type message_loop_type) { 116 void RunTest_PostTask(MessageLoop::Type message_loop_type) {
117 MessageLoop loop(message_loop_type); 117 MessageLoop loop("MessageLoopTest", message_loop_type);
118 118
119 // Add tests to message loop 119 // Add tests to message loop
120 scoped_refptr<Foo> foo(new Foo()); 120 scoped_refptr<Foo> foo(new Foo());
121 std::string a("a"), b("b"), c("c"), d("d"); 121 std::string a("a"), b("b"), c("c"), d("d");
122 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 122 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
123 &Foo::Test0, foo.get())); 123 &Foo::Test0, foo.get()));
124 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 124 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
125 &Foo::Test1ConstRef, foo.get(), a)); 125 &Foo::Test1ConstRef, foo.get(), a));
126 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 126 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
127 &Foo::Test1Ptr, foo.get(), &b)); 127 &Foo::Test1Ptr, foo.get(), &b));
128 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 128 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
129 &Foo::Test1Int, foo.get(), 100)); 129 &Foo::Test1Int, foo.get(), 100));
130 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 130 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
131 &Foo::Test2Ptr, foo.get(), &a, &c)); 131 &Foo::Test2Ptr, foo.get(), &a, &c));
132 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 132 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
133 &Foo::Test2Mixed, foo.get(), a, &d)); 133 &Foo::Test2Mixed, foo.get(), a, &d));
134 134
135 // After all tests, post a message that will shut down the message loop 135 // After all tests, post a message that will shut down the message loop
136 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 136 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
137 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); 137 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
138 138
139 // Now kick things off 139 // Now kick things off
140 MessageLoop::current()->Run(); 140 MessageLoop::current()->Run();
141 141
142 EXPECT_EQ(foo->test_count(), 105); 142 EXPECT_EQ(foo->test_count(), 105);
143 EXPECT_EQ(foo->result(), "abacad"); 143 EXPECT_EQ(foo->result(), "abacad");
144 } 144 }
145 145
146 void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) { 146 void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) {
147 MessageLoop loop(message_loop_type); 147 MessageLoop loop("MessageLoopTest", message_loop_type);
148 148
149 // Add tests to message loop 149 // Add tests to message loop
150 scoped_refptr<Foo> foo(new Foo()); 150 scoped_refptr<Foo> foo(new Foo());
151 std::string a("a"), b("b"), c("c"), d("d"); 151 std::string a("a"), b("b"), c("c"), d("d");
152 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 152 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
153 &Foo::Test0, foo.get())); 153 &Foo::Test0, foo.get()));
154 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 154 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
155 &Foo::Test1ConstRef, foo.get(), a)); 155 &Foo::Test1ConstRef, foo.get(), a));
156 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 156 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
157 &Foo::Test1Ptr, foo.get(), &b)); 157 &Foo::Test1Ptr, foo.get(), &b));
(...skipping 29 matching lines...) Expand all
187 static void RecordRunTimeFunc(Time* run_time, int* quit_counter) { 187 static void RecordRunTimeFunc(Time* run_time, int* quit_counter) {
188 *run_time = Time::Now(); 188 *run_time = Time::Now();
189 189
190 // Cause our Run function to take some time to execute. As a result we can 190 // Cause our Run function to take some time to execute. As a result we can
191 // count on subsequent RecordRunTimeFunc()s running at a future time, 191 // count on subsequent RecordRunTimeFunc()s running at a future time,
192 // without worry about the resolution of our system clock being an issue. 192 // without worry about the resolution of our system clock being an issue.
193 SlowFunc(10, quit_counter); 193 SlowFunc(10, quit_counter);
194 } 194 }
195 195
196 void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) { 196 void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) {
197 MessageLoop loop(message_loop_type); 197 MessageLoop loop("MessageLoopTest", message_loop_type);
198 198
199 // Test that PostDelayedTask results in a delayed task. 199 // Test that PostDelayedTask results in a delayed task.
200 200
201 const int kDelayMS = 100; 201 const int kDelayMS = 100;
202 202
203 int num_tasks = 1; 203 int num_tasks = 1;
204 Time run_time; 204 Time run_time;
205 205
206 loop.PostDelayedTask( 206 loop.PostDelayedTask(
207 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), 207 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
208 kDelayMS); 208 kDelayMS);
209 209
210 Time time_before_run = Time::Now(); 210 Time time_before_run = Time::Now();
211 loop.Run(); 211 loop.Run();
212 Time time_after_run = Time::Now(); 212 Time time_after_run = Time::Now();
213 213
214 EXPECT_EQ(0, num_tasks); 214 EXPECT_EQ(0, num_tasks);
215 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds()); 215 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds());
216 } 216 }
217 217
218 void RunTest_PostDelayedTask_InDelayOrder( 218 void RunTest_PostDelayedTask_InDelayOrder(
219 MessageLoop::Type message_loop_type) { 219 MessageLoop::Type message_loop_type) {
220 MessageLoop loop(message_loop_type); 220 MessageLoop loop("MessageLoopTest", message_loop_type);
221 221
222 // Test that two tasks with different delays run in the right order. 222 // Test that two tasks with different delays run in the right order.
223 int num_tasks = 2; 223 int num_tasks = 2;
224 Time run_time1, run_time2; 224 Time run_time1, run_time2;
225 225
226 loop.PostDelayedTask( 226 loop.PostDelayedTask(
227 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), 200); 227 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), 200);
228 // If we get a large pause in execution (due to a context switch) here, this 228 // If we get a large pause in execution (due to a context switch) here, this
229 // test could fail. 229 // test could fail.
230 loop.PostDelayedTask( 230 loop.PostDelayedTask(
231 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10); 231 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10);
232 232
233 loop.Run(); 233 loop.Run();
234 EXPECT_EQ(0, num_tasks); 234 EXPECT_EQ(0, num_tasks);
235 235
236 EXPECT_TRUE(run_time2 < run_time1); 236 EXPECT_TRUE(run_time2 < run_time1);
237 } 237 }
238 238
239 void RunTest_PostDelayedTask_InPostOrder( 239 void RunTest_PostDelayedTask_InPostOrder(
240 MessageLoop::Type message_loop_type) { 240 MessageLoop::Type message_loop_type) {
241 MessageLoop loop(message_loop_type); 241 MessageLoop loop("MessageLoopTest", message_loop_type);
242 242
243 // Test that two tasks with the same delay run in the order in which they 243 // Test that two tasks with the same delay run in the order in which they
244 // were posted. 244 // were posted.
245 // 245 //
246 // NOTE: This is actually an approximate test since the API only takes a 246 // NOTE: This is actually an approximate test since the API only takes a
247 // "delay" parameter, so we are not exactly simulating two tasks that get 247 // "delay" parameter, so we are not exactly simulating two tasks that get
248 // posted at the exact same time. It would be nice if the API allowed us to 248 // posted at the exact same time. It would be nice if the API allowed us to
249 // specify the desired run time. 249 // specify the desired run time.
250 250
251 const int kDelayMS = 100; 251 const int kDelayMS = 100;
252 252
253 int num_tasks = 2; 253 int num_tasks = 2;
254 Time run_time1, run_time2; 254 Time run_time1, run_time2;
255 255
256 loop.PostDelayedTask( 256 loop.PostDelayedTask(
257 FROM_HERE, 257 FROM_HERE,
258 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelayMS); 258 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelayMS);
259 loop.PostDelayedTask( 259 loop.PostDelayedTask(
260 FROM_HERE, 260 FROM_HERE,
261 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelayMS); 261 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelayMS);
262 262
263 loop.Run(); 263 loop.Run();
264 EXPECT_EQ(0, num_tasks); 264 EXPECT_EQ(0, num_tasks);
265 265
266 EXPECT_TRUE(run_time1 < run_time2); 266 EXPECT_TRUE(run_time1 < run_time2);
267 } 267 }
268 268
269 void RunTest_PostDelayedTask_InPostOrder_2( 269 void RunTest_PostDelayedTask_InPostOrder_2(
270 MessageLoop::Type message_loop_type) { 270 MessageLoop::Type message_loop_type) {
271 MessageLoop loop(message_loop_type); 271 MessageLoop loop("MessageLoopTest", message_loop_type);
272 272
273 // Test that a delayed task still runs after a normal tasks even if the 273 // Test that a delayed task still runs after a normal tasks even if the
274 // normal tasks take a long time to run. 274 // normal tasks take a long time to run.
275 275
276 const int kPauseMS = 50; 276 const int kPauseMS = 50;
277 277
278 int num_tasks = 2; 278 int num_tasks = 2;
279 Time run_time; 279 Time run_time;
280 280
281 loop.PostTask(FROM_HERE, base::Bind(&SlowFunc, kPauseMS, &num_tasks)); 281 loop.PostTask(FROM_HERE, base::Bind(&SlowFunc, kPauseMS, &num_tasks));
282 loop.PostDelayedTask( 282 loop.PostDelayedTask(
283 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), 10); 283 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), 10);
284 284
285 Time time_before_run = Time::Now(); 285 Time time_before_run = Time::Now();
286 loop.Run(); 286 loop.Run();
287 Time time_after_run = Time::Now(); 287 Time time_after_run = Time::Now();
288 288
289 EXPECT_EQ(0, num_tasks); 289 EXPECT_EQ(0, num_tasks);
290 290
291 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds()); 291 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds());
292 } 292 }
293 293
294 void RunTest_PostDelayedTask_InPostOrder_3( 294 void RunTest_PostDelayedTask_InPostOrder_3(
295 MessageLoop::Type message_loop_type) { 295 MessageLoop::Type message_loop_type) {
296 MessageLoop loop(message_loop_type); 296 MessageLoop loop("MessageLoopTest", message_loop_type);
297 297
298 // Test that a delayed task still runs after a pile of normal tasks. The key 298 // Test that a delayed task still runs after a pile of normal tasks. The key
299 // difference between this test and the previous one is that here we return 299 // difference between this test and the previous one is that here we return
300 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities 300 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities
301 // to maybe run the delayed task. It should know not to do so until the 301 // to maybe run the delayed task. It should know not to do so until the
302 // delayed task's delay has passed. 302 // delayed task's delay has passed.
303 303
304 int num_tasks = 11; 304 int num_tasks = 11;
305 Time run_time1, run_time2; 305 Time run_time1, run_time2;
306 306
307 // Clutter the ML with tasks. 307 // Clutter the ML with tasks.
308 for (int i = 1; i < num_tasks; ++i) 308 for (int i = 1; i < num_tasks; ++i)
309 loop.PostTask(FROM_HERE, 309 loop.PostTask(FROM_HERE,
310 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks)); 310 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks));
311 311
312 loop.PostDelayedTask( 312 loop.PostDelayedTask(
313 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 1); 313 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 1);
314 314
315 loop.Run(); 315 loop.Run();
316 EXPECT_EQ(0, num_tasks); 316 EXPECT_EQ(0, num_tasks);
317 317
318 EXPECT_TRUE(run_time2 > run_time1); 318 EXPECT_TRUE(run_time2 > run_time1);
319 } 319 }
320 320
321 void RunTest_PostDelayedTask_SharedTimer( 321 void RunTest_PostDelayedTask_SharedTimer(
322 MessageLoop::Type message_loop_type) { 322 MessageLoop::Type message_loop_type) {
323 MessageLoop loop(message_loop_type); 323 MessageLoop loop("MessageLoopTest", message_loop_type);
324 324
325 // Test that the interval of the timer, used to run the next delayed task, is 325 // Test that the interval of the timer, used to run the next delayed task, is
326 // set to a value corresponding to when the next delayed task should run. 326 // set to a value corresponding to when the next delayed task should run.
327 327
328 // By setting num_tasks to 1, we ensure that the first task to run causes the 328 // By setting num_tasks to 1, we ensure that the first task to run causes the
329 // run loop to exit. 329 // run loop to exit.
330 int num_tasks = 1; 330 int num_tasks = 1;
331 Time run_time1, run_time2; 331 Time run_time1, run_time2;
332 332
333 loop.PostDelayedTask( 333 loop.PostDelayedTask(
(...skipping 28 matching lines...) Expand all
362 MessageLoop::current()->SetNestableTasksAllowed(true); 362 MessageLoop::current()->SetNestableTasksAllowed(true);
363 MSG msg; 363 MSG msg;
364 while (GetMessage(&msg, NULL, 0, 0)) { 364 while (GetMessage(&msg, NULL, 0, 0)) {
365 TranslateMessage(&msg); 365 TranslateMessage(&msg);
366 DispatchMessage(&msg); 366 DispatchMessage(&msg);
367 } 367 }
368 MessageLoop::current()->Quit(); 368 MessageLoop::current()->Quit();
369 } 369 }
370 370
371 void RunTest_PostDelayedTask_SharedTimer_SubPump() { 371 void RunTest_PostDelayedTask_SharedTimer_SubPump() {
372 MessageLoop loop(MessageLoop::TYPE_UI); 372 MessageLoop loop("MessageLoopTest", MessageLoop::TYPE_UI);
373 373
374 // Test that the interval of the timer, used to run the next delayed task, is 374 // Test that the interval of the timer, used to run the next delayed task, is
375 // set to a value corresponding to when the next delayed task should run. 375 // set to a value corresponding to when the next delayed task should run.
376 376
377 // By setting num_tasks to 1, we ensure that the first task to run causes the 377 // By setting num_tasks to 1, we ensure that the first task to run causes the
378 // run loop to exit. 378 // run loop to exit.
379 int num_tasks = 1; 379 int num_tasks = 1;
380 Time run_time; 380 Time run_time;
381 381
382 loop.PostTask(FROM_HERE, base::Bind(&SubPumpFunc)); 382 loop.PostTask(FROM_HERE, base::Bind(&SubPumpFunc));
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 void Run() {} 428 void Run() {}
429 private: 429 private:
430 scoped_refptr<RecordDeletionProbe> post_on_delete_; 430 scoped_refptr<RecordDeletionProbe> post_on_delete_;
431 bool* was_deleted_; 431 bool* was_deleted_;
432 }; 432 };
433 433
434 void RunTest_EnsureDeletion(MessageLoop::Type message_loop_type) { 434 void RunTest_EnsureDeletion(MessageLoop::Type message_loop_type) {
435 bool a_was_deleted = false; 435 bool a_was_deleted = false;
436 bool b_was_deleted = false; 436 bool b_was_deleted = false;
437 { 437 {
438 MessageLoop loop(message_loop_type); 438 MessageLoop loop("MessageLoopTest", message_loop_type);
439 loop.PostTask( 439 loop.PostTask(
440 FROM_HERE, base::Bind(&RecordDeletionProbe::Run, 440 FROM_HERE, base::Bind(&RecordDeletionProbe::Run,
441 new RecordDeletionProbe(NULL, &a_was_deleted))); 441 new RecordDeletionProbe(NULL, &a_was_deleted)));
442 loop.PostDelayedTask( 442 loop.PostDelayedTask(
443 FROM_HERE, base::Bind(&RecordDeletionProbe::Run, 443 FROM_HERE, base::Bind(&RecordDeletionProbe::Run,
444 new RecordDeletionProbe(NULL, &b_was_deleted)), 444 new RecordDeletionProbe(NULL, &b_was_deleted)),
445 1000); // TODO(ajwong): Do we really need 1000ms here? 445 1000); // TODO(ajwong): Do we really need 1000ms here?
446 } 446 }
447 EXPECT_TRUE(a_was_deleted); 447 EXPECT_TRUE(a_was_deleted);
448 EXPECT_TRUE(b_was_deleted); 448 EXPECT_TRUE(b_was_deleted);
449 } 449 }
450 450
451 void RunTest_EnsureDeletion_Chain(MessageLoop::Type message_loop_type) { 451 void RunTest_EnsureDeletion_Chain(MessageLoop::Type message_loop_type) {
452 bool a_was_deleted = false; 452 bool a_was_deleted = false;
453 bool b_was_deleted = false; 453 bool b_was_deleted = false;
454 bool c_was_deleted = false; 454 bool c_was_deleted = false;
455 { 455 {
456 MessageLoop loop(message_loop_type); 456 MessageLoop loop("MessageLoopTest", message_loop_type);
457 // The scoped_refptr for each of the below is held either by the chained 457 // The scoped_refptr for each of the below is held either by the chained
458 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback. 458 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback.
459 RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted); 459 RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted);
460 RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted); 460 RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted);
461 RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted); 461 RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted);
462 loop.PostTask(FROM_HERE, base::Bind(&RecordDeletionProbe::Run, c)); 462 loop.PostTask(FROM_HERE, base::Bind(&RecordDeletionProbe::Run, c));
463 } 463 }
464 EXPECT_TRUE(a_was_deleted); 464 EXPECT_TRUE(a_was_deleted);
465 EXPECT_TRUE(b_was_deleted); 465 EXPECT_TRUE(b_was_deleted);
466 EXPECT_TRUE(c_was_deleted); 466 EXPECT_TRUE(c_was_deleted);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 #elif defined(_M_X64) 550 #elif defined(_M_X64)
551 551
552 ex_info->ContextRecord->Rip -= 5; 552 ex_info->ContextRecord->Rip -= 5;
553 553
554 #endif 554 #endif
555 555
556 return EXCEPTION_CONTINUE_EXECUTION; 556 return EXCEPTION_CONTINUE_EXECUTION;
557 } 557 }
558 558
559 void RunTest_Crasher(MessageLoop::Type message_loop_type) { 559 void RunTest_Crasher(MessageLoop::Type message_loop_type) {
560 MessageLoop loop(message_loop_type); 560 MessageLoop loop("MessageLoopTest", message_loop_type);
561 561
562 if (::IsDebuggerPresent()) 562 if (::IsDebuggerPresent())
563 return; 563 return;
564 564
565 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter = 565 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
566 ::SetUnhandledExceptionFilter(&HandleCrasherException); 566 ::SetUnhandledExceptionFilter(&HandleCrasherException);
567 567
568 MessageLoop::current()->PostTask( 568 MessageLoop::current()->PostTask(
569 FROM_HERE, 569 FROM_HERE,
570 base::Bind(&Crasher::Run, new Crasher(false))); 570 base::Bind(&Crasher::Run, new Crasher(false)));
571 MessageLoop::current()->set_exception_restoration(true); 571 MessageLoop::current()->set_exception_restoration(true);
572 MessageLoop::current()->Run(); 572 MessageLoop::current()->Run();
573 MessageLoop::current()->set_exception_restoration(false); 573 MessageLoop::current()->set_exception_restoration(false);
574 574
575 ::SetUnhandledExceptionFilter(old_SEH_filter); 575 ::SetUnhandledExceptionFilter(old_SEH_filter);
576 } 576 }
577 577
578 void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) { 578 void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) {
579 MessageLoop loop(message_loop_type); 579 MessageLoop loop("MessageLoopTest", message_loop_type);
580 580
581 if (::IsDebuggerPresent()) 581 if (::IsDebuggerPresent())
582 return; 582 return;
583 583
584 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter = 584 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
585 ::SetUnhandledExceptionFilter(&HandleCrasherException); 585 ::SetUnhandledExceptionFilter(&HandleCrasherException);
586 586
587 MessageLoop::current()->PostTask( 587 MessageLoop::current()->PostTask(
588 FROM_HERE, 588 FROM_HERE,
589 base::Bind(&Crasher::Run, new Crasher(true))); 589 base::Bind(&Crasher::Run, new Crasher(true)));
590 MessageLoop::current()->set_exception_restoration(true); 590 MessageLoop::current()->set_exception_restoration(true);
591 MessageLoop::current()->Run(); 591 MessageLoop::current()->Run();
592 MessageLoop::current()->set_exception_restoration(false); 592 MessageLoop::current()->set_exception_restoration(false);
593 593
594 ::SetUnhandledExceptionFilter(old_SEH_filter); 594 ::SetUnhandledExceptionFilter(old_SEH_filter);
595 } 595 }
596 596
597 #endif // defined(OS_WIN) 597 #endif // defined(OS_WIN)
598 598
599 void RunTest_Nesting(MessageLoop::Type message_loop_type) { 599 void RunTest_Nesting(MessageLoop::Type message_loop_type) {
600 MessageLoop loop(message_loop_type); 600 MessageLoop loop("MessageLoopTest", message_loop_type);
601 601
602 int depth = 100; 602 int depth = 100;
603 MessageLoop::current()->PostTask(FROM_HERE, 603 MessageLoop::current()->PostTask(FROM_HERE,
604 base::Bind(&NestingFunc, &depth)); 604 base::Bind(&NestingFunc, &depth));
605 MessageLoop::current()->Run(); 605 MessageLoop::current()->Run();
606 EXPECT_EQ(depth, 0); 606 EXPECT_EQ(depth, 0);
607 } 607 }
608 608
609 const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test"; 609 const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test";
610 610
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 } 793 }
794 } 794 }
795 break; 795 break;
796 } 796 }
797 } 797 }
798 } 798 }
799 799
800 #endif // defined(OS_WIN) 800 #endif // defined(OS_WIN)
801 801
802 void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) { 802 void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) {
803 MessageLoop loop(message_loop_type); 803 MessageLoop loop("MessageLoopTest", message_loop_type);
804 804
805 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); 805 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
806 TaskList order; 806 TaskList order;
807 MessageLoop::current()->PostTask( 807 MessageLoop::current()->PostTask(
808 FROM_HERE, 808 FROM_HERE,
809 base::Bind(&RecursiveFunc, &order, 1, 2, false)); 809 base::Bind(&RecursiveFunc, &order, 1, 2, false));
810 MessageLoop::current()->PostTask( 810 MessageLoop::current()->PostTask(
811 FROM_HERE, 811 FROM_HERE,
812 base::Bind(&RecursiveFunc, &order, 2, 2, false)); 812 base::Bind(&RecursiveFunc, &order, 2, 2, false));
813 MessageLoop::current()->PostTask( 813 MessageLoop::current()->PostTask(
(...skipping 14 matching lines...) Expand all
828 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false)); 828 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false));
829 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); 829 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
830 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); 830 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
831 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true)); 831 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
832 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false)); 832 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
833 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true)); 833 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
834 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false)); 834 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
835 } 835 }
836 836
837 void RunTest_RecursiveDenial3(MessageLoop::Type message_loop_type) { 837 void RunTest_RecursiveDenial3(MessageLoop::Type message_loop_type) {
838 MessageLoop loop(message_loop_type); 838 MessageLoop loop("MessageLoopTest", message_loop_type);
839 839
840 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); 840 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
841 TaskList order; 841 TaskList order;
842 MessageLoop::current()->PostTask( 842 MessageLoop::current()->PostTask(
843 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 1, 2, false)); 843 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 1, 2, false));
844 MessageLoop::current()->PostTask( 844 MessageLoop::current()->PostTask(
845 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 2, 2, false)); 845 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 2, 2, false));
846 MessageLoop::current()->PostDelayedTask( 846 MessageLoop::current()->PostDelayedTask(
847 FROM_HERE, base::Bind(&OrderedFunc, &order, 3), 5); 847 FROM_HERE, base::Bind(&OrderedFunc, &order, 3), 5);
848 MessageLoop::current()->PostDelayedTask( 848 MessageLoop::current()->PostDelayedTask(
(...skipping 15 matching lines...) Expand all
864 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); 864 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
865 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true)); 865 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true));
866 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false)); 866 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false));
867 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true)); 867 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true));
868 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false)); 868 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false));
869 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true)); 869 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true));
870 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false)); 870 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false));
871 } 871 }
872 872
873 void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) { 873 void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) {
874 MessageLoop loop(message_loop_type); 874 MessageLoop loop("MessageLoopTest", message_loop_type);
875 875
876 TaskList order; 876 TaskList order;
877 MessageLoop::current()->PostTask( 877 MessageLoop::current()->PostTask(
878 FROM_HERE, base::Bind(&RecursiveFunc, &order, 1, 2, true)); 878 FROM_HERE, base::Bind(&RecursiveFunc, &order, 1, 2, true));
879 MessageLoop::current()->PostTask( 879 MessageLoop::current()->PostTask(
880 FROM_HERE, base::Bind(&RecursiveFunc, &order, 2, 2, true)); 880 FROM_HERE, base::Bind(&RecursiveFunc, &order, 2, 2, true));
881 MessageLoop::current()->PostTask( 881 MessageLoop::current()->PostTask(
882 FROM_HERE, base::Bind(&QuitFunc, &order, 3)); 882 FROM_HERE, base::Bind(&QuitFunc, &order, 3));
883 883
884 MessageLoop::current()->Run(); 884 MessageLoop::current()->Run();
(...skipping 15 matching lines...) Expand all
900 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true)); 900 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
901 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false)); 901 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
902 } 902 }
903 903
904 #if defined(OS_WIN) 904 #if defined(OS_WIN)
905 // TODO(darin): These tests need to be ported since they test critical 905 // TODO(darin): These tests need to be ported since they test critical
906 // message loop functionality. 906 // message loop functionality.
907 907
908 // A side effect of this test is the generation a beep. Sorry. 908 // A side effect of this test is the generation a beep. Sorry.
909 void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) { 909 void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) {
910 MessageLoop loop(message_loop_type); 910 MessageLoop loop("MessageLoopTest", message_loop_type);
911 911
912 Thread worker("RecursiveDenial2_worker"); 912 Thread worker("RecursiveDenial2_worker");
913 Thread::Options options; 913 Thread::Options options;
914 options.message_loop_type = message_loop_type; 914 options.message_loop_type = message_loop_type;
915 ASSERT_EQ(true, worker.StartWithOptions(options)); 915 ASSERT_EQ(true, worker.StartWithOptions(options));
916 TaskList order; 916 TaskList order;
917 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); 917 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
918 worker.message_loop()->PostTask(FROM_HERE, 918 worker.message_loop()->PostTask(FROM_HERE,
919 base::Bind(&RecursiveFuncWin, 919 base::Bind(&RecursiveFuncWin,
920 MessageLoop::current(), 920 MessageLoop::current(),
(...skipping 23 matching lines...) Expand all
944 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, false)); 944 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, false));
945 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, true)); 945 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, true));
946 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, false)); 946 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, false));
947 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 3, true)); 947 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 3, true));
948 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, false)); 948 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, false));
949 } 949 }
950 950
951 // A side effect of this test is the generation a beep. Sorry. This test also 951 // A side effect of this test is the generation a beep. Sorry. This test also
952 // needs to process windows messages on the current thread. 952 // needs to process windows messages on the current thread.
953 void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) { 953 void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) {
954 MessageLoop loop(message_loop_type); 954 MessageLoop loop("MessageLoopTest", message_loop_type);
955 955
956 Thread worker("RecursiveSupport2_worker"); 956 Thread worker("RecursiveSupport2_worker");
957 Thread::Options options; 957 Thread::Options options;
958 options.message_loop_type = message_loop_type; 958 options.message_loop_type = message_loop_type;
959 ASSERT_EQ(true, worker.StartWithOptions(options)); 959 ASSERT_EQ(true, worker.StartWithOptions(options));
960 TaskList order; 960 TaskList order;
961 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); 961 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
962 worker.message_loop()->PostTask(FROM_HERE, 962 worker.message_loop()->PostTask(FROM_HERE,
963 base::Bind(&RecursiveFuncWin, 963 base::Bind(&RecursiveFuncWin,
964 MessageLoop::current(), 964 MessageLoop::current(),
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 bool old_state = MessageLoop::current()->NestableTasksAllowed(); 1004 bool old_state = MessageLoop::current()->NestableTasksAllowed();
1005 MessageLoop::current()->SetNestableTasksAllowed(true); 1005 MessageLoop::current()->SetNestableTasksAllowed(true);
1006 MessageLoop::current()->RunAllPending(); 1006 MessageLoop::current()->RunAllPending();
1007 MessageLoop::current()->SetNestableTasksAllowed(old_state); 1007 MessageLoop::current()->SetNestableTasksAllowed(old_state);
1008 order->RecordEnd(PUMPS, cookie); 1008 order->RecordEnd(PUMPS, cookie);
1009 } 1009 }
1010 1010
1011 // Tests that non nestable tasks run in FIFO if there are no nested loops. 1011 // Tests that non nestable tasks run in FIFO if there are no nested loops.
1012 void RunTest_NonNestableWithNoNesting( 1012 void RunTest_NonNestableWithNoNesting(
1013 MessageLoop::Type message_loop_type) { 1013 MessageLoop::Type message_loop_type) {
1014 MessageLoop loop(message_loop_type); 1014 MessageLoop loop("MessageLoopTest", message_loop_type);
1015 1015
1016 TaskList order; 1016 TaskList order;
1017 1017
1018 MessageLoop::current()->PostNonNestableTask( 1018 MessageLoop::current()->PostNonNestableTask(
1019 FROM_HERE, 1019 FROM_HERE,
1020 base::Bind(&OrderedFunc, &order, 1)); 1020 base::Bind(&OrderedFunc, &order, 1));
1021 MessageLoop::current()->PostTask(FROM_HERE, 1021 MessageLoop::current()->PostTask(FROM_HERE,
1022 base::Bind(&OrderedFunc, &order, 2)); 1022 base::Bind(&OrderedFunc, &order, 2));
1023 MessageLoop::current()->PostTask(FROM_HERE, 1023 MessageLoop::current()->PostTask(FROM_HERE,
1024 base::Bind(&QuitFunc, &order, 3)); 1024 base::Bind(&QuitFunc, &order, 3));
1025 MessageLoop::current()->Run(); 1025 MessageLoop::current()->Run();
1026 1026
1027 // FIFO order. 1027 // FIFO order.
1028 ASSERT_EQ(6U, order.Size()); 1028 ASSERT_EQ(6U, order.Size());
1029 EXPECT_EQ(order.Get(0), TaskItem(ORDERERD, 1, true)); 1029 EXPECT_EQ(order.Get(0), TaskItem(ORDERERD, 1, true));
1030 EXPECT_EQ(order.Get(1), TaskItem(ORDERERD, 1, false)); 1030 EXPECT_EQ(order.Get(1), TaskItem(ORDERERD, 1, false));
1031 EXPECT_EQ(order.Get(2), TaskItem(ORDERERD, 2, true)); 1031 EXPECT_EQ(order.Get(2), TaskItem(ORDERERD, 2, true));
1032 EXPECT_EQ(order.Get(3), TaskItem(ORDERERD, 2, false)); 1032 EXPECT_EQ(order.Get(3), TaskItem(ORDERERD, 2, false));
1033 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); 1033 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
1034 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); 1034 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
1035 } 1035 }
1036 1036
1037 // Tests that non nestable tasks don't run when there's code in the call stack. 1037 // Tests that non nestable tasks don't run when there's code in the call stack.
1038 void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type, 1038 void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type,
1039 bool use_delayed) { 1039 bool use_delayed) {
1040 MessageLoop loop(message_loop_type); 1040 MessageLoop loop("MessageLoopTest", message_loop_type);
1041 1041
1042 TaskList order; 1042 TaskList order;
1043 1043
1044 MessageLoop::current()->PostTask( 1044 MessageLoop::current()->PostTask(
1045 FROM_HERE, 1045 FROM_HERE,
1046 base::Bind(&FuncThatPumps, &order, 1)); 1046 base::Bind(&FuncThatPumps, &order, 1));
1047 if (use_delayed) { 1047 if (use_delayed) {
1048 MessageLoop::current()->PostNonNestableDelayedTask( 1048 MessageLoop::current()->PostNonNestableDelayedTask(
1049 FROM_HERE, 1049 FROM_HERE,
1050 base::Bind(&OrderedFunc, &order, 2), 1050 base::Bind(&OrderedFunc, &order, 2),
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 1108
1109 int dispatch_count_; 1109 int dispatch_count_;
1110 }; 1110 };
1111 1111
1112 void MouseDownUp() { 1112 void MouseDownUp() {
1113 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); 1113 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
1114 PostMessage(NULL, WM_LBUTTONUP, 'A', 0); 1114 PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
1115 } 1115 }
1116 1116
1117 void RunTest_Dispatcher(MessageLoop::Type message_loop_type) { 1117 void RunTest_Dispatcher(MessageLoop::Type message_loop_type) {
1118 MessageLoop loop(message_loop_type); 1118 MessageLoop loop("MessageLoopTest", message_loop_type);
1119 1119
1120 MessageLoop::current()->PostDelayedTask(FROM_HERE, 1120 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1121 base::Bind(&MouseDownUp), 100); 1121 base::Bind(&MouseDownUp), 100);
1122 DispatcherImpl dispatcher; 1122 DispatcherImpl dispatcher;
1123 MessageLoopForUI::current()->Run(&dispatcher); 1123 MessageLoopForUI::current()->Run(&dispatcher);
1124 ASSERT_EQ(2, dispatcher.dispatch_count_); 1124 ASSERT_EQ(2, dispatcher.dispatch_count_);
1125 } 1125 }
1126 1126
1127 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { 1127 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) {
1128 if (code == base::MessagePumpForUI::kMessageFilterCode) { 1128 if (code == base::MessagePumpForUI::kMessageFilterCode) {
1129 MSG* msg = reinterpret_cast<MSG*>(lparam); 1129 MSG* msg = reinterpret_cast<MSG*>(lparam);
1130 if (msg->message == WM_LBUTTONDOWN) 1130 if (msg->message == WM_LBUTTONDOWN)
1131 return TRUE; 1131 return TRUE;
1132 } 1132 }
1133 return FALSE; 1133 return FALSE;
1134 } 1134 }
1135 1135
1136 void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) { 1136 void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) {
1137 MessageLoop loop(message_loop_type); 1137 MessageLoop loop("MessageLoopTest", message_loop_type);
1138 1138
1139 MessageLoop::current()->PostDelayedTask(FROM_HERE, 1139 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1140 base::Bind(&MouseDownUp), 100); 1140 base::Bind(&MouseDownUp), 100);
1141 HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER, 1141 HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER,
1142 MsgFilterProc, 1142 MsgFilterProc,
1143 NULL, 1143 NULL,
1144 GetCurrentThreadId()); 1144 GetCurrentThreadId());
1145 DispatcherImpl dispatcher; 1145 DispatcherImpl dispatcher;
1146 MessageLoopForUI::current()->Run(&dispatcher); 1146 MessageLoopForUI::current()->Run(&dispatcher);
1147 ASSERT_EQ(1, dispatcher.dispatch_count_); 1147 ASSERT_EQ(1, dispatcher.dispatch_count_);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1484 int num_tasks_processed_; 1484 int num_tasks_processed_;
1485 const int num_tasks_; 1485 const int num_tasks_;
1486 1486
1487 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver); 1487 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver);
1488 }; 1488 };
1489 1489
1490 TEST(MessageLoopTest, TaskObserver) { 1490 TEST(MessageLoopTest, TaskObserver) {
1491 const int kNumPosts = 6; 1491 const int kNumPosts = 6;
1492 DummyTaskObserver observer(kNumPosts); 1492 DummyTaskObserver observer(kNumPosts);
1493 1493
1494 MessageLoop loop; 1494 MessageLoop loop("MessageLoopTest");
1495 loop.AddTaskObserver(&observer); 1495 loop.AddTaskObserver(&observer);
1496 loop.PostTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, kNumPosts)); 1496 loop.PostTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, kNumPosts));
1497 loop.Run(); 1497 loop.Run();
1498 loop.RemoveTaskObserver(&observer); 1498 loop.RemoveTaskObserver(&observer);
1499 1499
1500 EXPECT_EQ(kNumPosts, observer.num_tasks_started()); 1500 EXPECT_EQ(kNumPosts, observer.num_tasks_started());
1501 EXPECT_EQ(kNumPosts, observer.num_tasks_processed()); 1501 EXPECT_EQ(kNumPosts, observer.num_tasks_processed());
1502 } 1502 }
1503 1503
1504 #if defined(OS_WIN) 1504 #if defined(OS_WIN)
1505 TEST(MessageLoopTest, Dispatcher) { 1505 TEST(MessageLoopTest, Dispatcher) {
1506 // This test requires a UI loop 1506 // This test requires a UI loop
1507 RunTest_Dispatcher(MessageLoop::TYPE_UI); 1507 RunTest_Dispatcher(MessageLoop::TYPE_UI);
1508 } 1508 }
1509 1509
1510 TEST(MessageLoopTest, DispatcherWithMessageHook) { 1510 TEST(MessageLoopTest, DispatcherWithMessageHook) {
1511 // This test requires a UI loop 1511 // This test requires a UI loop
1512 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI); 1512 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI);
1513 } 1513 }
1514 1514
1515 TEST(MessageLoopTest, IOHandler) { 1515 TEST(MessageLoopTest, IOHandler) {
1516 RunTest_IOHandler(); 1516 RunTest_IOHandler();
1517 } 1517 }
1518 1518
1519 TEST(MessageLoopTest, WaitForIO) { 1519 TEST(MessageLoopTest, WaitForIO) {
1520 RunTest_WaitForIO(); 1520 RunTest_WaitForIO();
1521 } 1521 }
1522 1522
1523 TEST(MessageLoopTest, HighResolutionTimer) { 1523 TEST(MessageLoopTest, HighResolutionTimer) {
1524 MessageLoop loop; 1524 MessageLoop loop("MessageLoopTest");
1525 1525
1526 const int kFastTimerMs = 5; 1526 const int kFastTimerMs = 5;
1527 const int kSlowTimerMs = 100; 1527 const int kSlowTimerMs = 100;
1528 1528
1529 EXPECT_FALSE(loop.high_resolution_timers_enabled()); 1529 EXPECT_FALSE(loop.high_resolution_timers_enabled());
1530 1530
1531 // Post a fast task to enable the high resolution timers. 1531 // Post a fast task to enable the high resolution timers.
1532 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1), 1532 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
1533 kFastTimerMs); 1533 kFastTimerMs);
1534 loop.Run(); 1534 loop.Run();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1575 // as we don't need to actually get any notifications. 1575 // as we don't need to actually get any notifications.
1576 // pipe() is just the easiest way to do it. 1576 // pipe() is just the easiest way to do it.
1577 int pipefds[2]; 1577 int pipefds[2];
1578 int err = pipe(pipefds); 1578 int err = pipe(pipefds);
1579 ASSERT_EQ(0, err); 1579 ASSERT_EQ(0, err);
1580 int fd = pipefds[1]; 1580 int fd = pipefds[1];
1581 { 1581 {
1582 // Arrange for controller to live longer than message loop. 1582 // Arrange for controller to live longer than message loop.
1583 MessageLoopForIO::FileDescriptorWatcher controller; 1583 MessageLoopForIO::FileDescriptorWatcher controller;
1584 { 1584 {
1585 MessageLoopForIO message_loop; 1585 MessageLoopForIO message_loop("MessageLoopTest");
1586 1586
1587 QuitDelegate delegate; 1587 QuitDelegate delegate;
1588 message_loop.WatchFileDescriptor(fd, 1588 message_loop.WatchFileDescriptor(fd,
1589 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); 1589 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1590 // and don't run the message loop, just destroy it. 1590 // and don't run the message loop, just destroy it.
1591 } 1591 }
1592 } 1592 }
1593 if (HANDLE_EINTR(close(pipefds[0])) < 0) 1593 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1594 PLOG(ERROR) << "close"; 1594 PLOG(ERROR) << "close";
1595 if (HANDLE_EINTR(close(pipefds[1])) < 0) 1595 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1596 PLOG(ERROR) << "close"; 1596 PLOG(ERROR) << "close";
1597 } 1597 }
1598 1598
1599 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { 1599 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) {
1600 // Verify that it's ok to call StopWatchingFileDescriptor(). 1600 // Verify that it's ok to call StopWatchingFileDescriptor().
1601 // (Errors only showed up in valgrind.) 1601 // (Errors only showed up in valgrind.)
1602 int pipefds[2]; 1602 int pipefds[2];
1603 int err = pipe(pipefds); 1603 int err = pipe(pipefds);
1604 ASSERT_EQ(0, err); 1604 ASSERT_EQ(0, err);
1605 int fd = pipefds[1]; 1605 int fd = pipefds[1];
1606 { 1606 {
1607 // Arrange for message loop to live longer than controller. 1607 // Arrange for message loop to live longer than controller.
1608 MessageLoopForIO message_loop; 1608 MessageLoopForIO message_loop("MessageLoopTest");
1609 { 1609 {
1610 MessageLoopForIO::FileDescriptorWatcher controller; 1610 MessageLoopForIO::FileDescriptorWatcher controller;
1611 1611
1612 QuitDelegate delegate; 1612 QuitDelegate delegate;
1613 message_loop.WatchFileDescriptor(fd, 1613 message_loop.WatchFileDescriptor(fd,
1614 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); 1614 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1615 controller.StopWatchingFileDescriptor(); 1615 controller.StopWatchingFileDescriptor();
1616 } 1616 }
1617 } 1617 }
1618 if (HANDLE_EINTR(close(pipefds[0])) < 0) 1618 if (HANDLE_EINTR(close(pipefds[0])) < 0)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1668 bool* task_destroyed_; 1668 bool* task_destroyed_;
1669 bool* destruction_observer_called_; 1669 bool* destruction_observer_called_;
1670 bool task_destroyed_before_message_loop_; 1670 bool task_destroyed_before_message_loop_;
1671 }; 1671 };
1672 1672
1673 } // namespace 1673 } // namespace
1674 1674
1675 TEST(MessageLoopTest, DestructionObserverTest) { 1675 TEST(MessageLoopTest, DestructionObserverTest) {
1676 // Verify that the destruction observer gets called at the very end (after 1676 // Verify that the destruction observer gets called at the very end (after
1677 // all the pending tasks have been destroyed). 1677 // all the pending tasks have been destroyed).
1678 MessageLoop* loop = new MessageLoop; 1678 MessageLoop* loop = new MessageLoop("MessageLoopTest");
1679 const int kDelayMS = 100; 1679 const int kDelayMS = 100;
1680 1680
1681 bool task_destroyed = false; 1681 bool task_destroyed = false;
1682 bool destruction_observer_called = false; 1682 bool destruction_observer_called = false;
1683 1683
1684 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called); 1684 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called);
1685 loop->AddDestructionObserver(&observer); 1685 loop->AddDestructionObserver(&observer);
1686 loop->PostDelayedTask( 1686 loop->PostDelayedTask(
1687 FROM_HERE, 1687 FROM_HERE,
1688 base::Bind(&DestructionObserverProbe::Run, 1688 base::Bind(&DestructionObserverProbe::Run,
1689 new DestructionObserverProbe(&task_destroyed, 1689 new DestructionObserverProbe(&task_destroyed,
1690 &destruction_observer_called)), 1690 &destruction_observer_called)),
1691 kDelayMS); 1691 kDelayMS);
1692 delete loop; 1692 delete loop;
1693 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); 1693 EXPECT_TRUE(observer.task_destroyed_before_message_loop());
1694 // The task should have been destroyed when we deleted the loop. 1694 // The task should have been destroyed when we deleted the loop.
1695 EXPECT_TRUE(task_destroyed); 1695 EXPECT_TRUE(task_destroyed);
1696 EXPECT_TRUE(destruction_observer_called); 1696 EXPECT_TRUE(destruction_observer_called);
1697 } 1697 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698