xref: /aosp_15_r20/external/libchrome/base/timer/mock_timer.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TIMER_MOCK_TIMER_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_TIMER_MOCK_TIMER_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include "base/test/simple_test_tick_clock.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/timer/timer.h"
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker namespace base {
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker class TestSimpleTaskRunner;
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker // A mock implementation of base::Timer which requires being explicitly
16*635a8641SAndroid Build Coastguard Worker // Fire()'d.
17*635a8641SAndroid Build Coastguard Worker // Prefer using ScopedTaskEnvironment::MOCK_TIME + FastForward*() to this when
18*635a8641SAndroid Build Coastguard Worker // possible.
19*635a8641SAndroid Build Coastguard Worker class MockOneShotTimer : public OneShotTimer {
20*635a8641SAndroid Build Coastguard Worker  public:
21*635a8641SAndroid Build Coastguard Worker   MockOneShotTimer();
22*635a8641SAndroid Build Coastguard Worker   ~MockOneShotTimer() override;
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker   // Testing method.
25*635a8641SAndroid Build Coastguard Worker   void Fire();
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker  private:
28*635a8641SAndroid Build Coastguard Worker   // Timer implementation.
29*635a8641SAndroid Build Coastguard Worker   // MockOneShotTimer doesn't support SetTaskRunner. Do not use this.
30*635a8641SAndroid Build Coastguard Worker   void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker   SimpleTestTickClock clock_;
33*635a8641SAndroid Build Coastguard Worker   scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
34*635a8641SAndroid Build Coastguard Worker };
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker // See MockOneShotTimer's comment. Prefer using
37*635a8641SAndroid Build Coastguard Worker // ScopedTaskEnvironment::MOCK_TIME.
38*635a8641SAndroid Build Coastguard Worker class MockRepeatingTimer : public RepeatingTimer {
39*635a8641SAndroid Build Coastguard Worker  public:
40*635a8641SAndroid Build Coastguard Worker   MockRepeatingTimer();
41*635a8641SAndroid Build Coastguard Worker   ~MockRepeatingTimer() override;
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker   // Testing method.
44*635a8641SAndroid Build Coastguard Worker   void Fire();
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker  private:
47*635a8641SAndroid Build Coastguard Worker   // Timer implementation.
48*635a8641SAndroid Build Coastguard Worker   // MockRepeatingTimer doesn't support SetTaskRunner. Do not use this.
49*635a8641SAndroid Build Coastguard Worker   void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker   SimpleTestTickClock clock_;
52*635a8641SAndroid Build Coastguard Worker   scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
53*635a8641SAndroid Build Coastguard Worker };
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker // See MockOneShotTimer's comment. Prefer using
56*635a8641SAndroid Build Coastguard Worker // ScopedTaskEnvironment::MOCK_TIME.
57*635a8641SAndroid Build Coastguard Worker class MockRetainingOneShotTimer : public RetainingOneShotTimer {
58*635a8641SAndroid Build Coastguard Worker  public:
59*635a8641SAndroid Build Coastguard Worker   MockRetainingOneShotTimer();
60*635a8641SAndroid Build Coastguard Worker   ~MockRetainingOneShotTimer() override;
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker   // Testing method.
63*635a8641SAndroid Build Coastguard Worker   void Fire();
64*635a8641SAndroid Build Coastguard Worker 
65*635a8641SAndroid Build Coastguard Worker  private:
66*635a8641SAndroid Build Coastguard Worker   // Timer implementation.
67*635a8641SAndroid Build Coastguard Worker   // MockRetainingOneShotTimer doesn't support SetTaskRunner. Do not use this.
68*635a8641SAndroid Build Coastguard Worker   void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker   SimpleTestTickClock clock_;
71*635a8641SAndroid Build Coastguard Worker   scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
72*635a8641SAndroid Build Coastguard Worker };
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker }  // namespace base
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker #endif  // BASE_TIMER_MOCK_TIMER_H_
77