1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 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 | #include <QtTest/QTest> |
30 | #include <Qt3DRender/qproximityfilter.h> |
31 | #include <Qt3DRender/private/qproximityfilter_p.h> |
32 | #include <QObject> |
33 | #include <QSignalSpy> |
34 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
35 | #include <Qt3DCore/qnodecreatedchange.h> |
36 | #include <Qt3DCore/qentity.h> |
37 | #include "testpostmanarbiter.h" |
38 | |
39 | class tst_QProximityFilter : public QObject |
40 | { |
41 | Q_OBJECT |
42 | |
43 | public: |
44 | tst_QProximityFilter() : QObject() |
45 | { |
46 | qRegisterMetaType<Qt3DCore::QEntity*>(); |
47 | } |
48 | |
49 | private Q_SLOTS: |
50 | |
51 | void checkDefaultConstruction() |
52 | { |
53 | // GIVEN |
54 | Qt3DRender::QProximityFilter proximityFilter; |
55 | |
56 | // THEN |
57 | QVERIFY(proximityFilter.entity() == nullptr); |
58 | QCOMPARE(proximityFilter.distanceThreshold(), 0.0f); |
59 | } |
60 | |
61 | void checkPropertyChanges() |
62 | { |
63 | // GIVEN |
64 | Qt3DRender::QProximityFilter proximityFilter; |
65 | |
66 | { |
67 | // WHEN |
68 | Qt3DCore::QEntity entity; |
69 | |
70 | QSignalSpy spy(&proximityFilter, SIGNAL(entityChanged(Qt3DCore::QEntity *))); |
71 | Qt3DCore::QEntity *newValue = &entity; |
72 | proximityFilter.setEntity(newValue); |
73 | |
74 | // THEN |
75 | QVERIFY(spy.isValid()); |
76 | QCOMPARE(proximityFilter.entity(), newValue); |
77 | QCOMPARE(spy.count(), 1); |
78 | |
79 | // WHEN |
80 | spy.clear(); |
81 | proximityFilter.setEntity(newValue); |
82 | |
83 | // THEN |
84 | QCOMPARE(proximityFilter.entity(), newValue); |
85 | QCOMPARE(spy.count(), 0); |
86 | } |
87 | { |
88 | // WHEN |
89 | QSignalSpy spy(&proximityFilter, SIGNAL(distanceThresholdChanged(float))); |
90 | const float newValue = 883.0f; |
91 | proximityFilter.setDistanceThreshold(newValue); |
92 | |
93 | // THEN |
94 | QVERIFY(spy.isValid()); |
95 | QCOMPARE(proximityFilter.distanceThreshold(), newValue); |
96 | QCOMPARE(spy.count(), 1); |
97 | |
98 | // WHEN |
99 | spy.clear(); |
100 | proximityFilter.setDistanceThreshold(newValue); |
101 | |
102 | // THEN |
103 | QCOMPARE(proximityFilter.distanceThreshold(), newValue); |
104 | QCOMPARE(spy.count(), 0); |
105 | } |
106 | } |
107 | |
108 | void checkCreationData() |
109 | { |
110 | // GIVEN |
111 | Qt3DRender::QProximityFilter proximityFilter; |
112 | Qt3DCore::QEntity entity; |
113 | |
114 | proximityFilter.setEntity(&entity); |
115 | proximityFilter.setDistanceThreshold(1584.0f); |
116 | |
117 | // WHEN |
118 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
119 | |
120 | { |
121 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&proximityFilter); |
122 | creationChanges = creationChangeGenerator.creationChanges(); |
123 | } |
124 | |
125 | // THEN |
126 | { |
127 | QCOMPARE(creationChanges.size(), 2); // Entity creation change is the second change |
128 | |
129 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QProximityFilterData>>(src: creationChanges.first()); |
130 | const Qt3DRender::QProximityFilterData cloneData = creationChangeData->data; |
131 | |
132 | QCOMPARE(proximityFilter.entity()->id(), cloneData.entityId); |
133 | QCOMPARE(proximityFilter.distanceThreshold(), cloneData.distanceThreshold); |
134 | QCOMPARE(proximityFilter.id(), creationChangeData->subjectId()); |
135 | QCOMPARE(proximityFilter.isEnabled(), true); |
136 | QCOMPARE(proximityFilter.isEnabled(), creationChangeData->isNodeEnabled()); |
137 | QCOMPARE(proximityFilter.metaObject(), creationChangeData->metaObject()); |
138 | } |
139 | |
140 | // WHEN |
141 | proximityFilter.setEnabled(false); |
142 | |
143 | { |
144 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&proximityFilter); |
145 | creationChanges = creationChangeGenerator.creationChanges(); |
146 | } |
147 | |
148 | // THEN |
149 | { |
150 | QCOMPARE(creationChanges.size(), 2); // Entity creation change is the second change |
151 | |
152 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QProximityFilterData>>(src: creationChanges.first()); |
153 | const Qt3DRender::QProximityFilterData cloneData = creationChangeData->data; |
154 | |
155 | QCOMPARE(proximityFilter.entity()->id(), cloneData.entityId); |
156 | QCOMPARE(proximityFilter.distanceThreshold(), cloneData.distanceThreshold); |
157 | QCOMPARE(proximityFilter.id(), creationChangeData->subjectId()); |
158 | QCOMPARE(proximityFilter.isEnabled(), false); |
159 | QCOMPARE(proximityFilter.isEnabled(), creationChangeData->isNodeEnabled()); |
160 | QCOMPARE(proximityFilter.metaObject(), creationChangeData->metaObject()); |
161 | } |
162 | } |
163 | |
164 | void checkEntityUpdate() |
165 | { |
166 | // GIVEN |
167 | TestArbiter arbiter; |
168 | Qt3DRender::QProximityFilter proximityFilter; |
169 | arbiter.setArbiterOnNode(&proximityFilter); |
170 | Qt3DCore::QEntity entity; |
171 | |
172 | { |
173 | // WHEN |
174 | proximityFilter.setEntity(&entity); |
175 | QCoreApplication::processEvents(); |
176 | |
177 | // THEN |
178 | QCOMPARE(arbiter.events.size(), 0); |
179 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
180 | QCOMPARE(arbiter.dirtyNodes.front(), &proximityFilter); |
181 | |
182 | arbiter.dirtyNodes.clear(); |
183 | } |
184 | |
185 | { |
186 | // WHEN |
187 | proximityFilter.setEntity(&entity); |
188 | QCoreApplication::processEvents(); |
189 | |
190 | // THEN |
191 | QCOMPARE(arbiter.events.size(), 0); |
192 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
193 | } |
194 | |
195 | } |
196 | |
197 | void checkDistanceThresholdUpdate() |
198 | { |
199 | // GIVEN |
200 | TestArbiter arbiter; |
201 | Qt3DRender::QProximityFilter proximityFilter; |
202 | arbiter.setArbiterOnNode(&proximityFilter); |
203 | |
204 | { |
205 | // WHEN |
206 | proximityFilter.setDistanceThreshold(454.0f); |
207 | QCoreApplication::processEvents(); |
208 | |
209 | // THEN |
210 | QCOMPARE(arbiter.events.size(), 0); |
211 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
212 | QCOMPARE(arbiter.dirtyNodes.front(), &proximityFilter); |
213 | |
214 | arbiter.dirtyNodes.clear(); |
215 | } |
216 | |
217 | { |
218 | // WHEN |
219 | proximityFilter.setDistanceThreshold(454.0f); |
220 | QCoreApplication::processEvents(); |
221 | |
222 | // THEN |
223 | QCOMPARE(arbiter.events.size(), 0); |
224 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
225 | } |
226 | |
227 | } |
228 | |
229 | void checkEntityBookkeeping() |
230 | { |
231 | // GIVEN |
232 | QScopedPointer<Qt3DRender::QProximityFilter> proximityFilter(new Qt3DRender::QProximityFilter); |
233 | { |
234 | // WHEN |
235 | Qt3DCore::QEntity entity; |
236 | proximityFilter->setEntity(&entity); |
237 | |
238 | // THEN |
239 | QCOMPARE(entity.parent(), proximityFilter.data()); |
240 | QCOMPARE(proximityFilter->entity(), &entity); |
241 | } |
242 | |
243 | // THEN (Should not crash and parameter be unset) |
244 | QVERIFY(proximityFilter->entity() == nullptr); |
245 | |
246 | { |
247 | // WHEN |
248 | Qt3DRender::QProximityFilter someOtherProximityFilter; |
249 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity(&someOtherProximityFilter)); |
250 | proximityFilter->setEntity(entity.data()); |
251 | |
252 | // THEN |
253 | QCOMPARE(entity->parent(), &someOtherProximityFilter); |
254 | QCOMPARE(proximityFilter->entity(), entity.data()); |
255 | |
256 | // WHEN |
257 | proximityFilter.reset(); |
258 | entity.reset(); |
259 | |
260 | // THEN Should not crash when the camera is destroyed (tests for failed removal of destruction helper) |
261 | } |
262 | } |
263 | }; |
264 | |
265 | QTEST_MAIN(tst_QProximityFilter) |
266 | |
267 | #include "tst_qproximityfilter.moc" |
268 | |