| 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/QActionInput> |
| 11 | |
| 12 | class tst_ActionInput : public Qt3DCore::QBackendNodeTester |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | private Q_SLOTS: |
| 16 | void shouldMirrorPeerProperties() |
| 17 | { |
| 18 | // GIVEN |
| 19 | Qt3DInput::Input::ActionInput backendActionInput; |
| 20 | Qt3DInput::QActionInput actionInput; |
| 21 | TestDevice sourceDevice; |
| 22 | |
| 23 | actionInput.setButtons(QList<int> { 1 << 8 }); |
| 24 | actionInput.setSourceDevice(&sourceDevice); |
| 25 | |
| 26 | // WHEN |
| 27 | simulateInitializationSync(&actionInput, &backendActionInput); |
| 28 | |
| 29 | // THEN |
| 30 | QCOMPARE(backendActionInput.peerId(), actionInput.id()); |
| 31 | QCOMPARE(backendActionInput.isEnabled(), actionInput.isEnabled()); |
| 32 | QCOMPARE(backendActionInput.buttons(), actionInput.buttons()); |
| 33 | QCOMPARE(backendActionInput.sourceDevice(), sourceDevice.id()); |
| 34 | } |
| 35 | |
| 36 | void shouldHaveInitialAndCleanedUpStates() |
| 37 | { |
| 38 | // GIVEN |
| 39 | Qt3DInput::Input::ActionInput backendActionInput; |
| 40 | |
| 41 | // THEN |
| 42 | QVERIFY(backendActionInput.peerId().isNull()); |
| 43 | QVERIFY(backendActionInput.buttons().isEmpty()); |
| 44 | QCOMPARE(backendActionInput.isEnabled(), false); |
| 45 | QCOMPARE(backendActionInput.sourceDevice(), Qt3DCore::QNodeId()); |
| 46 | |
| 47 | // GIVEN |
| 48 | Qt3DInput::QActionInput actionInput; |
| 49 | TestDevice sourceDevice; |
| 50 | |
| 51 | actionInput.setButtons(QList<int> { 1 << 8 }); |
| 52 | actionInput.setSourceDevice(&sourceDevice); |
| 53 | |
| 54 | // WHEN |
| 55 | simulateInitializationSync(&actionInput, &backendActionInput); |
| 56 | backendActionInput.cleanup(); |
| 57 | |
| 58 | // THEN |
| 59 | QVERIFY(backendActionInput.buttons().isEmpty()); |
| 60 | QCOMPARE(backendActionInput.isEnabled(), false); |
| 61 | QCOMPARE(backendActionInput.sourceDevice(), Qt3DCore::QNodeId()); |
| 62 | } |
| 63 | |
| 64 | void shouldHandlePropertyChanges() |
| 65 | { |
| 66 | // GIVEN |
| 67 | Qt3DInput::QActionInput actionInput; |
| 68 | Qt3DInput::Input::ActionInput backendActionInput; |
| 69 | simulateInitializationSync(&actionInput, &backendActionInput); |
| 70 | |
| 71 | // WHEN |
| 72 | actionInput.setButtons(QList<int> { 64 }); |
| 73 | backendActionInput.syncFromFrontEnd(&actionInput, false); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(backendActionInput.buttons(), QList<int> { 64 }); |
| 77 | |
| 78 | // WHEN |
| 79 | actionInput.setEnabled(false); |
| 80 | backendActionInput.syncFromFrontEnd(&actionInput, false); |
| 81 | |
| 82 | // THEN |
| 83 | QCOMPARE(backendActionInput.isEnabled(), false); |
| 84 | |
| 85 | // WHEN |
| 86 | TestDevice device; |
| 87 | actionInput.setSourceDevice(&device); |
| 88 | backendActionInput.syncFromFrontEnd(&actionInput, false); |
| 89 | |
| 90 | // THEN |
| 91 | QCOMPARE(backendActionInput.sourceDevice(), device.id()); |
| 92 | } |
| 93 | |
| 94 | void shouldDealWithKeyPresses() |
| 95 | { |
| 96 | // GIVEN |
| 97 | TestDeviceIntegration deviceIntegration; |
| 98 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 99 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 100 | Qt3DInput::Input::InputHandler handler; |
| 101 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 102 | |
| 103 | Qt3DInput::Input::ActionInput backendActionInput; |
| 104 | Qt3DInput::QActionInput actionInput; |
| 105 | actionInput.setEnabled(true); |
| 106 | actionInput.setButtons(QList<int> { Qt::Key_Space, Qt::Key_Return }); |
| 107 | actionInput.setSourceDevice(device); |
| 108 | simulateInitializationSync(&actionInput, &backendActionInput); |
| 109 | |
| 110 | // WHEN |
| 111 | deviceBackend->setButtonPressed(Qt::Key_Up, true); |
| 112 | |
| 113 | // THEN |
| 114 | QCOMPARE(backendActionInput.process(&handler, 1000000000), false); |
| 115 | |
| 116 | // WHEN |
| 117 | deviceBackend->setButtonPressed(Qt::Key_Space, true); |
| 118 | |
| 119 | // THEN |
| 120 | QCOMPARE(backendActionInput.process(&handler, 1000000000), true); |
| 121 | |
| 122 | // WHEN |
| 123 | deviceBackend->setButtonPressed(Qt::Key_Return, true); |
| 124 | |
| 125 | // THEN |
| 126 | QCOMPARE(backendActionInput.process(&handler, 1000000000), true); |
| 127 | |
| 128 | // WHEN |
| 129 | deviceBackend->setButtonPressed(Qt::Key_Space, false); |
| 130 | |
| 131 | // THEN |
| 132 | QCOMPARE(backendActionInput.process(&handler, 1000000000), true); |
| 133 | |
| 134 | // WHEN |
| 135 | deviceBackend->setButtonPressed(Qt::Key_Up, false); |
| 136 | |
| 137 | // THEN |
| 138 | QCOMPARE(backendActionInput.process(&handler, 1000000000), true); |
| 139 | |
| 140 | // WHEN |
| 141 | deviceBackend->setButtonPressed(Qt::Key_Return, false); |
| 142 | |
| 143 | // THEN |
| 144 | QCOMPARE(backendActionInput.process(&handler, 1000000000), false); |
| 145 | } |
| 146 | |
| 147 | void shouldNotProcessWhenDisabled() |
| 148 | { |
| 149 | // GIVEN |
| 150 | TestDeviceIntegration deviceIntegration; |
| 151 | TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); |
| 152 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); |
| 153 | Qt3DInput::Input::InputHandler handler; |
| 154 | handler.addInputDeviceIntegration(&deviceIntegration); |
| 155 | |
| 156 | Qt3DInput::Input::ActionInput backendActionInput; |
| 157 | Qt3DInput::QActionInput actionInput; |
| 158 | actionInput.setEnabled(false); |
| 159 | actionInput.setButtons(QList<int> { Qt::Key_Space, Qt::Key_Return }); |
| 160 | actionInput.setSourceDevice(device); |
| 161 | simulateInitializationSync(&actionInput, &backendActionInput); |
| 162 | |
| 163 | // WHEN |
| 164 | deviceBackend->setButtonPressed(Qt::Key_Space, true); |
| 165 | |
| 166 | // THEN |
| 167 | QCOMPARE(backendActionInput.process(&handler, 1000000000), false); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | QTEST_APPLESS_MAIN(tst_ActionInput) |
| 172 | |
| 173 | #include "tst_actioninput.moc" |