1// Copyright 2013 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 "media/cast/test/fake_task_runner.h"
6
7#include "base/time/tick_clock.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace media {
11namespace cast {
12namespace test {
13
14FakeTaskRunner::FakeTaskRunner(base::SimpleTestTickClock* clock)
15    : clock_(clock) {
16}
17
18FakeTaskRunner::~FakeTaskRunner() {}
19
20bool FakeTaskRunner::PostDelayedTask(const tracked_objects::Location& from_here,
21                                     const base::Closure& task,
22                                     base::TimeDelta delay) {
23  EXPECT_GE(delay, base::TimeDelta());
24  PostedTask posed_task(from_here, task, clock_->NowTicks(), delay,
25                        base::TestPendingTask::NESTABLE);
26
27  tasks_.insert(std::make_pair(posed_task.GetTimeToRun(), posed_task));
28  return false;
29}
30
31bool FakeTaskRunner::RunsTasksOnCurrentThread() const {
32  return true;
33}
34
35void FakeTaskRunner::RunTasks() {
36  for (;;) {
37    // Run all tasks equal or older than current time.
38    std::multimap<base::TimeTicks, PostedTask>::iterator it = tasks_.begin();
39    if (it == tasks_.end()) return;  // No more tasks.
40
41    PostedTask task = it->second;
42    if (clock_->NowTicks() < task.GetTimeToRun()) return;
43
44    tasks_.erase(it);
45    task.task.Run();
46  }
47}
48
49}  // namespace test
50}  // namespace cast
51}  // namespace media
52