| 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 <Qt3DAnimation/qanimationcliploader.h> |
| 6 |
#include <Qt3DAnimation/qclipanimator.h> |
| 7 |
#include <Qt3DAnimation/qchannelmapper.h> |
| 8 |
#include <Qt3DAnimation/qchannelmapping.h> |
| 9 |
#include <Qt3DAnimation/private/clipanimator_p.h> |
| 10 |
#include <Qt3DAnimation/private/channelmapper_p.h> |
| 11 |
#include <Qt3DAnimation/private/channelmapping_p.h> |
| 12 |
#include <Qt3DAnimation/private/findrunningclipanimatorsjob_p.h> |
| 13 |
#include <Qt3DAnimation/private/handler_p.h> |
| 14 |
#include <Qt3DAnimation/private/managers_p.h> |
| 15 |
#include <Qt3DCore/private/qnode_p.h> |
| 16 |
#include <Qt3DCore/private/qscene_p.h> |
| 17 |
#include <Qt3DCore/private/qbackendnode_p.h> |
| 18 |
#include <qbackendnodetester.h> |
| 19 |
#include <testarbiter.h> |
| 20 |
|
| 21 |
using namespace Qt3DAnimation::Animation; |
| 22 |
|
| 23 |
Q_DECLARE_METATYPE(Qt3DAnimation::Animation::Handler*) |
| 24 |
Q_DECLARE_METATYPE(QVector<Qt3DAnimation::Animation::HClipAnimator>) |
| 25 |
|
| 26 |
using MappingDataResults = QHash<ClipAnimator *, QVector<Qt3DAnimation::Animation::MappingData>>; |
| 27 |
Q_DECLARE_METATYPE(MappingDataResults) |
| 28 |
|
| 29 |
class tst_FindRunningClipAnimatorsJob: public Qt3DCore::QBackendNodeTester |
| 30 |
{ |
| 31 |
Q_OBJECT |
| 32 |
public: |
| 33 |
ChannelMapping *createChannelMapping(Handler *handler, |
| 34 |
const QString &channelName, |
| 35 |
const Qt3DCore::QNodeId targetId, |
| 36 |
const char *propertyName, |
| 37 |
int type, |
| 38 |
int componentCount) |
| 39 |
{ |
| 40 |
auto channelMappingId = Qt3DCore::QNodeId::createId(); |
| 41 |
ChannelMapping *channelMapping = handler->channelMappingManager()->getOrCreateResource(channelMappingId); |
| 42 |
setPeerId(channelMapping, channelMappingId); |
| 43 |
channelMapping->setHandler(handler); |
| 44 |
channelMapping->setTargetId(targetId); |
| 45 |
channelMapping->setPropertyName(propertyName); |
| 46 |
channelMapping->setChannelName(channelName); |
| 47 |
channelMapping->setType(type); |
| 48 |
channelMapping->setComponentCount(componentCount); |
| 49 |
channelMapping->setMappingType(ChannelMapping::ChannelMappingType); |
| 50 |
return channelMapping; |
| 51 |
} |
| 52 |
|
| 53 |
ChannelMapper *createChannelMapper(Handler *handler, |
| 54 |
const QList<Qt3DCore::QNodeId> &mappingIds) |
| 55 |
{ |
| 56 |
auto channelMapperId = Qt3DCore::QNodeId::createId(); |
| 57 |
ChannelMapper *channelMapper = handler->channelMapperManager()->getOrCreateResource(channelMapperId); |
| 58 |
setPeerId(channelMapper, channelMapperId); |
| 59 |
channelMapper->setHandler(handler); |
| 60 |
channelMapper->setMappingIds(mappingIds); |
| 61 |
return channelMapper; |
| 62 |
} |
| 63 |
|
| 64 |
AnimationClip *createAnimationClipLoader(Handler *handler, |
| 65 |
const QUrl &source) |
| 66 |
{ |
| 67 |
auto clipId = Qt3DCore::QNodeId::createId(); |
| 68 |
AnimationClip *clip = handler->animationClipLoaderManager()->getOrCreateResource(clipId); |
| 69 |
setPeerId(clip, clipId); |
| 70 |
clip->setHandler(handler); |
| 71 |
clip->setDataType(AnimationClip::File); |
| 72 |
clip->setSource(source); |
| 73 |
clip->loadAnimation(); |
| 74 |
return clip; |
| 75 |
} |
| 76 |
|
| 77 |
ClipAnimator *createClipAnimator(Handler *handler, |
| 78 |
qint64 globalStartTimeNS, |
| 79 |
int loops) |
| 80 |
{ |
| 81 |
auto animatorId = Qt3DCore::QNodeId::createId(); |
| 82 |
ClipAnimator *animator = handler->clipAnimatorManager()->getOrCreateResource(animatorId); |
| 83 |
setPeerId(animator, animatorId); |
| 84 |
animator->setHandler(handler); |
| 85 |
animator->setStartTime(globalStartTimeNS); |
| 86 |
animator->setLoops(loops); |
| 87 |
return animator; |
| 88 |
} |
| 89 |
|
| 90 |
private Q_SLOTS: |
| 91 |
void checkJob_data() |
| 92 |
{ |
| 93 |
QTest::addColumn<Handler *>("handler"); |
| 94 |
QTest::addColumn<QVector<HClipAnimator>>("dirtyClipAnimators"); |
| 95 |
QTest::addColumn<MappingDataResults>("expectedResults"); |
| 96 |
|
| 97 |
|
| 98 |
{ |
| 99 |
Handler *handler; |
| 100 |
AnimationClip *clip; |
| 101 |
ClipAnimator *animator; |
| 102 |
ChannelMapper *channelMapper; |
| 103 |
MappingDataResults expectedResults; |
| 104 |
handler = new Handler(); |
| 105 |
clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json")); |
| 106 |
|
| 107 |
const qint64 globalStartTimeNS = 0; |
| 108 |
const int loops = 1; |
| 109 |
animator = createClipAnimator(handler, globalStartTimeNS, loops); |
| 110 |
animator->setClipId(clip->peerId()); |
| 111 |
const QVector<HClipAnimator> dirtyClipAnimators |
| 112 |
= { handler->clipAnimatorManager()->getOrAcquireHandle(animator->peerId()) }; |
| 113 |
|
| 114 |
auto channelMapping = createChannelMapping(handler, |
| 115 |
QLatin1String("Location"), |
| 116 |
Qt3DCore::QNodeId::createId(), |
| 117 |
"translation", |
| 118 |
static_cast<int>(QMetaType::QVector3D), |
| 119 |
3); |
| 120 |
channelMapper = createChannelMapper(handler, QList<Qt3DCore::QNodeId> { channelMapping->peerId() }); |
| 121 |
animator->setMapperId(channelMapper->peerId()); |
| 122 |
animator->setRunning(true); // Has to be marked as running for the job to process it |
| 123 |
animator->setEnabled(true); // Has to be marked as enabled for the job to process it |
| 124 |
|
| 125 |
const ComponentIndices locationIndices = { 0, 1, 2 }; |
| 126 |
MappingData expectedMapping; |
| 127 |
expectedMapping.targetId = channelMapping->targetId(); |
| 128 |
expectedMapping.propertyName = channelMapping->propertyName(); |
| 129 |
expectedMapping.type = channelMapping->type(); |
| 130 |
expectedMapping.channelIndices = locationIndices; |
| 131 |
expectedResults.insert(animator, QVector<MappingData> { expectedMapping }); |
| 132 |
|
| 133 |
QTest::newRow("single mapping") |
| 134 |
<< handler |
| 135 |
<< dirtyClipAnimators |
| 136 |
<< expectedResults; |
| 137 |
} |
| 138 |
|
| 139 |
{ |
| 140 |
Handler *handler; |
| 141 |
AnimationClip *clip; |
| 142 |
ClipAnimator *animator; |
| 143 |
ChannelMapper *channelMapper; |
| 144 |
MappingDataResults expectedResults; |
| 145 |
handler = new Handler(); |
| 146 |
clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json")); |
| 147 |
|
| 148 |
const qint64 globalStartTimeNS = 0; |
| 149 |
const int loops = 1; |
| 150 |
animator = createClipAnimator(handler, globalStartTimeNS, loops); |
| 151 |
animator->setClipId(clip->peerId()); |
| 152 |
const QVector<HClipAnimator> dirtyClipAnimators |
| 153 |
= { handler->clipAnimatorManager()->getOrAcquireHandle(animator->peerId()) }; |
| 154 |
|
| 155 |
auto channelMapping = createChannelMapping(handler, |
| 156 |
QLatin1String("Location"), |
| 157 |
Qt3DCore::QNodeId::createId(), |
| 158 |
"translation", |
| 159 |
static_cast<int>(QMetaType::QVector3D), |
| 160 |
3); |
| 161 |
channelMapper = createChannelMapper(handler, QList<Qt3DCore::QNodeId> { channelMapping->peerId() }); |
| 162 |
animator->setMapperId(channelMapper->peerId()); |
| 163 |
animator->setRunning(true); // Has to be marked as running for the job to process it |
| 164 |
animator->setEnabled(false); // Has to be marked as enabled for the job to process it |
| 165 |
|
| 166 |
QTest::newRow("disabled animator") |
| 167 |
<< handler |
| 168 |
<< dirtyClipAnimators |
| 169 |
<< expectedResults; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void checkJob() |
| 174 |
{ |
| 175 |
// GIVEN |
| 176 |
QFETCH(Handler *, handler); |
| 177 |
QFETCH(QVector<HClipAnimator>, dirtyClipAnimators); |
| 178 |
QFETCH(MappingDataResults, expectedResults); |
| 179 |
FindRunningClipAnimatorsJob job; |
| 180 |
|
| 181 |
// WHEN |
| 182 |
job.setHandler(handler); |
| 183 |
job.setDirtyClipAnimators(dirtyClipAnimators); |
| 184 |
job.run(); |
| 185 |
|
| 186 |
// THEN - check the resulting MappingData on the animator matches the expected results |
| 187 |
for (const auto &dirtyClipAnimator : dirtyClipAnimators) { |
| 188 |
const auto animator = handler->clipAnimatorManager()->data(dirtyClipAnimator); |
| 189 |
const QVector<MappingData> actualMappingData = animator->mappingData(); |
| 190 |
const QVector<MappingData> expectedMappingData = expectedResults[animator]; |
| 191 |
|
| 192 |
QCOMPARE(expectedMappingData.size(), actualMappingData.size()); |
| 193 |
for (int i = 0; i < actualMappingData.size(); ++i) { |
| 194 |
QCOMPARE(expectedMappingData[i].targetId, actualMappingData[i].targetId); |
| 195 |
QCOMPARE(expectedMappingData[i].type, actualMappingData[i].type); |
| 196 |
QVERIFY(qstrcmp(expectedMappingData[i].propertyName, actualMappingData[i].propertyName) == 0); |
| 197 |
QCOMPARE(expectedMappingData[i].channelIndices.size(), actualMappingData[i].channelIndices.size()); |
| 198 |
|
| 199 |
for (int j = 0; j < actualMappingData[i].channelIndices.size(); ++j) { |
| 200 |
QCOMPARE(expectedMappingData[i].channelIndices[j], actualMappingData[i].channelIndices[j]); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
QTEST_APPLESS_MAIN(tst_FindRunningClipAnimatorsJob) |
| 208 |
|
| 209 |
#include "tst_findrunningclipanimatorsjob.moc" |