Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_FAKE_CLOCK_H_ |
| 12 | #define RTC_BASE_FAKE_CLOCK_H_ |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 16 | #include "api/units/time_delta.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/critical_section.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "rtc_base/thread_annotations.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/time_utils.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 20 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | namespace rtc { |
| 22 | |
| 23 | // Fake clock for use with unit tests, which does not tick on its own. |
| 24 | // Starts at time 0. |
| 25 | // |
| 26 | // TODO(deadbeef): Unify with webrtc::SimulatedClock. |
| 27 | class FakeClock : public ClockInterface { |
| 28 | public: |
| 29 | ~FakeClock() override {} |
| 30 | |
| 31 | // ClockInterface implementation. |
| 32 | int64_t TimeNanos() const override; |
| 33 | |
| 34 | // Methods that can be used by the test to control the time. |
| 35 | |
| 36 | // Should only be used to set a time in the future. |
| 37 | void SetTimeNanos(int64_t nanos); |
| 38 | void SetTimeMicros(int64_t micros) { |
| 39 | SetTimeNanos(kNumNanosecsPerMicrosec * micros); |
| 40 | } |
| 41 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 42 | void AdvanceTime(webrtc::TimeDelta delta); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | void AdvanceTimeMicros(int64_t micros) { |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 44 | AdvanceTime(webrtc::TimeDelta::us(micros)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 45 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 47 | private: |
| 48 | CriticalSection lock_; |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 49 | int64_t time_ RTC_GUARDED_BY(lock_) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // Helper class that sets itself as the global clock in its constructor and |
| 53 | // unsets it in its destructor. |
| 54 | class ScopedFakeClock : public FakeClock { |
| 55 | public: |
| 56 | ScopedFakeClock(); |
Sebastian Jansson | 9debe5a | 2019-03-22 15:42:38 +0100 | [diff] [blame] | 57 | ScopedFakeClock(const ScopedFakeClock&) = delete; |
| 58 | ScopedFakeClock& operator=(const ScopedFakeClock&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | ~ScopedFakeClock() override; |
| 60 | |
| 61 | private: |
| 62 | ClockInterface* prev_clock_; |
| 63 | }; |
| 64 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | } // namespace rtc |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 66 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 67 | #endif // RTC_BASE_FAKE_CLOCK_H_ |