| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_FAKE_CLOCK_H_ |
| 12 | #define RTC_BASE_FAKE_CLOCK_H_ |
| 13 |
| 14 | #include <stdint.h> |
| 15 |
| 16 | #include "api/units/time_delta.h" |
| 17 | #include "api/units/timestamp.h" |
| 18 | #include "rtc_base/synchronization/mutex.h" |
| 19 | #include "rtc_base/thread_annotations.h" |
| 20 | #include "rtc_base/time_utils.h" |
| 21 |
| 22 | namespace rtc { |
| 23 |
| 24 | // Fake clock for use with unit tests, which does not tick on its own. |
| 25 | // Starts at time 0. |
| 26 | // |
| 27 | // TODO(deadbeef): Unify with webrtc::SimulatedClock. |
| 28 | class FakeClock : public ClockInterface { |
| 29 | public: |
| 30 | FakeClock() = default; |
| 31 | FakeClock(const FakeClock&) = delete; |
| 32 | FakeClock& operator=(const FakeClock&) = delete; |
| 33 | ~FakeClock() override = default; |
| 34 |
| 35 | // ClockInterface implementation. |
| 36 | int64_t TimeNanos() const override; |
| 37 |
| 38 | // Methods that can be used by the test to control the time. |
| 39 |
| 40 | // Should only be used to set a time in the future. |
| 41 | void SetTime(webrtc::Timestamp new_time); |
| 42 |
| 43 | void AdvanceTime(webrtc::TimeDelta delta); |
| 44 |
| 45 | private: |
| 46 | mutable webrtc::Mutex lock_; |
| 47 | int64_t time_ns_ RTC_GUARDED_BY(lock_) = 0; |
| 48 | }; |
| 49 |
| 50 | class ThreadProcessingFakeClock : public ClockInterface { |
| 51 | public: |
| 52 | int64_t TimeNanos() const override { return clock_.TimeNanos(); } |
| 53 | void SetTime(webrtc::Timestamp time); |
| 54 | void AdvanceTime(webrtc::TimeDelta delta); |
| 55 |
| 56 | private: |
| 57 | FakeClock clock_; |
| 58 | }; |
| 59 |
| 60 | // Helper class that sets itself as the global clock in its constructor and |
| 61 | // unsets it in its destructor. |
| 62 | class ScopedBaseFakeClock : public FakeClock { |
| 63 | public: |
| 64 | ScopedBaseFakeClock(); |
| 65 | ~ScopedBaseFakeClock() override; |
| 66 |
| 67 | private: |
| 68 | ClockInterface* prev_clock_; |
| 69 | }; |
| 70 |
| 71 | // TODO(srte): Rename this to reflect that it also does thread processing. |
| 72 | class ScopedFakeClock : public ThreadProcessingFakeClock { |
| 73 | public: |
| 74 | ScopedFakeClock(); |
| 75 | ~ScopedFakeClock() override; |
| 76 |
| 77 | private: |
| 78 | ClockInterface* prev_clock_; |
| 79 | }; |
| 80 |
| 81 | } // namespace rtc |
| 82 |
| 83 | #endif // RTC_BASE_FAKE_CLOCK_H_ |
| 84 |