| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest/QTest> |
| 30 | #include <Qt3DCore/private/qnode_p.h> |
| 31 | #include <Qt3DCore/private/qscene_p.h> |
| 32 | #include <Qt3DCore/qentity.h> |
| 33 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
| 34 | |
| 35 | #include <Qt3DRender/qframegraphnode.h> |
| 36 | #include <Qt3DRender/private/qframegraphnode_p.h> |
| 37 | #include <Qt3DRender/qframegraphnodecreatedchange.h> |
| 38 | #include <Qt3DRender/private/qframegraphnodecreatedchange_p.h> |
| 39 | |
| 40 | #include "testpostmanarbiter.h" |
| 41 | |
| 42 | class MyFrameGraphNode : public Qt3DRender::QFrameGraphNode |
| 43 | { |
| 44 | Q_OBJECT |
| 45 | public: |
| 46 | explicit MyFrameGraphNode(Qt3DCore::QNode *parent = nullptr) |
| 47 | : QFrameGraphNode(parent) |
| 48 | { |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | class tst_QFrameGraphNode: public QObject |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | private Q_SLOTS: |
| 57 | |
| 58 | void checkSaneDefaults() |
| 59 | { |
| 60 | QScopedPointer<Qt3DRender::QFrameGraphNode> defaultFrameGraphNode(new MyFrameGraphNode); |
| 61 | |
| 62 | QVERIFY(defaultFrameGraphNode->isEnabled()); |
| 63 | QVERIFY(defaultFrameGraphNode->parentFrameGraphNode() == nullptr); |
| 64 | } |
| 65 | |
| 66 | void checkCloning_data() |
| 67 | { |
| 68 | QTest::addColumn<Qt3DRender::QFrameGraphNode *>(name: "frameGraphNode" ); |
| 69 | QTest::addColumn<QVector<Qt3DCore::QNodeId>>(name: "childFrameGraphNodeIds" ); |
| 70 | QTest::addColumn<bool>(name: "enabled" ); |
| 71 | QTest::addColumn<int>(name: "creationChangeCount" ); |
| 72 | |
| 73 | QVector<Qt3DCore::QNodeId> noChildIds; |
| 74 | |
| 75 | { |
| 76 | Qt3DRender::QFrameGraphNode *defaultConstructed = new MyFrameGraphNode(); |
| 77 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed << noChildIds << true << 1; |
| 78 | } |
| 79 | |
| 80 | { |
| 81 | Qt3DRender::QFrameGraphNode *disabledFrameGraphNode = new MyFrameGraphNode(); |
| 82 | disabledFrameGraphNode->setEnabled(false); |
| 83 | QTest::newRow(dataTag: "allBuffers" ) << disabledFrameGraphNode << noChildIds << false << 1; |
| 84 | } |
| 85 | |
| 86 | { |
| 87 | Qt3DRender::QFrameGraphNode *nodeWithChildren = new MyFrameGraphNode(); |
| 88 | Qt3DRender::QFrameGraphNode *child1 = new MyFrameGraphNode(nodeWithChildren); |
| 89 | Qt3DRender::QFrameGraphNode *child2 = new MyFrameGraphNode(nodeWithChildren); |
| 90 | QVector<Qt3DCore::QNodeId> childIds = {child1->id(), child2->id()}; |
| 91 | QTest::newRow(dataTag: "nodeWithChildren" ) << nodeWithChildren << childIds << true << 3; |
| 92 | } |
| 93 | |
| 94 | { |
| 95 | Qt3DRender::QFrameGraphNode *nodeWithNestedChildren = new MyFrameGraphNode(); |
| 96 | Qt3DRender::QFrameGraphNode *child = new MyFrameGraphNode(nodeWithNestedChildren); |
| 97 | new Qt3DCore::QNode(nodeWithNestedChildren); |
| 98 | Qt3DRender::QFrameGraphNode *grandChild = new MyFrameGraphNode(nodeWithNestedChildren); |
| 99 | QVector<Qt3DCore::QNodeId> childIds = {child->id(), grandChild->id()}; |
| 100 | QTest::newRow(dataTag: "nodeWithNestedChildren" ) << nodeWithNestedChildren << childIds << true << 4; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void checkCloning() |
| 105 | { |
| 106 | // GIVEN |
| 107 | QFETCH(Qt3DRender::QFrameGraphNode *, frameGraphNode); |
| 108 | QFETCH(QVector<Qt3DCore::QNodeId>, childFrameGraphNodeIds); |
| 109 | QFETCH(bool, enabled); |
| 110 | QFETCH(int, creationChangeCount); |
| 111 | |
| 112 | // THEN |
| 113 | QCOMPARE(frameGraphNode->isEnabled(), enabled); |
| 114 | |
| 115 | // WHEN |
| 116 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(frameGraphNode); |
| 117 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
| 118 | |
| 119 | // THEN |
| 120 | QCOMPARE(creationChanges.size(), creationChangeCount); |
| 121 | const Qt3DCore::QNodeCreatedChangeBasePtr creationChangeData = creationChanges.first(); |
| 122 | |
| 123 | // THEN |
| 124 | QCOMPARE(frameGraphNode->id(), creationChangeData->subjectId()); |
| 125 | QCOMPARE(frameGraphNode->isEnabled(), creationChangeData->isNodeEnabled()); |
| 126 | QCOMPARE(frameGraphNode->metaObject(), creationChangeData->metaObject()); |
| 127 | |
| 128 | // THEN |
| 129 | Qt3DRender::QFrameGraphNodeCreatedChangeBasePtr frameGraphNodeCreatedChange = qSharedPointerCast<Qt3DRender::QFrameGraphNodeCreatedChangeBase>(src: creationChangeData); |
| 130 | Qt3DRender::QFrameGraphNodeCreatedChangeBasePrivate *creationChangeDataPrivate = Qt3DRender::QFrameGraphNodeCreatedChangeBasePrivate::get(change: frameGraphNodeCreatedChange.get()); |
| 131 | QCOMPARE(creationChangeDataPrivate->m_parentFrameGraphNodeId, frameGraphNode->parentNode() ? frameGraphNode->parentNode()->id() : Qt3DCore::QNodeId()); |
| 132 | QCOMPARE(creationChangeDataPrivate->m_childFrameGraphNodeIds, childFrameGraphNodeIds); |
| 133 | |
| 134 | delete frameGraphNode; |
| 135 | } |
| 136 | |
| 137 | void checkCreationData() |
| 138 | { |
| 139 | // GIVEN |
| 140 | Qt3DRender::QFrameGraphNode *parentFrameGraphNode = new MyFrameGraphNode(); |
| 141 | Qt3DRender::QFrameGraphNode *childFrameGraphNode = new MyFrameGraphNode(parentFrameGraphNode); |
| 142 | |
| 143 | // WHEN |
| 144 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
| 145 | |
| 146 | { |
| 147 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(parentFrameGraphNode); |
| 148 | creationChanges = creationChangeGenerator.creationChanges(); |
| 149 | } |
| 150 | |
| 151 | { |
| 152 | // THEN |
| 153 | QCOMPARE(creationChanges.size(), 2); |
| 154 | { |
| 155 | |
| 156 | const auto creationChangeData = qSharedPointerCast<Qt3DRender::QFrameGraphNodeCreatedChangeBase>(src: creationChanges.first()); |
| 157 | QCOMPARE(parentFrameGraphNode->isEnabled(), creationChangeData->isNodeEnabled()); |
| 158 | QCOMPARE(parentFrameGraphNode->metaObject(), creationChangeData->metaObject()); |
| 159 | QCOMPARE(Qt3DCore::qIdForNode(parentFrameGraphNode->parentFrameGraphNode()), creationChangeData->parentFrameGraphNodeId()); |
| 160 | } |
| 161 | // THEN |
| 162 | { |
| 163 | const auto creationChangeData = qSharedPointerCast<Qt3DRender::QFrameGraphNodeCreatedChangeBase>(src: creationChanges.last()); |
| 164 | QCOMPARE(childFrameGraphNode->isEnabled(), creationChangeData->isNodeEnabled()); |
| 165 | QCOMPARE(childFrameGraphNode->metaObject(), creationChangeData->metaObject()); |
| 166 | QCOMPARE(Qt3DCore::qIdForNode(childFrameGraphNode->parentFrameGraphNode()), parentFrameGraphNode->id()); |
| 167 | QCOMPARE(Qt3DCore::qIdForNode(childFrameGraphNode->parentFrameGraphNode()), creationChangeData->parentFrameGraphNodeId()); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void checkPropertyUpdates() |
| 173 | { |
| 174 | // GIVEN |
| 175 | TestArbiter arbiter; |
| 176 | QScopedPointer<Qt3DRender::QFrameGraphNode> frameGraphNode(new MyFrameGraphNode()); |
| 177 | arbiter.setArbiterOnNode(frameGraphNode.data()); |
| 178 | |
| 179 | // WHEN |
| 180 | frameGraphNode->setEnabled(false); |
| 181 | QCoreApplication::processEvents(); |
| 182 | |
| 183 | // THEN |
| 184 | QCOMPARE(arbiter.events.size(), 0); |
| 185 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 186 | QCOMPARE(arbiter.dirtyNodes.front(), frameGraphNode.data()); |
| 187 | |
| 188 | arbiter.dirtyNodes.clear(); |
| 189 | |
| 190 | // WHEN |
| 191 | frameGraphNode->setEnabled(false); |
| 192 | QCoreApplication::processEvents(); |
| 193 | |
| 194 | // THEN |
| 195 | QCOMPARE(arbiter.events.size(), 0); |
| 196 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 197 | |
| 198 | // WHEN |
| 199 | frameGraphNode->setEnabled(true); |
| 200 | QCoreApplication::processEvents(); |
| 201 | |
| 202 | // THEN |
| 203 | QCOMPARE(arbiter.events.size(), 0); |
| 204 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 205 | QCOMPARE(arbiter.dirtyNodes.front(), frameGraphNode.data()); |
| 206 | |
| 207 | arbiter.dirtyNodes.clear(); |
| 208 | } |
| 209 | |
| 210 | void checkParentFrameNodeRetrieval() |
| 211 | { |
| 212 | // GIVEN |
| 213 | QScopedPointer<Qt3DRender::QFrameGraphNode> root(new MyFrameGraphNode); |
| 214 | |
| 215 | Qt3DRender::QFrameGraphNode *child1 = new MyFrameGraphNode(root.data()); |
| 216 | Qt3DRender::QFrameGraphNode *child2 = new MyFrameGraphNode(root.data()); |
| 217 | Qt3DCore::QEntity *child3 = new Qt3DCore::QEntity(root.data()); |
| 218 | |
| 219 | Qt3DRender::QFrameGraphNode *child11 = new MyFrameGraphNode(child1); |
| 220 | Qt3DCore::QEntity *child21 = new Qt3DCore::QEntity(child2); |
| 221 | Qt3DRender::QFrameGraphNode *child31 = new MyFrameGraphNode(child3); |
| 222 | |
| 223 | Qt3DRender::QFrameGraphNode *child211 = new MyFrameGraphNode(child21); |
| 224 | |
| 225 | // WHEN |
| 226 | QCoreApplication::processEvents(); |
| 227 | |
| 228 | // THEN |
| 229 | QVERIFY(child1->parent() == root.data()); |
| 230 | QVERIFY(child1->parentFrameGraphNode() == root.data()); |
| 231 | |
| 232 | QVERIFY(child2->parent() == root.data()); |
| 233 | QVERIFY(child2->parentFrameGraphNode() == root.data()); |
| 234 | |
| 235 | QVERIFY(child3->parent() == root.data()); |
| 236 | |
| 237 | |
| 238 | QVERIFY(child11->parent() == child1); |
| 239 | QVERIFY(child11->parentFrameGraphNode() == child1); |
| 240 | |
| 241 | QVERIFY(child21->parent() == child2); |
| 242 | |
| 243 | QVERIFY(child31->parent() == child3); |
| 244 | QVERIFY(child31->parentFrameGraphNode() == root.data()); |
| 245 | |
| 246 | |
| 247 | QVERIFY(child211->parent() == child21); |
| 248 | QVERIFY(child211->parentFrameGraphNode() == child2); |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | QTEST_MAIN(tst_QFrameGraphNode) |
| 253 | |
| 254 | #include "tst_qframegraphnode.moc" |
| 255 | |