1 // Copyright 2013 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 "ash/display/display_change_observer_chromeos.h"
7 #include "ash/display/display_info.h"
8 #include "base/memory/scoped_vector.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/display/chromeos/display_configurator.h"
11 #include "ui/display/chromeos/test/test_display_snapshot.h"
12 #include "ui/display/types/display_mode.h"
14 using ui::DisplayConfigurator
;
16 typedef testing::Test DisplayChangeObserverTest
;
20 TEST_F(DisplayChangeObserverTest
, GetExternalDisplayModeList
) {
21 ScopedVector
<const ui::DisplayMode
> modes
;
22 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1200), false, 60));
24 // All non-interlaced (as would be seen with different refresh rates).
25 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 80));
26 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 70));
27 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
29 // Interlaced vs non-interlaced.
30 modes
.push_back(new ui::DisplayMode(gfx::Size(1280, 720), true, 60));
31 modes
.push_back(new ui::DisplayMode(gfx::Size(1280, 720), false, 60));
34 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), true, 70));
35 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), true, 60));
38 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), true, 60));
39 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), false, 70));
40 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), false, 60));
42 // Just one interlaced mode.
43 modes
.push_back(new ui::DisplayMode(gfx::Size(640, 480), true, 60));
45 ui::TestDisplaySnapshot display_snapshot
;
46 display_snapshot
.set_modes(modes
.get());
48 std::vector
<DisplayMode
> display_modes
=
49 DisplayChangeObserver::GetExternalDisplayModeList(display_snapshot
);
50 ASSERT_EQ(6u, display_modes
.size());
51 EXPECT_EQ("640x480", display_modes
[0].size
.ToString());
52 EXPECT_TRUE(display_modes
[0].interlaced
);
53 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
55 EXPECT_EQ("1024x600", display_modes
[1].size
.ToString());
56 EXPECT_FALSE(display_modes
[1].interlaced
);
57 EXPECT_EQ(display_modes
[1].refresh_rate
, 70);
59 EXPECT_EQ("1024x768", display_modes
[2].size
.ToString());
60 EXPECT_TRUE(display_modes
[2].interlaced
);
61 EXPECT_EQ(display_modes
[2].refresh_rate
, 70);
63 EXPECT_EQ("1280x720", display_modes
[3].size
.ToString());
64 EXPECT_FALSE(display_modes
[3].interlaced
);
65 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
67 EXPECT_EQ("1920x1080", display_modes
[4].size
.ToString());
68 EXPECT_FALSE(display_modes
[4].interlaced
);
69 EXPECT_EQ(display_modes
[4].refresh_rate
, 80);
71 EXPECT_EQ("1920x1200", display_modes
[5].size
.ToString());
72 EXPECT_FALSE(display_modes
[5].interlaced
);
73 EXPECT_EQ(display_modes
[5].refresh_rate
, 60);
75 // Outputs without any modes shouldn't cause a crash.
77 display_snapshot
.set_modes(modes
.get());
80 DisplayChangeObserver::GetExternalDisplayModeList(display_snapshot
);
81 EXPECT_EQ(0u, display_modes
.size());
84 TEST_F(DisplayChangeObserverTest
, GetInternalDisplayModeList
) {
85 ScopedVector
<const ui::DisplayMode
> modes
;
86 // Data picked from peppy.
87 modes
.push_back(new ui::DisplayMode(gfx::Size(1366, 768), false, 60));
88 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), false, 60));
89 modes
.push_back(new ui::DisplayMode(gfx::Size(800, 600), false, 60));
90 modes
.push_back(new ui::DisplayMode(gfx::Size(600, 600), false, 56.2));
91 modes
.push_back(new ui::DisplayMode(gfx::Size(640, 480), false, 59.9));
93 ui::TestDisplaySnapshot display_snapshot
;
94 display_snapshot
.set_modes(modes
.get());
95 display_snapshot
.set_native_mode(modes
[0]);
97 DisplayInfo
info(1, "", false);
98 info
.SetBounds(gfx::Rect(0, 0, 1366, 768));
100 std::vector
<DisplayMode
> display_modes
=
101 DisplayChangeObserver::GetInternalDisplayModeList(info
, display_snapshot
);
102 ASSERT_EQ(5u, display_modes
.size());
103 EXPECT_EQ("1366x768", display_modes
[0].size
.ToString());
104 EXPECT_FALSE(display_modes
[0].native
);
105 EXPECT_NEAR(display_modes
[0].ui_scale
, 0.5, 0.01);
106 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
108 EXPECT_EQ("1366x768", display_modes
[1].size
.ToString());
109 EXPECT_FALSE(display_modes
[1].native
);
110 EXPECT_NEAR(display_modes
[1].ui_scale
, 0.6, 0.01);
111 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);
113 EXPECT_EQ("1366x768", display_modes
[2].size
.ToString());
114 EXPECT_FALSE(display_modes
[2].native
);
115 EXPECT_NEAR(display_modes
[2].ui_scale
, 0.75, 0.01);
116 EXPECT_EQ(display_modes
[2].refresh_rate
, 60);
118 EXPECT_EQ("1366x768", display_modes
[3].size
.ToString());
119 EXPECT_TRUE(display_modes
[3].native
);
120 EXPECT_NEAR(display_modes
[3].ui_scale
, 1.0, 0.01);
121 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
123 EXPECT_EQ("1366x768", display_modes
[4].size
.ToString());
124 EXPECT_FALSE(display_modes
[4].native
);
125 EXPECT_NEAR(display_modes
[4].ui_scale
, 1.125, 0.01);
126 EXPECT_EQ(display_modes
[4].refresh_rate
, 60);
129 TEST_F(DisplayChangeObserverTest
, GetInternalHiDPIDisplayModeList
) {
130 ScopedVector
<const ui::DisplayMode
> modes
;
131 // Data picked from peppy.
132 modes
.push_back(new ui::DisplayMode(gfx::Size(2560, 1700), false, 60));
133 modes
.push_back(new ui::DisplayMode(gfx::Size(2048, 1536), false, 60));
134 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1440), false, 60));
136 ui::TestDisplaySnapshot display_snapshot
;
137 display_snapshot
.set_modes(modes
.get());
138 display_snapshot
.set_native_mode(modes
[0]);
140 DisplayInfo
info(1, "", false);
141 info
.SetBounds(gfx::Rect(0, 0, 2560, 1700));
142 info
.set_device_scale_factor(2.0f
);
144 std::vector
<DisplayMode
> display_modes
=
145 DisplayChangeObserver::GetInternalDisplayModeList(info
, display_snapshot
);
146 ASSERT_EQ(8u, display_modes
.size());
147 EXPECT_EQ("2560x1700", display_modes
[0].size
.ToString());
148 EXPECT_FALSE(display_modes
[0].native
);
149 EXPECT_NEAR(display_modes
[0].ui_scale
, 0.5, 0.01);
150 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
152 EXPECT_EQ("2560x1700", display_modes
[1].size
.ToString());
153 EXPECT_FALSE(display_modes
[1].native
);
154 EXPECT_NEAR(display_modes
[1].ui_scale
, 0.625, 0.01);
155 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);
157 EXPECT_EQ("2560x1700", display_modes
[2].size
.ToString());
158 EXPECT_FALSE(display_modes
[2].native
);
159 EXPECT_NEAR(display_modes
[2].ui_scale
, 0.8, 0.01);
160 EXPECT_EQ(display_modes
[2].refresh_rate
, 60);
162 EXPECT_EQ("2560x1700", display_modes
[3].size
.ToString());
163 EXPECT_FALSE(display_modes
[3].native
);
164 EXPECT_NEAR(display_modes
[3].ui_scale
, 1.0, 0.01);
165 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
167 EXPECT_EQ("2560x1700", display_modes
[4].size
.ToString());
168 EXPECT_FALSE(display_modes
[4].native
);
169 EXPECT_NEAR(display_modes
[4].ui_scale
, 1.125, 0.01);
170 EXPECT_EQ(display_modes
[4].refresh_rate
, 60);
172 EXPECT_EQ("2560x1700", display_modes
[5].size
.ToString());
173 EXPECT_FALSE(display_modes
[5].native
);
174 EXPECT_NEAR(display_modes
[5].ui_scale
, 1.25, 0.01);
175 EXPECT_EQ(display_modes
[5].refresh_rate
, 60);
177 EXPECT_EQ("2560x1700", display_modes
[6].size
.ToString());
178 EXPECT_FALSE(display_modes
[6].native
);
179 EXPECT_NEAR(display_modes
[6].ui_scale
, 1.5, 0.01);
180 EXPECT_EQ(display_modes
[6].refresh_rate
, 60);
182 EXPECT_EQ("2560x1700", display_modes
[7].size
.ToString());
183 EXPECT_TRUE(display_modes
[7].native
);
184 EXPECT_NEAR(display_modes
[7].ui_scale
, 2.0, 0.01);
185 EXPECT_EQ(display_modes
[7].refresh_rate
, 60);
188 TEST_F(DisplayChangeObserverTest
, GetInternalDisplayModeList1_25
) {
189 ScopedVector
<const ui::DisplayMode
> modes
;
190 // Data picked from peppy.
191 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
193 ui::TestDisplaySnapshot display_snapshot
;
194 display_snapshot
.set_modes(modes
.get());
195 display_snapshot
.set_native_mode(modes
[0]);
197 DisplayInfo
info(1, "", false);
198 info
.SetBounds(gfx::Rect(0, 0, 1920, 1080));
199 info
.set_device_scale_factor(1.25);
201 std::vector
<DisplayMode
> display_modes
=
202 DisplayChangeObserver::GetInternalDisplayModeList(info
, display_snapshot
);
203 ASSERT_EQ(5u, display_modes
.size());
204 EXPECT_EQ("1920x1080", display_modes
[0].size
.ToString());
205 EXPECT_FALSE(display_modes
[0].native
);
206 EXPECT_NEAR(display_modes
[0].ui_scale
, 0.5, 0.01);
207 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
209 EXPECT_EQ("1920x1080", display_modes
[1].size
.ToString());
210 EXPECT_FALSE(display_modes
[1].native
);
211 EXPECT_NEAR(display_modes
[1].ui_scale
, 0.625, 0.01);
212 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);
214 EXPECT_EQ("1920x1080", display_modes
[2].size
.ToString());
215 EXPECT_FALSE(display_modes
[2].native
);
216 EXPECT_NEAR(display_modes
[2].ui_scale
, 0.8, 0.01);
217 EXPECT_EQ(display_modes
[2].refresh_rate
, 60);
219 EXPECT_EQ("1920x1080", display_modes
[3].size
.ToString());
220 EXPECT_TRUE(display_modes
[3].native
);
221 EXPECT_NEAR(display_modes
[3].ui_scale
, 1.0, 0.01);
222 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
224 EXPECT_EQ("1920x1080", display_modes
[4].size
.ToString());
225 EXPECT_FALSE(display_modes
[4].native
);
226 EXPECT_NEAR(display_modes
[4].ui_scale
, 1.25, 0.01);
227 EXPECT_EQ(display_modes
[4].refresh_rate
, 60);
230 TEST_F(DisplayChangeObserverTest
, GetExternalDisplayModeList4K
) {
231 ScopedVector
<const ui::DisplayMode
> modes
;
232 modes
.push_back(new ui::DisplayMode(gfx::Size(3840, 2160), false, 30));
233 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1200), false, 60));
235 // All non-interlaced (as would be seen with different refresh rates).
236 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 80));
237 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 70));
238 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
240 // Interlaced vs non-interlaced.
241 modes
.push_back(new ui::DisplayMode(gfx::Size(1280, 720), true, 60));
242 modes
.push_back(new ui::DisplayMode(gfx::Size(1280, 720), false, 60));
245 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), true, 70));
246 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), true, 60));
249 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), true, 60));
250 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), false, 70));
251 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), false, 60));
253 // Just one interlaced mode.
254 modes
.push_back(new ui::DisplayMode(gfx::Size(640, 480), true, 60));
256 ui::TestDisplaySnapshot display_snapshot
;
257 display_snapshot
.set_modes(modes
.get());
258 display_snapshot
.set_native_mode(modes
[0]);
260 std::vector
<DisplayMode
> display_modes
=
261 DisplayChangeObserver::GetExternalDisplayModeList(display_snapshot
);
262 DisplayInfo
info(1, "", false);
263 info
.SetDisplayModes(display_modes
); // Sort as external display.
264 display_modes
= info
.display_modes();
266 ASSERT_EQ(9u, display_modes
.size());
267 EXPECT_EQ("640x480", display_modes
[0].size
.ToString());
268 EXPECT_TRUE(display_modes
[0].interlaced
);
269 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
271 EXPECT_EQ("1024x600", display_modes
[1].size
.ToString());
272 EXPECT_FALSE(display_modes
[1].interlaced
);
273 EXPECT_EQ(display_modes
[1].refresh_rate
, 70);
275 EXPECT_EQ("1024x768", display_modes
[2].size
.ToString());
276 EXPECT_TRUE(display_modes
[2].interlaced
);
277 EXPECT_EQ(display_modes
[2].refresh_rate
, 70);
279 EXPECT_EQ("1280x720", display_modes
[3].size
.ToString());
280 EXPECT_FALSE(display_modes
[3].interlaced
);
281 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
283 EXPECT_EQ("1920x1080", display_modes
[4].size
.ToString());
284 EXPECT_FALSE(display_modes
[4].interlaced
);
285 EXPECT_EQ(display_modes
[4].refresh_rate
, 80);
287 EXPECT_EQ("3840x2160", display_modes
[5].size
.ToString());
288 EXPECT_FALSE(display_modes
[5].interlaced
);
289 EXPECT_FALSE(display_modes
[5].native
);
290 EXPECT_EQ(display_modes
[5].refresh_rate
, 30);
291 EXPECT_EQ(display_modes
[5].device_scale_factor
, 2.0);
293 EXPECT_EQ("1920x1200", display_modes
[6].size
.ToString());
294 EXPECT_FALSE(display_modes
[6].interlaced
);
295 EXPECT_EQ(display_modes
[6].refresh_rate
, 60);
297 EXPECT_EQ("3840x2160", display_modes
[7].size
.ToString());
298 EXPECT_FALSE(display_modes
[7].interlaced
);
299 EXPECT_FALSE(display_modes
[7].native
);
300 EXPECT_EQ(display_modes
[7].refresh_rate
, 30);
301 EXPECT_EQ(display_modes
[7].device_scale_factor
, 1.25);
303 EXPECT_EQ("3840x2160", display_modes
[8].size
.ToString());
304 EXPECT_FALSE(display_modes
[8].interlaced
);
305 EXPECT_TRUE(display_modes
[8].native
);
306 EXPECT_EQ(display_modes
[8].refresh_rate
, 30);
308 // Outputs without any modes shouldn't cause a crash.
310 display_snapshot
.set_modes(modes
.get());
311 display_snapshot
.set_native_mode(NULL
);
314 DisplayChangeObserver::GetExternalDisplayModeList(display_snapshot
);
315 EXPECT_EQ(0u, display_modes
.size());
320 float ComputeDeviceScaleFactor(float diagonal_inch
,
321 const gfx::Rect
& resolution
) {
322 // We assume that displays have square pixel.
323 float diagonal_pixel
= std::sqrt(std::pow(resolution
.width(), 2) +
324 std::pow(resolution
.height(), 2));
325 float dpi
= diagonal_pixel
/ diagonal_inch
;
326 return DisplayChangeObserver::FindDeviceScaleFactor(dpi
);
331 TEST_F(DisplayChangeObserverTest
, FindDeviceScaleFactor
) {
332 EXPECT_EQ(1.0f
, ComputeDeviceScaleFactor(19.5f
, gfx::Rect(1600, 900)));
335 EXPECT_EQ(1.0f
, ComputeDeviceScaleFactor(21.5f
, gfx::Rect(1920, 1080)));
338 EXPECT_EQ(1.0f
, ComputeDeviceScaleFactor(12.1f
, gfx::Rect(1280, 800)));
341 EXPECT_EQ(1.25f
, ComputeDeviceScaleFactor(11.6f
, gfx::Rect(1920, 1080)));
344 EXPECT_EQ(1.25f
, ComputeDeviceScaleFactor(13.3f
, gfx::Rect(1920, 1080)));
347 EXPECT_EQ(1.25f
, ComputeDeviceScaleFactor(14.0f
, gfx::Rect(1920, 1080)));
350 EXPECT_EQ(2.0f
, ComputeDeviceScaleFactor(12.85f
, gfx::Rect(2560, 1700)));
352 // Erroneous values should still work.
353 EXPECT_EQ(1.0f
, DisplayChangeObserver::FindDeviceScaleFactor(-100.0f
));
354 EXPECT_EQ(1.0f
, DisplayChangeObserver::FindDeviceScaleFactor(0.0f
));
355 EXPECT_EQ(2.0f
, DisplayChangeObserver::FindDeviceScaleFactor(10000.0f
));
358 TEST_F(DisplayChangeObserverTest
,
359 FindExternalDisplayNativeModeWhenOverwritten
) {
360 ScopedVector
<const ui::DisplayMode
> modes
;
361 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), true, 60));
362 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
364 ui::TestDisplaySnapshot display_snapshot
;
365 display_snapshot
.set_modes(modes
.get());
366 display_snapshot
.set_native_mode(modes
[0]);
368 std::vector
<DisplayMode
> display_modes
=
369 DisplayChangeObserver::GetExternalDisplayModeList(display_snapshot
);
370 ASSERT_EQ(2u, display_modes
.size());
371 EXPECT_EQ("1920x1080", display_modes
[0].size
.ToString());
372 EXPECT_FALSE(display_modes
[0].interlaced
);
373 EXPECT_FALSE(display_modes
[0].native
);
374 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
376 EXPECT_EQ("1920x1080", display_modes
[1].size
.ToString());
377 EXPECT_TRUE(display_modes
[1].interlaced
);
378 EXPECT_TRUE(display_modes
[1].native
);
379 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);