screen_rotation_animation_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "ash/rotator/screen_rotation_animation.h"
  5. #include <memory>
  6. #include "ash/test/ash_test_base.h"
  7. #include "base/time/time.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/compositor/layer.h"
  11. #include "ui/compositor/layer_animation_sequence.h"
  12. #include "ui/compositor/layer_animator.h"
  13. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  14. #include "ui/gfx/animation/tween.h"
  15. #include "ui/gfx/geometry/transform.h"
  16. namespace ash {
  17. class ScreenRotationAnimationTest : public AshTestBase {
  18. public:
  19. ScreenRotationAnimationTest() = default;
  20. ScreenRotationAnimationTest(const ScreenRotationAnimationTest&) = delete;
  21. ScreenRotationAnimationTest& operator=(const ScreenRotationAnimationTest&) =
  22. delete;
  23. ~ScreenRotationAnimationTest() override = default;
  24. // AshTestBase:
  25. void SetUp() override;
  26. private:
  27. std::unique_ptr<ui::ScopedAnimationDurationScaleMode> non_zero_duration_mode_;
  28. };
  29. void ScreenRotationAnimationTest::SetUp() {
  30. AshTestBase::SetUp();
  31. non_zero_duration_mode_ =
  32. std::make_unique<ui::ScopedAnimationDurationScaleMode>(
  33. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  34. }
  35. TEST_F(ScreenRotationAnimationTest, LayerTransformGetsSetToTargetWhenAborted) {
  36. std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(9));
  37. ui::Layer* layer = window->layer();
  38. std::unique_ptr<ScreenRotationAnimation> screen_rotation =
  39. std::make_unique<ScreenRotationAnimation>(
  40. layer, 45 /* start_degrees */, 0 /* end_degrees */,
  41. 0.5f /* initial_opacity */, 1.0f /* target_opacity */,
  42. gfx::Point(10, 10) /* pivot */, base::Seconds(10) /* duration */,
  43. gfx::Tween::LINEAR);
  44. ui::LayerAnimator* animator = layer->GetAnimator();
  45. animator->set_preemption_strategy(
  46. ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
  47. std::unique_ptr<ui::LayerAnimationSequence> animation_sequence =
  48. std::make_unique<ui::LayerAnimationSequence>(std::move(screen_rotation));
  49. animator->StartAnimation(animation_sequence.release());
  50. const gfx::Transform identity_transform;
  51. ASSERT_EQ(identity_transform, layer->GetTargetTransform());
  52. ASSERT_NE(identity_transform, layer->transform());
  53. layer->GetAnimator()->AbortAllAnimations();
  54. EXPECT_EQ(identity_transform, layer->transform());
  55. }
  56. // Tests that ScreenRotationAnimation::OnAbort() doesn't segfault when passed a
  57. // null delegate in response to its ui::Layer being destroyed:
  58. // http://crbug.com/661313
  59. TEST_F(ScreenRotationAnimationTest, DestroyLayerDuringAnimation) {
  60. // Create a ui::Layer directly rather than an aura::Window, as the latter
  61. // finishes all of its animation before destroying its layer.
  62. std::unique_ptr<ui::Layer> layer = std::make_unique<ui::Layer>();
  63. ui::Layer* root_layer = GetContext()->layer();
  64. layer->SetBounds(gfx::Rect(root_layer->bounds().size()));
  65. root_layer->Add(layer.get());
  66. std::unique_ptr<ScreenRotationAnimation> screen_rotation =
  67. std::make_unique<ScreenRotationAnimation>(layer.get(), 45, 0, 1.0f, 1.0f,
  68. gfx::Point(), base::Seconds(1),
  69. gfx::Tween::LINEAR);
  70. ui::LayerAnimator* animator = layer->GetAnimator();
  71. std::unique_ptr<ui::LayerAnimationSequence> animation_sequence =
  72. std::make_unique<ui::LayerAnimationSequence>(std::move(screen_rotation));
  73. animator->StartAnimation(animation_sequence.release());
  74. // Explicitly destroy the layer to verify that the animation doesn't crash.
  75. layer.reset();
  76. }
  77. } // namespace ash