| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2017 The Qt Company Ltd and/or its subsidiary(-ies). | 
| 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:LGPL$ | 
| 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 Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include <QtTest/qtest.h> | 
| 41 |  | 
| 42 | #include <QtCore/QScopedPointer> | 
| 43 | #include <QtCore/private/qfactoryloader_p.h> | 
| 44 |  | 
| 45 | #include <Qt3DRender/qattribute.h> | 
| 46 | #include <Qt3DRender/qgeometry.h> | 
| 47 |  | 
| 48 | #include <Qt3DRender/private/qgeometryloaderfactory_p.h> | 
| 49 | #include <Qt3DRender/private/qgeometryloaderinterface_p.h> | 
| 50 |  | 
| 51 | #include "../../../../src/plugins/geometryloaders/qtgeometryloaders-config.h" | 
| 52 |  | 
| 53 | using namespace Qt3DRender; | 
| 54 |  | 
| 55 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, geometryLoader, | 
| 56 |     (QGeometryLoaderFactory_iid, QLatin1String("/geometryloaders" ), Qt::CaseInsensitive)) | 
| 57 |  | 
| 58 | class tst_geometryloaders : public QObject | 
| 59 | { | 
| 60 |     Q_OBJECT | 
| 61 |  | 
| 62 | private Q_SLOTS: | 
| 63 |     void testOBJLoader_data(); | 
| 64 |     void testOBJLoader(); | 
| 65 |     void testPLYLoader(); | 
| 66 |     void testSTLLoader(); | 
| 67 |     void testGLTFLoader(); | 
| 68 | #ifdef QT_3DGEOMETRYLOADERS_FBX | 
| 69 |     void testFBXLoader(); | 
| 70 | #endif | 
| 71 | }; | 
| 72 |  | 
| 73 | void tst_geometryloaders::testOBJLoader_data() | 
| 74 | { | 
| 75 |     QTest::addColumn<QString>(name: "fileName" ); | 
| 76 |     QTest::newRow(dataTag: "nominal case" ) << QStringLiteral(":/cube.obj" ); | 
| 77 |     QTest::newRow(dataTag: "trailing space + crlf" ) << QStringLiteral(":/cube2.obj" ); | 
| 78 | } | 
| 79 | void tst_geometryloaders::testOBJLoader() | 
| 80 | { | 
| 81 |     QScopedPointer<QGeometryLoaderInterface> loader; | 
| 82 |     loader.reset(other: qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(loader: geometryLoader(), QStringLiteral("obj" ))); | 
| 83 |     QVERIFY(loader); | 
| 84 |     if (!loader) | 
| 85 |         return; | 
| 86 |  | 
| 87 |     QFETCH(QString, fileName); | 
| 88 |     QFile file(fileName); | 
| 89 |     if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { | 
| 90 |         qDebug(msg: "Could not open test file for reading" ); | 
| 91 |         return; | 
| 92 |     } | 
| 93 |  | 
| 94 |     bool loaded = loader->load(ioDev: &file, QStringLiteral("Cube" )); | 
| 95 |     QVERIFY(loaded); | 
| 96 |     if (!loaded) | 
| 97 |         return; | 
| 98 |  | 
| 99 |     QGeometry *geometry = loader->geometry(); | 
| 100 |     QVERIFY(geometry); | 
| 101 |     if (!geometry) | 
| 102 |         return; | 
| 103 |  | 
| 104 |     QCOMPARE(geometry->attributes().count(), 3); | 
| 105 |     for (QAttribute *attr : geometry->attributes()) { | 
| 106 |         switch (attr->attributeType()) { | 
| 107 |         case QAttribute::IndexAttribute: | 
| 108 |             QCOMPARE(attr->count(), 36u); | 
| 109 |             break; | 
| 110 |         case QAttribute::VertexAttribute: | 
| 111 |             QCOMPARE(attr->count(), 24u); | 
| 112 |             break; | 
| 113 |         default: | 
| 114 |             Q_UNREACHABLE(); | 
| 115 |             break; | 
| 116 |         } | 
| 117 |     } | 
| 118 |  | 
| 119 |     file.close(); | 
| 120 | } | 
| 121 |  | 
| 122 | void tst_geometryloaders::testPLYLoader() | 
| 123 | { | 
| 124 |     QScopedPointer<QGeometryLoaderInterface> loader; | 
| 125 |     loader.reset(other: qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(loader: geometryLoader(), QStringLiteral("ply" ))); | 
| 126 |     QVERIFY(loader); | 
| 127 |     if (!loader) | 
| 128 |         return; | 
| 129 |  | 
| 130 |     QFile file(QStringLiteral(":/cube.ply" )); | 
| 131 |     if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { | 
| 132 |         qDebug(msg: "Could not open test file for reading" ); | 
| 133 |         return; | 
| 134 |     } | 
| 135 |  | 
| 136 |     bool loaded = loader->load(ioDev: &file, QStringLiteral("Cube" )); | 
| 137 |     QVERIFY(loaded); | 
| 138 |     if (!loaded) | 
| 139 |         return; | 
| 140 |  | 
| 141 |     QGeometry *geometry = loader->geometry(); | 
| 142 |     QVERIFY(geometry); | 
| 143 |     if (!geometry) | 
| 144 |         return; | 
| 145 |  | 
| 146 |     QCOMPARE(geometry->attributes().count(), 3); | 
| 147 |     for (QAttribute *attr : geometry->attributes()) { | 
| 148 |         switch (attr->attributeType()) { | 
| 149 |         case QAttribute::IndexAttribute: | 
| 150 |             QCOMPARE(attr->count(), 36u); | 
| 151 |             break; | 
| 152 |         case QAttribute::VertexAttribute: | 
| 153 |             QCOMPARE(attr->count(), 24u); | 
| 154 |             break; | 
| 155 |         default: | 
| 156 |             Q_UNREACHABLE(); | 
| 157 |             break; | 
| 158 |         } | 
| 159 |     } | 
| 160 |  | 
| 161 |     file.close(); | 
| 162 | } | 
| 163 |  | 
| 164 | void tst_geometryloaders::testSTLLoader() | 
| 165 | { | 
| 166 |     QScopedPointer<QGeometryLoaderInterface> loader; | 
| 167 |     loader.reset(other: qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(loader: geometryLoader(), QStringLiteral("stl" ))); | 
| 168 |     QVERIFY(loader); | 
| 169 |     if (!loader) | 
| 170 |         return; | 
| 171 |  | 
| 172 |     QFile file(QStringLiteral(":/cube.stl" )); | 
| 173 |     if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { | 
| 174 |         qDebug(msg: "Could not open test file for reading" ); | 
| 175 |         return; | 
| 176 |     } | 
| 177 |  | 
| 178 |     bool loaded = loader->load(ioDev: &file, QStringLiteral("Cube" )); | 
| 179 |     QVERIFY(loaded); | 
| 180 |     if (!loaded) | 
| 181 |         return; | 
| 182 |  | 
| 183 |     QGeometry *geometry = loader->geometry(); | 
| 184 |     QVERIFY(geometry); | 
| 185 |     if (!geometry) | 
| 186 |         return; | 
| 187 |  | 
| 188 |     QCOMPARE(geometry->attributes().count(), 3); | 
| 189 |     for (QAttribute *attr : geometry->attributes()) { | 
| 190 |         QCOMPARE(attr->count(), 36u); | 
| 191 |     } | 
| 192 |  | 
| 193 |     file.close(); | 
| 194 | } | 
| 195 |  | 
| 196 | void tst_geometryloaders::testGLTFLoader() | 
| 197 | { | 
| 198 |     QScopedPointer<QGeometryLoaderInterface> loader; | 
| 199 |     loader.reset(other: qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(loader: geometryLoader(), QStringLiteral("gltf" ))); | 
| 200 |     QVERIFY(loader); | 
| 201 |     if (!loader) | 
| 202 |         return; | 
| 203 |  | 
| 204 |     QFile file(QStringLiteral(":/cube.gltf" )); | 
| 205 |     if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { | 
| 206 |         qDebug(msg: "Could not open test file for reading" ); | 
| 207 |         return; | 
| 208 |     } | 
| 209 |  | 
| 210 |     bool loaded = loader->load(ioDev: &file, QStringLiteral("Cube" )); | 
| 211 |     QVERIFY(loaded); | 
| 212 |     if (!loaded) | 
| 213 |         return; | 
| 214 |  | 
| 215 |     QGeometry *geometry = loader->geometry(); | 
| 216 |     QVERIFY(geometry); | 
| 217 |     if (!geometry) | 
| 218 |         return; | 
| 219 |  | 
| 220 |     QCOMPARE(geometry->attributes().count(), 3); | 
| 221 |     for (QAttribute *attr : geometry->attributes()) { | 
| 222 |         switch (attr->attributeType()) { | 
| 223 |         case QAttribute::IndexAttribute: | 
| 224 |             QCOMPARE(attr->count(), 36u); | 
| 225 |             break; | 
| 226 |         case QAttribute::VertexAttribute: | 
| 227 |             QCOMPARE(attr->count(), 24u); | 
| 228 |             break; | 
| 229 |         default: | 
| 230 |             Q_UNREACHABLE(); | 
| 231 |             break; | 
| 232 |         } | 
| 233 |     } | 
| 234 |  | 
| 235 |     file.close(); | 
| 236 | } | 
| 237 |  | 
| 238 | #ifdef QT_3DGEOMETRYLOADERS_FBX | 
| 239 | void tst_geometryloaders::testFBXLoader() | 
| 240 | { | 
| 241 |     QScopedPointer<QGeometryLoaderInterface> loader; | 
| 242 |     loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), QStringLiteral("fbx" ))); | 
| 243 |     QVERIFY(loader); | 
| 244 |     if (!loader) | 
| 245 |         return; | 
| 246 |  | 
| 247 |     QFile file(QStringLiteral(":/cube.fbx" )); | 
| 248 |     if (!file.open(QIODevice::ReadOnly)) { | 
| 249 |         qDebug("Could not open test file for reading" ); | 
| 250 |         return; | 
| 251 |     } | 
| 252 |  | 
| 253 |     bool loaded = loader->load(&file, QStringLiteral("Cube" )); | 
| 254 |     QVERIFY(loaded); | 
| 255 |     if (!loaded) | 
| 256 |         return; | 
| 257 |  | 
| 258 |     QGeometry *geometry = loader->geometry(); | 
| 259 |     QVERIFY(geometry); | 
| 260 |     if (!geometry) | 
| 261 |         return; | 
| 262 |  | 
| 263 |     QCOMPARE(geometry->attributes().count(), 3); | 
| 264 |     for (QAttribute *attr : geometry->attributes()) { | 
| 265 |         switch (attr->attributeType()) { | 
| 266 |         case QAttribute::IndexAttribute: | 
| 267 |             QCOMPARE(attr->count(), 36u); | 
| 268 |             break; | 
| 269 |         case QAttribute::VertexAttribute: | 
| 270 |             QCOMPARE(attr->count(), 24u); | 
| 271 |             break; | 
| 272 |         } | 
| 273 |     } | 
| 274 |  | 
| 275 |     file.close(); | 
| 276 | } | 
| 277 | #endif | 
| 278 |  | 
| 279 | QTEST_MAIN(tst_geometryloaders) | 
| 280 |  | 
| 281 | #include "tst_geometryloaders.moc" | 
| 282 |  |