display_manager_unittest.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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.
4
5#include "ash/display/display_manager.h"
6
7#include "ash/display/display_controller.h"
8#include "ash/display/display_layout_store.h"
9#include "ash/screen_util.h"
10#include "ash/shell.h"
11#include "ash/test/ash_test_base.h"
12#include "ash/test/display_manager_test_api.h"
13#include "ash/test/mirror_window_test_api.h"
14#include "base/format_macros.h"
15#include "base/strings/string_number_conversions.h"
16#include "base/strings/stringprintf.h"
17#include "ui/aura/env.h"
18#include "ui/aura/test/event_generator.h"
19#include "ui/aura/window_observer.h"
20#include "ui/aura/window_tree_host.h"
21#include "ui/gfx/display_observer.h"
22#include "ui/gfx/display.h"
23#include "ui/gfx/screen.h"
24#include "ui/gfx/screen_type_delegate.h"
25
26namespace ash {
27namespace internal {
28
29using std::vector;
30using std::string;
31
32using base::StringPrintf;
33
34namespace {
35
36std::string ToDisplayName(int64 id) {
37  return "x-" + base::Int64ToString(id);
38}
39
40}  // namespace
41
42class DisplayManagerTest : public test::AshTestBase,
43                           public gfx::DisplayObserver,
44                           public aura::WindowObserver {
45 public:
46  DisplayManagerTest()
47      : removed_count_(0U),
48        root_window_destroyed_(false) {
49  }
50  virtual ~DisplayManagerTest() {}
51
52  virtual void SetUp() OVERRIDE {
53    AshTestBase::SetUp();
54    Shell::GetScreen()->AddObserver(this);
55    Shell::GetPrimaryRootWindow()->AddObserver(this);
56  }
57  virtual void TearDown() OVERRIDE {
58    Shell::GetPrimaryRootWindow()->RemoveObserver(this);
59    Shell::GetScreen()->RemoveObserver(this);
60    AshTestBase::TearDown();
61  }
62
63  DisplayManager* display_manager() {
64    return Shell::GetInstance()->display_manager();
65  }
66  const vector<gfx::Display>& changed() const { return changed_; }
67  const vector<gfx::Display>& added() const { return added_; }
68
69  string GetCountSummary() const {
70    return StringPrintf("%" PRIuS " %" PRIuS " %" PRIuS,
71                        changed_.size(), added_.size(), removed_count_);
72  }
73
74  void reset() {
75    changed_.clear();
76    added_.clear();
77    removed_count_ = 0U;
78    root_window_destroyed_ = false;
79  }
80
81  bool root_window_destroyed() const {
82    return root_window_destroyed_;
83  }
84
85  const DisplayInfo& GetDisplayInfo(const gfx::Display& display) {
86    return display_manager()->GetDisplayInfo(display.id());
87  }
88
89  const DisplayInfo& GetDisplayInfoAt(int index) {
90    return GetDisplayInfo(display_manager()->GetDisplayAt(index));
91  }
92
93  const gfx::Display& GetDisplayForId(int64 id) {
94    return display_manager()->GetDisplayForId(id);
95  }
96
97  const DisplayInfo& GetDisplayInfoForId(int64 id) {
98    return GetDisplayInfo(display_manager()->GetDisplayForId(id));
99  }
100
101  // aura::DisplayObserver overrides:
102  virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE {
103    changed_.push_back(display);
104  }
105  virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE {
106    added_.push_back(new_display);
107  }
108  virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE {
109    ++removed_count_;
110  }
111
112  // aura::WindowObserver overrides:
113  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
114    ASSERT_EQ(Shell::GetPrimaryRootWindow(), window);
115    root_window_destroyed_ = true;
116  }
117
118 private:
119  vector<gfx::Display> changed_;
120  vector<gfx::Display> added_;
121  size_t removed_count_;
122  bool root_window_destroyed_;
123
124  DISALLOW_COPY_AND_ASSIGN(DisplayManagerTest);
125};
126
127TEST_F(DisplayManagerTest, UpdateDisplayTest) {
128  if (!SupportsMultipleDisplays())
129    return;
130
131  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
132
133  // Update primary and add seconary.
134  UpdateDisplay("100+0-500x500,0+501-400x400");
135  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
136  EXPECT_EQ("0,0 500x500",
137            display_manager()->GetDisplayAt(0).bounds().ToString());
138
139  EXPECT_EQ("1 1 0", GetCountSummary());
140  EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
141  EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
142  EXPECT_EQ("0,0 500x500", changed()[0].bounds().ToString());
143  // Secondary display is on right.
144  EXPECT_EQ("500,0 400x400", added()[0].bounds().ToString());
145  EXPECT_EQ("0,501 400x400",
146            GetDisplayInfo(added()[0]).bounds_in_native().ToString());
147  reset();
148
149  // Delete secondary.
150  UpdateDisplay("100+0-500x500");
151  EXPECT_EQ("0 0 1", GetCountSummary());
152  reset();
153
154  // Change primary.
155  UpdateDisplay("1+1-1000x600");
156  EXPECT_EQ("1 0 0", GetCountSummary());
157  EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
158  EXPECT_EQ("0,0 1000x600", changed()[0].bounds().ToString());
159  reset();
160
161  // Add secondary.
162  UpdateDisplay("1+1-1000x600,1002+0-600x400");
163  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
164  EXPECT_EQ("0 1 0", GetCountSummary());
165  EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
166  // Secondary display is on right.
167  EXPECT_EQ("1000,0 600x400", added()[0].bounds().ToString());
168  EXPECT_EQ("1002,0 600x400",
169            GetDisplayInfo(added()[0]).bounds_in_native().ToString());
170  reset();
171
172  // Secondary removed, primary changed.
173  UpdateDisplay("1+1-800x300");
174  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
175  EXPECT_EQ("1 0 1", GetCountSummary());
176  EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[0].id());
177  EXPECT_EQ("0,0 800x300", changed()[0].bounds().ToString());
178  reset();
179
180  // # of display can go to zero when screen is off.
181  const vector<DisplayInfo> empty;
182  display_manager()->OnNativeDisplaysChanged(empty);
183  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
184  EXPECT_EQ("0 0 0", GetCountSummary());
185  EXPECT_FALSE(root_window_destroyed());
186  // Display configuration stays the same
187  EXPECT_EQ("0,0 800x300",
188            display_manager()->GetDisplayAt(0).bounds().ToString());
189  reset();
190
191  // Connect to display again
192  UpdateDisplay("100+100-500x400");
193  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
194  EXPECT_EQ("1 0 0", GetCountSummary());
195  EXPECT_FALSE(root_window_destroyed());
196  EXPECT_EQ("0,0 500x400", changed()[0].bounds().ToString());
197  EXPECT_EQ("100,100 500x400",
198            GetDisplayInfo(changed()[0]).bounds_in_native().ToString());
199  reset();
200
201  // Go back to zero and wake up with multiple displays.
202  display_manager()->OnNativeDisplaysChanged(empty);
203  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
204  EXPECT_FALSE(root_window_destroyed());
205  reset();
206
207  // Add secondary.
208  UpdateDisplay("0+0-1000x600,1000+1000-600x400");
209  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
210  EXPECT_EQ("0,0 1000x600",
211            display_manager()->GetDisplayAt(0).bounds().ToString());
212  // Secondary display is on right.
213  EXPECT_EQ("1000,0 600x400",
214            display_manager()->GetDisplayAt(1).bounds().ToString());
215  EXPECT_EQ("1000,1000 600x400",
216            GetDisplayInfoAt(1).bounds_in_native().ToString());
217  reset();
218
219  // Changing primary will update secondary as well.
220  UpdateDisplay("0+0-800x600,1000+1000-600x400");
221  EXPECT_EQ("2 0 0", GetCountSummary());
222  reset();
223  EXPECT_EQ("0,0 800x600",
224            display_manager()->GetDisplayAt(0).bounds().ToString());
225  EXPECT_EQ("800,0 600x400",
226            display_manager()->GetDisplayAt(1).bounds().ToString());
227}
228
229// Test in emulation mode (use_fullscreen_host_window=false)
230TEST_F(DisplayManagerTest, EmulatorTest) {
231  if (!SupportsMultipleDisplays())
232    return;
233
234  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
235
236  display_manager()->AddRemoveDisplay();
237  // Update primary and add seconary.
238  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
239  EXPECT_EQ("0 1 0", GetCountSummary());
240  reset();
241
242  display_manager()->AddRemoveDisplay();
243  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
244  EXPECT_EQ("0 0 1", GetCountSummary());
245  reset();
246
247  display_manager()->AddRemoveDisplay();
248  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
249  EXPECT_EQ("0 1 0", GetCountSummary());
250  reset();
251}
252
253TEST_F(DisplayManagerTest, OverscanInsetsTest) {
254  if (!SupportsMultipleDisplays())
255    return;
256
257  UpdateDisplay("0+0-500x500,0+501-400x400");
258  reset();
259  ASSERT_EQ(2u, display_manager()->GetNumDisplays());
260  const DisplayInfo& display_info1 = GetDisplayInfoAt(0);
261  const DisplayInfo& display_info2 = GetDisplayInfoAt(1);
262  display_manager()->SetOverscanInsets(
263      display_info2.id(), gfx::Insets(13, 12, 11, 10));
264
265  std::vector<gfx::Display> changed_displays = changed();
266  EXPECT_EQ(1u, changed_displays.size());
267  EXPECT_EQ(display_info2.id(), changed_displays[0].id());
268  EXPECT_EQ("0,0 500x500",
269            GetDisplayInfoAt(0).bounds_in_native().ToString());
270  DisplayInfo updated_display_info2 = GetDisplayInfoAt(1);
271  EXPECT_EQ("0,501 400x400",
272            updated_display_info2.bounds_in_native().ToString());
273  EXPECT_EQ("378x376",
274            updated_display_info2.size_in_pixel().ToString());
275  EXPECT_EQ("13,12,11,10",
276            updated_display_info2.overscan_insets_in_dip().ToString());
277  EXPECT_EQ("500,0 378x376",
278            ScreenUtil::GetSecondaryDisplay().bounds().ToString());
279
280  // Make sure that SetOverscanInsets() is idempotent.
281  display_manager()->SetOverscanInsets(display_info1.id(), gfx::Insets());
282  display_manager()->SetOverscanInsets(
283      display_info2.id(), gfx::Insets(13, 12, 11, 10));
284  EXPECT_EQ("0,0 500x500",
285            GetDisplayInfoAt(0).bounds_in_native().ToString());
286  updated_display_info2 = GetDisplayInfoAt(1);
287  EXPECT_EQ("0,501 400x400",
288            updated_display_info2.bounds_in_native().ToString());
289  EXPECT_EQ("378x376",
290            updated_display_info2.size_in_pixel().ToString());
291  EXPECT_EQ("13,12,11,10",
292            updated_display_info2.overscan_insets_in_dip().ToString());
293
294  display_manager()->SetOverscanInsets(
295      display_info2.id(), gfx::Insets(10, 11, 12, 13));
296  EXPECT_EQ("0,0 500x500",
297            GetDisplayInfoAt(0).bounds_in_native().ToString());
298  EXPECT_EQ("376x378",
299            GetDisplayInfoAt(1).size_in_pixel().ToString());
300  EXPECT_EQ("10,11,12,13",
301            GetDisplayInfoAt(1).overscan_insets_in_dip().ToString());
302
303  // Recreate a new 2nd display. It won't apply the overscan inset because the
304  // new display has a different ID.
305  UpdateDisplay("0+0-500x500");
306  UpdateDisplay("0+0-500x500,0+501-400x400");
307  EXPECT_EQ("0,0 500x500",
308            GetDisplayInfoAt(0).bounds_in_native().ToString());
309  EXPECT_EQ("0,501 400x400",
310            GetDisplayInfoAt(1).bounds_in_native().ToString());
311
312  // Recreate the displays with the same ID.  It should apply the overscan
313  // inset.
314  UpdateDisplay("0+0-500x500");
315  std::vector<DisplayInfo> display_info_list;
316  display_info_list.push_back(display_info1);
317  display_info_list.push_back(display_info2);
318  display_manager()->OnNativeDisplaysChanged(display_info_list);
319  EXPECT_EQ("1,1 500x500",
320            GetDisplayInfoAt(0).bounds_in_native().ToString());
321  updated_display_info2 = GetDisplayInfoAt(1);
322  EXPECT_EQ("376x378",
323            updated_display_info2.size_in_pixel().ToString());
324  EXPECT_EQ("10,11,12,13",
325            updated_display_info2.overscan_insets_in_dip().ToString());
326
327  // HiDPI but overscan display. The specified insets size should be doubled.
328  UpdateDisplay("0+0-500x500,0+501-400x400*2");
329  display_manager()->SetOverscanInsets(
330      display_manager()->GetDisplayAt(1).id(), gfx::Insets(4, 5, 6, 7));
331  EXPECT_EQ("0,0 500x500",
332            GetDisplayInfoAt(0).bounds_in_native().ToString());
333  updated_display_info2 = GetDisplayInfoAt(1);
334  EXPECT_EQ("0,501 400x400",
335            updated_display_info2.bounds_in_native().ToString());
336  EXPECT_EQ("376x380",
337            updated_display_info2.size_in_pixel().ToString());
338  EXPECT_EQ("4,5,6,7",
339            updated_display_info2.overscan_insets_in_dip().ToString());
340  EXPECT_EQ("8,10,12,14",
341            updated_display_info2.GetOverscanInsetsInPixel().ToString());
342
343  // Make sure switching primary display applies the overscan offset only once.
344  ash::Shell::GetInstance()->display_controller()->SetPrimaryDisplay(
345      ScreenUtil::GetSecondaryDisplay());
346  EXPECT_EQ("-500,0 500x500",
347            ScreenUtil::GetSecondaryDisplay().bounds().ToString());
348  EXPECT_EQ("0,0 500x500",
349            GetDisplayInfo(ScreenUtil::GetSecondaryDisplay()).
350            bounds_in_native().ToString());
351  EXPECT_EQ("0,501 400x400",
352            GetDisplayInfo(Shell::GetScreen()->GetPrimaryDisplay()).
353            bounds_in_native().ToString());
354  EXPECT_EQ("0,0 188x190",
355            Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
356}
357
358TEST_F(DisplayManagerTest, ZeroOverscanInsets) {
359  if (!SupportsMultipleDisplays())
360    return;
361
362  // Make sure the display change events is emitted for overscan inset changes.
363  UpdateDisplay("0+0-500x500,0+501-400x400");
364  ASSERT_EQ(2u, display_manager()->GetNumDisplays());
365  int64 display2_id = display_manager()->GetDisplayAt(1).id();
366
367  reset();
368  display_manager()->SetOverscanInsets(display2_id, gfx::Insets(0, 0, 0, 0));
369  EXPECT_EQ(0u, changed().size());
370
371  reset();
372  display_manager()->SetOverscanInsets(display2_id, gfx::Insets(1, 0, 0, 0));
373  EXPECT_EQ(1u, changed().size());
374  EXPECT_EQ(display2_id, changed()[0].id());
375
376  reset();
377  display_manager()->SetOverscanInsets(display2_id, gfx::Insets(0, 0, 0, 0));
378  EXPECT_EQ(1u, changed().size());
379  EXPECT_EQ(display2_id, changed()[0].id());
380}
381
382TEST_F(DisplayManagerTest, TestDeviceScaleOnlyChange) {
383  if (!SupportsHostWindowResize())
384    return;
385
386  UpdateDisplay("1000x600");
387  aura::WindowTreeHost* host = Shell::GetPrimaryRootWindow()->GetHost();
388  EXPECT_EQ(1, host->compositor()->device_scale_factor());
389  EXPECT_EQ("1000x600",
390            Shell::GetPrimaryRootWindow()->bounds().size().ToString());
391  UpdateDisplay("1000x600*2");
392  EXPECT_EQ(2, host->compositor()->device_scale_factor());
393  EXPECT_EQ("500x300",
394            Shell::GetPrimaryRootWindow()->bounds().size().ToString());
395}
396
397DisplayInfo CreateDisplayInfo(int64 id, const gfx::Rect& bounds) {
398  DisplayInfo info(id, ToDisplayName(id), false);
399  info.SetBounds(bounds);
400  return info;
401}
402
403TEST_F(DisplayManagerTest, TestNativeDisplaysChanged) {
404  const int64 internal_display_id =
405      test::DisplayManagerTestApi(display_manager()).
406      SetFirstDisplayAsInternalDisplay();
407  const int external_id = 10;
408  const int mirror_id = 11;
409  const int64 invalid_id = gfx::Display::kInvalidDisplayID;
410  const DisplayInfo internal_display_info =
411      CreateDisplayInfo(internal_display_id, gfx::Rect(0, 0, 500, 500));
412  const DisplayInfo external_display_info =
413      CreateDisplayInfo(external_id, gfx::Rect(1, 1, 100, 100));
414  const DisplayInfo mirrored_display_info =
415      CreateDisplayInfo(mirror_id, gfx::Rect(0, 0, 500, 500));
416
417  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
418  EXPECT_EQ(1U, display_manager()->num_connected_displays());
419  std::string default_bounds =
420      display_manager()->GetDisplayAt(0).bounds().ToString();
421
422  std::vector<DisplayInfo> display_info_list;
423  // Primary disconnected.
424  display_manager()->OnNativeDisplaysChanged(display_info_list);
425  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
426  EXPECT_EQ(default_bounds,
427            display_manager()->GetDisplayAt(0).bounds().ToString());
428  EXPECT_EQ(1U, display_manager()->num_connected_displays());
429  EXPECT_FALSE(display_manager()->IsMirrored());
430
431  if (!SupportsMultipleDisplays())
432    return;
433
434  // External connected while primary was disconnected.
435  display_info_list.push_back(external_display_info);
436  display_manager()->OnNativeDisplaysChanged(display_info_list);
437  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
438
439  EXPECT_EQ(invalid_id, GetDisplayForId(internal_display_id).id());
440  EXPECT_EQ("1,1 100x100",
441            GetDisplayInfoForId(external_id).bounds_in_native().ToString());
442  EXPECT_EQ(1U, display_manager()->num_connected_displays());
443  EXPECT_FALSE(display_manager()->IsMirrored());
444  EXPECT_EQ(external_id, Shell::GetScreen()->GetPrimaryDisplay().id());
445
446  EXPECT_EQ(internal_display_id, gfx::Display::InternalDisplayId());
447
448  // Primary connected, with different bounds.
449  display_info_list.clear();
450  display_info_list.push_back(internal_display_info);
451  display_info_list.push_back(external_display_info);
452  display_manager()->OnNativeDisplaysChanged(display_info_list);
453  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
454  EXPECT_EQ(internal_display_id, Shell::GetScreen()->GetPrimaryDisplay().id());
455
456  // This combinatino is new, so internal display becomes primary.
457  EXPECT_EQ("0,0 500x500",
458            GetDisplayForId(internal_display_id).bounds().ToString());
459  EXPECT_EQ("1,1 100x100",
460            GetDisplayInfoForId(10).bounds_in_native().ToString());
461  EXPECT_EQ(2U, display_manager()->num_connected_displays());
462  EXPECT_FALSE(display_manager()->IsMirrored());
463  EXPECT_EQ(ToDisplayName(internal_display_id),
464            display_manager()->GetDisplayNameForId(internal_display_id));
465
466  // Emulate suspend.
467  display_info_list.clear();
468  display_manager()->OnNativeDisplaysChanged(display_info_list);
469  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
470  EXPECT_EQ("0,0 500x500",
471            GetDisplayForId(internal_display_id).bounds().ToString());
472  EXPECT_EQ("1,1 100x100",
473            GetDisplayInfoForId(10).bounds_in_native().ToString());
474  EXPECT_EQ(2U, display_manager()->num_connected_displays());
475  EXPECT_FALSE(display_manager()->IsMirrored());
476  EXPECT_EQ(ToDisplayName(internal_display_id),
477            display_manager()->GetDisplayNameForId(internal_display_id));
478
479  // External display has disconnected then resumed.
480  display_info_list.push_back(internal_display_info);
481  display_manager()->OnNativeDisplaysChanged(display_info_list);
482  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
483  EXPECT_EQ("0,0 500x500",
484            GetDisplayForId(internal_display_id).bounds().ToString());
485  EXPECT_EQ(1U, display_manager()->num_connected_displays());
486  EXPECT_FALSE(display_manager()->IsMirrored());
487
488  // External display was changed during suspend.
489  display_info_list.push_back(external_display_info);
490  display_manager()->OnNativeDisplaysChanged(display_info_list);
491  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
492  EXPECT_EQ(2U, display_manager()->num_connected_displays());
493  EXPECT_FALSE(display_manager()->IsMirrored());
494
495  // suspend...
496  display_info_list.clear();
497  display_manager()->OnNativeDisplaysChanged(display_info_list);
498  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
499  EXPECT_EQ(2U, display_manager()->num_connected_displays());
500  EXPECT_FALSE(display_manager()->IsMirrored());
501
502  // and resume with different external display.
503  display_info_list.push_back(internal_display_info);
504  display_info_list.push_back(CreateDisplayInfo(12, gfx::Rect(1, 1, 100, 100)));
505  display_manager()->OnNativeDisplaysChanged(display_info_list);
506  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
507  EXPECT_EQ(2U, display_manager()->num_connected_displays());
508  EXPECT_FALSE(display_manager()->IsMirrored());
509  EXPECT_FALSE(display_manager()->IsMirrored());
510
511  // mirrored...
512  display_info_list.clear();
513  display_info_list.push_back(internal_display_info);
514  display_info_list.push_back(mirrored_display_info);
515  display_manager()->OnNativeDisplaysChanged(display_info_list);
516  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
517  EXPECT_EQ("0,0 500x500",
518            GetDisplayForId(internal_display_id).bounds().ToString());
519  EXPECT_EQ(2U, display_manager()->num_connected_displays());
520  EXPECT_EQ(11U, display_manager()->mirrored_display_id());
521  EXPECT_TRUE(display_manager()->IsMirrored());
522
523  // Test display name.
524  EXPECT_EQ(ToDisplayName(internal_display_id),
525            display_manager()->GetDisplayNameForId(internal_display_id));
526  EXPECT_EQ("x-10", display_manager()->GetDisplayNameForId(10));
527  EXPECT_EQ("x-11", display_manager()->GetDisplayNameForId(11));
528  EXPECT_EQ("x-12", display_manager()->GetDisplayNameForId(12));
529  // Default name for the id that doesn't exist.
530  EXPECT_EQ("Display 100", display_manager()->GetDisplayNameForId(100));
531
532  // and exit mirroring.
533  display_info_list.clear();
534  display_info_list.push_back(internal_display_info);
535  display_info_list.push_back(external_display_info);
536  display_manager()->OnNativeDisplaysChanged(display_info_list);
537  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
538  EXPECT_EQ(2U, display_manager()->num_connected_displays());
539  EXPECT_FALSE(display_manager()->IsMirrored());
540  EXPECT_EQ("0,0 500x500",
541            GetDisplayForId(internal_display_id).bounds().ToString());
542  EXPECT_EQ("500,0 100x100",
543            GetDisplayForId(10).bounds().ToString());
544
545  // Turn off internal
546  display_info_list.clear();
547  display_info_list.push_back(external_display_info);
548  display_manager()->OnNativeDisplaysChanged(display_info_list);
549  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
550  EXPECT_EQ(invalid_id, GetDisplayForId(internal_display_id).id());
551  EXPECT_EQ("1,1 100x100",
552            GetDisplayInfoForId(external_id).bounds_in_native().ToString());
553  EXPECT_EQ(1U, display_manager()->num_connected_displays());
554  EXPECT_FALSE(display_manager()->IsMirrored());
555
556  // Switched to another display
557  display_info_list.clear();
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(
562      "0,0 500x500",
563      GetDisplayInfoForId(internal_display_id).bounds_in_native().ToString());
564  EXPECT_EQ(1U, display_manager()->num_connected_displays());
565  EXPECT_FALSE(display_manager()->IsMirrored());
566}
567
568#if defined(OS_WIN)
569// TODO(scottmg): RootWindow doesn't get resized on Windows
570// Ash. http://crbug.com/247916.
571#define MAYBE_TestNativeDisplaysChangedNoInternal \
572        DISABLED_TestNativeDisplaysChangedNoInternal
573#else
574#define MAYBE_TestNativeDisplaysChangedNoInternal \
575        TestNativeDisplaysChangedNoInternal
576#endif
577
578TEST_F(DisplayManagerTest, MAYBE_TestNativeDisplaysChangedNoInternal) {
579  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
580
581  // Don't change the display info if all displays are disconnected.
582  std::vector<DisplayInfo> display_info_list;
583  display_manager()->OnNativeDisplaysChanged(display_info_list);
584  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
585
586  // Connect another display which will become primary.
587  const DisplayInfo external_display_info =
588      CreateDisplayInfo(10, gfx::Rect(1, 1, 100, 100));
589  display_info_list.push_back(external_display_info);
590  display_manager()->OnNativeDisplaysChanged(display_info_list);
591  EXPECT_EQ(1U, display_manager()->GetNumDisplays());
592  EXPECT_EQ("1,1 100x100",
593            GetDisplayInfoForId(10).bounds_in_native().ToString());
594  EXPECT_EQ("100x100", ash::Shell::GetPrimaryRootWindow()->GetHost()->
595      GetBounds().size().ToString());
596}
597
598#if defined(OS_WIN)
599// Tests that rely on the actual host size/location does not work on windows.
600#define MAYBE_EnsurePointerInDisplays DISABLED_EnsurePointerInDisplays
601#define MAYBE_EnsurePointerInDisplays_2ndOnLeft DISABLED_EnsurePointerInDisplays_2ndOnLeft
602#else
603#define MAYBE_EnsurePointerInDisplays EnsurePointerInDisplays
604#define MAYBE_EnsurePointerInDisplays_2ndOnLeft EnsurePointerInDisplays_2ndOnLeft
605#endif
606
607TEST_F(DisplayManagerTest, MAYBE_EnsurePointerInDisplays) {
608  UpdateDisplay("200x200,300x300");
609  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
610
611  aura::Env* env = aura::Env::GetInstance();
612
613  aura::test::EventGenerator generator(root_windows[0]);
614
615  // Set the initial position.
616  generator.MoveMouseToInHost(350, 150);
617  EXPECT_EQ("350,150", env->last_mouse_location().ToString());
618
619  // A mouse pointer will stay in the 2nd display.
620  UpdateDisplay("300x300,200x200");
621  EXPECT_EQ("450,50", env->last_mouse_location().ToString());
622
623  // A mouse pointer will be outside of displays and move to the
624  // center of 2nd display.
625  UpdateDisplay("300x300,100x100");
626  EXPECT_EQ("350,50", env->last_mouse_location().ToString());
627
628  // 2nd display was disconnected, and the cursor is
629  // now in the 1st display.
630  UpdateDisplay("400x400");
631  EXPECT_EQ("50,350", env->last_mouse_location().ToString());
632
633  // 1st display's resolution has changed, and the mouse pointer is
634  // now outside. Move the mouse pointer to the center of 1st display.
635  UpdateDisplay("300x300");
636  EXPECT_EQ("150,150", env->last_mouse_location().ToString());
637
638  // Move the mouse pointer to the bottom of 1st display.
639  generator.MoveMouseToInHost(150, 290);
640  EXPECT_EQ("150,290", env->last_mouse_location().ToString());
641
642  // The mouse pointer is now on 2nd display.
643  UpdateDisplay("300x280,200x200");
644  EXPECT_EQ("450,10", env->last_mouse_location().ToString());
645}
646
647TEST_F(DisplayManagerTest, MAYBE_EnsurePointerInDisplays_2ndOnLeft) {
648  // Set the 2nd display on the left.
649  DisplayLayoutStore* layout_store =
650      Shell::GetInstance()->display_manager()->layout_store();
651  DisplayLayout layout = layout_store->default_display_layout();
652  layout.position = DisplayLayout::LEFT;
653  layout_store->SetDefaultDisplayLayout(layout);
654
655  UpdateDisplay("200x200,300x300");
656  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
657
658  EXPECT_EQ("-300,0 300x300",
659            ScreenUtil::GetSecondaryDisplay().bounds().ToString());
660
661  aura::Env* env = aura::Env::GetInstance();
662
663  // Set the initial position.
664  root_windows[0]->MoveCursorTo(gfx::Point(-150, 250));
665  EXPECT_EQ("-150,250", env->last_mouse_location().ToString());
666
667  // A mouse pointer will stay in 2nd display.
668  UpdateDisplay("300x300,200x300");
669  EXPECT_EQ("-50,150", env->last_mouse_location().ToString());
670
671  // A mouse pointer will be outside of displays and move to the
672  // center of 2nd display.
673  UpdateDisplay("300x300,200x100");
674  EXPECT_EQ("-100,50", env->last_mouse_location().ToString());
675
676  // 2nd display was disconnected. Mouse pointer should move to
677  // 1st display.
678  UpdateDisplay("300x300");
679  EXPECT_EQ("150,150", env->last_mouse_location().ToString());
680}
681
682TEST_F(DisplayManagerTest, NativeDisplaysChangedAfterPrimaryChange) {
683  if (!SupportsMultipleDisplays())
684    return;
685
686  const int64 internal_display_id =
687      test::DisplayManagerTestApi(display_manager()).
688      SetFirstDisplayAsInternalDisplay();
689  const DisplayInfo native_display_info =
690      CreateDisplayInfo(internal_display_id, gfx::Rect(0, 0, 500, 500));
691  const DisplayInfo secondary_display_info =
692      CreateDisplayInfo(10, gfx::Rect(1, 1, 100, 100));
693
694  std::vector<DisplayInfo> display_info_list;
695  display_info_list.push_back(native_display_info);
696  display_info_list.push_back(secondary_display_info);
697  display_manager()->OnNativeDisplaysChanged(display_info_list);
698  EXPECT_EQ(2U, display_manager()->GetNumDisplays());
699  EXPECT_EQ("0,0 500x500",
700            GetDisplayForId(internal_display_id).bounds().ToString());
701  EXPECT_EQ("500,0 100x100", GetDisplayForId(10).bounds().ToString());
702
703  ash::Shell::GetInstance()->display_controller()->SetPrimaryDisplay(
704      GetDisplayForId(secondary_display_info.id()));
705  EXPECT_EQ("-500,0 500x500",
706            GetDisplayForId(internal_display_id).bounds().ToString());
707  EXPECT_EQ("0,0 100x100", GetDisplayForId(10).bounds().ToString());
708
709  // OnNativeDisplaysChanged may change the display bounds.  Here makes sure
710  // nothing changed if the exactly same displays are specified.
711  display_manager()->OnNativeDisplaysChanged(display_info_list);
712  EXPECT_EQ("-500,0 500x500",
713            GetDisplayForId(internal_display_id).bounds().ToString());
714  EXPECT_EQ("0,0 100x100", GetDisplayForId(10).bounds().ToString());
715}
716
717TEST_F(DisplayManagerTest, DontRememberBestResolution) {
718  int display_id = 1000;
719  DisplayInfo native_display_info =
720      CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1000, 500));
721  std::vector<DisplayMode> display_modes;
722  display_modes.push_back(
723      DisplayMode(gfx::Size(1000, 500), 58.0f, false, true));
724  display_modes.push_back(
725      DisplayMode(gfx::Size(800, 300), 59.0f, false, false));
726  display_modes.push_back(
727      DisplayMode(gfx::Size(400, 500), 60.0f, false, false));
728
729  native_display_info.set_display_modes(display_modes);
730
731  std::vector<DisplayInfo> display_info_list;
732  display_info_list.push_back(native_display_info);
733  display_manager()->OnNativeDisplaysChanged(display_info_list);
734
735  DisplayMode mode;
736  EXPECT_FALSE(
737      display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
738
739  // Unsupported resolution.
740  display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 4000));
741  EXPECT_FALSE(
742      display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
743
744  // Supported resolution.
745  display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 300));
746  EXPECT_TRUE(
747      display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
748  EXPECT_EQ("800x300", mode.size.ToString());
749  EXPECT_EQ(59.0f, mode.refresh_rate);
750  EXPECT_FALSE(mode.native);
751
752  // Best resolution.
753  display_manager()->SetDisplayResolution(display_id, gfx::Size(1000, 500));
754  EXPECT_TRUE(
755      display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
756  EXPECT_EQ("1000x500", mode.size.ToString());
757  EXPECT_EQ(58.0f, mode.refresh_rate);
758  EXPECT_TRUE(mode.native);
759}
760
761TEST_F(DisplayManagerTest, ResolutionFallback) {
762  int display_id = 1000;
763  DisplayInfo native_display_info =
764      CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1000, 500));
765  std::vector<DisplayMode> display_modes;
766  display_modes.push_back(
767      DisplayMode(gfx::Size(1000, 500), 58.0f, false, true));
768  display_modes.push_back(
769      DisplayMode(gfx::Size(800, 300), 59.0f, false, false));
770  display_modes.push_back(
771      DisplayMode(gfx::Size(400, 500), 60.0f, false, false));
772
773  std::vector<DisplayMode> copy = display_modes;
774  native_display_info.set_display_modes(copy);
775
776  std::vector<DisplayInfo> display_info_list;
777  display_info_list.push_back(native_display_info);
778  display_manager()->OnNativeDisplaysChanged(display_info_list);
779  {
780    display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 300));
781    DisplayInfo new_native_display_info =
782        CreateDisplayInfo(display_id, gfx::Rect(0, 0, 400, 500));
783    copy = display_modes;
784    new_native_display_info.set_display_modes(copy);
785    std::vector<DisplayInfo> new_display_info_list;
786    new_display_info_list.push_back(new_native_display_info);
787    display_manager()->OnNativeDisplaysChanged(new_display_info_list);
788
789    DisplayMode mode;
790    EXPECT_TRUE(
791        display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
792    EXPECT_EQ("400x500", mode.size.ToString());
793    EXPECT_EQ(60.0f, mode.refresh_rate);
794    EXPECT_FALSE(mode.native);
795  }
796  {
797    // Best resolution should find itself on the resolutions list.
798    display_manager()->SetDisplayResolution(display_id, gfx::Size(800, 300));
799    DisplayInfo new_native_display_info =
800        CreateDisplayInfo(display_id, gfx::Rect(0, 0, 1000, 500));
801    std::vector<DisplayMode> copy = display_modes;
802    new_native_display_info.set_display_modes(copy);
803    std::vector<DisplayInfo> new_display_info_list;
804    new_display_info_list.push_back(new_native_display_info);
805    display_manager()->OnNativeDisplaysChanged(new_display_info_list);
806
807    DisplayMode mode;
808    EXPECT_TRUE(
809        display_manager()->GetSelectedModeForDisplayId(display_id, &mode));
810    EXPECT_EQ("1000x500", mode.size.ToString());
811    EXPECT_EQ(58.0f, mode.refresh_rate);
812    EXPECT_TRUE(mode.native);
813  }
814}
815
816TEST_F(DisplayManagerTest, Rotate) {
817  if (!SupportsMultipleDisplays())
818    return;
819
820  UpdateDisplay("100x200/r,300x400/l");
821  EXPECT_EQ("1,1 100x200",
822            GetDisplayInfoAt(0).bounds_in_native().ToString());
823  EXPECT_EQ("200x100",
824            GetDisplayInfoAt(0).size_in_pixel().ToString());
825
826  EXPECT_EQ("1,201 300x400",
827            GetDisplayInfoAt(1).bounds_in_native().ToString());
828  EXPECT_EQ("400x300",
829            GetDisplayInfoAt(1).size_in_pixel().ToString());
830  reset();
831  UpdateDisplay("100x200/b,300x400");
832  EXPECT_EQ("2 0 0", GetCountSummary());
833  reset();
834
835  EXPECT_EQ("1,1 100x200",
836            GetDisplayInfoAt(0).bounds_in_native().ToString());
837  EXPECT_EQ("100x200",
838            GetDisplayInfoAt(0).size_in_pixel().ToString());
839
840  EXPECT_EQ("1,201 300x400",
841            GetDisplayInfoAt(1).bounds_in_native().ToString());
842  EXPECT_EQ("300x400",
843            GetDisplayInfoAt(1).size_in_pixel().ToString());
844
845  // Just Rotating display will change the bounds on both display.
846  UpdateDisplay("100x200/l,300x400");
847  EXPECT_EQ("2 0 0", GetCountSummary());
848  reset();
849
850  // Updating tothe same configuration should report no changes.
851  UpdateDisplay("100x200/l,300x400");
852  EXPECT_EQ("0 0 0", GetCountSummary());
853  reset();
854
855  UpdateDisplay("100x200/l,300x400");
856  EXPECT_EQ("0 0 0", GetCountSummary());
857  reset();
858
859  UpdateDisplay("200x200");
860  EXPECT_EQ("1 0 1", GetCountSummary());
861  reset();
862
863  UpdateDisplay("200x200/l");
864  EXPECT_EQ("1 0 0", GetCountSummary());
865}
866
867TEST_F(DisplayManagerTest, UIScale) {
868  UpdateDisplay("1280x800");
869  int64 display_id = Shell::GetScreen()->GetPrimaryDisplay().id();
870  display_manager()->SetDisplayUIScale(display_id, 1.125f);
871  EXPECT_EQ(1.0, GetDisplayInfoAt(0).configured_ui_scale());
872  display_manager()->SetDisplayUIScale(display_id, 0.8f);
873  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
874  display_manager()->SetDisplayUIScale(display_id, 0.75f);
875  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
876  display_manager()->SetDisplayUIScale(display_id, 0.625f);
877  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
878
879  gfx::Display::SetInternalDisplayId(display_id);
880
881  display_manager()->SetDisplayUIScale(display_id, 1.5f);
882  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
883  display_manager()->SetDisplayUIScale(display_id, 1.25f);
884  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
885  display_manager()->SetDisplayUIScale(display_id, 1.125f);
886  EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
887  display_manager()->SetDisplayUIScale(display_id, 0.8f);
888  EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
889  display_manager()->SetDisplayUIScale(display_id, 0.75f);
890  EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
891  display_manager()->SetDisplayUIScale(display_id, 0.625f);
892  EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
893  display_manager()->SetDisplayUIScale(display_id, 0.6f);
894  EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
895  display_manager()->SetDisplayUIScale(display_id, 0.5f);
896  EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
897
898  UpdateDisplay("1366x768");
899  display_manager()->SetDisplayUIScale(display_id, 1.5f);
900  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
901  display_manager()->SetDisplayUIScale(display_id, 1.25f);
902  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
903  display_manager()->SetDisplayUIScale(display_id, 1.125f);
904  EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
905  display_manager()->SetDisplayUIScale(display_id, 0.8f);
906  EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
907  display_manager()->SetDisplayUIScale(display_id, 0.75f);
908  EXPECT_EQ(0.75f, GetDisplayInfoAt(0).configured_ui_scale());
909  display_manager()->SetDisplayUIScale(display_id, 0.6f);
910  EXPECT_EQ(0.6f, GetDisplayInfoAt(0).configured_ui_scale());
911  display_manager()->SetDisplayUIScale(display_id, 0.625f);
912  EXPECT_EQ(0.6f, GetDisplayInfoAt(0).configured_ui_scale());
913  display_manager()->SetDisplayUIScale(display_id, 0.5f);
914  EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
915
916  UpdateDisplay("1280x850*2");
917  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
918  display_manager()->SetDisplayUIScale(display_id, 1.5f);
919  EXPECT_EQ(1.5f, GetDisplayInfoAt(0).configured_ui_scale());
920  display_manager()->SetDisplayUIScale(display_id, 1.25f);
921  EXPECT_EQ(1.25f, GetDisplayInfoAt(0).configured_ui_scale());
922  display_manager()->SetDisplayUIScale(display_id, 1.125f);
923  EXPECT_EQ(1.125f, GetDisplayInfoAt(0).configured_ui_scale());
924  display_manager()->SetDisplayUIScale(display_id, 1.0f);
925  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).configured_ui_scale());
926  gfx::Display display = Shell::GetScreen()->GetPrimaryDisplay();
927  EXPECT_EQ(2.0f, display.device_scale_factor());
928  EXPECT_EQ("640x425", display.bounds().size().ToString());
929
930  display_manager()->SetDisplayUIScale(display_id, 0.8f);
931  EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
932  display_manager()->SetDisplayUIScale(display_id, 0.75f);
933  EXPECT_EQ(0.8f, GetDisplayInfoAt(0).configured_ui_scale());
934  display_manager()->SetDisplayUIScale(display_id, 0.625f);
935  EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
936  display_manager()->SetDisplayUIScale(display_id, 0.6f);
937  EXPECT_EQ(0.625f, GetDisplayInfoAt(0).configured_ui_scale());
938  display_manager()->SetDisplayUIScale(display_id, 0.5f);
939  EXPECT_EQ(0.5f, GetDisplayInfoAt(0).configured_ui_scale());
940
941  display_manager()->SetDisplayUIScale(display_id, 2.0f);
942  EXPECT_EQ(2.0f, GetDisplayInfoAt(0).configured_ui_scale());
943  EXPECT_EQ(1.0f, GetDisplayInfoAt(0).GetEffectiveUIScale());
944  display = Shell::GetScreen()->GetPrimaryDisplay();
945  EXPECT_EQ(1.0f, display.device_scale_factor());
946  EXPECT_EQ("1280x850", display.bounds().size().ToString());
947}
948
949
950#if defined(OS_WIN)
951// TODO(scottmg): RootWindow doesn't get resized on Windows
952// Ash. http://crbug.com/247916.
953#define MAYBE_UpdateMouseCursorAfterRotateZoom DISABLED_UpdateMouseCursorAfterRotateZoom
954#else
955#define MAYBE_UpdateMouseCursorAfterRotateZoom UpdateMouseCursorAfterRotateZoom
956#endif
957
958TEST_F(DisplayManagerTest, MAYBE_UpdateMouseCursorAfterRotateZoom) {
959  // Make sure just rotating will not change native location.
960  UpdateDisplay("300x200,200x150");
961  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
962  aura::Env* env = aura::Env::GetInstance();
963
964  aura::test::EventGenerator generator1(root_windows[0]);
965  aura::test::EventGenerator generator2(root_windows[1]);
966
967  // Test on 1st display.
968  generator1.MoveMouseToInHost(150, 50);
969  EXPECT_EQ("150,50", env->last_mouse_location().ToString());
970  UpdateDisplay("300x200/r,200x150");
971  EXPECT_EQ("50,149", env->last_mouse_location().ToString());
972
973  // Test on 2nd display.
974  generator2.MoveMouseToInHost(50, 100);
975  EXPECT_EQ("250,100", env->last_mouse_location().ToString());
976  UpdateDisplay("300x200/r,200x150/l");
977  EXPECT_EQ("249,50", env->last_mouse_location().ToString());
978
979  // The native location is now outside, so move to the center
980  // of closest display.
981  UpdateDisplay("300x200/r,100x50/l");
982  EXPECT_EQ("225,50", env->last_mouse_location().ToString());
983
984  // Make sure just zooming will not change native location.
985  UpdateDisplay("600x400*2,400x300");
986
987  // Test on 1st display.
988  generator1.MoveMouseToInHost(200, 300);
989  EXPECT_EQ("100,150", env->last_mouse_location().ToString());
990  UpdateDisplay("600x400*2@1.5,400x300");
991  EXPECT_EQ("150,225", env->last_mouse_location().ToString());
992
993  // Test on 2nd display.
994  UpdateDisplay("600x400,400x300*2");
995  generator2.MoveMouseToInHost(200, 250);
996  EXPECT_EQ("700,125", env->last_mouse_location().ToString());
997  UpdateDisplay("600x400,400x300*2@1.5");
998  EXPECT_EQ("750,187", env->last_mouse_location().ToString());
999
1000  // The native location is now outside, so move to the
1001  // center of closest display.
1002  UpdateDisplay("600x400,400x200*2@1.5");
1003  EXPECT_EQ("750,75", env->last_mouse_location().ToString());
1004}
1005
1006class TestDisplayObserver : public gfx::DisplayObserver {
1007 public:
1008  TestDisplayObserver() : changed_(false) {}
1009  virtual ~TestDisplayObserver() {}
1010
1011  // gfx::DisplayObserver overrides:
1012  virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE {
1013  }
1014  virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE {
1015    // Mirror window should already be delete before restoring
1016    // the external display.
1017    EXPECT_FALSE(test_api.GetHost());
1018    changed_ = true;
1019  }
1020  virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE {
1021    // Mirror window should not be created until the external display
1022    // is removed.
1023    EXPECT_FALSE(test_api.GetHost());
1024    changed_ = true;
1025  }
1026
1027  bool changed_and_reset() {
1028    bool changed = changed_;
1029    changed_ = false;
1030    return changed;
1031  }
1032
1033 private:
1034  test::MirrorWindowTestApi test_api;
1035  bool changed_;
1036
1037  DISALLOW_COPY_AND_ASSIGN(TestDisplayObserver);
1038};
1039
1040TEST_F(DisplayManagerTest, SoftwareMirroring) {
1041  if (!SupportsMultipleDisplays())
1042    return;
1043
1044  UpdateDisplay("300x400,400x500");
1045
1046  test::MirrorWindowTestApi test_api;
1047  EXPECT_EQ(NULL, test_api.GetHost());
1048
1049  TestDisplayObserver display_observer;
1050  Shell::GetScreen()->AddObserver(&display_observer);
1051
1052  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1053  display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
1054  display_manager->UpdateDisplays();
1055  EXPECT_TRUE(display_observer.changed_and_reset());
1056  EXPECT_EQ(1U, display_manager->GetNumDisplays());
1057  EXPECT_EQ("0,0 300x400",
1058            Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
1059  EXPECT_EQ("400x500", test_api.GetHost()->GetBounds().size().ToString());
1060  EXPECT_EQ("300x400",
1061            test_api.GetHost()->window()->bounds().size().ToString());
1062  EXPECT_TRUE(display_manager->IsMirrored());
1063
1064  display_manager->SetMirrorMode(false);
1065  EXPECT_TRUE(display_observer.changed_and_reset());
1066  EXPECT_EQ(NULL, test_api.GetHost());
1067  EXPECT_EQ(2U, display_manager->GetNumDisplays());
1068  EXPECT_FALSE(display_manager->IsMirrored());
1069
1070  // Make sure the mirror window has the pixel size of the
1071  // source display.
1072  display_manager->SetMirrorMode(true);
1073  EXPECT_TRUE(display_observer.changed_and_reset());
1074
1075  UpdateDisplay("300x400@0.5,400x500");
1076  EXPECT_FALSE(display_observer.changed_and_reset());
1077  EXPECT_EQ("300x400",
1078            test_api.GetHost()->window()->bounds().size().ToString());
1079
1080  UpdateDisplay("310x410*2,400x500");
1081  EXPECT_FALSE(display_observer.changed_and_reset());
1082  EXPECT_EQ("310x410",
1083            test_api.GetHost()->window()->bounds().size().ToString());
1084
1085  UpdateDisplay("320x420/r,400x500");
1086  EXPECT_FALSE(display_observer.changed_and_reset());
1087  EXPECT_EQ("320x420",
1088            test_api.GetHost()->window()->bounds().size().ToString());
1089
1090  UpdateDisplay("330x440/r,400x500");
1091  EXPECT_FALSE(display_observer.changed_and_reset());
1092  EXPECT_EQ("330x440",
1093            test_api.GetHost()->window()->bounds().size().ToString());
1094
1095  // Overscan insets are ignored.
1096  UpdateDisplay("400x600/o,600x800/o");
1097  EXPECT_FALSE(display_observer.changed_and_reset());
1098  EXPECT_EQ("400x600",
1099            test_api.GetHost()->window()->bounds().size().ToString());
1100
1101  Shell::GetScreen()->RemoveObserver(&display_observer);
1102}
1103
1104TEST_F(DisplayManagerTest, MirroredLayout) {
1105  if (!SupportsMultipleDisplays())
1106    return;
1107
1108  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
1109  UpdateDisplay("500x500,400x400");
1110  EXPECT_FALSE(display_manager->GetCurrentDisplayLayout().mirrored);
1111  EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1112  EXPECT_EQ(2U, display_manager->num_connected_displays());
1113
1114  UpdateDisplay("1+0-500x500,1+0-500x500");
1115  EXPECT_TRUE(display_manager->GetCurrentDisplayLayout().mirrored);
1116  EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
1117  EXPECT_EQ(2U, display_manager->num_connected_displays());
1118
1119  UpdateDisplay("500x500,500x500");
1120  EXPECT_FALSE(display_manager->GetCurrentDisplayLayout().mirrored);
1121  EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1122  EXPECT_EQ(2U, display_manager->num_connected_displays());
1123}
1124
1125TEST_F(DisplayManagerTest, InvertLayout) {
1126  EXPECT_EQ("left, 0",
1127            DisplayLayout(DisplayLayout::RIGHT, 0).Invert().ToString());
1128  EXPECT_EQ("left, -100",
1129            DisplayLayout(DisplayLayout::RIGHT, 100).Invert().ToString());
1130  EXPECT_EQ("left, 50",
1131            DisplayLayout(DisplayLayout::RIGHT, -50).Invert().ToString());
1132
1133  EXPECT_EQ("right, 0",
1134            DisplayLayout(DisplayLayout::LEFT, 0).Invert().ToString());
1135  EXPECT_EQ("right, -90",
1136            DisplayLayout(DisplayLayout::LEFT, 90).Invert().ToString());
1137  EXPECT_EQ("right, 60",
1138            DisplayLayout(DisplayLayout::LEFT, -60).Invert().ToString());
1139
1140  EXPECT_EQ("bottom, 0",
1141            DisplayLayout(DisplayLayout::TOP, 0).Invert().ToString());
1142  EXPECT_EQ("bottom, -80",
1143            DisplayLayout(DisplayLayout::TOP, 80).Invert().ToString());
1144  EXPECT_EQ("bottom, 70",
1145            DisplayLayout(DisplayLayout::TOP, -70).Invert().ToString());
1146
1147  EXPECT_EQ("top, 0",
1148            DisplayLayout(DisplayLayout::BOTTOM, 0).Invert().ToString());
1149  EXPECT_EQ("top, -70",
1150            DisplayLayout(DisplayLayout::BOTTOM, 70).Invert().ToString());
1151  EXPECT_EQ("top, 80",
1152            DisplayLayout(DisplayLayout::BOTTOM, -80).Invert().ToString());
1153}
1154
1155#if defined(OS_WIN)
1156// TODO(scottmg): RootWindow doesn't get resized on Windows
1157// Ash. http://crbug.com/247916.
1158#define MAYBE_UpdateDisplayWithHostOrigin DISABLED_UpdateDisplayWithHostOrigin
1159#else
1160#define MAYBE_UpdateDisplayWithHostOrigin UpdateDisplayWithHostOrigin
1161#endif
1162
1163TEST_F(DisplayManagerTest, MAYBE_UpdateDisplayWithHostOrigin) {
1164  UpdateDisplay("100x200,300x400");
1165  ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1166  aura::Window::Windows root_windows =
1167      Shell::GetInstance()->GetAllRootWindows();
1168  ASSERT_EQ(2U, root_windows.size());
1169  aura::WindowTreeHost* host0 = root_windows[0]->GetHost();
1170  aura::WindowTreeHost* host1 = root_windows[1]->GetHost();
1171
1172  EXPECT_EQ("1,1", host0->GetBounds().origin().ToString());
1173  EXPECT_EQ("100x200", host0->GetBounds().size().ToString());
1174  // UpdateDisplay set the origin if it's not set.
1175  EXPECT_NE("1,1", host1->GetBounds().origin().ToString());
1176  EXPECT_EQ("300x400", host1->GetBounds().size().ToString());
1177
1178  UpdateDisplay("100x200,200+300-300x400");
1179  ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1180  EXPECT_EQ("0,0", host0->GetBounds().origin().ToString());
1181  EXPECT_EQ("100x200", host0->GetBounds().size().ToString());
1182  EXPECT_EQ("200,300", host1->GetBounds().origin().ToString());
1183  EXPECT_EQ("300x400", host1->GetBounds().size().ToString());
1184
1185  UpdateDisplay("400+500-200x300,300x400");
1186  ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1187  EXPECT_EQ("400,500", host0->GetBounds().origin().ToString());
1188  EXPECT_EQ("200x300", host0->GetBounds().size().ToString());
1189  EXPECT_EQ("0,0", host1->GetBounds().origin().ToString());
1190  EXPECT_EQ("300x400", host1->GetBounds().size().ToString());
1191
1192  UpdateDisplay("100+200-100x200,300+500-200x300");
1193  ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
1194  EXPECT_EQ("100,200", host0->GetBounds().origin().ToString());
1195  EXPECT_EQ("100x200", host0->GetBounds().size().ToString());
1196  EXPECT_EQ("300,500", host1->GetBounds().origin().ToString());
1197  EXPECT_EQ("200x300", host1->GetBounds().size().ToString());
1198}
1199
1200
1201class ScreenShutdownTest : public test::AshTestBase {
1202 public:
1203  ScreenShutdownTest() {
1204  }
1205  virtual ~ScreenShutdownTest() {}
1206
1207  virtual void TearDown() OVERRIDE {
1208    gfx::Screen* orig_screen =
1209        gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
1210    AshTestBase::TearDown();
1211    if (!SupportsMultipleDisplays())
1212      return;
1213    gfx::Screen* screen =
1214        gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
1215    EXPECT_NE(orig_screen, screen);
1216    EXPECT_EQ(2, screen->GetNumDisplays());
1217    EXPECT_EQ("500x300", screen->GetPrimaryDisplay().size().ToString());
1218    std::vector<gfx::Display> all = screen->GetAllDisplays();
1219    EXPECT_EQ("500x300", all[0].size().ToString());
1220    EXPECT_EQ("800x400", all[1].size().ToString());
1221  }
1222
1223 private:
1224  DISALLOW_COPY_AND_ASSIGN(ScreenShutdownTest);
1225};
1226
1227TEST_F(ScreenShutdownTest, ScreenAfterShutdown) {
1228  if (!SupportsMultipleDisplays())
1229    return;
1230  UpdateDisplay("500x300,800x400");
1231}
1232
1233}  // namespace internal
1234}  // namespace ash
1235