| 1 | // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <qbackendnodetester.h> |
| 6 | #include <Qt3DAnimation/private/handler_p.h> |
| 7 | #include <Qt3DAnimation/private/channelmapping_p.h> |
| 8 | #include <Qt3DAnimation/qchannelmapping.h> |
| 9 | #include <Qt3DAnimation/qskeletonmapping.h> |
| 10 | #include <Qt3DAnimation/private/qchannelmapping_p.h> |
| 11 | #include <Qt3DCore/qentity.h> |
| 12 | #include <Qt3DCore/qtransform.h> |
| 13 | #include <Qt3DCore/qskeleton.h> |
| 14 | #include <Qt3DCore/private/qnode_p.h> |
| 15 | #include <Qt3DCore/private/qscene_p.h> |
| 16 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 17 | #include "testarbiter.h" |
| 18 | |
| 19 | class tst_TargetEntity : public Qt3DCore::QEntity |
| 20 | { |
| 21 | Q_OBJECT |
| 22 | Q_PROPERTY(QVector2D foo MEMBER m_foo NOTIFY fooChanged) |
| 23 | |
| 24 | signals: |
| 25 | void fooChanged(); |
| 26 | |
| 27 | private: |
| 28 | QVector2D m_foo; |
| 29 | }; |
| 30 | |
| 31 | class tst_ChannelMapping : public Qt3DCore::QBackendNodeTester |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | |
| 35 | private Q_SLOTS: |
| 36 | void checkPeerPropertyMirroring() |
| 37 | { |
| 38 | // GIVEN |
| 39 | Qt3DAnimation::Animation::Handler handler; |
| 40 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
| 41 | backendMapping.setHandler(&handler); |
| 42 | Qt3DAnimation::QChannelMapping mapping; |
| 43 | auto target = new tst_TargetEntity; |
| 44 | |
| 45 | mapping.setChannelName(QLatin1String("Location")); |
| 46 | mapping.setTarget(target); |
| 47 | mapping.setProperty(QLatin1String("foo")); |
| 48 | |
| 49 | // WHEN |
| 50 | simulateInitializationSync(&mapping, &backendMapping); |
| 51 | |
| 52 | // THEN |
| 53 | QCOMPARE(backendMapping.peerId(), mapping.id()); |
| 54 | QCOMPARE(backendMapping.isEnabled(), mapping.isEnabled()); |
| 55 | QCOMPARE(backendMapping.channelName(), mapping.channelName()); |
| 56 | QCOMPARE(backendMapping.targetId(), mapping.target()->id()); |
| 57 | QVERIFY(qstrcmp(backendMapping.propertyName(), mapping.property().toLatin1().constData()) == 0); |
| 58 | QVERIFY(qstrcmp(backendMapping.propertyName(), "foo") == 0); |
| 59 | QCOMPARE(backendMapping.componentCount(), 2); |
| 60 | QCOMPARE(backendMapping.type(), static_cast<int>(QMetaType::QVector2D)); |
| 61 | QCOMPARE(backendMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::ChannelMappingType); |
| 62 | |
| 63 | // GIVEN |
| 64 | Qt3DAnimation::Animation::ChannelMapping backendSkeletonMapping; |
| 65 | backendSkeletonMapping.setHandler(&handler); |
| 66 | Qt3DAnimation::QSkeletonMapping skeletonMapping; |
| 67 | auto skeleton = new Qt3DCore::QSkeleton; |
| 68 | skeletonMapping.setSkeleton(skeleton); |
| 69 | |
| 70 | // WHEN |
| 71 | simulateInitializationSync(&skeletonMapping, &backendSkeletonMapping); |
| 72 | |
| 73 | // THEN |
| 74 | QCOMPARE(backendSkeletonMapping.peerId(), skeletonMapping.id()); |
| 75 | QCOMPARE(backendSkeletonMapping.isEnabled(), skeletonMapping.isEnabled()); |
| 76 | QCOMPARE(backendSkeletonMapping.skeletonId(), skeletonMapping.skeleton()->id()); |
| 77 | QCOMPARE(backendSkeletonMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::SkeletonMappingType); |
| 78 | } |
| 79 | |
| 80 | void checkInitialAndCleanedUpState() |
| 81 | { |
| 82 | // GIVEN |
| 83 | Qt3DAnimation::Animation::Handler handler; |
| 84 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
| 85 | backendMapping.setHandler(&handler); |
| 86 | |
| 87 | // THEN |
| 88 | QVERIFY(backendMapping.peerId().isNull()); |
| 89 | QCOMPARE(backendMapping.isEnabled(), false); |
| 90 | QCOMPARE(backendMapping.channelName(), QString()); |
| 91 | QCOMPARE(backendMapping.targetId(), Qt3DCore::QNodeId()); |
| 92 | QCOMPARE(backendMapping.propertyName(), nullptr); |
| 93 | QCOMPARE(backendMapping.componentCount(), 0); |
| 94 | QCOMPARE(backendMapping.type(), static_cast<int>(QMetaType::UnknownType)); |
| 95 | QCOMPARE(backendMapping.skeletonId(), Qt3DCore::QNodeId()); |
| 96 | QCOMPARE(backendMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::ChannelMappingType); |
| 97 | |
| 98 | // GIVEN |
| 99 | Qt3DAnimation::QChannelMapping mapping; |
| 100 | auto target = new tst_TargetEntity; |
| 101 | mapping.setChannelName(QLatin1String("Location")); |
| 102 | mapping.setTarget(target); |
| 103 | mapping.setProperty(QLatin1String("foo")); |
| 104 | |
| 105 | // WHEN |
| 106 | simulateInitializationSync(&mapping, &backendMapping); |
| 107 | backendMapping.setSkeletonId(Qt3DCore::QNodeId::createId()); |
| 108 | backendMapping.cleanup(); |
| 109 | |
| 110 | // THEN |
| 111 | QCOMPARE(backendMapping.isEnabled(), false); |
| 112 | QCOMPARE(backendMapping.channelName(), QString()); |
| 113 | QCOMPARE(backendMapping.targetId(), Qt3DCore::QNodeId()); |
| 114 | QCOMPARE(backendMapping.propertyName(), nullptr); |
| 115 | QCOMPARE(backendMapping.componentCount(), 0); |
| 116 | QCOMPARE(backendMapping.type(), static_cast<int>(QMetaType::UnknownType)); |
| 117 | QCOMPARE(backendMapping.skeletonId(), Qt3DCore::QNodeId()); |
| 118 | QCOMPARE(backendMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::ChannelMappingType); |
| 119 | } |
| 120 | |
| 121 | void checkPropertyChanges() |
| 122 | { |
| 123 | // GIVEN |
| 124 | Qt3DAnimation::QChannelMapping mapping; |
| 125 | Qt3DAnimation::Animation::Handler handler; |
| 126 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
| 127 | backendMapping.setHandler(&handler); |
| 128 | simulateInitializationSync(&mapping, &backendMapping); |
| 129 | |
| 130 | // WHEN |
| 131 | mapping.setEnabled(false); |
| 132 | backendMapping.syncFromFrontEnd(&mapping, false); |
| 133 | |
| 134 | // THEN |
| 135 | QCOMPARE(backendMapping.isEnabled(), false); |
| 136 | |
| 137 | // WHEN |
| 138 | const QString channelName(QLatin1String("Translation")); |
| 139 | mapping.setChannelName(channelName); |
| 140 | backendMapping.syncFromFrontEnd(&mapping, false); |
| 141 | |
| 142 | // THEN |
| 143 | QCOMPARE(backendMapping.channelName(), channelName); |
| 144 | |
| 145 | // WHEN |
| 146 | const auto target = new Qt3DCore::QTransform(); |
| 147 | mapping.setTarget(target); |
| 148 | mapping.setProperty("translation"); |
| 149 | backendMapping.syncFromFrontEnd(&mapping, false); |
| 150 | |
| 151 | // THEN |
| 152 | QCOMPARE(backendMapping.targetId(), target->id()); |
| 153 | QCOMPARE(backendMapping.type(), static_cast<int>(QMetaType::QVector3D)); |
| 154 | QCOMPARE(backendMapping.componentCount(), 3); |
| 155 | |
| 156 | const char *testName = "translation"; |
| 157 | QCOMPARE(qstrcmp(testName, backendMapping.propertyName()), 0); |
| 158 | } |
| 159 | |
| 160 | void checkSkeletonPropertyUpdate() |
| 161 | { |
| 162 | // GIVEN |
| 163 | Qt3DAnimation::QSkeletonMapping mapping; |
| 164 | Qt3DAnimation::Animation::Handler handler; |
| 165 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
| 166 | backendMapping.setHandler(&handler); |
| 167 | simulateInitializationSync(&mapping, &backendMapping); |
| 168 | |
| 169 | // WHEN |
| 170 | mapping.setEnabled(false); |
| 171 | backendMapping.syncFromFrontEnd(&mapping, false); |
| 172 | |
| 173 | // THEN |
| 174 | QCOMPARE(backendMapping.isEnabled(), false); |
| 175 | |
| 176 | // WHEN |
| 177 | auto skeleton = new Qt3DCore::QSkeleton; |
| 178 | mapping.setSkeleton(skeleton); |
| 179 | backendMapping.syncFromFrontEnd(&mapping, false); |
| 180 | |
| 181 | // THEN |
| 182 | QCOMPARE(backendMapping.skeletonId(), skeleton->id()); |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | QTEST_MAIN(tst_ChannelMapping) |
| 187 | |
| 188 | #include "tst_channelmapping.moc" |