1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
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.h> |
30 | #include <Qt3DAnimation/qkeyframeanimation.h> |
31 | #include <qobject.h> |
32 | #include <qsignalspy.h> |
33 | |
34 | class tst_QKeyframeAnimation : public QObject |
35 | { |
36 | Q_OBJECT |
37 | |
38 | private Q_SLOTS: |
39 | |
40 | void initTestCase() |
41 | { |
42 | qRegisterMetaType<Qt3DAnimation::QKeyframeAnimation::RepeatMode>( |
43 | typeName: "QKeyframeAnimation::RepeatMode" ); |
44 | } |
45 | |
46 | void checkDefaultConstruction() |
47 | { |
48 | // GIVEN |
49 | Qt3DAnimation::QKeyframeAnimation keyframeAnimation; |
50 | |
51 | // THEN |
52 | QCOMPARE(keyframeAnimation.target(), nullptr); |
53 | QCOMPARE(keyframeAnimation.easing(), QEasingCurve()); |
54 | QCOMPARE(keyframeAnimation.targetName(), QString()); |
55 | QCOMPARE(keyframeAnimation.startMode(), Qt3DAnimation::QKeyframeAnimation::Constant); |
56 | QCOMPARE(keyframeAnimation.endMode(), Qt3DAnimation::QKeyframeAnimation::Constant); |
57 | } |
58 | |
59 | void checkPropertyChanges() |
60 | { |
61 | // GIVEN |
62 | Qt3DAnimation::QKeyframeAnimation keyframeAnimation; |
63 | |
64 | { |
65 | // WHEN |
66 | QScopedPointer<Qt3DCore::QTransform> transform(new Qt3DCore::QTransform); |
67 | QSignalSpy spy(&keyframeAnimation, SIGNAL(targetChanged(Qt3DCore::QTransform *))); |
68 | Qt3DCore::QTransform * newValue = transform.data(); |
69 | keyframeAnimation.setTarget(newValue); |
70 | |
71 | // THEN |
72 | QCOMPARE(keyframeAnimation.target(), newValue); |
73 | QCOMPARE(spy.count(), 1); |
74 | |
75 | // WHEN |
76 | spy.clear(); |
77 | keyframeAnimation.setTarget(newValue); |
78 | |
79 | // THEN |
80 | QCOMPARE(keyframeAnimation.target(), newValue); |
81 | QCOMPARE(spy.count(), 0); |
82 | |
83 | } |
84 | { |
85 | // WHEN |
86 | QSignalSpy spy(&keyframeAnimation, SIGNAL(easingChanged(const QEasingCurve &))); |
87 | const QEasingCurve newValue = QEasingCurve(QEasingCurve::CosineCurve); |
88 | keyframeAnimation.setEasing(newValue); |
89 | |
90 | // THEN |
91 | QCOMPARE(keyframeAnimation.easing(), newValue); |
92 | QCOMPARE(spy.count(), 1); |
93 | |
94 | // WHEN |
95 | spy.clear(); |
96 | keyframeAnimation.setEasing(newValue); |
97 | |
98 | // THEN |
99 | QCOMPARE(keyframeAnimation.easing(), newValue); |
100 | QCOMPARE(spy.count(), 0); |
101 | |
102 | } |
103 | { |
104 | // WHEN |
105 | QSignalSpy spy(&keyframeAnimation, SIGNAL(targetNameChanged(QString))); |
106 | const QString newValue = QString("target" ); |
107 | keyframeAnimation.setTargetName(newValue); |
108 | |
109 | // THEN |
110 | QCOMPARE(keyframeAnimation.targetName(), newValue); |
111 | QCOMPARE(spy.count(), 1); |
112 | |
113 | // WHEN |
114 | spy.clear(); |
115 | keyframeAnimation.setTargetName(newValue); |
116 | |
117 | // THEN |
118 | QCOMPARE(keyframeAnimation.targetName(), newValue); |
119 | QCOMPARE(spy.count(), 0); |
120 | |
121 | } |
122 | { |
123 | // WHEN |
124 | QSignalSpy spy(&keyframeAnimation, |
125 | SIGNAL(startModeChanged(QKeyframeAnimation::RepeatMode))); |
126 | const Qt3DAnimation::QKeyframeAnimation::RepeatMode newValue |
127 | = Qt3DAnimation::QKeyframeAnimation::Repeat; |
128 | keyframeAnimation.setStartMode(newValue); |
129 | |
130 | // THEN |
131 | QCOMPARE(keyframeAnimation.startMode(), newValue); |
132 | QCOMPARE(spy.count(), 1); |
133 | |
134 | // WHEN |
135 | spy.clear(); |
136 | keyframeAnimation.setStartMode(newValue); |
137 | |
138 | // THEN |
139 | QCOMPARE(keyframeAnimation.startMode(), newValue); |
140 | QCOMPARE(spy.count(), 0); |
141 | |
142 | } |
143 | { |
144 | // WHEN |
145 | QSignalSpy spy(&keyframeAnimation, |
146 | SIGNAL(endModeChanged(QKeyframeAnimation::RepeatMode))); |
147 | const Qt3DAnimation::QKeyframeAnimation::RepeatMode newValue |
148 | = Qt3DAnimation::QKeyframeAnimation::Repeat; |
149 | keyframeAnimation.setEndMode(newValue); |
150 | |
151 | // THEN |
152 | QCOMPARE(keyframeAnimation.endMode(), newValue); |
153 | QCOMPARE(spy.count(), 1); |
154 | |
155 | // WHEN |
156 | spy.clear(); |
157 | keyframeAnimation.setEndMode(newValue); |
158 | |
159 | // THEN |
160 | QCOMPARE(keyframeAnimation.endMode(), newValue); |
161 | QCOMPARE(spy.count(), 0); |
162 | |
163 | } |
164 | } |
165 | |
166 | void testAnimating() |
167 | { |
168 | // GIVEN |
169 | Qt3DAnimation::QKeyframeAnimation keyframeAnimation; |
170 | Qt3DCore::QTransform targetTransform; |
171 | keyframeAnimation.setTarget(&targetTransform); |
172 | |
173 | const float positions[3] = {0.0f, 1.0f, 2.0f}; |
174 | |
175 | Qt3DCore::QTransform keyframes[3]; |
176 | keyframes[0].setScale(1.0f); |
177 | keyframes[1].setScale(2.0f); |
178 | keyframes[2].setScale(3.0f); |
179 | |
180 | keyframes[0].setTranslation(QVector3D(0.0f, 0.0f, 0.0f)); |
181 | keyframes[1].setTranslation(QVector3D(1.0f, 1.0f, 0.0f)); |
182 | keyframes[2].setTranslation(QVector3D(2.0f, 0.0f, 2.0f)); |
183 | |
184 | keyframes[0].setRotationX(0.0f); |
185 | keyframes[1].setRotationY(45.0f); |
186 | keyframes[2].setRotationZ(90.0f); |
187 | |
188 | QVector<float> framePositions; |
189 | framePositions.push_back(t: positions[0]); |
190 | framePositions.push_back(t: positions[1]); |
191 | framePositions.push_back(t: positions[2]); |
192 | |
193 | QVector<Qt3DCore::QTransform*> frames; |
194 | frames.push_back(t: &keyframes[0]); |
195 | frames.push_back(t: &keyframes[1]); |
196 | frames.push_back(t: &keyframes[2]); |
197 | |
198 | keyframeAnimation.setFramePositions(framePositions); |
199 | keyframeAnimation.setKeyframes(frames); |
200 | |
201 | { |
202 | // WHEN |
203 | keyframeAnimation.setPosition(0.0f); |
204 | |
205 | // THEN |
206 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(1.0f, 1.0f, 1.0f))); |
207 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(0.0f, 0.0f, 0.0f))); |
208 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
209 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 0.0f, 0.0f)))); |
210 | |
211 | // WHEN |
212 | keyframeAnimation.setPosition(1.0f); |
213 | |
214 | // THEN |
215 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(2.0f, 2.0f, 2.0f))); |
216 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(1.0f, 1.0f, 0.0f))); |
217 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
218 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 45.0f, 0.0f)))); |
219 | |
220 | // WHEN |
221 | keyframeAnimation.setPosition(2.0f); |
222 | |
223 | // THEN |
224 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(3.0f, 3.0f, 3.0f))); |
225 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(2.0f, 0.0f, 2.0f))); |
226 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
227 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 0.0f, 90.0f)))); |
228 | |
229 | // WHEN |
230 | keyframeAnimation.setPosition(3.0f); |
231 | |
232 | // THEN |
233 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(3.0f, 3.0f, 3.0f))); |
234 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(2.0f, 0.0f, 2.0f))); |
235 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
236 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 0.0f, 90.0f)))); |
237 | |
238 | // WHEN |
239 | keyframeAnimation.setStartMode(Qt3DAnimation::QKeyframeAnimation::None); |
240 | keyframeAnimation.setEndMode(Qt3DAnimation::QKeyframeAnimation::None); |
241 | keyframeAnimation.setPosition(-1.0f); |
242 | |
243 | // THEN |
244 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(3.0f, 3.0f, 3.0f))); |
245 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(2.0f, 0.0f, 2.0f))); |
246 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
247 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 0.0f, 90.0f)))); |
248 | |
249 | // WHEN |
250 | keyframeAnimation.setPosition(5.0f); |
251 | |
252 | // THEN |
253 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(3.0f, 3.0f, 3.0f))); |
254 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(2.0f, 0.0f, 2.0f))); |
255 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
256 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 0.0f, 90.0f)))); |
257 | |
258 | // WHEN |
259 | keyframeAnimation.setStartMode(Qt3DAnimation::QKeyframeAnimation::Repeat); |
260 | keyframeAnimation.setEndMode(Qt3DAnimation::QKeyframeAnimation::Repeat); |
261 | keyframeAnimation.setPosition(-1.0f); |
262 | |
263 | // THEN |
264 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(2.0f, 2.0f, 2.0f))); |
265 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(1.0f, 1.0f, 0.0f))); |
266 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
267 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 45.0f, 0.0f)))); |
268 | |
269 | // WHEN |
270 | keyframeAnimation.setStartMode(Qt3DAnimation::QKeyframeAnimation::Repeat); |
271 | keyframeAnimation.setEndMode(Qt3DAnimation::QKeyframeAnimation::Repeat); |
272 | keyframeAnimation.setPosition(4.0f); |
273 | |
274 | // THEN |
275 | QVERIFY(qFuzzyCompare(targetTransform.scale3D(), QVector3D(1.0f, 1.0f, 1.0f))); |
276 | QVERIFY(qFuzzyCompare(targetTransform.translation(), QVector3D(0.0f, 0.0f, 0.0f))); |
277 | QVERIFY(qFuzzyCompare(targetTransform.rotation(), |
278 | QQuaternion::fromEulerAngles(QVector3D(0.0f, 0.0f, 0.0f)))); |
279 | } |
280 | } |
281 | }; |
282 | |
283 | QTEST_APPLESS_MAIN(tst_QKeyframeAnimation) |
284 | |
285 | #include "tst_qkeyframeanimation.moc" |
286 | |