| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <qbackendnodetester.h> |
| 6 | #include "testdevice.h" |
| 7 | |
| 8 | #include <Qt3DInput/private/actioninput_p.h> |
| 9 | #include <Qt3DInput/private/inputhandler_p.h> |
| 10 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 11 | #include <Qt3DInput/private/inputsequence_p.h> |
| 12 | #include <Qt3DInput/QActionInput> |
| 13 | #include <Qt3DInput/QInputSequence> |
| 14 | |
| 15 | class tst_InputSequence : public Qt3DCore::QBackendNodeTester |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | private Q_SLOTS: |
| 19 | void shouldMirrorPeerProperties() |
| 20 | { |
| 21 | // GIVEN |
| 22 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 23 | Qt3DInput::QInputSequence inputSequence; |
| 24 | Qt3DInput::QActionInput actionInput; |
| 25 | |
| 26 | inputSequence.setTimeout(250); |
| 27 | inputSequence.setButtonInterval(100); |
| 28 | inputSequence.addSequence(&actionInput); |
| 29 | |
| 30 | // WHEN |
| 31 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 32 | |
| 33 | // THEN |
| 34 | QCOMPARE(backendInputSequence.peerId(), inputSequence.id()); |
| 35 | QCOMPARE(backendInputSequence.isEnabled(), inputSequence.isEnabled()); |
| 36 | QCOMPARE(backendInputSequence.timeout(), inputSequence.timeout() * 1000000); |
| 37 | QCOMPARE(backendInputSequence.buttonInterval(), inputSequence.buttonInterval() * 1000000); |
| 38 | QCOMPARE(backendInputSequence.sequences().size(), inputSequence.sequences().size()); |
| 39 | |
| 40 | const int inputsCount = backendInputSequence.sequences().size(); |
| 41 | if (inputsCount > 0) { |
| 42 | for (int i = 0; i < inputsCount; ++i) |
| 43 | QCOMPARE(backendInputSequence.sequences().at(i), inputSequence.sequences().at(i)->id()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void shouldHaveInitialAndCleanedUpStates() |
| 48 | { |
| 49 | // GIVEN |
| 50 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 51 | |
| 52 | // THEN |
| 53 | QVERIFY(backendInputSequence.peerId().isNull()); |
| 54 | QCOMPARE(backendInputSequence.isEnabled(), false); |
| 55 | QCOMPARE(backendInputSequence.timeout(), 0); |
| 56 | QCOMPARE(backendInputSequence.buttonInterval(), 0); |
| 57 | QCOMPARE(backendInputSequence.sequences().size(), 0); |
| 58 | |
| 59 | // GIVEN |
| 60 | Qt3DInput::QInputSequence inputSequence; |
| 61 | Qt3DInput::QActionInput actionInput; |
| 62 | |
| 63 | inputSequence.setTimeout(250); |
| 64 | inputSequence.setButtonInterval(100); |
| 65 | inputSequence.addSequence(&actionInput); |
| 66 | |
| 67 | // WHEN |
| 68 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 69 | backendInputSequence.cleanup(); |
| 70 | |
| 71 | // THEN |
| 72 | QCOMPARE(backendInputSequence.isEnabled(), false); |
| 73 | QCOMPARE(backendInputSequence.timeout(), 0); |
| 74 | QCOMPARE(backendInputSequence.buttonInterval(), 0); |
| 75 | QCOMPARE(backendInputSequence.sequences().size(), 0); |
| 76 | } |
| 77 | |
| 78 | void shouldHandlePropertyChanges() |
| 79 | { |
| 80 | // GIVEN |
| 81 | Qt3DInput::QInputSequence inputSequence; |
| 82 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 83 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 84 | |
| 85 | // WHEN |
| 86 | inputSequence.setTimeout(250); |
| 87 | backendInputSequence.syncFromFrontEnd(&inputSequence, false); |
| 88 | |
| 89 | // THEN |
| 90 | QCOMPARE(backendInputSequence.timeout(), 250000000); |
| 91 | |
| 92 | // WHEN |
| 93 | inputSequence.setButtonInterval(150); |
| 94 | backendInputSequence.syncFromFrontEnd(&inputSequence, false); |
| 95 | |
| 96 | // THEN |
| 97 | QCOMPARE(backendInputSequence.buttonInterval(), 150000000); |
| 98 | |
| 99 | // WHEN |
| 100 | inputSequence.setEnabled(false); |
| 101 | backendInputSequence.syncFromFrontEnd(&inputSequence, false); |
| 102 | |
| 103 | // THEN |
| 104 | QCOMPARE(backendInputSequence.isEnabled(), false); |
| 105 | |
| 106 | // WHEN |
| 107 | Qt3DInput::QActionInput input; |
| 108 | const Qt3DCore::QNodeId inputId = input.id(); |
| 109 | inputSequence.addSequence(&input); |
| 110 | backendInputSequence.syncFromFrontEnd(&inputSequence, false); |
| 111 | |
| 112 | // THEN |
| 113 | QCOMPARE(backendInputSequence.sequences().size(), 1); |
| 114 | QCOMPARE(backendInputSequence.sequences().first(), inputId); |
| 115 | |
| 116 | // WHEN |
| 117 | inputSequence.removeSequence(&input); |
| 118 | backendInputSequence.syncFromFrontEnd(&inputSequence, false); |
| 119 | |
| 120 | // THEN |
| 121 | QCOMPARE(backendInputSequence.sequences().size(), 0); |
| 122 | } |
| 123 | |
| 124 | void shouldActivateWhenSequenceIsConsumedInOrderOnly() |
| 125 | { |
| 126 | // GIVEN |
| 127 | TestDeviceIntegration deviceIntegration; |
| 128 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 129 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 130 | Qt3DInput::Input::InputHandler handler; |
| 131 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 132 | |
| 133 | auto firstInput = new Qt3DInput::QActionInput; |
| 134 | firstInput->setButtons(QList<int> { Qt::Key_Q, Qt::Key_A }); |
| 135 | firstInput->setSourceDevice(device); |
| 136 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 137 | simulateInitializationSync(firstInput, backendFirstInput); |
| 138 | |
| 139 | auto secondInput = new Qt3DInput::QActionInput; |
| 140 | secondInput->setButtons(QList<int> { Qt::Key_S, Qt::Key_W }); |
| 141 | secondInput->setSourceDevice(device); |
| 142 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 143 | simulateInitializationSync(secondInput, backendSecondInput); |
| 144 | |
| 145 | auto thirdInput = new Qt3DInput::QActionInput; |
| 146 | thirdInput->setButtons(QList<int> { Qt::Key_D, Qt::Key_E }); |
| 147 | thirdInput->setSourceDevice(device); |
| 148 | auto backendThirdInput = handler.actionInputManager()->getOrCreateResource(thirdInput->id()); |
| 149 | simulateInitializationSync(thirdInput, backendThirdInput); |
| 150 | |
| 151 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 152 | Qt3DInput::QInputSequence inputSequence; |
| 153 | inputSequence.setEnabled(true); |
| 154 | inputSequence.setButtonInterval(150); |
| 155 | inputSequence.setTimeout(450); |
| 156 | inputSequence.addSequence(firstInput); |
| 157 | inputSequence.addSequence(secondInput); |
| 158 | inputSequence.addSequence(thirdInput); |
| 159 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 160 | |
| 161 | // WHEN |
| 162 | deviceBackend->setButtonPressed(Qt::Key_Up, true); |
| 163 | |
| 164 | // THEN |
| 165 | QCOMPARE(backendInputSequence.process(&handler, 1000000000), false); |
| 166 | |
| 167 | // WHEN |
| 168 | deviceBackend->setButtonPressed(Qt::Key_Up, false); |
| 169 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 170 | |
| 171 | // THEN |
| 172 | QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); |
| 173 | |
| 174 | // WHEN |
| 175 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 176 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 177 | |
| 178 | // THEN |
| 179 | QCOMPARE(backendInputSequence.process(&handler, 1200000000), false); |
| 180 | |
| 181 | // WHEN |
| 182 | deviceBackend->setButtonPressed(Qt::Key_S, false); |
| 183 | deviceBackend->setButtonPressed(Qt::Key_E, true); |
| 184 | |
| 185 | // THEN |
| 186 | QCOMPARE(backendInputSequence.process(&handler, 1300000000), true); |
| 187 | |
| 188 | // WHEN |
| 189 | deviceBackend->setButtonPressed(Qt::Key_E, false); |
| 190 | |
| 191 | // THEN |
| 192 | QCOMPARE(backendInputSequence.process(&handler, 1400000000), false); |
| 193 | |
| 194 | // WHEN |
| 195 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 196 | |
| 197 | // THEN |
| 198 | QCOMPARE(backendInputSequence.process(&handler, 1500000000), false); |
| 199 | |
| 200 | // WHEN |
| 201 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 202 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 203 | |
| 204 | // THEN |
| 205 | QCOMPARE(backendInputSequence.process(&handler, 1600000000), false); |
| 206 | |
| 207 | // WHEN |
| 208 | deviceBackend->setButtonPressed(Qt::Key_S, false); |
| 209 | deviceBackend->setButtonPressed(Qt::Key_E, true); |
| 210 | |
| 211 | // THEN |
| 212 | QCOMPARE(backendInputSequence.process(&handler, 1700000000), true); |
| 213 | |
| 214 | // WHEN |
| 215 | deviceBackend->setButtonPressed(Qt::Key_E, false); |
| 216 | |
| 217 | // THEN |
| 218 | QCOMPARE(backendInputSequence.process(&handler, 1800000000), false); |
| 219 | |
| 220 | |
| 221 | // Now out of order |
| 222 | |
| 223 | // WHEN |
| 224 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 225 | |
| 226 | // THEN |
| 227 | QCOMPARE(backendInputSequence.process(&handler, 1900000000), false); |
| 228 | |
| 229 | // WHEN |
| 230 | deviceBackend->setButtonPressed(Qt::Key_S, false); |
| 231 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 232 | |
| 233 | // THEN |
| 234 | QCOMPARE(backendInputSequence.process(&handler, 2000000000), false); |
| 235 | |
| 236 | // WHEN |
| 237 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 238 | deviceBackend->setButtonPressed(Qt::Key_D, true); |
| 239 | |
| 240 | // THEN |
| 241 | QCOMPARE(backendInputSequence.process(&handler, 2100000000), false); |
| 242 | |
| 243 | // WHEN |
| 244 | deviceBackend->setButtonPressed(Qt::Key_D, false); |
| 245 | |
| 246 | // THEN |
| 247 | QCOMPARE(backendInputSequence.process(&handler, 22000000000), false); |
| 248 | } |
| 249 | |
| 250 | void shouldRespectSequenceTimeout() |
| 251 | { |
| 252 | // GIVEN |
| 253 | TestDeviceIntegration deviceIntegration; |
| 254 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 255 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 256 | Qt3DInput::Input::InputHandler handler; |
| 257 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 258 | |
| 259 | auto firstInput = new Qt3DInput::QActionInput; |
| 260 | firstInput->setButtons(QList<int> { Qt::Key_Q, Qt::Key_A }); |
| 261 | firstInput->setSourceDevice(device); |
| 262 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 263 | simulateInitializationSync(firstInput, backendFirstInput); |
| 264 | |
| 265 | auto secondInput = new Qt3DInput::QActionInput; |
| 266 | secondInput->setButtons(QList<int> { Qt::Key_S, Qt::Key_W }); |
| 267 | secondInput->setSourceDevice(device); |
| 268 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 269 | simulateInitializationSync(secondInput, backendSecondInput); |
| 270 | |
| 271 | auto thirdInput = new Qt3DInput::QActionInput; |
| 272 | thirdInput->setButtons(QList<int> { Qt::Key_D, Qt::Key_E }); |
| 273 | thirdInput->setSourceDevice(device); |
| 274 | auto backendThirdInput = handler.actionInputManager()->getOrCreateResource(thirdInput->id()); |
| 275 | simulateInitializationSync(thirdInput, backendThirdInput); |
| 276 | |
| 277 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 278 | Qt3DInput::QInputSequence inputSequence; |
| 279 | inputSequence.setEnabled(true); |
| 280 | inputSequence.setButtonInterval(250); |
| 281 | inputSequence.setTimeout(450); |
| 282 | inputSequence.addSequence(firstInput); |
| 283 | inputSequence.addSequence(secondInput); |
| 284 | inputSequence.addSequence(thirdInput); |
| 285 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 286 | |
| 287 | // WHEN |
| 288 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 289 | |
| 290 | // THEN |
| 291 | QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); |
| 292 | |
| 293 | // WHEN |
| 294 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 295 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 296 | |
| 297 | // THEN |
| 298 | QCOMPARE(backendInputSequence.process(&handler, 1300000000), false); |
| 299 | |
| 300 | // WHEN |
| 301 | deviceBackend->setButtonPressed(Qt::Key_S, false); |
| 302 | deviceBackend->setButtonPressed(Qt::Key_E, true); |
| 303 | |
| 304 | // THEN |
| 305 | QCOMPARE(backendInputSequence.process(&handler, 1600000000), false); // Too late |
| 306 | } |
| 307 | |
| 308 | void shouldRespectSequenceButtonInterval() |
| 309 | { |
| 310 | // GIVEN |
| 311 | TestDeviceIntegration deviceIntegration; |
| 312 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 313 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 314 | Qt3DInput::Input::InputHandler handler; |
| 315 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 316 | |
| 317 | auto firstInput = new Qt3DInput::QActionInput; |
| 318 | firstInput->setButtons(QList<int> { Qt::Key_Q, Qt::Key_A }); |
| 319 | firstInput->setSourceDevice(device); |
| 320 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 321 | simulateInitializationSync(firstInput, backendFirstInput); |
| 322 | |
| 323 | auto secondInput = new Qt3DInput::QActionInput; |
| 324 | secondInput->setButtons(QList<int> { Qt::Key_S, Qt::Key_W }); |
| 325 | secondInput->setSourceDevice(device); |
| 326 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 327 | simulateInitializationSync(secondInput, backendSecondInput); |
| 328 | |
| 329 | auto thirdInput = new Qt3DInput::QActionInput; |
| 330 | thirdInput->setButtons(QList<int> { Qt::Key_D, Qt::Key_E }); |
| 331 | thirdInput->setSourceDevice(device); |
| 332 | auto backendThirdInput = handler.actionInputManager()->getOrCreateResource(thirdInput->id()); |
| 333 | simulateInitializationSync(thirdInput, backendThirdInput); |
| 334 | |
| 335 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 336 | Qt3DInput::QInputSequence inputSequence; |
| 337 | inputSequence.setEnabled(true); |
| 338 | inputSequence.setButtonInterval(100); |
| 339 | inputSequence.setTimeout(450); |
| 340 | inputSequence.addSequence(firstInput); |
| 341 | inputSequence.addSequence(secondInput); |
| 342 | inputSequence.addSequence(thirdInput); |
| 343 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 344 | |
| 345 | // WHEN |
| 346 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 347 | |
| 348 | // THEN |
| 349 | QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); |
| 350 | |
| 351 | // WHEN |
| 352 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 353 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 354 | |
| 355 | // THEN |
| 356 | QCOMPARE(backendInputSequence.process(&handler, 1250000000), false); // Too late |
| 357 | |
| 358 | // WHEN |
| 359 | deviceBackend->setButtonPressed(Qt::Key_S, false); |
| 360 | deviceBackend->setButtonPressed(Qt::Key_E, true); |
| 361 | |
| 362 | // THEN |
| 363 | QCOMPARE(backendInputSequence.process(&handler, 1300000000), false); |
| 364 | } |
| 365 | |
| 366 | void shouldNotProcessWhenDisabled() |
| 367 | { |
| 368 | // GIVEN |
| 369 | TestDeviceIntegration deviceIntegration; |
| 370 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 371 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 372 | Qt3DInput::Input::InputHandler handler; |
| 373 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 374 | |
| 375 | auto firstInput = new Qt3DInput::QActionInput; |
| 376 | firstInput->setButtons(QList<int> { Qt::Key_Q }); |
| 377 | firstInput->setSourceDevice(device); |
| 378 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 379 | simulateInitializationSync(firstInput, backendFirstInput); |
| 380 | |
| 381 | auto secondInput = new Qt3DInput::QActionInput; |
| 382 | secondInput->setButtons(QList<int> { Qt::Key_S }); |
| 383 | secondInput->setSourceDevice(device); |
| 384 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 385 | simulateInitializationSync(secondInput, backendSecondInput); |
| 386 | |
| 387 | Qt3DInput::Input::InputSequence backendInputSequence; |
| 388 | Qt3DInput::QInputSequence inputSequence; |
| 389 | inputSequence.setEnabled(false); |
| 390 | inputSequence.setButtonInterval(150); |
| 391 | inputSequence.setTimeout(450); |
| 392 | inputSequence.addSequence(firstInput); |
| 393 | inputSequence.addSequence(secondInput); |
| 394 | simulateInitializationSync(&inputSequence, &backendInputSequence); |
| 395 | |
| 396 | // WHEN |
| 397 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 398 | |
| 399 | // THEN |
| 400 | QCOMPARE(backendInputSequence.process(&handler, 1000000000), false); |
| 401 | |
| 402 | // WHEN |
| 403 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 404 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 405 | |
| 406 | // THEN |
| 407 | QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); |
| 408 | } |
| 409 | }; |
| 410 | |
| 411 | QTEST_APPLESS_MAIN(tst_InputSequence) |
| 412 | |
| 413 | #include "tst_inputsequence.moc" |