| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 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 <Qt3DCore/private/qnode_p.h> |
| 34 | #include <Qt3DCore/private/qscene_p.h> |
| 35 | #include <Qt3DInput/private/analogaxisinput_p.h> |
| 36 | #include <Qt3DInput/private/inputhandler_p.h> |
| 37 | #include <Qt3DInput/QAnalogAxisInput> |
| 38 | |
| 39 | class tst_AnalogAxisInput: public Qt3DCore::QBackendNodeTester |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | |
| 43 | private Q_SLOTS: |
| 44 | |
| 45 | void checkPeerPropertyMirroring() |
| 46 | { |
| 47 | // GIVEN |
| 48 | Qt3DInput::Input::AnalogAxisInput backendAxisInput; |
| 49 | Qt3DInput::QAnalogAxisInput axisInput; |
| 50 | TestDevice sourceDevice; |
| 51 | |
| 52 | axisInput.setAxis(327); |
| 53 | axisInput.setSourceDevice(&sourceDevice); |
| 54 | |
| 55 | // WHEN |
| 56 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 57 | |
| 58 | // THEN |
| 59 | QCOMPARE(backendAxisInput.peerId(), axisInput.id()); |
| 60 | QCOMPARE(backendAxisInput.isEnabled(), axisInput.isEnabled()); |
| 61 | QCOMPARE(backendAxisInput.axis(), axisInput.axis()); |
| 62 | QCOMPARE(backendAxisInput.sourceDevice(), sourceDevice.id()); |
| 63 | } |
| 64 | |
| 65 | void checkInitialAndCleanedUpState() |
| 66 | { |
| 67 | // GIVEN |
| 68 | Qt3DInput::Input::AnalogAxisInput backendAxisInput; |
| 69 | |
| 70 | // THEN |
| 71 | QVERIFY(backendAxisInput.peerId().isNull()); |
| 72 | QCOMPARE(backendAxisInput.axis(), 0); |
| 73 | QCOMPARE(backendAxisInput.isEnabled(), false); |
| 74 | QCOMPARE(backendAxisInput.sourceDevice(), Qt3DCore::QNodeId()); |
| 75 | |
| 76 | // GIVEN |
| 77 | Qt3DInput::QAnalogAxisInput axisInput; |
| 78 | TestDevice sourceDevice; |
| 79 | |
| 80 | axisInput.setAxis(327); |
| 81 | axisInput.setSourceDevice(&sourceDevice); |
| 82 | |
| 83 | // WHEN |
| 84 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 85 | backendAxisInput.cleanup(); |
| 86 | |
| 87 | // THEN |
| 88 | QCOMPARE(backendAxisInput.axis(), 0); |
| 89 | QCOMPARE(backendAxisInput.isEnabled(), false); |
| 90 | QCOMPARE(backendAxisInput.sourceDevice(), Qt3DCore::QNodeId()); |
| 91 | } |
| 92 | |
| 93 | void checkPropertyChanges() |
| 94 | { |
| 95 | // GIVEN |
| 96 | Qt3DInput::QAnalogAxisInput axisInput; |
| 97 | Qt3DInput::Input::AnalogAxisInput backendAxisInput; |
| 98 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 99 | |
| 100 | // WHEN |
| 101 | axisInput.setAxis(32); |
| 102 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
| 103 | |
| 104 | // THEN |
| 105 | QCOMPARE(backendAxisInput.axis(), 32); |
| 106 | |
| 107 | // WHEN |
| 108 | axisInput.setEnabled(false); |
| 109 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
| 110 | |
| 111 | // THEN |
| 112 | QCOMPARE(backendAxisInput.isEnabled(), false); |
| 113 | |
| 114 | // WHEN |
| 115 | TestDevice device; |
| 116 | axisInput.setSourceDevice(&device); |
| 117 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
| 118 | |
| 119 | // THEN |
| 120 | QCOMPARE(backendAxisInput.sourceDevice(), device.id()); |
| 121 | } |
| 122 | |
| 123 | void shouldProcessAxisValue() |
| 124 | { |
| 125 | const qint64 s = 1000000000; |
| 126 | |
| 127 | // GIVEN |
| 128 | TestDeviceIntegration deviceIntegration; |
| 129 | TestDevice *device = deviceIntegration.createPhysicalDevice(name: "keyboard" ); |
| 130 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(id: device->id()); |
| 131 | Qt3DInput::Input::InputHandler handler; |
| 132 | handler.addInputDeviceIntegration(inputIntegration: &deviceIntegration); |
| 133 | |
| 134 | Qt3DInput::Input::AnalogAxisInput backendAxisInput; |
| 135 | Qt3DInput::QAnalogAxisInput axisInput; |
| 136 | axisInput.setEnabled(true); |
| 137 | axisInput.setAxis(2); |
| 138 | axisInput.setSourceDevice(device); |
| 139 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 140 | QCOMPARE(backendAxisInput.axis(), 2); |
| 141 | |
| 142 | // WHEN |
| 143 | deviceBackend->setAxisValue(axisIdentifier: 2, value: 0.1f); |
| 144 | |
| 145 | // THEN |
| 146 | QCOMPARE(backendAxisInput.process(&handler, 30 * s), 0.1f); |
| 147 | QCOMPARE(backendAxisInput.process(&handler, 31 * s), 0.1f); |
| 148 | |
| 149 | // WHEN |
| 150 | deviceBackend->setAxisValue(axisIdentifier: 2, value: 0.2f); |
| 151 | |
| 152 | // THEN |
| 153 | QCOMPARE(backendAxisInput.process(&handler, 32 * s), 0.2f); |
| 154 | QCOMPARE(backendAxisInput.process(&handler, 33 * s), 0.2f); |
| 155 | } |
| 156 | |
| 157 | void shouldNotProcessAxisValueWhenDisabled() |
| 158 | { |
| 159 | const qint64 s = 1000000000; |
| 160 | |
| 161 | // GIVEN |
| 162 | TestDeviceIntegration deviceIntegration; |
| 163 | TestDevice *device = deviceIntegration.createPhysicalDevice(name: "keyboard" ); |
| 164 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(id: device->id()); |
| 165 | Qt3DInput::Input::InputHandler handler; |
| 166 | handler.addInputDeviceIntegration(inputIntegration: &deviceIntegration); |
| 167 | |
| 168 | Qt3DInput::Input::AnalogAxisInput backendAxisInput; |
| 169 | Qt3DInput::QAnalogAxisInput axisInput; |
| 170 | axisInput.setEnabled(false); |
| 171 | axisInput.setAxis(2); |
| 172 | axisInput.setSourceDevice(device); |
| 173 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 174 | QCOMPARE(backendAxisInput.axis(), 2); |
| 175 | |
| 176 | // WHEN |
| 177 | deviceBackend->setAxisValue(axisIdentifier: 2, value: 0.1f); |
| 178 | |
| 179 | // THEN |
| 180 | QCOMPARE(backendAxisInput.process(&handler, 30 * s), 0.0f); |
| 181 | QCOMPARE(backendAxisInput.process(&handler, 31 * s), 0.0f); |
| 182 | |
| 183 | // WHEN |
| 184 | deviceBackend->setAxisValue(axisIdentifier: 2, value: 0.2f); |
| 185 | |
| 186 | // THEN |
| 187 | QCOMPARE(backendAxisInput.process(&handler, 32 * s), 0.0f); |
| 188 | QCOMPARE(backendAxisInput.process(&handler, 33 * s), 0.0f); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | QTEST_APPLESS_MAIN(tst_AnalogAxisInput) |
| 193 | |
| 194 | #include "tst_analogaxisinput.moc" |
| 195 | |