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());
47 DisplayConfigurator::DisplayState output
;
48 output
.display
= &display_snapshot
;
50 std::vector
<DisplayMode
> display_modes
=
51 DisplayChangeObserver::GetExternalDisplayModeList(output
);
52 ASSERT_EQ(6u, display_modes
.size());
53 EXPECT_EQ("640x480", display_modes
[0].size
.ToString());
54 EXPECT_TRUE(display_modes
[0].interlaced
);
55 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
57 EXPECT_EQ("1024x600", display_modes
[1].size
.ToString());
58 EXPECT_FALSE(display_modes
[1].interlaced
);
59 EXPECT_EQ(display_modes
[1].refresh_rate
, 70);
61 EXPECT_EQ("1024x768", display_modes
[2].size
.ToString());
62 EXPECT_TRUE(display_modes
[2].interlaced
);
63 EXPECT_EQ(display_modes
[2].refresh_rate
, 70);
65 EXPECT_EQ("1280x720", display_modes
[3].size
.ToString());
66 EXPECT_FALSE(display_modes
[3].interlaced
);
67 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
69 EXPECT_EQ("1920x1080", display_modes
[4].size
.ToString());
70 EXPECT_FALSE(display_modes
[4].interlaced
);
71 EXPECT_EQ(display_modes
[4].refresh_rate
, 80);
73 EXPECT_EQ("1920x1200", display_modes
[5].size
.ToString());
74 EXPECT_FALSE(display_modes
[5].interlaced
);
75 EXPECT_EQ(display_modes
[5].refresh_rate
, 60);
77 // Outputs without any modes shouldn't cause a crash.
79 display_snapshot
.set_modes(modes
.get());
81 display_modes
= DisplayChangeObserver::GetExternalDisplayModeList(output
);
82 EXPECT_EQ(0u, display_modes
.size());
85 TEST_F(DisplayChangeObserverTest
, GetInternalDisplayModeList
) {
86 ScopedVector
<const ui::DisplayMode
> modes
;
87 // Data picked from peppy.
88 modes
.push_back(new ui::DisplayMode(gfx::Size(1366, 768), false, 60));
89 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), false, 60));
90 modes
.push_back(new ui::DisplayMode(gfx::Size(800, 600), false, 60));
91 modes
.push_back(new ui::DisplayMode(gfx::Size(600, 600), false, 56.2));
92 modes
.push_back(new ui::DisplayMode(gfx::Size(640, 480), false, 59.9));
94 ui::TestDisplaySnapshot display_snapshot
;
95 display_snapshot
.set_modes(modes
.get());
96 display_snapshot
.set_native_mode(modes
[0]);
97 DisplayConfigurator::DisplayState output
;
98 output
.display
= &display_snapshot
;
101 info
.SetBounds(gfx::Rect(0, 0, 1366, 768));
103 std::vector
<DisplayMode
> display_modes
=
104 DisplayChangeObserver::GetInternalDisplayModeList(info
, output
);
105 ASSERT_EQ(5u, display_modes
.size());
106 EXPECT_EQ("1366x768", display_modes
[0].size
.ToString());
107 EXPECT_FALSE(display_modes
[0].native
);
108 EXPECT_NEAR(display_modes
[0].ui_scale
, 0.5, 0.01);
109 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
111 EXPECT_EQ("1366x768", display_modes
[1].size
.ToString());
112 EXPECT_FALSE(display_modes
[1].native
);
113 EXPECT_NEAR(display_modes
[1].ui_scale
, 0.6, 0.01);
114 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);
116 EXPECT_EQ("1366x768", display_modes
[2].size
.ToString());
117 EXPECT_FALSE(display_modes
[2].native
);
118 EXPECT_NEAR(display_modes
[2].ui_scale
, 0.75, 0.01);
119 EXPECT_EQ(display_modes
[2].refresh_rate
, 60);
121 EXPECT_EQ("1366x768", display_modes
[3].size
.ToString());
122 EXPECT_TRUE(display_modes
[3].native
);
123 EXPECT_NEAR(display_modes
[3].ui_scale
, 1.0, 0.01);
124 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
126 EXPECT_EQ("1366x768", display_modes
[4].size
.ToString());
127 EXPECT_FALSE(display_modes
[4].native
);
128 EXPECT_NEAR(display_modes
[4].ui_scale
, 1.125, 0.01);
129 EXPECT_EQ(display_modes
[4].refresh_rate
, 60);
132 TEST_F(DisplayChangeObserverTest
, GetInternalHiDPIDisplayModeList
) {
133 ScopedVector
<const ui::DisplayMode
> modes
;
134 // Data picked from peppy.
135 modes
.push_back(new ui::DisplayMode(gfx::Size(2560, 1700), false, 60));
136 modes
.push_back(new ui::DisplayMode(gfx::Size(2048, 1536), false, 60));
137 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1440), false, 60));
139 ui::TestDisplaySnapshot display_snapshot
;
140 display_snapshot
.set_modes(modes
.get());
141 display_snapshot
.set_native_mode(modes
[0]);
142 DisplayConfigurator::DisplayState output
;
143 output
.display
= &display_snapshot
;
146 info
.SetBounds(gfx::Rect(0, 0, 2560, 1700));
147 info
.set_device_scale_factor(2.0f
);
149 std::vector
<DisplayMode
> display_modes
=
150 DisplayChangeObserver::GetInternalDisplayModeList(info
, output
);
151 ASSERT_EQ(8u, display_modes
.size());
152 EXPECT_EQ("2560x1700", display_modes
[0].size
.ToString());
153 EXPECT_FALSE(display_modes
[0].native
);
154 EXPECT_NEAR(display_modes
[0].ui_scale
, 0.5, 0.01);
155 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
157 EXPECT_EQ("2560x1700", display_modes
[1].size
.ToString());
158 EXPECT_FALSE(display_modes
[1].native
);
159 EXPECT_NEAR(display_modes
[1].ui_scale
, 0.625, 0.01);
160 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);
162 EXPECT_EQ("2560x1700", display_modes
[2].size
.ToString());
163 EXPECT_FALSE(display_modes
[2].native
);
164 EXPECT_NEAR(display_modes
[2].ui_scale
, 0.8, 0.01);
165 EXPECT_EQ(display_modes
[2].refresh_rate
, 60);
167 EXPECT_EQ("2560x1700", display_modes
[3].size
.ToString());
168 EXPECT_FALSE(display_modes
[3].native
);
169 EXPECT_NEAR(display_modes
[3].ui_scale
, 1.0, 0.01);
170 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
172 EXPECT_EQ("2560x1700", display_modes
[4].size
.ToString());
173 EXPECT_FALSE(display_modes
[4].native
);
174 EXPECT_NEAR(display_modes
[4].ui_scale
, 1.125, 0.01);
175 EXPECT_EQ(display_modes
[4].refresh_rate
, 60);
177 EXPECT_EQ("2560x1700", display_modes
[5].size
.ToString());
178 EXPECT_FALSE(display_modes
[5].native
);
179 EXPECT_NEAR(display_modes
[5].ui_scale
, 1.25, 0.01);
180 EXPECT_EQ(display_modes
[5].refresh_rate
, 60);
182 EXPECT_EQ("2560x1700", display_modes
[6].size
.ToString());
183 EXPECT_FALSE(display_modes
[6].native
);
184 EXPECT_NEAR(display_modes
[6].ui_scale
, 1.5, 0.01);
185 EXPECT_EQ(display_modes
[6].refresh_rate
, 60);
187 EXPECT_EQ("2560x1700", display_modes
[7].size
.ToString());
188 EXPECT_TRUE(display_modes
[7].native
);
189 EXPECT_NEAR(display_modes
[7].ui_scale
, 2.0, 0.01);
190 EXPECT_EQ(display_modes
[7].refresh_rate
, 60);
193 TEST_F(DisplayChangeObserverTest
, GetInternalDisplayModeList1_25
) {
194 ScopedVector
<const ui::DisplayMode
> modes
;
195 // Data picked from peppy.
196 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
198 ui::TestDisplaySnapshot display_snapshot
;
199 display_snapshot
.set_modes(modes
.get());
200 display_snapshot
.set_native_mode(modes
[0]);
201 DisplayConfigurator::DisplayState output
;
202 output
.display
= &display_snapshot
;
205 info
.SetBounds(gfx::Rect(0, 0, 1920, 1080));
206 info
.set_device_scale_factor(1.25);
208 std::vector
<DisplayMode
> display_modes
=
209 DisplayChangeObserver::GetInternalDisplayModeList(info
, output
);
210 ASSERT_EQ(5u, display_modes
.size());
211 EXPECT_EQ("1920x1080", display_modes
[0].size
.ToString());
212 EXPECT_FALSE(display_modes
[0].native
);
213 EXPECT_NEAR(display_modes
[0].ui_scale
, 0.5, 0.01);
214 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
216 EXPECT_EQ("1920x1080", display_modes
[1].size
.ToString());
217 EXPECT_FALSE(display_modes
[1].native
);
218 EXPECT_NEAR(display_modes
[1].ui_scale
, 0.625, 0.01);
219 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);
221 EXPECT_EQ("1920x1080", display_modes
[2].size
.ToString());
222 EXPECT_FALSE(display_modes
[2].native
);
223 EXPECT_NEAR(display_modes
[2].ui_scale
, 0.8, 0.01);
224 EXPECT_EQ(display_modes
[2].refresh_rate
, 60);
226 EXPECT_EQ("1920x1080", display_modes
[3].size
.ToString());
227 EXPECT_TRUE(display_modes
[3].native
);
228 EXPECT_NEAR(display_modes
[3].ui_scale
, 1.0, 0.01);
229 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
231 EXPECT_EQ("1920x1080", display_modes
[4].size
.ToString());
232 EXPECT_FALSE(display_modes
[4].native
);
233 EXPECT_NEAR(display_modes
[4].ui_scale
, 1.25, 0.01);
234 EXPECT_EQ(display_modes
[4].refresh_rate
, 60);
237 TEST_F(DisplayChangeObserverTest
, GetExternalDisplayModeList4K
) {
238 ScopedVector
<const ui::DisplayMode
> modes
;
239 modes
.push_back(new ui::DisplayMode(gfx::Size(3840, 2160), false, 30));
240 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1200), false, 60));
242 // All non-interlaced (as would be seen with different refresh rates).
243 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 80));
244 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 70));
245 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
247 // Interlaced vs non-interlaced.
248 modes
.push_back(new ui::DisplayMode(gfx::Size(1280, 720), true, 60));
249 modes
.push_back(new ui::DisplayMode(gfx::Size(1280, 720), false, 60));
252 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), true, 70));
253 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 768), true, 60));
256 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), true, 60));
257 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), false, 70));
258 modes
.push_back(new ui::DisplayMode(gfx::Size(1024, 600), false, 60));
260 // Just one interlaced mode.
261 modes
.push_back(new ui::DisplayMode(gfx::Size(640, 480), true, 60));
263 ui::TestDisplaySnapshot display_snapshot
;
264 display_snapshot
.set_modes(modes
.get());
265 display_snapshot
.set_native_mode(modes
[0]);
266 DisplayConfigurator::DisplayState output
;
267 output
.display
= &display_snapshot
;
269 std::vector
<DisplayMode
> display_modes
=
270 DisplayChangeObserver::GetExternalDisplayModeList(output
);
272 info
.SetDisplayModes(display_modes
); // Sort as external display.
273 display_modes
= info
.display_modes();
275 ASSERT_EQ(9u, display_modes
.size());
276 EXPECT_EQ("640x480", display_modes
[0].size
.ToString());
277 EXPECT_TRUE(display_modes
[0].interlaced
);
278 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
280 EXPECT_EQ("1024x600", display_modes
[1].size
.ToString());
281 EXPECT_FALSE(display_modes
[1].interlaced
);
282 EXPECT_EQ(display_modes
[1].refresh_rate
, 70);
284 EXPECT_EQ("1024x768", display_modes
[2].size
.ToString());
285 EXPECT_TRUE(display_modes
[2].interlaced
);
286 EXPECT_EQ(display_modes
[2].refresh_rate
, 70);
288 EXPECT_EQ("1280x720", display_modes
[3].size
.ToString());
289 EXPECT_FALSE(display_modes
[3].interlaced
);
290 EXPECT_EQ(display_modes
[3].refresh_rate
, 60);
292 EXPECT_EQ("1920x1080", display_modes
[4].size
.ToString());
293 EXPECT_FALSE(display_modes
[4].interlaced
);
294 EXPECT_EQ(display_modes
[4].refresh_rate
, 80);
296 EXPECT_EQ("3840x2160", display_modes
[5].size
.ToString());
297 EXPECT_FALSE(display_modes
[5].interlaced
);
298 EXPECT_FALSE(display_modes
[5].native
);
299 EXPECT_EQ(display_modes
[5].refresh_rate
, 30);
300 EXPECT_EQ(display_modes
[5].device_scale_factor
, 2.0);
302 EXPECT_EQ("1920x1200", display_modes
[6].size
.ToString());
303 EXPECT_FALSE(display_modes
[6].interlaced
);
304 EXPECT_EQ(display_modes
[6].refresh_rate
, 60);
306 EXPECT_EQ("3840x2160", display_modes
[7].size
.ToString());
307 EXPECT_FALSE(display_modes
[7].interlaced
);
308 EXPECT_FALSE(display_modes
[7].native
);
309 EXPECT_EQ(display_modes
[7].refresh_rate
, 30);
310 EXPECT_EQ(display_modes
[7].device_scale_factor
, 1.25);
312 EXPECT_EQ("3840x2160", display_modes
[8].size
.ToString());
313 EXPECT_FALSE(display_modes
[8].interlaced
);
314 EXPECT_TRUE(display_modes
[8].native
);
315 EXPECT_EQ(display_modes
[8].refresh_rate
, 30);
317 // Outputs without any modes shouldn't cause a crash.
319 display_snapshot
.set_modes(modes
.get());
320 display_snapshot
.set_native_mode(NULL
);
322 display_modes
= DisplayChangeObserver::GetExternalDisplayModeList(output
);
323 EXPECT_EQ(0u, display_modes
.size());
328 float ComputeDeviceScaleFactor(float diagonal_inch
,
329 const gfx::Rect
& resolution
) {
330 // We assume that displays have square pixel.
331 float diagonal_pixel
= std::sqrt(std::pow(resolution
.width(), 2) +
332 std::pow(resolution
.height(), 2));
333 float dpi
= diagonal_pixel
/ diagonal_inch
;
334 return DisplayChangeObserver::FindDeviceScaleFactor(dpi
);
339 TEST_F(DisplayChangeObserverTest
, FindDeviceScaleFactor
) {
340 EXPECT_EQ(1.0f
, ComputeDeviceScaleFactor(19.5f
, gfx::Rect(1600, 900)));
343 EXPECT_EQ(1.0f
, ComputeDeviceScaleFactor(21.5f
, gfx::Rect(1920, 1080)));
346 EXPECT_EQ(1.0f
, ComputeDeviceScaleFactor(12.1f
, gfx::Rect(1280, 800)));
349 EXPECT_EQ(1.25f
, ComputeDeviceScaleFactor(11.6f
, gfx::Rect(1920, 1080)));
352 EXPECT_EQ(1.25f
, ComputeDeviceScaleFactor(13.3f
, gfx::Rect(1920, 1080)));
355 EXPECT_EQ(1.25f
, ComputeDeviceScaleFactor(14.0f
, gfx::Rect(1920, 1080)));
358 EXPECT_EQ(2.0f
, ComputeDeviceScaleFactor(12.85f
, gfx::Rect(2560, 1700)));
360 // Erroneous values should still work.
361 EXPECT_EQ(1.0f
, DisplayChangeObserver::FindDeviceScaleFactor(-100.0f
));
362 EXPECT_EQ(1.0f
, DisplayChangeObserver::FindDeviceScaleFactor(0.0f
));
363 EXPECT_EQ(2.0f
, DisplayChangeObserver::FindDeviceScaleFactor(10000.0f
));
366 TEST_F(DisplayChangeObserverTest
,
367 FindExternalDisplayNativeModeWhenOverwritten
) {
368 ScopedVector
<const ui::DisplayMode
> modes
;
369 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), true, 60));
370 modes
.push_back(new ui::DisplayMode(gfx::Size(1920, 1080), false, 60));
372 ui::TestDisplaySnapshot display_snapshot
;
373 display_snapshot
.set_modes(modes
.get());
374 display_snapshot
.set_native_mode(modes
[0]);
375 DisplayConfigurator::DisplayState output
;
376 output
.display
= &display_snapshot
;
378 std::vector
<DisplayMode
> display_modes
=
379 DisplayChangeObserver::GetExternalDisplayModeList(output
);
380 ASSERT_EQ(2u, display_modes
.size());
381 EXPECT_EQ("1920x1080", display_modes
[0].size
.ToString());
382 EXPECT_FALSE(display_modes
[0].interlaced
);
383 EXPECT_FALSE(display_modes
[0].native
);
384 EXPECT_EQ(display_modes
[0].refresh_rate
, 60);
386 EXPECT_EQ("1920x1080", display_modes
[1].size
.ToString());
387 EXPECT_TRUE(display_modes
[1].interlaced
);
388 EXPECT_TRUE(display_modes
[1].native
);
389 EXPECT_EQ(display_modes
[1].refresh_rate
, 60);