1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QTest>
30#include <private/fcurve_p.h>
31
32using namespace Qt3DAnimation;
33using namespace Qt3DAnimation::Animation;
34
35class tst_FCurve : public QObject
36{
37 Q_OBJECT
38
39private Q_SLOTS:
40 void checkDefaultConstruction()
41 {
42 // WHEN
43 FCurve fcurve;
44
45 // THEN
46 QCOMPARE(fcurve.keyframeCount(), 0);
47 QCOMPARE(fcurve.startTime(), 0.0f);
48 QCOMPARE(fcurve.endTime(), 0.0f);
49 }
50
51 void checkAddingKeyframes()
52 {
53 // GIVEN
54 FCurve fcurve;
55
56 // WHEN
57 const Keyframe kf0{.value: 0.0f, .leftControlPoint: {-5.0f, 0.0f}, .rightControlPoint: {5.0f, 0.0f}, .interpolation: QKeyFrame::BezierInterpolation};
58 fcurve.appendKeyframe(localTime: 0.0f, keyframe: kf0);
59
60 // THEN
61 QCOMPARE(fcurve.keyframeCount(), 1);
62 QCOMPARE(fcurve.startTime(), 0.0f);
63 QCOMPARE(fcurve.endTime(), 0.0f);
64
65 // WHEN
66 const Keyframe kf1{.value: 5.0f, .leftControlPoint: {45.0f, 5.0f}, .rightControlPoint: {55.0f, 5.0f}, .interpolation: QKeyFrame::BezierInterpolation};
67 fcurve.appendKeyframe(localTime: 50.0f, keyframe: kf1);
68
69 // THEN
70 QCOMPARE(fcurve.keyframeCount(), 2);
71 QCOMPARE(fcurve.startTime(), 0.0f);
72 QCOMPARE(fcurve.endTime(), 50.0f);
73 QCOMPARE(fcurve.keyframe(0), kf0);
74 QCOMPARE(fcurve.keyframe(1), kf1);
75 }
76
77 void checkClearKeyframes()
78 {
79 // GIVEN
80 FCurve fcurve;
81 fcurve.appendKeyframe(localTime: 0.0f, keyframe: Keyframe{.value: 0.0f, .leftControlPoint: {-5.0f, 0.0f}, .rightControlPoint: {5.0f, 0.0f}, .interpolation: QKeyFrame::BezierInterpolation});
82 fcurve.appendKeyframe(localTime: 50.0f, keyframe: Keyframe{.value: 5.0f, .leftControlPoint: {45.0f, 5.0f}, .rightControlPoint: {55.0f, 5.0f}, .interpolation: QKeyFrame::BezierInterpolation});
83
84 // WHEN
85 fcurve.clearKeyframes();
86
87 // THEN
88 QCOMPARE(fcurve.keyframeCount(), 0);
89 QCOMPARE(fcurve.startTime(), 0.0f);
90 QCOMPARE(fcurve.endTime(), 0.0f);
91 }
92
93 void checkEvaluateAtTime_data()
94 {
95
96 QTest::addColumn<float>(name: "time");
97 QTest::addColumn<float>(name: "expectedValue");
98
99 QTest::addRow(format: "Constant_before_time") << 0.0f << 1.0f;
100 QTest::addRow(format: "Constant_1.5") << 1.5f << 1.0f;
101 QTest::addRow(format: "Constant_2.5") << 2.5f << 2.0f;
102 QTest::addRow(format: "Constant_after_time") << 3.5f << 4.0f;
103 }
104
105 void checkEvaluateAtTime()
106 {
107 QFETCH(float, time);
108 QFETCH(float, expectedValue);
109
110 FCurve curve;
111 curve.appendKeyframe(localTime: 1.0, keyframe: Keyframe{.value: 1.0, .leftControlPoint: {0.0, 0.0}, .rightControlPoint: {0.0,0.0}, .interpolation: QKeyFrame::ConstantInterpolation});
112 curve.appendKeyframe(localTime: 2.0, keyframe: Keyframe{.value: 2.0, .leftControlPoint: {0.0, 0.0}, .rightControlPoint: {0.0,0.0}, .interpolation: QKeyFrame::ConstantInterpolation});
113 curve.appendKeyframe(localTime: 3.0, keyframe: Keyframe{.value: 4.0, .leftControlPoint: {0.0, 0.0}, .rightControlPoint: {0.0,0.0}, .interpolation: QKeyFrame::ConstantInterpolation});
114
115 float actualValue = curve.evaluateAtTime(localTime: time);
116
117 QCOMPARE(expectedValue, actualValue);
118 }
119};
120
121QTEST_APPLESS_MAIN(tst_FCurve)
122
123#include "tst_fcurve.moc"
124

source code of qt3d/tests/auto/animation/fcurve/tst_fcurve.cpp