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