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
30#include <QtTest/QTest>
31#include <Qt3DAnimation/qanimationcliploader.h>
32#include <Qt3DAnimation/qchannelmapper.h>
33#include <Qt3DAnimation/qclock.h>
34#include <Qt3DAnimation/qclipanimator.h>
35#include <Qt3DAnimation/private/qanimationclip_p.h>
36#include <Qt3DAnimation/private/qclipanimator_p.h>
37#include <Qt3DCore/qnodecreatedchange.h>
38#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
39#include <QObject>
40#include <QSignalSpy>
41#include <testpostmanarbiter.h>
42
43class tst_QClipAnimator : public QObject
44{
45 Q_OBJECT
46
47private Q_SLOTS:
48 void initTestCase()
49 {
50 qRegisterMetaType<Qt3DAnimation::QAbstractAnimationClip*>();
51 qRegisterMetaType<Qt3DAnimation::QChannelMapper*>();
52 qRegisterMetaType<Qt3DAnimation::QClock*>();
53 }
54
55 void checkDefaultConstruction()
56 {
57 // GIVEN
58 Qt3DAnimation::QClipAnimator animator;
59
60 // THEN
61 QCOMPARE(animator.clip(), static_cast<Qt3DAnimation::QAbstractAnimationClip *>(nullptr));
62 QCOMPARE(animator.channelMapper(), static_cast<Qt3DAnimation::QChannelMapper *>(nullptr));
63 QCOMPARE(animator.clock(), static_cast<Qt3DAnimation::QClock*>(nullptr));
64 QCOMPARE(animator.loopCount(), 1);
65 QCOMPARE(animator.normalizedTime(), 0.0f);
66 }
67
68 void checkPropertyChanges()
69 {
70 // GIVEN
71 Qt3DAnimation::QClipAnimator animator;
72
73 {
74 // WHEN
75 QSignalSpy spy(&animator, SIGNAL(clipChanged(Qt3DAnimation::QAbstractAnimationClip *)));
76 auto newValue = new Qt3DAnimation::QAnimationClipLoader();
77 animator.setClip(newValue);
78
79 // THEN
80 QVERIFY(spy.isValid());
81 QCOMPARE(animator.clip(), newValue);
82 QCOMPARE(newValue->parent(), &animator);
83 QCOMPARE(spy.count(), 1);
84
85 // WHEN
86 spy.clear();
87 animator.setClip(newValue);
88
89 // THEN
90 QCOMPARE(animator.clip(), newValue);
91 QCOMPARE(spy.count(), 0);
92 }
93
94 {
95 // WHEN
96 QSignalSpy spy(&animator, SIGNAL(channelMapperChanged(Qt3DAnimation::QChannelMapper *)));
97 auto newValue = new Qt3DAnimation::QChannelMapper();
98 animator.setChannelMapper(newValue);
99
100 // THEN
101 QVERIFY(spy.isValid());
102 QCOMPARE(animator.channelMapper(), newValue);
103 QCOMPARE(newValue->parent(), &animator);
104 QCOMPARE(spy.count(), 1);
105
106 // WHEN
107 spy.clear();
108 animator.setChannelMapper(newValue);
109
110 // THEN
111 QCOMPARE(animator.channelMapper(), newValue);
112 QCOMPARE(spy.count(), 0);
113 }
114
115 {
116 // WHEN
117 QSignalSpy spy(&animator, SIGNAL(clockChanged(Qt3DAnimation::QClock *)));
118 auto clock = new Qt3DAnimation::QClock();
119 animator.setClock(clock);
120
121 // THEN
122 QVERIFY(spy.isValid());
123 QCOMPARE(animator.clock(), clock);
124 QCOMPARE(clock->parent(), &animator);
125 QCOMPARE(spy.count(), 1);
126
127 // WHEN
128 spy.clear();
129 animator.setClock(clock);
130
131 // THEN
132 QCOMPARE(animator.clock(), clock);
133 QCOMPARE(spy.count(), 0);
134 }
135
136 {
137 // WHEN
138 QSignalSpy spy(&animator, SIGNAL(loopCountChanged(int)));
139 const int newValue = 5;
140 animator.setLoopCount(newValue);
141
142 // THEN
143 QVERIFY(spy.isValid());
144 QCOMPARE(animator.loopCount(), newValue);
145 QCOMPARE(spy.count(), 1);
146
147 // WHEN
148 spy.clear();
149 animator.setLoopCount(newValue);
150
151 // THEN
152 QCOMPARE(animator.loopCount(), newValue);
153 QCOMPARE(spy.count(), 0);
154 }
155 }
156
157 void checkRunning()
158 {
159 // GIVEN
160 Qt3DAnimation::QClipAnimator animator;
161 animator.stop();
162
163 {
164 // WHEN
165 animator.setRunning(true);
166
167 // THEN
168 QCOMPARE(animator.isRunning(), false);
169 }
170
171 {
172 // WHEN
173 Qt3DAnimation::QChannelMapper *mapper = new Qt3DAnimation::QChannelMapper;
174 Qt3DAnimation::QAnimationClip *clip = new Qt3DAnimation::QAnimationClip;
175 animator.setClip(clip);
176 animator.setChannelMapper(mapper);
177 animator.setRunning(true);
178
179 // THEN
180 QCOMPARE(animator.isRunning(), true);
181 }
182 }
183
184 void checkCreationData()
185 {
186 // GIVEN
187 Qt3DAnimation::QClipAnimator animator;
188 auto clip = new Qt3DAnimation::QAnimationClipLoader();
189 animator.setClip(clip);
190 auto mapper = new Qt3DAnimation::QChannelMapper();
191 animator.setChannelMapper(mapper);
192 auto clock = new Qt3DAnimation::QClock();
193 animator.setClock(clock);
194
195 // WHEN
196 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
197 {
198 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&animator);
199 creationChanges = creationChangeGenerator.creationChanges();
200 }
201
202 // THEN
203 {
204 QCOMPARE(creationChanges.size(), 4);
205
206 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DAnimation::QClipAnimatorData>>(src: creationChanges.first());
207 const Qt3DAnimation::QClipAnimatorData data = creationChangeData->data;
208
209 QCOMPARE(animator.id(), creationChangeData->subjectId());
210 QCOMPARE(animator.isEnabled(), true);
211 QCOMPARE(animator.isEnabled(), creationChangeData->isNodeEnabled());
212 QCOMPARE(animator.metaObject(), creationChangeData->metaObject());
213 QCOMPARE(animator.clip()->id(), data.clipId);
214 QCOMPARE(animator.channelMapper()->id(), data.mapperId);
215 QCOMPARE(animator.clock()->id(), data.clockId);
216 QCOMPARE(animator.loopCount(), data.loops);
217 }
218
219 // WHEN
220 animator.setEnabled(false);
221 {
222 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&animator);
223 creationChanges = creationChangeGenerator.creationChanges();
224 }
225
226 // THEN
227 {
228 QCOMPARE(creationChanges.size(), 4);
229
230 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DAnimation::QClipAnimatorData>>(src: creationChanges.first());
231
232 QCOMPARE(animator.id(), creationChangeData->subjectId());
233 QCOMPARE(animator.isEnabled(), false);
234 QCOMPARE(animator.isEnabled(), creationChangeData->isNodeEnabled());
235 QCOMPARE(animator.metaObject(), creationChangeData->metaObject());
236 }
237 }
238
239 void checkPropertyUpdate()
240 {
241 // GIVEN
242 TestArbiter arbiter;
243 Qt3DAnimation::QClipAnimator animator;
244 auto clip = new Qt3DAnimation::QAnimationClipLoader();
245 arbiter.setArbiterOnNode(&animator);
246
247 {
248 // WHEN
249 animator.setClip(clip);
250
251 // THEN
252 QCOMPARE(arbiter.dirtyNodes.size(), 1);
253 QCOMPARE(arbiter.dirtyNodes.front(), &animator);
254
255 arbiter.dirtyNodes.clear();
256 }
257
258 {
259 // WHEN
260 animator.setClip(clip);
261
262 // THEN
263 QCOMPARE(arbiter.dirtyNodes.size(), 0);
264 }
265
266 // GIVEN
267 auto mapper = new Qt3DAnimation::QChannelMapper;
268 {
269 // WHEN
270 animator.setChannelMapper(mapper);
271
272 // THEN
273 QCOMPARE(arbiter.dirtyNodes.size(), 1);
274 QCOMPARE(arbiter.dirtyNodes.front(), &animator);
275
276 arbiter.dirtyNodes.clear();
277 }
278
279 {
280 // WHEN
281 animator.setChannelMapper(mapper);
282
283 // THEN
284 QCOMPARE(arbiter.dirtyNodes.size(), 0);
285 }
286
287 // GIVEN
288 auto clock = new Qt3DAnimation::QClock;
289 {
290 // WHEN
291 animator.setClock(clock);
292
293 // THEN
294 QCOMPARE(arbiter.dirtyNodes.size(), 1);
295 QCOMPARE(arbiter.dirtyNodes.front(), &animator);
296
297 arbiter.dirtyNodes.clear();
298 }
299
300 {
301 // WHEN
302 animator.setClock(clock);
303
304 // THEN
305 QCOMPARE(arbiter.dirtyNodes.size(), 0);
306 }
307
308 {
309 // WHEN
310 animator.setLoopCount(10);
311
312 // THEN
313 QCOMPARE(arbiter.dirtyNodes.size(), 1);
314 QCOMPARE(arbiter.dirtyNodes.front(), &animator);
315
316 arbiter.dirtyNodes.clear();
317 }
318
319 {
320 // WHEN
321 animator.setLoopCount(10);
322
323 // THEN
324 QCOMPARE(arbiter.dirtyNodes.size(), 0);
325 }
326
327 }
328};
329
330QTEST_MAIN(tst_QClipAnimator)
331
332#include "tst_qclipanimator.moc"
333

source code of qt3d/tests/auto/animation/qclipanimator/tst_qclipanimator.cpp