[ Merge of http://go/wvgerrit/89848 ] Apps query a number of properties at initialization. The mediaDrm API getProperty allows the query of a single property at a time. This causes a series of requests. If no crypto sessions are concurrently open, a series of expensive OEMCrypto Initialization and Termination calls will occur. In this change OEMCrypto termination is delayed. If an OEMCrypto Terminate is followed in close succession by an Initialize, neither will occur avoiding the overhead. A timer enables a countdown process. If no session activity occurs, the timer will eventually terminate OEMCrypto and exit. Bug: 136282358 Test: Android unit/integration tests Change-Id: I442b7919b4e7835c52583516c8bc64d0c150241d
116 lines
2.9 KiB
C++
116 lines
2.9 KiB
C++
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine Master
|
|
// 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
|