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 | // TODO Remove in Qt6 |
30 | #include <QtCore/qcompilerdetection.h> |
31 | QT_WARNING_DISABLE_DEPRECATED |
32 | |
33 | #include <QtTest/QTest> |
34 | #include <Qt3DRender/qtexture.h> |
35 | #include <Qt3DRender/private/qtexture_p.h> |
36 | #include <QObject> |
37 | #include <QSignalSpy> |
38 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
39 | #include <Qt3DCore/qnodecreatedchange.h> |
40 | #include "testpostmanarbiter.h" |
41 | |
42 | class tst_QTextureLoader : public QObject |
43 | { |
44 | Q_OBJECT |
45 | |
46 | private Q_SLOTS: |
47 | |
48 | void checkDefaultConstruction() |
49 | { |
50 | // GIVEN |
51 | Qt3DRender::QTextureLoader textureLoader; |
52 | |
53 | // THEN |
54 | QCOMPARE(textureLoader.source(), QUrl()); |
55 | QCOMPARE(textureLoader.isMirrored(), true); |
56 | QCOMPARE(textureLoader.target(), Qt3DRender::QTextureLoader::TargetAutomatic); |
57 | QCOMPARE(textureLoader.wrapMode()->x(), Qt3DRender::QTextureWrapMode::Repeat); |
58 | QCOMPARE(textureLoader.wrapMode()->y(), Qt3DRender::QTextureWrapMode::Repeat); |
59 | QCOMPARE(textureLoader.magnificationFilter(), Qt3DRender::QTextureLoader::Linear); |
60 | QCOMPARE(textureLoader.minificationFilter(), Qt3DRender::QTextureLoader::LinearMipMapLinear); |
61 | QCOMPARE(textureLoader.generateMipMaps(), true); |
62 | QCOMPARE(textureLoader.maximumAnisotropy(), 16.0f); |
63 | } |
64 | |
65 | void checkPropertyChanges() |
66 | { |
67 | // GIVEN |
68 | Qt3DRender::QTextureLoader textureLoader; |
69 | |
70 | { |
71 | // WHEN |
72 | QSignalSpy spy(&textureLoader, SIGNAL(sourceChanged(QUrl))); |
73 | const QUrl newValue(QStringLiteral("http://msn.com" )); |
74 | textureLoader.setSource(newValue); |
75 | |
76 | // THEN |
77 | QVERIFY(spy.isValid()); |
78 | QCOMPARE(textureLoader.source(), newValue); |
79 | QCOMPARE(spy.count(), 1); |
80 | |
81 | // WHEN |
82 | spy.clear(); |
83 | textureLoader.setSource(newValue); |
84 | |
85 | // THEN |
86 | QCOMPARE(textureLoader.source(), newValue); |
87 | QCOMPARE(spy.count(), 0); |
88 | } |
89 | { |
90 | // WHEN |
91 | QSignalSpy spy(&textureLoader, SIGNAL(mirroredChanged(bool))); |
92 | const bool newValue = false; |
93 | textureLoader.setMirrored(newValue); |
94 | |
95 | // THEN |
96 | QVERIFY(spy.isValid()); |
97 | QCOMPARE(textureLoader.isMirrored(), newValue); |
98 | QCOMPARE(spy.count(), 1); |
99 | |
100 | // WHEN |
101 | spy.clear(); |
102 | textureLoader.setMirrored(newValue); |
103 | |
104 | // THEN |
105 | QCOMPARE(textureLoader.isMirrored(), newValue); |
106 | QCOMPARE(spy.count(), 0); |
107 | } |
108 | } |
109 | |
110 | void checkCreationData() |
111 | { |
112 | // GIVEN |
113 | Qt3DRender::QTextureLoader textureLoader; |
114 | |
115 | textureLoader.setSource(QUrl(QStringLiteral("SomeUrl" ))); |
116 | textureLoader.setMirrored(false); |
117 | |
118 | // WHEN |
119 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
120 | |
121 | { |
122 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&textureLoader); |
123 | creationChanges = creationChangeGenerator.creationChanges(); |
124 | } |
125 | |
126 | // THEN |
127 | { |
128 | QCOMPARE(creationChanges.size(), 1); |
129 | |
130 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QAbstractTextureData>>(src: creationChanges.first()); |
131 | const Qt3DRender::QAbstractTextureData cloneData = creationChangeData->data; |
132 | |
133 | QCOMPARE(textureLoader.id(), creationChangeData->subjectId()); |
134 | QCOMPARE(textureLoader.isEnabled(), true); |
135 | QCOMPARE(textureLoader.isEnabled(), creationChangeData->isNodeEnabled()); |
136 | QCOMPARE(textureLoader.metaObject(), creationChangeData->metaObject()); |
137 | } |
138 | |
139 | // WHEN |
140 | textureLoader.setEnabled(false); |
141 | |
142 | { |
143 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&textureLoader); |
144 | creationChanges = creationChangeGenerator.creationChanges(); |
145 | } |
146 | |
147 | // THEN |
148 | { |
149 | QCOMPARE(creationChanges.size(), 1); |
150 | |
151 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QAbstractTextureData>>(src: creationChanges.first()); |
152 | |
153 | QCOMPARE(textureLoader.id(), creationChangeData->subjectId()); |
154 | QCOMPARE(textureLoader.isEnabled(), false); |
155 | QCOMPARE(textureLoader.isEnabled(), creationChangeData->isNodeEnabled()); |
156 | QCOMPARE(textureLoader.metaObject(), creationChangeData->metaObject()); |
157 | } |
158 | } |
159 | |
160 | void checkSourceUpdate() |
161 | { |
162 | // GIVEN |
163 | TestArbiter arbiter; |
164 | Qt3DRender::QTextureLoader textureLoader; |
165 | arbiter.setArbiterOnNode(&textureLoader); |
166 | |
167 | { |
168 | // WHEN |
169 | textureLoader.setSource(QUrl(QStringLiteral("Gary" ))); |
170 | QCoreApplication::processEvents(); |
171 | |
172 | // THEN |
173 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
174 | QCOMPARE(arbiter.dirtyNodes.front(), &textureLoader); |
175 | |
176 | Qt3DRender::QAbstractTexturePrivate *d = dynamic_cast<Qt3DRender::QAbstractTexturePrivate *>(Qt3DRender::QAbstractTexturePrivate::get(q: &textureLoader)); |
177 | const auto generator = qSharedPointerCast<Qt3DRender::QTextureFromSourceGenerator>(src: d->dataFunctor()); |
178 | QVERIFY(generator); |
179 | QCOMPARE(generator->url(), QUrl(QStringLiteral("Gary" ))); |
180 | |
181 | arbiter.events.clear(); |
182 | } |
183 | |
184 | { |
185 | // WHEN |
186 | textureLoader.setSource(QUrl(QStringLiteral("Gary" ))); |
187 | QCoreApplication::processEvents(); |
188 | |
189 | // THEN |
190 | QCOMPARE(arbiter.events.size(), 0); |
191 | } |
192 | |
193 | } |
194 | |
195 | void checkMirroredUpdate() |
196 | { |
197 | // GIVEN |
198 | TestArbiter arbiter; |
199 | Qt3DRender::QTextureLoader textureLoader; |
200 | arbiter.setArbiterOnNode(&textureLoader); |
201 | |
202 | { |
203 | // WHEN |
204 | textureLoader.setMirrored(false); |
205 | QCoreApplication::processEvents(); |
206 | |
207 | // THEN |
208 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
209 | QCOMPARE(arbiter.dirtyNodes.front(), &textureLoader); |
210 | |
211 | Qt3DRender::QAbstractTexturePrivate *d = dynamic_cast<Qt3DRender::QAbstractTexturePrivate *>(Qt3DRender::QAbstractTexturePrivate::get(q: &textureLoader)); |
212 | const auto generator = qSharedPointerCast<Qt3DRender::QTextureFromSourceGenerator>(src: d->dataFunctor()); |
213 | QVERIFY(generator); |
214 | QCOMPARE(generator->isMirrored(), false); |
215 | |
216 | arbiter.dirtyNodes.clear(); |
217 | } |
218 | |
219 | { |
220 | // WHEN |
221 | textureLoader.setMirrored(false); |
222 | QCoreApplication::processEvents(); |
223 | |
224 | // THEN |
225 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
226 | } |
227 | |
228 | } |
229 | |
230 | }; |
231 | |
232 | QTEST_MAIN(tst_QTextureLoader) |
233 | |
234 | #include "tst_qtextureloader.moc" |
235 | |