OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop_test.h" | 5 #include "base/message_loop/message_loop_test.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 std::unique_ptr<MessagePump> pump(factory()); | 371 std::unique_ptr<MessagePump> pump(factory()); |
372 MessageLoop loop(std::move(pump)); | 372 MessageLoop loop(std::move(pump)); |
373 | 373 |
374 int depth = 100; | 374 int depth = 100; |
375 MessageLoop::current()->PostTask(FROM_HERE, | 375 MessageLoop::current()->PostTask(FROM_HERE, |
376 Bind(&NestingFunc, &depth)); | 376 Bind(&NestingFunc, &depth)); |
377 MessageLoop::current()->Run(); | 377 MessageLoop::current()->Run(); |
378 EXPECT_EQ(depth, 0); | 378 EXPECT_EQ(depth, 0); |
379 } | 379 } |
380 | 380 |
| 381 // A NestingObserver that tracks the number of nested message loop starts it |
| 382 // has seen. |
| 383 class TestNestingObserver : public MessageLoop::NestingObserver { |
| 384 public: |
| 385 TestNestingObserver() {} |
| 386 ~TestNestingObserver() override {} |
| 387 |
| 388 int begin_nested_loop_count() const { return begin_nested_loop_count_; } |
| 389 |
| 390 // MessageLoop::NestingObserver: |
| 391 void OnBeginNestedMessageLoop() override { begin_nested_loop_count_++; } |
| 392 |
| 393 private: |
| 394 int begin_nested_loop_count_ = 0; |
| 395 |
| 396 DISALLOW_COPY_AND_ASSIGN(TestNestingObserver); |
| 397 }; |
| 398 |
| 399 void ExpectOneBeginNestedLoop(TestNestingObserver* observer) { |
| 400 EXPECT_EQ(1, observer->begin_nested_loop_count()); |
| 401 } |
| 402 |
| 403 // Starts a nested message loop. |
| 404 void RunNestedLoop(TestNestingObserver* observer, |
| 405 const Closure& quit_outer_loop) { |
| 406 // The nested loop hasn't started yet. |
| 407 EXPECT_EQ(0, observer->begin_nested_loop_count()); |
| 408 |
| 409 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 410 RunLoop nested_loop; |
| 411 // Verify that by the time the first task is run the observer has seen the |
| 412 // message loop begin. |
| 413 MessageLoop::current()->PostTask(FROM_HERE, |
| 414 Bind(&ExpectOneBeginNestedLoop, observer)); |
| 415 MessageLoop::current()->PostTask(FROM_HERE, nested_loop.QuitClosure()); |
| 416 nested_loop.Run(); |
| 417 |
| 418 // Quitting message loops doesn't change the begin count. |
| 419 EXPECT_EQ(1, observer->begin_nested_loop_count()); |
| 420 |
| 421 quit_outer_loop.Run(); |
| 422 } |
| 423 |
| 424 // Tests that a NestingObserver is notified when a nested message loop begins. |
| 425 void RunTest_NestingObserver(MessagePumpFactory factory) { |
| 426 std::unique_ptr<MessagePump> pump(factory()); |
| 427 MessageLoop outer_loop(std::move(pump)); |
| 428 |
| 429 // Observe the outer loop for nested message loops beginning. |
| 430 TestNestingObserver nesting_observer; |
| 431 outer_loop.AddNestingObserver(&nesting_observer); |
| 432 |
| 433 // Post a task that runs a nested message loop. |
| 434 outer_loop.PostTask(FROM_HERE, Bind(&RunNestedLoop, &nesting_observer, |
| 435 outer_loop.QuitWhenIdleClosure())); |
| 436 outer_loop.Run(); |
| 437 |
| 438 outer_loop.RemoveNestingObserver(&nesting_observer); |
| 439 } |
| 440 |
381 enum TaskType { | 441 enum TaskType { |
382 MESSAGEBOX, | 442 MESSAGEBOX, |
383 ENDDIALOG, | 443 ENDDIALOG, |
384 RECURSIVE, | 444 RECURSIVE, |
385 TIMEDMESSAGELOOP, | 445 TIMEDMESSAGELOOP, |
386 QUITMESSAGELOOP, | 446 QUITMESSAGELOOP, |
387 ORDERED, | 447 ORDERED, |
388 PUMPS, | 448 PUMPS, |
389 SLEEP, | 449 SLEEP, |
390 RUNS, | 450 RUNS, |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 void RunTest_RecursivePosts(MessagePumpFactory factory) { | 1071 void RunTest_RecursivePosts(MessagePumpFactory factory) { |
1012 const int kNumTimes = 1 << 17; | 1072 const int kNumTimes = 1 << 17; |
1013 std::unique_ptr<MessagePump> pump(factory()); | 1073 std::unique_ptr<MessagePump> pump(factory()); |
1014 MessageLoop loop(std::move(pump)); | 1074 MessageLoop loop(std::move(pump)); |
1015 loop.PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumTimes)); | 1075 loop.PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumTimes)); |
1016 loop.Run(); | 1076 loop.Run(); |
1017 } | 1077 } |
1018 | 1078 |
1019 } // namespace test | 1079 } // namespace test |
1020 } // namespace base | 1080 } // namespace base |
OLD | NEW |