Chromium Code Reviews| Index: base/test/scoped_task_environment.cc |
| diff --git a/base/test/scoped_task_environment.cc b/base/test/scoped_task_environment.cc |
| index af871e6a11d4c273197960d73ed3a1a227d32c86..d1c0907c4b0d7e32ab411826fe006c98efc3a7d6 100644 |
| --- a/base/test/scoped_task_environment.cc |
| +++ b/base/test/scoped_task_environment.cc |
| @@ -12,6 +12,27 @@ |
| namespace base { |
| namespace test { |
| +namespace { |
| + |
| +class TaskObserver : public MessageLoop::TaskObserver { |
| + public: |
| + TaskObserver() = default; |
| + |
| + // MessageLoop::TaskObserver: |
| + void WillProcessTask(const PendingTask& pending_task) override {} |
| + void DidProcessTask(const PendingTask& pending_task) override { |
| + ran_task_ = true; |
|
robliao
2017/05/09 21:52:52
It would be more accurate to call this called_did_
|
| + } |
| + |
| + bool ran_task() const { return ran_task_; } |
| + |
| + private: |
| + bool ran_task_ = false; |
| + DISALLOW_COPY_AND_ASSIGN(TaskObserver); |
| +}; |
| + |
| +} // namespace |
| + |
| ScopedTaskEnvironment::ScopedTaskEnvironment(MainThreadType main_thread_type) |
| : message_loop_(main_thread_type == MainThreadType::DEFAULT |
| ? MessageLoop::TYPE_DEFAULT |
| @@ -45,5 +66,19 @@ ScopedTaskEnvironment::~ScopedTaskEnvironment() { |
| TaskScheduler::SetInstance(nullptr); |
| } |
| +void ScopedTaskEnvironment::RunUntilIdle() { |
| + for (;;) { |
| + TaskScheduler::GetInstance()->FlushForTesting(); |
| + |
| + TaskObserver task_observer; |
| + MessageLoop::current()->AddTaskObserver(&task_observer); |
| + RunLoop().RunUntilIdle(); |
| + MessageLoop::current()->RemoveTaskObserver(&task_observer); |
| + |
| + if (!task_observer.ran_task()) |
| + return; |
| + } |
| +} |
| + |
| } // namespace test |
| } // namespace base |