summaryrefslogtreecommitdiffstats
path: root/tests/auto/animation/qanimationcontroller/tst_qanimationcontroller.cpp
blob: a4babc61ff8fba60a5c01f6845a703e2028c6570 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

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

class tst_QAnimationController : public QObject
{
    Q_OBJECT

private Q_SLOTS:

    void checkDefaultConstruction()
    {
        // GIVEN
        Qt3DAnimation::QAnimationController animationController;

        // THEN
        QCOMPARE(animationController.activeAnimationGroup(), 0);
        QCOMPARE(animationController.position(), 0.0f);
        QCOMPARE(animationController.positionScale(), 1.0f);
        QCOMPARE(animationController.positionOffset(), 0.0f);
        QCOMPARE(animationController.entity(), nullptr);
        QCOMPARE(animationController.recursive(), true);
    }

    void checkPropertyChanges()
    {
        // GIVEN
        Qt3DAnimation::QAnimationController animationController;

        {
            // WHEN
            QSignalSpy spy(&animationController, SIGNAL(activeAnimationGroupChanged(int)));
            const int newValue = 1;
            animationController.setActiveAnimationGroup(newValue);

            // THEN
            QCOMPARE(animationController.activeAnimationGroup(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            animationController.setActiveAnimationGroup(newValue);

            // THEN
            QCOMPARE(animationController.activeAnimationGroup(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&animationController, SIGNAL(positionChanged(float)));
            const float newValue = 2.0f;
            animationController.setPosition(newValue);

            // THEN
            QCOMPARE(animationController.position(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            animationController.setPosition(newValue);

            // THEN
            QCOMPARE(animationController.position(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&animationController, SIGNAL(positionScaleChanged(float)));
            const float newValue = 3.0f;
            animationController.setPositionScale(newValue);

            // THEN
            QCOMPARE(animationController.positionScale(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            animationController.setPositionScale(newValue);

            // THEN
            QCOMPARE(animationController.positionScale(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&animationController, SIGNAL(positionOffsetChanged(float)));
            const float newValue = -1.0f;
            animationController.setPositionOffset(newValue);

            // THEN
            QCOMPARE(animationController.positionOffset(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            animationController.setPositionOffset(newValue);

            // THEN
            QCOMPARE(animationController.positionOffset(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity);
            QSignalSpy spy(&animationController, SIGNAL(entityChanged(Qt3DCore::QEntity *)));
            Qt3DCore::QEntity * newValue = entity.data();
            animationController.setEntity(newValue);

            // THEN
            QCOMPARE(animationController.entity(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            animationController.setEntity(newValue);

            // THEN
            QCOMPARE(animationController.entity(), newValue);
            QCOMPARE(spy.size(), 0);

        }
        {
            // WHEN
            QSignalSpy spy(&animationController, SIGNAL(recursiveChanged(bool)));
            const bool newValue = false;
            animationController.setRecursive(newValue);

            // THEN
            QCOMPARE(animationController.recursive(), newValue);
            QCOMPARE(spy.size(), 1);

            // WHEN
            spy.clear();
            animationController.setRecursive(newValue);

            // THEN
            QCOMPARE(animationController.recursive(), newValue);
            QCOMPARE(spy.size(), 0);

        }
    }

    void testSetEntity()
    {
        // GIVEN
        Qt3DAnimation::QAnimationController animationController;
        Qt3DAnimation::QKeyframeAnimation *keyframeAnimation;
        Qt3DAnimation::QKeyframeAnimation *keyframeAnimation2;
        Qt3DAnimation::QMorphingAnimation *morphingAnimation;

        QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity);
        keyframeAnimation = new Qt3DAnimation::QKeyframeAnimation(entity.data());
        keyframeAnimation2 = new Qt3DAnimation::QKeyframeAnimation(entity.data());
        morphingAnimation = new Qt3DAnimation::QMorphingAnimation(entity.data());

        const QString animName1 = QString("animation1");
        const QString animName2 = QString("animation2");
        morphingAnimation->setAnimationName(animName1);
        keyframeAnimation->setAnimationName(animName1);
        keyframeAnimation2->setAnimationName(animName2);

        {
            // WHEN
            animationController.setEntity(entity.data());

            // THEN
            const QList<Qt3DAnimation::QAnimationGroup *> list = animationController.animationGroupList();
            QCOMPARE(list.size(), 2);

            QCOMPARE(list.at(0)->name(), animName1);
            QCOMPARE(list.at(1)->name(), animName2);

            QCOMPARE(list.at(0)->animationList().size(), 2);
            QCOMPARE(list.at(1)->animationList().size(), 1);
        }
    }

    void testSetEntityRecursive()
    {
        // GIVEN
        Qt3DAnimation::QAnimationController animationController;
        Qt3DAnimation::QKeyframeAnimation *keyframeAnimation;
        Qt3DAnimation::QMorphingAnimation *morphingAnimation;

        QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity);
        keyframeAnimation = new Qt3DAnimation::QKeyframeAnimation(entity.data());
        Qt3DCore::QEntity *childEntity = new Qt3DCore::QEntity(entity.data());
        morphingAnimation = new Qt3DAnimation::QMorphingAnimation(childEntity);

        const QString animName1 = QString("animation1");
        const QString animName2 = QString("animation2");

        keyframeAnimation->setAnimationName(animName1);
        morphingAnimation->setAnimationName(animName2);

        {
            // WHEN
            animationController.setEntity(entity.data());

            // THEN
            const QList<Qt3DAnimation::QAnimationGroup *> list = animationController.animationGroupList();
            QCOMPARE(list.size(), 2);

            QCOMPARE(list.at(0)->name(), animName1);
            QCOMPARE(list.at(1)->name(), animName2);

            QCOMPARE(list.at(0)->animationList().size(), 1);
            QCOMPARE(list.at(1)->animationList().size(), 1);

            animationController.setEntity(nullptr);
        }

        {
            // WHEN
            animationController.setRecursive(false);
            animationController.setEntity(entity.data());

            // THEN
            const QList<Qt3DAnimation::QAnimationGroup *> list = animationController.animationGroupList();
            QCOMPARE(list.size(), 1);

            QCOMPARE(list.at(0)->name(), animName1);

            QCOMPARE(list.at(0)->animationList().size(), 1);
        }
    }


    void testPropagatePosition()
    {
        // GIVEN
        Qt3DAnimation::QAnimationController animationController;
        Qt3DAnimation::QKeyframeAnimation *keyframeAnimation;

        QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity);
        keyframeAnimation = new Qt3DAnimation::QKeyframeAnimation(entity.data());

        const QString animName1 = QString("animation1");
        keyframeAnimation->setAnimationName(animName1);

        {
            // WHEN
            animationController.setEntity(entity.data());
            animationController.setPosition(2.0f);

            // THEN
            QCOMPARE(animationController.animationGroupList().at(0)->position(), 2.0f);
            QCOMPARE(keyframeAnimation->position(), 2.0f);
        }

        {
            // WHEN
            animationController.setPositionOffset(1.0);
            animationController.setPositionScale(2.0f);
            animationController.setPosition(2.0f);

            // THEN
            QCOMPARE(animationController.animationGroupList().at(0)->position(), 5.0f);
            QCOMPARE(keyframeAnimation->position(), 5.0f);
        }
    }

};

QTEST_APPLESS_MAIN(tst_QAnimationController)

#include "tst_qanimationcontroller.moc"