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_info.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 // TODO(tzik): Remove this helper once TaskRunner has migrated from Closure to |
| 21 // OnceClosure. |
| 22 void RunOnceClosure(OnceClosure closure) { |
| 23 std::move(closure).Run(); |
| 24 } |
| 25 |
| 26 } // namespace |
17 | 27 |
18 ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner() | 28 ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner() |
19 : task_runner_(new TestMockTimeTaskRunner), | 29 : task_runner_(new TestMockTimeTaskRunner), |
20 previous_task_runner_(ThreadTaskRunnerHandle::Get()) { | 30 previous_task_runner_(ThreadTaskRunnerHandle::Get()) { |
21 DCHECK(MessageLoop::current()); | 31 DCHECK(MessageLoop::current()); |
22 // To ensure that we process any initialization tasks posted to the | 32 // To ensure that we process any initialization tasks posted to the |
23 // MessageLoop by a test fixture before replacing its TaskRunner. | 33 // MessageLoop by a test fixture before replacing its TaskRunner. |
24 RunLoop().RunUntilIdle(); | 34 RunLoop().RunUntilIdle(); |
25 MessageLoop::current()->SetTaskRunner(task_runner_); | 35 MessageLoop::current()->SetTaskRunner(task_runner_); |
26 } | 36 } |
27 | 37 |
28 ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() { | 38 ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() { |
29 DCHECK(previous_task_runner_->RunsTasksOnCurrentThread()); | 39 DCHECK(previous_task_runner_->RunsTasksOnCurrentThread()); |
30 DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); | 40 DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); |
31 for (const auto& pending_task : task_runner_->TakePendingTasks()) { | 41 for (auto& pending_task : task_runner_->TakePendingTasks()) { |
| 42 const TestPendingTaskInfo& task_info = pending_task.first; |
| 43 OnceClosure task = std::move(pending_task.second); |
32 previous_task_runner_->PostDelayedTask( | 44 previous_task_runner_->PostDelayedTask( |
33 pending_task.location, pending_task.task, | 45 task_info.location, Bind(&RunOnceClosure, Passed(&task)), |
34 pending_task.GetTimeToRun() - task_runner_->NowTicks()); | 46 task_info.GetTimeToRun() - task_runner_->NowTicks()); |
35 } | 47 } |
36 MessageLoop::current()->SetTaskRunner(std::move(previous_task_runner_)); | 48 MessageLoop::current()->SetTaskRunner(std::move(previous_task_runner_)); |
37 } | 49 } |
38 | 50 |
39 } // namespace base | 51 } // namespace base |
OLD | NEW |