Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / dri / screen_manager_unittest.cc
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.
4
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"
10
11 namespace {
12
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'}};
16
17 const uint32_t kPrimaryCrtc = 1;
18 const uint32_t kPrimaryConnector = 2;
19 const uint32_t kSecondaryCrtc = 3;
20 const uint32_t kSecondaryConnector = 4;
21
22 class MockScreenManager : public ui::ScreenManager {
23  public:
24   MockScreenManager(ui::DriWrapper* dri,
25                     ui::ScanoutBufferGenerator* buffer_generator)
26       : ScreenManager(dri, buffer_generator), dri_(dri) {}
27
28   virtual void ForceInitializationOfPrimaryDisplay() OVERRIDE {}
29
30  private:
31   ui::DriWrapper* dri_;
32
33   DISALLOW_COPY_AND_ASSIGN(MockScreenManager);
34 };
35
36 }  // namespace
37
38 class ScreenManagerTest : public testing::Test {
39  public:
40   ScreenManagerTest() {}
41   virtual ~ScreenManagerTest() {}
42
43   gfx::Rect GetPrimaryBounds() const {
44     return gfx::Rect(0, 0, kDefaultMode.hdisplay, kDefaultMode.vdisplay);
45   }
46
47   // Secondary is in extended mode, right-of primary.
48   gfx::Rect GetSecondaryBounds() const {
49     return gfx::Rect(
50         kDefaultMode.hdisplay, 0, kDefaultMode.hdisplay, kDefaultMode.vdisplay);
51   }
52
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()));
58   }
59   virtual void TearDown() OVERRIDE {
60     screen_manager_.reset();
61     dri_.reset();
62   }
63
64  protected:
65   scoped_ptr<ui::MockDriWrapper> dri_;
66   scoped_ptr<ui::DriBufferGenerator> buffer_generator_;
67   scoped_ptr<MockScreenManager> screen_manager_;
68
69  private:
70   DISALLOW_COPY_AND_ASSIGN(ScreenManagerTest);
71 };
72
73 TEST_F(ScreenManagerTest, CheckWithNoControllers) {
74   EXPECT_FALSE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
75 }
76
77 TEST_F(ScreenManagerTest, CheckWithValidController) {
78   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
79   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
80                                               kPrimaryConnector,
81                                               GetPrimaryBounds().origin(),
82                                               kDefaultMode);
83   base::WeakPtr<ui::HardwareDisplayController> controller =
84       screen_manager_->GetDisplayController(GetPrimaryBounds());
85
86   EXPECT_TRUE(controller);
87   EXPECT_TRUE(controller->HasCrtc(kPrimaryCrtc));
88 }
89
90 TEST_F(ScreenManagerTest, CheckWithInvalidBounds) {
91   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
92   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
93                                               kPrimaryConnector,
94                                               GetPrimaryBounds().origin(),
95                                               kDefaultMode);
96
97   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
98   EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
99 }
100
101 TEST_F(ScreenManagerTest, CheckForSecondValidController) {
102   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
103   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
104                                               kPrimaryConnector,
105                                               GetPrimaryBounds().origin(),
106                                               kDefaultMode);
107   screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
108   screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
109                                               kSecondaryConnector,
110                                               GetSecondaryBounds().origin(),
111                                               kDefaultMode);
112
113   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
114   EXPECT_TRUE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
115 }
116
117 TEST_F(ScreenManagerTest, CheckControllerAfterItIsRemoved) {
118   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
119   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
120                                               kPrimaryConnector,
121                                               GetPrimaryBounds().origin(),
122                                               kDefaultMode);
123   base::WeakPtr<ui::HardwareDisplayController> controller =
124       screen_manager_->GetDisplayController(GetPrimaryBounds());
125
126   EXPECT_TRUE(controller);
127   screen_manager_->RemoveDisplayController(kPrimaryCrtc);
128   EXPECT_FALSE(controller);
129 }
130
131 TEST_F(ScreenManagerTest, CheckDuplicateConfiguration) {
132   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
133   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
134                                               kPrimaryConnector,
135                                               GetPrimaryBounds().origin(),
136                                               kDefaultMode);
137   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
138                                               kPrimaryConnector,
139                                               GetPrimaryBounds().origin(),
140                                               kDefaultMode);
141
142   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
143   EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
144 }
145
146 TEST_F(ScreenManagerTest, CheckChangingMode) {
147   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
148   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
149                                               kPrimaryConnector,
150                                               GetPrimaryBounds().origin(),
151                                               kDefaultMode);
152   drmModeModeInfo new_mode = kDefaultMode;
153   new_mode.vdisplay = 10;
154   screen_manager_->ConfigureDisplayController(
155       kPrimaryCrtc, kPrimaryConnector, GetPrimaryBounds().origin(), new_mode);
156
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);
164 }
165
166 TEST_F(ScreenManagerTest, CheckForControllersInMirroredMode) {
167   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
168   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
169                                               kPrimaryConnector,
170                                               GetPrimaryBounds().origin(),
171                                               kDefaultMode);
172   screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
173   screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
174                                               kSecondaryConnector,
175                                               GetPrimaryBounds().origin(),
176                                               kDefaultMode);
177
178   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
179   EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
180 }
181
182 TEST_F(ScreenManagerTest, CheckMirrorModeTransitions) {
183   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
184   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
185                                               kPrimaryConnector,
186                                               GetPrimaryBounds().origin(),
187                                               kDefaultMode);
188   screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
189   screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
190                                               kSecondaryConnector,
191                                               GetSecondaryBounds().origin(),
192                                               kDefaultMode);
193
194   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
195   EXPECT_TRUE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
196
197   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
198                                               kPrimaryConnector,
199                                               GetPrimaryBounds().origin(),
200                                               kDefaultMode);
201   screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
202                                               kSecondaryConnector,
203                                               GetPrimaryBounds().origin(),
204                                               kDefaultMode);
205   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
206   EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
207
208   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
209                                               kPrimaryConnector,
210                                               GetSecondaryBounds().origin(),
211                                               kDefaultMode);
212   screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
213                                               kSecondaryConnector,
214                                               GetPrimaryBounds().origin(),
215                                               kDefaultMode);
216   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
217   EXPECT_TRUE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
218 }
219
220 TEST_F(ScreenManagerTest, MonitorGoneInMirrorMode) {
221   screen_manager_->AddDisplayController(kPrimaryCrtc, kPrimaryConnector);
222   screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
223                                               kPrimaryConnector,
224                                               GetPrimaryBounds().origin(),
225                                               kDefaultMode);
226   screen_manager_->AddDisplayController(kSecondaryCrtc, kSecondaryConnector);
227   screen_manager_->ConfigureDisplayController(kSecondaryCrtc,
228                                               kSecondaryConnector,
229                                               GetPrimaryBounds().origin(),
230                                               kDefaultMode);
231
232   screen_manager_->RemoveDisplayController(kSecondaryCrtc);
233   EXPECT_TRUE(
234       screen_manager_->ConfigureDisplayController(kPrimaryCrtc,
235                                                   kPrimaryConnector,
236                                                   GetPrimaryBounds().origin(),
237                                                   kDefaultMode));
238   EXPECT_TRUE(screen_manager_->GetDisplayController(GetPrimaryBounds()));
239   EXPECT_FALSE(screen_manager_->GetDisplayController(GetSecondaryBounds()));
240 }