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/screen_ash.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/system/tray/system_tray.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/wm/coordinate_conversion.h"
13 #include "ash/wm/property_util.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/base/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 DisplayController
* display_controller
=
39 Shell::GetInstance()->display_controller();
40 DisplayLayout layout
= display_controller
->GetCurrentDisplayLayout();
41 layout
.position
= position
;
42 display_controller
->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 internal::DisplayManager
* GetDisplayManager() {
63 return Shell::GetInstance()->display_manager();
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
{
70 explicit MoveWindowByClickEventHandler(aura::Window
* target
)
72 virtual ~MoveWindowByClickEventHandler() {}
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
{
91 explicit EventLocationRecordingEventHandler() {
94 virtual ~EventLocationRecordingEventHandler() {}
96 std::string
GetLocationsAndReset() {
98 location_
.ToString() + " " + root_location_
.ToString();
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();
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
);
126 class ExtendedDesktopTest
: public test::AshTestBase
{
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
,
137 return CreateTestWidgetWithParentAndContext(parent
, NULL
, bounds
, child
);
140 views::Widget
* CreateTestWidgetWithParentAndContext(views::Widget
* parent
,
141 gfx::NativeView context
,
142 const gfx::Rect
& bounds
,
144 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_WINDOW
);
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
);
157 // Test conditions that root windows in extended desktop mode
159 TEST_F(ExtendedDesktopTest
, Basic
) {
160 if (!SupportsMultipleDisplays())
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(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())
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())
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::GetActiveRootWindow());
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(),
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::GetActiveRootWindow());
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::GetActiveRootWindow());
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::GetActiveRootWindow());
253 TEST_F(ExtendedDesktopTest
, TestCursor
) {
254 if (!SupportsMultipleDisplays())
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())
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())
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()));
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 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)));
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())
365 UpdateDisplay("700x500,500x500");
366 SetSecondaryDisplayLayout(DisplayLayout::LEFT
);
368 Shell::RootWindowList 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 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]));
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
436 generator2
.MoveMouseTo(-985, 15);
437 EXPECT_EQ("0 1 0", r1_d1
.GetMouseMotionCountsAndReset());
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())
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
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())
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())
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),
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),
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())
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());
611 // Test if the Window::ConvertPointToTarget works across root windows.
612 // TODO(oshima): Move multiple display suport and this test to aura.
613 TEST_F(ExtendedDesktopTest
, ConvertPoint
) {
614 if (!SupportsMultipleDisplays())
617 UpdateDisplay("1000x600,600x400");
618 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
619 gfx::Display
& display_1
=
620 GetDisplayManager()->FindDisplayForRootWindow(root_windows
[0]);
621 EXPECT_EQ("0,0", display_1
.bounds().origin().ToString());
622 gfx::Display
& display_2
=
623 GetDisplayManager()->FindDisplayForRootWindow(root_windows
[1]);
624 EXPECT_EQ("1000,0", display_2
.bounds().origin().ToString());
627 CreateTestWidget(gfx::Rect(10, 10, 100, 100))->GetNativeView();
629 CreateTestWidget(gfx::Rect(1020, 20, 100, 100))->GetNativeView();
630 EXPECT_EQ(root_windows
[0], d1
->GetRootWindow());
631 EXPECT_EQ(root_windows
[1], d2
->GetRootWindow());
633 // Convert point in the Root2's window to the Root1's window Coord.
635 aura::Window::ConvertPointToTarget(root_windows
[1], root_windows
[0], &p
);
636 EXPECT_EQ("1000,0", p
.ToString());
638 aura::Window::ConvertPointToTarget(d2
, d1
, &p
);
639 EXPECT_EQ("1010,10", p
.ToString());
641 // Convert point in the Root1's window to the Root2's window Coord.
643 aura::Window::ConvertPointToTarget(root_windows
[0], root_windows
[1], &p
);
644 EXPECT_EQ("-1000,0", p
.ToString());
646 aura::Window::ConvertPointToTarget(d1
, d2
, &p
);
647 EXPECT_EQ("-1010,-10", p
.ToString());
649 // Move the 2nd display to the bottom and test again.
650 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM
);
652 display_2
= GetDisplayManager()->FindDisplayForRootWindow(root_windows
[1]);
653 EXPECT_EQ("0,600", display_2
.bounds().origin().ToString());
655 // Convert point in Root2's window to Root1's window Coord.
657 aura::Window::ConvertPointToTarget(root_windows
[1], root_windows
[0], &p
);
658 EXPECT_EQ("0,600", p
.ToString());
660 aura::Window::ConvertPointToTarget(d2
, d1
, &p
);
661 EXPECT_EQ("10,610", p
.ToString());
663 // Convert point in Root1's window to Root2's window Coord.
665 aura::Window::ConvertPointToTarget(root_windows
[0], root_windows
[1], &p
);
666 EXPECT_EQ("0,-600", p
.ToString());
668 aura::Window::ConvertPointToTarget(d1
, d2
, &p
);
669 EXPECT_EQ("-10,-610", p
.ToString());
672 TEST_F(ExtendedDesktopTest
, OpenSystemTray
) {
673 if (!SupportsMultipleDisplays())
676 UpdateDisplay("500x600,600x400");
677 SystemTray
* tray
= ash::Shell::GetInstance()->GetPrimarySystemTray();
678 ASSERT_FALSE(tray
->HasSystemBubble());
680 aura::test::EventGenerator
& event_generator(GetEventGenerator());
682 // Opens the tray by a dummy click event and makes sure that adding/removing
683 // displays doesn't break anything.
684 event_generator
.MoveMouseToCenterOf(tray
->GetWidget()->GetNativeWindow());
685 event_generator
.ClickLeftButton();
686 EXPECT_TRUE(tray
->HasSystemBubble());
688 UpdateDisplay("500x600");
689 EXPECT_TRUE(tray
->HasSystemBubble());
690 UpdateDisplay("500x600,600x400");
691 EXPECT_TRUE(tray
->HasSystemBubble());
693 // Closes the tray and again makes sure that adding/removing displays doesn't
695 event_generator
.ClickLeftButton();
696 RunAllPendingInMessageLoop();
698 EXPECT_FALSE(tray
->HasSystemBubble());
700 UpdateDisplay("500x600");
701 EXPECT_FALSE(tray
->HasSystemBubble());
702 UpdateDisplay("500x600,600x400");
703 EXPECT_FALSE(tray
->HasSystemBubble());
706 TEST_F(ExtendedDesktopTest
, StayInSameRootWindow
) {
707 if (!SupportsMultipleDisplays())
710 UpdateDisplay("100x100,200x200");
711 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
712 views::Widget
* w1
= CreateTestWidget(gfx::Rect(10, 10, 50, 50));
713 EXPECT_EQ(root_windows
[0], w1
->GetNativeView()->GetRootWindow());
714 w1
->SetBounds(gfx::Rect(150, 10, 50, 50));
715 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
717 // The widget stays in the same root if kStayInSameRootWindowKey is set to
719 w1
->GetNativeView()->SetProperty(internal::kStayInSameRootWindowKey
, true);
720 w1
->SetBounds(gfx::Rect(10, 10, 50, 50));
721 EXPECT_EQ(root_windows
[1], w1
->GetNativeView()->GetRootWindow());
723 // The widget should now move to the 1st root window without the property.
724 w1
->GetNativeView()->ClearProperty(internal::kStayInSameRootWindowKey
);
725 w1
->SetBounds(gfx::Rect(10, 10, 50, 50));
726 EXPECT_EQ(root_windows
[0], w1
->GetNativeView()->GetRootWindow());
729 TEST_F(ExtendedDesktopTest
, KeyEventsOnLockScreen
) {
730 if (!SupportsMultipleDisplays())
733 UpdateDisplay("100x100,200x200");
734 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
736 // Create normal windows on both displays.
737 views::Widget
* widget1
= CreateTestWidget(
738 Shell::GetScreen()->GetPrimaryDisplay().bounds());
740 EXPECT_EQ(root_windows
[0], widget1
->GetNativeView()->GetRootWindow());
741 views::Widget
* widget2
= CreateTestWidget(
742 ScreenAsh::GetSecondaryDisplay().bounds());
744 EXPECT_EQ(root_windows
[1], widget2
->GetNativeView()->GetRootWindow());
746 // Create a LockScreen window.
747 views::Widget
* lock_widget
= CreateTestWidget(
748 Shell::GetScreen()->GetPrimaryDisplay().bounds());
749 views::Textfield
* textfield
= new views::Textfield
;
750 lock_widget
->client_view()->AddChildView(textfield
);
752 ash::Shell::GetContainer(
753 Shell::GetPrimaryRootWindow(),
754 ash::internal::kShellWindowId_LockScreenContainer
)->
755 AddChild(lock_widget
->GetNativeView());
757 textfield
->RequestFocus();
759 aura::client::FocusClient
* focus_client
=
760 aura::client::GetFocusClient(root_windows
[0]);
761 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
763 // The lock window should get events on both root windows.
764 aura::test::EventGenerator
& event_generator(GetEventGenerator());
766 event_generator
.set_current_root_window(root_windows
[0]);
767 event_generator
.PressKey(ui::VKEY_A
, 0);
768 event_generator
.ReleaseKey(ui::VKEY_A
, 0);
769 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
770 EXPECT_EQ("a", UTF16ToASCII(textfield
->text()));
772 event_generator
.set_current_root_window(root_windows
[1]);
773 event_generator
.PressKey(ui::VKEY_B
, 0);
774 event_generator
.ReleaseKey(ui::VKEY_B
, 0);
775 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
776 EXPECT_EQ("ab", UTF16ToASCII(textfield
->text()));
778 // Deleting 2nd display. The lock window still should get the events.
779 UpdateDisplay("100x100");
780 event_generator
.PressKey(ui::VKEY_C
, 0);
781 event_generator
.ReleaseKey(ui::VKEY_C
, 0);
782 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
783 EXPECT_EQ("abc", UTF16ToASCII(textfield
->text()));
785 // Creating 2nd display again, and lock window still should get events
786 // on both root windows.
787 UpdateDisplay("100x100,200x200");
788 root_windows
= Shell::GetAllRootWindows();
789 event_generator
.set_current_root_window(root_windows
[0]);
790 event_generator
.PressKey(ui::VKEY_D
, 0);
791 event_generator
.ReleaseKey(ui::VKEY_D
, 0);
792 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
793 EXPECT_EQ("abcd", UTF16ToASCII(textfield
->text()));
795 event_generator
.set_current_root_window(root_windows
[1]);
796 event_generator
.PressKey(ui::VKEY_E
, 0);
797 event_generator
.ReleaseKey(ui::VKEY_E
, 0);
798 EXPECT_EQ(lock_widget
->GetNativeView(), focus_client
->GetFocusedWindow());
799 EXPECT_EQ("abcde", UTF16ToASCII(textfield
->text()));
802 TEST_F(ExtendedDesktopTest
, PassiveGrab
) {
803 if (!SupportsMultipleDisplays())
806 EventLocationRecordingEventHandler event_handler
;
807 ash::Shell::GetInstance()->AddPreTargetHandler(&event_handler
);
809 UpdateDisplay("300x300,200x200");
811 views::Widget
* widget
= CreateTestWidget(gfx::Rect(50, 50, 200, 200));
813 ASSERT_EQ("50,50 200x200", widget
->GetWindowBoundsInScreen().ToString());
815 aura::test::EventGenerator
& generator(GetEventGenerator());
816 generator
.MoveMouseTo(150, 150);
817 EXPECT_EQ("100,100 150,150", event_handler
.GetLocationsAndReset());
819 generator
.PressLeftButton();
820 generator
.MoveMouseTo(400, 150);
822 EXPECT_EQ("350,100 400,150", event_handler
.GetLocationsAndReset());
824 generator
.ReleaseLeftButton();
825 EXPECT_EQ("-999,-999 -999,-999", event_handler
.GetLocationsAndReset());
827 generator
.MoveMouseTo(400, 150);
828 EXPECT_EQ("100,150 100,150", event_handler
.GetLocationsAndReset());
830 ash::Shell::GetInstance()->RemovePreTargetHandler(&event_handler
);
833 } // namespace internal