From e5b256effa320fb7806205f5a37a4dc1943175f0 Mon Sep 17 00:00:00 2001 From: Svenn-Arne Dragly Date: Thu, 21 Feb 2019 15:54:02 +0100 Subject: Fix broken creation order for nodes used as properties Otherwise, child nodes may be constructed before their parents and their node creation changes will arrive at the backend out-of-order. This could result in a child node referencing a parent that we have not yet received a creation change for. Also add a unit test to take this case into account. Change-Id: I26b29e63863d1686e7b9239c63297c7e6c341f4e Task-number: QTBUG-73986 Reviewed-by: Paul Lemire --- tests/auto/core/nodes/tst_nodes.cpp | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'tests') diff --git a/tests/auto/core/nodes/tst_nodes.cpp b/tests/auto/core/nodes/tst_nodes.cpp index 75d7a7799..accd20ceb 100644 --- a/tests/auto/core/nodes/tst_nodes.cpp +++ b/tests/auto/core/nodes/tst_nodes.cpp @@ -82,6 +82,7 @@ private slots: void checkConstructionSetParentMix(); // QTBUG-60612 void checkConstructionWithParent(); + void checkConstructionWithNonRootParent(); // QTBUG-73986 void checkConstructionAsListElement(); void checkSceneIsSetOnConstructionWithParent(); // QTBUG-69352 @@ -1119,6 +1120,61 @@ void tst_Nodes::checkConstructionWithParent() QCOMPARE(propertyEvent->value().value(), node->id()); } +void tst_Nodes::checkConstructionWithNonRootParent() +{ + // GIVEN + ObserverSpy spy; + Qt3DCore::QScene scene; + QScopedPointer root(new MyQNode()); + + // WHEN + root->setArbiterAndScene(&spy, &scene); + root->setSimulateBackendCreated(true); + QScopedPointer parent(new MyQNode(root.data())); + + // THEN + QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); + QVERIFY(Qt3DCore::QNodePrivate::get(parent.data())->scene() != nullptr); + + // WHEN we create a child and then set it as a Node* property + auto *child = new MyQNode(parent.data()); + root->setNodeProperty(child); + + // THEN we should get + // - one creation change for parent, + // - one creation change for child, + // - one child added change for root->parent, + // - and one property change event, + // in that order. + QCoreApplication::processEvents(); + QCOMPARE(root->children().count(), 1); + QCOMPARE(parent->children().count(), 1); + + QCOMPARE(spy.events.size(), 4); // 2 creation changes, 1 child added changes, 1 property change + + // Ensure first event is parent node's creation change + const auto parentCreationEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!parentCreationEvent.isNull()); + QCOMPARE(parentCreationEvent->subjectId(), parent->id()); + + const auto childCreationEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!childCreationEvent.isNull()); + QCOMPARE(childCreationEvent->subjectId(), child->id()); + + const auto parentNewChildEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!parentNewChildEvent.isNull()); + QCOMPARE(parentNewChildEvent->subjectId(), root->id()); + QCOMPARE(parentNewChildEvent->propertyName(), "children"); + QCOMPARE(parentNewChildEvent->addedNodeId(), parent->id()); + + // Ensure second and last event is property set change + const auto propertyEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!propertyEvent.isNull()); + QCOMPARE(propertyEvent->subjectId(), root->id()); + QCOMPARE(propertyEvent->propertyName(), "nodeProperty"); + QCOMPARE(propertyEvent->value().value(), child->id()); +} + void tst_Nodes::checkConstructionAsListElement() { // GIVEN -- cgit v1.2.3