Debian Codegraph @

tests/auto/core/qaspectengine/tst_qaspectengine.cpp

raw symbols SCIP
1 // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QTest>
5 #include <QTimer>
6
7 #include <Qt3DCore/QAbstractAspect>
8 #include <Qt3DCore/qaspectengine.h>
9 #include <Qt3DCore/qentity.h>
10 #include <Qt3DCore/qtransform.h>
11
12 using namespace Qt3DCore;
13
14 class PrintRootAspect : public QAbstractAspect
15 {
16 Q_OBJECT
17 public:
18 explicit PrintRootAspect(QObject *parent = 0)
19 : QAbstractAspect(parent)
20 , m_rootEntityId()
21 {
22 qDebug() << Q_FUNC_INFO;
23 }
24
25 private:
26 void onRegistered() override
27 {
28 qDebug() << Q_FUNC_INFO;
29 }
30
31 void onEngineStartup() override
32 {
33 qDebug() << Q_FUNC_INFO;
34 m_rootEntityId = rootEntityId();
35 }
36
37 void onEngineShutdown() override
38 {
39 qDebug() << Q_FUNC_INFO;
40 }
41
42 std::vector<QAspectJobPtr> jobsToExecute(qint64) override \
43 {
44 if (m_rootEntityId)
45 qDebug() << Q_FUNC_INFO << m_rootEntityId;
46 return {};
47 }
48
49 QNodeId m_rootEntityId;
50 };
51
52 #define FAKE_ASPECT(ClassName, dependAspects) \
53 class ClassName : public QAbstractAspect \
54 { \
55 Q_OBJECT \
56 public: \
57 explicit ClassName(QObject *parent = nullptr) \
58 : QAbstractAspect(parent) {} \
59 \
60 private: \
61 void onRegistered() override {} \
62 void onEngineStartup() override {} \
63 void onEngineShutdown() override {} \
64 \
65 std::vector<QAspectJobPtr> jobsToExecute(qint64) override \
66 { \
67 return std::vector<QAspectJobPtr>(); \
68 } \
69 \
70 QVariant executeCommand(const QStringList &args) override \
71 { \
72 if (args.size() >= 2 && args.first() == QLatin1String("echo")) { \
73 QStringList list = args; \
74 list.removeFirst(); \
75 return QString("%1 said '%2'").arg(metaObject()->className()).arg(list.join(QLatin1Char(' '))); \
76 } \
77 \
78 return QVariant(); \
79 } \
80 QStringList dependencies() const override { return dependAspects; } \
81 };
82
83 FAKE_ASPECT(FakeAspect, {})
84 FAKE_ASPECT(FakeAspect2, {})
85 FAKE_ASPECT(FakeAspect3, {})
86 FAKE_ASPECT(FakeAspectDependent, {QLatin1String("fake")})
87
88 QT3D_REGISTER_ASPECT("fake", FakeAspect)
89 QT3D_REGISTER_ASPECT("otherfake", FakeAspect2)
90 QT3D_REGISTER_ASPECT("dependfake", FakeAspectDependent)
91
92
93 class tst_QAspectEngine : public QObject
94 {
95 Q_OBJECT
96 public:
97 tst_QAspectEngine() : QObject() {}
98 ~tst_QAspectEngine() {}
99
100 private Q_SLOTS:
101 // TODO: Add more QAspectEngine tests
102
103 void constructionDestruction()
104 {
105 QAspectEngine *engine = new QAspectEngine;
106 QVERIFY(engine->rootEntity() == nullptr);
107 delete engine;
108 }
109
110 void setRootEntity()
111 {
112 QAspectEngine *engine = new QAspectEngine;
113
114 QEntity *e = new QEntity;
115 e->setObjectName("root");
116 engine->setRootEntity(QEntityPtr(e));
117
118 QSharedPointer<QEntity> root = engine->rootEntity();
119 QVERIFY(root == e);
120 QVERIFY(root->objectName() == "root");
121 root = QSharedPointer<QEntity>();
122 QVERIFY(engine->rootEntity()->objectName() == "root");
123
124 delete engine;
125 }
126
127 void shouldNotCrashInNormalStartupShutdownSequence()
128 {
129 #ifdef Q_OS_MACOS
130 QSKIP("Test frequently times out. See QTBUG-80660.");
131 #endif
132
133 // GIVEN
134 // An initialized aspect engine...
135 QAspectEngine engine;
136 // ...and a simple aspect
137 PrintRootAspect *aspect = new PrintRootAspect;
138
139 // WHEN
140 // We register the aspect
141 engine.registerAspect(aspect);
142
143 // THEN
144 const auto registeredAspects = engine.aspects();
145 QCOMPARE(registeredAspects.size(), 1);
146 QCOMPARE(registeredAspects.first(), aspect);
147
148 // WHEN
149 QEntityPtr entity(new QEntity);
150 entity->setObjectName("RootEntity");
151 // we set a scene root entity
152 engine.setRootEntity(entity);
153
154 QEventLoop eventLoop;
155 QTimer::singleShot(1000, &eventLoop, SLOT(quit()));
156 eventLoop.exec();
157
158 // THEN
159 // we don't crash and...
160 const auto rootEntity = engine.rootEntity();
161 QCOMPARE(rootEntity, entity);
162
163 // WHEN
164 // we set an empty/null scene root...
165 engine.setRootEntity(QEntityPtr());
166 QTimer::singleShot(600, &eventLoop, SLOT(quit()));
167
168 // ...and allow events to process...
169 eventLoop.exec();
170
171 // THEN
172 // ... we don't crash.
173
174 // TODO: Add more tests to check for
175 // * re-setting a scene
176 // * deregistering aspects
177 // * destroying the aspect engine
178 }
179
180 void shouldNotCrashWhenEntityIsAddedThenImmediatelyDeleted()
181 {
182 // GIVEN
183 // An initialized aspect engine...
184 QAspectEngine engine;
185 // ...and a simple aspect
186 PrintRootAspect *aspect = new PrintRootAspect;
187
188 // WHEN
189 // We register the aspect
190 engine.registerAspect(aspect);
191
192 // THEN
193 const auto registeredAspects = engine.aspects();
194 QCOMPARE(registeredAspects.size(), 1);
195 QCOMPARE(registeredAspects.first(), aspect);
196
197 // WHEN
198 QEntityPtr entity(new QEntity);
199 entity->setObjectName("RootEntity");
200 // we set a scene root entity
201 engine.setRootEntity(entity);
202
203 QEventLoop eventLoop;
204 QTimer::singleShot(1000, &eventLoop, SLOT(quit()));
205 eventLoop.exec();
206
207 // THEN
208 // we don't crash and...
209 const auto rootEntity = engine.rootEntity();
210 QCOMPARE(rootEntity, entity);
211
212 // WHEN
213 // we create a child node and delete within the same spin of
214 // the event loop
215 Qt3DCore::QEntity *childEntity = new Qt3DCore::QEntity(entity.data());
216 delete childEntity;
217 entity = nullptr;
218 QTimer::singleShot(600, &eventLoop, SLOT(quit()));
219
220 // ...and allow events to process...
221 eventLoop.exec();
222
223 // ...and we don't crash when the childEntity is removed from the
224 // post construction init routines
225 }
226
227 void shouldNotCrashOnShutdownWhenComponentIsCreatedWithParentBeforeItsEntity()
228 {
229 // GIVEN
230 QEntity *root = new QEntity;
231 // A component parented to an entity...
232 QComponent *component = new Qt3DCore::QTransform(root);
233 // ... created *before* the entity it will be added to.
234 QEntity *entity = new QEntity(root);
235 entity->addComponent(component);
236
237 // An initialized engine (so that the arbiter has been fed)
238 QAspectEngine engine;
239
240 // WHEN
241 engine.setRootEntity(QEntityPtr(root));
242
243 // THEN
244 // Nothing particular happen on exit, especially no crash
245 }
246
247 void shouldRegisterAspectsByName()
248 {
249 // GIVEN
250 QAspectEngine engine;
251
252 // THEN
253 QVERIFY(engine.aspects().isEmpty());
254
255 // WHEN
256 engine.registerAspect("fake");
257
258 // THEN
259 QCOMPARE(engine.aspects().size(), 1);
260 QVERIFY(qobject_cast<FakeAspect*>(engine.aspects().first()));
261 }
262
263 void shouldListLoadedAspects()
264 {
265 // GIVEN
266 QAspectEngine engine;
267
268 // THEN
269 QCOMPARE(engine.executeCommand("list aspects").toString(),
270 QString("No loaded aspect"));
271
272 // WHEN
273 engine.registerAspect("fake");
274
275 // THEN
276 QCOMPARE(engine.executeCommand("list aspects").toString(),
277 QString("fake"));
278
279 // WHEN
280 engine.registerAspect("otherfake");
281
282 // THEN
283 QCOMPARE(engine.executeCommand("list aspects").toString(),
284 QString("fake\notherfake"));
285
286 // WHEN
287 engine.registerAspect(new FakeAspect3);
288
289 // THEN
290 QCOMPARE(engine.executeCommand("list aspects").toString(),
291 QString("fake\notherfake\n<unnamed>"));
292 }
293
294 void shouldDelegateCommandsToAspects()
295 {
296 // GIVEN
297 QAspectEngine engine;
298 engine.registerAspect("fake");
299 engine.registerAspect("otherfake");
300 engine.registerAspect(new FakeAspect3);
301
302 // WHEN
303 QVariant output = engine.executeCommand("fake echo foo bar");
304
305 // THEN
306 QVERIFY(output.isValid());
307 QCOMPARE(output.toString(), QString("FakeAspect said 'foo bar'"));
308
309 // WHEN
310 output = engine.executeCommand("otherfake echo fizz buzz");
311
312 // THEN
313 QVERIFY(output.isValid());
314 QCOMPARE(output.toString(), QString("FakeAspect2 said 'fizz buzz'"));
315
316 // WHEN
317 output = engine.executeCommand("mehfake echo fizz buzz");
318
319 // THEN
320 QVERIFY(!output.isValid());
321
322 // WHEN
323 output = engine.executeCommand("fake mooh fizz buzz");
324
325 // THEN
326 QVERIFY(!output.isValid());
327 }
328
329 void shouldRegisterDependentAspects()
330 {
331 // GIVEN
332 QAspectEngine engine;
333
334 // THEN
335 QVERIFY(engine.aspects().isEmpty());
336
337 // WHEN
338 engine.registerAspect("dependfake");
339
340 // THEN
341 QCOMPARE(engine.aspects().size(), 2);
342 QVERIFY(qobject_cast<FakeAspect*>(engine.aspects().at(0)));
343 QVERIFY(qobject_cast<FakeAspectDependent*>(engine.aspects().at(1)));
344 }
345 };
346
347 QTEST_MAIN(tst_QAspectEngine)
348
349 #include "tst_qaspectengine.moc"