blob: 01fb1b3b2fd3b14a6c9ecd40a377fbd1b115a2f4 [file] [log] [blame]
[email protected]ac4c6682012-01-04 00:57:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]b7243c42010-07-23 05:23:135#include <vector>
6
[email protected]b224f792011-04-20 16:02:237#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/compiler_specific.h"
[email protected]9cfb89a2010-06-09 21:20:4110#include "base/eintr_wrapper.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
initial.commitd7cae122008-07-26 21:49:3813#include "base/message_loop.h"
[email protected]8e937c1e2012-06-28 22:57:3014#include "base/run_loop.h"
[email protected]4fc20d12012-05-09 20:16:1415#include "base/thread_task_runner_handle.h"
[email protected]ce072a72010-12-31 20:02:1616#include "base/threading/platform_thread.h"
[email protected]34b99632011-01-01 01:01:0617#include "base/threading/thread.h"
initial.commitd7cae122008-07-26 21:49:3818#include "testing/gtest/include/gtest/gtest.h"
19
[email protected]295039bd2008-08-15 04:32:5720#if defined(OS_WIN)
21#include "base/message_pump_win.h"
[email protected]b90d7e802011-01-09 16:32:2022#include "base/win/scoped_handle.h"
[email protected]295039bd2008-08-15 04:32:5723#endif
24
[email protected]ce072a72010-12-31 20:02:1625using base::PlatformThread;
[email protected]4d9bdfaf2008-08-26 05:53:5726using base::Thread;
[email protected]e1acf6f2008-10-27 20:43:3327using base::Time;
28using base::TimeDelta;
[email protected]b224f792011-04-20 16:02:2329using base::TimeTicks;
[email protected]4d9bdfaf2008-08-26 05:53:5730
31// TODO(darin): Platform-specific MessageLoop tests should be grouped together
32// to avoid chopping this file up with so many #ifdefs.
[email protected]b16ef312008-08-19 18:36:2333
initial.commitd7cae122008-07-26 21:49:3834namespace {
35
[email protected]4d9bdfaf2008-08-26 05:53:5736class MessageLoopTest : public testing::Test {};
initial.commitd7cae122008-07-26 21:49:3837
38class Foo : public base::RefCounted<Foo> {
39 public:
40 Foo() : test_count_(0) {
41 }
42
43 void Test0() {
44 ++test_count_;
45 }
46
47 void Test1ConstRef(const std::string& a) {
48 ++test_count_;
49 result_.append(a);
50 }
51
52 void Test1Ptr(std::string* a) {
53 ++test_count_;
54 result_.append(*a);
55 }
56
57 void Test1Int(int a) {
58 test_count_ += a;
59 }
60
61 void Test2Ptr(std::string* a, std::string* b) {
62 ++test_count_;
63 result_.append(*a);
64 result_.append(*b);
65 }
66
67 void Test2Mixed(const std::string& a, std::string* b) {
68 ++test_count_;
69 result_.append(a);
70 result_.append(*b);
71 }
72
73 int test_count() const { return test_count_; }
74 const std::string& result() const { return result_; }
75
76 private:
[email protected]877d55d2009-11-05 21:53:0877 friend class base::RefCounted<Foo>;
78
79 ~Foo() {}
80
initial.commitd7cae122008-07-26 21:49:3881 int test_count_;
82 std::string result_;
83};
84
[email protected]b224f792011-04-20 16:02:2385void RunTest_PostTask(MessageLoop::Type message_loop_type) {
86 MessageLoop loop(message_loop_type);
87
88 // Add tests to message loop
89 scoped_refptr<Foo> foo(new Foo());
90 std::string a("a"), b("b"), c("c"), d("d");
91 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
92 &Foo::Test0, foo.get()));
93 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
94 &Foo::Test1ConstRef, foo.get(), a));
95 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
96 &Foo::Test1Ptr, foo.get(), &b));
97 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
98 &Foo::Test1Int, foo.get(), 100));
99 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
100 &Foo::Test2Ptr, foo.get(), &a, &c));
101 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
102 &Foo::Test2Mixed, foo.get(), a, &d));
103
104 // After all tests, post a message that will shut down the message loop
105 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
106 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
initial.commitd7cae122008-07-26 21:49:38107
108 // Now kick things off
109 MessageLoop::current()->Run();
110
111 EXPECT_EQ(foo->test_count(), 105);
112 EXPECT_EQ(foo->result(), "abacad");
113}
114
[email protected]4d9bdfaf2008-08-26 05:53:57115void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) {
116 MessageLoop loop(message_loop_type);
117
initial.commitd7cae122008-07-26 21:49:38118 // Add tests to message loop
[email protected]ad8e04a2010-11-01 04:16:27119 scoped_refptr<Foo> foo(new Foo());
initial.commitd7cae122008-07-26 21:49:38120 std::string a("a"), b("b"), c("c"), d("d");
[email protected]b224f792011-04-20 16:02:23121 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
122 &Foo::Test0, foo.get()));
123 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
124 &Foo::Test1ConstRef, foo.get(), a));
125 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
126 &Foo::Test1Ptr, foo.get(), &b));
127 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
128 &Foo::Test1Int, foo.get(), 100));
129 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
130 &Foo::Test2Ptr, foo.get(), &a, &c));
131 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
132 &Foo::Test2Mixed, foo.get(), a, &d));
initial.commitd7cae122008-07-26 21:49:38133
134 // After all tests, post a message that will shut down the message loop
[email protected]b224f792011-04-20 16:02:23135 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
136 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
initial.commitd7cae122008-07-26 21:49:38137
138 // Now kick things off with the SEH block active.
139 MessageLoop::current()->set_exception_restoration(true);
140 MessageLoop::current()->Run();
141 MessageLoop::current()->set_exception_restoration(false);
142
143 EXPECT_EQ(foo->test_count(), 105);
144 EXPECT_EQ(foo->result(), "abacad");
145}
146
[email protected]b224f792011-04-20 16:02:23147// This function runs slowly to simulate a large amount of work being done.
[email protected]a1b75b942011-12-31 22:53:51148static void SlowFunc(TimeDelta pause, int* quit_counter) {
149 PlatformThread::Sleep(pause);
[email protected]b224f792011-04-20 16:02:23150 if (--(*quit_counter) == 0)
[email protected]752578562008-09-07 08:08:29151 MessageLoop::current()->Quit();
[email protected]b224f792011-04-20 16:02:23152}
[email protected]752578562008-09-07 08:08:29153
[email protected]b224f792011-04-20 16:02:23154// This function records the time when Run was called in a Time object, which is
[email protected]752578562008-09-07 08:08:29155// useful for building a variety of MessageLoop tests.
[email protected]b224f792011-04-20 16:02:23156static void RecordRunTimeFunc(Time* run_time, int* quit_counter) {
157 *run_time = Time::Now();
158
[email protected]752578562008-09-07 08:08:29159 // Cause our Run function to take some time to execute. As a result we can
[email protected]b224f792011-04-20 16:02:23160 // count on subsequent RecordRunTimeFunc()s running at a future time,
[email protected]752578562008-09-07 08:08:29161 // without worry about the resolution of our system clock being an issue.
[email protected]a1b75b942011-12-31 22:53:51162 SlowFunc(TimeDelta::FromMilliseconds(10), quit_counter);
[email protected]b224f792011-04-20 16:02:23163}
[email protected]752578562008-09-07 08:08:29164
165void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) {
166 MessageLoop loop(message_loop_type);
167
168 // Test that PostDelayedTask results in a delayed task.
169
[email protected]5761ab9c2012-02-04 16:44:53170 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
[email protected]752578562008-09-07 08:08:29171
172 int num_tasks = 1;
173 Time run_time;
174
175 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23176 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
[email protected]5761ab9c2012-02-04 16:44:53177 kDelay);
[email protected]752578562008-09-07 08:08:29178
179 Time time_before_run = Time::Now();
180 loop.Run();
181 Time time_after_run = Time::Now();
182
183 EXPECT_EQ(0, num_tasks);
[email protected]5761ab9c2012-02-04 16:44:53184 EXPECT_LT(kDelay, time_after_run - time_before_run);
[email protected]752578562008-09-07 08:08:29185}
186
[email protected]b224f792011-04-20 16:02:23187void RunTest_PostDelayedTask_InDelayOrder(
188 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29189 MessageLoop loop(message_loop_type);
190
191 // Test that two tasks with different delays run in the right order.
[email protected]752578562008-09-07 08:08:29192 int num_tasks = 2;
193 Time run_time1, run_time2;
194
195 loop.PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53196 FROM_HERE,
197 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks),
198 TimeDelta::FromMilliseconds(200));
[email protected]752578562008-09-07 08:08:29199 // If we get a large pause in execution (due to a context switch) here, this
200 // test could fail.
201 loop.PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53202 FROM_HERE,
203 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks),
204 TimeDelta::FromMilliseconds(10));
[email protected]752578562008-09-07 08:08:29205
206 loop.Run();
207 EXPECT_EQ(0, num_tasks);
208
209 EXPECT_TRUE(run_time2 < run_time1);
210}
211
[email protected]b224f792011-04-20 16:02:23212void RunTest_PostDelayedTask_InPostOrder(
213 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29214 MessageLoop loop(message_loop_type);
215
216 // Test that two tasks with the same delay run in the order in which they
217 // were posted.
218 //
219 // NOTE: This is actually an approximate test since the API only takes a
220 // "delay" parameter, so we are not exactly simulating two tasks that get
221 // posted at the exact same time. It would be nice if the API allowed us to
222 // specify the desired run time.
223
[email protected]5761ab9c2012-02-04 16:44:53224 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
[email protected]752578562008-09-07 08:08:29225
226 int num_tasks = 2;
227 Time run_time1, run_time2;
228
229 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23230 FROM_HERE,
[email protected]5761ab9c2012-02-04 16:44:53231 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelay);
[email protected]752578562008-09-07 08:08:29232 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23233 FROM_HERE,
[email protected]5761ab9c2012-02-04 16:44:53234 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelay);
[email protected]752578562008-09-07 08:08:29235
236 loop.Run();
237 EXPECT_EQ(0, num_tasks);
238
239 EXPECT_TRUE(run_time1 < run_time2);
240}
241
[email protected]32cda29d2008-10-09 23:58:43242void RunTest_PostDelayedTask_InPostOrder_2(
243 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29244 MessageLoop loop(message_loop_type);
245
246 // Test that a delayed task still runs after a normal tasks even if the
247 // normal tasks take a long time to run.
248
[email protected]a1b75b942011-12-31 22:53:51249 const TimeDelta kPause = TimeDelta::FromMilliseconds(50);
[email protected]752578562008-09-07 08:08:29250
251 int num_tasks = 2;
252 Time run_time;
253
[email protected]a1b75b942011-12-31 22:53:51254 loop.PostTask(FROM_HERE, base::Bind(&SlowFunc, kPause, &num_tasks));
[email protected]752578562008-09-07 08:08:29255 loop.PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53256 FROM_HERE,
257 base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
258 TimeDelta::FromMilliseconds(10));
[email protected]752578562008-09-07 08:08:29259
260 Time time_before_run = Time::Now();
261 loop.Run();
262 Time time_after_run = Time::Now();
263
264 EXPECT_EQ(0, num_tasks);
265
[email protected]a1b75b942011-12-31 22:53:51266 EXPECT_LT(kPause, time_after_run - time_before_run);
[email protected]752578562008-09-07 08:08:29267}
268
[email protected]32cda29d2008-10-09 23:58:43269void RunTest_PostDelayedTask_InPostOrder_3(
270 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29271 MessageLoop loop(message_loop_type);
272
273 // Test that a delayed task still runs after a pile of normal tasks. The key
274 // difference between this test and the previous one is that here we return
275 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities
276 // to maybe run the delayed task. It should know not to do so until the
277 // delayed task's delay has passed.
278
279 int num_tasks = 11;
280 Time run_time1, run_time2;
281
282 // Clutter the ML with tasks.
283 for (int i = 1; i < num_tasks; ++i)
[email protected]b224f792011-04-20 16:02:23284 loop.PostTask(FROM_HERE,
285 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks));
[email protected]752578562008-09-07 08:08:29286
287 loop.PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53288 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks),
289 TimeDelta::FromMilliseconds(1));
[email protected]752578562008-09-07 08:08:29290
291 loop.Run();
292 EXPECT_EQ(0, num_tasks);
293
294 EXPECT_TRUE(run_time2 > run_time1);
295}
296
[email protected]b224f792011-04-20 16:02:23297void RunTest_PostDelayedTask_SharedTimer(
298 MessageLoop::Type message_loop_type) {
[email protected]72deacd2008-09-23 19:19:20299 MessageLoop loop(message_loop_type);
300
301 // Test that the interval of the timer, used to run the next delayed task, is
302 // set to a value corresponding to when the next delayed task should run.
303
304 // By setting num_tasks to 1, we ensure that the first task to run causes the
305 // run loop to exit.
306 int num_tasks = 1;
307 Time run_time1, run_time2;
308
309 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23310 FROM_HERE,
311 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks),
[email protected]5761ab9c2012-02-04 16:44:53312 TimeDelta::FromSeconds(1000));
[email protected]72deacd2008-09-23 19:19:20313 loop.PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53314 FROM_HERE,
315 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks),
316 TimeDelta::FromMilliseconds(10));
[email protected]72deacd2008-09-23 19:19:20317
318 Time start_time = Time::Now();
319
320 loop.Run();
321 EXPECT_EQ(0, num_tasks);
322
323 // Ensure that we ran in far less time than the slower timer.
[email protected]4615f1982008-09-23 19:22:33324 TimeDelta total_time = Time::Now() - start_time;
325 EXPECT_GT(5000, total_time.InMilliseconds());
[email protected]32cda29d2008-10-09 23:58:43326
[email protected]72deacd2008-09-23 19:19:20327 // In case both timers somehow run at nearly the same time, sleep a little
328 // and then run all pending to force them both to have run. This is just
329 // encouraging flakiness if there is any.
[email protected]a1b75b942011-12-31 22:53:51330 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
[email protected]72deacd2008-09-23 19:19:20331 loop.RunAllPending();
332
333 EXPECT_TRUE(run_time1.is_null());
334 EXPECT_FALSE(run_time2.is_null());
335}
336
337#if defined(OS_WIN)
338
[email protected]b224f792011-04-20 16:02:23339void SubPumpFunc() {
340 MessageLoop::current()->SetNestableTasksAllowed(true);
341 MSG msg;
342 while (GetMessage(&msg, NULL, 0, 0)) {
343 TranslateMessage(&msg);
344 DispatchMessage(&msg);
[email protected]72deacd2008-09-23 19:19:20345 }
[email protected]b224f792011-04-20 16:02:23346 MessageLoop::current()->Quit();
347}
[email protected]72deacd2008-09-23 19:19:20348
349void RunTest_PostDelayedTask_SharedTimer_SubPump() {
350 MessageLoop loop(MessageLoop::TYPE_UI);
351
352 // Test that the interval of the timer, used to run the next delayed task, is
353 // set to a value corresponding to when the next delayed task should run.
354
355 // By setting num_tasks to 1, we ensure that the first task to run causes the
356 // run loop to exit.
357 int num_tasks = 1;
358 Time run_time;
359
[email protected]b224f792011-04-20 16:02:23360 loop.PostTask(FROM_HERE, base::Bind(&SubPumpFunc));
[email protected]72deacd2008-09-23 19:19:20361
362 // This very delayed task should never run.
363 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23364 FROM_HERE,
365 base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
[email protected]5761ab9c2012-02-04 16:44:53366 TimeDelta::FromSeconds(1000));
[email protected]72deacd2008-09-23 19:19:20367
[email protected]b224f792011-04-20 16:02:23368 // This slightly delayed task should run from within SubPumpFunc).
[email protected]5761ab9c2012-02-04 16:44:53369 loop.PostDelayedTask(
370 FROM_HERE,
371 base::Bind(&PostQuitMessage, 0),
372 TimeDelta::FromMilliseconds(10));
[email protected]72deacd2008-09-23 19:19:20373
374 Time start_time = Time::Now();
375
376 loop.Run();
377 EXPECT_EQ(1, num_tasks);
378
379 // Ensure that we ran in far less time than the slower timer.
[email protected]4615f1982008-09-23 19:22:33380 TimeDelta total_time = Time::Now() - start_time;
381 EXPECT_GT(5000, total_time.InMilliseconds());
[email protected]72deacd2008-09-23 19:19:20382
383 // In case both timers somehow run at nearly the same time, sleep a little
384 // and then run all pending to force them both to have run. This is just
385 // encouraging flakiness if there is any.
[email protected]a1b75b942011-12-31 22:53:51386 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
[email protected]72deacd2008-09-23 19:19:20387 loop.RunAllPending();
388
389 EXPECT_TRUE(run_time.is_null());
390}
391
392#endif // defined(OS_WIN)
393
[email protected]b224f792011-04-20 16:02:23394// This is used to inject a test point for recording the destructor calls for
395// Closure objects send to MessageLoop::PostTask(). It is awkward usage since we
396// are trying to hook the actual destruction, which is not a common operation.
397class RecordDeletionProbe : public base::RefCounted<RecordDeletionProbe> {
[email protected]001747c2008-09-10 00:37:07398 public:
[email protected]b224f792011-04-20 16:02:23399 RecordDeletionProbe(RecordDeletionProbe* post_on_delete, bool* was_deleted)
[email protected]001747c2008-09-10 00:37:07400 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) {
401 }
[email protected]a9aaa9d12012-04-25 00:42:51402 void Run() {}
403
404 private:
405 friend class base::RefCounted<RecordDeletionProbe>;
406
[email protected]b224f792011-04-20 16:02:23407 ~RecordDeletionProbe() {
[email protected]001747c2008-09-10 00:37:07408 *was_deleted_ = true;
409 if (post_on_delete_)
[email protected]b224f792011-04-20 16:02:23410 MessageLoop::current()->PostTask(
411 FROM_HERE,
412 base::Bind(&RecordDeletionProbe::Run, post_on_delete_.get()));
[email protected]001747c2008-09-10 00:37:07413 }
[email protected]a9aaa9d12012-04-25 00:42:51414
[email protected]b224f792011-04-20 16:02:23415 scoped_refptr<RecordDeletionProbe> post_on_delete_;
[email protected]001747c2008-09-10 00:37:07416 bool* was_deleted_;
417};
418
[email protected]b224f792011-04-20 16:02:23419void RunTest_EnsureDeletion(MessageLoop::Type message_loop_type) {
[email protected]001747c2008-09-10 00:37:07420 bool a_was_deleted = false;
421 bool b_was_deleted = false;
422 {
423 MessageLoop loop(message_loop_type);
424 loop.PostTask(
[email protected]b224f792011-04-20 16:02:23425 FROM_HERE, base::Bind(&RecordDeletionProbe::Run,
426 new RecordDeletionProbe(NULL, &a_was_deleted)));
[email protected]5761ab9c2012-02-04 16:44:53427 // TODO(ajwong): Do we really need 1000ms here?
[email protected]001747c2008-09-10 00:37:07428 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23429 FROM_HERE, base::Bind(&RecordDeletionProbe::Run,
430 new RecordDeletionProbe(NULL, &b_was_deleted)),
[email protected]5761ab9c2012-02-04 16:44:53431 TimeDelta::FromMilliseconds(1000));
[email protected]001747c2008-09-10 00:37:07432 }
433 EXPECT_TRUE(a_was_deleted);
434 EXPECT_TRUE(b_was_deleted);
435}
436
[email protected]b224f792011-04-20 16:02:23437void RunTest_EnsureDeletion_Chain(MessageLoop::Type message_loop_type) {
[email protected]001747c2008-09-10 00:37:07438 bool a_was_deleted = false;
439 bool b_was_deleted = false;
440 bool c_was_deleted = false;
441 {
442 MessageLoop loop(message_loop_type);
[email protected]b224f792011-04-20 16:02:23443 // The scoped_refptr for each of the below is held either by the chained
444 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback.
445 RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted);
446 RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted);
447 RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted);
448 loop.PostTask(FROM_HERE, base::Bind(&RecordDeletionProbe::Run, c));
[email protected]001747c2008-09-10 00:37:07449 }
450 EXPECT_TRUE(a_was_deleted);
451 EXPECT_TRUE(b_was_deleted);
452 EXPECT_TRUE(c_was_deleted);
453}
454
[email protected]b224f792011-04-20 16:02:23455void NestingFunc(int* depth) {
456 if (*depth > 0) {
457 *depth -= 1;
458 MessageLoop::current()->PostTask(FROM_HERE,
459 base::Bind(&NestingFunc, depth));
initial.commitd7cae122008-07-26 21:49:38460
[email protected]b224f792011-04-20 16:02:23461 MessageLoop::current()->SetNestableTasksAllowed(true);
462 MessageLoop::current()->Run();
initial.commitd7cae122008-07-26 21:49:38463 }
[email protected]b224f792011-04-20 16:02:23464 MessageLoop::current()->Quit();
465}
initial.commitd7cae122008-07-26 21:49:38466
[email protected]b16ef312008-08-19 18:36:23467#if defined(OS_WIN)
468
initial.commitd7cae122008-07-26 21:49:38469LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) {
470 ADD_FAILURE() << "bad exception handler";
471 ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode);
472 return EXCEPTION_EXECUTE_HANDLER;
473}
474
475// This task throws an SEH exception: initially write to an invalid address.
476// If the right SEH filter is installed, it will fix the error.
[email protected]b224f792011-04-20 16:02:23477class Crasher : public base::RefCounted<Crasher> {
initial.commitd7cae122008-07-26 21:49:38478 public:
479 // Ctor. If trash_SEH_handler is true, the task will override the unhandled
480 // exception handler with one sure to crash this test.
[email protected]b224f792011-04-20 16:02:23481 explicit Crasher(bool trash_SEH_handler)
initial.commitd7cae122008-07-26 21:49:38482 : trash_SEH_handler_(trash_SEH_handler) {
483 }
[email protected]b224f792011-04-20 16:02:23484
initial.commitd7cae122008-07-26 21:49:38485 void Run() {
[email protected]a1b75b942011-12-31 22:53:51486 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1));
initial.commitd7cae122008-07-26 21:49:38487 if (trash_SEH_handler_)
488 ::SetUnhandledExceptionFilter(&BadExceptionHandler);
489 // Generate a SEH fault. We do it in asm to make sure we know how to undo
490 // the damage.
[email protected]c88873922008-07-30 13:02:03491
492#if defined(_M_IX86)
493
initial.commitd7cae122008-07-26 21:49:38494 __asm {
[email protected]b224f792011-04-20 16:02:23495 mov eax, dword ptr [Crasher::bad_array_]
initial.commitd7cae122008-07-26 21:49:38496 mov byte ptr [eax], 66
497 }
[email protected]c88873922008-07-30 13:02:03498
499#elif defined(_M_X64)
500
501 bad_array_[0] = 66;
502
[email protected]b16ef312008-08-19 18:36:23503#else
504#error "needs architecture support"
[email protected]c88873922008-07-30 13:02:03505#endif
506
initial.commitd7cae122008-07-26 21:49:38507 MessageLoop::current()->Quit();
508 }
509 // Points the bad array to a valid memory location.
510 static void FixError() {
511 bad_array_ = &valid_store_;
512 }
513
514 private:
515 bool trash_SEH_handler_;
516 static volatile char* bad_array_;
517 static char valid_store_;
518};
519
[email protected]b224f792011-04-20 16:02:23520volatile char* Crasher::bad_array_ = 0;
521char Crasher::valid_store_ = 0;
initial.commitd7cae122008-07-26 21:49:38522
523// This SEH filter fixes the problem and retries execution. Fixing requires
[email protected]b224f792011-04-20 16:02:23524// that the last instruction: mov eax, [Crasher::bad_array_] to be retried
initial.commitd7cae122008-07-26 21:49:38525// so we move the instruction pointer 5 bytes back.
[email protected]b224f792011-04-20 16:02:23526LONG WINAPI HandleCrasherException(EXCEPTION_POINTERS *ex_info) {
initial.commitd7cae122008-07-26 21:49:38527 if (ex_info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
528 return EXCEPTION_EXECUTE_HANDLER;
529
[email protected]b224f792011-04-20 16:02:23530 Crasher::FixError();
[email protected]c88873922008-07-30 13:02:03531
532#if defined(_M_IX86)
533
initial.commitd7cae122008-07-26 21:49:38534 ex_info->ContextRecord->Eip -= 5;
[email protected]c88873922008-07-30 13:02:03535
536#elif defined(_M_X64)
537
538 ex_info->ContextRecord->Rip -= 5;
539
540#endif
541
initial.commitd7cae122008-07-26 21:49:38542 return EXCEPTION_CONTINUE_EXECUTION;
543}
544
[email protected]4d9bdfaf2008-08-26 05:53:57545void RunTest_Crasher(MessageLoop::Type message_loop_type) {
546 MessageLoop loop(message_loop_type);
[email protected]b16ef312008-08-19 18:36:23547
initial.commitd7cae122008-07-26 21:49:38548 if (::IsDebuggerPresent())
549 return;
550
551 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
[email protected]b224f792011-04-20 16:02:23552 ::SetUnhandledExceptionFilter(&HandleCrasherException);
initial.commitd7cae122008-07-26 21:49:38553
[email protected]b224f792011-04-20 16:02:23554 MessageLoop::current()->PostTask(
555 FROM_HERE,
556 base::Bind(&Crasher::Run, new Crasher(false)));
initial.commitd7cae122008-07-26 21:49:38557 MessageLoop::current()->set_exception_restoration(true);
558 MessageLoop::current()->Run();
559 MessageLoop::current()->set_exception_restoration(false);
560
561 ::SetUnhandledExceptionFilter(old_SEH_filter);
562}
563
[email protected]4d9bdfaf2008-08-26 05:53:57564void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) {
565 MessageLoop loop(message_loop_type);
[email protected]32cda29d2008-10-09 23:58:43566
initial.commitd7cae122008-07-26 21:49:38567 if (::IsDebuggerPresent())
568 return;
569
570 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
[email protected]b224f792011-04-20 16:02:23571 ::SetUnhandledExceptionFilter(&HandleCrasherException);
initial.commitd7cae122008-07-26 21:49:38572
[email protected]b224f792011-04-20 16:02:23573 MessageLoop::current()->PostTask(
574 FROM_HERE,
575 base::Bind(&Crasher::Run, new Crasher(true)));
initial.commitd7cae122008-07-26 21:49:38576 MessageLoop::current()->set_exception_restoration(true);
577 MessageLoop::current()->Run();
578 MessageLoop::current()->set_exception_restoration(false);
579
580 ::SetUnhandledExceptionFilter(old_SEH_filter);
581}
582
[email protected]295039bd2008-08-15 04:32:57583#endif // defined(OS_WIN)
584
[email protected]4d9bdfaf2008-08-26 05:53:57585void RunTest_Nesting(MessageLoop::Type message_loop_type) {
586 MessageLoop loop(message_loop_type);
[email protected]c88873922008-07-30 13:02:03587
initial.commitd7cae122008-07-26 21:49:38588 int depth = 100;
[email protected]b224f792011-04-20 16:02:23589 MessageLoop::current()->PostTask(FROM_HERE,
590 base::Bind(&NestingFunc, &depth));
initial.commitd7cae122008-07-26 21:49:38591 MessageLoop::current()->Run();
592 EXPECT_EQ(depth, 0);
593}
594
initial.commitd7cae122008-07-26 21:49:38595const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test";
596
597enum TaskType {
598 MESSAGEBOX,
599 ENDDIALOG,
600 RECURSIVE,
601 TIMEDMESSAGELOOP,
602 QUITMESSAGELOOP,
[email protected]8e937c1e2012-06-28 22:57:30603 ORDERED,
initial.commitd7cae122008-07-26 21:49:38604 PUMPS,
[email protected]c4280a92009-06-25 19:23:11605 SLEEP,
[email protected]8e937c1e2012-06-28 22:57:30606 RUNS,
initial.commitd7cae122008-07-26 21:49:38607};
608
609// Saves the order in which the tasks executed.
610struct TaskItem {
611 TaskItem(TaskType t, int c, bool s)
612 : type(t),
613 cookie(c),
614 start(s) {
615 }
616
617 TaskType type;
618 int cookie;
619 bool start;
620
621 bool operator == (const TaskItem& other) const {
622 return type == other.type && cookie == other.cookie && start == other.start;
623 }
624};
625
initial.commitd7cae122008-07-26 21:49:38626std::ostream& operator <<(std::ostream& os, TaskType type) {
627 switch (type) {
628 case MESSAGEBOX: os << "MESSAGEBOX"; break;
629 case ENDDIALOG: os << "ENDDIALOG"; break;
630 case RECURSIVE: os << "RECURSIVE"; break;
631 case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break;
632 case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break;
[email protected]8e937c1e2012-06-28 22:57:30633 case ORDERED: os << "ORDERED"; break;
initial.commitd7cae122008-07-26 21:49:38634 case PUMPS: os << "PUMPS"; break;
[email protected]c4280a92009-06-25 19:23:11635 case SLEEP: os << "SLEEP"; break;
initial.commitd7cae122008-07-26 21:49:38636 default:
637 NOTREACHED();
638 os << "Unknown TaskType";
639 break;
640 }
641 return os;
642}
643
644std::ostream& operator <<(std::ostream& os, const TaskItem& item) {
645 if (item.start)
646 return os << item.type << " " << item.cookie << " starts";
647 else
648 return os << item.type << " " << item.cookie << " ends";
649}
650
[email protected]b224f792011-04-20 16:02:23651class TaskList {
initial.commitd7cae122008-07-26 21:49:38652 public:
[email protected]b224f792011-04-20 16:02:23653 void RecordStart(TaskType type, int cookie) {
654 TaskItem item(type, cookie, true);
[email protected]b026e35d2010-10-19 02:31:03655 DVLOG(1) << item;
[email protected]b224f792011-04-20 16:02:23656 task_list_.push_back(item);
initial.commitd7cae122008-07-26 21:49:38657 }
[email protected]b224f792011-04-20 16:02:23658
659 void RecordEnd(TaskType type, int cookie) {
660 TaskItem item(type, cookie, false);
[email protected]b026e35d2010-10-19 02:31:03661 DVLOG(1) << item;
[email protected]b224f792011-04-20 16:02:23662 task_list_.push_back(item);
initial.commitd7cae122008-07-26 21:49:38663 }
664
[email protected]b224f792011-04-20 16:02:23665 size_t Size() {
666 return task_list_.size();
initial.commitd7cae122008-07-26 21:49:38667 }
668
[email protected]b224f792011-04-20 16:02:23669 TaskItem Get(int n) {
670 return task_list_[n];
initial.commitd7cae122008-07-26 21:49:38671 }
672
673 private:
[email protected]b224f792011-04-20 16:02:23674 std::vector<TaskItem> task_list_;
initial.commitd7cae122008-07-26 21:49:38675};
676
[email protected]b224f792011-04-20 16:02:23677// Saves the order the tasks ran.
678void OrderedFunc(TaskList* order, int cookie) {
[email protected]8e937c1e2012-06-28 22:57:30679 order->RecordStart(ORDERED, cookie);
680 order->RecordEnd(ORDERED, cookie);
[email protected]b224f792011-04-20 16:02:23681}
682
[email protected]b16ef312008-08-19 18:36:23683#if defined(OS_WIN)
684
initial.commitd7cae122008-07-26 21:49:38685// MessageLoop implicitly start a "modal message loop". Modal dialog boxes,
686// common controls (like OpenFile) and StartDoc printing function can cause
687// implicit message loops.
[email protected]b224f792011-04-20 16:02:23688void MessageBoxFunc(TaskList* order, int cookie, bool is_reentrant) {
689 order->RecordStart(MESSAGEBOX, cookie);
690 if (is_reentrant)
691 MessageLoop::current()->SetNestableTasksAllowed(true);
692 MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK);
693 order->RecordEnd(MESSAGEBOX, cookie);
694}
initial.commitd7cae122008-07-26 21:49:38695
696// Will end the MessageBox.
[email protected]b224f792011-04-20 16:02:23697void EndDialogFunc(TaskList* order, int cookie) {
698 order->RecordStart(ENDDIALOG, cookie);
699 HWND window = GetActiveWindow();
700 if (window != NULL) {
701 EXPECT_NE(EndDialog(window, IDCONTINUE), 0);
702 // Cheap way to signal that the window wasn't found if RunEnd() isn't
703 // called.
704 order->RecordEnd(ENDDIALOG, cookie);
initial.commitd7cae122008-07-26 21:49:38705 }
[email protected]b224f792011-04-20 16:02:23706}
initial.commitd7cae122008-07-26 21:49:38707
[email protected]b16ef312008-08-19 18:36:23708#endif // defined(OS_WIN)
709
[email protected]b224f792011-04-20 16:02:23710void RecursiveFunc(TaskList* order, int cookie, int depth,
711 bool is_reentrant) {
712 order->RecordStart(RECURSIVE, cookie);
713 if (depth > 0) {
714 if (is_reentrant)
715 MessageLoop::current()->SetNestableTasksAllowed(true);
716 MessageLoop::current()->PostTask(
717 FROM_HERE,
718 base::Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant));
initial.commitd7cae122008-07-26 21:49:38719 }
[email protected]b224f792011-04-20 16:02:23720 order->RecordEnd(RECURSIVE, cookie);
721}
initial.commitd7cae122008-07-26 21:49:38722
[email protected]b224f792011-04-20 16:02:23723void RecursiveSlowFunc(TaskList* order, int cookie, int depth,
724 bool is_reentrant) {
725 RecursiveFunc(order, cookie, depth, is_reentrant);
[email protected]a1b75b942011-12-31 22:53:51726 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
[email protected]b224f792011-04-20 16:02:23727}
initial.commitd7cae122008-07-26 21:49:38728
[email protected]b224f792011-04-20 16:02:23729void QuitFunc(TaskList* order, int cookie) {
730 order->RecordStart(QUITMESSAGELOOP, cookie);
731 MessageLoop::current()->Quit();
732 order->RecordEnd(QUITMESSAGELOOP, cookie);
733}
initial.commitd7cae122008-07-26 21:49:38734
[email protected]a1b75b942011-12-31 22:53:51735void SleepFunc(TaskList* order, int cookie, TimeDelta delay) {
[email protected]b224f792011-04-20 16:02:23736 order->RecordStart(SLEEP, cookie);
[email protected]a1b75b942011-12-31 22:53:51737 PlatformThread::Sleep(delay);
[email protected]b224f792011-04-20 16:02:23738 order->RecordEnd(SLEEP, cookie);
739}
[email protected]c4280a92009-06-25 19:23:11740
[email protected]295039bd2008-08-15 04:32:57741#if defined(OS_WIN)
[email protected]b224f792011-04-20 16:02:23742void RecursiveFuncWin(MessageLoop* target,
743 HANDLE event,
744 bool expect_window,
745 TaskList* order,
746 bool is_reentrant) {
747 target->PostTask(FROM_HERE,
748 base::Bind(&RecursiveFunc, order, 1, 2, is_reentrant));
749 target->PostTask(FROM_HERE,
750 base::Bind(&MessageBoxFunc, order, 2, is_reentrant));
751 target->PostTask(FROM_HERE,
752 base::Bind(&RecursiveFunc, order, 3, 2, is_reentrant));
753 // The trick here is that for recursive task processing, this task will be
754 // ran _inside_ the MessageBox message loop, dismissing the MessageBox
755 // without a chance.
756 // For non-recursive task processing, this will be executed _after_ the
757 // MessageBox will have been dismissed by the code below, where
758 // expect_window_ is true.
759 target->PostTask(FROM_HERE,
760 base::Bind(&EndDialogFunc, order, 4));
761 target->PostTask(FROM_HERE,
762 base::Bind(&QuitFunc, order, 5));
[email protected]295039bd2008-08-15 04:32:57763
[email protected]b224f792011-04-20 16:02:23764 // Enforce that every tasks are sent before starting to run the main thread
765 // message loop.
766 ASSERT_TRUE(SetEvent(event));
initial.commitd7cae122008-07-26 21:49:38767
[email protected]b224f792011-04-20 16:02:23768 // Poll for the MessageBox. Don't do this at home! At the speed we do it,
769 // you will never realize one MessageBox was shown.
770 for (; expect_window;) {
771 HWND window = FindWindow(L"#32770", kMessageBoxTitle);
772 if (window) {
773 // Dismiss it.
774 for (;;) {
775 HWND button = FindWindowEx(window, NULL, L"Button", NULL);
776 if (button != NULL) {
777 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONDOWN, 0, 0));
778 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONUP, 0, 0));
779 break;
initial.commitd7cae122008-07-26 21:49:38780 }
initial.commitd7cae122008-07-26 21:49:38781 }
[email protected]b224f792011-04-20 16:02:23782 break;
initial.commitd7cae122008-07-26 21:49:38783 }
784 }
[email protected]b224f792011-04-20 16:02:23785}
initial.commitd7cae122008-07-26 21:49:38786
[email protected]295039bd2008-08-15 04:32:57787#endif // defined(OS_WIN)
788
[email protected]4d9bdfaf2008-08-26 05:53:57789void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) {
790 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:38791
initial.commitd7cae122008-07-26 21:49:38792 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
793 TaskList order;
[email protected]b224f792011-04-20 16:02:23794 MessageLoop::current()->PostTask(
795 FROM_HERE,
796 base::Bind(&RecursiveFunc, &order, 1, 2, false));
797 MessageLoop::current()->PostTask(
798 FROM_HERE,
799 base::Bind(&RecursiveFunc, &order, 2, 2, false));
800 MessageLoop::current()->PostTask(
801 FROM_HERE,
802 base::Bind(&QuitFunc, &order, 3));
initial.commitd7cae122008-07-26 21:49:38803
804 MessageLoop::current()->Run();
805
806 // FIFO order.
[email protected]b224f792011-04-20 16:02:23807 ASSERT_EQ(14U, order.Size());
808 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
809 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
810 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
811 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
812 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
813 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
814 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true));
815 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false));
816 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
817 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
818 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
819 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
820 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
821 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
initial.commitd7cae122008-07-26 21:49:38822}
823
[email protected]4554c232011-02-17 19:25:04824void RunTest_RecursiveDenial3(MessageLoop::Type message_loop_type) {
825 MessageLoop loop(message_loop_type);
826
827 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
828 TaskList order;
[email protected]b224f792011-04-20 16:02:23829 MessageLoop::current()->PostTask(
830 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 1, 2, false));
831 MessageLoop::current()->PostTask(
832 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 2, 2, false));
833 MessageLoop::current()->PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53834 FROM_HERE,
835 base::Bind(&OrderedFunc, &order, 3),
836 TimeDelta::FromMilliseconds(5));
[email protected]b224f792011-04-20 16:02:23837 MessageLoop::current()->PostDelayedTask(
[email protected]5761ab9c2012-02-04 16:44:53838 FROM_HERE,
839 base::Bind(&QuitFunc, &order, 4),
840 TimeDelta::FromMilliseconds(5));
[email protected]4554c232011-02-17 19:25:04841
842 MessageLoop::current()->Run();
843
844 // FIFO order.
[email protected]b224f792011-04-20 16:02:23845 ASSERT_EQ(16U, order.Size());
846 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
847 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
848 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
849 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
850 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true));
851 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false));
[email protected]8e937c1e2012-06-28 22:57:30852 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 3, true));
853 EXPECT_EQ(order.Get(7), TaskItem(ORDERED, 3, false));
[email protected]b224f792011-04-20 16:02:23854 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
855 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
856 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true));
857 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false));
858 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true));
859 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false));
860 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true));
861 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false));
[email protected]4554c232011-02-17 19:25:04862}
863
[email protected]4d9bdfaf2008-08-26 05:53:57864void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) {
865 MessageLoop loop(message_loop_type);
[email protected]32cda29d2008-10-09 23:58:43866
initial.commitd7cae122008-07-26 21:49:38867 TaskList order;
[email protected]b224f792011-04-20 16:02:23868 MessageLoop::current()->PostTask(
869 FROM_HERE, base::Bind(&RecursiveFunc, &order, 1, 2, true));
870 MessageLoop::current()->PostTask(
871 FROM_HERE, base::Bind(&RecursiveFunc, &order, 2, 2, true));
872 MessageLoop::current()->PostTask(
873 FROM_HERE, base::Bind(&QuitFunc, &order, 3));
initial.commitd7cae122008-07-26 21:49:38874
875 MessageLoop::current()->Run();
876
877 // FIFO order.
[email protected]b224f792011-04-20 16:02:23878 ASSERT_EQ(14U, order.Size());
879 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
880 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
881 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
882 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
883 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
884 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
885 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true));
886 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false));
887 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
888 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
889 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
890 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
891 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
892 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
initial.commitd7cae122008-07-26 21:49:38893}
894
[email protected]295039bd2008-08-15 04:32:57895#if defined(OS_WIN)
896// TODO(darin): These tests need to be ported since they test critical
897// message loop functionality.
898
initial.commitd7cae122008-07-26 21:49:38899// A side effect of this test is the generation a beep. Sorry.
[email protected]4d9bdfaf2008-08-26 05:53:57900void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) {
901 MessageLoop loop(message_loop_type);
902
initial.commitd7cae122008-07-26 21:49:38903 Thread worker("RecursiveDenial2_worker");
[email protected]4d9bdfaf2008-08-26 05:53:57904 Thread::Options options;
905 options.message_loop_type = message_loop_type;
906 ASSERT_EQ(true, worker.StartWithOptions(options));
initial.commitd7cae122008-07-26 21:49:38907 TaskList order;
[email protected]b90d7e802011-01-09 16:32:20908 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
initial.commitd7cae122008-07-26 21:49:38909 worker.message_loop()->PostTask(FROM_HERE,
[email protected]b224f792011-04-20 16:02:23910 base::Bind(&RecursiveFuncWin,
911 MessageLoop::current(),
912 event.Get(),
913 true,
914 &order,
915 false));
initial.commitd7cae122008-07-26 21:49:38916 // Let the other thread execute.
917 WaitForSingleObject(event, INFINITE);
918 MessageLoop::current()->Run();
919
[email protected]b224f792011-04-20 16:02:23920 ASSERT_EQ(order.Size(), 17);
921 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
922 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
923 EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true));
924 EXPECT_EQ(order.Get(3), TaskItem(MESSAGEBOX, 2, false));
925 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, true));
926 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 3, false));
927 // When EndDialogFunc is processed, the window is already dismissed, hence no
initial.commitd7cae122008-07-26 21:49:38928 // "end" entry.
[email protected]b224f792011-04-20 16:02:23929 EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, true));
930 EXPECT_EQ(order.Get(7), TaskItem(QUITMESSAGELOOP, 5, true));
931 EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, false));
932 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 1, true));
933 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, false));
934 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 3, true));
935 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, false));
936 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, true));
937 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, false));
938 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 3, true));
939 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, false));
initial.commitd7cae122008-07-26 21:49:38940}
941
[email protected]4d9bdfaf2008-08-26 05:53:57942// A side effect of this test is the generation a beep. Sorry. This test also
943// needs to process windows messages on the current thread.
944void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) {
945 MessageLoop loop(message_loop_type);
946
initial.commitd7cae122008-07-26 21:49:38947 Thread worker("RecursiveSupport2_worker");
[email protected]4d9bdfaf2008-08-26 05:53:57948 Thread::Options options;
949 options.message_loop_type = message_loop_type;
950 ASSERT_EQ(true, worker.StartWithOptions(options));
initial.commitd7cae122008-07-26 21:49:38951 TaskList order;
[email protected]b90d7e802011-01-09 16:32:20952 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
initial.commitd7cae122008-07-26 21:49:38953 worker.message_loop()->PostTask(FROM_HERE,
[email protected]b224f792011-04-20 16:02:23954 base::Bind(&RecursiveFuncWin,
955 MessageLoop::current(),
956 event.Get(),
957 false,
958 &order,
959 true));
initial.commitd7cae122008-07-26 21:49:38960 // Let the other thread execute.
961 WaitForSingleObject(event, INFINITE);
962 MessageLoop::current()->Run();
963
[email protected]b224f792011-04-20 16:02:23964 ASSERT_EQ(order.Size(), 18);
965 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
966 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
967 EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true));
initial.commitd7cae122008-07-26 21:49:38968 // Note that this executes in the MessageBox modal loop.
[email protected]b224f792011-04-20 16:02:23969 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 3, true));
970 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, false));
971 EXPECT_EQ(order.Get(5), TaskItem(ENDDIALOG, 4, true));
972 EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, false));
973 EXPECT_EQ(order.Get(7), TaskItem(MESSAGEBOX, 2, false));
974 /* The order can subtly change here. The reason is that when RecursiveFunc(1)
initial.commitd7cae122008-07-26 21:49:38975 is called in the main thread, if it is faster than getting to the
[email protected]b224f792011-04-20 16:02:23976 PostTask(FROM_HERE, base::Bind(&QuitFunc) execution, the order of task
977 execution can change. We don't care anyway that the order isn't correct.
978 EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, true));
979 EXPECT_EQ(order.Get(9), TaskItem(QUITMESSAGELOOP, 5, false));
980 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
981 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
initial.commitd7cae122008-07-26 21:49:38982 */
[email protected]b224f792011-04-20 16:02:23983 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, true));
984 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 3, false));
985 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, true));
986 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 1, false));
987 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, true));
988 EXPECT_EQ(order.Get(17), TaskItem(RECURSIVE, 3, false));
initial.commitd7cae122008-07-26 21:49:38989}
990
[email protected]295039bd2008-08-15 04:32:57991#endif // defined(OS_WIN)
992
[email protected]b224f792011-04-20 16:02:23993void FuncThatPumps(TaskList* order, int cookie) {
994 order->RecordStart(PUMPS, cookie);
[email protected]b5717a42012-02-14 19:33:52995 {
996 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
997 MessageLoop::current()->RunAllPending();
998 }
[email protected]b224f792011-04-20 16:02:23999 order->RecordEnd(PUMPS, cookie);
1000}
initial.commitd7cae122008-07-26 21:49:381001
[email protected]8e937c1e2012-06-28 22:57:301002void FuncThatRuns(TaskList* order, int cookie, base::RunLoop* run_loop) {
1003 order->RecordStart(RUNS, cookie);
1004 {
1005 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
1006 run_loop->Run();
1007 }
1008 order->RecordEnd(RUNS, cookie);
1009}
1010
1011void FuncThatQuitsNow() {
1012 MessageLoop::current()->QuitNow();
1013}
1014
initial.commitd7cae122008-07-26 21:49:381015// Tests that non nestable tasks run in FIFO if there are no nested loops.
[email protected]b224f792011-04-20 16:02:231016void RunTest_NonNestableWithNoNesting(
1017 MessageLoop::Type message_loop_type) {
[email protected]4d9bdfaf2008-08-26 05:53:571018 MessageLoop loop(message_loop_type);
1019
initial.commitd7cae122008-07-26 21:49:381020 TaskList order;
1021
[email protected]b224f792011-04-20 16:02:231022 MessageLoop::current()->PostNonNestableTask(
1023 FROM_HERE,
1024 base::Bind(&OrderedFunc, &order, 1));
1025 MessageLoop::current()->PostTask(FROM_HERE,
1026 base::Bind(&OrderedFunc, &order, 2));
1027 MessageLoop::current()->PostTask(FROM_HERE,
1028 base::Bind(&QuitFunc, &order, 3));
initial.commitd7cae122008-07-26 21:49:381029 MessageLoop::current()->Run();
1030
1031 // FIFO order.
[email protected]b224f792011-04-20 16:02:231032 ASSERT_EQ(6U, order.Size());
[email protected]8e937c1e2012-06-28 22:57:301033 EXPECT_EQ(order.Get(0), TaskItem(ORDERED, 1, true));
1034 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 1, false));
1035 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 2, true));
1036 EXPECT_EQ(order.Get(3), TaskItem(ORDERED, 2, false));
[email protected]b224f792011-04-20 16:02:231037 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
1038 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
initial.commitd7cae122008-07-26 21:49:381039}
1040
1041// Tests that non nestable tasks don't run when there's code in the call stack.
[email protected]c4280a92009-06-25 19:23:111042void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type,
1043 bool use_delayed) {
[email protected]4d9bdfaf2008-08-26 05:53:571044 MessageLoop loop(message_loop_type);
1045
initial.commitd7cae122008-07-26 21:49:381046 TaskList order;
1047
[email protected]b224f792011-04-20 16:02:231048 MessageLoop::current()->PostTask(
1049 FROM_HERE,
1050 base::Bind(&FuncThatPumps, &order, 1));
[email protected]c4280a92009-06-25 19:23:111051 if (use_delayed) {
[email protected]b224f792011-04-20 16:02:231052 MessageLoop::current()->PostNonNestableDelayedTask(
1053 FROM_HERE,
1054 base::Bind(&OrderedFunc, &order, 2),
[email protected]5761ab9c2012-02-04 16:44:531055 TimeDelta::FromMilliseconds(1));
[email protected]c4280a92009-06-25 19:23:111056 } else {
[email protected]b224f792011-04-20 16:02:231057 MessageLoop::current()->PostNonNestableTask(
1058 FROM_HERE,
1059 base::Bind(&OrderedFunc, &order, 2));
[email protected]c4280a92009-06-25 19:23:111060 }
[email protected]b224f792011-04-20 16:02:231061 MessageLoop::current()->PostTask(FROM_HERE,
1062 base::Bind(&OrderedFunc, &order, 3));
[email protected]a1b75b942011-12-31 22:53:511063 MessageLoop::current()->PostTask(
1064 FROM_HERE,
1065 base::Bind(&SleepFunc, &order, 4, TimeDelta::FromMilliseconds(50)));
[email protected]b224f792011-04-20 16:02:231066 MessageLoop::current()->PostTask(FROM_HERE,
1067 base::Bind(&OrderedFunc, &order, 5));
[email protected]c4280a92009-06-25 19:23:111068 if (use_delayed) {
[email protected]b224f792011-04-20 16:02:231069 MessageLoop::current()->PostNonNestableDelayedTask(
1070 FROM_HERE,
1071 base::Bind(&QuitFunc, &order, 6),
[email protected]5761ab9c2012-02-04 16:44:531072 TimeDelta::FromMilliseconds(2));
[email protected]c4280a92009-06-25 19:23:111073 } else {
[email protected]b224f792011-04-20 16:02:231074 MessageLoop::current()->PostNonNestableTask(
1075 FROM_HERE,
1076 base::Bind(&QuitFunc, &order, 6));
[email protected]c4280a92009-06-25 19:23:111077 }
initial.commitd7cae122008-07-26 21:49:381078
initial.commitd7cae122008-07-26 21:49:381079 MessageLoop::current()->Run();
1080
1081 // FIFO order.
[email protected]b224f792011-04-20 16:02:231082 ASSERT_EQ(12U, order.Size());
1083 EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true));
[email protected]8e937c1e2012-06-28 22:57:301084 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 3, true));
1085 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 3, false));
[email protected]b224f792011-04-20 16:02:231086 EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true));
1087 EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false));
[email protected]8e937c1e2012-06-28 22:57:301088 EXPECT_EQ(order.Get(5), TaskItem(ORDERED, 5, true));
1089 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 5, false));
[email protected]b224f792011-04-20 16:02:231090 EXPECT_EQ(order.Get(7), TaskItem(PUMPS, 1, false));
[email protected]8e937c1e2012-06-28 22:57:301091 EXPECT_EQ(order.Get(8), TaskItem(ORDERED, 2, true));
1092 EXPECT_EQ(order.Get(9), TaskItem(ORDERED, 2, false));
[email protected]b224f792011-04-20 16:02:231093 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 6, true));
1094 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 6, false));
initial.commitd7cae122008-07-26 21:49:381095}
1096
[email protected]8e937c1e2012-06-28 22:57:301097// Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
1098void RunTest_QuitNow(MessageLoop::Type message_loop_type) {
1099 MessageLoop loop(message_loop_type);
1100
1101 TaskList order;
1102
1103 base::RunLoop run_loop;
1104
1105 MessageLoop::current()->PostTask(FROM_HERE,
1106 base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&run_loop)));
1107 MessageLoop::current()->PostTask(
1108 FROM_HERE, base::Bind(&OrderedFunc, &order, 2));
1109 MessageLoop::current()->PostTask(
1110 FROM_HERE, base::Bind(&FuncThatQuitsNow));
1111 MessageLoop::current()->PostTask(
1112 FROM_HERE, base::Bind(&OrderedFunc, &order, 3));
1113 MessageLoop::current()->PostTask(
1114 FROM_HERE, base::Bind(&FuncThatQuitsNow));
1115 MessageLoop::current()->PostTask(
1116 FROM_HERE, base::Bind(&OrderedFunc, &order, 4)); // never runs
1117
1118 MessageLoop::current()->Run();
1119
1120 ASSERT_EQ(6U, order.Size());
1121 int task_index = 0;
1122 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1123 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
1124 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
1125 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1126 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true));
1127 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false));
1128 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1129}
1130
1131// Tests RunLoopQuit works before RunWithID.
1132void RunTest_RunLoopQuitOrderBefore(MessageLoop::Type message_loop_type) {
1133 MessageLoop loop(message_loop_type);
1134
1135 TaskList order;
1136
1137 base::RunLoop run_loop;
1138
1139 run_loop.Quit();
1140
1141 MessageLoop::current()->PostTask(
1142 FROM_HERE, base::Bind(&OrderedFunc, &order, 1)); // never runs
1143 MessageLoop::current()->PostTask(
1144 FROM_HERE, base::Bind(&FuncThatQuitsNow)); // never runs
1145
1146 run_loop.Run();
1147
1148 ASSERT_EQ(0U, order.Size());
1149}
1150
1151// Tests RunLoopQuit works during RunWithID.
1152void RunTest_RunLoopQuitOrderDuring(MessageLoop::Type message_loop_type) {
1153 MessageLoop loop(message_loop_type);
1154
1155 TaskList order;
1156
1157 base::RunLoop run_loop;
1158
1159 MessageLoop::current()->PostTask(
1160 FROM_HERE, base::Bind(&OrderedFunc, &order, 1));
1161 MessageLoop::current()->PostTask(
1162 FROM_HERE, run_loop.QuitClosure());
1163 MessageLoop::current()->PostTask(
1164 FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); // never runs
1165 MessageLoop::current()->PostTask(
1166 FROM_HERE, base::Bind(&FuncThatQuitsNow)); // never runs
1167
1168 run_loop.Run();
1169
1170 ASSERT_EQ(2U, order.Size());
1171 int task_index = 0;
1172 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, true));
1173 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, false));
1174 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1175}
1176
1177// Tests RunLoopQuit works after RunWithID.
1178void RunTest_RunLoopQuitOrderAfter(MessageLoop::Type message_loop_type) {
1179 MessageLoop loop(message_loop_type);
1180
1181 TaskList order;
1182
1183 base::RunLoop run_loop;
1184
1185 MessageLoop::current()->PostTask(FROM_HERE,
1186 base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&run_loop)));
1187 MessageLoop::current()->PostTask(
1188 FROM_HERE, base::Bind(&OrderedFunc, &order, 2));
1189 MessageLoop::current()->PostTask(
1190 FROM_HERE, base::Bind(&FuncThatQuitsNow));
1191 MessageLoop::current()->PostTask(
1192 FROM_HERE, base::Bind(&OrderedFunc, &order, 3));
1193 MessageLoop::current()->PostTask(
1194 FROM_HERE, run_loop.QuitClosure()); // has no affect
1195 MessageLoop::current()->PostTask(
1196 FROM_HERE, base::Bind(&OrderedFunc, &order, 4));
1197 MessageLoop::current()->PostTask(
1198 FROM_HERE, base::Bind(&FuncThatQuitsNow));
1199
1200 base::RunLoop outer_run_loop;
1201 outer_run_loop.Run();
1202
1203 ASSERT_EQ(8U, order.Size());
1204 int task_index = 0;
1205 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1206 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
1207 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
1208 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1209 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true));
1210 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false));
1211 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, true));
1212 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, false));
1213 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1214}
1215
1216// Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
1217void RunTest_RunLoopQuitTop(MessageLoop::Type message_loop_type) {
1218 MessageLoop loop(message_loop_type);
1219
1220 TaskList order;
1221
1222 base::RunLoop outer_run_loop;
1223 base::RunLoop nested_run_loop;
1224
1225 MessageLoop::current()->PostTask(FROM_HERE,
1226 base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_run_loop)));
1227 MessageLoop::current()->PostTask(
1228 FROM_HERE, outer_run_loop.QuitClosure());
1229 MessageLoop::current()->PostTask(
1230 FROM_HERE, base::Bind(&OrderedFunc, &order, 2));
1231 MessageLoop::current()->PostTask(
1232 FROM_HERE, nested_run_loop.QuitClosure());
1233
1234 outer_run_loop.Run();
1235
1236 ASSERT_EQ(4U, order.Size());
1237 int task_index = 0;
1238 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1239 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
1240 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
1241 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1242 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1243}
1244
1245// Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
1246void RunTest_RunLoopQuitNested(MessageLoop::Type message_loop_type) {
1247 MessageLoop loop(message_loop_type);
1248
1249 TaskList order;
1250
1251 base::RunLoop outer_run_loop;
1252 base::RunLoop nested_run_loop;
1253
1254 MessageLoop::current()->PostTask(FROM_HERE,
1255 base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_run_loop)));
1256 MessageLoop::current()->PostTask(
1257 FROM_HERE, nested_run_loop.QuitClosure());
1258 MessageLoop::current()->PostTask(
1259 FROM_HERE, base::Bind(&OrderedFunc, &order, 2));
1260 MessageLoop::current()->PostTask(
1261 FROM_HERE, outer_run_loop.QuitClosure());
1262
1263 outer_run_loop.Run();
1264
1265 ASSERT_EQ(4U, order.Size());
1266 int task_index = 0;
1267 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1268 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1269 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
1270 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
1271 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1272}
1273
1274// Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
1275void RunTest_RunLoopQuitBogus(MessageLoop::Type message_loop_type) {
1276 MessageLoop loop(message_loop_type);
1277
1278 TaskList order;
1279
1280 base::RunLoop outer_run_loop;
1281 base::RunLoop nested_run_loop;
1282 base::RunLoop bogus_run_loop;
1283
1284 MessageLoop::current()->PostTask(FROM_HERE,
1285 base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_run_loop)));
1286 MessageLoop::current()->PostTask(
1287 FROM_HERE, bogus_run_loop.QuitClosure());
1288 MessageLoop::current()->PostTask(
1289 FROM_HERE, base::Bind(&OrderedFunc, &order, 2));
1290 MessageLoop::current()->PostTask(
1291 FROM_HERE, outer_run_loop.QuitClosure());
1292 MessageLoop::current()->PostTask(
1293 FROM_HERE, nested_run_loop.QuitClosure());
1294
1295 outer_run_loop.Run();
1296
1297 ASSERT_EQ(4U, order.Size());
1298 int task_index = 0;
1299 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1300 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
1301 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
1302 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1303 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1304}
1305
1306// Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
1307void RunTest_RunLoopQuitDeep(MessageLoop::Type message_loop_type) {
1308 MessageLoop loop(message_loop_type);
1309
1310 TaskList order;
1311
1312 base::RunLoop outer_run_loop;
1313 base::RunLoop nested_loop1;
1314 base::RunLoop nested_loop2;
1315 base::RunLoop nested_loop3;
1316 base::RunLoop nested_loop4;
1317
1318 MessageLoop::current()->PostTask(FROM_HERE,
1319 base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_loop1)));
1320 MessageLoop::current()->PostTask(FROM_HERE,
1321 base::Bind(&FuncThatRuns, &order, 2, base::Unretained(&nested_loop2)));
1322 MessageLoop::current()->PostTask(FROM_HERE,
1323 base::Bind(&FuncThatRuns, &order, 3, base::Unretained(&nested_loop3)));
1324 MessageLoop::current()->PostTask(FROM_HERE,
1325 base::Bind(&FuncThatRuns, &order, 4, base::Unretained(&nested_loop4)));
1326 MessageLoop::current()->PostTask(
1327 FROM_HERE, base::Bind(&OrderedFunc, &order, 5));
1328 MessageLoop::current()->PostTask(
1329 FROM_HERE, outer_run_loop.QuitClosure());
1330 MessageLoop::current()->PostTask(
1331 FROM_HERE, base::Bind(&OrderedFunc, &order, 6));
1332 MessageLoop::current()->PostTask(
1333 FROM_HERE, nested_loop1.QuitClosure());
1334 MessageLoop::current()->PostTask(
1335 FROM_HERE, base::Bind(&OrderedFunc, &order, 7));
1336 MessageLoop::current()->PostTask(
1337 FROM_HERE, nested_loop2.QuitClosure());
1338 MessageLoop::current()->PostTask(
1339 FROM_HERE, base::Bind(&OrderedFunc, &order, 8));
1340 MessageLoop::current()->PostTask(
1341 FROM_HERE, nested_loop3.QuitClosure());
1342 MessageLoop::current()->PostTask(
1343 FROM_HERE, base::Bind(&OrderedFunc, &order, 9));
1344 MessageLoop::current()->PostTask(
1345 FROM_HERE, nested_loop4.QuitClosure());
1346 MessageLoop::current()->PostTask(
1347 FROM_HERE, base::Bind(&OrderedFunc, &order, 10));
1348
1349 outer_run_loop.Run();
1350
1351 ASSERT_EQ(18U, order.Size());
1352 int task_index = 0;
1353 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1354 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, true));
1355 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, true));
1356 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, true));
1357 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 5, true));
1358 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 5, false));
1359 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 6, true));
1360 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 6, false));
1361 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 7, true));
1362 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 7, false));
1363 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 8, true));
1364 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 8, false));
1365 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 9, true));
1366 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 9, false));
1367 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, false));
1368 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, false));
1369 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, false));
1370 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1371 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1372}
1373
[email protected]295039bd2008-08-15 04:32:571374#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381375
[email protected]4d9bdfaf2008-08-26 05:53:571376class DispatcherImpl : public MessageLoopForUI::Dispatcher {
initial.commitd7cae122008-07-26 21:49:381377 public:
1378 DispatcherImpl() : dispatch_count_(0) {}
1379
[email protected]046460d2012-04-09 19:50:061380 virtual bool Dispatch(const base::NativeEvent& msg) OVERRIDE {
initial.commitd7cae122008-07-26 21:49:381381 ::TranslateMessage(&msg);
1382 ::DispatchMessage(&msg);
[email protected]6aa4a1c02010-01-15 18:49:581383 // Do not count WM_TIMER since it is not what we post and it will cause
1384 // flakiness.
1385 if (msg.message != WM_TIMER)
1386 ++dispatch_count_;
1387 // We treat WM_LBUTTONUP as the last message.
1388 return msg.message != WM_LBUTTONUP;
initial.commitd7cae122008-07-26 21:49:381389 }
1390
1391 int dispatch_count_;
1392};
1393
[email protected]b224f792011-04-20 16:02:231394void MouseDownUp() {
1395 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
1396 PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
1397}
1398
[email protected]4d9bdfaf2008-08-26 05:53:571399void RunTest_Dispatcher(MessageLoop::Type message_loop_type) {
1400 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:381401
[email protected]5761ab9c2012-02-04 16:44:531402 MessageLoop::current()->PostDelayedTask(
1403 FROM_HERE,
1404 base::Bind(&MouseDownUp),
1405 TimeDelta::FromMilliseconds(100));
initial.commitd7cae122008-07-26 21:49:381406 DispatcherImpl dispatcher;
[email protected]8e937c1e2012-06-28 22:57:301407 base::RunLoop run_loop(&dispatcher);
1408 run_loop.Run();
initial.commitd7cae122008-07-26 21:49:381409 ASSERT_EQ(2, dispatcher.dispatch_count_);
1410}
[email protected]295039bd2008-08-15 04:32:571411
[email protected]6aa4a1c02010-01-15 18:49:581412LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) {
1413 if (code == base::MessagePumpForUI::kMessageFilterCode) {
1414 MSG* msg = reinterpret_cast<MSG*>(lparam);
1415 if (msg->message == WM_LBUTTONDOWN)
1416 return TRUE;
1417 }
1418 return FALSE;
1419}
1420
1421void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) {
1422 MessageLoop loop(message_loop_type);
1423
[email protected]5761ab9c2012-02-04 16:44:531424 MessageLoop::current()->PostDelayedTask(
1425 FROM_HERE,
1426 base::Bind(&MouseDownUp),
1427 TimeDelta::FromMilliseconds(100));
[email protected]6aa4a1c02010-01-15 18:49:581428 HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER,
1429 MsgFilterProc,
1430 NULL,
1431 GetCurrentThreadId());
1432 DispatcherImpl dispatcher;
[email protected]8e937c1e2012-06-28 22:57:301433 base::RunLoop run_loop(&dispatcher);
1434 run_loop.Run();
[email protected]6aa4a1c02010-01-15 18:49:581435 ASSERT_EQ(1, dispatcher.dispatch_count_);
1436 UnhookWindowsHookEx(msg_hook);
1437}
1438
[email protected]32cda29d2008-10-09 23:58:431439class TestIOHandler : public MessageLoopForIO::IOHandler {
1440 public:
[email protected]17b89142008-11-07 21:52:151441 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait);
[email protected]32cda29d2008-10-09 23:58:431442
[email protected]17b89142008-11-07 21:52:151443 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
1444 DWORD bytes_transfered, DWORD error);
[email protected]32cda29d2008-10-09 23:58:431445
[email protected]17b89142008-11-07 21:52:151446 void Init();
1447 void WaitForIO();
1448 OVERLAPPED* context() { return &context_.overlapped; }
[email protected]32cda29d2008-10-09 23:58:431449 DWORD size() { return sizeof(buffer_); }
1450
1451 private:
1452 char buffer_[48];
[email protected]17b89142008-11-07 21:52:151453 MessageLoopForIO::IOContext context_;
[email protected]32cda29d2008-10-09 23:58:431454 HANDLE signal_;
[email protected]b90d7e802011-01-09 16:32:201455 base::win::ScopedHandle file_;
[email protected]17b89142008-11-07 21:52:151456 bool wait_;
[email protected]32cda29d2008-10-09 23:58:431457};
1458
[email protected]17b89142008-11-07 21:52:151459TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait)
1460 : signal_(signal), wait_(wait) {
[email protected]32cda29d2008-10-09 23:58:431461 memset(buffer_, 0, sizeof(buffer_));
1462 memset(&context_, 0, sizeof(context_));
[email protected]17b89142008-11-07 21:52:151463 context_.handler = this;
[email protected]32cda29d2008-10-09 23:58:431464
1465 file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1466 FILE_FLAG_OVERLAPPED, NULL));
1467 EXPECT_TRUE(file_.IsValid());
1468}
1469
[email protected]17b89142008-11-07 21:52:151470void TestIOHandler::Init() {
1471 MessageLoopForIO::current()->RegisterIOHandler(file_, this);
1472
1473 DWORD read;
1474 EXPECT_FALSE(ReadFile(file_, buffer_, size(), &read, context()));
1475 EXPECT_EQ(ERROR_IO_PENDING, GetLastError());
1476 if (wait_)
1477 WaitForIO();
1478}
1479
1480void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext* context,
1481 DWORD bytes_transfered, DWORD error) {
[email protected]32cda29d2008-10-09 23:58:431482 ASSERT_TRUE(context == &context_);
[email protected]32cda29d2008-10-09 23:58:431483 ASSERT_TRUE(SetEvent(signal_));
1484}
1485
[email protected]17b89142008-11-07 21:52:151486void TestIOHandler::WaitForIO() {
1487 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this));
1488 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this));
1489}
1490
[email protected]32cda29d2008-10-09 23:58:431491void RunTest_IOHandler() {
[email protected]b90d7e802011-01-09 16:32:201492 base::win::ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL));
[email protected]32cda29d2008-10-09 23:58:431493 ASSERT_TRUE(callback_called.IsValid());
1494
1495 const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe";
[email protected]b90d7e802011-01-09 16:32:201496 base::win::ScopedHandle server(
1497 CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
[email protected]32cda29d2008-10-09 23:58:431498 ASSERT_TRUE(server.IsValid());
1499
1500 Thread thread("IOHandler test");
1501 Thread::Options options;
1502 options.message_loop_type = MessageLoop::TYPE_IO;
1503 ASSERT_TRUE(thread.StartWithOptions(options));
1504
1505 MessageLoop* thread_loop = thread.message_loop();
1506 ASSERT_TRUE(NULL != thread_loop);
1507
[email protected]17b89142008-11-07 21:52:151508 TestIOHandler handler(kPipeName, callback_called, false);
[email protected]b224f792011-04-20 16:02:231509 thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init,
1510 base::Unretained(&handler)));
[email protected]a1b75b942011-12-31 22:53:511511 // Make sure the thread runs and sleeps for lack of work.
[email protected]5761ab9c2012-02-04 16:44:531512 base::PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
[email protected]32cda29d2008-10-09 23:58:431513
1514 const char buffer[] = "Hello there!";
1515 DWORD written;
1516 EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL));
1517
1518 DWORD result = WaitForSingleObject(callback_called, 1000);
1519 EXPECT_EQ(WAIT_OBJECT_0, result);
1520
1521 thread.Stop();
1522}
1523
[email protected]17b89142008-11-07 21:52:151524void RunTest_WaitForIO() {
[email protected]b90d7e802011-01-09 16:32:201525 base::win::ScopedHandle callback1_called(
1526 CreateEvent(NULL, TRUE, FALSE, NULL));
1527 base::win::ScopedHandle callback2_called(
1528 CreateEvent(NULL, TRUE, FALSE, NULL));
[email protected]17b89142008-11-07 21:52:151529 ASSERT_TRUE(callback1_called.IsValid());
1530 ASSERT_TRUE(callback2_called.IsValid());
1531
1532 const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1";
1533 const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2";
[email protected]b90d7e802011-01-09 16:32:201534 base::win::ScopedHandle server1(
1535 CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
1536 base::win::ScopedHandle server2(
1537 CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
[email protected]17b89142008-11-07 21:52:151538 ASSERT_TRUE(server1.IsValid());
1539 ASSERT_TRUE(server2.IsValid());
1540
1541 Thread thread("IOHandler test");
1542 Thread::Options options;
1543 options.message_loop_type = MessageLoop::TYPE_IO;
1544 ASSERT_TRUE(thread.StartWithOptions(options));
1545
1546 MessageLoop* thread_loop = thread.message_loop();
1547 ASSERT_TRUE(NULL != thread_loop);
1548
1549 TestIOHandler handler1(kPipeName1, callback1_called, false);
1550 TestIOHandler handler2(kPipeName2, callback2_called, true);
[email protected]b224f792011-04-20 16:02:231551 thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init,
1552 base::Unretained(&handler1)));
1553 // TODO(ajwong): Do we really need such long Sleeps in ths function?
[email protected]a1b75b942011-12-31 22:53:511554 // Make sure the thread runs and sleeps for lack of work.
[email protected]5761ab9c2012-02-04 16:44:531555 TimeDelta delay = TimeDelta::FromMilliseconds(100);
[email protected]a1b75b942011-12-31 22:53:511556 base::PlatformThread::Sleep(delay);
[email protected]b224f792011-04-20 16:02:231557 thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init,
1558 base::Unretained(&handler2)));
[email protected]a1b75b942011-12-31 22:53:511559 base::PlatformThread::Sleep(delay);
[email protected]17b89142008-11-07 21:52:151560
1561 // At this time handler1 is waiting to be called, and the thread is waiting
1562 // on the Init method of handler2, filtering only handler2 callbacks.
1563
1564 const char buffer[] = "Hello there!";
1565 DWORD written;
1566 EXPECT_TRUE(WriteFile(server1, buffer, sizeof(buffer), &written, NULL));
[email protected]a1b75b942011-12-31 22:53:511567 base::PlatformThread::Sleep(2 * delay);
[email protected]17b89142008-11-07 21:52:151568 EXPECT_EQ(WAIT_TIMEOUT, WaitForSingleObject(callback1_called, 0)) <<
1569 "handler1 has not been called";
1570
1571 EXPECT_TRUE(WriteFile(server2, buffer, sizeof(buffer), &written, NULL));
1572
1573 HANDLE objects[2] = { callback1_called.Get(), callback2_called.Get() };
1574 DWORD result = WaitForMultipleObjects(2, objects, TRUE, 1000);
1575 EXPECT_EQ(WAIT_OBJECT_0, result);
1576
1577 thread.Stop();
1578}
1579
[email protected]4d9bdfaf2008-08-26 05:53:571580#endif // defined(OS_WIN)
license.botbf09a502008-08-24 00:55:551581
[email protected]4d9bdfaf2008-08-26 05:53:571582} // namespace
1583
1584//-----------------------------------------------------------------------------
1585// Each test is run against each type of MessageLoop. That way we are sure
1586// that message loops work properly in all configurations. Of course, in some
1587// cases, a unit test may only be for a particular type of loop.
1588
1589TEST(MessageLoopTest, PostTask) {
1590 RunTest_PostTask(MessageLoop::TYPE_DEFAULT);
1591 RunTest_PostTask(MessageLoop::TYPE_UI);
1592 RunTest_PostTask(MessageLoop::TYPE_IO);
1593}
1594
1595TEST(MessageLoopTest, PostTask_SEH) {
1596 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT);
1597 RunTest_PostTask_SEH(MessageLoop::TYPE_UI);
1598 RunTest_PostTask_SEH(MessageLoop::TYPE_IO);
1599}
1600
[email protected]752578562008-09-07 08:08:291601TEST(MessageLoopTest, PostDelayedTask_Basic) {
1602 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT);
1603 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI);
1604 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO);
1605}
1606
1607TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) {
1608 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT);
1609 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI);
1610 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO);
1611}
1612
1613TEST(MessageLoopTest, PostDelayedTask_InPostOrder) {
1614 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT);
1615 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI);
1616 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO);
1617}
1618
1619TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) {
1620 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT);
1621 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI);
1622 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO);
1623}
1624
1625TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) {
1626 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT);
1627 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI);
1628 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO);
1629}
1630
[email protected]72deacd2008-09-23 19:19:201631TEST(MessageLoopTest, PostDelayedTask_SharedTimer) {
1632 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT);
1633 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI);
1634 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO);
1635}
1636
1637#if defined(OS_WIN)
1638TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) {
1639 RunTest_PostDelayedTask_SharedTimer_SubPump();
1640}
1641#endif
1642
[email protected]5affb7e2010-05-25 20:13:361643// TODO(darin): MessageLoop does not support deleting all tasks in the
1644// destructor.
[email protected]2f861b32010-07-26 21:23:181645// Fails, http://crbug.com/50272.
[email protected]b224f792011-04-20 16:02:231646TEST(MessageLoopTest, FAILS_EnsureDeletion) {
1647 RunTest_EnsureDeletion(MessageLoop::TYPE_DEFAULT);
1648 RunTest_EnsureDeletion(MessageLoop::TYPE_UI);
1649 RunTest_EnsureDeletion(MessageLoop::TYPE_IO);
[email protected]001747c2008-09-10 00:37:071650}
1651
[email protected]5affb7e2010-05-25 20:13:361652// TODO(darin): MessageLoop does not support deleting all tasks in the
1653// destructor.
[email protected]2f861b32010-07-26 21:23:181654// Fails, http://crbug.com/50272.
[email protected]b224f792011-04-20 16:02:231655TEST(MessageLoopTest, FAILS_EnsureDeletion_Chain) {
1656 RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_DEFAULT);
1657 RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_UI);
1658 RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_IO);
[email protected]001747c2008-09-10 00:37:071659}
1660
[email protected]4d9bdfaf2008-08-26 05:53:571661#if defined(OS_WIN)
1662TEST(MessageLoopTest, Crasher) {
1663 RunTest_Crasher(MessageLoop::TYPE_DEFAULT);
1664 RunTest_Crasher(MessageLoop::TYPE_UI);
1665 RunTest_Crasher(MessageLoop::TYPE_IO);
1666}
1667
1668TEST(MessageLoopTest, CrasherNasty) {
1669 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT);
1670 RunTest_CrasherNasty(MessageLoop::TYPE_UI);
1671 RunTest_CrasherNasty(MessageLoop::TYPE_IO);
1672}
1673#endif // defined(OS_WIN)
1674
1675TEST(MessageLoopTest, Nesting) {
1676 RunTest_Nesting(MessageLoop::TYPE_DEFAULT);
1677 RunTest_Nesting(MessageLoop::TYPE_UI);
1678 RunTest_Nesting(MessageLoop::TYPE_IO);
1679}
1680
1681TEST(MessageLoopTest, RecursiveDenial1) {
1682 RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT);
1683 RunTest_RecursiveDenial1(MessageLoop::TYPE_UI);
1684 RunTest_RecursiveDenial1(MessageLoop::TYPE_IO);
1685}
1686
[email protected]4554c232011-02-17 19:25:041687TEST(MessageLoopTest, RecursiveDenial3) {
1688 RunTest_RecursiveDenial3(MessageLoop::TYPE_DEFAULT);
1689 RunTest_RecursiveDenial3(MessageLoop::TYPE_UI);
1690 RunTest_RecursiveDenial3(MessageLoop::TYPE_IO);
1691}
1692
[email protected]4d9bdfaf2008-08-26 05:53:571693TEST(MessageLoopTest, RecursiveSupport1) {
1694 RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT);
1695 RunTest_RecursiveSupport1(MessageLoop::TYPE_UI);
1696 RunTest_RecursiveSupport1(MessageLoop::TYPE_IO);
1697}
1698
1699#if defined(OS_WIN)
[email protected]85f39f82010-05-19 14:33:441700// This test occasionally hangs http://crbug.com/44567
1701TEST(MessageLoopTest, DISABLED_RecursiveDenial2) {
[email protected]4d9bdfaf2008-08-26 05:53:571702 RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT);
1703 RunTest_RecursiveDenial2(MessageLoop::TYPE_UI);
1704 RunTest_RecursiveDenial2(MessageLoop::TYPE_IO);
1705}
1706
1707TEST(MessageLoopTest, RecursiveSupport2) {
1708 // This test requires a UI loop
1709 RunTest_RecursiveSupport2(MessageLoop::TYPE_UI);
1710}
1711#endif // defined(OS_WIN)
1712
1713TEST(MessageLoopTest, NonNestableWithNoNesting) {
1714 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT);
1715 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI);
1716 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO);
1717}
1718
1719TEST(MessageLoopTest, NonNestableInNestedLoop) {
[email protected]c4280a92009-06-25 19:23:111720 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false);
1721 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false);
1722 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false);
1723}
1724
1725TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) {
1726 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true);
1727 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true);
1728 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true);
[email protected]4d9bdfaf2008-08-26 05:53:571729}
1730
[email protected]8e937c1e2012-06-28 22:57:301731TEST(MessageLoopTest, QuitNow) {
1732 RunTest_QuitNow(MessageLoop::TYPE_DEFAULT);
1733 RunTest_QuitNow(MessageLoop::TYPE_UI);
1734 RunTest_QuitNow(MessageLoop::TYPE_IO);
1735}
1736
1737TEST(MessageLoopTest, RunLoopQuitTop) {
1738 RunTest_RunLoopQuitTop(MessageLoop::TYPE_DEFAULT);
1739 RunTest_RunLoopQuitTop(MessageLoop::TYPE_UI);
1740 RunTest_RunLoopQuitTop(MessageLoop::TYPE_IO);
1741}
1742
1743TEST(MessageLoopTest, RunLoopQuitNested) {
1744 RunTest_RunLoopQuitNested(MessageLoop::TYPE_DEFAULT);
1745 RunTest_RunLoopQuitNested(MessageLoop::TYPE_UI);
1746 RunTest_RunLoopQuitNested(MessageLoop::TYPE_IO);
1747}
1748
1749TEST(MessageLoopTest, RunLoopQuitBogus) {
1750 RunTest_RunLoopQuitBogus(MessageLoop::TYPE_DEFAULT);
1751 RunTest_RunLoopQuitBogus(MessageLoop::TYPE_UI);
1752 RunTest_RunLoopQuitBogus(MessageLoop::TYPE_IO);
1753}
1754
1755TEST(MessageLoopTest, RunLoopQuitDeep) {
1756 RunTest_RunLoopQuitDeep(MessageLoop::TYPE_DEFAULT);
1757 RunTest_RunLoopQuitDeep(MessageLoop::TYPE_UI);
1758 RunTest_RunLoopQuitDeep(MessageLoop::TYPE_IO);
1759}
1760
1761TEST(MessageLoopTest, RunLoopQuitOrderBefore) {
1762 RunTest_RunLoopQuitOrderBefore(MessageLoop::TYPE_DEFAULT);
1763 RunTest_RunLoopQuitOrderBefore(MessageLoop::TYPE_UI);
1764 RunTest_RunLoopQuitOrderBefore(MessageLoop::TYPE_IO);
1765}
1766
1767TEST(MessageLoopTest, RunLoopQuitOrderDuring) {
1768 RunTest_RunLoopQuitOrderDuring(MessageLoop::TYPE_DEFAULT);
1769 RunTest_RunLoopQuitOrderDuring(MessageLoop::TYPE_UI);
1770 RunTest_RunLoopQuitOrderDuring(MessageLoop::TYPE_IO);
1771}
1772
1773TEST(MessageLoopTest, RunLoopQuitOrderAfter) {
1774 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_DEFAULT);
1775 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_UI);
1776 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_IO);
1777}
1778
[email protected]b224f792011-04-20 16:02:231779void PostNTasksThenQuit(int posts_remaining) {
1780 if (posts_remaining > 1) {
1781 MessageLoop::current()->PostTask(
1782 FROM_HERE,
1783 base::Bind(&PostNTasksThenQuit, posts_remaining - 1));
1784 } else {
1785 MessageLoop::current()->Quit();
[email protected]9cfb89a2010-06-09 21:20:411786 }
[email protected]b224f792011-04-20 16:02:231787}
[email protected]9cfb89a2010-06-09 21:20:411788
1789class DummyTaskObserver : public MessageLoop::TaskObserver {
1790 public:
[email protected]b7243c42010-07-23 05:23:131791 explicit DummyTaskObserver(int num_tasks)
[email protected]9cfb89a2010-06-09 21:20:411792 : num_tasks_started_(0),
1793 num_tasks_processed_(0),
1794 num_tasks_(num_tasks) {}
1795
1796 virtual ~DummyTaskObserver() {}
1797
[email protected]b224f792011-04-20 16:02:231798 virtual void WillProcessTask(TimeTicks time_posted) OVERRIDE {
[email protected]9cfb89a2010-06-09 21:20:411799 num_tasks_started_++;
[email protected]b224f792011-04-20 16:02:231800 EXPECT_TRUE(time_posted != TimeTicks());
[email protected]9cfb89a2010-06-09 21:20:411801 EXPECT_LE(num_tasks_started_, num_tasks_);
1802 EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1);
1803 }
1804
[email protected]b224f792011-04-20 16:02:231805 virtual void DidProcessTask(TimeTicks time_posted) OVERRIDE {
[email protected]9cfb89a2010-06-09 21:20:411806 num_tasks_processed_++;
[email protected]b224f792011-04-20 16:02:231807 EXPECT_TRUE(time_posted != TimeTicks());
[email protected]9cfb89a2010-06-09 21:20:411808 EXPECT_LE(num_tasks_started_, num_tasks_);
1809 EXPECT_EQ(num_tasks_started_, num_tasks_processed_);
1810 }
1811
1812 int num_tasks_started() const { return num_tasks_started_; }
1813 int num_tasks_processed() const { return num_tasks_processed_; }
1814
1815 private:
1816 int num_tasks_started_;
1817 int num_tasks_processed_;
1818 const int num_tasks_;
1819
1820 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver);
1821};
1822
1823TEST(MessageLoopTest, TaskObserver) {
[email protected]b224f792011-04-20 16:02:231824 const int kNumPosts = 6;
1825 DummyTaskObserver observer(kNumPosts);
[email protected]9cfb89a2010-06-09 21:20:411826
1827 MessageLoop loop;
1828 loop.AddTaskObserver(&observer);
[email protected]b224f792011-04-20 16:02:231829 loop.PostTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, kNumPosts));
[email protected]9cfb89a2010-06-09 21:20:411830 loop.Run();
1831 loop.RemoveTaskObserver(&observer);
1832
[email protected]b224f792011-04-20 16:02:231833 EXPECT_EQ(kNumPosts, observer.num_tasks_started());
1834 EXPECT_EQ(kNumPosts, observer.num_tasks_processed());
[email protected]9cfb89a2010-06-09 21:20:411835}
1836
[email protected]4d9bdfaf2008-08-26 05:53:571837#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:571838TEST(MessageLoopTest, Dispatcher) {
1839 // This test requires a UI loop
1840 RunTest_Dispatcher(MessageLoop::TYPE_UI);
1841}
[email protected]32cda29d2008-10-09 23:58:431842
[email protected]6aa4a1c02010-01-15 18:49:581843TEST(MessageLoopTest, DispatcherWithMessageHook) {
1844 // This test requires a UI loop
1845 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI);
1846}
1847
[email protected]32cda29d2008-10-09 23:58:431848TEST(MessageLoopTest, IOHandler) {
1849 RunTest_IOHandler();
1850}
[email protected]17b89142008-11-07 21:52:151851
1852TEST(MessageLoopTest, WaitForIO) {
1853 RunTest_WaitForIO();
1854}
[email protected]57f030a2010-06-29 04:58:151855
1856TEST(MessageLoopTest, HighResolutionTimer) {
1857 MessageLoop loop;
1858
[email protected]5761ab9c2012-02-04 16:44:531859 const TimeDelta kFastTimer = TimeDelta::FromMilliseconds(5);
1860 const TimeDelta kSlowTimer = TimeDelta::FromMilliseconds(100);
[email protected]57f030a2010-06-29 04:58:151861
[email protected]515f2492011-01-14 10:36:281862 EXPECT_FALSE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151863
1864 // Post a fast task to enable the high resolution timers.
[email protected]b224f792011-04-20 16:02:231865 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
[email protected]5761ab9c2012-02-04 16:44:531866 kFastTimer);
[email protected]57f030a2010-06-29 04:58:151867 loop.Run();
[email protected]515f2492011-01-14 10:36:281868 EXPECT_TRUE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151869
1870 // Post a slow task and verify high resolution timers
1871 // are still enabled.
[email protected]b224f792011-04-20 16:02:231872 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
[email protected]5761ab9c2012-02-04 16:44:531873 kSlowTimer);
[email protected]57f030a2010-06-29 04:58:151874 loop.Run();
[email protected]515f2492011-01-14 10:36:281875 EXPECT_TRUE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151876
1877 // Wait for a while so that high-resolution mode elapses.
[email protected]a1b75b942011-12-31 22:53:511878 base::PlatformThread::Sleep(TimeDelta::FromMilliseconds(
1879 MessageLoop::kHighResolutionTimerModeLeaseTimeMs));
[email protected]57f030a2010-06-29 04:58:151880
1881 // Post a slow task to disable the high resolution timers.
[email protected]b224f792011-04-20 16:02:231882 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
[email protected]5761ab9c2012-02-04 16:44:531883 kSlowTimer);
[email protected]57f030a2010-06-29 04:58:151884 loop.Run();
[email protected]515f2492011-01-14 10:36:281885 EXPECT_FALSE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151886}
1887
[email protected]4d9bdfaf2008-08-26 05:53:571888#endif // defined(OS_WIN)
[email protected]f74c8962009-04-22 20:01:361889
[email protected]5cffdfd2010-12-01 08:45:511890#if defined(OS_POSIX) && !defined(OS_NACL)
[email protected]f74c8962009-04-22 20:01:361891
1892namespace {
1893
[email protected]a27de262011-06-22 06:33:051894class QuitDelegate : public MessageLoopForIO::Watcher {
[email protected]f74c8962009-04-22 20:01:361895 public:
[email protected]44106182012-04-06 03:53:021896 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
[email protected]f74c8962009-04-22 20:01:361897 MessageLoop::current()->Quit();
1898 }
[email protected]44106182012-04-06 03:53:021899 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
[email protected]f74c8962009-04-22 20:01:361900 MessageLoop::current()->Quit();
1901 }
1902};
1903
[email protected]f45213772010-06-11 00:06:191904TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) {
[email protected]f74c8962009-04-22 20:01:361905 // Simulate a MessageLoop that dies before an FileDescriptorWatcher.
1906 // This could happen when people use the Singleton pattern or atexit.
[email protected]f74c8962009-04-22 20:01:361907
1908 // Create a file descriptor. Doesn't need to be readable or writable,
1909 // as we don't need to actually get any notifications.
1910 // pipe() is just the easiest way to do it.
1911 int pipefds[2];
1912 int err = pipe(pipefds);
[email protected]b7243c42010-07-23 05:23:131913 ASSERT_EQ(0, err);
[email protected]f74c8962009-04-22 20:01:361914 int fd = pipefds[1];
1915 {
1916 // Arrange for controller to live longer than message loop.
[email protected]a27de262011-06-22 06:33:051917 MessageLoopForIO::FileDescriptorWatcher controller;
[email protected]f74c8962009-04-22 20:01:361918 {
1919 MessageLoopForIO message_loop;
1920
1921 QuitDelegate delegate;
1922 message_loop.WatchFileDescriptor(fd,
1923 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1924 // and don't run the message loop, just destroy it.
1925 }
1926 }
[email protected]70eb6572010-06-23 00:37:461927 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1928 PLOG(ERROR) << "close";
1929 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1930 PLOG(ERROR) << "close";
[email protected]f74c8962009-04-22 20:01:361931}
1932
1933TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) {
1934 // Verify that it's ok to call StopWatchingFileDescriptor().
1935 // (Errors only showed up in valgrind.)
1936 int pipefds[2];
1937 int err = pipe(pipefds);
[email protected]b7243c42010-07-23 05:23:131938 ASSERT_EQ(0, err);
[email protected]f74c8962009-04-22 20:01:361939 int fd = pipefds[1];
1940 {
1941 // Arrange for message loop to live longer than controller.
1942 MessageLoopForIO message_loop;
1943 {
[email protected]a27de262011-06-22 06:33:051944 MessageLoopForIO::FileDescriptorWatcher controller;
[email protected]f74c8962009-04-22 20:01:361945
1946 QuitDelegate delegate;
1947 message_loop.WatchFileDescriptor(fd,
1948 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1949 controller.StopWatchingFileDescriptor();
1950 }
1951 }
[email protected]70eb6572010-06-23 00:37:461952 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1953 PLOG(ERROR) << "close";
1954 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1955 PLOG(ERROR) << "close";
[email protected]f74c8962009-04-22 20:01:361956}
1957
[email protected]9cfb89a2010-06-09 21:20:411958} // namespace
1959
[email protected]5cffdfd2010-12-01 08:45:511960#endif // defined(OS_POSIX) && !defined(OS_NACL)
[email protected]582384772010-11-30 00:25:291961
1962namespace {
[email protected]b224f792011-04-20 16:02:231963// Inject a test point for recording the destructor calls for Closure objects
1964// send to MessageLoop::PostTask(). It is awkward usage since we are trying to
1965// hook the actual destruction, which is not a common operation.
1966class DestructionObserverProbe :
1967 public base::RefCounted<DestructionObserverProbe> {
[email protected]582384772010-11-30 00:25:291968 public:
[email protected]b224f792011-04-20 16:02:231969 DestructionObserverProbe(bool* task_destroyed,
1970 bool* destruction_observer_called)
[email protected]582384772010-11-30 00:25:291971 : task_destroyed_(task_destroyed),
1972 destruction_observer_called_(destruction_observer_called) {
1973 }
[email protected]582384772010-11-30 00:25:291974 virtual void Run() {
1975 // This task should never run.
1976 ADD_FAILURE();
1977 }
1978 private:
[email protected]a9aaa9d12012-04-25 00:42:511979 friend class base::RefCounted<DestructionObserverProbe>;
1980
1981 virtual ~DestructionObserverProbe() {
1982 EXPECT_FALSE(*destruction_observer_called_);
1983 *task_destroyed_ = true;
1984 }
1985
[email protected]582384772010-11-30 00:25:291986 bool* task_destroyed_;
1987 bool* destruction_observer_called_;
1988};
1989
1990class MLDestructionObserver : public MessageLoop::DestructionObserver {
1991 public:
1992 MLDestructionObserver(bool* task_destroyed, bool* destruction_observer_called)
1993 : task_destroyed_(task_destroyed),
1994 destruction_observer_called_(destruction_observer_called),
1995 task_destroyed_before_message_loop_(false) {
1996 }
[email protected]44106182012-04-06 03:53:021997 virtual void WillDestroyCurrentMessageLoop() OVERRIDE {
[email protected]582384772010-11-30 00:25:291998 task_destroyed_before_message_loop_ = *task_destroyed_;
1999 *destruction_observer_called_ = true;
2000 }
2001 bool task_destroyed_before_message_loop() const {
2002 return task_destroyed_before_message_loop_;
2003 }
2004 private:
2005 bool* task_destroyed_;
2006 bool* destruction_observer_called_;
2007 bool task_destroyed_before_message_loop_;
2008};
2009
2010} // namespace
2011
2012TEST(MessageLoopTest, DestructionObserverTest) {
2013 // Verify that the destruction observer gets called at the very end (after
2014 // all the pending tasks have been destroyed).
2015 MessageLoop* loop = new MessageLoop;
[email protected]5761ab9c2012-02-04 16:44:532016 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
[email protected]582384772010-11-30 00:25:292017
2018 bool task_destroyed = false;
2019 bool destruction_observer_called = false;
2020
2021 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called);
2022 loop->AddDestructionObserver(&observer);
2023 loop->PostDelayedTask(
2024 FROM_HERE,
[email protected]b224f792011-04-20 16:02:232025 base::Bind(&DestructionObserverProbe::Run,
2026 new DestructionObserverProbe(&task_destroyed,
2027 &destruction_observer_called)),
[email protected]5761ab9c2012-02-04 16:44:532028 kDelay);
[email protected]582384772010-11-30 00:25:292029 delete loop;
2030 EXPECT_TRUE(observer.task_destroyed_before_message_loop());
2031 // The task should have been destroyed when we deleted the loop.
2032 EXPECT_TRUE(task_destroyed);
2033 EXPECT_TRUE(destruction_observer_called);
2034}
[email protected]4fc20d12012-05-09 20:16:142035
2036
2037// Verify that MessageLoop sets ThreadMainTaskRunner::current() and it
2038// posts tasks on that message loop.
2039TEST(MessageLoopTest, ThreadMainTaskRunner) {
2040 MessageLoop loop;
2041
2042 scoped_refptr<Foo> foo(new Foo());
2043 std::string a("a");
2044 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind(
2045 &Foo::Test1ConstRef, foo.get(), a));
2046
2047 // Post quit task;
2048 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
2049 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
2050
2051 // Now kick things off
2052 MessageLoop::current()->Run();
2053
2054 EXPECT_EQ(foo->test_count(), 1);
2055 EXPECT_EQ(foo->result(), "a");
2056}
[email protected]ab563ff2012-07-21 00:26:322057
2058TEST(MessageLoopTest, IsType) {
2059 MessageLoop loop(MessageLoop::TYPE_UI);
2060 EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI));
2061 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO));
2062 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT));
2063}