OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/rotator/screen_rotation_animator.h" | 5 #include "ash/rotator/screen_rotation_animator.h" |
6 #include "ash/common/wm_shell.h" | 6 #include "ash/common/wm_shell.h" |
7 #include "ash/rotator/screen_rotation_animator_observer.h" | 7 #include "ash/rotator/screen_rotation_animator_observer.h" |
8 #include "ash/rotator/test/screen_rotation_animator_test_api.h" | 8 #include "ash/rotator/test/screen_rotation_animator_test_api.h" |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/test/ash_test_base.h" | 10 #include "ash/test/ash_test_base.h" |
| 11 #include "base/callback_forward.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "cc/output/copy_output_request.h" |
| 15 #include "cc/output/copy_output_result.h" |
12 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 16 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
13 #include "ui/display/display.h" | 17 #include "ui/display/display.h" |
14 #include "ui/display/manager/display_manager.h" | 18 #include "ui/display/manager/display_manager.h" |
15 #include "ui/display/screen.h" | 19 #include "ui/display/screen.h" |
16 | 20 |
17 namespace ash { | 21 namespace ash { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 display::Display::Rotation GetDisplayRotation(int64_t display_id) { | 25 display::Display::Rotation GetDisplayRotation(int64_t display_id) { |
(...skipping 20 matching lines...) Expand all Loading... |
42 ScreenRotationAnimator* animator) override { | 46 ScreenRotationAnimator* animator) override { |
43 notified_ = true; | 47 notified_ = true; |
44 } | 48 } |
45 | 49 |
46 private: | 50 private: |
47 bool notified_ = false; | 51 bool notified_ = false; |
48 | 52 |
49 DISALLOW_COPY_AND_ASSIGN(AnimationObserver); | 53 DISALLOW_COPY_AND_ASSIGN(AnimationObserver); |
50 }; | 54 }; |
51 | 55 |
| 56 class ScreenRotationTestAnimator : public ScreenRotationAnimator { |
| 57 public: |
| 58 explicit ScreenRotationTestAnimator(int64_t display_id); |
| 59 ~ScreenRotationTestAnimator() override {} |
| 60 |
| 61 void set_callback(const base::Closure& callback) { callback_ = callback; } |
| 62 |
| 63 protected: |
| 64 std::unique_ptr<cc::CopyOutputRequest> CreateAfterCopyCallback( |
| 65 std::unique_ptr<ScreenRotationRequest> rotation_request) override; |
| 66 |
| 67 private: |
| 68 void OnRootLayerCopiedBeforeRotationForTest( |
| 69 std::unique_ptr<ScreenRotationRequest> rotation_request, |
| 70 std::unique_ptr<cc::CopyOutputResult> result); |
| 71 |
| 72 base::Closure callback_; |
| 73 base::WeakPtrFactory<ScreenRotationTestAnimator> weak_factory_test_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(ScreenRotationTestAnimator); |
| 76 }; |
| 77 |
| 78 ScreenRotationTestAnimator::ScreenRotationTestAnimator(int64_t display_id) |
| 79 : ScreenRotationAnimator(display_id), weak_factory_test_(this) {} |
| 80 |
| 81 void ScreenRotationTestAnimator::OnRootLayerCopiedBeforeRotationForTest( |
| 82 std::unique_ptr<ScreenRotationRequest> rotation_request, |
| 83 std::unique_ptr<cc::CopyOutputResult> result) { |
| 84 callback_.Run(); |
| 85 OnRootLayerCopiedBeforeRotation(std::move(rotation_request), |
| 86 std::move(result)); |
| 87 } |
| 88 |
| 89 std::unique_ptr<cc::CopyOutputRequest> |
| 90 ScreenRotationTestAnimator::CreateAfterCopyCallback( |
| 91 std::unique_ptr<ScreenRotationRequest> rotation_request) { |
| 92 return cc::CopyOutputRequest::CreateRequest(base::Bind( |
| 93 &ScreenRotationTestAnimator::OnRootLayerCopiedBeforeRotationForTest, |
| 94 weak_factory_test_.GetWeakPtr(), base::Passed(&rotation_request))); |
| 95 } |
| 96 |
52 } // namespace | 97 } // namespace |
53 | 98 |
54 class ScreenRotationAnimatorTest : public test::AshTestBase { | 99 class ScreenRotationAnimatorTest : public test::AshTestBase { |
55 public: | 100 public: |
56 ScreenRotationAnimatorTest() {} | 101 ScreenRotationAnimatorTest() {} |
57 ~ScreenRotationAnimatorTest() override {} | 102 ~ScreenRotationAnimatorTest() override {} |
58 | 103 |
59 // AshTestBase: | 104 // AshTestBase: |
60 void SetUp() override; | 105 void SetUp() override; |
61 | 106 |
| 107 void DoNothingCallback(); |
| 108 |
| 109 void RemoveSecondaryDisplay(const std::string& specs); |
| 110 |
62 protected: | 111 protected: |
63 int64_t display_id() const { return display_.id(); } | 112 int64_t display_id() const { return display_.id(); } |
64 | 113 |
65 ScreenRotationAnimator* animator() { return animator_.get(); } | 114 ScreenRotationTestAnimator* animator() { return animator_.get(); } |
| 115 |
| 116 void SetSecreenRotatioAnimator(int64_t display_id); |
66 | 117 |
67 test::ScreenRotationAnimatorTestApi* test_api() { return test_api_.get(); } | 118 test::ScreenRotationAnimatorTestApi* test_api() { return test_api_.get(); } |
68 | 119 |
| 120 void AddSecondaryDisplay(const std::string& specs); |
| 121 |
| 122 void WaitForCopyCallback(); |
| 123 |
69 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> non_zero_duration_mode_; | 124 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> non_zero_duration_mode_; |
| 125 std::unique_ptr<base::RunLoop> run_loop_; |
70 | 126 |
71 private: | 127 private: |
72 display::Display display_; | 128 display::Display display_; |
73 | 129 |
74 std::unique_ptr<ScreenRotationAnimator> animator_; | 130 std::unique_ptr<ScreenRotationTestAnimator> animator_; |
75 | 131 |
76 std::unique_ptr<test::ScreenRotationAnimatorTestApi> test_api_; | 132 std::unique_ptr<test::ScreenRotationAnimatorTestApi> test_api_; |
77 | 133 |
78 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimatorTest); | 134 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimatorTest); |
79 }; | 135 }; |
80 | 136 |
| 137 void ScreenRotationAnimatorTest::DoNothingCallback() { |
| 138 run_loop_->Quit(); |
| 139 } |
| 140 |
| 141 void ScreenRotationAnimatorTest::RemoveSecondaryDisplay( |
| 142 const std::string& specs) { |
| 143 UpdateDisplay(specs); |
| 144 run_loop_->Quit(); |
| 145 } |
| 146 |
81 void ScreenRotationAnimatorTest::SetUp() { | 147 void ScreenRotationAnimatorTest::SetUp() { |
82 AshTestBase::SetUp(); | 148 AshTestBase::SetUp(); |
83 | 149 |
84 display_ = display::Screen::GetScreen()->GetPrimaryDisplay(); | 150 display_ = display::Screen::GetScreen()->GetPrimaryDisplay(); |
85 animator_ = base::MakeUnique<ScreenRotationAnimator>(display_.id()); | 151 SetSecreenRotatioAnimator(display_.id()); |
| 152 } |
| 153 |
| 154 void ScreenRotationAnimatorTest::SetSecreenRotatioAnimator(int64_t display_id) { |
| 155 animator_ = base::MakeUnique<ScreenRotationTestAnimator>(display_id); |
86 test_api_ = | 156 test_api_ = |
87 base::MakeUnique<test::ScreenRotationAnimatorTestApi>(animator_.get()); | 157 base::MakeUnique<test::ScreenRotationAnimatorTestApi>(animator_.get()); |
88 test_api()->DisableAnimationTimers(); | 158 test_api()->DisableAnimationTimers(); |
89 non_zero_duration_mode_ = | 159 non_zero_duration_mode_ = |
90 base::MakeUnique<ui::ScopedAnimationDurationScaleMode>( | 160 base::MakeUnique<ui::ScopedAnimationDurationScaleMode>( |
91 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION); | 161 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION); |
92 } | 162 } |
93 | 163 |
| 164 void ScreenRotationAnimatorTest::AddSecondaryDisplay(const std::string& specs) { |
| 165 UpdateDisplay(specs); |
| 166 } |
| 167 |
| 168 void ScreenRotationAnimatorTest::WaitForCopyCallback() { |
| 169 run_loop_.reset(new base::RunLoop); |
| 170 run_loop_->Run(); |
| 171 } |
| 172 |
94 TEST_F(ScreenRotationAnimatorTest, ShouldNotifyObserver) { | 173 TEST_F(ScreenRotationAnimatorTest, ShouldNotifyObserver) { |
95 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. | 174 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
96 if (WmShell::Get()->IsRunningInMash()) { | 175 if (WmShell::Get()->IsRunningInMash()) { |
97 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != | 176 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
98 display_id()); | 177 display_id()); |
99 return; | 178 return; |
100 } | 179 } |
101 | 180 |
102 SetDisplayRotation(display_id(), display::Display::ROTATE_0); | 181 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
103 AnimationObserver observer; | 182 AnimationObserver observer; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 295 |
217 animator()->Rotate(display::Display::ROTATE_270, | 296 animator()->Rotate(display::Display::ROTATE_270, |
218 display::Display::RotationSource::ROTATION_SOURCE_USER); | 297 display::Display::RotationSource::ROTATION_SOURCE_USER); |
219 EXPECT_TRUE(test_api()->HasActiveAnimations()); | 298 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
220 | 299 |
221 test_api()->CompleteAnimations(); | 300 test_api()->CompleteAnimations(); |
222 EXPECT_FALSE(test_api()->HasActiveAnimations()); | 301 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
223 EXPECT_EQ(display::Display::ROTATE_270, GetDisplayRotation(display_id())); | 302 EXPECT_EQ(display::Display::ROTATE_270, GetDisplayRotation(display_id())); |
224 } | 303 } |
225 | 304 |
| 305 // Test enable smooth screen rotation code path. |
| 306 TEST_F(ScreenRotationAnimatorTest, RotatesToDifferentRotationWithCopyCallback) { |
| 307 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 308 if (WmShell::Get()->IsRunningInMash()) { |
| 309 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 310 display_id()); |
| 311 return; |
| 312 } |
| 313 |
| 314 SetDisplayRotation(display_id(), display::Display::ROTATE_0); |
| 315 animator()->set_callback(base::Bind( |
| 316 &ScreenRotationAnimatorTest::DoNothingCallback, base::Unretained(this))); |
| 317 test_api()->EnableSmoothRotation(); |
| 318 animator()->Rotate(display::Display::ROTATE_90, |
| 319 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 320 WaitForCopyCallback(); |
| 321 EXPECT_TRUE(test_api()->HasActiveAnimations()); |
| 322 |
| 323 test_api()->CompleteAnimations(); |
| 324 EXPECT_FALSE(test_api()->HasActiveAnimations()); |
| 325 } |
| 326 |
| 327 // If the external display is removed, it should not crash. |
| 328 TEST_F(ScreenRotationAnimatorTest, RemoveSecondaryDisplayAfterCopyCallback) { |
| 329 // TODO(wutao): needs GetDisplayInfo http://crbug.com/622480. |
| 330 if (WmShell::Get()->IsRunningInMash()) { |
| 331 ASSERT_TRUE(WmShell::Get()->GetDisplayInfo(display_id()).id() != |
| 332 display_id()); |
| 333 return; |
| 334 } |
| 335 |
| 336 AddSecondaryDisplay("640x480,800x600"); |
| 337 EXPECT_EQ(2U, display_manager()->GetNumDisplays()); |
| 338 |
| 339 const unsigned int primary_display_id = |
| 340 display_manager()->GetDisplayAt(0).id(); |
| 341 SetSecreenRotatioAnimator(display_manager()->GetDisplayAt(1).id()); |
| 342 animator()->set_callback( |
| 343 base::Bind(&ScreenRotationAnimatorTest::RemoveSecondaryDisplay, |
| 344 base::Unretained(this), "640x480")); |
| 345 test_api()->EnableSmoothRotation(); |
| 346 SetDisplayRotation(display_manager()->GetDisplayAt(1).id(), |
| 347 display::Display::ROTATE_0); |
| 348 animator()->Rotate(display::Display::ROTATE_90, |
| 349 display::Display::RotationSource::ROTATION_SOURCE_USER); |
| 350 WaitForCopyCallback(); |
| 351 EXPECT_EQ(1U, display_manager()->GetNumDisplays()); |
| 352 EXPECT_EQ(primary_display_id, display_manager()->GetDisplayAt(0).id()); |
| 353 } |
| 354 |
226 } // namespace ash | 355 } // namespace ash |
OLD | NEW |