Add screen space opacity to opacity tree
[chromium-blink-merge.git] / ash / display / display_manager_unittest.cc
blob7ada798f9957cccf0ef8890405c3d61773a5f2fc
1 // Copyright (c) 2012 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_manager.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_controller.h"
9 #include "ash/display/display_info.h"
10 #include "ash/display/display_layout_store.h"
11 #include "ash/display/display_util.h"
12 #include "ash/display/mirror_window_controller.h"
13 #include "ash/screen_util.h"
14 #include "ash/shell.h"
15 #include "ash/test/ash_test_base.h"
16 #include "ash/test/display_manager_test_api.h"
17 #include "ash/test/mirror_window_test_api.h"
18 #include "ash/wm/window_state.h"
19 #include "base/command_line.h"
20 #include "base/format_macros.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/stringprintf.h"
23 #include "ui/aura/client/aura_constants.h"
24 #include "ui/aura/env.h"
25 #include "ui/aura/window_observer.h"
26 #include "ui/aura/window_tree_host.h"
27 #include "ui/events/test/event_generator.h"
28 #include "ui/gfx/display.h"
29 #include "ui/gfx/display_observer.h"
30 #include "ui/gfx/font_render_params.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/gfx/screen_type_delegate.h"
34 namespace ash {
36 using std::vector;
37 using std::string;
39 using base::StringPrintf;
41 namespace {
43 std::string ToDisplayName(int64 id) {
44 return "x-" + base::Int64ToString(id);
47 } // namespace
49 class DisplayManagerTest : public test::AshTestBase,
50 public gfx::DisplayObserver,
51 public aura::WindowObserver {
52 public:
53 DisplayManagerTest()
54 : removed_count_(0U),
55 root_window_destroyed_(false),
56 changed_metrics_(0U) {
58 ~DisplayManagerTest() override {}
60 void SetUp() override {
61 AshTestBase::SetUp();
62 Shell::GetScreen()->AddObserver(this);
63 Shell::GetPrimaryRootWindow()->AddObserver(this);
65 void TearDown() override {
66 Shell::GetPrimaryRootWindow()->RemoveObserver(this);
67 Shell::GetScreen()->RemoveObserver(this);
68 AshTestBase::TearDown();
71 DisplayManager* display_manager() {
72 return Shell::GetInstance()->display_manager();
74 const vector<gfx::Display>& changed() const { return changed_; }
75 const vector<gfx::Display>& added() const { return added_; }
76 uint32_t changed_metrics() const { return changed_metrics_; }
78 string GetCountSummary() const {
79 return StringPrintf("%" PRIuS " %" PRIuS " %" PRIuS,
80 changed_.size(), added_.size(), removed_count_);
83 void reset() {
84 changed_.clear();
85 added_.clear();
86 removed_count_ = 0U;
87 changed_metrics_ = 0U;
88 root_window_destroyed_ = false;
91 bool root_window_destroyed() const {
92 return root_window_destroyed_;
95 const DisplayInfo& GetDisplayInfo(const gfx::Display& display) {
96 return display_manager()->GetDisplayInfo(display.id());
99 const DisplayInfo& GetDisplayInfoAt(int index) {
100 return GetDisplayInfo(display_manager()->GetDisplayAt(index));
103 const gfx::Display& GetDisplayForId(int64 id) {
104 return display_manager()->GetDisplayForId(id);
107 const DisplayInfo& GetDisplayInfoForId(int64 id) {
108 return GetDisplayInfo(display_manager()->GetDisplayForId(id));
111 // aura::DisplayObserver overrides:
112 void OnDisplayMetricsChanged(const gfx::Display& display,
113 uint32_t changed_metrics) override {
114 changed_.push_back(display);
115 changed_metrics_ |= changed_metrics;
117 void OnDisplayAdded(const gfx::Display& new_display) override {
118 added_.push_back(new_display);
120 void OnDisplayRemoved(const gfx::Display& old_display) override {
121 ++removed_count_;
124 // aura::WindowObserver overrides:
125 void OnWindowDestroying(aura::Window* window) override {
126 ASSERT_EQ(Shell::GetPrimaryRootWindow(), window);
127 root_window_destroyed_ = true;
130 private:
131 vector<gfx::Display> changed_;
132 vector<gfx::Display> added_;
133 size_t removed_count_;
134 bool root_window_destroyed_;
135 uint32_t changed_metrics_;
137 DISALLOW_COPY_AND_ASSIGN(DisplayManagerTest);
140 TEST_F(DisplayManagerTest, UpdateDisplayTest) {
141 if (!SupportsMultipleDisplays())
142 return;
144 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
146 // Update primary and add seconary.
147 UpdateDisplay("100+0-500x500,0+501-400x400");
148 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
149 EXPECT_EQ("0,0 500x500",
150 display_manager()->GetDisplayAt(0).bounds().ToString());
152 EXPECT_EQ("1 1 0", GetCountSummary());
153 EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
154 EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
155 EXPECT_EQ("0,0 500x500", changed()[0].bounds().ToString());
156 // Secondary display is on right.
157 EXPECT_EQ("500,0 400x400", added()[0].bounds().ToString());
158 EXPECT_EQ("0,501 400x400",
159 GetDisplayInfo(added()[0]).bounds_in_native().ToString());
160 reset();
162 // Delete secondary.
163 UpdateDisplay("100+0-500x500");
164 EXPECT_EQ("0 0 1", GetCountSummary());
165 reset();
167 // Change primary.
168 UpdateDisplay("1+1-1000x600");
169 EXPECT_EQ("1 0 0", GetCountSummary());
170 EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
171 EXPECT_EQ("0,0 1000x600", changed()[0].bounds().ToString());
172 reset();
174 // Add secondary.
175 UpdateDisplay("1+1-1000x600,1002+0-600x400");
176 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
177 EXPECT_EQ("0 1 0", GetCountSummary());
178 EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
179 // Secondary display is on right.
180 EXPECT_EQ("1000,0 600x400", added()[0].bounds().ToString());
181 EXPECT_EQ("1002,0 600x400",
182 GetDisplayInfo(added()[0]).bounds_in_native().ToString());
183 reset();
185 // Secondary removed, primary changed.
186 UpdateDisplay("1+1-800x300");
187 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
188 EXPECT_EQ("1 0 1", GetCountSummary());
189 EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
190 EXPECT_EQ("0,0 800x300", changed()[0].bounds().ToString());
191 reset();
193 // # of display can go to zero when screen is off.
194 const vector<DisplayInfo> empty;
195 display_manager()->OnNativeDisplaysChanged(empty);
196 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
197 EXPECT_EQ("0 0 0", GetCountSummary());
198 EXPECT_FALSE(root_window_destroyed());
199 // Display configuration stays the same
200 EXPECT_EQ("0,0 800x300",
201 display_manager()->GetDisplayAt(0).bounds().ToString());
202 reset();
204 // Connect to display again
205 UpdateDisplay("100+100-500x400");
206 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
207 EXPECT_EQ("1 0 0", GetCountSummary());
208 EXPECT_FALSE(root_window_destroyed());
209 EXPECT_EQ("0,0 500x400", changed()[0].bounds().ToString());
210 EXPECT_EQ("100,100 500x400",
211 GetDisplayInfo(changed()[0]).bounds_in_native().ToString());
212 reset();
214 // Go back to zero and wake up with multiple displays.
215 display_manager()->OnNativeDisplaysChanged(empty);
216 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
217 EXPECT_FALSE(root_window_destroyed());
218 reset();
220 // Add secondary.
221 UpdateDisplay("0+0-1000x600,1000+1000-600x400");
222 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
223 EXPECT_EQ("0,0 1000x600",
224 display_manager()->GetDisplayAt(0).bounds().ToString());
225 // Secondary display is on right.
226 EXPECT_EQ("1000,0 600x400",
227 display_manager()->GetDisplayAt(1).bounds().ToString());
228 EXPECT_EQ("1000,1000 600x400",
229 GetDisplayInfoAt(1).bounds_in_native().ToString());
230 reset();
232 // Changing primary will update secondary as well.
233 UpdateDisplay("0+0-800x600,1000+1000-600x400");
234 EXPECT_EQ("2 0 0", GetCountSummary());
235 reset();
236 EXPECT_EQ("0,0 800x600",
237 display_manager()->GetDisplayAt(0).bounds().ToString());
238 EXPECT_EQ("800,0 600x400",
239 display_manager()->GetDisplayAt(1).bounds().ToString());
242 TEST_F(DisplayManagerTest, ScaleOnlyChange) {
243 if (!SupportsMultipleDisplays())
244 return;
245 display_manager()->ToggleDisplayScaleFactor();
246 EXPECT_TRUE(changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
247 EXPECT_TRUE(changed_metrics() &
248 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
251 // Test in emulation mode (use_fullscreen_host_window=false)
252 TEST_F(DisplayManagerTest, EmulatorTest) {
253 if (!SupportsMultipleDisplays())
254 return;
256 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
258 display_manager()->AddRemoveDisplay();
259 // Update primary and add seconary.
260 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
261 EXPECT_EQ("0 1 0", GetCountSummary());
262 reset();
264 display_manager()->AddRemoveDisplay();
265 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
266 EXPECT_EQ("0 0 1", GetCountSummary());
267 reset();
269 display_manager()->AddRemoveDisplay();
270 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
271 EXPECT_EQ("0 1 0", GetCountSummary());
272 reset();
275 // Tests support for 3 displays.
276 TEST_F(DisplayManagerTest, UpdateThreeDisplaysTest) {
277 if (!SupportsMultipleDisplays())
278 return;
280 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
282 // Test with three displays.
283 UpdateDisplay("0+0-640x480,640+0-320x200,960+0-400x300");
284 EXPECT_EQ(3U, display_manager()->GetNumDisplays());
285 EXPECT_EQ("0,0 640x480",
286 display_manager()->GetDisplayAt(0).bounds().ToString());
287 EXPECT_EQ("640,0 320x200",
288 display_manager()->GetDisplayAt(1).bounds().ToString());
289 EXPECT_EQ("960,0 400x300",
290 display_manager()->GetDisplayAt(2).bounds().ToString());
292 EXPECT_EQ("1 2 0", GetCountSummary());
293 EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
294 EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
295 EXPECT_EQ(display_manager()->GetDisplayAt(2).id(), added()[1].id());
296 EXPECT_EQ("0,0 640x480", changed()[0].bounds().ToString());
297 // Secondary and terniary displays are on right.
298 EXPECT_EQ("640,0 320x200", added()[0].bounds().ToString());
299 EXPECT_EQ("640,0 320x200",
300 GetDisplayInfo(added()[0]).bounds_in_native().ToString());
301 EXPECT_EQ("960,0 400x300", added()[1].bounds().ToString());
302 EXPECT_EQ("960,0 400x300",
303 GetDisplayInfo(added()[1]).bounds_in_native().ToString());
304 reset();
307 TEST_F(DisplayManagerTest, OverscanInsetsTest) {
308 if (!SupportsMultipleDisplays())
309 return;
311 UpdateDisplay("0+0-500x500,0+501-400x400");
312 reset();
313 ASSERT_EQ(2u, display_manager()->GetNumDisplays());
314 const DisplayInfo& display_info1 = GetDisplayInfoAt(0);
315 const DisplayInfo& display_info2 = GetDisplayInfoAt(1);
316 display_manager()->SetOverscanInsets(
317 display_info2.id(), gfx::Insets(13, 12, 11, 10));
319 std::vector<gfx::Display> changed_displays = changed();
320 EXPECT_EQ(1u, changed_displays.size());
321 EXPECT_EQ(display_info2.id(), changed_displays[0].id());
322 EXPECT_EQ("0,0 500x500",
323 GetDisplayInfoAt(0).bounds_in_native().ToString());
324 DisplayInfo updated_display_info2 = GetDisplayInfoAt(1);
325 EXPECT_EQ("0,501 400x400",
326 updated_display_info2.bounds_in_native().ToString());
327 EXPECT_EQ("378x376",
328 updated_display_info2.size_in_pixel().ToString());
329 EXPECT_EQ("13,12,11,10",
330 updated_display_info2.overscan_insets_in_dip().ToString());
331 EXPECT_EQ("500,0 378x376",
332 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
334 // Make sure that SetOverscanInsets() is idempotent.
335 display_manager()->SetOverscanInsets(display_info1.id(), gfx::Insets());
336 display_manager()->SetOverscanInsets(
337 display_info2.id(), gfx::Insets(13, 12, 11, 10));
338 EXPECT_EQ("0,0 500x500",
339 GetDisplayInfoAt(0).bounds_in_native().ToString());
340 updated_display_info2 = GetDisplayInfoAt(1);
341 EXPECT_EQ("0,501 400x400",
342 updated_display_info2.bounds_in_native().ToString());
343 EXPECT_EQ("378x376",
344 updated_display_info2.size_in_pixel().ToString());
345 EXPECT_EQ("13,12,11,10",
346 updated_display_info2.overscan_insets_in_dip().ToString());
348 display_manager()->SetOverscanInsets(
349 display_info2.id(), gfx::Insets(10, 11, 12, 13));
350 EXPECT_EQ("0,0 500x500",
351 GetDisplayInfoAt(0).bounds_in_native().ToString());
352 EXPECT_EQ("376x378",
353 GetDisplayInfoAt(1).size_in_pixel().ToString());
354 EXPECT_EQ("10,11,12,13",
355 GetDisplayInfoAt(1).overscan_insets_in_dip().ToString());
357 // Recreate a new 2nd display. It won't apply the overscan inset because the
358 // new display has a different ID.
359 UpdateDisplay("0+0-500x500");
360 UpdateDisplay("0+0-500x500,0+501-400x400");
361 EXPECT_EQ("0,0 500x500",
362 GetDisplayInfoAt(0).bounds_in_native().ToString());
363 EXPECT_EQ("0,501 400x400",
364 GetDisplayInfoAt(1).bounds_in_native().ToString());
366 // Recreate the displays with the same ID. It should apply the overscan
367 // inset.
368 UpdateDisplay("0+0-500x500");
369 std::vector<DisplayInfo> display_info_list;
370 display_info_list.push_back(display_info1);
371 display_info_list.push_back(display_info2);
372 display_manager()->OnNativeDisplaysChanged(display_info_list);
373 EXPECT_EQ("1,1 500x500",
374 GetDisplayInfoAt(0).bounds_in_native().ToString());
375 updated_display_info2 = GetDisplayInfoAt(1);
376 EXPECT_EQ("376x378",
377 updated_display_info2.size_in_pixel().ToString());
378 EXPECT_EQ("10,11,12,13",
379 updated_display_info2.overscan_insets_in_dip().ToString());
381 // HiDPI but overscan display. The specified insets size should be doubled.
382 UpdateDisplay("0+0-500x500,0+501-400x400*2");
383 display_manager()->SetOverscanInsets(
384 display_manager()->GetDisplayAt(1).id(), gfx::Insets(4, 5, 6, 7));
385 EXPECT_EQ("0,0 500x500",
386 GetDisplayInfoAt(0).bounds_in_native().ToString());
387 updated_display_info2 = GetDisplayInfoAt(1);
388 EXPECT_EQ("0,501 400x400",
389 updated_display_info2.bounds_in_native().ToString());
390 EXPECT_EQ("376x380",
391 updated_display_info2.size_in_pixel().ToString());
392 EXPECT_EQ("4,5,6,7",
393 updated_display_info2.overscan_insets_in_dip().ToString());
394 EXPECT_EQ("8,10,12,14",
395 updated_display_info2.GetOverscanInsetsInPixel().ToString());
397 // Make sure switching primary display applies the overscan offset only once.
398 ash::Shell::GetInstance()->display_controller()->SetPrimaryDisplay(
399 ScreenUtil::GetSecondaryDisplay());
400 EXPECT_EQ("-500,0 500x500",
401 ScreenUtil::GetSecondaryDisplay().bounds().ToString());
402 EXPECT_EQ("0,0 500x500",
403 GetDisplayInfo(ScreenUtil::GetSecondaryDisplay()).
404 bounds_in_native().ToString());
405 EXPECT_EQ("0,501 400x400",
406 GetDisplayInfo(Shell::GetScreen()->GetPrimaryDisplay()).
407 bounds_in_native().ToString());
408 EXPECT_EQ("0,0 188x190",
409 Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
411 // Make sure just moving the overscan area should property notify observers.
412 UpdateDisplay("0+0-500x500");
413 int64 primary_id = Shell::GetScreen()->GetPrimaryDisplay().id();
414 display_manager()->SetOverscanInsets(primary_id, gfx::Insets(0, 0, 20, 20));
415 EXPECT_EQ("0,0 480x480",
416 Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
417 reset();
418 display_manager()->SetOverscanInsets(primary_id, gfx::Insets(10, 10, 10, 10));
419 EXPECT_TRUE(changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
420 EXPECT_TRUE(
421 changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
422 EXPECT_EQ("0,0 480x480",
423 Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
424 reset();
425 display_manager()->SetOverscanInsets(primary_id, gfx::Insets(0, 0, 0, 0));
426 EXPECT_TRUE(changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
427 EXPECT_TRUE(
428 changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
429 EXPECT_EQ("0,0 500x500",
430 Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
433 TEST_F(DisplayManagerTest, ZeroOverscanInsets) {
434 if (!SupportsMultipleDisplays())
435 return;
437 // Make sure the display change events is emitted for overscan inset changes.
438 UpdateDisplay("0+0-500x500,0+501-400x400");
439 ASSERT_EQ(2u, display_manager()->GetNumDisplays());
440 int64 display2_id = display_manager()->GetDisplayAt(1).id();
442 reset();
443 display_manager()->SetOverscanInsets(display2_id, gfx::Insets(0, 0, 0, 0));
444 EXPECT_EQ(0u, changed().size());
446 reset();
447 display_manager()->SetOverscanInsets(display2_id, gfx::Insets(1, 0, 0, 0));
448 EXPECT_EQ(1u, changed().size());
449 EXPECT_EQ(display2_id, changed()[0].id());
451 reset();
452 display_manager()->SetOverscanInsets(display2_id, gfx::Insets(0, 0, 0, 0));
453 EXPECT_EQ(1u, changed().size());
454 EXPECT_EQ(display2_id, changed()[0].id());
457 TEST_F(DisplayManagerTest, TestDeviceScaleOnlyChange) {
458 if (!SupportsHostWindowResize())
459 return;
461 UpdateDisplay("1000x600");
462 aura::WindowTreeHost* host = Shell::GetPrimaryRootWindow()->GetHost();
463 EXPECT_EQ(1, host->compositor()->device_scale_factor());
464 EXPECT_EQ("1000x600",
465 Shell::GetPrimaryRootWindow()->bounds().size().ToString());
466 EXPECT_EQ("1 0 0", GetCountSummary());
468 UpdateDisplay("1000x600*2");
469 EXPECT_EQ(2, host->compositor()->device_scale_factor());
470 EXPECT_EQ("2 0 0", GetCountSummary());
471 EXPECT_EQ("500x300",
472 Shell::GetPrimaryRootWindow()->bounds().size().ToString());
475 DisplayInfo CreateDisplayInfo(int64 id, const gfx::Rect& bounds) {
476 DisplayInfo info(id, ToDisplayName(id), false);
477 info.SetBounds(bounds);
478 return info;
481 TEST_F(DisplayManagerTest, TestNativeDisplaysChanged) {
482 const int64 internal_display_id =
483 test::DisplayManagerTestApi(display_manager()).
484 SetFirstDisplayAsInternalDisplay();
485 const int external_id = 10;
486 const int mirror_id = 11;
487 const int64 invalid_id = gfx::Display::kInvalidDisplayID;
488 const DisplayInfo internal_display_info =
489 CreateDisplayInfo(internal_display_id, gfx::Rect(0, 0, 500, 500));
490 const DisplayInfo external_display_info =
491 CreateDisplayInfo(external_id, gfx::Rect(1, 1, 100, 100));
492 const DisplayInfo mirroring_display_info =
493 CreateDisplayInfo(mirror_id, gfx::Rect(0, 0, 500, 500));
495 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
496 EXPECT_EQ(1U, display_manager()->num_connected_displays());
497 std::string default_bounds =
498 display_manager()->GetDisplayAt(0).bounds().ToString();
500 std::vector<DisplayInfo> display_info_list;
501 // Primary disconnected.
502 display_manager()->OnNativeDisplaysChanged(display_info_list);
503 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
504 EXPECT_EQ(default_bounds,
505 display_manager()->GetDisplayAt(0).bounds().ToString());
506 EXPECT_EQ(1U, display_manager()->num_connected_displays());
507 EXPECT_FALSE(display_manager()->IsInMirrorMode());
509 if (!SupportsMultipleDisplays())
510 return;
512 // External connected while primary was disconnected.
513 display_info_list.push_back(external_display_info);
514 display_manager()->OnNativeDisplaysChanged(display_info_list);
515 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
517 EXPECT_EQ(invalid_id, GetDisplayForId(internal_display_id).id());
518 EXPECT_EQ("1,1 100x100",
519 GetDisplayInfoForId(external_id).bounds_in_native().ToString());
520 EXPECT_EQ(1U, display_manager()->num_connected_displays());
521 EXPECT_FALSE(display_manager()->IsInMirrorMode());
522 EXPECT_EQ(external_id, Shell::GetScreen()->GetPrimaryDisplay().id());
524 EXPECT_EQ(internal_display_id, gfx::Display::InternalDisplayId());
526 // Primary connected, with different bounds.
527 display_info_list.clear();
528 display_info_list.push_back(internal_display_info);
529 display_info_list.push_back(external_display_info);
530 display_manager()->OnNativeDisplaysChanged(display_info_list);
531 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
532 EXPECT_EQ(internal_display_id, Shell::GetScreen()->GetPrimaryDisplay().id());
534 // This combinatino is new, so internal display becomes primary.
535 EXPECT_EQ("0,0 500x500",
536 GetDisplayForId(internal_display_id).bounds().ToString());
537 EXPECT_EQ("1,1 100x100",
538 GetDisplayInfoForId(10).bounds_in_native().ToString());
539 EXPECT_EQ(2U, display_manager()->num_connected_displays());
540 EXPECT_FALSE(display_manager()->IsInMirrorMode());
541 EXPECT_EQ(ToDisplayName(internal_display_id),
542 display_manager()->GetDisplayNameForId(internal_display_id));
544 // Emulate suspend.
545 display_info_list.clear();
546 display_manager()->OnNativeDisplaysChanged(display_info_list);
547 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
548 EXPECT_EQ("0,0 500x500",
549 GetDisplayForId(internal_display_id).bounds().ToString());
550 EXPECT_EQ("1,1 100x100",
551 GetDisplayInfoForId(10).bounds_in_native().ToString());
552 EXPECT_EQ(2U, display_manager()->num_connected_displays());
553 EXPECT_FALSE(display_manager()->IsInMirrorMode());
554 EXPECT_EQ(ToDisplayName(internal_display_id),
555 display_manager()->GetDisplayNameForId(internal_display_id));
557 // External display has disconnected then resumed.
558 display_info_list.push_back(internal_display_info);
559 display_manager()->OnNativeDisplaysChanged(display_info_list);
560 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
561 EXPECT_EQ("0,0 500x500",
562 GetDisplayForId(internal_display_id).bounds().ToString());
563 EXPECT_EQ(1U, display_manager()->num_connected_displays());
564 EXPECT_FALSE(display_manager()->IsInMirrorMode());
566 // External display was changed during suspend.
567 display_info_list.push_back(external_display_info);
568 display_manager()->OnNativeDisplaysChanged(display_info_list);
569 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
570 EXPECT_EQ(2U, display_manager()->num_connected_displays());
571 EXPECT_FALSE(display_manager()->IsInMirrorMode());
573 // suspend...
574 display_info_list.clear();
575 display_manager()->OnNativeDisplaysChanged(display_info_list);
576 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
577 EXPECT_EQ(2U, display_manager()->num_connected_displays());
578 EXPECT_FALSE(display_manager()->IsInMirrorMode());
580 // and resume with different external display.
581 display_info_list.push_back(internal_display_info);
582 display_info_list.push_back(CreateDisplayInfo(12, gfx::Rect(1, 1, 100, 100)));
583 display_manager()->OnNativeDisplaysChanged(display_info_list);
584 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
585 EXPECT_EQ(2U, display_manager()->num_connected_displays());
586 EXPECT_FALSE(display_manager()->IsInMirrorMode());
587 EXPECT_FALSE(display_manager()->IsInMirrorMode());
589 // mirrored...
590 display_info_list.clear();
591 display_info_list.push_back(internal_display_info);
592 display_info_list.push_back(mirroring_display_info);
593 display_manager()->OnNativeDisplaysChanged(display_info_list);
594 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
595 EXPECT_EQ("0,0 500x500",
596 GetDisplayForId(internal_display_id).bounds().ToString());
597 EXPECT_EQ(2U, display_manager()->num_connected_displays());
598 EXPECT_EQ(11U, display_manager()->mirroring_display_id());
599 EXPECT_TRUE(display_manager()->IsInMirrorMode());
601 // Test display name.
602 EXPECT_EQ(ToDisplayName(internal_display_id),
603 display_manager()->GetDisplayNameForId(internal_display_id));
604 EXPECT_EQ("x-10", display_manager()->GetDisplayNameForId(10));
605 EXPECT_EQ("x-11", display_manager()->GetDisplayNameForId(11));
606 EXPECT_EQ("x-12", display_manager()->GetDisplayNameForId(12));
607 // Default name for the id that doesn't exist.
608 EXPECT_EQ("Display 100", display_manager()->GetDisplayNameForId(100));
610 // and exit mirroring.
611 display_info_list.clear();
612 display_info_list.push_back(internal_display_info);
613 display_info_list.push_back(external_display_info);
614 display_manager()->OnNativeDisplaysChanged(display_info_list);
615 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
616 EXPECT_EQ(2U, display_manager()->num_connected_displays());
617 EXPECT_FALSE(display_manager()->IsInMirrorMode());
618 EXPECT_EQ("0,0 500x500",
619 GetDisplayForId(internal_display_id).bounds().ToString());
620 EXPECT_EQ("500,0 100x100",
621 GetDisplayForId(10).bounds().ToString());
623 // Turn off internal
624 display_info_list.clear();
625 display_info_list.push_back(external_display_info);
626 display_manager()->OnNativeDisplaysChanged(display_info_list);
627 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
628 EXPECT_EQ(invalid_id, GetDisplayForId(internal_display_id).id());
629 EXPECT_EQ("1,1 100x100",
630 GetDisplayInfoForId(external_id).bounds_in_native().ToString());
631 EXPECT_EQ(1U, display_manager()->num_connected_displays());
632 EXPECT_FALSE(display_manager()->IsInMirrorMode());
634 // Switched to another display
635 display_info_list.clear();
636 display_info_list.push_back(internal_display_info);
637 display_manager()->OnNativeDisplaysChanged(display_info_list);
638 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
639 EXPECT_EQ(
640 "0,0 500x500",
641 GetDisplayInfoForId(internal_display_id).bounds_in_native().ToString());
642 EXPECT_EQ(1U, display_manager()->num_connected_displays());
643 EXPECT_FALSE(display_manager()->IsInMirrorMode());
646 // Make sure crash does not happen if add and remove happens at the same time.
647 // See: crbug.com/414394
648 TEST_F(DisplayManagerTest, DisplayAddRemoveAtTheSameTime) {
649 if (!SupportsMultipleDisplays())
650 return;
652 UpdateDisplay("100+0-500x500,0+501-400x400");
654 const int64 primary_id = DisplayController::GetPrimaryDisplayId();
655 const int64 secondary_id = ScreenUtil::GetSecondaryDisplay().id();
657 DisplayInfo primary_info = display_manager()->GetDisplayInfo(primary_id);
658 DisplayInfo secondary_info = display_manager()->GetDisplayInfo(secondary_id);
660 // An id which is different from primary and secondary.
661 const int64 third_id = primary_id + secondary_id;
663 DisplayInfo third_info =
664 CreateDisplayInfo(third_id, gfx::Rect(0, 0, 600, 600));
666 std::vector<DisplayInfo> display_info_list;
667 display_info_list.push_back(third_info);
668 display_info_list.push_back(secondary_info);
669 display_manager()->OnNativeDisplaysChanged(display_info_list);
671 EXPECT_EQ(third_id, DisplayController::GetPrimaryDisplayId());
672 EXPECT_EQ("600x600", GetDisplayForId(third_id).size().ToString());
673 EXPECT_EQ(secondary_id, ScreenUtil::GetSecondaryDisplay().id());
676 #if defined(OS_WIN)
677 // TODO(scottmg): RootWindow doesn't get resized on Windows
678 // Ash. http://crbug.com/247916.
679 #define MAYBE_TestNativeDisplaysChangedNoInternal \
680 DISABLED_TestNativeDisplaysChangedNoInternal
681 #else
682 #define MAYBE_TestNativeDisplaysChangedNoInternal \
683 TestNativeDisplaysChangedNoInternal
684 #endif
686 TEST_F(DisplayManagerTest, MAYBE_TestNativeDisplaysChangedNoInternal) {
687 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
689 // Don't change the display info if all displays are disconnected.
690 std::vector<DisplayInfo> display_info_list;
691 display_manager()->OnNativeDisplaysChanged(display_info_list);
692 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
694 // Connect another display which will become primary.
695 const DisplayInfo external_display_info =
696 CreateDisplayInfo(10, gfx::Rect(1, 1, 100, 100));
697 display_info_list.push_back(external_display_info);
698 display_manager()->OnNativeDisplaysChanged(display_info_list);
699 EXPECT_EQ(1U, display_manager()->GetNumDisplays());
700 EXPECT_EQ("1,1 100x100",
701 GetDisplayInfoForId(10).bounds_in_native().ToString());
702 EXPECT_EQ("100x100", ash::Shell::GetPrimaryRootWindow()->GetHost()->
703 GetBounds().size().ToString());
706 TEST_F(DisplayManagerTest, NativeDisplaysChangedAfterPrimaryChange) {
707 if (!SupportsMultipleDisplays())
708 return;
710 const int64 internal_display_id =
711 test::DisplayManagerTestApi(display_manager()).
712 SetFirstDisplayAsInternalDisplay();
713 const DisplayInfo native_display_info =
714 CreateDisplayInfo(internal_display_id, gfx::Rect(0, 0, 500, 500));
715 const DisplayInfo secondary_display_info =
716 CreateDisplayInfo(10, gfx::Rect(1, 1, 100, 100));
718 std::vector<DisplayInfo> display_info_list;
719 display_info_list.push_back(native_display_info);
720 display_info_list.push_back(secondary_display_info);
721 display_manager()->OnNativeDisplaysChanged(display_info_list);
722 EXPECT_EQ(2U, display_manager()->GetNumDisplays());
723 EXPECT_EQ("0,0 500x500",
724 GetDisplayForId(internal_display_id).bounds().ToString());
725 EXPECT_EQ("500,0 100x100", GetDisplayForId(10).bounds().ToString());
727 ash::Shell::GetInstance()->display_controller()->SetPrimaryDisplay(
728 GetDisplayForId(secondary_display_info.id()));
729 EXPECT_EQ("-500,0 500x500",
730 GetDisplayForId(internal_display_id).bounds().ToString());
731 EXPECT_EQ("0,0 100x100", GetDisplayForId(10).bounds().ToString());
733 // OnNativeDisplaysChanged may change the display bounds. Here makes sure
734 // nothing changed if the exactly same displays are specified.
735 display_manager()->OnNativeDisplaysChanged(display_info_list);
736 EXPECT_EQ("-500,0 500x500",
737 GetDisplayForId(internal_display_id).bounds().ToString());
738 EXPECT_EQ("0,0 100x100", GetDisplayForId(10).bounds().ToString());
741 TEST_F(DisplayManagerTest, DontRememberBestResolution) {
742 int display_id = 1000;
743 DisplayInfo native_display_info =
744 CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1000, 500));
745 std::vector<DisplayMode> display_modes;
746 display_modes.push_back(
747 DisplayMode(gfx::Size(1000, 500), 58.0f, false, true));
748 display_modes.push_back(
749 DisplayMode(gfx::Size(800, 300), 59.0f, false, false));
750 display_modes.push_back(
751 DisplayMode(gfx::Size(400, 500), 60.0f, false, false));
753 native_display_info.SetDisplayModes(display_modes);
755 std::vector<DisplayInfo> display_info_list;
756 display_info_list.push_back(native_display_info);
757 display_manager()->OnNativeDisplaysChanged(display_info_list);
759 DisplayMode mode;
760 DisplayMode expected_mode;
761 expected_mode.size = gfx::Size(1000, 500);
762 EXPECT_FALSE(
763 display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
764 EXPECT_TRUE(expected_mode.IsEquivalent(
765 display_manager()->GetActiveModeForDisplayId(display_id)));
767 // Unsupported resolution.
768 display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 4000));
769 EXPECT_FALSE(
770 display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
771 EXPECT_TRUE(expected_mode.IsEquivalent(
772 display_manager()->GetActiveModeForDisplayId(display_id)));
774 // Supported resolution.
775 display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 300));
776 EXPECT_TRUE(
777 display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
778 EXPECT_EQ("800x300", mode.size.ToString());
779 EXPECT_EQ(59.0f, mode.refresh_rate);
780 EXPECT_FALSE(mode.native);
781 expected_mode.size = gfx::Size(800, 300);
782 EXPECT_TRUE(expected_mode.IsEquivalent(
783 display_manager()->GetActiveModeForDisplayId(display_id)));
785 // Best resolution.
786 display_manager()->SetDisplayResolution(display_id, gfx::Size(1000, 500));
787 EXPECT_TRUE(
788 display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
789 EXPECT_EQ("1000x500", mode.size.ToString());
790 EXPECT_EQ(58.0f, mode.refresh_rate);
791 EXPECT_TRUE(mode.native);
792 expected_mode.size = gfx::Size(1000, 500);
793 EXPECT_TRUE(expected_mode.IsEquivalent(
794 display_manager()->GetActiveModeForDisplayId(display_id)));
797 TEST_F(DisplayManagerTest, ResolutionFallback) {
798 int display_id = 1000;
799 DisplayInfo native_display_info =
800 CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1000, 500));
801 std::vector<DisplayMode> display_modes;
802 display_modes.push_back(
803 DisplayMode(gfx::Size(1000, 500), 58.0f, false, true));
804 display_modes.push_back(
805 DisplayMode(gfx::Size(800, 300), 59.0f, false, false));
806 display_modes.push_back(
807 DisplayMode(gfx::Size(400, 500), 60.0f, false, false));
809 std::vector<DisplayMode> copy = display_modes;
810 native_display_info.SetDisplayModes(copy);
812 std::vector<DisplayInfo> display_info_list;
813 display_info_list.push_back(native_display_info);
814 display_manager()->OnNativeDisplaysChanged(display_info_list);
816 display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 300));
817 DisplayInfo new_native_display_info =
818 CreateDisplayInfo(display_id, gfx::Rect(0, 0, 400, 500));
819 copy = display_modes;
820 new_native_display_info.SetDisplayModes(copy);
821 std::vector<DisplayInfo> new_display_info_list;
822 new_display_info_list.push_back(new_native_display_info);
823 display_manager()->OnNativeDisplaysChanged(new_display_info_list);
825 DisplayMode mode;
826 EXPECT_TRUE(
827 display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
828 EXPECT_EQ("400x500", mode.size.ToString());
829 EXPECT_EQ(60.0f, mode.refresh_rate);
830 EXPECT_FALSE(mode.native);
833 // Best resolution should find itself on the resolutions list.
834 display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 300));
835 DisplayInfo new_native_display_info =
836 CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1000, 500));
837 std::vector<DisplayMode> copy = display_modes;
838 new_native_display_info.SetDisplayModes(copy);
839 std::vector<DisplayInfo> new_display_info_list;
840 new_display_info_list.push_back(new_native_display_info);
841 display_manager()->OnNativeDisplaysChanged(new_display_info_list);
843 DisplayMode mode;
844 EXPECT_TRUE(
845 display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
846 EXPECT_EQ("1000x500", mode.size.ToString());
847 EXPECT_EQ(58.0f, mode.refresh_rate);
848 EXPECT_TRUE(mode.native);
852 TEST_F(DisplayManagerTest, Rotate) {
853 if (!SupportsMultipleDisplays())
854 return;
856 UpdateDisplay("100x200/r,300x400/l");
857 EXPECT_EQ("1,1 100x200",
858 GetDisplayInfoAt(0).bounds_in_native().ToString());
859 EXPECT_EQ("200x100",
860 GetDisplayInfoAt(0).size_in_pixel().ToString());
862 EXPECT_EQ("1,201 300x400",
863 GetDisplayInfoAt(1).bounds_in_native().ToString());
864 EXPECT_EQ("400x300",
865 GetDisplayInfoAt(1).size_in_pixel().ToString());
866 reset();
867 UpdateDisplay("100x200/b,300x400");
868 EXPECT_EQ("2 0 0", GetCountSummary());
869 reset();
871 EXPECT_EQ("1,1 100x200",
872 GetDisplayInfoAt(0).bounds_in_native().ToString());
873 EXPECT_EQ("100x200",
874 GetDisplayInfoAt(0).size_in_pixel().ToString());
876 EXPECT_EQ("1,201 300x400",
877 GetDisplayInfoAt(1).bounds_in_native().ToString());
878 EXPECT_EQ("300x400",
879 GetDisplayInfoAt(1).size_in_pixel().ToString());
881 // Just Rotating display will change the bounds on both display.
882 UpdateDisplay("100x200/l,300x400");
883 EXPECT_EQ("2 0 0", GetCountSummary());
884 reset();
886 // Updating to the same configuration should report no changes.
887 UpdateDisplay("100x200/l,300x400");
888 EXPECT_EQ("0 0 0", GetCountSummary());
889 reset();
891 // Rotating 180 degrees should report one change.
892 UpdateDisplay("100x200/r,300x400");
893 EXPECT_EQ("1 0 0", GetCountSummary());
894 reset();
896 UpdateDisplay("200x200");
897 EXPECT_EQ("1 0 1", GetCountSummary());
898 reset();
900 // Rotating 180 degrees should report one change.
901 UpdateDisplay("200x200/u");
902 EXPECT_EQ("1 0 0", GetCountSummary());
903 reset();
905 UpdateDisplay("200x200/l");
906 EXPECT_EQ("1 0 0", GetCountSummary());
908 // Having the internal display deactivated should restore user rotation. Newly
909 // set rotations should be applied.
910 UpdateDisplay("200x200, 200x200");
911 const int64 internal_display_id =
912 test::DisplayManagerTestApi(display_manager())
913 .SetFirstDisplayAsInternalDisplay();
915 display_manager()->SetDisplayRotation(internal_display_id,
916 gfx::Display::ROTATE_90,
917 gfx::Display::ROTATION_SOURCE_USER);
918 display_manager()->SetDisplayRotation(internal_display_id,
919 gfx::Display::ROTATE_0,
920 gfx::Display::ROTATION_SOURCE_ACTIVE);
922 const DisplayInfo info = GetDisplayInfoForId(internal_display_id);
923 EXPECT_EQ(gfx::Display::ROTATE_0, info.GetActiveRotation());
925 // Deactivate internal display to simulate Docked Mode.
926 vector<DisplayInfo> secondary_only;
927 secondary_only.push_back(GetDisplayInfoAt(1));
928 display_manager()->OnNativeDisplaysChanged(secondary_only);
930 const DisplayInfo post_removal_info =
931 display_manager()->display_info_[internal_display_id];
932 EXPECT_NE(info.GetActiveRotation(), post_removal_info.GetActiveRotation());
933 EXPECT_EQ(gfx::Display::ROTATE_90, post_removal_info.GetActiveRotation());
935 display_manager()->SetDisplayRotation(internal_display_id,
936 gfx::Display::ROTATE_180,
937 gfx::Display::ROTATION_SOURCE_ACTIVE);
938 const DisplayInfo post_rotation_info =
939 display_manager()->display_info_[internal_display_id];
940 EXPECT_NE(info.GetActiveRotation(), post_rotation_info.GetActiveRotation());
941 EXPECT_EQ(gfx::Display::ROTATE_180, post_rotation_info.GetActiveRotation());
944 TEST_F(DisplayManagerTest, UIScale) {
945 DisplayInfo::SetUse125DSFForUIScaling(false);
947 UpdateDisplay("1280x800");
948 int64 display_id = Shell::GetScreen()->GetPrimaryDisplay().id();
949 display_manager()->SetDisplayUIScale(display_id, 1.125f);
950 EXPECT_EQ(1.0, GetDisplayInfoAt(0).configured_ui_scale());
951 display_manager()->SetDisplayUIScale(display_id, 0.8f);
952 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
953 display_manager()->SetDisplayUIScale(display_id, 0.75f);
954 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
955 display_manager()->SetDisplayUIScale(display_id, 0.625f);
956 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
958 test::DisplayManagerTestApi(display_manager())
959 .SetInternalDisplayId(display_id);
961 display_manager()->SetDisplayUIScale(display_id, 1.5f);
962 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
963 display_manager()->SetDisplayUIScale(display_id, 1.25f);
964 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
965 display_manager()->SetDisplayUIScale(display_id, 1.125f);
966 EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
967 display_manager()->SetDisplayUIScale(display_id, 0.8f);
968 EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
969 display_manager()->SetDisplayUIScale(display_id, 0.75f);
970 EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
971 display_manager()->SetDisplayUIScale(display_id, 0.625f);
972 EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
973 display_manager()->SetDisplayUIScale(display_id, 0.6f);
974 EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
975 display_manager()->SetDisplayUIScale(display_id, 0.5f);
976 EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
978 UpdateDisplay("1366x768");
979 display_manager()->SetDisplayUIScale(display_id, 1.5f);
980 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
981 display_manager()->SetDisplayUIScale(display_id, 1.25f);
982 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
983 display_manager()->SetDisplayUIScale(display_id, 1.125f);
984 EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
985 display_manager()->SetDisplayUIScale(display_id, 0.8f);
986 EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
987 display_manager()->SetDisplayUIScale(display_id, 0.75f);
988 EXPECT_EQ(0.75f, GetDisplayInfoAt(0).configured_ui_scale());
989 display_manager()->SetDisplayUIScale(display_id, 0.6f);
990 EXPECT_EQ(0.6f, GetDisplayInfoAt(0).configured_ui_scale());
991 display_manager()->SetDisplayUIScale(display_id, 0.625f);
992 EXPECT_EQ(0.6f, GetDisplayInfoAt(0).configured_ui_scale());
993 display_manager()->SetDisplayUIScale(display_id, 0.5f);
994 EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
996 UpdateDisplay("1280x850*2");
997 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
998 display_manager()->SetDisplayUIScale(display_id, 1.5f);
999 EXPECT_EQ(1.5f, GetDisplayInfoAt(0).configured_ui_scale());
1000 display_manager()->SetDisplayUIScale(display_id, 1.25f);
1001 EXPECT_EQ(1.25f, GetDisplayInfoAt(0).configured_ui_scale());
1002 display_manager()->SetDisplayUIScale(display_id, 1.125f);
1003 EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
1004 display_manager()->SetDisplayUIScale(display_id, 1.0f);
1005 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
1006 gfx::Display display = Shell::GetScreen()->GetPrimaryDisplay();
1007 EXPECT_EQ(2.0f, display.device_scale_factor());
1008 EXPECT_EQ("640x425", display.bounds().size().ToString());
1010 display_manager()->SetDisplayUIScale(display_id, 0.8f);
1011 EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
1012 display_manager()->SetDisplayUIScale(display_id, 0.75f);
1013 EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
1014 display_manager()->SetDisplayUIScale(display_id, 0.625f);
1015 EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
1016 display_manager()->SetDisplayUIScale(display_id, 0.6f);
1017 EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
1018 display_manager()->SetDisplayUIScale(display_id, 0.5f);
1019 EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
1021 display_manager()->SetDisplayUIScale(display_id, 2.0f);
1022 EXPECT_EQ(2.0f, GetDisplayInfoAt(0).configured_ui_scale());
1023 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveUIScale());
1024 display = Shell::GetScreen()->GetPrimaryDisplay();
1025 EXPECT_EQ(1.0f, display.device_scale_factor());
1026 EXPECT_EQ("1280x850", display.bounds().size().ToString());
1028 // 1.25 ui scaling on 1.25 DSF device should use 1.0 DSF
1029 // on screen.
1030 UpdateDisplay("1280x850*1.25");
1031 display_manager()->SetDisplayUIScale(display_id, 1.25f);
1032 EXPECT_EQ(1.25f, GetDisplayInfoAt(0).configured_ui_scale());
1033 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveUIScale());
1034 display = Shell::GetScreen()->GetPrimaryDisplay();
1035 EXPECT_EQ(1.0f, display.device_scale_factor());
1036 EXPECT_EQ("1280x850", display.bounds().size().ToString());
1039 TEST_F(DisplayManagerTest, UIScaleWithDisplayMode) {
1040 int display_id = 1000;
1042 // Setup the display modes with UI-scale.
1043 DisplayInfo native_display_info =
1044 CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1280, 800));
1045 std::vector<DisplayMode> display_modes;
1046 const DisplayMode base_mode(gfx::Size(1280, 800), 60.0f, false, false);
1047 std::vector<DisplayMode> mode_list = CreateInternalDisplayModeList(base_mode);
1048 native_display_info.SetDisplayModes(mode_list);
1050 std::vector<DisplayInfo> display_info_list;
1051 display_info_list.push_back(native_display_info);
1052 display_manager()->OnNativeDisplaysChanged(display_info_list);
1054 DisplayMode expected_mode = base_mode;
1055 EXPECT_TRUE(expected_mode.IsEquivalent(
1056 display_manager()->GetActiveModeForDisplayId(display_id)));
1058 display_manager()->SetDisplayUIScale(display_id, 1.125f);
1059 EXPECT_EQ(1.0, GetDisplayInfoAt(0).configured_ui_scale());
1060 EXPECT_TRUE(expected_mode.IsEquivalent(
1061 display_manager()->GetActiveModeForDisplayId(display_id)));
1062 display_manager()->SetDisplayUIScale(display_id, 0.8f);
1063 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
1064 EXPECT_TRUE(expected_mode.IsEquivalent(
1065 display_manager()->GetActiveModeForDisplayId(display_id)));
1066 display_manager()->SetDisplayUIScale(display_id, 0.75f);
1067 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
1068 EXPECT_TRUE(expected_mode.IsEquivalent(
1069 display_manager()->GetActiveModeForDisplayId(display_id)));
1070 display_manager()->SetDisplayUIScale(display_id, 0.625f);
1071 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
1072 EXPECT_TRUE(expected_mode.IsEquivalent(
1073 display_manager()->GetActiveModeForDisplayId(display_id)));
1075 test::DisplayManagerTestApi(display_manager())
1076 .SetInternalDisplayId(display_id);
1078 display_manager()->SetDisplayUIScale(display_id, 1.5f);
1079 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
1080 EXPECT_TRUE(expected_mode.IsEquivalent(
1081 display_manager()->GetActiveModeForDisplayId(display_id)));
1082 display_manager()->SetDisplayUIScale(display_id, 1.25f);
1083 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
1084 EXPECT_TRUE(expected_mode.IsEquivalent(
1085 display_manager()->GetActiveModeForDisplayId(display_id)));
1086 display_manager()->SetDisplayUIScale(display_id, 1.125f);
1087 EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
1088 expected_mode.ui_scale = 1.125f;
1089 EXPECT_TRUE(expected_mode.IsEquivalent(
1090 display_manager()->GetActiveModeForDisplayId(display_id)));
1091 display_manager()->SetDisplayUIScale(display_id, 0.8f);
1092 EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
1093 expected_mode.ui_scale = 0.8f;
1094 EXPECT_TRUE(expected_mode.IsEquivalent(
1095 display_manager()->GetActiveModeForDisplayId(display_id)));
1096 display_manager()->SetDisplayUIScale(display_id, 0.75f);
1097 EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
1098 EXPECT_TRUE(expected_mode.IsEquivalent(
1099 display_manager()->GetActiveModeForDisplayId(display_id)));
1100 display_manager()->SetDisplayUIScale(display_id, 0.625f);
1101 EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
1102 expected_mode.ui_scale = 0.625f;
1103 EXPECT_TRUE(expected_mode.IsEquivalent(
1104 display_manager()->GetActiveModeForDisplayId(display_id)));
1105 display_manager()->SetDisplayUIScale(display_id, 0.6f);
1106 EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
1107 EXPECT_TRUE(expected_mode.IsEquivalent(
1108 display_manager()->GetActiveModeForDisplayId(display_id)));
1109 display_manager()->SetDisplayUIScale(display_id, 0.5f);
1110 EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
1111 expected_mode.ui_scale = 0.5f;
1112 EXPECT_TRUE(expected_mode.IsEquivalent(
1113 display_manager()->GetActiveModeForDisplayId(display_id)));
1116 TEST_F(DisplayManagerTest, Use125DSFRorUIScaling) {
1117 DisplayInfo::SetUse125DSFForUIScaling(true);
1119 int64 display_id = Shell::GetScreen()->GetPrimaryDisplay().id();
1120 test::DisplayManagerTestApi(display_manager())
1121 .SetInternalDisplayId(display_id);
1123 UpdateDisplay("1920x1080*1.25");
1124 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveDeviceScaleFactor());
1125 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveUIScale());
1127 display_manager()->SetDisplayUIScale(display_id, 0.8f);
1128 EXPECT_EQ(1.25f, GetDisplayInfoAt(0).GetEffectiveDeviceScaleFactor());
1129 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveUIScale());
1130 EXPECT_EQ("1536x864", GetDisplayForId(display_id).size().ToString());
1132 display_manager()->SetDisplayUIScale(display_id, 0.5f);
1133 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveDeviceScaleFactor());
1134 EXPECT_EQ(0.5f, GetDisplayInfoAt(0).GetEffectiveUIScale());
1135 EXPECT_EQ("960x540", GetDisplayForId(display_id).size().ToString());
1137 display_manager()->SetDisplayUIScale(display_id, 1.25f);
1138 EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveDeviceScaleFactor());
1139 EXPECT_EQ(1.25f, GetDisplayInfoAt(0).GetEffectiveUIScale());
1140 EXPECT_EQ("2400x1350", GetDisplayForId(display_id).size().ToString());
1142 DisplayInfo::SetUse125DSFForUIScaling(false);
1145 #if defined(OS_WIN)
1146 // TODO(scottmg): RootWindow doesn't get resized on Windows
1147 // Ash. http://crbug.com/247916.
1148 #define MAYBE_UpdateMouseCursorAfterRotateZoom DISABLED_UpdateMouseCursorAfterRotateZoom
1149 #else
1150 #define MAYBE_UpdateMouseCursorAfterRotateZoom UpdateMouseCursorAfterRotateZoom
1151 #endif
1153 TEST_F(DisplayManagerTest, MAYBE_UpdateMouseCursorAfterRotateZoom) {
1154 // Make sure just rotating will not change native location.
1155 UpdateDisplay("300x200,200x150");
1156 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1157 aura::Env* env = aura::Env::GetInstance();
1159 ui::test::EventGenerator generator1(root_windows[0]);
1160 ui::test::EventGenerator generator2(root_windows[1]);
1162 // Test on 1st display.
1163 generator1.MoveMouseToInHost(150, 50);
1164 EXPECT_EQ("150,50", env->last_mouse_location().ToString());
1165 UpdateDisplay("300x200/r,200x150");
1166 EXPECT_EQ("50,149", env->last_mouse_location().ToString());
1168 // Test on 2nd display.
1169 generator2.MoveMouseToInHost(50, 100);
1170 EXPECT_EQ("250,100", env->last_mouse_location().ToString());
1171 UpdateDisplay("300x200/r,200x150/l");
1172 EXPECT_EQ("249,50", env->last_mouse_location().ToString());
1174 // The native location is now outside, so move to the center
1175 // of closest display.
1176 UpdateDisplay("300x200/r,100x50/l");
1177 EXPECT_EQ("225,50", env->last_mouse_location().ToString());
1179 // Make sure just zooming will not change native location.
1180 UpdateDisplay("600x400*2,400x300");
1182 // Test on 1st display.
1183 generator1.MoveMouseToInHost(200, 300);
1184 EXPECT_EQ("100,150", env->last_mouse_location().ToString());
1185 UpdateDisplay("600x400*2@1.5,400x300");
1186 EXPECT_EQ("150,225", env->last_mouse_location().ToString());
1188 // Test on 2nd display.
1189 UpdateDisplay("600x400,400x300*2");
1190 generator2.MoveMouseToInHost(200, 250);
1191 EXPECT_EQ("700,125", env->last_mouse_location().ToString());
1192 UpdateDisplay("600x400,400x300*2@1.5");
1193 EXPECT_EQ("750,187", env->last_mouse_location().ToString());
1195 // The native location is now outside, so move to the
1196 // center of closest display.
1197 UpdateDisplay("600x400,400x200*2@1.5");
1198 EXPECT_EQ("750,75", env->last_mouse_location().ToString());
1201 class TestDisplayObserver : public gfx::DisplayObserver {
1202 public:
1203 TestDisplayObserver() : changed_(false) {}
1204 ~TestDisplayObserver() override {}
1206 // gfx::DisplayObserver overrides:
1207 void OnDisplayMetricsChanged(const gfx::Display&, uint32_t) override {}
1208 void OnDisplayAdded(const gfx::Display& new_display) override {
1209 // Mirror window should already be delete before restoring
1210 // the external display.
1211 EXPECT_FALSE(test_api.GetHost());
1212 changed_ = true;
1214 void OnDisplayRemoved(const gfx::Display& old_display) override {
1215 // Mirror window should not be created until the external display
1216 // is removed.
1217 EXPECT_FALSE(test_api.GetHost());
1218 changed_ = true;
1221 bool changed_and_reset() {
1222 bool changed = changed_;
1223 changed_ = false;
1224 return changed;
1227 private:
1228 test::MirrorWindowTestApi test_api;
1229 bool changed_;
1231 DISALLOW_COPY_AND_ASSIGN(TestDisplayObserver);
1234 TEST_F(DisplayManagerTest, SoftwareMirroring) {
1235 if (!SupportsMultipleDisplays())
1236 return;
1238 UpdateDisplay("300x400,400x500");
1240 test::MirrorWindowTestApi test_api;
1241 EXPECT_EQ(NULL, test_api.GetHost());
1243 TestDisplayObserver display_observer;
1244 Shell::GetScreen()->AddObserver(&display_observer);
1246 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1247 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
1248 display_manager->UpdateDisplays();
1249 RunAllPendingInMessageLoop();
1250 EXPECT_TRUE(display_observer.changed_and_reset());
1251 EXPECT_EQ(1U, display_manager->GetNumDisplays());
1252 EXPECT_EQ("0,0 300x400",
1253 Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
1254 EXPECT_EQ("400x500", test_api.GetHost()->GetBounds().size().ToString());
1255 EXPECT_EQ("300x400",
1256 test_api.GetHost()->window()->bounds().size().ToString());
1257 EXPECT_TRUE(display_manager->IsInMirrorMode());
1259 display_manager->SetMirrorMode(false);
1260 EXPECT_TRUE(display_observer.changed_and_reset());
1261 EXPECT_EQ(NULL, test_api.GetHost());
1262 EXPECT_EQ(2U, display_manager->GetNumDisplays());
1263 EXPECT_FALSE(display_manager->IsInMirrorMode());
1265 // Make sure the mirror window has the pixel size of the
1266 // source display.
1267 display_manager->SetMirrorMode(true);
1268 EXPECT_TRUE(display_observer.changed_and_reset());
1270 UpdateDisplay("300x400@0.5,400x500");
1271 EXPECT_FALSE(display_observer.changed_and_reset());
1272 EXPECT_EQ("300x400",
1273 test_api.GetHost()->window()->bounds().size().ToString());
1275 UpdateDisplay("310x410*2,400x500");
1276 EXPECT_FALSE(display_observer.changed_and_reset());
1277 EXPECT_EQ("310x410",
1278 test_api.GetHost()->window()->bounds().size().ToString());
1280 UpdateDisplay("320x420/r,400x500");
1281 EXPECT_FALSE(display_observer.changed_and_reset());
1282 EXPECT_EQ("320x420",
1283 test_api.GetHost()->window()->bounds().size().ToString());
1285 UpdateDisplay("330x440/r,400x500");
1286 EXPECT_FALSE(display_observer.changed_and_reset());
1287 EXPECT_EQ("330x440",
1288 test_api.GetHost()->window()->bounds().size().ToString());
1290 // Overscan insets are ignored.
1291 UpdateDisplay("400x600/o,600x800/o");
1292 EXPECT_FALSE(display_observer.changed_and_reset());
1293 EXPECT_EQ("400x600",
1294 test_api.GetHost()->window()->bounds().size().ToString());
1296 Shell::GetScreen()->RemoveObserver(&display_observer);
1299 TEST_F(DisplayManagerTest, SingleDisplayToSoftwareMirroring) {
1300 if (!SupportsMultipleDisplays())
1301 return;
1302 UpdateDisplay("600x400");
1304 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1305 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
1306 UpdateDisplay("600x400,600x400");
1308 EXPECT_TRUE(display_manager->IsInMirrorMode());
1309 EXPECT_EQ(1U, display_manager->GetNumDisplays());
1310 DisplayController* display_controller =
1311 ash::Shell::GetInstance()->display_controller();
1312 EXPECT_TRUE(display_controller->mirror_window_controller()->GetWindow());
1314 UpdateDisplay("600x400");
1315 EXPECT_FALSE(display_manager->IsInMirrorMode());
1316 EXPECT_EQ(1U, display_manager->GetNumDisplays());
1317 EXPECT_FALSE(display_controller->mirror_window_controller()->GetWindow());
1320 #if defined(OS_CHROMEOS)
1321 // Make sure this does not cause any crashes. See http://crbug.com/412910
1322 // This test is limited to OS_CHROMEOS because CursorCompositingEnabled is only
1323 // for ChromeOS.
1324 TEST_F(DisplayManagerTest, SoftwareMirroringWithCompositingCursor) {
1325 if (!SupportsMultipleDisplays())
1326 return;
1328 UpdateDisplay("300x400,400x500");
1330 test::MirrorWindowTestApi test_api;
1331 EXPECT_EQ(NULL, test_api.GetHost());
1333 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1334 DisplayInfo secondary_info = display_manager->GetDisplayInfo(
1335 ScreenUtil::GetSecondaryDisplay().id());
1337 display_manager->SetSoftwareMirroring(true);
1338 display_manager->UpdateDisplays();
1340 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
1341 EXPECT_FALSE(root_windows[0]->Contains(test_api.GetCursorWindow()));
1343 Shell::GetInstance()->SetCursorCompositingEnabled(true);
1345 EXPECT_TRUE(root_windows[0]->Contains(test_api.GetCursorWindow()));
1347 // Removes the first display and keeps the second one.
1348 display_manager->SetSoftwareMirroring(false);
1349 std::vector<DisplayInfo> new_info_list;
1350 new_info_list.push_back(secondary_info);
1351 display_manager->OnNativeDisplaysChanged(new_info_list);
1353 root_windows = Shell::GetAllRootWindows();
1354 EXPECT_TRUE(root_windows[0]->Contains(test_api.GetCursorWindow()));
1356 Shell::GetInstance()->SetCursorCompositingEnabled(false);
1358 #endif // OS_CHROMEOS
1360 TEST_F(DisplayManagerTest, MirroredLayout) {
1361 if (!SupportsMultipleDisplays())
1362 return;
1364 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1365 UpdateDisplay("500x500,400x400");
1366 EXPECT_FALSE(display_manager->GetCurrentDisplayLayout().mirrored);
1367 EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1368 EXPECT_EQ(2U, display_manager->num_connected_displays());
1370 UpdateDisplay("1+0-500x500,1+0-500x500");
1371 EXPECT_TRUE(display_manager->GetCurrentDisplayLayout().mirrored);
1372 EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
1373 EXPECT_EQ(2U, display_manager->num_connected_displays());
1375 UpdateDisplay("500x500,500x500");
1376 EXPECT_FALSE(display_manager->GetCurrentDisplayLayout().mirrored);
1377 EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1378 EXPECT_EQ(2U, display_manager->num_connected_displays());
1381 TEST_F(DisplayManagerTest, InvertLayout) {
1382 EXPECT_EQ("left, 0",
1383 DisplayLayout(DisplayLayout::RIGHT, 0).Invert().ToString());
1384 EXPECT_EQ("left, -100",
1385 DisplayLayout(DisplayLayout::RIGHT, 100).Invert().ToString());
1386 EXPECT_EQ("left, 50",
1387 DisplayLayout(DisplayLayout::RIGHT, -50).Invert().ToString());
1389 EXPECT_EQ("right, 0",
1390 DisplayLayout(DisplayLayout::LEFT, 0).Invert().ToString());
1391 EXPECT_EQ("right, -90",
1392 DisplayLayout(DisplayLayout::LEFT, 90).Invert().ToString());
1393 EXPECT_EQ("right, 60",
1394 DisplayLayout(DisplayLayout::LEFT, -60).Invert().ToString());
1396 EXPECT_EQ("bottom, 0",
1397 DisplayLayout(DisplayLayout::TOP, 0).Invert().ToString());
1398 EXPECT_EQ("bottom, -80",
1399 DisplayLayout(DisplayLayout::TOP, 80).Invert().ToString());
1400 EXPECT_EQ("bottom, 70",
1401 DisplayLayout(DisplayLayout::TOP, -70).Invert().ToString());
1403 EXPECT_EQ("top, 0",
1404 DisplayLayout(DisplayLayout::BOTTOM, 0).Invert().ToString());
1405 EXPECT_EQ("top, -70",
1406 DisplayLayout(DisplayLayout::BOTTOM, 70).Invert().ToString());
1407 EXPECT_EQ("top, 80",
1408 DisplayLayout(DisplayLayout::BOTTOM, -80).Invert().ToString());
1411 TEST_F(DisplayManagerTest, NotifyPrimaryChange) {
1412 if (!SupportsMultipleDisplays())
1413 return;
1414 UpdateDisplay("500x500,500x500");
1415 ash::Shell::GetInstance()->display_controller()->SwapPrimaryDisplay();
1416 reset();
1417 UpdateDisplay("500x500");
1418 EXPECT_FALSE(changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
1419 EXPECT_FALSE(changed_metrics() &
1420 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
1421 EXPECT_TRUE(changed_metrics() &
1422 gfx::DisplayObserver::DISPLAY_METRIC_PRIMARY);
1424 UpdateDisplay("500x500,500x500");
1425 ash::Shell::GetInstance()->display_controller()->SwapPrimaryDisplay();
1426 reset();
1427 UpdateDisplay("500x400");
1428 EXPECT_TRUE(changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
1429 EXPECT_TRUE(changed_metrics() &
1430 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
1431 EXPECT_TRUE(changed_metrics() &
1432 gfx::DisplayObserver::DISPLAY_METRIC_PRIMARY);
1435 TEST_F(DisplayManagerTest, NotifyPrimaryChangeUndock) {
1436 if (!SupportsMultipleDisplays())
1437 return;
1438 // Assume the default display is an external display, and
1439 // emulates undocking by switching to another display.
1440 DisplayInfo another_display_info =
1441 CreateDisplayInfo(1, gfx::Rect(0, 0, 1280, 800));
1442 std::vector<DisplayInfo> info_list;
1443 info_list.push_back(another_display_info);
1444 reset();
1445 display_manager()->OnNativeDisplaysChanged(info_list);
1446 EXPECT_TRUE(changed_metrics() & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS);
1447 EXPECT_TRUE(changed_metrics() &
1448 gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA);
1449 EXPECT_TRUE(changed_metrics() &
1450 gfx::DisplayObserver::DISPLAY_METRIC_PRIMARY);
1453 #if defined(OS_WIN)
1454 // TODO(scottmg): RootWindow doesn't get resized on Windows
1455 // Ash. http://crbug.com/247916.
1456 #define MAYBE_UpdateDisplayWithHostOrigin DISABLED_UpdateDisplayWithHostOrigin
1457 #else
1458 #define MAYBE_UpdateDisplayWithHostOrigin UpdateDisplayWithHostOrigin
1459 #endif
1461 TEST_F(DisplayManagerTest, MAYBE_UpdateDisplayWithHostOrigin) {
1462 UpdateDisplay("100x200,300x400");
1463 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1464 aura::Window::Windows root_windows =
1465 Shell::GetInstance()->GetAllRootWindows();
1466 ASSERT_EQ(2U, root_windows.size());
1467 aura::WindowTreeHost* host0 = root_windows[0]->GetHost();
1468 aura::WindowTreeHost* host1 = root_windows[1]->GetHost();
1470 EXPECT_EQ("1,1", host0->GetBounds().origin().ToString());
1471 EXPECT_EQ("100x200", host0->GetBounds().size().ToString());
1472 // UpdateDisplay set the origin if it's not set.
1473 EXPECT_NE("1,1", host1->GetBounds().origin().ToString());
1474 EXPECT_EQ("300x400", host1->GetBounds().size().ToString());
1476 UpdateDisplay("100x200,200+300-300x400");
1477 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1478 EXPECT_EQ("0,0", host0->GetBounds().origin().ToString());
1479 EXPECT_EQ("100x200", host0->GetBounds().size().ToString());
1480 EXPECT_EQ("200,300", host1->GetBounds().origin().ToString());
1481 EXPECT_EQ("300x400", host1->GetBounds().size().ToString());
1483 UpdateDisplay("400+500-200x300,300x400");
1484 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1485 EXPECT_EQ("400,500", host0->GetBounds().origin().ToString());
1486 EXPECT_EQ("200x300", host0->GetBounds().size().ToString());
1487 EXPECT_EQ("0,0", host1->GetBounds().origin().ToString());
1488 EXPECT_EQ("300x400", host1->GetBounds().size().ToString());
1490 UpdateDisplay("100+200-100x200,300+500-200x300");
1491 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1492 EXPECT_EQ("100,200", host0->GetBounds().origin().ToString());
1493 EXPECT_EQ("100x200", host0->GetBounds().size().ToString());
1494 EXPECT_EQ("300,500", host1->GetBounds().origin().ToString());
1495 EXPECT_EQ("200x300", host1->GetBounds().size().ToString());
1498 TEST_F(DisplayManagerTest, UnifiedDesktopBasic) {
1499 if (!SupportsMultipleDisplays())
1500 return;
1501 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest();
1503 // Don't check root window destruction in unified mode.
1504 Shell::GetPrimaryRootWindow()->RemoveObserver(this);
1506 UpdateDisplay("300x200,400x500");
1508 // Defaults to the unified desktop.
1509 gfx::Screen* screen =
1510 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
1511 EXPECT_EQ("700x500", screen->GetPrimaryDisplay().size().ToString());
1513 display_manager()->SetMirrorMode(true);
1514 EXPECT_EQ("300x200", screen->GetPrimaryDisplay().size().ToString());
1516 display_manager()->SetMirrorMode(false);
1517 EXPECT_EQ("700x500", screen->GetPrimaryDisplay().size().ToString());
1519 // Switch to single desktop.
1520 UpdateDisplay("500x300");
1521 EXPECT_EQ("500x300", screen->GetPrimaryDisplay().size().ToString());
1523 // Switch to unified desktop.
1524 UpdateDisplay("500x300,400x500");
1525 EXPECT_EQ("900x500", screen->GetPrimaryDisplay().size().ToString());
1527 // Switch back to extended desktop.
1528 display_manager()->SetDefaultMultiDisplayMode(DisplayManager::EXTENDED);
1529 display_manager()->ReconfigureDisplays();
1530 EXPECT_EQ("500x300", screen->GetPrimaryDisplay().size().ToString());
1531 EXPECT_EQ("400x500", ScreenUtil::GetSecondaryDisplay().size().ToString());
1534 // Updating displays again in unified desktop mode should not crash.
1535 // crbug.com/491094.
1536 TEST_F(DisplayManagerTest, ConfigureUnifiedTwice) {
1537 if (!SupportsMultipleDisplays())
1538 return;
1539 // Don't check root window destruction in unified mode.
1540 Shell::GetPrimaryRootWindow()->RemoveObserver(this);
1542 UpdateDisplay("300x200,400x500");
1543 // Mirror windows are created in a posted task.
1544 RunAllPendingInMessageLoop();
1546 UpdateDisplay("300x250,400x550");
1547 RunAllPendingInMessageLoop();
1550 TEST_F(DisplayManagerTest, RotateUnifiedDesktop) {
1551 if (!SupportsMultipleDisplays())
1552 return;
1553 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest();
1555 // Don't check root window destruction in unified mode.
1556 Shell::GetPrimaryRootWindow()->RemoveObserver(this);
1558 UpdateDisplay("300x200,400x500");
1560 gfx::Screen* screen =
1561 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
1562 const gfx::Display& display = screen->GetPrimaryDisplay();
1563 EXPECT_EQ("700x500", display.size().ToString());
1564 display_manager()->SetDisplayRotation(display.id(), gfx::Display::ROTATE_90,
1565 gfx::Display::ROTATION_SOURCE_ACTIVE);
1566 EXPECT_EQ("500x700", screen->GetPrimaryDisplay().size().ToString());
1567 display_manager()->SetDisplayRotation(display.id(), gfx::Display::ROTATE_0,
1568 gfx::Display::ROTATION_SOURCE_ACTIVE);
1569 EXPECT_EQ("700x500", screen->GetPrimaryDisplay().size().ToString());
1571 UpdateDisplay("300x200");
1572 EXPECT_EQ("300x200", screen->GetPrimaryDisplay().size().ToString());
1575 // Makes sure the transition from unified to single won't crash
1576 // with docked windows.
1577 TEST_F(DisplayManagerTest, UnifiedWithDockWindows) {
1578 if (!SupportsMultipleDisplays())
1579 return;
1580 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest();
1582 // Don't check root window destruction in unified mode.
1583 Shell::GetPrimaryRootWindow()->RemoveObserver(this);
1585 UpdateDisplay("300x200,400x500");
1587 scoped_ptr<aura::Window> docked(
1588 CreateTestWindowInShellWithBounds(gfx::Rect(10, 10, 50, 50)));
1589 docked->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_DOCKED);
1590 ASSERT_TRUE(wm::GetWindowState(docked.get())->IsDocked());
1591 // 47 pixels reserved for launcher shelf height.
1592 EXPECT_EQ("0,0 250x453", docked->bounds().ToString());
1593 UpdateDisplay("300x300");
1594 // Make sure the window is still docked.
1595 EXPECT_TRUE(wm::GetWindowState(docked.get())->IsDocked());
1596 EXPECT_EQ("0,0 250x253", docked->bounds().ToString());
1599 class ScreenShutdownTest : public test::AshTestBase {
1600 public:
1601 ScreenShutdownTest() {
1603 ~ScreenShutdownTest() override {}
1605 void TearDown() override {
1606 gfx::Screen* orig_screen =
1607 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
1608 AshTestBase::TearDown();
1609 if (!SupportsMultipleDisplays())
1610 return;
1611 gfx::Screen* screen =
1612 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
1613 EXPECT_NE(orig_screen, screen);
1614 EXPECT_EQ(2, screen->GetNumDisplays());
1615 EXPECT_EQ("500x300", screen->GetPrimaryDisplay().size().ToString());
1616 std::vector<gfx::Display> all = screen->GetAllDisplays();
1617 EXPECT_EQ("500x300", all[0].size().ToString());
1618 EXPECT_EQ("800x400", all[1].size().ToString());
1621 private:
1622 DISALLOW_COPY_AND_ASSIGN(ScreenShutdownTest);
1625 TEST_F(ScreenShutdownTest, ScreenAfterShutdown) {
1626 if (!SupportsMultipleDisplays())
1627 return;
1628 UpdateDisplay("500x300,800x400");
1632 #if defined(OS_CHROMEOS)
1633 namespace {
1635 // A helper class that sets the display configuration and starts ash.
1636 // This is to make sure the font configuration happens during ash
1637 // initialization process.
1638 class FontTestHelper : public test::AshTestBase {
1639 public:
1640 enum DisplayType {
1641 INTERNAL,
1642 EXTERNAL
1645 FontTestHelper(float scale, DisplayType display_type) {
1646 gfx::ClearFontRenderParamsCacheForTest();
1647 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
1648 if (display_type == INTERNAL)
1649 command_line->AppendSwitch(switches::kAshUseFirstDisplayAsInternal);
1650 command_line->AppendSwitchASCII(switches::kAshHostWindowBounds,
1651 StringPrintf("1000x800*%f", scale));
1652 SetUp();
1655 ~FontTestHelper() override { TearDown(); }
1657 // test::AshTestBase:
1658 void TestBody() override { NOTREACHED(); }
1660 private:
1661 DISALLOW_COPY_AND_ASSIGN(FontTestHelper);
1665 bool IsTextSubpixelPositioningEnabled() {
1666 gfx::FontRenderParams params =
1667 gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), NULL);
1668 return params.subpixel_positioning;
1671 gfx::FontRenderParams::Hinting GetFontHintingParams() {
1672 gfx::FontRenderParams params =
1673 gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), NULL);
1674 return params.hinting;
1677 } // namespace
1679 typedef testing::Test DisplayManagerFontTest;
1681 TEST_F(DisplayManagerFontTest, TextSubpixelPositioningWithDsf100Internal) {
1682 FontTestHelper helper(1.0f, FontTestHelper::INTERNAL);
1683 ASSERT_DOUBLE_EQ(
1684 1.0f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1685 EXPECT_FALSE(IsTextSubpixelPositioningEnabled());
1686 EXPECT_NE(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1689 TEST_F(DisplayManagerFontTest, TextSubpixelPositioningWithDsf125Internal) {
1690 FontTestHelper helper(1.25f, FontTestHelper::INTERNAL);
1691 ASSERT_DOUBLE_EQ(
1692 1.25f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1693 EXPECT_TRUE(IsTextSubpixelPositioningEnabled());
1694 EXPECT_EQ(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1697 TEST_F(DisplayManagerFontTest, TextSubpixelPositioningWithDsf200Internal) {
1698 FontTestHelper helper(2.0f, FontTestHelper::INTERNAL);
1699 ASSERT_DOUBLE_EQ(
1700 2.0f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1701 EXPECT_TRUE(IsTextSubpixelPositioningEnabled());
1702 EXPECT_EQ(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1704 Shell::GetInstance()->display_manager()->SetDisplayUIScale(
1705 Shell::GetScreen()->GetPrimaryDisplay().id(), 2.0f);
1707 ASSERT_DOUBLE_EQ(
1708 1.0f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1709 EXPECT_FALSE(IsTextSubpixelPositioningEnabled());
1710 EXPECT_NE(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1713 TEST_F(DisplayManagerFontTest, TextSubpixelPositioningWithDsf100External) {
1714 FontTestHelper helper(1.0f, FontTestHelper::EXTERNAL);
1715 ASSERT_DOUBLE_EQ(
1716 1.0f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1717 EXPECT_FALSE(IsTextSubpixelPositioningEnabled());
1718 EXPECT_NE(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1721 TEST_F(DisplayManagerFontTest, TextSubpixelPositioningWithDsf125External) {
1722 FontTestHelper helper(1.25f, FontTestHelper::EXTERNAL);
1723 ASSERT_DOUBLE_EQ(
1724 1.25f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1725 EXPECT_TRUE(IsTextSubpixelPositioningEnabled());
1726 EXPECT_EQ(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1729 TEST_F(DisplayManagerFontTest, TextSubpixelPositioningWithDsf200External) {
1730 FontTestHelper helper(2.0f, FontTestHelper::EXTERNAL);
1731 ASSERT_DOUBLE_EQ(
1732 2.0f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1733 EXPECT_TRUE(IsTextSubpixelPositioningEnabled());
1734 EXPECT_EQ(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1737 TEST_F(DisplayManagerFontTest,
1738 TextSubpixelPositioningWithDsf125InternalWithScaling) {
1739 DisplayInfo::SetUse125DSFForUIScaling(true);
1740 FontTestHelper helper(1.25f, FontTestHelper::INTERNAL);
1741 ASSERT_DOUBLE_EQ(
1742 1.0f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1743 EXPECT_FALSE(IsTextSubpixelPositioningEnabled());
1744 EXPECT_NE(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1746 Shell::GetInstance()->display_manager()->SetDisplayUIScale(
1747 Shell::GetScreen()->GetPrimaryDisplay().id(), 0.8f);
1749 ASSERT_DOUBLE_EQ(
1750 1.25f, Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
1751 EXPECT_TRUE(IsTextSubpixelPositioningEnabled());
1752 EXPECT_EQ(gfx::FontRenderParams::HINTING_NONE, GetFontHintingParams());
1754 DisplayInfo::SetUse125DSFForUIScaling(false);
1757 TEST_F(DisplayManagerTest, CheckInitializationOfRotationProperty) {
1758 int64_t id = display_manager()->GetDisplayAt(0).id();
1759 display_manager()->RegisterDisplayProperty(id, gfx::Display::ROTATE_90, 1.0f,
1760 nullptr, gfx::Size(), 1.0f,
1761 ui::COLOR_PROFILE_STANDARD);
1763 const DisplayInfo& info = display_manager()->GetDisplayInfo(id);
1765 EXPECT_EQ(gfx::Display::ROTATE_90,
1766 info.GetRotation(gfx::Display::ROTATION_SOURCE_USER));
1767 EXPECT_EQ(gfx::Display::ROTATE_90,
1768 info.GetRotation(gfx::Display::ROTATION_SOURCE_ACTIVE));
1771 #endif // OS_CHROMEOS
1773 } // namespace ash