1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/ozone/platform/dri/dri_buffer.h"
7 #include "ui/ozone/platform/dri/hardware_display_controller.h"
8 #include "ui/ozone/platform/dri/screen_manager.h"
9 #include "ui/ozone/platform/dri/test/mock_dri_wrapper.h"
13 // Create a basic mode for a 6x4 screen.
14 const drmModeModeInfo kDefaultMode =
15 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
17 const uint32_t kPrimaryCrtc = 1;
18 const uint32_t kPrimaryConnector = 2;
19 const uint32_t kSecondaryCrtc = 3;
20 const uint32_t kSecondaryConnector = 4;
22 class MockScreenManager : public ui::ScreenManager {
24 MockScreenManager(ui::DriWrapper* dri,
25 ui::ScanoutBufferGenerator* buffer_generator)
26 : ScreenManager(dri, buffer_generator), dri_(dri) {}
28 virtual void ForceInitializationOfPrimaryDisplay() OVERRIDE {}
33 DISALLOW_COPY_AND_ASSIGN(MockScreenManager);
38 class ScreenManagerTest : public testing::Test {
40 ScreenManagerTest() {}
41 virtual ~ScreenManagerTest() {}
43 gfx::Rect GetPrimaryBounds() const {
44 return gfx::Rect(0, 0, kDefaultMode.hdisplay, kDefaultMode.vdisplay);
47 // Secondary is in extended mode, right-of primary.
48 gfx::Rect GetSecondaryBounds() const {
50 kDefaultMode.hdisplay, 0, kDefaultMode.hdisplay, kDefaultMode.vdisplay);
53 virtual void SetUp() OVERRIDE {
54 dri_.reset(new ui::MockDriWrapper(3));
55 buffer_generator_.reset(new ui::DriBufferGenerator(dri_.get()));
56 screen_manager_.reset(new MockScreenManager(
57 dri_.get(), buffer_generator_.get()));
59 virtual void TearDown() OVERRIDE {
60 screen_manager_.reset();
65 scoped_ptr<ui::MockDriWrapper> dri_;
66 scoped_ptr<ui::DriBufferGenerator> buffer_generator_;
67 scoped_ptr<MockScreenManager> screen_manager_;
70 DISALLOW_COPY_AND_ASSIGN(ScreenManagerTest);
73 TEST_F(ScreenManagerTest, CheckWithNoControllers) {
74 EXPECT_FALSE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
77 TEST_F(ScreenManagerTest, CheckWithValidController) {
78 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
79 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
81 GetPrimaryBounds().origin(),
83 base::WeakPtr<ui::HardwareDisplayController> controller =
84 screen_manager_->GetDisplayController(GetPrimaryBounds());
86 EXPECT_TRUE(controller);
87 EXPECT_TRUE(controller->HasCrtc(kPrimaryCrtc));
90 TEST_F(ScreenManagerTest, CheckWithInvalidBounds) {
91 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
92 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
94 GetPrimaryBounds().origin(),
97 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
98 EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
101 TEST_F(ScreenManagerTest, CheckForSecondValidController) {
102 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
103 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
105 GetPrimaryBounds().origin(),
107 screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
108 screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
110 GetSecondaryBounds().origin(),
113 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
114 EXPECT_TRUE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
117 TEST_F(ScreenManagerTest, CheckControllerAfterItIsRemoved) {
118 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
119 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
121 GetPrimaryBounds().origin(),
123 base::WeakPtr<ui::HardwareDisplayController> controller =
124 screen_manager_->GetDisplayController(GetPrimaryBounds());
126 EXPECT_TRUE(controller);
127 screen_manager_->RemoveDisplayController(kPrimaryCrtc);
128 EXPECT_FALSE(controller);
131 TEST_F(ScreenManagerTest, CheckDuplicateConfiguration) {
132 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
133 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
135 GetPrimaryBounds().origin(),
137 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
139 GetPrimaryBounds().origin(),
142 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
143 EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
146 TEST_F(ScreenManagerTest, CheckChangingMode) {
147 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
148 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
150 GetPrimaryBounds().origin(),
152 drmModeModeInfo new_mode = kDefaultMode;
153 new_mode.vdisplay = 10;
154 screen_manager_->ConfigureDisplayController(
155 kPrimaryCrtc, kPrimaryConnector, GetPrimaryBounds().origin(), new_mode);
157 gfx::Rect new_bounds(0, 0, new_mode.hdisplay, new_mode.vdisplay);
158 EXPECT_TRUE(screen_manager_->GetDisplayController(new_bounds));
159 EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
160 drmModeModeInfo mode =
161 screen_manager_->GetDisplayController(new_bounds)->get_mode();
162 EXPECT_EQ(new_mode.vdisplay, mode.vdisplay);
163 EXPECT_EQ(new_mode.hdisplay, mode.hdisplay);
166 TEST_F(ScreenManagerTest, CheckForControllersInMirroredMode) {
167 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
168 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
170 GetPrimaryBounds().origin(),
172 screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
173 screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
175 GetPrimaryBounds().origin(),
178 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
179 EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
182 TEST_F(ScreenManagerTest, CheckMirrorModeTransitions) {
183 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
184 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
186 GetPrimaryBounds().origin(),
188 screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
189 screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
191 GetSecondaryBounds().origin(),
194 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
195 EXPECT_TRUE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
197 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
199 GetPrimaryBounds().origin(),
201 screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
203 GetPrimaryBounds().origin(),
205 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
206 EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
208 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
210 GetSecondaryBounds().origin(),
212 screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
214 GetPrimaryBounds().origin(),
216 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
217 EXPECT_TRUE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
220 TEST_F(ScreenManagerTest, MonitorGoneInMirrorMode) {
221 screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
222 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
224 GetPrimaryBounds().origin(),
226 screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
227 screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
229 GetPrimaryBounds().origin(),
232 screen_manager_->RemoveDisplayController(kSecondaryCrtc);
234 screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
236 GetPrimaryBounds().origin(),
238 EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
239 EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));