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/private/qnodecreatedchangegenerator_p.h> |
33 | |
34 | #include <Qt3DRender/qlayer.h> |
35 | #include <Qt3DRender/qlayerfilter.h> |
36 | #include <Qt3DRender/private/qlayerfilter_p.h> |
37 | |
38 | #include "testpostmanarbiter.h" |
39 | |
40 | class tst_QLayerFilter: public QObject |
41 | { |
42 | Q_OBJECT |
43 | public: |
44 | tst_QLayerFilter() |
45 | : QObject() |
46 | { |
47 | qRegisterMetaType<Qt3DCore::QNode*>(); |
48 | } |
49 | |
50 | private Q_SLOTS: |
51 | |
52 | void checkCloning_data() |
53 | { |
54 | QTest::addColumn<Qt3DRender::QLayerFilter *>(name: "layerFilter" ); |
55 | QTest::addColumn<QVector<Qt3DRender::QLayer*>>(name: "layers" ); |
56 | |
57 | |
58 | Qt3DRender::QLayerFilter *defaultConstructed = new Qt3DRender::QLayerFilter(); |
59 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed << QVector<Qt3DRender::QLayer*>(); |
60 | |
61 | Qt3DRender::QLayerFilter *singleLayer = new Qt3DRender::QLayerFilter(); |
62 | auto layer = QVector<Qt3DRender::QLayer*>() << new Qt3DRender::QLayer(); |
63 | QCoreApplication::processEvents(); |
64 | singleLayer->addLayer(layer: layer.first()); |
65 | QTest::newRow(dataTag: "single layer" ) << singleLayer << layer; |
66 | |
67 | Qt3DRender::QLayerFilter *multiLayers = new Qt3DRender::QLayerFilter(); |
68 | auto layers = QVector<Qt3DRender::QLayer*>() << new Qt3DRender::QLayer() |
69 | << new Qt3DRender::QLayer() |
70 | << new Qt3DRender::QLayer(); |
71 | QCoreApplication::processEvents(); |
72 | for (Qt3DRender::QLayer *layer : qAsConst(t&: layers)) |
73 | multiLayers->addLayer(layer); |
74 | QTest::newRow(dataTag: "multi layers" ) << multiLayers << layers; |
75 | } |
76 | |
77 | void checkCloning() |
78 | { |
79 | // GIVEN |
80 | QFETCH(Qt3DRender::QLayerFilter *, layerFilter); |
81 | QFETCH(QVector<Qt3DRender::QLayer*>, layers); |
82 | |
83 | // THEN |
84 | QCOMPARE(layerFilter->layers(), layers); |
85 | |
86 | // WHEN |
87 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(layerFilter); |
88 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
89 | |
90 | // THEN |
91 | QCOMPARE(creationChanges.size(), layers.size() + 1); |
92 | |
93 | const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QLayerFilterData> creationChangeData = |
94 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QLayerFilterData>>(src: creationChanges.first()); |
95 | const Qt3DRender::QLayerFilterData &cloneData = creationChangeData->data; |
96 | |
97 | // THEN |
98 | QCOMPARE(layerFilter->id(), creationChangeData->subjectId()); |
99 | QCOMPARE(layerFilter->isEnabled(), creationChangeData->isNodeEnabled()); |
100 | QCOMPARE(layerFilter->metaObject(), creationChangeData->metaObject()); |
101 | QCOMPARE(Qt3DCore::qIdsForNodes(layerFilter->layers()), cloneData.layerIds); |
102 | |
103 | delete layerFilter; |
104 | } |
105 | |
106 | void checkPropertyUpdates() |
107 | { |
108 | // GIVEN |
109 | TestArbiter arbiter; |
110 | QScopedPointer<Qt3DRender::QLayerFilter> layerFilter(new Qt3DRender::QLayerFilter()); |
111 | arbiter.setArbiterOnNode(layerFilter.data()); |
112 | |
113 | // WHEN |
114 | auto layer = new Qt3DRender::QLayer(layerFilter.data()); |
115 | QCoreApplication::processEvents(); |
116 | layerFilter->addLayer(layer); |
117 | QCoreApplication::processEvents(); |
118 | |
119 | // THEN |
120 | QCOMPARE(arbiter.events.size(), 0); |
121 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
122 | QVERIFY(arbiter.dirtyNodes.contains(layerFilter.data())); |
123 | |
124 | arbiter.dirtyNodes.clear(); |
125 | |
126 | // WHEN |
127 | layer = new Qt3DRender::QLayer(layerFilter.data()); |
128 | QCoreApplication::processEvents(); |
129 | layerFilter->addLayer(layer); |
130 | QCoreApplication::processEvents(); |
131 | |
132 | // THEN |
133 | QCOMPARE(arbiter.events.size(), 0); |
134 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
135 | QVERIFY(arbiter.dirtyNodes.contains(layerFilter.data())); |
136 | |
137 | arbiter.dirtyNodes.clear(); |
138 | |
139 | // WHEN |
140 | layer = layerFilter->layers().at(i: 0); |
141 | layerFilter->removeLayer(layer); |
142 | QCoreApplication::processEvents(); |
143 | |
144 | // THEN |
145 | QCOMPARE(arbiter.events.size(), 0); |
146 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
147 | QVERIFY(arbiter.dirtyNodes.contains(layerFilter.data())); |
148 | |
149 | arbiter.dirtyNodes.clear(); |
150 | } |
151 | |
152 | void checkLayerBookkeeping() |
153 | { |
154 | // GIVEN |
155 | QScopedPointer<Qt3DRender::QLayerFilter> layerFilter(new Qt3DRender::QLayerFilter); |
156 | { |
157 | // WHEN |
158 | Qt3DRender::QLayer layer; |
159 | layerFilter->addLayer(layer: &layer); |
160 | |
161 | // THEN |
162 | QCOMPARE(layer.parent(), layerFilter.data()); |
163 | QCOMPARE(layerFilter->layers().size(), 1); |
164 | } |
165 | // THEN (Should not crash and parameter be unset) |
166 | QVERIFY(layerFilter->layers().empty()); |
167 | |
168 | { |
169 | // WHEN |
170 | Qt3DRender::QLayerFilter someOtherLayerFilter; |
171 | QScopedPointer<Qt3DRender::QLayer> layer(new Qt3DRender::QLayer(&someOtherLayerFilter)); |
172 | layerFilter->addLayer(layer: layer.data()); |
173 | |
174 | // THEN |
175 | QCOMPARE(layer->parent(), &someOtherLayerFilter); |
176 | QCOMPARE(layerFilter->layers().size(), 1); |
177 | |
178 | // WHEN |
179 | layerFilter.reset(); |
180 | layer.reset(); |
181 | |
182 | // THEN Should not crash when the layer is destroyed (tests for failed removal of destruction helper) |
183 | } |
184 | } |
185 | }; |
186 | |
187 | QTEST_MAIN(tst_QLayerFilter) |
188 | |
189 | #include "tst_qlayerfilter.moc" |
190 | |