blob: dce15391f7e8e010c318ccbce0062b2c5b4e431c [file] [log] [blame]
Taylor Brandstetterb3c68102016-05-27 14:15:43 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_FAKE_CLOCK_H_
12#define RTC_BASE_FAKE_CLOCK_H_
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
15
Sebastian Jansson5f83cf02018-05-08 14:52:22 +020016#include "api/units/time_delta.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/critical_section.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/time_utils.h"
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021namespace 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.
27class 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 Jansson5f83cf02018-05-08 14:52:22 +020042 void AdvanceTime(webrtc::TimeDelta delta);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043 void AdvanceTimeMicros(int64_t micros) {
Sebastian Jansson5f83cf02018-05-08 14:52:22 +020044 AdvanceTime(webrtc::TimeDelta::us(micros));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045 }
Yves Gerey665174f2018-06-19 15:03:05 +020046
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047 private:
48 CriticalSection lock_;
danilchap3c6abd22017-09-06 05:46:29 -070049 int64_t time_ RTC_GUARDED_BY(lock_) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050};
51
52// Helper class that sets itself as the global clock in its constructor and
53// unsets it in its destructor.
54class ScopedFakeClock : public FakeClock {
55 public:
56 ScopedFakeClock();
Sebastian Jansson9debe5a2019-03-22 15:42:38 +010057 ScopedFakeClock(const ScopedFakeClock&) = delete;
58 ScopedFakeClock& operator=(const ScopedFakeClock&) = delete;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059 ~ScopedFakeClock() override;
60
61 private:
62 ClockInterface* prev_clock_;
63};
64
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065} // namespace rtc
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070066
Steve Anton10542f22019-01-11 09:11:00 -080067#endif // RTC_BASE_FAKE_CLOCK_H_