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