blob: 0ea868c5cc737b0f20058cbfebc901a5b4acbafa [file] [log] [blame]
Bjorn A Mellemc4f86542019-11-21 10:37:18 -08001/*
2 * Copyright 2019 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
11#include "api/test/create_time_controller.h"
12
13#include "api/test/time_controller.h"
14#include "api/units/time_delta.h"
15#include "api/units/timestamp.h"
16#include "test/gmock.h"
17#include "test/gtest.h"
18
19namespace webrtc {
20namespace {
21
22class FakeAlarm : public ControlledAlarmClock {
23 public:
24 explicit FakeAlarm(Timestamp start_time);
25
26 Clock* GetClock() override;
27 bool ScheduleAlarmAt(Timestamp deadline) override;
28 void SetCallback(std::function<void()> callback) override;
29 void Sleep(TimeDelta duration) override;
30
31 private:
32 SimulatedClock clock_;
33 Timestamp deadline_;
34 std::function<void()> callback_;
35};
36
37FakeAlarm::FakeAlarm(Timestamp start_time)
38 : clock_(start_time),
39 deadline_(Timestamp::PlusInfinity()),
40 callback_([] {}) {}
41
42Clock* FakeAlarm::GetClock() {
43 return &clock_;
44}
45
46bool FakeAlarm::ScheduleAlarmAt(Timestamp deadline) {
47 if (deadline < deadline_) {
48 deadline_ = deadline;
49 return true;
50 }
51 return false;
52}
53
54void FakeAlarm::SetCallback(std::function<void()> callback) {
55 callback_ = callback;
56}
57
58void FakeAlarm::Sleep(TimeDelta duration) {
59 Timestamp end_time = clock_.CurrentTime() + duration;
60
61 while (deadline_ <= end_time) {
62 clock_.AdvanceTime(deadline_ - clock_.CurrentTime());
63 deadline_ = Timestamp::PlusInfinity();
64 callback_();
65 }
66
67 clock_.AdvanceTime(end_time - clock_.CurrentTime());
68}
69
70TEST(CreateTimeControllerTest, CreatesNonNullController) {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010071 FakeAlarm alarm(Timestamp::Millis(100));
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080072 EXPECT_NE(CreateTimeController(&alarm), nullptr);
73}
74
75} // namespace
76} // namespace webrtc