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/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"
15 #include "ui/aura/test/event_generator.h"
16 #include "ui/aura/test/test_window_delegate.h"
17 #include "ui/aura/window.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/gfx/screen.h"
22 #include "base/win/windows_version.h"
28 class WorkspaceEventHandlerTest
: public test::AshTestBase
{
30 WorkspaceEventHandlerTest() {}
31 virtual ~WorkspaceEventHandlerTest() {}
34 aura::Window
* CreateTestWindow(aura::WindowDelegate
* delegate
,
35 const gfx::Rect
& bounds
) {
36 aura::Window
* window
= new aura::Window(delegate
);
37 window
->SetType(aura::client::WINDOW_TYPE_NORMAL
);
38 window
->Init(ui::LAYER_TEXTURED
);
39 SetDefaultParentByPrimaryRootWindow(window
);
40 window
->SetBounds(bounds
);
46 DISALLOW_COPY_AND_ASSIGN(WorkspaceEventHandlerTest
);
49 TEST_F(WorkspaceEventHandlerTest
, DoubleClickSingleAxisResizeEdge
) {
50 // Double clicking the vertical resize edge of a window should maximize it
52 gfx::Rect
restored_bounds(10, 10, 50, 50);
53 aura::test::TestWindowDelegate wd
;
54 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, restored_bounds
));
56 wm::ActivateWindow(window
.get());
58 gfx::Rect work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
59 window
.get()).work_area();
61 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
64 // Double-click the top resize edge.
65 wd
.set_window_component(HTTOP
);
66 // On X a double click actually generates a drag between each press/release.
67 // Explicitly trigger this path since we had bugs in dealing with it
69 generator
.PressLeftButton();
70 generator
.ReleaseLeftButton();
71 generator
.set_flags(ui::EF_IS_DOUBLE_CLICK
);
72 generator
.PressLeftButton();
73 generator
.MoveMouseTo(generator
.current_location(), 1);
74 generator
.ReleaseLeftButton();
75 gfx::Rect bounds_in_screen
= window
->GetBoundsInScreen();
76 EXPECT_EQ(restored_bounds
.x(), bounds_in_screen
.x());
77 EXPECT_EQ(restored_bounds
.width(), bounds_in_screen
.width());
78 EXPECT_EQ(work_area
.y(), bounds_in_screen
.y());
79 EXPECT_EQ(work_area
.height(), bounds_in_screen
.height());
80 // Single-axis maximization is not considered real maximization.
81 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
84 generator
.DoubleClickLeftButton();
85 bounds_in_screen
= window
->GetBoundsInScreen();
86 EXPECT_EQ(restored_bounds
.ToString(), bounds_in_screen
.ToString());
87 // Note that it should not even be restored at this point, it should have
88 // also cleared the restore rectangle.
89 EXPECT_EQ(NULL
, GetRestoreBoundsInScreen(window
.get()));
91 // Double-click the top resize edge again to maximize vertically, then double
92 // click again to restore.
93 generator
.DoubleClickLeftButton();
94 wd
.set_window_component(HTCAPTION
);
95 generator
.DoubleClickLeftButton();
96 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
97 bounds_in_screen
= window
->GetBoundsInScreen();
98 EXPECT_EQ(restored_bounds
.ToString(), bounds_in_screen
.ToString());
100 // Double clicking the left resize edge should maximize horizontally.
101 wd
.set_window_component(HTLEFT
);
102 generator
.DoubleClickLeftButton();
103 bounds_in_screen
= window
->GetBoundsInScreen();
104 EXPECT_EQ(restored_bounds
.y(), bounds_in_screen
.y());
105 EXPECT_EQ(restored_bounds
.height(), bounds_in_screen
.height());
106 EXPECT_EQ(work_area
.x(), bounds_in_screen
.x());
107 EXPECT_EQ(work_area
.width(), bounds_in_screen
.width());
108 // Single-axis maximization is not considered real maximization.
109 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
112 wd
.set_window_component(HTCAPTION
);
113 generator
.DoubleClickLeftButton();
114 EXPECT_EQ(restored_bounds
.ToString(), window
->GetBoundsInScreen().ToString());
117 // Multi display test does not run on Win8 bot. crbug.com/247427.
118 if (base::win::GetVersion() >= base::win::VERSION_WIN8
)
122 // Verify the double clicking the resize edge works on 2nd display too.
123 UpdateDisplay("200x200,400x300");
124 gfx::Rect work_area2
= ScreenAsh::GetSecondaryDisplay().work_area();
125 restored_bounds
.SetRect(220,20, 50, 50);
126 window
->SetBoundsInScreen(restored_bounds
, ScreenAsh::GetSecondaryDisplay());
127 aura::RootWindow
* second_root
= Shell::GetAllRootWindows()[1];
128 EXPECT_EQ(second_root
, window
->GetRootWindow());
129 aura::test::EventGenerator
generator2(second_root
, window
.get());
131 // Y-axis maximization.
132 wd
.set_window_component(HTTOP
);
133 generator2
.PressLeftButton();
134 generator2
.ReleaseLeftButton();
135 generator2
.set_flags(ui::EF_IS_DOUBLE_CLICK
);
136 generator2
.PressLeftButton();
137 generator2
.MoveMouseTo(generator
.current_location(), 1);
138 generator2
.ReleaseLeftButton();
139 generator
.DoubleClickLeftButton();
140 bounds_in_screen
= window
->GetBoundsInScreen();
141 EXPECT_EQ(restored_bounds
.x(), bounds_in_screen
.x());
142 EXPECT_EQ(restored_bounds
.width(), bounds_in_screen
.width());
143 EXPECT_EQ(work_area2
.y(), bounds_in_screen
.y());
144 EXPECT_EQ(work_area2
.height(), bounds_in_screen
.height());
145 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
148 wd
.set_window_component(HTCAPTION
);
149 generator2
.DoubleClickLeftButton();
150 EXPECT_EQ(restored_bounds
.ToString(), window
->GetBoundsInScreen().ToString());
152 // X-axis maximization.
153 wd
.set_window_component(HTLEFT
);
154 generator2
.DoubleClickLeftButton();
155 bounds_in_screen
= window
->GetBoundsInScreen();
156 EXPECT_EQ(restored_bounds
.y(), bounds_in_screen
.y());
157 EXPECT_EQ(restored_bounds
.height(), bounds_in_screen
.height());
158 EXPECT_EQ(work_area2
.x(), bounds_in_screen
.x());
159 EXPECT_EQ(work_area2
.width(), bounds_in_screen
.width());
160 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
163 wd
.set_window_component(HTCAPTION
);
164 generator2
.DoubleClickLeftButton();
165 EXPECT_EQ(restored_bounds
.ToString(), window
->GetBoundsInScreen().ToString());
168 TEST_F(WorkspaceEventHandlerTest
,
169 DoubleClickSingleAxisDoesntResizeVerticalEdgeIfConstrained
) {
170 gfx::Rect
restored_bounds(10, 10, 50, 50);
171 aura::test::TestWindowDelegate wd
;
172 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, restored_bounds
));
174 wm::ActivateWindow(window
.get());
176 gfx::Rect work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
177 window
.get()).work_area();
179 wd
.set_maximum_size(gfx::Size(0, 100));
181 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
183 // Double-click the top resize edge.
184 wd
.set_window_component(HTTOP
);
185 generator
.DoubleClickLeftButton();
187 // The size of the window should be unchanged.
188 EXPECT_EQ(restored_bounds
.y(), window
->bounds().y());
189 EXPECT_EQ(restored_bounds
.height(), window
->bounds().height());
192 TEST_F(WorkspaceEventHandlerTest
,
193 DoubleClickSingleAxisDoesntResizeHorizontalEdgeIfConstrained
) {
194 gfx::Rect
restored_bounds(10, 10, 50, 50);
195 aura::test::TestWindowDelegate wd
;
196 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, restored_bounds
));
198 wm::ActivateWindow(window
.get());
200 gfx::Rect work_area
= Shell::GetScreen()->GetDisplayNearestWindow(
201 window
.get()).work_area();
203 wd
.set_maximum_size(gfx::Size(100, 0));
205 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
207 // Double-click the top resize edge.
208 wd
.set_window_component(HTRIGHT
);
209 generator
.DoubleClickLeftButton();
211 // The size of the window should be unchanged.
212 EXPECT_EQ(restored_bounds
.x(), window
->bounds().x());
213 EXPECT_EQ(restored_bounds
.width(), window
->bounds().width());
216 TEST_F(WorkspaceEventHandlerTest
, DoubleClickCaptionTogglesMaximize
) {
217 aura::test::TestWindowDelegate wd
;
218 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, gfx::Rect(1, 2, 3, 4)));
219 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
220 wd
.set_window_component(HTCAPTION
);
221 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
222 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
224 generator
.DoubleClickLeftButton();
225 EXPECT_NE("1,2 3x4", window
->bounds().ToString());
227 EXPECT_TRUE(wm::IsWindowMaximized(window
.get()));
228 generator
.DoubleClickLeftButton();
230 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
231 EXPECT_EQ("1,2 3x4", window
->bounds().ToString());
234 TEST_F(WorkspaceEventHandlerTest
, DoubleTapCaptionTogglesMaximize
) {
235 aura::test::TestWindowDelegate wd
;
236 gfx::Rect
bounds(10, 20, 30, 40);
237 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, bounds
));
238 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
239 wd
.set_window_component(HTCAPTION
);
240 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
241 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
243 generator
.GestureTapAt(gfx::Point(25, 25));
244 generator
.GestureTapAt(gfx::Point(25, 25));
245 RunAllPendingInMessageLoop();
246 EXPECT_NE(bounds
.ToString(), window
->bounds().ToString());
247 EXPECT_TRUE(wm::IsWindowMaximized(window
.get()));
249 generator
.GestureTapAt(gfx::Point(5, 5));
250 generator
.GestureTapAt(gfx::Point(10, 10));
252 EXPECT_FALSE(wm::IsWindowMaximized(window
.get()));
253 EXPECT_EQ(bounds
.ToString(), window
->bounds().ToString());
256 // Verifies deleting the window while dragging doesn't crash.
257 TEST_F(WorkspaceEventHandlerTest
, DeleteWhenDragging
) {
258 // Create a large window in the background. This is necessary so that when we
259 // delete |window| WorkspaceEventHandler is still the active event handler.
260 aura::test::TestWindowDelegate wd2
;
261 scoped_ptr
<aura::Window
> window2(
262 CreateTestWindow(&wd2
, gfx::Rect(0, 0, 500, 500)));
264 aura::test::TestWindowDelegate wd
;
265 const gfx::Rect
bounds(10, 20, 30, 40);
266 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, bounds
));
267 wd
.set_window_component(HTCAPTION
);
268 aura::test::EventGenerator
generator(window
->GetRootWindow());
269 generator
.MoveMouseToCenterOf(window
.get());
270 generator
.PressLeftButton();
271 generator
.MoveMouseTo(generator
.current_location() + gfx::Vector2d(50, 50));
272 DCHECK_NE(bounds
.origin().ToString(), window
->bounds().origin().ToString());
274 generator
.MoveMouseTo(generator
.current_location() + gfx::Vector2d(50, 50));
277 // Verifies deleting the window while in a run loop doesn't crash.
278 TEST_F(WorkspaceEventHandlerTest
, DeleteWhileInRunLoop
) {
279 aura::test::TestWindowDelegate wd
;
280 const gfx::Rect
bounds(10, 20, 30, 40);
281 scoped_ptr
<aura::Window
> window(CreateTestWindow(&wd
, bounds
));
282 wd
.set_window_component(HTCAPTION
);
284 ASSERT_TRUE(aura::client::GetWindowMoveClient(window
->parent()));
285 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, window
.get());
286 aura::client::GetWindowMoveClient(window
->parent())
287 ->RunMoveLoop(window
.release(),
289 aura::client::WINDOW_MOVE_SOURCE_MOUSE
);
292 } // namespace internal