scoped_event_test_tick_clock.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2018 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 UI_EVENTS_TEST_SCOPED_EVENT_TEST_TICK_CLOCK_H_
  5. #define UI_EVENTS_TEST_SCOPED_EVENT_TEST_TICK_CLOCK_H_
  6. #include "base/test/simple_test_tick_clock.h"
  7. #include "base/time/time.h"
  8. #include "ui/events/base_event_utils.h"
  9. namespace ui {
  10. namespace test {
  11. // Helper class to mock ui events tick clock. On construction it registers a
  12. // simple test tick clock for ui events. On destruction, it clears the test
  13. // clock.
  14. //
  15. // Example:
  16. // TEST_F(SomeFixture, MyTest) {
  17. // ScopedEventTestTickClock clock;
  18. // clock.SetNowSeconds(1200);
  19. // }
  20. class ScopedEventTestTickClock {
  21. public:
  22. ScopedEventTestTickClock() { ui::SetEventTickClockForTesting(&test_clock_); }
  23. ScopedEventTestTickClock(const ScopedEventTestTickClock&) = delete;
  24. ScopedEventTestTickClock& operator=(const ScopedEventTestTickClock&) = delete;
  25. ~ScopedEventTestTickClock() { ui::SetEventTickClockForTesting(nullptr); }
  26. void SetNowSeconds(int64_t seconds) {
  27. test_clock_.SetNowTicks(base::TimeTicks() + base::Seconds(seconds));
  28. }
  29. void SetNowTicks(base::TimeTicks ticks) { test_clock_.SetNowTicks(ticks); }
  30. void Advance(base::TimeDelta delta) { test_clock_.Advance(delta); }
  31. private:
  32. base::SimpleTestTickClock test_clock_;
  33. };
  34. } // namespace test
  35. } // namespace ui
  36. #endif // UI_EVENTS_TEST_SCOPED_EVENT_TEST_TICK_CLOCK_H_