| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/vr_shell/ui_element.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "chrome/browser/android/vr_shell/animation.h" | |
| 11 #include "chrome/browser/android/vr_shell/easing.h" | |
| 12 #include "device/vr/vr_types.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 #define EXPECT_VEC3F_EQ(a, b) \ | |
| 16 EXPECT_FLOAT_EQ(a.x(), b.x()); \ | |
| 17 EXPECT_FLOAT_EQ(a.y(), b.y()); \ | |
| 18 EXPECT_FLOAT_EQ(a.z(), b.z()); | |
| 19 | |
| 20 #define EXPECT_RECTF_EQ(a, b) \ | |
| 21 EXPECT_FLOAT_EQ(a.x(), b.x()); \ | |
| 22 EXPECT_FLOAT_EQ(a.y(), b.y()); \ | |
| 23 EXPECT_FLOAT_EQ(a.width(), b.width()); \ | |
| 24 EXPECT_FLOAT_EQ(a.height(), b.height()); | |
| 25 | |
| 26 #define EXPECT_ROTATION(a, b) \ | |
| 27 EXPECT_FLOAT_EQ(a.x, b.x); \ | |
| 28 EXPECT_FLOAT_EQ(a.y, b.y); \ | |
| 29 EXPECT_FLOAT_EQ(a.z, b.z); \ | |
| 30 EXPECT_FLOAT_EQ(a.angle, b.angle); | |
| 31 | |
| 32 namespace vr_shell { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 base::TimeTicks usToTicks(uint64_t us) { | |
| 37 return base::TimeTicks::FromInternalValue(us); | |
| 38 } | |
| 39 | |
| 40 base::TimeDelta usToDelta(uint64_t us) { | |
| 41 return base::TimeDelta::FromInternalValue(us); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 TEST(UiElements, AnimateSize) { | |
| 47 UiElement rect; | |
| 48 rect.size = {10, 100, 1}; | |
| 49 std::unique_ptr<Animation> animation( | |
| 50 new Animation(0, Animation::Property::SIZE, | |
| 51 std::unique_ptr<easing::Easing>(new easing::Linear()), {}, | |
| 52 {20, 200}, usToTicks(50000), usToDelta(10000))); | |
| 53 rect.animations.emplace_back(std::move(animation)); | |
| 54 rect.Animate(usToTicks(50000)); | |
| 55 EXPECT_VEC3F_EQ(rect.size, gfx::Vector3dF(10, 100, 1)); | |
| 56 rect.Animate(usToTicks(60000)); | |
| 57 EXPECT_VEC3F_EQ(rect.size, gfx::Vector3dF(20, 200, 1)); | |
| 58 } | |
| 59 | |
| 60 TEST(UiElements, AnimateTranslation) { | |
| 61 UiElement rect; | |
| 62 rect.translation = {10, 100, 1000}; | |
| 63 std::unique_ptr<Animation> animation( | |
| 64 new Animation(0, Animation::Property::TRANSLATION, | |
| 65 std::unique_ptr<easing::Easing>(new easing::Linear()), {}, | |
| 66 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 67 rect.animations.emplace_back(std::move(animation)); | |
| 68 rect.Animate(usToTicks(50000)); | |
| 69 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(10, 100, 1000)); | |
| 70 rect.Animate(usToTicks(60000)); | |
| 71 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(20, 200, 2000)); | |
| 72 } | |
| 73 | |
| 74 TEST(UiElements, AnimateRotation) { | |
| 75 UiElement rect; | |
| 76 rect.rotation = {10, 100, 1000, 10000}; | |
| 77 std::unique_ptr<Animation> animation(new Animation( | |
| 78 0, Animation::Property::ROTATION, | |
| 79 std::unique_ptr<easing::Easing>(new easing::Linear()), {}, | |
| 80 {20, 200, 2000, 20000}, usToTicks(50000), usToDelta(10000))); | |
| 81 rect.animations.emplace_back(std::move(animation)); | |
| 82 rect.Animate(usToTicks(50000)); | |
| 83 EXPECT_ROTATION(rect.rotation, vr::RotationAxisAngle({10, 100, 1000, 10000})); | |
| 84 rect.Animate(usToTicks(60000)); | |
| 85 EXPECT_ROTATION(rect.rotation, vr::RotationAxisAngle({20, 200, 2000, 20000})); | |
| 86 } | |
| 87 | |
| 88 TEST(UiElements, AnimationHasNoEffectBeforeScheduledStart) { | |
| 89 UiElement rect; | |
| 90 std::unique_ptr<Animation> animation(new Animation( | |
| 91 0, Animation::Property::TRANSLATION, | |
| 92 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 93 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 94 rect.animations.emplace_back(std::move(animation)); | |
| 95 rect.Animate(usToTicks(49999)); | |
| 96 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(0, 0, 0)); | |
| 97 } | |
| 98 | |
| 99 TEST(UiElements, AnimationPurgedWhenDone) { | |
| 100 UiElement rect; | |
| 101 std::unique_ptr<Animation> animation(new Animation( | |
| 102 0, Animation::Property::TRANSLATION, | |
| 103 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 104 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 105 rect.animations.emplace_back(std::move(animation)); | |
| 106 rect.Animate(usToTicks(60000)); | |
| 107 EXPECT_EQ(0u, rect.animations.size()); | |
| 108 } | |
| 109 | |
| 110 TEST(UiElements, AnimationLinearEasing) { | |
| 111 UiElement rect; | |
| 112 std::unique_ptr<Animation> animation(new Animation( | |
| 113 0, Animation::Property::TRANSLATION, | |
| 114 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 115 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 116 rect.animations.emplace_back(std::move(animation)); | |
| 117 rect.Animate(usToTicks(50000)); | |
| 118 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(10, 100, 1000)); | |
| 119 rect.Animate(usToTicks(55000)); | |
| 120 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(15, 150, 1500)); | |
| 121 rect.Animate(usToTicks(60000)); | |
| 122 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(20, 200, 2000)); | |
| 123 } | |
| 124 | |
| 125 TEST(UiElements, AnimationStartFromSpecifiedLocation) { | |
| 126 UiElement rect; | |
| 127 std::unique_ptr<Animation> animation(new Animation( | |
| 128 0, Animation::Property::TRANSLATION, | |
| 129 std::unique_ptr<easing::Easing>(new easing::Linear()), {10, 100, 1000}, | |
| 130 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 131 rect.animations.emplace_back(std::move(animation)); | |
| 132 rect.Animate(usToTicks(50000)); | |
| 133 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(10, 100, 1000)); | |
| 134 rect.Animate(usToTicks(60000)); | |
| 135 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(20, 200, 2000)); | |
| 136 } | |
| 137 | |
| 138 // Ensure that when a new animation overlaps another of the same type, the | |
| 139 // newly added animation overrides the original. For example: | |
| 140 // Animation 1: ? .......... 20 | |
| 141 // Animation 2: ? .......... 50 | |
| 142 // Result: 0 ... 10 ... 30 ... 50 | |
| 143 TEST(UiElements, AnimationOverlap) { | |
| 144 UiElement rect; | |
| 145 std::unique_ptr<Animation> animation( | |
| 146 new Animation(0, Animation::Property::TRANSLATION, | |
| 147 std::unique_ptr<easing::Easing>(new easing::Linear()), {}, | |
| 148 {20, 200, 2000}, usToTicks(50000), usToDelta(10000))); | |
| 149 std::unique_ptr<Animation> animation2( | |
| 150 new Animation(0, Animation::Property::TRANSLATION, | |
| 151 std::unique_ptr<easing::Easing>(new easing::Linear()), {}, | |
| 152 {50, 500, 5000}, usToTicks(55000), usToDelta(10000))); | |
| 153 rect.animations.emplace_back(std::move(animation)); | |
| 154 rect.animations.emplace_back(std::move(animation2)); | |
| 155 rect.Animate(usToTicks(55000)); | |
| 156 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(10, 100, 1000)); | |
| 157 rect.Animate(usToTicks(60000)); | |
| 158 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(30, 300, 3000)); | |
| 159 rect.Animate(usToTicks(65000)); | |
| 160 EXPECT_VEC3F_EQ(rect.translation, gfx::Vector3dF(50, 500, 5000)); | |
| 161 } | |
| 162 | |
| 163 } // namespace vr_shell | |
| OLD | NEW |