Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | namespace webrtc { |
| 20 | namespace { |
| 21 | |
| 22 | class 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 | |
| 37 | FakeAlarm::FakeAlarm(Timestamp start_time) |
| 38 | : clock_(start_time), |
| 39 | deadline_(Timestamp::PlusInfinity()), |
| 40 | callback_([] {}) {} |
| 41 | |
| 42 | Clock* FakeAlarm::GetClock() { |
| 43 | return &clock_; |
| 44 | } |
| 45 | |
| 46 | bool FakeAlarm::ScheduleAlarmAt(Timestamp deadline) { |
| 47 | if (deadline < deadline_) { |
| 48 | deadline_ = deadline; |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | void FakeAlarm::SetCallback(std::function<void()> callback) { |
| 55 | callback_ = callback; |
| 56 | } |
| 57 | |
| 58 | void 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 | |
| 70 | TEST(CreateTimeControllerTest, CreatesNonNullController) { |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame^] | 71 | FakeAlarm alarm(Timestamp::Millis(100)); |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 72 | EXPECT_NE(CreateTimeController(&alarm), nullptr); |
| 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | } // namespace webrtc |