| 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 | #include <QtTest/QTest> | 
| 30 | #include <QObject> | 
| 31 | #include <QMetaObject> | 
| 32 | #include <Qt3DRender/qrendertargetoutput.h> | 
| 33 | #include <Qt3DRender/private/qrendertargetoutput_p.h> | 
| 34 | #include <Qt3DRender/qabstracttexture.h> | 
| 35 | #include <Qt3DRender/qtexture.h> | 
| 36 | #include <QSignalSpy> | 
| 37 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> | 
| 38 | #include <Qt3DCore/qnodecreatedchange.h> | 
| 39 | #include "testpostmanarbiter.h" | 
| 40 |  | 
| 41 | class tst_QRenderTargetOutput : public QObject | 
| 42 | { | 
| 43 |     Q_OBJECT | 
| 44 |  | 
| 45 | private Q_SLOTS: | 
| 46 |  | 
| 47 |     void initTestCase() | 
| 48 |     { | 
| 49 |         qRegisterMetaType<Qt3DRender::QRenderTargetOutput::AttachmentPoint>(typeName: "AttachmentPoint" ); | 
| 50 |         qRegisterMetaType<Qt3DRender::QAbstractTexture::CubeMapFace>(typeName: "QAbstractTexture::CubeMapFace" ); | 
| 51 |     } | 
| 52 |  | 
| 53 |     void checkDefaultConstruction() | 
| 54 |     { | 
| 55 |         // GIVEN | 
| 56 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 57 |  | 
| 58 |         // THEN | 
| 59 |         QCOMPARE(renderTargetOutput.attachmentPoint(), Qt3DRender::QRenderTargetOutput::Color0); | 
| 60 |         QVERIFY(renderTargetOutput.texture() == nullptr); | 
| 61 |         QCOMPARE(renderTargetOutput.mipLevel(), 0); | 
| 62 |         QCOMPARE(renderTargetOutput.layer(), 0); | 
| 63 |         QCOMPARE(renderTargetOutput.face(), Qt3DRender::QAbstractTexture::CubeMapNegativeX); | 
| 64 |     } | 
| 65 |  | 
| 66 |     void checkPropertyChanges() | 
| 67 |     { | 
| 68 |         // GIVEN | 
| 69 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 70 |  | 
| 71 |         { | 
| 72 |             // WHEN | 
| 73 |             QSignalSpy spy(&renderTargetOutput, SIGNAL(attachmentPointChanged(AttachmentPoint))); | 
| 74 |             const Qt3DRender::QRenderTargetOutput::AttachmentPoint newValue = Qt3DRender::QRenderTargetOutput::Color15; | 
| 75 |             renderTargetOutput.setAttachmentPoint(newValue); | 
| 76 |  | 
| 77 |             // THEN | 
| 78 |             QVERIFY(spy.isValid()); | 
| 79 |             QCOMPARE(renderTargetOutput.attachmentPoint(), newValue); | 
| 80 |             QCOMPARE(spy.count(), 1); | 
| 81 |  | 
| 82 |             // WHEN | 
| 83 |             spy.clear(); | 
| 84 |             renderTargetOutput.setAttachmentPoint(newValue); | 
| 85 |  | 
| 86 |             // THEN | 
| 87 |             QCOMPARE(renderTargetOutput.attachmentPoint(), newValue); | 
| 88 |             QCOMPARE(spy.count(), 0); | 
| 89 |  | 
| 90 |         } | 
| 91 |         { | 
| 92 |             // WHEN | 
| 93 |             QSignalSpy spy(&renderTargetOutput, SIGNAL(textureChanged(QAbstractTexture *))); | 
| 94 |             Qt3DRender::QTexture3D newValue; | 
| 95 |             renderTargetOutput.setTexture(&newValue); | 
| 96 |  | 
| 97 |             // THEN | 
| 98 |             QVERIFY(spy.isValid()); | 
| 99 |             QCOMPARE(renderTargetOutput.texture(), &newValue); | 
| 100 |             QCOMPARE(spy.count(), 1); | 
| 101 |  | 
| 102 |             // WHEN | 
| 103 |             spy.clear(); | 
| 104 |             renderTargetOutput.setTexture(&newValue); | 
| 105 |  | 
| 106 |             // THEN | 
| 107 |             QCOMPARE(renderTargetOutput.texture(), &newValue); | 
| 108 |             QCOMPARE(spy.count(), 0); | 
| 109 |  | 
| 110 |         } | 
| 111 |         { | 
| 112 |             // WHEN | 
| 113 |             QSignalSpy spy(&renderTargetOutput, SIGNAL(mipLevelChanged(int))); | 
| 114 |             const int newValue = 5; | 
| 115 |             renderTargetOutput.setMipLevel(newValue); | 
| 116 |  | 
| 117 |             // THEN | 
| 118 |             QVERIFY(spy.isValid()); | 
| 119 |             QCOMPARE(renderTargetOutput.mipLevel(), newValue); | 
| 120 |             QCOMPARE(spy.count(), 1); | 
| 121 |  | 
| 122 |             // WHEN | 
| 123 |             spy.clear(); | 
| 124 |             renderTargetOutput.setMipLevel(newValue); | 
| 125 |  | 
| 126 |             // THEN | 
| 127 |             QCOMPARE(renderTargetOutput.mipLevel(), newValue); | 
| 128 |             QCOMPARE(spy.count(), 0); | 
| 129 |  | 
| 130 |         } | 
| 131 |         { | 
| 132 |             // WHEN | 
| 133 |             QSignalSpy spy(&renderTargetOutput, SIGNAL(layerChanged(int))); | 
| 134 |             const int newValue = 300; | 
| 135 |             renderTargetOutput.setLayer(newValue); | 
| 136 |  | 
| 137 |             // THEN | 
| 138 |             QVERIFY(spy.isValid()); | 
| 139 |             QCOMPARE(renderTargetOutput.layer(), newValue); | 
| 140 |             QCOMPARE(spy.count(), 1); | 
| 141 |  | 
| 142 |             // WHEN | 
| 143 |             spy.clear(); | 
| 144 |             renderTargetOutput.setLayer(newValue); | 
| 145 |  | 
| 146 |             // THEN | 
| 147 |             QCOMPARE(renderTargetOutput.layer(), newValue); | 
| 148 |             QCOMPARE(spy.count(), 0); | 
| 149 |  | 
| 150 |         } | 
| 151 |         { | 
| 152 |             // WHEN | 
| 153 |             QSignalSpy spy(&renderTargetOutput, SIGNAL(faceChanged(QAbstractTexture::CubeMapFace))); | 
| 154 |             const Qt3DRender::QAbstractTexture::CubeMapFace newValue = Qt3DRender::QAbstractTexture::CubeMapNegativeZ; | 
| 155 |             renderTargetOutput.setFace(newValue); | 
| 156 |  | 
| 157 |             // THEN | 
| 158 |             QVERIFY(spy.isValid()); | 
| 159 |             QCOMPARE(renderTargetOutput.face(), newValue); | 
| 160 |             QCOMPARE(spy.count(), 1); | 
| 161 |  | 
| 162 |             // WHEN | 
| 163 |             spy.clear(); | 
| 164 |             renderTargetOutput.setFace(newValue); | 
| 165 |  | 
| 166 |             // THEN | 
| 167 |             QCOMPARE(renderTargetOutput.face(), newValue); | 
| 168 |             QCOMPARE(spy.count(), 0); | 
| 169 |  | 
| 170 |         } | 
| 171 |     } | 
| 172 |  | 
| 173 |     void checkCreationData() | 
| 174 |     { | 
| 175 |         // GIVEN | 
| 176 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 177 |  | 
| 178 |         renderTargetOutput.setAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color5); | 
| 179 |         renderTargetOutput.setMipLevel(10); | 
| 180 |         renderTargetOutput.setLayer(2); | 
| 181 |         renderTargetOutput.setFace(Qt3DRender::QAbstractTexture::CubeMapNegativeY); | 
| 182 |  | 
| 183 |         // WHEN | 
| 184 |         QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; | 
| 185 |  | 
| 186 |         { | 
| 187 |             Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderTargetOutput); | 
| 188 |             creationChanges = creationChangeGenerator.creationChanges(); | 
| 189 |         } | 
| 190 |  | 
| 191 |         // THEN | 
| 192 |         { | 
| 193 |             QCOMPARE(creationChanges.size(), 1); | 
| 194 |  | 
| 195 |             const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderTargetOutputData>>(src: creationChanges.first()); | 
| 196 |             const Qt3DRender::QRenderTargetOutputData cloneData = creationChangeData->data; | 
| 197 |  | 
| 198 |             QCOMPARE(renderTargetOutput.attachmentPoint(), cloneData.attachmentPoint); | 
| 199 |             QCOMPARE(Qt3DCore::QNodeId(), cloneData.textureId); | 
| 200 |             QCOMPARE(renderTargetOutput.mipLevel(), cloneData.mipLevel); | 
| 201 |             QCOMPARE(renderTargetOutput.layer(), cloneData.layer); | 
| 202 |             QCOMPARE(renderTargetOutput.face(), cloneData.face); | 
| 203 |             QCOMPARE(renderTargetOutput.id(), creationChangeData->subjectId()); | 
| 204 |             QCOMPARE(renderTargetOutput.isEnabled(), true); | 
| 205 |             QCOMPARE(renderTargetOutput.isEnabled(), creationChangeData->isNodeEnabled()); | 
| 206 |             QCOMPARE(renderTargetOutput.metaObject(), creationChangeData->metaObject()); | 
| 207 |         } | 
| 208 |  | 
| 209 |         // WHEN | 
| 210 |         renderTargetOutput.setEnabled(false); | 
| 211 |  | 
| 212 |         { | 
| 213 |             Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderTargetOutput); | 
| 214 |             creationChanges = creationChangeGenerator.creationChanges(); | 
| 215 |         } | 
| 216 |  | 
| 217 |         // THEN | 
| 218 |         { | 
| 219 |             QCOMPARE(creationChanges.size(), 1); | 
| 220 |  | 
| 221 |             const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderTargetOutputData>>(src: creationChanges.first()); | 
| 222 |             const Qt3DRender::QRenderTargetOutputData cloneData = creationChangeData->data; | 
| 223 |  | 
| 224 |             QCOMPARE(renderTargetOutput.attachmentPoint(), cloneData.attachmentPoint); | 
| 225 |             QCOMPARE(Qt3DCore::QNodeId(), cloneData.textureId); | 
| 226 |             QCOMPARE(renderTargetOutput.mipLevel(), cloneData.mipLevel); | 
| 227 |             QCOMPARE(renderTargetOutput.layer(), cloneData.layer); | 
| 228 |             QCOMPARE(renderTargetOutput.face(), cloneData.face); | 
| 229 |             QCOMPARE(renderTargetOutput.id(), creationChangeData->subjectId()); | 
| 230 |             QCOMPARE(renderTargetOutput.isEnabled(), false); | 
| 231 |             QCOMPARE(renderTargetOutput.isEnabled(), creationChangeData->isNodeEnabled()); | 
| 232 |             QCOMPARE(renderTargetOutput.metaObject(), creationChangeData->metaObject()); | 
| 233 |         } | 
| 234 |     } | 
| 235 |  | 
| 236 |     void checkAttachmentPointUpdate() | 
| 237 |     { | 
| 238 |         // GIVEN | 
| 239 |         TestArbiter arbiter; | 
| 240 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 241 |         arbiter.setArbiterOnNode(&renderTargetOutput); | 
| 242 |  | 
| 243 |         { | 
| 244 |             // WHEN | 
| 245 |             renderTargetOutput.setAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1); | 
| 246 |             QCoreApplication::processEvents(); | 
| 247 |  | 
| 248 |             // THEN | 
| 249 |             QCOMPARE(arbiter.events.size(), 0); | 
| 250 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 251 |             QCOMPARE(arbiter.dirtyNodes.front(), &renderTargetOutput); | 
| 252 |  | 
| 253 |             arbiter.dirtyNodes.clear(); | 
| 254 |         } | 
| 255 |  | 
| 256 |         { | 
| 257 |             // WHEN | 
| 258 |             renderTargetOutput.setAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1); | 
| 259 |             QCoreApplication::processEvents(); | 
| 260 |  | 
| 261 |             // THEN | 
| 262 |             QCOMPARE(arbiter.events.size(), 0); | 
| 263 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 264 |         } | 
| 265 |  | 
| 266 |     } | 
| 267 |  | 
| 268 |     void checkTextureUpdate() | 
| 269 |     { | 
| 270 |         // GIVEN | 
| 271 |         TestArbiter arbiter; | 
| 272 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 273 |         Qt3DRender::QTexture3D texture; | 
| 274 |         arbiter.setArbiterOnNode(&renderTargetOutput); | 
| 275 |  | 
| 276 |         { | 
| 277 |             // WHEN | 
| 278 |             renderTargetOutput.setTexture(&texture); | 
| 279 |             QCoreApplication::processEvents(); | 
| 280 |  | 
| 281 |             // THEN | 
| 282 |             QCOMPARE(arbiter.events.size(), 0); | 
| 283 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 284 |             QCOMPARE(arbiter.dirtyNodes.front(), &renderTargetOutput); | 
| 285 |  | 
| 286 |             arbiter.dirtyNodes.clear(); | 
| 287 |         } | 
| 288 |  | 
| 289 |         { | 
| 290 |             // WHEN | 
| 291 |             renderTargetOutput.setTexture(&texture); | 
| 292 |             QCoreApplication::processEvents(); | 
| 293 |  | 
| 294 |             // THEN | 
| 295 |             QCOMPARE(arbiter.events.size(), 0); | 
| 296 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 297 |         } | 
| 298 |  | 
| 299 |     } | 
| 300 |  | 
| 301 |     void checkMipLevelUpdate() | 
| 302 |     { | 
| 303 |         // GIVEN | 
| 304 |         TestArbiter arbiter; | 
| 305 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 306 |         arbiter.setArbiterOnNode(&renderTargetOutput); | 
| 307 |  | 
| 308 |         { | 
| 309 |             // WHEN | 
| 310 |             renderTargetOutput.setMipLevel(6); | 
| 311 |             QCoreApplication::processEvents(); | 
| 312 |  | 
| 313 |             // THEN | 
| 314 |             QCOMPARE(arbiter.events.size(), 0); | 
| 315 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 316 |             QCOMPARE(arbiter.dirtyNodes.front(), &renderTargetOutput); | 
| 317 |  | 
| 318 |             arbiter.dirtyNodes.clear(); | 
| 319 |         } | 
| 320 |  | 
| 321 |         { | 
| 322 |             // WHEN | 
| 323 |             renderTargetOutput.setMipLevel(6); | 
| 324 |             QCoreApplication::processEvents(); | 
| 325 |  | 
| 326 |             // THEN | 
| 327 |             QCOMPARE(arbiter.events.size(), 0); | 
| 328 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 329 |         } | 
| 330 |  | 
| 331 |     } | 
| 332 |  | 
| 333 |     void checkLayerUpdate() | 
| 334 |     { | 
| 335 |         // GIVEN | 
| 336 |         TestArbiter arbiter; | 
| 337 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 338 |         arbiter.setArbiterOnNode(&renderTargetOutput); | 
| 339 |  | 
| 340 |         { | 
| 341 |             // WHEN | 
| 342 |             renderTargetOutput.setLayer(8); | 
| 343 |             QCoreApplication::processEvents(); | 
| 344 |  | 
| 345 |             // THEN | 
| 346 |             QCOMPARE(arbiter.events.size(), 0); | 
| 347 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 348 |             QCOMPARE(arbiter.dirtyNodes.front(), &renderTargetOutput); | 
| 349 |  | 
| 350 |             arbiter.dirtyNodes.clear(); | 
| 351 |         } | 
| 352 |  | 
| 353 |         { | 
| 354 |             // WHEN | 
| 355 |             renderTargetOutput.setLayer(8); | 
| 356 |             QCoreApplication::processEvents(); | 
| 357 |  | 
| 358 |             // THEN | 
| 359 |             QCOMPARE(arbiter.events.size(), 0); | 
| 360 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 361 |         } | 
| 362 |  | 
| 363 |     } | 
| 364 |  | 
| 365 |     void checkFaceUpdate() | 
| 366 |     { | 
| 367 |         // GIVEN | 
| 368 |         TestArbiter arbiter; | 
| 369 |         Qt3DRender::QRenderTargetOutput renderTargetOutput; | 
| 370 |         arbiter.setArbiterOnNode(&renderTargetOutput); | 
| 371 |  | 
| 372 |         { | 
| 373 |             // WHEN | 
| 374 |             renderTargetOutput.setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveY); | 
| 375 |             QCoreApplication::processEvents(); | 
| 376 |  | 
| 377 |             // THEN | 
| 378 |             QCOMPARE(arbiter.events.size(), 0); | 
| 379 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 380 |             QCOMPARE(arbiter.dirtyNodes.front(), &renderTargetOutput); | 
| 381 |  | 
| 382 |             arbiter.dirtyNodes.clear(); | 
| 383 |         } | 
| 384 |  | 
| 385 |         { | 
| 386 |             // WHEN | 
| 387 |             renderTargetOutput.setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveY); | 
| 388 |             QCoreApplication::processEvents(); | 
| 389 |  | 
| 390 |             // THEN | 
| 391 |             QCOMPARE(arbiter.events.size(), 0); | 
| 392 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 393 |         } | 
| 394 |  | 
| 395 |     } | 
| 396 |  | 
| 397 | }; | 
| 398 |  | 
| 399 | QTEST_MAIN(tst_QRenderTargetOutput) | 
| 400 |  | 
| 401 | #include "tst_qrendertargetoutput.moc" | 
| 402 |  |