| 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/private/qscene_p.h> |
| 6 | #include <Qt3DCore/qnode.h> |
| 7 | #include <Qt3DCore/qentity.h> |
| 8 | #include <Qt3DCore/qcomponent.h> |
| 9 | #include <private/qnode_p.h> |
| 10 | #include <testarbiter.h> |
| 11 | |
| 12 | class tst_QScene : public QObject |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | public: |
| 16 | tst_QScene() : QObject() {} |
| 17 | ~tst_QScene() {} |
| 18 | |
| 19 | private slots: |
| 20 | void addNodeObservable(); |
| 21 | void removeNodeObservable(); |
| 22 | void addChildNode(); |
| 23 | void deleteChildNode(); |
| 24 | void removeChildNode(); |
| 25 | void addEntityForComponent(); |
| 26 | void removeEntityForComponent(); |
| 27 | void hasEntityForComponent(); |
| 28 | }; |
| 29 | |
| 30 | class tst_Node : public Qt3DCore::QNode |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | public: |
| 34 | tst_Node() : Qt3DCore::QNode() |
| 35 | {} |
| 36 | }; |
| 37 | |
| 38 | class tst_Component : public Qt3DCore::QComponent |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | public: |
| 42 | tst_Component() : Qt3DCore::QComponent() |
| 43 | {} |
| 44 | }; |
| 45 | |
| 46 | void tst_QScene::addNodeObservable() |
| 47 | { |
| 48 | // GIBEN |
| 49 | QList<Qt3DCore::QNode *> nodes; |
| 50 | |
| 51 | for (int i = 0; i < 10; i++) |
| 52 | nodes.append(new tst_Node()); |
| 53 | |
| 54 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 55 | scene->setArbiter(new TestArbiter); |
| 56 | |
| 57 | // WHEN |
| 58 | for (int i = 0; i < 10; i++) |
| 59 | scene->addObservable(nodes.at(i)); |
| 60 | |
| 61 | // THEN |
| 62 | for (Qt3DCore::QNode *n : std::as_const(nodes)) { |
| 63 | QVERIFY(n == scene->lookupNode(n->id())); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void tst_QScene::removeNodeObservable() |
| 68 | { |
| 69 | // GIVEN |
| 70 | Qt3DCore::QNode *node1 = new tst_Node(); |
| 71 | Qt3DCore::QNode *node2 = new tst_Node(); |
| 72 | |
| 73 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 74 | scene->setArbiter(new TestArbiter); |
| 75 | |
| 76 | // WHEN |
| 77 | scene->addObservable(node1); |
| 78 | scene->addObservable(node2); |
| 79 | |
| 80 | // WHEN |
| 81 | scene->removeObservable(node1); |
| 82 | |
| 83 | // THEN |
| 84 | QVERIFY(scene->lookupNode(node1->id()) == nullptr); |
| 85 | QVERIFY(scene->lookupNode(node2->id()) == node2); |
| 86 | } |
| 87 | |
| 88 | void tst_QScene::addChildNode() |
| 89 | { |
| 90 | // GIVEN |
| 91 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 92 | |
| 93 | QList<Qt3DCore::QNode *> nodes; |
| 94 | |
| 95 | Qt3DCore::QNode *root = new tst_Node(); |
| 96 | Qt3DCore::QNodePrivate::get(root)->setScene(scene); |
| 97 | |
| 98 | // WHEN |
| 99 | scene->addObservable(root); |
| 100 | // THEN |
| 101 | QVERIFY(scene->lookupNode(root->id()) == root); |
| 102 | |
| 103 | // WHEN |
| 104 | for (int i = 0; i < 10; i++) { |
| 105 | Qt3DCore::QNode *child = new tst_Node(); |
| 106 | if (nodes.isEmpty()) |
| 107 | child->setParent(root); |
| 108 | else |
| 109 | child->setParent(nodes.last()); |
| 110 | nodes.append(child); |
| 111 | } |
| 112 | QCoreApplication::processEvents(); |
| 113 | |
| 114 | // THEN |
| 115 | for (Qt3DCore::QNode *n : std::as_const(nodes)) { |
| 116 | QVERIFY(scene->lookupNode(n->id()) == n); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void tst_QScene::deleteChildNode() |
| 121 | { |
| 122 | // GIVEN |
| 123 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 124 | |
| 125 | QList<Qt3DCore::QNode *> nodes1, nodes2; |
| 126 | |
| 127 | Qt3DCore::QNode *root1 = new tst_Node(); |
| 128 | Qt3DCore::QNode *root2 = new tst_Node(); |
| 129 | Qt3DCore::QNodePrivate::get(root1)->setScene(scene); |
| 130 | Qt3DCore::QNodePrivate::get(root2)->setScene(scene); |
| 131 | Qt3DCore::QNodePrivate::get(root1)->m_hasBackendNode = true; |
| 132 | Qt3DCore::QNodePrivate::get(root2)->m_hasBackendNode = true; |
| 133 | |
| 134 | // WHEN |
| 135 | scene->addObservable(root1); |
| 136 | scene->addObservable(root2); |
| 137 | // THEN |
| 138 | QVERIFY(scene->lookupNode(root1->id()) == root1); |
| 139 | QVERIFY(scene->lookupNode(root2->id()) == root2); |
| 140 | |
| 141 | // WHEN |
| 142 | for (int i = 0; i < 10; i++) { |
| 143 | Qt3DCore::QNode *child1 = new tst_Node(); |
| 144 | child1->setParent(nodes1.isEmpty() ? root1 : nodes1.last()); |
| 145 | Qt3DCore::QNodePrivate::get(child1)->m_hasBackendNode = true; |
| 146 | nodes1.append(child1); |
| 147 | |
| 148 | Qt3DCore::QNode *child2 = new tst_Node(); |
| 149 | child2->setParent(nodes2.isEmpty() ? root2 : nodes2.last()); |
| 150 | Qt3DCore::QNodePrivate::get(child2)->m_hasBackendNode = true; |
| 151 | nodes2.append(child2); |
| 152 | } |
| 153 | QCoreApplication::processEvents(); |
| 154 | |
| 155 | // THEN |
| 156 | for (Qt3DCore::QNode *n : std::as_const(nodes1)) { |
| 157 | QVERIFY(scene->lookupNode(n->id()) == n); |
| 158 | } |
| 159 | for (Qt3DCore::QNode *n : std::as_const(nodes2)) { |
| 160 | QVERIFY(scene->lookupNode(n->id()) == n); |
| 161 | } |
| 162 | |
| 163 | // gather node IDs |
| 164 | Qt3DCore::QNodeIdVector root1ChildIds; |
| 165 | for (Qt3DCore::QNode *n : std::as_const(nodes1)) |
| 166 | root1ChildIds << n->id(); |
| 167 | |
| 168 | // WHEN |
| 169 | delete root1; |
| 170 | QCoreApplication::processEvents(); |
| 171 | |
| 172 | // THEN |
| 173 | for (Qt3DCore::QNodeId id : std::as_const(root1ChildIds)) { |
| 174 | QVERIFY(scene->lookupNode(id) == nullptr); |
| 175 | } |
| 176 | |
| 177 | // WHEN |
| 178 | nodes2.first()->setParent(static_cast<Qt3DCore::QNode*>(nullptr)); |
| 179 | QCoreApplication::processEvents(); |
| 180 | |
| 181 | // THEN |
| 182 | for (Qt3DCore::QNode *n : std::as_const(nodes2)) { |
| 183 | QVERIFY(scene->lookupNode(n->id()) == nullptr); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void tst_QScene::removeChildNode() |
| 188 | { |
| 189 | // GIVEN |
| 190 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 191 | |
| 192 | QList<Qt3DCore::QNode *> nodes; |
| 193 | |
| 194 | Qt3DCore::QNode *root = new tst_Node; |
| 195 | Qt3DCore::QNodePrivate::get(root)->setScene(scene); |
| 196 | Qt3DCore::QNodePrivate::get(root)->m_hasBackendNode = true; |
| 197 | scene->addObservable(root); |
| 198 | |
| 199 | // WHEN |
| 200 | for (int i = 0; i < 10; i++) { |
| 201 | Qt3DCore::QNode *child = new tst_Node; |
| 202 | if (nodes.isEmpty()) |
| 203 | child->setParent(root); |
| 204 | else |
| 205 | child->setParent(nodes.last()); |
| 206 | Qt3DCore::QNodePrivate::get(child)->m_hasBackendNode = true; |
| 207 | nodes.append(child); |
| 208 | } |
| 209 | |
| 210 | // THEN |
| 211 | while (!nodes.isEmpty()) { |
| 212 | Qt3DCore::QNode *lst = nodes.takeLast(); |
| 213 | QVERIFY(scene->lookupNode(lst->id()) == lst); |
| 214 | if (lst->parentNode() != nullptr) { |
| 215 | lst->setParent(Q_NODE_NULLPTR); |
| 216 | QCoreApplication::processEvents(); |
| 217 | QVERIFY(scene->lookupNode(lst->id()) == nullptr); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void tst_QScene::addEntityForComponent() |
| 223 | { |
| 224 | // GIVEN |
| 225 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 226 | |
| 227 | QList<Qt3DCore::QEntity *> entities; |
| 228 | QList<Qt3DCore::QComponent *> components; |
| 229 | |
| 230 | for (int i = 0; i < 10; i++) { |
| 231 | Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(); |
| 232 | Qt3DCore::QComponent *comp = new tst_Component(); |
| 233 | |
| 234 | Qt3DCore::QNodePrivate::get(entity)->setScene(scene); |
| 235 | Qt3DCore::QNodePrivate::get(comp)->setScene(scene); |
| 236 | entities << entity; |
| 237 | components << comp; |
| 238 | } |
| 239 | |
| 240 | // WHEN |
| 241 | for (int i = 0; i < 10; i++) { |
| 242 | Qt3DCore::QEntity *e = entities.at(i); |
| 243 | for (int j = 0; j < 10; j++) { |
| 244 | e->addComponent(components.at(j)); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // THEN |
| 249 | for (int i = 0; i < 10; i++) { |
| 250 | const QList<Qt3DCore::QNodeId> ids = scene->entitiesForComponent(components.at(i)->id()); |
| 251 | QCOMPARE(ids.size(), 10); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void tst_QScene::removeEntityForComponent() |
| 256 | { |
| 257 | // GIVEN |
| 258 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 259 | |
| 260 | QList<Qt3DCore::QEntity *> entities; |
| 261 | QList<Qt3DCore::QComponent *> components; |
| 262 | |
| 263 | for (int i = 0; i < 10; i++) { |
| 264 | Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(); |
| 265 | Qt3DCore::QComponent *comp = new tst_Component(); |
| 266 | |
| 267 | Qt3DCore::QNodePrivate::get(entity)->setScene(scene); |
| 268 | Qt3DCore::QNodePrivate::get(comp)->setScene(scene); |
| 269 | entities << entity; |
| 270 | components << comp; |
| 271 | } |
| 272 | |
| 273 | // WHEN |
| 274 | for (int i = 0; i < 10; i++) { |
| 275 | Qt3DCore::QEntity *e = entities.at(i); |
| 276 | for (int j = 0; j < 10; j++) { |
| 277 | e->addComponent(components.at(j)); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // THEN |
| 282 | for (int i = 0; i < 10; i++) { |
| 283 | Qt3DCore::QEntity *e = entities.at(i); |
| 284 | for (int j = 0; j < 10; j++) { |
| 285 | e->removeComponent(components.at(j)); |
| 286 | QCOMPARE(scene->entitiesForComponent(components.at(j)->id()).size(), 10 - (i + 1)); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void tst_QScene::hasEntityForComponent() |
| 292 | { |
| 293 | // GIVEN |
| 294 | Qt3DCore::QScene *scene = new Qt3DCore::QScene; |
| 295 | |
| 296 | QList<Qt3DCore::QEntity *> entities; |
| 297 | QList<Qt3DCore::QComponent *> components; |
| 298 | |
| 299 | for (int i = 0; i < 10; i++) { |
| 300 | Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(); |
| 301 | Qt3DCore::QComponent *comp = new tst_Component(); |
| 302 | |
| 303 | Qt3DCore::QNodePrivate::get(entity)->setScene(scene); |
| 304 | Qt3DCore::QNodePrivate::get(comp)->setScene(scene); |
| 305 | entities << entity; |
| 306 | components << comp; |
| 307 | } |
| 308 | |
| 309 | // WHEN |
| 310 | for (int i = 0; i < 10; i++) { |
| 311 | Qt3DCore::QEntity *e = entities.at(i); |
| 312 | for (int j = 0; j < 10; j++) { |
| 313 | e->addComponent(components.at(j)); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // THEN |
| 318 | for (int i = 0; i < 10; i++) |
| 319 | QVERIFY(scene->hasEntityForComponent(components.at(i)->id(), entities.at(i)->id())); |
| 320 | } |
| 321 | |
| 322 | QTEST_MAIN(tst_QScene) |
| 323 | |
| 324 | #include "tst_qscene.moc" |