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"
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"
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
{
47 ModalWidgetDelegate() {}
48 virtual ~ModalWidgetDelegate() {}
50 // Overridden from views::WidgetDelegate:
51 virtual views::View
* GetContentsView() OVERRIDE
{
54 virtual ui::ModalType
GetModalType() const OVERRIDE
{
55 return ui::MODAL_TYPE_SYSTEM
;
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
{
66 explicit MoveWindowByClickEventHandler(aura::Window
* target
)
68 virtual ~MoveWindowByClickEventHandler() {}
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
{
87 explicit EventLocationRecordingEventHandler() {
90 virtual ~EventLocationRecordingEventHandler() {}
92 std::string
GetLocationsAndReset() {
94 location_
.ToString() + " " + root_location_
.ToString();
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();
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
);
122 class ExtendedDesktopTest
: public test::AshTestBase
{
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
,
133 return CreateTestWidgetWithParentAndContext(parent
, NULL
, bounds
, child
);
136 views::Widget
* CreateTestWidgetWithParentAndContext(views::Widget
* parent
,
137 gfx::NativeView context
,
138 const gfx::Rect
& bounds
,
140 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_WINDOW
);
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
);
153 // Test conditions that root windows in extended desktop mode
155 TEST_F(ExtendedDesktopTest
, Basic
) {
156 if (!SupportsMultipleDisplays())
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())
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())
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(),
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())
253 UpdateDisplay("1000x600,600x400");
254 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
255 aura::WindowEventDispatcher
* dispatcher0
= root_windows
[0]->GetDispatcher();
256 aura::WindowEventDispatcher
* dispatcher1
= root_windows
[1]->GetDispatcher();
257 EXPECT_EQ(ui::kCursorPointer
,
258 dispatcher0
->host()->last_cursor().native_type());
259 EXPECT_EQ(ui::kCursorPointer
,
260 dispatcher1
->host()->last_cursor().native_type());
261 Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorCopy
);
262 EXPECT_EQ(ui::kCursorCopy
, dispatcher0
->host()->last_cursor().native_type());
263 EXPECT_EQ(ui::kCursorCopy
, dispatcher1
->host()->last_cursor().native_type());
266 TEST_F(ExtendedDesktopTest
, TestCursorLocation
) {
267 if (!SupportsMultipleDisplays())
270 UpdateDisplay("1000x600,600x400");
271 aura::Window::Windows 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())
293 UpdateDisplay("700x500,500x500");
294 aura::Window::Windows 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()));
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())
344 UpdateDisplay("700x500,500x500");
345 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
346 aura::Window::Windows 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)));
354 EXPECT_EQ(root_windows
[0], wm::GetRootWindowAt(gfx::Point(0, 0)));
356 // Out of range point should return the nearest root window
357 EXPECT_EQ(root_windows
[1], 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())
365 UpdateDisplay("700x500,500x500");
366 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
368 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
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)));
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)));
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())
405 UpdateDisplay("1000x600,600x400");
406 aura::Window::Windows 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]));
421 EXPECT_EQ(r1_w1
.get(),
422 aura::client::GetCaptureWindow(r2_w1
->GetRootWindow()));
424 aura::test::EventGenerator
& generator
= GetEventGenerator();
425 generator
.MoveMouseToCenterOf(r2_w1
.get());
426 // |r1_w1| will receive the events because it has capture.
427 EXPECT_EQ("1 1 0", r1_d1
.GetMouseMotionCountsAndReset());
428 EXPECT_EQ("0 0 0", r1_d2
.GetMouseMotionCountsAndReset());
429 EXPECT_EQ("0 0 0", r2_d1
.GetMouseMotionCountsAndReset());
431 generator
.ClickLeftButton();
432 EXPECT_EQ("0 0 0", r2_d1
.GetMouseMotionCountsAndReset());
433 EXPECT_EQ("0 0", r2_d1
.GetMouseButtonCountsAndReset());
434 // The mouse is outside. On chromeos, the mouse is warped to the
435 // dest root window, but it's not implemented on Win yet, so
436 // no mouse move event on Win.
437 EXPECT_EQ("0 0 0", r1_d1
.GetMouseMotionCountsAndReset());
438 EXPECT_EQ("1 1", r1_d1
.GetMouseButtonCountsAndReset());
440 generator
.MoveMouseTo(15, 15);
441 EXPECT_EQ("0 1 0", r1_d1
.GetMouseMotionCountsAndReset());
442 EXPECT_EQ("0 0 0", r1_d2
.GetMouseMotionCountsAndReset());
445 EXPECT_EQ(r1_w2
.get(),
446 aura::client::GetCaptureWindow(r2_w1
->GetRootWindow()));
447 generator
.MoveMouseBy(10, 10);
448 // |r1_w2| has the capture. So it will receive the mouse-move event.
449 EXPECT_EQ("0 0 0", r1_d1
.GetMouseMotionCountsAndReset());
450 EXPECT_EQ("0 1 0", r1_d2
.GetMouseMotionCountsAndReset());
451 EXPECT_EQ("0 0 0", r2_d1
.GetMouseMotionCountsAndReset());
453 generator
.ClickLeftButton();
454 EXPECT_EQ("0 0 0", r2_d1
.GetMouseMotionCountsAndReset());
455 EXPECT_EQ("0 0", r2_d1
.GetMouseButtonCountsAndReset());
456 EXPECT_EQ("0 0 0", r1_d2
.GetMouseMotionCountsAndReset());
457 EXPECT_EQ("1 1", r1_d2
.GetMouseButtonCountsAndReset());
459 r1_w2
->ReleaseCapture();
460 EXPECT_EQ(NULL
, aura::client::GetCaptureWindow(r2_w1
->GetRootWindow()));
462 generator
.MoveMouseToCenterOf(r2_w1
.get());
463 generator
.ClickLeftButton();
464 EXPECT_EQ("1 1 0", r2_d1
.GetMouseMotionCountsAndReset());
465 EXPECT_EQ("1 1", r2_d1
.GetMouseButtonCountsAndReset());
466 // Make sure the mouse_moved_handler_ is properly reset.
467 EXPECT_EQ("0 0 0", r1_d2
.GetMouseMotionCountsAndReset());
468 EXPECT_EQ("0 0", r1_d2
.GetMouseButtonCountsAndReset());
471 TEST_F(ExtendedDesktopTest
, MoveWindow
) {
472 if (!SupportsMultipleDisplays())
475 UpdateDisplay("1000x600,600x400");
476 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
477 views::Widget
* d1
= CreateTestWidget(gfx::Rect(10, 10, 100, 100));
479 EXPECT_EQ(root_windows
[0], d1
->GetNativeView()->GetRootWindow());
481 d1
->SetBounds(gfx::Rect(1010, 10, 100, 100));
482 EXPECT_EQ("1010,10 100x100",
483 d1
->GetWindowBoundsInScreen().ToString());
485 EXPECT_EQ(root_windows
[1], d1
->GetNativeView()->GetRootWindow());
487 d1
->SetBounds(gfx::Rect(10, 10, 100, 100));
488 EXPECT_EQ("10,10 100x100",
489 d1
->GetWindowBoundsInScreen().ToString());
491 EXPECT_EQ(root_windows
[0], d1
->GetNativeView()->GetRootWindow());
493 // Make sure the bounds which doesn't fit to the root window
495 d1
->SetBounds(gfx::Rect(1560, 30, 100, 100));
496 EXPECT_EQ(root_windows
[1], d1
->GetNativeView()->GetRootWindow());
497 EXPECT_EQ("1560,30 100x100",
498 d1
->GetWindowBoundsInScreen().ToString());
500 // Setting outside of root windows will be moved to primary root window.
501 // TODO(oshima): This one probably should pick the closest root window.
502 d1
->SetBounds(gfx::Rect(200, 10, 100, 100));
503 EXPECT_EQ(root_windows
[0], d1
->GetNativeView()->GetRootWindow());
506 // Verifies if the mouse event arrives to the window even when the window
507 // moves to another root in a pre-target handler. See: crbug.com/157583
508 TEST_F(ExtendedDesktopTest
, MoveWindowByMouseClick
) {
509 if (!SupportsMultipleDisplays())
512 UpdateDisplay("1000x600,600x400");
514 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
515 aura::test::EventCountDelegate delegate
;
516 scoped_ptr
<aura::Window
> window(aura::test::CreateTestWindowWithDelegate(
517 &delegate
, 0, gfx::Rect(10, 10, 100, 100), root_windows
[0]));
518 MoveWindowByClickEventHandler
event_handler(window
.get());
519 window
->AddPreTargetHandler(&event_handler
);
521 aura::test::EventGenerator
& event_generator(GetEventGenerator());
523 event_generator
.MoveMouseToCenterOf(window
.get());
524 event_generator
.ClickLeftButton();
525 // Both mouse pressed and released arrive at the window and its delegate.
526 EXPECT_EQ("1 1", delegate
.GetMouseButtonCountsAndReset());
527 // Also event_handler moves the window to another root at mouse release.
528 EXPECT_EQ(root_windows
[1], window
->GetRootWindow());
531 TEST_F(ExtendedDesktopTest
, MoveWindowToDisplay
) {
532 if (!SupportsMultipleDisplays())
535 UpdateDisplay("1000x1000,1000x1000");
536 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
538 gfx::Display display0
= Shell::GetScreen()->GetDisplayMatching(
539 root_windows
[0]->GetBoundsInScreen());
540 gfx::Display display1
= Shell::GetScreen()->GetDisplayMatching(
541 root_windows
[1]->GetBoundsInScreen());
542 EXPECT_NE(display0
.id(), display1
.id());
544 views::Widget
* d1
= CreateTestWidget(gfx::Rect(10, 10, 1000, 100));
545 EXPECT_EQ(root_windows
[0], d1
->GetNativeView()->GetRootWindow());
547 // Move the window where the window spans both root windows. Since the second
548 // parameter is |display1|, the window should be shown on the secondary root.
549 d1
->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
551 EXPECT_EQ("500,10 1000x100",
552 d1
->GetWindowBoundsInScreen().ToString());
553 EXPECT_EQ(root_windows
[1], d1
->GetNativeView()->GetRootWindow());
555 // Move to the primary root.
556 d1
->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
558 EXPECT_EQ("500,10 1000x100",
559 d1
->GetWindowBoundsInScreen().ToString());
560 EXPECT_EQ(root_windows
[0], d1
->GetNativeView()->GetRootWindow());
563 TEST_F(ExtendedDesktopTest
, MoveWindowWithTransient
) {
564 if (!SupportsMultipleDisplays())
567 UpdateDisplay("1000x600,600x400");
568 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
569 views::Widget
* w1
= CreateTestWidget(gfx::Rect(10, 10, 100, 100));
570 views::Widget
* w1_t1
= CreateTestWidgetWithParent(
571 w1
, gfx::Rect(50, 50, 50, 50), false /* transient */);
572 // Transient child of the transient child.
573 views::Widget
* w1_t11
= CreateTestWidgetWithParent(
574 w1_t1
, gfx::Rect(1200, 70, 30, 30), false /* transient */);
576 views::Widget
* w11
= CreateTestWidgetWithParent(
577 w1
, gfx::Rect(10, 10, 40, 40), true /* child */);
578 views::Widget
* w11_t1
= CreateTestWidgetWithParent(
579 w1
, gfx::Rect(1300, 100, 80, 80), false /* transient */);
581 EXPECT_EQ(root_windows
[0], w1
->GetNativeView()->GetRootWindow());
582 EXPECT_EQ(root_windows
[0], w11
->GetNativeView()->GetRootWindow());
583 EXPECT_EQ(root_windows
[0], w1_t1
->GetNativeView()->GetRootWindow());
584 EXPECT_EQ(root_windows
[0], w1_t11
->GetNativeView()->GetRootWindow());
585 EXPECT_EQ(root_windows
[0], w11_t1
->GetNativeView()->GetRootWindow());
586 EXPECT_EQ("50,50 50x50",
587 w1_t1
->GetWindowBoundsInScreen().ToString());
588 EXPECT_EQ("1200,70 30x30",
589 w1_t11
->GetWindowBoundsInScreen().ToString());
590 EXPECT_EQ("20,20 40x40",
591 w11
->GetWindowBoundsInScreen().ToString());
592 EXPECT_EQ("1300,100 80x80",
593 w11_t1
->GetWindowBoundsInScreen().ToString());
595 w1
->SetBounds(gfx::Rect(1100,10,100,100));
597 EXPECT_EQ(root_windows
[1], w1_t1
->GetNativeView()->GetRootWindow());
598 EXPECT_EQ(root_windows
[1], w1_t1
->GetNativeView()->GetRootWindow());
599 EXPECT_EQ(root_windows
[1], w1_t11
->GetNativeView()->GetRootWindow());
600 EXPECT_EQ(root_windows
[1], w11
->GetNativeView()->GetRootWindow());
601 EXPECT_EQ(root_windows
[1], w11_t1
->GetNativeView()->GetRootWindow());
603 EXPECT_EQ("1110,20 40x40",
604 w11
->GetWindowBoundsInScreen().ToString());
605 // Transient window's screen bounds stays the same.
606 EXPECT_EQ("50,50 50x50",
607 w1_t1
->GetWindowBoundsInScreen().ToString());
608 EXPECT_EQ("1200,70 30x30",
609 w1_t11
->GetWindowBoundsInScreen().ToString());
610 EXPECT_EQ("1300,100 80x80",
611 w11_t1
->GetWindowBoundsInScreen().ToString());
613 // Transient window doesn't move between root window unless
614 // its transient parent moves.
615 w1_t1
->SetBounds(gfx::Rect(10, 50, 50, 50));
616 EXPECT_EQ(root_windows
[1], w1_t1
->GetNativeView()->GetRootWindow());
617 EXPECT_EQ("10,50 50x50",
618 w1_t1
->GetWindowBoundsInScreen().ToString());
621 // Test if the Window::ConvertPointToTarget works across root windows.
622 // TODO(oshima): Move multiple display suport and this test to aura.
623 TEST_F(ExtendedDesktopTest
, ConvertPoint
) {
624 if (!SupportsMultipleDisplays())
626 gfx::Screen
* screen
= Shell::GetScreen();
627 UpdateDisplay("1000x600,600x400");
628 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
629 gfx::Display display_1
= screen
->GetDisplayNearestWindow(root_windows
[0]);
630 EXPECT_EQ("0,0", display_1
.bounds().origin().ToString());
631 gfx::Display display_2
= screen
->GetDisplayNearestWindow(root_windows
[1]);
632 EXPECT_EQ("1000,0", display_2
.bounds().origin().ToString());
635 CreateTestWidget(gfx::Rect(10, 10, 100, 100))->GetNativeView();
637 CreateTestWidget(gfx::Rect(1020, 20, 100, 100))->GetNativeView();
638 EXPECT_EQ(root_windows
[0], d1
->GetRootWindow());
639 EXPECT_EQ(root_windows
[1], d2
->GetRootWindow());
641 // Convert point in the Root2's window to the Root1's window Coord.
643 aura::Window::ConvertPointToTarget(root_windows
[1], root_windows
[0], &p
);
644 EXPECT_EQ("1000,0", p
.ToString());
646 aura::Window::ConvertPointToTarget(d2
, d1
, &p
);
647 EXPECT_EQ("1010,10", p
.ToString());
649 // Convert point in the Root1's window to the Root2's window Coord.
651 aura::Window::ConvertPointToTarget(root_windows
[0], root_windows
[1], &p
);
652 EXPECT_EQ("-1000,0", p
.ToString());
654 aura::Window::ConvertPointToTarget(d1
, d2
, &p
);
655 EXPECT_EQ("-1010,-10", p
.ToString());
657 // Move the 2nd display to the bottom and test again.
658 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
660 display_2
= screen
->GetDisplayNearestWindow(root_windows
[1]);
661 EXPECT_EQ("0,600", display_2
.bounds().origin().ToString());
663 // Convert point in Root2's window to Root1's window Coord.
665 aura::Window::ConvertPointToTarget(root_windows
[1], root_windows
[0], &p
);
666 EXPECT_EQ("0,600", p
.ToString());
668 aura::Window::ConvertPointToTarget(d2
, d1
, &p
);
669 EXPECT_EQ("10,610", p
.ToString());
671 // Convert point in Root1's window to Root2's window Coord.
673 aura::Window::ConvertPointToTarget(root_windows
[0], root_windows
[1], &p
);
674 EXPECT_EQ("0,-600", p
.ToString());
676 aura::Window::ConvertPointToTarget(d1
, d2
, &p
);
677 EXPECT_EQ("-10,-610", p
.ToString());
680 TEST_F(ExtendedDesktopTest
, OpenSystemTray
) {
681 if (!SupportsMultipleDisplays())
684 UpdateDisplay("500x600,600x400");
685 SystemTray
* tray
= ash::Shell::GetInstance()->GetPrimarySystemTray();
686 ASSERT_FALSE(tray
->HasSystemBubble());
688 aura::test::EventGenerator
& event_generator(GetEventGenerator());
690 // Opens the tray by a dummy click event and makes sure that adding/removing
691 // displays doesn't break anything.
692 event_generator
.MoveMouseToCenterOf(tray
->GetWidget()->GetNativeWindow());
693 event_generator
.ClickLeftButton();
694 EXPECT_TRUE(tray
->HasSystemBubble());
696 UpdateDisplay("500x600");
697 EXPECT_TRUE(tray
->HasSystemBubble());
698 UpdateDisplay("500x600,600x400");
699 EXPECT_TRUE(tray
->HasSystemBubble());
701 // Closes the tray and again makes sure that adding/removing displays doesn't
703 event_generator
.ClickLeftButton();
704 RunAllPendingInMessageLoop();
706 EXPECT_FALSE(tray
->HasSystemBubble());
708 UpdateDisplay("500x600");
709 EXPECT_FALSE(tray
->HasSystemBubble());
710 UpdateDisplay("500x600,600x400");
711 EXPECT_FALSE(tray
->HasSystemBubble());
714 TEST_F(ExtendedDesktopTest
, StayInSameRootWindow
) {
715 if (!SupportsMultipleDisplays())
718 UpdateDisplay("100x100,200x200");
719 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
720 views::Widget
* w1
= CreateTestWidget(gfx::Rect(10, 10, 50, 50));
721 EXPECT_EQ(root_windows
[0], w1
->GetNativeView()->GetRootWindow());
722 w1
->SetBounds(gfx::Rect(150, 10, 50, 50));
723 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
725 // The widget stays in the same root if kStayInSameRootWindowKey is set to
727 w1
->GetNativeView()->SetProperty(internal::kStayInSameRootWindowKey
, true);
728 w1
->SetBounds(gfx::Rect(10, 10, 50, 50));
729 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
731 // The widget should now move to the 1st root window without the property.
732 w1
->GetNativeView()->ClearProperty(internal::kStayInSameRootWindowKey
);
733 w1
->SetBounds(gfx::Rect(10, 10, 50, 50));
734 EXPECT_EQ(root_windows
[0], w1
->GetNativeView()->GetRootWindow());
736 // a window in SettingsBubbleContainer and StatusContainer should
737 // not move to another root window regardles of the bounds specified.
738 aura::Window
* settings_bubble_container
=
739 Shell::GetPrimaryRootWindowController()->GetContainer(
740 internal::kShellWindowId_SettingBubbleContainer
);
741 aura::Window
* window
= aura::test::CreateTestWindowWithId(
742 100, settings_bubble_container
);
743 window
->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
744 ScreenUtil::GetSecondaryDisplay());
745 EXPECT_EQ(root_windows
[0], window
->GetRootWindow());
747 aura::Window
* status_container
=
748 Shell::GetPrimaryRootWindowController()->GetContainer(
749 internal::kShellWindowId_StatusContainer
);
750 window
= aura::test::CreateTestWindowWithId(100, status_container
);
751 window
->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
752 ScreenUtil::GetSecondaryDisplay());
753 EXPECT_EQ(root_windows
[0], window
->GetRootWindow());
756 TEST_F(ExtendedDesktopTest
, KeyEventsOnLockScreen
) {
757 if (!SupportsMultipleDisplays())
760 UpdateDisplay("100x100,200x200");
761 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
763 // Create normal windows on both displays.
764 views::Widget
* widget1
= CreateTestWidget(
765 Shell::GetScreen()->GetPrimaryDisplay().bounds());
767 EXPECT_EQ(root_windows
[0], widget1
->GetNativeView()->GetRootWindow());
768 views::Widget
* widget2
= CreateTestWidget(
769 ScreenUtil::GetSecondaryDisplay().bounds());
771 EXPECT_EQ(root_windows
[1], widget2
->GetNativeView()->GetRootWindow());
773 // Create a LockScreen window.
774 views::Widget
* lock_widget
= CreateTestWidget(
775 Shell::GetScreen()->GetPrimaryDisplay().bounds());
776 views::Textfield
* textfield
= new views::Textfield
;
777 lock_widget
->client_view()->AddChildView(textfield
);
779 ash::Shell::GetContainer(
780 Shell::GetPrimaryRootWindow(),
781 ash::internal::kShellWindowId_LockScreenContainer
)->
782 AddChild(lock_widget
->GetNativeView());
784 textfield
->RequestFocus();
786 aura::client::FocusClient
* focus_client
=
787 aura::client::GetFocusClient(root_windows
[0]);
788 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
790 // The lock window should get events on both root windows.
791 aura::test::EventGenerator
& event_generator(GetEventGenerator());
793 event_generator
.set_current_dispatcher(root_windows
[0]->GetDispatcher());
794 event_generator
.PressKey(ui::VKEY_A
, 0);
795 event_generator
.ReleaseKey(ui::VKEY_A
, 0);
796 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
797 EXPECT_EQ("a", UTF16ToASCII(textfield
->text()));
799 event_generator
.set_current_dispatcher(root_windows
[1]->GetDispatcher());
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(root_windows
[0]->GetDispatcher());
817 event_generator
.PressKey(ui::VKEY_D
, 0);
818 event_generator
.ReleaseKey(ui::VKEY_D
, 0);
819 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
820 EXPECT_EQ("abcd", UTF16ToASCII(textfield
->text()));
822 event_generator
.set_current_dispatcher(root_windows
[1]->GetDispatcher());
823 event_generator
.PressKey(ui::VKEY_E
, 0);
824 event_generator
.ReleaseKey(ui::VKEY_E
, 0);
825 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
826 EXPECT_EQ("abcde", UTF16ToASCII(textfield
->text()));
829 TEST_F(ExtendedDesktopTest
, PassiveGrab
) {
830 if (!SupportsMultipleDisplays())
833 EventLocationRecordingEventHandler event_handler
;
834 ash::Shell::GetInstance()->AddPreTargetHandler(&event_handler
);
836 UpdateDisplay("300x300,200x200");
838 views::Widget
* widget
= CreateTestWidget(gfx::Rect(50, 50, 200, 200));
840 ASSERT_EQ("50,50 200x200", widget
->GetWindowBoundsInScreen().ToString());
842 aura::test::EventGenerator
& generator(GetEventGenerator());
843 generator
.MoveMouseTo(150, 150);
844 EXPECT_EQ("100,100 150,150", event_handler
.GetLocationsAndReset());
846 generator
.PressLeftButton();
847 generator
.MoveMouseTo(400, 150);
849 EXPECT_EQ("350,100 400,150", event_handler
.GetLocationsAndReset());
851 generator
.ReleaseLeftButton();
852 EXPECT_EQ("-999,-999 -999,-999", event_handler
.GetLocationsAndReset());
854 generator
.MoveMouseTo(400, 150);
855 EXPECT_EQ("100,150 100,150", event_handler
.GetLocationsAndReset());
857 ash::Shell::GetInstance()->RemovePreTargetHandler(&event_handler
);