15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
2bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles) * Copyright (c) 2013, Google Inc. All rights reserved.
302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
1702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
32bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)#include "core/animation/AnimationClock.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)#include "wtf/OwnPtr.h"
35bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)#include <gtest/gtest.h>
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
37c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)using namespace blink;
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
39bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)namespace {
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)class AnimationAnimationClockTest : public ::testing::Test {
42d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)public:
43d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationAnimationClockTest()
44d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        : animationClock(mockTimeFunction)
45d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    { }
46bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)protected:
47bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    virtual void SetUp()
48bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    {
49d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        mockTime = 0;
50d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        animationClock.resetTimeForTesting();
51bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    }
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
53bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    static double mockTimeFunction()
54bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    {
55d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return mockTime;
56bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    }
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
58bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    static double mockTime;
59d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock animationClock;
60bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)};
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
6251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)double AnimationAnimationClockTest::mockTime;
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
64d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, TimeIsGreaterThanZeroForUnitTests)
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
66d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock clock;
67d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // unit tests outside core/animation shouldn't need to do anything to get
68d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // a non-zero currentTime().
69d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_GT(clock.currentTime(), 0);
70d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)}
71d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
72d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, TimeDoesNotChange)
73d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles){
74d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
75d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
76d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
77d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)}
78d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
79d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, TimeAdvancesWhenUpdated)
80d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles){
81d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
82d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
83d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
84d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(200);
85d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(200, animationClock.currentTime());
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
88d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, TimeAdvancesToTaskTime)
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
90d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
91d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
92d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
93d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    mockTime = 150;
94d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock::notifyTaskStart();
95d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_GE(animationClock.currentTime(), mockTime);
96d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)}
97d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
98d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, TimeAdvancesToTaskTimeOnlyWhenRequired)
99d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles){
100d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
101d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
102d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
103d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock::notifyTaskStart();
104d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(125);
105d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(125, animationClock.currentTime());
106d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)}
107d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
108d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, UpdateTimeIsMonotonic)
109d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles){
110d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
111d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
112d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
113d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // Update can't go backwards.
114d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(50);
115d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
116d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
117d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    mockTime = 50;
118d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock::notifyTaskStart();
119d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
120d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
121d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    mockTime = 150;
122d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock::notifyTaskStart();
123d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_GE(animationClock.currentTime(), mockTime);
124d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
125d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // Update can't go backwards after advance to estimate.
126d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
127d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_GE(animationClock.currentTime(), mockTime);
128d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)}
129d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
130d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)TEST_F(AnimationAnimationClockTest, CurrentTimeUpdatesTask)
131d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles){
132d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    animationClock.updateTime(100);
133d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
134d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
135d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    mockTime = 100;
136d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationClock::notifyTaskStart();
137d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
138d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
139d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    mockTime = 150;
140d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    EXPECT_EQ(100, animationClock.currentTime());
1415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
143bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)}
144