| 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 | #include <QtTest/QTest> |
| 30 | #include <Qt3DCore/qentity.h> |
| 31 | #include <Qt3DCore/private/qnode_p.h> |
| 32 | #include <Qt3DCore/private/qscene_p.h> |
| 33 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
| 34 | #include <Qt3DCore/qtransform.h> |
| 35 | |
| 36 | #include <Qt3DRender/qsceneloader.h> |
| 37 | #include <Qt3DRender/qcameralens.h> |
| 38 | #include <Qt3DRender/qmaterial.h> |
| 39 | #include <Qt3DRender/qgeometryrenderer.h> |
| 40 | #include <Qt3DRender/qspotlight.h> |
| 41 | #include <Qt3DRender/private/qsceneloader_p.h> |
| 42 | #include <QSignalSpy> |
| 43 | |
| 44 | #include "testpostmanarbiter.h" |
| 45 | |
| 46 | class tst_QSceneLoader: public QObject |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | |
| 50 | private Q_SLOTS: |
| 51 | |
| 52 | void checkInitialState() |
| 53 | { |
| 54 | // GIVEN |
| 55 | Qt3DRender::QSceneLoader sceneLoader; |
| 56 | |
| 57 | // THEN |
| 58 | QCOMPARE(sceneLoader.status(), Qt3DRender::QSceneLoader::None); |
| 59 | QVERIFY(sceneLoader.source().isEmpty()); |
| 60 | QVERIFY(static_cast<Qt3DRender::QSceneLoaderPrivate *>(Qt3DCore::QNodePrivate::get(&sceneLoader))->m_subTreeRoot == nullptr); |
| 61 | } |
| 62 | |
| 63 | void checkCreationData() |
| 64 | { |
| 65 | // GIVEN |
| 66 | Qt3DRender::QSceneLoader sceneLoader; |
| 67 | const QUrl sceneUrl = QUrl(QStringLiteral("LA ConventionCenter" )); |
| 68 | sceneLoader.setSource(sceneUrl); |
| 69 | |
| 70 | // WHEN |
| 71 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
| 72 | |
| 73 | { |
| 74 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&sceneLoader); |
| 75 | creationChanges = creationChangeGenerator.creationChanges(); |
| 76 | } |
| 77 | |
| 78 | // THEN |
| 79 | { |
| 80 | QCOMPARE(creationChanges.size(), 1); |
| 81 | |
| 82 | const auto creationChangeData = |
| 83 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QSceneLoaderData>>(src: creationChanges.first()); |
| 84 | const Qt3DRender::QSceneLoaderData cloneData = creationChangeData->data; |
| 85 | |
| 86 | QCOMPARE(sceneLoader.id(), creationChangeData->subjectId()); |
| 87 | QCOMPARE(sceneLoader.isEnabled(), true); |
| 88 | QCOMPARE(sceneLoader.isEnabled(), creationChangeData->isNodeEnabled()); |
| 89 | QCOMPARE(sceneLoader.metaObject(), creationChangeData->metaObject()); |
| 90 | QCOMPARE(sceneLoader.source(), sceneUrl); |
| 91 | QCOMPARE(sceneLoader.source(), cloneData.source); |
| 92 | } |
| 93 | |
| 94 | // WHEN |
| 95 | sceneLoader.setEnabled(false); |
| 96 | |
| 97 | { |
| 98 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&sceneLoader); |
| 99 | creationChanges = creationChangeGenerator.creationChanges(); |
| 100 | } |
| 101 | |
| 102 | // THEN |
| 103 | { |
| 104 | QCOMPARE(creationChanges.size(), 1); |
| 105 | |
| 106 | const auto creationChangeData = |
| 107 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QSceneLoaderData>>(src: creationChanges.first()); |
| 108 | const Qt3DRender::QSceneLoaderData cloneData = creationChangeData->data; |
| 109 | |
| 110 | QCOMPARE(sceneLoader.id(), creationChangeData->subjectId()); |
| 111 | QCOMPARE(sceneLoader.isEnabled(), false); |
| 112 | QCOMPARE(sceneLoader.isEnabled(), creationChangeData->isNodeEnabled()); |
| 113 | QCOMPARE(sceneLoader.metaObject(), creationChangeData->metaObject()); |
| 114 | QCOMPARE(sceneLoader.source(), sceneUrl); |
| 115 | QCOMPARE(sceneLoader.source(), cloneData.source); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void checkSourcePropertyUpdate() |
| 120 | { |
| 121 | // GIVEN |
| 122 | TestArbiter arbiter; |
| 123 | QScopedPointer<Qt3DRender::QSceneLoader> sceneLoader(new Qt3DRender::QSceneLoader()); |
| 124 | arbiter.setArbiterOnNode(sceneLoader.data()); |
| 125 | |
| 126 | // WHEN |
| 127 | const QUrl sourceUrl = QUrl(QStringLiteral("Milwaukee" )); |
| 128 | sceneLoader->setSource(sourceUrl); |
| 129 | QCoreApplication::processEvents(); |
| 130 | |
| 131 | // THEN |
| 132 | QCOMPARE(arbiter.events.size(), 0); |
| 133 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 134 | QCOMPARE(arbiter.dirtyNodes.front(), sceneLoader.data()); |
| 135 | |
| 136 | arbiter.dirtyNodes.clear(); |
| 137 | } |
| 138 | |
| 139 | void checkEntities() |
| 140 | { |
| 141 | // GIVEN |
| 142 | const QString e1Name = QStringLiteral("e1" ); |
| 143 | const QString e2Name = QStringLiteral("e2" ); |
| 144 | const QString e3Name = QStringLiteral("e3" ); |
| 145 | const QString e4Name = QStringLiteral("e4" ); |
| 146 | Qt3DRender::QSceneLoader loader; |
| 147 | Qt3DCore::QEntity e1; // Scene container entity, will not be listed among scene entities |
| 148 | Qt3DCore::QEntity e2(&e1); |
| 149 | Qt3DCore::QEntity e3(&e1); |
| 150 | Qt3DCore::QEntity e4(&e3); |
| 151 | e1.setObjectName(e1Name); |
| 152 | e2.setObjectName(e2Name); |
| 153 | e3.setObjectName(e3Name); |
| 154 | e4.setObjectName(e4Name); |
| 155 | Qt3DCore::QTransform trans; |
| 156 | Qt3DRender::QMaterial mat; |
| 157 | Qt3DRender::QCameraLens cam; |
| 158 | Qt3DRender::QSpotLight light; |
| 159 | Qt3DRender::QGeometryRenderer mesh; |
| 160 | e2.addComponent(comp: &trans); |
| 161 | e2.addComponent(comp: &cam); |
| 162 | e3.addComponent(comp: &mat); |
| 163 | e3.addComponent(comp: &mesh); |
| 164 | e4.addComponent(comp: &light); |
| 165 | |
| 166 | // WHEN |
| 167 | static_cast<Qt3DRender::QSceneLoaderPrivate *>( |
| 168 | Qt3DCore::QNodePrivate::get(q: &loader))->populateEntityMap(parentEntity: &e1); |
| 169 | |
| 170 | // THEN |
| 171 | QStringList entityNames = loader.entityNames(); |
| 172 | entityNames.sort(); |
| 173 | QVERIFY(entityNames.size() == 3); |
| 174 | QCOMPARE(entityNames.at(0), e2Name); |
| 175 | QCOMPARE(entityNames.at(1), e3Name); |
| 176 | QCOMPARE(entityNames.at(2), e4Name); |
| 177 | |
| 178 | QCOMPARE(loader.entity(e1Name), nullptr); |
| 179 | QCOMPARE(loader.entity(e2Name), &e2); |
| 180 | QCOMPARE(loader.entity(e3Name), &e3); |
| 181 | QCOMPARE(loader.entity(e4Name), &e4); |
| 182 | |
| 183 | QCOMPARE(loader.component(e1Name, Qt3DRender::QSceneLoader::UnknownComponent), nullptr); |
| 184 | QCOMPARE(loader.component(e1Name, Qt3DRender::QSceneLoader::TransformComponent), nullptr); |
| 185 | QCOMPARE(loader.component(e1Name, Qt3DRender::QSceneLoader::GeometryRendererComponent), nullptr); |
| 186 | QCOMPARE(loader.component(e1Name, Qt3DRender::QSceneLoader::MaterialComponent), nullptr); |
| 187 | QCOMPARE(loader.component(e1Name, Qt3DRender::QSceneLoader::LightComponent), nullptr); |
| 188 | QCOMPARE(loader.component(e1Name, Qt3DRender::QSceneLoader::CameraLensComponent), nullptr); |
| 189 | |
| 190 | QCOMPARE(loader.component(e2Name, Qt3DRender::QSceneLoader::UnknownComponent), nullptr); |
| 191 | QCOMPARE(loader.component(e2Name, Qt3DRender::QSceneLoader::TransformComponent), &trans); |
| 192 | QCOMPARE(loader.component(e2Name, Qt3DRender::QSceneLoader::GeometryRendererComponent), nullptr); |
| 193 | QCOMPARE(loader.component(e2Name, Qt3DRender::QSceneLoader::MaterialComponent), nullptr); |
| 194 | QCOMPARE(loader.component(e2Name, Qt3DRender::QSceneLoader::LightComponent), nullptr); |
| 195 | QCOMPARE(loader.component(e2Name, Qt3DRender::QSceneLoader::CameraLensComponent), &cam); |
| 196 | |
| 197 | QCOMPARE(loader.component(e3Name, Qt3DRender::QSceneLoader::UnknownComponent), nullptr); |
| 198 | QCOMPARE(loader.component(e3Name, Qt3DRender::QSceneLoader::TransformComponent), nullptr); |
| 199 | QCOMPARE(loader.component(e3Name, Qt3DRender::QSceneLoader::GeometryRendererComponent), &mesh); |
| 200 | QCOMPARE(loader.component(e3Name, Qt3DRender::QSceneLoader::MaterialComponent), &mat); |
| 201 | QCOMPARE(loader.component(e3Name, Qt3DRender::QSceneLoader::LightComponent), nullptr); |
| 202 | QCOMPARE(loader.component(e3Name, Qt3DRender::QSceneLoader::CameraLensComponent), nullptr); |
| 203 | |
| 204 | QCOMPARE(loader.component(e4Name, Qt3DRender::QSceneLoader::UnknownComponent), nullptr); |
| 205 | QCOMPARE(loader.component(e4Name, Qt3DRender::QSceneLoader::TransformComponent), nullptr); |
| 206 | QCOMPARE(loader.component(e4Name, Qt3DRender::QSceneLoader::GeometryRendererComponent), nullptr); |
| 207 | QCOMPARE(loader.component(e4Name, Qt3DRender::QSceneLoader::MaterialComponent), nullptr); |
| 208 | QCOMPARE(loader.component(e4Name, Qt3DRender::QSceneLoader::LightComponent), &light); |
| 209 | QCOMPARE(loader.component(e4Name, Qt3DRender::QSceneLoader::CameraLensComponent), nullptr); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | QTEST_MAIN(tst_QSceneLoader) |
| 214 | |
| 215 | #include "tst_qsceneloader.moc" |
| 216 | |
| 217 | |