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/qclipblendvalue.h>
32#include <Qt3DAnimation/qanimationcliploader.h>
33#include <Qt3DAnimation/private/qclipblendvalue_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
41class tst_QClipBlendValue : public QObject
42{
43 Q_OBJECT
44
45private Q_SLOTS:
46 void initTestCase()
47 {
48 qRegisterMetaType<Qt3DAnimation::QAbstractAnimationClip*>();
49 }
50
51 void checkDefaultConstruction()
52 {
53 // GIVEN
54 Qt3DAnimation::QClipBlendValue clipBlendNode;
55
56 // THEN;
57 QCOMPARE(clipBlendNode.clip(), static_cast<Qt3DAnimation::QAbstractAnimationClip *>(nullptr));
58 }
59
60 void checkPropertyChanges()
61 {
62 // GIVEN
63 Qt3DAnimation::QClipBlendValue clipBlendNode;
64
65 {
66 // WHEN
67 QSignalSpy spy(&clipBlendNode, SIGNAL(clipChanged(Qt3DAnimation::QAbstractAnimationClip*)));
68 auto newValue = new Qt3DAnimation::QAnimationClipLoader();
69 clipBlendNode.setClip(newValue);
70
71 // THEN
72 QVERIFY(spy.isValid());
73 QCOMPARE(clipBlendNode.clip(), newValue);
74 QCOMPARE(spy.count(), 1);
75
76 // WHEN
77 spy.clear();
78 clipBlendNode.setClip(newValue);
79
80 // THEN
81 QCOMPARE(clipBlendNode.clip(), newValue);
82 QCOMPARE(spy.count(), 0);
83 }
84 }
85
86 void checkCreationData()
87 {
88 // GIVEN
89 Qt3DAnimation::QClipBlendValue clipBlendNode;
90 Qt3DAnimation::QAnimationClipLoader clip;
91
92 clipBlendNode.setClip(&clip);
93
94 // WHEN
95 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
96
97 {
98 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&clipBlendNode);
99 creationChanges = creationChangeGenerator.creationChanges();
100 }
101
102 // THEN
103 {
104 QCOMPARE(creationChanges.size(), 2); // 1 + 1 clip
105
106 const auto creationChangeData = qSharedPointerCast<Qt3DAnimation::QClipBlendNodeCreatedChange<Qt3DAnimation::QClipBlendValueData>>(src: creationChanges.first());
107 const Qt3DAnimation::QClipBlendValueData cloneData = creationChangeData->data;
108
109 QCOMPARE(clipBlendNode.id(), creationChangeData->subjectId());
110 QCOMPARE(clipBlendNode.isEnabled(), true);
111 QCOMPARE(clipBlendNode.isEnabled(), creationChangeData->isNodeEnabled());
112 QCOMPARE(clipBlendNode.metaObject(), creationChangeData->metaObject());
113 QCOMPARE(cloneData.clipId, clip.id());
114 }
115
116 // WHEN
117 clipBlendNode.setEnabled(false);
118
119 {
120 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&clipBlendNode);
121 creationChanges = creationChangeGenerator.creationChanges();
122 }
123
124 // THEN
125 {
126 QCOMPARE(creationChanges.size(), 2); // 1 + 1 clip
127
128 const auto creationChangeData = qSharedPointerCast<Qt3DAnimation::QClipBlendNodeCreatedChange<Qt3DAnimation::QClipBlendValueData>>(src: creationChanges.first());
129 const Qt3DAnimation::QClipBlendValueData cloneData = creationChangeData->data;
130
131 QCOMPARE(clipBlendNode.id(), creationChangeData->subjectId());
132 QCOMPARE(clipBlendNode.isEnabled(), false);
133 QCOMPARE(clipBlendNode.isEnabled(), creationChangeData->isNodeEnabled());
134 QCOMPARE(clipBlendNode.metaObject(), creationChangeData->metaObject());
135 QCOMPARE(cloneData.clipId, clip.id());
136 }
137 }
138
139 void checkClipUpdate()
140 {
141 // GIVEN
142 TestArbiter arbiter;
143 Qt3DAnimation::QClipBlendValue clipBlendNode;
144 arbiter.setArbiterOnNode(&clipBlendNode);
145 auto clip = new Qt3DAnimation::QAnimationClipLoader();
146
147 {
148 // WHEN
149 clipBlendNode.setClip(clip);
150
151 // THEN
152 QCOMPARE(arbiter.dirtyNodes.size(), 1);
153 QCOMPARE(arbiter.dirtyNodes.front(), &clipBlendNode);
154
155 arbiter.dirtyNodes.clear();
156 }
157
158 {
159 // WHEN
160 clipBlendNode.setClip(clip);
161
162 // THEN
163 QCOMPARE(arbiter.dirtyNodes.size(), 0);
164 }
165 }
166
167 void checkStartClipBookkeeping()
168 {
169 // GIVEN
170 QScopedPointer<Qt3DAnimation::QClipBlendValue> clipBlendNode(new Qt3DAnimation::QClipBlendValue);
171 {
172 // WHEN
173 Qt3DAnimation::QAnimationClipLoader clip;
174 clipBlendNode->setClip(&clip);
175
176 // THEN
177 QCOMPARE(clip.parent(), clipBlendNode.data());
178 QCOMPARE(clipBlendNode->clip(), &clip);
179 }
180 // THEN (Should not crash and clip be unset)
181 QVERIFY(clipBlendNode->clip() == nullptr);
182
183 {
184 // WHEN
185 Qt3DAnimation::QClipBlendValue someOtherNode;
186 QScopedPointer<Qt3DAnimation::QAnimationClipLoader> clip(new Qt3DAnimation::QAnimationClipLoader(&someOtherNode));
187 clipBlendNode->setClip(clip.data());
188
189 // THEN
190 QCOMPARE(clip->parent(), &someOtherNode);
191 QCOMPARE(clipBlendNode->clip(), clip.data());
192
193 // WHEN
194 clipBlendNode.reset();
195 clip.reset();
196
197 // THEN Should not crash when the effect is destroyed (tests for failed removal of destruction helper)
198 }
199 }
200};
201
202QTEST_MAIN(tst_QClipBlendValue)
203
204#include "tst_qclipblendvalue.moc"
205

source code of qt3d/tests/auto/animation/qclipblendvalue/tst_qclipblendvalue.cpp