1 // Copyright 2012 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 "cc/animation/animation.h" 6 7 #include "cc/test/animation_test_common.h" 8 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace cc { 12 namespace { 13 14 scoped_ptr<Animation> CreateAnimation(int iterations, double duration) { 15 scoped_ptr<Animation> to_return(Animation::Create( 16 make_scoped_ptr( 17 new FakeFloatAnimationCurve(duration)).PassAs<AnimationCurve>(), 18 0, 19 1, 20 Animation::Opacity)); 21 to_return->set_iterations(iterations); 22 return to_return.Pass(); 23 } 24 25 scoped_ptr<Animation> CreateAnimation(int iterations) { 26 return CreateAnimation(iterations, 1); 27 } 28 29 TEST(AnimationTest, TrimTimeZeroIterations) { 30 scoped_ptr<Animation> anim(CreateAnimation(0)); 31 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(-1.0)); 32 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 33 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(1.0)); 34 } 35 36 TEST(AnimationTest, TrimTimeOneIteration) { 37 scoped_ptr<Animation> anim(CreateAnimation(1)); 38 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(-1.0)); 39 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 40 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(1.0)); 41 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(2.0)); 42 } 43 44 TEST(AnimationTest, TrimTimeInfiniteIterations) { 45 scoped_ptr<Animation> anim(CreateAnimation(-1)); 46 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 47 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(0.5)); 48 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(1.0)); 49 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(1.5)); 50 } 51 52 TEST(AnimationTest, TrimTimeAlternating) { 53 scoped_ptr<Animation> anim(CreateAnimation(-1)); 54 anim->set_alternates_direction(true); 55 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 56 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(0.5)); 57 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(1.0)); 58 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(1.25)); 59 } 60 61 TEST(AnimationTest, TrimTimeStartTime) { 62 scoped_ptr<Animation> anim(CreateAnimation(1)); 63 anim->set_start_time(4); 64 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 65 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(4.0)); 66 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(4.5)); 67 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(5.0)); 68 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(6.0)); 69 } 70 71 TEST(AnimationTest, TrimTimeTimeOffset) { 72 scoped_ptr<Animation> anim(CreateAnimation(1)); 73 anim->set_time_offset(4); 74 anim->set_start_time(4); 75 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 76 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(0.5)); 77 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(1.0)); 78 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(1.0)); 79 } 80 81 TEST(AnimationTest, TrimTimeNegativeTimeOffset) { 82 scoped_ptr<Animation> anim(CreateAnimation(1)); 83 anim->set_time_offset(-4); 84 85 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 86 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(4.0)); 87 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(4.5)); 88 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(5.0)); 89 } 90 91 TEST(AnimationTest, TrimTimePauseResume) { 92 scoped_ptr<Animation> anim(CreateAnimation(1)); 93 anim->SetRunState(Animation::Running, 0.0); 94 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 95 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(0.5)); 96 anim->SetRunState(Animation::Paused, 0.5); 97 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(1024.0)); 98 anim->SetRunState(Animation::Running, 1024.0); 99 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(1024.0)); 100 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(1024.5)); 101 } 102 103 TEST(AnimationTest, TrimTimeSuspendResume) { 104 scoped_ptr<Animation> anim(CreateAnimation(1)); 105 anim->SetRunState(Animation::Running, 0.0); 106 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 107 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(0.5)); 108 anim->Suspend(0.5); 109 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(1024.0)); 110 anim->Resume(1024); 111 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(1024.0)); 112 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(1024.5)); 113 } 114 115 TEST(AnimationTest, TrimTimeZeroDuration) { 116 scoped_ptr<Animation> anim(CreateAnimation(0, 0)); 117 anim->SetRunState(Animation::Running, 0.0); 118 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(-1.0)); 119 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(0.0)); 120 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(1.0)); 121 } 122 123 TEST(AnimationTest, TrimTimeStarting) { 124 scoped_ptr<Animation> anim(CreateAnimation(1, 5.0)); 125 anim->SetRunState(Animation::Starting, 0.0); 126 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(-1.0)); 127 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(0.0)); 128 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(1.0)); 129 anim->set_time_offset(2.0); 130 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(-1.0)); 131 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(0.0)); 132 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(1.0)); 133 anim->set_start_time(1.0); 134 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(-1.0)); 135 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(0.0)); 136 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(1.0)); 137 EXPECT_EQ(3.0, anim->TrimTimeToCurrentIteration(2.0)); 138 } 139 140 TEST(AnimationTest, TrimTimeNeedsSynchronizedStartTime) { 141 scoped_ptr<Animation> anim(CreateAnimation(1, 5.0)); 142 anim->SetRunState(Animation::Running, 0.0); 143 anim->set_needs_synchronized_start_time(true); 144 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(-1.0)); 145 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(0.0)); 146 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(1.0)); 147 anim->set_time_offset(2.0); 148 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(-1.0)); 149 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(0.0)); 150 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(1.0)); 151 anim->set_start_time(1.0); 152 anim->set_needs_synchronized_start_time(false); 153 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(0.0)); 154 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(1.0)); 155 EXPECT_EQ(3.0, anim->TrimTimeToCurrentIteration(2.0)); 156 } 157 158 TEST(AnimationTest, IsFinishedAtZeroIterations) { 159 scoped_ptr<Animation> anim(CreateAnimation(0)); 160 anim->SetRunState(Animation::Running, 0.0); 161 EXPECT_FALSE(anim->IsFinishedAt(-1.0)); 162 EXPECT_TRUE(anim->IsFinishedAt(0.0)); 163 EXPECT_TRUE(anim->IsFinishedAt(1.0)); 164 } 165 166 TEST(AnimationTest, IsFinishedAtOneIteration) { 167 scoped_ptr<Animation> anim(CreateAnimation(1)); 168 anim->SetRunState(Animation::Running, 0.0); 169 EXPECT_FALSE(anim->IsFinishedAt(-1.0)); 170 EXPECT_FALSE(anim->IsFinishedAt(0.0)); 171 EXPECT_TRUE(anim->IsFinishedAt(1.0)); 172 EXPECT_TRUE(anim->IsFinishedAt(2.0)); 173 } 174 175 TEST(AnimationTest, IsFinishedAtInfiniteIterations) { 176 scoped_ptr<Animation> anim(CreateAnimation(-1)); 177 anim->SetRunState(Animation::Running, 0.0); 178 EXPECT_FALSE(anim->IsFinishedAt(0.0)); 179 EXPECT_FALSE(anim->IsFinishedAt(0.5)); 180 EXPECT_FALSE(anim->IsFinishedAt(1.0)); 181 EXPECT_FALSE(anim->IsFinishedAt(1.5)); 182 } 183 184 TEST(AnimationTest, IsFinishedNegativeTimeOffset) { 185 scoped_ptr<Animation> anim(CreateAnimation(1)); 186 anim->set_time_offset(-0.5); 187 anim->SetRunState(Animation::Running, 0.0); 188 189 EXPECT_FALSE(anim->IsFinishedAt(-1.0)); 190 EXPECT_FALSE(anim->IsFinishedAt(0.0)); 191 EXPECT_FALSE(anim->IsFinishedAt(0.5)); 192 EXPECT_FALSE(anim->IsFinishedAt(1.0)); 193 EXPECT_TRUE(anim->IsFinishedAt(1.5)); 194 EXPECT_TRUE(anim->IsFinishedAt(2.0)); 195 EXPECT_TRUE(anim->IsFinishedAt(2.5)); 196 } 197 198 TEST(AnimationTest, IsFinishedPositiveTimeOffset) { 199 scoped_ptr<Animation> anim(CreateAnimation(1)); 200 anim->set_time_offset(0.5); 201 anim->SetRunState(Animation::Running, 0.0); 202 203 EXPECT_FALSE(anim->IsFinishedAt(-1.0)); 204 EXPECT_FALSE(anim->IsFinishedAt(0.0)); 205 EXPECT_TRUE(anim->IsFinishedAt(0.5)); 206 EXPECT_TRUE(anim->IsFinishedAt(1.0)); 207 } 208 209 TEST(AnimationTest, IsFinishedAtNotRunning) { 210 scoped_ptr<Animation> anim(CreateAnimation(0)); 211 anim->SetRunState(Animation::Running, 0.0); 212 EXPECT_TRUE(anim->IsFinishedAt(0.0)); 213 anim->SetRunState(Animation::Paused, 0.0); 214 EXPECT_FALSE(anim->IsFinishedAt(0.0)); 215 anim->SetRunState(Animation::WaitingForTargetAvailability, 0.0); 216 EXPECT_FALSE(anim->IsFinishedAt(0.0)); 217 anim->SetRunState(Animation::Finished, 0.0); 218 EXPECT_TRUE(anim->IsFinishedAt(0.0)); 219 anim->SetRunState(Animation::Aborted, 0.0); 220 EXPECT_TRUE(anim->IsFinishedAt(0.0)); 221 } 222 223 TEST(AnimationTest, IsFinished) { 224 scoped_ptr<Animation> anim(CreateAnimation(1)); 225 anim->SetRunState(Animation::Running, 0.0); 226 EXPECT_FALSE(anim->is_finished()); 227 anim->SetRunState(Animation::Paused, 0.0); 228 EXPECT_FALSE(anim->is_finished()); 229 anim->SetRunState(Animation::WaitingForTargetAvailability, 0.0); 230 EXPECT_FALSE(anim->is_finished()); 231 anim->SetRunState(Animation::Finished, 0.0); 232 EXPECT_TRUE(anim->is_finished()); 233 anim->SetRunState(Animation::Aborted, 0.0); 234 EXPECT_TRUE(anim->is_finished()); 235 } 236 237 TEST(AnimationTest, IsFinishedNeedsSynchronizedStartTime) { 238 scoped_ptr<Animation> anim(CreateAnimation(1)); 239 anim->SetRunState(Animation::Running, 2.0); 240 EXPECT_FALSE(anim->is_finished()); 241 anim->SetRunState(Animation::Paused, 2.0); 242 EXPECT_FALSE(anim->is_finished()); 243 anim->SetRunState(Animation::WaitingForTargetAvailability, 2.0); 244 EXPECT_FALSE(anim->is_finished()); 245 anim->SetRunState(Animation::Finished, 0.0); 246 EXPECT_TRUE(anim->is_finished()); 247 anim->SetRunState(Animation::Aborted, 0.0); 248 EXPECT_TRUE(anim->is_finished()); 249 } 250 251 TEST(AnimationTest, RunStateChangesIgnoredWhileSuspended) { 252 scoped_ptr<Animation> anim(CreateAnimation(1)); 253 anim->Suspend(0); 254 EXPECT_EQ(Animation::Paused, anim->run_state()); 255 anim->SetRunState(Animation::Running, 0.0); 256 EXPECT_EQ(Animation::Paused, anim->run_state()); 257 anim->Resume(0); 258 anim->SetRunState(Animation::Running, 0.0); 259 EXPECT_EQ(Animation::Running, anim->run_state()); 260 } 261 262 } // namespace 263 } // namespace cc 264