OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/browser/web_contents/aura/overscroll_window_animation.h" |
| 6 |
| 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" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 class OverscrollWindowAnimationTest |
| 18 : public OverscrollWindowAnimation::Delegate, |
| 19 public aura::test::AuraTestBase { |
| 20 public: |
| 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) {} |
| 28 |
| 29 ~OverscrollWindowAnimationTest() override {} |
| 30 |
| 31 OverscrollWindowAnimation* owa() { return owa_.get(); } |
| 32 |
| 33 // Set to true to return a window in the Create*Window functions, false to |
| 34 // return null. |
| 35 void set_create_window(bool create_window) { create_window_ = create_window; } |
| 36 |
| 37 // The following functions indicate if the events have been called on this |
| 38 // delegate. |
| 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_; } |
| 43 |
| 44 void ResetFlags() { |
| 45 overscroll_started_ = false; |
| 46 overscroll_completing_ = false; |
| 47 overscroll_completed_ = false; |
| 48 overscroll_aborted_ = false; |
| 49 } |
| 50 |
| 51 protected: |
| 52 // aura::test::AuraTestBase: |
| 53 void SetUp() override { |
| 54 aura::test::AuraTestBase::SetUp(); |
| 55 main_window_.reset(CreateNormalWindow(0, root_window(), nullptr)); |
| 56 ResetFlags(); |
| 57 create_window_ = true; |
| 58 last_window_id_ = 0; |
| 59 owa_.reset(new OverscrollWindowAnimation(this)); |
| 60 } |
| 61 |
| 62 void TearDown() override { |
| 63 owa_.reset(); |
| 64 main_window_.reset(); |
| 65 aura::test::AuraTestBase::TearDown(); |
| 66 } |
| 67 |
| 68 // OverscrollWindowAnimation::Delegate: |
| 69 scoped_ptr<aura::Window> CreateFrontWindow(const gfx::Rect& bounds) override { |
| 70 return CreateSlideWindow(bounds); |
| 71 } |
| 72 |
| 73 scoped_ptr<aura::Window> CreateBackWindow(const gfx::Rect& bounds) override { |
| 74 return CreateSlideWindow(bounds); |
| 75 } |
| 76 |
| 77 aura::Window* GetMainWindow() const override { return main_window_.get(); } |
| 78 |
| 79 void OnOverscrollCompleting() override { overscroll_completing_ = true; } |
| 80 |
| 81 void OnOverscrollCompleted(scoped_ptr<aura::Window> window) override { |
| 82 overscroll_completed_ = true; |
| 83 } |
| 84 |
| 85 void OnOverscrollCancelled() override { overscroll_aborted_ = true; } |
| 86 |
| 87 private: |
| 88 // The overscroll window animation under test. |
| 89 scoped_ptr<OverscrollWindowAnimation> owa_; |
| 90 |
| 91 scoped_ptr<aura::Window> CreateSlideWindow(const gfx::Rect& bounds) { |
| 92 overscroll_started_ = true; |
| 93 if (create_window_) { |
| 94 scoped_ptr<aura::Window> window( |
| 95 CreateNormalWindow(++last_window_id_, root_window(), nullptr)); |
| 96 window->SetBounds(bounds); |
| 97 return window.Pass(); |
| 98 } |
| 99 return nullptr; |
| 100 } |
| 101 |
| 102 // Controls if we return a window for the window creation callbacks or not. |
| 103 bool create_window_; |
| 104 |
| 105 // State flags. |
| 106 bool overscroll_started_; |
| 107 bool overscroll_completing_; |
| 108 bool overscroll_completed_; |
| 109 bool overscroll_aborted_; |
| 110 |
| 111 int last_window_id_; |
| 112 |
| 113 // The dummy target window we provide. |
| 114 scoped_ptr<aura::Window> main_window_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowAnimationTest); |
| 117 }; |
| 118 |
| 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()); |
| 126 |
| 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()); |
| 134 |
| 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()); |
| 142 } |
| 143 |
| 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()); |
| 155 } |
| 156 |
| 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()); |
| 167 } |
| 168 |
| 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 |
| 174 // finishes. |
| 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); |
| 183 |
| 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()); |
| 192 |
| 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()); |
| 200 |
| 201 // Complete the overscroll gesture. |
| 202 ResetFlags(); |
| 203 owa()->OnOverscrollComplete(OVERSCROLL_EAST); |
| 204 |
| 205 base::TimeDelta duration = settings.GetTransitionDuration(); |
| 206 test_controller.StartThreadedAnimationsIfNeeded(); |
| 207 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 208 |
| 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()); |
| 216 |
| 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()); |
| 224 } |
| 225 |
| 226 } // namespace content |
OLD | NEW |