1/****************************************************************************
2**
3** Copyright (C) 2016 Paul Lemire <paul.lemire350@gmail.com>
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 <Qt3DRender/private/scene_p.h>
31#include <Qt3DRender/private/loadscenejob_p.h>
32#include <Qt3DRender/private/qsceneimporter_p.h>
33#include <Qt3DRender/private/nodemanagers_p.h>
34#include <Qt3DRender/private/scenemanager_p.h>
35#include <Qt3DCore/qpropertyupdatedchange.h>
36#include <Qt3DCore/QEntity>
37#include <Qt3DCore/private/qbackendnode_p.h>
38#include "testpostmanarbiter.h"
39
40class TestSceneImporter : public Qt3DRender::QSceneImporter
41{
42public:
43 explicit TestSceneImporter(bool supportsFormat, bool shouldFail)
44 : m_supportsFormat(supportsFormat)
45 , m_shouldFail(shouldFail)
46 {}
47
48 void setSource(const QUrl &source) override
49 {
50 m_source = source;
51 }
52
53 void setData(const QByteArray& data, const QString &basePath) override
54 {
55 Q_UNUSED(data)
56 Q_UNUSED(basePath)
57 }
58
59 bool areFileTypesSupported(const QStringList &extensions) const override
60 {
61 Q_UNUSED(extensions);
62 return m_supportsFormat;
63 }
64
65 Qt3DCore::QEntity *scene(const QString &) override
66 {
67 return m_shouldFail ? nullptr : new Qt3DCore::QEntity();
68 }
69
70 Qt3DCore::QEntity *node(const QString &) override
71 {
72 return m_shouldFail ? nullptr : new Qt3DCore::QEntity();
73 }
74
75 QUrl source() const
76 {
77 return m_source;
78 }
79
80private:
81 QUrl m_source;
82 bool m_supportsFormat;
83 bool m_shouldFail;
84};
85
86class tst_LoadSceneJob : public QObject
87{
88 Q_OBJECT
89
90private Q_SLOTS:
91
92 void checkInitialState()
93 {
94 // GIVEN
95 QUrl url;
96 Qt3DCore::QNodeId nodeId;
97 Qt3DRender::Render::LoadSceneJob backendLoadSceneJob(url, nodeId);
98
99 // THEN
100 QCOMPARE(backendLoadSceneJob.source(), url);
101 QCOMPARE(backendLoadSceneJob.sceneComponentId(), nodeId);
102 QVERIFY(backendLoadSceneJob.nodeManagers() == nullptr);
103 QCOMPARE(backendLoadSceneJob.sceneImporters().size(), 0);
104 }
105
106 void checkInitialize()
107 {
108 // GIVEN
109 const QUrl url(QStringLiteral("file:///URL"));
110 const Qt3DCore::QNodeId sceneId = Qt3DCore::QNodeId::createId();
111 Qt3DRender::Render::NodeManagers nodeManagers;
112 TestSceneImporter fakeImporter(true, true);
113
114 // WHEN
115 Qt3DRender::Render::LoadSceneJob backendLoadSceneJob(url, sceneId);
116 backendLoadSceneJob.setNodeManagers(&nodeManagers);
117 backendLoadSceneJob.setSceneImporters(
118 QList<Qt3DRender::QSceneImporter *>() << &fakeImporter);
119
120 // THEN
121 QCOMPARE(backendLoadSceneJob.source(), url);
122 QCOMPARE(backendLoadSceneJob.sceneComponentId(), sceneId);
123 QVERIFY(backendLoadSceneJob.nodeManagers() == &nodeManagers);
124 QCOMPARE(backendLoadSceneJob.sceneImporters().size(), 1);
125 QCOMPARE(backendLoadSceneJob.sceneImporters().first(), &fakeImporter);
126 }
127
128 void checkRunValidSourceSupportedFormat()
129 {
130 QSKIP("Can't test successful loading");
131
132 // GIVEN
133 const QUrl url(QStringLiteral("file:///URL"));
134 TestArbiter arbiter;
135 Qt3DRender::Render::NodeManagers nodeManagers;
136 TestSceneImporter fakeImporter(true, false);
137 Qt3DCore::QNodeId sceneId = Qt3DCore::QNodeId::createId();
138 Qt3DRender::Render::Scene *scene = nodeManagers.sceneManager()->getOrCreateResource(id: sceneId);
139
140 // THEN
141 QVERIFY(scene != nullptr);
142 Qt3DCore::QBackendNodePrivate::get(n: scene)->setArbiter(&arbiter);
143
144 // WHEN
145 Qt3DRender::Render::LoadSceneJob loadSceneJob(url, sceneId);
146 loadSceneJob.setNodeManagers(&nodeManagers);
147 loadSceneJob.setSceneImporters(QList<Qt3DRender::QSceneImporter *>() << &fakeImporter);
148 loadSceneJob.run();
149
150 // THEN
151 Qt3DRender::Render::LoadSceneJobPrivate *dJob = static_cast<decltype(dJob)>(Qt3DCore::QAspectJobPrivate::get(job: &loadSceneJob));
152 QCOMPARE(dJob->m_status, Qt3DRender::QSceneLoader::Ready);
153 QVERIFY(dJob->m_sceneSubtree != nullptr);
154 }
155
156 void checkEmptySource()
157 {
158 // GIVEN
159 QUrl url;
160 TestArbiter arbiter;
161 Qt3DRender::Render::NodeManagers nodeManagers;
162 TestSceneImporter fakeImporter(true, false);
163 Qt3DCore::QNodeId sceneId = Qt3DCore::QNodeId::createId();
164 Qt3DRender::Render::Scene *scene = nodeManagers.sceneManager()->getOrCreateResource(id: sceneId);
165
166 // THEN
167 QVERIFY(scene != nullptr);
168 Qt3DCore::QBackendNodePrivate::get(n: scene)->setArbiter(&arbiter);
169
170 // WHEN
171 Qt3DRender::Render::LoadSceneJob loadSceneJob(url, sceneId);
172 loadSceneJob.setNodeManagers(&nodeManagers);
173 loadSceneJob.setSceneImporters(QList<Qt3DRender::QSceneImporter *>() << &fakeImporter);
174 loadSceneJob.run();
175
176 // THEN
177 Qt3DRender::Render::LoadSceneJobPrivate *dJob = static_cast<decltype(dJob)>(Qt3DCore::QAspectJobPrivate::get(job: &loadSceneJob));
178 QCOMPARE(dJob->m_status, Qt3DRender::QSceneLoader::None);
179 QVERIFY(dJob->m_sceneSubtree == nullptr);
180 }
181
182 void checkRunValidSourceUnsupportedFormat()
183 {
184 // no data is loaded so...
185 QSKIP("Can't differentiate between no data and unsupported data");
186
187 // GIVEN
188 const QUrl url(QStringLiteral("file:///URL"));
189 TestArbiter arbiter;
190 Qt3DRender::Render::NodeManagers nodeManagers;
191 TestSceneImporter fakeImporter(false, false);
192 Qt3DCore::QNodeId sceneId = Qt3DCore::QNodeId::createId();
193 Qt3DRender::Render::Scene *scene = nodeManagers.sceneManager()->getOrCreateResource(id: sceneId);
194
195 // THEN
196 QVERIFY(scene != nullptr);
197 Qt3DCore::QBackendNodePrivate::get(n: scene)->setArbiter(&arbiter);
198
199 // WHEN
200 Qt3DRender::Render::LoadSceneJob loadSceneJob(url, sceneId);
201 loadSceneJob.setNodeManagers(&nodeManagers);
202 loadSceneJob.setSceneImporters(QList<Qt3DRender::QSceneImporter *>() << &fakeImporter);
203 loadSceneJob.run();
204
205 // THEN
206 Qt3DRender::Render::LoadSceneJobPrivate *dJob = static_cast<decltype(dJob)>(Qt3DCore::QAspectJobPrivate::get(job: &loadSceneJob));
207 QCOMPARE(dJob->m_status, Qt3DRender::QSceneLoader::Error);
208 QVERIFY(dJob->m_sceneSubtree == nullptr);
209 }
210
211 void checkRunErrorAtLoading()
212 {
213 // GIVEN
214 const QUrl url(QStringLiteral("file:///URL"));
215 TestArbiter arbiter;
216 Qt3DRender::Render::NodeManagers nodeManagers;
217 TestSceneImporter fakeImporter(true, true);
218 Qt3DCore::QNodeId sceneId = Qt3DCore::QNodeId::createId();
219 Qt3DRender::Render::Scene *scene = nodeManagers.sceneManager()->getOrCreateResource(id: sceneId);
220
221 // THEN
222 QVERIFY(scene != nullptr);
223 Qt3DCore::QBackendNodePrivate::get(n: scene)->setArbiter(&arbiter);
224
225 // WHEN
226 Qt3DRender::Render::LoadSceneJob loadSceneJob(url, sceneId);
227 loadSceneJob.setNodeManagers(&nodeManagers);
228 loadSceneJob.setSceneImporters(QList<Qt3DRender::QSceneImporter *>() << &fakeImporter);
229 loadSceneJob.run();
230
231 // THEN
232 // THEN
233 Qt3DRender::Render::LoadSceneJobPrivate *dJob = static_cast<decltype(dJob)>(Qt3DCore::QAspectJobPrivate::get(job: &loadSceneJob));
234 QCOMPARE(dJob->m_status, Qt3DRender::QSceneLoader::Error);
235 QVERIFY(dJob->m_sceneSubtree == nullptr);
236 }
237};
238
239QTEST_MAIN(tst_LoadSceneJob)
240
241#include "tst_loadscenejob.moc"
242

source code of qt3d/tests/auto/render/loadscenejob/tst_loadscenejob.cpp