| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 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 <qbackendnodetester.h> |
| 31 | #include <Qt3DCore/private/qnode_p.h> |
| 32 | #include <Qt3DCore/private/qscene_p.h> |
| 33 | #include <Qt3DInput/private/axis_p.h> |
| 34 | #include <Qt3DInput/private/axisaccumulator_p.h> |
| 35 | #include <Qt3DInput/private/qabstractaxisinput_p.h> |
| 36 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 37 | #include <Qt3DInput/QAnalogAxisInput> |
| 38 | #include <Qt3DInput/QAxis> |
| 39 | #include <Qt3DInput/QAxisAccumulator> |
| 40 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 41 | #include "testpostmanarbiter.h" |
| 42 | |
| 43 | class tst_AxisAccumulator: public Qt3DCore::QBackendNodeTester |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | |
| 47 | private Q_SLOTS: |
| 48 | |
| 49 | void checkPeerPropertyMirroring() |
| 50 | { |
| 51 | // GIVEN |
| 52 | Qt3DInput::Input::AxisAccumulator backendAccumulator; |
| 53 | Qt3DInput::QAxisAccumulator axisAccumulator; |
| 54 | Qt3DInput::QAxis axis; |
| 55 | |
| 56 | axisAccumulator.setSourceAxis(&axis); |
| 57 | axisAccumulator.setSourceAxisType(Qt3DInput::QAxisAccumulator::Velocity); |
| 58 | axisAccumulator.setScale(2.0f); |
| 59 | |
| 60 | // WHEN |
| 61 | simulateInitializationSync(frontend: &axisAccumulator, backend: &backendAccumulator); |
| 62 | |
| 63 | // THEN |
| 64 | QCOMPARE(backendAccumulator.peerId(), axisAccumulator.id()); |
| 65 | QCOMPARE(backendAccumulator.isEnabled(), axisAccumulator.isEnabled()); |
| 66 | QCOMPARE(backendAccumulator.sourceAxisType(), axisAccumulator.sourceAxisType()); |
| 67 | QCOMPARE(backendAccumulator.sourceAxisId(), axisAccumulator.sourceAxis()->id()); |
| 68 | QCOMPARE(backendAccumulator.scale(), axisAccumulator.scale()); |
| 69 | } |
| 70 | |
| 71 | void checkInitialAndCleanedUpState() |
| 72 | { |
| 73 | // GIVEN |
| 74 | Qt3DInput::Input::AxisAccumulator backendAxisAccumulator; |
| 75 | |
| 76 | // THEN |
| 77 | QVERIFY(backendAxisAccumulator.peerId().isNull()); |
| 78 | QCOMPARE(backendAxisAccumulator.isEnabled(), false); |
| 79 | QCOMPARE(backendAxisAccumulator.value(), 0.0f); |
| 80 | QCOMPARE(backendAxisAccumulator.velocity(), 0.0f); |
| 81 | QCOMPARE(backendAxisAccumulator.scale(), 1.0f); |
| 82 | QCOMPARE(backendAxisAccumulator.sourceAxisId(), Qt3DCore::QNodeId()); |
| 83 | QCOMPARE(backendAxisAccumulator.sourceAxisType(), Qt3DInput::QAxisAccumulator::Velocity); |
| 84 | |
| 85 | // GIVEN |
| 86 | Qt3DInput::QAxisAccumulator axisAccumulator; |
| 87 | Qt3DInput::QAxis axis; |
| 88 | |
| 89 | axisAccumulator.setSourceAxis(&axis); |
| 90 | axisAccumulator.setScale(2.0f); |
| 91 | axisAccumulator.setSourceAxisType(Qt3DInput::QAxisAccumulator::Acceleration); |
| 92 | axisAccumulator.setEnabled(true); |
| 93 | |
| 94 | // WHEN |
| 95 | simulateInitializationSync(frontend: &axisAccumulator, backend: &backendAxisAccumulator); |
| 96 | backendAxisAccumulator.cleanup(); |
| 97 | |
| 98 | // THEN |
| 99 | QCOMPARE(backendAxisAccumulator.isEnabled(), false); |
| 100 | QCOMPARE(backendAxisAccumulator.value(), 0.0f); |
| 101 | QCOMPARE(backendAxisAccumulator.velocity(), 0.0f); |
| 102 | QCOMPARE(backendAxisAccumulator.scale(), 1.0f); |
| 103 | QCOMPARE(backendAxisAccumulator.sourceAxisId(), Qt3DCore::QNodeId()); |
| 104 | QCOMPARE(backendAxisAccumulator.sourceAxisType(), Qt3DInput::QAxisAccumulator::Velocity); |
| 105 | } |
| 106 | |
| 107 | void checkPropertyChanges() |
| 108 | { |
| 109 | // GIVEN |
| 110 | Qt3DInput::QAxisAccumulator axisAccumulator; |
| 111 | Qt3DInput::Input::AxisAccumulator backendAxisAccumulator; |
| 112 | simulateInitializationSync(frontend: &axisAccumulator, backend: &backendAxisAccumulator); |
| 113 | |
| 114 | // WHEN |
| 115 | axisAccumulator.setEnabled(false); |
| 116 | backendAxisAccumulator.syncFromFrontEnd(frontEnd: &axisAccumulator, firstTime: false); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(backendAxisAccumulator.isEnabled(), false); |
| 120 | |
| 121 | // WHEN |
| 122 | Qt3DInput::QAxis axis; |
| 123 | const Qt3DCore::QNodeId axisId = axis.id(); |
| 124 | axisAccumulator.setSourceAxis(&axis); |
| 125 | backendAxisAccumulator.syncFromFrontEnd(frontEnd: &axisAccumulator, firstTime: false); |
| 126 | |
| 127 | // THEN |
| 128 | QCOMPARE(backendAxisAccumulator.sourceAxisId(), axisId); |
| 129 | |
| 130 | // WHEN |
| 131 | axisAccumulator.setSourceAxisType(Qt3DInput::QAxisAccumulator::Acceleration); |
| 132 | backendAxisAccumulator.syncFromFrontEnd(frontEnd: &axisAccumulator, firstTime: false); |
| 133 | |
| 134 | // THEN |
| 135 | QCOMPARE(backendAxisAccumulator.sourceAxisType(), Qt3DInput::QAxisAccumulator::Acceleration); |
| 136 | |
| 137 | // WHEN |
| 138 | axisAccumulator.setScale(3.f); |
| 139 | backendAxisAccumulator.syncFromFrontEnd(frontEnd: &axisAccumulator, firstTime: false); |
| 140 | |
| 141 | // THEN |
| 142 | QCOMPARE(backendAxisAccumulator.scale(), 3.0f); |
| 143 | } |
| 144 | |
| 145 | void shouldNotChangeValueWhenDisabled() |
| 146 | { |
| 147 | // GIVEN |
| 148 | TestArbiter arbiter; |
| 149 | Qt3DInput::Input::AxisAccumulator backendAxisAccumulator; |
| 150 | Qt3DCore::QBackendNodePrivate::get(n: &backendAxisAccumulator)->setArbiter(&arbiter); |
| 151 | backendAxisAccumulator.setEnabled(false); |
| 152 | |
| 153 | // WHEN |
| 154 | backendAxisAccumulator.setValue(454.0f); |
| 155 | |
| 156 | // THEN |
| 157 | QCOMPARE(backendAxisAccumulator.value(), 0.0f); |
| 158 | QCOMPARE(arbiter.events.count(), 0); |
| 159 | } |
| 160 | |
| 161 | void checkIntegration_data() |
| 162 | { |
| 163 | QTest::addColumn<Qt3DInput::QAxisAccumulator::SourceAxisType>(name: "sourceAxisType" ); |
| 164 | QTest::addColumn<float>(name: "axisValue" ); |
| 165 | QTest::addColumn<float>(name: "scale" ); |
| 166 | QTest::addColumn<float>(name: "dt" ); |
| 167 | QTest::addColumn<float>(name: "valueResult" ); |
| 168 | QTest::addColumn<float>(name: "velocityResult" ); |
| 169 | |
| 170 | QTest::newRow(dataTag: "velocity=10, axis=1, dt=1" ) << Qt3DInput::QAxisAccumulator::Velocity |
| 171 | << 1.0f |
| 172 | << 10.0f |
| 173 | << 1.0f |
| 174 | << 10.0f |
| 175 | << 0.0f; |
| 176 | |
| 177 | QTest::newRow(dataTag: "velocity=10, axis=1, dt=0.2" ) << Qt3DInput::QAxisAccumulator::Velocity |
| 178 | << 1.0f |
| 179 | << 10.0f |
| 180 | << 0.2f |
| 181 | << 2.0f |
| 182 | << 0.0f; |
| 183 | |
| 184 | QTest::newRow(dataTag: "velocity=20, axis=1, dt=0.1" ) << Qt3DInput::QAxisAccumulator::Velocity |
| 185 | << 1.0f |
| 186 | << 20.0f |
| 187 | << 0.1f |
| 188 | << 2.0f |
| 189 | << 0.0f; |
| 190 | |
| 191 | QTest::newRow(dataTag: "velocity=10, axis=0.5, dt=1" ) << Qt3DInput::QAxisAccumulator::Velocity |
| 192 | << 0.5f |
| 193 | << 10.0f |
| 194 | << 1.0f |
| 195 | << 5.0f |
| 196 | << 0.0f; |
| 197 | |
| 198 | QTest::newRow(dataTag: "acceleration=10, axis=1, dt=1" ) << Qt3DInput::QAxisAccumulator::Acceleration |
| 199 | << 1.0f |
| 200 | << 10.0f |
| 201 | << 1.0f |
| 202 | << 10.0f |
| 203 | << 10.0f; |
| 204 | } |
| 205 | |
| 206 | void checkIntegration() |
| 207 | { |
| 208 | // GIVEN |
| 209 | QFETCH(Qt3DInput::QAxisAccumulator::SourceAxisType, sourceAxisType); |
| 210 | QFETCH(float, axisValue); |
| 211 | QFETCH(float, scale); |
| 212 | QFETCH(float, dt); |
| 213 | QFETCH(float, valueResult); |
| 214 | QFETCH(float, velocityResult); |
| 215 | |
| 216 | Qt3DInput::QAxis *axis = new Qt3DInput::QAxis; |
| 217 | Qt3DInput::Input::AxisManager axisManager; |
| 218 | Qt3DInput::Input::Axis *backendAxis = axisManager.getOrCreateResource(id: axis->id()); |
| 219 | Qt3DInput::QAxisAccumulator axisAccumulator; |
| 220 | Qt3DInput::Input::AxisAccumulator backendAxisAccumulator; |
| 221 | |
| 222 | // WHEN |
| 223 | backendAxis->setEnabled(true); |
| 224 | backendAxis->setAxisValue(axisValue); |
| 225 | axisAccumulator.setSourceAxis(axis); |
| 226 | axisAccumulator.setScale(scale); |
| 227 | axisAccumulator.setSourceAxisType(sourceAxisType); |
| 228 | axisAccumulator.setEnabled(true); |
| 229 | simulateInitializationSync(frontend: &axisAccumulator, backend: &backendAxisAccumulator); |
| 230 | |
| 231 | backendAxisAccumulator.stepIntegration(axisManager: &axisManager, dt); |
| 232 | |
| 233 | // THEN |
| 234 | switch (sourceAxisType) { |
| 235 | case Qt3DInput::QAxisAccumulator::Velocity: |
| 236 | QCOMPARE(backendAxisAccumulator.value(), valueResult); |
| 237 | break; |
| 238 | |
| 239 | case Qt3DInput::QAxisAccumulator::Acceleration: |
| 240 | QCOMPARE(backendAxisAccumulator.velocity(), velocityResult); |
| 241 | QCOMPARE(backendAxisAccumulator.value(), valueResult); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | QTEST_APPLESS_MAIN(tst_AxisAccumulator) |
| 248 | |
| 249 | #include "tst_axisaccumulator.moc" |
| 250 | |