Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / wm / resize_shadow_and_cursor_unittest.cc
blob066c3509f0e92c31817092e801e030962e440f12
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/ash_constants.h"
6 #include "ash/frame/custom_frame_view_ash.h"
7 #include "ash/shell.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/cursor_manager_test_api.h"
10 #include "ash/wm/resize_shadow.h"
11 #include "ash/wm/resize_shadow_controller.h"
12 #include "ash/wm/window_state.h"
13 #include "base/bind.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/base/cursor/cursor.h"
16 #include "ui/base/hit_test.h"
17 #include "ui/events/test/event_generator.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/widget/widget_delegate.h"
21 namespace ash {
22 namespace test {
24 namespace {
26 // views::WidgetDelegate which uses ash::CustomFrameViewAsh.
27 class TestWidgetDelegate : public views::WidgetDelegateView {
28 public:
29 TestWidgetDelegate() {}
30 virtual ~TestWidgetDelegate() {}
32 // views::WidgetDelegateView overrides:
33 virtual bool CanResize() const OVERRIDE {
34 return true;
36 virtual bool CanMaximize() const OVERRIDE {
37 return true;
39 virtual views::NonClientFrameView* CreateNonClientFrameView(
40 views::Widget* widget) OVERRIDE {
41 return new ash::CustomFrameViewAsh(widget);
44 private:
45 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate);
48 } // namespace
50 // The test tests that the mouse cursor is changed and that the resize shadows
51 // are shown when the mouse is hovered over the window edge.
52 class ResizeShadowAndCursorTest : public AshTestBase {
53 public:
54 ResizeShadowAndCursorTest() {}
55 virtual ~ResizeShadowAndCursorTest() {}
57 // AshTestBase override:
58 virtual void SetUp() OVERRIDE {
59 AshTestBase::SetUp();
61 views::Widget* widget(views::Widget::CreateWindowWithContextAndBounds(
62 new TestWidgetDelegate(), CurrentContext(), gfx::Rect(0, 0, 200, 100)));
63 widget->Show();
64 window_ = widget->GetNativeView();
66 // Add a child window to |window_| in order to properly test that the resize
67 // handles and the resize shadows are shown when the mouse is
68 // ash::kResizeInsideBoundsSize inside of |window_|'s edges.
69 aura::Window* child = CreateTestWindowInShell(
70 SK_ColorWHITE, 0, gfx::Rect(0, 10, 200, 90));
71 window_->AddChild(child);
74 // Returns the hit test code if there is a resize shadow. Returns HTNOWHERE if
75 // there is no resize shadow.
76 int ResizeShadowHitTest() const {
77 ash::ResizeShadow* resize_shadow = ash::Shell::GetInstance()
78 ->resize_shadow_controller()
79 ->GetShadowForWindowForTest(window_);
80 return resize_shadow ? resize_shadow->GetLastHitTestForTest() : HTNOWHERE;
83 // Returns true if there is a resize shadow.
84 bool HasResizeShadow() const {
85 return ResizeShadowHitTest() != HTNOWHERE;
88 // Returns the current cursor type.
89 int GetCurrentCursorType() const {
90 CursorManagerTestApi test_api(ash::Shell::GetInstance()->cursor_manager());
91 return test_api.GetCurrentCursor().native_type();
94 // Called for each step of a scroll sequence initiated at the bottom right
95 // corner of |window_|. Tests whether the resize shadow is shown.
96 void ProcessBottomRightResizeGesture(ui::EventType type,
97 const gfx::Vector2dF& delta) {
98 if (type == ui::ET_GESTURE_SCROLL_END) {
99 // After gesture scroll ends, there should be no resize shadow.
100 EXPECT_FALSE(HasResizeShadow());
101 } else {
102 EXPECT_EQ(HTBOTTOMRIGHT, ResizeShadowHitTest());
106 aura::Window* window() {
107 return window_;
110 private:
111 aura::Window* window_;
113 DISALLOW_COPY_AND_ASSIGN(ResizeShadowAndCursorTest);
116 // Test whether the resize shadows are visible and the cursor type based on the
117 // mouse's position.
118 TEST_F(ResizeShadowAndCursorTest, MouseHover) {
119 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
120 ASSERT_TRUE(ash::wm::GetWindowState(window())->IsNormalStateType());
122 generator.MoveMouseTo(50, 50);
123 EXPECT_FALSE(HasResizeShadow());
124 EXPECT_EQ(ui::kCursorNull, GetCurrentCursorType());
126 generator.MoveMouseTo(gfx::Point(50, 0));
127 EXPECT_EQ(HTTOP, ResizeShadowHitTest());
128 EXPECT_EQ(ui::kCursorNorthResize, GetCurrentCursorType());
130 generator.MoveMouseTo(50, 50);
131 EXPECT_FALSE(HasResizeShadow());
132 EXPECT_EQ(ui::kCursorNull, GetCurrentCursorType());
134 generator.MoveMouseTo(200, 100);
135 EXPECT_EQ(HTBOTTOMRIGHT, ResizeShadowHitTest());
136 EXPECT_EQ(ui::kCursorSouthEastResize, GetCurrentCursorType());
138 generator.MoveMouseTo(50, 100);
139 EXPECT_EQ(HTBOTTOM, ResizeShadowHitTest());
140 EXPECT_EQ(ui::kCursorSouthResize, GetCurrentCursorType());
142 generator.MoveMouseTo(50, 100 + ash::kResizeOutsideBoundsSize - 1);
143 EXPECT_EQ(HTBOTTOM, ResizeShadowHitTest());
144 EXPECT_EQ(ui::kCursorSouthResize, GetCurrentCursorType());
146 generator.MoveMouseTo(50, 100 + ash::kResizeOutsideBoundsSize + 10);
147 EXPECT_FALSE(HasResizeShadow());
148 EXPECT_EQ(ui::kCursorNull, GetCurrentCursorType());
150 generator.MoveMouseTo(50, 100 - ash::kResizeInsideBoundsSize);
151 EXPECT_EQ(HTBOTTOM, ResizeShadowHitTest());
152 EXPECT_EQ(ui::kCursorSouthResize, GetCurrentCursorType());
154 generator.MoveMouseTo(50, 100 - ash::kResizeInsideBoundsSize - 10);
155 EXPECT_FALSE(HasResizeShadow());
156 EXPECT_EQ(ui::kCursorNull, GetCurrentCursorType());
159 // Test that the resize shadows stay visible and that the cursor stays the same
160 // as long as a user is resizing a window.
161 TEST_F(ResizeShadowAndCursorTest, MouseDrag) {
162 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
163 ASSERT_TRUE(ash::wm::GetWindowState(window())->IsNormalStateType());
164 gfx::Size initial_size(window()->bounds().size());
166 generator.MoveMouseTo(200, 50);
167 generator.PressLeftButton();
168 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
169 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
171 generator.MoveMouseTo(210, 50);
172 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
173 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
175 generator.ReleaseLeftButton();
176 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
177 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
179 gfx::Size new_size(window()->bounds().size());
180 EXPECT_NE(new_size.ToString(), initial_size.ToString());
183 // Test that the resize shadows stay visible while resizing a window via touch.
184 TEST_F(ResizeShadowAndCursorTest, Touch) {
185 ASSERT_TRUE(ash::wm::GetWindowState(window())->IsNormalStateType());
186 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
188 int start_x = 200 + ash::kResizeOutsideBoundsSize - 1;
189 int start_y = 100 + ash::kResizeOutsideBoundsSize - 1;
190 generator.GestureScrollSequenceWithCallback(
191 gfx::Point(start_x, start_y),
192 gfx::Point(start_x + 50, start_y + 50),
193 base::TimeDelta::FromMilliseconds(200),
195 base::Bind(&ResizeShadowAndCursorTest::ProcessBottomRightResizeGesture,
196 base::Unretained(this)));
199 // Test that the resize shadows are not visible and that the default cursor is
200 // used when the window is maximized.
201 TEST_F(ResizeShadowAndCursorTest, MaximizeRestore) {
202 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
203 ASSERT_TRUE(ash::wm::GetWindowState(window())->IsNormalStateType());
205 generator.MoveMouseTo(200, 50);
206 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
207 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
208 generator.MoveMouseTo(200 - ash::kResizeInsideBoundsSize, 50);
209 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
210 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
212 ash::wm::GetWindowState(window())->Maximize();
213 gfx::Rect bounds(window()->GetBoundsInRootWindow());
214 gfx::Point right_center(bounds.right() - 1,
215 (bounds.y() + bounds.bottom()) / 2);
216 generator.MoveMouseTo(right_center);
217 EXPECT_FALSE(HasResizeShadow());
218 EXPECT_EQ(ui::kCursorNull, GetCurrentCursorType());
220 ash::wm::GetWindowState(window())->Restore();
221 generator.MoveMouseTo(200, 50);
222 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
223 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
224 generator.MoveMouseTo(200 - ash::kResizeInsideBoundsSize, 50);
225 EXPECT_EQ(HTRIGHT, ResizeShadowHitTest());
226 EXPECT_EQ(ui::kCursorEastResize, GetCurrentCursorType());
229 } // namespace test
230 } // namespace ash