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