1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 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/QTest> |
34 | #include <Qt3DRender/qtextureimage.h> |
35 | #include <Qt3DRender/private/qtextureimage_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_QTextureImage : public QObject |
43 | { |
44 | Q_OBJECT |
45 | |
46 | private Q_SLOTS: |
47 | |
48 | void checkDefaultConstruction() |
49 | { |
50 | // GIVEN |
51 | Qt3DRender::QTextureImage textureImage; |
52 | |
53 | // THEN |
54 | QCOMPARE(textureImage.source(), QUrl()); |
55 | QCOMPARE(textureImage.status(), Qt3DRender::QTextureImage::None); |
56 | QCOMPARE(textureImage.isMirrored(), true); |
57 | } |
58 | |
59 | void checkPropertyChanges() |
60 | { |
61 | // GIVEN |
62 | Qt3DRender::QTextureImage textureImage; |
63 | |
64 | { |
65 | // WHEN |
66 | QSignalSpy spy(&textureImage, SIGNAL(sourceChanged(QUrl))); |
67 | const QUrl newValue(QStringLiteral("Boulder" )); |
68 | textureImage.setSource(newValue); |
69 | |
70 | // THEN |
71 | QVERIFY(spy.isValid()); |
72 | QCOMPARE(textureImage.source(), newValue); |
73 | QCOMPARE(spy.count(), 1); |
74 | |
75 | // WHEN |
76 | spy.clear(); |
77 | textureImage.setSource(newValue); |
78 | |
79 | // THEN |
80 | QCOMPARE(textureImage.source(), newValue); |
81 | QCOMPARE(spy.count(), 0); |
82 | } |
83 | { |
84 | // WHEN |
85 | QSignalSpy spy(&textureImage, SIGNAL(mirroredChanged(bool))); |
86 | const bool newValue = false; |
87 | textureImage.setMirrored(newValue); |
88 | |
89 | // THEN |
90 | QVERIFY(spy.isValid()); |
91 | QCOMPARE(textureImage.isMirrored(), newValue); |
92 | QCOMPARE(spy.count(), 1); |
93 | |
94 | // WHEN |
95 | spy.clear(); |
96 | textureImage.setMirrored(newValue); |
97 | |
98 | // THEN |
99 | QCOMPARE(textureImage.isMirrored(), newValue); |
100 | QCOMPARE(spy.count(), 0); |
101 | } |
102 | } |
103 | |
104 | void checkCreationData() |
105 | { |
106 | // GIVEN |
107 | Qt3DRender::QTextureImage textureImage; |
108 | |
109 | textureImage.setSource(QUrl(QStringLiteral("URL" ))); |
110 | textureImage.setMirrored(false); |
111 | |
112 | // WHEN |
113 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
114 | |
115 | { |
116 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&textureImage); |
117 | creationChanges = creationChangeGenerator.creationChanges(); |
118 | } |
119 | |
120 | // THEN |
121 | { |
122 | QCOMPARE(creationChanges.size(), 1); |
123 | |
124 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QAbstractTextureImageData>>(src: creationChanges.first()); |
125 | const Qt3DRender::QAbstractTextureImageData cloneData = creationChangeData->data; |
126 | |
127 | QCOMPARE(textureImage.id(), creationChangeData->subjectId()); |
128 | QCOMPARE(textureImage.isEnabled(), true); |
129 | QCOMPARE(textureImage.isEnabled(), creationChangeData->isNodeEnabled()); |
130 | QCOMPARE(textureImage.metaObject(), creationChangeData->metaObject()); |
131 | |
132 | const auto generator = qSharedPointerCast<Qt3DRender::QImageTextureDataFunctor>(src: cloneData.generator); |
133 | QVERIFY(generator); |
134 | QCOMPARE(generator->isMirrored(), textureImage.isMirrored()); |
135 | QCOMPARE(generator->url(), textureImage.source()); |
136 | |
137 | } |
138 | |
139 | // WHEN |
140 | textureImage.setEnabled(false); |
141 | |
142 | { |
143 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&textureImage); |
144 | creationChanges = creationChangeGenerator.creationChanges(); |
145 | } |
146 | |
147 | // THEN |
148 | { |
149 | QCOMPARE(creationChanges.size(), 1); |
150 | |
151 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QAbstractTextureImageData>>(src: creationChanges.first()); |
152 | const Qt3DRender::QAbstractTextureImageData cloneData = creationChangeData->data; |
153 | |
154 | QCOMPARE(textureImage.id(), creationChangeData->subjectId()); |
155 | QCOMPARE(textureImage.isEnabled(), false); |
156 | QCOMPARE(textureImage.isEnabled(), creationChangeData->isNodeEnabled()); |
157 | QCOMPARE(textureImage.metaObject(), creationChangeData->metaObject()); |
158 | |
159 | const auto generator = qSharedPointerCast<Qt3DRender::QImageTextureDataFunctor>(src: cloneData.generator); |
160 | QVERIFY(generator); |
161 | QCOMPARE(generator->isMirrored(), textureImage.isMirrored()); |
162 | QCOMPARE(generator->url(), textureImage.source()); |
163 | } |
164 | } |
165 | |
166 | void checkSourceUpdate() |
167 | { |
168 | // GIVEN |
169 | TestArbiter arbiter; |
170 | Qt3DRender::QTextureImage textureImage; |
171 | arbiter.setArbiterOnNode(&textureImage); |
172 | |
173 | { |
174 | // WHEN |
175 | textureImage.setSource(QUrl(QStringLiteral("Qt3D" ))); |
176 | QCoreApplication::processEvents(); |
177 | |
178 | // THEN |
179 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
180 | QCOMPARE(arbiter.dirtyNodes.front(), &textureImage); |
181 | |
182 | arbiter.events.clear(); |
183 | } |
184 | |
185 | { |
186 | // WHEN |
187 | textureImage.setSource(QUrl(QStringLiteral("Qt3D" ))); |
188 | QCoreApplication::processEvents(); |
189 | |
190 | // THEN |
191 | QCOMPARE(arbiter.events.size(), 0); |
192 | } |
193 | |
194 | } |
195 | |
196 | void checkMirroredUpdate() |
197 | { |
198 | // GIVEN |
199 | TestArbiter arbiter; |
200 | Qt3DRender::QTextureImage textureImage; |
201 | arbiter.setArbiterOnNode(&textureImage); |
202 | |
203 | { |
204 | // WHEN |
205 | textureImage.setMirrored(false); |
206 | QCoreApplication::processEvents(); |
207 | |
208 | // THEN |
209 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
210 | QCOMPARE(arbiter.dirtyNodes.front(), &textureImage); |
211 | |
212 | arbiter.events.clear(); |
213 | } |
214 | |
215 | { |
216 | // WHEN |
217 | textureImage.setMirrored(false); |
218 | QCoreApplication::processEvents(); |
219 | |
220 | // THEN |
221 | QCOMPARE(arbiter.events.size(), 0); |
222 | } |
223 | |
224 | } |
225 | |
226 | }; |
227 | |
228 | QTEST_MAIN(tst_QTextureImage) |
229 | |
230 | #include "tst_qtextureimage.moc" |
231 | |