OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/test/scoped_mock_time_message_loop_task_runner.h" | 5 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/test/test_pending_task.h" | 13 #include "base/test/test_pending_task.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
| 18 namespace { |
| 19 |
| 20 void RunOnceClosure(OnceClosure closure) { |
| 21 std::move(closure).Run(); |
| 22 } |
| 23 |
| 24 } // namespace |
17 | 25 |
18 ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner() | 26 ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner() |
19 : task_runner_(new TestMockTimeTaskRunner), | 27 : task_runner_(new TestMockTimeTaskRunner), |
20 previous_task_runner_(ThreadTaskRunnerHandle::Get()) { | 28 previous_task_runner_(ThreadTaskRunnerHandle::Get()) { |
21 DCHECK(MessageLoop::current()); | 29 DCHECK(MessageLoop::current()); |
22 // To ensure that we process any initialization tasks posted to the | 30 // To ensure that we process any initialization tasks posted to the |
23 // MessageLoop by a test fixture before replacing its TaskRunner. | 31 // MessageLoop by a test fixture before replacing its TaskRunner. |
24 RunLoop().RunUntilIdle(); | 32 RunLoop().RunUntilIdle(); |
25 MessageLoop::current()->SetTaskRunner(task_runner_); | 33 MessageLoop::current()->SetTaskRunner(task_runner_); |
26 } | 34 } |
27 | 35 |
28 ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() { | 36 ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() { |
29 DCHECK(previous_task_runner_->RunsTasksOnCurrentThread()); | 37 DCHECK(previous_task_runner_->RunsTasksOnCurrentThread()); |
30 DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); | 38 DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); |
31 for (const auto& pending_task : task_runner_->TakePendingTasks()) { | 39 for (auto& pending_task : task_runner_->TakePendingTasks()) { |
| 40 // TODO(tzik): Remove RunOnceClosure once TaskRunner migrates from Closure |
| 41 // to OnceClosure. |
32 previous_task_runner_->PostDelayedTask( | 42 previous_task_runner_->PostDelayedTask( |
33 pending_task.location, pending_task.task, | 43 pending_task.location, |
| 44 Bind(&RunOnceClosure, Passed(&pending_task.task)), |
34 pending_task.GetTimeToRun() - task_runner_->NowTicks()); | 45 pending_task.GetTimeToRun() - task_runner_->NowTicks()); |
35 } | 46 } |
36 MessageLoop::current()->SetTaskRunner(std::move(previous_task_runner_)); | 47 MessageLoop::current()->SetTaskRunner(std::move(previous_task_runner_)); |
37 } | 48 } |
38 | 49 |
39 } // namespace base | 50 } // namespace base |
OLD | NEW |