1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QTest>
30#include <Qt3DAnimation/qcallbackmapping.h>
31#include <Qt3DAnimation/private/qcallbackmapping_p.h>
32#include <Qt3DAnimation/private/qchannelmappingcreatedchange_p.h>
33#include <Qt3DCore/qentity.h>
34#include <Qt3DCore/qnodecreatedchange.h>
35#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
36#include <QObject>
37#include <QSignalSpy>
38#include <testpostmanarbiter.h>
39
40class DummyCallback : public Qt3DAnimation::QAnimationCallback
41{
42public:
43 void valueChanged(const QVariant &) override { }
44};
45
46class tst_QCallbackMapping : public QObject
47{
48 Q_OBJECT
49
50private Q_SLOTS:
51 void checkDefaultConstruction()
52 {
53 // GIVEN
54 Qt3DAnimation::QCallbackMapping mapping;
55
56 // THEN
57 QCOMPARE(mapping.channelName(), QString());
58 QCOMPARE(mapping.callback(), static_cast<Qt3DAnimation::QAnimationCallback *>(nullptr));
59 }
60
61 void checkPropertyChanges()
62 {
63 // GIVEN
64 Qt3DAnimation::QCallbackMapping mapping;
65
66 {
67 // WHEN
68 QSignalSpy spy(&mapping, SIGNAL(channelNameChanged(QString)));
69 const QString newValue(QStringLiteral("Rotation"));
70 mapping.setChannelName(newValue);
71
72 // THEN
73 QVERIFY(spy.isValid());
74 QCOMPARE(mapping.channelName(), newValue);
75 QCOMPARE(spy.count(), 1);
76
77 // WHEN
78 spy.clear();
79 mapping.setChannelName(newValue);
80
81 // THEN
82 QCOMPARE(mapping.channelName(), newValue);
83 QCOMPARE(spy.count(), 0);
84 }
85
86 {
87 // WHEN
88 auto newValue = new DummyCallback();
89 mapping.setCallback(type: QVariant::Quaternion, callback: newValue);
90
91 // THEN - no signals for callback
92 QCOMPARE(mapping.callback(), newValue);
93 }
94 }
95
96 void checkCreationData()
97 {
98 // GIVEN
99 Qt3DAnimation::QCallbackMapping mapping;
100 auto callback = new DummyCallback();
101
102 mapping.setChannelName(QStringLiteral("Location"));
103 mapping.setCallback(type: QVariant::Vector3D, callback);
104
105 // WHEN
106 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
107
108 {
109 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&mapping);
110 creationChanges = creationChangeGenerator.creationChanges();
111 }
112
113 // THEN
114 {
115 QCOMPARE(creationChanges.size(), 1);
116
117 const auto creationChangeData = qSharedPointerCast<Qt3DAnimation::QChannelMappingCreatedChange<Qt3DAnimation::QCallbackMappingData>>(src: creationChanges.first());
118 const Qt3DAnimation::QCallbackMappingData data = creationChangeData->data;
119
120 QCOMPARE(mapping.id(), creationChangeData->subjectId());
121 QCOMPARE(mapping.isEnabled(), true);
122 QCOMPARE(mapping.isEnabled(), creationChangeData->isNodeEnabled());
123 QCOMPARE(mapping.metaObject(), creationChangeData->metaObject());
124 QCOMPARE(creationChangeData->type(), Qt3DAnimation::QChannelMappingCreatedChangeBase::CallbackMapping);
125 QCOMPARE(mapping.channelName(), data.channelName);
126 QCOMPARE(mapping.callback(), data.callback);
127 QCOMPARE(int(QVariant::Vector3D), data.type);
128 }
129
130 // WHEN
131 mapping.setEnabled(false);
132
133 {
134 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&mapping);
135 creationChanges = creationChangeGenerator.creationChanges();
136 }
137
138 // THEN
139 {
140 QCOMPARE(creationChanges.size(), 1);
141
142 const auto creationChangeData = qSharedPointerCast<Qt3DAnimation::QChannelMappingCreatedChange<Qt3DAnimation::QCallbackMappingData>>(src: creationChanges.first());
143 const Qt3DAnimation::QCallbackMappingData data = creationChangeData->data;
144
145 QCOMPARE(mapping.id(), creationChangeData->subjectId());
146 QCOMPARE(mapping.isEnabled(), false);
147 QCOMPARE(mapping.isEnabled(), creationChangeData->isNodeEnabled());
148 QCOMPARE(mapping.metaObject(), creationChangeData->metaObject());
149 QCOMPARE(creationChangeData->type(), Qt3DAnimation::QChannelMappingCreatedChangeBase::CallbackMapping);
150 QCOMPARE(mapping.channelName(), data.channelName);
151 QCOMPARE(mapping.callback(), data.callback);
152 QCOMPARE(QVariant::Vector3D, data.type);
153 }
154 }
155
156 void checkPropertyUpdateChanges()
157 {
158 // GIVEN
159 TestArbiter arbiter;
160 Qt3DAnimation::QCallbackMapping mapping;
161 arbiter.setArbiterOnNode(&mapping);
162
163 {
164 // WHEN
165 mapping.setChannelName(QStringLiteral("Scale"));
166
167 // THEN
168 QCOMPARE(arbiter.dirtyNodes.size(), 1);
169 QCOMPARE(arbiter.dirtyNodes.front(), &mapping);
170
171 arbiter.dirtyNodes.clear();
172
173 // WHEN
174 mapping.setChannelName(QStringLiteral("Scale"));
175
176 // THEN
177 QCOMPARE(arbiter.dirtyNodes.size(), 0);
178 }
179
180 {
181 // WHEN
182 auto callback = new DummyCallback();
183 mapping.setCallback(type: QVariant::Vector3D, callback, flags: Qt3DAnimation::QAnimationCallback::OnThreadPool);
184 QCoreApplication::processEvents();
185
186 // THEN
187 QCOMPARE(arbiter.events.size(), 0);
188 QCOMPARE(arbiter.dirtyNodes.size(), 1);
189 QCOMPARE(arbiter.dirtyNodes.front(), &mapping);
190
191 arbiter.dirtyNodes.clear();
192
193 // WHEN
194 mapping.setCallback(type: QVariant::Vector3D, callback, flags: Qt3DAnimation::QAnimationCallback::OnThreadPool);
195 QCoreApplication::processEvents();
196
197 // THEN
198 QCOMPARE(arbiter.events.size(), 0);
199 QCOMPARE(arbiter.dirtyNodes.size(), 0);
200 }
201 }
202};
203
204QTEST_MAIN(tst_QCallbackMapping)
205
206#include "tst_qcallbackmapping.moc"
207

source code of qt3d/tests/auto/animation/qcallbackmapping/tst_qcallbackmapping.cpp