workspace_event_handler_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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.
4
5#include "ash/wm/workspace/workspace_event_handler.h"
6
7#include "ash/screen_ash.h"
8#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"
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"
20
21#if defined(OS_WIN)
22#include "base/win/windows_version.h"
23#endif
24
25namespace ash {
26namespace internal {
27
28class WorkspaceEventHandlerTest : public test::AshTestBase {
29 public:
30  WorkspaceEventHandlerTest() {}
31  virtual ~WorkspaceEventHandlerTest() {}
32
33 protected:
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);
41    window->Show();
42    return window;
43  }
44
45 private:
46  DISALLOW_COPY_AND_ASSIGN(WorkspaceEventHandlerTest);
47};
48
49TEST_F(WorkspaceEventHandlerTest, DoubleClickSingleAxisResizeEdge) {
50  // Double clicking the vertical resize edge of a window should maximize it
51  // vertically.
52  gfx::Rect restored_bounds(10, 10, 50, 50);
53  aura::test::TestWindowDelegate wd;
54  scoped_ptr<aura::Window> window(CreateTestWindow(&wd, restored_bounds));
55
56  wm::ActivateWindow(window.get());
57
58  gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
59      window.get()).work_area();
60
61  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
62                                       window.get());
63
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
68  // correctly.
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()));
82
83  // Restore.
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()));
90
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());
99
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()));
110
111  // Restore.
112  wd.set_window_component(HTCAPTION);
113  generator.DoubleClickLeftButton();
114  EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
115
116#if defined(OS_WIN)
117  // Multi display test does not run on Win8 bot. crbug.com/247427.
118  if (base::win::GetVersion() >= base::win::VERSION_WIN8)
119    return;
120#endif
121
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());
130
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()));
146
147  // Restore.
148  wd.set_window_component(HTCAPTION);
149  generator2.DoubleClickLeftButton();
150  EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
151
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()));
161
162  // Restore.
163  wd.set_window_component(HTCAPTION);
164  generator2.DoubleClickLeftButton();
165  EXPECT_EQ(restored_bounds.ToString(), window->GetBoundsInScreen().ToString());
166}
167
168TEST_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));
173
174  wm::ActivateWindow(window.get());
175
176  gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
177      window.get()).work_area();
178
179  wd.set_maximum_size(gfx::Size(0, 100));
180
181  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
182                                       window.get());
183  // Double-click the top resize edge.
184  wd.set_window_component(HTTOP);
185  generator.DoubleClickLeftButton();
186
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());
190}
191
192TEST_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));
197
198  wm::ActivateWindow(window.get());
199
200  gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
201      window.get()).work_area();
202
203  wd.set_maximum_size(gfx::Size(100, 0));
204
205  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
206                                       window.get());
207  // Double-click the top resize edge.
208  wd.set_window_component(HTRIGHT);
209  generator.DoubleClickLeftButton();
210
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());
214}
215
216TEST_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(),
223                                       window.get());
224  generator.DoubleClickLeftButton();
225  EXPECT_NE("1,2 3x4", window->bounds().ToString());
226
227  EXPECT_TRUE(wm::IsWindowMaximized(window.get()));
228  generator.DoubleClickLeftButton();
229
230  EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
231  EXPECT_EQ("1,2 3x4", window->bounds().ToString());
232}
233
234TEST_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(),
242                                       window.get());
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()));
248
249  generator.GestureTapAt(gfx::Point(5, 5));
250  generator.GestureTapAt(gfx::Point(10, 10));
251
252  EXPECT_FALSE(wm::IsWindowMaximized(window.get()));
253  EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
254}
255
256// Verifies deleting the window while dragging doesn't crash.
257TEST_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)));
263
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());
273  window.reset();
274  generator.MoveMouseTo(generator.current_location() + gfx::Vector2d(50, 50));
275}
276
277// Verifies deleting the window while in a run loop doesn't crash.
278TEST_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);
283
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(),
288                    gfx::Vector2d(),
289                    aura::client::WINDOW_MOVE_SOURCE_MOUSE);
290}
291
292}  // namespace internal
293}  // namespace ash
294