Files
android/libwvdrmengine/cdm/util/test/test_clock.cpp
Kyle Zhang 642965c678 Merge latest oemcrypto-v17 change
No-Typo-Check: Not related to this change.

Bug: 161477208
Change-Id: I99e4780f6855b7045aa0cd5a49c13d2d0d51ed64
2022-01-27 20:07:15 -08:00

43 lines
1.1 KiB
C++

// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Clock - A fake clock just for running tests.
#include <chrono>
#include "clock.h"
#include "test_sleep.h"
namespace wvutil {
namespace {
// A fake clock that only advances when TestSleep::Sleep is called.
class FakeClock : public wvcdm::TestSleep::CallBack {
public:
FakeClock() {
auto now = std::chrono::system_clock().now();
now_ = now.time_since_epoch() / std::chrono::milliseconds(1);
TestSleep::set_callback(this);
}
~FakeClock() { TestSleep::set_callback(nullptr); }
void ElapseTime(int64_t milliseconds) { now_ += milliseconds; }
int64_t now() const { return now_; }
private:
int64_t now_;
};
FakeClock* g_fake_clock = nullptr;
} // namespace
// On devices running a fake OEMCrypto, we can use a fake sleep and fake time.
int64_t Clock::GetCurrentTime() {
wvcdm::TestSleep::SyncFakeClock();
if (g_fake_clock == nullptr) g_fake_clock = new FakeClock();
return g_fake_clock->now() / 1000;
}
} // namespace wvutil