| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). | 
| 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> | 
| 30 | #include <Qt3DCore/qarmature.h> | 
| 31 | #include <Qt3DCore/private/qarmature_p.h> | 
| 32 | #include <Qt3DCore/qskeleton.h> | 
| 33 |  | 
| 34 | #include <Qt3DCore/private/qnode_p.h> | 
| 35 | #include <Qt3DCore/private/qscene_p.h> | 
| 36 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> | 
| 37 |  | 
| 38 | #include "testpostmanarbiter.h" | 
| 39 |  | 
| 40 | using namespace Qt3DCore; | 
| 41 |  | 
| 42 | class tst_QArmature: public QObject | 
| 43 | { | 
| 44 |     Q_OBJECT | 
| 45 | public: | 
| 46 |     tst_QArmature() | 
| 47 |     { | 
| 48 |         qRegisterMetaType<Qt3DCore::QAbstractSkeleton*>(); | 
| 49 |     } | 
| 50 |  | 
| 51 | private Q_SLOTS: | 
| 52 |  | 
| 53 |     void checkCreationChange_data() | 
| 54 |     { | 
| 55 |         QTest::addColumn<QArmature *>(name: "armature" ); | 
| 56 |  | 
| 57 |         QArmature *defaultConstructed = new QArmature(); | 
| 58 |         QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed; | 
| 59 |  | 
| 60 |         QArmature *armatureWithSkeleton = new QArmature(); | 
| 61 |         armatureWithSkeleton->setSkeleton(new QSkeleton()); | 
| 62 |         QTest::newRow(dataTag: "skeletonWithOneJoint" ) << armatureWithSkeleton; | 
| 63 |     } | 
| 64 |  | 
| 65 |     void checkCreationChange() | 
| 66 |     { | 
| 67 |         // GIVEN | 
| 68 |         QFETCH(QArmature *, armature); | 
| 69 |  | 
| 70 |         // WHEN | 
| 71 |         QNodeCreatedChangeGenerator creationChangeGenerator(armature); | 
| 72 |         QVector<QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); | 
| 73 |  | 
| 74 |         const int skeletonCount = armature->skeleton() ? 1 : 0; | 
| 75 |  | 
| 76 |         // THEN | 
| 77 |         QCOMPARE(creationChanges.size(), 1 + skeletonCount); | 
| 78 |  | 
| 79 |         const auto creationChangeData = qSharedPointerCast<QNodeCreatedChange<QArmatureData>>(src: creationChanges.first()); | 
| 80 |         const QArmatureData &data = creationChangeData->data; | 
| 81 |  | 
| 82 |         // THEN | 
| 83 |         QCOMPARE(armature->id(), creationChangeData->subjectId()); | 
| 84 |         QCOMPARE(armature->isEnabled(), creationChangeData->isNodeEnabled()); | 
| 85 |         QCOMPARE(armature->metaObject(), creationChangeData->metaObject()); | 
| 86 |         if (armature->skeleton()) { | 
| 87 |             QCOMPARE(armature->skeleton()->id(), data.skeletonId); | 
| 88 |         } | 
| 89 |     } | 
| 90 |  | 
| 91 |     void checkPropertyUpdates() | 
| 92 |     { | 
| 93 |         // GIVEN | 
| 94 |         TestArbiter arbiter; | 
| 95 |         QScopedPointer<QArmature> armature(new QArmature()); | 
| 96 |         arbiter.setArbiterOnNode(armature.data()); | 
| 97 |  | 
| 98 |         // WHEN | 
| 99 |         QSkeleton *skeleton = new QSkeleton(armature.data()); | 
| 100 |         armature->setSkeleton(skeleton); | 
| 101 |  | 
| 102 |         // THEN | 
| 103 |         QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 104 |         QCOMPARE(arbiter.dirtyNodes.front(), armature.data()); | 
| 105 |  | 
| 106 |         arbiter.dirtyNodes.clear(); | 
| 107 |  | 
| 108 |         // WHEN | 
| 109 |         armature->setSkeleton(nullptr); | 
| 110 |  | 
| 111 |         // THEN | 
| 112 |         QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 113 |         QCOMPARE(arbiter.dirtyNodes.front(), armature.data()); | 
| 114 |  | 
| 115 |         arbiter.dirtyNodes.clear(); | 
| 116 |     } | 
| 117 |  | 
| 118 |     void checkSkeletonBookkeeping() | 
| 119 |     { | 
| 120 |         // GIVEN | 
| 121 |         QScopedPointer<QArmature> armature(new QArmature); | 
| 122 |         { | 
| 123 |             // WHEN | 
| 124 |             QSkeleton skeleton; | 
| 125 |             armature->setSkeleton(&skeleton); | 
| 126 |  | 
| 127 |             // THEN | 
| 128 |             QCOMPARE(skeleton.parent(), armature.data()); | 
| 129 |             QCOMPARE(armature->skeleton(), &skeleton); | 
| 130 |         } | 
| 131 |         // THEN (Should not crash and parameter be unset) | 
| 132 |         QVERIFY(armature->skeleton() == nullptr); | 
| 133 |  | 
| 134 |         { | 
| 135 |             // WHEN | 
| 136 |             QArmature someOtherArmature; | 
| 137 |             QScopedPointer<QSkeleton> skeleton(new QSkeleton(&someOtherArmature)); | 
| 138 |             armature->setSkeleton(skeleton.data()); | 
| 139 |  | 
| 140 |             // THEN | 
| 141 |             QCOMPARE(skeleton->parent(), &someOtherArmature); | 
| 142 |             QCOMPARE(armature->skeleton(), skeleton.data()); | 
| 143 |  | 
| 144 |             // WHEN | 
| 145 |             armature.reset(); | 
| 146 |             skeleton.reset(); | 
| 147 |  | 
| 148 |             // THEN Should not crash when the joint is destroyed (tests for failed removal of destruction helper) | 
| 149 |         } | 
| 150 |     } | 
| 151 | }; | 
| 152 |  | 
| 153 | QTEST_MAIN(tst_QArmature) | 
| 154 |  | 
| 155 | #include "tst_qarmature.moc" | 
| 156 |  |