skewed_tick_clock.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 MEDIA_CAST_TEST_SKEWED_TICK_CLOCK_H_
  5. #define MEDIA_CAST_TEST_SKEWED_TICK_CLOCK_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/time/tick_clock.h"
  8. #include "base/time/time.h"
  9. namespace media {
  10. namespace cast {
  11. namespace test {
  12. // Wraps a base::TickClock, and lets you change the speed and offset
  13. // of time compared to the wrapped clock. See SetSkew for how usage.
  14. class SkewedTickClock : public base::TickClock {
  15. public:
  16. // Does not take ownership of |clock_|.
  17. explicit SkewedTickClock(const base::TickClock* clock_);
  18. SkewedTickClock(const SkewedTickClock&) = delete;
  19. SkewedTickClock& operator=(const SkewedTickClock&) = delete;
  20. // |skew| > 1.0 means clock runs faster.
  21. // |offset| > 0 means clock returns times from the future.
  22. // Note, |offset| is cumulative.
  23. // Also note that changing the skew will never make the clock
  24. // jump forwards or backwards, only changing the offset will
  25. // do that.
  26. void SetSkew(double skew, base::TimeDelta offset);
  27. base::TimeTicks NowTicks() const final;
  28. private:
  29. base::TimeTicks SkewTicks(base::TimeTicks now) const;
  30. raw_ptr<const base::TickClock> clock_; // Not owned.
  31. double skew_;
  32. base::TimeTicks last_skew_set_time_;
  33. base::TimeTicks skew_clock_at_last_set_;
  34. };
  35. } // namespace test
  36. } // namespace cast
  37. } // namespace media
  38. #endif // MEDIA_CAST_TEST_SKEWED_TICK_CLOCK_H_