1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
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 <QSignalSpy>
31
32#include <Qt3DRender/qtexture.h>
33#include <Qt3DRender/qenvironmentlight.h>
34#include <Qt3DRender/private/qenvironmentlight_p.h>
35#include <Qt3DRender/private/qshaderdata_p.h>
36
37#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
38#include <Qt3DCore/qnodecreatedchange.h>
39#include "testpostmanarbiter.h"
40
41class tst_QEnvironmentLight: public QObject
42{
43 Q_OBJECT
44private Q_SLOTS:
45 void checkDefaultConstruction()
46 {
47 // GIVEN
48 Qt3DRender::QEnvironmentLight light;
49
50 // THEN
51 QVERIFY(light.findChild<Qt3DRender::QShaderData*>());
52 QCOMPARE(light.irradiance(), nullptr);
53 QCOMPARE(light.specular(), nullptr);
54 }
55
56 void shouldTakeOwnershipOfParentlessTextures()
57 {
58 // GIVEN
59 Qt3DRender::QEnvironmentLight light;
60 auto irradiance = new Qt3DRender::QTexture2D;
61 auto specular = new Qt3DRender::QTexture2D;
62
63 // WHEN
64 light.setIrradiance(irradiance);
65 light.setSpecular(specular);
66
67 // THEN
68 QCOMPARE(irradiance->parent(), &light);
69 QCOMPARE(specular->parent(), &light);
70 }
71
72 void shouldNotChangeOwnershipOfParentedTextures()
73 {
74 // GIVEN
75 Qt3DCore::QNode node;
76 Qt3DRender::QEnvironmentLight light;
77 auto irradiance = new Qt3DRender::QTexture2D(&node);
78 auto specular = new Qt3DRender::QTexture2D(&node);
79
80 // WHEN
81 light.setIrradiance(irradiance);
82 light.setSpecular(specular);
83
84 // WHEN
85 delete irradiance;
86 delete specular;
87
88 // THEN
89 }
90
91 void checkPropertyChanges()
92 {
93 // GIVEN
94 Qt3DRender::QEnvironmentLight light;
95 auto shaderData = light.findChild<Qt3DRender::QShaderData*>();
96
97 {
98 auto texture = new Qt3DRender::QTexture2D(&light);
99 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::irradianceChanged);
100
101 // WHEN
102 light.setIrradiance(texture);
103
104 // THEN
105 QCOMPARE(light.irradiance(), texture);
106 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), texture);
107 QCOMPARE(spy.count(), 1);
108 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
109
110 // WHEN
111 light.setIrradiance(texture);
112
113 // THEN
114 QCOMPARE(light.irradiance(), texture);
115 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), texture);
116 QCOMPARE(spy.count(), 0);
117
118 // WHEN
119 light.setIrradiance(nullptr);
120
121 // THEN
122 QCOMPARE(light.irradiance(), nullptr);
123 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), nullptr);
124 QCOMPARE(spy.count(), 1);
125 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
126 }
127 {
128 auto texture = new Qt3DRender::QTexture2D(&light);
129 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::irradianceChanged);
130
131 // WHEN
132 light.setIrradiance(texture);
133
134 // THEN
135 QCOMPARE(light.irradiance(), texture);
136 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), texture);
137 QCOMPARE(spy.count(), 1);
138 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
139
140 // WHEN
141 delete texture;
142
143 // THEN
144 QCOMPARE(light.irradiance(), nullptr);
145 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), nullptr);
146 QCOMPARE(spy.count(), 1);
147 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
148 }
149 {
150 auto texture = new Qt3DRender::QTexture2D;
151 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::irradianceChanged);
152
153 // WHEN
154 light.setIrradiance(texture);
155
156 // THEN
157 QCOMPARE(light.irradiance(), texture);
158 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), texture);
159 QCOMPARE(spy.count(), 1);
160 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
161
162 // WHEN
163 delete texture;
164
165 // THEN
166 QCOMPARE(light.irradiance(), nullptr);
167 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), nullptr);
168 QCOMPARE(spy.count(), 1);
169 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
170 }
171 {
172 auto texture = new Qt3DRender::QTexture2D(&light);
173 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::irradianceChanged);
174
175 // WHEN
176 light.setIrradiance(texture);
177
178 // THEN
179 QCOMPARE(light.irradiance(), texture);
180 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), texture);
181 QCOMPARE(shaderData->property("irradianceSize").value<QVector3D>(),
182 QVector3D(texture->width(), texture->height(), texture->depth()));
183 QCOMPARE(spy.count(), 1);
184 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
185
186 // WHEN
187 texture->setWidth(883);
188
189 // THEN
190 QCOMPARE(shaderData->property("irradianceSize").value<QVector3D>(),
191 QVector3D(883.0f, texture->height(), texture->depth()));
192
193 // WHEN
194 texture->setHeight(1340);
195
196 // THEN
197 QCOMPARE(shaderData->property("irradianceSize").value<QVector3D>(),
198 QVector3D(883.0f, 1340.0f, texture->depth()));
199
200 // WHEN
201 texture->setDepth(1584);
202
203 // THEN
204 QCOMPARE(shaderData->property("irradianceSize").value<QVector3D>(),
205 QVector3D(883.0f, 1340.0f, 1584.0f));
206
207 // WHEN
208 delete texture;
209
210 // THEN
211 QCOMPARE(light.irradiance(), nullptr);
212 QCOMPARE(shaderData->property("irradiance").value<Qt3DRender::QAbstractTexture*>(), nullptr);
213 QCOMPARE(spy.count(), 1);
214 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
215
216 // THEN
217 QCOMPARE(shaderData->property("irradianceSize").value<QVector3D>(),
218 QVector3D());
219 }
220 {
221 auto texture = new Qt3DRender::QTexture2D(&light);
222 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::specularChanged);
223
224 // WHEN
225 light.setSpecular(texture);
226
227 // THEN
228 QCOMPARE(light.specular(), texture);
229 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), texture);
230 QCOMPARE(spy.count(), 1);
231 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
232
233 // WHEN
234 light.setSpecular(texture);
235
236 // THEN
237 QCOMPARE(light.specular(), texture);
238 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), texture);
239 QCOMPARE(spy.count(), 0);
240
241 // WHEN
242 light.setSpecular(nullptr);
243
244 // THEN
245 QCOMPARE(light.specular(), nullptr);
246 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), nullptr);
247 QCOMPARE(spy.count(), 1);
248 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
249 }
250 {
251 auto texture = new Qt3DRender::QTexture2D(&light);
252 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::specularChanged);
253
254 // WHEN
255 light.setSpecular(texture);
256
257 // THEN
258 QCOMPARE(light.specular(), texture);
259 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), texture);
260 QCOMPARE(spy.count(), 1);
261 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
262
263 // WHEN
264 delete texture;
265
266 // THEN
267 QCOMPARE(light.specular(), nullptr);
268 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), nullptr);
269 QCOMPARE(spy.count(), 1);
270 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
271 }
272 {
273 auto texture = new Qt3DRender::QTexture2D;
274 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::specularChanged);
275
276 // WHEN
277 light.setSpecular(texture);
278
279 // THEN
280 QCOMPARE(light.specular(), texture);
281 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), texture);
282 QCOMPARE(spy.count(), 1);
283 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
284
285 // WHEN
286 delete texture;
287
288 // THEN
289 QCOMPARE(light.specular(), nullptr);
290 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), nullptr);
291 QCOMPARE(spy.count(), 1);
292 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
293 }
294 {
295 auto texture = new Qt3DRender::QTexture2D(&light);
296 QSignalSpy spy(&light, &Qt3DRender::QEnvironmentLight::specularChanged);
297
298 // WHEN
299 light.setSpecular(texture);
300
301 // THEN
302 QCOMPARE(light.specular(), texture);
303 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), texture);
304 QCOMPARE(shaderData->property("specularSize").value<QVector3D>(),
305 QVector3D(texture->width(), texture->height(), texture->depth()));
306 QCOMPARE(spy.count(), 1);
307 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), texture);
308
309 // WHEN
310 texture->setWidth(883);
311
312 // THEN
313 QCOMPARE(shaderData->property("specularSize").value<QVector3D>(),
314 QVector3D(883.0f, texture->height(), texture->depth()));
315
316 // WHEN
317 texture->setHeight(1340);
318
319 // THEN
320 QCOMPARE(shaderData->property("specularSize").value<QVector3D>(),
321 QVector3D(883.0f, 1340.0f, texture->depth()));
322
323 // WHEN
324 texture->setDepth(1584);
325
326 // THEN
327 QCOMPARE(shaderData->property("specularSize").value<QVector3D>(),
328 QVector3D(883.0f, 1340.0f, 1584.0f));
329
330 // WHEN
331 delete texture;
332
333 // THEN
334 QCOMPARE(light.specular(), nullptr);
335 QCOMPARE(shaderData->property("specular").value<Qt3DRender::QAbstractTexture*>(), nullptr);
336 QCOMPARE(spy.count(), 1);
337 QCOMPARE(spy.takeFirst().first().value<Qt3DRender::QAbstractTexture*>(), nullptr);
338
339 // THEN
340 QCOMPARE(shaderData->property("specularSize").value<QVector3D>(),
341 QVector3D());
342 }
343 }
344
345 void checkCreationData()
346 {
347 // GIVEN
348 Qt3DRender::QEnvironmentLight light;
349 auto shaderData = light.findChild<Qt3DRender::QShaderData*>();
350
351 // WHEN
352 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
353
354 {
355 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&light);
356 creationChanges = creationChangeGenerator.creationChanges();
357 }
358
359 // THEN
360 {
361 QCOMPARE(creationChanges.size(), 2); // EnvironmentLight + ShaderData
362
363 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QEnvironmentLightData>>(src: creationChanges.first());
364 const Qt3DRender::QEnvironmentLightData cloneData = creationChangeData->data;
365
366 QCOMPARE(cloneData.shaderDataId, shaderData->id());
367 QCOMPARE(light.id(), creationChangeData->subjectId());
368 QCOMPARE(light.isEnabled(), true);
369 QCOMPARE(light.isEnabled(), creationChangeData->isNodeEnabled());
370 QCOMPARE(light.metaObject(), creationChangeData->metaObject());
371 }
372
373 // WHEN
374 light.setEnabled(false);
375
376 {
377 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&light);
378 creationChanges = creationChangeGenerator.creationChanges();
379 }
380
381 // THEN
382 {
383 QCOMPARE(creationChanges.size(), 2); // EnvironmentLight + ShaderData
384
385 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QEnvironmentLightData>>(src: creationChanges.first());
386 const Qt3DRender::QEnvironmentLightData cloneData = creationChangeData->data;
387
388 QCOMPARE(cloneData.shaderDataId, shaderData->id());
389 QCOMPARE(light.id(), creationChangeData->subjectId());
390 QCOMPARE(light.isEnabled(), false);
391 QCOMPARE(light.isEnabled(), creationChangeData->isNodeEnabled());
392 QCOMPARE(light.metaObject(), creationChangeData->metaObject());
393 }
394 }
395};
396
397QTEST_MAIN(tst_QEnvironmentLight)
398
399#include "tst_qenvironmentlight.moc"
400

source code of qt3d/tests/auto/render/qenvironmentlight/tst_qenvironmentlight.cpp