Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / window_sizer / window_sizer_ash_uitest.cc
blob51a71ab4a8e60cafae128e46a2d607dff731acbb
1 // Copyright 2013 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/shelf/shelf.h"
6 #include "ash/shelf/shelf_view.h"
7 #include "ash/shell.h"
8 #include "ash/test/shelf_test_api.h"
9 #include "base/command_line.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop.h"
12 #include "chrome/browser/lifetime/application_lifetime.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/base/test/ui_controls.h"
19 #include "ui/events/test/event_generator.h"
20 #include "ui/views/controls/menu/menu_controller.h"
21 #include "ui/views/view.h"
22 #include "ui/views/view_model.h"
24 namespace {
26 class WindowSizerTest : public InProcessBrowserTest {
27 public:
28 WindowSizerTest() {}
29 ~WindowSizerTest() override {}
31 void SetUpCommandLine(base::CommandLine* command_line) override {
32 InProcessBrowserTest::SetUpCommandLine(command_line);
33 // Make screens sufficiently wide to host 2 browsers side by side.
34 command_line->AppendSwitchASCII("ash-host-window-bounds",
35 "600x600,601+0-600x600");
38 private:
39 DISALLOW_COPY_AND_ASSIGN(WindowSizerTest);
42 void CloseBrowser(Browser* browser) {
43 browser->window()->Close();
44 base::MessageLoop::current()->RunUntilIdle();
47 gfx::Rect GetChromeIconBoundsForRootWindow(aura::Window* root_window) {
48 ash::Shelf* shelf = ash::Shelf::ForWindow(root_window);
49 const ash::ShelfView* shelf_view =
50 ash::test::ShelfTestAPI(shelf).shelf_view();
51 const views::ViewModel* view_model = shelf_view->view_model_for_test();
53 EXPECT_EQ(2, view_model->view_size());
54 return view_model->view_at(1)->GetBoundsInScreen();
57 void OpenBrowserUsingShelfOnRootWindow(aura::Window* root_window) {
58 ui::test::EventGenerator generator(root_window);
59 gfx::Point center =
60 GetChromeIconBoundsForRootWindow(root_window).CenterPoint();
61 gfx::Display display =
62 ash::Shell::GetScreen()->GetDisplayNearestWindow(root_window);
63 const gfx::Point& origin = display.bounds().origin();
64 center.Offset(- origin.x(), - origin.y());
65 generator.MoveMouseTo(center);
66 generator.ClickLeftButton();
69 } // namespace
71 #if !defined(OS_CHROMEOS)
72 #define MAYBE_OpenBrowserUsingShelfOnOtherDisplay DISABLED_OpenBrowserUsingShelfOnOtherDisplay
73 #define MAYBE_OpenBrowserUsingContextMenuOnOtherDisplay DISABLED_OpenBrowserUsingContextMenuOnOtherDisplay
74 #else
75 #define MAYBE_OpenBrowserUsingShelfOnOtherDisplay OpenBrowserUsingShelfOnOtherDisplay
76 #define MAYBE_OpenBrowserUsingContextMenuOnOtherDisplay OpenBrowserUsingContextMenuOnOtherDisplay
77 #endif
79 IN_PROC_BROWSER_TEST_F(WindowSizerTest,
80 MAYBE_OpenBrowserUsingShelfOnOtherDisplay) {
81 // Don't shutdown when closing the last browser window.
82 chrome::IncrementKeepAliveCount();
84 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();
86 BrowserList* browser_list =
87 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
89 EXPECT_EQ(1u, browser_list->size());
90 // Close the browser window so that clicking icon will create a new window.
91 CloseBrowser(browser_list->get(0));
92 EXPECT_EQ(0u, browser_list->size());
93 EXPECT_EQ(root_windows[0], ash::Shell::GetTargetRootWindow());
95 OpenBrowserUsingShelfOnRootWindow(root_windows[1]);
97 // A new browser must be created on 2nd display.
98 EXPECT_EQ(1u, browser_list->size());
99 EXPECT_EQ(root_windows[1],
100 browser_list->get(0)->window()->GetNativeWindow()->GetRootWindow());
101 EXPECT_EQ(root_windows[1], ash::Shell::GetTargetRootWindow());
103 // Close the browser window so that clicking icon will create a new window.
104 CloseBrowser(browser_list->get(0));
105 EXPECT_EQ(0u, browser_list->size());
107 OpenBrowserUsingShelfOnRootWindow(root_windows[0]);
109 // A new browser must be created on 1st display.
110 EXPECT_EQ(1u, browser_list->size());
111 EXPECT_EQ(root_windows[0],
112 browser_list->get(0)->window()->GetNativeWindow()->GetRootWindow());
113 EXPECT_EQ(root_windows[0], ash::Shell::GetTargetRootWindow());
115 // Balanced with the chrome::IncrementKeepAliveCount above.
116 chrome::DecrementKeepAliveCount();
119 namespace {
121 class WindowSizerContextMenuTest : public WindowSizerTest {
122 public:
123 WindowSizerContextMenuTest() {}
124 ~WindowSizerContextMenuTest() override {}
126 static void Step1(gfx::Point release_point) {
127 ui_controls::SendMouseEventsNotifyWhenDone(
128 ui_controls::RIGHT, ui_controls::DOWN,
129 base::Bind(&WindowSizerContextMenuTest::Step2, release_point));
132 static void Step2(gfx::Point release_point) {
133 ui_controls::SendMouseMoveNotifyWhenDone(
134 release_point.x(), release_point.y(),
135 base::Bind(&WindowSizerContextMenuTest::Step3));
138 static void Step3() {
139 ui_controls::SendMouseEventsNotifyWhenDone(
140 ui_controls::RIGHT, ui_controls::UP,
141 base::Bind(&WindowSizerContextMenuTest::QuitLoop));
144 static void QuitLoop() {
145 base::MessageLoop::current()->task_runner()->PostTask(
146 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
149 private:
150 DISALLOW_COPY_AND_ASSIGN(WindowSizerContextMenuTest);
153 void OpenBrowserUsingContextMenuOnRootWindow(aura::Window* root_window) {
154 gfx::Point chrome_icon =
155 GetChromeIconBoundsForRootWindow(root_window).CenterPoint();
156 gfx::Point release_point = chrome_icon;
157 release_point.Offset(50, -120);
158 ui_controls::SendMouseMoveNotifyWhenDone(
159 chrome_icon.x(), chrome_icon.y(),
160 base::Bind(&WindowSizerContextMenuTest::Step1, release_point));
161 base::MessageLoop::current()->Run();
164 } // namespace
166 IN_PROC_BROWSER_TEST_F(WindowSizerContextMenuTest,
167 MAYBE_OpenBrowserUsingContextMenuOnOtherDisplay) {
168 // Don't shutdown when closing the last browser window.
169 chrome::IncrementKeepAliveCount();
171 views::MenuController::TurnOffMenuSelectionHoldForTest();
173 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();
175 BrowserList* browser_list =
176 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
178 ASSERT_EQ(1u, browser_list->size());
179 EXPECT_EQ(root_windows[0], ash::Shell::GetTargetRootWindow());
180 CloseBrowser(browser_list->get(0));
182 OpenBrowserUsingContextMenuOnRootWindow(root_windows[1]);
184 // A new browser must be created on 2nd display.
185 ASSERT_EQ(1u, browser_list->size());
186 EXPECT_EQ(root_windows[1],
187 browser_list->get(0)->window()->GetNativeWindow()->GetRootWindow());
188 EXPECT_EQ(root_windows[1], ash::Shell::GetTargetRootWindow());
190 OpenBrowserUsingContextMenuOnRootWindow(root_windows[0]);
192 // Next new browser must be created on 1st display.
193 ASSERT_EQ(2u, browser_list->size());
194 EXPECT_EQ(root_windows[0],
195 browser_list->get(1)->window()->GetNativeWindow()->GetRootWindow());
196 EXPECT_EQ(root_windows[0], ash::Shell::GetTargetRootWindow());
198 // Balanced with the chrome::IncrementKeepAliveCount above.
199 chrome::DecrementKeepAliveCount();