roll skia DEPS to 13693
[chromium-blink-merge.git] / ash / extended_desktop_unittest.cc
blob600cdef9b335bb9e3b2dd0e2b129b9f3d8369e99
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_util.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/test/event_generator.h"
22 #include "ui/aura/test/test_windows.h"
23 #include "ui/aura/test/window_test_api.h"
24 #include "ui/aura/window.h"
25 #include "ui/aura/window_event_dispatcher.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 class ModalWidgetDelegate : public views::WidgetDelegateView {
46 public:
47 ModalWidgetDelegate() {}
48 virtual ~ModalWidgetDelegate() {}
50 // Overridden from views::WidgetDelegate:
51 virtual views::View* GetContentsView() OVERRIDE {
52 return this;
54 virtual ui::ModalType GetModalType() const OVERRIDE {
55 return ui::MODAL_TYPE_SYSTEM;
58 private:
59 DISALLOW_COPY_AND_ASSIGN(ModalWidgetDelegate);
62 // An event handler which moves the target window to the secondary root window
63 // at pre-handle phase of a mouse release event.
64 class MoveWindowByClickEventHandler : public ui::EventHandler {
65 public:
66 explicit MoveWindowByClickEventHandler(aura::Window* target)
67 : target_(target) {}
68 virtual ~MoveWindowByClickEventHandler() {}
70 private:
71 // ui::EventHandler overrides:
72 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
73 if (event->type() == ui::ET_MOUSE_RELEASED) {
74 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
75 DCHECK_LT(1u, root_windows.size());
76 root_windows[1]->AddChild(target_);
80 aura::Window* target_;
81 DISALLOW_COPY_AND_ASSIGN(MoveWindowByClickEventHandler);
84 // An event handler which records the event's locations.
85 class EventLocationRecordingEventHandler : public ui::EventHandler {
86 public:
87 explicit EventLocationRecordingEventHandler() {
88 reset();
90 virtual ~EventLocationRecordingEventHandler() {}
92 std::string GetLocationsAndReset() {
93 std::string result =
94 location_.ToString() + " " + root_location_.ToString();
95 reset();
96 return result;
99 private:
100 // ui::EventHandler overrides:
101 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
102 if (event->type() == ui::ET_MOUSE_MOVED ||
103 event->type() == ui::ET_MOUSE_DRAGGED) {
104 location_ = event->location();
105 root_location_ = event->root_location();
109 void reset() {
110 location_.SetPoint(-999, -999);
111 root_location_.SetPoint(-999, -999);
114 gfx::Point root_location_;
115 gfx::Point location_;
117 DISALLOW_COPY_AND_ASSIGN(EventLocationRecordingEventHandler);
120 } // namespace
122 class ExtendedDesktopTest : public test::AshTestBase {
123 public:
124 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
125 return CreateTestWidgetWithParentAndContext(
126 NULL, CurrentContext(), bounds, false);
129 views::Widget* CreateTestWidgetWithParent(views::Widget* parent,
130 const gfx::Rect& bounds,
131 bool child) {
132 CHECK(parent);
133 return CreateTestWidgetWithParentAndContext(parent, NULL, bounds, child);
136 views::Widget* CreateTestWidgetWithParentAndContext(views::Widget* parent,
137 gfx::NativeView context,
138 const gfx::Rect& bounds,
139 bool child) {
140 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
141 if (parent)
142 params.parent = parent->GetNativeView();
143 params.context = context;
144 params.bounds = bounds;
145 params.child = child;
146 views::Widget* widget = new views::Widget;
147 widget->Init(params);
148 widget->Show();
149 return widget;
153 // Test conditions that root windows in extended desktop mode
154 // must satisfy.
155 TEST_F(ExtendedDesktopTest, Basic) {
156 if (!SupportsMultipleDisplays())
157 return;
159 UpdateDisplay("1000x600,600x400");
160 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
162 // All root windows must have the root window controller.
163 ASSERT_EQ(2U, root_windows.size());
164 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
165 iter != root_windows.end(); ++iter) {
166 EXPECT_TRUE(internal::GetRootWindowController(*iter) != NULL);
168 // Make sure root windows share the same controllers.
169 EXPECT_EQ(aura::client::GetFocusClient(root_windows[0]),
170 aura::client::GetFocusClient(root_windows[1]));
171 EXPECT_EQ(aura::client::GetActivationClient(root_windows[0]),
172 aura::client::GetActivationClient(root_windows[1]));
173 EXPECT_EQ(aura::client::GetCaptureClient(root_windows[0]),
174 aura::client::GetCaptureClient(root_windows[1]));
177 TEST_F(ExtendedDesktopTest, Activation) {
178 if (!SupportsMultipleDisplays())
179 return;
181 UpdateDisplay("1000x600,600x400");
182 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
184 views::Widget* widget_on_1st = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
185 views::Widget* widget_on_2nd =
186 CreateTestWidget(gfx::Rect(1200, 10, 100, 100));
187 EXPECT_EQ(root_windows[0], widget_on_1st->GetNativeView()->GetRootWindow());
188 EXPECT_EQ(root_windows[1], widget_on_2nd->GetNativeView()->GetRootWindow());
190 EXPECT_EQ(widget_on_2nd->GetNativeView(),
191 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
192 EXPECT_TRUE(wm::IsActiveWindow(widget_on_2nd->GetNativeView()));
194 aura::test::EventGenerator& event_generator(GetEventGenerator());
195 // Clicking a window changes the active window and active root window.
196 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
197 event_generator.ClickLeftButton();
199 EXPECT_EQ(widget_on_1st->GetNativeView(),
200 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
201 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
203 event_generator.MoveMouseToCenterOf(widget_on_2nd->GetNativeView());
204 event_generator.ClickLeftButton();
206 EXPECT_EQ(widget_on_2nd->GetNativeView(),
207 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
208 EXPECT_TRUE(wm::IsActiveWindow(widget_on_2nd->GetNativeView()));
211 TEST_F(ExtendedDesktopTest, SystemModal) {
212 if (!SupportsMultipleDisplays())
213 return;
215 UpdateDisplay("1000x600,600x400");
216 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
218 views::Widget* widget_on_1st = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
219 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
220 EXPECT_EQ(root_windows[0], widget_on_1st->GetNativeView()->GetRootWindow());
221 EXPECT_EQ(root_windows[0], Shell::GetTargetRootWindow());
223 // Open system modal. Make sure it's on 2nd root window and active.
224 views::Widget* modal_widget = views::Widget::CreateWindowWithContextAndBounds(
225 new ModalWidgetDelegate(),
226 CurrentContext(),
227 gfx::Rect(1200, 100, 100, 100));
228 modal_widget->Show();
229 EXPECT_TRUE(wm::IsActiveWindow(modal_widget->GetNativeView()));
230 EXPECT_EQ(root_windows[1], modal_widget->GetNativeView()->GetRootWindow());
231 EXPECT_EQ(root_windows[1], Shell::GetTargetRootWindow());
233 aura::test::EventGenerator& event_generator(GetEventGenerator());
235 // Clicking a widget on widget_on_1st display should not change activation.
236 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
237 event_generator.ClickLeftButton();
238 EXPECT_TRUE(wm::IsActiveWindow(modal_widget->GetNativeView()));
239 EXPECT_EQ(root_windows[1], Shell::GetTargetRootWindow());
241 // Close system modal and so clicking a widget should work now.
242 modal_widget->Close();
243 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
244 event_generator.ClickLeftButton();
245 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
246 EXPECT_EQ(root_windows[0], Shell::GetTargetRootWindow());
249 TEST_F(ExtendedDesktopTest, TestCursor) {
250 if (!SupportsMultipleDisplays())
251 return;
253 UpdateDisplay("1000x600,600x400");
254 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
255 aura::WindowTreeHost* host0 = root_windows[0]->GetHost();
256 aura::WindowTreeHost* host1 = root_windows[1]->GetHost();
257 EXPECT_EQ(ui::kCursorPointer, host0->last_cursor().native_type());
258 EXPECT_EQ(ui::kCursorPointer, host1->last_cursor().native_type());
259 Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorCopy);
260 EXPECT_EQ(ui::kCursorCopy, host0->last_cursor().native_type());
261 EXPECT_EQ(ui::kCursorCopy, host1->last_cursor().native_type());
264 TEST_F(ExtendedDesktopTest, TestCursorLocation) {
265 if (!SupportsMultipleDisplays())
266 return;
268 UpdateDisplay("1000x600,600x400");
269 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
270 aura::test::WindowTestApi root_window0_test_api(root_windows[0]);
271 aura::test::WindowTestApi root_window1_test_api(root_windows[1]);
273 root_windows[0]->MoveCursorTo(gfx::Point(10, 10));
274 EXPECT_EQ("10,10", Shell::GetScreen()->GetCursorScreenPoint().ToString());
275 EXPECT_TRUE(root_window0_test_api.ContainsMouse());
276 EXPECT_FALSE(root_window1_test_api.ContainsMouse());
277 root_windows[1]->MoveCursorTo(gfx::Point(10, 20));
278 EXPECT_EQ("1010,20", Shell::GetScreen()->GetCursorScreenPoint().ToString());
279 EXPECT_FALSE(root_window0_test_api.ContainsMouse());
280 EXPECT_TRUE(root_window1_test_api.ContainsMouse());
281 root_windows[0]->MoveCursorTo(gfx::Point(20, 10));
282 EXPECT_EQ("20,10", Shell::GetScreen()->GetCursorScreenPoint().ToString());
283 EXPECT_TRUE(root_window0_test_api.ContainsMouse());
284 EXPECT_FALSE(root_window1_test_api.ContainsMouse());
287 TEST_F(ExtendedDesktopTest, CycleWindows) {
288 if (!SupportsMultipleDisplays())
289 return;
291 UpdateDisplay("700x500,500x500");
292 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
294 WindowCycleController* controller =
295 Shell::GetInstance()->window_cycle_controller();
297 views::Widget* d1_w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
298 EXPECT_EQ(root_windows[0], d1_w1->GetNativeView()->GetRootWindow());
299 views::Widget* d2_w1 = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
300 EXPECT_EQ(root_windows[1], d2_w1->GetNativeView()->GetRootWindow());
301 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
303 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
304 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
305 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
306 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
307 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
308 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
309 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
310 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
312 // Cycle through all windows across root windows.
313 views::Widget* d1_w2 = CreateTestWidget(gfx::Rect(10, 200, 100, 100));
314 EXPECT_EQ(root_windows[0], d1_w2->GetNativeView()->GetRootWindow());
315 views::Widget* d2_w2 = CreateTestWidget(gfx::Rect(800, 200, 100, 100));
316 EXPECT_EQ(root_windows[1], d2_w2->GetNativeView()->GetRootWindow());
318 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
319 EXPECT_TRUE(wm::IsActiveWindow(d1_w2->GetNativeView()));
320 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
321 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
322 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
323 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
324 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
325 EXPECT_TRUE(wm::IsActiveWindow(d2_w2->GetNativeView()));
327 // Backwards
328 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
329 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
330 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
331 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
332 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
333 EXPECT_TRUE(wm::IsActiveWindow(d1_w2->GetNativeView()));
334 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
335 EXPECT_TRUE(wm::IsActiveWindow(d2_w2->GetNativeView()));
338 TEST_F(ExtendedDesktopTest, GetRootWindowAt) {
339 if (!SupportsMultipleDisplays())
340 return;
342 UpdateDisplay("700x500,500x500");
343 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
344 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
346 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-400, 100)));
347 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-1, 100)));
348 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(0, 300)));
349 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(700,300)));
351 // Zero origin.
352 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(0, 0)));
354 // Out of range point should return the nearest root window
355 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-600, 0)));
356 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(701, 100)));
359 TEST_F(ExtendedDesktopTest, GetRootWindowMatching) {
360 if (!SupportsMultipleDisplays())
361 return;
363 UpdateDisplay("700x500,500x500");
364 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
366 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
368 // Containing rect.
369 EXPECT_EQ(root_windows[1],
370 wm::GetRootWindowMatching(gfx::Rect(-300, 10, 50, 50)));
371 EXPECT_EQ(root_windows[0],
372 wm::GetRootWindowMatching(gfx::Rect(100, 10, 50, 50)));
374 // Intersecting rect.
375 EXPECT_EQ(root_windows[1],
376 wm::GetRootWindowMatching(gfx::Rect(-200, 0, 300, 300)));
377 EXPECT_EQ(root_windows[0],
378 wm::GetRootWindowMatching(gfx::Rect(-100, 0, 300, 300)));
380 // Zero origin.
381 EXPECT_EQ(root_windows[0],
382 wm::GetRootWindowMatching(gfx::Rect(0, 0, 0, 0)));
383 EXPECT_EQ(root_windows[0],
384 wm::GetRootWindowMatching(gfx::Rect(0, 0, 1, 1)));
386 // Empty rect.
387 EXPECT_EQ(root_windows[1],
388 wm::GetRootWindowMatching(gfx::Rect(-400, 100, 0, 0)));
389 EXPECT_EQ(root_windows[0],
390 wm::GetRootWindowMatching(gfx::Rect(100, 100, 0, 0)));
392 // Out of range rect should return the primary root window.
393 EXPECT_EQ(root_windows[0],
394 wm::GetRootWindowMatching(gfx::Rect(-600, -300, 50, 50)));
395 EXPECT_EQ(root_windows[0],
396 wm::GetRootWindowMatching(gfx::Rect(0, 1000, 50, 50)));
399 TEST_F(ExtendedDesktopTest, Capture) {
400 if (!SupportsMultipleDisplays())
401 return;
403 UpdateDisplay("1000x600,600x400");
404 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
406 aura::test::EventCountDelegate r1_d1;
407 aura::test::EventCountDelegate r1_d2;
408 aura::test::EventCountDelegate r2_d1;
410 scoped_ptr<aura::Window> r1_w1(aura::test::CreateTestWindowWithDelegate(
411 &r1_d1, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
412 scoped_ptr<aura::Window> r1_w2(aura::test::CreateTestWindowWithDelegate(
413 &r1_d2, 0, gfx::Rect(10, 100, 100, 100), root_windows[0]));
414 scoped_ptr<aura::Window> r2_w1(aura::test::CreateTestWindowWithDelegate(
415 &r2_d1, 0, gfx::Rect(10, 10, 100, 100), root_windows[1]));
417 r1_w1->SetCapture();
419 EXPECT_EQ(r1_w1.get(),
420 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
422 aura::test::EventGenerator& generator = GetEventGenerator();
423 generator.MoveMouseToCenterOf(r2_w1.get());
424 // |r1_w1| will receive the events because it has capture.
425 EXPECT_EQ("1 1 0", r1_d1.GetMouseMotionCountsAndReset());
426 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
427 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
429 generator.ClickLeftButton();
430 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
431 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
432 // The mouse is outside. On chromeos, the mouse is warped to the
433 // dest root window, but it's not implemented on Win yet, so
434 // no mouse move event on Win.
435 EXPECT_EQ("0 0 0", r1_d1.GetMouseMotionCountsAndReset());
436 EXPECT_EQ("1 1", r1_d1.GetMouseButtonCountsAndReset());
438 generator.MoveMouseTo(15, 15);
439 EXPECT_EQ("0 1 0", r1_d1.GetMouseMotionCountsAndReset());
440 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
442 r1_w2->SetCapture();
443 EXPECT_EQ(r1_w2.get(),
444 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
445 generator.MoveMouseBy(10, 10);
446 // |r1_w2| has the capture. So it will receive the mouse-move event.
447 EXPECT_EQ("0 0 0", r1_d1.GetMouseMotionCountsAndReset());
448 EXPECT_EQ("0 1 0", r1_d2.GetMouseMotionCountsAndReset());
449 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
451 generator.ClickLeftButton();
452 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
453 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
454 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
455 EXPECT_EQ("1 1", r1_d2.GetMouseButtonCountsAndReset());
457 r1_w2->ReleaseCapture();
458 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
460 generator.MoveMouseToCenterOf(r2_w1.get());
461 generator.ClickLeftButton();
462 EXPECT_EQ("1 1 0", r2_d1.GetMouseMotionCountsAndReset());
463 EXPECT_EQ("1 1", r2_d1.GetMouseButtonCountsAndReset());
464 // Make sure the mouse_moved_handler_ is properly reset.
465 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
466 EXPECT_EQ("0 0", r1_d2.GetMouseButtonCountsAndReset());
469 TEST_F(ExtendedDesktopTest, MoveWindow) {
470 if (!SupportsMultipleDisplays())
471 return;
473 UpdateDisplay("1000x600,600x400");
474 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
475 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
477 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
479 d1->SetBounds(gfx::Rect(1010, 10, 100, 100));
480 EXPECT_EQ("1010,10 100x100",
481 d1->GetWindowBoundsInScreen().ToString());
483 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
485 d1->SetBounds(gfx::Rect(10, 10, 100, 100));
486 EXPECT_EQ("10,10 100x100",
487 d1->GetWindowBoundsInScreen().ToString());
489 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
491 // Make sure the bounds which doesn't fit to the root window
492 // works correctly.
493 d1->SetBounds(gfx::Rect(1560, 30, 100, 100));
494 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
495 EXPECT_EQ("1560,30 100x100",
496 d1->GetWindowBoundsInScreen().ToString());
498 // Setting outside of root windows will be moved to primary root window.
499 // TODO(oshima): This one probably should pick the closest root window.
500 d1->SetBounds(gfx::Rect(200, 10, 100, 100));
501 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
504 // Verifies if the mouse event arrives to the window even when the window
505 // moves to another root in a pre-target handler. See: crbug.com/157583
506 TEST_F(ExtendedDesktopTest, MoveWindowByMouseClick) {
507 if (!SupportsMultipleDisplays())
508 return;
510 UpdateDisplay("1000x600,600x400");
512 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
513 aura::test::EventCountDelegate delegate;
514 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
515 &delegate, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
516 MoveWindowByClickEventHandler event_handler(window.get());
517 window->AddPreTargetHandler(&event_handler);
519 aura::test::EventGenerator& event_generator(GetEventGenerator());
521 event_generator.MoveMouseToCenterOf(window.get());
522 event_generator.ClickLeftButton();
523 // Both mouse pressed and released arrive at the window and its delegate.
524 EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
525 // Also event_handler moves the window to another root at mouse release.
526 EXPECT_EQ(root_windows[1], window->GetRootWindow());
529 TEST_F(ExtendedDesktopTest, MoveWindowToDisplay) {
530 if (!SupportsMultipleDisplays())
531 return;
533 UpdateDisplay("1000x1000,1000x1000");
534 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
536 gfx::Display display0 = Shell::GetScreen()->GetDisplayMatching(
537 root_windows[0]->GetBoundsInScreen());
538 gfx::Display display1 = Shell::GetScreen()->GetDisplayMatching(
539 root_windows[1]->GetBoundsInScreen());
540 EXPECT_NE(display0.id(), display1.id());
542 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 1000, 100));
543 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
545 // Move the window where the window spans both root windows. Since the second
546 // parameter is |display1|, the window should be shown on the secondary root.
547 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
548 display1);
549 EXPECT_EQ("500,10 1000x100",
550 d1->GetWindowBoundsInScreen().ToString());
551 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
553 // Move to the primary root.
554 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
555 display0);
556 EXPECT_EQ("500,10 1000x100",
557 d1->GetWindowBoundsInScreen().ToString());
558 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
561 TEST_F(ExtendedDesktopTest, MoveWindowWithTransient) {
562 if (!SupportsMultipleDisplays())
563 return;
565 UpdateDisplay("1000x600,600x400");
566 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
567 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
568 views::Widget* w1_t1 = CreateTestWidgetWithParent(
569 w1, gfx::Rect(50, 50, 50, 50), false /* transient */);
570 // Transient child of the transient child.
571 views::Widget* w1_t11 = CreateTestWidgetWithParent(
572 w1_t1, gfx::Rect(1200, 70, 30, 30), false /* transient */);
574 views::Widget* w11 = CreateTestWidgetWithParent(
575 w1, gfx::Rect(10, 10, 40, 40), true /* child */);
576 views::Widget* w11_t1 = CreateTestWidgetWithParent(
577 w1, gfx::Rect(1300, 100, 80, 80), false /* transient */);
579 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
580 EXPECT_EQ(root_windows[0], w11->GetNativeView()->GetRootWindow());
581 EXPECT_EQ(root_windows[0], w1_t1->GetNativeView()->GetRootWindow());
582 EXPECT_EQ(root_windows[0], w1_t11->GetNativeView()->GetRootWindow());
583 EXPECT_EQ(root_windows[0], w11_t1->GetNativeView()->GetRootWindow());
584 EXPECT_EQ("50,50 50x50",
585 w1_t1->GetWindowBoundsInScreen().ToString());
586 EXPECT_EQ("1200,70 30x30",
587 w1_t11->GetWindowBoundsInScreen().ToString());
588 EXPECT_EQ("20,20 40x40",
589 w11->GetWindowBoundsInScreen().ToString());
590 EXPECT_EQ("1300,100 80x80",
591 w11_t1->GetWindowBoundsInScreen().ToString());
593 w1->SetBounds(gfx::Rect(1100,10,100,100));
595 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
596 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
597 EXPECT_EQ(root_windows[1], w1_t11->GetNativeView()->GetRootWindow());
598 EXPECT_EQ(root_windows[1], w11->GetNativeView()->GetRootWindow());
599 EXPECT_EQ(root_windows[1], w11_t1->GetNativeView()->GetRootWindow());
601 EXPECT_EQ("1110,20 40x40",
602 w11->GetWindowBoundsInScreen().ToString());
603 // Transient window's screen bounds stays the same.
604 EXPECT_EQ("50,50 50x50",
605 w1_t1->GetWindowBoundsInScreen().ToString());
606 EXPECT_EQ("1200,70 30x30",
607 w1_t11->GetWindowBoundsInScreen().ToString());
608 EXPECT_EQ("1300,100 80x80",
609 w11_t1->GetWindowBoundsInScreen().ToString());
611 // Transient window doesn't move between root window unless
612 // its transient parent moves.
613 w1_t1->SetBounds(gfx::Rect(10, 50, 50, 50));
614 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
615 EXPECT_EQ("10,50 50x50",
616 w1_t1->GetWindowBoundsInScreen().ToString());
619 // Test if the Window::ConvertPointToTarget works across root windows.
620 // TODO(oshima): Move multiple display suport and this test to aura.
621 TEST_F(ExtendedDesktopTest, ConvertPoint) {
622 if (!SupportsMultipleDisplays())
623 return;
624 gfx::Screen* screen = Shell::GetScreen();
625 UpdateDisplay("1000x600,600x400");
626 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
627 gfx::Display display_1 = screen->GetDisplayNearestWindow(root_windows[0]);
628 EXPECT_EQ("0,0", display_1.bounds().origin().ToString());
629 gfx::Display display_2 = screen->GetDisplayNearestWindow(root_windows[1]);
630 EXPECT_EQ("1000,0", display_2.bounds().origin().ToString());
632 aura::Window* d1 =
633 CreateTestWidget(gfx::Rect(10, 10, 100, 100))->GetNativeView();
634 aura::Window* d2 =
635 CreateTestWidget(gfx::Rect(1020, 20, 100, 100))->GetNativeView();
636 EXPECT_EQ(root_windows[0], d1->GetRootWindow());
637 EXPECT_EQ(root_windows[1], d2->GetRootWindow());
639 // Convert point in the Root2's window to the Root1's window Coord.
640 gfx::Point p(0, 0);
641 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
642 EXPECT_EQ("1000,0", p.ToString());
643 p.SetPoint(0, 0);
644 aura::Window::ConvertPointToTarget(d2, d1, &p);
645 EXPECT_EQ("1010,10", p.ToString());
647 // Convert point in the Root1's window to the Root2's window Coord.
648 p.SetPoint(0, 0);
649 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
650 EXPECT_EQ("-1000,0", p.ToString());
651 p.SetPoint(0, 0);
652 aura::Window::ConvertPointToTarget(d1, d2, &p);
653 EXPECT_EQ("-1010,-10", p.ToString());
655 // Move the 2nd display to the bottom and test again.
656 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
658 display_2 = screen->GetDisplayNearestWindow(root_windows[1]);
659 EXPECT_EQ("0,600", display_2.bounds().origin().ToString());
661 // Convert point in Root2's window to Root1's window Coord.
662 p.SetPoint(0, 0);
663 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
664 EXPECT_EQ("0,600", p.ToString());
665 p.SetPoint(0, 0);
666 aura::Window::ConvertPointToTarget(d2, d1, &p);
667 EXPECT_EQ("10,610", p.ToString());
669 // Convert point in Root1's window to Root2's window Coord.
670 p.SetPoint(0, 0);
671 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
672 EXPECT_EQ("0,-600", p.ToString());
673 p.SetPoint(0, 0);
674 aura::Window::ConvertPointToTarget(d1, d2, &p);
675 EXPECT_EQ("-10,-610", p.ToString());
678 TEST_F(ExtendedDesktopTest, OpenSystemTray) {
679 if (!SupportsMultipleDisplays())
680 return;
682 UpdateDisplay("500x600,600x400");
683 SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
684 ASSERT_FALSE(tray->HasSystemBubble());
686 aura::test::EventGenerator& event_generator(GetEventGenerator());
688 // Opens the tray by a dummy click event and makes sure that adding/removing
689 // displays doesn't break anything.
690 event_generator.MoveMouseToCenterOf(tray->GetWidget()->GetNativeWindow());
691 event_generator.ClickLeftButton();
692 EXPECT_TRUE(tray->HasSystemBubble());
694 UpdateDisplay("500x600");
695 EXPECT_TRUE(tray->HasSystemBubble());
696 UpdateDisplay("500x600,600x400");
697 EXPECT_TRUE(tray->HasSystemBubble());
699 // Closes the tray and again makes sure that adding/removing displays doesn't
700 // break anything.
701 event_generator.ClickLeftButton();
702 RunAllPendingInMessageLoop();
704 EXPECT_FALSE(tray->HasSystemBubble());
706 UpdateDisplay("500x600");
707 EXPECT_FALSE(tray->HasSystemBubble());
708 UpdateDisplay("500x600,600x400");
709 EXPECT_FALSE(tray->HasSystemBubble());
712 TEST_F(ExtendedDesktopTest, StayInSameRootWindow) {
713 if (!SupportsMultipleDisplays())
714 return;
716 UpdateDisplay("100x100,200x200");
717 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
718 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 50, 50));
719 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
720 w1->SetBounds(gfx::Rect(150, 10, 50, 50));
721 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
723 // The widget stays in the same root if kStayInSameRootWindowKey is set to
724 // true.
725 w1->GetNativeView()->SetProperty(internal::kStayInSameRootWindowKey, true);
726 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
727 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
729 // The widget should now move to the 1st root window without the property.
730 w1->GetNativeView()->ClearProperty(internal::kStayInSameRootWindowKey);
731 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
732 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
734 // a window in SettingsBubbleContainer and StatusContainer should
735 // not move to another root window regardles of the bounds specified.
736 aura::Window* settings_bubble_container =
737 Shell::GetPrimaryRootWindowController()->GetContainer(
738 internal::kShellWindowId_SettingBubbleContainer);
739 aura::Window* window = aura::test::CreateTestWindowWithId(
740 100, settings_bubble_container);
741 window->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
742 ScreenUtil::GetSecondaryDisplay());
743 EXPECT_EQ(root_windows[0], window->GetRootWindow());
745 aura::Window* status_container =
746 Shell::GetPrimaryRootWindowController()->GetContainer(
747 internal::kShellWindowId_StatusContainer);
748 window = aura::test::CreateTestWindowWithId(100, status_container);
749 window->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
750 ScreenUtil::GetSecondaryDisplay());
751 EXPECT_EQ(root_windows[0], window->GetRootWindow());
754 TEST_F(ExtendedDesktopTest, KeyEventsOnLockScreen) {
755 if (!SupportsMultipleDisplays())
756 return;
758 UpdateDisplay("100x100,200x200");
759 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
761 // Create normal windows on both displays.
762 views::Widget* widget1 = CreateTestWidget(
763 Shell::GetScreen()->GetPrimaryDisplay().bounds());
764 widget1->Show();
765 EXPECT_EQ(root_windows[0], widget1->GetNativeView()->GetRootWindow());
766 views::Widget* widget2 = CreateTestWidget(
767 ScreenUtil::GetSecondaryDisplay().bounds());
768 widget2->Show();
769 EXPECT_EQ(root_windows[1], widget2->GetNativeView()->GetRootWindow());
771 // Create a LockScreen window.
772 views::Widget* lock_widget = CreateTestWidget(
773 Shell::GetScreen()->GetPrimaryDisplay().bounds());
774 views::Textfield* textfield = new views::Textfield;
775 lock_widget->client_view()->AddChildView(textfield);
777 ash::Shell::GetContainer(
778 Shell::GetPrimaryRootWindow(),
779 ash::internal::kShellWindowId_LockScreenContainer)->
780 AddChild(lock_widget->GetNativeView());
781 lock_widget->Show();
782 textfield->RequestFocus();
784 aura::client::FocusClient* focus_client =
785 aura::client::GetFocusClient(root_windows[0]);
786 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
788 // The lock window should get events on both root windows.
789 aura::test::EventGenerator& event_generator(GetEventGenerator());
791 event_generator.set_current_dispatcher(
792 root_windows[0]->GetHost()->dispatcher());
793 event_generator.PressKey(ui::VKEY_A, 0);
794 event_generator.ReleaseKey(ui::VKEY_A, 0);
795 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
796 EXPECT_EQ("a", UTF16ToASCII(textfield->text()));
798 event_generator.set_current_dispatcher(
799 root_windows[1]->GetHost()->dispatcher());
800 event_generator.PressKey(ui::VKEY_B, 0);
801 event_generator.ReleaseKey(ui::VKEY_B, 0);
802 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
803 EXPECT_EQ("ab", UTF16ToASCII(textfield->text()));
805 // Deleting 2nd display. The lock window still should get the events.
806 UpdateDisplay("100x100");
807 event_generator.PressKey(ui::VKEY_C, 0);
808 event_generator.ReleaseKey(ui::VKEY_C, 0);
809 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
810 EXPECT_EQ("abc", UTF16ToASCII(textfield->text()));
812 // Creating 2nd display again, and lock window still should get events
813 // on both root windows.
814 UpdateDisplay("100x100,200x200");
815 root_windows = Shell::GetAllRootWindows();
816 event_generator.set_current_dispatcher(
817 root_windows[0]->GetHost()->dispatcher());
818 event_generator.PressKey(ui::VKEY_D, 0);
819 event_generator.ReleaseKey(ui::VKEY_D, 0);
820 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
821 EXPECT_EQ("abcd", UTF16ToASCII(textfield->text()));
823 event_generator.set_current_dispatcher(
824 root_windows[1]->GetHost()->dispatcher());
825 event_generator.PressKey(ui::VKEY_E, 0);
826 event_generator.ReleaseKey(ui::VKEY_E, 0);
827 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
828 EXPECT_EQ("abcde", UTF16ToASCII(textfield->text()));
831 TEST_F(ExtendedDesktopTest, PassiveGrab) {
832 if (!SupportsMultipleDisplays())
833 return;
835 EventLocationRecordingEventHandler event_handler;
836 ash::Shell::GetInstance()->AddPreTargetHandler(&event_handler);
838 UpdateDisplay("300x300,200x200");
840 views::Widget* widget = CreateTestWidget(gfx::Rect(50, 50, 200, 200));
841 widget->Show();
842 ASSERT_EQ("50,50 200x200", widget->GetWindowBoundsInScreen().ToString());
844 aura::test::EventGenerator& generator(GetEventGenerator());
845 generator.MoveMouseTo(150, 150);
846 EXPECT_EQ("100,100 150,150", event_handler.GetLocationsAndReset());
848 generator.PressLeftButton();
849 generator.MoveMouseTo(400, 150);
851 EXPECT_EQ("350,100 400,150", event_handler.GetLocationsAndReset());
853 generator.ReleaseLeftButton();
854 EXPECT_EQ("-999,-999 -999,-999", event_handler.GetLocationsAndReset());
856 generator.MoveMouseTo(400, 150);
857 EXPECT_EQ("100,150 100,150", event_handler.GetLocationsAndReset());
859 ash::Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
862 } // namespace ash