• 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 #ifndef BASE_TIMER_MOCK_TIMER_H_
6 #define BASE_TIMER_MOCK_TIMER_H_
7 
8 #include "base/test/simple_test_tick_clock.h"
9 #include "base/timer/timer.h"
10 
11 namespace base {
12 
13 class TestSimpleTaskRunner;
14 
15 // A mock implementation of base::Timer which requires being explicitly
16 // Fire()'d.
17 // Prefer using ScopedTaskEnvironment::MOCK_TIME + FastForward*() to this when
18 // possible.
19 class MockOneShotTimer : public OneShotTimer {
20  public:
21   MockOneShotTimer();
22   ~MockOneShotTimer() override;
23 
24   // Testing method.
25   void Fire();
26 
27  private:
28   // Timer implementation.
29   // MockOneShotTimer doesn't support SetTaskRunner. Do not use this.
30   void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
31 
32   SimpleTestTickClock clock_;
33   scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
34 };
35 
36 // See MockOneShotTimer's comment. Prefer using
37 // ScopedTaskEnvironment::MOCK_TIME.
38 class MockRepeatingTimer : public RepeatingTimer {
39  public:
40   MockRepeatingTimer();
41   ~MockRepeatingTimer() override;
42 
43   // Testing method.
44   void Fire();
45 
46  private:
47   // Timer implementation.
48   // MockRepeatingTimer doesn't support SetTaskRunner. Do not use this.
49   void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
50 
51   SimpleTestTickClock clock_;
52   scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
53 };
54 
55 // See MockOneShotTimer's comment. Prefer using
56 // ScopedTaskEnvironment::MOCK_TIME.
57 class MockRetainingOneShotTimer : public RetainingOneShotTimer {
58  public:
59   MockRetainingOneShotTimer();
60   ~MockRetainingOneShotTimer() override;
61 
62   // Testing method.
63   void Fire();
64 
65  private:
66   // Timer implementation.
67   // MockRetainingOneShotTimer doesn't support SetTaskRunner. Do not use this.
68   void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
69 
70   SimpleTestTickClock clock_;
71   scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
72 };
73 
74 }  // namespace base
75 
76 #endif  // BASE_TIMER_MOCK_TIMER_H_
77