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"
13 #include "ui/gfx/frame_time.h"
17 class OverscrollWindowAnimationTest
18 : public OverscrollWindowAnimation::Delegate
,
19 public aura::test::AuraTestBase
{
21 OverscrollWindowAnimationTest()
22 : create_window_(true),
23 overscroll_started_(false),
24 overscroll_completing_(false),
25 overscroll_completed_(false),
26 overscroll_aborted_(false),
27 main_window_(nullptr) {}
29 ~OverscrollWindowAnimationTest() override
{}
31 OverscrollWindowAnimation
* owa() { return owa_
.get(); }
33 // Set to true to return a window in the Create*Window functions, false to
35 void set_create_window(bool create_window
) { create_window_
= create_window
; }
37 // The following functions indicate if the events have been called on this
39 bool overscroll_started() { return overscroll_started_
; }
40 bool overscroll_completing() { return overscroll_completing_
; }
41 bool overscroll_completed() { return overscroll_completed_
; }
42 bool overscroll_aborted() { return overscroll_aborted_
; }
45 overscroll_started_
= false;
46 overscroll_completing_
= false;
47 overscroll_completed_
= false;
48 overscroll_aborted_
= false;
52 // aura::test::AuraTestBase:
53 void SetUp() override
{
54 aura::test::AuraTestBase::SetUp();
55 main_window_
.reset(CreateNormalWindow(0, root_window(), nullptr));
57 create_window_
= true;
59 owa_
.reset(new OverscrollWindowAnimation(this));
62 void TearDown() override
{
65 aura::test::AuraTestBase::TearDown();
68 // OverscrollWindowAnimation::Delegate:
69 scoped_ptr
<aura::Window
> CreateFrontWindow(const gfx::Rect
& bounds
) override
{
70 return CreateSlideWindow(bounds
);
73 scoped_ptr
<aura::Window
> CreateBackWindow(const gfx::Rect
& bounds
) override
{
74 return CreateSlideWindow(bounds
);
77 aura::Window
* GetMainWindow() const override
{ return main_window_
.get(); }
79 void OnOverscrollCompleting() override
{ overscroll_completing_
= true; }
81 void OnOverscrollCompleted(scoped_ptr
<aura::Window
> window
) override
{
82 overscroll_completed_
= true;
85 void OnOverscrollCancelled() override
{ overscroll_aborted_
= true; }
88 // The overscroll window animation under test.
89 scoped_ptr
<OverscrollWindowAnimation
> owa_
;
91 scoped_ptr
<aura::Window
> CreateSlideWindow(const gfx::Rect
& bounds
) {
92 overscroll_started_
= true;
94 scoped_ptr
<aura::Window
> window(
95 CreateNormalWindow(++last_window_id_
, root_window(), nullptr));
96 window
->SetBounds(bounds
);
102 // Controls if we return a window for the window creation callbacks or not.
106 bool overscroll_started_
;
107 bool overscroll_completing_
;
108 bool overscroll_completed_
;
109 bool overscroll_aborted_
;
113 // The dummy target window we provide.
114 scoped_ptr
<aura::Window
> main_window_
;
116 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowAnimationTest
);
119 // Tests a simple overscroll gesture.
120 TEST_F(OverscrollWindowAnimationTest
, BasicOverscroll
) {
121 EXPECT_FALSE(owa()->is_active());
122 EXPECT_FALSE(overscroll_started());
123 EXPECT_FALSE(overscroll_completing());
124 EXPECT_FALSE(overscroll_completed());
125 EXPECT_FALSE(overscroll_aborted());
127 // Start an OVERSCROLL_EAST gesture.
128 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
129 EXPECT_TRUE(owa()->is_active());
130 EXPECT_TRUE(overscroll_started());
131 EXPECT_FALSE(overscroll_completing());
132 EXPECT_FALSE(overscroll_completed());
133 EXPECT_FALSE(overscroll_aborted());
135 // Complete the overscroll.
136 owa()->OnOverscrollComplete(OVERSCROLL_EAST
);
137 EXPECT_FALSE(owa()->is_active());
138 EXPECT_TRUE(overscroll_started());
139 EXPECT_TRUE(overscroll_completing());
140 EXPECT_TRUE(overscroll_completed());
141 EXPECT_FALSE(overscroll_aborted());
144 // Tests aborting an overscroll gesture.
145 TEST_F(OverscrollWindowAnimationTest
, BasicAbort
) {
146 // Start an OVERSCROLL_EAST gesture.
147 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
148 // Abort the overscroll.
149 owa()->OnOverscrollModeChange(OVERSCROLL_EAST
, OVERSCROLL_NONE
);
150 EXPECT_FALSE(owa()->is_active());
151 EXPECT_TRUE(overscroll_started());
152 EXPECT_FALSE(overscroll_completing());
153 EXPECT_FALSE(overscroll_completed());
154 EXPECT_TRUE(overscroll_aborted());
157 // Tests starting an overscroll gesture when the slide window cannot be created.
158 TEST_F(OverscrollWindowAnimationTest
, BasicCannotNavigate
) {
159 set_create_window(false);
160 // Start an OVERSCROLL_EAST gesture.
161 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
162 EXPECT_FALSE(owa()->is_active());
163 EXPECT_TRUE(overscroll_started());
164 EXPECT_FALSE(overscroll_completing());
165 EXPECT_FALSE(overscroll_completed());
166 EXPECT_FALSE(overscroll_aborted());
169 // Tests starting an overscroll gesture while another one was in progress
170 // completes the first one.
171 TEST_F(OverscrollWindowAnimationTest
, NewOverscrollCompletesPreviousGesture
) {
172 // This test requires a normal animation duration so that
173 // OnImplicitAnimationsCancelled is not called as soon as the first overscroll
175 ui::ScopedAnimationDurationScaleMode
normal_duration(
176 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
177 ui::LayerAnimator
* animator
= GetMainWindow()->layer()->GetAnimator();
178 ui::ScopedLayerAnimationSettings
settings(animator
);
179 animator
->set_disable_timer_for_test(true);
180 ui::LayerAnimatorTestController
test_controller(animator
);
181 // Start an OVERSCROLL_EAST gesture.
182 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
184 // Finishes the OVERSCROLL_EAST gesture. At this point the window should be
185 // being animated to its final position.
186 owa()->OnOverscrollComplete(OVERSCROLL_EAST
);
187 EXPECT_TRUE(owa()->is_active());
188 EXPECT_TRUE(overscroll_started());
189 EXPECT_TRUE(overscroll_completing());
190 EXPECT_FALSE(overscroll_completed());
191 EXPECT_FALSE(overscroll_aborted());
193 // Start another OVERSCROLL_EAST gesture.
194 owa()->OnOverscrollModeChange(OVERSCROLL_NONE
, OVERSCROLL_EAST
);
195 EXPECT_TRUE(owa()->is_active());
196 EXPECT_TRUE(overscroll_started());
197 EXPECT_TRUE(overscroll_completing());
198 EXPECT_TRUE(overscroll_completed());
199 EXPECT_FALSE(overscroll_aborted());
201 // Complete the overscroll gesture.
203 owa()->OnOverscrollComplete(OVERSCROLL_EAST
);
205 base::TimeDelta duration
= settings
.GetTransitionDuration();
206 test_controller
.StartThreadedAnimationsIfNeeded();
207 base::TimeTicks start_time
= gfx::FrameTime::Now();
209 // Halfway through the animation, OverscrollCompleting should have been fired.
210 animator
->Step(start_time
+ duration
/ 2);
211 EXPECT_TRUE(owa()->is_active());
212 EXPECT_FALSE(overscroll_started());
213 EXPECT_TRUE(overscroll_completing());
214 EXPECT_FALSE(overscroll_completed());
215 EXPECT_FALSE(overscroll_aborted());
217 // The animation has finished, OverscrollCompleted should have been fired.
218 animator
->Step(start_time
+ duration
);
219 EXPECT_FALSE(owa()->is_active());
220 EXPECT_FALSE(overscroll_started());
221 EXPECT_TRUE(overscroll_completing());
222 EXPECT_TRUE(overscroll_completed());
223 EXPECT_FALSE(overscroll_aborted());
226 } // namespace content