| 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 } // namespace |
| 36 |
| 37 namespace test { |
| 38 |
| 39 class AnimationObserver : public ScreenRotationAnimatorObserver { |
| 40 public: |
| 41 AnimationObserver(); |
| 42 |
| 43 bool notified() { return notified_; } |
| 44 |
| 45 void OnEndedOrAbortedScreenRotationAnimation( |
| 46 ScreenRotationAnimator* animator) override; |
| 47 |
| 48 private: |
| 49 bool notified_; |
| 50 }; |
| 51 |
| 52 AnimationObserver::AnimationObserver() : notified_(false) {} |
| 53 |
| 54 void AnimationObserver::OnEndedOrAbortedScreenRotationAnimation( |
| 55 ScreenRotationAnimator* animator) { |
| 56 notified_ = true; |
| 57 } |
| 58 |
| 59 class ScreenRotationAnimatorTest : public AshTestBase { |
| 60 public: |
| 61 ScreenRotationAnimatorTest() {} |
| 62 ~ScreenRotationAnimatorTest() override {} |
| 63 |
| 64 // AshTestBase: |
| 65 void SetUp() override; |
| 66 |
| 67 protected: |
| 68 int64_t display_id() { return display_.id(); } |
| 69 |
| 70 ScreenRotationAnimator* animator() { return animator_.get(); } |
| 71 |
| 72 test::ScreenRotationAnimatorTestApi* test_api() { return test_api_.get(); } |
| 73 |
| 74 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> non_zero_duration_mode_; |
| 75 |
| 76 private: |
| 77 display::Display display_; |
| 78 |
| 79 std::unique_ptr<ScreenRotationAnimator> animator_; |
| 80 |
| 81 std::unique_ptr<test::ScreenRotationAnimatorTestApi> test_api_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimatorTest); |
| 84 }; |
| 85 |
| 86 void ScreenRotationAnimatorTest::SetUp() { |
| 87 AshTestBase::SetUp(); |
| 88 |
| 89 display_ = display::Screen::GetScreen()->GetPrimaryDisplay(); |
| 90 animator_.reset( |
| 91 base::MakeUnique<ScreenRotationAnimator>(display_.id()).release()); |
| 92 test_api_.reset( |
| 93 base::MakeUnique<test::ScreenRotationAnimatorTestApi>(animator_.get()) |
| 94 .release()); |
| 95 test_api()->SetDisableAnimationTimers(true); |
| 96 |
| 97 non_zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( |
| 98 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION)); |
| 99 } |
| 100 |
| 101 TEST_F(ScreenRotationAnimatorTest, ShouldNotifyObserver) { |
| 102 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 103 if (WmShell::Get()->IsRunningInMash()) { |
| 104 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 105 display_id()); |
| 106 return; |
| 107 } |
| 108 |
| 109 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 110 AnimationObserver observer; |
| 111 animator()->SetScreenRotationAnimatorObserver(&observer); |
| 112 EXPECT_FALSE(observer.notified()); |
| 113 |
| 114 animator()->Rotate(display::Display::ROTATE_90, |
| 115 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 116 EXPECT_FALSE(observer.notified()); |
| 117 |
| 118 test_api()->CompleteAnimations(); |
| 119 EXPECT_TRUE(observer.notified()); |
| 120 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 121 } |
| 122 |
| 123 TEST_F(ScreenRotationAnimatorTest, RotatesToDifferentRotation) { |
| 124 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 125 if (WmShell::Get()->IsRunningInMash()) { |
| 126 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 127 display_id()); |
| 128 return; |
| 129 } |
| 130 |
| 131 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 132 animator()->Rotate(display::Display::ROTATE_90, |
| 133 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 134 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 135 |
| 136 test_api()->CompleteAnimations(); |
| 137 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 138 } |
| 139 |
| 140 TEST_F(ScreenRotationAnimatorTest, ShouldNotRotateTheSameRotation) { |
| 141 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 142 if (WmShell::Get()->IsRunningInMash()) { |
| 143 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 144 display_id()); |
| 145 return; |
| 146 } |
| 147 |
| 148 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 149 animator()->Rotate(display::Display::ROTATE_0, |
| 150 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 151 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 152 } |
| 153 |
| 154 // Simulates the situation that if there is a new rotation request during |
| 155 // animation, it should stop the animation immediately and add the new rotation |
| 156 // request to the |last_pending_request_|. |
| 157 TEST_F(ScreenRotationAnimatorTest, AddsToPendingRequest) { |
| 158 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 159 if (WmShell::Get()->IsRunningInMash()) { |
| 160 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 161 display_id()); |
| 162 return; |
| 163 } |
| 164 |
| 165 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 166 animator()->Rotate(display::Display::ROTATE_90, |
| 167 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 168 animator()->Rotate(display::Display::ROTATE_180, |
| 169 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 170 EXPECT_EQ(display::Display::ROTATE_180, GetDisplayRotation(display_id())); |
| 171 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 172 |
| 173 test_api()->CompleteAnimations(); |
| 174 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 175 } |
| 176 |
| 177 TEST_F(ScreenRotationAnimatorTest, ShouldCompleteAnimations) { |
| 178 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 179 if (WmShell::Get()->IsRunningInMash()) { |
| 180 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 181 display_id()); |
| 182 return; |
| 183 } |
| 184 |
| 185 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 186 animator()->Rotate(display::Display::ROTATE_90, |
| 187 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 188 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 189 |
| 190 animator()->Rotate(display::Display::ROTATE_180, |
| 191 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 192 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 193 |
| 194 animator()->Rotate(display::Display::ROTATE_270, |
| 195 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 196 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 197 |
| 198 test_api()->CompleteAnimations(); |
| 199 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 200 } |
| 201 |
| 202 } // namespace test |
| 203 } // namespace ash |
| OLD | NEW |