Chromium Code Reviews| Index: base/test/scoped_mock_time_message_loop_task_runner.cc |
| diff --git a/base/test/scoped_mock_time_message_loop_task_runner.cc b/base/test/scoped_mock_time_message_loop_task_runner.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..80be7c4ed86bf475c9b22c207c105c9d14c657ae |
| --- /dev/null |
| +++ b/base/test/scoped_mock_time_message_loop_task_runner.cc |
| @@ -0,0 +1,37 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| + |
| +#include <deque> |
| + |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/test/test_pending_task.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "base/time/time.h" |
| + |
| +namespace base { |
| + |
| +ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner() |
| + : task_runner_(new TestMockTimeTaskRunner), |
| + previous_task_runner_(ThreadTaskRunnerHandle::Get()) { |
| + DCHECK(MessageLoop::current()); |
| + RunLoop().RunUntilIdle(); |
|
danakj
2016/11/18 21:27:21
Can you leave a comment explaining this?
bruthig
2016/11/18 22:53:28
Done.
|
| + MessageLoop::current()->SetTaskRunner(task_runner_); |
| +} |
| + |
| +ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() { |
| + DCHECK(previous_task_runner_->RunsTasksOnCurrentThread()); |
| + DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); |
| + for (const auto& pending_task : task_runner_->TakePendingTasks()) { |
| + previous_task_runner_->PostDelayedTask( |
| + pending_task.location, pending_task.task, |
| + pending_task.GetTimeToRun() - task_runner_->NowTicks()); |
| + } |
| + MessageLoop::current()->SetTaskRunner(std::move(previous_task_runner_)); |
| +} |
| + |
| +} // namespace base |