• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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/timer/mock_timer.h"
6 
7 #include "base/test/test_simple_task_runner.h"
8 
9 namespace base {
10 
11 namespace {
12 
FlushPendingTasks(TestSimpleTaskRunner * task_runner)13 void FlushPendingTasks(TestSimpleTaskRunner* task_runner) {
14   // Do not use TestSimpleTaskRunner::RunPendingTasks() here. As RunPendingTasks
15   // overrides ThreadTaskRunnerHandle when it runs tasks, tasks posted by timer
16   // tasks to TTRH go to |test_task_runner_|, though they should be posted to
17   // the original task runner.
18   // Do not use TestSimpleTaskRunner::RunPendingTasks(), as its overridden
19   // ThreadTaskRunnerHandle causes unexpected side effects.
20   for (TestPendingTask& task : task_runner->TakePendingTasks())
21     std::move(task.task).Run();
22 }
23 
24 }  // namespace
25 
MockOneShotTimer()26 MockOneShotTimer::MockOneShotTimer()
27     : OneShotTimer(&clock_),
28       test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
29   OneShotTimer::SetTaskRunner(test_task_runner_);
30 }
31 
32 MockOneShotTimer::~MockOneShotTimer() = default;
33 
SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner)34 void MockOneShotTimer::SetTaskRunner(
35     scoped_refptr<SequencedTaskRunner> task_runner) {
36   NOTREACHED() << "MockOneShotTimer doesn't support SetTaskRunner().";
37 }
38 
Fire()39 void MockOneShotTimer::Fire() {
40   DCHECK(IsRunning());
41   clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
42   FlushPendingTasks(test_task_runner_.get());
43 }
44 
MockRepeatingTimer()45 MockRepeatingTimer::MockRepeatingTimer()
46     : RepeatingTimer(&clock_),
47       test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
48   RepeatingTimer::SetTaskRunner(test_task_runner_);
49 }
50 
51 MockRepeatingTimer::~MockRepeatingTimer() = default;
52 
SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner)53 void MockRepeatingTimer::SetTaskRunner(
54     scoped_refptr<SequencedTaskRunner> task_runner) {
55   NOTREACHED() << "MockRepeatingTimer doesn't support SetTaskRunner().";
56 }
57 
Fire()58 void MockRepeatingTimer::Fire() {
59   DCHECK(IsRunning());
60   clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
61   FlushPendingTasks(test_task_runner_.get());
62 }
63 
MockRetainingOneShotTimer()64 MockRetainingOneShotTimer::MockRetainingOneShotTimer()
65     : RetainingOneShotTimer(&clock_),
66       test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
67   RetainingOneShotTimer::SetTaskRunner(test_task_runner_);
68 }
69 
70 MockRetainingOneShotTimer::~MockRetainingOneShotTimer() = default;
71 
SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner)72 void MockRetainingOneShotTimer::SetTaskRunner(
73     scoped_refptr<SequencedTaskRunner> task_runner) {
74   NOTREACHED() << "MockRetainingOneShotTimer doesn't support SetTaskRunner().";
75 }
76 
Fire()77 void MockRetainingOneShotTimer::Fire() {
78   DCHECK(IsRunning());
79   clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
80   FlushPendingTasks(test_task_runner_.get());
81 }
82 
83 }  // namespace base
84