| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "../../shared/util.h" |
| 5 | |
| 6 | #include <qtest.h> |
| 7 | #include <QSignalSpy> |
| 8 | #include <QDebug> |
| 9 | |
| 10 | #include <Qt3DQuick/qqmlaspectengine.h> |
| 11 | |
| 12 | #include <Qt3DCore/qentity.h> |
| 13 | #include <Qt3DCore/qnode.h> |
| 14 | #include <Qt3DCore/private/qnode_p.h> |
| 15 | |
| 16 | #include <QtQml/qqmlengine.h> |
| 17 | #include <QtQml/qqmlcomponent.h> |
| 18 | #include <QtQml/qqmlcontext.h> |
| 19 | |
| 20 | using namespace Qt3DCore::Quick; |
| 21 | using namespace Qt3DCore; |
| 22 | |
| 23 | class tst_dynamicnodecreation : public QQmlDataTest |
| 24 | { |
| 25 | Q_OBJECT |
| 26 | |
| 27 | private slots: |
| 28 | void createSingleEntityViaQmlEngine(); |
| 29 | void createSingleEntityViaAspectEngine(); |
| 30 | void createMultipleEntitiesViaAspectEngine(); |
| 31 | void createEntityAndDynamicChild(); |
| 32 | }; |
| 33 | |
| 34 | void tst_dynamicnodecreation::createSingleEntityViaQmlEngine() |
| 35 | { |
| 36 | // GIVEN |
| 37 | QQmlAspectEngine engine; |
| 38 | |
| 39 | // WHEN |
| 40 | QQmlEngine *qmlEngine = engine.qmlEngine(); |
| 41 | |
| 42 | // THEN |
| 43 | QVERIFY(qmlEngine != nullptr); |
| 44 | |
| 45 | // WHEN |
| 46 | QQmlComponent component(qmlEngine, testFileUrl("createSingle.qml")); |
| 47 | auto entity = qobject_cast<QEntity *>(component.create()); |
| 48 | |
| 49 | // THEN |
| 50 | QVERIFY(entity != nullptr); |
| 51 | QVERIFY(entity->parent() == nullptr); |
| 52 | QCOMPARE(entity->property("success").toBool(), true); |
| 53 | QCOMPARE(entity->property("value").toInt(), 12345); |
| 54 | |
| 55 | // WHEN |
| 56 | auto nodeD = Qt3DCore::QNodePrivate::get(entity); |
| 57 | |
| 58 | // THEN |
| 59 | QVERIFY(nodeD->m_scene == nullptr); |
| 60 | QVERIFY(nodeD->m_changeArbiter == nullptr); |
| 61 | QCOMPARE(nodeD->m_id.isNull(), false); |
| 62 | } |
| 63 | |
| 64 | void tst_dynamicnodecreation::createSingleEntityViaAspectEngine() |
| 65 | { |
| 66 | // GIVEN |
| 67 | QQmlAspectEngine engine; |
| 68 | |
| 69 | // WHEN |
| 70 | engine.setSource(testFileUrl("createSingle.qml")); |
| 71 | auto entity = engine.aspectEngine()->rootEntity().data(); |
| 72 | |
| 73 | // THEN |
| 74 | QVERIFY(entity != nullptr); |
| 75 | QVERIFY(entity->parent() == engine.aspectEngine()); |
| 76 | QCOMPARE(entity->property("success").toBool(), true); |
| 77 | QCOMPARE(entity->property("value").toInt(), 12345); |
| 78 | |
| 79 | // WHEN |
| 80 | auto nodeD = QNodePrivate::get(entity); |
| 81 | |
| 82 | // THEN |
| 83 | QVERIFY(nodeD->m_scene != nullptr); |
| 84 | QVERIFY(nodeD->m_changeArbiter != nullptr); |
| 85 | QCOMPARE(nodeD->m_id.isNull(), false); |
| 86 | } |
| 87 | |
| 88 | void tst_dynamicnodecreation::createMultipleEntitiesViaAspectEngine() |
| 89 | { |
| 90 | // GIVEN |
| 91 | QQmlAspectEngine engine; |
| 92 | |
| 93 | // WHEN |
| 94 | engine.setSource(testFileUrl("createMultiple.qml")); |
| 95 | auto entity = engine.aspectEngine()->rootEntity().data(); |
| 96 | |
| 97 | // THEN |
| 98 | QVERIFY(entity != nullptr); |
| 99 | QVERIFY(entity->parent() == engine.aspectEngine()); |
| 100 | QCOMPARE(entity->property("success").toBool(), true); |
| 101 | QCOMPARE(entity->property("value").toInt(), 12345); |
| 102 | QCOMPARE(entity->childNodes().size(), 1); |
| 103 | |
| 104 | // WHEN |
| 105 | auto nodeD = Qt3DCore::QNodePrivate::get(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 | // WHEN |
| 113 | auto child = qobject_cast<QEntity *>(entity->childNodes().first()); |
| 114 | |
| 115 | // THEN |
| 116 | QCOMPARE(child->objectName(), QLatin1String("childEntity")); |
| 117 | QCOMPARE(child->property("success").toBool(), false); |
| 118 | QCOMPARE(child->property("value").toInt(), 54321); |
| 119 | QCOMPARE(child->childNodes().size(), 0); |
| 120 | QCOMPARE(child->parentEntity(), entity); |
| 121 | } |
| 122 | |
| 123 | void tst_dynamicnodecreation::createEntityAndDynamicChild() |
| 124 | { |
| 125 | QSKIP("Fail on CI for unknown reason"); |
| 126 | |
| 127 | // GIVEN |
| 128 | QQmlAspectEngine engine; |
| 129 | |
| 130 | // WHEN |
| 131 | engine.setSource(testFileUrl("createDynamicChild.qml")); |
| 132 | auto entity = engine.aspectEngine()->rootEntity().data(); |
| 133 | |
| 134 | // THEN |
| 135 | QVERIFY(entity != nullptr); |
| 136 | QVERIFY(entity->parent() == engine.aspectEngine()); |
| 137 | QCOMPARE(entity->property("success").toBool(), true); |
| 138 | QCOMPARE(entity->property("value").toInt(), 12345); |
| 139 | QCOMPARE(entity->childNodes().size(), 0); |
| 140 | |
| 141 | // WHEN |
| 142 | auto *nodeD = Qt3DCore::QNodePrivate::get(entity); |
| 143 | |
| 144 | // THEN |
| 145 | QVERIFY(nodeD->m_scene != nullptr); |
| 146 | QVERIFY(nodeD->m_changeArbiter != nullptr); |
| 147 | QCOMPARE(nodeD->m_id.isNull(), false); |
| 148 | |
| 149 | // WHEN |
| 150 | //QGuiApplication::processEvents(QEventLoop::AllEvents, 10); |
| 151 | QGuiApplication::processEvents(); |
| 152 | auto child = qobject_cast<QEntity *>(entity->childNodes().first()); |
| 153 | |
| 154 | // THEN |
| 155 | QCOMPARE(child->objectName(), QLatin1String("dynamicChildEntity")); |
| 156 | QCOMPARE(child->property("success").toBool(), false); |
| 157 | QCOMPARE(child->property("value").toInt(), 27182818); |
| 158 | QCOMPARE(child->childNodes().size(), 0); |
| 159 | QCOMPARE(child->parentEntity(), entity); |
| 160 | |
| 161 | // WHEN |
| 162 | auto *childD = Qt3DCore::QNodePrivate::get(child); |
| 163 | |
| 164 | // THEN |
| 165 | QCOMPARE(childD->m_scene, nodeD->m_scene); |
| 166 | QCOMPARE(childD->m_changeArbiter, nodeD->m_changeArbiter); |
| 167 | QCOMPARE(childD->m_id.isNull(), false); |
| 168 | } |
| 169 | |
| 170 | QTEST_MAIN(tst_dynamicnodecreation) |
| 171 | |
| 172 | #include "tst_dynamicnodecreation.moc" |