summaryrefslogtreecommitdiffstats
path: root/tests/auto/quick3d/dynamicnodecreation/tst_dynamicnodecreation.cpp
blob: 2d5c9107add3b416b77cc054d067f565736712e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "../../shared/util.h"

#include <qtest.h>
#include <QSignalSpy>
#include <QDebug>

#include <Qt3DQuick/qqmlaspectengine.h>

#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qnode.h>
#include <Qt3DCore/private/qnode_p.h>

#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlcomponent.h>
#include <QtQml/qqmlcontext.h>

using namespace Qt3DCore::Quick;
using namespace Qt3DCore;

class tst_dynamicnodecreation : public QQmlDataTest
{
    Q_OBJECT

private slots:
    void createSingleEntityViaQmlEngine();
    void createSingleEntityViaAspectEngine();
    void createMultipleEntitiesViaAspectEngine();
    void createEntityAndDynamicChild();
};

void tst_dynamicnodecreation::createSingleEntityViaQmlEngine()
{
    // GIVEN
    QQmlAspectEngine engine;

    // WHEN
    QQmlEngine *qmlEngine = engine.qmlEngine();

    // THEN
    QVERIFY(qmlEngine != nullptr);

    // WHEN
    QQmlComponent component(qmlEngine, testFileUrl("createSingle.qml"));
    auto entity = qobject_cast<QEntity *>(component.create());

    // THEN
    QVERIFY(entity != nullptr);
    QVERIFY(entity->parent() == nullptr);
    QCOMPARE(entity->property("success").toBool(), true);
    QCOMPARE(entity->property("value").toInt(), 12345);

    // WHEN
    auto nodeD = Qt3DCore::QNodePrivate::get(entity);

    // THEN
    QVERIFY(nodeD->m_scene == nullptr);
    QVERIFY(nodeD->m_changeArbiter == nullptr);
    QCOMPARE(nodeD->m_id.isNull(), false);
}

void tst_dynamicnodecreation::createSingleEntityViaAspectEngine()
{
    // GIVEN
    QQmlAspectEngine engine;

    // WHEN
    engine.setSource(testFileUrl("createSingle.qml"));
    auto entity = engine.aspectEngine()->rootEntity().data();

    // THEN
    QVERIFY(entity != nullptr);
    QVERIFY(entity->parent() == engine.aspectEngine());
    QCOMPARE(entity->property("success").toBool(), true);
    QCOMPARE(entity->property("value").toInt(), 12345);

    // WHEN
    auto nodeD = QNodePrivate::get(entity);

    // THEN
    QVERIFY(nodeD->m_scene != nullptr);
    QVERIFY(nodeD->m_changeArbiter != nullptr);
    QCOMPARE(nodeD->m_id.isNull(), false);
}

void tst_dynamicnodecreation::createMultipleEntitiesViaAspectEngine()
{
    // GIVEN
    QQmlAspectEngine engine;

    // WHEN
    engine.setSource(testFileUrl("createMultiple.qml"));
    auto entity = engine.aspectEngine()->rootEntity().data();

    // THEN
    QVERIFY(entity != nullptr);
    QVERIFY(entity->parent() == engine.aspectEngine());
    QCOMPARE(entity->property("success").toBool(), true);
    QCOMPARE(entity->property("value").toInt(), 12345);
    QCOMPARE(entity->childNodes().size(), 1);

    // WHEN
    auto nodeD = Qt3DCore::QNodePrivate::get(entity);

    // THEN
    QVERIFY(nodeD->m_scene != nullptr);
    QVERIFY(nodeD->m_changeArbiter != nullptr);
    QCOMPARE(nodeD->m_id.isNull(), false);

    // WHEN
    auto child = qobject_cast<QEntity *>(entity->childNodes().first());

    // THEN
    QCOMPARE(child->objectName(), QLatin1String("childEntity"));
    QCOMPARE(child->property("success").toBool(), false);
    QCOMPARE(child->property("value").toInt(), 54321);
    QCOMPARE(child->childNodes().size(), 0);
    QCOMPARE(child->parentEntity(), entity);
}

void tst_dynamicnodecreation::createEntityAndDynamicChild()
{
    QSKIP("Fail on CI for unknown reason");

    // GIVEN
    QQmlAspectEngine engine;

    // WHEN
    engine.setSource(testFileUrl("createDynamicChild.qml"));
    auto entity = engine.aspectEngine()->rootEntity().data();

    // THEN
    QVERIFY(entity != nullptr);
    QVERIFY(entity->parent() == engine.aspectEngine());
    QCOMPARE(entity->property("success").toBool(), true);
    QCOMPARE(entity->property("value").toInt(), 12345);
    QCOMPARE(entity->childNodes().size(), 0);

    // WHEN
    auto *nodeD = Qt3DCore::QNodePrivate::get(entity);

    // THEN
    QVERIFY(nodeD->m_scene != nullptr);
    QVERIFY(nodeD->m_changeArbiter != nullptr);
    QCOMPARE(nodeD->m_id.isNull(), false);

    // WHEN
    //QGuiApplication::processEvents(QEventLoop::AllEvents, 10);
    QGuiApplication::processEvents();
    auto child = qobject_cast<QEntity *>(entity->childNodes().first());

    // THEN
    QCOMPARE(child->objectName(), QLatin1String("dynamicChildEntity"));
    QCOMPARE(child->property("success").toBool(), false);
    QCOMPARE(child->property("value").toInt(), 27182818);
    QCOMPARE(child->childNodes().size(), 0);
    QCOMPARE(child->parentEntity(), entity);

    // WHEN
    auto *childD = Qt3DCore::QNodePrivate::get(child);

    // THEN
    QCOMPARE(childD->m_scene, nodeD->m_scene);
    QCOMPARE(childD->m_changeArbiter, nodeD->m_changeArbiter);
    QCOMPARE(childD->m_id.isNull(), false);
}

QTEST_MAIN(tst_dynamicnodecreation)

#include "tst_dynamicnodecreation.moc"