1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Paul Lemire <paul.lemire350@gmail.com> |
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/qadditiveclipblend.h> |
32 | #include <Qt3DAnimation/qanimationcliploader.h> |
33 | #include <Qt3DAnimation/private/qadditiveclipblend_p.h> |
34 | #include <QObject> |
35 | #include <QSignalSpy> |
36 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
37 | #include <Qt3DAnimation/qclipblendnodecreatedchange.h> |
38 | #include <Qt3DCore/qnodecreatedchange.h> |
39 | #include "testpostmanarbiter.h" |
40 | |
41 | class tst_QAdditiveClipBlend : public QObject |
42 | { |
43 | Q_OBJECT |
44 | |
45 | private Q_SLOTS: |
46 | void initTestCase() |
47 | { |
48 | qRegisterMetaType<Qt3DAnimation::QAbstractClipBlendNode*>(); |
49 | } |
50 | |
51 | void checkDefaultConstruction() |
52 | { |
53 | // GIVEN |
54 | Qt3DAnimation::QAdditiveClipBlend addBlend; |
55 | |
56 | // THEN |
57 | QCOMPARE(addBlend.additiveFactor(), 0.0f); |
58 | QCOMPARE(addBlend.baseClip(), static_cast<Qt3DAnimation::QAbstractClipBlendNode *>(nullptr)); |
59 | QCOMPARE(addBlend.additiveClip(), static_cast<Qt3DAnimation::QAbstractClipBlendNode *>(nullptr)); |
60 | } |
61 | |
62 | void checkPropertyChanges() |
63 | { |
64 | // GIVEN |
65 | Qt3DAnimation::QAdditiveClipBlend addBlend; |
66 | |
67 | { |
68 | // WHEN |
69 | QSignalSpy spy(&addBlend, SIGNAL(additiveFactorChanged(float))); |
70 | const float newValue = 0.5f; |
71 | addBlend.setAdditiveFactor(newValue); |
72 | |
73 | // THEN |
74 | QVERIFY(spy.isValid()); |
75 | QCOMPARE(addBlend.additiveFactor(), newValue); |
76 | QCOMPARE(spy.count(), 1); |
77 | |
78 | // WHEN |
79 | spy.clear(); |
80 | addBlend.setAdditiveFactor(newValue); |
81 | |
82 | // THEN |
83 | QCOMPARE(addBlend.additiveFactor(), newValue); |
84 | QCOMPARE(spy.count(), 0); |
85 | } |
86 | |
87 | { |
88 | // WHEN |
89 | QSignalSpy spy(&addBlend, SIGNAL(baseClipChanged(Qt3DAnimation::QAbstractClipBlendNode*))); |
90 | auto newValue = new Qt3DAnimation::QAdditiveClipBlend(); |
91 | addBlend.setBaseClip(newValue); |
92 | |
93 | // THEN |
94 | QVERIFY(spy.isValid()); |
95 | QCOMPARE(addBlend.baseClip(), newValue); |
96 | QCOMPARE(spy.count(), 1); |
97 | |
98 | // WHEN |
99 | spy.clear(); |
100 | addBlend.setBaseClip(newValue); |
101 | |
102 | // THEN |
103 | QCOMPARE(addBlend.baseClip(), newValue); |
104 | QCOMPARE(spy.count(), 0); |
105 | } |
106 | |
107 | { |
108 | // WHEN |
109 | QSignalSpy spy(&addBlend, SIGNAL(additiveClipChanged(Qt3DAnimation::QAbstractClipBlendNode*))); |
110 | auto newValue = new Qt3DAnimation::QAdditiveClipBlend(); |
111 | addBlend.setAdditiveClip(newValue); |
112 | |
113 | // THEN |
114 | QVERIFY(spy.isValid()); |
115 | QCOMPARE(addBlend.additiveClip(), newValue); |
116 | QCOMPARE(spy.count(), 1); |
117 | |
118 | // WHEN |
119 | spy.clear(); |
120 | addBlend.setAdditiveClip(newValue); |
121 | |
122 | // THEN |
123 | QCOMPARE(addBlend.additiveClip(), newValue); |
124 | QCOMPARE(spy.count(), 0); |
125 | } |
126 | } |
127 | |
128 | void checkCreationData() |
129 | { |
130 | // GIVEN |
131 | Qt3DAnimation::QAdditiveClipBlend addBlend; |
132 | Qt3DAnimation::QAdditiveClipBlend baseClip; |
133 | Qt3DAnimation::QAdditiveClipBlend additiveClip; |
134 | |
135 | addBlend.setBaseClip(&baseClip); |
136 | addBlend.setAdditiveClip(&additiveClip); |
137 | addBlend.setAdditiveFactor(0.8f); |
138 | |
139 | |
140 | // WHEN |
141 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
142 | |
143 | { |
144 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&addBlend); |
145 | creationChanges = creationChangeGenerator.creationChanges(); |
146 | } |
147 | |
148 | // THEN |
149 | { |
150 | QCOMPARE(creationChanges.size(), 3); // 1 + 2 clips |
151 | |
152 | const auto creationChangeData = qSharedPointerCast<Qt3DAnimation::QClipBlendNodeCreatedChange<Qt3DAnimation::QAdditiveClipBlendData>>(src: creationChanges.first()); |
153 | const Qt3DAnimation::QAdditiveClipBlendData cloneData = creationChangeData->data; |
154 | |
155 | QCOMPARE(addBlend.additiveFactor(), cloneData.additiveFactor); |
156 | QCOMPARE(addBlend.id(), creationChangeData->subjectId()); |
157 | QCOMPARE(addBlend.isEnabled(), true); |
158 | QCOMPARE(addBlend.isEnabled(), creationChangeData->isNodeEnabled()); |
159 | QCOMPARE(addBlend.metaObject(), creationChangeData->metaObject()); |
160 | QCOMPARE(cloneData.baseClipId, baseClip.id()); |
161 | QCOMPARE(cloneData.additiveClipId, additiveClip.id()); |
162 | } |
163 | |
164 | // WHEN |
165 | addBlend.setEnabled(false); |
166 | |
167 | { |
168 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&addBlend); |
169 | creationChanges = creationChangeGenerator.creationChanges(); |
170 | } |
171 | |
172 | // THEN |
173 | { |
174 | QCOMPARE(creationChanges.size(), 3); // 1 + 2 clips |
175 | |
176 | const auto creationChangeData = qSharedPointerCast<Qt3DAnimation::QClipBlendNodeCreatedChange<Qt3DAnimation::QAdditiveClipBlendData>>(src: creationChanges.first()); |
177 | const Qt3DAnimation::QAdditiveClipBlendData cloneData = creationChangeData->data; |
178 | |
179 | QCOMPARE(addBlend.additiveFactor(), cloneData.additiveFactor); |
180 | QCOMPARE(addBlend.id(), creationChangeData->subjectId()); |
181 | QCOMPARE(addBlend.isEnabled(), false); |
182 | QCOMPARE(addBlend.isEnabled(), creationChangeData->isNodeEnabled()); |
183 | QCOMPARE(addBlend.metaObject(), creationChangeData->metaObject()); |
184 | QCOMPARE(cloneData.baseClipId, baseClip.id()); |
185 | QCOMPARE(cloneData.additiveClipId, additiveClip.id()); |
186 | } |
187 | } |
188 | |
189 | void checkAdditiveFactorUpdate() |
190 | { |
191 | // GIVEN |
192 | TestArbiter arbiter; |
193 | Qt3DAnimation::QAdditiveClipBlend addBlend; |
194 | arbiter.setArbiterOnNode(&addBlend); |
195 | |
196 | { |
197 | // WHEN |
198 | addBlend.setAdditiveFactor(0.4f); |
199 | |
200 | // THEN |
201 | QCOMPARE(arbiter.events.size(), 0); |
202 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
203 | QCOMPARE(arbiter.dirtyNodes.front(), &addBlend); |
204 | |
205 | arbiter.dirtyNodes.clear(); |
206 | } |
207 | |
208 | { |
209 | // WHEN |
210 | addBlend.setAdditiveFactor(0.4f); |
211 | |
212 | // THEN |
213 | QCOMPARE(arbiter.events.size(), 0); |
214 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
215 | } |
216 | |
217 | } |
218 | |
219 | void checkBaseClipUpdate() |
220 | { |
221 | // GIVEN |
222 | TestArbiter arbiter; |
223 | Qt3DAnimation::QAdditiveClipBlend addBlend; |
224 | arbiter.setArbiterOnNode(&addBlend); |
225 | auto baseClip = new Qt3DAnimation::QAdditiveClipBlend(); |
226 | |
227 | { |
228 | // WHEN |
229 | addBlend.setBaseClip(baseClip); |
230 | |
231 | // THEN |
232 | QCOMPARE(arbiter.events.size(), 0); |
233 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
234 | QCOMPARE(arbiter.dirtyNodes.front(), &addBlend); |
235 | |
236 | arbiter.dirtyNodes.clear(); |
237 | } |
238 | |
239 | { |
240 | // WHEN |
241 | addBlend.setBaseClip(baseClip); |
242 | |
243 | // THEN |
244 | QCOMPARE(arbiter.events.size(), 0); |
245 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
246 | } |
247 | } |
248 | |
249 | void checkAdditiveClipUpdate() |
250 | { |
251 | // GIVEN |
252 | TestArbiter arbiter; |
253 | Qt3DAnimation::QAdditiveClipBlend addBlend; |
254 | arbiter.setArbiterOnNode(&addBlend); |
255 | auto additiveClip = new Qt3DAnimation::QAdditiveClipBlend(); |
256 | |
257 | { |
258 | // WHEN |
259 | addBlend.setAdditiveClip(additiveClip); |
260 | |
261 | // THEN |
262 | QCOMPARE(arbiter.events.size(), 0); |
263 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
264 | QCOMPARE(arbiter.dirtyNodes.front(), &addBlend); |
265 | arbiter.dirtyNodes.clear(); |
266 | } |
267 | |
268 | { |
269 | // WHEN |
270 | addBlend.setAdditiveClip(additiveClip); |
271 | |
272 | // |
273 | QCOMPARE(arbiter.events.size(), 0); |
274 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
275 | } |
276 | } |
277 | |
278 | void checkBaseClipBookkeeping() |
279 | { |
280 | // GIVEN |
281 | QScopedPointer<Qt3DAnimation::QAdditiveClipBlend> additiveBlend(new Qt3DAnimation::QAdditiveClipBlend); |
282 | { |
283 | // WHEN |
284 | Qt3DAnimation::QAdditiveClipBlend clip; |
285 | additiveBlend->setBaseClip(&clip); |
286 | |
287 | // THEN |
288 | QCOMPARE(clip.parent(), additiveBlend.data()); |
289 | QCOMPARE(additiveBlend->baseClip(), &clip); |
290 | } |
291 | // THEN (Should not crash and clip be unset) |
292 | QVERIFY(additiveBlend->baseClip() == nullptr); |
293 | |
294 | { |
295 | // WHEN |
296 | Qt3DAnimation::QAdditiveClipBlend someOtherAdditiveBlend; |
297 | QScopedPointer<Qt3DAnimation::QAdditiveClipBlend> clip(new Qt3DAnimation::QAdditiveClipBlend(&someOtherAdditiveBlend)); |
298 | additiveBlend->setBaseClip(clip.data()); |
299 | |
300 | // THEN |
301 | QCOMPARE(clip->parent(), &someOtherAdditiveBlend); |
302 | QCOMPARE(additiveBlend->baseClip(), clip.data()); |
303 | |
304 | // WHEN |
305 | additiveBlend.reset(); |
306 | clip.reset(); |
307 | |
308 | // THEN Should not crash when the effect is destroyed (tests for failed removal of destruction helper) |
309 | } |
310 | } |
311 | |
312 | void checkAdditiveClipBookkeeping() |
313 | { |
314 | // GIVEN |
315 | QScopedPointer<Qt3DAnimation::QAdditiveClipBlend> additiveBlend(new Qt3DAnimation::QAdditiveClipBlend); |
316 | { |
317 | // WHEN |
318 | Qt3DAnimation::QAdditiveClipBlend clip; |
319 | additiveBlend->setAdditiveClip(&clip); |
320 | |
321 | // THEN |
322 | QCOMPARE(clip.parent(), additiveBlend.data()); |
323 | QCOMPARE(additiveBlend->additiveClip(), &clip); |
324 | } |
325 | // THEN (Should not crash and clip be unset) |
326 | QVERIFY(additiveBlend->additiveClip() == nullptr); |
327 | |
328 | { |
329 | // WHEN |
330 | Qt3DAnimation::QAdditiveClipBlend someOtherAdditiveBlend; |
331 | QScopedPointer<Qt3DAnimation::QAdditiveClipBlend> clip(new Qt3DAnimation::QAdditiveClipBlend(&someOtherAdditiveBlend)); |
332 | additiveBlend->setAdditiveClip(clip.data()); |
333 | |
334 | // THEN |
335 | QCOMPARE(clip->parent(), &someOtherAdditiveBlend); |
336 | QCOMPARE(additiveBlend->additiveClip(), clip.data()); |
337 | |
338 | // WHEN |
339 | additiveBlend.reset(); |
340 | clip.reset(); |
341 | |
342 | // THEN Should not crash when the effect is destroyed (tests for failed removal of destruction helper) |
343 | } |
344 | } |
345 | }; |
346 | |
347 | QTEST_MAIN(tst_QAdditiveClipBlend) |
348 | |
349 | #include "tst_qadditiveclipblend.moc" |
350 | |