| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DInput/qmousedevice.h> |
| 7 | #include <Qt3DInput/private/qmousedevice_p.h> |
| 8 | #include <Qt3DInput/private/mousedevice_p.h> |
| 9 | #include "qbackendnodetester.h" |
| 10 | |
| 11 | class tst_MouseDevice : public Qt3DCore::QBackendNodeTester |
| 12 | { |
| 13 | Q_OBJECT |
| 14 | |
| 15 | private Q_SLOTS: |
| 16 | |
| 17 | void checkInitialState() |
| 18 | { |
| 19 | // GIVEN |
| 20 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
| 21 | |
| 22 | // THEN |
| 23 | QCOMPARE(backendMouseDevice.isEnabled(), false); |
| 24 | QVERIFY(backendMouseDevice.peerId().isNull()); |
| 25 | QVERIFY(backendMouseDevice.inputHandler() == nullptr); |
| 26 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
| 27 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
| 28 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 29 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 30 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
| 31 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 32 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 33 | QCOMPARE(backendMouseDevice.previousPos(), QPointF()); |
| 34 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
| 35 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 36 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
| 37 | } |
| 38 | |
| 39 | void checkInitializeFromPeer() |
| 40 | { |
| 41 | // GIVEN |
| 42 | Qt3DInput::QMouseDevice mouseDevice; |
| 43 | mouseDevice.setSensitivity(0.8f); |
| 44 | |
| 45 | { |
| 46 | // WHEN |
| 47 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
| 48 | simulateInitializationSync(&mouseDevice, &backendMouseDevice); |
| 49 | |
| 50 | // THEN |
| 51 | QCOMPARE(backendMouseDevice.isEnabled(), true); |
| 52 | QCOMPARE(backendMouseDevice.peerId(), mouseDevice.id()); |
| 53 | QVERIFY(backendMouseDevice.inputHandler() == nullptr); |
| 54 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
| 55 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
| 56 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 57 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 58 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
| 59 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 60 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 61 | QCOMPARE(backendMouseDevice.previousPos(), QPointF()); |
| 62 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
| 63 | QCOMPARE(backendMouseDevice.sensitivity(), 0.8f); |
| 64 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
| 65 | } |
| 66 | { |
| 67 | // WHEN |
| 68 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
| 69 | mouseDevice.setEnabled(false); |
| 70 | simulateInitializationSync(&mouseDevice, &backendMouseDevice); |
| 71 | |
| 72 | // THEN |
| 73 | QCOMPARE(backendMouseDevice.peerId(), mouseDevice.id()); |
| 74 | QCOMPARE(backendMouseDevice.isEnabled(), false); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void checkMouseMoveStatesUpdates() |
| 79 | { |
| 80 | // GIVEN |
| 81 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
| 82 | |
| 83 | { |
| 84 | // WHEN |
| 85 | auto event = QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseButtonPress, |
| 86 | QPointF(400.0, 400.0), |
| 87 | QPointF(400.0, 400.0), |
| 88 | QPointF(400.0, 400.0), |
| 89 | Qt::LeftButton, |
| 90 | Qt::LeftButton, |
| 91 | Qt::NoModifier); |
| 92 | backendMouseDevice.resetMouseAxisState(); |
| 93 | backendMouseDevice.updateMouseEvent(&event); |
| 94 | |
| 95 | // THEN |
| 96 | // Note: axis are only modified when moving (> 1 event) |
| 97 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
| 98 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
| 99 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 100 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 101 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, true); |
| 102 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 103 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 104 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(400.0, 400.0)); |
| 105 | QCOMPARE(backendMouseDevice.wasPressed(), true); |
| 106 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 107 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | // WHEN |
| 112 | auto event = QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseMove, |
| 113 | QPointF(600.0, 600.0), |
| 114 | QPointF(600.0, 600.0), |
| 115 | QPointF(600.0, 600.0), |
| 116 | Qt::LeftButton, |
| 117 | Qt::LeftButton, |
| 118 | Qt::NoModifier); |
| 119 | backendMouseDevice.resetMouseAxisState(); |
| 120 | backendMouseDevice.updateMouseEvent(&event); |
| 121 | |
| 122 | // THEN |
| 123 | QCOMPARE(backendMouseDevice.mouseState().xAxis, (600.0f - 400.0f) * 0.1f); |
| 124 | QCOMPARE(backendMouseDevice.mouseState().yAxis, (400.0f - 600.0f) * 0.1f); |
| 125 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 126 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 127 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, true); |
| 128 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 129 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 130 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(600.0, 600.0)); |
| 131 | QCOMPARE(backendMouseDevice.wasPressed(), true); |
| 132 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 133 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | // WHEN |
| 138 | auto event = QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseButtonRelease, |
| 139 | QPointF(800.0, 800.0), |
| 140 | QPointF(800.0, 800.0), |
| 141 | QPointF(800.0, 800.0), |
| 142 | Qt::LeftButton, |
| 143 | Qt::NoButton, |
| 144 | Qt::NoModifier); |
| 145 | backendMouseDevice.resetMouseAxisState(); |
| 146 | backendMouseDevice.updateMouseEvent(&event); |
| 147 | |
| 148 | // THEN |
| 149 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
| 150 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
| 151 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 152 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 153 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
| 154 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 155 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 156 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(800.0, 800.0)); |
| 157 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
| 158 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 159 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
| 160 | } |
| 161 | |
| 162 | { |
| 163 | // WHEN |
| 164 | auto event = QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseMove, |
| 165 | QPointF(900.0, 900.0), |
| 166 | QPointF(900.0, 900.0), |
| 167 | QPointF(900.0, 900.0), |
| 168 | Qt::NoButton, |
| 169 | Qt::NoButton, |
| 170 | Qt::NoModifier); |
| 171 | |
| 172 | // THEN -> no axes update |
| 173 | backendMouseDevice.updateMouseEvent(&event); |
| 174 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
| 175 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
| 176 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 177 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 178 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
| 179 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 180 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 181 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(900.0, 900.0)); |
| 182 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
| 183 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 184 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
| 185 | } |
| 186 | |
| 187 | { |
| 188 | // WHEN |
| 189 | auto event = QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseMove, |
| 190 | QPointF(1000.0, 1000.0), |
| 191 | QPointF(1000.0, 1000.0), |
| 192 | QPointF(1000.0, 1000.0), |
| 193 | Qt::NoButton, |
| 194 | Qt::NoButton, |
| 195 | Qt::NoModifier); |
| 196 | |
| 197 | Qt3DInput::QMouseDevice mouseDevice; |
| 198 | mouseDevice.setUpdateAxesContinuously(true); |
| 199 | backendMouseDevice.syncFromFrontEnd(&mouseDevice, false); |
| 200 | backendMouseDevice.resetMouseAxisState(); |
| 201 | backendMouseDevice.updateMouseEvent(&event); |
| 202 | |
| 203 | // THEN |
| 204 | QCOMPARE(backendMouseDevice.mouseState().xAxis, (1000.0f - 900.0f) * 0.1f); |
| 205 | QCOMPARE(backendMouseDevice.mouseState().yAxis, (900.0f - 1000.0f) * 0.1f); |
| 206 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 207 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 208 | QCOMPARE(backendMouseDevice.mouseState().leftPressed,false); |
| 209 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
| 210 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
| 211 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(1000.0, 1000.0)); |
| 212 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
| 213 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 214 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), true); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #if QT_CONFIG(wheelevent) |
| 219 | void checkMouseWheelState() |
| 220 | { |
| 221 | // GIVEN |
| 222 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
| 223 | |
| 224 | { |
| 225 | // WHEN |
| 226 | auto event = QT_PREPEND_NAMESPACE(QWheelEvent)(QPointF(500.0, 500.0), |
| 227 | QPointF(), QPoint(), |
| 228 | QPoint(0, 120), |
| 229 | Qt::NoButton, |
| 230 | Qt::NoModifier, |
| 231 | Qt::NoScrollPhase, |
| 232 | false); |
| 233 | backendMouseDevice.resetMouseAxisState(); |
| 234 | backendMouseDevice.updateWheelEvent(&event); |
| 235 | |
| 236 | // THEN |
| 237 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 238 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.1f * 120); |
| 239 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 240 | } |
| 241 | |
| 242 | { |
| 243 | // WHEN |
| 244 | auto event = QT_PREPEND_NAMESPACE(QWheelEvent)(QPointF(500.0, 500.0), |
| 245 | QPointF(), QPoint(), |
| 246 | QPoint(120, 0), |
| 247 | Qt::NoButton, |
| 248 | Qt::NoModifier, |
| 249 | Qt::NoScrollPhase, |
| 250 | false); |
| 251 | backendMouseDevice.resetMouseAxisState(); |
| 252 | backendMouseDevice.updateWheelEvent(&event); |
| 253 | |
| 254 | // THEN |
| 255 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.1f * 120); |
| 256 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 257 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 258 | } |
| 259 | |
| 260 | { |
| 261 | // WHEN |
| 262 | auto event = QT_PREPEND_NAMESPACE(QWheelEvent)(QPointF(500.0, 500.0), |
| 263 | QPointF(), QPoint(), QPoint(), |
| 264 | Qt::NoButton, |
| 265 | Qt::NoModifier, |
| 266 | Qt::NoScrollPhase, |
| 267 | false); |
| 268 | backendMouseDevice.resetMouseAxisState(); |
| 269 | backendMouseDevice.updateWheelEvent(&event); |
| 270 | |
| 271 | // THEN |
| 272 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
| 273 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
| 274 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
| 275 | } |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | void checkSceneChangeEvents() |
| 280 | { |
| 281 | // GIVEN |
| 282 | Qt3DInput::QMouseDevice mouseDevice; |
| 283 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
| 284 | simulateInitializationSync(&mouseDevice, &backendMouseDevice); |
| 285 | |
| 286 | { |
| 287 | // WHEN |
| 288 | const bool newValue = false; |
| 289 | mouseDevice.setEnabled(newValue); |
| 290 | backendMouseDevice.syncFromFrontEnd(&mouseDevice, false); |
| 291 | |
| 292 | // THEN |
| 293 | QCOMPARE(backendMouseDevice.isEnabled(), newValue); |
| 294 | } |
| 295 | { |
| 296 | // WHEN |
| 297 | const float newValue = 99.0f; |
| 298 | mouseDevice.setSensitivity(newValue); |
| 299 | backendMouseDevice.syncFromFrontEnd(&mouseDevice, false); |
| 300 | |
| 301 | // THEN |
| 302 | QCOMPARE(backendMouseDevice.sensitivity(), newValue); |
| 303 | } |
| 304 | { |
| 305 | // WHEN |
| 306 | const bool newValue = true; |
| 307 | mouseDevice.setUpdateAxesContinuously(newValue); |
| 308 | backendMouseDevice.syncFromFrontEnd(&mouseDevice, false); |
| 309 | |
| 310 | // THEN |
| 311 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), newValue); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | }; |
| 316 | |
| 317 | QTEST_MAIN(tst_MouseDevice) |
| 318 | |
| 319 | #include "tst_mousedevice.moc" |