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