1 /* 2 * Copyright (c) 2013, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "core/animation/TimedItemCalculations.h" 33 34 #include <gtest/gtest.h> 35 36 using namespace WebCore; 37 38 namespace { 39 40 TEST(TimedItemCalculations, ActiveTime) 41 { 42 Timing timing; 43 44 // calculateActiveTime(activeDuration, localTime, startTime) 45 46 // Before Phase 47 timing.startDelay = 10; 48 timing.fillMode = Timing::FillModeForwards; 49 ASSERT_TRUE(isNull(calculateActiveTime(20, 0, TimedItem::PhaseActive, TimedItem::PhaseBefore, timing))); 50 timing.fillMode = Timing::FillModeNone; 51 ASSERT_TRUE(isNull(calculateActiveTime(20, 0, TimedItem::PhaseActive, TimedItem::PhaseBefore, timing))); 52 timing.fillMode = Timing::FillModeBackwards; 53 ASSERT_EQ(0, calculateActiveTime(20, 0, TimedItem::PhaseActive, TimedItem::PhaseBefore, timing)); 54 timing.fillMode = Timing::FillModeBoth; 55 ASSERT_EQ(0, calculateActiveTime(20, 0, TimedItem::PhaseActive, TimedItem::PhaseBefore, timing)); 56 57 // Active Phase 58 timing.startDelay = 10; 59 // Active, and parent Before 60 timing.fillMode = Timing::FillModeNone; 61 ASSERT_TRUE(isNull(calculateActiveTime(20, 15, TimedItem::PhaseBefore, TimedItem::PhaseActive, timing))); 62 timing.fillMode = Timing::FillModeForwards; 63 ASSERT_TRUE(isNull(calculateActiveTime(20, 15, TimedItem::PhaseBefore, TimedItem::PhaseActive, timing))); 64 // Active, and parent After 65 timing.fillMode = Timing::FillModeNone; 66 ASSERT_TRUE(isNull(calculateActiveTime(20, 15, TimedItem::PhaseAfter, TimedItem::PhaseActive, timing))); 67 timing.fillMode = Timing::FillModeBackwards; 68 ASSERT_TRUE(isNull(calculateActiveTime(20, 15, TimedItem::PhaseAfter, TimedItem::PhaseActive, timing))); 69 // Active, and parent Active 70 timing.fillMode = Timing::FillModeForwards; 71 ASSERT_EQ(5, calculateActiveTime(20, 15, TimedItem::PhaseActive, TimedItem::PhaseActive, timing)); 72 73 // After Phase 74 timing.startDelay = 10; 75 timing.fillMode = Timing::FillModeForwards; 76 ASSERT_EQ(21, calculateActiveTime(21, 45, TimedItem::PhaseActive, TimedItem::PhaseAfter, timing)); 77 timing.fillMode = Timing::FillModeBoth; 78 ASSERT_EQ(21, calculateActiveTime(21, 45, TimedItem::PhaseActive, TimedItem::PhaseAfter, timing)); 79 timing.fillMode = Timing::FillModeBackwards; 80 ASSERT_TRUE(isNull(calculateActiveTime(21, 45, TimedItem::PhaseActive, TimedItem::PhaseAfter, timing))); 81 timing.fillMode = Timing::FillModeNone; 82 ASSERT_TRUE(isNull(calculateActiveTime(21, 45, TimedItem::PhaseActive, TimedItem::PhaseAfter, timing))); 83 84 // None 85 ASSERT_TRUE(isNull(calculateActiveTime(32, nullValue(), TimedItem::PhaseNone, TimedItem::PhaseNone, timing))); 86 } 87 88 TEST(TimedItemCalculations, ScaledActiveTime) 89 { 90 Timing timing; 91 92 // calculateScaledActiveTime(activeDuration, activeTime, startOffset) 93 94 // if the active time is null 95 ASSERT_TRUE(isNull(calculateScaledActiveTime(4, nullValue(), 5, timing))); 96 97 // if the playback rate is negative 98 timing.playbackRate = -1; 99 ASSERT_EQ(-5, calculateScaledActiveTime(10, 20, 5, timing)); 100 101 // otherwise 102 timing.playbackRate = 0; 103 ASSERT_EQ(5, calculateScaledActiveTime(10, 20, 5, timing)); 104 timing.playbackRate = 1; 105 ASSERT_EQ(25, calculateScaledActiveTime(10, 20, 5, timing)); 106 } 107 108 TEST(TimedItemCalculations, IterationTime) 109 { 110 Timing timing; 111 112 // calculateIterationTime(iterationDuration, repeatedDuration, scaledActiveTime, startOffset) 113 114 // if the scaled active time is null 115 ASSERT_TRUE(isNull(calculateIterationTime(1, 1, nullValue(), 1, timing))); 116 117 // if the iteration duration is zero 118 ASSERT_EQ(0, calculateIterationTime(0, 0, 0, 4, timing)); 119 120 // if (complex-conditions)... 121 ASSERT_EQ(12, calculateIterationTime(12, 12, 12, 0, timing)); 122 123 // otherwise 124 timing.iterationCount = 10; 125 ASSERT_EQ(5, calculateIterationTime(10, 100, 25, 4, timing)); 126 ASSERT_EQ(7, calculateIterationTime(11, 110, 29, 1, timing)); 127 timing.iterationStart = 1.1; 128 ASSERT_EQ(8, calculateIterationTime(12, 120, 20, 7, timing)); 129 } 130 131 TEST(TimedItemCalculations, CurrentIteration) 132 { 133 Timing timing; 134 135 // calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime) 136 137 // if the scaled active time is null 138 ASSERT_TRUE(isNull(calculateCurrentIteration(1, 1, nullValue(), timing))); 139 140 // if the scaled active time is zero 141 ASSERT_EQ(0, calculateCurrentIteration(1, 1, 0, timing)); 142 143 // if iterationDuration is zero 144 ASSERT_EQ(1, calculateCurrentIteration(0, 0, 9, timing)); 145 146 // if the iteration time equals the iteration duration 147 timing.iterationStart = 4; 148 timing.iterationCount = 7; 149 ASSERT_EQ(10, calculateCurrentIteration(5, 5, 9, timing)); 150 151 // otherwise 152 ASSERT_EQ(3, calculateCurrentIteration(3.2, 3.1, 10, timing)); 153 } 154 155 TEST(TimedItemCalculations, DirectedTime) 156 { 157 Timing timing; 158 159 // calculateDirectedTime(currentIteration, iterationDuration, iterationTime) 160 161 // if the iteration time is null 162 ASSERT_TRUE(isNull(calculateDirectedTime(1, 2, nullValue(), timing))); 163 164 // forwards 165 ASSERT_EQ(17, calculateDirectedTime(0, 20, 17, timing)); 166 ASSERT_EQ(17, calculateDirectedTime(1, 20, 17, timing)); 167 timing.direction = Timing::PlaybackDirectionAlternate; 168 ASSERT_EQ(17, calculateDirectedTime(0, 20, 17, timing)); 169 ASSERT_EQ(17, calculateDirectedTime(2, 20, 17, timing)); 170 timing.direction = Timing::PlaybackDirectionAlternateReverse; 171 ASSERT_EQ(17, calculateDirectedTime(1, 20, 17, timing)); 172 ASSERT_EQ(17, calculateDirectedTime(3, 20, 17, timing)); 173 174 // reverse 175 timing.direction = Timing::PlaybackDirectionReverse; 176 ASSERT_EQ(3, calculateDirectedTime(0, 20, 17, timing)); 177 ASSERT_EQ(3, calculateDirectedTime(1, 20, 17, timing)); 178 timing.direction = Timing::PlaybackDirectionAlternate; 179 ASSERT_EQ(3, calculateDirectedTime(1, 20, 17, timing)); 180 ASSERT_EQ(3, calculateDirectedTime(3, 20, 17, timing)); 181 timing.direction = Timing::PlaybackDirectionAlternateReverse; 182 ASSERT_EQ(3, calculateDirectedTime(0, 20, 17, timing)); 183 ASSERT_EQ(3, calculateDirectedTime(2, 20, 17, timing)); 184 } 185 186 TEST(TimedItemCalculations, TransformedTime) 187 { 188 Timing timing; 189 190 // calculateTransformedTime(currentIteration, iterationDuration, iterationTime) 191 192 // Iteration time is null 193 ASSERT_TRUE(isNull(calculateTransformedTime(1, 2, nullValue(), timing))); 194 195 // PlaybackDirectionForwards 196 ASSERT_EQ(12, calculateTransformedTime(0, 20, 12, timing)); 197 ASSERT_EQ(12, calculateTransformedTime(1, 20, 12, timing)); 198 199 // PlaybackDirectionForwards with timing function 200 timing.timingFunction = StepsTimingFunction::create(4, false /* stepAtStart */); 201 ASSERT_EQ(10, calculateTransformedTime(0, 20, 12, timing)); 202 ASSERT_EQ(10, calculateTransformedTime(1, 20, 12, timing)); 203 204 // PlaybackDirectionReverse 205 timing.timingFunction = 0; 206 timing.direction = Timing::PlaybackDirectionReverse; 207 ASSERT_EQ(8, calculateTransformedTime(0, 20, 12, timing)); 208 ASSERT_EQ(8, calculateTransformedTime(1, 20, 12, timing)); 209 210 // PlaybackDirectionReverse with timing function 211 timing.timingFunction = StepsTimingFunction::create(4, false /* stepAtStart */); 212 ASSERT_EQ(5, calculateTransformedTime(0, 20, 12, timing)); 213 ASSERT_EQ(5, calculateTransformedTime(1, 20, 12, timing)); 214 215 // Timing function when directed time is null. 216 ASSERT_TRUE(isNull(calculateTransformedTime(1, 2, nullValue(), timing))); 217 } 218 219 } 220