summaryrefslogtreecommitdiffstats
path: root/tests/auto/animation/findrunningclipanimatorsjob/tst_findrunningclipanimatorsjob.cpp
blob: 29cc7b54c8f8876002a36304a96f97033a4c8fca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtTest/QTest>
#include <Qt3DAnimation/qanimationcliploader.h>
#include <Qt3DAnimation/qclipanimator.h>
#include <Qt3DAnimation/qchannelmapper.h>
#include <Qt3DAnimation/qchannelmapping.h>
#include <Qt3DAnimation/private/clipanimator_p.h>
#include <Qt3DAnimation/private/channelmapper_p.h>
#include <Qt3DAnimation/private/channelmapping_p.h>
#include <Qt3DAnimation/private/findrunningclipanimatorsjob_p.h>
#include <Qt3DAnimation/private/handler_p.h>
#include <Qt3DAnimation/private/managers_p.h>
#include <Qt3DCore/private/qnode_p.h>
#include <Qt3DCore/private/qscene_p.h>
#include <Qt3DCore/private/qbackendnode_p.h>
#include <qbackendnodetester.h>
#include <testarbiter.h>

using namespace Qt3DAnimation::Animation;

Q_DECLARE_METATYPE(Qt3DAnimation::Animation::Handler*)
Q_DECLARE_METATYPE(QVector<Qt3DAnimation::Animation::HClipAnimator>)

using MappingDataResults = QHash<ClipAnimator *, QVector<Qt3DAnimation::Animation::MappingData>>;
Q_DECLARE_METATYPE(MappingDataResults)

class tst_FindRunningClipAnimatorsJob: public Qt3DCore::QBackendNodeTester
{
    Q_OBJECT
public:
    ChannelMapping *createChannelMapping(Handler *handler,
                                         const QString &channelName,
                                         const Qt3DCore::QNodeId targetId,
                                         const char *propertyName,
                                         int type,
                                         int componentCount)
    {
        auto channelMappingId = Qt3DCore::QNodeId::createId();
        ChannelMapping *channelMapping = handler->channelMappingManager()->getOrCreateResource(channelMappingId);
        setPeerId(channelMapping, channelMappingId);
        channelMapping->setHandler(handler);
        channelMapping->setTargetId(targetId);
        channelMapping->setPropertyName(propertyName);
        channelMapping->setChannelName(channelName);
        channelMapping->setType(type);
        channelMapping->setComponentCount(componentCount);
        channelMapping->setMappingType(ChannelMapping::ChannelMappingType);
        return channelMapping;
    }

    ChannelMapper *createChannelMapper(Handler *handler,
                                       const QList<Qt3DCore::QNodeId> &mappingIds)
    {
        auto channelMapperId = Qt3DCore::QNodeId::createId();
        ChannelMapper *channelMapper = handler->channelMapperManager()->getOrCreateResource(channelMapperId);
        setPeerId(channelMapper, channelMapperId);
        channelMapper->setHandler(handler);
        channelMapper->setMappingIds(mappingIds);
        return channelMapper;
    }

    AnimationClip *createAnimationClipLoader(Handler *handler,
                                             const QUrl &source)
    {
        auto clipId = Qt3DCore::QNodeId::createId();
        AnimationClip *clip = handler->animationClipLoaderManager()->getOrCreateResource(clipId);
        setPeerId(clip, clipId);
        clip->setHandler(handler);
        clip->setDataType(AnimationClip::File);
        clip->setSource(source);
        clip->loadAnimation();
        return clip;
    }

    ClipAnimator *createClipAnimator(Handler *handler,
                                     qint64 globalStartTimeNS,
                                     int loops)
    {
        auto animatorId = Qt3DCore::QNodeId::createId();
        ClipAnimator *animator = handler->clipAnimatorManager()->getOrCreateResource(animatorId);
        setPeerId(animator, animatorId);
        animator->setHandler(handler);
        animator->setStartTime(globalStartTimeNS);
        animator->setLoops(loops);
        return animator;
    }

private Q_SLOTS:
    void checkJob_data()
    {
        QTest::addColumn<Handler *>("handler");
        QTest::addColumn<QVector<HClipAnimator>>("dirtyClipAnimators");
        QTest::addColumn<MappingDataResults>("expectedResults");


        {
            Handler *handler;
            AnimationClip *clip;
            ClipAnimator *animator;
            ChannelMapper *channelMapper;
            MappingDataResults expectedResults;
            handler = new Handler();
            clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));

            const qint64 globalStartTimeNS = 0;
            const int loops = 1;
            animator = createClipAnimator(handler, globalStartTimeNS, loops);
            animator->setClipId(clip->peerId());
            const QVector<HClipAnimator> dirtyClipAnimators
                    = { handler->clipAnimatorManager()->getOrAcquireHandle(animator->peerId()) };

            auto channelMapping = createChannelMapping(handler,
                                                       QLatin1String("Location"),
                                                       Qt3DCore::QNodeId::createId(),
                                                       "translation",
                                                       static_cast<int>(QMetaType::QVector3D),
                                                       3);
            channelMapper = createChannelMapper(handler, QList<Qt3DCore::QNodeId> { channelMapping->peerId() });
            animator->setMapperId(channelMapper->peerId());
            animator->setRunning(true); // Has to be marked as running for the job to process it
            animator->setEnabled(true); // Has to be marked as enabled for the job to process it

            const ComponentIndices locationIndices = { 0, 1, 2 };
            MappingData expectedMapping;
            expectedMapping.targetId = channelMapping->targetId();
            expectedMapping.propertyName = channelMapping->propertyName();
            expectedMapping.type = channelMapping->type();
            expectedMapping.channelIndices = locationIndices;
            expectedResults.insert(animator, QVector<MappingData> { expectedMapping });

            QTest::newRow("single mapping")
                            << handler
                            << dirtyClipAnimators
                            << expectedResults;
        }

        {
            Handler *handler;
            AnimationClip *clip;
            ClipAnimator *animator;
            ChannelMapper *channelMapper;
            MappingDataResults expectedResults;
            handler = new Handler();
            clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json"));

            const qint64 globalStartTimeNS = 0;
            const int loops = 1;
            animator = createClipAnimator(handler, globalStartTimeNS, loops);
            animator->setClipId(clip->peerId());
            const QVector<HClipAnimator> dirtyClipAnimators
                    = { handler->clipAnimatorManager()->getOrAcquireHandle(animator->peerId()) };

            auto channelMapping = createChannelMapping(handler,
                                                       QLatin1String("Location"),
                                                       Qt3DCore::QNodeId::createId(),
                                                       "translation",
                                                       static_cast<int>(QMetaType::QVector3D),
                                                       3);
            channelMapper = createChannelMapper(handler, QList<Qt3DCore::QNodeId> { channelMapping->peerId() });
            animator->setMapperId(channelMapper->peerId());
            animator->setRunning(true); // Has to be marked as running for the job to process it
            animator->setEnabled(false); // Has to be marked as enabled for the job to process it

            QTest::newRow("disabled animator")
                    << handler
                    << dirtyClipAnimators
                    << expectedResults;
        }
    }

    void checkJob()
    {
        // GIVEN
        QFETCH(Handler *, handler);
        QFETCH(QVector<HClipAnimator>, dirtyClipAnimators);
        QFETCH(MappingDataResults, expectedResults);
        FindRunningClipAnimatorsJob job;

        // WHEN
        job.setHandler(handler);
        job.setDirtyClipAnimators(dirtyClipAnimators);
        job.run();

        // THEN - check the resulting MappingData on the animator matches the expected results
        for (const auto &dirtyClipAnimator : dirtyClipAnimators) {
            const auto animator = handler->clipAnimatorManager()->data(dirtyClipAnimator);
            const QVector<MappingData> actualMappingData = animator->mappingData();
            const QVector<MappingData> expectedMappingData = expectedResults[animator];

            QCOMPARE(expectedMappingData.size(), actualMappingData.size());
            for (int i = 0; i < actualMappingData.size(); ++i) {
                QCOMPARE(expectedMappingData[i].targetId, actualMappingData[i].targetId);
                QCOMPARE(expectedMappingData[i].type, actualMappingData[i].type);
                QVERIFY(qstrcmp(expectedMappingData[i].propertyName, actualMappingData[i].propertyName) == 0);
                QCOMPARE(expectedMappingData[i].channelIndices.size(), actualMappingData[i].channelIndices.size());

                for (int j = 0; j < actualMappingData[i].channelIndices.size(); ++j) {
                    QCOMPARE(expectedMappingData[i].channelIndices[j], actualMappingData[i].channelIndices[j]);
                }
            }
        }
    }
};

QTEST_APPLESS_MAIN(tst_FindRunningClipAnimatorsJob)

#include "tst_findrunningclipanimatorsjob.moc"