Files
android/libwvdrmengine/cdm/test/timer_unittest.cpp
Rahul Frias ec8bed38f4 Update Widevine Copyright header for android
[ Merge of http://go/wvgerrit/108084 ]

The Widevine License Agreement has been renamed to use inclusive
language. This covers files in the android directory.

Bug: 168562298
Test: verified compilation (comment only change)
Change-Id: I0f9e6445e0168ebe85425baeb81371e182e5a39c
2020-10-21 02:49:05 -07:00

116 lines
2.8 KiB
C++

// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#include <gtest/gtest.h>
#include "timer.h"
namespace wvcdm {
namespace {
// duration in seconds after starting the timer
constexpr uint32_t kTestDuration = 5;
// duration in seconds after test completes, before evaluating the result
constexpr uint32_t kPostTestDuration = 3;
} // namespace
class TestTimerHandler : public TimerHandler {
public:
TestTimerHandler() : timer_events_count_(0){};
virtual ~TestTimerHandler(){};
virtual void OnTimerEvent() { timer_events_count_++; }
uint32_t timer_events_count() { return timer_events_count_; }
protected:
uint32_t timer_events_count_;
};
class TestStopTimerHandler : public TestTimerHandler {
public:
TestStopTimerHandler(uint32_t stop_at_timer_events_count, Timer* timer)
: stop_at_timer_events_count_(stop_at_timer_events_count),
timer_(timer) {}
virtual ~TestStopTimerHandler() {}
virtual void OnTimerEvent() {
if (++timer_events_count_ >= stop_at_timer_events_count_) {
if (timer_ != nullptr) {
timer_->Stop(false);
}
}
}
protected:
uint32_t stop_at_timer_events_count_;
Timer* timer_;
};
TEST(TimerTest, ParametersCheck) {
Timer timer;
EXPECT_FALSE(timer.Start(nullptr, 10));
TestTimerHandler handler;
EXPECT_FALSE(timer.Start(&handler, 0));
}
TEST(TimerTest, TimerCheck_StopAndWaitForExit) {
TestTimerHandler handler;
Timer timer;
EXPECT_EQ(0u, handler.timer_events_count());
EXPECT_FALSE(timer.IsRunning());
EXPECT_TRUE(timer.Start(&handler, 1));
EXPECT_TRUE(timer.IsRunning());
sleep(kTestDuration);
EXPECT_LE(kTestDuration - 1, handler.timer_events_count());
EXPECT_LE(handler.timer_events_count(), kTestDuration + 1);
timer.Stop(true);
EXPECT_FALSE(timer.IsRunning());
sleep(kPostTestDuration);
EXPECT_LE(kTestDuration - 1, handler.timer_events_count());
EXPECT_LE(handler.timer_events_count(), kTestDuration + 1);
}
TEST(TimerTest, TimerCheck_Stop) {
TestTimerHandler handler;
Timer timer;
EXPECT_EQ(0u, handler.timer_events_count());
EXPECT_FALSE(timer.IsRunning());
EXPECT_TRUE(timer.Start(&handler, 1));
EXPECT_TRUE(timer.IsRunning());
sleep(kTestDuration);
EXPECT_LE(kTestDuration - 1, handler.timer_events_count());
EXPECT_LE(handler.timer_events_count(), kTestDuration + 1);
timer.Stop(false);
sleep(kPostTestDuration);
EXPECT_FALSE(timer.IsRunning());
}
TEST(TimerTest, StopOnTimerThread) {
Timer timer;
TestStopTimerHandler handler(kTestDuration - 2, &timer);
EXPECT_EQ(0u, handler.timer_events_count());
EXPECT_FALSE(timer.IsRunning());
EXPECT_TRUE(timer.Start(&handler, 1));
EXPECT_TRUE(timer.IsRunning());
sleep(kTestDuration + kPostTestDuration);
EXPECT_FALSE(timer.IsRunning());
}
} // namespace wvcdm