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/qrenderpassfilter.h>
36#include <Qt3DRender/private/qrenderpassfilter_p.h>
37#include <Qt3DRender/qparameter.h>
38#include <Qt3DRender/qfilterkey.h>
39
40#include "testpostmanarbiter.h"
41
42class tst_QRenderPassFilter: public QObject
43{
44 Q_OBJECT
45
46private Q_SLOTS:
47
48 void checkSaneDefaults()
49 {
50 QScopedPointer<Qt3DRender::QRenderPassFilter> defaultRenderPassFilter(new Qt3DRender::QRenderPassFilter);
51
52 QCOMPARE(defaultRenderPassFilter->matchAny().count(), 0);
53 QCOMPARE(defaultRenderPassFilter->parameters().count(), 0);
54 }
55
56 void checkCloning_data()
57 {
58 QTest::addColumn<Qt3DRender::QRenderPassFilter *>(name: "renderPassFilter");
59 QTest::addColumn<QVector<Qt3DRender::QParameter *> >(name: "parameters");
60 QTest::addColumn<QVector<Qt3DRender::QFilterKey *> >(name: "filterKeys");
61
62 Qt3DRender::QRenderPassFilter *defaultConstructed = new Qt3DRender::QRenderPassFilter();
63 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed << QVector<Qt3DRender::QParameter *>() << QVector<Qt3DRender::QFilterKey *>();
64
65 Qt3DRender::QRenderPassFilter *renderPassFilterWithParams = new Qt3DRender::QRenderPassFilter();
66 Qt3DRender::QParameter *parameter1 = new Qt3DRender::QParameter(QStringLiteral("displacement"), 454.0f);
67 Qt3DRender::QParameter *parameter2 = new Qt3DRender::QParameter(QStringLiteral("torque"), 650);
68 QVector<Qt3DRender::QParameter *> params1 = QVector<Qt3DRender::QParameter *>() << parameter1 << parameter2;
69 renderPassFilterWithParams->addParameter(parameter: parameter1);
70 renderPassFilterWithParams->addParameter(parameter: parameter2);
71 QTest::newRow(dataTag: "renderPassFilterWithParams") << renderPassFilterWithParams << params1 << QVector<Qt3DRender::QFilterKey *>();
72
73 Qt3DRender::QRenderPassFilter *renderPassFilterWithAnnotations = new Qt3DRender::QRenderPassFilter();
74 Qt3DRender::QFilterKey *filterKey1 = new Qt3DRender::QFilterKey();
75 Qt3DRender::QFilterKey *filterKey2 = new Qt3DRender::QFilterKey();
76 filterKey1->setName(QStringLiteral("hasSuperCharger"));
77 filterKey1->setValue(true);
78 filterKey1->setName(QStringLiteral("hasNitroKit"));
79 filterKey1->setValue(false);
80 QVector<Qt3DRender::QFilterKey *> filterKeys1 = QVector<Qt3DRender::QFilterKey *>() << filterKey1 << filterKey2;
81 renderPassFilterWithAnnotations->addMatch(filterKey: filterKey1);
82 renderPassFilterWithAnnotations->addMatch(filterKey: filterKey2);
83 QTest::newRow(dataTag: "renderPassFilterWithAnnotations") << renderPassFilterWithAnnotations << QVector<Qt3DRender::QParameter *>() << filterKeys1;
84
85 Qt3DRender::QRenderPassFilter *renderPassFilterWithParamsAndAnnotations = new Qt3DRender::QRenderPassFilter();
86 Qt3DRender::QParameter *parameter3 = new Qt3DRender::QParameter(QStringLiteral("displacement"), 383.0f);
87 Qt3DRender::QParameter *parameter4 = new Qt3DRender::QParameter(QStringLiteral("torque"), 555);
88 Qt3DRender::QFilterKey *filterKey3 = new Qt3DRender::QFilterKey();
89 Qt3DRender::QFilterKey *filterKey4 = new Qt3DRender::QFilterKey();
90 filterKey3->setName(QStringLiteral("hasSuperCharger"));
91 filterKey3->setValue(false);
92 filterKey4->setName(QStringLiteral("hasNitroKit"));
93 filterKey4->setValue(true);
94 QVector<Qt3DRender::QParameter *> params2 = QVector<Qt3DRender::QParameter *>() << parameter3 << parameter4;
95 QVector<Qt3DRender::QFilterKey *> filterKeys2 = QVector<Qt3DRender::QFilterKey *>() << filterKey3 << filterKey4;
96 renderPassFilterWithParamsAndAnnotations->addParameter(parameter: parameter3);
97 renderPassFilterWithParamsAndAnnotations->addParameter(parameter: parameter4);
98 renderPassFilterWithParamsAndAnnotations->addMatch(filterKey: filterKey3);
99 renderPassFilterWithParamsAndAnnotations->addMatch(filterKey: filterKey4);
100 QTest::newRow(dataTag: "renderPassFilterWithParamsAndAnnotations") << renderPassFilterWithParamsAndAnnotations << params2 << filterKeys2 ;
101 }
102
103 // TODO: Avoid cloning here
104 void checkCloning()
105 {
106 // GIVEN
107 QFETCH(Qt3DRender::QRenderPassFilter*, renderPassFilter);
108 QFETCH(QVector<Qt3DRender::QParameter *>, parameters);
109 QFETCH(QVector<Qt3DRender::QFilterKey *>, filterKeys);
110
111 // THEN
112 QCOMPARE(renderPassFilter->parameters(), parameters);
113 QCOMPARE(renderPassFilter->matchAny(), filterKeys);
114
115 // WHEN
116 // WHEN
117 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(renderPassFilter);
118 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
119
120 // THEN
121 QCOMPARE(creationChanges.size(), 1 + renderPassFilter->parameters().size() + renderPassFilter->matchAny().size());
122
123 const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QRenderPassFilterData> creationChangeData =
124 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderPassFilterData>>(src: creationChanges.first());
125 const Qt3DRender::QRenderPassFilterData &cloneData = creationChangeData->data;
126
127 // THEN
128 QCOMPARE(renderPassFilter->id(), creationChangeData->subjectId());
129 QCOMPARE(renderPassFilter->isEnabled(), creationChangeData->isNodeEnabled());
130 QCOMPARE(renderPassFilter->metaObject(), creationChangeData->metaObject());
131
132 QCOMPARE(renderPassFilter->matchAny().count(), cloneData.matchIds.count());
133 QCOMPARE(renderPassFilter->parameters().count(), cloneData.parameterIds.count());
134
135 // TO DO: Add unit tests for QParameter / QFilterKey
136
137 delete renderPassFilter;
138 }
139
140 void checkPropertyUpdates()
141 {
142 // GIVEN
143 TestArbiter arbiter;
144 QScopedPointer<Qt3DRender::QRenderPassFilter> renderPassFilter(new Qt3DRender::QRenderPassFilter());
145 arbiter.setArbiterOnNode(renderPassFilter.data());
146
147 // WHEN
148 Qt3DRender::QParameter *param1 = new Qt3DRender::QParameter();
149 renderPassFilter->addParameter(parameter: param1);
150 QCoreApplication::processEvents();
151
152 // THEN
153 QCOMPARE(arbiter.events.size(), 0);
154 QCOMPARE(arbiter.dirtyNodes.size(), 1);
155 QVERIFY(arbiter.dirtyNodes.contains(renderPassFilter.data()));
156
157 arbiter.dirtyNodes.clear();
158
159 // WHEN
160 renderPassFilter->addParameter(parameter: param1);
161 QCoreApplication::processEvents();
162
163 // THEN
164 QCOMPARE(arbiter.events.size(), 0);
165 QCOMPARE(arbiter.dirtyNodes.size(), 0);
166
167 // WHEN
168 renderPassFilter->removeParameter(parameter: param1);
169 QCoreApplication::processEvents();
170
171 // THEN
172 QCOMPARE(arbiter.events.size(), 0);
173 QCOMPARE(arbiter.dirtyNodes.size(), 1);
174 QVERIFY(arbiter.dirtyNodes.contains(renderPassFilter.data()));
175
176 arbiter.dirtyNodes.clear();
177
178 // WHEN
179 Qt3DRender::QFilterKey *filterKey1 = new Qt3DRender::QFilterKey();
180 renderPassFilter->addMatch(filterKey: filterKey1);
181 QCoreApplication::processEvents();
182
183 // THEN
184 QCOMPARE(arbiter.events.size(), 0);
185 QCOMPARE(arbiter.dirtyNodes.size(), 1);
186 QVERIFY(arbiter.dirtyNodes.contains(renderPassFilter.data()));
187
188 arbiter.dirtyNodes.clear();
189
190 // WHEN
191 renderPassFilter->addMatch(filterKey: filterKey1);
192 QCoreApplication::processEvents();
193
194 // THEN
195 QCOMPARE(arbiter.events.size(), 0);
196 QCOMPARE(arbiter.dirtyNodes.size(), 0);
197
198 // WHEN
199 renderPassFilter->removeMatch(filterKey: filterKey1);
200 QCoreApplication::processEvents();
201
202 // THEN
203 QCOMPARE(arbiter.events.size(), 0);
204 QCOMPARE(arbiter.dirtyNodes.size(), 1);
205 QVERIFY(arbiter.dirtyNodes.contains(renderPassFilter.data()));
206
207 arbiter.dirtyNodes.clear();
208 }
209
210 void checkParameterBookkeeping()
211 {
212 // GIVEN
213 QScopedPointer<Qt3DRender::QRenderPassFilter> passFilter(new Qt3DRender::QRenderPassFilter);
214 {
215 // WHEN
216 Qt3DRender::QParameter param;
217 passFilter->addParameter(parameter: &param);
218
219 // THEN
220 QCOMPARE(param.parent(), passFilter.data());
221 QCOMPARE(passFilter->parameters().size(), 1);
222 }
223 // THEN (Should not crash and parameter be unset)
224 QVERIFY(passFilter->parameters().empty());
225
226 {
227 // WHEN
228 Qt3DRender::QRenderPassFilter someOtherPassFilter;
229 QScopedPointer<Qt3DRender::QParameter> param(new Qt3DRender::QParameter(&someOtherPassFilter));
230 passFilter->addParameter(parameter: param.data());
231
232 // THEN
233 QCOMPARE(param->parent(), &someOtherPassFilter);
234 QCOMPARE(passFilter->parameters().size(), 1);
235
236 // WHEN
237 passFilter.reset();
238 param.reset();
239
240 // THEN Should not crash when the parameter is destroyed (tests for failed removal of destruction helper)
241 }
242 }
243
244 void checkFilterKeyBookkeeping()
245 {
246 // GIVEN
247 QScopedPointer<Qt3DRender::QRenderPassFilter> passFilter(new Qt3DRender::QRenderPassFilter);
248 {
249 // WHEN
250 Qt3DRender::QFilterKey filterKey;
251 passFilter->addMatch(filterKey: &filterKey);
252
253 // THEN
254 QCOMPARE(filterKey.parent(), passFilter.data());
255 QCOMPARE(passFilter->matchAny().size(), 1);
256 }
257 // THEN (Should not crash and parameter be unset)
258 QVERIFY(passFilter->matchAny().empty());
259
260 {
261 // WHEN
262 Qt3DRender::QRenderPassFilter someOtherPassFilter;
263 QScopedPointer<Qt3DRender::QFilterKey> filterKey(new Qt3DRender::QFilterKey(&someOtherPassFilter));
264 passFilter->addMatch(filterKey: filterKey.data());
265
266 // THEN
267 QCOMPARE(filterKey->parent(), &someOtherPassFilter);
268 QCOMPARE(passFilter->matchAny().size(), 1);
269
270 // WHEN
271 passFilter.reset();
272 filterKey.reset();
273
274 // THEN Should not crash when the parameter is destroyed (tests for failed removal of destruction helper)
275 }
276 }
277};
278
279QTEST_MAIN(tst_QRenderPassFilter)
280
281#include "tst_qrenderpassfilter.moc"
282

source code of qt3d/tests/auto/render/qrenderpassfilter/tst_qrenderpassfilter.cpp