1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 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 <Qt3DRender/qeffect.h> |
32 | #include <Qt3DRender/qparameter.h> |
33 | #include <Qt3DRender/qtechnique.h> |
34 | #include <Qt3DRender/private/qeffect_p.h> |
35 | #include <QObject> |
36 | #include <QSignalSpy> |
37 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
38 | #include <Qt3DCore/qnodecreatedchange.h> |
39 | #include "testpostmanarbiter.h" |
40 | |
41 | class tst_QEffect : public QObject |
42 | { |
43 | Q_OBJECT |
44 | |
45 | private Q_SLOTS: |
46 | |
47 | void checkDefaultConstruction() |
48 | { |
49 | // GIVEN |
50 | Qt3DRender::QEffect effect; |
51 | |
52 | // THEN |
53 | QCOMPARE(effect.parameters().size(), 0); |
54 | QCOMPARE(effect.techniques().size(), 0); |
55 | } |
56 | |
57 | void checkPropertyChanges() |
58 | { |
59 | // GIVEN |
60 | Qt3DRender::QEffect effect; |
61 | |
62 | { |
63 | // WHEN |
64 | Qt3DRender::QParameter newValue; |
65 | effect.addParameter(parameter: &newValue); |
66 | |
67 | // THEN |
68 | QCOMPARE(effect.parameters().size(), 1); |
69 | |
70 | // WHEN |
71 | effect.addParameter(parameter: &newValue); |
72 | |
73 | // THEN |
74 | QCOMPARE(effect.parameters().size(), 1); |
75 | |
76 | // WHEN |
77 | effect.removeParameter(parameter: &newValue); |
78 | |
79 | // THEN |
80 | QCOMPARE(effect.parameters().size(), 0); |
81 | } |
82 | { |
83 | // WHEN |
84 | Qt3DRender::QTechnique newValue; |
85 | effect.addTechnique(t: &newValue); |
86 | |
87 | // THEN |
88 | QCOMPARE(effect.techniques().size(), 1); |
89 | |
90 | // WHEN |
91 | effect.addTechnique(t: &newValue); |
92 | |
93 | // THEN |
94 | QCOMPARE(effect.techniques().size(), 1); |
95 | |
96 | // WHEN |
97 | effect.removeTechnique(t: &newValue); |
98 | |
99 | // THEN |
100 | QCOMPARE(effect.techniques().size(), 0); |
101 | } |
102 | } |
103 | |
104 | void checkCreationData() |
105 | { |
106 | // GIVEN |
107 | Qt3DRender::QEffect effect; |
108 | |
109 | Qt3DRender::QParameter parameter; |
110 | effect.addParameter(parameter: ¶meter); |
111 | Qt3DRender::QTechnique technique; |
112 | effect.addTechnique(t: &technique); |
113 | |
114 | // WHEN |
115 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
116 | |
117 | { |
118 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&effect); |
119 | creationChanges = creationChangeGenerator.creationChanges(); |
120 | } |
121 | |
122 | // THEN |
123 | { |
124 | QCOMPARE(creationChanges.size(), 3); // Effect + Parameter + Technique |
125 | |
126 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QEffectData>>(src: creationChanges.first()); |
127 | const Qt3DRender::QEffectData cloneData = creationChangeData->data; |
128 | |
129 | QCOMPARE(cloneData.parameterIds.size(), 1); |
130 | QCOMPARE(parameter.id(), cloneData.parameterIds.first()); |
131 | QCOMPARE(cloneData.techniqueIds.size(), 1); |
132 | QCOMPARE(technique.id(), cloneData.techniqueIds.first()); |
133 | QCOMPARE(effect.id(), creationChangeData->subjectId()); |
134 | QCOMPARE(effect.isEnabled(), true); |
135 | QCOMPARE(effect.isEnabled(), creationChangeData->isNodeEnabled()); |
136 | QCOMPARE(effect.metaObject(), creationChangeData->metaObject()); |
137 | } |
138 | |
139 | // WHEN |
140 | effect.setEnabled(false); |
141 | |
142 | { |
143 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&effect); |
144 | creationChanges = creationChangeGenerator.creationChanges(); |
145 | } |
146 | |
147 | // THEN |
148 | { |
149 | QCOMPARE(creationChanges.size(), 3); // Effect + Parameter + Technique |
150 | |
151 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QEffectData>>(src: creationChanges.first()); |
152 | const Qt3DRender::QEffectData cloneData = creationChangeData->data; |
153 | |
154 | QCOMPARE(cloneData.parameterIds.size(), 1); |
155 | QCOMPARE(parameter.id(), cloneData.parameterIds.first()); |
156 | QCOMPARE(cloneData.techniqueIds.size(), 1); |
157 | QCOMPARE(technique.id(), cloneData.techniqueIds.first()); |
158 | QCOMPARE(effect.id(), creationChangeData->subjectId()); |
159 | QCOMPARE(effect.isEnabled(), false); |
160 | QCOMPARE(effect.isEnabled(), creationChangeData->isNodeEnabled()); |
161 | QCOMPARE(effect.metaObject(), creationChangeData->metaObject()); |
162 | } |
163 | } |
164 | |
165 | void checkParameterBookkeeping() |
166 | { |
167 | // GIVEN |
168 | Qt3DRender::QEffect effect; |
169 | |
170 | { |
171 | // WHEN |
172 | Qt3DRender::QParameter parameter; |
173 | effect.addParameter(parameter: ¶meter); |
174 | |
175 | QCOMPARE(effect.parameters().size(), 1); |
176 | |
177 | } |
178 | |
179 | // THEN -> should not crash |
180 | QCOMPARE(effect.parameters().size(), 0); |
181 | |
182 | } |
183 | |
184 | void checkTechniqueBookkeeping() |
185 | { |
186 | // GIVEN |
187 | Qt3DRender::QEffect effect; |
188 | |
189 | { |
190 | // WHEN |
191 | Qt3DRender::QTechnique technique; |
192 | effect.addTechnique(t: &technique); |
193 | |
194 | QCOMPARE(effect.techniques().size(), 1); |
195 | |
196 | } |
197 | |
198 | // THEN -> should not crash |
199 | QCOMPARE(effect.techniques().size(), 0); |
200 | |
201 | } |
202 | |
203 | void checkParameterUpdate() |
204 | { |
205 | // GIVEN |
206 | TestArbiter arbiter; |
207 | Qt3DRender::QEffect effect; |
208 | Qt3DRender::QParameter parameter; |
209 | arbiter.setArbiterOnNode(&effect); |
210 | |
211 | { |
212 | // WHEN |
213 | effect.addParameter(parameter: ¶meter); |
214 | QCoreApplication::processEvents(); |
215 | |
216 | // THEN |
217 | QCOMPARE(arbiter.events.size(), 0); |
218 | QVERIFY(effect.parameters().contains(¶meter)); |
219 | } |
220 | |
221 | { |
222 | // WHEN |
223 | effect.removeParameter(parameter: ¶meter); |
224 | QCoreApplication::processEvents(); |
225 | |
226 | // THEN |
227 | QCOMPARE(arbiter.events.size(), 0); |
228 | QVERIFY(!effect.parameters().contains(¶meter)); |
229 | } |
230 | |
231 | } |
232 | |
233 | void checkTechniqueUpdate() |
234 | { |
235 | // GIVEN |
236 | TestArbiter arbiter; |
237 | Qt3DRender::QEffect effect; |
238 | Qt3DRender::QTechnique technique; |
239 | arbiter.setArbiterOnNode(&effect); |
240 | |
241 | { |
242 | // WHEN |
243 | effect.addTechnique(t: &technique); |
244 | QCoreApplication::processEvents(); |
245 | |
246 | // THEN |
247 | QCOMPARE(arbiter.events.size(), 0); |
248 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
249 | QCOMPARE(arbiter.dirtyNodes.front(), &effect); |
250 | arbiter.dirtyNodes.clear(); |
251 | } |
252 | |
253 | { |
254 | // WHEN |
255 | effect.removeTechnique(t: &technique); |
256 | QCoreApplication::processEvents(); |
257 | |
258 | // THEN |
259 | QCOMPARE(arbiter.events.size(), 0); |
260 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
261 | QCOMPARE(arbiter.dirtyNodes.front(), &effect); |
262 | arbiter.dirtyNodes.clear(); |
263 | } |
264 | |
265 | } |
266 | |
267 | }; |
268 | |
269 | QTEST_MAIN(tst_QEffect) |
270 | |
271 | #include "tst_qeffect.moc" |
272 | |