#include "media/gpu/android/promotion_hint_aggregator_impl.h"
#include <stdint.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/test/simple_test_tick_clock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr base::TimeDelta FrameTime = base::Milliseconds(10);
}
namespace media {
class PromotionHintAggregatorImplTest : public testing::Test {
public:
~PromotionHintAggregatorImplTest() override {}
void SetUp() override {
tick_clock_.Advance(base::Seconds(10000));
impl_ = std::make_unique<PromotionHintAggregatorImpl>(&tick_clock_);
}
void TearDown() override {}
bool SendFrame(bool is_promotable, base::TimeDelta elapsed = FrameTime) {
tick_clock_.Advance(elapsed);
PromotionHintAggregator::Hint hint(gfx::Rect(), is_promotable);
impl_->NotifyPromotionHint(hint);
return impl_->IsSafeToPromote();
}
base::SimpleTestTickClock tick_clock_;
std::unique_ptr<PromotionHintAggregatorImpl> impl_;
};
TEST_F(PromotionHintAggregatorImplTest, InitiallyNotPromotable) {
ASSERT_FALSE(impl_->IsSafeToPromote());
}
TEST_F(PromotionHintAggregatorImplTest, SomePromotableFramesArePromotable) {
for (int i = 0; i < 9; i++)
ASSERT_FALSE(SendFrame(true));
ASSERT_TRUE(SendFrame(true));
ASSERT_TRUE(SendFrame(true, base::Milliseconds(10000)));
ASSERT_TRUE(SendFrame(true, base::Milliseconds(10000)));
}
TEST_F(PromotionHintAggregatorImplTest, UnpromotableFramesDelayPromotion) {
ASSERT_FALSE(SendFrame(false));
base::TimeTicks start = tick_clock_.NowTicks();
while (tick_clock_.NowTicks() - start + FrameTime < base::Seconds(2))
ASSERT_FALSE(SendFrame(true));
ASSERT_TRUE(SendFrame(true));
}
TEST_F(PromotionHintAggregatorImplTest, PromotableFramesMustBeFastEnough) {
for (int i = 0; i < 8; i++)
ASSERT_FALSE(SendFrame(true));
tick_clock_.Advance(base::Milliseconds(500));
for (int i = 0; i < 9; i++)
ASSERT_FALSE(SendFrame(true));
ASSERT_TRUE(SendFrame(true));
}
}