summaryrefslogtreecommitdiffstats
path: root/tests/auto/animation/qchannelmapping/tst_qchannelmapping.cpp
blob: 10283d0d6225561f4034aa6bbf404dced4f90433 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/****************************************************************************
**
** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QTest>
#include <Qt3DAnimation/qchannelmapping.h>
#include <Qt3DAnimation/private/qabstractchannelmapping_p.h>
#include <Qt3DAnimation/private/qchannelmapping_p.h>
#include <Qt3DCore/qentity.h>
#include <QObject>
#include <QSignalSpy>
#include <QQuaternion>
#include <testarbiter.h>

class tst_QTargetEntity : public Qt3DCore::QEntity
{
    Q_OBJECT
    Q_PROPERTY(QQuaternion rotation MEMBER m_rotation NOTIFY rotationChanged)
    Q_PROPERTY(QVector3D translation MEMBER m_translation NOTIFY translationChanged)
    Q_PROPERTY(QVector3D scale MEMBER m_scale NOTIFY scaleChanged)
    Q_PROPERTY(float floatProperty MEMBER m_floatProperty NOTIFY floatPropertyChanged)
    Q_PROPERTY(QVector2D vec2Property MEMBER m_vec2Property NOTIFY vec2PropertyChanged)
    Q_PROPERTY(QVector3D vec3Property MEMBER m_vec3Property NOTIFY vec3PropertyChanged)
    Q_PROPERTY(QVector4D vec4Property MEMBER m_vec4Property NOTIFY vec4PropertyChanged)
    Q_PROPERTY(QQuaternion quaternionProperty MEMBER m_quaternionProperty NOTIFY quaternionPropertyChanged)
    Q_PROPERTY(QVariantList listProperty MEMBER m_listProperty NOTIFY listPropertyChanged)
    Q_PROPERTY(QList<float> vecProperty MEMBER m_vecProperty NOTIFY vecPropertyChanged)

signals:
    void rotationChanged();
    void translationChanged();
    void scaleChanged();
    void floatPropertyChanged();
    void vec2PropertyChanged();
    void vec3PropertyChanged();
    void vec4PropertyChanged();
    void quaternionPropertyChanged();
    void listPropertyChanged();
    void vecPropertyChanged();

private:
    QQuaternion m_rotation;
    QVector3D m_translation;
    QVector3D m_scale;
    float m_floatProperty;
    QVector2D m_vec2Property;
    QVector3D m_vec3Property;
    QVector4D m_vec4Property;
    QQuaternion m_quaternionProperty;
    QVariantList m_listProperty;
    QList<float> m_vecProperty;
};


class tst_QChannelMapping : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void checkDefaultConstruction()
    {
        // GIVEN
        Qt3DAnimation::QChannelMapping mapping;

        // THEN
        QCOMPARE(mapping.channelName(), QString());
        QCOMPARE(mapping.target(), static_cast<Qt3DCore::QNode *>(nullptr));
        QCOMPARE(mapping.property(), QString());

        const Qt3DAnimation::QChannelMappingPrivate *d =
            static_cast<const Qt3DAnimation::QChannelMappingPrivate *>(
                Qt3DAnimation::QChannelMappingPrivate::get(&mapping));

        QCOMPARE(d->m_type, static_cast<int>(QVariant::Invalid));
        QCOMPARE(d->m_componentCount, 0);
    }

    void checkPropertyChanges()
    {
        // GIVEN
        Qt3DAnimation::QChannelMapping mapping;

        {
            // WHEN
            QSignalSpy spy(&mapping, SIGNAL(channelNameChanged(QString)));
            const QString newValue(QStringLiteral("Rotation"));
            mapping.setChannelName(newValue);

            // THEN
            QVERIFY(spy.isValid());
            QCOMPARE(mapping.channelName(), newValue);
            QCOMPARE(spy.count(), 1);

            // WHEN
            spy.clear();
            mapping.setChannelName(newValue);

            // THEN
            QCOMPARE(mapping.channelName(), newValue);
            QCOMPARE(spy.count(), 0);
        }

        {
            // WHEN
            QSignalSpy spy(&mapping, SIGNAL(targetChanged(Qt3DCore::QNode*)));
            auto newValue = new Qt3DCore::QEntity();
            mapping.setTarget(newValue);

            // THEN
            QVERIFY(spy.isValid());
            QCOMPARE(mapping.target(), newValue);
            QCOMPARE(newValue->parent(), &mapping);
            QCOMPARE(spy.count(), 1);

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

            // THEN
            QCOMPARE(mapping.target(), newValue);
            QCOMPARE(spy.count(), 0);
        }

        {
            // WHEN
            QSignalSpy spy(&mapping, SIGNAL(propertyChanged(QString)));
            const QString newValue(QStringLiteral("rotation"));
            mapping.setProperty(newValue);

            // THEN
            QVERIFY(spy.isValid());
            QCOMPARE(mapping.property(), newValue);
            QCOMPARE(spy.count(), 1);

            // WHEN
            spy.clear();
            mapping.setProperty(newValue);

            // THEN
            QCOMPARE(mapping.property(), newValue);
            QCOMPARE(spy.count(), 0);
        }
    }

    void checkPropertyUpdateChanges()
    {
        // GIVEN
        TestArbiter arbiter;
        Qt3DAnimation::QChannelMapping mapping;
        QScopedPointer<Qt3DCore::QEntity> target(new tst_QTargetEntity());
        arbiter.setArbiterOnNode(&mapping);

        {
            // WHEN
            mapping.setChannelName(QStringLiteral("Scale"));

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 1);
            QCOMPARE(arbiter.dirtyNodes().front(), &mapping);

            arbiter.clear();

            // WHEN
            mapping.setChannelName(QStringLiteral("Scale"));

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 0);
        }

        {
            // WHEN
            mapping.setTarget(target.data());

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 1);
            QCOMPARE(arbiter.dirtyNodes().front(), &mapping);

            arbiter.clear();

            // WHEN
            mapping.setTarget(target.data());

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 0);
        }

        {
            // WHEN
            target->setProperty("scale", QVector3D(1.0f, 0.0f, 0.0f));
            mapping.setProperty(QStringLiteral("scale"));
            QCoreApplication::processEvents();

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 1);
            QCOMPARE(arbiter.dirtyNodes().front(), &mapping);

            arbiter.clear();

            // WHEN
            mapping.setProperty(QStringLiteral("scale"));
            QCoreApplication::processEvents();

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 0);
        }
    }

    void checkPropertyUpdateNameTypeAndComponentCount_data()
    {
        QTest::addColumn<QByteArray>("propertyName");
        QTest::addColumn<QVariant>("value");
        QTest::addColumn<int>("expectedType");
        QTest::addColumn<int>("expectedComponentCount");

        QTest::newRow("float") << QByteArrayLiteral("floatProperty") << QVariant(1.0f) << static_cast<int>(QMetaType::Float) << 1;
        QTest::newRow("vec2") << QByteArrayLiteral("vec2Property") << QVariant(QVector2D(1.0f, 1.0f)) << static_cast<int>(QVariant::Vector2D) << 2;
        QTest::newRow("vec3") << QByteArrayLiteral("vec3Property") << QVariant(QVector3D(1.0f, 1.0f, 1.0f)) << static_cast<int>(QVariant::Vector3D) << 3;
        QTest::newRow("vec4") << QByteArrayLiteral("vec4Property") << QVariant(QVector4D(1.0f, 1.0f, 1.0f, 1.0f)) << static_cast<int>(QVariant::Vector4D) << 4;
        QTest::newRow("quaternion") << QByteArrayLiteral("quaternionProperty") << QVariant(QQuaternion(1.0f, 1.0f, 1.0f, 1.0f)) << static_cast<int>(QVariant::Quaternion) << 4;

        QVariantList list = QVariantList() << QVariant(1.0f) << QVariant(1.0) << QVariant(1.0f) << QVariant(1.0f) << QVariant(1.0f);
        QTest::newRow("variantlist") << QByteArrayLiteral("listProperty") << QVariant::fromValue(list) << static_cast<int>(QVariant::List) << 5;

        QList<float> vec(8);
        QTest::newRow("vector") << QByteArrayLiteral("vecProperty") << QVariant::fromValue(vec) << qMetaTypeId<decltype(vec)>() << 8;
    }

    void checkPropertyUpdateNameTypeAndComponentCount()
    {
        // GIVEN
        QFETCH(QByteArray, propertyName);
        QFETCH(QVariant, value);
        QFETCH(int, expectedType);
        QFETCH(int, expectedComponentCount);

        Q_UNUSED(expectedType);
        Q_UNUSED(expectedComponentCount);

        TestArbiter arbiter;
        Qt3DAnimation::QChannelMapping mapping;
        QScopedPointer<Qt3DCore::QEntity> target(new tst_QTargetEntity());
        mapping.setTarget(target.data());
        arbiter.setArbiterOnNode(&mapping);

        {
            // WHEN
            target->setProperty(propertyName.constData(), value);

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 1);
            QCOMPARE(arbiter.dirtyNodes().front(), target.data());

            arbiter.clear();

            // THEN
            mapping.setProperty(QString::fromLatin1(propertyName));

            // THEN
            QCOMPARE(arbiter.dirtyNodes().size(), 1);
        }
    }

};

QTEST_MAIN(tst_QChannelMapping)

#include "tst_qchannelmapping.moc"