| 1 | // Copyright (C) 2017 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DAnimation/qclipblendvalue.h> |
| 7 | #include <Qt3DAnimation/qanimationcliploader.h> |
| 8 | #include <Qt3DAnimation/private/qclipblendvalue_p.h> |
| 9 | #include <Qt3DAnimation/private/clipblendvalue_p.h> |
| 10 | #include "qbackendnodetester.h" |
| 11 | |
| 12 | #include <random> |
| 13 | #include <algorithm> |
| 14 | |
| 15 | using namespace Qt3DAnimation::Animation; |
| 16 | |
| 17 | Q_DECLARE_METATYPE(ClipBlendValue *) |
| 18 | Q_DECLARE_METATYPE(QList<ClipFormat>) |
| 19 | |
| 20 | class tst_ClipBlendValue : public Qt3DCore::QBackendNodeTester |
| 21 | { |
| 22 | Q_OBJECT |
| 23 | public: |
| 24 | AnimationClip *createAnimationClipLoader(Handler *handler, |
| 25 | double duration) |
| 26 | { |
| 27 | auto clipId = Qt3DCore::QNodeId::createId(); |
| 28 | AnimationClip *clip = handler->animationClipLoaderManager()->getOrCreateResource(clipId); |
| 29 | setPeerId(clip, clipId); |
| 30 | clip->setDuration(duration); |
| 31 | return clip; |
| 32 | } |
| 33 | |
| 34 | private Q_SLOTS: |
| 35 | void checkInitialState() |
| 36 | { |
| 37 | // GIVEN |
| 38 | ClipBlendValue backendClipBlendValue; |
| 39 | |
| 40 | // THEN |
| 41 | QCOMPARE(backendClipBlendValue.isEnabled(), false); |
| 42 | QVERIFY(backendClipBlendValue.peerId().isNull()); |
| 43 | QCOMPARE(backendClipBlendValue.clipId(), Qt3DCore::QNodeId()); |
| 44 | QCOMPARE(backendClipBlendValue.blendType(), ClipBlendNode::ValueType); |
| 45 | } |
| 46 | |
| 47 | void checkInitializeFromPeer() |
| 48 | { |
| 49 | // GIVEN |
| 50 | Qt3DAnimation::QClipBlendValue clipBlendValue; |
| 51 | Qt3DAnimation::QAnimationClipLoader clip; |
| 52 | clipBlendValue.setClip(&clip); |
| 53 | |
| 54 | { |
| 55 | // WHEN |
| 56 | ClipBlendValue backendClipBlendValue; |
| 57 | simulateInitializationSync(&clipBlendValue, &backendClipBlendValue); |
| 58 | |
| 59 | // THEN |
| 60 | QCOMPARE(backendClipBlendValue.isEnabled(), true); |
| 61 | QCOMPARE(backendClipBlendValue.peerId(), clipBlendValue.id()); |
| 62 | QCOMPARE(backendClipBlendValue.clipId(), clip.id()); |
| 63 | } |
| 64 | { |
| 65 | // WHEN |
| 66 | ClipBlendValue backendClipBlendValue; |
| 67 | clipBlendValue.setEnabled(false); |
| 68 | simulateInitializationSync(&clipBlendValue, &backendClipBlendValue); |
| 69 | |
| 70 | // THEN |
| 71 | QCOMPARE(backendClipBlendValue.peerId(), clipBlendValue.id()); |
| 72 | QCOMPARE(backendClipBlendValue.isEnabled(), false); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void checkSceneChangeEvents() |
| 77 | { |
| 78 | // GIVEN |
| 79 | Qt3DAnimation::QClipBlendValue clipBlendValue; |
| 80 | ClipBlendValue backendClipBlendValue; |
| 81 | simulateInitializationSync(&clipBlendValue, &backendClipBlendValue); |
| 82 | { |
| 83 | // WHEN |
| 84 | const bool newValue = false; |
| 85 | clipBlendValue.setEnabled(newValue); |
| 86 | backendClipBlendValue.syncFromFrontEnd(&clipBlendValue, false); |
| 87 | |
| 88 | // THEN |
| 89 | QCOMPARE(backendClipBlendValue.isEnabled(), newValue); |
| 90 | } |
| 91 | { |
| 92 | // WHEN |
| 93 | Qt3DAnimation::QAnimationClipLoader newValue; |
| 94 | clipBlendValue.setClip(&newValue); |
| 95 | backendClipBlendValue.syncFromFrontEnd(&clipBlendValue, false); |
| 96 | |
| 97 | // THEN |
| 98 | QCOMPARE(backendClipBlendValue.clipId(), newValue.id()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void checkDependencyIds() |
| 103 | { |
| 104 | // GIVEN |
| 105 | ClipBlendValue clipNode; |
| 106 | auto clipId = Qt3DCore::QNodeId::createId(); |
| 107 | |
| 108 | // WHEN |
| 109 | clipNode.setClipId(clipId); |
| 110 | QList<Qt3DCore::QNodeId> actualIds = clipNode.currentDependencyIds(); |
| 111 | |
| 112 | // THEN |
| 113 | QCOMPARE(actualIds.size(), 0); |
| 114 | |
| 115 | // WHEN |
| 116 | auto anotherClipId = Qt3DCore::QNodeId::createId(); |
| 117 | clipNode.setClipId(anotherClipId); |
| 118 | actualIds = clipNode.currentDependencyIds(); |
| 119 | |
| 120 | // THEN |
| 121 | QCOMPARE(actualIds.size(), 0); |
| 122 | } |
| 123 | |
| 124 | void checkDuration() |
| 125 | { |
| 126 | // GIVEN |
| 127 | auto handler = new Handler(); |
| 128 | const double expectedDuration = 123.5; |
| 129 | auto clip = createAnimationClipLoader(handler, expectedDuration); |
| 130 | ClipBlendValue clipNode; |
| 131 | clipNode.setHandler(handler); |
| 132 | clipNode.setClipBlendNodeManager(handler->clipBlendNodeManager()); |
| 133 | clipNode.setClipId(clip->peerId()); |
| 134 | |
| 135 | // WHEN |
| 136 | double actualDuration = clipNode.duration(); |
| 137 | |
| 138 | // THEN |
| 139 | QCOMPARE(actualDuration, expectedDuration); |
| 140 | } |
| 141 | |
| 142 | void checkFormatIndices_data() |
| 143 | { |
| 144 | QTest::addColumn<ClipBlendValue *>("blendNode"); |
| 145 | QTest::addColumn<QList<int>>("indexes"); |
| 146 | QTest::addColumn<QList<Qt3DCore::QNodeId>>("animatorIds"); |
| 147 | QTest::addColumn<QList<ClipFormat>>("expectedClipFormat"); |
| 148 | |
| 149 | // Single entry |
| 150 | { |
| 151 | auto blendNode = new ClipBlendValue; |
| 152 | QList<Qt3DCore::QNodeId> animatorIds; |
| 153 | QList<ClipFormat> expectedClipFormat; |
| 154 | |
| 155 | const auto animatorId = Qt3DCore::QNodeId::createId(); |
| 156 | animatorIds.push_back(animatorId); |
| 157 | |
| 158 | ClipFormat clipFormat; |
| 159 | clipFormat.sourceClipIndices = { 0, 1, 2 }; |
| 160 | expectedClipFormat.push_back(clipFormat); |
| 161 | |
| 162 | // Set data and indexes |
| 163 | blendNode->setClipFormat(animatorId, clipFormat); |
| 164 | QList<int> indexes = { 0 }; |
| 165 | |
| 166 | QTest::newRow("single entry") |
| 167 | << blendNode << indexes << animatorIds << expectedClipFormat; |
| 168 | } |
| 169 | |
| 170 | // Multiple entries, ordered |
| 171 | { |
| 172 | auto blendNode = new ClipBlendValue; |
| 173 | QList<Qt3DCore::QNodeId> animatorIds; |
| 174 | QList<ClipFormat> expectedClipFormat; |
| 175 | |
| 176 | const int animatorCount = 10; |
| 177 | for (int j = 0; j < animatorCount; ++j) { |
| 178 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 179 | animatorIds.push_back(animatorId); |
| 180 | |
| 181 | ClipFormat clipFormat; |
| 182 | for (int i = 0; i < j + 5; ++i) |
| 183 | clipFormat.sourceClipIndices.push_back(i + j); |
| 184 | expectedClipFormat.push_back(clipFormat); |
| 185 | |
| 186 | blendNode->setClipFormat(animatorId, clipFormat); |
| 187 | } |
| 188 | |
| 189 | QVector<int> indexes(animatorCount); |
| 190 | std::iota(indexes.begin(), indexes.end(), 0); |
| 191 | |
| 192 | QTest::newRow("multiple entries, ordered") |
| 193 | << blendNode << indexes << animatorIds << expectedClipFormat; |
| 194 | } |
| 195 | |
| 196 | // Multiple entries, unordered |
| 197 | { |
| 198 | auto blendNode = new ClipBlendValue; |
| 199 | QList<Qt3DCore::QNodeId> animatorIds; |
| 200 | QList<ClipFormat> expectedClipFormat; |
| 201 | |
| 202 | const int animatorCount = 10; |
| 203 | for (int j = 0; j < animatorCount; ++j) { |
| 204 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 205 | animatorIds.push_back(animatorId); |
| 206 | |
| 207 | ClipFormat clipFormat; |
| 208 | for (int i = 0; i < j + 5; ++i) |
| 209 | clipFormat.sourceClipIndices.push_back(i + j); |
| 210 | expectedClipFormat.push_back(clipFormat); |
| 211 | |
| 212 | blendNode->setClipFormat(animatorId, clipFormat); |
| 213 | } |
| 214 | |
| 215 | // Shuffle the animatorIds to randomise the lookups |
| 216 | QVector<int> indexes(animatorCount); |
| 217 | std::iota(indexes.begin(), indexes.end(), 0); |
| 218 | std::random_device rd; |
| 219 | std::mt19937 generator(rd()); |
| 220 | std::shuffle(indexes.begin(), indexes.end(), generator); |
| 221 | |
| 222 | QTest::newRow("multiple entries, unordered") |
| 223 | << blendNode << indexes << animatorIds << expectedClipFormat; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void checkFormatIndices() |
| 228 | { |
| 229 | // GIVEN |
| 230 | QFETCH(ClipBlendValue *, blendNode); |
| 231 | QFETCH(QList<int>, indexes); |
| 232 | QFETCH(QList<Qt3DCore::QNodeId>, animatorIds); |
| 233 | QFETCH(QList<ClipFormat>, expectedClipFormat); |
| 234 | |
| 235 | for (int i = 0; i < indexes.size(); ++i) { |
| 236 | // WHEN |
| 237 | const int index = indexes[i]; |
| 238 | const ClipFormat actualClipFormat = blendNode->clipFormat(animatorIds[index]); |
| 239 | |
| 240 | // THEN |
| 241 | QCOMPARE(actualClipFormat.sourceClipIndices.size(), expectedClipFormat[index].sourceClipIndices.size()); |
| 242 | for (int j = 0; j < actualClipFormat.sourceClipIndices.size(); ++j) |
| 243 | QCOMPARE(actualClipFormat.sourceClipIndices[j], expectedClipFormat[index].sourceClipIndices[j]); |
| 244 | } |
| 245 | |
| 246 | delete blendNode; |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | QTEST_MAIN(tst_ClipBlendValue) |
| 251 | |
| 252 | #include "tst_clipblendvalue.moc" |