1 // Copyright 2015 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 "content/browser/web_contents/aura/overscroll_window_animation.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/aura/test/aura_test_base.h"
9 #include "ui/aura/window.h"
10 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
11 #include "ui/compositor/scoped_layer_animation_settings.h"
12 #include "ui/compositor/test/layer_animator_test_controller.h"
16 class OverscrollWindowAnimationTest
17 : public OverscrollWindowAnimation::Delegate
,
18 public aura::test::AuraTestBase
{
20 OverscrollWindowAnimationTest()
21 : create_window_(true),
22 overscroll_started_(false),
23 overscroll_completing_(false),
24 overscroll_completed_(false),
25 overscroll_aborted_(false),
26 main_window_(nullptr) {}
28 ~OverscrollWindowAnimationTest() override
{}
30 OverscrollWindowAnimation
* owa() { return owa_
.get(); }
32 // Set to true to return a window in the Create*Window functions, false to
34 void set_create_window(bool create_window
) { create_window_
= create_window
; }
36 // The following functions indicate if the events have been called on this
38 bool overscroll_started() { return overscroll_started_
; }
39 bool overscroll_completing() { return overscroll_completing_
; }
40 bool overscroll_completed() { return overscroll_completed_
; }
41 bool overscroll_aborted() { return overscroll_aborted_
; }
44 overscroll_started_
= false;
45 overscroll_completing_
= false;
46 overscroll_completed_
= false;
47 overscroll_aborted_
= false;
51 // aura::test::AuraTestBase:
52 void SetUp() override
{
53 aura::test::AuraTestBase::SetUp();
54 main_window_
.reset(CreateNormalWindow(0, root_window(), nullptr));
56 create_window_
= true;
58 owa_
.reset(new OverscrollWindowAnimation(this));
61 void TearDown() override
{
64 aura::test::AuraTestBase::TearDown();
67 // OverscrollWindowAnimation::Delegate:
68 scoped_ptr
<aura::Window
> CreateFrontWindow(const gfx::Rect
& bounds
) override
{
69 return CreateSlideWindow(bounds
);
72 scoped_ptr
<aura::Window
> CreateBackWindow(const gfx::Rect
& bounds
) override
{
73 return CreateSlideWindow(bounds
);
76 aura::Window
* GetMainWindow() const override
{ return main_window_
.get(); }
78 void OnOverscrollCompleting() override
{ overscroll_completing_
= true; }
80 void OnOverscrollCompleted(scoped_ptr
<aura::Window
> window
) override
{
81 overscroll_completed_
= true;
84 void OnOverscrollCancelled() override
{ overscroll_aborted_
= true; }
87 // The overscroll window animation under test.
88 scoped_ptr
<OverscrollWindowAnimation
> owa_
;
90 scoped_ptr
<aura::Window
> CreateSlideWindow(const gfx::Rect
& bounds
) {
91 overscroll_started_
= true;
93 scoped_ptr
<aura::Window
> window(
94 CreateNormalWindow(++last_window_id_
, root_window(), nullptr));
95 window
->SetBounds(bounds
);
101 // Controls if we return a window for the window creation callbacks or not.
105 bool overscroll_started_
;
106 bool overscroll_completing_
;
107 bool overscroll_completed_
;
108 bool overscroll_aborted_
;
112 // The dummy target window we provide.
113 scoped_ptr
<aura::Window
> main_window_
;
115 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowAnimationTest
);
118 // Tests a simple overscroll gesture.
119 TEST_F(OverscrollWindowAnimationTest
, BasicOverscroll
) {
120 EXPECT_FALSE(owa()->is_active());
121 EXPECT_FALSE(overscroll_started());
122 EXPECT_FALSE(overscroll_completing());
123 EXPECT_FALSE(overscroll_completed());
124 EXPECT_FALSE(overscroll_aborted());
126 // Start an OVERSCROLL_EAST gesture.
127 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
128 EXPECT_TRUE(owa()->is_active());
129 EXPECT_TRUE(overscroll_started());
130 EXPECT_FALSE(overscroll_completing());
131 EXPECT_FALSE(overscroll_completed());
132 EXPECT_FALSE(overscroll_aborted());
134 // Complete the overscroll.
135 owa()->OnOverscrollComplete(OVERSCROLL_EAST
);
136 EXPECT_FALSE(owa()->is_active());
137 EXPECT_TRUE(overscroll_started());
138 EXPECT_TRUE(overscroll_completing());
139 EXPECT_TRUE(overscroll_completed());
140 EXPECT_FALSE(overscroll_aborted());
143 // Tests aborting an overscroll gesture.
144 TEST_F(OverscrollWindowAnimationTest
, BasicAbort
) {
145 // Start an OVERSCROLL_EAST gesture.
146 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
147 // Abort the overscroll.
148 owa()->OnOverscrollModeChange(OVERSCROLL_EAST
, OVERSCROLL_NONE
);
149 EXPECT_FALSE(owa()->is_active());
150 EXPECT_TRUE(overscroll_started());
151 EXPECT_FALSE(overscroll_completing());
152 EXPECT_FALSE(overscroll_completed());
153 EXPECT_TRUE(overscroll_aborted());
156 // Tests starting an overscroll gesture when the slide window cannot be created.
157 TEST_F(OverscrollWindowAnimationTest
, BasicCannotNavigate
) {
158 set_create_window(false);
159 // Start an OVERSCROLL_EAST gesture.
160 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
161 EXPECT_FALSE(owa()->is_active());
162 EXPECT_TRUE(overscroll_started());
163 EXPECT_FALSE(overscroll_completing());
164 EXPECT_FALSE(overscroll_completed());
165 EXPECT_FALSE(overscroll_aborted());
168 // Tests starting an overscroll gesture while another one was in progress
169 // completes the first one.
170 TEST_F(OverscrollWindowAnimationTest
, NewOverscrollCompletesPreviousGesture
) {
171 // This test requires a normal animation duration so that
172 // OnImplicitAnimationsCancelled is not called as soon as the first overscroll
174 ui::ScopedAnimationDurationScaleMode
normal_duration(
175 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
176 ui::LayerAnimator
* animator
= GetMainWindow()->layer()->GetAnimator();
177 ui::ScopedLayerAnimationSettings
settings(animator
);
178 animator
->set_disable_timer_for_test(true);
179 ui::LayerAnimatorTestController
test_controller(animator
);
180 // Start an OVERSCROLL_EAST gesture.
181 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
183 // Finishes the OVERSCROLL_EAST gesture. At this point the window should be
184 // being animated to its final position.
185 owa()->OnOverscrollComplete(OVERSCROLL_EAST
);
186 EXPECT_TRUE(owa()->is_active());
187 EXPECT_TRUE(overscroll_started());
188 EXPECT_TRUE(overscroll_completing());
189 EXPECT_FALSE(overscroll_completed());
190 EXPECT_FALSE(overscroll_aborted());
192 // Start another OVERSCROLL_EAST gesture.
193 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
194 EXPECT_TRUE(owa()->is_active());
195 EXPECT_TRUE(overscroll_started());
196 EXPECT_TRUE(overscroll_completing());
197 EXPECT_TRUE(overscroll_completed());
198 EXPECT_FALSE(overscroll_aborted());
200 // Complete the overscroll gesture.
202 owa()->OnOverscrollComplete(OVERSCROLL_EAST
);
204 base::TimeDelta duration
= settings
.GetTransitionDuration();
205 test_controller
.StartThreadedAnimationsIfNeeded();
206 base::TimeTicks start_time
= base::TimeTicks::Now();
208 // Halfway through the animation, OverscrollCompleting should have been fired.
209 animator
->Step(start_time
+ duration
/ 2);
210 EXPECT_TRUE(owa()->is_active());
211 EXPECT_FALSE(overscroll_started());
212 EXPECT_TRUE(overscroll_completing());
213 EXPECT_FALSE(overscroll_completed());
214 EXPECT_FALSE(overscroll_aborted());
216 // The animation has finished, OverscrollCompleted should have been fired.
217 animator
->Step(start_time
+ duration
);
218 EXPECT_FALSE(owa()->is_active());
219 EXPECT_FALSE(overscroll_started());
220 EXPECT_TRUE(overscroll_completing());
221 EXPECT_TRUE(overscroll_completed());
222 EXPECT_FALSE(overscroll_aborted());
225 } // namespace content