| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 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/private/qnodecreatedchangegenerator_p.h> | 
| 33 |  | 
| 34 | #include <Qt3DInput/qaxis.h> | 
| 35 | #include <Qt3DInput/qaxisaccumulator.h> | 
| 36 | #include <Qt3DInput/private/qaxisaccumulator_p.h> | 
| 37 |  | 
| 38 | #include "testpostmanarbiter.h" | 
| 39 |  | 
| 40 | class tst_QAxisAccumulator: public Qt3DInput::QAxisAccumulator | 
| 41 | { | 
| 42 |     Q_OBJECT | 
| 43 | public: | 
| 44 |     tst_QAxisAccumulator() | 
| 45 |     { | 
| 46 |     } | 
| 47 |  | 
| 48 | private Q_SLOTS: | 
| 49 |  | 
| 50 |     void checkCreationChange_data() | 
| 51 |     { | 
| 52 |         QTest::addColumn<QSharedPointer<Qt3DInput::QAxisAccumulator>>(name: "accumulator" ); | 
| 53 |         QTest::addColumn<Qt3DInput::QAxis *>(name: "sourceAxis" ); | 
| 54 |         QTest::addColumn<Qt3DInput::QAxisAccumulator::SourceAxisType>(name: "sourceAxisType" ); | 
| 55 |         QTest::addColumn<float>(name: "scale" ); | 
| 56 |  | 
| 57 |         { | 
| 58 |             auto accumulator = QSharedPointer<Qt3DInput::QAxisAccumulator>::create(); | 
| 59 |             QTest::newRow(dataTag: "defaultConstructed" ) | 
| 60 |                     << accumulator | 
| 61 |                     << static_cast<Qt3DInput::QAxis *>(nullptr) | 
| 62 |                     << Qt3DInput::QAxisAccumulator::Velocity | 
| 63 |                     << 1.0f; | 
| 64 |         } | 
| 65 |  | 
| 66 |         { | 
| 67 |             auto accumulator = QSharedPointer<Qt3DInput::QAxisAccumulator>::create(); | 
| 68 |             Qt3DInput::QAxis *axis = new Qt3DInput::QAxis(); | 
| 69 |             accumulator->setSourceAxis(axis); | 
| 70 |             QTest::newRow(dataTag: "withSourceAxis" ) | 
| 71 |                     << accumulator | 
| 72 |                     << axis | 
| 73 |                     << Qt3DInput::QAxisAccumulator::Velocity | 
| 74 |                     << 1.0f; | 
| 75 |         } | 
| 76 |  | 
| 77 |         { | 
| 78 |             auto accumulator = QSharedPointer<Qt3DInput::QAxisAccumulator>::create(); | 
| 79 |             accumulator->setSourceAxisType(Qt3DInput::QAxisAccumulator::Acceleration); | 
| 80 |             accumulator->setScale(2.5f); | 
| 81 |             Qt3DInput::QAxis *axis = new Qt3DInput::QAxis(); | 
| 82 |             accumulator->setSourceAxis(axis); | 
| 83 |             QTest::newRow(dataTag: "accelerationNonUniformScale" ) | 
| 84 |                     << accumulator | 
| 85 |                     << axis | 
| 86 |                     << Qt3DInput::QAxisAccumulator::Acceleration | 
| 87 |                     << 2.5f; | 
| 88 |         } | 
| 89 |     } | 
| 90 |  | 
| 91 |     void checkCreationChange() | 
| 92 |     { | 
| 93 |         // GIVEN | 
| 94 |         QFETCH(QSharedPointer<Qt3DInput::QAxisAccumulator>, accumulator); | 
| 95 |         QFETCH(Qt3DInput::QAxis *, sourceAxis); | 
| 96 |         QFETCH(Qt3DInput::QAxisAccumulator::SourceAxisType, sourceAxisType); | 
| 97 |         QFETCH(float, scale); | 
| 98 |  | 
| 99 |         // WHEN | 
| 100 |         Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(accumulator.data()); | 
| 101 |         QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); | 
| 102 |  | 
| 103 |         // THEN | 
| 104 |         QCOMPARE(creationChanges.size(), sourceAxis ? 2 : 1); | 
| 105 |  | 
| 106 |         const auto creationChangeData = | 
| 107 |                qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QAxisAccumulatorData>>(src: creationChanges.first()); | 
| 108 |         const Qt3DInput::QAxisAccumulatorData &creationData = creationChangeData->data; | 
| 109 |  | 
| 110 |         // THEN | 
| 111 |         QCOMPARE(accumulator->id(), creationChangeData->subjectId()); | 
| 112 |         QCOMPARE(accumulator->isEnabled(), creationChangeData->isNodeEnabled()); | 
| 113 |         QCOMPARE(accumulator->metaObject(), creationChangeData->metaObject()); | 
| 114 |         if (sourceAxis) | 
| 115 |             QCOMPARE(sourceAxis->id(), creationData.sourceAxisId); | 
| 116 |         QCOMPARE(sourceAxisType, creationData.sourceAxisType); | 
| 117 |         QCOMPARE(scale, creationData.scale); | 
| 118 |     } | 
| 119 |  | 
| 120 |     void checkPropertyUpdates() | 
| 121 |     { | 
| 122 |         // GIVEN | 
| 123 |         TestArbiter arbiter; | 
| 124 |         QScopedPointer<Qt3DInput::QAxisAccumulator> accumulator(new Qt3DInput::QAxisAccumulator()); | 
| 125 |         arbiter.setArbiterOnNode(accumulator.data()); | 
| 126 |         Qt3DInput::QAxis *axis = new Qt3DInput::QAxis; | 
| 127 |  | 
| 128 |         // WHEN | 
| 129 |         accumulator->setSourceAxis(axis); | 
| 130 |  | 
| 131 |         // THEN | 
| 132 |         QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 133 |         QCOMPARE(arbiter.dirtyNodes.front(), accumulator.data()); | 
| 134 |  | 
| 135 |         arbiter.dirtyNodes.clear(); | 
| 136 |  | 
| 137 |  | 
| 138 |         // WHEN | 
| 139 |         accumulator->setScale(2.0f); | 
| 140 |  | 
| 141 |         // THEN | 
| 142 |         QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 143 |         QCOMPARE(arbiter.dirtyNodes.front(), accumulator.data()); | 
| 144 |  | 
| 145 |         arbiter.dirtyNodes.clear(); | 
| 146 |  | 
| 147 |         // WHEN | 
| 148 |         accumulator->setSourceAxisType(Qt3DInput::QAxisAccumulator::Acceleration); | 
| 149 |  | 
| 150 |         // THEN | 
| 151 |         QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 152 |         QCOMPARE(arbiter.dirtyNodes.front(), accumulator.data()); | 
| 153 |  | 
| 154 |         arbiter.dirtyNodes.clear(); | 
| 155 |     } | 
| 156 |  | 
| 157 |     void checkAxisInputBookkeeping() | 
| 158 |     { | 
| 159 |         // GIVEN | 
| 160 |         QScopedPointer<Qt3DInput::QAxisAccumulator> accumulator(new Qt3DInput::QAxisAccumulator); | 
| 161 |         { | 
| 162 |             // WHEN | 
| 163 |             Qt3DInput::QAxis axis; | 
| 164 |             accumulator->setSourceAxis(&axis); | 
| 165 |  | 
| 166 |             // THEN | 
| 167 |             QCOMPARE(axis.parent(), accumulator.data()); | 
| 168 |             QCOMPARE(accumulator->sourceAxis(), &axis); | 
| 169 |         } | 
| 170 |  | 
| 171 |         // THEN (Should not crash and parameter be unset) | 
| 172 |         QVERIFY(accumulator->sourceAxis() == nullptr); | 
| 173 |  | 
| 174 |         { | 
| 175 |             // WHEN | 
| 176 |             Qt3DInput::QAxisAccumulator someOtherAccumulator; | 
| 177 |             QScopedPointer<Qt3DInput::QAxis> axis(new Qt3DInput::QAxis(&someOtherAccumulator)); | 
| 178 |             accumulator->setSourceAxis(axis.data()); | 
| 179 |  | 
| 180 |             // THEN | 
| 181 |             QCOMPARE(axis->parent(), &someOtherAccumulator); | 
| 182 |             QCOMPARE(accumulator->sourceAxis(), axis.data()); | 
| 183 |  | 
| 184 |             // WHEN | 
| 185 |             accumulator.reset(); | 
| 186 |             axis.reset(); | 
| 187 |  | 
| 188 |             // THEN Should not crash when the input is destroyed (tests for failed removal of destruction helper) | 
| 189 |         } | 
| 190 |     } | 
| 191 | }; | 
| 192 |  | 
| 193 | QTEST_MAIN(tst_QAxisAccumulator) | 
| 194 |  | 
| 195 | #include "tst_qaxisaccumulator.moc" | 
| 196 |  |