1/****************************************************************************
2**
3** Copyright (C) 2016 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 <Qt3DRender/qrendersettings.h>
32#include <Qt3DRender/qviewport.h>
33#include <Qt3DRender/private/qrendersettings_p.h>
34#include <QObject>
35#include <QSignalSpy>
36#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
37#include <Qt3DCore/qnodecreatedchange.h>
38#include "testpostmanarbiter.h"
39
40class tst_QRenderSettings : public QObject
41{
42 Q_OBJECT
43
44private Q_SLOTS:
45
46 void initTestCase()
47 {
48 qRegisterMetaType<Qt3DRender::QRenderSettings::RenderPolicy >(typeName: "RenderPolicy");
49 qRegisterMetaType<Qt3DRender::QPickingSettings::PickMethod>(typeName: "QPickingSettings::PickMethod");
50 qRegisterMetaType<Qt3DRender::QPickingSettings::PickResultMode>(typeName: "QPickingSettings::PickResultMode");
51 qRegisterMetaType<Qt3DRender::QPickingSettings::FaceOrientationPickingMode>(typeName: "QPickingSettings::FaceOrientationPickingMode");
52 qRegisterMetaType<Qt3DRender::QFrameGraphNode*>(typeName: "QFrameGraphNode *");
53 }
54
55 void checkDefaultConstruction()
56 {
57 // GIVEN
58 Qt3DRender::QRenderSettings renderSettings;
59
60 // THEN
61 QVERIFY(renderSettings.pickingSettings() != nullptr);
62 QCOMPARE(renderSettings.renderPolicy(), Qt3DRender::QRenderSettings::Always);
63 QVERIFY(renderSettings.activeFrameGraph() == nullptr);
64 QCOMPARE(renderSettings.pickingSettings()->pickMethod(), Qt3DRender::QPickingSettings::BoundingVolumePicking);
65 QCOMPARE(renderSettings.pickingSettings()->pickResultMode(), Qt3DRender::QPickingSettings::NearestPick);
66 QCOMPARE(renderSettings.pickingSettings()->faceOrientationPickingMode(), Qt3DRender::QPickingSettings::FrontFace);
67 }
68
69 void checkPropertyChanges()
70 {
71 // GIVEN
72 Qt3DRender::QRenderSettings renderSettings;
73 Qt3DRender::QPickingSettings *pickingSettings = renderSettings.pickingSettings();
74
75 {
76 // WHEN
77 QSignalSpy spy(&renderSettings, SIGNAL(renderPolicyChanged(RenderPolicy)));
78 const Qt3DRender::QRenderSettings::RenderPolicy newValue = Qt3DRender::QRenderSettings::OnDemand;
79 renderSettings.setRenderPolicy(newValue);
80
81 // THEN
82 QVERIFY(spy.isValid());
83 QCOMPARE(renderSettings.renderPolicy(), newValue);
84 QCOMPARE(spy.count(), 1);
85
86 // WHEN
87 spy.clear();
88 renderSettings.setRenderPolicy(newValue);
89
90 // THEN
91 QCOMPARE(renderSettings.renderPolicy(), newValue);
92 QCOMPARE(spy.count(), 0);
93 }
94 {
95 // WHEN
96 QSignalSpy spy(&renderSettings, SIGNAL(activeFrameGraphChanged(QFrameGraphNode *)));
97
98 Qt3DRender::QViewport newValue;
99 renderSettings.setActiveFrameGraph(&newValue);
100
101 // THEN
102 QVERIFY(spy.isValid());
103 QCOMPARE(renderSettings.activeFrameGraph(), &newValue);
104 QCOMPARE(spy.count(), 1);
105
106 // WHEN
107 spy.clear();
108 renderSettings.setActiveFrameGraph(&newValue);
109
110 // THEN
111 QCOMPARE(renderSettings.activeFrameGraph(), &newValue);
112 QCOMPARE(spy.count(), 0);
113 }
114 {
115 // WHEN
116 QSignalSpy spy(pickingSettings, SIGNAL(pickMethodChanged(QPickingSettings::PickMethod)));
117 const Qt3DRender::QPickingSettings::PickMethod newValue = Qt3DRender::QPickingSettings::TrianglePicking;
118 pickingSettings->setPickMethod(newValue);
119
120 // THEN
121 QVERIFY(spy.isValid());
122 QCOMPARE(pickingSettings->pickMethod(), newValue);
123 QCOMPARE(spy.count(), 1);
124
125 // WHEN
126 spy.clear();
127 pickingSettings->setPickMethod(newValue);
128
129 // THEN
130 QCOMPARE(pickingSettings->pickMethod(), newValue);
131 QCOMPARE(spy.count(), 0);
132 }
133 {
134 // WHEN
135 QSignalSpy spy(pickingSettings, SIGNAL(pickResultModeChanged(QPickingSettings::PickResultMode)));
136 const Qt3DRender::QPickingSettings::PickResultMode newValue = Qt3DRender::QPickingSettings::AllPicks;
137 pickingSettings->setPickResultMode(newValue);
138
139 // THEN
140 QVERIFY(spy.isValid());
141 QCOMPARE(pickingSettings->pickResultMode(), newValue);
142 QCOMPARE(spy.count(), 1);
143
144 // WHEN
145 spy.clear();
146 pickingSettings->setPickResultMode(newValue);
147
148 // THEN
149 QCOMPARE(pickingSettings->pickResultMode(), newValue);
150 QCOMPARE(spy.count(), 0);
151 }
152 {
153 // WHEN
154 QSignalSpy spy(pickingSettings, SIGNAL(faceOrientationPickingModeChanged(QPickingSettings::FaceOrientationPickingMode)));
155 const Qt3DRender::QPickingSettings::FaceOrientationPickingMode newValue = Qt3DRender::QPickingSettings::FrontAndBackFace;
156 pickingSettings->setFaceOrientationPickingMode(newValue);
157
158 // THEN
159 QVERIFY(spy.isValid());
160 QCOMPARE(pickingSettings->faceOrientationPickingMode(), newValue);
161 QCOMPARE(spy.count(), 1);
162
163 // WHEN
164 spy.clear();
165 pickingSettings->setFaceOrientationPickingMode(newValue);
166
167 // THEN
168 QCOMPARE(pickingSettings->faceOrientationPickingMode(), newValue);
169 QCOMPARE(spy.count(), 0);
170 }
171
172 }
173
174 void checkCreationData()
175 {
176 // GIVEN
177 Qt3DRender::QRenderSettings renderSettings;
178 Qt3DRender::QPickingSettings *pickingSettings = renderSettings.pickingSettings();
179 Qt3DRender::QViewport frameGraphRoot;
180
181 renderSettings.setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
182 renderSettings.setActiveFrameGraph(&frameGraphRoot);
183 pickingSettings->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
184 pickingSettings->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
185 pickingSettings->setFaceOrientationPickingMode(Qt3DRender::QPickingSettings::FrontAndBackFace);
186 pickingSettings->setWorldSpaceTolerance(5.f);
187
188 // WHEN
189 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
190
191 {
192 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderSettings);
193 creationChanges = creationChangeGenerator.creationChanges();
194 }
195
196 // THEN
197 {
198 QCOMPARE(creationChanges.size(), 2); // RenderSettings + PickingSettings (because it's a QNode)
199
200 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderSettingsData>>(src: creationChanges.first());
201 const Qt3DRender::QRenderSettingsData cloneData = creationChangeData->data;
202
203 QCOMPARE(renderSettings.pickingSettings()->pickMethod(), cloneData.pickMethod);
204 QCOMPARE(renderSettings.pickingSettings()->pickResultMode(), cloneData.pickResultMode);
205 QCOMPARE(renderSettings.pickingSettings()->faceOrientationPickingMode(), cloneData.faceOrientationPickingMode);
206 QCOMPARE(renderSettings.pickingSettings()->worldSpaceTolerance(), cloneData.pickWorldSpaceTolerance);
207 QCOMPARE(renderSettings.renderPolicy(), cloneData.renderPolicy);
208 QCOMPARE(renderSettings.activeFrameGraph()->id(), cloneData.activeFrameGraphId);
209 QCOMPARE(renderSettings.id(), creationChangeData->subjectId());
210 QCOMPARE(renderSettings.isEnabled(), true);
211 QCOMPARE(renderSettings.isEnabled(), creationChangeData->isNodeEnabled());
212 QCOMPARE(renderSettings.metaObject(), creationChangeData->metaObject());
213 }
214
215 // WHEN
216 renderSettings.setEnabled(false);
217
218 {
219 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderSettings);
220 creationChanges = creationChangeGenerator.creationChanges();
221 }
222
223 // THEN
224 {
225 QCOMPARE(creationChanges.size(), 2); // RenderSettings + PickingSettings (because it's a QNode)
226
227 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderSettingsData>>(src: creationChanges.first());
228 const Qt3DRender::QRenderSettingsData cloneData = creationChangeData->data;
229
230 QCOMPARE(renderSettings.pickingSettings()->pickMethod(), cloneData.pickMethod);
231 QCOMPARE(renderSettings.pickingSettings()->pickResultMode(), cloneData.pickResultMode);
232 QCOMPARE(renderSettings.pickingSettings()->faceOrientationPickingMode(), cloneData.faceOrientationPickingMode);
233 QCOMPARE(renderSettings.renderPolicy(), cloneData.renderPolicy);
234 QCOMPARE(renderSettings.activeFrameGraph()->id(), cloneData.activeFrameGraphId);
235 QCOMPARE(renderSettings.id(), creationChangeData->subjectId());
236 QCOMPARE(renderSettings.isEnabled(), false);
237 QCOMPARE(renderSettings.isEnabled(), creationChangeData->isNodeEnabled());
238 QCOMPARE(renderSettings.metaObject(), creationChangeData->metaObject());
239 }
240 }
241
242 void checkRenderPolicyUpdate()
243 {
244 // GIVEN
245 TestArbiter arbiter;
246 Qt3DRender::QRenderSettings renderSettings;
247 arbiter.setArbiterOnNode(&renderSettings);
248
249 {
250 // WHEN
251 renderSettings.setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
252 QCoreApplication::processEvents();
253
254 // THEN
255 QCOMPARE(arbiter.events.size(), 0);
256 QCOMPARE(arbiter.dirtyNodes.size(), 1);
257 QCOMPARE(arbiter.dirtyNodes.front(), &renderSettings);
258
259 arbiter.dirtyNodes.clear();
260 }
261
262 {
263 // WHEN
264 renderSettings.setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
265 QCoreApplication::processEvents();
266
267 // THEN
268 QCOMPARE(arbiter.events.size(), 0);
269 QCOMPARE(arbiter.dirtyNodes.size(), 0);
270 }
271
272 }
273
274 void checkActiveFrameGraphUpdate()
275 {
276 // GIVEN
277 TestArbiter arbiter;
278 Qt3DRender::QRenderSettings renderSettings;
279 arbiter.setArbiterOnNode(&renderSettings);
280 Qt3DRender::QViewport viewport;
281
282 {
283 // WHEN
284 renderSettings.setActiveFrameGraph(&viewport);
285 QCoreApplication::processEvents();
286
287 // THEN
288 QCOMPARE(arbiter.events.size(), 0);
289 QCOMPARE(arbiter.dirtyNodes.size(), 1);
290 QCOMPARE(arbiter.dirtyNodes.front(), &renderSettings);
291
292 arbiter.dirtyNodes.clear();
293 }
294
295 {
296 // WHEN
297 renderSettings.setActiveFrameGraph(&viewport);
298 QCoreApplication::processEvents();
299
300 // THEN
301 QCOMPARE(arbiter.events.size(), 0);
302 QCOMPARE(arbiter.dirtyNodes.size(), 0);
303 }
304
305 }
306
307 void checkPickMethodUpdate()
308 {
309 // GIVEN
310 TestArbiter arbiter;
311 Qt3DRender::QRenderSettings renderSettings;
312 Qt3DRender::QPickingSettings *pickingSettings = renderSettings.pickingSettings();
313
314 arbiter.setArbiterOnNode(&renderSettings);
315
316 {
317 // WHEN
318 pickingSettings->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
319 QCoreApplication::processEvents();
320
321 // THEN
322 QCOMPARE(arbiter.events.size(), 0);
323 QCOMPARE(arbiter.dirtyNodes.size(), 1);
324 QCOMPARE(arbiter.dirtyNodes.front(), &renderSettings);
325
326 arbiter.dirtyNodes.clear();
327 }
328
329 {
330 // WHEN
331 pickingSettings->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
332 QCoreApplication::processEvents();
333
334 // THEN
335 QCOMPARE(arbiter.events.size(), 0);
336 QCOMPARE(arbiter.dirtyNodes.size(), 0);
337 }
338
339 }
340
341 void checkPickResultModeUpdate()
342 {
343 // GIVEN
344 TestArbiter arbiter;
345 Qt3DRender::QRenderSettings renderSettings;
346 Qt3DRender::QPickingSettings *pickingSettings = renderSettings.pickingSettings();
347
348 arbiter.setArbiterOnNode(&renderSettings);
349
350 {
351 // WHEN
352 pickingSettings->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
353 QCoreApplication::processEvents();
354
355 // THEN
356 QCOMPARE(arbiter.events.size(), 0);
357 QCOMPARE(arbiter.dirtyNodes.size(), 1);
358 QCOMPARE(arbiter.dirtyNodes.front(), &renderSettings);
359
360 arbiter.dirtyNodes.clear();
361 }
362
363 {
364 // WHEN
365 pickingSettings->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
366 QCoreApplication::processEvents();
367
368 // THEN
369 QCOMPARE(arbiter.events.size(), 0);
370 QCOMPARE(arbiter.dirtyNodes.size(), 0);
371 }
372
373 }
374
375 void checkFaceOrientationPickingModeUpdate()
376 {
377 // GIVEN
378 TestArbiter arbiter;
379 Qt3DRender::QRenderSettings renderSettings;
380 Qt3DRender::QPickingSettings *pickingSettings = renderSettings.pickingSettings();
381
382 arbiter.setArbiterOnNode(&renderSettings);
383
384 {
385 // WHEN
386 pickingSettings->setFaceOrientationPickingMode(Qt3DRender::QPickingSettings::FrontAndBackFace);
387 QCoreApplication::processEvents();
388
389 // THEN
390 QCOMPARE(arbiter.events.size(), 0);
391 QCOMPARE(arbiter.dirtyNodes.size(), 1);
392 QCOMPARE(arbiter.dirtyNodes.front(), &renderSettings);
393
394 arbiter.dirtyNodes.clear();
395 }
396
397 {
398 // WHEN
399 pickingSettings->setFaceOrientationPickingMode(Qt3DRender::QPickingSettings::FrontAndBackFace);
400 QCoreApplication::processEvents();
401
402 // THEN
403 QCOMPARE(arbiter.events.size(), 0);
404 QCOMPARE(arbiter.dirtyNodes.size(), 0);
405 }
406
407 }
408
409 void checkWorldSpaceToleranceUpdate()
410 {
411 // GIVEN
412 TestArbiter arbiter;
413 Qt3DRender::QRenderSettings renderSettings;
414 Qt3DRender::QPickingSettings *pickingSettings = renderSettings.pickingSettings();
415
416 arbiter.setArbiterOnNode(&renderSettings);
417
418 {
419 // WHEN
420 pickingSettings->setWorldSpaceTolerance(5.f);
421 QCoreApplication::processEvents();
422
423 // THEN
424 QCOMPARE(arbiter.events.size(), 0);
425 QCOMPARE(arbiter.dirtyNodes.size(), 1);
426 QCOMPARE(arbiter.dirtyNodes.front(), &renderSettings);
427
428 arbiter.dirtyNodes.clear();
429 }
430
431 {
432 // WHEN
433 pickingSettings->setWorldSpaceTolerance(5.f);
434 QCoreApplication::processEvents();
435
436 // THEN
437 QCOMPARE(arbiter.events.size(), 0);
438 QCOMPARE(arbiter.dirtyNodes.size(), 0);
439 }
440
441 }
442
443};
444
445QTEST_MAIN(tst_QRenderSettings)
446
447#include "tst_qrendersettings.moc"
448

source code of qt3d/tests/auto/render/qrendersettings/tst_qrendersettings.cpp