android: disable NLS when building yasm.
[chromium-blink-merge.git] / ash / extended_desktop_unittest.cc
blob6e027268780bddf4dc7b145e05509c905141a3a5
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/root_window.h"
22 #include "ui/aura/test/event_generator.h"
23 #include "ui/aura/test/test_windows.h"
24 #include "ui/aura/test/window_test_api.h"
25 #include "ui/aura/window.h"
26 #include "ui/base/cursor/cursor.h"
27 #include "ui/events/event_handler.h"
28 #include "ui/gfx/display.h"
29 #include "ui/gfx/screen.h"
30 #include "ui/views/controls/textfield/textfield.h"
31 #include "ui/views/widget/widget.h"
32 #include "ui/views/widget/widget_delegate.h"
34 namespace ash {
35 namespace {
37 void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
38 DisplayLayout layout =
39 Shell::GetInstance()->display_manager()->GetCurrentDisplayLayout();
40 layout.position = position;
41 Shell::GetInstance()->display_manager()->
42 SetLayoutForCurrentDisplays(layout);
45 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::WindowEventDispatcher* dispatcher0 = root_windows[0]->GetDispatcher();
256 aura::WindowEventDispatcher* dispatcher1 = root_windows[1]->GetDispatcher();
257 EXPECT_EQ(ui::kCursorPointer, dispatcher0->last_cursor().native_type());
258 EXPECT_EQ(ui::kCursorPointer, dispatcher1->last_cursor().native_type());
259 Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorCopy);
260 EXPECT_EQ(ui::kCursorCopy, dispatcher0->last_cursor().native_type());
261 EXPECT_EQ(ui::kCursorCopy, dispatcher1->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 generator2(root_windows[1]);
423 generator2.MoveMouseToCenterOf(r2_w1.get());
424 generator2.ClickLeftButton();
425 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
426 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
427 // The mouse is outside. On chromeos, the mouse is warped to the
428 // dest root window, but it's not implemented on Win yet, so
429 // no mouse move event on Win.
430 EXPECT_EQ("1 1 0", r1_d1.GetMouseMotionCountsAndReset());
431 EXPECT_EQ("1 1", r1_d1.GetMouseButtonCountsAndReset());
432 // Emulate passive grab. (15,15) on 1st display is (-985,15) on 2nd
433 // display.
434 generator2.MoveMouseTo(-985, 15);
435 EXPECT_EQ("0 1 0", r1_d1.GetMouseMotionCountsAndReset());
437 r1_w2->SetCapture();
438 EXPECT_EQ(r1_w2.get(),
439 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
440 generator2.MoveMouseBy(10, 10);
441 generator2.ClickLeftButton();
442 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
443 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
444 EXPECT_EQ("1 1 0", r1_d2.GetMouseMotionCountsAndReset());
445 EXPECT_EQ("1 1", r1_d2.GetMouseButtonCountsAndReset());
446 r1_w2->ReleaseCapture();
447 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
448 generator2.MoveMouseTo(15, 15);
449 generator2.ClickLeftButton();
450 EXPECT_EQ("1 1 0", r2_d1.GetMouseMotionCountsAndReset());
451 EXPECT_EQ("1 1", r2_d1.GetMouseButtonCountsAndReset());
452 // Make sure the mouse_moved_handler_ is properly reset.
453 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
454 EXPECT_EQ("0 0", r1_d2.GetMouseButtonCountsAndReset());
457 TEST_F(ExtendedDesktopTest, MoveWindow) {
458 if (!SupportsMultipleDisplays())
459 return;
461 UpdateDisplay("1000x600,600x400");
462 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
463 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
465 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
467 d1->SetBounds(gfx::Rect(1010, 10, 100, 100));
468 EXPECT_EQ("1010,10 100x100",
469 d1->GetWindowBoundsInScreen().ToString());
471 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
473 d1->SetBounds(gfx::Rect(10, 10, 100, 100));
474 EXPECT_EQ("10,10 100x100",
475 d1->GetWindowBoundsInScreen().ToString());
477 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
479 // Make sure the bounds which doesn't fit to the root window
480 // works correctly.
481 d1->SetBounds(gfx::Rect(1560, 30, 100, 100));
482 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
483 EXPECT_EQ("1560,30 100x100",
484 d1->GetWindowBoundsInScreen().ToString());
486 // Setting outside of root windows will be moved to primary root window.
487 // TODO(oshima): This one probably should pick the closest root window.
488 d1->SetBounds(gfx::Rect(200, 10, 100, 100));
489 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
492 // Verifies if the mouse event arrives to the window even when the window
493 // moves to another root in a pre-target handler. See: crbug.com/157583
494 TEST_F(ExtendedDesktopTest, MoveWindowByMouseClick) {
495 if (!SupportsMultipleDisplays())
496 return;
498 UpdateDisplay("1000x600,600x400");
500 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
501 aura::test::EventCountDelegate delegate;
502 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
503 &delegate, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
504 MoveWindowByClickEventHandler event_handler(window.get());
505 window->AddPreTargetHandler(&event_handler);
507 aura::test::EventGenerator& event_generator(GetEventGenerator());
509 event_generator.MoveMouseToCenterOf(window.get());
510 event_generator.ClickLeftButton();
511 // Both mouse pressed and released arrive at the window and its delegate.
512 EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
513 // Also event_handler moves the window to another root at mouse release.
514 EXPECT_EQ(root_windows[1], window->GetRootWindow());
517 TEST_F(ExtendedDesktopTest, MoveWindowToDisplay) {
518 if (!SupportsMultipleDisplays())
519 return;
521 UpdateDisplay("1000x1000,1000x1000");
522 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
524 gfx::Display display0 = Shell::GetScreen()->GetDisplayMatching(
525 root_windows[0]->GetBoundsInScreen());
526 gfx::Display display1 = Shell::GetScreen()->GetDisplayMatching(
527 root_windows[1]->GetBoundsInScreen());
528 EXPECT_NE(display0.id(), display1.id());
530 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 1000, 100));
531 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
533 // Move the window where the window spans both root windows. Since the second
534 // parameter is |display1|, the window should be shown on the secondary root.
535 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
536 display1);
537 EXPECT_EQ("500,10 1000x100",
538 d1->GetWindowBoundsInScreen().ToString());
539 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
541 // Move to the primary root.
542 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
543 display0);
544 EXPECT_EQ("500,10 1000x100",
545 d1->GetWindowBoundsInScreen().ToString());
546 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
549 TEST_F(ExtendedDesktopTest, MoveWindowWithTransient) {
550 if (!SupportsMultipleDisplays())
551 return;
553 UpdateDisplay("1000x600,600x400");
554 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
555 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
556 views::Widget* w1_t1 = CreateTestWidgetWithParent(
557 w1, gfx::Rect(50, 50, 50, 50), false /* transient */);
558 // Transient child of the transient child.
559 views::Widget* w1_t11 = CreateTestWidgetWithParent(
560 w1_t1, gfx::Rect(1200, 70, 30, 30), false /* transient */);
562 views::Widget* w11 = CreateTestWidgetWithParent(
563 w1, gfx::Rect(10, 10, 40, 40), true /* child */);
564 views::Widget* w11_t1 = CreateTestWidgetWithParent(
565 w1, gfx::Rect(1300, 100, 80, 80), false /* transient */);
567 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
568 EXPECT_EQ(root_windows[0], w11->GetNativeView()->GetRootWindow());
569 EXPECT_EQ(root_windows[0], w1_t1->GetNativeView()->GetRootWindow());
570 EXPECT_EQ(root_windows[0], w1_t11->GetNativeView()->GetRootWindow());
571 EXPECT_EQ(root_windows[0], w11_t1->GetNativeView()->GetRootWindow());
572 EXPECT_EQ("50,50 50x50",
573 w1_t1->GetWindowBoundsInScreen().ToString());
574 EXPECT_EQ("1200,70 30x30",
575 w1_t11->GetWindowBoundsInScreen().ToString());
576 EXPECT_EQ("20,20 40x40",
577 w11->GetWindowBoundsInScreen().ToString());
578 EXPECT_EQ("1300,100 80x80",
579 w11_t1->GetWindowBoundsInScreen().ToString());
581 w1->SetBounds(gfx::Rect(1100,10,100,100));
583 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
584 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
585 EXPECT_EQ(root_windows[1], w1_t11->GetNativeView()->GetRootWindow());
586 EXPECT_EQ(root_windows[1], w11->GetNativeView()->GetRootWindow());
587 EXPECT_EQ(root_windows[1], w11_t1->GetNativeView()->GetRootWindow());
589 EXPECT_EQ("1110,20 40x40",
590 w11->GetWindowBoundsInScreen().ToString());
591 // Transient window's screen bounds stays the same.
592 EXPECT_EQ("50,50 50x50",
593 w1_t1->GetWindowBoundsInScreen().ToString());
594 EXPECT_EQ("1200,70 30x30",
595 w1_t11->GetWindowBoundsInScreen().ToString());
596 EXPECT_EQ("1300,100 80x80",
597 w11_t1->GetWindowBoundsInScreen().ToString());
599 // Transient window doesn't move between root window unless
600 // its transient parent moves.
601 w1_t1->SetBounds(gfx::Rect(10, 50, 50, 50));
602 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
603 EXPECT_EQ("10,50 50x50",
604 w1_t1->GetWindowBoundsInScreen().ToString());
607 // Test if the Window::ConvertPointToTarget works across root windows.
608 // TODO(oshima): Move multiple display suport and this test to aura.
609 TEST_F(ExtendedDesktopTest, ConvertPoint) {
610 if (!SupportsMultipleDisplays())
611 return;
612 gfx::Screen* screen = Shell::GetScreen();
613 UpdateDisplay("1000x600,600x400");
614 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
615 gfx::Display display_1 = screen->GetDisplayNearestWindow(root_windows[0]);
616 EXPECT_EQ("0,0", display_1.bounds().origin().ToString());
617 gfx::Display display_2 = screen->GetDisplayNearestWindow(root_windows[1]);
618 EXPECT_EQ("1000,0", display_2.bounds().origin().ToString());
620 aura::Window* d1 =
621 CreateTestWidget(gfx::Rect(10, 10, 100, 100))->GetNativeView();
622 aura::Window* d2 =
623 CreateTestWidget(gfx::Rect(1020, 20, 100, 100))->GetNativeView();
624 EXPECT_EQ(root_windows[0], d1->GetRootWindow());
625 EXPECT_EQ(root_windows[1], d2->GetRootWindow());
627 // Convert point in the Root2's window to the Root1's window Coord.
628 gfx::Point p(0, 0);
629 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
630 EXPECT_EQ("1000,0", p.ToString());
631 p.SetPoint(0, 0);
632 aura::Window::ConvertPointToTarget(d2, d1, &p);
633 EXPECT_EQ("1010,10", p.ToString());
635 // Convert point in the Root1's window to the Root2's window Coord.
636 p.SetPoint(0, 0);
637 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
638 EXPECT_EQ("-1000,0", p.ToString());
639 p.SetPoint(0, 0);
640 aura::Window::ConvertPointToTarget(d1, d2, &p);
641 EXPECT_EQ("-1010,-10", p.ToString());
643 // Move the 2nd display to the bottom and test again.
644 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
646 display_2 = screen->GetDisplayNearestWindow(root_windows[1]);
647 EXPECT_EQ("0,600", display_2.bounds().origin().ToString());
649 // Convert point in Root2's window to Root1's window Coord.
650 p.SetPoint(0, 0);
651 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
652 EXPECT_EQ("0,600", p.ToString());
653 p.SetPoint(0, 0);
654 aura::Window::ConvertPointToTarget(d2, d1, &p);
655 EXPECT_EQ("10,610", p.ToString());
657 // Convert point in Root1's window to Root2's window Coord.
658 p.SetPoint(0, 0);
659 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
660 EXPECT_EQ("0,-600", p.ToString());
661 p.SetPoint(0, 0);
662 aura::Window::ConvertPointToTarget(d1, d2, &p);
663 EXPECT_EQ("-10,-610", p.ToString());
666 TEST_F(ExtendedDesktopTest, OpenSystemTray) {
667 if (!SupportsMultipleDisplays())
668 return;
670 UpdateDisplay("500x600,600x400");
671 SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
672 ASSERT_FALSE(tray->HasSystemBubble());
674 aura::test::EventGenerator& event_generator(GetEventGenerator());
676 // Opens the tray by a dummy click event and makes sure that adding/removing
677 // displays doesn't break anything.
678 event_generator.MoveMouseToCenterOf(tray->GetWidget()->GetNativeWindow());
679 event_generator.ClickLeftButton();
680 EXPECT_TRUE(tray->HasSystemBubble());
682 UpdateDisplay("500x600");
683 EXPECT_TRUE(tray->HasSystemBubble());
684 UpdateDisplay("500x600,600x400");
685 EXPECT_TRUE(tray->HasSystemBubble());
687 // Closes the tray and again makes sure that adding/removing displays doesn't
688 // break anything.
689 event_generator.ClickLeftButton();
690 RunAllPendingInMessageLoop();
692 EXPECT_FALSE(tray->HasSystemBubble());
694 UpdateDisplay("500x600");
695 EXPECT_FALSE(tray->HasSystemBubble());
696 UpdateDisplay("500x600,600x400");
697 EXPECT_FALSE(tray->HasSystemBubble());
700 TEST_F(ExtendedDesktopTest, StayInSameRootWindow) {
701 if (!SupportsMultipleDisplays())
702 return;
704 UpdateDisplay("100x100,200x200");
705 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
706 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 50, 50));
707 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
708 w1->SetBounds(gfx::Rect(150, 10, 50, 50));
709 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
711 // The widget stays in the same root if kStayInSameRootWindowKey is set to
712 // true.
713 w1->GetNativeView()->SetProperty(internal::kStayInSameRootWindowKey, true);
714 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
715 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
717 // The widget should now move to the 1st root window without the property.
718 w1->GetNativeView()->ClearProperty(internal::kStayInSameRootWindowKey);
719 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
720 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
722 // a window in SettingsBubbleContainer and StatusContainer should
723 // not move to another root window regardles of the bounds specified.
724 aura::Window* settings_bubble_container =
725 Shell::GetPrimaryRootWindowController()->GetContainer(
726 internal::kShellWindowId_SettingBubbleContainer);
727 aura::Window* window = aura::test::CreateTestWindowWithId(
728 100, settings_bubble_container);
729 window->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
730 ScreenUtil::GetSecondaryDisplay());
731 EXPECT_EQ(root_windows[0], window->GetRootWindow());
733 aura::Window* status_container =
734 Shell::GetPrimaryRootWindowController()->GetContainer(
735 internal::kShellWindowId_StatusContainer);
736 window = aura::test::CreateTestWindowWithId(100, status_container);
737 window->SetBoundsInScreen(gfx::Rect(150, 10, 50, 50),
738 ScreenUtil::GetSecondaryDisplay());
739 EXPECT_EQ(root_windows[0], window->GetRootWindow());
742 TEST_F(ExtendedDesktopTest, KeyEventsOnLockScreen) {
743 if (!SupportsMultipleDisplays())
744 return;
746 UpdateDisplay("100x100,200x200");
747 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
749 // Create normal windows on both displays.
750 views::Widget* widget1 = CreateTestWidget(
751 Shell::GetScreen()->GetPrimaryDisplay().bounds());
752 widget1->Show();
753 EXPECT_EQ(root_windows[0], widget1->GetNativeView()->GetRootWindow());
754 views::Widget* widget2 = CreateTestWidget(
755 ScreenUtil::GetSecondaryDisplay().bounds());
756 widget2->Show();
757 EXPECT_EQ(root_windows[1], widget2->GetNativeView()->GetRootWindow());
759 // Create a LockScreen window.
760 views::Widget* lock_widget = CreateTestWidget(
761 Shell::GetScreen()->GetPrimaryDisplay().bounds());
762 views::Textfield* textfield = new views::Textfield;
763 lock_widget->client_view()->AddChildView(textfield);
765 ash::Shell::GetContainer(
766 Shell::GetPrimaryRootWindow(),
767 ash::internal::kShellWindowId_LockScreenContainer)->
768 AddChild(lock_widget->GetNativeView());
769 lock_widget->Show();
770 textfield->RequestFocus();
772 aura::client::FocusClient* focus_client =
773 aura::client::GetFocusClient(root_windows[0]);
774 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
776 // The lock window should get events on both root windows.
777 aura::test::EventGenerator& event_generator(GetEventGenerator());
779 event_generator.set_current_root_window(root_windows[0]->GetDispatcher());
780 event_generator.PressKey(ui::VKEY_A, 0);
781 event_generator.ReleaseKey(ui::VKEY_A, 0);
782 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
783 EXPECT_EQ("a", UTF16ToASCII(textfield->text()));
785 event_generator.set_current_root_window(root_windows[1]->GetDispatcher());
786 event_generator.PressKey(ui::VKEY_B, 0);
787 event_generator.ReleaseKey(ui::VKEY_B, 0);
788 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
789 EXPECT_EQ("ab", UTF16ToASCII(textfield->text()));
791 // Deleting 2nd display. The lock window still should get the events.
792 UpdateDisplay("100x100");
793 event_generator.PressKey(ui::VKEY_C, 0);
794 event_generator.ReleaseKey(ui::VKEY_C, 0);
795 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
796 EXPECT_EQ("abc", UTF16ToASCII(textfield->text()));
798 // Creating 2nd display again, and lock window still should get events
799 // on both root windows.
800 UpdateDisplay("100x100,200x200");
801 root_windows = Shell::GetAllRootWindows();
802 event_generator.set_current_root_window(root_windows[0]->GetDispatcher());
803 event_generator.PressKey(ui::VKEY_D, 0);
804 event_generator.ReleaseKey(ui::VKEY_D, 0);
805 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
806 EXPECT_EQ("abcd", UTF16ToASCII(textfield->text()));
808 event_generator.set_current_root_window(root_windows[1]->GetDispatcher());
809 event_generator.PressKey(ui::VKEY_E, 0);
810 event_generator.ReleaseKey(ui::VKEY_E, 0);
811 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
812 EXPECT_EQ("abcde", UTF16ToASCII(textfield->text()));
815 TEST_F(ExtendedDesktopTest, PassiveGrab) {
816 if (!SupportsMultipleDisplays())
817 return;
819 EventLocationRecordingEventHandler event_handler;
820 ash::Shell::GetInstance()->AddPreTargetHandler(&event_handler);
822 UpdateDisplay("300x300,200x200");
824 views::Widget* widget = CreateTestWidget(gfx::Rect(50, 50, 200, 200));
825 widget->Show();
826 ASSERT_EQ("50,50 200x200", widget->GetWindowBoundsInScreen().ToString());
828 aura::test::EventGenerator& generator(GetEventGenerator());
829 generator.MoveMouseTo(150, 150);
830 EXPECT_EQ("100,100 150,150", event_handler.GetLocationsAndReset());
832 generator.PressLeftButton();
833 generator.MoveMouseTo(400, 150);
835 EXPECT_EQ("350,100 400,150", event_handler.GetLocationsAndReset());
837 generator.ReleaseLeftButton();
838 EXPECT_EQ("-999,-999 -999,-999", event_handler.GetLocationsAndReset());
840 generator.MoveMouseTo(400, 150);
841 EXPECT_EQ("100,150 100,150", event_handler.GetLocationsAndReset());
843 ash::Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
846 } // namespace ash