| 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/inputchord_p.h> |
| 10 | #include <Qt3DInput/private/inputhandler_p.h> |
| 11 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 12 | #include <Qt3DInput/QActionInput> |
| 13 | #include <Qt3DInput/QInputChord> |
| 14 | |
| 15 | class tst_InputChord : public Qt3DCore::QBackendNodeTester |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | private Q_SLOTS: |
| 19 | void shouldMirrorPeerProperties() |
| 20 | { |
| 21 | // GIVEN |
| 22 | Qt3DInput::Input::InputChord backendInputChord; |
| 23 | Qt3DInput::QInputChord inputChord; |
| 24 | Qt3DInput::QActionInput actionInput; |
| 25 | |
| 26 | inputChord.setTimeout(250); |
| 27 | inputChord.addChord(&actionInput); |
| 28 | |
| 29 | // WHEN |
| 30 | simulateInitializationSync(&inputChord, &backendInputChord); |
| 31 | |
| 32 | // THEN |
| 33 | QCOMPARE(backendInputChord.peerId(), inputChord.id()); |
| 34 | QCOMPARE(backendInputChord.isEnabled(), inputChord.isEnabled()); |
| 35 | QCOMPARE(backendInputChord.timeout(), inputChord.timeout() * 1000000); |
| 36 | QCOMPARE(backendInputChord.chords().size(), inputChord.chords().size()); |
| 37 | |
| 38 | const int inputsCount = backendInputChord.chords().size(); |
| 39 | if (inputsCount > 0) { |
| 40 | for (int i = 0; i < inputsCount; ++i) |
| 41 | QCOMPARE(backendInputChord.chords().at(i), inputChord.chords().at(i)->id()); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void shouldHaveInitialAndCleanedUpStates() |
| 46 | { |
| 47 | // GIVEN |
| 48 | Qt3DInput::Input::InputChord backendInputChord; |
| 49 | |
| 50 | // THEN |
| 51 | QVERIFY(backendInputChord.peerId().isNull()); |
| 52 | QCOMPARE(backendInputChord.isEnabled(), false); |
| 53 | QCOMPARE(backendInputChord.timeout(), 0); |
| 54 | QCOMPARE(backendInputChord.chords().size(), 0); |
| 55 | |
| 56 | // GIVEN |
| 57 | Qt3DInput::QInputChord inputChord; |
| 58 | Qt3DInput::QActionInput actionInput; |
| 59 | |
| 60 | inputChord.setTimeout(250); |
| 61 | inputChord.addChord(&actionInput); |
| 62 | |
| 63 | // WHEN |
| 64 | simulateInitializationSync(&inputChord, &backendInputChord); |
| 65 | backendInputChord.cleanup(); |
| 66 | |
| 67 | // THEN |
| 68 | QCOMPARE(backendInputChord.isEnabled(), false); |
| 69 | QCOMPARE(backendInputChord.timeout(), 0); |
| 70 | QCOMPARE(backendInputChord.chords().size(), 0); |
| 71 | } |
| 72 | |
| 73 | void shouldHandlePropertyChanges() |
| 74 | { |
| 75 | // GIVEN |
| 76 | Qt3DInput::QInputChord inputChord; |
| 77 | Qt3DInput::Input::InputChord backendInputChord; |
| 78 | simulateInitializationSync(&inputChord, &backendInputChord); |
| 79 | |
| 80 | // WHEN |
| 81 | inputChord.setTimeout(250); |
| 82 | backendInputChord.syncFromFrontEnd(&inputChord, false); |
| 83 | |
| 84 | // THEN |
| 85 | QCOMPARE(backendInputChord.timeout(), 250000000); |
| 86 | |
| 87 | // WHEN |
| 88 | inputChord.setEnabled(false); |
| 89 | backendInputChord.syncFromFrontEnd(&inputChord, false); |
| 90 | |
| 91 | // THEN |
| 92 | QCOMPARE(backendInputChord.isEnabled(), false); |
| 93 | |
| 94 | // WHEN |
| 95 | Qt3DInput::QActionInput input; |
| 96 | const Qt3DCore::QNodeId inputId = input.id(); |
| 97 | inputChord.addChord(&input); |
| 98 | backendInputChord.syncFromFrontEnd(&inputChord, false); |
| 99 | |
| 100 | // THEN |
| 101 | QCOMPARE(backendInputChord.chords().size(), 1); |
| 102 | QCOMPARE(backendInputChord.chords().first(), inputId); |
| 103 | |
| 104 | // WHEN |
| 105 | inputChord.removeChord(&input); |
| 106 | backendInputChord.syncFromFrontEnd(&inputChord, false); |
| 107 | |
| 108 | // THEN |
| 109 | QCOMPARE(backendInputChord.chords().size(), 0); |
| 110 | } |
| 111 | |
| 112 | void shouldActivateWhenAllArePressed() |
| 113 | { |
| 114 | // GIVEN |
| 115 | TestDeviceIntegration deviceIntegration; |
| 116 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 117 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 118 | Qt3DInput::Input::InputHandler handler; |
| 119 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 120 | |
| 121 | auto firstInput = new Qt3DInput::QActionInput; |
| 122 | firstInput->setButtons(QList<int> { Qt::Key_Q, Qt::Key_W }); |
| 123 | firstInput->setSourceDevice(device); |
| 124 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 125 | simulateInitializationSync(firstInput, backendFirstInput); |
| 126 | |
| 127 | auto secondInput = new Qt3DInput::QActionInput; |
| 128 | secondInput->setButtons(QList<int> { Qt::Key_A, Qt::Key_S }); |
| 129 | secondInput->setSourceDevice(device); |
| 130 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 131 | simulateInitializationSync(secondInput, backendSecondInput); |
| 132 | |
| 133 | Qt3DInput::Input::InputChord backendInputChord; |
| 134 | Qt3DInput::QInputChord inputChord; |
| 135 | inputChord.setEnabled(true); |
| 136 | inputChord.setTimeout(300); |
| 137 | inputChord.addChord(firstInput); |
| 138 | inputChord.addChord(secondInput); |
| 139 | simulateInitializationSync(&inputChord, &backendInputChord); |
| 140 | |
| 141 | // WHEN |
| 142 | deviceBackend->setButtonPressed(Qt::Key_Up, true); |
| 143 | |
| 144 | // THEN |
| 145 | QCOMPARE(backendInputChord.process(&handler, 1000000000), false); |
| 146 | |
| 147 | // WHEN |
| 148 | deviceBackend->setButtonPressed(Qt::Key_Up, false); |
| 149 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 150 | |
| 151 | // THEN |
| 152 | QCOMPARE(backendInputChord.process(&handler, 1100000000), false); |
| 153 | |
| 154 | // WHEN |
| 155 | deviceBackend->setButtonPressed(Qt::Key_A, true); |
| 156 | |
| 157 | // THEN |
| 158 | QCOMPARE(backendInputChord.process(&handler, 1200000000), true); |
| 159 | |
| 160 | // WHEN |
| 161 | deviceBackend->setButtonPressed(Qt::Key_S, true); |
| 162 | |
| 163 | // THEN |
| 164 | QCOMPARE(backendInputChord.process(&handler, 1300000000), true); |
| 165 | |
| 166 | // WHEN |
| 167 | deviceBackend->setButtonPressed(Qt::Key_W, true); |
| 168 | |
| 169 | // THEN |
| 170 | QCOMPARE(backendInputChord.process(&handler, 1400000000), true); |
| 171 | |
| 172 | // WHEN |
| 173 | deviceBackend->setButtonPressed(Qt::Key_W, false); |
| 174 | |
| 175 | // THEN |
| 176 | QCOMPARE(backendInputChord.process(&handler, 1500000000), true); |
| 177 | |
| 178 | // THEN |
| 179 | QCOMPARE(backendInputChord.process(&handler, 1600000000), true); |
| 180 | |
| 181 | // THEN |
| 182 | QCOMPARE(backendInputChord.process(&handler, 1700000000), true); |
| 183 | |
| 184 | // THEN |
| 185 | QCOMPARE(backendInputChord.process(&handler, 1800000000), true); |
| 186 | |
| 187 | // THEN |
| 188 | QCOMPARE(backendInputChord.process(&handler, 1900000000), true); |
| 189 | |
| 190 | // WHEN |
| 191 | deviceBackend->setButtonPressed(Qt::Key_Q, false); |
| 192 | |
| 193 | // THEN |
| 194 | QCOMPARE(backendInputChord.process(&handler, 2000000000), false); |
| 195 | |
| 196 | // THEN |
| 197 | QCOMPARE(backendInputChord.process(&handler, 2100000000), false); |
| 198 | } |
| 199 | |
| 200 | void shouldRespectChordTimeout() |
| 201 | { |
| 202 | // GIVEN |
| 203 | TestDeviceIntegration deviceIntegration; |
| 204 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 205 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 206 | Qt3DInput::Input::InputHandler handler; |
| 207 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 208 | |
| 209 | auto firstInput = new Qt3DInput::QActionInput; |
| 210 | firstInput->setButtons(QList<int> { Qt::Key_Q }); |
| 211 | firstInput->setSourceDevice(device); |
| 212 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 213 | simulateInitializationSync(firstInput, backendFirstInput); |
| 214 | |
| 215 | auto secondInput = new Qt3DInput::QActionInput; |
| 216 | secondInput->setButtons(QList<int> { Qt::Key_W }); |
| 217 | secondInput->setSourceDevice(device); |
| 218 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 219 | simulateInitializationSync(secondInput, backendSecondInput); |
| 220 | |
| 221 | Qt3DInput::Input::InputChord backendInputChord; |
| 222 | Qt3DInput::QInputChord inputChord; |
| 223 | inputChord.setEnabled(true); |
| 224 | inputChord.setTimeout(300); |
| 225 | inputChord.addChord(firstInput); |
| 226 | inputChord.addChord(secondInput); |
| 227 | simulateInitializationSync(&inputChord, &backendInputChord); |
| 228 | |
| 229 | // WHEN |
| 230 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 231 | |
| 232 | // THEN |
| 233 | QCOMPARE(backendInputChord.process(&handler, 1000000000), false); |
| 234 | |
| 235 | // WHEN |
| 236 | deviceBackend->setButtonPressed(Qt::Key_W, true); |
| 237 | |
| 238 | // THEN |
| 239 | QCOMPARE(backendInputChord.process(&handler, 1400000000), false); // Too late |
| 240 | |
| 241 | // THEN |
| 242 | QCOMPARE(backendInputChord.process(&handler, 1600000000), false); |
| 243 | |
| 244 | // THEN |
| 245 | QCOMPARE(backendInputChord.process(&handler, 1800000000), false); |
| 246 | } |
| 247 | |
| 248 | void shouldNotProcessWhenDisabled() |
| 249 | { |
| 250 | // GIVEN |
| 251 | TestDeviceIntegration deviceIntegration; |
| 252 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 253 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 254 | Qt3DInput::Input::InputHandler handler; |
| 255 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 256 | |
| 257 | auto firstInput = new Qt3DInput::QActionInput; |
| 258 | firstInput->setButtons(QList<int> { Qt::Key_Q }); |
| 259 | firstInput->setSourceDevice(device); |
| 260 | auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); |
| 261 | simulateInitializationSync(firstInput, backendFirstInput); |
| 262 | |
| 263 | auto secondInput = new Qt3DInput::QActionInput; |
| 264 | secondInput->setButtons(QList<int> { Qt::Key_W }); |
| 265 | secondInput->setSourceDevice(device); |
| 266 | auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); |
| 267 | simulateInitializationSync(secondInput, backendSecondInput); |
| 268 | |
| 269 | Qt3DInput::Input::InputChord backendInputChord; |
| 270 | Qt3DInput::QInputChord inputChord; |
| 271 | inputChord.setEnabled(false); |
| 272 | inputChord.setTimeout(300); |
| 273 | inputChord.addChord(firstInput); |
| 274 | inputChord.addChord(secondInput); |
| 275 | simulateInitializationSync(&inputChord, &backendInputChord); |
| 276 | |
| 277 | // WHEN |
| 278 | deviceBackend->setButtonPressed(Qt::Key_Q, true); |
| 279 | deviceBackend->setButtonPressed(Qt::Key_W, true); |
| 280 | |
| 281 | // THEN |
| 282 | QCOMPARE(backendInputChord.process(&handler, 1000000000), false); |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | QTEST_APPLESS_MAIN(tst_InputChord) |
| 287 | |
| 288 | #include "tst_inputchord.moc" |