• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop_current.h"
10 #include "base/run_loop.h"
11 #include "base/test/test_pending_task.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/time/time.h"
14 
15 namespace base {
16 
ScopedMockTimeMessageLoopTaskRunner()17 ScopedMockTimeMessageLoopTaskRunner::ScopedMockTimeMessageLoopTaskRunner()
18     : task_runner_(new TestMockTimeTaskRunner),
19       previous_task_runner_(ThreadTaskRunnerHandle::Get()) {
20   DCHECK(MessageLoopCurrent::Get());
21   // To ensure that we process any initialization tasks posted to the
22   // MessageLoop by a test fixture before replacing its TaskRunner.
23   RunLoop().RunUntilIdle();
24   MessageLoopCurrent::Get()->SetTaskRunner(task_runner_);
25 }
26 
~ScopedMockTimeMessageLoopTaskRunner()27 ScopedMockTimeMessageLoopTaskRunner::~ScopedMockTimeMessageLoopTaskRunner() {
28   DCHECK(previous_task_runner_->RunsTasksInCurrentSequence());
29   DCHECK_EQ(task_runner_, ThreadTaskRunnerHandle::Get());
30   for (auto& pending_task : task_runner_->TakePendingTasks()) {
31     previous_task_runner_->PostDelayedTask(
32         pending_task.location, std::move(pending_task.task),
33         pending_task.GetTimeToRun() - task_runner_->NowTicks());
34   }
35   MessageLoopCurrent::Get()->SetTaskRunner(std::move(previous_task_runner_));
36 }
37 
38 }  // namespace base
39