mock_timer.h 2.0 KB

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