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/wm/workspace/workspace_event_handler.h"
7 #include "ash/screen_ash.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/wm/window_state.h"
11 #include "ash/wm/window_util.h"
12 #include "ash/wm/workspace_controller.h"
13 #include "ash/wm/workspace_controller_test_helper.h"
14 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/root_window.h"
16 #include "ui/aura/test/event_generator.h"
17 #include "ui/aura/test/test_window_delegate.h"
18 #include "ui/aura/window.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/gfx/screen.h"
23 #include "base/win/windows_version.h"
29 class WorkspaceEventHandlerTest : public test::AshTestBase {
31 WorkspaceEventHandlerTest() {}
32 virtual ~WorkspaceEventHandlerTest() {}
35 aura::Window* CreateTestWindow(aura::WindowDelegate* delegate,
36 const gfx::Rect& bounds) {
37 aura::Window* window = new aura::Window(delegate);
38 window->SetType(aura::client::WINDOW_TYPE_NORMAL);
39 window->Init(ui::LAYER_TEXTURED);
40 ParentWindowInPrimaryRootWindow(window);
41 window->SetBounds(bounds);
47 DISALLOW_COPY_AND_ASSIGN(WorkspaceEventHandlerTest);
50 // Keeps track of the properties changed of a particular window.
51 class WindowPropertyObserver : public aura::WindowObserver {
53 explicit WindowPropertyObserver(aura::Window* window)
55 window->AddObserver(this);
58 virtual ~WindowPropertyObserver() {
59 window_->RemoveObserver(this);
62 bool DidPropertyChange(const void* property) const {
63 return std::find(properties_changed_.begin(),
64 properties_changed_.end(),
65 property) != properties_changed_.end();
69 virtual void OnWindowPropertyChanged(aura::Window* window,
71 intptr_t old) OVERRIDE {
72 properties_changed_.push_back(key);
75 aura::Window* window_;
76 std::vector<const void*> properties_changed_;
78 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver);
81 TEST_F(WorkspaceEventHandlerTest, DoubleClickSingleAxisResizeEdge) {
82 // Double clicking the vertical resize edge of a window should maximize it
84 gfx::Rect restored_bounds(10, 10, 50, 50);
85 aura::test::TestWindowDelegate wd;
86 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
88 wm::ActivateWindow(window.get());
90 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
91 window.get()).work_area();
93 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
96 // Double-click the top resize edge.
97 wd.set_window_component(HTTOP);
98 // On X a double click actually generates a drag between each press/release.
99 // Explicitly trigger this path since we had bugs in dealing with it
101 generator.PressLeftButton();
102 generator.ReleaseLeftButton();
103 generator.set_flags(ui::EF_IS_DOUBLE_CLICK);
104 generator.PressLeftButton();
105 generator.MoveMouseTo(generator.current_location(), 1);
106 generator.ReleaseLeftButton();
107 gfx::Rect bounds_in_screen = window->GetBoundsInScreen();
108 EXPECT_EQ(restored_bounds.x(), bounds_in_screen.x());
109 EXPECT_EQ(restored_bounds.width(), bounds_in_screen.width());
110 EXPECT_EQ(work_area.y(), bounds_in_screen.y());
111 EXPECT_EQ(work_area.height(), bounds_in_screen.height());
113 wm::WindowState* window_state = wm::GetWindowState(window.get());
114 // Single-axis maximization is not considered real maximization.
115 EXPECT_FALSE(window_state->IsMaximized());
118 generator.DoubleClickLeftButton();
119 bounds_in_screen = window->GetBoundsInScreen();
120 EXPECT_EQ(restored_bounds.ToString(), bounds_in_screen.ToString());
121 // Note that it should not even be restored at this point, it should have
122 // also cleared the restore rectangle.
123 EXPECT_FALSE(window_state->HasRestoreBounds());
125 // Double-click the top resize edge again to maximize vertically, then double
126 // click again to restore.
127 generator.DoubleClickLeftButton();
128 wd.set_window_component(HTCAPTION);
129 generator.DoubleClickLeftButton();
130 EXPECT_FALSE(window_state->IsMaximized());
131 bounds_in_screen = window->GetBoundsInScreen();
132 EXPECT_EQ(restored_bounds.ToString(), bounds_in_screen.ToString());
134 // Double clicking the left resize edge should maximize horizontally.
135 wd.set_window_component(HTLEFT);
136 generator.DoubleClickLeftButton();
137 bounds_in_screen = window->GetBoundsInScreen();
138 EXPECT_EQ(restored_bounds.y(), bounds_in_screen.y());
139 EXPECT_EQ(restored_bounds.height(), bounds_in_screen.height());
140 EXPECT_EQ(work_area.x(), bounds_in_screen.x());
141 EXPECT_EQ(work_area.width(), bounds_in_screen.width());
142 // Single-axis maximization is not considered real maximization.
143 EXPECT_FALSE(window_state->IsMaximized());
146 wd.set_window_component(HTCAPTION);
147 generator.DoubleClickLeftButton();
148 EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
151 // Multi display test does not run on Win8 bot. crbug.com/247427.
152 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
156 // Verify the double clicking the resize edge works on 2nd display too.
157 UpdateDisplay("200x200,400x300");
158 gfx::Rect work_area2 = ScreenAsh::GetSecondaryDisplay().work_area();
159 restored_bounds.SetRect(220,20, 50, 50);
160 window->SetBoundsInScreen(restored_bounds, ScreenAsh::GetSecondaryDisplay());
161 aura::RootWindow* second_root = Shell::GetAllRootWindows()[1];
162 EXPECT_EQ(second_root, window->GetRootWindow());
163 aura::test::EventGenerator generator2(second_root, window.get());
165 // Y-axis maximization.
166 wd.set_window_component(HTTOP);
167 generator2.PressLeftButton();
168 generator2.ReleaseLeftButton();
169 generator2.set_flags(ui::EF_IS_DOUBLE_CLICK);
170 generator2.PressLeftButton();
171 generator2.MoveMouseTo(generator.current_location(), 1);
172 generator2.ReleaseLeftButton();
173 generator.DoubleClickLeftButton();
174 bounds_in_screen = window->GetBoundsInScreen();
175 EXPECT_EQ(restored_bounds.x(), bounds_in_screen.x());
176 EXPECT_EQ(restored_bounds.width(), bounds_in_screen.width());
177 EXPECT_EQ(work_area2.y(), bounds_in_screen.y());
178 EXPECT_EQ(work_area2.height(), bounds_in_screen.height());
179 EXPECT_FALSE(window_state->IsMaximized());
182 wd.set_window_component(HTCAPTION);
183 generator2.DoubleClickLeftButton();
184 EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
186 // X-axis maximization.
187 wd.set_window_component(HTLEFT);
188 generator2.DoubleClickLeftButton();
189 bounds_in_screen = window->GetBoundsInScreen();
190 EXPECT_EQ(restored_bounds.y(), bounds_in_screen.y());
191 EXPECT_EQ(restored_bounds.height(), bounds_in_screen.height());
192 EXPECT_EQ(work_area2.x(), bounds_in_screen.x());
193 EXPECT_EQ(work_area2.width(), bounds_in_screen.width());
194 EXPECT_FALSE(window_state->IsMaximized());
197 wd.set_window_component(HTCAPTION);
198 generator2.DoubleClickLeftButton();
199 EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
202 TEST_F(WorkspaceEventHandlerTest,
203 DoubleClickSingleAxisDoesntResizeVerticalEdgeIfConstrained) {
204 gfx::Rect restored_bounds(10, 10, 50, 50);
205 aura::test::TestWindowDelegate wd;
206 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
208 wm::ActivateWindow(window.get());
210 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
211 window.get()).work_area();
213 wd.set_maximum_size(gfx::Size(0, 100));
215 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
217 // Double-click the top resize edge.
218 wd.set_window_component(HTTOP);
219 generator.DoubleClickLeftButton();
221 // The size of the window should be unchanged.
222 EXPECT_EQ(restored_bounds.y(), window->bounds().y());
223 EXPECT_EQ(restored_bounds.height(), window->bounds().height());
226 TEST_F(WorkspaceEventHandlerTest,
227 DoubleClickSingleAxisDoesntResizeHorizontalEdgeIfConstrained) {
228 gfx::Rect restored_bounds(10, 10, 50, 50);
229 aura::test::TestWindowDelegate wd;
230 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
232 wm::ActivateWindow(window.get());
234 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
235 window.get()).work_area();
237 wd.set_maximum_size(gfx::Size(100, 0));
239 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
241 // Double-click the top resize edge.
242 wd.set_window_component(HTRIGHT);
243 generator.DoubleClickLeftButton();
245 // The size of the window should be unchanged.
246 EXPECT_EQ(restored_bounds.x(), window->bounds().x());
247 EXPECT_EQ(restored_bounds.width(), window->bounds().width());
250 TEST_F(WorkspaceEventHandlerTest, DoubleClickCaptionTogglesMaximize) {
251 aura::test::TestWindowDelegate wd;
252 scoped_ptr<aura::Window> window(
253 CreateTestWindow(&wd, gfx::Rect(1, 2, 30, 40)));
254 window->SetProperty(aura::client::kCanMaximizeKey, true);
255 wd.set_window_component(HTCAPTION);
257 wm::WindowState* window_state = wm::GetWindowState(window.get());
258 EXPECT_FALSE(window_state->IsMaximized());
259 aura::Window* root = Shell::GetPrimaryRootWindow();
260 aura::test::EventGenerator generator(root, window.get());
261 generator.DoubleClickLeftButton();
262 EXPECT_NE("1,2 30x40", window->bounds().ToString());
264 EXPECT_TRUE(window_state->IsMaximized());
265 generator.DoubleClickLeftButton();
267 EXPECT_FALSE(window_state->IsMaximized());
268 EXPECT_EQ("1,2 30x40", window->bounds().ToString());
270 // Double-clicking the middle button shouldn't toggle the maximized state.
271 WindowPropertyObserver observer(window.get());
272 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, generator.current_location(),
273 generator.current_location(),
274 ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK);
275 aura::WindowEventDispatcher* dispatcher = root->GetDispatcher();
276 dispatcher->AsRootWindowHostDelegate()->OnHostMouseEvent(&press);
277 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, generator.current_location(),
278 generator.current_location(),
279 ui::EF_IS_DOUBLE_CLICK);
280 dispatcher->AsRootWindowHostDelegate()->OnHostMouseEvent(&release);
282 EXPECT_FALSE(window_state->IsMaximized());
283 EXPECT_EQ("1,2 30x40", window->bounds().ToString());
284 EXPECT_FALSE(observer.DidPropertyChange(aura::client::kShowStateKey));
287 TEST_F(WorkspaceEventHandlerTest, DoubleTapCaptionTogglesMaximize) {
288 aura::test::TestWindowDelegate wd;
289 gfx::Rect bounds(10, 20, 30, 40);
290 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, bounds));
291 window->SetProperty(aura::client::kCanMaximizeKey, true);
292 wd.set_window_component(HTCAPTION);
294 wm::WindowState* window_state = wm::GetWindowState(window.get());
295 EXPECT_FALSE(window_state->IsMaximized());
296 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
298 generator.GestureTapAt(gfx::Point(25, 25));
299 generator.GestureTapAt(gfx::Point(25, 25));
300 RunAllPendingInMessageLoop();
301 EXPECT_NE(bounds.ToString(), window->bounds().ToString());
302 EXPECT_TRUE(window_state->IsMaximized());
304 generator.GestureTapAt(gfx::Point(5, 5));
305 generator.GestureTapAt(gfx::Point(10, 10));
307 EXPECT_FALSE(window_state->IsMaximized());
308 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
311 // Verifies deleting the window while dragging doesn't crash.
312 TEST_F(WorkspaceEventHandlerTest, DeleteWhenDragging) {
313 // Create a large window in the background. This is necessary so that when we
314 // delete |window| WorkspaceEventHandler is still the active event handler.
315 aura::test::TestWindowDelegate wd2;
316 scoped_ptr<aura::Window> window2(
317 CreateTestWindow(&wd2, gfx::Rect(0, 0, 500, 500)));
319 aura::test::TestWindowDelegate wd;
320 const gfx::Rect bounds(10, 20, 30, 40);
321 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, bounds));
322 wd.set_window_component(HTCAPTION);
323 aura::test::EventGenerator generator(window->GetRootWindow());
324 generator.MoveMouseToCenterOf(window.get());
325 generator.PressLeftButton();
326 generator.MoveMouseTo(generator.current_location() + gfx::Vector2d(50, 50));
327 DCHECK_NE(bounds.origin().ToString(), window->bounds().origin().ToString());
329 generator.MoveMouseTo(generator.current_location() + gfx::Vector2d(50, 50));
332 // Verifies deleting the window while in a run loop doesn't crash.
333 TEST_F(WorkspaceEventHandlerTest, DeleteWhileInRunLoop) {
334 aura::test::TestWindowDelegate wd;
335 const gfx::Rect bounds(10, 20, 30, 40);
336 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, bounds));
337 wd.set_window_component(HTCAPTION);
339 ASSERT_TRUE(aura::client::GetWindowMoveClient(window->parent()));
340 base::MessageLoop::current()->DeleteSoon(FROM_HERE, window.get());
341 aura::client::GetWindowMoveClient(window->parent())
342 ->RunMoveLoop(window.release(),
344 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
347 } // namespace internal