summaryrefslogtreecommitdiffstats
path: root/cc/keyframed_animation_curve_unittest.cc
blob: 2809190281ee121e95a2dbd9907bd61820b77094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/keyframed_animation_curve.h"

#include "cc/transform_operations.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
namespace {

void expectTranslateX(double translateX, const gfx::Transform& transform)
{
    EXPECT_FLOAT_EQ(translateX, transform.matrix().getDouble(0, 3));
}

// Tests that a float animation with one keyframe works as expected.
TEST(KeyframedAnimationCurveTest, OneFloatKeyframe)
{
    scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
    curve->addKeyframe(FloatKeyframe::create(0, 2, scoped_ptr<TimingFunction>()));
    EXPECT_FLOAT_EQ(2, curve->getValue(-1));
    EXPECT_FLOAT_EQ(2, curve->getValue(0));
    EXPECT_FLOAT_EQ(2, curve->getValue(0.5));
    EXPECT_FLOAT_EQ(2, curve->getValue(1));
    EXPECT_FLOAT_EQ(2, curve->getValue(2));
}

// Tests that a float animation with two keyframes works as expected.
TEST(KeyframedAnimationCurveTest, TwoFloatKeyframe)
{
    scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
    curve->addKeyframe(FloatKeyframe::create(0, 2, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(1, 4, scoped_ptr<TimingFunction>()));
    EXPECT_FLOAT_EQ(2, curve->getValue(-1));
    EXPECT_FLOAT_EQ(2, curve->getValue(0));
    EXPECT_FLOAT_EQ(3, curve->getValue(0.5));
    EXPECT_FLOAT_EQ(4, curve->getValue(1));
    EXPECT_FLOAT_EQ(4, curve->getValue(2));
}

// Tests that a float animation with three keyframes works as expected.
TEST(KeyframedAnimationCurveTest, ThreeFloatKeyframe)
{
    scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
    curve->addKeyframe(FloatKeyframe::create(0, 2, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(1, 4, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(2, 8, scoped_ptr<TimingFunction>()));
    EXPECT_FLOAT_EQ(2, curve->getValue(-1));
    EXPECT_FLOAT_EQ(2, curve->getValue(0));
    EXPECT_FLOAT_EQ(3, curve->getValue(0.5));
    EXPECT_FLOAT_EQ(4, curve->getValue(1));
    EXPECT_FLOAT_EQ(6, curve->getValue(1.5));
    EXPECT_FLOAT_EQ(8, curve->getValue(2));
    EXPECT_FLOAT_EQ(8, curve->getValue(3));
}

// Tests that a float animation with multiple keys at a given time works sanely.
TEST(KeyframedAnimationCurveTest, RepeatedFloatKeyTimes)
{
    scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
    curve->addKeyframe(FloatKeyframe::create(0, 4, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(1, 4, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(1, 6, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(2, 6, scoped_ptr<TimingFunction>()));

    EXPECT_FLOAT_EQ(4, curve->getValue(-1));
    EXPECT_FLOAT_EQ(4, curve->getValue(0));
    EXPECT_FLOAT_EQ(4, curve->getValue(0.5));

    // There is a discontinuity at 1. Any value between 4 and 6 is valid.
    float value = curve->getValue(1);
    EXPECT_TRUE(value >= 4 && value <= 6);

    EXPECT_FLOAT_EQ(6, curve->getValue(1.5));
    EXPECT_FLOAT_EQ(6, curve->getValue(2));
    EXPECT_FLOAT_EQ(6, curve->getValue(3));
}


// Tests that a transform animation with one keyframe works as expected.
TEST(KeyframedAnimationCurveTest, OneTransformKeyframe)
{
    scoped_ptr<KeyframedTransformAnimationCurve> curve(KeyframedTransformAnimationCurve::create());
    TransformOperations operations;
    operations.AppendTranslate(2, 0, 0);
    curve->addKeyframe(TransformKeyframe::create(0, operations, scoped_ptr<TimingFunction>()));

    expectTranslateX(2, curve->getValue(-1));
    expectTranslateX(2, curve->getValue(0));
    expectTranslateX(2, curve->getValue(0.5));
    expectTranslateX(2, curve->getValue(1));
    expectTranslateX(2, curve->getValue(2));
}

// Tests that a transform animation with two keyframes works as expected.
TEST(KeyframedAnimationCurveTest, TwoTransformKeyframe)
{
    scoped_ptr<KeyframedTransformAnimationCurve> curve(KeyframedTransformAnimationCurve::create());
    TransformOperations operations1;
    operations1.AppendTranslate(2, 0, 0);
    TransformOperations operations2;
    operations2.AppendTranslate(4, 0, 0);

    curve->addKeyframe(TransformKeyframe::create(0, operations1, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(TransformKeyframe::create(1, operations2, scoped_ptr<TimingFunction>()));
    expectTranslateX(2, curve->getValue(-1));
    expectTranslateX(2, curve->getValue(0));
    expectTranslateX(3, curve->getValue(0.5));
    expectTranslateX(4, curve->getValue(1));
    expectTranslateX(4, curve->getValue(2));
}

// Tests that a transform animation with three keyframes works as expected.
TEST(KeyframedAnimationCurveTest, ThreeTransformKeyframe)
{
    scoped_ptr<KeyframedTransformAnimationCurve> curve(KeyframedTransformAnimationCurve::create());
    TransformOperations operations1;
    operations1.AppendTranslate(2, 0, 0);
    TransformOperations operations2;
    operations2.AppendTranslate(4, 0, 0);
    TransformOperations operations3;
    operations3.AppendTranslate(8, 0, 0);
    curve->addKeyframe(TransformKeyframe::create(0, operations1, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(TransformKeyframe::create(1, operations2, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(TransformKeyframe::create(2, operations3, scoped_ptr<TimingFunction>()));
    expectTranslateX(2, curve->getValue(-1));
    expectTranslateX(2, curve->getValue(0));
    expectTranslateX(3, curve->getValue(0.5));
    expectTranslateX(4, curve->getValue(1));
    expectTranslateX(6, curve->getValue(1.5));
    expectTranslateX(8, curve->getValue(2));
    expectTranslateX(8, curve->getValue(3));
}

// Tests that a transform animation with multiple keys at a given time works sanely.
TEST(KeyframedAnimationCurveTest, RepeatedTransformKeyTimes)
{
    scoped_ptr<KeyframedTransformAnimationCurve> curve(KeyframedTransformAnimationCurve::create());
    // A step function.
    TransformOperations operations1;
    operations1.AppendTranslate(4, 0, 0);
    TransformOperations operations2;
    operations2.AppendTranslate(4, 0, 0);
    TransformOperations operations3;
    operations3.AppendTranslate(6, 0, 0);
    TransformOperations operations4;
    operations4.AppendTranslate(6, 0, 0);
    curve->addKeyframe(TransformKeyframe::create(0, operations1, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(TransformKeyframe::create(1, operations2, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(TransformKeyframe::create(1, operations3, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(TransformKeyframe::create(2, operations4, scoped_ptr<TimingFunction>()));

    expectTranslateX(4, curve->getValue(-1));
    expectTranslateX(4, curve->getValue(0));
    expectTranslateX(4, curve->getValue(0.5));

    // There is a discontinuity at 1. Any value between 4 and 6 is valid.
    gfx::Transform value = curve->getValue(1);
    EXPECT_TRUE(value.matrix().getDouble(0, 3) >= 4 && value.matrix().getDouble(0, 3) <= 6);

    expectTranslateX(6, curve->getValue(1.5));
    expectTranslateX(6, curve->getValue(2));
    expectTranslateX(6, curve->getValue(3));
}

// Tests that the keyframes may be added out of order.
TEST(KeyframedAnimationCurveTest, UnsortedKeyframes)
{
    scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
    curve->addKeyframe(FloatKeyframe::create(2, 8, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(0, 2, scoped_ptr<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(1, 4, scoped_ptr<TimingFunction>()));
    EXPECT_FLOAT_EQ(2, curve->getValue(-1));
    EXPECT_FLOAT_EQ(2, curve->getValue(0));
    EXPECT_FLOAT_EQ(3, curve->getValue(0.5));
    EXPECT_FLOAT_EQ(4, curve->getValue(1));
    EXPECT_FLOAT_EQ(6, curve->getValue(1.5));
    EXPECT_FLOAT_EQ(8, curve->getValue(2));
    EXPECT_FLOAT_EQ(8, curve->getValue(3));
}

// Tests that a cubic bezier timing function works as expected.
TEST(KeyframedAnimationCurveTest, CubicBezierTimingFunction)
{
    scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
    curve->addKeyframe(FloatKeyframe::create(0, 0, CubicBezierTimingFunction::create(0.25, 0, 0.75, 1).PassAs<TimingFunction>()));
    curve->addKeyframe(FloatKeyframe::create(1, 1, scoped_ptr<TimingFunction>()));

    EXPECT_FLOAT_EQ(0, curve->getValue(0));
    EXPECT_LT(0, curve->getValue(0.25));
    EXPECT_GT(0.25, curve->getValue(0.25));
    EXPECT_NEAR(curve->getValue(0.5), 0.5, 0.00015);
    EXPECT_LT(0.75, curve->getValue(0.75));
    EXPECT_GT(1, curve->getValue(0.75));
    EXPECT_FLOAT_EQ(1, curve->getValue(1));
}

}  // namespace
}  // namespace cc