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#include "../../shared/util.h"
30
31#include <qtest.h>
32#include <QSignalSpy>
33#include <QDebug>
34
35#include <Qt3DQuick/qqmlaspectengine.h>
36
37#include <Qt3DCore/qentity.h>
38#include <Qt3DCore/qnode.h>
39#include <Qt3DCore/private/qnode_p.h>
40
41#include <QtQml/qqmlengine.h>
42#include <QtQml/qqmlcomponent.h>
43#include <QtQml/qqmlcontext.h>
44
45using namespace Qt3DCore::Quick;
46using namespace Qt3DCore;
47
48class tst_dynamicnodecreation : public QQmlDataTest
49{
50 Q_OBJECT
51
52private slots:
53 void createSingleEntityViaQmlEngine();
54 void createSingleEntityViaAspectEngine();
55 void createMultipleEntitiesViaAspectEngine();
56 void createEntityAndDynamicChild();
57};
58
59void tst_dynamicnodecreation::createSingleEntityViaQmlEngine()
60{
61 // GIVEN
62 QQmlAspectEngine engine;
63
64 // WHEN
65 QQmlEngine *qmlEngine = engine.qmlEngine();
66
67 // THEN
68 QVERIFY(qmlEngine != nullptr);
69
70 // WHEN
71 QQmlComponent component(qmlEngine, testFileUrl(fileName: "createSingle.qml"));
72 auto entity = qobject_cast<QEntity *>(object: component.create());
73
74 // THEN
75 QVERIFY(entity != nullptr);
76 QVERIFY(entity->parent() == nullptr);
77 QCOMPARE(entity->property("success").toBool(), true);
78 QCOMPARE(entity->property("value").toInt(), 12345);
79
80 // WHEN
81 auto nodeD = Qt3DCore::QNodePrivate::get(q: entity);
82
83 // THEN
84 QVERIFY(nodeD->m_scene == nullptr);
85 QVERIFY(nodeD->m_changeArbiter == nullptr);
86 QCOMPARE(nodeD->m_id.isNull(), false);
87}
88
89void tst_dynamicnodecreation::createSingleEntityViaAspectEngine()
90{
91 // GIVEN
92 QQmlAspectEngine engine;
93
94 // WHEN
95 engine.setSource(testFileUrl(fileName: "createSingle.qml"));
96 auto entity = engine.aspectEngine()->rootEntity().data();
97
98 // THEN
99 QVERIFY(entity != nullptr);
100 QVERIFY(entity->parent() == engine.aspectEngine());
101 QCOMPARE(entity->property("success").toBool(), true);
102 QCOMPARE(entity->property("value").toInt(), 12345);
103
104 // WHEN
105 auto nodeD = QNodePrivate::get(q: entity);
106
107 // THEN
108 QVERIFY(nodeD->m_scene != nullptr);
109 QVERIFY(nodeD->m_changeArbiter != nullptr);
110 QCOMPARE(nodeD->m_id.isNull(), false);
111}
112
113void tst_dynamicnodecreation::createMultipleEntitiesViaAspectEngine()
114{
115 // GIVEN
116 QQmlAspectEngine engine;
117
118 // WHEN
119 engine.setSource(testFileUrl(fileName: "createMultiple.qml"));
120 auto entity = engine.aspectEngine()->rootEntity().data();
121
122 // THEN
123 QVERIFY(entity != nullptr);
124 QVERIFY(entity->parent() == engine.aspectEngine());
125 QCOMPARE(entity->property("success").toBool(), true);
126 QCOMPARE(entity->property("value").toInt(), 12345);
127 QCOMPARE(entity->childNodes().size(), 1);
128
129 // WHEN
130 auto nodeD = Qt3DCore::QNodePrivate::get(q: entity);
131
132 // THEN
133 QVERIFY(nodeD->m_scene != nullptr);
134 QVERIFY(nodeD->m_changeArbiter != nullptr);
135 QCOMPARE(nodeD->m_id.isNull(), false);
136
137 // WHEN
138 auto child = qobject_cast<QEntity *>(object: entity->childNodes().first());
139
140 // THEN
141 QCOMPARE(child->objectName(), QLatin1String("childEntity"));
142 QCOMPARE(child->property("success").toBool(), false);
143 QCOMPARE(child->property("value").toInt(), 54321);
144 QCOMPARE(child->childNodes().size(), 0);
145 QCOMPARE(child->parentEntity(), entity);
146}
147
148void tst_dynamicnodecreation::createEntityAndDynamicChild()
149{
150 // GIVEN
151 QQmlAspectEngine engine;
152
153 // WHEN
154 engine.setSource(testFileUrl(fileName: "createDynamicChild.qml"));
155 auto entity = engine.aspectEngine()->rootEntity().data();
156
157 // THEN
158 QVERIFY(entity != nullptr);
159 QVERIFY(entity->parent() == engine.aspectEngine());
160 QCOMPARE(entity->property("success").toBool(), true);
161 QCOMPARE(entity->property("value").toInt(), 12345);
162 QCOMPARE(entity->childNodes().size(), 0);
163
164 // WHEN
165 auto *nodeD = Qt3DCore::QNodePrivate::get(q: entity);
166
167 // THEN
168 QVERIFY(nodeD->m_scene != nullptr);
169 QVERIFY(nodeD->m_changeArbiter != nullptr);
170 QCOMPARE(nodeD->m_id.isNull(), false);
171
172 // WHEN
173 //QGuiApplication::processEvents(QEventLoop::AllEvents, 10);
174 QGuiApplication::processEvents();
175 auto child = qobject_cast<QEntity *>(object: entity->childNodes().first());
176
177 // THEN
178 QCOMPARE(child->objectName(), QLatin1String("dynamicChildEntity"));
179 QCOMPARE(child->property("success").toBool(), false);
180 QCOMPARE(child->property("value").toInt(), 27182818);
181 QCOMPARE(child->childNodes().size(), 0);
182 QCOMPARE(child->parentEntity(), entity);
183
184 // WHEN
185 auto *childD = Qt3DCore::QNodePrivate::get(q: child);
186
187 // THEN
188 QCOMPARE(childD->m_scene, nodeD->m_scene);
189 QCOMPARE(childD->m_changeArbiter, nodeD->m_changeArbiter);
190 QCOMPARE(childD->m_id.isNull(), false);
191}
192
193QTEST_MAIN(tst_dynamicnodecreation)
194
195#include "tst_dynamicnodecreation.moc"
196

source code of qt3d/tests/auto/quick3d/dynamicnodecreation/tst_dynamicnodecreation.cpp