summaryrefslogtreecommitdiffstats
path: root/tests/auto/animation/qmorphinganimation/tst_qmorphinganimation.cpp
blob: db39fdb2d87c6460170534e8a76324a47c141aaa (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtTest/qtest.h>
#include <Qt3DAnimation/qmorphinganimation.h>
#include <qobject.h>
#include <qsignalspy.h>

class tst_QMorphingAnimation : public QObject
{
    Q_OBJECT

    bool verifyAttribute(Qt3DCore::QGeometry *geometry, QString name,
                         Qt3DCore::QAttribute *attribute)
    {
        const QList<Qt3DCore::QAttribute *> attributes = geometry->attributes();
        for (const Qt3DCore::QAttribute *attr : attributes) {
            if (attr->name() == name) {
                if (attr == attribute)
                    return true;
                return false;
            }
        }
        return false;
    }

private Q_SLOTS:

    void initTestCase()
    {
        qRegisterMetaType<Qt3DAnimation::QMorphingAnimation::Method>("QMorphingAnimation::Method");
    }

    void checkDefaultConstruction()
    {
        // GIVEN
        Qt3DAnimation::QMorphingAnimation morphingAnimation;

        // THEN
        QCOMPARE(morphingAnimation.interpolator(), 0.0f);
        QCOMPARE(morphingAnimation.target(), nullptr);
        QCOMPARE(morphingAnimation.targetName(), QString());
        QCOMPARE(morphingAnimation.method(), Qt3DAnimation::QMorphingAnimation::Relative);
        QCOMPARE(morphingAnimation.easing(), QEasingCurve());
    }

    void checkPropertyChanges()
    {
        // GIVEN
        Qt3DAnimation::QMorphingAnimation morphingAnimation;

        {
            // WHEN
            QScopedPointer<Qt3DRender::QGeometryRenderer> gr(new Qt3DRender::QGeometryRenderer);
            QSignalSpy spy(&morphingAnimation,
                           SIGNAL(targetChanged(Qt3DRender::QGeometryRenderer *)));
            Qt3DRender::QGeometryRenderer *newValue = gr.data();
            morphingAnimation.setTarget(newValue);

            // THEN
            QCOMPARE(morphingAnimation.target(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            morphingAnimation.setTarget(newValue);

            // THEN
            QCOMPARE(morphingAnimation.target(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&morphingAnimation, SIGNAL(targetNameChanged(QString)));
            const QString newValue = QString("target");
            morphingAnimation.setTargetName(newValue);

            // THEN
            QCOMPARE(morphingAnimation.targetName(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            morphingAnimation.setTargetName(newValue);

            // THEN
            QCOMPARE(morphingAnimation.targetName(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&morphingAnimation, SIGNAL(methodChanged(QMorphingAnimation::Method)));
            const Qt3DAnimation::QMorphingAnimation::Method newValue
                    = Qt3DAnimation::QMorphingAnimation::Normalized;
            morphingAnimation.setMethod(newValue);

            // THEN
            QCOMPARE(morphingAnimation.method(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            morphingAnimation.setMethod(newValue);

            // THEN
            QCOMPARE(morphingAnimation.method(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&morphingAnimation, SIGNAL(easingChanged(QEasingCurve)));
            const QEasingCurve newValue = QEasingCurve(QEasingCurve::InBounce);
            morphingAnimation.setEasing(newValue);

            // THEN
            QCOMPARE(morphingAnimation.easing(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            morphingAnimation.setEasing(newValue);

            // THEN
            QCOMPARE(morphingAnimation.easing(), newValue);
            QCOMPARE(spy.size(), 0);

        }
    }

    void testAnimation()
    {
        // GIVEN
        const QString baseName("position");
        const QString targetName("positionTarget");
        Qt3DAnimation::QMorphingAnimation morphingAnimation;
        Qt3DCore::QAttribute *base = new Qt3DCore::QAttribute;

        Qt3DCore::QGeometry *geometry = new Qt3DCore::QGeometry;
        Qt3DAnimation::QMorphTarget *mt1 = new Qt3DAnimation::QMorphTarget(&morphingAnimation);
        Qt3DAnimation::QMorphTarget *mt2 = new Qt3DAnimation::QMorphTarget(&morphingAnimation);
        Qt3DAnimation::QMorphTarget *mt3 = new Qt3DAnimation::QMorphTarget(&morphingAnimation);
        Qt3DCore::QAttribute *a1 = new Qt3DCore::QAttribute(geometry);
        Qt3DCore::QAttribute *a2 = new Qt3DCore::QAttribute(geometry);
        Qt3DCore::QAttribute *a3 = new Qt3DCore::QAttribute(geometry);
        Qt3DRender::QGeometryRenderer gr;

        base->setName(baseName);
        geometry->addAttribute(base);
        gr.setGeometry(geometry);
        morphingAnimation.setTarget(&gr);
        a1->setName(baseName);
        a2->setName(baseName);
        a3->setName(baseName);
        mt1->addAttribute(a1);
        mt2->addAttribute(a2);
        mt3->addAttribute(a3);
        morphingAnimation.addMorphTarget(mt1);
        morphingAnimation.addMorphTarget(mt2);
        morphingAnimation.addMorphTarget(mt3);

        const QVector<float> positions = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f };
        morphingAnimation.setTargetPositions(positions);

        morphingAnimation.setWeights(0, QVector<float> { 1.0f, 0.0f, 0.0f });
        morphingAnimation.setWeights(1, QVector<float> { 0.0f, 0.0f, 0.0f });
        morphingAnimation.setWeights(2, QVector<float> { 0.0f, 1.0f, 0.0f });
        morphingAnimation.setWeights(3, QVector<float> { 0.0f, 0.0f, 0.0f });
        morphingAnimation.setWeights(4, QVector<float> { 0.0f, 0.0f, 1.0f });

        morphingAnimation.setMethod(Qt3DAnimation::QMorphingAnimation::Relative);
        {
            // WHEN
            morphingAnimation.setPosition(0.0f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -1.0f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a1));

            // WHEN
            morphingAnimation.setPosition(0.5f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -0.5f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a1));

            // WHEN
            morphingAnimation.setPosition(1.0f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -0.0f));
            QVERIFY(verifyAttribute(geometry, baseName, base));

            // WHEN
            morphingAnimation.setPosition(1.5f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -0.5f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a2));

            // WHEN
            morphingAnimation.setPosition(4.0f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), -1.0f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a3));
        }

        morphingAnimation.setMethod(Qt3DAnimation::QMorphingAnimation::Normalized);
        {
            // WHEN
            morphingAnimation.setPosition(0.0f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 1.0f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a1));

            // WHEN
            morphingAnimation.setPosition(0.5f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 0.5f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a1));

            // WHEN
            morphingAnimation.setPosition(1.0f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 0.0f));
            QVERIFY(verifyAttribute(geometry, baseName, base));

            // WHEN
            morphingAnimation.setPosition(1.5f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 0.5f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a2));

            // WHEN
            morphingAnimation.setPosition(4.0f);

            // THEN
            QVERIFY(qFuzzyCompare(morphingAnimation.interpolator(), 1.0f));
            QVERIFY(verifyAttribute(geometry, baseName, base));
            QVERIFY(verifyAttribute(geometry, targetName, a3));
        }
    }
};

QTEST_APPLESS_MAIN(tst_QMorphingAnimation)

#include "tst_qmorphinganimation.moc"