| 1 | // Copyright (C) 2017 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/qmorphinganimation.h> |
| 6 | #include <qobject.h> |
| 7 | #include <qsignalspy.h> |
| 8 | |
| 9 | class tst_QMorphingAnimation : public QObject |
| 10 | { |
| 11 | Q_OBJECT |
| 12 | |
| 13 | bool verifyAttribute(Qt3DCore::QGeometry *geometry, QString name, |
| 14 | Qt3DCore::QAttribute *attribute) |
| 15 | { |
| 16 | const QList<Qt3DCore::QAttribute *> attributes = geometry->attributes(); |
| 17 | for (const Qt3DCore::QAttribute *attr : attributes) { |
| 18 | if (attr->name() == name) { |
| 19 | if (attr == attribute) |
| 20 | return true; |
| 21 | return false; |
| 22 | } |
| 23 | } |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | private Q_SLOTS: |
| 28 | |
| 29 | void initTestCase() |
| 30 | { |
| 31 | qRegisterMetaType<Qt3DAnimation::QMorphingAnimation::Method>("QMorphingAnimation::Method"); |
| 32 | } |
| 33 | |
| 34 | void checkDefaultConstruction() |
| 35 | { |
| 36 | // GIVEN |
| 37 | Qt3DAnimation::QMorphingAnimation morphingAnimation; |
| 38 | |
| 39 | // THEN |
| 40 | QCOMPARE(morphingAnimation.interpolator(), 0.0f); |
| 41 | QCOMPARE(morphingAnimation.target(), nullptr); |
| 42 | QCOMPARE(morphingAnimation.targetName(), QString()); |
| 43 | QCOMPARE(morphingAnimation.method(), Qt3DAnimation::QMorphingAnimation::Relative); |
| 44 | QCOMPARE(morphingAnimation.easing(), QEasingCurve()); |
| 45 | } |
| 46 | |
| 47 | void checkPropertyChanges() |
| 48 | { |
| 49 | // GIVEN |
| 50 | Qt3DAnimation::QMorphingAnimation morphingAnimation; |
| 51 | |
| 52 | { |
| 53 | // WHEN |
| 54 | QScopedPointer<Qt3DRender::QGeometryRenderer> gr(new Qt3DRender::QGeometryRenderer); |
| 55 | QSignalSpy spy(&morphingAnimation, |
| 56 | SIGNAL(targetChanged(Qt3DRender::QGeometryRenderer*))); |
| 57 | Qt3DRender::QGeometryRenderer *newValue = gr.data(); |
| 58 | morphingAnimation.setTarget(newValue); |
| 59 | |
| 60 | // THEN |
| 61 | QCOMPARE(morphingAnimation.target(), newValue); |
| 62 | QCOMPARE(spy.size(), 1); |
| 63 | |
| 64 | // WHEN |
| 65 | spy.clear(); |
| 66 | morphingAnimation.setTarget(newValue); |
| 67 | |
| 68 | // THEN |
| 69 | QCOMPARE(morphingAnimation.target(), newValue); |
| 70 | QCOMPARE(spy.size(), 0); |
| 71 | |
| 72 | } |
| 73 | { |
| 74 | // WHEN |
| 75 | QSignalSpy spy(&morphingAnimation, SIGNAL(targetNameChanged(QString))); |
| 76 | const QString newValue = QString("target"); |
| 77 | morphingAnimation.setTargetName(newValue); |
| 78 | |
| 79 | // THEN |
| 80 | QCOMPARE(morphingAnimation.targetName(), newValue); |
| 81 | QCOMPARE(spy.size(), 1); |
| 82 | |
| 83 | // WHEN |
| 84 | spy.clear(); |
| 85 | morphingAnimation.setTargetName(newValue); |
| 86 | |
| 87 | // THEN |
| 88 | QCOMPARE(morphingAnimation.targetName(), newValue); |
| 89 | QCOMPARE(spy.size(), 0); |
| 90 | |
| 91 | } |
| 92 | { |
| 93 | // WHEN |
| 94 | QSignalSpy spy(&morphingAnimation, SIGNAL(methodChanged(QMorphingAnimation::Method))); |
| 95 | const Qt3DAnimation::QMorphingAnimation::Method newValue |
| 96 | = Qt3DAnimation::QMorphingAnimation::Normalized; |
| 97 | morphingAnimation.setMethod(newValue); |
| 98 | |
| 99 | // THEN |
| 100 | QCOMPARE(morphingAnimation.method(), newValue); |
| 101 | QCOMPARE(spy.size(), 1); |
| 102 | |
| 103 | // WHEN |
| 104 | spy.clear(); |
| 105 | morphingAnimation.setMethod(newValue); |
| 106 | |
| 107 | // THEN |
| 108 | QCOMPARE(morphingAnimation.method(), newValue); |
| 109 | QCOMPARE(spy.size(), 0); |
| 110 | |
| 111 | } |
| 112 | { |
| 113 | // WHEN |
| 114 | QSignalSpy spy(&morphingAnimation, SIGNAL(easingChanged(QEasingCurve))); |
| 115 | const QEasingCurve newValue = QEasingCurve(QEasingCurve::InBounce); |
| 116 | morphingAnimation.setEasing(newValue); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(morphingAnimation.easing(), newValue); |
| 120 | QCOMPARE(spy.size(), 1); |
| 121 | |
| 122 | // WHEN |
| 123 | spy.clear(); |
| 124 | morphingAnimation.setEasing(newValue); |
| 125 | |
| 126 | // THEN |
| 127 | QCOMPARE(morphingAnimation.easing(), newValue); |
| 128 | QCOMPARE(spy.size(), 0); |
| 129 | |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void testAnimation() |
| 134 | { |
| 135 | // GIVEN |
| 136 | const QString baseName("position"); |
| 137 | const QString targetName("positionTarget"); |
| 138 | Qt3DAnimation::QMorphingAnimation morphingAnimation; |
| 139 | Qt3DCore::QAttribute *base = new Qt3DCore::QAttribute; |
| 140 | |
| 141 | Qt3DCore::QGeometry *geometry = new Qt3DCore::QGeometry; |
| 142 | Qt3DAnimation::QMorphTarget *mt1 = new Qt3DAnimation::QMorphTarget(&morphingAnimation); |
| 143 | Qt3DAnimation::QMorphTarget *mt2 = new Qt3DAnimation::QMorphTarget(&morphingAnimation); |
| 144 | Qt3DAnimation::QMorphTarget *mt3 = new Qt3DAnimation::QMorphTarget(&morphingAnimation); |
| 145 | Qt3DCore::QAttribute *a1 = new Qt3DCore::QAttribute(geometry); |
| 146 | Qt3DCore::QAttribute *a2 = new Qt3DCore::QAttribute(geometry); |
| 147 | Qt3DCore::QAttribute *a3 = new Qt3DCore::QAttribute(geometry); |
| 148 | Qt3DRender::QGeometryRenderer gr; |
| 149 | |
| 150 | base->setName(baseName); |
| 151 | geometry->addAttribute(base); |
| 152 | gr.setGeometry(geometry); |
| 153 | morphingAnimation.setTarget(&gr); |
| 154 | a1->setName(baseName); |
| 155 | a2->setName(baseName); |
| 156 | a3->setName(baseName); |
| 157 | mt1->addAttribute(a1); |
| 158 | mt2->addAttribute(a2); |
| 159 | mt3->addAttribute(a3); |
| 160 | morphingAnimation.addMorphTarget(mt1); |
| 161 | morphingAnimation.addMorphTarget(mt2); |
| 162 | morphingAnimation.addMorphTarget(mt3); |
| 163 | |
| 164 | const QVector<float> positions = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f }; |
| 165 | morphingAnimation.setTargetPositions(positions); |
| 166 | |
| 167 | morphingAnimation.setWeights(0, QVector<float> { 1.0f, 0.0f, 0.0f }); |
| 168 | morphingAnimation.setWeights(1, QVector<float> { 0.0f, 0.0f, 0.0f }); |
| 169 | morphingAnimation.setWeights(2, QVector<float> { 0.0f, 1.0f, 0.0f }); |
| 170 | morphingAnimation.setWeights(3, QVector<float> { 0.0f, 0.0f, 0.0f }); |
| 171 | morphingAnimation.setWeights(4, QVector<float> { 0.0f, 0.0f, 1.0f }); |
| 172 | |
| 173 | morphingAnimation.setMethod(Qt3DAnimation::QMorphingAnimation::Relative); |
| 174 | { |
| 175 | // WHEN |
| 176 | morphingAnimation.setPosition(0.0f); |
| 177 | |
| 178 | // THEN |
| 179 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -1.0f)); |
| 180 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 181 | QVERIFY(verifyAttribute(geometry, targetName, a1)); |
| 182 | |
| 183 | // WHEN |
| 184 | morphingAnimation.setPosition(0.5f); |
| 185 | |
| 186 | // THEN |
| 187 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -0.5f)); |
| 188 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 189 | QVERIFY(verifyAttribute(geometry, targetName, a1)); |
| 190 | |
| 191 | // WHEN |
| 192 | morphingAnimation.setPosition(1.0f); |
| 193 | |
| 194 | // THEN |
| 195 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -0.0f)); |
| 196 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 197 | |
| 198 | // WHEN |
| 199 | morphingAnimation.setPosition(1.5f); |
| 200 | |
| 201 | // THEN |
| 202 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -0.5f)); |
| 203 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 204 | QVERIFY(verifyAttribute(geometry, targetName, a2)); |
| 205 | |
| 206 | // WHEN |
| 207 | morphingAnimation.setPosition(4.0f); |
| 208 | |
| 209 | // THEN |
| 210 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -1.0f)); |
| 211 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 212 | QVERIFY(verifyAttribute(geometry, targetName, a3)); |
| 213 | } |
| 214 | |
| 215 | morphingAnimation.setMethod(Qt3DAnimation::QMorphingAnimation::Normalized); |
| 216 | { |
| 217 | // WHEN |
| 218 | morphingAnimation.setPosition(0.0f); |
| 219 | |
| 220 | // THEN |
| 221 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 1.0f)); |
| 222 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 223 | QVERIFY(verifyAttribute(geometry, targetName, a1)); |
| 224 | |
| 225 | // WHEN |
| 226 | morphingAnimation.setPosition(0.5f); |
| 227 | |
| 228 | // THEN |
| 229 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 0.5f)); |
| 230 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 231 | QVERIFY(verifyAttribute(geometry, targetName, a1)); |
| 232 | |
| 233 | // WHEN |
| 234 | morphingAnimation.setPosition(1.0f); |
| 235 | |
| 236 | // THEN |
| 237 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 0.0f)); |
| 238 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 239 | |
| 240 | // WHEN |
| 241 | morphingAnimation.setPosition(1.5f); |
| 242 | |
| 243 | // THEN |
| 244 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 0.5f)); |
| 245 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 246 | QVERIFY(verifyAttribute(geometry, targetName, a2)); |
| 247 | |
| 248 | // WHEN |
| 249 | morphingAnimation.setPosition(4.0f); |
| 250 | |
| 251 | // THEN |
| 252 | QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 1.0f)); |
| 253 | QVERIFY(verifyAttribute(geometry, baseName, base)); |
| 254 | QVERIFY(verifyAttribute(geometry, targetName, a3)); |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | QTEST_APPLESS_MAIN(tst_QMorphingAnimation) |
| 260 | |
| 261 | #include "tst_qmorphinganimation.moc" |