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/AnimationClock.h"
33
34#include "wtf/OwnPtr.h"
35#include <gtest/gtest.h>
36
37using namespace blink;
38
39namespace {
40
41class AnimationAnimationClockTest : public ::testing::Test {
42public:
43    AnimationAnimationClockTest()
44        : animationClock(mockTimeFunction)
45    { }
46protected:
47    virtual void SetUp()
48    {
49        mockTime = 0;
50        animationClock.resetTimeForTesting();
51    }
52
53    static double mockTimeFunction()
54    {
55        return mockTime;
56    }
57
58    static double mockTime;
59    AnimationClock animationClock;
60};
61
62double AnimationAnimationClockTest::mockTime;
63
64TEST_F(AnimationAnimationClockTest, TimeIsGreaterThanZeroForUnitTests)
65{
66    AnimationClock clock;
67    // unit tests outside core/animation shouldn't need to do anything to get
68    // a non-zero currentTime().
69    EXPECT_GT(clock.currentTime(), 0);
70}
71
72TEST_F(AnimationAnimationClockTest, TimeDoesNotChange)
73{
74    animationClock.updateTime(100);
75    EXPECT_EQ(100, animationClock.currentTime());
76    EXPECT_EQ(100, animationClock.currentTime());
77}
78
79TEST_F(AnimationAnimationClockTest, TimeAdvancesWhenUpdated)
80{
81    animationClock.updateTime(100);
82    EXPECT_EQ(100, animationClock.currentTime());
83
84    animationClock.updateTime(200);
85    EXPECT_EQ(200, animationClock.currentTime());
86}
87
88TEST_F(AnimationAnimationClockTest, TimeAdvancesToTaskTime)
89{
90    animationClock.updateTime(100);
91    EXPECT_EQ(100, animationClock.currentTime());
92
93    mockTime = 150;
94    AnimationClock::notifyTaskStart();
95    EXPECT_GE(animationClock.currentTime(), mockTime);
96}
97
98TEST_F(AnimationAnimationClockTest, TimeAdvancesToTaskTimeOnlyWhenRequired)
99{
100    animationClock.updateTime(100);
101    EXPECT_EQ(100, animationClock.currentTime());
102
103    AnimationClock::notifyTaskStart();
104    animationClock.updateTime(125);
105    EXPECT_EQ(125, animationClock.currentTime());
106}
107
108TEST_F(AnimationAnimationClockTest, UpdateTimeIsMonotonic)
109{
110    animationClock.updateTime(100);
111    EXPECT_EQ(100, animationClock.currentTime());
112
113    // Update can't go backwards.
114    animationClock.updateTime(50);
115    EXPECT_EQ(100, animationClock.currentTime());
116
117    mockTime = 50;
118    AnimationClock::notifyTaskStart();
119    EXPECT_EQ(100, animationClock.currentTime());
120
121    mockTime = 150;
122    AnimationClock::notifyTaskStart();
123    EXPECT_GE(animationClock.currentTime(), mockTime);
124
125    // Update can't go backwards after advance to estimate.
126    animationClock.updateTime(100);
127    EXPECT_GE(animationClock.currentTime(), mockTime);
128}
129
130TEST_F(AnimationAnimationClockTest, CurrentTimeUpdatesTask)
131{
132    animationClock.updateTime(100);
133    EXPECT_EQ(100, animationClock.currentTime());
134
135    mockTime = 100;
136    AnimationClock::notifyTaskStart();
137    EXPECT_EQ(100, animationClock.currentTime());
138
139    mockTime = 150;
140    EXPECT_EQ(100, animationClock.currentTime());
141}
142
143}
144