| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Paul Lemire <paul.lemire350@gmail.com> | 
| 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 |  | 
| 30 | #include <QtTest/QTest> | 
| 31 | #include <Qt3DInput/private/utils_p.h> | 
| 32 | #include <Qt3DInput/private/axis_p.h> | 
| 33 | #include <Qt3DInput/qanalogaxisinput.h> | 
| 34 | #include "qbackendnodetester.h" | 
| 35 | #include "testdeviceproxy.h" | 
| 36 |  | 
| 37 | namespace { | 
| 38 |  | 
| 39 | class FakeBackendDevice : public Qt3DInput::QAbstractPhysicalDeviceBackendNode | 
| 40 | { | 
| 41 |     // QAbstractPhysicalDeviceBackendNode interface | 
| 42 | public: | 
| 43 |     FakeBackendDevice() | 
| 44 |         : Qt3DInput::QAbstractPhysicalDeviceBackendNode(Qt3DCore::QBackendNode::ReadOnly) | 
| 45 |     {} | 
| 46 |     float axisValue(int) const { return 0.0f; } | 
| 47 |     bool isButtonPressed(int) const { return false; } | 
| 48 | }; | 
| 49 |  | 
| 50 | class FakeInputDeviceIntegration : public Qt3DInput::QInputDeviceIntegration | 
| 51 | { | 
| 52 | public: | 
| 53 |     explicit FakeInputDeviceIntegration(Qt3DInput::QAbstractPhysicalDevice *device) | 
| 54 |         : Qt3DInput::QInputDeviceIntegration() | 
| 55 |         , m_device(device) | 
| 56 |     {} | 
| 57 |  | 
| 58 |     QVector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64) override { return QVector<Qt3DCore::QAspectJobPtr>(); } | 
| 59 |     Qt3DInput::QAbstractPhysicalDevice *createPhysicalDevice(const QString &) override { return nullptr; } | 
| 60 |     QVector<Qt3DCore::QNodeId> physicalDevices() const override { return QVector<Qt3DCore::QNodeId>(); } | 
| 61 |     QStringList deviceNames() const override { return QStringList(); } | 
| 62 |  | 
| 63 |     Qt3DInput::QAbstractPhysicalDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId deviceId) const override | 
| 64 |     { | 
| 65 |         if (m_device->id() == deviceId) | 
| 66 |             return new FakeBackendDevice(); | 
| 67 |         return nullptr; | 
| 68 |     } | 
| 69 |  | 
| 70 | private: | 
| 71 |     void onInitialize() override {} | 
| 72 |     Qt3DInput::QAbstractPhysicalDevice *m_device; | 
| 73 | }; | 
| 74 |  | 
| 75 |  | 
| 76 | } // anonymous | 
| 77 |  | 
| 78 | class tst_Utils : public Qt3DCore::QBackendNodeTester | 
| 79 | { | 
| 80 |     Q_OBJECT | 
| 81 |  | 
| 82 | private Q_SLOTS: | 
| 83 |  | 
| 84 |     void checkPhysicalDeviceForValidAxisInput() | 
| 85 |     { | 
| 86 |         // GIVEN | 
| 87 |         Qt3DInput::QAnalogAxisInput analogAxisInput; | 
| 88 |         Qt3DInput::Input::InputHandler handler; | 
| 89 |         TestPhysicalDevice testPhysicalDevice; | 
| 90 |         FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); | 
| 91 |  | 
| 92 |         analogAxisInput.setSourceDevice(&testPhysicalDevice); | 
| 93 |  | 
| 94 |         // WHEN -> Create backend AnalogAxisInput | 
| 95 |         Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(id: analogAxisInput.id()); | 
| 96 |         simulateInitializationSync(frontend: &analogAxisInput, backend: backendAxisInput); | 
| 97 |  | 
| 98 |         // THEN | 
| 99 |         QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); | 
| 100 |         QCOMPARE(backendAxisInput->sourceDevice(), testPhysicalDevice.id()); | 
| 101 |  | 
| 102 |         // Create backend integration | 
| 103 |         handler.addInputDeviceIntegration(inputIntegration: &fakeIntegration); | 
| 104 |  | 
| 105 |         // WHEN | 
| 106 |         Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(input: backendAxisInput, handler: &handler); | 
| 107 |  | 
| 108 |         // THEN -> FakeIntegration returns something non null if it receives | 
| 109 |         // the same id as the device used to create it | 
| 110 |         QVERIFY(backendDevice != nullptr); | 
| 111 |         delete backendDevice; | 
| 112 |     } | 
| 113 |  | 
| 114 |     void checkProxyPhysicalDeviceForValidAxisInput() | 
| 115 |     { | 
| 116 |         // GIVEN | 
| 117 |         Qt3DInput::QAnalogAxisInput analogAxisInput; | 
| 118 |         Qt3DInput::Input::InputHandler handler; | 
| 119 |         TestProxy testProxyPhysicalDevice; | 
| 120 |         TestPhysicalDevice testPhysicalDevice; | 
| 121 |         FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); | 
| 122 |  | 
| 123 |         analogAxisInput.setSourceDevice(&testProxyPhysicalDevice); | 
| 124 |  | 
| 125 |         // WHEN -> Create backend AnalogAxisInput | 
| 126 |         Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(id: analogAxisInput.id()); | 
| 127 |         simulateInitializationSync(frontend: &analogAxisInput, backend: backendAxisInput); | 
| 128 |  | 
| 129 |         // THEN | 
| 130 |         QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); | 
| 131 |         QCOMPARE(backendAxisInput->sourceDevice(), testProxyPhysicalDevice.id()); | 
| 132 |  | 
| 133 |         // WHEN -> Create backend PhysicalProxiDevice | 
| 134 |         Qt3DInput::Input::PhysicalDeviceProxy *backendProxyDevice = handler.physicalDeviceProxyManager()->getOrCreateResource(id: testProxyPhysicalDevice.id()); | 
| 135 |         backendProxyDevice->setManager(handler.physicalDeviceProxyManager()); | 
| 136 |         simulateInitialization(frontend: &testProxyPhysicalDevice, backend: backendProxyDevice); | 
| 137 |         backendProxyDevice->setDevice(&testPhysicalDevice); | 
| 138 |  | 
| 139 |         // THEN | 
| 140 |         QCOMPARE(backendProxyDevice->physicalDeviceId(), testPhysicalDevice.id()); | 
| 141 |  | 
| 142 |         // Create backend integration | 
| 143 |         handler.addInputDeviceIntegration(inputIntegration: &fakeIntegration); | 
| 144 |  | 
| 145 |         // WHEN | 
| 146 |         Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(input: backendAxisInput, handler: &handler); | 
| 147 |  | 
| 148 |         // THEN -> FakeIntegration returns something non null if it receives | 
| 149 |         // the same id as the device used to create it | 
| 150 |         QVERIFY(backendDevice != nullptr); | 
| 151 |         delete backendDevice; | 
| 152 |     } | 
| 153 |  | 
| 154 |     void checkNoPhysicalDeviceForInvalidAxisInput() | 
| 155 |     { | 
| 156 |         // GIVEN | 
| 157 |         Qt3DInput::QAnalogAxisInput analogAxisInput; | 
| 158 |         Qt3DInput::Input::InputHandler handler; | 
| 159 |         TestPhysicalDevice testPhysicalDevice; | 
| 160 |         TestPhysicalDevice testPhysicalDevice2; | 
| 161 |         FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); | 
| 162 |  | 
| 163 |         analogAxisInput.setSourceDevice(&testPhysicalDevice2); | 
| 164 |  | 
| 165 |         // WHEN -> Create backend AnalogAxisInput | 
| 166 |         Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(id: analogAxisInput.id()); | 
| 167 |         simulateInitializationSync(frontend: &analogAxisInput, backend: backendAxisInput); | 
| 168 |  | 
| 169 |         // THEN | 
| 170 |         QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); | 
| 171 |         QCOMPARE(backendAxisInput->sourceDevice(), testPhysicalDevice2.id()); | 
| 172 |  | 
| 173 |         // Create backend integration | 
| 174 |         handler.addInputDeviceIntegration(inputIntegration: &fakeIntegration); | 
| 175 |  | 
| 176 |         // WHEN | 
| 177 |         Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(input: backendAxisInput, handler: &handler); | 
| 178 |  | 
| 179 |         // THEN ->  FakeIntegration returns something non null if it receives | 
| 180 |         // the same id as the device used to create it (testPhysicalDevice != testPhysicalDevice2) | 
| 181 |         QVERIFY(backendDevice == nullptr); | 
| 182 |     } | 
| 183 |  | 
| 184 |     void checkNoPysicalDeviceForInvalidProxyPhysicalDevice() | 
| 185 |     { | 
| 186 |         // GIVEN | 
| 187 |         Qt3DInput::QAnalogAxisInput analogAxisInput; | 
| 188 |         Qt3DInput::Input::InputHandler handler; | 
| 189 |         TestProxy testProxyPhysicalDevice; | 
| 190 |         TestPhysicalDevice testPhysicalDevice; | 
| 191 |         TestPhysicalDevice testPhysicalDevice2; | 
| 192 |         FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); | 
| 193 |  | 
| 194 |         analogAxisInput.setSourceDevice(&testProxyPhysicalDevice); | 
| 195 |  | 
| 196 |         // WHEN -> Create backend AnalogAxisInput | 
| 197 |         Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(id: analogAxisInput.id()); | 
| 198 |         simulateInitializationSync(frontend: &analogAxisInput, backend: backendAxisInput); | 
| 199 |  | 
| 200 |         // THEN | 
| 201 |         QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); | 
| 202 |         QCOMPARE(backendAxisInput->sourceDevice(), testProxyPhysicalDevice.id()); | 
| 203 |  | 
| 204 |         // WHEN -> Create backend PhysicalProxiDevice | 
| 205 |         Qt3DInput::Input::PhysicalDeviceProxy *backendProxyDevice = handler.physicalDeviceProxyManager()->getOrCreateResource(id: testProxyPhysicalDevice.id()); | 
| 206 |         backendProxyDevice->setManager(handler.physicalDeviceProxyManager()); | 
| 207 |         simulateInitialization(frontend: &testProxyPhysicalDevice, backend: backendProxyDevice); | 
| 208 |         backendProxyDevice->setDevice(&testPhysicalDevice2); | 
| 209 |  | 
| 210 |         // THEN | 
| 211 |         QCOMPARE(backendProxyDevice->physicalDeviceId(), testPhysicalDevice2.id()); | 
| 212 |  | 
| 213 |         // Create backend integration | 
| 214 |         handler.addInputDeviceIntegration(inputIntegration: &fakeIntegration); | 
| 215 |  | 
| 216 |         // WHEN | 
| 217 |         Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(input: backendAxisInput, handler: &handler); | 
| 218 |  | 
| 219 |         // THEN ->  FakeIntegration returns something non null if it receives | 
| 220 |         // the same id as the device used to create it (testPhysicalDevice != testPhysicalDevice2) | 
| 221 |         QVERIFY(backendDevice == nullptr); | 
| 222 |     } | 
| 223 | }; | 
| 224 |  | 
| 225 | QTEST_MAIN(tst_Utils) | 
| 226 |  | 
| 227 | #include "tst_utils.moc" | 
| 228 |  |