| 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/qlerpclipblend.h> |
| 7 | #include <Qt3DAnimation/qanimationcliploader.h> |
| 8 | #include <Qt3DAnimation/private/qabstractclipblendnode_p.h> |
| 9 | #include <Qt3DAnimation/private/clipblendnode_p.h> |
| 10 | #include <Qt3DAnimation/private/managers_p.h> |
| 11 | #include "qbackendnodetester.h" |
| 12 | |
| 13 | #include <random> |
| 14 | #include <algorithm> |
| 15 | |
| 16 | using namespace Qt3DAnimation::Animation; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class TestClipBlendNode : public ClipBlendNode |
| 21 | { |
| 22 | public: |
| 23 | TestClipBlendNode(const ClipResults &clipResults = ClipResults()) |
| 24 | : ClipBlendNode(ClipBlendNode::LerpBlendType) |
| 25 | , m_clipResults(clipResults) |
| 26 | {} |
| 27 | |
| 28 | inline QList<Qt3DCore::QNodeId> allDependencyIds() const override |
| 29 | { |
| 30 | return currentDependencyIds(); |
| 31 | } |
| 32 | |
| 33 | QList<Qt3DCore::QNodeId> currentDependencyIds() const final |
| 34 | { |
| 35 | return {}; |
| 36 | } |
| 37 | |
| 38 | using ClipBlendNode::setClipResults; |
| 39 | |
| 40 | double duration() const final { return 0.0f; } |
| 41 | |
| 42 | protected: |
| 43 | ClipResults doBlend(const QList<ClipResults> &) const final |
| 44 | { |
| 45 | return m_clipResults; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | ClipResults m_clipResults; |
| 50 | }; |
| 51 | |
| 52 | } // anonymous |
| 53 | |
| 54 | Q_DECLARE_METATYPE(TestClipBlendNode *) |
| 55 | |
| 56 | class tst_ClipBlendNode : public Qt3DCore::QBackendNodeTester |
| 57 | { |
| 58 | Q_OBJECT |
| 59 | public: |
| 60 | TestClipBlendNode *createTestClipBlendNode(Handler *handler, const ClipResults &clipResults) |
| 61 | { |
| 62 | auto id = Qt3DCore::QNodeId::createId(); |
| 63 | TestClipBlendNode *node = new TestClipBlendNode(clipResults); |
| 64 | setPeerId(node, id); |
| 65 | handler->clipBlendNodeManager()->appendNode(id, node); |
| 66 | return node; |
| 67 | } |
| 68 | |
| 69 | BlendedClipAnimator *createBlendedClipAnimator(Handler *handler, |
| 70 | qint64 globalStartTimeNS, |
| 71 | int loops) |
| 72 | { |
| 73 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 74 | BlendedClipAnimator *animator = handler->blendedClipAnimatorManager()->getOrCreateResource(animatorId); |
| 75 | setPeerId(animator, animatorId); |
| 76 | animator->setStartTime(globalStartTimeNS); |
| 77 | animator->setLoops(loops); |
| 78 | return animator; |
| 79 | } |
| 80 | |
| 81 | private Q_SLOTS: |
| 82 | |
| 83 | void checkInitialState() |
| 84 | { |
| 85 | // GIVEN |
| 86 | TestClipBlendNode backendClipBlendNode; |
| 87 | |
| 88 | // THEN |
| 89 | QCOMPARE(backendClipBlendNode.isEnabled(), false); |
| 90 | QVERIFY(backendClipBlendNode.peerId().isNull()); |
| 91 | QVERIFY(backendClipBlendNode.clipBlendNodeManager() == nullptr); |
| 92 | QCOMPARE(backendClipBlendNode.blendType(), ClipBlendNode::LerpBlendType); |
| 93 | QCOMPARE(backendClipBlendNode.clipResults(Qt3DCore::QNodeId()), ClipResults()); |
| 94 | } |
| 95 | |
| 96 | void checkInitializeFromPeer() |
| 97 | { |
| 98 | // GIVEN |
| 99 | Qt3DAnimation::QLerpClipBlend clipBlendNode; |
| 100 | Qt3DAnimation::QAnimationClipLoader clip; |
| 101 | |
| 102 | QCoreApplication::processEvents(); |
| 103 | |
| 104 | { |
| 105 | // WHEN |
| 106 | ClipBlendNodeManager manager; |
| 107 | TestClipBlendNode backendClipBlendNode; |
| 108 | backendClipBlendNode.setClipBlendNodeManager(&manager); |
| 109 | simulateInitializationSync(&clipBlendNode, &backendClipBlendNode); |
| 110 | |
| 111 | // THEN |
| 112 | QCOMPARE(backendClipBlendNode.isEnabled(), true); |
| 113 | QCOMPARE(backendClipBlendNode.peerId(), clipBlendNode.id()); |
| 114 | QCOMPARE(backendClipBlendNode.clipBlendNodeManager(), &manager); |
| 115 | QCOMPARE(backendClipBlendNode.blendType(), ClipBlendNode::LerpBlendType); |
| 116 | QCOMPARE(backendClipBlendNode.clipResults(Qt3DCore::QNodeId()), ClipResults()); |
| 117 | } |
| 118 | { |
| 119 | // WHEN |
| 120 | ClipBlendNodeManager manager; |
| 121 | TestClipBlendNode backendClipBlendNode; |
| 122 | clipBlendNode.setEnabled(false); |
| 123 | backendClipBlendNode.setClipBlendNodeManager(&manager); |
| 124 | simulateInitializationSync(&clipBlendNode, &backendClipBlendNode); |
| 125 | |
| 126 | // THEN |
| 127 | QCOMPARE(backendClipBlendNode.peerId(), clipBlendNode.id()); |
| 128 | QCOMPARE(backendClipBlendNode.isEnabled(), false); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void checkClipResults_data() |
| 133 | { |
| 134 | QTest::addColumn<TestClipBlendNode *>("blendNode"); |
| 135 | QTest::addColumn<QList<int>>("indexes"); |
| 136 | QTest::addColumn<QList<Qt3DCore::QNodeId>>("animatorIds"); |
| 137 | QTest::addColumn<QList<ClipResults>>("expectedClipResults"); |
| 138 | |
| 139 | // Single entry |
| 140 | { |
| 141 | auto blendNode = new TestClipBlendNode; |
| 142 | QList<Qt3DCore::QNodeId> animatorIds; |
| 143 | QList<ClipResults> expectedClipResults; |
| 144 | |
| 145 | const auto animatorId = Qt3DCore::QNodeId::createId(); |
| 146 | animatorIds.push_back(animatorId); |
| 147 | |
| 148 | ClipResults clipResults = { 0.0f, 1.0f, 2.0f }; |
| 149 | for (int i = 0; i < 3; ++i) |
| 150 | clipResults.push_back(float(i)); |
| 151 | expectedClipResults.push_back(clipResults); |
| 152 | |
| 153 | // Set data and indexes |
| 154 | blendNode->setClipResults(animatorId, clipResults); |
| 155 | const QList<int> indexes = { 0 }; |
| 156 | |
| 157 | QTest::newRow("single entry") |
| 158 | << blendNode << indexes << animatorIds << expectedClipResults; |
| 159 | } |
| 160 | |
| 161 | // No data |
| 162 | { |
| 163 | auto blendNode = new TestClipBlendNode; |
| 164 | QList<Qt3DCore::QNodeId> animatorIds; |
| 165 | QList<ClipResults> expectedClipResults; |
| 166 | |
| 167 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 168 | animatorIds.push_back(animatorId); |
| 169 | |
| 170 | ClipResults clipResults; |
| 171 | expectedClipResults.push_back(clipResults); |
| 172 | |
| 173 | // Don't set any data |
| 174 | const QList<int> indexes = { 0 }; |
| 175 | |
| 176 | QTest::newRow("no entries") |
| 177 | << blendNode << indexes << animatorIds << expectedClipResults; |
| 178 | } |
| 179 | |
| 180 | // Multiple entries, ordered |
| 181 | { |
| 182 | auto blendNode = new TestClipBlendNode; |
| 183 | QList<Qt3DCore::QNodeId> animatorIds; |
| 184 | QList<ClipResults> expectedClipResults; |
| 185 | |
| 186 | const int animatorCount = 10; |
| 187 | for (int j = 0; j < animatorCount; ++j) { |
| 188 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 189 | animatorIds.push_back(animatorId); |
| 190 | |
| 191 | ClipResults clipResults; |
| 192 | for (int i = 0; i < j + 5; ++i) |
| 193 | clipResults.push_back(float(i + j)); |
| 194 | expectedClipResults.push_back(clipResults); |
| 195 | |
| 196 | blendNode->setClipResults(animatorId, clipResults); |
| 197 | } |
| 198 | |
| 199 | QVector<int> indexes(animatorCount); |
| 200 | std::iota(indexes.begin(), indexes.end(), 0); |
| 201 | |
| 202 | QTest::newRow("multiple entries, ordered") |
| 203 | << blendNode << indexes << animatorIds << expectedClipResults; |
| 204 | } |
| 205 | |
| 206 | // Multiple entries, unordered |
| 207 | { |
| 208 | auto blendNode = new TestClipBlendNode; |
| 209 | QList<Qt3DCore::QNodeId> animatorIds; |
| 210 | QList<ClipResults> expectedClipResults; |
| 211 | |
| 212 | const int animatorCount = 10; |
| 213 | for (int j = 0; j < animatorCount; ++j) { |
| 214 | auto animatorId = Qt3DCore::QNodeId::createId(); |
| 215 | animatorIds.push_back(animatorId); |
| 216 | |
| 217 | ClipResults clipResults; |
| 218 | for (int i = 0; i < j + 5; ++i) |
| 219 | clipResults.push_back(float(i + j)); |
| 220 | expectedClipResults.push_back(clipResults); |
| 221 | |
| 222 | blendNode->setClipResults(animatorId, clipResults); |
| 223 | } |
| 224 | |
| 225 | // Shuffle the animatorIds to randomise the lookups |
| 226 | QVector<int> indexes(animatorCount); |
| 227 | std::iota(indexes.begin(), indexes.end(), 0); |
| 228 | std::random_device rd; |
| 229 | std::mt19937 generator(rd()); |
| 230 | std::shuffle(indexes.begin(), indexes.end(), generator); |
| 231 | |
| 232 | QTest::newRow("multiple entries, unordered") |
| 233 | << blendNode << indexes << animatorIds << expectedClipResults; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void checkClipResults() |
| 238 | { |
| 239 | // GIVEN |
| 240 | QFETCH(TestClipBlendNode *, blendNode); |
| 241 | QFETCH(QVector<int>, indexes); |
| 242 | QFETCH(QList<Qt3DCore::QNodeId>, animatorIds); |
| 243 | QFETCH(QList<ClipResults>, expectedClipResults); |
| 244 | |
| 245 | for (int i = 0; i < indexes.size(); ++i) { |
| 246 | // WHEN |
| 247 | const int index = indexes[i]; |
| 248 | const ClipResults actualClipResults = blendNode->clipResults(animatorIds[index]); |
| 249 | |
| 250 | // THEN |
| 251 | QCOMPARE(actualClipResults.size(), expectedClipResults[index].size()); |
| 252 | for (int j = 0; j < actualClipResults.size(); ++j) |
| 253 | QCOMPARE(actualClipResults[j], expectedClipResults[index][j]); |
| 254 | } |
| 255 | |
| 256 | delete blendNode; |
| 257 | } |
| 258 | |
| 259 | void checkPerformBlend() |
| 260 | { |
| 261 | // GIVEN |
| 262 | auto handler = new Handler(); |
| 263 | ClipResults expectedResults = { 1.0f, 2.0f, 3.0f }; |
| 264 | auto blendNode = createTestClipBlendNode(handler, expectedResults); |
| 265 | const qint64 globalStartTimeNS = 0; |
| 266 | const int loopCount = 1; |
| 267 | auto animator = createBlendedClipAnimator(handler, globalStartTimeNS, loopCount); |
| 268 | |
| 269 | // WHEN |
| 270 | blendNode->blend(animator->peerId()); |
| 271 | |
| 272 | // THEN |
| 273 | const ClipResults actualResults = blendNode->clipResults(animator->peerId()); |
| 274 | QCOMPARE(actualResults.size(), expectedResults.size()); |
| 275 | for (int i = 0; i < actualResults.size(); ++i) |
| 276 | QCOMPARE(actualResults[i], expectedResults[i]); |
| 277 | } |
| 278 | }; |
| 279 | |
| 280 | QTEST_MAIN(tst_ClipBlendNode) |
| 281 | |
| 282 | #include "tst_clipblendnode.moc" |