| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DQuick/private/quick3dbuffer_p.h> |
| 7 | #include <QObject> |
| 8 | #include <QVector3D> |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | bool writeBinaryFile(const QString filePath, const void *data, int byteSize) |
| 13 | { |
| 14 | QFile f(filePath); |
| 15 | if (f.open(QIODevice::WriteOnly)) |
| 16 | return f.write(reinterpret_cast<const char *>(data), byteSize) == byteSize; |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | } // anonymous |
| 21 | |
| 22 | class tst_Quick3DBuffer : public QObject |
| 23 | { |
| 24 | Q_OBJECT |
| 25 | |
| 26 | private Q_SLOTS: |
| 27 | |
| 28 | void checkInvalidBinaryFile() |
| 29 | { |
| 30 | // GIVEN |
| 31 | Qt3DCore::Quick::Quick3DBuffer buf; |
| 32 | |
| 33 | // WHEN |
| 34 | QVariant data = buf.readBinaryFile(QUrl::fromLocalFile(QLatin1String("this_should_not_exist.bin"))); |
| 35 | |
| 36 | // THEN |
| 37 | QCOMPARE(data.userType(), static_cast<int>(QMetaType::QByteArray)); |
| 38 | QVERIFY(data.value<QByteArray>().isEmpty()); |
| 39 | } |
| 40 | |
| 41 | void checkValidBinaryFile() |
| 42 | { |
| 43 | // GIVEN |
| 44 | Qt3DCore::Quick::Quick3DBuffer buf; |
| 45 | const QVector<QVector3D> dataArray = { QVector3D(327.0f, 350.0f, 355.0f), |
| 46 | QVector3D(383.0f, 427.0f, 454.0f) }; |
| 47 | |
| 48 | const int bufferByteSize = int(dataArray.size()) * sizeof(QVector3D); |
| 49 | const QLatin1String filePath("binary_data.bin"); |
| 50 | const bool writingSucceeded = writeBinaryFile(filePath, dataArray.constData(), bufferByteSize); |
| 51 | Q_ASSERT(writingSucceeded); |
| 52 | |
| 53 | // WHEN |
| 54 | const QUrl path = QUrl::fromLocalFile(filePath); |
| 55 | QVariant data = buf.readBinaryFile(path); |
| 56 | |
| 57 | // THEN |
| 58 | QCOMPARE(data.userType(), static_cast<int>(QMetaType::QByteArray)); |
| 59 | const QByteArray byteArray = data.value<QByteArray>(); |
| 60 | QCOMPARE(byteArray.size(), bufferByteSize); |
| 61 | QVERIFY(memcmp(byteArray, dataArray.constData(), bufferByteSize) == 0); |
| 62 | } |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | QTEST_MAIN(tst_Quick3DBuffer) |
| 67 | |
| 68 | #include "tst_quick3dbuffer.moc" |