#include "ash/rounded_display/rounded_display_host.h"
#include <memory>
#include <utility>
#include <vector>
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window_tree_host.h"
#include "ui/compositor/layer_type.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
namespace ash {
namespace {
class TestRoundedDisplayHost : public RoundedDisplayHost {
public:
explicit TestRoundedDisplayHost(GetGuttersCallback callback)
: RoundedDisplayHost(std::move(callback)) {}
TestRoundedDisplayHost(const TestRoundedDisplayHost&) = delete;
TestRoundedDisplayHost& operator=(const TestRoundedDisplayHost&) = delete;
~TestRoundedDisplayHost() override = default;
std::unique_ptr<viz::CompositorFrame> CreateCompositorFrameForTest(
const gfx::Size& last_submitted_frame_size,
float last_submitted_frame_dsf) {
return RoundedDisplayHost::CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), resource_manager_,
false, last_submitted_frame_size,
last_submitted_frame_dsf);
}
private:
UiResourceManager resource_manager_;
};
class RoundedDisplayHostTest : public AshTestBase {
public:
RoundedDisplayHostTest() = default;
RoundedDisplayHostTest(const RoundedDisplayHostTest&) = delete;
RoundedDisplayHostTest& operator=(const RoundedDisplayHostTest&) = delete;
~RoundedDisplayHostTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
auto host_window = std::make_unique<aura::Window>(nullptr);
host_window_ = host_window.release();
host_window_->Init(ui::LayerType::LAYER_SOLID_COLOR);
auto* root_window = ash_test_helper()->GetHost()->window();
root_window->AddChild(host_window_);
host_ = std::make_unique<TestRoundedDisplayHost>(base::DoNothing());
host_->Init(host_window_);
}
void TearDown() override {
host_.reset();
AshTestBase::TearDown();
}
protected:
raw_ptr<aura::Window, DanglingUntriaged> host_window_;
std::unique_ptr<TestRoundedDisplayHost> host_;
};
TEST_F(RoundedDisplayHostTest, NewSurfaceIdIsCreatedWhenNeeded) {
UpdateDisplay("1920x1080");
host_window_->SetBounds(gfx::Rect(1920, 1080));
auto primary_display = display::Screen::Get()->GetPrimaryDisplay();
gfx::Size initial_frame_size = primary_display.GetSizeInPixel();
float initial_device_scale_factor = primary_display.device_scale_factor();
auto frame = host_->CreateCompositorFrameForTest(initial_frame_size,
initial_device_scale_factor);
auto frame_sink_id = host_window_->GetSurfaceId();
ASSERT_TRUE(frame_sink_id.is_valid());
UpdateDisplay("1920x1080*2");
auto old_frame_sink_id = frame_sink_id;
frame = host_->CreateCompositorFrameForTest(frame->size_in_pixels(),
frame->device_scale_factor());
auto new_frame_sink_id = host_window_->GetSurfaceId();
ASSERT_TRUE(new_frame_sink_id.is_valid());
EXPECT_TRUE(new_frame_sink_id.IsNewerThan(old_frame_sink_id));
UpdateDisplay("1920x1080*2/r");
old_frame_sink_id = new_frame_sink_id;
frame = host_->CreateCompositorFrameForTest(frame->size_in_pixels(),
frame->device_scale_factor());
new_frame_sink_id = host_window_->GetSurfaceId();
ASSERT_TRUE(new_frame_sink_id.is_valid());
EXPECT_TRUE(new_frame_sink_id.IsNewerThan(old_frame_sink_id));
old_frame_sink_id = new_frame_sink_id;
frame = host_->CreateCompositorFrameForTest(frame->size_in_pixels(),
frame->device_scale_factor());
new_frame_sink_id = host_window_->GetSurfaceId();
ASSERT_TRUE(new_frame_sink_id.is_valid());
EXPECT_EQ(new_frame_sink_id, old_frame_sink_id);
}
}
}