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 | // TODO Remove in Qt6 |
30 | #include <QtCore/qcompilerdetection.h> |
31 | QT_WARNING_DISABLE_DEPRECATED |
32 | |
33 | #include <QtTest/QtTest> |
34 | #include <Qt3DCore/qcomponent.h> |
35 | #include <Qt3DRender/qgeometryfactory.h> |
36 | #include <Qt3DRender/qgeometry.h> |
37 | #include <Qt3DRender/qmesh.h> |
38 | #include <Qt3DRender/private/qmesh_p.h> |
39 | #include <Qt3DCore/qaspectengine.h> |
40 | |
41 | class MeshFunctorA : public Qt3DRender::QGeometryFactory |
42 | { |
43 | public: |
44 | MeshFunctorA() |
45 | {} |
46 | |
47 | ~MeshFunctorA() |
48 | {} |
49 | |
50 | Qt3DRender::QGeometry *operator ()() override |
51 | { |
52 | return nullptr; |
53 | } |
54 | |
55 | bool equals(const Qt3DRender::QGeometryFactory &other) const override |
56 | { |
57 | return Qt3DRender::functor_cast<MeshFunctorA>(other: &other); |
58 | } |
59 | |
60 | QT3D_FUNCTOR(MeshFunctorA) |
61 | }; |
62 | |
63 | class MeshFunctorB : public Qt3DRender::QGeometryFactory |
64 | { |
65 | public: |
66 | MeshFunctorB() |
67 | {} |
68 | |
69 | ~MeshFunctorB() |
70 | {} |
71 | |
72 | Qt3DRender::QGeometry *operator ()() override |
73 | { |
74 | return nullptr; |
75 | } |
76 | |
77 | bool equals(const Qt3DRender::QGeometryFactory &other) const override |
78 | { |
79 | return Qt3DRender::functor_cast<MeshFunctorB>(other: &other); |
80 | } |
81 | |
82 | QT3D_FUNCTOR(MeshFunctorB) |
83 | }; |
84 | |
85 | class MeshFunctorASub : public MeshFunctorA |
86 | { |
87 | public: |
88 | MeshFunctorASub() |
89 | {} |
90 | |
91 | ~MeshFunctorASub() |
92 | {} |
93 | |
94 | bool equals(const Qt3DRender::QGeometryFactory &other) const override |
95 | { |
96 | return Qt3DRender::functor_cast<MeshFunctorASub>(other: &other); |
97 | } |
98 | |
99 | QT3D_FUNCTOR(MeshFunctorASub) |
100 | }; |
101 | |
102 | |
103 | class tst_MeshFunctors : public QObject |
104 | { |
105 | Q_OBJECT |
106 | private Q_SLOTS: |
107 | |
108 | void checkInitialState() |
109 | { |
110 | // GIVEN |
111 | Qt3DRender::QMesh mesh; |
112 | mesh.setSource(QUrl(QStringLiteral("./some_path.obj" ))); |
113 | |
114 | // WHEN |
115 | const Qt3DRender::MeshLoaderFunctor functor(&mesh); |
116 | |
117 | // THEN |
118 | QVERIFY(functor.nodeManagers() == nullptr); |
119 | QVERIFY(functor.downloaderService() == nullptr); |
120 | QVERIFY(functor.sourceData().isEmpty()); |
121 | QCOMPARE(functor.mesh(), mesh.id()); |
122 | QCOMPARE(functor.sourcePath(), mesh.source()); |
123 | QCOMPARE(functor.status(), Qt3DRender::QMesh::None); |
124 | } |
125 | |
126 | void functorComparison() |
127 | { |
128 | // GIVEN |
129 | QScopedPointer<MeshFunctorA> functorA(new MeshFunctorA); |
130 | QScopedPointer<MeshFunctorB> functorB(new MeshFunctorB); |
131 | QScopedPointer<MeshFunctorASub> functorASub(new MeshFunctorASub); |
132 | |
133 | // THEN |
134 | QVERIFY(!(*functorA == *functorB)); |
135 | QVERIFY(!(*functorA == *functorASub)); |
136 | |
137 | QVERIFY(!(*functorB == *functorA)); |
138 | QVERIFY(!(*functorB == *functorASub)); |
139 | |
140 | QVERIFY(!(*functorASub == *functorA)); |
141 | QVERIFY(!(*functorASub == *functorB)); |
142 | |
143 | QVERIFY(*functorA == *functorA); |
144 | QVERIFY(*functorB == *functorB); |
145 | QVERIFY(*functorASub == *functorASub); |
146 | } |
147 | |
148 | void checkMeshFunctorEquality() |
149 | { |
150 | // GIVEN |
151 | auto meshA = new Qt3DRender::QMesh(); |
152 | meshA->setSource(QUrl::fromLocalFile(localfile: QLatin1String("/foo" ))); |
153 | meshA->setMeshName(QLatin1String("bar" )); |
154 | |
155 | auto meshB = new Qt3DRender::QMesh(); |
156 | meshB->setSource(QUrl::fromLocalFile(localfile: QLatin1String("/foo" ))); |
157 | meshB->setMeshName(QLatin1String("baz" )); |
158 | |
159 | auto meshC = new Qt3DRender::QMesh(); |
160 | meshC->setSource(QUrl::fromLocalFile(localfile: QLatin1String("/baz" ))); |
161 | meshC->setMeshName(QLatin1String("bar" )); |
162 | |
163 | auto meshD = new Qt3DRender::QMesh(); |
164 | meshD->setSource(QUrl::fromLocalFile(localfile: QLatin1String("/foo" ))); |
165 | meshD->setMeshName(QLatin1String("bar" )); |
166 | |
167 | const Qt3DRender::MeshLoaderFunctor functorA(meshA); |
168 | const Qt3DRender::MeshLoaderFunctor functorB(meshB); |
169 | const Qt3DRender::MeshLoaderFunctor functorC(meshC); |
170 | const Qt3DRender::MeshLoaderFunctor functorD(meshD); |
171 | |
172 | // WHEN |
173 | const bool selfEquality = (functorA == functorA); |
174 | const bool sameSource = (functorA == functorB); |
175 | const bool sameMeshName = (functorA == functorC); |
176 | const bool perfectMatch = (functorA == functorD); |
177 | |
178 | // THEN |
179 | QCOMPARE(selfEquality, true); |
180 | QCOMPARE(sameSource, false); |
181 | QCOMPARE(sameMeshName, false); |
182 | QCOMPARE(perfectMatch, true); |
183 | } |
184 | |
185 | void checkExecution() |
186 | { |
187 | { |
188 | // GIVEN |
189 | Qt3DRender::QMesh mesh; |
190 | Qt3DRender::MeshLoaderFunctor functor(&mesh); |
191 | |
192 | // WHEN |
193 | const Qt3DRender::QGeometry *g = functor(); |
194 | |
195 | // THEN |
196 | QVERIFY(g == nullptr); |
197 | QCOMPARE(functor.status(), Qt3DRender::QMesh::Error); |
198 | } |
199 | |
200 | { |
201 | // GIVEN |
202 | Qt3DRender::QMesh mesh; |
203 | mesh.setSource(QUrl(QStringLiteral("./non_existing.obj" ))); |
204 | Qt3DRender::MeshLoaderFunctor functor(&mesh); |
205 | |
206 | // WHEN |
207 | const Qt3DRender::QGeometry *g = functor(); |
208 | |
209 | // THEN |
210 | QVERIFY(g == nullptr); |
211 | QCOMPARE(functor.status(), Qt3DRender::QMesh::Error); |
212 | } |
213 | |
214 | { |
215 | // GIVEN |
216 | Qt3DRender::QMesh mesh; |
217 | mesh.setSource(QUrl(QStringLiteral("http://www.somedomain.org/non_exisiting.obj" ))); |
218 | Qt3DRender::MeshLoaderFunctor functor(&mesh); |
219 | |
220 | // WHEN |
221 | const Qt3DRender::QGeometry *g = functor(); |
222 | |
223 | // THEN |
224 | QVERIFY(g == nullptr); |
225 | QCOMPARE(functor.status(), Qt3DRender::QMesh::Error); |
226 | } |
227 | } |
228 | }; |
229 | |
230 | QTEST_MAIN(tst_MeshFunctors) |
231 | |
232 | #include "tst_meshfunctors.moc" |
233 | |