By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / ash / extended_desktop_unittest.cc
blob952b21fcb35471d30e69b79420c0d3dbf247bc2c
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"
8 #include "ash/shell.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/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/window.h"
25 #include "ui/base/cursor/cursor.h"
26 #include "ui/base/events/event_handler.h"
27 #include "ui/gfx/display.h"
28 #include "ui/gfx/screen.h"
29 #include "ui/views/controls/textfield/textfield.h"
30 #include "ui/views/widget/widget.h"
31 #include "ui/views/widget/widget_delegate.h"
33 namespace ash {
34 namespace {
36 void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
37 DisplayController* display_controller =
38 Shell::GetInstance()->display_controller();
39 DisplayLayout layout = display_controller->default_display_layout();
40 layout.position = position;
41 display_controller->SetDefaultDisplayLayout(layout);
44 class ModalWidgetDelegate : public views::WidgetDelegateView {
45 public:
46 ModalWidgetDelegate() {}
47 virtual ~ModalWidgetDelegate() {}
49 // Overridden from views::WidgetDelegate:
50 virtual views::View* GetContentsView() OVERRIDE {
51 return this;
53 virtual ui::ModalType GetModalType() const OVERRIDE {
54 return ui::MODAL_TYPE_SYSTEM;
57 private:
58 DISALLOW_COPY_AND_ASSIGN(ModalWidgetDelegate);
61 internal::DisplayManager* GetDisplayManager() {
62 return Shell::GetInstance()->display_manager();
65 // An event handler which moves the target window to the secondary root window
66 // at pre-handle phase of a mouse release event.
67 class MoveWindowByClickEventHandler : public ui::EventHandler {
68 public:
69 explicit MoveWindowByClickEventHandler(aura::Window* target)
70 : target_(target) {}
71 virtual ~MoveWindowByClickEventHandler() {}
73 private:
74 // ui::EventHandler overrides:
75 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
76 if (event->type() == ui::ET_MOUSE_RELEASED) {
77 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
78 DCHECK_LT(1u, root_windows.size());
79 root_windows[1]->AddChild(target_);
83 aura::Window* target_;
84 DISALLOW_COPY_AND_ASSIGN(MoveWindowByClickEventHandler);
87 // An event handler which records the event's locations.
88 class EventLocationRecordingEventHandler : public ui::EventHandler {
89 public:
90 explicit EventLocationRecordingEventHandler() {
91 reset();
93 virtual ~EventLocationRecordingEventHandler() {}
95 std::string GetLocationsAndReset() {
96 std::string result =
97 location_.ToString() + " " + root_location_.ToString();
98 reset();
99 return result;
102 private:
103 // ui::EventHandler overrides:
104 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
105 if (event->type() == ui::ET_MOUSE_MOVED ||
106 event->type() == ui::ET_MOUSE_DRAGGED) {
107 location_ = event->location();
108 root_location_ = event->root_location();
112 void reset() {
113 location_.SetPoint(-999, -999);
114 root_location_.SetPoint(-999, -999);
117 gfx::Point root_location_;
118 gfx::Point location_;
120 DISALLOW_COPY_AND_ASSIGN(EventLocationRecordingEventHandler);
123 } // namespace
125 class ExtendedDesktopTest : public test::AshTestBase {
126 public:
127 views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
128 return CreateTestWidgetWithParentAndContext(
129 NULL, CurrentContext(), bounds, false);
132 views::Widget* CreateTestWidgetWithParent(views::Widget* parent,
133 const gfx::Rect& bounds,
134 bool child) {
135 CHECK(parent);
136 return CreateTestWidgetWithParentAndContext(parent, NULL, bounds, child);
139 views::Widget* CreateTestWidgetWithParentAndContext(views::Widget* parent,
140 gfx::NativeView context,
141 const gfx::Rect& bounds,
142 bool child) {
143 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
144 if (parent)
145 params.parent = parent->GetNativeView();
146 params.context = context;
147 params.bounds = bounds;
148 params.child = child;
149 views::Widget* widget = new views::Widget;
150 widget->Init(params);
151 widget->Show();
152 return widget;
156 // Test conditions that root windows in extended desktop mode
157 // must satisfy.
158 TEST_F(ExtendedDesktopTest, Basic) {
159 UpdateDisplay("1000x600,600x400");
160 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
162 // All root windows must have the root window controller.
163 ASSERT_EQ(2U, root_windows.size());
164 for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
165 iter != root_windows.end(); ++iter) {
166 EXPECT_TRUE(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 UpdateDisplay("1000x600,600x400");
179 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
181 views::Widget* widget_on_1st = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
182 views::Widget* widget_on_2nd =
183 CreateTestWidget(gfx::Rect(1200, 10, 100, 100));
184 EXPECT_EQ(root_windows[0], widget_on_1st->GetNativeView()->GetRootWindow());
185 EXPECT_EQ(root_windows[1], widget_on_2nd->GetNativeView()->GetRootWindow());
187 EXPECT_EQ(widget_on_2nd->GetNativeView(),
188 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
189 EXPECT_TRUE(wm::IsActiveWindow(widget_on_2nd->GetNativeView()));
191 aura::test::EventGenerator& event_generator(GetEventGenerator());
192 // Clicking a window changes the active window and active root window.
193 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
194 event_generator.ClickLeftButton();
196 EXPECT_EQ(widget_on_1st->GetNativeView(),
197 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
198 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
200 event_generator.MoveMouseToCenterOf(widget_on_2nd->GetNativeView());
201 event_generator.ClickLeftButton();
203 EXPECT_EQ(widget_on_2nd->GetNativeView(),
204 aura::client::GetFocusClient(root_windows[0])->GetFocusedWindow());
205 EXPECT_TRUE(wm::IsActiveWindow(widget_on_2nd->GetNativeView()));
208 TEST_F(ExtendedDesktopTest, SystemModal) {
209 UpdateDisplay("1000x600,600x400");
210 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
212 views::Widget* widget_on_1st = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
213 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
214 EXPECT_EQ(root_windows[0], widget_on_1st->GetNativeView()->GetRootWindow());
215 EXPECT_EQ(root_windows[0], Shell::GetActiveRootWindow());
217 // Open system modal. Make sure it's on 2nd root window and active.
218 views::Widget* modal_widget = views::Widget::CreateWindowWithContextAndBounds(
219 new ModalWidgetDelegate(),
220 CurrentContext(),
221 gfx::Rect(1200, 100, 100, 100));
222 modal_widget->Show();
223 EXPECT_TRUE(wm::IsActiveWindow(modal_widget->GetNativeView()));
224 EXPECT_EQ(root_windows[1], modal_widget->GetNativeView()->GetRootWindow());
225 EXPECT_EQ(root_windows[1], Shell::GetActiveRootWindow());
227 aura::test::EventGenerator& event_generator(GetEventGenerator());
229 // Clicking a widget on widget_on_1st display should not change activation.
230 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
231 event_generator.ClickLeftButton();
232 EXPECT_TRUE(wm::IsActiveWindow(modal_widget->GetNativeView()));
233 EXPECT_EQ(root_windows[1], Shell::GetActiveRootWindow());
235 // Close system modal and so clicking a widget should work now.
236 modal_widget->Close();
237 event_generator.MoveMouseToCenterOf(widget_on_1st->GetNativeView());
238 event_generator.ClickLeftButton();
239 EXPECT_TRUE(wm::IsActiveWindow(widget_on_1st->GetNativeView()));
240 EXPECT_EQ(root_windows[0], Shell::GetActiveRootWindow());
243 TEST_F(ExtendedDesktopTest, TestCursor) {
244 UpdateDisplay("1000x600,600x400");
245 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
246 EXPECT_EQ(ui::kCursorPointer, root_windows[0]->last_cursor().native_type());
247 EXPECT_EQ(ui::kCursorPointer, root_windows[1]->last_cursor().native_type());
248 Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorCopy);
249 EXPECT_EQ(ui::kCursorCopy, root_windows[0]->last_cursor().native_type());
250 EXPECT_EQ(ui::kCursorCopy, root_windows[1]->last_cursor().native_type());
253 TEST_F(ExtendedDesktopTest, TestCursorLocation) {
254 UpdateDisplay("0+0-1000x600,1001+0-600x400");
255 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
256 aura::Window::TestApi root_window0_test_api(root_windows[0]);
257 aura::Window::TestApi root_window1_test_api(root_windows[1]);
259 root_windows[0]->MoveCursorTo(gfx::Point(10, 10));
260 EXPECT_EQ("10,10", Shell::GetScreen()->GetCursorScreenPoint().ToString());
261 EXPECT_TRUE(root_window0_test_api.ContainsMouse());
262 EXPECT_FALSE(root_window1_test_api.ContainsMouse());
263 root_windows[1]->MoveCursorTo(gfx::Point(10, 20));
264 EXPECT_EQ("1010,20", Shell::GetScreen()->GetCursorScreenPoint().ToString());
265 EXPECT_FALSE(root_window0_test_api.ContainsMouse());
266 EXPECT_TRUE(root_window1_test_api.ContainsMouse());
267 root_windows[0]->MoveCursorTo(gfx::Point(20, 10));
268 EXPECT_EQ("20,10", Shell::GetScreen()->GetCursorScreenPoint().ToString());
269 EXPECT_TRUE(root_window0_test_api.ContainsMouse());
270 EXPECT_FALSE(root_window1_test_api.ContainsMouse());
273 TEST_F(ExtendedDesktopTest, CycleWindows) {
274 UpdateDisplay("700x500,500x500");
275 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
277 WindowCycleController* controller =
278 Shell::GetInstance()->window_cycle_controller();
280 views::Widget* d1_w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
281 EXPECT_EQ(root_windows[0], d1_w1->GetNativeView()->GetRootWindow());
282 views::Widget* d2_w1 = CreateTestWidget(gfx::Rect(800, 10, 100, 100));
283 EXPECT_EQ(root_windows[1], d2_w1->GetNativeView()->GetRootWindow());
284 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
286 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
287 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
288 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
289 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
290 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
291 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
292 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
293 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
295 // Cycle through all windows across root windows.
296 views::Widget* d1_w2 = CreateTestWidget(gfx::Rect(10, 200, 100, 100));
297 EXPECT_EQ(root_windows[0], d1_w2->GetNativeView()->GetRootWindow());
298 views::Widget* d2_w2 = CreateTestWidget(gfx::Rect(800, 200, 100, 100));
299 EXPECT_EQ(root_windows[1], d2_w2->GetNativeView()->GetRootWindow());
301 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
302 EXPECT_TRUE(wm::IsActiveWindow(d1_w2->GetNativeView()));
303 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
304 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
305 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
306 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
307 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
308 EXPECT_TRUE(wm::IsActiveWindow(d2_w2->GetNativeView()));
310 // Backwards
311 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
312 EXPECT_TRUE(wm::IsActiveWindow(d1_w1->GetNativeView()));
313 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
314 EXPECT_TRUE(wm::IsActiveWindow(d2_w1->GetNativeView()));
315 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
316 EXPECT_TRUE(wm::IsActiveWindow(d1_w2->GetNativeView()));
317 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
318 EXPECT_TRUE(wm::IsActiveWindow(d2_w2->GetNativeView()));
321 TEST_F(ExtendedDesktopTest, GetRootWindowAt) {
322 UpdateDisplay("700x500,500x500");
323 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
324 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
326 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-400, 100)));
327 EXPECT_EQ(root_windows[1], wm::GetRootWindowAt(gfx::Point(-1, 100)));
328 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(0, 300)));
329 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(700,300)));
331 // Zero origin.
332 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(0, 0)));
334 // Out of range point should return the primary root window
335 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(-600, 0)));
336 EXPECT_EQ(root_windows[0], wm::GetRootWindowAt(gfx::Point(701, 100)));
339 TEST_F(ExtendedDesktopTest, GetRootWindowMatching) {
340 UpdateDisplay("700x500,500x500");
341 SetSecondaryDisplayLayout(DisplayLayout::LEFT);
343 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
345 // Containing rect.
346 EXPECT_EQ(root_windows[1],
347 wm::GetRootWindowMatching(gfx::Rect(-300, 10, 50, 50)));
348 EXPECT_EQ(root_windows[0],
349 wm::GetRootWindowMatching(gfx::Rect(100, 10, 50, 50)));
351 // Intersecting rect.
352 EXPECT_EQ(root_windows[1],
353 wm::GetRootWindowMatching(gfx::Rect(-200, 0, 300, 300)));
354 EXPECT_EQ(root_windows[0],
355 wm::GetRootWindowMatching(gfx::Rect(-100, 0, 300, 300)));
357 // Zero origin.
358 EXPECT_EQ(root_windows[0],
359 wm::GetRootWindowMatching(gfx::Rect(0, 0, 0, 0)));
360 EXPECT_EQ(root_windows[0],
361 wm::GetRootWindowMatching(gfx::Rect(0, 0, 1, 1)));
363 // Empty rect.
364 EXPECT_EQ(root_windows[1],
365 wm::GetRootWindowMatching(gfx::Rect(-400, 100, 0, 0)));
366 EXPECT_EQ(root_windows[0],
367 wm::GetRootWindowMatching(gfx::Rect(100, 100, 0, 0)));
369 // Out of range rect should return the primary root window.
370 EXPECT_EQ(root_windows[0],
371 wm::GetRootWindowMatching(gfx::Rect(-600, -300, 50, 50)));
372 EXPECT_EQ(root_windows[0],
373 wm::GetRootWindowMatching(gfx::Rect(0, 1000, 50, 50)));
376 TEST_F(ExtendedDesktopTest, Capture) {
377 UpdateDisplay("1000x600,600x400");
378 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
380 aura::test::EventCountDelegate r1_d1;
381 aura::test::EventCountDelegate r1_d2;
382 aura::test::EventCountDelegate r2_d1;
384 scoped_ptr<aura::Window> r1_w1(aura::test::CreateTestWindowWithDelegate(
385 &r1_d1, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
386 scoped_ptr<aura::Window> r1_w2(aura::test::CreateTestWindowWithDelegate(
387 &r1_d2, 0, gfx::Rect(10, 100, 100, 100), root_windows[0]));
388 scoped_ptr<aura::Window> r2_w1(aura::test::CreateTestWindowWithDelegate(
389 &r2_d1, 0, gfx::Rect(10, 10, 100, 100), root_windows[1]));
391 r1_w1->SetCapture();
393 EXPECT_EQ(r1_w1.get(),
394 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
396 aura::test::EventGenerator generator2(root_windows[1]);
397 generator2.MoveMouseToCenterOf(r2_w1.get());
398 generator2.ClickLeftButton();
399 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
400 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
401 // The mouse is outside. On chromeos, the mouse is warped to the
402 // dest root window, but it's not implemented on Win yet, so
403 // no mouse move event on Win.
404 EXPECT_EQ("1 1 0", r1_d1.GetMouseMotionCountsAndReset());
405 EXPECT_EQ("1 1", r1_d1.GetMouseButtonCountsAndReset());
406 // Emulate passive grab. (15,15) on 1st display is (-985,15) on 2nd
407 // display.
408 generator2.MoveMouseTo(-985, 15);
409 EXPECT_EQ("0 1 0", r1_d1.GetMouseMotionCountsAndReset());
411 r1_w2->SetCapture();
412 EXPECT_EQ(r1_w2.get(),
413 aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
414 generator2.MoveMouseBy(10, 10);
415 generator2.ClickLeftButton();
416 EXPECT_EQ("0 0 0", r2_d1.GetMouseMotionCountsAndReset());
417 EXPECT_EQ("0 0", r2_d1.GetMouseButtonCountsAndReset());
418 // mouse is already entered.
419 EXPECT_EQ("0 1 0", r1_d2.GetMouseMotionCountsAndReset());
420 EXPECT_EQ("1 1", r1_d2.GetMouseButtonCountsAndReset());
421 r1_w2->ReleaseCapture();
422 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(r2_w1->GetRootWindow()));
423 generator2.MoveMouseTo(15, 15);
424 generator2.ClickLeftButton();
425 EXPECT_EQ("1 1 0", r2_d1.GetMouseMotionCountsAndReset());
426 EXPECT_EQ("1 1", r2_d1.GetMouseButtonCountsAndReset());
427 // Make sure the mouse_moved_handler_ is properly reset.
428 EXPECT_EQ("0 0 0", r1_d2.GetMouseMotionCountsAndReset());
429 EXPECT_EQ("0 0", r1_d2.GetMouseButtonCountsAndReset());
432 TEST_F(ExtendedDesktopTest, MoveWindow) {
433 UpdateDisplay("1000x600,600x400");
434 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
435 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
437 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
439 d1->SetBounds(gfx::Rect(1010, 10, 100, 100));
440 EXPECT_EQ("1010,10 100x100",
441 d1->GetWindowBoundsInScreen().ToString());
443 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
445 d1->SetBounds(gfx::Rect(10, 10, 100, 100));
446 EXPECT_EQ("10,10 100x100",
447 d1->GetWindowBoundsInScreen().ToString());
449 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
451 // Make sure the bounds which doesn't fit to the root window
452 // works correctly.
453 d1->SetBounds(gfx::Rect(1560, 30, 100, 100));
454 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
455 EXPECT_EQ("1560,30 100x100",
456 d1->GetWindowBoundsInScreen().ToString());
458 // Setting outside of root windows will be moved to primary root window.
459 // TODO(oshima): This one probably should pick the closest root window.
460 d1->SetBounds(gfx::Rect(200, 10, 100, 100));
461 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
464 // Verifies if the mouse event arrives to the window even when the window
465 // moves to another root in a pre-target handler. See: crbug.com/157583
466 TEST_F(ExtendedDesktopTest, MoveWindowByMouseClick) {
467 UpdateDisplay("1000x600,600x400");
469 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
470 aura::test::EventCountDelegate delegate;
471 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
472 &delegate, 0, gfx::Rect(10, 10, 100, 100), root_windows[0]));
473 MoveWindowByClickEventHandler event_handler(window.get());
474 window->AddPreTargetHandler(&event_handler);
476 aura::test::EventGenerator& event_generator(GetEventGenerator());
478 event_generator.MoveMouseToCenterOf(window.get());
479 event_generator.ClickLeftButton();
480 // Both mouse pressed and released arrive at the window and its delegate.
481 EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
482 // Also event_handler moves the window to another root at mouse release.
483 EXPECT_EQ(root_windows[1], window->GetRootWindow());
486 TEST_F(ExtendedDesktopTest, MoveWindowToDisplay) {
487 UpdateDisplay("1000x1000,1000x1000");
488 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
490 gfx::Display display0 = Shell::GetScreen()->GetDisplayMatching(
491 root_windows[0]->GetBoundsInScreen());
492 gfx::Display display1 = Shell::GetScreen()->GetDisplayMatching(
493 root_windows[1]->GetBoundsInScreen());
494 EXPECT_NE(display0.id(), display1.id());
496 views::Widget* d1 = CreateTestWidget(gfx::Rect(10, 10, 1000, 100));
497 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
499 // Move the window where the window spans both root windows. Since the second
500 // parameter is |display1|, the window should be shown on the secondary root.
501 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
502 display1);
503 EXPECT_EQ("500,10 1000x100",
504 d1->GetWindowBoundsInScreen().ToString());
505 EXPECT_EQ(root_windows[1], d1->GetNativeView()->GetRootWindow());
507 // Move to the primary root.
508 d1->GetNativeWindow()->SetBoundsInScreen(gfx::Rect(500, 10, 1000, 100),
509 display0);
510 EXPECT_EQ("500,10 1000x100",
511 d1->GetWindowBoundsInScreen().ToString());
512 EXPECT_EQ(root_windows[0], d1->GetNativeView()->GetRootWindow());
515 TEST_F(ExtendedDesktopTest, MoveWindowWithTransient) {
516 UpdateDisplay("1000x600,600x400");
517 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
518 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 100, 100));
519 views::Widget* w1_t1 = CreateTestWidgetWithParent(
520 w1, gfx::Rect(50, 50, 50, 50), false /* transient */);
521 // Transient child of the transient child.
522 views::Widget* w1_t11 = CreateTestWidgetWithParent(
523 w1_t1, gfx::Rect(1200, 70, 30, 30), false /* transient */);
525 views::Widget* w11 = CreateTestWidgetWithParent(
526 w1, gfx::Rect(10, 10, 40, 40), true /* child */);
527 views::Widget* w11_t1 = CreateTestWidgetWithParent(
528 w1, gfx::Rect(1300, 100, 80, 80), false /* transient */);
530 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
531 EXPECT_EQ(root_windows[0], w11->GetNativeView()->GetRootWindow());
532 EXPECT_EQ(root_windows[0], w1_t1->GetNativeView()->GetRootWindow());
533 EXPECT_EQ(root_windows[0], w1_t11->GetNativeView()->GetRootWindow());
534 EXPECT_EQ(root_windows[0], w11_t1->GetNativeView()->GetRootWindow());
535 EXPECT_EQ("50,50 50x50",
536 w1_t1->GetWindowBoundsInScreen().ToString());
537 EXPECT_EQ("1200,70 30x30",
538 w1_t11->GetWindowBoundsInScreen().ToString());
539 EXPECT_EQ("20,20 40x40",
540 w11->GetWindowBoundsInScreen().ToString());
541 EXPECT_EQ("1300,100 80x80",
542 w11_t1->GetWindowBoundsInScreen().ToString());
544 w1->SetBounds(gfx::Rect(1100,10,100,100));
546 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
547 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
548 EXPECT_EQ(root_windows[1], w1_t11->GetNativeView()->GetRootWindow());
549 EXPECT_EQ(root_windows[1], w11->GetNativeView()->GetRootWindow());
550 EXPECT_EQ(root_windows[1], w11_t1->GetNativeView()->GetRootWindow());
552 EXPECT_EQ("1110,20 40x40",
553 w11->GetWindowBoundsInScreen().ToString());
554 // Transient window's screen bounds stays the same.
555 EXPECT_EQ("50,50 50x50",
556 w1_t1->GetWindowBoundsInScreen().ToString());
557 EXPECT_EQ("1200,70 30x30",
558 w1_t11->GetWindowBoundsInScreen().ToString());
559 EXPECT_EQ("1300,100 80x80",
560 w11_t1->GetWindowBoundsInScreen().ToString());
562 // Transient window doesn't move between root window unless
563 // its transient parent moves.
564 w1_t1->SetBounds(gfx::Rect(10, 50, 50, 50));
565 EXPECT_EQ(root_windows[1], w1_t1->GetNativeView()->GetRootWindow());
566 EXPECT_EQ("10,50 50x50",
567 w1_t1->GetWindowBoundsInScreen().ToString());
570 namespace internal {
571 // Test if the Window::ConvertPointToTarget works across root windows.
572 // TODO(oshima): Move multiple display suport and this test to aura.
573 TEST_F(ExtendedDesktopTest, ConvertPoint) {
574 UpdateDisplay("1000x600,600x400");
575 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
576 gfx::Display& display_1 =
577 GetDisplayManager()->FindDisplayForRootWindow(root_windows[0]);
578 EXPECT_EQ("0,0", display_1.bounds().origin().ToString());
579 gfx::Display& display_2 =
580 GetDisplayManager()->FindDisplayForRootWindow(root_windows[1]);
581 EXPECT_EQ("1000,0", display_2.bounds().origin().ToString());
583 aura::Window* d1 =
584 CreateTestWidget(gfx::Rect(10, 10, 100, 100))->GetNativeView();
585 aura::Window* d2 =
586 CreateTestWidget(gfx::Rect(1020, 20, 100, 100))->GetNativeView();
587 EXPECT_EQ(root_windows[0], d1->GetRootWindow());
588 EXPECT_EQ(root_windows[1], d2->GetRootWindow());
590 // Convert point in the Root2's window to the Root1's window Coord.
591 gfx::Point p(0, 0);
592 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
593 EXPECT_EQ("1000,0", p.ToString());
594 p.SetPoint(0, 0);
595 aura::Window::ConvertPointToTarget(d2, d1, &p);
596 EXPECT_EQ("1010,10", p.ToString());
598 // Convert point in the Root1's window to the Root2's window Coord.
599 p.SetPoint(0, 0);
600 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
601 EXPECT_EQ("-1000,0", p.ToString());
602 p.SetPoint(0, 0);
603 aura::Window::ConvertPointToTarget(d1, d2, &p);
604 EXPECT_EQ("-1010,-10", p.ToString());
606 // Move the 2nd display to the bottom and test again.
607 SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
609 display_2 = GetDisplayManager()->FindDisplayForRootWindow(root_windows[1]);
610 EXPECT_EQ("0,600", display_2.bounds().origin().ToString());
612 // Convert point in Root2's window to Root1's window Coord.
613 p.SetPoint(0, 0);
614 aura::Window::ConvertPointToTarget(root_windows[1], root_windows[0], &p);
615 EXPECT_EQ("0,600", p.ToString());
616 p.SetPoint(0, 0);
617 aura::Window::ConvertPointToTarget(d2, d1, &p);
618 EXPECT_EQ("10,610", p.ToString());
620 // Convert point in Root1's window to Root2's window Coord.
621 p.SetPoint(0, 0);
622 aura::Window::ConvertPointToTarget(root_windows[0], root_windows[1], &p);
623 EXPECT_EQ("0,-600", p.ToString());
624 p.SetPoint(0, 0);
625 aura::Window::ConvertPointToTarget(d1, d2, &p);
626 EXPECT_EQ("-10,-610", p.ToString());
629 TEST_F(ExtendedDesktopTest, OpenSystemTray) {
630 UpdateDisplay("500x600,600x400");
631 SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
632 ASSERT_FALSE(tray->HasSystemBubble());
634 aura::test::EventGenerator& event_generator(GetEventGenerator());
636 // Opens the tray by a dummy click event and makes sure that adding/removing
637 // displays doesn't break anything.
638 event_generator.MoveMouseToCenterOf(tray->GetWidget()->GetNativeWindow());
639 event_generator.ClickLeftButton();
640 EXPECT_TRUE(tray->HasSystemBubble());
642 UpdateDisplay("500x600");
643 EXPECT_TRUE(tray->HasSystemBubble());
644 UpdateDisplay("500x600,600x400");
645 EXPECT_TRUE(tray->HasSystemBubble());
647 // Closes the tray and again makes sure that adding/removing displays doesn't
648 // break anything.
649 event_generator.ClickLeftButton();
650 RunAllPendingInMessageLoop();
652 EXPECT_FALSE(tray->HasSystemBubble());
654 UpdateDisplay("500x600");
655 EXPECT_FALSE(tray->HasSystemBubble());
656 UpdateDisplay("500x600,600x400");
657 EXPECT_FALSE(tray->HasSystemBubble());
660 TEST_F(ExtendedDesktopTest, StayInSameRootWindow) {
661 UpdateDisplay("100x100,200x200");
662 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
663 views::Widget* w1 = CreateTestWidget(gfx::Rect(10, 10, 50, 50));
664 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
665 w1->SetBounds(gfx::Rect(150, 10, 50, 50));
666 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
668 // The widget stays in the same root if kStayInSameRootWindowKey is set to
669 // true.
670 w1->GetNativeView()->SetProperty(internal::kStayInSameRootWindowKey, true);
671 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
672 EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
674 // The widget should now move to the 1st root window without the property.
675 w1->GetNativeView()->ClearProperty(internal::kStayInSameRootWindowKey);
676 w1->SetBounds(gfx::Rect(10, 10, 50, 50));
677 EXPECT_EQ(root_windows[0], w1->GetNativeView()->GetRootWindow());
680 TEST_F(ExtendedDesktopTest, KeyEventsOnLockScreen) {
681 UpdateDisplay("100x100,200x200");
682 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
684 // Create normal windows on both displays.
685 views::Widget* widget1 = CreateTestWidget(
686 Shell::GetScreen()->GetPrimaryDisplay().bounds());
687 widget1->Show();
688 EXPECT_EQ(root_windows[0], widget1->GetNativeView()->GetRootWindow());
689 views::Widget* widget2 = CreateTestWidget(
690 ScreenAsh::GetSecondaryDisplay().bounds());
691 widget2->Show();
692 EXPECT_EQ(root_windows[1], widget2->GetNativeView()->GetRootWindow());
694 // Create a LockScreen window.
695 views::Widget* lock_widget = CreateTestWidget(
696 Shell::GetScreen()->GetPrimaryDisplay().bounds());
697 views::Textfield* textfield = new views::Textfield;
698 lock_widget->SetContentsView(textfield);
700 ash::Shell::GetContainer(
701 Shell::GetPrimaryRootWindow(),
702 ash::internal::kShellWindowId_LockScreenContainer)->
703 AddChild(lock_widget->GetNativeView());
704 lock_widget->Show();
705 textfield->RequestFocus();
707 aura::client::FocusClient* focus_client =
708 aura::client::GetFocusClient(root_windows[0]);
709 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
711 // The lock window should get events on both root windows.
712 aura::test::EventGenerator& event_generator(GetEventGenerator());
714 event_generator.set_current_root_window(root_windows[0]);
715 event_generator.PressKey(ui::VKEY_A, 0);
716 event_generator.ReleaseKey(ui::VKEY_A, 0);
717 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
718 EXPECT_EQ("a", UTF16ToASCII(textfield->text()));
720 event_generator.set_current_root_window(root_windows[1]);
721 event_generator.PressKey(ui::VKEY_B, 0);
722 event_generator.ReleaseKey(ui::VKEY_B, 0);
723 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
724 EXPECT_EQ("ab", UTF16ToASCII(textfield->text()));
726 // Deleting 2nd display. The lock window still should get the events.
727 UpdateDisplay("100x100");
728 event_generator.PressKey(ui::VKEY_C, 0);
729 event_generator.ReleaseKey(ui::VKEY_C, 0);
730 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
731 EXPECT_EQ("abc", UTF16ToASCII(textfield->text()));
733 // Creating 2nd display again, and lock window still should get events
734 // on both root windows.
735 UpdateDisplay("100x100,200x200");
736 root_windows = Shell::GetAllRootWindows();
737 event_generator.set_current_root_window(root_windows[0]);
738 event_generator.PressKey(ui::VKEY_D, 0);
739 event_generator.ReleaseKey(ui::VKEY_D, 0);
740 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
741 EXPECT_EQ("abcd", UTF16ToASCII(textfield->text()));
743 event_generator.set_current_root_window(root_windows[1]);
744 event_generator.PressKey(ui::VKEY_E, 0);
745 event_generator.ReleaseKey(ui::VKEY_E, 0);
746 EXPECT_EQ(lock_widget->GetNativeView(), focus_client->GetFocusedWindow());
747 EXPECT_EQ("abcde", UTF16ToASCII(textfield->text()));
750 TEST_F(ExtendedDesktopTest, PassiveGrab) {
751 EventLocationRecordingEventHandler event_handler;
752 ash::Shell::GetInstance()->AddPreTargetHandler(&event_handler);
754 UpdateDisplay("300x300,200x200");
756 views::Widget* widget = CreateTestWidget(gfx::Rect(50, 50, 200, 200));
757 widget->Show();
758 ASSERT_EQ("50,50 200x200", widget->GetWindowBoundsInScreen().ToString());
760 aura::test::EventGenerator& generator(GetEventGenerator());
761 generator.MoveMouseTo(150, 150);
762 EXPECT_EQ("100,100 150,150", event_handler.GetLocationsAndReset());
764 generator.PressLeftButton();
765 generator.MoveMouseTo(400, 150);
767 EXPECT_EQ("350,100 400,150", event_handler.GetLocationsAndReset());
769 generator.ReleaseLeftButton();
770 EXPECT_EQ("-999,-999 -999,-999", event_handler.GetLocationsAndReset());
772 generator.MoveMouseTo(400, 150);
773 EXPECT_EQ("100,150 100,150", event_handler.GetLocationsAndReset());
775 ash::Shell::GetInstance()->RemovePreTargetHandler(&event_handler);
778 } // namespace internal
779 } // namespace ash