Make DesktopResizer use ScreenResolution to facilitate DPI-awareness.
[chromium-blink-merge.git] / ash / extended_desktop_unittest.cc
blob689c546142808a9ae66581997624354108835843
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/display/display_controller.h"
6 #include "ash/display/display_manager.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/screen_ash.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/system/tray/system_tray.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/wm/coordinate_conversion.h"
14 #include "ash/wm/window_cycle_controller.h"
15 #include "ash/wm/window_properties.h"
16 #include "ash/wm/window_util.h"
17 #include "base/strings/string_util.h"
18 #include "ui/aura/client/activation_client.h"
19 #include "ui/aura/client/capture_client.h"
20 #include "ui/aura/client/focus_client.h"
21 #include "ui/aura/root_window.h"
22 #include "ui/aura/test/event_generator.h"
23 #include "ui/aura/test/test_windows.h"
24 #include "ui/aura/test/window_test_api.h"
25 #include "ui/aura/window.h"
26 #include "ui/base/cursor/cursor.h"
27 #include "ui/events/event_handler.h"
28 #include "ui/gfx/display.h"
29 #include "ui/gfx/screen.h"
30 #include "ui/views/controls/textfield/textfield.h"
31 #include "ui/views/widget/widget.h"
32 #include "ui/views/widget/widget_delegate.h"
34 namespace ash {
35 namespace {
37 void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
38 DisplayLayout layout =
39 Shell::GetInstance()->display_manager()->GetCurrentDisplayLayout();
40 layout.position = position;
41 Shell::GetInstance()->display_manager()->
42 SetLayoutForCurrentDisplays(layout);
45 internal::DisplayManager* GetDisplayManager() {
46 return Shell::GetInstance()->display_manager();
49 class ModalWidgetDelegate : public views::WidgetDelegateView {
50 public:
51 ModalWidgetDelegate() {}
52 virtual ~ModalWidgetDelegate() {}
54 // Overridden from views::WidgetDelegate:
55 virtual views::View* GetContentsView() OVERRIDE {
56 return this;
58 virtual ui::ModalType GetModalType() const OVERRIDE {
59 return ui::MODAL_TYPE_SYSTEM;
62 private:
63 DISALLOW_COPY_AND_ASSIGN(ModalWidgetDelegate);
66 // An event handler which moves the target window to the secondary root window
67 // at pre-handle phase of a mouse release event.
68 class MoveWindowByClickEventHandler : public ui::EventHandler {
69 public:
70 explicit MoveWindowByClickEventHandler(aura::Window* target)
71 : target_(target) {}
72 virtual ~MoveWindowByClickEventHandler() {}
74 private:
75 // ui::EventHandler overrides:
76 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
77 if (event->type() == ui::ET_MOUSE_RELEASED) {
78 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
79 DCHECK_LT(1u, root_windows.size());
80 root_windows[1]->AddChild(target_);
84 aura::Window* target_;
85 DISALLOW_COPY_AND_ASSIGN(MoveWindowByClickEventHandler);
88 // An event handler which records the event's locations.
89 class EventLocationRecordingEventHandler : public ui::EventHandler {
90 public:
91 explicit EventLocationRecordingEventHandler() {
92 reset();
94 virtual ~EventLocationRecordingEventHandler() {}
96 std::string GetLocationsAndReset() {
97 std::string result =
98 location_.ToString() + " " + root_location_.ToString();
99 reset();
100 return result;
103 private:
104 // ui::EventHandler overrides:
105 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
106 if (event->type() == ui::ET_MOUSE_MOVED ||
107 event->type() == ui::ET_MOUSE_DRAGGED) {
108 location_ = event->location();
109 root_location_ = event->root_location();
113 void reset() {
114 location_.SetPoint(-999, -999);
115 root_location_.SetPoint(-999, -999);
118 gfx::Point root_location_;
119 gfx::Point location_;
121 DISALLOW_COPY_AND_ASSIGN(EventLocationRecordingEventHandler);
124 } // namespace
126 class ExtendedDesktopTest : public test::AshTestBase {
127 public:
128 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
129 return CreateTestWidgetWithParentAndContext(
130 NULL, CurrentContext(), bounds, false);
133 views::Widget* CreateTestWidgetWithParent(views::Widget* parent,
134 const gfx::Rect& bounds,
135 bool child) {
136 CHECK(parent);
137 return CreateTestWidgetWithParentAndContext(parent, NULL, bounds, child);
140 views::Widget* CreateTestWidgetWithParentAndContext(views::Widget* parent,
141 gfx::NativeView context,
142 const gfx::Rect& bounds,
143 bool child) {
144 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
145 if (parent)
146 params.parent = parent->GetNativeView();
147 params.context = context;
148 params.bounds = bounds;
149 params.child = child;
150 views::Widget* widget = new views::Widget;
151 widget->Init(params);
152 widget->Show();
153 return widget;
157 // Test conditions that root windows in extended desktop mode
158 // must satisfy.
159 TEST_F(ExtendedDesktopTest, Basic) {
160 if (!SupportsMultipleDisplays())
161 return;
163 UpdateDisplay("1000x600,600x400");
164 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
166 // All root windows must have the root window controller.
167 ASSERT_EQ(2U, root_windows.size());
168 for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
169 iter != root_windows.end(); ++iter) {
170 EXPECT_TRUE(internal::GetRootWindowController(*iter) != NULL);
172 // Make sure root windows share the same controllers.
173 EXPECT_EQ(aura::client::GetFocusClient(root_windows[0]),
174 aura::client::GetFocusClient(root_windows[1]));
175 EXPECT_EQ(aura::client::GetActivationClient(root_windows[0]),
176 aura::client::GetActivationClient(root_windows[1]));
177 EXPECT_EQ(aura::client::GetCaptureClient(root_windows[0]),
178 aura::client::GetCaptureClient(root_windows[1]));
181 TEST_F(ExtendedDesktopTest, Activation) {
182 if (!SupportsMultipleDisplays())
183 return;
185 UpdateDisplay("1000x600,600x400");
186 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
188 views::Widget* widget_on_1st = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
189 views::Widget* widget_on_2nd =
190 CreateTestWidget(gfx::Rect(1200, 10, 100, 100));
191 EXPECT_EQ(root_windows[0], widget_on_1st->GetNativeView()->GetRootWindow());
192 EXPECT_EQ(root_windows[1], widget_on_2nd->GetNativeView()->GetRootWindow());
194 EXPECT_EQ(widget_on_2nd->GetNativeView(),
195 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
196 EXPECT_TRUE(wm::IsActiveWindow(widget_on_2nd->GetNativeView()));
198 aura::test::EventGenerator& event_generator(GetEventGenerator());
199 // Clicking a window changes the active window and active root window.
200 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
201 event_generator.ClickLeftButton();
203 EXPECT_EQ(widget_on_1st->GetNativeView(),
204 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
205 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
207 event_generator.MoveMouseToCenterOf(widget_on_2nd->GetNativeView());
208 event_generator.ClickLeftButton();
210 EXPECT_EQ(widget_on_2nd->GetNativeView(),
211 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
212 EXPECT_TRUE(wm::IsActiveWindow(widget_on_2nd->GetNativeView()));
215 TEST_F(ExtendedDesktopTest, SystemModal) {
216 if (!SupportsMultipleDisplays())
217 return;
219 UpdateDisplay("1000x600,600x400");
220 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
222 views::Widget* widget_on_1st = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
223 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
224 EXPECT_EQ(root_windows[0], widget_on_1st->GetNativeView()->GetRootWindow());
225 EXPECT_EQ(root_windows[0], Shell::GetTargetRootWindow());
227 // Open system modal. Make sure it's on 2nd root window and active.
228 views::Widget* modal_widget = views::Widget::CreateWindowWithContextAndBounds(
229 new ModalWidgetDelegate(),
230 CurrentContext(),
231 gfx::Rect(1200, 100, 100, 100));
232 modal_widget->Show();
233 EXPECT_TRUE(wm::IsActiveWindow(modal_widget->GetNativeView()));
234 EXPECT_EQ(root_windows[1], modal_widget->GetNativeView()->GetRootWindow());
235 EXPECT_EQ(root_windows[1], Shell::GetTargetRootWindow());
237 aura::test::EventGenerator& event_generator(GetEventGenerator());
239 // Clicking a widget on widget_on_1st display should not change activation.
240 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
241 event_generator.ClickLeftButton();
242 EXPECT_TRUE(wm::IsActiveWindow(modal_widget->GetNativeView()));
243 EXPECT_EQ(root_windows[1], Shell::GetTargetRootWindow());
245 // Close system modal and so clicking a widget should work now.
246 modal_widget->Close();
247 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
248 event_generator.ClickLeftButton();
249 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
250 EXPECT_EQ(root_windows[0], Shell::GetTargetRootWindow());
253 TEST_F(ExtendedDesktopTest, TestCursor) {
254 if (!SupportsMultipleDisplays())
255 return;
257 UpdateDisplay("1000x600,600x400");
258 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
259 EXPECT_EQ(ui::kCursorPointer, root_windows[0]->last_cursor().native_type());
260 EXPECT_EQ(ui::kCursorPointer, root_windows[1]->last_cursor().native_type());
261 Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorCopy);
262 EXPECT_EQ(ui::kCursorCopy, root_windows[0]->last_cursor().native_type());
263 EXPECT_EQ(ui::kCursorCopy, root_windows[1]->last_cursor().native_type());
266 TEST_F(ExtendedDesktopTest, TestCursorLocation) {
267 if (!SupportsMultipleDisplays())
268 return;
270 UpdateDisplay("1000x600,600x400");
271 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
272 aura::test::WindowTestApi root_window0_test_api(root_windows[0]);
273 aura::test::WindowTestApi root_window1_test_api(root_windows[1]);
275 root_windows[0]->MoveCursorTo(gfx::Point(10, 10));
276 EXPECT_EQ("10,10", Shell::GetScreen()->GetCursorScreenPoint().ToString());
277 EXPECT_TRUE(root_window0_test_api.ContainsMouse());
278 EXPECT_FALSE(root_window1_test_api.ContainsMouse());
279 root_windows[1]->MoveCursorTo(gfx::Point(10, 20));
280 EXPECT_EQ("1010,20", Shell::GetScreen()->GetCursorScreenPoint().ToString());
281 EXPECT_FALSE(root_window0_test_api.ContainsMouse());
282 EXPECT_TRUE(root_window1_test_api.ContainsMouse());
283 root_windows[0]->MoveCursorTo(gfx::Point(20, 10));
284 EXPECT_EQ("20,10", Shell::GetScreen()->GetCursorScreenPoint().ToString());
285 EXPECT_TRUE(root_window0_test_api.ContainsMouse());
286 EXPECT_FALSE(root_window1_test_api.ContainsMouse());
289 TEST_F(ExtendedDesktopTest, CycleWindows) {
290 if (!SupportsMultipleDisplays())
291 return;
293 UpdateDisplay("700x500,500x500");
294 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
296 WindowCycleController* controller =
297 Shell::GetInstance()->window_cycle_controller();
299 views::Widget* d1_w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
300 EXPECT_EQ(root_windows[0], d1_w1->GetNativeView()->GetRootWindow());
301 views::Widget* d2_w1 = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
302 EXPECT_EQ(root_windows[1], d2_w1->GetNativeView()->GetRootWindow());
303 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
305 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
306 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
307 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
308 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
309 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
310 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
311 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
312 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
314 // Cycle through all windows across root windows.
315 views::Widget* d1_w2 = CreateTestWidget(gfx::Rect(10, 200, 100, 100));
316 EXPECT_EQ(root_windows[0], d1_w2->GetNativeView()->GetRootWindow());
317 views::Widget* d2_w2 = CreateTestWidget(gfx::Rect(800, 200, 100, 100));
318 EXPECT_EQ(root_windows[1], d2_w2->GetNativeView()->GetRootWindow());
320 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
321 EXPECT_TRUE(wm::IsActiveWindow(d1_w2->GetNativeView()));
322 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
323 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
324 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
325 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
326 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
327 EXPECT_TRUE(wm::IsActiveWindow(d2_w2->GetNativeView()));
329 // Backwards
330 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
331 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
332 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
333 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
334 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
335 EXPECT_TRUE(wm::IsActiveWindow(d1_w2->GetNativeView()));
336 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
337 EXPECT_TRUE(wm::IsActiveWindow(d2_w2->GetNativeView()));
340 TEST_F(ExtendedDesktopTest, GetRootWindowAt) {
341 if (!SupportsMultipleDisplays())
342 return;
344 UpdateDisplay("700x500,500x500");
345 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
346 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
348 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-400, 100)));
349 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-1, 100)));
350 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(0, 300)));
351 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(700,300)));
353 // Zero origin.
354 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(0, 0)));
356 // Out of range point should return the primary root window
357 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(-600, 0)));
358 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(701, 100)));
361 TEST_F(ExtendedDesktopTest, GetRootWindowMatching) {
362 if (!SupportsMultipleDisplays())
363 return;
365 UpdateDisplay("700x500,500x500");
366 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
368 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
370 // Containing rect.
371 EXPECT_EQ(root_windows[1],
372 wm::GetRootWindowMatching(gfx::Rect(-300, 10, 50, 50)));
373 EXPECT_EQ(root_windows[0],
374 wm::GetRootWindowMatching(gfx::Rect(100, 10, 50, 50)));
376 // Intersecting rect.
377 EXPECT_EQ(root_windows[1],
378 wm::GetRootWindowMatching(gfx::Rect(-200, 0, 300, 300)));
379 EXPECT_EQ(root_windows[0],
380 wm::GetRootWindowMatching(gfx::Rect(-100, 0, 300, 300)));
382 // Zero origin.
383 EXPECT_EQ(root_windows[0],
384 wm::GetRootWindowMatching(gfx::Rect(0, 0, 0, 0)));
385 EXPECT_EQ(root_windows[0],
386 wm::GetRootWindowMatching(gfx::Rect(0, 0, 1, 1)));
388 // Empty rect.
389 EXPECT_EQ(root_windows[1],
390 wm::GetRootWindowMatching(gfx::Rect(-400, 100, 0, 0)));
391 EXPECT_EQ(root_windows[0],
392 wm::GetRootWindowMatching(gfx::Rect(100, 100, 0, 0)));
394 // Out of range rect should return the primary root window.
395 EXPECT_EQ(root_windows[0],
396 wm::GetRootWindowMatching(gfx::Rect(-600, -300, 50, 50)));
397 EXPECT_EQ(root_windows[0],
398 wm::GetRootWindowMatching(gfx::Rect(0, 1000, 50, 50)));
401 TEST_F(ExtendedDesktopTest, Capture) {
402 if (!SupportsMultipleDisplays())
403 return;
405 UpdateDisplay("1000x600,600x400");
406 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
408 aura::test::EventCountDelegate r1_d1;
409 aura::test::EventCountDelegate r1_d2;
410 aura::test::EventCountDelegate r2_d1;
412 scoped_ptr<aura::Window> r1_w1(aura::test::CreateTestWindowWithDelegate(
413 &r1_d1, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
414 scoped_ptr<aura::Window> r1_w2(aura::test::CreateTestWindowWithDelegate(
415 &r1_d2, 0, gfx::Rect(10, 100, 100, 100), root_windows[0]));
416 scoped_ptr<aura::Window> r2_w1(aura::test::CreateTestWindowWithDelegate(
417 &r2_d1, 0, gfx::Rect(10, 10, 100, 100), root_windows[1]));
419 r1_w1->SetCapture();
421 EXPECT_EQ(r1_w1.get(),
422 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
424 aura::test::EventGenerator generator2(root_windows[1]);
425 generator2.MoveMouseToCenterOf(r2_w1.get());
426 generator2.ClickLeftButton();
427 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
428 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
429 // The mouse is outside. On chromeos, the mouse is warped to the
430 // dest root window, but it's not implemented on Win yet, so
431 // no mouse move event on Win.
432 EXPECT_EQ("1 1 0", r1_d1.GetMouseMotionCountsAndReset());
433 EXPECT_EQ("1 1", r1_d1.GetMouseButtonCountsAndReset());
434 // Emulate passive grab. (15,15) on 1st display is (-985,15) on 2nd
435 // display.
436 generator2.MoveMouseTo(-985, 15);
437 EXPECT_EQ("0 1 0", r1_d1.GetMouseMotionCountsAndReset());
439 r1_w2->SetCapture();
440 EXPECT_EQ(r1_w2.get(),
441 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
442 generator2.MoveMouseBy(10, 10);
443 generator2.ClickLeftButton();
444 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
445 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
446 // mouse is already entered.
447 EXPECT_EQ("0 1 0", r1_d2.GetMouseMotionCountsAndReset());
448 EXPECT_EQ("1 1", r1_d2.GetMouseButtonCountsAndReset());
449 r1_w2->ReleaseCapture();
450 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
451 generator2.MoveMouseTo(15, 15);
452 generator2.ClickLeftButton();
453 EXPECT_EQ("1 1 0", r2_d1.GetMouseMotionCountsAndReset());
454 EXPECT_EQ("1 1", r2_d1.GetMouseButtonCountsAndReset());
455 // Make sure the mouse_moved_handler_ is properly reset.
456 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
457 EXPECT_EQ("0 0", r1_d2.GetMouseButtonCountsAndReset());
460 TEST_F(ExtendedDesktopTest, MoveWindow) {
461 if (!SupportsMultipleDisplays())
462 return;
464 UpdateDisplay("1000x600,600x400");
465 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
466 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
468 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
470 d1->SetBounds(gfx::Rect(1010, 10, 100, 100));
471 EXPECT_EQ("1010,10 100x100",
472 d1->GetWindowBoundsInScreen().ToString());
474 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
476 d1->SetBounds(gfx::Rect(10, 10, 100, 100));
477 EXPECT_EQ("10,10 100x100",
478 d1->GetWindowBoundsInScreen().ToString());
480 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
482 // Make sure the bounds which doesn't fit to the root window
483 // works correctly.
484 d1->SetBounds(gfx::Rect(1560, 30, 100, 100));
485 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
486 EXPECT_EQ("1560,30 100x100",
487 d1->GetWindowBoundsInScreen().ToString());
489 // Setting outside of root windows will be moved to primary root window.
490 // TODO(oshima): This one probably should pick the closest root window.
491 d1->SetBounds(gfx::Rect(200, 10, 100, 100));
492 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
495 // Verifies if the mouse event arrives to the window even when the window
496 // moves to another root in a pre-target handler. See: crbug.com/157583
497 TEST_F(ExtendedDesktopTest, MoveWindowByMouseClick) {
498 if (!SupportsMultipleDisplays())
499 return;
501 UpdateDisplay("1000x600,600x400");
503 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
504 aura::test::EventCountDelegate delegate;
505 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
506 &delegate, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
507 MoveWindowByClickEventHandler event_handler(window.get());
508 window->AddPreTargetHandler(&event_handler);
510 aura::test::EventGenerator& event_generator(GetEventGenerator());
512 event_generator.MoveMouseToCenterOf(window.get());
513 event_generator.ClickLeftButton();
514 // Both mouse pressed and released arrive at the window and its delegate.
515 EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
516 // Also event_handler moves the window to another root at mouse release.
517 EXPECT_EQ(root_windows[1], window->GetRootWindow());
520 TEST_F(ExtendedDesktopTest, MoveWindowToDisplay) {
521 if (!SupportsMultipleDisplays())
522 return;
524 UpdateDisplay("1000x1000,1000x1000");
525 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
527 gfx::Display display0 = Shell::GetScreen()->GetDisplayMatching(
528 root_windows[0]->GetBoundsInScreen());
529 gfx::Display display1 = Shell::GetScreen()->GetDisplayMatching(
530 root_windows[1]->GetBoundsInScreen());
531 EXPECT_NE(display0.id(), display1.id());
533 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 1000, 100));
534 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
536 // Move the window where the window spans both root windows. Since the second
537 // parameter is |display1|, the window should be shown on the secondary root.
538 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
539 display1);
540 EXPECT_EQ("500,10 1000x100",
541 d1->GetWindowBoundsInScreen().ToString());
542 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
544 // Move to the primary root.
545 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
546 display0);
547 EXPECT_EQ("500,10 1000x100",
548 d1->GetWindowBoundsInScreen().ToString());
549 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
552 TEST_F(ExtendedDesktopTest, MoveWindowWithTransient) {
553 if (!SupportsMultipleDisplays())
554 return;
556 UpdateDisplay("1000x600,600x400");
557 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
558 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
559 views::Widget* w1_t1 = CreateTestWidgetWithParent(
560 w1, gfx::Rect(50, 50, 50, 50), false /* transient */);
561 // Transient child of the transient child.
562 views::Widget* w1_t11 = CreateTestWidgetWithParent(
563 w1_t1, gfx::Rect(1200, 70, 30, 30), false /* transient */);
565 views::Widget* w11 = CreateTestWidgetWithParent(
566 w1, gfx::Rect(10, 10, 40, 40), true /* child */);
567 views::Widget* w11_t1 = CreateTestWidgetWithParent(
568 w1, gfx::Rect(1300, 100, 80, 80), false /* transient */);
570 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
571 EXPECT_EQ(root_windows[0], w11->GetNativeView()->GetRootWindow());
572 EXPECT_EQ(root_windows[0], w1_t1->GetNativeView()->GetRootWindow());
573 EXPECT_EQ(root_windows[0], w1_t11->GetNativeView()->GetRootWindow());
574 EXPECT_EQ(root_windows[0], w11_t1->GetNativeView()->GetRootWindow());
575 EXPECT_EQ("50,50 50x50",
576 w1_t1->GetWindowBoundsInScreen().ToString());
577 EXPECT_EQ("1200,70 30x30",
578 w1_t11->GetWindowBoundsInScreen().ToString());
579 EXPECT_EQ("20,20 40x40",
580 w11->GetWindowBoundsInScreen().ToString());
581 EXPECT_EQ("1300,100 80x80",
582 w11_t1->GetWindowBoundsInScreen().ToString());
584 w1->SetBounds(gfx::Rect(1100,10,100,100));
586 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
587 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
588 EXPECT_EQ(root_windows[1], w1_t11->GetNativeView()->GetRootWindow());
589 EXPECT_EQ(root_windows[1], w11->GetNativeView()->GetRootWindow());
590 EXPECT_EQ(root_windows[1], w11_t1->GetNativeView()->GetRootWindow());
592 EXPECT_EQ("1110,20 40x40",
593 w11->GetWindowBoundsInScreen().ToString());
594 // Transient window's screen bounds stays the same.
595 EXPECT_EQ("50,50 50x50",
596 w1_t1->GetWindowBoundsInScreen().ToString());
597 EXPECT_EQ("1200,70 30x30",
598 w1_t11->GetWindowBoundsInScreen().ToString());
599 EXPECT_EQ("1300,100 80x80",
600 w11_t1->GetWindowBoundsInScreen().ToString());
602 // Transient window doesn't move between root window unless
603 // its transient parent moves.
604 w1_t1->SetBounds(gfx::Rect(10, 50, 50, 50));
605 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
606 EXPECT_EQ("10,50 50x50",
607 w1_t1->GetWindowBoundsInScreen().ToString());
610 // Test if the Window::ConvertPointToTarget works across root windows.
611 // TODO(oshima): Move multiple display suport and this test to aura.
612 TEST_F(ExtendedDesktopTest, ConvertPoint) {
613 if (!SupportsMultipleDisplays())
614 return;
615 gfx::Screen* screen = Shell::GetInstance()->screen();
616 UpdateDisplay("1000x600,600x400");
617 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
618 gfx::Display display_1 = screen->GetDisplayNearestWindow(root_windows[0]);
619 EXPECT_EQ("0,0", display_1.bounds().origin().ToString());
620 gfx::Display display_2 = screen->GetDisplayNearestWindow(root_windows[1]);
621 EXPECT_EQ("1000,0", display_2.bounds().origin().ToString());
623 aura::Window* d1 =
624 CreateTestWidget(gfx::Rect(10, 10, 100, 100))->GetNativeView();
625 aura::Window* d2 =
626 CreateTestWidget(gfx::Rect(1020, 20, 100, 100))->GetNativeView();
627 EXPECT_EQ(root_windows[0], d1->GetRootWindow());
628 EXPECT_EQ(root_windows[1], d2->GetRootWindow());
630 // Convert point in the Root2's window to the Root1's window Coord.
631 gfx::Point p(0, 0);
632 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
633 EXPECT_EQ("1000,0", p.ToString());
634 p.SetPoint(0, 0);
635 aura::Window::ConvertPointToTarget(d2, d1, &p);
636 EXPECT_EQ("1010,10", p.ToString());
638 // Convert point in the Root1's window to the Root2's window Coord.
639 p.SetPoint(0, 0);
640 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
641 EXPECT_EQ("-1000,0", p.ToString());
642 p.SetPoint(0, 0);
643 aura::Window::ConvertPointToTarget(d1, d2, &p);
644 EXPECT_EQ("-1010,-10", p.ToString());
646 // Move the 2nd display to the bottom and test again.
647 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
649 display_2 = screen->GetDisplayNearestWindow(root_windows[1]);
650 EXPECT_EQ("0,600", display_2.bounds().origin().ToString());
652 // Convert point in Root2's window to Root1's window Coord.
653 p.SetPoint(0, 0);
654 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
655 EXPECT_EQ("0,600", p.ToString());
656 p.SetPoint(0, 0);
657 aura::Window::ConvertPointToTarget(d2, d1, &p);
658 EXPECT_EQ("10,610", p.ToString());
660 // Convert point in Root1's window to Root2's window Coord.
661 p.SetPoint(0, 0);
662 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
663 EXPECT_EQ("0,-600", p.ToString());
664 p.SetPoint(0, 0);
665 aura::Window::ConvertPointToTarget(d1, d2, &p);
666 EXPECT_EQ("-10,-610", p.ToString());
669 TEST_F(ExtendedDesktopTest, OpenSystemTray) {
670 if (!SupportsMultipleDisplays())
671 return;
673 UpdateDisplay("500x600,600x400");
674 SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
675 ASSERT_FALSE(tray->HasSystemBubble());
677 aura::test::EventGenerator& event_generator(GetEventGenerator());
679 // Opens the tray by a dummy click event and makes sure that adding/removing
680 // displays doesn't break anything.
681 event_generator.MoveMouseToCenterOf(tray->GetWidget()->GetNativeWindow());
682 event_generator.ClickLeftButton();
683 EXPECT_TRUE(tray->HasSystemBubble());
685 UpdateDisplay("500x600");
686 EXPECT_TRUE(tray->HasSystemBubble());
687 UpdateDisplay("500x600,600x400");
688 EXPECT_TRUE(tray->HasSystemBubble());
690 // Closes the tray and again makes sure that adding/removing displays doesn't
691 // break anything.
692 event_generator.ClickLeftButton();
693 RunAllPendingInMessageLoop();
695 EXPECT_FALSE(tray->HasSystemBubble());
697 UpdateDisplay("500x600");
698 EXPECT_FALSE(tray->HasSystemBubble());
699 UpdateDisplay("500x600,600x400");
700 EXPECT_FALSE(tray->HasSystemBubble());
703 TEST_F(ExtendedDesktopTest, StayInSameRootWindow) {
704 if (!SupportsMultipleDisplays())
705 return;
707 UpdateDisplay("100x100,200x200");
708 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
709 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 50, 50));
710 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
711 w1->SetBounds(gfx::Rect(150, 10, 50, 50));
712 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
714 // The widget stays in the same root if kStayInSameRootWindowKey is set to
715 // true.
716 w1->GetNativeView()->SetProperty(internal::kStayInSameRootWindowKey, true);
717 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
718 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
720 // The widget should now move to the 1st root window without the property.
721 w1->GetNativeView()->ClearProperty(internal::kStayInSameRootWindowKey);
722 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
723 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
725 // a window in SettingsBubbleContainer and StatusContainer should
726 // not move to another root window regardles of the bounds specified.
727 aura::Window* settings_bubble_container =
728 Shell::GetPrimaryRootWindowController()->GetContainer(
729 internal::kShellWindowId_SettingBubbleContainer);
730 aura::Window* window = aura::test::CreateTestWindowWithId(
731 100, settings_bubble_container);
732 window->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
733 ScreenAsh::GetSecondaryDisplay());
734 EXPECT_EQ(root_windows[0], window->GetRootWindow());
736 aura::Window* status_container =
737 Shell::GetPrimaryRootWindowController()->GetContainer(
738 internal::kShellWindowId_StatusContainer);
739 window = aura::test::CreateTestWindowWithId(100, status_container);
740 window->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
741 ScreenAsh::GetSecondaryDisplay());
742 EXPECT_EQ(root_windows[0], window->GetRootWindow());
745 TEST_F(ExtendedDesktopTest, KeyEventsOnLockScreen) {
746 if (!SupportsMultipleDisplays())
747 return;
749 UpdateDisplay("100x100,200x200");
750 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
752 // Create normal windows on both displays.
753 views::Widget* widget1 = CreateTestWidget(
754 Shell::GetScreen()->GetPrimaryDisplay().bounds());
755 widget1->Show();
756 EXPECT_EQ(root_windows[0], widget1->GetNativeView()->GetRootWindow());
757 views::Widget* widget2 = CreateTestWidget(
758 ScreenAsh::GetSecondaryDisplay().bounds());
759 widget2->Show();
760 EXPECT_EQ(root_windows[1], widget2->GetNativeView()->GetRootWindow());
762 // Create a LockScreen window.
763 views::Widget* lock_widget = CreateTestWidget(
764 Shell::GetScreen()->GetPrimaryDisplay().bounds());
765 views::Textfield* textfield = new views::Textfield;
766 lock_widget->client_view()->AddChildView(textfield);
768 ash::Shell::GetContainer(
769 Shell::GetPrimaryRootWindow(),
770 ash::internal::kShellWindowId_LockScreenContainer)->
771 AddChild(lock_widget->GetNativeView());
772 lock_widget->Show();
773 textfield->RequestFocus();
775 aura::client::FocusClient* focus_client =
776 aura::client::GetFocusClient(root_windows[0]);
777 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
779 // The lock window should get events on both root windows.
780 aura::test::EventGenerator& event_generator(GetEventGenerator());
782 event_generator.set_current_root_window(root_windows[0]);
783 event_generator.PressKey(ui::VKEY_A, 0);
784 event_generator.ReleaseKey(ui::VKEY_A, 0);
785 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
786 EXPECT_EQ("a", UTF16ToASCII(textfield->text()));
788 event_generator.set_current_root_window(root_windows[1]);
789 event_generator.PressKey(ui::VKEY_B, 0);
790 event_generator.ReleaseKey(ui::VKEY_B, 0);
791 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
792 EXPECT_EQ("ab", UTF16ToASCII(textfield->text()));
794 // Deleting 2nd display. The lock window still should get the events.
795 UpdateDisplay("100x100");
796 event_generator.PressKey(ui::VKEY_C, 0);
797 event_generator.ReleaseKey(ui::VKEY_C, 0);
798 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
799 EXPECT_EQ("abc", UTF16ToASCII(textfield->text()));
801 // Creating 2nd display again, and lock window still should get events
802 // on both root windows.
803 UpdateDisplay("100x100,200x200");
804 root_windows = Shell::GetAllRootWindows();
805 event_generator.set_current_root_window(root_windows[0]);
806 event_generator.PressKey(ui::VKEY_D, 0);
807 event_generator.ReleaseKey(ui::VKEY_D, 0);
808 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
809 EXPECT_EQ("abcd", UTF16ToASCII(textfield->text()));
811 event_generator.set_current_root_window(root_windows[1]);
812 event_generator.PressKey(ui::VKEY_E, 0);
813 event_generator.ReleaseKey(ui::VKEY_E, 0);
814 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
815 EXPECT_EQ("abcde", UTF16ToASCII(textfield->text()));
818 TEST_F(ExtendedDesktopTest, PassiveGrab) {
819 if (!SupportsMultipleDisplays())
820 return;
822 EventLocationRecordingEventHandler event_handler;
823 ash::Shell::GetInstance()->AddPreTargetHandler(&event_handler);
825 UpdateDisplay("300x300,200x200");
827 views::Widget* widget = CreateTestWidget(gfx::Rect(50, 50, 200, 200));
828 widget->Show();
829 ASSERT_EQ("50,50 200x200", widget->GetWindowBoundsInScreen().ToString());
831 aura::test::EventGenerator& generator(GetEventGenerator());
832 generator.MoveMouseTo(150, 150);
833 EXPECT_EQ("100,100 150,150", event_handler.GetLocationsAndReset());
835 generator.PressLeftButton();
836 generator.MoveMouseTo(400, 150);
838 EXPECT_EQ("350,100 400,150", event_handler.GetLocationsAndReset());
840 generator.ReleaseLeftButton();
841 EXPECT_EQ("-999,-999 -999,-999", event_handler.GetLocationsAndReset());
843 generator.MoveMouseTo(400, 150);
844 EXPECT_EQ("100,150 100,150", event_handler.GetLocationsAndReset());
846 ash::Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
849 } // namespace ash