| 1 | // Copyright (C) 2017 The Qt Company Ltd and/or its subsidiary(-ies). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/qtest.h> |
| 5 | |
| 6 | #include <QtCore/QScopedPointer> |
| 7 | #include <QtCore/private/qfactoryloader_p.h> |
| 8 | |
| 9 | #include <Qt3DCore/qattribute.h> |
| 10 | #include <Qt3DCore/qgeometry.h> |
| 11 | |
| 12 | #include <Qt3DRender/private/qgeometryloaderfactory_p.h> |
| 13 | #include <Qt3DRender/private/qgeometryloaderinterface_p.h> |
| 14 | |
| 15 | #include "../../../../src/plugins/geometryloaders/qtgeometryloaders-config.h" |
| 16 | |
| 17 | using namespace Qt3DCore; |
| 18 | using namespace Qt3DRender; |
| 19 | |
| 20 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, geometryLoader, |
| 21 | (QGeometryLoaderFactory_iid, QLatin1String("/geometryloaders"), Qt::CaseInsensitive)) |
| 22 | |
| 23 | class tst_geometryloaders : public QObject |
| 24 | { |
| 25 | Q_OBJECT |
| 26 | |
| 27 | private Q_SLOTS: |
| 28 | void testOBJLoader_data(); |
| 29 | void testOBJLoader(); |
| 30 | void testPLYLoader(); |
| 31 | void testSTLLoader(); |
| 32 | void testGLTFLoader(); |
| 33 | #ifdef QT_3DGEOMETRYLOADERS_FBX |
| 34 | void testFBXLoader(); |
| 35 | #endif |
| 36 | }; |
| 37 | |
| 38 | void tst_geometryloaders::testOBJLoader_data() |
| 39 | { |
| 40 | QTest::addColumn<QString>("fileName"); |
| 41 | QTest::newRow("nominal case") << QStringLiteral(":/cube.obj"); |
| 42 | QTest::newRow("trailing space + crlf") << QStringLiteral(":/cube2.obj"); |
| 43 | } |
| 44 | void tst_geometryloaders::testOBJLoader() |
| 45 | { |
| 46 | QScopedPointer<QGeometryLoaderInterface> loader; |
| 47 | loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), QStringLiteral("obj"))); |
| 48 | QVERIFY(loader); |
| 49 | if (!loader) |
| 50 | return; |
| 51 | |
| 52 | QFETCH(QString, fileName); |
| 53 | QFile file(fileName); |
| 54 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 55 | qDebug("Could not open test file for reading"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | bool loaded = loader->load(&file, QStringLiteral("Cube")); |
| 60 | QVERIFY(loaded); |
| 61 | if (!loaded) |
| 62 | return; |
| 63 | |
| 64 | QGeometry *geometry = loader->geometry(); |
| 65 | QVERIFY(geometry); |
| 66 | if (!geometry) |
| 67 | return; |
| 68 | |
| 69 | QCOMPARE(geometry->attributes().size(), 3); |
| 70 | for (QAttribute *attr : geometry->attributes()) { |
| 71 | switch (attr->attributeType()) { |
| 72 | case QAttribute::IndexAttribute: |
| 73 | QCOMPARE(attr->count(), 36u); |
| 74 | break; |
| 75 | case QAttribute::VertexAttribute: |
| 76 | QCOMPARE(attr->count(), 24u); |
| 77 | break; |
| 78 | default: |
| 79 | Q_UNREACHABLE(); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | file.close(); |
| 85 | } |
| 86 | |
| 87 | void tst_geometryloaders::testPLYLoader() |
| 88 | { |
| 89 | QScopedPointer<QGeometryLoaderInterface> loader; |
| 90 | loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), QStringLiteral("ply"))); |
| 91 | QVERIFY(loader); |
| 92 | if (!loader) |
| 93 | return; |
| 94 | |
| 95 | QFile file(QStringLiteral(":/cube.ply")); |
| 96 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 97 | qDebug("Could not open test file for reading"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | bool loaded = loader->load(&file, QStringLiteral("Cube")); |
| 102 | QVERIFY(loaded); |
| 103 | if (!loaded) |
| 104 | return; |
| 105 | |
| 106 | QGeometry *geometry = loader->geometry(); |
| 107 | QVERIFY(geometry); |
| 108 | if (!geometry) |
| 109 | return; |
| 110 | |
| 111 | QCOMPARE(geometry->attributes().size(), 3); |
| 112 | for (QAttribute *attr : geometry->attributes()) { |
| 113 | switch (attr->attributeType()) { |
| 114 | case QAttribute::IndexAttribute: |
| 115 | QCOMPARE(attr->count(), 36u); |
| 116 | break; |
| 117 | case QAttribute::VertexAttribute: |
| 118 | QCOMPARE(attr->count(), 24u); |
| 119 | break; |
| 120 | default: |
| 121 | Q_UNREACHABLE(); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | file.close(); |
| 127 | } |
| 128 | |
| 129 | void tst_geometryloaders::testSTLLoader() |
| 130 | { |
| 131 | QScopedPointer<QGeometryLoaderInterface> loader; |
| 132 | loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), QStringLiteral("stl"))); |
| 133 | QVERIFY(loader); |
| 134 | if (!loader) |
| 135 | return; |
| 136 | |
| 137 | QFile file(QStringLiteral(":/cube.stl")); |
| 138 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 139 | qDebug("Could not open test file for reading"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | bool loaded = loader->load(&file, QStringLiteral("Cube")); |
| 144 | QVERIFY(loaded); |
| 145 | if (!loaded) |
| 146 | return; |
| 147 | |
| 148 | QGeometry *geometry = loader->geometry(); |
| 149 | QVERIFY(geometry); |
| 150 | if (!geometry) |
| 151 | return; |
| 152 | |
| 153 | QCOMPARE(geometry->attributes().size(), 3); |
| 154 | for (QAttribute *attr : geometry->attributes()) { |
| 155 | QCOMPARE(attr->count(), 36u); |
| 156 | } |
| 157 | |
| 158 | file.close(); |
| 159 | } |
| 160 | |
| 161 | void tst_geometryloaders::testGLTFLoader() |
| 162 | { |
| 163 | QScopedPointer<QGeometryLoaderInterface> loader; |
| 164 | loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), QStringLiteral("gltf"))); |
| 165 | QVERIFY(loader); |
| 166 | if (!loader) |
| 167 | return; |
| 168 | |
| 169 | QFile file(QStringLiteral(":/cube.gltf")); |
| 170 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 171 | qDebug("Could not open test file for reading"); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | bool loaded = loader->load(&file, QStringLiteral("Cube")); |
| 176 | QVERIFY(loaded); |
| 177 | if (!loaded) |
| 178 | return; |
| 179 | |
| 180 | QGeometry *geometry = loader->geometry(); |
| 181 | QVERIFY(geometry); |
| 182 | if (!geometry) |
| 183 | return; |
| 184 | |
| 185 | QCOMPARE(geometry->attributes().size(), 3); |
| 186 | for (QAttribute *attr : geometry->attributes()) { |
| 187 | switch (attr->attributeType()) { |
| 188 | case QAttribute::IndexAttribute: |
| 189 | QCOMPARE(attr->count(), 36u); |
| 190 | break; |
| 191 | case QAttribute::VertexAttribute: |
| 192 | QCOMPARE(attr->count(), 24u); |
| 193 | break; |
| 194 | default: |
| 195 | Q_UNREACHABLE(); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | file.close(); |
| 201 | } |
| 202 | |
| 203 | #ifdef QT_3DGEOMETRYLOADERS_FBX |
| 204 | void tst_geometryloaders::testFBXLoader() |
| 205 | { |
| 206 | QScopedPointer<QGeometryLoaderInterface> loader; |
| 207 | loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), QStringLiteral("fbx"))); |
| 208 | QVERIFY(loader); |
| 209 | if (!loader) |
| 210 | return; |
| 211 | |
| 212 | QFile file(QStringLiteral(":/cube.fbx")); |
| 213 | if (!file.open(QIODevice::ReadOnly)) { |
| 214 | qDebug("Could not open test file for reading"); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | bool loaded = loader->load(&file, QStringLiteral("Cube")); |
| 219 | QVERIFY(loaded); |
| 220 | if (!loaded) |
| 221 | return; |
| 222 | |
| 223 | QGeometry *geometry = loader->geometry(); |
| 224 | QVERIFY(geometry); |
| 225 | if (!geometry) |
| 226 | return; |
| 227 | |
| 228 | QCOMPARE(geometry->attributes().count(), 3); |
| 229 | for (QAttribute *attr : geometry->attributes()) { |
| 230 | switch (attr->attributeType()) { |
| 231 | case QAttribute::IndexAttribute: |
| 232 | QCOMPARE(attr->count(), 36u); |
| 233 | break; |
| 234 | case QAttribute::VertexAttribute: |
| 235 | QCOMPARE(attr->count(), 24u); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | file.close(); |
| 241 | } |
| 242 | #endif |
| 243 | |
| 244 | QTEST_MAIN(tst_geometryloaders) |
| 245 | |
| 246 | #include "tst_geometryloaders.moc" |