- add sources.
[platform/framework/web/crosswalk.git] / src / ash / display / display_controller_unittest.cc
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_controller.h"
6
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_info.h"
9 #include "ash/display/display_layout_store.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/launcher/launcher.h"
12 #include "ash/screen_ash.h"
13 #include "ash/shelf/shelf_widget.h"
14 #include "ash/shell.h"
15 #include "ash/test/ash_test_base.h"
16 #include "ash/test/cursor_manager_test_api.h"
17 #include "base/command_line.h"
18 #include "ui/aura/client/activation_change_observer.h"
19 #include "ui/aura/client/activation_client.h"
20 #include "ui/aura/client/focus_change_observer.h"
21 #include "ui/aura/client/focus_client.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/root_window.h"
24 #include "ui/aura/test/event_generator.h"
25 #include "ui/aura/window_tracker.h"
26 #include "ui/events/event_handler.h"
27 #include "ui/gfx/display.h"
28 #include "ui/gfx/screen.h"
29 #include "ui/views/widget/widget.h"
30
31 #if defined(USE_X11)
32 #include "ui/gfx/x/x11_types.h"
33 #include <X11/Xlib.h>
34 #undef RootWindow
35 #endif
36
37 namespace ash {
38 namespace {
39
40 const char kDesktopBackgroundView[] = "DesktopBackgroundView";
41
42 template<typename T>
43 class Resetter {
44  public:
45   explicit Resetter(T* value) : value_(*value) {
46     *value = 0;
47   }
48   ~Resetter() { }
49   T value() { return value_; }
50
51  private:
52   T value_;
53   DISALLOW_COPY_AND_ASSIGN(Resetter);
54 };
55
56 class TestObserver : public DisplayController::Observer,
57                      public gfx::DisplayObserver,
58                      public aura::client::FocusChangeObserver,
59                      public aura::client::ActivationChangeObserver {
60  public:
61   TestObserver()
62       : changing_count_(0),
63         changed_count_(0),
64         bounds_changed_count_(0),
65         changed_display_id_(0),
66         focus_changed_count_(0),
67         activation_changed_count_(0) {
68     Shell::GetInstance()->display_controller()->AddObserver(this);
69     Shell::GetScreen()->AddObserver(this);
70     aura::client::GetFocusClient(Shell::GetPrimaryRootWindow())->
71         AddObserver(this);
72     aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
73         AddObserver(this);
74   }
75
76   virtual ~TestObserver() {
77     Shell::GetInstance()->display_controller()->RemoveObserver(this);
78     Shell::GetScreen()->RemoveObserver(this);
79     aura::client::GetFocusClient(Shell::GetPrimaryRootWindow())->
80         RemoveObserver(this);
81     aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
82         RemoveObserver(this);
83   }
84
85   // Overridden from DisplayController::Observer
86   virtual void OnDisplayConfigurationChanging() OVERRIDE {
87     ++changing_count_;
88   }
89   virtual void OnDisplayConfigurationChanged() OVERRIDE {
90     ++changed_count_;
91   }
92
93   // Overrideen from gfx::DisplayObserver
94   virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE {
95     changed_display_id_ = display.id();
96     bounds_changed_count_ ++;
97   }
98   virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE {
99   }
100   virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE {
101   }
102
103   // Overridden from aura::client::FocusChangeObserver
104   virtual void OnWindowFocused(aura::Window* gained_focus,
105                                aura::Window* lost_focus) OVERRIDE {
106     focus_changed_count_++;
107   }
108
109   // Overridden from aura::client::ActivationChangeObserver
110   virtual void OnWindowActivated(aura::Window* gained_active,
111                                  aura::Window* lost_active) OVERRIDE {
112     activation_changed_count_++;
113   }
114   virtual void OnAttemptToReactivateWindow(
115       aura::Window* request_active,
116       aura::Window* actual_active) OVERRIDE {
117   }
118
119   int CountAndReset() {
120     EXPECT_EQ(changing_count_, changed_count_);
121     changed_count_ = 0;
122     return Resetter<int>(&changing_count_).value();
123   }
124
125   int64 GetBoundsChangedCountAndReset() {
126     return Resetter<int>(&bounds_changed_count_).value();
127   }
128
129   int64 GetChangedDisplayIdAndReset() {
130     return Resetter<int64>(&changed_display_id_).value();
131   }
132
133   int GetFocusChangedCountAndReset() {
134     return Resetter<int>(&focus_changed_count_).value();
135   }
136
137   int GetActivationChangedCountAndReset() {
138     return Resetter<int>(&activation_changed_count_).value();
139   }
140
141  private:
142   int changing_count_;
143   int changed_count_;
144
145   int bounds_changed_count_;
146   int64 changed_display_id_;
147
148   int focus_changed_count_;
149   int activation_changed_count_;
150
151   DISALLOW_COPY_AND_ASSIGN(TestObserver);
152 };
153
154 gfx::Display GetPrimaryDisplay() {
155   return Shell::GetScreen()->GetDisplayNearestWindow(
156       Shell::GetAllRootWindows()[0]);
157 }
158
159 gfx::Display GetSecondaryDisplay() {
160   return Shell::GetScreen()->GetDisplayNearestWindow(
161       Shell::GetAllRootWindows()[1]);
162 }
163
164 void SetSecondaryDisplayLayoutAndOffset(DisplayLayout::Position position,
165                                         int offset) {
166   DisplayLayout layout(position, offset);
167   ASSERT_GT(Shell::GetScreen()->GetNumDisplays(), 1);
168   Shell::GetInstance()->display_manager()->
169       SetLayoutForCurrentDisplays(layout);
170 }
171
172 void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
173   SetSecondaryDisplayLayoutAndOffset(position, 0);
174 }
175
176 void SetDefaultDisplayLayout(DisplayLayout::Position position) {
177   Shell::GetInstance()->display_manager()->layout_store()->
178       SetDefaultDisplayLayout(DisplayLayout(position, 0));
179 }
180
181 class DisplayControllerShutdownTest : public test::AshTestBase {
182  public:
183   virtual void TearDown() OVERRIDE {
184     test::AshTestBase::TearDown();
185     if (!SupportsMultipleDisplays())
186       return;
187
188     // Make sure that primary display is accessible after shutdown.
189     gfx::Display primary = Shell::GetScreen()->GetPrimaryDisplay();
190     EXPECT_EQ("0,0 444x333", primary.bounds().ToString());
191     EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
192   }
193 };
194
195 class TestEventHandler : public ui::EventHandler {
196  public:
197   TestEventHandler() : target_root_(NULL),
198                        touch_radius_x_(0.0),
199                        touch_radius_y_(0.0),
200                        scroll_x_offset_(0.0),
201                        scroll_y_offset_(0.0),
202                        scroll_x_offset_ordinal_(0.0),
203                        scroll_y_offset_ordinal_(0.0) {}
204   virtual ~TestEventHandler() {}
205
206   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
207     if (event->flags() & ui::EF_IS_SYNTHESIZED &&
208         event->type() != ui::ET_MOUSE_EXITED &&
209         event->type() != ui::ET_MOUSE_ENTERED) {
210       return;
211     }
212     aura::Window* target = static_cast<aura::Window*>(event->target());
213     mouse_location_ = event->root_location();
214     target_root_ = target->GetRootWindow();
215     event->StopPropagation();
216   }
217
218   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
219     aura::Window* target = static_cast<aura::Window*>(event->target());
220     // Only record when the target is the background which covers
221     // entire root window.
222     if (target->name() != kDesktopBackgroundView)
223       return;
224     touch_radius_x_ = event->radius_x();
225     touch_radius_y_ = event->radius_y();
226     event->StopPropagation();
227   }
228
229   virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE {
230     aura::Window* target = static_cast<aura::Window*>(event->target());
231     // Only record when the target is the background which covers
232     // entire root window.
233     if (target->name() != kDesktopBackgroundView)
234       return;
235
236     if (event->type() == ui::ET_SCROLL) {
237       scroll_x_offset_ = event->x_offset();
238       scroll_y_offset_ = event->y_offset();
239       scroll_x_offset_ordinal_ = event->x_offset_ordinal();
240       scroll_y_offset_ordinal_ = event->y_offset_ordinal();
241     }
242     event->StopPropagation();
243   }
244
245   std::string GetLocationAndReset() {
246     std::string result = mouse_location_.ToString();
247     mouse_location_.SetPoint(0, 0);
248     target_root_ = NULL;
249     return result;
250   }
251
252   float touch_radius_x() { return touch_radius_x_; }
253   float touch_radius_y() { return touch_radius_y_; }
254   float scroll_x_offset() { return scroll_x_offset_; }
255   float scroll_y_offset() { return scroll_y_offset_; }
256   float scroll_x_offset_ordinal() { return scroll_x_offset_ordinal_; }
257   float scroll_y_offset_ordinal() { return scroll_y_offset_ordinal_; }
258
259  private:
260   gfx::Point mouse_location_;
261   aura::Window* target_root_;
262
263   float touch_radius_x_;
264   float touch_radius_y_;
265   float scroll_x_offset_;
266   float scroll_y_offset_;
267   float scroll_x_offset_ordinal_;
268   float scroll_y_offset_ordinal_;
269
270   DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
271 };
272
273 gfx::Display::Rotation GetStoredRotation(int64 id) {
274   return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).rotation();
275 }
276
277 float GetStoredUIScale(int64 id) {
278   return Shell::GetInstance()->display_manager()->GetDisplayInfo(id).ui_scale();
279 }
280
281 #if defined(USE_X11)
282 void GetPrimaryAndSeconary(aura::Window** primary,
283                            aura::Window** secondary) {
284   *primary = Shell::GetPrimaryRootWindow();
285   Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
286   *secondary = root_windows[0] == *primary ? root_windows[1] : root_windows[0];
287 }
288
289 std::string GetXWindowName(aura::RootWindow* window) {
290   char* name = NULL;
291   XFetchName(gfx::GetXDisplay(), window->GetAcceleratedWidget(), &name);
292   std::string ret(name);
293   XFree(name);
294   return ret;
295 }
296 #endif
297
298 }  // namespace
299
300 typedef test::AshTestBase DisplayControllerTest;
301
302 TEST_F(DisplayControllerShutdownTest, Shutdown) {
303   if (!SupportsMultipleDisplays())
304     return;
305
306   UpdateDisplay("444x333, 200x200");
307 }
308
309 TEST_F(DisplayControllerTest, SecondaryDisplayLayout) {
310   if (!SupportsMultipleDisplays())
311     return;
312
313   // Creates windows to catch activation change event.
314   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(1));
315   w1->Focus();
316
317   TestObserver observer;
318   UpdateDisplay("500x500,400x400");
319   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
320   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
321   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
322   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
323   gfx::Insets insets(5, 5, 5, 5);
324   int64 secondary_display_id = ScreenAsh::GetSecondaryDisplay().id();
325   Shell::GetInstance()->display_manager()->UpdateWorkAreaOfDisplay(
326       secondary_display_id, insets);
327
328   // Default layout is RIGHT.
329   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
330   EXPECT_EQ("500,0 400x400", GetSecondaryDisplay().bounds().ToString());
331   EXPECT_EQ("505,5 390x390", GetSecondaryDisplay().work_area().ToString());
332   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
333   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
334
335   // Layout the secondary display to the bottom of the primary.
336   SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
337   EXPECT_EQ(1, observer.CountAndReset());
338   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
339   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
340   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
341   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
342   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
343   EXPECT_EQ("0,500 400x400", GetSecondaryDisplay().bounds().ToString());
344   EXPECT_EQ("5,505 390x390", GetSecondaryDisplay().work_area().ToString());
345
346   // Layout the secondary display to the left of the primary.
347   SetSecondaryDisplayLayout(DisplayLayout::LEFT);
348   EXPECT_EQ(1, observer.CountAndReset());
349   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
350   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
351   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
352   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
353   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
354   EXPECT_EQ("-400,0 400x400", GetSecondaryDisplay().bounds().ToString());
355   EXPECT_EQ("-395,5 390x390", GetSecondaryDisplay().work_area().ToString());
356
357   // Layout the secondary display to the top of the primary.
358   SetSecondaryDisplayLayout(DisplayLayout::TOP);
359   EXPECT_EQ(1, observer.CountAndReset());
360   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
361   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
362   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
363   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
364   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
365   EXPECT_EQ("0,-400 400x400", GetSecondaryDisplay().bounds().ToString());
366   EXPECT_EQ("5,-395 390x390", GetSecondaryDisplay().work_area().ToString());
367
368   // Layout to the right with an offset.
369   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::RIGHT, 300);
370   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
371   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
372   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
373   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
374   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
375   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
376   EXPECT_EQ("500,300 400x400", GetSecondaryDisplay().bounds().ToString());
377
378   // Keep the minimum 100.
379   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::RIGHT, 490);
380   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
381   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
382   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
383   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
384   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
385   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
386   EXPECT_EQ("500,400 400x400", GetSecondaryDisplay().bounds().ToString());
387
388   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::RIGHT, -400);
389   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
390   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
391   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
392   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
393   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
394   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
395   EXPECT_EQ("500,-300 400x400", GetSecondaryDisplay().bounds().ToString());
396
397   //  Layout to the bottom with an offset.
398   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, -200);
399   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
400   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
401   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
402   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
403   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
404   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
405   EXPECT_EQ("-200,500 400x400", GetSecondaryDisplay().bounds().ToString());
406
407   // Keep the minimum 100.
408   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, 490);
409   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
410   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
411   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
412   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
413   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
414   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
415   EXPECT_EQ("400,500 400x400", GetSecondaryDisplay().bounds().ToString());
416
417   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, -400);
418   EXPECT_EQ(secondary_display_id, observer.GetChangedDisplayIdAndReset());
419   EXPECT_EQ(1, observer.GetBoundsChangedCountAndReset());
420   EXPECT_EQ(1, observer.CountAndReset());  // resize and add
421   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
422   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
423   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
424   EXPECT_EQ("-300,500 400x400", GetSecondaryDisplay().bounds().ToString());
425
426   // Setting the same layout shouldn't invoke observers.
427   SetSecondaryDisplayLayoutAndOffset(DisplayLayout::BOTTOM, -400);
428   EXPECT_EQ(0, observer.GetChangedDisplayIdAndReset());
429   EXPECT_EQ(0, observer.GetBoundsChangedCountAndReset());
430   EXPECT_EQ(0, observer.CountAndReset());  // resize and add
431   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
432   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
433   EXPECT_EQ("0,0 500x500", GetPrimaryDisplay().bounds().ToString());
434   EXPECT_EQ("-300,500 400x400", GetSecondaryDisplay().bounds().ToString());
435
436   UpdateDisplay("500x500");
437   EXPECT_LE(1, observer.GetFocusChangedCountAndReset());
438   EXPECT_LE(1, observer.GetActivationChangedCountAndReset());
439 }
440
441 TEST_F(DisplayControllerTest, BoundsUpdated) {
442   if (!SupportsMultipleDisplays())
443     return;
444
445   // Creates windows to catch activation change event.
446   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(1));
447   w1->Focus();
448
449   TestObserver observer;
450   SetDefaultDisplayLayout(DisplayLayout::BOTTOM);
451   UpdateDisplay("200x200,300x300");  // layout, resize and add.
452   EXPECT_EQ(1, observer.CountAndReset());
453   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
454   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
455
456   internal::DisplayManager* display_manager =
457       Shell::GetInstance()->display_manager();
458   gfx::Insets insets(5, 5, 5, 5);
459   display_manager->UpdateWorkAreaOfDisplay(
460       ScreenAsh::GetSecondaryDisplay().id(), insets);
461
462   EXPECT_EQ("0,0 200x200", GetPrimaryDisplay().bounds().ToString());
463   EXPECT_EQ("0,200 300x300", GetSecondaryDisplay().bounds().ToString());
464   EXPECT_EQ("5,205 290x290", GetSecondaryDisplay().work_area().ToString());
465
466   UpdateDisplay("400x400,200x200");
467   EXPECT_EQ(1, observer.CountAndReset());  // two resizes
468   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
469   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
470   EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString());
471   EXPECT_EQ("0,400 200x200", GetSecondaryDisplay().bounds().ToString());
472
473   UpdateDisplay("400x400,300x300");
474   EXPECT_EQ(1, observer.CountAndReset());
475   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
476   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
477   EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString());
478   EXPECT_EQ("0,400 300x300", GetSecondaryDisplay().bounds().ToString());
479
480   UpdateDisplay("400x400");
481   EXPECT_EQ(1, observer.CountAndReset());
482   EXPECT_LE(1, observer.GetFocusChangedCountAndReset());
483   EXPECT_LE(1, observer.GetActivationChangedCountAndReset());
484   EXPECT_EQ("0,0 400x400", GetPrimaryDisplay().bounds().ToString());
485   EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
486
487   UpdateDisplay("400x500*2,300x300");
488   EXPECT_EQ(1, observer.CountAndReset());
489   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
490   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
491   ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays());
492   EXPECT_EQ("0,0 200x250", GetPrimaryDisplay().bounds().ToString());
493   EXPECT_EQ("0,250 300x300", GetSecondaryDisplay().bounds().ToString());
494
495   // No change
496   UpdateDisplay("400x500*2,300x300");
497   EXPECT_EQ(0, observer.CountAndReset());
498   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
499   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
500
501   // Rotation
502   int64 primary_id = GetPrimaryDisplay().id();
503   display_manager->SetDisplayRotation(primary_id, gfx::Display::ROTATE_90);
504   EXPECT_EQ(1, observer.CountAndReset());
505   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
506   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
507   display_manager->SetDisplayRotation(primary_id, gfx::Display::ROTATE_90);
508   EXPECT_EQ(0, observer.CountAndReset());
509   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
510   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
511
512   // UI scale is eanbled only on internal display.
513   int64 secondary_id = GetSecondaryDisplay().id();
514   gfx::Display::SetInternalDisplayId(secondary_id);
515   display_manager->SetDisplayUIScale(secondary_id, 1.125f);
516   EXPECT_EQ(1, observer.CountAndReset());
517   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
518   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
519   display_manager->SetDisplayUIScale(secondary_id, 1.125f);
520   EXPECT_EQ(0, observer.CountAndReset());
521   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
522   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
523   display_manager->SetDisplayUIScale(primary_id, 1.125f);
524   EXPECT_EQ(0, observer.CountAndReset());
525   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
526   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
527   display_manager->SetDisplayUIScale(primary_id, 1.125f);
528   EXPECT_EQ(0, observer.CountAndReset());
529   EXPECT_EQ(0, observer.GetFocusChangedCountAndReset());
530   EXPECT_EQ(0, observer.GetActivationChangedCountAndReset());
531 }
532
533 TEST_F(DisplayControllerTest, SwapPrimary) {
534   if (!SupportsMultipleDisplays())
535     return;
536
537   DisplayController* display_controller =
538       Shell::GetInstance()->display_controller();
539   internal::DisplayManager* display_manager =
540       Shell::GetInstance()->display_manager();
541
542   UpdateDisplay("200x200,300x300");
543   gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
544   gfx::Display secondary_display = ScreenAsh::GetSecondaryDisplay();
545
546   DisplayLayout display_layout(DisplayLayout::RIGHT, 50);
547   display_manager->SetLayoutForCurrentDisplays(display_layout);
548
549   EXPECT_NE(primary_display.id(), secondary_display.id());
550   aura::Window* primary_root =
551       display_controller->GetRootWindowForDisplayId(primary_display.id());
552   aura::Window* secondary_root =
553       display_controller->GetRootWindowForDisplayId(secondary_display.id());
554   EXPECT_NE(primary_root, secondary_root);
555   aura::Window* launcher_window =
556       Launcher::ForPrimaryDisplay()->shelf_widget()->GetNativeView();
557   EXPECT_TRUE(primary_root->Contains(launcher_window));
558   EXPECT_FALSE(secondary_root->Contains(launcher_window));
559   EXPECT_EQ(primary_display.id(),
560             Shell::GetScreen()->GetDisplayNearestPoint(
561                 gfx::Point(-100, -100)).id());
562   EXPECT_EQ(primary_display.id(),
563             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
564
565   EXPECT_EQ("0,0 200x200", primary_display.bounds().ToString());
566   EXPECT_EQ("0,0 200x153", primary_display.work_area().ToString());
567   EXPECT_EQ("200,0 300x300", secondary_display.bounds().ToString());
568   EXPECT_EQ("200,0 300x253", secondary_display.work_area().ToString());
569   EXPECT_EQ("right, 50",
570             display_manager->GetCurrentDisplayLayout().ToString());
571
572   // Switch primary and secondary
573   display_controller->SetPrimaryDisplay(secondary_display);
574   const DisplayLayout& inverted_layout =
575       display_manager->GetCurrentDisplayLayout();
576   EXPECT_EQ("left, -50", inverted_layout.ToString());
577
578   EXPECT_EQ(secondary_display.id(),
579             Shell::GetScreen()->GetPrimaryDisplay().id());
580   EXPECT_EQ(primary_display.id(), ScreenAsh::GetSecondaryDisplay().id());
581   EXPECT_EQ(secondary_display.id(),
582             Shell::GetScreen()->GetDisplayNearestPoint(
583                 gfx::Point(-100, -100)).id());
584   EXPECT_EQ(secondary_display.id(),
585             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
586
587   EXPECT_EQ(
588       primary_root,
589       display_controller->GetRootWindowForDisplayId(secondary_display.id()));
590   EXPECT_EQ(
591       secondary_root,
592       display_controller->GetRootWindowForDisplayId(primary_display.id()));
593   EXPECT_TRUE(primary_root->Contains(launcher_window));
594   EXPECT_FALSE(secondary_root->Contains(launcher_window));
595
596   // Test if the bounds are correctly swapped.
597   gfx::Display swapped_primary = Shell::GetScreen()->GetPrimaryDisplay();
598   gfx::Display swapped_secondary = ScreenAsh::GetSecondaryDisplay();
599   EXPECT_EQ("0,0 300x300", swapped_primary.bounds().ToString());
600   EXPECT_EQ("0,0 300x253", swapped_primary.work_area().ToString());
601   EXPECT_EQ("-200,-50 200x200", swapped_secondary.bounds().ToString());
602
603   EXPECT_EQ("-200,-50 200x153", swapped_secondary.work_area().ToString());
604
605   aura::WindowTracker tracker;
606   tracker.Add(primary_root);
607   tracker.Add(secondary_root);
608
609   // Deleting 2nd display should move the primary to original primary display.
610   UpdateDisplay("200x200");
611   RunAllPendingInMessageLoop();  // RootWindow is deleted in a posted task.
612   EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
613   EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetPrimaryDisplay().id());
614   EXPECT_EQ(primary_display.id(),
615             Shell::GetScreen()->GetDisplayNearestPoint(
616                 gfx::Point(-100, -100)).id());
617   EXPECT_EQ(primary_display.id(),
618             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
619   EXPECT_TRUE(tracker.Contains(primary_root));
620   EXPECT_FALSE(tracker.Contains(secondary_root));
621   EXPECT_TRUE(primary_root->Contains(launcher_window));
622 }
623
624 TEST_F(DisplayControllerTest, SwapPrimaryForLegacyShelfLayout) {
625   if (!SupportsMultipleDisplays())
626     return;
627
628   CommandLine::ForCurrentProcess()->AppendSwitch(
629       ash::switches::kAshDisableAlternateShelfLayout);
630
631   DisplayController* display_controller =
632       Shell::GetInstance()->display_controller();
633   internal::DisplayManager* display_manager =
634       Shell::GetInstance()->display_manager();
635
636   UpdateDisplay("200x200,300x300");
637   gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
638   gfx::Display secondary_display = ScreenAsh::GetSecondaryDisplay();
639
640   DisplayLayout display_layout(DisplayLayout::RIGHT, 50);
641   display_manager->SetLayoutForCurrentDisplays(display_layout);
642
643   EXPECT_NE(primary_display.id(), secondary_display.id());
644   aura::Window* primary_root =
645       display_controller->GetRootWindowForDisplayId(primary_display.id());
646   aura::Window* secondary_root =
647       display_controller->GetRootWindowForDisplayId(secondary_display.id());
648   EXPECT_NE(primary_root, secondary_root);
649   aura::Window* launcher_window =
650       Launcher::ForPrimaryDisplay()->shelf_widget()->GetNativeView();
651   EXPECT_TRUE(primary_root->Contains(launcher_window));
652   EXPECT_FALSE(secondary_root->Contains(launcher_window));
653   EXPECT_EQ(primary_display.id(),
654             Shell::GetScreen()->GetDisplayNearestPoint(
655                 gfx::Point(-100, -100)).id());
656   EXPECT_EQ(primary_display.id(),
657             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
658
659   EXPECT_EQ("0,0 200x200", primary_display.bounds().ToString());
660   EXPECT_EQ("0,0 200x152", primary_display.work_area().ToString());
661   EXPECT_EQ("200,0 300x300", secondary_display.bounds().ToString());
662   EXPECT_EQ("200,0 300x252", secondary_display.work_area().ToString());
663   EXPECT_EQ("right, 50",
664             display_manager->GetCurrentDisplayLayout().ToString());
665
666   // Switch primary and secondary
667   display_controller->SetPrimaryDisplay(secondary_display);
668   const DisplayLayout& inverted_layout =
669       display_manager->GetCurrentDisplayLayout();
670   EXPECT_EQ("left, -50", inverted_layout.ToString());
671
672   EXPECT_EQ(secondary_display.id(),
673             Shell::GetScreen()->GetPrimaryDisplay().id());
674   EXPECT_EQ(primary_display.id(), ScreenAsh::GetSecondaryDisplay().id());
675   EXPECT_EQ(secondary_display.id(),
676             Shell::GetScreen()->GetDisplayNearestPoint(
677                 gfx::Point(-100, -100)).id());
678   EXPECT_EQ(secondary_display.id(),
679             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
680
681   EXPECT_EQ(
682       primary_root,
683       display_controller->GetRootWindowForDisplayId(secondary_display.id()));
684   EXPECT_EQ(
685       secondary_root,
686       display_controller->GetRootWindowForDisplayId(primary_display.id()));
687   EXPECT_TRUE(primary_root->Contains(launcher_window));
688   EXPECT_FALSE(secondary_root->Contains(launcher_window));
689
690   // Test if the bounds are correctly swapped.
691   gfx::Display swapped_primary = Shell::GetScreen()->GetPrimaryDisplay();
692   gfx::Display swapped_secondary = ScreenAsh::GetSecondaryDisplay();
693   EXPECT_EQ("0,0 300x300", swapped_primary.bounds().ToString());
694   EXPECT_EQ("0,0 300x252", swapped_primary.work_area().ToString());
695   EXPECT_EQ("-200,-50 200x200", swapped_secondary.bounds().ToString());
696
697   EXPECT_EQ("-200,-50 200x152", swapped_secondary.work_area().ToString());
698
699   aura::WindowTracker tracker;
700   tracker.Add(primary_root);
701   tracker.Add(secondary_root);
702
703   // Deleting 2nd display should move the primary to original primary display.
704   UpdateDisplay("200x200");
705   RunAllPendingInMessageLoop();  // RootWindow is deleted in a posted task.
706   EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
707   EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetPrimaryDisplay().id());
708   EXPECT_EQ(primary_display.id(),
709             Shell::GetScreen()->GetDisplayNearestPoint(
710                 gfx::Point(-100, -100)).id());
711   EXPECT_EQ(primary_display.id(),
712             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
713   EXPECT_TRUE(tracker.Contains(primary_root));
714   EXPECT_FALSE(tracker.Contains(secondary_root));
715   EXPECT_TRUE(primary_root->Contains(launcher_window));
716 }
717
718 TEST_F(DisplayControllerTest, SwapPrimaryById) {
719   if (!SupportsMultipleDisplays())
720     return;
721
722   DisplayController* display_controller =
723       Shell::GetInstance()->display_controller();
724   internal::DisplayManager* display_manager =
725       Shell::GetInstance()->display_manager();
726
727   UpdateDisplay("200x200,300x300");
728   gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
729   gfx::Display secondary_display = ScreenAsh::GetSecondaryDisplay();
730
731   DisplayLayout display_layout(DisplayLayout::RIGHT, 50);
732   display_manager->SetLayoutForCurrentDisplays(display_layout);
733
734   EXPECT_NE(primary_display.id(), secondary_display.id());
735   aura::Window* primary_root =
736       display_controller->GetRootWindowForDisplayId(primary_display.id());
737   aura::Window* secondary_root =
738       display_controller->GetRootWindowForDisplayId(secondary_display.id());
739   aura::Window* launcher_window =
740       Launcher::ForPrimaryDisplay()->shelf_widget()->GetNativeView();
741   EXPECT_TRUE(primary_root->Contains(launcher_window));
742   EXPECT_FALSE(secondary_root->Contains(launcher_window));
743   EXPECT_NE(primary_root, secondary_root);
744   EXPECT_EQ(primary_display.id(),
745             Shell::GetScreen()->GetDisplayNearestPoint(
746                 gfx::Point(-100, -100)).id());
747   EXPECT_EQ(primary_display.id(),
748             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
749
750   // Switch primary and secondary by display ID.
751   TestObserver observer;
752   display_controller->SetPrimaryDisplayId(secondary_display.id());
753   EXPECT_EQ(secondary_display.id(),
754             Shell::GetScreen()->GetPrimaryDisplay().id());
755   EXPECT_EQ(primary_display.id(), ScreenAsh::GetSecondaryDisplay().id());
756   EXPECT_LT(0, observer.CountAndReset());
757
758   EXPECT_EQ(
759       primary_root,
760       display_controller->GetRootWindowForDisplayId(secondary_display.id()));
761   EXPECT_EQ(
762       secondary_root,
763       display_controller->GetRootWindowForDisplayId(primary_display.id()));
764   EXPECT_TRUE(primary_root->Contains(launcher_window));
765   EXPECT_FALSE(secondary_root->Contains(launcher_window));
766
767   const DisplayLayout& inverted_layout =
768       display_manager->GetCurrentDisplayLayout();
769
770   EXPECT_EQ("left, -50", inverted_layout.ToString());
771
772   // Calling the same ID don't do anything.
773   display_controller->SetPrimaryDisplayId(secondary_display.id());
774   EXPECT_EQ(0, observer.CountAndReset());
775
776   aura::WindowTracker tracker;
777   tracker.Add(primary_root);
778   tracker.Add(secondary_root);
779
780   // Deleting 2nd display should move the primary to original primary display.
781   UpdateDisplay("200x200");
782   RunAllPendingInMessageLoop();  // RootWindow is deleted in a posted task.
783   EXPECT_EQ(1, Shell::GetScreen()->GetNumDisplays());
784   EXPECT_EQ(primary_display.id(), Shell::GetScreen()->GetPrimaryDisplay().id());
785   EXPECT_EQ(primary_display.id(),
786             Shell::GetScreen()->GetDisplayNearestPoint(
787                 gfx::Point(-100, -100)).id());
788   EXPECT_EQ(primary_display.id(),
789             Shell::GetScreen()->GetDisplayNearestWindow(NULL).id());
790   EXPECT_TRUE(tracker.Contains(primary_root));
791   EXPECT_FALSE(tracker.Contains(secondary_root));
792   EXPECT_TRUE(primary_root->Contains(launcher_window));
793
794   // Adding 2nd display with the same ID.  The 2nd display should become primary
795   // since secondary id is still stored as desirable_primary_id.
796   std::vector<internal::DisplayInfo> display_info_list;
797   display_info_list.push_back(
798       display_manager->GetDisplayInfo(primary_display.id()));
799   display_info_list.push_back(
800       display_manager->GetDisplayInfo(secondary_display.id()));
801   display_manager->OnNativeDisplaysChanged(display_info_list);
802
803   EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
804   EXPECT_EQ(secondary_display.id(),
805             Shell::GetScreen()->GetPrimaryDisplay().id());
806   EXPECT_EQ(primary_display.id(), ScreenAsh::GetSecondaryDisplay().id());
807   EXPECT_EQ(
808       primary_root,
809       display_controller->GetRootWindowForDisplayId(secondary_display.id()));
810   EXPECT_NE(
811       primary_root,
812       display_controller->GetRootWindowForDisplayId(primary_display.id()));
813   EXPECT_TRUE(primary_root->Contains(launcher_window));
814
815   // Deleting 2nd display and adding 2nd display with a different ID.  The 2nd
816   // display shouldn't become primary.
817   UpdateDisplay("200x200");
818   internal::DisplayInfo third_display_info(
819       secondary_display.id() + 1, std::string(), false);
820   third_display_info.SetBounds(secondary_display.bounds());
821   ASSERT_NE(primary_display.id(), third_display_info.id());
822
823   const internal::DisplayInfo& primary_display_info =
824       display_manager->GetDisplayInfo(primary_display.id());
825   std::vector<internal::DisplayInfo> display_info_list2;
826   display_info_list2.push_back(primary_display_info);
827   display_info_list2.push_back(third_display_info);
828   display_manager->OnNativeDisplaysChanged(display_info_list2);
829   EXPECT_EQ(2, Shell::GetScreen()->GetNumDisplays());
830   EXPECT_EQ(primary_display.id(),
831             Shell::GetScreen()->GetPrimaryDisplay().id());
832   EXPECT_EQ(third_display_info.id(), ScreenAsh::GetSecondaryDisplay().id());
833   EXPECT_EQ(
834       primary_root,
835       display_controller->GetRootWindowForDisplayId(primary_display.id()));
836   EXPECT_NE(
837       primary_root,
838       display_controller->GetRootWindowForDisplayId(third_display_info.id()));
839   EXPECT_TRUE(primary_root->Contains(launcher_window));
840 }
841
842 TEST_F(DisplayControllerTest, CursorDeviceScaleFactorSwapPrimary) {
843   if (!SupportsMultipleDisplays())
844     return;
845
846   DisplayController* display_controller =
847       Shell::GetInstance()->display_controller();
848
849   UpdateDisplay("200x200,200x200*2");
850   gfx::Display primary_display = Shell::GetScreen()->GetPrimaryDisplay();
851   gfx::Display secondary_display = ScreenAsh::GetSecondaryDisplay();
852
853   aura::Window* primary_root =
854       display_controller->GetRootWindowForDisplayId(primary_display.id());
855   aura::Window* secondary_root =
856       display_controller->GetRootWindowForDisplayId(secondary_display.id());
857   EXPECT_NE(primary_root, secondary_root);
858
859   test::CursorManagerTestApi test_api(Shell::GetInstance()->cursor_manager());
860
861   EXPECT_EQ(1.0f, primary_root->GetDispatcher()->AsRootWindowHostDelegate()->
862       GetDeviceScaleFactor());
863   primary_root->MoveCursorTo(gfx::Point(50, 50));
864   EXPECT_EQ(1.0f, test_api.GetDisplay().device_scale_factor());
865   EXPECT_EQ(2.0f, secondary_root->GetDispatcher()->AsRootWindowHostDelegate()->
866       GetDeviceScaleFactor());
867   secondary_root->MoveCursorTo(gfx::Point(50, 50));
868   EXPECT_EQ(2.0f, test_api.GetDisplay().device_scale_factor());
869
870   // Switch primary and secondary
871   display_controller->SetPrimaryDisplay(secondary_display);
872
873   // Cursor's device scale factor should be updated accroding to the swap of
874   // primary and secondary.
875   EXPECT_EQ(1.0f, secondary_root->GetDispatcher()->AsRootWindowHostDelegate()->
876       GetDeviceScaleFactor());
877   secondary_root->MoveCursorTo(gfx::Point(50, 50));
878   EXPECT_EQ(1.0f, test_api.GetDisplay().device_scale_factor());
879   primary_root->MoveCursorTo(gfx::Point(50, 50));
880   EXPECT_EQ(2.0f, primary_root->GetDispatcher()->AsRootWindowHostDelegate()->
881       GetDeviceScaleFactor());
882   EXPECT_EQ(2.0f, test_api.GetDisplay().device_scale_factor());
883
884   // Deleting 2nd display.
885   UpdateDisplay("200x200");
886   RunAllPendingInMessageLoop();  // RootWindow is deleted in a posted task.
887
888   // Cursor's device scale factor should be updated even without moving cursor.
889   EXPECT_EQ(1.0f, test_api.GetDisplay().device_scale_factor());
890
891   primary_root->MoveCursorTo(gfx::Point(50, 50));
892   EXPECT_EQ(1.0f, primary_root->GetDispatcher()->AsRootWindowHostDelegate()->
893       GetDeviceScaleFactor());
894   EXPECT_EQ(1.0f, test_api.GetDisplay().device_scale_factor());
895 }
896
897 TEST_F(DisplayControllerTest, OverscanInsets) {
898   if (!SupportsMultipleDisplays())
899     return;
900
901   DisplayController* display_controller =
902       Shell::GetInstance()->display_controller();
903   TestEventHandler event_handler;
904   Shell::GetInstance()->AddPreTargetHandler(&event_handler);
905
906   UpdateDisplay("120x200,300x400*2");
907   gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
908   Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
909
910   display_controller->SetOverscanInsets(display1.id(),
911                                         gfx::Insets(10, 15, 20, 25));
912   EXPECT_EQ("0,0 80x170", root_windows[0]->bounds().ToString());
913   EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
914   EXPECT_EQ("80,0 150x200",
915             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
916
917   aura::test::EventGenerator generator(root_windows[0]);
918   generator.MoveMouseToInHost(20, 25);
919   EXPECT_EQ("5,15", event_handler.GetLocationAndReset());
920
921   display_controller->SetOverscanInsets(display1.id(), gfx::Insets());
922   EXPECT_EQ("0,0 120x200", root_windows[0]->bounds().ToString());
923   EXPECT_EQ("120,0 150x200",
924             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
925
926   generator.MoveMouseToInHost(30, 20);
927   EXPECT_EQ("30,20", event_handler.GetLocationAndReset());
928
929   // Make sure the root window transformer uses correct scale
930   // factor when swapping display. Test crbug.com/253690.
931   UpdateDisplay("400x300*2,600x400/o");
932   root_windows = Shell::GetAllRootWindows();
933   gfx::Point point;
934   Shell::GetAllRootWindows()[1]->GetRootTransform().TransformPoint(&point);
935   EXPECT_EQ("15,10", point.ToString());
936
937   display_controller->SwapPrimaryDisplay();
938   point.SetPoint(0, 0);
939   Shell::GetAllRootWindows()[1]->GetRootTransform().TransformPoint(&point);
940   EXPECT_EQ("15,10", point.ToString());
941
942   Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
943 }
944
945 TEST_F(DisplayControllerTest, Rotate) {
946   if (!SupportsMultipleDisplays())
947     return;
948
949   internal::DisplayManager* display_manager =
950       Shell::GetInstance()->display_manager();
951   TestEventHandler event_handler;
952   Shell::GetInstance()->AddPreTargetHandler(&event_handler);
953
954   UpdateDisplay("120x200,300x400*2");
955   gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
956   int64 display2_id = ScreenAsh::GetSecondaryDisplay().id();
957   Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
958   aura::test::EventGenerator generator1(root_windows[0]);
959
960   EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
961   EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
962   EXPECT_EQ("120,0 150x200",
963             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
964   generator1.MoveMouseToInHost(50, 40);
965   EXPECT_EQ("50,40", event_handler.GetLocationAndReset());
966   EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display1.id()));
967   EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display2_id));
968
969   display_manager->SetDisplayRotation(display1.id(),
970                                       gfx::Display::ROTATE_90);
971   EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
972   EXPECT_EQ("150x200", root_windows[1]->bounds().size().ToString());
973   EXPECT_EQ("200,0 150x200",
974             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
975   generator1.MoveMouseToInHost(50, 40);
976   EXPECT_EQ("40,69", event_handler.GetLocationAndReset());
977   EXPECT_EQ(gfx::Display::ROTATE_90, GetStoredRotation(display1.id()));
978   EXPECT_EQ(gfx::Display::ROTATE_0, GetStoredRotation(display2_id));
979
980   DisplayLayout display_layout(DisplayLayout::BOTTOM, 50);
981   display_manager->SetLayoutForCurrentDisplays(display_layout);
982   EXPECT_EQ("50,120 150x200",
983             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
984
985   display_manager->SetDisplayRotation(display2_id,
986                                       gfx::Display::ROTATE_270);
987   EXPECT_EQ("200x120", root_windows[0]->bounds().size().ToString());
988   EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
989   EXPECT_EQ("50,120 200x150",
990             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
991   EXPECT_EQ(gfx::Display::ROTATE_90, GetStoredRotation(display1.id()));
992   EXPECT_EQ(gfx::Display::ROTATE_270, GetStoredRotation(display2_id));
993
994 #if !defined(OS_WIN)
995   aura::test::EventGenerator generator2(root_windows[1]);
996   generator2.MoveMouseToInHost(50, 40);
997   EXPECT_EQ("179,25", event_handler.GetLocationAndReset());
998   display_manager->SetDisplayRotation(display1.id(),
999                                       gfx::Display::ROTATE_180);
1000
1001   EXPECT_EQ("120x200", root_windows[0]->bounds().size().ToString());
1002   EXPECT_EQ("200x150", root_windows[1]->bounds().size().ToString());
1003   // Dislay must share at least 100, so the x's offset becomes 20.
1004   EXPECT_EQ("20,200 200x150",
1005             ScreenAsh::GetSecondaryDisplay().bounds().ToString());
1006   EXPECT_EQ(gfx::Display::ROTATE_180, GetStoredRotation(display1.id()));
1007   EXPECT_EQ(gfx::Display::ROTATE_270, GetStoredRotation(display2_id));
1008
1009   generator1.MoveMouseToInHost(50, 40);
1010   EXPECT_EQ("69,159", event_handler.GetLocationAndReset());
1011 #endif
1012
1013   Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1014 }
1015
1016 TEST_F(DisplayControllerTest, ScaleRootWindow) {
1017   if (!SupportsMultipleDisplays())
1018     return;
1019
1020   TestEventHandler event_handler;
1021   Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1022
1023   UpdateDisplay("600x400*2@1.5,500x300");
1024
1025   gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
1026   gfx::Display::SetInternalDisplayId(display1.id());
1027
1028   gfx::Display display2 = ScreenAsh::GetSecondaryDisplay();
1029   Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
1030   EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
1031   EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
1032   EXPECT_EQ("450,0 500x300", display2.bounds().ToString());
1033   EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1034   EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
1035
1036   aura::test::EventGenerator generator(root_windows[0]);
1037   generator.MoveMouseToInHost(599, 200);
1038   EXPECT_EQ("449,150", event_handler.GetLocationAndReset());
1039
1040   internal::DisplayManager* display_manager =
1041       Shell::GetInstance()->display_manager();
1042   display_manager->SetDisplayUIScale(display1.id(), 1.25f);
1043   display1 = Shell::GetScreen()->GetPrimaryDisplay();
1044   display2 = ScreenAsh::GetSecondaryDisplay();
1045   EXPECT_EQ("0,0 375x250", display1.bounds().ToString());
1046   EXPECT_EQ("0,0 375x250", root_windows[0]->bounds().ToString());
1047   EXPECT_EQ("375,0 500x300", display2.bounds().ToString());
1048   EXPECT_EQ(1.25f, GetStoredUIScale(display1.id()));
1049   EXPECT_EQ(1.0f, GetStoredUIScale(display2.id()));
1050
1051   Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1052 }
1053
1054 TEST_F(DisplayControllerTest, TouchScale) {
1055   if (!SupportsMultipleDisplays())
1056     return;
1057
1058   TestEventHandler event_handler;
1059   Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1060
1061   UpdateDisplay("200x200*2");
1062   gfx::Display display = Shell::GetScreen()->GetPrimaryDisplay();
1063   Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
1064   aura::RootWindow* root_window = root_windows[0];
1065   aura::test::EventGenerator generator(root_window);
1066
1067   generator.PressMoveAndReleaseTouchTo(50, 50);
1068   // Default test touches have radius_x/y = 1.0, with device scale
1069   // factor = 2, the scaled radius_x/y should be 0.5.
1070   EXPECT_EQ(0.5, event_handler.touch_radius_x());
1071   EXPECT_EQ(0.5, event_handler.touch_radius_y());
1072
1073   generator.ScrollSequence(gfx::Point(0,0),
1074                            base::TimeDelta::FromMilliseconds(100),
1075                            10.0, 1.0, 5, 1);
1076
1077   // ordinal_offset is invariant to the device scale factor.
1078   EXPECT_EQ(event_handler.scroll_x_offset(),
1079             event_handler.scroll_x_offset_ordinal());
1080   EXPECT_EQ(event_handler.scroll_y_offset(),
1081             event_handler.scroll_y_offset_ordinal());
1082
1083   Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1084 }
1085
1086 TEST_F(DisplayControllerTest, ConvertHostToRootCoords) {
1087   if (!SupportsMultipleDisplays())
1088     return;
1089
1090   TestEventHandler event_handler;
1091   Shell::GetInstance()->AddPreTargetHandler(&event_handler);
1092
1093   UpdateDisplay("600x400*2/r@1.5");
1094
1095   gfx::Display display1 = Shell::GetScreen()->GetPrimaryDisplay();
1096   Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
1097   EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
1098   EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
1099   EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1100
1101   aura::test::EventGenerator generator(root_windows[0]);
1102   generator.MoveMouseToInHost(0, 0);
1103   EXPECT_EQ("0,449", event_handler.GetLocationAndReset());
1104   generator.MoveMouseToInHost(599, 0);
1105   EXPECT_EQ("0,0", event_handler.GetLocationAndReset());
1106   generator.MoveMouseToInHost(599, 399);
1107   EXPECT_EQ("299,0", event_handler.GetLocationAndReset());
1108   generator.MoveMouseToInHost(0, 399);
1109   EXPECT_EQ("299,449", event_handler.GetLocationAndReset());
1110
1111   UpdateDisplay("600x400*2/u@1.5");
1112   display1 = Shell::GetScreen()->GetPrimaryDisplay();
1113   root_windows = Shell::GetAllRootWindows();
1114   EXPECT_EQ("0,0 450x300", display1.bounds().ToString());
1115   EXPECT_EQ("0,0 450x300", root_windows[0]->bounds().ToString());
1116   EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1117
1118   generator.MoveMouseToInHost(0, 0);
1119   EXPECT_EQ("449,299", event_handler.GetLocationAndReset());
1120   generator.MoveMouseToInHost(599, 0);
1121   EXPECT_EQ("0,299", event_handler.GetLocationAndReset());
1122   generator.MoveMouseToInHost(599, 399);
1123   EXPECT_EQ("0,0", event_handler.GetLocationAndReset());
1124   generator.MoveMouseToInHost(0, 399);
1125   EXPECT_EQ("449,0", event_handler.GetLocationAndReset());
1126
1127   UpdateDisplay("600x400*2/l@1.5");
1128   display1 = Shell::GetScreen()->GetPrimaryDisplay();
1129   root_windows = Shell::GetAllRootWindows();
1130   EXPECT_EQ("0,0 300x450", display1.bounds().ToString());
1131   EXPECT_EQ("0,0 300x450", root_windows[0]->bounds().ToString());
1132   EXPECT_EQ(1.5f, GetStoredUIScale(display1.id()));
1133
1134   generator.MoveMouseToInHost(0, 0);
1135   EXPECT_EQ("299,0", event_handler.GetLocationAndReset());
1136   generator.MoveMouseToInHost(599, 0);
1137   EXPECT_EQ("299,449", event_handler.GetLocationAndReset());
1138   generator.MoveMouseToInHost(599, 399);
1139   EXPECT_EQ("0,449", event_handler.GetLocationAndReset());
1140   generator.MoveMouseToInHost(0, 399);
1141   EXPECT_EQ("0,0", event_handler.GetLocationAndReset());
1142
1143   Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
1144 }
1145
1146 #if defined(USE_X11)
1147 TEST_F(DisplayControllerTest, XWidowNameForRootWindow) {
1148   EXPECT_EQ("aura_root_0", GetXWindowName(
1149       Shell::GetPrimaryRootWindow()->GetDispatcher()));
1150
1151   // Multiple display.
1152   UpdateDisplay("200x200,300x300");
1153   aura::Window* primary, *secondary;
1154   GetPrimaryAndSeconary(&primary, &secondary);
1155   EXPECT_EQ("aura_root_0", GetXWindowName(primary->GetDispatcher()));
1156   EXPECT_EQ("aura_root_x", GetXWindowName(secondary->GetDispatcher()));
1157
1158   // Swap primary.
1159   primary = secondary = NULL;
1160   Shell::GetInstance()->display_controller()->SwapPrimaryDisplay();
1161   GetPrimaryAndSeconary(&primary, &secondary);
1162   EXPECT_EQ("aura_root_0", GetXWindowName(primary->GetDispatcher()));
1163   EXPECT_EQ("aura_root_x", GetXWindowName(secondary->GetDispatcher()));
1164
1165   // Switching back to single display.
1166   UpdateDisplay("300x400");
1167   EXPECT_EQ("aura_root_0", GetXWindowName(
1168       Shell::GetPrimaryRootWindow()->GetDispatcher()));
1169 }
1170 #endif
1171
1172 }  // namespace ash