| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Paul Lemire <paul.lemire350@gmail.com> |
| 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 | |
| 30 | #include <QtTest/QTest> |
| 31 | #include <Qt3DInput/private/qabstractphysicaldevicebackendnode_p.h> |
| 32 | #include <Qt3DInput/private/qabstractphysicaldevicebackendnode_p_p.h> |
| 33 | #include <Qt3DInput/private/inputhandler_p.h> |
| 34 | #include <Qt3DInput/qaxissetting.h> |
| 35 | #include <Qt3DInput/qinputaspect.h> |
| 36 | #include <Qt3DInput/private/qinputaspect_p.h> |
| 37 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 38 | #include <Qt3DInput/private/axissetting_p.h> |
| 39 | #include <Qt3DCore/qpropertynoderemovedchange.h> |
| 40 | #include "testdevice.h" |
| 41 | |
| 42 | class TestPhysicalDeviceBackendNode : public Qt3DInput::QAbstractPhysicalDeviceBackendNode |
| 43 | { |
| 44 | public: |
| 45 | TestPhysicalDeviceBackendNode(Qt3DCore::QBackendNode::Mode mode = Qt3DCore::QBackendNode::ReadOnly) |
| 46 | : Qt3DInput::QAbstractPhysicalDeviceBackendNode(mode) |
| 47 | {} |
| 48 | |
| 49 | float axisValue(int axisIdentifier) const override |
| 50 | { |
| 51 | if (axisIdentifier == 883) |
| 52 | return 883.0f; |
| 53 | return 0.0f; |
| 54 | } |
| 55 | |
| 56 | bool isButtonPressed(int buttonIdentifier) const override |
| 57 | { |
| 58 | if (buttonIdentifier == 454) |
| 59 | return true; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | class tst_QAbstractPhysicalDeviceBackendNode : public Qt3DCore::QBackendNodeTester |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | |
| 69 | private Q_SLOTS: |
| 70 | |
| 71 | void checkInitialState() |
| 72 | { |
| 73 | // GIVEN |
| 74 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 75 | |
| 76 | // THEN |
| 77 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.isEnabled(), false); |
| 78 | QVERIFY(backendQAbstractPhysicalDeviceBackendNode.inputAspect() == nullptr); |
| 79 | QVERIFY(backendQAbstractPhysicalDeviceBackendNode.peerId().isNull()); |
| 80 | } |
| 81 | |
| 82 | void checkAxisValue() |
| 83 | { |
| 84 | // GIVEN |
| 85 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 86 | |
| 87 | // WHEN |
| 88 | float axisValue = backendQAbstractPhysicalDeviceBackendNode.axisValue(axisIdentifier: 883); |
| 89 | // THEN |
| 90 | QCOMPARE(axisValue, 883.0f); |
| 91 | |
| 92 | // WHEN |
| 93 | axisValue = backendQAbstractPhysicalDeviceBackendNode.axisValue(axisIdentifier: 454); |
| 94 | // THEN |
| 95 | QCOMPARE(axisValue, 0.0f); |
| 96 | } |
| 97 | |
| 98 | void checkButtonPressed() |
| 99 | { |
| 100 | // GIVEN |
| 101 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 102 | |
| 103 | // WHEN |
| 104 | bool buttonPressed = backendQAbstractPhysicalDeviceBackendNode.isButtonPressed(buttonIdentifier: 883); |
| 105 | // THEN |
| 106 | QCOMPARE(buttonPressed, false); |
| 107 | |
| 108 | // WHEN |
| 109 | buttonPressed = backendQAbstractPhysicalDeviceBackendNode.isButtonPressed(buttonIdentifier: 454); |
| 110 | // THEN |
| 111 | QCOMPARE(buttonPressed, true); |
| 112 | } |
| 113 | |
| 114 | void checkCleanupState() |
| 115 | { |
| 116 | // GIVEN |
| 117 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 118 | Qt3DInput::QInputAspect aspect; |
| 119 | |
| 120 | // WHEN |
| 121 | backendQAbstractPhysicalDeviceBackendNode.setEnabled(true); |
| 122 | backendQAbstractPhysicalDeviceBackendNode.setInputAspect(&aspect); |
| 123 | |
| 124 | // THEN |
| 125 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.inputAspect(), &aspect); |
| 126 | |
| 127 | // WHEN |
| 128 | backendQAbstractPhysicalDeviceBackendNode.cleanup(); |
| 129 | |
| 130 | // THEN |
| 131 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.isEnabled(), false); |
| 132 | QVERIFY(backendQAbstractPhysicalDeviceBackendNode.inputAspect() == nullptr); |
| 133 | } |
| 134 | |
| 135 | void checkInitializeFromPeer() |
| 136 | { |
| 137 | // GIVEN |
| 138 | TestDevice physicalDeviceNode; |
| 139 | |
| 140 | { |
| 141 | // WHEN |
| 142 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 143 | simulateInitializationSync(frontend: &physicalDeviceNode, backend: &backendQAbstractPhysicalDeviceBackendNode); |
| 144 | |
| 145 | // THEN |
| 146 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.isEnabled(), true); |
| 147 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.peerId(), physicalDeviceNode.id()); |
| 148 | } |
| 149 | { |
| 150 | // WHEN |
| 151 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 152 | physicalDeviceNode.setEnabled(false); |
| 153 | simulateInitializationSync(frontend: &physicalDeviceNode, backend: &backendQAbstractPhysicalDeviceBackendNode); |
| 154 | |
| 155 | // THEN |
| 156 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.peerId(), physicalDeviceNode.id()); |
| 157 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.isEnabled(), false); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void checkSceneChangeEvents() |
| 162 | { |
| 163 | // GIVEN |
| 164 | TestDevice physicalDeviceNode; |
| 165 | TestPhysicalDeviceBackendNode backendQAbstractPhysicalDeviceBackendNode; |
| 166 | Qt3DInput::QInputAspect aspect; |
| 167 | backendQAbstractPhysicalDeviceBackendNode.setInputAspect(&aspect); |
| 168 | simulateInitializationSync(frontend: &physicalDeviceNode, backend: &backendQAbstractPhysicalDeviceBackendNode); |
| 169 | |
| 170 | { |
| 171 | // WHEN |
| 172 | const bool newValue = false; |
| 173 | physicalDeviceNode.setEnabled(newValue); |
| 174 | backendQAbstractPhysicalDeviceBackendNode.syncFromFrontEnd(frontEnd: &physicalDeviceNode, firstTime: false); |
| 175 | |
| 176 | // THEN |
| 177 | QCOMPARE(backendQAbstractPhysicalDeviceBackendNode.isEnabled(), newValue); |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | Qt3DInput::QAxisSetting settings1; |
| 182 | Qt3DInput::QAxisSetting settings2; |
| 183 | |
| 184 | settings1.setAxes(QVector<int>() << 883); |
| 185 | settings2.setAxes(QVector<int>() << 454); |
| 186 | Qt3DInput::QAbstractPhysicalDeviceBackendNodePrivate *priv = static_cast<Qt3DInput::QAbstractPhysicalDeviceBackendNodePrivate *>( |
| 187 | Qt3DCore::QBackendNodePrivate::get(n: &backendQAbstractPhysicalDeviceBackendNode)); |
| 188 | |
| 189 | // Create backend resource |
| 190 | { |
| 191 | Qt3DInput::QInputAspectPrivate *aspectPrivate = static_cast<Qt3DInput::QInputAspectPrivate *>(Qt3DCore::QAbstractAspectPrivate::get(aspect: &aspect)); |
| 192 | Qt3DInput::Input::InputHandler *handler = aspectPrivate->m_inputHandler.data(); |
| 193 | Qt3DInput::Input::AxisSetting *backendSetting1 = handler->axisSettingManager()->getOrCreateResource(id: settings1.id()); |
| 194 | Qt3DInput::Input::AxisSetting *backendSetting2 = handler->axisSettingManager()->getOrCreateResource(id: settings2.id()); |
| 195 | simulateInitializationSync(frontend: &settings1, backend: backendSetting1); |
| 196 | simulateInitializationSync(frontend: &settings2, backend: backendSetting2); |
| 197 | } |
| 198 | |
| 199 | // Adding AxisSettings |
| 200 | { |
| 201 | // WHEN |
| 202 | physicalDeviceNode.addAxisSetting(axisSetting: &settings1); |
| 203 | backendQAbstractPhysicalDeviceBackendNode.syncFromFrontEnd(frontEnd: &physicalDeviceNode, firstTime: false); |
| 204 | |
| 205 | // THEN |
| 206 | QCOMPARE(priv->m_axisSettings.size(), 1); |
| 207 | |
| 208 | // WHEN |
| 209 | physicalDeviceNode.addAxisSetting(axisSetting: &settings2); |
| 210 | backendQAbstractPhysicalDeviceBackendNode.syncFromFrontEnd(frontEnd: &physicalDeviceNode, firstTime: false); |
| 211 | |
| 212 | // THEN |
| 213 | QCOMPARE(priv->m_axisSettings.size(), 2); |
| 214 | } |
| 215 | |
| 216 | // Removing AxisSettings |
| 217 | { |
| 218 | // WHEN |
| 219 | physicalDeviceNode.removeAxisSetting(axisSetting: &settings1); |
| 220 | backendQAbstractPhysicalDeviceBackendNode.syncFromFrontEnd(frontEnd: &physicalDeviceNode, firstTime: false); |
| 221 | |
| 222 | // THEN |
| 223 | QCOMPARE(priv->m_axisSettings.size(), 1); |
| 224 | |
| 225 | // WHEN |
| 226 | physicalDeviceNode.removeAxisSetting(axisSetting: &settings2); |
| 227 | backendQAbstractPhysicalDeviceBackendNode.syncFromFrontEnd(frontEnd: &physicalDeviceNode, firstTime: false); |
| 228 | |
| 229 | // THEN |
| 230 | QCOMPARE(priv->m_axisSettings.size(), 0); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | }; |
| 236 | |
| 237 | QTEST_MAIN(tst_QAbstractPhysicalDeviceBackendNode) |
| 238 | |
| 239 | #include "tst_qabstractphysicaldevicebackendnode.moc" |
| 240 | |