1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 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 | |
30 | #include <QtTest/QTest> |
31 | #include <Qt3DRender/qabstracttextureimage.h> |
32 | #include <Qt3DRender/private/qabstracttextureimage_p.h> |
33 | #include <QObject> |
34 | #include <QSignalSpy> |
35 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
36 | #include <Qt3DCore/qnodecreatedchange.h> |
37 | #include "testpostmanarbiter.h" |
38 | |
39 | class FakeTextureImage : public Qt3DRender::QAbstractTextureImage |
40 | { |
41 | protected: |
42 | Qt3DRender::QTextureImageDataGeneratorPtr dataGenerator() const override |
43 | { |
44 | return Qt3DRender::QTextureImageDataGeneratorPtr(); |
45 | } |
46 | }; |
47 | |
48 | class tst_QAbstractTextureImage : public QObject |
49 | { |
50 | Q_OBJECT |
51 | |
52 | private Q_SLOTS: |
53 | |
54 | void initTestCase() |
55 | { |
56 | qRegisterMetaType<Qt3DRender::QAbstractTexture::CubeMapFace>(typeName: "QAbstractTexture::CubeMapFace" ); |
57 | } |
58 | |
59 | void checkDefaultConstruction() |
60 | { |
61 | // GIVEN |
62 | FakeTextureImage abstractTextureImage; |
63 | |
64 | // THEN |
65 | QCOMPARE(abstractTextureImage.mipLevel(), 0); |
66 | QCOMPARE(abstractTextureImage.layer(), 0); |
67 | QCOMPARE(abstractTextureImage.face(), Qt3DRender::QAbstractTexture::CubeMapPositiveX); |
68 | } |
69 | |
70 | void checkPropertyChanges() |
71 | { |
72 | // GIVEN |
73 | FakeTextureImage abstractTextureImage; |
74 | |
75 | { |
76 | // WHEN |
77 | QSignalSpy spy(&abstractTextureImage, SIGNAL(mipLevelChanged(int))); |
78 | const int newValue = 5; |
79 | abstractTextureImage.setMipLevel(newValue); |
80 | |
81 | // THEN |
82 | QVERIFY(spy.isValid()); |
83 | QCOMPARE(abstractTextureImage.mipLevel(), newValue); |
84 | QCOMPARE(spy.count(), 1); |
85 | |
86 | // WHEN |
87 | spy.clear(); |
88 | abstractTextureImage.setMipLevel(newValue); |
89 | |
90 | // THEN |
91 | QCOMPARE(abstractTextureImage.mipLevel(), newValue); |
92 | QCOMPARE(spy.count(), 0); |
93 | } |
94 | { |
95 | // WHEN |
96 | QSignalSpy spy(&abstractTextureImage, SIGNAL(layerChanged(int))); |
97 | const int newValue = 12; |
98 | abstractTextureImage.setLayer(newValue); |
99 | |
100 | // THEN |
101 | QVERIFY(spy.isValid()); |
102 | QCOMPARE(abstractTextureImage.layer(), newValue); |
103 | QCOMPARE(spy.count(), 1); |
104 | |
105 | // WHEN |
106 | spy.clear(); |
107 | abstractTextureImage.setLayer(newValue); |
108 | |
109 | // THEN |
110 | QCOMPARE(abstractTextureImage.layer(), newValue); |
111 | QCOMPARE(spy.count(), 0); |
112 | } |
113 | { |
114 | // WHEN |
115 | QSignalSpy spy(&abstractTextureImage, SIGNAL(faceChanged(Qt3DRender::QAbstractTexture::CubeMapFace))); |
116 | const Qt3DRender::QAbstractTexture::CubeMapFace newValue = Qt3DRender::QAbstractTexture::CubeMapNegativeZ; |
117 | abstractTextureImage.setFace(newValue); |
118 | |
119 | // THEN |
120 | QVERIFY(spy.isValid()); |
121 | QCOMPARE(abstractTextureImage.face(), newValue); |
122 | QCOMPARE(spy.count(), 1); |
123 | |
124 | // WHEN |
125 | spy.clear(); |
126 | abstractTextureImage.setFace(newValue); |
127 | |
128 | // THEN |
129 | QCOMPARE(abstractTextureImage.face(), newValue); |
130 | QCOMPARE(spy.count(), 0); |
131 | } |
132 | } |
133 | |
134 | void checkCreationData() |
135 | { |
136 | // GIVEN |
137 | FakeTextureImage abstractTextureImage; |
138 | |
139 | abstractTextureImage.setMipLevel(32); |
140 | abstractTextureImage.setLayer(56); |
141 | abstractTextureImage.setFace(Qt3DRender::QAbstractTexture::CubeMapNegativeY); |
142 | |
143 | // WHEN |
144 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
145 | |
146 | { |
147 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&abstractTextureImage); |
148 | creationChanges = creationChangeGenerator.creationChanges(); |
149 | } |
150 | |
151 | // THEN |
152 | { |
153 | QCOMPARE(creationChanges.size(), 1); |
154 | |
155 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QAbstractTextureImageData>>(src: creationChanges.first()); |
156 | const Qt3DRender::QAbstractTextureImageData cloneData = creationChangeData->data; |
157 | |
158 | QCOMPARE(abstractTextureImage.mipLevel(), cloneData.mipLevel); |
159 | QCOMPARE(abstractTextureImage.layer(), cloneData.layer); |
160 | QCOMPARE(abstractTextureImage.face(), cloneData.face); |
161 | QCOMPARE(abstractTextureImage.id(), creationChangeData->subjectId()); |
162 | QCOMPARE(abstractTextureImage.isEnabled(), true); |
163 | QCOMPARE(abstractTextureImage.isEnabled(), creationChangeData->isNodeEnabled()); |
164 | QCOMPARE(abstractTextureImage.metaObject(), creationChangeData->metaObject()); |
165 | } |
166 | |
167 | // WHEN |
168 | abstractTextureImage.setEnabled(false); |
169 | |
170 | { |
171 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&abstractTextureImage); |
172 | creationChanges = creationChangeGenerator.creationChanges(); |
173 | } |
174 | |
175 | // THEN |
176 | { |
177 | QCOMPARE(creationChanges.size(), 1); |
178 | |
179 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QAbstractTextureImageData>>(src: creationChanges.first()); |
180 | const Qt3DRender::QAbstractTextureImageData cloneData = creationChangeData->data; |
181 | |
182 | QCOMPARE(abstractTextureImage.mipLevel(), cloneData.mipLevel); |
183 | QCOMPARE(abstractTextureImage.layer(), cloneData.layer); |
184 | QCOMPARE(abstractTextureImage.face(), cloneData.face); |
185 | QCOMPARE(abstractTextureImage.id(), creationChangeData->subjectId()); |
186 | QCOMPARE(abstractTextureImage.isEnabled(), false); |
187 | QCOMPARE(abstractTextureImage.isEnabled(), creationChangeData->isNodeEnabled()); |
188 | QCOMPARE(abstractTextureImage.metaObject(), creationChangeData->metaObject()); |
189 | } |
190 | } |
191 | |
192 | void checkMipLevelUpdate() |
193 | { |
194 | // GIVEN |
195 | TestArbiter arbiter; |
196 | FakeTextureImage abstractTextureImage; |
197 | arbiter.setArbiterOnNode(&abstractTextureImage); |
198 | |
199 | { |
200 | // WHEN |
201 | abstractTextureImage.setMipLevel(9); |
202 | |
203 | // THEN |
204 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
205 | QCOMPARE(arbiter.dirtyNodes.front(), &abstractTextureImage); |
206 | |
207 | arbiter.dirtyNodes.clear(); |
208 | } |
209 | |
210 | { |
211 | // WHEN |
212 | abstractTextureImage.setMipLevel(9); |
213 | |
214 | // THEN |
215 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
216 | } |
217 | |
218 | } |
219 | |
220 | void checkLayerUpdate() |
221 | { |
222 | // GIVEN |
223 | TestArbiter arbiter; |
224 | FakeTextureImage abstractTextureImage; |
225 | arbiter.setArbiterOnNode(&abstractTextureImage); |
226 | |
227 | { |
228 | // WHEN |
229 | abstractTextureImage.setLayer(12); |
230 | |
231 | // THEN |
232 | QCOMPARE(arbiter.events.size(), 0); |
233 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
234 | QCOMPARE(arbiter.dirtyNodes.front(), &abstractTextureImage); |
235 | |
236 | arbiter.dirtyNodes.clear(); |
237 | } |
238 | |
239 | { |
240 | // WHEN |
241 | abstractTextureImage.setLayer(12); |
242 | |
243 | // THEN |
244 | QCOMPARE(arbiter.events.size(), 0); |
245 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
246 | } |
247 | |
248 | } |
249 | |
250 | void checkFaceUpdate() |
251 | { |
252 | // GIVEN |
253 | TestArbiter arbiter; |
254 | FakeTextureImage abstractTextureImage; |
255 | arbiter.setArbiterOnNode(&abstractTextureImage); |
256 | |
257 | { |
258 | // WHEN |
259 | abstractTextureImage.setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveY); |
260 | |
261 | // THEN |
262 | QCOMPARE(arbiter.events.size(), 0); |
263 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
264 | QCOMPARE(arbiter.dirtyNodes.front(), &abstractTextureImage); |
265 | |
266 | arbiter.dirtyNodes.clear(); |
267 | } |
268 | |
269 | { |
270 | // WHEN |
271 | abstractTextureImage.setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveY); |
272 | |
273 | // THEN |
274 | QCOMPARE(arbiter.events.size(), 0); |
275 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
276 | } |
277 | |
278 | } |
279 | |
280 | }; |
281 | |
282 | QTEST_MAIN(tst_QAbstractTextureImage) |
283 | |
284 | #include "tst_qabstracttextureimage.moc" |
285 | |