| 1 | // Copyright (C) 2015 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/qnode_p.h> |
| 6 | #include <Qt3DCore/private/qscene_p.h> |
| 7 | |
| 8 | #include <Qt3DCore/qgeometry.h> |
| 9 | #include <Qt3DCore/private/qgeometry_p.h> |
| 10 | #include <Qt3DCore/qattribute.h> |
| 11 | #include <Qt3DCore/qbuffer.h> |
| 12 | |
| 13 | #include <QSignalSpy> |
| 14 | |
| 15 | #include "testarbiter.h" |
| 16 | |
| 17 | class FakeGeometry : public Qt3DCore::QGeometry |
| 18 | { |
| 19 | Q_OBJECT |
| 20 | }; |
| 21 | |
| 22 | class tst_QGeometry: public QObject |
| 23 | { |
| 24 | Q_OBJECT |
| 25 | |
| 26 | private Q_SLOTS: |
| 27 | |
| 28 | void checkPropertyUpdates() |
| 29 | { |
| 30 | // GIVEN |
| 31 | TestArbiter arbiter; |
| 32 | QScopedPointer<Qt3DCore::QGeometry> geometry(new Qt3DCore::QGeometry()); |
| 33 | arbiter.setArbiterOnNode(geometry.data()); |
| 34 | |
| 35 | // WHEN |
| 36 | Qt3DCore::QAttribute attr; |
| 37 | geometry->addAttribute(&attr); |
| 38 | QCoreApplication::processEvents(); |
| 39 | |
| 40 | // THEN |
| 41 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 42 | QCOMPARE(arbiter.dirtyNodes().front(), geometry.data()); |
| 43 | |
| 44 | arbiter.clear(); |
| 45 | |
| 46 | // WHEN |
| 47 | geometry->addAttribute(&attr); |
| 48 | QCoreApplication::processEvents(); |
| 49 | |
| 50 | // THEN |
| 51 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 52 | |
| 53 | // WHEN |
| 54 | geometry->removeAttribute(&attr); |
| 55 | QCoreApplication::processEvents(); |
| 56 | |
| 57 | // THEN |
| 58 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 59 | QCOMPARE(arbiter.dirtyNodes().front(), geometry.data()); |
| 60 | } |
| 61 | |
| 62 | void checkAttributeBookkeeping() |
| 63 | { |
| 64 | // GIVEN |
| 65 | QScopedPointer<Qt3DCore::QGeometry> geometry(new Qt3DCore::QGeometry); |
| 66 | { |
| 67 | // WHEN |
| 68 | Qt3DCore::QAttribute attribute; |
| 69 | geometry->addAttribute(&attribute); |
| 70 | |
| 71 | // THEN |
| 72 | QCOMPARE(attribute.parent(), geometry.data()); |
| 73 | QCOMPARE(geometry->attributes().size(), 1); |
| 74 | } |
| 75 | // THEN (Should not crash and parameter be unset) |
| 76 | QVERIFY(geometry->attributes().isEmpty()); |
| 77 | |
| 78 | { |
| 79 | // WHEN |
| 80 | Qt3DCore::QGeometry someOtherGeometry; |
| 81 | QScopedPointer<Qt3DCore::QAttribute> attribute(new Qt3DCore::QAttribute(&someOtherGeometry)); |
| 82 | geometry->addAttribute(attribute.data()); |
| 83 | |
| 84 | // THEN |
| 85 | QCOMPARE(attribute->parent(), &someOtherGeometry); |
| 86 | QCOMPARE(geometry->attributes().size(), 1); |
| 87 | |
| 88 | // WHEN |
| 89 | geometry.reset(); |
| 90 | attribute.reset(); |
| 91 | |
| 92 | // THEN Should not crash when the attribute is destroyed (tests for failed removal of destruction helper) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void checkExtentUpdates() |
| 97 | { |
| 98 | // GIVEN |
| 99 | TestArbiter arbiter; |
| 100 | QScopedPointer<FakeGeometry> geometry(new FakeGeometry()); |
| 101 | arbiter.setArbiterOnNode(geometry.data()); |
| 102 | QSignalSpy spyMinExtent(geometry.data(), SIGNAL(minExtentChanged(QVector3D))); |
| 103 | QSignalSpy spyMaxExtent(geometry.data(), SIGNAL(maxExtentChanged(QVector3D))); |
| 104 | |
| 105 | // THEN |
| 106 | QVERIFY(spyMinExtent.isValid()); |
| 107 | QVERIFY(spyMaxExtent.isValid()); |
| 108 | QCOMPARE(geometry->minExtent(), QVector3D()); |
| 109 | QCOMPARE(geometry->maxExtent(), QVector3D()); |
| 110 | |
| 111 | // WHEN |
| 112 | auto dNode = static_cast<Qt3DCore::QGeometryPrivate *>(Qt3DCore::QNodePrivate::get(geometry.data())); |
| 113 | const QVector3D minExt(-1.0f, -1.0f, -1.0f); |
| 114 | const QVector3D maxExt(1.0f, 1.0f, 1.0f); |
| 115 | dNode->setExtent(minExt, maxExt); |
| 116 | |
| 117 | // THEN |
| 118 | QCOMPARE(spyMinExtent.size(), 1); |
| 119 | QCOMPARE(spyMaxExtent.size(), 1); |
| 120 | QCOMPARE(geometry->minExtent(), minExt); |
| 121 | QCOMPARE(geometry->maxExtent(), maxExt); |
| 122 | |
| 123 | spyMaxExtent.clear(); |
| 124 | spyMinExtent.clear(); |
| 125 | |
| 126 | // WHEN |
| 127 | dNode->setExtent(minExt, maxExt); |
| 128 | |
| 129 | // THEN |
| 130 | QCOMPARE(spyMinExtent.size(), 0); |
| 131 | QCOMPARE(spyMaxExtent.size(), 0); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | QTEST_MAIN(tst_QGeometry) |
| 136 | |
| 137 | #include "tst_qgeometry.moc" |