[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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 "config.h" |
| 6 | |
| 7 | #include "CCActiveAnimation.h" |
| 8 | |
| 9 | #include "CCAnimationTestCommon.h" |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | #include <wtf/Vector.h> |
| 13 | |
| 14 | using namespace WebKitTests; |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 15 | using namespace cc; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
[email protected] | 4dad47e | 2012-09-21 22:08:47 | [diff] [blame^] | 19 | PassOwnPtr<CCActiveAnimation> createActiveAnimation(int iterations, double duration) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 20 | { |
[email protected] | 4dad47e | 2012-09-21 22:08:47 | [diff] [blame^] | 21 | OwnPtr<CCActiveAnimation> toReturn(CCActiveAnimation::create(adoptPtr(new FakeFloatAnimationCurve(duration)), 0, 1, CCActiveAnimation::Opacity)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 22 | toReturn->setIterations(iterations); |
| 23 | return toReturn.release(); |
| 24 | } |
| 25 | |
[email protected] | 4dad47e | 2012-09-21 22:08:47 | [diff] [blame^] | 26 | PassOwnPtr<CCActiveAnimation> createActiveAnimation(int iterations) |
| 27 | { |
| 28 | return createActiveAnimation(iterations, 1); |
| 29 | } |
| 30 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 31 | TEST(CCActiveAnimationTest, TrimTimeZeroIterations) |
| 32 | { |
| 33 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(0)); |
| 34 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(-1)); |
| 35 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 36 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(1)); |
| 37 | } |
| 38 | |
| 39 | TEST(CCActiveAnimationTest, TrimTimeOneIteration) |
| 40 | { |
| 41 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 42 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(-1)); |
| 43 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 44 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(1)); |
| 45 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(2)); |
| 46 | } |
| 47 | |
| 48 | TEST(CCActiveAnimationTest, TrimTimeInfiniteIterations) |
| 49 | { |
| 50 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(-1)); |
| 51 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 52 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(0.5)); |
| 53 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(1)); |
| 54 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(1.5)); |
| 55 | } |
| 56 | |
| 57 | TEST(CCActiveAnimationTest, TrimTimeAlternating) |
| 58 | { |
| 59 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(-1)); |
| 60 | anim->setAlternatesDirection(true); |
| 61 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 62 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(0.5)); |
| 63 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(1)); |
| 64 | EXPECT_EQ(0.75, anim->trimTimeToCurrentIteration(1.25)); |
| 65 | } |
| 66 | |
| 67 | TEST(CCActiveAnimationTest, TrimTimeStartTime) |
| 68 | { |
| 69 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 70 | anim->setStartTime(4); |
| 71 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 72 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(4)); |
| 73 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(4.5)); |
| 74 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(5)); |
| 75 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(6)); |
| 76 | } |
| 77 | |
| 78 | TEST(CCActiveAnimationTest, TrimTimeTimeOffset) |
| 79 | { |
| 80 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 81 | anim->setTimeOffset(4); |
| 82 | anim->setStartTime(4); |
| 83 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 84 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(0.5)); |
| 85 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(1)); |
| 86 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(1)); |
| 87 | } |
| 88 | |
| 89 | TEST(CCActiveAnimationTest, TrimTimePauseResume) |
| 90 | { |
| 91 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 92 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 93 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 94 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(0.5)); |
| 95 | anim->setRunState(CCActiveAnimation::Paused, 0.5); |
| 96 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(1024)); |
| 97 | anim->setRunState(CCActiveAnimation::Running, 1024); |
| 98 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(1024)); |
| 99 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(1024.5)); |
| 100 | } |
| 101 | |
| 102 | TEST(CCActiveAnimationTest, TrimTimeSuspendResume) |
| 103 | { |
| 104 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 105 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 106 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 107 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(0.5)); |
| 108 | anim->suspend(0.5); |
| 109 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(1024)); |
| 110 | anim->resume(1024); |
| 111 | EXPECT_EQ(0.5, anim->trimTimeToCurrentIteration(1024)); |
| 112 | EXPECT_EQ(1, anim->trimTimeToCurrentIteration(1024.5)); |
| 113 | } |
| 114 | |
[email protected] | 4dad47e | 2012-09-21 22:08:47 | [diff] [blame^] | 115 | TEST(CCActiveAnimationTest, TrimTimeZeroDuration) |
| 116 | { |
| 117 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(0, 0)); |
| 118 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 119 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(-1)); |
| 120 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(0)); |
| 121 | EXPECT_EQ(0, anim->trimTimeToCurrentIteration(1)); |
| 122 | } |
| 123 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 124 | TEST(CCActiveAnimationTest, IsFinishedAtZeroIterations) |
| 125 | { |
| 126 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(0)); |
| 127 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 128 | EXPECT_FALSE(anim->isFinishedAt(-1)); |
| 129 | EXPECT_TRUE(anim->isFinishedAt(0)); |
| 130 | EXPECT_TRUE(anim->isFinishedAt(1)); |
| 131 | } |
| 132 | |
| 133 | TEST(CCActiveAnimationTest, IsFinishedAtOneIteration) |
| 134 | { |
| 135 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 136 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 137 | EXPECT_FALSE(anim->isFinishedAt(-1)); |
| 138 | EXPECT_FALSE(anim->isFinishedAt(0)); |
| 139 | EXPECT_TRUE(anim->isFinishedAt(1)); |
| 140 | EXPECT_TRUE(anim->isFinishedAt(2)); |
| 141 | } |
| 142 | |
| 143 | TEST(CCActiveAnimationTest, IsFinishedAtInfiniteIterations) |
| 144 | { |
| 145 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(-1)); |
| 146 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 147 | EXPECT_FALSE(anim->isFinishedAt(0)); |
| 148 | EXPECT_FALSE(anim->isFinishedAt(0.5)); |
| 149 | EXPECT_FALSE(anim->isFinishedAt(1)); |
| 150 | EXPECT_FALSE(anim->isFinishedAt(1.5)); |
| 151 | } |
| 152 | |
| 153 | TEST(CCActiveAnimationTest, IsFinishedAtNotRunning) |
| 154 | { |
| 155 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(0)); |
| 156 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 157 | EXPECT_TRUE(anim->isFinishedAt(0)); |
| 158 | anim->setRunState(CCActiveAnimation::Paused, 0); |
| 159 | EXPECT_FALSE(anim->isFinishedAt(0)); |
| 160 | anim->setRunState(CCActiveAnimation::WaitingForNextTick, 0); |
| 161 | EXPECT_FALSE(anim->isFinishedAt(0)); |
| 162 | anim->setRunState(CCActiveAnimation::WaitingForTargetAvailability, 0); |
| 163 | EXPECT_FALSE(anim->isFinishedAt(0)); |
| 164 | anim->setRunState(CCActiveAnimation::WaitingForStartTime, 0); |
| 165 | EXPECT_FALSE(anim->isFinishedAt(0)); |
| 166 | anim->setRunState(CCActiveAnimation::Finished, 0); |
| 167 | EXPECT_TRUE(anim->isFinishedAt(0)); |
| 168 | anim->setRunState(CCActiveAnimation::Aborted, 0); |
| 169 | EXPECT_TRUE(anim->isFinishedAt(0)); |
| 170 | } |
| 171 | |
| 172 | TEST(CCActiveAnimationTest, IsFinished) |
| 173 | { |
| 174 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 175 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 176 | EXPECT_FALSE(anim->isFinished()); |
| 177 | anim->setRunState(CCActiveAnimation::Paused, 0); |
| 178 | EXPECT_FALSE(anim->isFinished()); |
| 179 | anim->setRunState(CCActiveAnimation::WaitingForNextTick, 0); |
| 180 | EXPECT_FALSE(anim->isFinished()); |
| 181 | anim->setRunState(CCActiveAnimation::WaitingForTargetAvailability, 0); |
| 182 | EXPECT_FALSE(anim->isFinished()); |
| 183 | anim->setRunState(CCActiveAnimation::WaitingForStartTime, 0); |
| 184 | EXPECT_FALSE(anim->isFinished()); |
| 185 | anim->setRunState(CCActiveAnimation::Finished, 0); |
| 186 | EXPECT_TRUE(anim->isFinished()); |
| 187 | anim->setRunState(CCActiveAnimation::Aborted, 0); |
| 188 | EXPECT_TRUE(anim->isFinished()); |
| 189 | } |
| 190 | |
| 191 | TEST(CCActiveAnimationTest, IsFinishedNeedsSynchronizedStartTime) |
| 192 | { |
| 193 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 194 | anim->setRunState(CCActiveAnimation::Running, 2); |
| 195 | EXPECT_FALSE(anim->isFinished()); |
| 196 | anim->setRunState(CCActiveAnimation::Paused, 2); |
| 197 | EXPECT_FALSE(anim->isFinished()); |
| 198 | anim->setRunState(CCActiveAnimation::WaitingForNextTick, 2); |
| 199 | EXPECT_FALSE(anim->isFinished()); |
| 200 | anim->setRunState(CCActiveAnimation::WaitingForTargetAvailability, 2); |
| 201 | EXPECT_FALSE(anim->isFinished()); |
| 202 | anim->setRunState(CCActiveAnimation::WaitingForStartTime, 2); |
| 203 | EXPECT_FALSE(anim->isFinished()); |
| 204 | anim->setRunState(CCActiveAnimation::Finished, 0); |
| 205 | EXPECT_TRUE(anim->isFinished()); |
| 206 | anim->setRunState(CCActiveAnimation::Aborted, 0); |
| 207 | EXPECT_TRUE(anim->isFinished()); |
| 208 | } |
| 209 | |
| 210 | TEST(CCActiveAnimationTest, RunStateChangesIgnoredWhileSuspended) |
| 211 | { |
| 212 | OwnPtr<CCActiveAnimation> anim(createActiveAnimation(1)); |
| 213 | anim->suspend(0); |
| 214 | EXPECT_EQ(CCActiveAnimation::Paused, anim->runState()); |
| 215 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 216 | EXPECT_EQ(CCActiveAnimation::Paused, anim->runState()); |
| 217 | anim->resume(0); |
| 218 | anim->setRunState(CCActiveAnimation::Running, 0); |
| 219 | EXPECT_EQ(CCActiveAnimation::Running, anim->runState()); |
| 220 | } |
| 221 | |
| 222 | } // namespace |