| 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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 std::unique_ptr<MessagePump> pump(factory()); | 368 std::unique_ptr<MessagePump> pump(factory()); |
| 369 MessageLoop loop(std::move(pump)); | 369 MessageLoop loop(std::move(pump)); |
| 370 | 370 |
| 371 int depth = 100; | 371 int depth = 100; |
| 372 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 372 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 373 BindOnce(&NestingFunc, &depth)); | 373 BindOnce(&NestingFunc, &depth)); |
| 374 RunLoop().Run(); | 374 RunLoop().Run(); |
| 375 EXPECT_EQ(depth, 0); | 375 EXPECT_EQ(depth, 0); |
| 376 } | 376 } |
| 377 | 377 |
| 378 // A NestingObserver that tracks the number of nested message loop starts it | |
| 379 // has seen. | |
| 380 class TestNestingObserver : public MessageLoop::NestingObserver { | |
| 381 public: | |
| 382 TestNestingObserver() {} | |
| 383 ~TestNestingObserver() override {} | |
| 384 | |
| 385 int begin_nested_loop_count() const { return begin_nested_loop_count_; } | |
| 386 | |
| 387 // MessageLoop::NestingObserver: | |
| 388 void OnBeginNestedMessageLoop() override { begin_nested_loop_count_++; } | |
| 389 | |
| 390 private: | |
| 391 int begin_nested_loop_count_ = 0; | |
| 392 | |
| 393 DISALLOW_COPY_AND_ASSIGN(TestNestingObserver); | |
| 394 }; | |
| 395 | |
| 396 void ExpectOneBeginNestedLoop(TestNestingObserver* observer) { | |
| 397 EXPECT_EQ(1, observer->begin_nested_loop_count()); | |
| 398 } | |
| 399 | |
| 400 // Starts a nested message loop. | |
| 401 void RunNestedLoop(TestNestingObserver* observer, | |
| 402 const Closure& quit_outer_loop) { | |
| 403 // The nested loop hasn't started yet. | |
| 404 EXPECT_EQ(0, observer->begin_nested_loop_count()); | |
| 405 | |
| 406 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 407 RunLoop nested_loop; | |
| 408 // Verify that by the time the first task is run the observer has seen the | |
| 409 // message loop begin. | |
| 410 ThreadTaskRunnerHandle::Get()->PostTask( | |
| 411 FROM_HERE, BindOnce(&ExpectOneBeginNestedLoop, observer)); | |
| 412 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, nested_loop.QuitClosure()); | |
| 413 nested_loop.Run(); | |
| 414 | |
| 415 // Quitting message loops doesn't change the begin count. | |
| 416 EXPECT_EQ(1, observer->begin_nested_loop_count()); | |
| 417 | |
| 418 quit_outer_loop.Run(); | |
| 419 } | |
| 420 | |
| 421 // Tests that a NestingObserver is notified when a nested message loop begins. | |
| 422 void RunTest_NestingObserver(MessagePumpFactory factory) { | |
| 423 std::unique_ptr<MessagePump> pump(factory()); | |
| 424 MessageLoop outer_loop(std::move(pump)); | |
| 425 | |
| 426 // Observe the outer loop for nested message loops beginning. | |
| 427 TestNestingObserver nesting_observer; | |
| 428 outer_loop.AddNestingObserver(&nesting_observer); | |
| 429 | |
| 430 // Post a task that runs a nested message loop. | |
| 431 outer_loop.task_runner()->PostTask( | |
| 432 FROM_HERE, BindOnce(&RunNestedLoop, &nesting_observer, | |
| 433 outer_loop.QuitWhenIdleClosure())); | |
| 434 RunLoop().Run(); | |
| 435 | |
| 436 outer_loop.RemoveNestingObserver(&nesting_observer); | |
| 437 } | |
| 438 | |
| 439 enum TaskType { | 378 enum TaskType { |
| 440 MESSAGEBOX, | 379 MESSAGEBOX, |
| 441 ENDDIALOG, | 380 ENDDIALOG, |
| 442 RECURSIVE, | 381 RECURSIVE, |
| 443 TIMEDMESSAGELOOP, | 382 TIMEDMESSAGELOOP, |
| 444 QUITMESSAGELOOP, | 383 QUITMESSAGELOOP, |
| 445 ORDERED, | 384 ORDERED, |
| 446 PUMPS, | 385 PUMPS, |
| 447 SLEEP, | 386 SLEEP, |
| 448 RUNS, | 387 RUNS, |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1047 const int kNumTimes = 1 << 17; | 986 const int kNumTimes = 1 << 17; |
| 1048 std::unique_ptr<MessagePump> pump(factory()); | 987 std::unique_ptr<MessagePump> pump(factory()); |
| 1049 MessageLoop loop(std::move(pump)); | 988 MessageLoop loop(std::move(pump)); |
| 1050 loop.task_runner()->PostTask(FROM_HERE, | 989 loop.task_runner()->PostTask(FROM_HERE, |
| 1051 BindOnce(&PostNTasksThenQuit, kNumTimes)); | 990 BindOnce(&PostNTasksThenQuit, kNumTimes)); |
| 1052 RunLoop().Run(); | 991 RunLoop().Run(); |
| 1053 } | 992 } |
| 1054 | 993 |
| 1055 } // namespace test | 994 } // namespace test |
| 1056 } // namespace base | 995 } // namespace base |
| OLD | NEW |