| 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/qanimationcontroller.h> |
| 6 | #include <Qt3DAnimation/qkeyframeanimation.h> |
| 7 | #include <Qt3DAnimation/qmorphinganimation.h> |
| 8 | #include <qobject.h> |
| 9 | #include <qsignalspy.h> |
| 10 | |
| 11 | class tst_QAnimationController : public QObject |
| 12 | { |
| 13 | Q_OBJECT |
| 14 | |
| 15 | private Q_SLOTS: |
| 16 | |
| 17 | void checkDefaultConstruction() |
| 18 | { |
| 19 | // GIVEN |
| 20 | Qt3DAnimation::QAnimationController animationController; |
| 21 | |
| 22 | // THEN |
| 23 | QCOMPARE(animationController.activeAnimationGroup(), 0); |
| 24 | QCOMPARE(animationController.position(), 0.0f); |
| 25 | QCOMPARE(animationController.positionScale(), 1.0f); |
| 26 | QCOMPARE(animationController.positionOffset(), 0.0f); |
| 27 | QCOMPARE(animationController.entity(), nullptr); |
| 28 | QCOMPARE(animationController.recursive(), true); |
| 29 | } |
| 30 | |
| 31 | void checkPropertyChanges() |
| 32 | { |
| 33 | // GIVEN |
| 34 | Qt3DAnimation::QAnimationController animationController; |
| 35 | |
| 36 | { |
| 37 | // WHEN |
| 38 | QSignalSpy spy(&animationController, SIGNAL(activeAnimationGroupChanged(int))); |
| 39 | const int newValue = 1; |
| 40 | animationController.setActiveAnimationGroup(newValue); |
| 41 | |
| 42 | // THEN |
| 43 | QCOMPARE(animationController.activeAnimationGroup(), newValue); |
| 44 | QCOMPARE(spy.size(), 1); |
| 45 | |
| 46 | // WHEN |
| 47 | spy.clear(); |
| 48 | animationController.setActiveAnimationGroup(newValue); |
| 49 | |
| 50 | // THEN |
| 51 | QCOMPARE(animationController.activeAnimationGroup(), newValue); |
| 52 | QCOMPARE(spy.size(), 0); |
| 53 | |
| 54 | } |
| 55 | { |
| 56 | // WHEN |
| 57 | QSignalSpy spy(&animationController, SIGNAL(positionChanged(float))); |
| 58 | const float newValue = 2.0f; |
| 59 | animationController.setPosition(newValue); |
| 60 | |
| 61 | // THEN |
| 62 | QCOMPARE(animationController.position(), newValue); |
| 63 | QCOMPARE(spy.size(), 1); |
| 64 | |
| 65 | // WHEN |
| 66 | spy.clear(); |
| 67 | animationController.setPosition(newValue); |
| 68 | |
| 69 | // THEN |
| 70 | QCOMPARE(animationController.position(), newValue); |
| 71 | QCOMPARE(spy.size(), 0); |
| 72 | |
| 73 | } |
| 74 | { |
| 75 | // WHEN |
| 76 | QSignalSpy spy(&animationController, SIGNAL(positionScaleChanged(float))); |
| 77 | const float newValue = 3.0f; |
| 78 | animationController.setPositionScale(newValue); |
| 79 | |
| 80 | // THEN |
| 81 | QCOMPARE(animationController.positionScale(), newValue); |
| 82 | QCOMPARE(spy.size(), 1); |
| 83 | |
| 84 | // WHEN |
| 85 | spy.clear(); |
| 86 | animationController.setPositionScale(newValue); |
| 87 | |
| 88 | // THEN |
| 89 | QCOMPARE(animationController.positionScale(), newValue); |
| 90 | QCOMPARE(spy.size(), 0); |
| 91 | |
| 92 | } |
| 93 | { |
| 94 | // WHEN |
| 95 | QSignalSpy spy(&animationController, SIGNAL(positionOffsetChanged(float))); |
| 96 | const float newValue = -1.0f; |
| 97 | animationController.setPositionOffset(newValue); |
| 98 | |
| 99 | // THEN |
| 100 | QCOMPARE(animationController.positionOffset(), newValue); |
| 101 | QCOMPARE(spy.size(), 1); |
| 102 | |
| 103 | // WHEN |
| 104 | spy.clear(); |
| 105 | animationController.setPositionOffset(newValue); |
| 106 | |
| 107 | // THEN |
| 108 | QCOMPARE(animationController.positionOffset(), newValue); |
| 109 | QCOMPARE(spy.size(), 0); |
| 110 | |
| 111 | } |
| 112 | { |
| 113 | // WHEN |
| 114 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity); |
| 115 | QSignalSpy spy(&animationController, SIGNAL(entityChanged(Qt3DCore::QEntity*))); |
| 116 | Qt3DCore::QEntity * newValue = entity.data(); |
| 117 | animationController.setEntity(newValue); |
| 118 | |
| 119 | // THEN |
| 120 | QCOMPARE(animationController.entity(), newValue); |
| 121 | QCOMPARE(spy.size(), 1); |
| 122 | |
| 123 | // WHEN |
| 124 | spy.clear(); |
| 125 | animationController.setEntity(newValue); |
| 126 | |
| 127 | // THEN |
| 128 | QCOMPARE(animationController.entity(), newValue); |
| 129 | QCOMPARE(spy.size(), 0); |
| 130 | |
| 131 | } |
| 132 | { |
| 133 | // WHEN |
| 134 | QSignalSpy spy(&animationController, SIGNAL(recursiveChanged(bool))); |
| 135 | const bool newValue = false; |
| 136 | animationController.setRecursive(newValue); |
| 137 | |
| 138 | // THEN |
| 139 | QCOMPARE(animationController.recursive(), newValue); |
| 140 | QCOMPARE(spy.size(), 1); |
| 141 | |
| 142 | // WHEN |
| 143 | spy.clear(); |
| 144 | animationController.setRecursive(newValue); |
| 145 | |
| 146 | // THEN |
| 147 | QCOMPARE(animationController.recursive(), newValue); |
| 148 | QCOMPARE(spy.size(), 0); |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void testSetEntity() |
| 154 | { |
| 155 | // GIVEN |
| 156 | Qt3DAnimation::QAnimationController animationController; |
| 157 | Qt3DAnimation::QKeyframeAnimation *keyframeAnimation; |
| 158 | Qt3DAnimation::QKeyframeAnimation *keyframeAnimation2; |
| 159 | Qt3DAnimation::QMorphingAnimation *morphingAnimation; |
| 160 | |
| 161 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity); |
| 162 | keyframeAnimation = new Qt3DAnimation::QKeyframeAnimation(entity.data()); |
| 163 | keyframeAnimation2 = new Qt3DAnimation::QKeyframeAnimation(entity.data()); |
| 164 | morphingAnimation = new Qt3DAnimation::QMorphingAnimation(entity.data()); |
| 165 | |
| 166 | const QString animName1 = QString("animation1"); |
| 167 | const QString animName2 = QString("animation2"); |
| 168 | morphingAnimation->setAnimationName(animName1); |
| 169 | keyframeAnimation->setAnimationName(animName1); |
| 170 | keyframeAnimation2->setAnimationName(animName2); |
| 171 | |
| 172 | { |
| 173 | // WHEN |
| 174 | animationController.setEntity(entity.data()); |
| 175 | |
| 176 | // THEN |
| 177 | const QList<Qt3DAnimation::QAnimationGroup *> list = animationController.animationGroupList(); |
| 178 | QCOMPARE(list.size(), 2); |
| 179 | |
| 180 | QCOMPARE(list.at(0)->name(), animName1); |
| 181 | QCOMPARE(list.at(1)->name(), animName2); |
| 182 | |
| 183 | QCOMPARE(list.at(0)->animationList().size(), 2); |
| 184 | QCOMPARE(list.at(1)->animationList().size(), 1); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void testSetEntityRecursive() |
| 189 | { |
| 190 | // GIVEN |
| 191 | Qt3DAnimation::QAnimationController animationController; |
| 192 | Qt3DAnimation::QKeyframeAnimation *keyframeAnimation; |
| 193 | Qt3DAnimation::QMorphingAnimation *morphingAnimation; |
| 194 | |
| 195 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity); |
| 196 | keyframeAnimation = new Qt3DAnimation::QKeyframeAnimation(entity.data()); |
| 197 | Qt3DCore::QEntity *childEntity = new Qt3DCore::QEntity(entity.data()); |
| 198 | morphingAnimation = new Qt3DAnimation::QMorphingAnimation(childEntity); |
| 199 | |
| 200 | const QString animName1 = QString("animation1"); |
| 201 | const QString animName2 = QString("animation2"); |
| 202 | |
| 203 | keyframeAnimation->setAnimationName(animName1); |
| 204 | morphingAnimation->setAnimationName(animName2); |
| 205 | |
| 206 | { |
| 207 | // WHEN |
| 208 | animationController.setEntity(entity.data()); |
| 209 | |
| 210 | // THEN |
| 211 | const QList<Qt3DAnimation::QAnimationGroup *> list = animationController.animationGroupList(); |
| 212 | QCOMPARE(list.size(), 2); |
| 213 | |
| 214 | QCOMPARE(list.at(0)->name(), animName1); |
| 215 | QCOMPARE(list.at(1)->name(), animName2); |
| 216 | |
| 217 | QCOMPARE(list.at(0)->animationList().size(), 1); |
| 218 | QCOMPARE(list.at(1)->animationList().size(), 1); |
| 219 | |
| 220 | animationController.setEntity(nullptr); |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | // WHEN |
| 225 | animationController.setRecursive(false); |
| 226 | animationController.setEntity(entity.data()); |
| 227 | |
| 228 | // THEN |
| 229 | const QList<Qt3DAnimation::QAnimationGroup *> list = animationController.animationGroupList(); |
| 230 | QCOMPARE(list.size(), 1); |
| 231 | |
| 232 | QCOMPARE(list.at(0)->name(), animName1); |
| 233 | |
| 234 | QCOMPARE(list.at(0)->animationList().size(), 1); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void testPropagatePosition() |
| 240 | { |
| 241 | // GIVEN |
| 242 | Qt3DAnimation::QAnimationController animationController; |
| 243 | Qt3DAnimation::QKeyframeAnimation *keyframeAnimation; |
| 244 | |
| 245 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity); |
| 246 | keyframeAnimation = new Qt3DAnimation::QKeyframeAnimation(entity.data()); |
| 247 | |
| 248 | const QString animName1 = QString("animation1"); |
| 249 | keyframeAnimation->setAnimationName(animName1); |
| 250 | |
| 251 | { |
| 252 | // WHEN |
| 253 | animationController.setEntity(entity.data()); |
| 254 | animationController.setPosition(2.0f); |
| 255 | |
| 256 | // THEN |
| 257 | QCOMPARE(animationController.animationGroupList().at(0)->position(), 2.0f); |
| 258 | QCOMPARE(keyframeAnimation->position(), 2.0f); |
| 259 | } |
| 260 | |
| 261 | { |
| 262 | // WHEN |
| 263 | animationController.setPositionOffset(1.0); |
| 264 | animationController.setPositionScale(2.0f); |
| 265 | animationController.setPosition(2.0f); |
| 266 | |
| 267 | // THEN |
| 268 | QCOMPARE(animationController.animationGroupList().at(0)->position(), 5.0f); |
| 269 | QCOMPARE(keyframeAnimation->position(), 5.0f); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | }; |
| 274 | |
| 275 | QTEST_APPLESS_MAIN(tst_QAnimationController) |
| 276 | |
| 277 | #include "tst_qanimationcontroller.moc" |