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