diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2015-05-13 16:02:33 +0200 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2015-06-11 19:27:53 +0000 |
commit | 1d2818870034eacb1b0b6de3df279284d3ad24c3 (patch) | |
tree | f5c8be7b5e9a6ac8af9e98adf5e35b66452f4bd1 /tests/auto/core/nodes/tst_nodes.cpp | |
parent | af5079012292ea13586bc72605c30321a7c72662 (diff) |
Make tst_nodes easier to read and updated node destruction test
Change-Id: Iefa1bef6449453950c230fa436f39adc74834b98
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests/auto/core/nodes/tst_nodes.cpp')
-rw-r--r-- | tests/auto/core/nodes/tst_nodes.cpp | 145 |
1 files changed, 121 insertions, 24 deletions
diff --git a/tests/auto/core/nodes/tst_nodes.cpp b/tests/auto/core/nodes/tst_nodes.cpp index 693488712..02268552c 100644 --- a/tests/auto/core/nodes/tst_nodes.cpp +++ b/tests/auto/core/nodes/tst_nodes.cpp @@ -34,7 +34,7 @@ ** ****************************************************************************/ -#include <QtTest/QtTest> +#include <QtTest/QTest> #include <Qt3DCore/qnode.h> #include <Qt3DCore/qentity.h> #include <Qt3DCore/qcomponent.h> @@ -72,8 +72,11 @@ public: { public: ChangeRecord(const Qt3D::QSceneChangePtr &event, bool locked) - : QPair<Qt3D::QSceneChangePtr, bool>(event, locked) {} + : QPair<Qt3D::QSceneChangePtr, bool>(event, locked) + {} + Qt3D::QSceneChangePtr change() const { return first; } + bool wasLocked() const { return second; } }; @@ -143,50 +146,61 @@ protected: void tst_Nodes::defaultNodeConstruction() { - MyQNode *node = new MyQNode(); + // GIVEN + QScopedPointer<MyQNode> node(new MyQNode()); + // THEN QVERIFY(node != Q_NULLPTR); QVERIFY(node->children().isEmpty()); - MyQNode *node2 = new MyQNode(node); - QVERIFY(node2->parent() == node); + + // GIVEN + MyQNode *node2 = new MyQNode(node.data()); + + // THEN + QVERIFY(node2->parent() == node.data()); QVERIFY(!node->children().isEmpty()); QVERIFY(node2->children().isEmpty()); - - delete node; } void tst_Nodes::defaultComponentConstruction() { - MyQComponent *comp = new MyQComponent(); - MyQComponent *comp2 = new MyQComponent(comp); + // GIVEN + QScopedPointer<MyQComponent> comp(new MyQComponent()); + MyQComponent *comp2 = new MyQComponent(comp.data()); + // THEN QVERIFY(comp != Q_NULLPTR); - QCOMPARE(comp2->parent(), comp); - - delete comp; + QCOMPARE(comp2->parent(), comp.data()); } void tst_Nodes::defaultEntityConstrution() { - Qt3D::QEntity *entity = new Qt3D::QEntity(); - Qt3D::QEntity *entity2 = new Qt3D::QEntity(entity); + // GIVEN + QScopedPointer<Qt3D::QEntity> entity(new Qt3D::QEntity()); + Qt3D::QEntity *entity2 = new Qt3D::QEntity(entity.data()); + // THEN QVERIFY(entity->components().isEmpty()); QVERIFY(entity2->components().isEmpty()); - QCOMPARE(entity2->parent(), entity); - - delete entity; + QCOMPARE(entity2->parent(), entity.data()); } void tst_Nodes::appendChildNodesToNode() { + // GIVEN MyQNode *node = new MyQNode(); ObserverSpy spy(node); - for (int i = 0; i < 10; i++) { + // WHEN + for (int i = 0; i < 10; ++i) { + // GIVEN MyQNode *child = new MyQNode(); + // THEN QVERIFY(child->parent() == Q_NULLPTR); + + // WHEN child->setParent(node); + // THEN QVERIFY(child->parent() == node); QVERIFY(child->parentNode() == node); QCOMPARE(spy.events.size(), 1); @@ -198,10 +212,16 @@ void tst_Nodes::appendChildNodesToNode() QCOMPARE(clone->id(), child->id()); QCOMPARE(clone->parentNode()->id(), node->id()); } + // THEN QVERIFY(node->children().count() == 10); - for (int i = 0; i < 10; i++) { + + // WHEN + for (int i = 0; i < 10; ++i) { + // GIVEN MyQNode *child = new MyQNode(); + // WHEN child->setParent(node); + // THEN QVERIFY(child->parent() == node); QVERIFY(child->parentNode() == node); QCOMPARE(spy.events.size(), 1); @@ -213,69 +233,105 @@ void tst_Nodes::appendChildNodesToNode() QCOMPARE(clone->id(), child->id()); QCOMPARE(clone->parentNode()->id(), node->id()); } + // THEN QVERIFY(node->children().count() == 20); Qt3D::QNode *child = qobject_cast<Qt3D::QNode *>(node->children().first()); QScopedPointer<ObserverSpy> childSpy(new ObserverSpy(child)); + // WHEN Qt3D::QNode *parent = node; for (int i = 0; i < 10; i++) { + // THEN QVERIFY(child->parent() == parent); QVERIFY(child->parentNode() == parent); + // WHEN (void) new MyQNode(child); parent = child; child = qobject_cast<Qt3D::QNode *>(child->children().first()); - + // THEN QVERIFY(childSpy->events.isEmpty()); childSpy.reset(new ObserverSpy(child)); } + // THEN QVERIFY(node->children().count() == 20); + + // WHEN child = qobject_cast<Qt3D::QNode *>(node->children().first()); parent = node; for (int i = 0; i < 10; i++) { + // THEN QVERIFY(child->parent() == parent); QVERIFY(child->parentNode() == parent); QVERIFY(child->children().count() == 1); + // WHEN parent = child; child = qobject_cast<Qt3D::QNode *>(child->children().first()); } + // THEN QVERIFY(child->children().count() == 0); } void tst_Nodes::removingChildNodesFromNode() { + // GIVEN MyQNode *root = new MyQNode(); Qt3D::QNode *child = new MyQNode(); + // WHEN child->setParent(root); + // THEN QVERIFY(root->children().count() == 1); + + // WHEN child->setParent(Q_NULLPTR); + // THEN QVERIFY(root->children().count() == 0); QVERIFY(child->parent() == Q_NULLPTR); + // GIVEN for (int i = 0; i < 10; i++) { + // WHEN (void) new MyQNode(root); } + + // THEN QVERIFY(root->children().count() == 10); + + // WHEN Q_FOREACH (QObject *c, root->children()) c->setParent(Q_NULLPTR); + + // THEN QVERIFY(root->children().count() == 0); + // GIVEN Qt3D::QNode *firstChild = child; for (int i = 0; i < 10; i++) { + // WHEN (void) new MyQNode(child); child = qobject_cast<Qt3D::QNode *>(child->children().first()); } + + // THEN QVERIFY(root->children().count() == 0); + + // WHEN firstChild->setParent(root); + // THEN QVERIFY(root->children().count() == 1); + // GIVEN Qt3D::QNode *parent = child->parentNode(); + // WHEN for (int i = 0; i < 10; i++) { + // THEN QVERIFY(parent->children().count() == 1); QVERIFY(child->parentNode() == parent); + // WHEN ObserverSpy spy(parent); child->setParent(Q_NULLPTR); + // THEN QVERIFY(child->parent() == Q_NULLPTR); QVERIFY(parent->children().count() == 0); @@ -288,9 +344,11 @@ void tst_Nodes::removingChildNodesFromNode() QCOMPARE(clone->id(), child->id()); QVERIFY(!clone->parentNode()); + // WHEN child = parent; parent = parent->parentNode(); } + // THEN QVERIFY(parent == root); QVERIFY(parent->children().first() == firstChild); QVERIFY(firstChild->children().isEmpty()); @@ -298,10 +356,13 @@ void tst_Nodes::removingChildNodesFromNode() void tst_Nodes::appendingChildEntitiesToNode() { + // GIVEN MyQNode *root = new MyQNode(); + // WHEN Qt3D::QEntity *childEntity = new Qt3D::QEntity(root); + // THEN QVERIFY(root->children().first() == childEntity); QVERIFY(childEntity->parentEntity() == Q_NULLPTR); QVERIFY(childEntity->parentNode() == root); @@ -309,16 +370,21 @@ void tst_Nodes::appendingChildEntitiesToNode() void tst_Nodes::removingChildEntitiesFromNode() { + // GIVEN MyQNode *root = new MyQNode(); + // WHEN Qt3D::QEntity *childEntity = new Qt3D::QEntity(root); + // THEN QVERIFY(root->children().first() == childEntity); QVERIFY(childEntity->parentEntity() == Q_NULLPTR); QVERIFY(childEntity->parentNode() == root); + // WHEN childEntity->setParent(Q_NULLPTR); + // THEN QVERIFY(root->children().isEmpty()); QVERIFY(childEntity->parentNode() == Q_NULLPTR); QVERIFY(childEntity->parent() == Q_NULLPTR); @@ -326,6 +392,7 @@ void tst_Nodes::removingChildEntitiesFromNode() void tst_Nodes::appendingComponentsToEntity() { + // GIVEN MyQNode *root = new MyQNode(); Qt3D::QEntity *entity = new Qt3D::QEntity(root); @@ -335,6 +402,7 @@ void tst_Nodes::appendingComponentsToEntity() MyQComponent *comp2 = new MyQComponent(entity); MyQComponent *comp3 = new MyQComponent(); + // THEN QVERIFY(entity->parentNode() == root); QVERIFY(root->children().count() == 2); QVERIFY(entity->children().count() == 1); @@ -343,7 +411,9 @@ void tst_Nodes::appendingComponentsToEntity() ObserverSpy spy(entity); + // WHEN entity->addComponent(comp1); + // THEN QVERIFY(entity->components().count() == 1); QVERIFY(entity->components().first() == comp1); QVERIFY(comp1->parentNode() == root); @@ -358,7 +428,9 @@ void tst_Nodes::appendingComponentsToEntity() QCOMPARE(clone->id(), comp1->id()); QVERIFY(!clone->parentNode()); + // WHEN entity->addComponent(comp2); + // THEN QVERIFY(entity->components().count() == 2); QVERIFY(entity->components().first() == comp1); QVERIFY(entity->components().last() == comp2); @@ -374,7 +446,9 @@ void tst_Nodes::appendingComponentsToEntity() QCOMPARE(clone->id(), comp2->id()); QVERIFY(!clone->parentNode()); + // WHEN entity->addComponent(comp3); + // THEN QVERIFY(entity->components().count() == 3); QVERIFY(entity->components().first() == comp1); QVERIFY(entity->components().last() == comp3); @@ -404,6 +478,7 @@ void tst_Nodes::appendingComponentsToEntity() void tst_Nodes::removingComponentsFromEntity() { + // GIVEN MyQNode *root = new MyQNode(); Qt3D::QEntity *entity = new Qt3D::QEntity(root); @@ -411,16 +486,20 @@ void tst_Nodes::removingComponentsFromEntity() MyQComponent *comp2 = new MyQComponent(entity); MyQComponent *comp3 = new MyQComponent(); - + // WHEN entity->addComponent(comp1); entity->addComponent(comp2); entity->addComponent(comp3); + + // THEN QVERIFY(entity->components().count() == 3); QCOMPARE(entity->children().count(), 2); ObserverSpy spy(entity); - + // WHEN entity->removeComponent(comp2); + + // THEN QVERIFY(entity->components().count() == 2); QVERIFY(comp2->parent() == entity); QVERIFY(entity->children().count() == 2); @@ -432,7 +511,9 @@ void tst_Nodes::removingComponentsFromEntity() Qt3D::QNodeId nodeId = event->value().value<Qt3D::QNodeId>(); QCOMPARE(nodeId, comp2->id()); + // WHEN entity->removeComponent(comp1); + // THEN QVERIFY(entity->components().count() == 1); QVERIFY(comp1->parentNode() == root); QCOMPARE(spy.events.size(), 1); @@ -443,7 +524,9 @@ void tst_Nodes::removingComponentsFromEntity() nodeId = event->value().value<Qt3D::QNodeId>(); QCOMPARE(nodeId, comp1->id()); + // WHEN entity->removeComponent(comp3); + // THEN QVERIFY(entity->components().count() == 0); QVERIFY(comp3->parentNode() == entity); QCOMPARE(spy.events.size(), 1); @@ -454,23 +537,30 @@ void tst_Nodes::removingComponentsFromEntity() nodeId = event->value().value<Qt3D::QNodeId>(); QCOMPARE(nodeId, comp3->id()); + // WHEN entity->addComponent(comp1); entity->addComponent(comp2); entity->addComponent(comp3); + // THEN QVERIFY(entity->components().count() == 3); QCOMPARE(entity->children().count(), 2); + // WHEN Q_FOREACH (QObject *c, entity->children()) c->setParent(Q_NULLPTR); + // THEN QCOMPARE(entity->components().count(), 3); QCOMPARE(entity->children().count(), 0); } void tst_Nodes::changeCustomProperty() { + // GIVEN MyQNode *node = new MyQNode(); ObserverSpy spy(node); + // WHEN node->setCustomProperty(QStringLiteral("foo")); + // THEN QCOMPARE(spy.events.size(), 1); QVERIFY(spy.events.first().wasLocked()); Qt3D::QScenePropertyChangePtr event = spy.events.takeFirst().change().dynamicCast<Qt3D::QScenePropertyChange>(); @@ -481,6 +571,7 @@ void tst_Nodes::changeCustomProperty() void tst_Nodes::checkDestruction() { + // GIVEN MyQNode *root = new MyQNode(); Qt3D::QEntity *entity = new Qt3D::QEntity(root); @@ -488,12 +579,18 @@ void tst_Nodes::checkDestruction() MyQComponent *comp2 = new MyQComponent(); MyQComponent *comp3 = new MyQComponent(); - entity->addComponent(comp1); entity->addComponent(comp2); entity->addComponent(comp3); - delete root; + // THEN + QVERIFY(!root->children().isEmpty()); + + // WHEN + delete entity; + + // THEN + QVERIFY(root->children().isEmpty()); } QTEST_GUILESS_MAIN(tst_Nodes) |