blob: 0ab9a937a843bce7370f25cf1d04d32a784b0a3b [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"
Sebastian Janssond624c392019-04-17 10:36:03 +020017#include "api/units/timestamp.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/critical_section.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/time_utils.h"
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace 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.
28class FakeClock : public ClockInterface {
29 public:
Sebastian Janssond624c392019-04-17 10:36:03 +020030 FakeClock() = default;
31 FakeClock(const FakeClock&) = delete;
32 FakeClock& operator=(const FakeClock&) = delete;
33 ~FakeClock() override = default;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034
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.
Sebastian Janssond624c392019-04-17 10:36:03 +020041 void SetTime(webrtc::Timestamp new_time);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042
Sebastian Jansson5f83cf02018-05-08 14:52:22 +020043 void AdvanceTime(webrtc::TimeDelta delta);
Sebastian Janssond624c392019-04-17 10:36:03 +020044
45 private:
46 CriticalSection lock_;
47 int64_t time_ns_ RTC_GUARDED_BY(lock_) = 0;
48};
49
50class ThreadProcessingFakeClock : public ClockInterface {
51 public:
52 int64_t TimeNanos() const override { return clock_.TimeNanos(); }
53 void SetTime(webrtc::Timestamp time);
Sebastian Janssond624c392019-04-17 10:36:03 +020054 void AdvanceTime(webrtc::TimeDelta delta);
Sebastian Jansson40889f32019-04-17 12:11:20 +020055
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 private:
Sebastian Janssond624c392019-04-17 10:36:03 +020057 FakeClock clock_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058};
59
60// Helper class that sets itself as the global clock in its constructor and
61// unsets it in its destructor.
Sebastian Janssond624c392019-04-17 10:36:03 +020062class 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.
72class ScopedFakeClock : public ThreadProcessingFakeClock {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 public:
74 ScopedFakeClock();
75 ~ScopedFakeClock() override;
76
77 private:
78 ClockInterface* prev_clock_;
79};
80
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081} // namespace rtc
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070082
Steve Anton10542f22019-01-11 09:11:00 -080083#endif // RTC_BASE_FAKE_CLOCK_H_