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
30#include <QtTest/QTest>
31#include <Qt3DQuickRender/private/quick3dbuffer_p.h>
32#include <QObject>
33#include <QVector3D>
34
35namespace {
36
37bool writeBinaryFile(const QString filePath, const void *data, int byteSize)
38{
39 QFile f(filePath);
40 if (f.open(flags: QIODevice::WriteOnly))
41 return f.write(data: reinterpret_cast<const char *>(data), len: byteSize) == byteSize;
42 return false;
43}
44
45} // anonymous
46
47class tst_Quick3DBuffer : public QObject
48{
49 Q_OBJECT
50
51private Q_SLOTS:
52
53 void checkInvalidBinaryFile()
54 {
55 // GIVEN
56 Qt3DRender::Render::Quick::Quick3DBuffer buf;
57
58 // WHEN
59 QVariant data = buf.readBinaryFile(fileUrl: QUrl::fromLocalFile(localfile: QLatin1String("this_should_not_exist.bin")));
60
61 // THEN
62 QCOMPARE(data.userType(), static_cast<int>(QVariant::ByteArray));
63 QVERIFY(data.value<QByteArray>().isEmpty());
64 }
65
66 void checkValidBinaryFile()
67 {
68 // GIVEN
69 Qt3DRender::Render::Quick::Quick3DBuffer buf;
70 QVector<QVector3D> dataArray = QVector<QVector3D>()
71 << QVector3D(327.0f, 350.0f, 355.0f)
72 << QVector3D(383.0f, 427.0f, 454.0f);
73
74 const int bufferByteSize = dataArray.size() * sizeof(QVector3D);
75 const QLatin1String filePath("binary_data.bin");
76 const bool writingSucceeded = writeBinaryFile(filePath, data: dataArray.constData(), byteSize: bufferByteSize);
77 Q_ASSERT(writingSucceeded);
78
79 // WHEN
80 const QUrl path = QUrl::fromLocalFile(localfile: filePath);
81 QVariant data = buf.readBinaryFile(fileUrl: path);
82
83 // THEN
84 QCOMPARE(data.userType(), static_cast<int>(QVariant::ByteArray));
85 const QByteArray byteArray = data.value<QByteArray>();
86 QCOMPARE(byteArray.size(), bufferByteSize);
87 QVERIFY(memcmp(byteArray, dataArray.constData(), bufferByteSize) == 0);
88 }
89
90};
91
92QTEST_MAIN(tst_Quick3DBuffer)
93
94#include "tst_quick3dbuffer.moc"
95

source code of qt3d/tests/auto/quick3d/quick3dbuffer/tst_quick3dbuffer.cpp