| 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 <Qt3DCore/qnode.h> |
| 6 | #include <Qt3DCore/qentity.h> |
| 7 | #include <Qt3DCore/qcomponent.h> |
| 8 | #include <Qt3DCore/qaspectengine.h> |
| 9 | #include <Qt3DCore/qabstractaspect.h> |
| 10 | #include <Qt3DCore/private/qscene_p.h> |
| 11 | #include <Qt3DCore/private/qaspectengine_p.h> |
| 12 | #include <Qt3DCore/private/qaspectengine_p.h> |
| 13 | #include <private/qabstractaspect_p.h> |
| 14 | |
| 15 | #include <Qt3DCore/private/qnode_p.h> |
| 16 | #include <Qt3DCore/private/qcomponent_p.h> |
| 17 | #include <Qt3DCore/private/vector_helper_p.h> |
| 18 | #include <QSet> |
| 19 | #include <QSignalSpy> |
| 20 | #include <testarbiter.h> |
| 21 | #include <vector> |
| 22 | |
| 23 | class tst_Nodes : public QObject |
| 24 | { |
| 25 | Q_OBJECT |
| 26 | public: |
| 27 | tst_Nodes() : QObject() |
| 28 | { |
| 29 | qRegisterMetaType<Qt3DCore::QNode*>(); |
| 30 | } |
| 31 | ~tst_Nodes() {} |
| 32 | |
| 33 | private slots: |
| 34 | void defaultNodeConstruction(); |
| 35 | void defaultComponentConstruction(); |
| 36 | void defaultEntityConstrution(); |
| 37 | |
| 38 | void appendSingleChildNodeToNodeNoSceneExplicitParenting(); |
| 39 | void appendSingleChildNodeToNodeNoSceneImplicitParenting(); |
| 40 | void appendMultipleChildNodesToNodeNoScene(); |
| 41 | |
| 42 | void appendSingleChildNodeToNodeSceneExplicitParenting(); |
| 43 | void appendSingleChildNodeToNodeSceneImplicitParenting(); |
| 44 | void appendMultipleChildNodesToNodeScene(); |
| 45 | |
| 46 | void checkParentChangeToNull(); |
| 47 | void checkParentChangeToOtherParent(); |
| 48 | void checkParentChangeFromExistingBackendParentToNewlyCreatedParent(); |
| 49 | void checkBackendNodesCreatedFromTopDown(); //QTBUG-74106 |
| 50 | void checkBackendNodesCreatedFromTopDownWithReparenting(); |
| 51 | void checkAllBackendCreationDoneInSingleFrame(); |
| 52 | |
| 53 | void removingSingleChildNodeFromNode(); |
| 54 | void removingMultipleChildNodesFromNode(); |
| 55 | |
| 56 | void appendingChildEntitiesToNode(); |
| 57 | void removingChildEntitiesFromNode(); |
| 58 | |
| 59 | void checkConstructionSetParentMix(); // QTBUG-60612 |
| 60 | void checkParentingQEntityToQNode(); // QTBUG-73905 |
| 61 | void checkConstructionWithParent(); |
| 62 | void checkConstructionWithNonRootParent(); // QTBUG-73986 |
| 63 | void checkConstructionAsListElement(); |
| 64 | void checkSceneIsSetOnConstructionWithParent(); // QTBUG-69352 |
| 65 | void checkSubNodePostConstructIsCalledWhenReferincingNodeProperty(); // QTBUG-79350 |
| 66 | |
| 67 | void appendingComponentToEntity(); |
| 68 | void appendingParentlessComponentToEntityWithoutScene(); |
| 69 | void appendingParentlessComponentToEntityWithScene(); |
| 70 | void appendingParentlessComponentToNonRootEntity(); |
| 71 | void removingComponentFromEntity(); |
| 72 | |
| 73 | void changeCustomProperty(); |
| 74 | void checkDestruction(); |
| 75 | |
| 76 | void checkDefaultConstruction(); |
| 77 | void checkPropertyChanges(); |
| 78 | void checkEnabledUpdate(); |
| 79 | |
| 80 | void checkNodeRemovedFromDirtyListOnDestruction(); |
| 81 | |
| 82 | void checkBookkeepingSingleNode(); |
| 83 | void checkBookkeeping_QVector_OfNode(); |
| 84 | void checkBookkeeping_stdvector_OfNode(); |
| 85 | }; |
| 86 | |
| 87 | class MyQNode : public Qt3DCore::QNode |
| 88 | { |
| 89 | Q_OBJECT |
| 90 | Q_PROPERTY(QString customProperty READ customProperty WRITE setCustomProperty NOTIFY customPropertyChanged) |
| 91 | Q_PROPERTY(Qt3DCore::QNode *nodeProperty READ nodeProperty WRITE setNodeProperty NOTIFY nodePropertyChanged) |
| 92 | |
| 93 | public: |
| 94 | explicit MyQNode(Qt3DCore::QNode *parent = nullptr) |
| 95 | : QNode(parent) |
| 96 | , m_nodeProperty(nullptr) |
| 97 | {} |
| 98 | |
| 99 | ~MyQNode() |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | void setCustomProperty(const QString &s) |
| 104 | { |
| 105 | if (m_customProperty == s) |
| 106 | return; |
| 107 | |
| 108 | m_customProperty = s; |
| 109 | emit customPropertyChanged(); |
| 110 | } |
| 111 | |
| 112 | QString customProperty() const |
| 113 | { |
| 114 | return m_customProperty; |
| 115 | } |
| 116 | |
| 117 | void setArbiterAndScene(Qt3DCore::QChangeArbiter *arbiter, |
| 118 | Qt3DCore::QScene *scene = nullptr) |
| 119 | { |
| 120 | Q_ASSERT(arbiter); |
| 121 | if (scene) |
| 122 | scene->setArbiter(arbiter); |
| 123 | Qt3DCore::QNodePrivate::get(this)->setScene(scene); |
| 124 | Qt3DCore::QNodePrivate::get(this)->setArbiter(arbiter); |
| 125 | } |
| 126 | |
| 127 | void setSimulateBackendCreated(bool created) |
| 128 | { |
| 129 | Qt3DCore::QNodePrivate::get(this)->m_hasBackendNode = created; |
| 130 | } |
| 131 | |
| 132 | Qt3DCore::QNode *nodeProperty() const { return m_nodeProperty; } |
| 133 | |
| 134 | const QList<Qt3DCore::QNode *> &attributes() const { return m_attributes; } |
| 135 | |
| 136 | public slots: |
| 137 | void setNodeProperty(Qt3DCore::QNode *node) |
| 138 | { |
| 139 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 140 | if (m_nodeProperty == node) |
| 141 | return; |
| 142 | |
| 143 | if (m_nodeProperty) |
| 144 | d->unregisterDestructionHelper(m_nodeProperty); |
| 145 | |
| 146 | if (node && !node->parent()) |
| 147 | node->setParent(this); |
| 148 | |
| 149 | m_nodeProperty = node; |
| 150 | |
| 151 | // Ensures proper bookkeeping |
| 152 | if (m_nodeProperty) |
| 153 | d->registerDestructionHelper(m_nodeProperty, &MyQNode::setNodeProperty, m_nodeProperty); |
| 154 | |
| 155 | emit nodePropertyChanged(node); |
| 156 | } |
| 157 | |
| 158 | void addAttribute(Qt3DCore::QNode *attribute) |
| 159 | { |
| 160 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 161 | if (!m_attributes.contains(attribute)) { |
| 162 | m_attributes.append(attribute); |
| 163 | |
| 164 | // Ensures proper bookkeeping |
| 165 | d->registerDestructionHelper(attribute, &MyQNode::removeAttribute, m_attributes); |
| 166 | |
| 167 | // We need to add it as a child of the current node if it has been declared inline |
| 168 | // Or not previously added as a child of the current node so that |
| 169 | // 1) The backend gets notified about it's creation |
| 170 | // 2) When the current node is destroyed, it gets destroyed as well |
| 171 | if (!attribute->parent()) |
| 172 | attribute->setParent(this); |
| 173 | |
| 174 | d->update(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void removeAttribute(Qt3DCore::QNode *attribute) |
| 179 | { |
| 180 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 181 | d->update(); |
| 182 | |
| 183 | m_attributes.removeOne(attribute); |
| 184 | // Remove bookkeeping connection |
| 185 | d->unregisterDestructionHelper(attribute); |
| 186 | } |
| 187 | |
| 188 | signals: |
| 189 | void customPropertyChanged(); |
| 190 | void nodePropertyChanged(Qt3DCore::QNode *node); |
| 191 | |
| 192 | protected: |
| 193 | QString m_customProperty; |
| 194 | Qt3DCore::QNode *m_nodeProperty; |
| 195 | QList<Qt3DCore::QNode *> m_attributes; |
| 196 | }; |
| 197 | |
| 198 | |
| 199 | class MyQNodeVec : public Qt3DCore::QNode |
| 200 | { |
| 201 | Q_OBJECT |
| 202 | public: |
| 203 | explicit MyQNodeVec(Qt3DCore::QNode *parent = nullptr) |
| 204 | : QNode(parent) |
| 205 | {} |
| 206 | |
| 207 | ~MyQNodeVec() |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | void setArbiterAndScene(Qt3DCore::QChangeArbiter *arbiter, |
| 212 | Qt3DCore::QScene *scene = nullptr) |
| 213 | { |
| 214 | Q_ASSERT(arbiter); |
| 215 | if (scene) |
| 216 | scene->setArbiter(arbiter); |
| 217 | Qt3DCore::QNodePrivate::get(this)->setScene(scene); |
| 218 | Qt3DCore::QNodePrivate::get(this)->setArbiter(arbiter); |
| 219 | } |
| 220 | |
| 221 | void setSimulateBackendCreated(bool created) |
| 222 | { |
| 223 | Qt3DCore::QNodePrivate::get(this)->m_hasBackendNode = created; |
| 224 | } |
| 225 | |
| 226 | const std::vector<Qt3DCore::QNode *> &attributes() const { return m_attributes; } |
| 227 | |
| 228 | public slots: |
| 229 | |
| 230 | void addAttribute(Qt3DCore::QNode *attribute) |
| 231 | { |
| 232 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 233 | if (!Qt3DCore::contains(m_attributes, attribute)) { |
| 234 | m_attributes.push_back(attribute); |
| 235 | |
| 236 | // Ensures proper bookkeeping |
| 237 | d->registerDestructionHelper(attribute, &MyQNodeVec::removeAttribute, m_attributes); |
| 238 | |
| 239 | // We need to add it as a child of the current node if it has been declared inline |
| 240 | // Or not previously added as a child of the current node so that |
| 241 | // 1) The backend gets notified about it's creation |
| 242 | // 2) When the current node is destroyed, it gets destroyed as well |
| 243 | if (!attribute->parent()) |
| 244 | attribute->setParent(this); |
| 245 | |
| 246 | d->update(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void removeAttribute(Qt3DCore::QNode *attribute) |
| 251 | { |
| 252 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 253 | d->update(); |
| 254 | |
| 255 | m_attributes.erase(std::remove(m_attributes.begin(), |
| 256 | m_attributes.end(), |
| 257 | attribute), m_attributes.end()); |
| 258 | // Remove bookkeeping connection |
| 259 | d->unregisterDestructionHelper(attribute); |
| 260 | } |
| 261 | |
| 262 | protected: |
| 263 | std::vector<Qt3DCore::QNode *> m_attributes; |
| 264 | }; |
| 265 | |
| 266 | class MyQEntity : public Qt3DCore::QEntity |
| 267 | { |
| 268 | Q_OBJECT |
| 269 | Q_PROPERTY(MyQNode *nodeProperty READ nodeProperty WRITE setNodeProperty NOTIFY nodePropertyChanged) |
| 270 | public: |
| 271 | explicit MyQEntity(Qt3DCore::QNode *parent = nullptr) |
| 272 | : QEntity(parent) |
| 273 | {} |
| 274 | |
| 275 | ~MyQEntity() |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | void setArbiterAndScene(Qt3DCore::QChangeArbiter *arbiter, |
| 280 | Qt3DCore::QScene *scene = nullptr) |
| 281 | { |
| 282 | Q_ASSERT(arbiter); |
| 283 | if (scene) |
| 284 | scene->setArbiter(arbiter); |
| 285 | Qt3DCore::QNodePrivate::get(this)->setScene(scene); |
| 286 | Qt3DCore::QNodePrivate::get(this)->setArbiter(arbiter); |
| 287 | } |
| 288 | |
| 289 | void setArbiterAndEngine(Qt3DCore::QChangeArbiter *arbiter, |
| 290 | Qt3DCore::QAspectEngine *engine) |
| 291 | { |
| 292 | Q_ASSERT(arbiter); |
| 293 | Q_ASSERT(engine); |
| 294 | auto scene = Qt3DCore::QAspectEnginePrivate::get(engine)->m_scene; |
| 295 | Qt3DCore::QNodePrivate::get(this)->setScene(scene); |
| 296 | Qt3DCore::QNodePrivate::get(this)->setArbiter(arbiter); |
| 297 | |
| 298 | if (scene) { |
| 299 | scene->setArbiter(arbiter); |
| 300 | engine->setRootEntity(Qt3DCore::QEntityPtr(this, [](QEntity *) {})); |
| 301 | scene->setArbiter(arbiter); |
| 302 | Qt3DCore::QNodePrivate::get(this)->setArbiter(arbiter); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void setSimulateBackendCreated(bool created) |
| 307 | { |
| 308 | Qt3DCore::QNodePrivate::get(this)->m_hasBackendNode = created; |
| 309 | } |
| 310 | |
| 311 | MyQNode *nodeProperty() const { return m_nodeProperty; } |
| 312 | |
| 313 | void addAttribute(MyQNode *attribute) |
| 314 | { |
| 315 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 316 | if (!m_attributes.contains(attribute)) { |
| 317 | m_attributes.append(attribute); |
| 318 | |
| 319 | // Ensures proper bookkeeping |
| 320 | d->registerDestructionHelper(attribute, &MyQEntity::removeAttribute, m_attributes); |
| 321 | |
| 322 | // We need to add it as a child of the current node if it has been declared inline |
| 323 | // Or not previously added as a child of the current node so that |
| 324 | // 1) The backend gets notified about it's creation |
| 325 | // 2) When the current node is destroyed, it gets destroyed as well |
| 326 | if (!attribute->parent()) |
| 327 | attribute->setParent(this); |
| 328 | |
| 329 | d->update(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void removeAttribute(MyQNode *attribute) |
| 334 | { |
| 335 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 336 | d->update(); |
| 337 | |
| 338 | m_attributes.removeOne(attribute); |
| 339 | // Remove bookkeeping connection |
| 340 | d->unregisterDestructionHelper(attribute); |
| 341 | } |
| 342 | |
| 343 | public slots: |
| 344 | void setNodeProperty(MyQNode *node) |
| 345 | { |
| 346 | Qt3DCore::QNodePrivate *d = Qt3DCore::QNodePrivate::get(this); |
| 347 | if (m_nodeProperty == node) |
| 348 | return; |
| 349 | |
| 350 | if (m_nodeProperty) |
| 351 | d->unregisterDestructionHelper(m_nodeProperty); |
| 352 | |
| 353 | if (node && !node->parent()) |
| 354 | node->setParent(this); |
| 355 | |
| 356 | m_nodeProperty = node; |
| 357 | |
| 358 | // Ensures proper bookkeeping |
| 359 | if (m_nodeProperty) |
| 360 | d->registerDestructionHelper(m_nodeProperty, &MyQEntity::setNodeProperty, m_nodeProperty); |
| 361 | |
| 362 | emit nodePropertyChanged(node); |
| 363 | } |
| 364 | |
| 365 | signals: |
| 366 | void nodePropertyChanged(MyQNode *node); |
| 367 | |
| 368 | private: |
| 369 | MyQNode *m_nodeProperty; |
| 370 | QList<MyQNode *> m_attributes; |
| 371 | }; |
| 372 | |
| 373 | class MyQComponent : public Qt3DCore::QComponent |
| 374 | { |
| 375 | Q_OBJECT |
| 376 | public: |
| 377 | explicit MyQComponent(Qt3DCore::QNode *parent = nullptr) : QComponent(parent) |
| 378 | {} |
| 379 | void setArbiter(Qt3DCore::QChangeArbiter *arbiter) |
| 380 | { |
| 381 | Q_ASSERT(arbiter); |
| 382 | Qt3DCore::QComponentPrivate::get(this)->setArbiter(arbiter); |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | class MyFakeMaterial : public Qt3DCore::QComponent |
| 387 | { |
| 388 | Q_OBJECT |
| 389 | public: |
| 390 | explicit MyFakeMaterial(Qt3DCore::QNode *parent = nullptr) |
| 391 | : QComponent(parent) |
| 392 | , m_effect(new MyQNode(this)) |
| 393 | , m_technique(new MyQNode(m_effect)) |
| 394 | , m_renderPass(new MyQNode(m_technique)) |
| 395 | { |
| 396 | } |
| 397 | |
| 398 | void setArbiter(Qt3DCore::QChangeArbiter *arbiter) |
| 399 | { |
| 400 | Q_ASSERT(arbiter); |
| 401 | Qt3DCore::QComponentPrivate::get(this)->setArbiter(arbiter); |
| 402 | } |
| 403 | |
| 404 | MyQNode *m_effect; |
| 405 | MyQNode *m_technique; |
| 406 | MyQNode *m_renderPass; |
| 407 | }; |
| 408 | |
| 409 | class TestAspectPrivate; |
| 410 | class TestAspect : public Qt3DCore::QAbstractAspect |
| 411 | { |
| 412 | Q_OBJECT |
| 413 | public: |
| 414 | explicit TestAspect(QObject *parent = nullptr); |
| 415 | |
| 416 | enum ChangeType { Creation, Destruction }; |
| 417 | |
| 418 | void clearNodes() |
| 419 | { |
| 420 | events.clear(); |
| 421 | allNodes.clear(); |
| 422 | } |
| 423 | |
| 424 | void addEvent(const Qt3DCore::QNodeId &id, ChangeType change) |
| 425 | { |
| 426 | events.push_back(Event{ change, id}); |
| 427 | } |
| 428 | |
| 429 | QList<Qt3DCore::QNodeId> filteredEvents(ChangeType change) const |
| 430 | { |
| 431 | QList<Qt3DCore::QNodeId> result; |
| 432 | for (const auto &event : events) { |
| 433 | if (event.type == change) |
| 434 | result.push_back(event.nodeId); |
| 435 | } |
| 436 | return result; |
| 437 | } |
| 438 | |
| 439 | struct Event{ |
| 440 | ChangeType type; |
| 441 | Qt3DCore::QNodeId nodeId; |
| 442 | }; |
| 443 | |
| 444 | mutable QList<Event> events; |
| 445 | mutable QHash<Qt3DCore::QNodeId, Qt3DCore::QNode *> allNodes; |
| 446 | |
| 447 | private: |
| 448 | Q_DECLARE_PRIVATE(TestAspect) |
| 449 | explicit TestAspect(TestAspectPrivate &dd, QObject *parent); |
| 450 | }; |
| 451 | |
| 452 | class TestFunctor : public Qt3DCore::QBackendNodeMapper |
| 453 | { |
| 454 | public: |
| 455 | TestFunctor(TestAspect *aspect) : m_aspect(aspect) {} |
| 456 | |
| 457 | Qt3DCore::QBackendNode *create(Qt3DCore::QNodeId id) const override |
| 458 | { |
| 459 | auto node = new Qt3DCore::QBackendNode; |
| 460 | m_Nodes.insert(id, node); |
| 461 | m_aspect->addEvent(id, TestAspect::Creation); |
| 462 | return node; |
| 463 | } |
| 464 | |
| 465 | Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const override |
| 466 | { |
| 467 | return m_Nodes.value(id, nullptr); |
| 468 | } |
| 469 | |
| 470 | void destroy(Qt3DCore::QNodeId id) const override |
| 471 | { |
| 472 | if (m_Nodes.contains(id)) { |
| 473 | m_aspect->addEvent(id, TestAspect::Destruction); |
| 474 | delete m_Nodes.take(id); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | private: |
| 479 | mutable QHash<Qt3DCore::QNodeId, Qt3DCore::QBackendNode *> m_Nodes; |
| 480 | TestAspect *m_aspect; |
| 481 | }; |
| 482 | |
| 483 | class TestAspectPrivate : public Qt3DCore::QAbstractAspectPrivate |
| 484 | { |
| 485 | public: |
| 486 | TestAspectPrivate() = default; |
| 487 | void syncDirtyFrontEndNode(Qt3DCore::QNode *node, Qt3DCore::QBackendNode *backend, |
| 488 | bool firstTime) const override |
| 489 | { |
| 490 | Q_UNUSED(backend); |
| 491 | auto q = q_func(); |
| 492 | if (firstTime) |
| 493 | q->allNodes.insert(node->id(), node); |
| 494 | } |
| 495 | |
| 496 | Q_DECLARE_PUBLIC(TestAspect) |
| 497 | }; |
| 498 | |
| 499 | TestAspect::TestAspect(QObject *parent) : TestAspect(*new TestAspectPrivate, parent) |
| 500 | { |
| 501 | registerBackendType<Qt3DCore::QEntity>(QSharedPointer<TestFunctor>::create(this)); |
| 502 | registerBackendType<MyQEntity>(QSharedPointer<TestFunctor>::create(this)); |
| 503 | registerBackendType<MyQNode>(QSharedPointer<TestFunctor>::create(this)); |
| 504 | registerBackendType<Qt3DCore::QNode>(QSharedPointer<TestFunctor>::create(this)); |
| 505 | } |
| 506 | |
| 507 | TestAspect::TestAspect(TestAspectPrivate &dd, QObject *parent) |
| 508 | : Qt3DCore::QAbstractAspect(dd, parent) |
| 509 | { |
| 510 | setObjectName(QStringLiteral("Test Aspect")); |
| 511 | } |
| 512 | |
| 513 | namespace { |
| 514 | void verifyChildrenCreatedBeforeParents(Qt3DCore::QNode *root, TestAspect *aspect) |
| 515 | { |
| 516 | QSet<Qt3DCore::QNodeId> processedNodes; |
| 517 | processedNodes.insert(root->id()); |
| 518 | for (const auto &nodeId : aspect->filteredEvents(TestAspect::Creation)) { |
| 519 | const auto node = aspect->allNodes.value(nodeId); |
| 520 | Q_ASSERT(node); |
| 521 | const auto parentNode = node->parentNode(); |
| 522 | QVERIFY(parentNode == nullptr || processedNodes.contains(parentNode->id())); |
| 523 | processedNodes.insert(nodeId); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void tst_Nodes::defaultNodeConstruction() |
| 529 | { |
| 530 | // GIVEN |
| 531 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 532 | |
| 533 | // THEN |
| 534 | QVERIFY(node != nullptr); |
| 535 | QVERIFY(node->children().isEmpty()); |
| 536 | |
| 537 | // GIVEN |
| 538 | MyQNode *node2 = new MyQNode(node.data()); |
| 539 | QCoreApplication::processEvents(); |
| 540 | |
| 541 | // THEN |
| 542 | QVERIFY(node2->parent() == node.data()); |
| 543 | QVERIFY(!node->children().isEmpty()); |
| 544 | QVERIFY(node2->children().isEmpty()); |
| 545 | } |
| 546 | |
| 547 | void tst_Nodes::defaultComponentConstruction() |
| 548 | { |
| 549 | // GIVEN |
| 550 | QScopedPointer<MyQComponent> comp(new MyQComponent()); |
| 551 | MyQComponent *comp2 = new MyQComponent(comp.data()); |
| 552 | QCoreApplication::processEvents(); |
| 553 | |
| 554 | // THEN |
| 555 | QVERIFY(comp != nullptr); |
| 556 | QCOMPARE(comp2->parent(), comp.data()); |
| 557 | } |
| 558 | |
| 559 | void tst_Nodes::defaultEntityConstrution() |
| 560 | { |
| 561 | // GIVEN |
| 562 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity()); |
| 563 | Qt3DCore::QEntity *entity2 = new Qt3DCore::QEntity(entity.data()); |
| 564 | QCoreApplication::processEvents(); |
| 565 | |
| 566 | // THEN |
| 567 | QVERIFY(entity->components().isEmpty()); |
| 568 | QVERIFY(entity2->components().isEmpty()); |
| 569 | QCOMPARE(entity2->parent(), entity.data()); |
| 570 | } |
| 571 | |
| 572 | void tst_Nodes::appendSingleChildNodeToNodeNoSceneExplicitParenting() |
| 573 | { |
| 574 | // Check nodes added when no scene is set |
| 575 | // GIVEN |
| 576 | TestArbiter arbiter; |
| 577 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 578 | node->setArbiterAndScene(&arbiter); |
| 579 | |
| 580 | // THEN |
| 581 | QVERIFY(Qt3DCore::QNodePrivate::get(node.data())->scene() == nullptr); |
| 582 | // WHEN |
| 583 | QScopedPointer<MyQNode> child(new MyQNode()); |
| 584 | |
| 585 | // THEN |
| 586 | QVERIFY(child->parent() == nullptr); |
| 587 | |
| 588 | // WHEN |
| 589 | child->setParent(node.data()); |
| 590 | |
| 591 | // THEN |
| 592 | QVERIFY(child->parent() == node.data()); |
| 593 | QVERIFY(child->parentNode() == node.data()); |
| 594 | QCOMPARE(node->children().size(), 1); |
| 595 | |
| 596 | // Events are only sent when a scene is set on the root node |
| 597 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 598 | } |
| 599 | |
| 600 | void tst_Nodes::appendSingleChildNodeToNodeNoSceneImplicitParenting() |
| 601 | { |
| 602 | // Check nodes added when no scene is set |
| 603 | // GIVEN |
| 604 | TestArbiter arbiter; |
| 605 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 606 | node->setArbiterAndScene(&arbiter); |
| 607 | |
| 608 | // THEN |
| 609 | QVERIFY(Qt3DCore::QNodePrivate::get(node.data())->scene() == nullptr); |
| 610 | // WHEN |
| 611 | QScopedPointer<MyQNode> child(new MyQNode(node.data())); |
| 612 | QCoreApplication::processEvents(); |
| 613 | |
| 614 | // THEN |
| 615 | QVERIFY(child->parent() == node.data()); |
| 616 | QVERIFY(child->parentNode() == node.data()); |
| 617 | QCOMPARE(node->children().size(), 1); |
| 618 | |
| 619 | // Events are only sent when a scene is set on the root node |
| 620 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 621 | } |
| 622 | |
| 623 | void tst_Nodes::appendMultipleChildNodesToNodeNoScene() |
| 624 | { |
| 625 | // Check multiple nodes added with no scene set |
| 626 | // GIVEN |
| 627 | TestArbiter arbiter; |
| 628 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 629 | node->setArbiterAndScene(&arbiter); |
| 630 | |
| 631 | // THEN |
| 632 | QVERIFY(Qt3DCore::QNodePrivate::get(node.data())->scene() == nullptr); |
| 633 | // WHEN |
| 634 | for (int i = 0; i < 10; i++) { |
| 635 | // WHEN |
| 636 | Qt3DCore::QNode *child = nullptr; |
| 637 | if (i % 2 == 0) { |
| 638 | child = new MyQNode(node.data()); |
| 639 | QCoreApplication::processEvents(); |
| 640 | } else { |
| 641 | child = new MyQNode(); |
| 642 | child->setParent(node.data()); |
| 643 | } |
| 644 | // THEN |
| 645 | QVERIFY(child->parent() == node.data()); |
| 646 | } |
| 647 | |
| 648 | // THEN |
| 649 | QCOMPARE(node->children().size(), 10); |
| 650 | |
| 651 | // Events are only sent when a scene is set on the root node |
| 652 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 653 | } |
| 654 | |
| 655 | void tst_Nodes::appendSingleChildNodeToNodeSceneExplicitParenting() |
| 656 | { |
| 657 | // Check nodes added when scene is set |
| 658 | // GIVEN |
| 659 | Qt3DCore::QScene scene; |
| 660 | TestArbiter arbiter; |
| 661 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 662 | // WHEN |
| 663 | node->setArbiterAndScene(&arbiter, &scene); |
| 664 | node->setSimulateBackendCreated(true); |
| 665 | |
| 666 | // THEN |
| 667 | QVERIFY(Qt3DCore::QNodePrivate::get(node.data())->scene() != nullptr); |
| 668 | |
| 669 | // WHEN |
| 670 | QScopedPointer<MyQNode> child(new MyQNode()); |
| 671 | |
| 672 | // THEN |
| 673 | QVERIFY(child->parent() == nullptr); |
| 674 | QVERIFY(Qt3DCore::QNodePrivate::get(child.data())->scene() == nullptr); |
| 675 | |
| 676 | // WHEN |
| 677 | child->setParent(node.data()); |
| 678 | |
| 679 | // THEN |
| 680 | QVERIFY(child->parent() == node.data()); |
| 681 | QVERIFY(child->parentNode() == node.data()); |
| 682 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // Child Added |
| 683 | QCOMPARE(node->children().size(), 1); |
| 684 | QVERIFY(Qt3DCore::QNodePrivate::get(child.data())->scene() != nullptr); |
| 685 | } |
| 686 | |
| 687 | void tst_Nodes::appendSingleChildNodeToNodeSceneImplicitParenting() |
| 688 | { |
| 689 | // Check nodes added when scene is set |
| 690 | // GIVEN |
| 691 | Qt3DCore::QScene scene; |
| 692 | TestArbiter arbiter; |
| 693 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 694 | |
| 695 | // WHEN |
| 696 | node->setArbiterAndScene(&arbiter, &scene); |
| 697 | // THEN |
| 698 | QVERIFY(Qt3DCore::QNodePrivate::get(node.data())->scene() != nullptr); |
| 699 | |
| 700 | // WHEN |
| 701 | QScopedPointer<MyQNode> child(new MyQNode(node.data())); |
| 702 | QCoreApplication::processEvents(); |
| 703 | |
| 704 | // THEN |
| 705 | QVERIFY(child->parent() == node.data()); |
| 706 | QVERIFY(child->parentNode() == node.data()); |
| 707 | QVERIFY(Qt3DCore::QNodePrivate::get(child.data())->scene() != nullptr); |
| 708 | |
| 709 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 710 | QCOMPARE(node->children().size(), 1); |
| 711 | } |
| 712 | |
| 713 | void tst_Nodes::appendMultipleChildNodesToNodeScene() |
| 714 | { |
| 715 | // Check nodes added when scene is set |
| 716 | |
| 717 | // GIVEN |
| 718 | Qt3DCore::QScene scene; |
| 719 | TestArbiter arbiter; |
| 720 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 721 | |
| 722 | // WHEN |
| 723 | node->setArbiterAndScene(&arbiter, &scene); |
| 724 | node->setSimulateBackendCreated(true); |
| 725 | // THEN |
| 726 | QVERIFY(Qt3DCore::QNodePrivate::get(node.data())->scene() != nullptr); |
| 727 | |
| 728 | // WHEN |
| 729 | const auto childCount = 10; |
| 730 | for (int i = 0; i < childCount; i++) { |
| 731 | // WHEN |
| 732 | Qt3DCore::QNode *child = nullptr; |
| 733 | if (i % 2 == 0) { |
| 734 | child = new MyQNode(node.data()); |
| 735 | } else { |
| 736 | child = new MyQNode(); |
| 737 | child->setParent(node.data()); |
| 738 | } |
| 739 | |
| 740 | // THEN parent and scene should be set synchronously |
| 741 | QVERIFY(child->parent() == node.data()); |
| 742 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->scene() == Qt3DCore::QNodePrivate::get(node.data())->m_scene); |
| 743 | } |
| 744 | // THEN |
| 745 | QCOMPARE(node->children().size(), 10); |
| 746 | |
| 747 | // WHEN |
| 748 | QCoreApplication::processEvents(); |
| 749 | |
| 750 | // THEN backend is notified after the event loop spins. Only the parent is notified |
| 751 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 752 | } |
| 753 | |
| 754 | void tst_Nodes::checkParentChangeToNull() |
| 755 | { |
| 756 | // GIVEN |
| 757 | Qt3DCore::QScene scene; |
| 758 | TestArbiter arbiter; |
| 759 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 760 | |
| 761 | // WHEN |
| 762 | root->setArbiterAndScene(&arbiter, &scene); |
| 763 | QScopedPointer<Qt3DCore::QNode> child(new MyQNode(root.data())); |
| 764 | QCoreApplication::processEvents(); |
| 765 | |
| 766 | // THEN |
| 767 | QVERIFY(child->parent() == root.data()); |
| 768 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 769 | QCOMPARE(root->children().size(), 1); |
| 770 | |
| 771 | // WHEN |
| 772 | arbiter.clear(); |
| 773 | child->setParent(Q_NODE_NULLPTR); |
| 774 | |
| 775 | // THEN |
| 776 | QVERIFY(child->parent() == nullptr); |
| 777 | QCOMPARE(root->children().size(), 0); |
| 778 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 779 | } |
| 780 | |
| 781 | void tst_Nodes::checkParentChangeToOtherParent() |
| 782 | { |
| 783 | // GIVEN |
| 784 | Qt3DCore::QScene scene; |
| 785 | TestArbiter arbiter; |
| 786 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 787 | root->setArbiterAndScene(&arbiter, &scene); |
| 788 | QScopedPointer<MyQNode> parent1(new MyQNode(root.data())); |
| 789 | QScopedPointer<MyQNode> parent2(new MyQNode(root.data())); |
| 790 | QCoreApplication::processEvents(); |
| 791 | |
| 792 | // THEN |
| 793 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // only parent node has changed |
| 794 | |
| 795 | // WHEN |
| 796 | arbiter.clear(); |
| 797 | QScopedPointer<Qt3DCore::QNode> child(new MyQNode(parent1.data())); |
| 798 | QCoreApplication::processEvents(); |
| 799 | |
| 800 | // THEN |
| 801 | QVERIFY(child->parent() == parent1.data()); |
| 802 | QCOMPARE(parent1->children().size(), 1); |
| 803 | QCOMPARE(parent2->children().size(), 0); |
| 804 | QVERIFY(Qt3DCore::QNodePrivate::get(child.data())->scene() != nullptr); |
| 805 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // 1 node added to children change |
| 806 | |
| 807 | // WHEN |
| 808 | arbiter.clear(); |
| 809 | child->setParent(parent2.data()); |
| 810 | |
| 811 | // THEN |
| 812 | QVERIFY(child->parent() == parent2.data()); |
| 813 | QCOMPARE(parent1->children().size(), 0); |
| 814 | QCOMPARE(parent2->children().size(), 1); |
| 815 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 816 | |
| 817 | // CHECK event 1 is a Node Removed event |
| 818 | // this no longer generated because nodes don't have backends yet |
| 819 | |
| 820 | QVERIFY(!Qt3DCore::QNodePrivate::get(child.data())->m_hasBackendNode); |
| 821 | } |
| 822 | |
| 823 | void tst_Nodes::checkParentChangeFromExistingBackendParentToNewlyCreatedParent() |
| 824 | { |
| 825 | // GIVEN |
| 826 | TestArbiter arbiter; |
| 827 | Qt3DCore::QAspectEngine engine; |
| 828 | engine.setRunMode(Qt3DCore::QAspectEngine::Manual); |
| 829 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 830 | root->setArbiterAndEngine(&arbiter, &engine); |
| 831 | auto aspect = new TestAspect; |
| 832 | engine.registerAspect(aspect); |
| 833 | |
| 834 | MyQNode *child(new MyQNode(root.data())); |
| 835 | MyQNode *child2(new MyQNode(root.data())); |
| 836 | |
| 837 | QCoreApplication::processEvents(); |
| 838 | engine.processFrame(); |
| 839 | |
| 840 | // Due to the way we create root, it has a backend |
| 841 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->m_hasBackendNode == true); |
| 842 | QCOMPARE(aspect->events.size(), 2); |
| 843 | QCOMPARE(aspect->events[0].type, TestAspect::Creation); |
| 844 | QCOMPARE(aspect->events[0].nodeId, child->id()); |
| 845 | QCOMPARE(aspect->events[1].type, TestAspect::Creation); |
| 846 | QCOMPARE(aspect->events[1].nodeId, child2->id()); |
| 847 | |
| 848 | // THEN |
| 849 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // only parent node has changed |
| 850 | |
| 851 | // WHEN -> Reparenting child with backend node to new parent with no backend yet |
| 852 | aspect->clearNodes(); |
| 853 | arbiter.clear(); |
| 854 | QScopedPointer<Qt3DCore::QNode> newParent(new MyQNode(root.data())); |
| 855 | child->setParent(newParent.data()); |
| 856 | |
| 857 | // THEN |
| 858 | QVERIFY(child->parent() == newParent.data()); |
| 859 | QCOMPARE(newParent->childNodes().size(), 1); |
| 860 | QCOMPARE(child2->childNodes().size(), 0); |
| 861 | QCOMPARE(root->childNodes().size(), 2); |
| 862 | QVERIFY(Qt3DCore::QNodePrivate::get(newParent.data())->m_hasBackendNode == false); |
| 863 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->m_hasBackendNode == false); |
| 864 | QVERIFY(Qt3DCore::QNodePrivate::get(newParent.data())->scene() != nullptr); |
| 865 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->scene() != nullptr); |
| 866 | |
| 867 | // WHEN |
| 868 | QCoreApplication::processEvents(); |
| 869 | engine.processFrame(); |
| 870 | |
| 871 | // THEN |
| 872 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // only parent node has changed |
| 873 | QVERIFY(Qt3DCore::QNodePrivate::get(newParent.data())->m_hasBackendNode == true); |
| 874 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->m_hasBackendNode == true); |
| 875 | |
| 876 | // WHEN -> Changing parent to node with existing backend |
| 877 | aspect->clearNodes(); |
| 878 | child->setParent(child2); |
| 879 | |
| 880 | // THEN |
| 881 | QCOMPARE(arbiter.dirtyNodes().size(), 3); // old parent, new parent, and child have all changed |
| 882 | arbiter.clear(); |
| 883 | |
| 884 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->m_hasBackendNode == true); |
| 885 | QCOMPARE(root->childNodes().size(), 2); |
| 886 | QCOMPARE(child2->childNodes().size(), 1); |
| 887 | QCOMPARE(newParent->childNodes().size(), 0); |
| 888 | |
| 889 | // WHEN -> Changing to parent which has no backend |
| 890 | QScopedPointer<Qt3DCore::QNode> newParent2(new MyQNode(root.data())); |
| 891 | child->setParent(newParent2.data()); |
| 892 | |
| 893 | // THEN |
| 894 | QVERIFY(Qt3DCore::QNodePrivate::get(newParent2.data())->m_hasBackendNode == false); |
| 895 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->m_hasBackendNode == false); |
| 896 | QVERIFY(Qt3DCore::QNodePrivate::get(newParent2.data())->scene() != nullptr); |
| 897 | QVERIFY(Qt3DCore::QNodePrivate::get(child)->scene() != nullptr); |
| 898 | |
| 899 | // WHEN |
| 900 | QCoreApplication::processEvents(); |
| 901 | engine.processFrame(); |
| 902 | |
| 903 | // THEN |
| 904 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 905 | } |
| 906 | |
| 907 | //Test creation changes happen for an entire subtree at once, starting at the top |
| 908 | // so that parents are always created before their children. Even if the front-end |
| 909 | // nodes are constructed in a different order. |
| 910 | void tst_Nodes::checkBackendNodesCreatedFromTopDown() |
| 911 | { |
| 912 | // GIVEN |
| 913 | TestArbiter arbiter; |
| 914 | Qt3DCore::QAspectEngine engine; |
| 915 | auto aspect = new TestAspect; |
| 916 | engine.registerAspect(aspect); |
| 917 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 918 | |
| 919 | root->setArbiterAndEngine(&arbiter, &engine); |
| 920 | QScopedPointer<Qt3DCore::QNode> parentWithBackend(new MyQNode(root.data())); |
| 921 | |
| 922 | // create parent backend node |
| 923 | QCoreApplication::processEvents(); |
| 924 | QVERIFY(Qt3DCore::QNodePrivate::get(parentWithBackend.get())->m_hasBackendNode); |
| 925 | |
| 926 | // WHEN -> creating 3 nodes and setting one as a property on the other |
| 927 | // child2 is set as property on child1, but is created AFTER child1 |
| 928 | arbiter.clear(); |
| 929 | MyQNode *dummyParent(new MyQNode(parentWithBackend.data())); |
| 930 | MyQNode *child1(new MyQNode(parentWithBackend.data())); |
| 931 | MyQNode *child2(new MyQNode(dummyParent)); |
| 932 | child1->setNodeProperty(child2); |
| 933 | |
| 934 | // THEN - we should have no events because the new nodes have no backend yet |
| 935 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 936 | |
| 937 | // WHEN - create the backend nodes |
| 938 | QCoreApplication::processEvents(); |
| 939 | |
| 940 | // THEN |
| 941 | QVERIFY(dummyParent->parent() == parentWithBackend.data()); |
| 942 | QVERIFY(child1->parent() == parentWithBackend.data()); |
| 943 | QVERIFY(child2->parent() == dummyParent); |
| 944 | |
| 945 | // THEN |
| 946 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // only parent node has changed |
| 947 | |
| 948 | { |
| 949 | QVERIFY(Qt3DCore::QNodePrivate::get(dummyParent)->m_hasBackendNode); |
| 950 | QVERIFY(Qt3DCore::QNodePrivate::get(child1)->m_hasBackendNode); |
| 951 | QVERIFY(Qt3DCore::QNodePrivate::get(child2)->m_hasBackendNode); |
| 952 | |
| 953 | verifyChildrenCreatedBeforeParents(root.get(), aspect); |
| 954 | } |
| 955 | |
| 956 | } |
| 957 | |
| 958 | void tst_Nodes::checkBackendNodesCreatedFromTopDownWithReparenting() |
| 959 | { |
| 960 | // GIVEN |
| 961 | TestArbiter arbiter; |
| 962 | Qt3DCore::QAspectEngine engine; |
| 963 | auto aspect = new TestAspect; |
| 964 | engine.registerAspect(aspect); |
| 965 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 966 | |
| 967 | root->setArbiterAndEngine(&arbiter, &engine); |
| 968 | QScopedPointer<Qt3DCore::QNode> parentWithBackend(new MyQNode(root.data())); |
| 969 | |
| 970 | // create parent backend node |
| 971 | QCoreApplication::processEvents(); |
| 972 | QCoreApplication::processEvents(); |
| 973 | QVERIFY(Qt3DCore::QNodePrivate::get(parentWithBackend.get())->m_hasBackendNode); |
| 974 | |
| 975 | // WHEN -> creating a node with a parent with backend, then reparenting it to another |
| 976 | // parent with backend created after it. |
| 977 | arbiter.clear(); |
| 978 | auto node1 = new MyQNode(parentWithBackend.data()); |
| 979 | auto parent1 = new MyQNode(parentWithBackend.data()); |
| 980 | node1->setParent(parent1); |
| 981 | |
| 982 | QCoreApplication::processEvents(); |
| 983 | |
| 984 | // THEN |
| 985 | QVERIFY(node1->parent() == parent1); |
| 986 | QVERIFY(parent1->parent() == parentWithBackend.data()); |
| 987 | verifyChildrenCreatedBeforeParents(root.get(), aspect); |
| 988 | |
| 989 | |
| 990 | // WHEN -> creating 2 nodes with no parent and reparenting the first to the second |
| 991 | auto node2 = new MyQNode(nullptr); |
| 992 | auto parent2 = new MyQNode(nullptr); |
| 993 | node2->setParent(parent2); |
| 994 | parent2->setParent(parentWithBackend.get()); |
| 995 | |
| 996 | QCoreApplication::processEvents(); |
| 997 | |
| 998 | // THEN |
| 999 | QVERIFY(node2->parent() == parent2); |
| 1000 | QVERIFY(parent2->parent() == parentWithBackend.data()); |
| 1001 | verifyChildrenCreatedBeforeParents(root.get(), aspect); |
| 1002 | } |
| 1003 | |
| 1004 | void tst_Nodes::removingSingleChildNodeFromNode() |
| 1005 | { |
| 1006 | // GIVEN |
| 1007 | TestArbiter arbiter; |
| 1008 | Qt3DCore::QAspectEngine engine; |
| 1009 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1010 | QScopedPointer<Qt3DCore::QNode> child(new MyQNode()); |
| 1011 | |
| 1012 | // WHEN |
| 1013 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1014 | root->setSimulateBackendCreated(true); |
| 1015 | child->setParent(root.data()); |
| 1016 | |
| 1017 | // Clear any creation event |
| 1018 | arbiter.clear(); |
| 1019 | |
| 1020 | // THEN |
| 1021 | QVERIFY(root->children().size() == 1); |
| 1022 | QVERIFY(child->parentNode() == root.data()); |
| 1023 | |
| 1024 | // WHEN |
| 1025 | child->setParent(Q_NODE_NULLPTR); |
| 1026 | |
| 1027 | // THEN |
| 1028 | QVERIFY(child->parent() == nullptr); |
| 1029 | QVERIFY(root->children().size() == 0); |
| 1030 | |
| 1031 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1032 | } |
| 1033 | |
| 1034 | void tst_Nodes::checkAllBackendCreationDoneInSingleFrame() |
| 1035 | { |
| 1036 | // GIVEN |
| 1037 | TestArbiter arbiter; |
| 1038 | Qt3DCore::QAspectEngine engine; |
| 1039 | engine.setRunMode(Qt3DCore::QAspectEngine::Manual); |
| 1040 | auto aspect = new TestAspect; |
| 1041 | engine.registerAspect(aspect); |
| 1042 | |
| 1043 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1044 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1045 | |
| 1046 | QCoreApplication::processEvents(); |
| 1047 | |
| 1048 | // THEN |
| 1049 | // Due to the way we create root, it has a backend |
| 1050 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->m_hasBackendNode == true); |
| 1051 | QCOMPARE(aspect->events.size(), 1); |
| 1052 | QCOMPARE(aspect->events[0].type, TestAspect::Creation); |
| 1053 | QCOMPARE(aspect->events[0].nodeId, root->id()); |
| 1054 | |
| 1055 | // WHEN -> create 2 children: |
| 1056 | // 1. a child with parent with backend node |
| 1057 | // 2. a child with no parent that is then reparented to a parent with backend node |
| 1058 | aspect->clearNodes(); |
| 1059 | auto child1 = new MyQNode(root.data()); |
| 1060 | auto child2 = new MyQNode; |
| 1061 | child2->setParent(root.data()); |
| 1062 | |
| 1063 | // THEN - reparented child should have a backend node, but other child should |
| 1064 | // still be waiting |
| 1065 | QCOMPARE(child1->parent(), root.data()); |
| 1066 | QCOMPARE(child2->parent(), root.data()); |
| 1067 | QVERIFY(Qt3DCore::QNodePrivate::get(child1)->m_hasBackendNode == false); |
| 1068 | QVERIFY(Qt3DCore::QNodePrivate::get(child2)->m_hasBackendNode == true); |
| 1069 | |
| 1070 | // WHEN |
| 1071 | QCoreApplication::processEvents(); |
| 1072 | engine.processFrame(); |
| 1073 | |
| 1074 | // THEN - both children have their backend nodes actually created. |
| 1075 | QCOMPARE(aspect->events.size(), 2); |
| 1076 | QCOMPARE(aspect->events[0].type, TestAspect::Creation); |
| 1077 | QCOMPARE(aspect->events[0].nodeId, child2->id()); |
| 1078 | QCOMPARE(aspect->events[1].type, TestAspect::Creation); |
| 1079 | QCOMPARE(aspect->events[1].nodeId, child1->id()); |
| 1080 | } |
| 1081 | |
| 1082 | void tst_Nodes::removingMultipleChildNodesFromNode() |
| 1083 | { |
| 1084 | // GIVEN |
| 1085 | TestArbiter arbiter; |
| 1086 | Qt3DCore::QAspectEngine engine; |
| 1087 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1088 | |
| 1089 | // WHEN |
| 1090 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1091 | |
| 1092 | // THEN |
| 1093 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1094 | |
| 1095 | // WHEN |
| 1096 | Qt3DCore::QNodeIdVector childIds; |
| 1097 | for (int i = 0; i < 10; i++) { |
| 1098 | auto child = new MyQNode(root.data()); |
| 1099 | childIds.push_back(child->id()); |
| 1100 | } |
| 1101 | |
| 1102 | QCoreApplication::processEvents(); |
| 1103 | |
| 1104 | // THEN |
| 1105 | QCOMPARE(root->children().size(), 10); |
| 1106 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1107 | |
| 1108 | // WHEN |
| 1109 | arbiter.clear(); |
| 1110 | auto cl = root->children(); |
| 1111 | qDeleteAll(cl); |
| 1112 | |
| 1113 | // THEN |
| 1114 | QVERIFY(root->children().size() == 0); |
| 1115 | QCOMPARE(arbiter.dirtyNodes().size(), 0); // since all nodes are deleted, there's no backend to notify |
| 1116 | } |
| 1117 | |
| 1118 | void tst_Nodes::appendingChildEntitiesToNode() |
| 1119 | { |
| 1120 | // GIVEN |
| 1121 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 1122 | |
| 1123 | // WHEN |
| 1124 | Qt3DCore::QEntity *childEntity = new Qt3DCore::QEntity(root.data()); |
| 1125 | QCoreApplication::processEvents(); |
| 1126 | |
| 1127 | // THEN |
| 1128 | QVERIFY(root->children().first() == childEntity); |
| 1129 | QVERIFY(childEntity->parentEntity() == nullptr); |
| 1130 | QVERIFY(childEntity->parentNode() == root.data()); |
| 1131 | } |
| 1132 | |
| 1133 | void tst_Nodes::removingChildEntitiesFromNode() |
| 1134 | { |
| 1135 | // GIVEN |
| 1136 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 1137 | |
| 1138 | // WHEN |
| 1139 | Qt3DCore::QEntity *childEntity = new Qt3DCore::QEntity(root.data()); |
| 1140 | QCoreApplication::processEvents(); |
| 1141 | |
| 1142 | // THEN |
| 1143 | QVERIFY(root->children().first() == childEntity); |
| 1144 | QVERIFY(childEntity->parentEntity() == nullptr); |
| 1145 | QVERIFY(childEntity->parentNode() == root.data()); |
| 1146 | |
| 1147 | // WHEN |
| 1148 | childEntity->setParent(Q_NODE_NULLPTR); |
| 1149 | |
| 1150 | // THEN |
| 1151 | QVERIFY(root->children().isEmpty()); |
| 1152 | QVERIFY(childEntity->parentNode() == nullptr); |
| 1153 | QVERIFY(childEntity->parent() == nullptr); |
| 1154 | } |
| 1155 | |
| 1156 | void tst_Nodes::checkConstructionSetParentMix() |
| 1157 | { |
| 1158 | // GIVEN |
| 1159 | TestArbiter arbiter; |
| 1160 | Qt3DCore::QAspectEngine engine; |
| 1161 | auto aspect = new TestAspect; |
| 1162 | engine.registerAspect(aspect); |
| 1163 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1164 | |
| 1165 | // WHEN |
| 1166 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1167 | root->setSimulateBackendCreated(true); |
| 1168 | |
| 1169 | // THEN |
| 1170 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1171 | |
| 1172 | // WHEN |
| 1173 | Qt3DCore::QEntity *subTreeRoot = new Qt3DCore::QEntity(root.data()); |
| 1174 | |
| 1175 | for (int i = 0; i < 100; ++i) { |
| 1176 | Qt3DCore::QEntity *child = new Qt3DCore::QEntity(); |
| 1177 | child->setParent(subTreeRoot); |
| 1178 | } |
| 1179 | |
| 1180 | // THEN |
| 1181 | QCoreApplication::processEvents(); |
| 1182 | QCOMPARE(root->children().size(), 1); |
| 1183 | QCOMPARE(subTreeRoot->children().size(), 100); |
| 1184 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // 1 child added (subTree to root) |
| 1185 | |
| 1186 | // Ensure first event is subTreeRoot |
| 1187 | verifyChildrenCreatedBeforeParents(root.data(), aspect); |
| 1188 | } |
| 1189 | |
| 1190 | void tst_Nodes::checkParentingQEntityToQNode() |
| 1191 | { |
| 1192 | // GIVEN |
| 1193 | TestArbiter arbiter; |
| 1194 | Qt3DCore::QAspectEngine engine; |
| 1195 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1196 | |
| 1197 | // WHEN |
| 1198 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1199 | root->setSimulateBackendCreated(true); |
| 1200 | |
| 1201 | // THEN |
| 1202 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1203 | |
| 1204 | // WHEN |
| 1205 | auto subTreeRoot = new Qt3DCore::QEntity(root.data()); |
| 1206 | auto childEntity = new Qt3DCore::QEntity(subTreeRoot); |
| 1207 | auto childNode = new Qt3DCore::QNode(subTreeRoot); |
| 1208 | |
| 1209 | // THEN |
| 1210 | QCoreApplication::processEvents(); |
| 1211 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1212 | |
| 1213 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->m_hasBackendNode); |
| 1214 | QVERIFY(Qt3DCore::QNodePrivate::get(subTreeRoot)->m_hasBackendNode); |
| 1215 | QVERIFY(Qt3DCore::QNodePrivate::get(childEntity)->m_hasBackendNode); |
| 1216 | QVERIFY(Qt3DCore::QNodePrivate::get(childNode)->m_hasBackendNode); |
| 1217 | |
| 1218 | // WHEN we reparent the childEntity to the childNode (QNode) |
| 1219 | arbiter.clear(); |
| 1220 | childEntity->setParent(childNode); |
| 1221 | QCoreApplication::processEvents(); |
| 1222 | |
| 1223 | // THEN |
| 1224 | QCOMPARE(arbiter.dirtyNodes().size(), 3); |
| 1225 | } |
| 1226 | |
| 1227 | void tst_Nodes::checkConstructionWithParent() |
| 1228 | { |
| 1229 | // GIVEN |
| 1230 | TestArbiter arbiter; |
| 1231 | Qt3DCore::QAspectEngine engine; |
| 1232 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1233 | |
| 1234 | // WHEN |
| 1235 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1236 | root->setSimulateBackendCreated(true); |
| 1237 | |
| 1238 | // THEN |
| 1239 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1240 | |
| 1241 | // WHEN we create a child and then set it as a Node* property |
| 1242 | auto *node = new MyQNode(root.data()); |
| 1243 | root->setNodeProperty(node); |
| 1244 | |
| 1245 | // THEN we should get one child added change |
| 1246 | QCoreApplication::processEvents(); |
| 1247 | QCOMPARE(root->children().size(), 1); |
| 1248 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // 1 child added change |
| 1249 | QCOMPARE(arbiter.dirtyNodes().front(), root.data()); |
| 1250 | } |
| 1251 | |
| 1252 | void tst_Nodes::checkConstructionWithNonRootParent() |
| 1253 | { |
| 1254 | // GIVEN |
| 1255 | TestArbiter arbiter; |
| 1256 | Qt3DCore::QAspectEngine engine; |
| 1257 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1258 | |
| 1259 | // WHEN |
| 1260 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1261 | root->setSimulateBackendCreated(true); |
| 1262 | QScopedPointer<MyQNode> parent(new MyQNode(root.data())); |
| 1263 | |
| 1264 | // THEN |
| 1265 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1266 | QVERIFY(Qt3DCore::QNodePrivate::get(parent.data())->scene() != nullptr); |
| 1267 | |
| 1268 | // WHEN we create a child and then set it as a Node* property |
| 1269 | auto *child = new MyQNode(parent.data()); |
| 1270 | root->setNodeProperty(child); |
| 1271 | |
| 1272 | // THEN we should get |
| 1273 | // - one creation change for parent, |
| 1274 | // - one creation change for child, |
| 1275 | // - one child added change for root->parent, |
| 1276 | // - and one property change event, |
| 1277 | // in that order. |
| 1278 | QCoreApplication::processEvents(); |
| 1279 | QCOMPARE(root->children().size(), 1); |
| 1280 | QCOMPARE(parent->children().size(), 1); |
| 1281 | |
| 1282 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // 1 child added changes |
| 1283 | QCOMPARE(arbiter.dirtyNodes().front(), root.data()); |
| 1284 | } |
| 1285 | |
| 1286 | void tst_Nodes::checkConstructionAsListElement() |
| 1287 | { |
| 1288 | // GIVEN |
| 1289 | TestArbiter arbiter; |
| 1290 | Qt3DCore::QAspectEngine engine; |
| 1291 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1292 | |
| 1293 | // WHEN |
| 1294 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1295 | root->setSimulateBackendCreated(true); |
| 1296 | |
| 1297 | // THEN |
| 1298 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1299 | |
| 1300 | // WHEN we create a child and then set it as a Node* property |
| 1301 | auto *node = new MyQNode(root.data()); |
| 1302 | root->addAttribute(node); |
| 1303 | |
| 1304 | // THEN we should get one creation change, one child added change |
| 1305 | // and one property change event, in that order. |
| 1306 | QCoreApplication::processEvents(); |
| 1307 | |
| 1308 | QCOMPARE(root->children().size(), 1); |
| 1309 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // 1 property change |
| 1310 | } |
| 1311 | |
| 1312 | void tst_Nodes::checkSceneIsSetOnConstructionWithParent() |
| 1313 | { |
| 1314 | // GIVEN |
| 1315 | TestArbiter arbiter; |
| 1316 | Qt3DCore::QScene scene; |
| 1317 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 1318 | |
| 1319 | // WHEN |
| 1320 | root->setArbiterAndScene(&arbiter, &scene); |
| 1321 | root->setSimulateBackendCreated(true); |
| 1322 | |
| 1323 | // THEN |
| 1324 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1325 | |
| 1326 | // WHEN |
| 1327 | Qt3DCore::QEntity *subTreeRoot = new Qt3DCore::QEntity(root.data()); |
| 1328 | |
| 1329 | QList<Qt3DCore::QEntity *> children; |
| 1330 | QList<Qt3DCore::QComponent *> cmps; |
| 1331 | children.reserve(5); |
| 1332 | cmps.reserve(5); |
| 1333 | for (int i = 0; i < 5; ++i) { |
| 1334 | const auto cmp = new MyQComponent(subTreeRoot); |
| 1335 | cmps << cmp; |
| 1336 | children << new Qt3DCore::QEntity(cmp); |
| 1337 | |
| 1338 | // When cmp is full created, it will also send the creation change for its child entity |
| 1339 | } |
| 1340 | QCoreApplication::processEvents(); |
| 1341 | QCOMPARE(root->children().size(), 1); |
| 1342 | QCOMPARE(subTreeRoot->children().size(), 5); |
| 1343 | QCOMPARE(arbiter.dirtyNodes().size(), 1); // 1 child added (subTree to root) |
| 1344 | |
| 1345 | arbiter.clear(); |
| 1346 | |
| 1347 | // Add component in a separate loop to ensure components are added after |
| 1348 | // entities have been fully created |
| 1349 | for (int i = 0; i < 5; ++i) { |
| 1350 | children.at(i)->addComponent(cmps.at(i)); |
| 1351 | } |
| 1352 | |
| 1353 | // THEN |
| 1354 | QCOMPARE(arbiter.dirtyNodes().size(), 5); |
| 1355 | } |
| 1356 | |
| 1357 | void tst_Nodes::checkSubNodePostConstructIsCalledWhenReferincingNodeProperty() |
| 1358 | { |
| 1359 | // GIVEN |
| 1360 | TestArbiter arbiter; |
| 1361 | Qt3DCore::QAspectEngine engine; |
| 1362 | Qt3DCore::QScene scene(&engine); |
| 1363 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 1364 | |
| 1365 | // WHEN |
| 1366 | root->setArbiterAndScene(&arbiter, &scene); |
| 1367 | root->setSimulateBackendCreated(true); |
| 1368 | |
| 1369 | // THEN |
| 1370 | QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); |
| 1371 | |
| 1372 | // WHEN |
| 1373 | Qt3DCore::QEntity *subTreeRoot = new Qt3DCore::QEntity(root.data()); |
| 1374 | QCoreApplication::processEvents(); |
| 1375 | |
| 1376 | // THEN |
| 1377 | QVERIFY(Qt3DCore::QNodePrivate::get(subTreeRoot)->m_hasBackendNode); |
| 1378 | |
| 1379 | // WHEN |
| 1380 | MyFakeMaterial *material = new MyFakeMaterial(subTreeRoot); |
| 1381 | subTreeRoot->addComponent(material); |
| 1382 | |
| 1383 | // THEN |
| 1384 | QVERIFY(Qt3DCore::QNodePrivate::get(material)->m_hasBackendNode); |
| 1385 | QVERIFY(Qt3DCore::QNodePrivate::get(material->m_effect)->m_hasBackendNode); |
| 1386 | QVERIFY(Qt3DCore::QNodePrivate::get(material->m_technique)->m_hasBackendNode); |
| 1387 | QVERIFY(Qt3DCore::QNodePrivate::get(material->m_renderPass)->m_hasBackendNode); |
| 1388 | } |
| 1389 | |
| 1390 | void tst_Nodes::appendingParentlessComponentToEntityWithoutScene() |
| 1391 | { |
| 1392 | // GIVEN |
| 1393 | TestArbiter arbiter; |
| 1394 | { |
| 1395 | QScopedPointer<MyQEntity> entity(new MyQEntity()); |
| 1396 | entity->setArbiterAndScene(&arbiter); |
| 1397 | entity->setSimulateBackendCreated(true); |
| 1398 | |
| 1399 | MyQComponent *comp = new MyQComponent(); |
| 1400 | comp->setArbiter(&arbiter); |
| 1401 | |
| 1402 | // THEN |
| 1403 | QVERIFY(entity->parentNode() == nullptr); |
| 1404 | QVERIFY(entity->children().size() == 0); |
| 1405 | QVERIFY(entity->components().empty()); |
| 1406 | QVERIFY(comp->parentNode() == nullptr); |
| 1407 | |
| 1408 | // WHEN |
| 1409 | entity->addComponent(comp); |
| 1410 | |
| 1411 | // THEN |
| 1412 | QVERIFY(entity->components().size() == 1); |
| 1413 | QVERIFY(entity->components().first() == comp); |
| 1414 | QVERIFY(comp->parentNode() == entity.data()); |
| 1415 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1416 | |
| 1417 | // Note: since QEntity has no scene in this test case, we only have the |
| 1418 | // ComponentAdded event In theory we should also get the NodeCreated event |
| 1419 | // when setting the parent but that doesn't happen since no scene is |
| 1420 | // actually set on the entity and that QNodePrivate::_q_addChild will |
| 1421 | // return early in such a case. |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | void tst_Nodes::appendingParentlessComponentToNonRootEntity() |
| 1426 | { |
| 1427 | // GIVEN |
| 1428 | TestArbiter arbiter; |
| 1429 | Qt3DCore::QAspectEngine engine; |
| 1430 | |
| 1431 | { |
| 1432 | QScopedPointer<MyQEntity> root(new MyQEntity()); |
| 1433 | root->setArbiterAndEngine(&arbiter, &engine); |
| 1434 | root->setSimulateBackendCreated(true); |
| 1435 | |
| 1436 | QCoreApplication::processEvents(); |
| 1437 | |
| 1438 | QScopedPointer<MyQEntity> entity(new MyQEntity(root.data())); |
| 1439 | MyQComponent *comp = new MyQComponent(); |
| 1440 | |
| 1441 | // THEN |
| 1442 | QVERIFY(root->parentNode() == nullptr); |
| 1443 | QVERIFY(root->children().size() == 1); |
| 1444 | QVERIFY(root->components().empty()); |
| 1445 | QVERIFY(entity->parentNode() == root.data()); |
| 1446 | QVERIFY(entity->children().size() == 0); |
| 1447 | QVERIFY(entity->components().empty()); |
| 1448 | QVERIFY(comp->parentNode() == nullptr); |
| 1449 | |
| 1450 | // WHEN |
| 1451 | entity->addComponent(comp); |
| 1452 | QCoreApplication::processEvents(); |
| 1453 | |
| 1454 | // THEN |
| 1455 | QVERIFY(entity->components().size() == 1); |
| 1456 | QVERIFY(entity->components().first() == comp); |
| 1457 | QVERIFY(comp->parentNode() == entity.data()); |
| 1458 | |
| 1459 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 1460 | QCOMPARE(arbiter.dirtyNodes().at(0), root.data()); |
| 1461 | QCOMPARE(arbiter.dirtyNodes().at(1), entity.data()); |
| 1462 | |
| 1463 | QVERIFY(Qt3DCore::QNodePrivate::get(entity.data())->m_hasBackendNode); |
| 1464 | QVERIFY(Qt3DCore::QNodePrivate::get(comp)->m_hasBackendNode); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | void tst_Nodes::appendingParentlessComponentToEntityWithScene() |
| 1469 | { |
| 1470 | // GIVEN |
| 1471 | TestArbiter arbiter; |
| 1472 | Qt3DCore::QAspectEngine engine; |
| 1473 | |
| 1474 | { |
| 1475 | QScopedPointer<MyQEntity> entity(new MyQEntity()); |
| 1476 | entity->setArbiterAndEngine(&arbiter, &engine); |
| 1477 | entity->setSimulateBackendCreated(true); |
| 1478 | |
| 1479 | QCoreApplication::processEvents(); |
| 1480 | |
| 1481 | MyQComponent *comp = new MyQComponent(); |
| 1482 | |
| 1483 | // THEN |
| 1484 | QVERIFY(entity->parentNode() == nullptr); |
| 1485 | QVERIFY(entity->children().size() == 0); |
| 1486 | QVERIFY(entity->components().empty()); |
| 1487 | QVERIFY(comp->parentNode() == nullptr); |
| 1488 | |
| 1489 | // WHEN |
| 1490 | entity->addComponent(comp); |
| 1491 | QCoreApplication::processEvents(); |
| 1492 | |
| 1493 | // THEN |
| 1494 | QVERIFY(entity->components().size() == 1); |
| 1495 | QVERIFY(entity->components().first() == comp); |
| 1496 | QVERIFY(comp->parentNode() == entity.data()); |
| 1497 | |
| 1498 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | void tst_Nodes::appendingComponentToEntity() |
| 1504 | { |
| 1505 | // GIVEN |
| 1506 | TestArbiter arbiter; |
| 1507 | { |
| 1508 | QScopedPointer<MyQEntity> entity(new MyQEntity()); |
| 1509 | entity->setArbiterAndScene(&arbiter); |
| 1510 | MyQComponent *comp = new MyQComponent(entity.data()); |
| 1511 | comp->setArbiter(&arbiter); |
| 1512 | QCoreApplication::processEvents(); |
| 1513 | |
| 1514 | // THEN |
| 1515 | QVERIFY(entity->parentNode() == nullptr); |
| 1516 | QVERIFY(entity->children().size() == 1); |
| 1517 | QVERIFY(entity->components().empty()); |
| 1518 | QVERIFY(comp->parentNode() == entity.data()); |
| 1519 | |
| 1520 | // WHEN |
| 1521 | entity->addComponent(comp); |
| 1522 | |
| 1523 | // THEN |
| 1524 | QVERIFY(entity->components().size() == 1); |
| 1525 | QVERIFY(entity->components().first() == comp); |
| 1526 | QVERIFY(comp->parentNode() == entity.data()); |
| 1527 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1528 | QCOMPARE(arbiter.dirtyNodes().first(), entity.data()); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | void tst_Nodes::removingComponentFromEntity() |
| 1533 | { |
| 1534 | // GIVEN |
| 1535 | TestArbiter arbiter; |
| 1536 | { |
| 1537 | QScopedPointer<MyQEntity> entity(new MyQEntity()); |
| 1538 | entity->setArbiterAndScene(&arbiter); |
| 1539 | MyQComponent *comp = new MyQComponent(); |
| 1540 | comp->setArbiter(&arbiter); |
| 1541 | |
| 1542 | // WHEN |
| 1543 | entity->addComponent(comp); |
| 1544 | |
| 1545 | // THEN |
| 1546 | QVERIFY(entity->components().size() == 1); |
| 1547 | QCOMPARE(entity->children().size(), 1); |
| 1548 | QVERIFY(comp->parent() == entity.data()); |
| 1549 | |
| 1550 | // WHEN |
| 1551 | arbiter.clear(); |
| 1552 | entity->removeComponent(comp); |
| 1553 | |
| 1554 | // THEN |
| 1555 | QVERIFY(entity->components().size() == 0); |
| 1556 | QVERIFY(comp->parent() == entity.data()); |
| 1557 | QVERIFY(entity->children().size() == 1); |
| 1558 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | void tst_Nodes::changeCustomProperty() |
| 1563 | { |
| 1564 | // GIVEN |
| 1565 | TestArbiter arbiter; |
| 1566 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 1567 | node->setArbiterAndScene(&arbiter); |
| 1568 | // WHEN |
| 1569 | node->setCustomProperty(QStringLiteral("foo")); |
| 1570 | // THEN |
| 1571 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1572 | QCOMPARE(arbiter.dirtyNodes().front(), node.data()); |
| 1573 | } |
| 1574 | |
| 1575 | void tst_Nodes::checkDestruction() |
| 1576 | { |
| 1577 | // GIVEN |
| 1578 | QScopedPointer<MyQNode> root(new MyQNode()); |
| 1579 | Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(root.data()); |
| 1580 | |
| 1581 | QCoreApplication::processEvents(); |
| 1582 | |
| 1583 | MyQComponent *comp1 = new MyQComponent(); |
| 1584 | MyQComponent *comp2 = new MyQComponent(); |
| 1585 | MyQComponent *comp3 = new MyQComponent(); |
| 1586 | |
| 1587 | entity->addComponent(comp1); |
| 1588 | entity->addComponent(comp2); |
| 1589 | entity->addComponent(comp3); |
| 1590 | |
| 1591 | // THEN |
| 1592 | QVERIFY(!root->children().isEmpty()); |
| 1593 | |
| 1594 | // WHEN |
| 1595 | delete entity; |
| 1596 | |
| 1597 | // THEN |
| 1598 | QVERIFY(root->children().isEmpty()); |
| 1599 | } |
| 1600 | |
| 1601 | void tst_Nodes::checkDefaultConstruction() |
| 1602 | { |
| 1603 | // GIVEN |
| 1604 | Qt3DCore::QNode node; |
| 1605 | |
| 1606 | // THEN |
| 1607 | QCOMPARE(node.parentNode(), nullptr); |
| 1608 | QCOMPARE(node.isEnabled(), true); |
| 1609 | } |
| 1610 | |
| 1611 | void tst_Nodes::checkPropertyChanges() |
| 1612 | { |
| 1613 | // GIVEN |
| 1614 | Qt3DCore::QNode parentNode; |
| 1615 | Qt3DCore::QNode node; |
| 1616 | |
| 1617 | { |
| 1618 | // WHEN |
| 1619 | QSignalSpy spy(&node, SIGNAL(parentChanged(QObject*))); |
| 1620 | Qt3DCore::QNode *newValue = &parentNode; |
| 1621 | node.setParent(newValue); |
| 1622 | |
| 1623 | // THEN |
| 1624 | QVERIFY(spy.isValid()); |
| 1625 | QCOMPARE(node.parentNode(), newValue); |
| 1626 | QCOMPARE(spy.size(), 1); |
| 1627 | |
| 1628 | // WHEN |
| 1629 | spy.clear(); |
| 1630 | node.setParent(newValue); |
| 1631 | |
| 1632 | // THEN |
| 1633 | QCOMPARE(node.parentNode(), newValue); |
| 1634 | QCOMPARE(spy.size(), 0); |
| 1635 | } |
| 1636 | { |
| 1637 | // WHEN |
| 1638 | QSignalSpy spy(&node, SIGNAL(enabledChanged(bool))); |
| 1639 | const bool newValue = false; |
| 1640 | node.setEnabled(newValue); |
| 1641 | |
| 1642 | // THEN |
| 1643 | QVERIFY(spy.isValid()); |
| 1644 | QCOMPARE(node.isEnabled(), newValue); |
| 1645 | QCOMPARE(spy.size(), 1); |
| 1646 | |
| 1647 | // WHEN |
| 1648 | spy.clear(); |
| 1649 | node.setEnabled(newValue); |
| 1650 | |
| 1651 | // THEN |
| 1652 | QCOMPARE(node.isEnabled(), newValue); |
| 1653 | QCOMPARE(spy.size(), 0); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | void tst_Nodes::checkEnabledUpdate() |
| 1658 | { |
| 1659 | // GIVEN |
| 1660 | TestArbiter arbiter; |
| 1661 | Qt3DCore::QNode node; |
| 1662 | arbiter.setArbiterOnNode(&node); |
| 1663 | |
| 1664 | { |
| 1665 | // WHEN |
| 1666 | node.setEnabled(false); |
| 1667 | |
| 1668 | // THEN |
| 1669 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1670 | QCOMPARE(arbiter.dirtyNodes().front(), &node); |
| 1671 | |
| 1672 | arbiter.clear(); |
| 1673 | } |
| 1674 | |
| 1675 | { |
| 1676 | // WHEN |
| 1677 | node.setEnabled(false); |
| 1678 | |
| 1679 | // THEN |
| 1680 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 1681 | } |
| 1682 | |
| 1683 | } |
| 1684 | |
| 1685 | void tst_Nodes::checkNodeRemovedFromDirtyListOnDestruction() |
| 1686 | { |
| 1687 | // GIVEN |
| 1688 | TestArbiter arbiter; |
| 1689 | Qt3DCore::QScene scene; |
| 1690 | |
| 1691 | { |
| 1692 | // GIVEN |
| 1693 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 1694 | node->setArbiterAndScene(&arbiter, &scene); |
| 1695 | |
| 1696 | // WHEN |
| 1697 | node->setEnabled(false); |
| 1698 | |
| 1699 | // THEN |
| 1700 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 1701 | QCOMPARE(arbiter.dirtyNodes().front(), node.data()); |
| 1702 | |
| 1703 | // WHEN |
| 1704 | // scene should be unset and node removed from arbiter dirtyList |
| 1705 | node.reset(); |
| 1706 | |
| 1707 | // THEN |
| 1708 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 1709 | } |
| 1710 | { |
| 1711 | // GIVEN |
| 1712 | QScopedPointer<MyQNode> node(new MyQNode()); |
| 1713 | node->setArbiterAndScene(&arbiter, &scene); |
| 1714 | |
| 1715 | |
| 1716 | Qt3DCore::QNode *child = new Qt3DCore::QNode(node.data()); |
| 1717 | // Wait for deferred initialization of child node |
| 1718 | QCoreApplication::processEvents(); |
| 1719 | |
| 1720 | // WHEN |
| 1721 | child->setEnabled(false); |
| 1722 | |
| 1723 | // THEN |
| 1724 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 1725 | QCOMPARE(arbiter.dirtyNodes().at(0), node.data()); |
| 1726 | QCOMPARE(arbiter.dirtyNodes().at(1), child); |
| 1727 | |
| 1728 | // WHEN |
| 1729 | // scene should be unset and child node removed from arbiter dirtyList |
| 1730 | node.reset(); |
| 1731 | |
| 1732 | // THEN |
| 1733 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | void tst_Nodes::checkBookkeepingSingleNode() |
| 1738 | { |
| 1739 | // GIVEN |
| 1740 | MyQNode root; |
| 1741 | |
| 1742 | // THEN |
| 1743 | QVERIFY(root.nodeProperty() == nullptr); |
| 1744 | |
| 1745 | { |
| 1746 | // WHEN |
| 1747 | MyQNode node; |
| 1748 | root.setNodeProperty(&node); |
| 1749 | |
| 1750 | // THEN |
| 1751 | QCOMPARE(root.nodeProperty(), &node); |
| 1752 | } |
| 1753 | // WHEN -> node destroyed |
| 1754 | |
| 1755 | // THEN |
| 1756 | QVERIFY(root.nodeProperty() == nullptr); |
| 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | void tst_Nodes::checkBookkeeping_QVector_OfNode() |
| 1761 | { |
| 1762 | // GIVEN |
| 1763 | MyQNode root; |
| 1764 | |
| 1765 | // THEN |
| 1766 | QVERIFY(root.attributes().size() == 0); |
| 1767 | |
| 1768 | { |
| 1769 | // WHEN |
| 1770 | MyQNode node1; |
| 1771 | root.addAttribute(&node1); |
| 1772 | |
| 1773 | // THEN |
| 1774 | QVERIFY(root.attributes().size() == 1); |
| 1775 | |
| 1776 | // WHEN |
| 1777 | { |
| 1778 | MyQNode node2; |
| 1779 | root.addAttribute(&node2); |
| 1780 | |
| 1781 | // THEN |
| 1782 | QVERIFY(root.attributes().size() == 2); |
| 1783 | QCOMPARE(root.attributes().first(), &node1); |
| 1784 | QCOMPARE(root.attributes().last(), &node2); |
| 1785 | } |
| 1786 | |
| 1787 | // THEN |
| 1788 | QVERIFY(root.attributes().size() == 1); |
| 1789 | QCOMPARE(root.attributes().first(), &node1); |
| 1790 | } |
| 1791 | |
| 1792 | // THEN |
| 1793 | QVERIFY(root.attributes().size() == 0); |
| 1794 | } |
| 1795 | |
| 1796 | void tst_Nodes::checkBookkeeping_stdvector_OfNode() |
| 1797 | { |
| 1798 | // GIVEN |
| 1799 | MyQNodeVec root; |
| 1800 | |
| 1801 | // THEN |
| 1802 | QVERIFY(root.attributes().size() == 0U); |
| 1803 | |
| 1804 | { |
| 1805 | // WHEN |
| 1806 | MyQNode node1; |
| 1807 | root.addAttribute(&node1); |
| 1808 | |
| 1809 | // THEN |
| 1810 | QVERIFY(root.attributes().size() == 1U); |
| 1811 | |
| 1812 | // WHEN |
| 1813 | { |
| 1814 | MyQNode node2; |
| 1815 | root.addAttribute(&node2); |
| 1816 | |
| 1817 | // THEN |
| 1818 | QVERIFY(root.attributes().size() == 2U); |
| 1819 | QCOMPARE(root.attributes().front(), &node1); |
| 1820 | QCOMPARE(root.attributes().back(), &node2); |
| 1821 | } |
| 1822 | |
| 1823 | // THEN |
| 1824 | QVERIFY(root.attributes().size() == 1U); |
| 1825 | QCOMPARE(root.attributes().front(), &node1); |
| 1826 | } |
| 1827 | |
| 1828 | // THEN |
| 1829 | QVERIFY(root.attributes().size() == 0U); |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | QTEST_MAIN(tst_Nodes) |
| 1834 | |
| 1835 | #include "tst_nodes.moc" |