| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). | 
| 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 | #include <QtTest/QTest> | 
| 30 | #include <qbackendnodetester.h> | 
| 31 | #include "testdevice.h" | 
| 32 |  | 
| 33 | #include <Qt3DInput/private/actioninput_p.h> | 
| 34 | #include <Qt3DInput/private/inputhandler_p.h> | 
| 35 | #include <Qt3DInput/QActionInput> | 
| 36 |  | 
| 37 | class tst_ActionInput : public Qt3DCore::QBackendNodeTester | 
| 38 | { | 
| 39 |     Q_OBJECT | 
| 40 | private Q_SLOTS: | 
| 41 |     void shouldMirrorPeerProperties() | 
| 42 |     { | 
| 43 |         // GIVEN | 
| 44 |         Qt3DInput::Input::ActionInput backendActionInput; | 
| 45 |         Qt3DInput::QActionInput actionInput; | 
| 46 |         TestDevice sourceDevice; | 
| 47 |  | 
| 48 |         actionInput.setButtons(QVector<int>() << (1 << 8)); | 
| 49 |         actionInput.setSourceDevice(&sourceDevice); | 
| 50 |  | 
| 51 |         // WHEN | 
| 52 |         simulateInitializationSync(frontend: &actionInput, backend: &backendActionInput); | 
| 53 |  | 
| 54 |         // THEN | 
| 55 |         QCOMPARE(backendActionInput.peerId(), actionInput.id()); | 
| 56 |         QCOMPARE(backendActionInput.isEnabled(), actionInput.isEnabled()); | 
| 57 |         QCOMPARE(backendActionInput.buttons(), actionInput.buttons()); | 
| 58 |         QCOMPARE(backendActionInput.sourceDevice(), sourceDevice.id()); | 
| 59 |     } | 
| 60 |  | 
| 61 |     void shouldHaveInitialAndCleanedUpStates() | 
| 62 |     { | 
| 63 |         // GIVEN | 
| 64 |         Qt3DInput::Input::ActionInput backendActionInput; | 
| 65 |  | 
| 66 |         // THEN | 
| 67 |         QVERIFY(backendActionInput.peerId().isNull()); | 
| 68 |         QVERIFY(backendActionInput.buttons().isEmpty()); | 
| 69 |         QCOMPARE(backendActionInput.isEnabled(), false); | 
| 70 |         QCOMPARE(backendActionInput.sourceDevice(), Qt3DCore::QNodeId()); | 
| 71 |  | 
| 72 |         // GIVEN | 
| 73 |         Qt3DInput::QActionInput actionInput; | 
| 74 |         TestDevice sourceDevice; | 
| 75 |  | 
| 76 |         actionInput.setButtons(QVector<int>() << (1 << 8)); | 
| 77 |         actionInput.setSourceDevice(&sourceDevice); | 
| 78 |  | 
| 79 |         // WHEN | 
| 80 |         simulateInitializationSync(frontend: &actionInput, backend: &backendActionInput); | 
| 81 |         backendActionInput.cleanup(); | 
| 82 |  | 
| 83 |         // THEN | 
| 84 |         QVERIFY(backendActionInput.buttons().isEmpty()); | 
| 85 |         QCOMPARE(backendActionInput.isEnabled(), false); | 
| 86 |         QCOMPARE(backendActionInput.sourceDevice(), Qt3DCore::QNodeId()); | 
| 87 |     } | 
| 88 |  | 
| 89 |     void shouldHandlePropertyChanges() | 
| 90 |     { | 
| 91 |         // GIVEN | 
| 92 |         Qt3DInput::QActionInput actionInput; | 
| 93 |         Qt3DInput::Input::ActionInput backendActionInput; | 
| 94 |         simulateInitializationSync(frontend: &actionInput, backend: &backendActionInput); | 
| 95 |  | 
| 96 |         // WHEN | 
| 97 |         actionInput.setButtons(QVector<int>() << 64); | 
| 98 |         backendActionInput.syncFromFrontEnd(frontEnd: &actionInput, firstTime: false); | 
| 99 |  | 
| 100 |         // THEN | 
| 101 |         QCOMPARE(backendActionInput.buttons(), QVector<int>() << 64); | 
| 102 |  | 
| 103 |         // WHEN | 
| 104 |         actionInput.setEnabled(false); | 
| 105 |         backendActionInput.syncFromFrontEnd(frontEnd: &actionInput, firstTime: false); | 
| 106 |  | 
| 107 |         // THEN | 
| 108 |         QCOMPARE(backendActionInput.isEnabled(), false); | 
| 109 |  | 
| 110 |         // WHEN | 
| 111 |         TestDevice device; | 
| 112 |         actionInput.setSourceDevice(&device); | 
| 113 |         backendActionInput.syncFromFrontEnd(frontEnd: &actionInput, firstTime: false); | 
| 114 |  | 
| 115 |         // THEN | 
| 116 |         QCOMPARE(backendActionInput.sourceDevice(), device.id()); | 
| 117 |     } | 
| 118 |  | 
| 119 |     void shouldDealWithKeyPresses() | 
| 120 |     { | 
| 121 |         // GIVEN | 
| 122 |         TestDeviceIntegration deviceIntegration; | 
| 123 |         TestDevice *device = deviceIntegration.createPhysicalDevice(name: "keyboard" ); | 
| 124 |         TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(id: device->id()); | 
| 125 |         Qt3DInput::Input::InputHandler handler; | 
| 126 |         handler.addInputDeviceIntegration(inputIntegration: &deviceIntegration); | 
| 127 |  | 
| 128 |         Qt3DInput::Input::ActionInput backendActionInput; | 
| 129 |         Qt3DInput::QActionInput actionInput; | 
| 130 |         actionInput.setEnabled(true); | 
| 131 |         actionInput.setButtons(QVector<int>() << Qt::Key_Space << Qt::Key_Return); | 
| 132 |         actionInput.setSourceDevice(device); | 
| 133 |         simulateInitializationSync(frontend: &actionInput, backend: &backendActionInput); | 
| 134 |  | 
| 135 |         // WHEN | 
| 136 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Up, pressed: true); | 
| 137 |  | 
| 138 |         // THEN | 
| 139 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), false); | 
| 140 |  | 
| 141 |         // WHEN | 
| 142 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Space, pressed: true); | 
| 143 |  | 
| 144 |         // THEN | 
| 145 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), true); | 
| 146 |  | 
| 147 |         // WHEN | 
| 148 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Return, pressed: true); | 
| 149 |  | 
| 150 |         // THEN | 
| 151 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), true); | 
| 152 |  | 
| 153 |         // WHEN | 
| 154 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Space, pressed: false); | 
| 155 |  | 
| 156 |         // THEN | 
| 157 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), true); | 
| 158 |  | 
| 159 |         // WHEN | 
| 160 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Up, pressed: false); | 
| 161 |  | 
| 162 |         // THEN | 
| 163 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), true); | 
| 164 |  | 
| 165 |         // WHEN | 
| 166 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Return, pressed: false); | 
| 167 |  | 
| 168 |         // THEN | 
| 169 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), false); | 
| 170 |     } | 
| 171 |  | 
| 172 |     void shouldNotProcessWhenDisabled() | 
| 173 |     { | 
| 174 |         // GIVEN | 
| 175 |         TestDeviceIntegration deviceIntegration; | 
| 176 |         TestDevice *device = deviceIntegration.createPhysicalDevice(name: "keyboard" ); | 
| 177 |         TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(id: device->id()); | 
| 178 |         Qt3DInput::Input::InputHandler handler; | 
| 179 |         handler.addInputDeviceIntegration(inputIntegration: &deviceIntegration); | 
| 180 |  | 
| 181 |         Qt3DInput::Input::ActionInput backendActionInput; | 
| 182 |         Qt3DInput::QActionInput actionInput; | 
| 183 |         actionInput.setEnabled(false); | 
| 184 |         actionInput.setButtons(QVector<int>() << Qt::Key_Space << Qt::Key_Return); | 
| 185 |         actionInput.setSourceDevice(device); | 
| 186 |         simulateInitializationSync(frontend: &actionInput, backend: &backendActionInput); | 
| 187 |  | 
| 188 |         // WHEN | 
| 189 |         deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Space, pressed: true); | 
| 190 |  | 
| 191 |         // THEN | 
| 192 |         QCOMPARE(backendActionInput.process(&handler, 1000000000), false); | 
| 193 |     } | 
| 194 | }; | 
| 195 |  | 
| 196 | QTEST_APPLESS_MAIN(tst_ActionInput) | 
| 197 |  | 
| 198 | #include "tst_actioninput.moc" | 
| 199 |  |