blob: e592ad5a948d5458440016aca1fe96e72b604bac [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "ash/wm/workspace/workspace_event_handler.h"
6
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01007#include "ash/screen_ash.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "ash/shell.h"
9#include "ash/test/ash_test_base.h"
10#include "ash/wm/property_util.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"
Ben Murdoch32409262013-08-07 11:04:47 +010015#include "ui/aura/root_window.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "ui/aura/test/event_generator.h"
17#include "ui/aura/test/test_window_delegate.h"
18#include "ui/aura/window.h"
Ben Murdoch32409262013-08-07 11:04:47 +010019#include "ui/aura/window_property.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020#include "ui/base/hit_test.h"
21#include "ui/gfx/screen.h"
22
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010023#if defined(OS_WIN)
24#include "base/win/windows_version.h"
25#endif
26
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027namespace ash {
28namespace internal {
29
30class WorkspaceEventHandlerTest : public test::AshTestBase {
31 public:
32 WorkspaceEventHandlerTest() {}
33 virtual ~WorkspaceEventHandlerTest() {}
34
35 protected:
36 aura::Window* CreateTestWindow(aura::WindowDelegate* delegate,
37 const gfx::Rect& bounds) {
38 aura::Window* window = new aura::Window(delegate);
39 window->SetType(aura::client::WINDOW_TYPE_NORMAL);
40 window->Init(ui::LAYER_TEXTURED);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000041 SetDefaultParentByPrimaryRootWindow(window);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000042 window->SetBounds(bounds);
43 window->Show();
44 return window;
45 }
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(WorkspaceEventHandlerTest);
49};
50
Ben Murdoch32409262013-08-07 11:04:47 +010051// Keeps track of the properties changed of a particular window.
52class WindowPropertyObserver : public aura::WindowObserver {
53 public:
54 explicit WindowPropertyObserver(aura::Window* window)
55 : window_(window) {
56 window->AddObserver(this);
57 }
58
59 virtual ~WindowPropertyObserver() {
60 window_->RemoveObserver(this);
61 }
62
63 bool DidPropertyChange(const void* property) const {
64 return std::find(properties_changed_.begin(),
65 properties_changed_.end(),
66 property) != properties_changed_.end();
67 }
68
69 private:
70 virtual void OnWindowPropertyChanged(aura::Window* window,
71 const void* key,
72 intptr_t old) OVERRIDE {
73 properties_changed_.push_back(key);
74 }
75
76 aura::Window* window_;
77 std::vector<const void*> properties_changed_;
78
79 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver);
80};
81
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082TEST_F(WorkspaceEventHandlerTest, DoubleClickSingleAxisResizeEdge) {
83 // Double clicking the vertical resize edge of a window should maximize it
84 // vertically.
85 gfx::Rect restored_bounds(10, 10, 50, 50);
86 aura::test::TestWindowDelegate wd;
87 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
88
89 wm::ActivateWindow(window.get());
90
91 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
92 window.get()).work_area();
93
94 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
95 window.get());
96
97 // Double-click the top resize edge.
98 wd.set_window_component(HTTOP);
99 // On X a double click actually generates a drag between each press/release.
100 // Explicitly trigger this path since we had bugs in dealing with it
101 // correctly.
102 generator.PressLeftButton();
103 generator.ReleaseLeftButton();
104 generator.set_flags(ui::EF_IS_DOUBLE_CLICK);
105 generator.PressLeftButton();
106 generator.MoveMouseTo(generator.current_location(), 1);
107 generator.ReleaseLeftButton();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100108 gfx::Rect bounds_in_screen = window->GetBoundsInScreen();
109 EXPECT_EQ(restored_bounds.x(), bounds_in_screen.x());
110 EXPECT_EQ(restored_bounds.width(), bounds_in_screen.width());
111 EXPECT_EQ(work_area.y(), bounds_in_screen.y());
112 EXPECT_EQ(work_area.height(), bounds_in_screen.height());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000113 // Single-axis maximization is not considered real maximization.
114 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
115
116 // Restore.
117 generator.DoubleClickLeftButton();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100118 bounds_in_screen = window->GetBoundsInScreen();
119 EXPECT_EQ(restored_bounds.ToString(), bounds_in_screen.ToString());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000120 // Note that it should not even be restored at this point, it should have
121 // also cleared the restore rectangle.
122 EXPECT_EQ(NULL, GetRestoreBoundsInScreen(window.get()));
123
124 // Double-click the top resize edge again to maximize vertically, then double
125 // click again to restore.
126 generator.DoubleClickLeftButton();
127 wd.set_window_component(HTCAPTION);
128 generator.DoubleClickLeftButton();
129 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100130 bounds_in_screen = window->GetBoundsInScreen();
131 EXPECT_EQ(restored_bounds.ToString(), bounds_in_screen.ToString());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000132
133 // Double clicking the left resize edge should maximize horizontally.
134 wd.set_window_component(HTLEFT);
135 generator.DoubleClickLeftButton();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100136 bounds_in_screen = window->GetBoundsInScreen();
137 EXPECT_EQ(restored_bounds.y(), bounds_in_screen.y());
138 EXPECT_EQ(restored_bounds.height(), bounds_in_screen.height());
139 EXPECT_EQ(work_area.x(), bounds_in_screen.x());
140 EXPECT_EQ(work_area.width(), bounds_in_screen.width());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000141 // Single-axis maximization is not considered real maximization.
142 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100143
144 // Restore.
145 wd.set_window_component(HTCAPTION);
146 generator.DoubleClickLeftButton();
147 EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
148
149#if defined(OS_WIN)
150 // Multi display test does not run on Win8 bot. crbug.com/247427.
151 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
152 return;
153#endif
154
155 // Verify the double clicking the resize edge works on 2nd display too.
156 UpdateDisplay("200x200,400x300");
157 gfx::Rect work_area2 = ScreenAsh::GetSecondaryDisplay().work_area();
158 restored_bounds.SetRect(220,20, 50, 50);
159 window->SetBoundsInScreen(restored_bounds, ScreenAsh::GetSecondaryDisplay());
160 aura::RootWindow* second_root = Shell::GetAllRootWindows()[1];
161 EXPECT_EQ(second_root, window->GetRootWindow());
162 aura::test::EventGenerator generator2(second_root, window.get());
163
164 // Y-axis maximization.
165 wd.set_window_component(HTTOP);
166 generator2.PressLeftButton();
167 generator2.ReleaseLeftButton();
168 generator2.set_flags(ui::EF_IS_DOUBLE_CLICK);
169 generator2.PressLeftButton();
170 generator2.MoveMouseTo(generator.current_location(), 1);
171 generator2.ReleaseLeftButton();
172 generator.DoubleClickLeftButton();
173 bounds_in_screen = window->GetBoundsInScreen();
174 EXPECT_EQ(restored_bounds.x(), bounds_in_screen.x());
175 EXPECT_EQ(restored_bounds.width(), bounds_in_screen.width());
176 EXPECT_EQ(work_area2.y(), bounds_in_screen.y());
177 EXPECT_EQ(work_area2.height(), bounds_in_screen.height());
178 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
179
180 // Restore.
181 wd.set_window_component(HTCAPTION);
182 generator2.DoubleClickLeftButton();
183 EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
184
185 // X-axis maximization.
186 wd.set_window_component(HTLEFT);
187 generator2.DoubleClickLeftButton();
188 bounds_in_screen = window->GetBoundsInScreen();
189 EXPECT_EQ(restored_bounds.y(), bounds_in_screen.y());
190 EXPECT_EQ(restored_bounds.height(), bounds_in_screen.height());
191 EXPECT_EQ(work_area2.x(), bounds_in_screen.x());
192 EXPECT_EQ(work_area2.width(), bounds_in_screen.width());
193 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
194
195 // Restore.
196 wd.set_window_component(HTCAPTION);
197 generator2.DoubleClickLeftButton();
198 EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000199}
200
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000201TEST_F(WorkspaceEventHandlerTest,
202 DoubleClickSingleAxisDoesntResizeVerticalEdgeIfConstrained) {
203 gfx::Rect restored_bounds(10, 10, 50, 50);
204 aura::test::TestWindowDelegate wd;
205 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
206
207 wm::ActivateWindow(window.get());
208
209 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
210 window.get()).work_area();
211
212 wd.set_maximum_size(gfx::Size(0, 100));
213
214 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
215 window.get());
216 // Double-click the top resize edge.
217 wd.set_window_component(HTTOP);
218 generator.DoubleClickLeftButton();
219
220 // The size of the window should be unchanged.
221 EXPECT_EQ(restored_bounds.y(), window->bounds().y());
222 EXPECT_EQ(restored_bounds.height(), window->bounds().height());
223}
224
225TEST_F(WorkspaceEventHandlerTest,
226 DoubleClickSingleAxisDoesntResizeHorizontalEdgeIfConstrained) {
227 gfx::Rect restored_bounds(10, 10, 50, 50);
228 aura::test::TestWindowDelegate wd;
229 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
230
231 wm::ActivateWindow(window.get());
232
233 gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
234 window.get()).work_area();
235
236 wd.set_maximum_size(gfx::Size(100, 0));
237
238 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
239 window.get());
240 // Double-click the top resize edge.
241 wd.set_window_component(HTRIGHT);
242 generator.DoubleClickLeftButton();
243
244 // The size of the window should be unchanged.
245 EXPECT_EQ(restored_bounds.x(), window->bounds().x());
246 EXPECT_EQ(restored_bounds.width(), window->bounds().width());
247}
248
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000249TEST_F(WorkspaceEventHandlerTest, DoubleClickCaptionTogglesMaximize) {
250 aura::test::TestWindowDelegate wd;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100251 scoped_ptr<aura::Window> window(
252 CreateTestWindow(&wd, gfx::Rect(1, 2, 30, 40)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000253 window->SetProperty(aura::client::kCanMaximizeKey, true);
254 wd.set_window_component(HTCAPTION);
255 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
Ben Murdoch32409262013-08-07 11:04:47 +0100256 aura::RootWindow* root = Shell::GetPrimaryRootWindow();
257 aura::test::EventGenerator generator(root, window.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000258 generator.DoubleClickLeftButton();
Ben Murdochbb1529c2013-08-08 10:24:53 +0100259 EXPECT_NE("1,2 30x40", window->bounds().ToString());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000260
261 EXPECT_TRUE(wm::IsWindowMaximized(window.get()));
262 generator.DoubleClickLeftButton();
263
264 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
Ben Murdochbb1529c2013-08-08 10:24:53 +0100265 EXPECT_EQ("1,2 30x40", window->bounds().ToString());
Ben Murdoch32409262013-08-07 11:04:47 +0100266
267 // Double-clicking the middle button shouldn't toggle the maximized state.
268 WindowPropertyObserver observer(window.get());
269 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, generator.current_location(),
270 generator.current_location(),
271 ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK);
272 root->AsRootWindowHostDelegate()->OnHostMouseEvent(&press);
273 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, generator.current_location(),
274 generator.current_location(),
275 ui::EF_IS_DOUBLE_CLICK);
276 root->AsRootWindowHostDelegate()->OnHostMouseEvent(&release);
277
278 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
Ben Murdochbb1529c2013-08-08 10:24:53 +0100279 EXPECT_EQ("1,2 30x40", window->bounds().ToString());
Ben Murdoch32409262013-08-07 11:04:47 +0100280 EXPECT_FALSE(observer.DidPropertyChange(aura::client::kShowStateKey));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000281}
282
283TEST_F(WorkspaceEventHandlerTest, DoubleTapCaptionTogglesMaximize) {
284 aura::test::TestWindowDelegate wd;
285 gfx::Rect bounds(10, 20, 30, 40);
286 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, bounds));
287 window->SetProperty(aura::client::kCanMaximizeKey, true);
288 wd.set_window_component(HTCAPTION);
289 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
290 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
291 window.get());
292 generator.GestureTapAt(gfx::Point(25, 25));
293 generator.GestureTapAt(gfx::Point(25, 25));
294 RunAllPendingInMessageLoop();
295 EXPECT_NE(bounds.ToString(), window->bounds().ToString());
296 EXPECT_TRUE(wm::IsWindowMaximized(window.get()));
297
298 generator.GestureTapAt(gfx::Point(5, 5));
299 generator.GestureTapAt(gfx::Point(10, 10));
300
301 EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
302 EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
303}
304
305// Verifies deleting the window while dragging doesn't crash.
306TEST_F(WorkspaceEventHandlerTest, DeleteWhenDragging) {
307 // Create a large window in the background. This is necessary so that when we
308 // delete |window| WorkspaceEventHandler is still the active event handler.
309 aura::test::TestWindowDelegate wd2;
310 scoped_ptr<aura::Window> window2(
311 CreateTestWindow(&wd2, gfx::Rect(0, 0, 500, 500)));
312
313 aura::test::TestWindowDelegate wd;
314 const gfx::Rect bounds(10, 20, 30, 40);
315 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, bounds));
316 wd.set_window_component(HTCAPTION);
317 aura::test::EventGenerator generator(window->GetRootWindow());
318 generator.MoveMouseToCenterOf(window.get());
319 generator.PressLeftButton();
320 generator.MoveMouseTo(generator.current_location() + gfx::Vector2d(50, 50));
321 DCHECK_NE(bounds.origin().ToString(), window->bounds().origin().ToString());
322 window.reset();
323 generator.MoveMouseTo(generator.current_location() + gfx::Vector2d(50, 50));
324}
325
326// Verifies deleting the window while in a run loop doesn't crash.
327TEST_F(WorkspaceEventHandlerTest, DeleteWhileInRunLoop) {
328 aura::test::TestWindowDelegate wd;
329 const gfx::Rect bounds(10, 20, 30, 40);
330 scoped_ptr<aura::Window> window(CreateTestWindow(&wd, bounds));
331 wd.set_window_component(HTCAPTION);
332
333 ASSERT_TRUE(aura::client::GetWindowMoveClient(window->parent()));
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100334 base::MessageLoop::current()->DeleteSoon(FROM_HERE, window.get());
335 aura::client::GetWindowMoveClient(window->parent())
336 ->RunMoveLoop(window.release(),
337 gfx::Vector2d(),
338 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000339}
340
341} // namespace internal
342} // namespace ash