OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 6 |
| 7 #include <deque> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/test/test_pending_task.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" |
| 15 |
| 16 namespace base { |
| 17 |
| 18 ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner() |
| 19 : task_runner_(new TestMockTimeTaskRunner), |
| 20 previous_task_runner_(ThreadTaskRunnerHandle::Get()) { |
| 21 DCHECK(MessageLoop::current()); |
| 22 // To ensure that we process any initialization tasks posted to the |
| 23 // MessageLoop by a test fixture before replacing its TaskRunner. |
| 24 RunLoop().RunUntilIdle(); |
| 25 MessageLoop::current()->SetTaskRunner(task_runner_); |
| 26 } |
| 27 |
| 28 ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() { |
| 29 DCHECK(previous_task_runner_->RunsTasksOnCurrentThread()); |
| 30 DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); |
| 31 for (const auto& pending_task : task_runner_->TakePendingTasks()) { |
| 32 previous_task_runner_->PostDelayedTask( |
| 33 pending_task.location, pending_task.task, |
| 34 pending_task.GetTimeToRun() - task_runner_->NowTicks()); |
| 35 } |
| 36 MessageLoop::current()->SetTaskRunner(std::move(previous_task_runner_)); |
| 37 } |
| 38 |
| 39 } // namespace base |
OLD | NEW |