1/****************************************************************************
2**
3** Copyright (C) 2015 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#include <QtTest/QTest>
30#include <Qt3DCore/private/qnode_p.h>
31#include <Qt3DCore/private/qscene_p.h>
32#include <Qt3DCore/qentity.h>
33#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
34
35#include <Qt3DRender/qrendertargetselector.h>
36#include <Qt3DRender/private/qrendertargetselector_p.h>
37#include <Qt3DRender/qrendertarget.h>
38#include "testpostmanarbiter.h"
39
40class tst_QRenderTargetSelector: public QObject
41{
42 Q_OBJECT
43
44private Q_SLOTS:
45
46 void checkSaneDefaults()
47 {
48 QScopedPointer<Qt3DRender::QRenderTargetSelector> defaultTargetSelector(new Qt3DRender::QRenderTargetSelector);
49
50 QVERIFY(defaultTargetSelector->target() == nullptr);
51 QCOMPARE(defaultTargetSelector->outputs().count(), 0);
52 }
53
54 void checkCloning_data()
55 {
56 QTest::addColumn<Qt3DRender::QRenderTargetSelector *>(name: "renderTargetSelector");
57 QTest::addColumn<QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint> >(name: "outputs");
58 QTest::addColumn<Qt3DRender::QRenderTarget * >(name: "target");
59
60 Qt3DRender::QRenderTargetSelector *defaultConstructed = new Qt3DRender::QRenderTargetSelector();
61 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed << QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint>() << static_cast<Qt3DRender::QRenderTarget *>(nullptr);
62
63 Qt3DRender::QRenderTargetSelector *renderTargetSelectorWithTarget = new Qt3DRender::QRenderTargetSelector();
64 Qt3DRender::QRenderTarget *target1 = new Qt3DRender::QRenderTarget();
65 renderTargetSelectorWithTarget->setTarget(target1);
66 QTest::newRow(dataTag: "renderTargetSelectorWithTarget") << renderTargetSelectorWithTarget << QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint>() << target1;
67
68 Qt3DRender::QRenderTargetSelector *renderTargetSelectorWithTargetAndBuffers = new Qt3DRender::QRenderTargetSelector();
69 Qt3DRender::QRenderTarget *target2 = new Qt3DRender::QRenderTarget();
70 renderTargetSelectorWithTargetAndBuffers->setTarget(target2);
71 QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint> attachmentPoints = QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint>() << Qt3DRender::QRenderTargetOutput::Color0 << Qt3DRender::QRenderTargetOutput::Depth;
72 renderTargetSelectorWithTargetAndBuffers->setOutputs(attachmentPoints);
73 QTest::newRow(dataTag: "renderTargetSelectorWithTargetAndDrawBuffers") << renderTargetSelectorWithTargetAndBuffers << attachmentPoints << target2;
74 }
75
76 void checkCloning()
77 {
78 // GIVEN
79 QFETCH(Qt3DRender::QRenderTargetSelector*, renderTargetSelector);
80 QFETCH(QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint>, outputs);
81 QFETCH(Qt3DRender::QRenderTarget *, target);
82
83 // THEN
84 QCOMPARE(renderTargetSelector->outputs(), outputs);
85 QCOMPARE(renderTargetSelector->target(), target);
86
87 // WHEN
88 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(renderTargetSelector);
89 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
90
91 // THEN
92 QCOMPARE(creationChanges.size(), 1 + (renderTargetSelector->target() ? 1 : 0));
93
94 const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QRenderTargetSelectorData> creationChangeData =
95 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderTargetSelectorData>>(src: creationChanges.first());
96 const Qt3DRender::QRenderTargetSelectorData &cloneData = creationChangeData->data;
97
98 // THEN
99 QCOMPARE(renderTargetSelector->id(), creationChangeData->subjectId());
100 QCOMPARE(renderTargetSelector->isEnabled(), creationChangeData->isNodeEnabled());
101 QCOMPARE(renderTargetSelector->metaObject(), creationChangeData->metaObject());
102 QCOMPARE(renderTargetSelector->target() ? renderTargetSelector->target()->id() : Qt3DCore::QNodeId(), cloneData.targetId);
103
104 delete renderTargetSelector;
105 }
106
107 void checkPropertyUpdates()
108 {
109 // GIVEN
110 TestArbiter arbiter;
111 QScopedPointer<Qt3DRender::QRenderTargetSelector> renderTargetSelector(new Qt3DRender::QRenderTargetSelector());
112 arbiter.setArbiterOnNode(renderTargetSelector.data());
113
114 // WHEN
115 Qt3DRender::QRenderTarget *target = new Qt3DRender::QRenderTarget();
116 renderTargetSelector->setTarget(target);
117 QCoreApplication::processEvents();
118
119 // THEN
120 QCOMPARE(arbiter.events.size(), 0);
121 QCOMPARE(arbiter.dirtyNodes.size(), 1);
122 QCOMPARE(arbiter.dirtyNodes.front(), renderTargetSelector.data());
123
124 arbiter.dirtyNodes.clear();
125
126 // WHEN
127 renderTargetSelector->setTarget(target);
128 QCoreApplication::processEvents();
129
130 // THEN
131 QCOMPARE(arbiter.events.size(), 0);
132 QCOMPARE(arbiter.dirtyNodes.size(), 0);
133
134 // WHEN
135 renderTargetSelector->setTarget(nullptr);
136 QCoreApplication::processEvents();
137
138 // THEN
139 QCOMPARE(arbiter.events.size(), 0);
140 QCOMPARE(arbiter.dirtyNodes.size(), 1);
141 QCOMPARE(arbiter.dirtyNodes.front(), renderTargetSelector.data());
142
143 arbiter.dirtyNodes.clear();
144
145 // WHEN
146 QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint> outputs;
147 outputs << Qt3DRender::QRenderTargetOutput::Color0 << Qt3DRender::QRenderTargetOutput::Depth;
148 renderTargetSelector->setOutputs(outputs);
149 QCoreApplication::processEvents();
150
151 // THEN
152 QCOMPARE(arbiter.events.size(), 0);
153 QCOMPARE(arbiter.dirtyNodes.size(), 1);
154 QCOMPARE(arbiter.dirtyNodes.front(), renderTargetSelector.data());
155
156 arbiter.dirtyNodes.clear();
157
158 // WHEN
159 renderTargetSelector->setOutputs(outputs);
160 QCoreApplication::processEvents();
161
162 // THEN
163 QCOMPARE(arbiter.events.size(), 0);
164 QCOMPARE(arbiter.dirtyNodes.size(), 0);
165
166 // WHEN
167 renderTargetSelector->setOutputs(QVector<Qt3DRender::QRenderTargetOutput::AttachmentPoint>());
168 QCoreApplication::processEvents();
169
170 // THEN
171 QCOMPARE(arbiter.events.size(), 0);
172 QCOMPARE(arbiter.dirtyNodes.size(), 1);
173 QCOMPARE(arbiter.dirtyNodes.front(), renderTargetSelector.data());
174
175 arbiter.dirtyNodes.clear();
176 }
177
178 void checkRenderTargetBookkeeping()
179 {
180 // GIVEN
181 QScopedPointer<Qt3DRender::QRenderTargetSelector> targetSelector(new Qt3DRender::QRenderTargetSelector);
182 {
183 // WHEN
184 Qt3DRender::QRenderTarget target;
185 targetSelector->setTarget(&target);
186
187 // THEN
188 QCOMPARE(target.parent(), targetSelector.data());
189 QCOMPARE(targetSelector->target(), &target);
190 }
191 // THEN (Should not crash and effect be unset)
192 QVERIFY(targetSelector->target() == nullptr);
193
194 {
195 // WHEN
196 Qt3DRender::QRenderTargetSelector someOtherTargetSelector;
197 QScopedPointer<Qt3DRender::QRenderTarget> target(new Qt3DRender::QRenderTarget(&someOtherTargetSelector));
198 targetSelector->setTarget(target.data());
199
200 // THEN
201 QCOMPARE(target->parent(), &someOtherTargetSelector);
202 QCOMPARE(targetSelector->target(), target.data());
203
204 // WHEN
205 targetSelector.reset();
206 target.reset();
207
208 // THEN Should not crash when the target is destroyed (tests for failed removal of destruction helper)
209 }
210 }
211};
212
213QTEST_MAIN(tst_QRenderTargetSelector)
214
215#include "tst_qrendertargetselector.moc"
216

source code of qt3d/tests/auto/render/qrendertargetselector/tst_qrendertargetselector.cpp