CCKeyframedAnimationCurveTest.cpp [plain text]
#include "config.h"
#include "cc/CCKeyframedAnimationCurve.h"
#include "Length.h"
#include "TranslateTransformOperation.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <wtf/OwnPtr.h>
#include <wtf/Vector.h>
using namespace WebCore;
namespace {
void expectTranslateX(double translateX, const TransformationMatrix& matrix)
{
TransformationMatrix::DecomposedType decomposedType;
matrix.decompose(decomposedType);
EXPECT_FLOAT_EQ(translateX, decomposedType.translateX);
}
TEST(CCKeyframedAnimationCurveTest, OneFloatKeyframe)
{
OwnPtr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create());
curve->addKeyframe(CCFloatKeyframe::create(0, 2, nullptr));
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));
}
TEST(CCKeyframedAnimationCurveTest, TwoFloatKeyframe)
{
OwnPtr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create());
curve->addKeyframe(CCFloatKeyframe::create(0, 2, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(1, 4, nullptr));
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));
}
TEST(CCKeyframedAnimationCurveTest, ThreeFloatKeyframe)
{
OwnPtr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create());
curve->addKeyframe(CCFloatKeyframe::create(0, 2, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(1, 4, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(2, 8, nullptr));
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));
}
TEST(CCKeyframedAnimationCurveTest, RepeatedFloatKeyTimes)
{
OwnPtr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create());
curve->addKeyframe(CCFloatKeyframe::create(0, 4, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(1, 4, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(1, 6, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(2, 6, nullptr));
EXPECT_FLOAT_EQ(4, curve->getValue(-1));
EXPECT_FLOAT_EQ(4, curve->getValue(0));
EXPECT_FLOAT_EQ(4, curve->getValue(0.5));
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));
}
TEST(CCKeyframedAnimationCurveTest, OneTransformKeyframe)
{
OwnPtr<CCKeyframedTransformAnimationCurve> curve(CCKeyframedTransformAnimationCurve::create());
TransformOperations operations;
operations.operations().append(TranslateTransformOperation::create(Length(2, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
curve->addKeyframe(CCTransformKeyframe::create(0, operations, nullptr));
IntSize layerSize; expectTranslateX(2, curve->getValue(-1, layerSize));
expectTranslateX(2, curve->getValue(0, layerSize));
expectTranslateX(2, curve->getValue(0.5, layerSize));
expectTranslateX(2, curve->getValue(1, layerSize));
expectTranslateX(2, curve->getValue(2, layerSize));
}
TEST(CCKeyframedAnimationCurveTest, TwoTransformKeyframe)
{
OwnPtr<CCKeyframedTransformAnimationCurve> curve(CCKeyframedTransformAnimationCurve::create());
TransformOperations operations1;
operations1.operations().append(TranslateTransformOperation::create(Length(2, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
TransformOperations operations2;
operations2.operations().append(TranslateTransformOperation::create(Length(4, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
curve->addKeyframe(CCTransformKeyframe::create(0, operations1, nullptr));
curve->addKeyframe(CCTransformKeyframe::create(1, operations2, nullptr));
IntSize layerSize; expectTranslateX(2, curve->getValue(-1, layerSize));
expectTranslateX(2, curve->getValue(0, layerSize));
expectTranslateX(3, curve->getValue(0.5, layerSize));
expectTranslateX(4, curve->getValue(1, layerSize));
expectTranslateX(4, curve->getValue(2, layerSize));
}
TEST(CCKeyframedAnimationCurveTest, ThreeTransformKeyframe)
{
OwnPtr<CCKeyframedTransformAnimationCurve> curve(CCKeyframedTransformAnimationCurve::create());
TransformOperations operations1;
operations1.operations().append(TranslateTransformOperation::create(Length(2, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
TransformOperations operations2;
operations2.operations().append(TranslateTransformOperation::create(Length(4, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
TransformOperations operations3;
operations3.operations().append(TranslateTransformOperation::create(Length(8, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
curve->addKeyframe(CCTransformKeyframe::create(0, operations1, nullptr));
curve->addKeyframe(CCTransformKeyframe::create(1, operations2, nullptr));
curve->addKeyframe(CCTransformKeyframe::create(2, operations3, nullptr));
IntSize layerSize; expectTranslateX(2, curve->getValue(-1, layerSize));
expectTranslateX(2, curve->getValue(0, layerSize));
expectTranslateX(3, curve->getValue(0.5, layerSize));
expectTranslateX(4, curve->getValue(1, layerSize));
expectTranslateX(6, curve->getValue(1.5, layerSize));
expectTranslateX(8, curve->getValue(2, layerSize));
expectTranslateX(8, curve->getValue(3, layerSize));
}
TEST(CCKeyframedAnimationCurveTest, RepeatedTransformKeyTimes)
{
OwnPtr<CCKeyframedTransformAnimationCurve> curve(CCKeyframedTransformAnimationCurve::create());
TransformOperations operations1;
operations1.operations().append(TranslateTransformOperation::create(Length(4, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
TransformOperations operations2;
operations2.operations().append(TranslateTransformOperation::create(Length(4, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
TransformOperations operations3;
operations3.operations().append(TranslateTransformOperation::create(Length(6, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
TransformOperations operations4;
operations4.operations().append(TranslateTransformOperation::create(Length(6, Fixed), Length(0, Fixed), TransformOperation::TRANSLATE_X));
curve->addKeyframe(CCTransformKeyframe::create(0, operations1, nullptr));
curve->addKeyframe(CCTransformKeyframe::create(1, operations2, nullptr));
curve->addKeyframe(CCTransformKeyframe::create(1, operations3, nullptr));
curve->addKeyframe(CCTransformKeyframe::create(2, operations4, nullptr));
IntSize layerSize;
expectTranslateX(4, curve->getValue(-1, layerSize));
expectTranslateX(4, curve->getValue(0, layerSize));
expectTranslateX(4, curve->getValue(0.5, layerSize));
TransformationMatrix value = curve->getValue(1, layerSize);
TransformationMatrix::DecomposedType decomposedType;
value.decompose(decomposedType);
EXPECT_TRUE(decomposedType.translateX >= 4 && decomposedType.translateX <= 6);
expectTranslateX(6, curve->getValue(1.5, layerSize));
expectTranslateX(6, curve->getValue(2, layerSize));
expectTranslateX(6, curve->getValue(3, layerSize));
}
TEST(CCKeyframedAnimationCurveTest, UnsortedKeyframes)
{
OwnPtr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create());
curve->addKeyframe(CCFloatKeyframe::create(2, 8, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(0, 2, nullptr));
curve->addKeyframe(CCFloatKeyframe::create(1, 4, nullptr));
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));
}
TEST(CCKeyframedAnimationCurveTest, CubicBezierTimingFunction)
{
OwnPtr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create());
curve->addKeyframe(CCFloatKeyframe::create(0, 0, CCCubicBezierTimingFunction::create(0.25, 0, 0.75, 1)));
curve->addKeyframe(CCFloatKeyframe::create(1, 1, nullptr));
EXPECT_FLOAT_EQ(0, curve->getValue(0));
EXPECT_LT(0, curve->getValue(0.25));
EXPECT_GT(0.25, curve->getValue(0.25));
EXPECT_FLOAT_EQ(0.5, curve->getValue(0.5));
EXPECT_LT(0.75, curve->getValue(0.75));
EXPECT_GT(1, curve->getValue(0.75));
EXPECT_FLOAT_EQ(1, curve->getValue(1));
}
}