| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/rotator/screen_rotation_animator.h" |
| 6 #include "ash/common/wm_shell.h" |
| 7 #include "ash/rotator/screen_rotation_animator_observer.h" |
| 8 #include "ash/rotator/test/screen_rotation_animator_test_api.h" |
| 9 #include "ash/shell.h" |
| 10 #include "ash/test/ash_test_base.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
| 13 #include "ui/display/display.h" |
| 14 #include "ui/display/manager/display_manager.h" |
| 15 #include "ui/display/screen.h" |
| 16 |
| 17 namespace ash { |
| 18 |
| 19 namespace { |
| 20 |
| 21 display::Display::Rotation GetDisplayRotation(int64_t display_id) { |
| 22 return Shell::GetInstance() |
| 23 ->display_manager() |
| 24 ->GetDisplayInfo(display_id) |
| 25 .GetActiveRotation(); |
| 26 } |
| 27 |
| 28 void SetDisplayRotation(int64_t display_id, |
| 29 display::Display::Rotation rotation) { |
| 30 Shell::GetInstance()->display_manager()->SetDisplayRotation( |
| 31 display_id, rotation, |
| 32 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 33 } |
| 34 |
| 35 class AnimationObserver : public ScreenRotationAnimatorObserver { |
| 36 public: |
| 37 AnimationObserver() {} |
| 38 |
| 39 bool notified() const { return notified_; } |
| 40 |
| 41 void OnScreenRotationAnimationFinished( |
| 42 ScreenRotationAnimator* animator) override { |
| 43 notified_ = true; |
| 44 } |
| 45 |
| 46 private: |
| 47 bool notified_ = false; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(AnimationObserver); |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class ScreenRotationAnimatorTest : public test::AshTestBase { |
| 55 public: |
| 56 ScreenRotationAnimatorTest() {} |
| 57 ~ScreenRotationAnimatorTest() override {} |
| 58 |
| 59 // AshTestBase: |
| 60 void SetUp() override; |
| 61 |
| 62 protected: |
| 63 int64_t display_id() const { return display_.id(); } |
| 64 |
| 65 ScreenRotationAnimator* animator() { return animator_.get(); } |
| 66 |
| 67 test::ScreenRotationAnimatorTestApi* test_api() { return test_api_.get(); } |
| 68 |
| 69 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> non_zero_duration_mode_; |
| 70 |
| 71 private: |
| 72 display::Display display_; |
| 73 |
| 74 std::unique_ptr<ScreenRotationAnimator> animator_; |
| 75 |
| 76 std::unique_ptr<test::ScreenRotationAnimatorTestApi> test_api_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimatorTest); |
| 79 }; |
| 80 |
| 81 void ScreenRotationAnimatorTest::SetUp() { |
| 82 AshTestBase::SetUp(); |
| 83 |
| 84 display_ = display::Screen::GetScreen()->GetPrimaryDisplay(); |
| 85 animator_ = base::MakeUnique<ScreenRotationAnimator>(display_.id()); |
| 86 test_api_ = |
| 87 base::MakeUnique<test::ScreenRotationAnimatorTestApi>(animator_.get()); |
| 88 test_api()->DisableAnimationTimers(); |
| 89 non_zero_duration_mode_ = |
| 90 base::MakeUnique<ui::ScopedAnimationDurationScaleMode>( |
| 91 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION); |
| 92 } |
| 93 |
| 94 TEST_F(ScreenRotationAnimatorTest, ShouldNotifyObserver) { |
| 95 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 96 if (WmShell::Get()->IsRunningInMash()) { |
| 97 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 98 display_id()); |
| 99 return; |
| 100 } |
| 101 |
| 102 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 103 AnimationObserver observer; |
| 104 animator()->AddScreenRotationAnimatorObserver(&observer); |
| 105 EXPECT_FALSE(observer.notified()); |
| 106 |
| 107 animator()->Rotate(display::Display::ROTATE_90, |
| 108 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 109 EXPECT_FALSE(observer.notified()); |
| 110 |
| 111 test_api()->CompleteAnimations(); |
| 112 EXPECT_TRUE(observer.notified()); |
| 113 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 114 animator()->RemoveScreenRotationAnimatorObserver(&observer); |
| 115 } |
| 116 |
| 117 TEST_F(ScreenRotationAnimatorTest, ShouldNotifyObserverOnce) { |
| 118 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 119 if (WmShell::Get()->IsRunningInMash()) { |
| 120 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 121 display_id()); |
| 122 return; |
| 123 } |
| 124 |
| 125 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 126 AnimationObserver observer; |
| 127 animator()->AddScreenRotationAnimatorObserver(&observer); |
| 128 EXPECT_FALSE(observer.notified()); |
| 129 |
| 130 animator()->Rotate(display::Display::ROTATE_90, |
| 131 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 132 EXPECT_FALSE(observer.notified()); |
| 133 |
| 134 animator()->Rotate(display::Display::ROTATE_180, |
| 135 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 136 EXPECT_FALSE(observer.notified()); |
| 137 |
| 138 test_api()->CompleteAnimations(); |
| 139 EXPECT_TRUE(observer.notified()); |
| 140 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 141 animator()->RemoveScreenRotationAnimatorObserver(&observer); |
| 142 } |
| 143 |
| 144 TEST_F(ScreenRotationAnimatorTest, RotatesToDifferentRotation) { |
| 145 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 146 if (WmShell::Get()->IsRunningInMash()) { |
| 147 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 148 display_id()); |
| 149 return; |
| 150 } |
| 151 |
| 152 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 153 animator()->Rotate(display::Display::ROTATE_90, |
| 154 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 155 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 156 |
| 157 test_api()->CompleteAnimations(); |
| 158 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 159 } |
| 160 |
| 161 TEST_F(ScreenRotationAnimatorTest, ShouldNotRotateTheSameRotation) { |
| 162 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 163 if (WmShell::Get()->IsRunningInMash()) { |
| 164 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 165 display_id()); |
| 166 return; |
| 167 } |
| 168 |
| 169 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 170 animator()->Rotate(display::Display::ROTATE_0, |
| 171 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 172 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 173 } |
| 174 |
| 175 // Simulates the situation that if there is a new rotation request during |
| 176 // animation, it should stop the animation immediately and add the new rotation |
| 177 // request to the |last_pending_request_|. |
| 178 TEST_F(ScreenRotationAnimatorTest, RotatesDuringRotation) { |
| 179 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 180 if (WmShell::Get()->IsRunningInMash()) { |
| 181 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 182 display_id()); |
| 183 return; |
| 184 } |
| 185 |
| 186 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 187 animator()->Rotate(display::Display::ROTATE_90, |
| 188 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 189 animator()->Rotate(display::Display::ROTATE_180, |
| 190 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 191 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 192 |
| 193 test_api()->CompleteAnimations(); |
| 194 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 195 EXPECT_EQ(display::Display::ROTATE_180, GetDisplayRotation(display_id())); |
| 196 } |
| 197 |
| 198 // If there are multiple requests queued during animation, it should process the |
| 199 // last request and finish the rotation animation. |
| 200 TEST_F(ScreenRotationAnimatorTest, ShouldCompleteAnimations) { |
| 201 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 202 if (WmShell::Get()->IsRunningInMash()) { |
| 203 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 204 display_id()); |
| 205 return; |
| 206 } |
| 207 |
| 208 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 209 animator()->Rotate(display::Display::ROTATE_90, |
| 210 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 211 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 212 |
| 213 animator()->Rotate(display::Display::ROTATE_180, |
| 214 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 215 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 216 |
| 217 animator()->Rotate(display::Display::ROTATE_270, |
| 218 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 219 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 220 |
| 221 test_api()->CompleteAnimations(); |
| 222 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 223 EXPECT_EQ(display::Display::ROTATE_270, GetDisplayRotation(display_id())); |
| 224 } |
| 225 |
| 226 } // namespace ash |
| OLD | NEW |