| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "chrome/browser/android/vr_shell/ui_elements/ui_element.h" | 5 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "chrome/browser/android/vr_shell/animation.h" | 10 #include "cc/animation/animation.h" |
| 11 #include "chrome/browser/android/vr_shell/easing.h" | 11 #include "cc/animation/animation_player.h" |
| 12 #include "cc/animation/keyframed_animation_curve.h" |
| 13 #include "cc/test/geometry_test_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 15 |
| 14 #define EXPECT_VEC3F_EQ(a, b) \ | |
| 15 EXPECT_FLOAT_EQ(a.x(), b.x()); \ | |
| 16 EXPECT_FLOAT_EQ(a.y(), b.y()); \ | |
| 17 EXPECT_FLOAT_EQ(a.z(), b.z()); | |
| 18 | |
| 19 #define EXPECT_RECTF_EQ(a, b) \ | |
| 20 EXPECT_FLOAT_EQ(a.x(), b.x()); \ | |
| 21 EXPECT_FLOAT_EQ(a.y(), b.y()); \ | |
| 22 EXPECT_FLOAT_EQ(a.width(), b.width()); \ | |
| 23 EXPECT_FLOAT_EQ(a.height(), b.height()); | |
| 24 | |
| 25 #define EXPECT_ROTATION(a, b) \ | |
| 26 EXPECT_FLOAT_EQ(a.x(), b.x()); \ | |
| 27 EXPECT_FLOAT_EQ(a.y(), b.y()); \ | |
| 28 EXPECT_FLOAT_EQ(a.z(), b.z()); \ | |
| 29 EXPECT_FLOAT_EQ(a.w(), b.w()); | |
| 30 | |
| 31 namespace vr_shell { | 16 namespace vr_shell { |
| 32 | 17 |
| 33 namespace { | 18 namespace { |
| 34 | 19 |
| 35 base::TimeTicks usToTicks(uint64_t us) { | 20 base::TimeTicks usToTicks(uint64_t us) { |
| 36 return base::TimeTicks::FromInternalValue(us); | 21 return base::TimeTicks::FromInternalValue(us); |
| 37 } | 22 } |
| 38 | 23 |
| 39 base::TimeDelta usToDelta(uint64_t us) { | 24 base::TimeDelta usToDelta(uint64_t us) { |
| 40 return base::TimeDelta::FromInternalValue(us); | 25 return base::TimeDelta::FromInternalValue(us); |
| 41 } | 26 } |
| 42 | 27 |
| 28 // A convenience class for generating cc::Animations |
| 29 class AnimationCreator { |
| 30 public: |
| 31 AnimationCreator() : end_time_(usToDelta(10000)) {} |
| 32 |
| 33 std::unique_ptr<cc::Animation> transform_animation() { |
| 34 std::unique_ptr<cc::KeyframedTransformAnimationCurve> curve( |
| 35 cc::KeyframedTransformAnimationCurve::Create()); |
| 36 curve->AddKeyframe( |
| 37 cc::TransformKeyframe::Create(begin_time_, from_operations_, nullptr)); |
| 38 curve->AddKeyframe( |
| 39 cc::TransformKeyframe::Create(end_time_, to_operations_, nullptr)); |
| 40 std::unique_ptr<cc::Animation> animation(cc::Animation::Create( |
| 41 std::move(curve), 1, 1, cc::TargetProperty::TRANSFORM)); |
| 42 return animation; |
| 43 } |
| 44 |
| 45 std::unique_ptr<cc::Animation> size_animation() { |
| 46 std::unique_ptr<cc::KeyframedSizeAnimationCurve> curve( |
| 47 cc::KeyframedSizeAnimationCurve::Create()); |
| 48 curve->AddKeyframe( |
| 49 cc::SizeKeyframe::Create(begin_time_, from_size_, nullptr)); |
| 50 curve->AddKeyframe(cc::SizeKeyframe::Create(end_time_, to_size_, nullptr)); |
| 51 std::unique_ptr<cc::Animation> animation(cc::Animation::Create( |
| 52 std::move(curve), 1, 1, cc::TargetProperty::BOUNDS)); |
| 53 return animation; |
| 54 } |
| 55 |
| 56 cc::TransformOperations& from_operations() { return from_operations_; } |
| 57 cc::TransformOperations& to_operations() { return to_operations_; } |
| 58 |
| 59 void set_from_size(const gfx::SizeF& size) { from_size_ = size; } |
| 60 void set_to_size(const gfx::SizeF& size) { to_size_ = size; } |
| 61 |
| 62 void set_time_offset(const base::TimeDelta& offset) { time_offset_ = offset; } |
| 63 |
| 64 private: |
| 65 base::TimeDelta begin_time_; |
| 66 base::TimeDelta end_time_; |
| 67 base::TimeDelta time_offset_; |
| 68 |
| 69 cc::TransformOperations from_operations_; |
| 70 cc::TransformOperations to_operations_; |
| 71 gfx::SizeF from_size_; |
| 72 gfx::SizeF to_size_; |
| 73 }; |
| 74 |
| 43 } // namespace | 75 } // namespace |
| 44 | 76 |
| 45 TEST(UiElements, AnimateSize) { | 77 TEST(UiElements, AnimateSize) { |
| 46 UiElement rect; | 78 UiElement rect(1); |
| 79 AnimationCreator creator; |
| 47 rect.set_size({10, 100, 1}); | 80 rect.set_size({10, 100, 1}); |
| 48 std::unique_ptr<Animation> animation(new Animation( | 81 creator.set_from_size({10, 100}); |
| 49 0, Animation::Property::SIZE, | 82 creator.set_to_size({20, 200}); |
| 50 std::unique_ptr<easing::Easing>(new easing::Linear()), | 83 rect.animation_player().AddAnimation(creator.size_animation()); |
| 51 std::vector<float>(), {20, 200}, usToTicks(50000), usToDelta(10000))); | 84 base::TimeTicks start_time = usToTicks(1); |
| 52 rect.animations().emplace_back(std::move(animation)); | 85 rect.Animate(start_time); |
| 53 rect.Animate(usToTicks(50000)); | 86 EXPECT_VECTOR3DF_EQ(gfx::Vector3dF(10, 100, 1), rect.size()); |
| 54 EXPECT_VEC3F_EQ(gfx::Vector3dF(10, 100, 1), rect.size()); | 87 rect.Animate(start_time + usToDelta(10000)); |
| 55 rect.Animate(usToTicks(60000)); | 88 EXPECT_VECTOR3DF_EQ(gfx::Vector3dF(20, 200, 1), rect.size()); |
| 56 EXPECT_VEC3F_EQ(gfx::Vector3dF(20, 200, 1), rect.size()); | |
| 57 } | 89 } |
| 58 | 90 |
| 59 TEST(UiElements, AnimateTranslation) { | 91 TEST(UiElements, AnimationAffectsInheritableTransform) { |
| 60 UiElement rect; | 92 UiElement rect(1); |
| 61 rect.set_translation({10, 100, 1000}); | 93 AnimationCreator creator; |
| 62 std::unique_ptr<Animation> animation( | 94 creator.from_operations().AppendTranslate(10, 100, 1000); |
| 63 new Animation(0, Animation::Property::TRANSLATION, | 95 creator.to_operations().AppendTranslate(20, 200, 2000); |
| 64 std::unique_ptr<easing::Easing>(new easing::Linear()), | 96 rect.animation_player().AddAnimation(creator.transform_animation()); |
| 65 std::vector<float>(), {20, 200, 2000}, usToTicks(50000), | 97 base::TimeTicks start_time = usToTicks(1); |
| 66 usToDelta(10000))); | 98 rect.Animate(start_time); |
| 67 rect.animations().emplace_back(std::move(animation)); | 99 gfx::Point3F p; |
| 68 rect.Animate(usToTicks(50000)); | 100 rect.transform_operations().Apply().TransformPoint(&p); |
| 69 EXPECT_VEC3F_EQ(gfx::Vector3dF(10, 100, 1000), rect.translation()); | 101 EXPECT_VECTOR3DF_EQ(gfx::Vector3dF(10, 100, 1000), p); |
| 70 rect.Animate(usToTicks(60000)); | 102 p = gfx::Point3F(); |
| 71 EXPECT_VEC3F_EQ(gfx::Vector3dF(20, 200, 2000), rect.translation()); | 103 rect.Animate(start_time + usToDelta(10000)); |
| 72 } | 104 rect.transform_operations().Apply().TransformPoint(&p); |
| 73 | 105 EXPECT_VECTOR3DF_EQ(gfx::Vector3dF(20, 200, 2000), p); |
| 74 TEST(UiElements, AnimateRotation) { | |
| 75 UiElement rect; | |
| 76 gfx::Quaternion from(gfx::Vector3dF(10, 100, 1000), 10000); | |
| 77 gfx::Quaternion to(gfx::Vector3dF(20, 200, 2000), 20000); | |
| 78 rect.set_rotation(from); | |
| 79 std::unique_ptr<Animation> animation( | |
| 80 new Animation(0, Animation::Property::ROTATION, | |
| 81 std::unique_ptr<easing::Easing>(new easing::Linear()), | |
| 82 std::vector<float>(), {to.x(), to.y(), to.z(), to.w()}, | |
| 83 usToTicks(50000), usToDelta(10000))); | |
| 84 rect.animations().emplace_back(std::move(animation)); | |
| 85 rect.Animate(usToTicks(50000)); | |
| 86 EXPECT_ROTATION(from, rect.rotation()); | |
| 87 rect.Animate(usToTicks(60000)); | |
| 88 EXPECT_ROTATION(to, rect.rotation()); | |
| 89 } | |
| 90 | |
| 91 TEST(UiElements, AnimationHasNoEffectBeforeScheduledStart) { | |
| 92 UiElement rect; | |
| 93 std::unique_ptr<Animation> animation(new Animation( | |
| 94 0, Animation::Property::TRANSLATION, | |
| 95 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 96 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 97 rect.animations().emplace_back(std::move(animation)); | |
| 98 rect.Animate(usToTicks(49999)); | |
| 99 EXPECT_VEC3F_EQ(gfx::Vector3dF(0, 0, 0), rect.translation()); | |
| 100 } | |
| 101 | |
| 102 TEST(UiElements, AnimationPurgedWhenDone) { | |
| 103 UiElement rect; | |
| 104 std::unique_ptr<Animation> animation(new Animation( | |
| 105 0, Animation::Property::TRANSLATION, | |
| 106 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 107 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 108 rect.animations().emplace_back(std::move(animation)); | |
| 109 rect.Animate(usToTicks(60000)); | |
| 110 EXPECT_EQ(0u, rect.animations().size()); | |
| 111 } | |
| 112 | |
| 113 TEST(UiElements, AnimationLinearEasing) { | |
| 114 UiElement rect; | |
| 115 std::unique_ptr<Animation> animation(new Animation( | |
| 116 0, Animation::Property::TRANSLATION, | |
| 117 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 118 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 119 rect.animations().emplace_back(std::move(animation)); | |
| 120 rect.Animate(usToTicks(50000)); | |
| 121 EXPECT_VEC3F_EQ(gfx::Vector3dF(10, 100, 1000), rect.translation()); | |
| 122 rect.Animate(usToTicks(55000)); | |
| 123 EXPECT_VEC3F_EQ(gfx::Vector3dF(15, 150, 1500), rect.translation()); | |
| 124 rect.Animate(usToTicks(60000)); | |
| 125 EXPECT_VEC3F_EQ(gfx::Vector3dF(20, 200, 2000), rect.translation()); | |
| 126 } | |
| 127 | |
| 128 TEST(UiElements, AnimationStartFromSpecifiedLocation) { | |
| 129 UiElement rect; | |
| 130 std::unique_ptr<Animation> animation(new Animation( | |
| 131 0, Animation::Property::TRANSLATION, | |
| 132 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 133 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 134 rect.animations().emplace_back(std::move(animation)); | |
| 135 rect.Animate(usToTicks(50000)); | |
| 136 EXPECT_VEC3F_EQ(gfx::Vector3dF(10, 100, 1000), rect.translation()); | |
| 137 rect.Animate(usToTicks(60000)); | |
| 138 EXPECT_VEC3F_EQ(gfx::Vector3dF(20, 200, 2000), rect.translation()); | |
| 139 } | |
| 140 | |
| 141 // Ensure that when a new animation overlaps another of the same type, the | |
| 142 // newly added animation overrides the original. For example: | |
| 143 // Animation 1: ? .......... 20 | |
| 144 // Animation 2: ? .......... 50 | |
| 145 // Result: 0 ... 10 ... 30 ... 50 | |
| 146 TEST(UiElements, AnimationOverlap) { | |
| 147 UiElement rect; | |
| 148 std::unique_ptr<Animation> animation( | |
| 149 new Animation(0, Animation::Property::TRANSLATION, | |
| 150 std::unique_ptr<easing::Easing>(new easing::Linear()), | |
| 151 std::vector<float>(), {20, 200, 2000}, usToTicks(50000), | |
| 152 usToDelta(10000))); | |
| 153 std::unique_ptr<Animation> animation2( | |
| 154 new Animation(0, Animation::Property::TRANSLATION, | |
| 155 std::unique_ptr<easing::Easing>(new easing::Linear()), | |
| 156 std::vector<float>(), {50, 500, 5000}, usToTicks(55000), | |
| 157 usToDelta(10000))); | |
| 158 rect.animations().emplace_back(std::move(animation)); | |
| 159 rect.animations().emplace_back(std::move(animation2)); | |
| 160 rect.Animate(usToTicks(55000)); | |
| 161 EXPECT_VEC3F_EQ(gfx::Vector3dF(10, 100, 1000), rect.translation()); | |
| 162 rect.Animate(usToTicks(60000)); | |
| 163 EXPECT_VEC3F_EQ(gfx::Vector3dF(30, 300, 3000), rect.translation()); | |
| 164 rect.Animate(usToTicks(65000)); | |
| 165 EXPECT_VEC3F_EQ(gfx::Vector3dF(50, 500, 5000), rect.translation()); | |
| 166 } | 106 } |
| 167 | 107 |
| 168 } // namespace vr_shell | 108 } // namespace vr_shell |
| OLD | NEW |