| 1 | // Copyright (C) 2017 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DRender/qproximityfilter.h> |
| 6 | #include <Qt3DRender/private/qproximityfilter_p.h> |
| 7 | #include <QObject> |
| 8 | #include <QSignalSpy> |
| 9 | #include <Qt3DCore/qentity.h> |
| 10 | #include "testarbiter.h" |
| 11 | |
| 12 | class tst_QProximityFilter : public QObject |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | |
| 16 | public: |
| 17 | tst_QProximityFilter() : QObject() |
| 18 | { |
| 19 | qRegisterMetaType<Qt3DCore::QEntity*>(); |
| 20 | } |
| 21 | |
| 22 | private Q_SLOTS: |
| 23 | |
| 24 | void checkDefaultConstruction() |
| 25 | { |
| 26 | // GIVEN |
| 27 | Qt3DRender::QProximityFilter proximityFilter; |
| 28 | |
| 29 | // THEN |
| 30 | QVERIFY(proximityFilter.entity() == nullptr); |
| 31 | QCOMPARE(proximityFilter.distanceThreshold(), 0.0f); |
| 32 | } |
| 33 | |
| 34 | void checkPropertyChanges() |
| 35 | { |
| 36 | // GIVEN |
| 37 | Qt3DRender::QProximityFilter proximityFilter; |
| 38 | |
| 39 | { |
| 40 | // WHEN |
| 41 | Qt3DCore::QEntity entity; |
| 42 | |
| 43 | QSignalSpy spy(&proximityFilter, SIGNAL(entityChanged(Qt3DCore::QEntity*))); |
| 44 | Qt3DCore::QEntity *newValue = &entity; |
| 45 | proximityFilter.setEntity(newValue); |
| 46 | |
| 47 | // THEN |
| 48 | QVERIFY(spy.isValid()); |
| 49 | QCOMPARE(proximityFilter.entity(), newValue); |
| 50 | QCOMPARE(spy.size(), 1); |
| 51 | |
| 52 | // WHEN |
| 53 | spy.clear(); |
| 54 | proximityFilter.setEntity(newValue); |
| 55 | |
| 56 | // THEN |
| 57 | QCOMPARE(proximityFilter.entity(), newValue); |
| 58 | QCOMPARE(spy.size(), 0); |
| 59 | } |
| 60 | { |
| 61 | // WHEN |
| 62 | QSignalSpy spy(&proximityFilter, SIGNAL(distanceThresholdChanged(float))); |
| 63 | const float newValue = 883.0f; |
| 64 | proximityFilter.setDistanceThreshold(newValue); |
| 65 | |
| 66 | // THEN |
| 67 | QVERIFY(spy.isValid()); |
| 68 | QCOMPARE(proximityFilter.distanceThreshold(), newValue); |
| 69 | QCOMPARE(spy.size(), 1); |
| 70 | |
| 71 | // WHEN |
| 72 | spy.clear(); |
| 73 | proximityFilter.setDistanceThreshold(newValue); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(proximityFilter.distanceThreshold(), newValue); |
| 77 | QCOMPARE(spy.size(), 0); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void checkEntityUpdate() |
| 82 | { |
| 83 | // GIVEN |
| 84 | TestArbiter arbiter; |
| 85 | Qt3DRender::QProximityFilter proximityFilter; |
| 86 | arbiter.setArbiterOnNode(&proximityFilter); |
| 87 | Qt3DCore::QEntity entity; |
| 88 | |
| 89 | { |
| 90 | // WHEN |
| 91 | proximityFilter.setEntity(&entity); |
| 92 | QCoreApplication::processEvents(); |
| 93 | |
| 94 | // THEN |
| 95 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 96 | QCOMPARE(arbiter.dirtyNodes().front(), &proximityFilter); |
| 97 | |
| 98 | arbiter.clear(); |
| 99 | } |
| 100 | |
| 101 | { |
| 102 | // WHEN |
| 103 | proximityFilter.setEntity(&entity); |
| 104 | QCoreApplication::processEvents(); |
| 105 | |
| 106 | // THEN |
| 107 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | void checkDistanceThresholdUpdate() |
| 113 | { |
| 114 | // GIVEN |
| 115 | TestArbiter arbiter; |
| 116 | Qt3DRender::QProximityFilter proximityFilter; |
| 117 | arbiter.setArbiterOnNode(&proximityFilter); |
| 118 | |
| 119 | { |
| 120 | // WHEN |
| 121 | proximityFilter.setDistanceThreshold(454.0f); |
| 122 | QCoreApplication::processEvents(); |
| 123 | |
| 124 | // THEN |
| 125 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 126 | QCOMPARE(arbiter.dirtyNodes().front(), &proximityFilter); |
| 127 | |
| 128 | arbiter.clear(); |
| 129 | } |
| 130 | |
| 131 | { |
| 132 | // WHEN |
| 133 | proximityFilter.setDistanceThreshold(454.0f); |
| 134 | QCoreApplication::processEvents(); |
| 135 | |
| 136 | // THEN |
| 137 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | void checkEntityBookkeeping() |
| 143 | { |
| 144 | // GIVEN |
| 145 | QScopedPointer<Qt3DRender::QProximityFilter> proximityFilter(new Qt3DRender::QProximityFilter); |
| 146 | { |
| 147 | // WHEN |
| 148 | Qt3DCore::QEntity entity; |
| 149 | proximityFilter->setEntity(&entity); |
| 150 | |
| 151 | // THEN |
| 152 | QCOMPARE(entity.parent(), proximityFilter.data()); |
| 153 | QCOMPARE(proximityFilter->entity(), &entity); |
| 154 | } |
| 155 | |
| 156 | // THEN (Should not crash and parameter be unset) |
| 157 | QVERIFY(proximityFilter->entity() == nullptr); |
| 158 | |
| 159 | { |
| 160 | // WHEN |
| 161 | Qt3DRender::QProximityFilter someOtherProximityFilter; |
| 162 | QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity(&someOtherProximityFilter)); |
| 163 | proximityFilter->setEntity(entity.data()); |
| 164 | |
| 165 | // THEN |
| 166 | QCOMPARE(entity->parent(), &someOtherProximityFilter); |
| 167 | QCOMPARE(proximityFilter->entity(), entity.data()); |
| 168 | |
| 169 | // WHEN |
| 170 | proximityFilter.reset(); |
| 171 | entity.reset(); |
| 172 | |
| 173 | // THEN Should not crash when the camera is destroyed (tests for failed removal of destruction helper) |
| 174 | } |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | QTEST_MAIN(tst_QProximityFilter) |
| 179 | |
| 180 | #include "tst_qproximityfilter.moc" |