// 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 #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