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