| 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/qentity.h> |
| 6 | #include <Qt3DCore/private/qentity_p.h> |
| 7 | #include <Qt3DCore/qcomponent.h> |
| 8 | #include <QtCore/qscopedpointer.h> |
| 9 | |
| 10 | using namespace Qt3DCore; |
| 11 | |
| 12 | class tst_Entity : public QObject |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | public: |
| 16 | tst_Entity() : QObject() |
| 17 | { |
| 18 | qRegisterMetaType<Qt3DCore::QNode*>(); |
| 19 | } |
| 20 | ~tst_Entity() {} |
| 21 | |
| 22 | private slots: |
| 23 | void constructionDestruction(); |
| 24 | |
| 25 | void addComponentSingleParentSingleAggregation(); |
| 26 | void addComponentSingleParentSeveralAggregations(); |
| 27 | void addComponentsSeveralParentsSingleAggregations(); |
| 28 | void addComponentsSeveralParentsSeveralAggregations(); |
| 29 | |
| 30 | void retrieveSingleComponent(); |
| 31 | |
| 32 | void removeComponentSingleParentSingleAggregation(); |
| 33 | void removeComponentSingleParentSeveralAggregations(); |
| 34 | void removeComponentsSeveralParentsSingleAggreation(); |
| 35 | void removeComponentsSeveralParentsSeveralAggregations(); |
| 36 | |
| 37 | void addSeveralTimesSameComponent(); |
| 38 | void removeSeveralTimesSameComponent(); |
| 39 | |
| 40 | void checkComponentBookkeeping(); |
| 41 | }; |
| 42 | |
| 43 | class MyQComponent : public Qt3DCore::QComponent |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | public: |
| 47 | explicit MyQComponent(Qt3DCore::QNode *parent = nullptr) |
| 48 | : QComponent(parent) |
| 49 | {} |
| 50 | }; |
| 51 | |
| 52 | class MyQ2Component : public Qt3DCore::QComponent |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | public: |
| 56 | explicit MyQ2Component(Qt3DCore::QNode *parent = 0) |
| 57 | : QComponent(parent) |
| 58 | {} |
| 59 | }; |
| 60 | |
| 61 | class MyEntity : public Qt3DCore::QEntity |
| 62 | { |
| 63 | public: |
| 64 | explicit MyEntity(Qt3DCore::QNode *parent = nullptr) |
| 65 | : QEntity(parent) |
| 66 | {} |
| 67 | }; |
| 68 | |
| 69 | void tst_Entity::constructionDestruction() |
| 70 | { |
| 71 | // GIVEN |
| 72 | QEntity *entity = nullptr; |
| 73 | // WHEN |
| 74 | entity = new QEntity; |
| 75 | // THEN |
| 76 | QVERIFY(entity != nullptr); |
| 77 | |
| 78 | delete entity; |
| 79 | |
| 80 | // GIVEN |
| 81 | QScopedPointer<QEntity> entity2(new QEntity); |
| 82 | // WHEN |
| 83 | entity2.reset(nullptr); |
| 84 | // THEN |
| 85 | // this should not crash |
| 86 | } |
| 87 | |
| 88 | void tst_Entity::addComponentSingleParentSingleAggregation() |
| 89 | { |
| 90 | // GIVEN |
| 91 | QScopedPointer<Qt3DCore::QEntity> entity(new QEntity()); |
| 92 | MyQComponent *comp = new MyQComponent(entity.data()); |
| 93 | QCoreApplication::processEvents(); |
| 94 | |
| 95 | // THEN |
| 96 | QVERIFY(comp->parent() == entity.data()); |
| 97 | QCOMPARE(entity->components().size(), 0); |
| 98 | QCOMPARE(entity->children().size(), 1); |
| 99 | QCOMPARE(comp->entities().size(), 0); |
| 100 | |
| 101 | // WHEN |
| 102 | entity->addComponent(comp); |
| 103 | |
| 104 | // THEN |
| 105 | QVERIFY(comp->parent() == entity.data()); |
| 106 | QCOMPARE(entity->components().size(), 1); |
| 107 | QCOMPARE(entity->children().size(), 1); |
| 108 | QCOMPARE(comp->entities().size(), 1); |
| 109 | } |
| 110 | |
| 111 | void tst_Entity::addComponentSingleParentSeveralAggregations() |
| 112 | { |
| 113 | // GIVEN |
| 114 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 115 | QScopedPointer<Qt3DCore::QEntity> entity2(new QEntity()); |
| 116 | |
| 117 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 118 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 119 | MyQComponent *comp3 = new MyQComponent(entity1.data()); |
| 120 | QCoreApplication::processEvents(); |
| 121 | |
| 122 | // THEN |
| 123 | QVERIFY(comp1->parent() == entity1.data()); |
| 124 | QVERIFY(comp2->parent() == entity1.data()); |
| 125 | QVERIFY(comp3->parent() == entity1.data()); |
| 126 | |
| 127 | QCOMPARE(entity1->components().size(), 0); |
| 128 | QCOMPARE(entity2->components().size(), 0); |
| 129 | |
| 130 | QCOMPARE(entity1->children().size(), 3); |
| 131 | QCOMPARE(entity2->children().size(), 0); |
| 132 | |
| 133 | QCOMPARE(comp1->entities().size(), 0); |
| 134 | QCOMPARE(comp2->entities().size(), 0); |
| 135 | QCOMPARE(comp3->entities().size(), 0); |
| 136 | |
| 137 | // WHEN |
| 138 | entity1->addComponent(comp1); |
| 139 | entity1->addComponent(comp2); |
| 140 | entity1->addComponent(comp3); |
| 141 | |
| 142 | entity2->addComponent(comp1); |
| 143 | entity2->addComponent(comp2); |
| 144 | entity2->addComponent(comp3); |
| 145 | |
| 146 | // THEN |
| 147 | QVERIFY(comp1->parent() == entity1.data()); |
| 148 | QVERIFY(comp2->parent() == entity1.data()); |
| 149 | QVERIFY(comp3->parent() == entity1.data()); |
| 150 | |
| 151 | QCOMPARE(entity1->components().size(), 3); |
| 152 | QCOMPARE(entity2->components().size(), 3); |
| 153 | |
| 154 | QCOMPARE(entity1->children().size(), 3); |
| 155 | QCOMPARE(entity2->children().size(), 0); |
| 156 | |
| 157 | QCOMPARE(comp1->entities().size(), 2); |
| 158 | QCOMPARE(comp2->entities().size(), 2); |
| 159 | QCOMPARE(comp3->entities().size(), 2); |
| 160 | } |
| 161 | |
| 162 | void tst_Entity::addComponentsSeveralParentsSingleAggregations() |
| 163 | { |
| 164 | // GIVEN |
| 165 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 166 | QScopedPointer<Qt3DCore::QEntity> entity2(new QEntity()); |
| 167 | |
| 168 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 169 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 170 | MyQComponent *comp3 = new MyQComponent(entity2.data()); |
| 171 | QCoreApplication::processEvents(); |
| 172 | |
| 173 | // THEN |
| 174 | QVERIFY(comp1->parent() == entity1.data()); |
| 175 | QVERIFY(comp2->parent() == entity1.data()); |
| 176 | QVERIFY(comp3->parent() == entity2.data()); |
| 177 | |
| 178 | QCOMPARE(entity1->components().size(), 0); |
| 179 | QCOMPARE(entity2->components().size(), 0); |
| 180 | |
| 181 | QCOMPARE(entity1->children().size(), 2); |
| 182 | QCOMPARE(entity2->children().size(), 1); |
| 183 | |
| 184 | QCOMPARE(comp1->entities().size(), 0); |
| 185 | QCOMPARE(comp2->entities().size(), 0); |
| 186 | QCOMPARE(comp3->entities().size(), 0); |
| 187 | |
| 188 | // WHEN |
| 189 | entity1->addComponent(comp1); |
| 190 | entity1->addComponent(comp2); |
| 191 | |
| 192 | entity2->addComponent(comp3); |
| 193 | |
| 194 | // THEN |
| 195 | QVERIFY(comp1->parent() == entity1.data()); |
| 196 | QVERIFY(comp2->parent() == entity1.data()); |
| 197 | QVERIFY(comp3->parent() == entity2.data()); |
| 198 | |
| 199 | QCOMPARE(entity1->components().size(), 2); |
| 200 | QCOMPARE(entity2->components().size(), 1); |
| 201 | |
| 202 | QCOMPARE(entity1->children().size(), 2); |
| 203 | QCOMPARE(entity2->children().size(), 1); |
| 204 | |
| 205 | QCOMPARE(comp1->entities().size(), 1); |
| 206 | QCOMPARE(comp2->entities().size(), 1); |
| 207 | QCOMPARE(comp3->entities().size(), 1); |
| 208 | } |
| 209 | |
| 210 | void tst_Entity::addComponentsSeveralParentsSeveralAggregations() |
| 211 | { |
| 212 | // GIVEN |
| 213 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 214 | QScopedPointer<Qt3DCore::QEntity> entity2(new QEntity()); |
| 215 | |
| 216 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 217 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 218 | MyQComponent *comp3 = new MyQComponent(entity2.data()); |
| 219 | QCoreApplication::processEvents(); |
| 220 | |
| 221 | // THEN |
| 222 | QVERIFY(comp1->parent() == entity1.data()); |
| 223 | QVERIFY(comp2->parent() == entity1.data()); |
| 224 | QVERIFY(comp3->parent() == entity2.data()); |
| 225 | |
| 226 | QCOMPARE(entity1->components().size(), 0); |
| 227 | QCOMPARE(entity2->components().size(), 0); |
| 228 | |
| 229 | QCOMPARE(entity1->children().size(), 2); |
| 230 | QCOMPARE(entity2->children().size(), 1); |
| 231 | |
| 232 | QCOMPARE(comp1->entities().size(), 0); |
| 233 | QCOMPARE(comp2->entities().size(), 0); |
| 234 | QCOMPARE(comp3->entities().size(), 0); |
| 235 | |
| 236 | // WHEN |
| 237 | entity1->addComponent(comp1); |
| 238 | entity1->addComponent(comp2); |
| 239 | entity1->addComponent(comp3); |
| 240 | |
| 241 | entity2->addComponent(comp1); |
| 242 | entity2->addComponent(comp2); |
| 243 | entity2->addComponent(comp3); |
| 244 | |
| 245 | // THEN |
| 246 | QVERIFY(comp1->parent() == entity1.data()); |
| 247 | QVERIFY(comp2->parent() == entity1.data()); |
| 248 | QVERIFY(comp3->parent() == entity2.data()); |
| 249 | |
| 250 | QCOMPARE(entity1->components().size(), 3); |
| 251 | QCOMPARE(entity2->components().size(), 3); |
| 252 | |
| 253 | QCOMPARE(entity1->children().size(), 2); |
| 254 | QCOMPARE(entity2->children().size(), 1); |
| 255 | |
| 256 | QCOMPARE(comp1->entities().size(), 2); |
| 257 | QCOMPARE(comp2->entities().size(), 2); |
| 258 | QCOMPARE(comp3->entities().size(), 2); |
| 259 | } |
| 260 | |
| 261 | void tst_Entity::removeComponentSingleParentSingleAggregation() |
| 262 | { |
| 263 | // GIVEN |
| 264 | QScopedPointer<Qt3DCore::QEntity> entity(new QEntity()); |
| 265 | MyQComponent *comp = new MyQComponent(entity.data()); |
| 266 | QCoreApplication::processEvents(); |
| 267 | entity->addComponent(comp); |
| 268 | |
| 269 | // THEN |
| 270 | QVERIFY(comp->parent() == entity.data()); |
| 271 | QCOMPARE(entity->components().size(), 1); |
| 272 | QCOMPARE(entity->children().size(), 1); |
| 273 | QCOMPARE(comp->entities().size(), 1); |
| 274 | |
| 275 | // WHEN |
| 276 | entity->removeComponent(comp); |
| 277 | |
| 278 | // THEN |
| 279 | QVERIFY(comp->parent() == entity.data()); |
| 280 | QCOMPARE(entity->components().size(), 0); |
| 281 | QCOMPARE(entity->children().size(), 1); |
| 282 | QCOMPARE(comp->entities().size(), 0); |
| 283 | } |
| 284 | |
| 285 | void tst_Entity::removeComponentSingleParentSeveralAggregations() |
| 286 | { |
| 287 | // GIVEN |
| 288 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 289 | QScopedPointer<Qt3DCore::QEntity> entity2(new QEntity()); |
| 290 | |
| 291 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 292 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 293 | MyQComponent *comp3 = new MyQComponent(entity1.data()); |
| 294 | QCoreApplication::processEvents(); |
| 295 | |
| 296 | entity1->addComponent(comp1); |
| 297 | entity1->addComponent(comp2); |
| 298 | entity1->addComponent(comp3); |
| 299 | |
| 300 | entity2->addComponent(comp1); |
| 301 | entity2->addComponent(comp2); |
| 302 | entity2->addComponent(comp3); |
| 303 | |
| 304 | // THEN |
| 305 | QVERIFY(comp1->parent() == entity1.data()); |
| 306 | QVERIFY(comp2->parent() == entity1.data()); |
| 307 | QVERIFY(comp3->parent() == entity1.data()); |
| 308 | |
| 309 | QCOMPARE(entity1->components().size(), 3); |
| 310 | QCOMPARE(entity2->components().size(), 3); |
| 311 | |
| 312 | QCOMPARE(entity1->children().size(), 3); |
| 313 | QCOMPARE(entity2->children().size(), 0); |
| 314 | |
| 315 | QCOMPARE(comp1->entities().size(), 2); |
| 316 | QCOMPARE(comp2->entities().size(), 2); |
| 317 | QCOMPARE(comp3->entities().size(), 2); |
| 318 | |
| 319 | // WHEN |
| 320 | entity1->removeComponent(comp1); |
| 321 | entity1->removeComponent(comp2); |
| 322 | entity1->removeComponent(comp3); |
| 323 | |
| 324 | // THEN |
| 325 | QVERIFY(comp1->parent() == entity1.data()); |
| 326 | QVERIFY(comp2->parent() == entity1.data()); |
| 327 | QVERIFY(comp3->parent() == entity1.data()); |
| 328 | |
| 329 | QCOMPARE(entity1->components().size(), 0); |
| 330 | QCOMPARE(entity2->components().size(), 3); |
| 331 | |
| 332 | QCOMPARE(entity1->children().size(), 3); |
| 333 | QCOMPARE(entity2->children().size(), 0); |
| 334 | |
| 335 | QCOMPARE(comp1->entities().size(), 1); |
| 336 | QCOMPARE(comp2->entities().size(), 1); |
| 337 | QCOMPARE(comp3->entities().size(), 1); |
| 338 | |
| 339 | // WHEN |
| 340 | entity2->removeComponent(comp1); |
| 341 | entity2->removeComponent(comp2); |
| 342 | entity2->removeComponent(comp3); |
| 343 | |
| 344 | // THEN |
| 345 | QVERIFY(comp1->parent() == entity1.data()); |
| 346 | QVERIFY(comp2->parent() == entity1.data()); |
| 347 | QVERIFY(comp3->parent() == entity1.data()); |
| 348 | |
| 349 | QCOMPARE(entity1->components().size(), 0); |
| 350 | QCOMPARE(entity2->components().size(), 0); |
| 351 | |
| 352 | QCOMPARE(entity1->children().size(), 3); |
| 353 | QCOMPARE(entity2->children().size(), 0); |
| 354 | |
| 355 | QCOMPARE(comp1->entities().size(), 0); |
| 356 | QCOMPARE(comp2->entities().size(), 0); |
| 357 | QCOMPARE(comp3->entities().size(), 0); |
| 358 | } |
| 359 | |
| 360 | void tst_Entity::removeComponentsSeveralParentsSingleAggreation() |
| 361 | { |
| 362 | // GIVEN |
| 363 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 364 | QScopedPointer<Qt3DCore::QEntity> entity2(new QEntity()); |
| 365 | |
| 366 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 367 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 368 | MyQComponent *comp3 = new MyQComponent(entity2.data()); |
| 369 | QCoreApplication::processEvents(); |
| 370 | |
| 371 | // WHEN |
| 372 | entity1->addComponent(comp1); |
| 373 | entity1->addComponent(comp2); |
| 374 | entity2->addComponent(comp3); |
| 375 | |
| 376 | // THEN |
| 377 | QVERIFY(comp1->parent() == entity1.data()); |
| 378 | QVERIFY(comp2->parent() == entity1.data()); |
| 379 | QVERIFY(comp3->parent() == entity2.data()); |
| 380 | |
| 381 | QCOMPARE(entity1->components().size(), 2); |
| 382 | QCOMPARE(entity2->components().size(), 1); |
| 383 | |
| 384 | QCOMPARE(entity1->children().size(), 2); |
| 385 | QCOMPARE(entity2->children().size(), 1); |
| 386 | |
| 387 | QCOMPARE(comp1->entities().size(), 1); |
| 388 | QCOMPARE(comp2->entities().size(), 1); |
| 389 | QCOMPARE(comp3->entities().size(), 1); |
| 390 | |
| 391 | // WHEN |
| 392 | entity1->removeComponent(comp1); |
| 393 | entity1->removeComponent(comp2); |
| 394 | entity2->removeComponent(comp3); |
| 395 | |
| 396 | // THEN |
| 397 | QVERIFY(comp1->parent() == entity1.data()); |
| 398 | QVERIFY(comp2->parent() == entity1.data()); |
| 399 | QVERIFY(comp3->parent() == entity2.data()); |
| 400 | |
| 401 | QCOMPARE(entity1->components().size(), 0); |
| 402 | QCOMPARE(entity2->components().size(), 0); |
| 403 | |
| 404 | QCOMPARE(entity1->children().size(), 2); |
| 405 | QCOMPARE(entity2->children().size(), 1); |
| 406 | |
| 407 | QCOMPARE(comp1->entities().size(), 0); |
| 408 | QCOMPARE(comp2->entities().size(), 0); |
| 409 | QCOMPARE(comp3->entities().size(), 0); |
| 410 | } |
| 411 | |
| 412 | void tst_Entity::removeComponentsSeveralParentsSeveralAggregations() |
| 413 | { |
| 414 | // GIVEN |
| 415 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 416 | QScopedPointer<Qt3DCore::QEntity> entity2(new QEntity()); |
| 417 | |
| 418 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 419 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 420 | MyQComponent *comp3 = new MyQComponent(entity2.data()); |
| 421 | QCoreApplication::processEvents(); |
| 422 | |
| 423 | // WHEN |
| 424 | entity1->addComponent(comp1); |
| 425 | entity1->addComponent(comp2); |
| 426 | entity1->addComponent(comp3); |
| 427 | |
| 428 | entity2->addComponent(comp1); |
| 429 | entity2->addComponent(comp2); |
| 430 | entity2->addComponent(comp3); |
| 431 | |
| 432 | // THEN |
| 433 | QVERIFY(comp1->parent() == entity1.data()); |
| 434 | QVERIFY(comp2->parent() == entity1.data()); |
| 435 | QVERIFY(comp3->parent() == entity2.data()); |
| 436 | |
| 437 | QCOMPARE(entity1->components().size(), 3); |
| 438 | QCOMPARE(entity2->components().size(), 3); |
| 439 | |
| 440 | QCOMPARE(entity1->children().size(), 2); |
| 441 | QCOMPARE(entity2->children().size(), 1); |
| 442 | |
| 443 | QCOMPARE(comp1->entities().size(), 2); |
| 444 | QCOMPARE(comp2->entities().size(), 2); |
| 445 | QCOMPARE(comp3->entities().size(), 2); |
| 446 | |
| 447 | // WHEN |
| 448 | entity1->removeComponent(comp1); |
| 449 | entity1->removeComponent(comp2); |
| 450 | entity1->removeComponent(comp3); |
| 451 | |
| 452 | // THEN |
| 453 | QVERIFY(comp1->parent() == entity1.data()); |
| 454 | QVERIFY(comp2->parent() == entity1.data()); |
| 455 | QVERIFY(comp3->parent() == entity2.data()); |
| 456 | |
| 457 | QCOMPARE(entity1->components().size(), 0); |
| 458 | QCOMPARE(entity2->components().size(), 3); |
| 459 | |
| 460 | QCOMPARE(entity1->children().size(), 2); |
| 461 | QCOMPARE(entity2->children().size(), 1); |
| 462 | |
| 463 | QCOMPARE(comp1->entities().size(), 1); |
| 464 | QCOMPARE(comp2->entities().size(), 1); |
| 465 | QCOMPARE(comp3->entities().size(), 1); |
| 466 | |
| 467 | // WHEN |
| 468 | entity2->removeComponent(comp1); |
| 469 | entity2->removeComponent(comp2); |
| 470 | entity2->removeComponent(comp3); |
| 471 | |
| 472 | // THEN |
| 473 | QVERIFY(comp1->parent() == entity1.data()); |
| 474 | QVERIFY(comp2->parent() == entity1.data()); |
| 475 | QVERIFY(comp3->parent() == entity2.data()); |
| 476 | |
| 477 | QCOMPARE(entity1->components().size(), 0); |
| 478 | QCOMPARE(entity2->components().size(), 0); |
| 479 | |
| 480 | QCOMPARE(entity1->children().size(), 2); |
| 481 | QCOMPARE(entity2->children().size(), 1); |
| 482 | |
| 483 | QCOMPARE(comp1->entities().size(), 0); |
| 484 | QCOMPARE(comp2->entities().size(), 0); |
| 485 | QCOMPARE(comp3->entities().size(), 0); |
| 486 | } |
| 487 | |
| 488 | void tst_Entity::retrieveSingleComponent() |
| 489 | { |
| 490 | // GIVEN |
| 491 | QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); |
| 492 | |
| 493 | MyQComponent *comp1 = new MyQComponent(entity1.data()); |
| 494 | MyQComponent *comp2 = new MyQComponent(entity1.data()); |
| 495 | QCoreApplication::processEvents(); |
| 496 | entity1->addComponent(comp1); |
| 497 | entity1->addComponent(comp2); |
| 498 | |
| 499 | // WHEN |
| 500 | const QList<MyQComponent *> myQComponentsInEntity = entity1->componentsOfType<MyQComponent>(); |
| 501 | const QList<MyQ2Component *> myQ2ComponentsInEntity = entity1->componentsOfType<MyQ2Component>(); |
| 502 | |
| 503 | // THEN |
| 504 | QVERIFY(myQComponentsInEntity.size() == 2); |
| 505 | QVERIFY(myQComponentsInEntity[0] == comp1); |
| 506 | QVERIFY(myQComponentsInEntity[1] == comp2); |
| 507 | |
| 508 | QVERIFY(myQ2ComponentsInEntity.size() == 0); |
| 509 | } |
| 510 | |
| 511 | void tst_Entity::addSeveralTimesSameComponent() |
| 512 | { |
| 513 | // GIVEN |
| 514 | QScopedPointer<Qt3DCore::QEntity> entity(new QEntity()); |
| 515 | MyQComponent *comp = new MyQComponent(entity.data()); |
| 516 | QCoreApplication::processEvents(); |
| 517 | entity->addComponent(comp); |
| 518 | |
| 519 | // THEN |
| 520 | QVERIFY(comp->parent() == entity.data()); |
| 521 | QCOMPARE(entity->components().size(), 1); |
| 522 | QCOMPARE(entity->children().size(), 1); |
| 523 | QCOMPARE(comp->entities().size(), 1); |
| 524 | |
| 525 | // WHEN |
| 526 | entity->addComponent(comp); |
| 527 | |
| 528 | // THEN |
| 529 | QVERIFY(comp->parent() == entity.data()); |
| 530 | QCOMPARE(entity->components().size(), 1); |
| 531 | QCOMPARE(entity->children().size(), 1); |
| 532 | QCOMPARE(comp->entities().size(), 1); |
| 533 | } |
| 534 | |
| 535 | void tst_Entity::removeSeveralTimesSameComponent() |
| 536 | { |
| 537 | // GIVEN |
| 538 | QScopedPointer<Qt3DCore::QEntity> entity(new QEntity()); |
| 539 | MyQComponent *comp = new MyQComponent(entity.data()); |
| 540 | QCoreApplication::processEvents(); |
| 541 | entity->addComponent(comp); |
| 542 | entity->removeComponent(comp); |
| 543 | |
| 544 | // THEN |
| 545 | QVERIFY(comp->parent() == entity.data()); |
| 546 | QCOMPARE(entity->components().size(), 0); |
| 547 | QCOMPARE(entity->children().size(), 1); |
| 548 | QCOMPARE(comp->entities().size(), 0); |
| 549 | |
| 550 | // WHEN |
| 551 | entity->removeComponent(comp); |
| 552 | |
| 553 | // THEN |
| 554 | QVERIFY(comp->parent() == entity.data()); |
| 555 | QCOMPARE(entity->components().size(), 0); |
| 556 | QCOMPARE(entity->children().size(), 1); |
| 557 | QCOMPARE(comp->entities().size(), 0); |
| 558 | } |
| 559 | |
| 560 | void tst_Entity::checkComponentBookkeeping() |
| 561 | { |
| 562 | // GIVEN |
| 563 | QScopedPointer<Qt3DCore::QEntity> rootEntity(new Qt3DCore::QEntity); |
| 564 | { |
| 565 | // WHEN |
| 566 | QScopedPointer<Qt3DCore::QComponent> comp(new MyQComponent(rootEntity.data())); |
| 567 | rootEntity->addComponent(comp.data()); |
| 568 | |
| 569 | // THEN |
| 570 | QCOMPARE(comp->parent(), rootEntity.data()); |
| 571 | QCOMPARE(rootEntity->components().size(), 1); |
| 572 | } |
| 573 | // THEN (Should not crash and comp should be automatically removed) |
| 574 | QVERIFY(rootEntity->components().empty()); |
| 575 | |
| 576 | { |
| 577 | // WHEN |
| 578 | QScopedPointer<Qt3DCore::QEntity> someOtherEntity(new Qt3DCore::QEntity); |
| 579 | QScopedPointer<Qt3DCore::QComponent> comp(new MyQComponent(someOtherEntity.data())); |
| 580 | rootEntity->addComponent(comp.data()); |
| 581 | |
| 582 | // THEN |
| 583 | QCOMPARE(comp->parent(), someOtherEntity.data()); |
| 584 | QCOMPARE(rootEntity->components().size(), 1); |
| 585 | |
| 586 | // WHEN |
| 587 | int sigCount = 0; |
| 588 | QObject *sigSender = comp.data(); |
| 589 | connect(comp.data(), &QComponent::removedFromEntity, this, [&sigCount, sigSender](QEntity *) { |
| 590 | QComponent *c = qobject_cast<QComponent *>(sigSender); |
| 591 | if (sigSender && c) |
| 592 | sigCount++; // test the sender is still a QComponent when signal is emitted |
| 593 | }); |
| 594 | |
| 595 | comp.reset(); |
| 596 | rootEntity.reset(); |
| 597 | |
| 598 | // THEN (Should not crash when the comp is destroyed (tests for failed removal of destruction helper) |
| 599 | QCOMPARE(sigCount, 1); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | Qt3DCore::QNodeId parentEntityId(Qt3DCore::QEntity *entity) |
| 604 | { |
| 605 | Qt3DCore::QEntityPrivate *d = static_cast<Qt3DCore::QEntityPrivate*>(Qt3DCore::QNodePrivate::get(entity)); |
| 606 | return d->parentEntityId(); |
| 607 | } |
| 608 | |
| 609 | QTEST_MAIN(tst_Entity) |
| 610 | |
| 611 | #include "tst_qentity.moc" |