| 1 | // Copyright (C) 2016 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DInput/private/utils_p.h> |
| 7 | #include <Qt3DInput/private/axis_p.h> |
| 8 | #include <Qt3DInput/qanalogaxisinput.h> |
| 9 | #include "qbackendnodetester.h" |
| 10 | #include "testdeviceproxy.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | class FakeBackendDevice : public Qt3DInput::QAbstractPhysicalDeviceBackendNode |
| 15 | { |
| 16 | // QAbstractPhysicalDeviceBackendNode interface |
| 17 | public: |
| 18 | FakeBackendDevice() |
| 19 | : Qt3DInput::QAbstractPhysicalDeviceBackendNode(Qt3DCore::QBackendNode::ReadOnly) |
| 20 | {} |
| 21 | float axisValue(int) const override { return 0.0f; } |
| 22 | bool isButtonPressed(int) const override { return false; } |
| 23 | }; |
| 24 | |
| 25 | class FakeInputDeviceIntegration : public Qt3DInput::QInputDeviceIntegration |
| 26 | { |
| 27 | public: |
| 28 | explicit FakeInputDeviceIntegration(Qt3DInput::QAbstractPhysicalDevice *device) |
| 29 | : Qt3DInput::QInputDeviceIntegration() |
| 30 | , m_device(device) |
| 31 | {} |
| 32 | |
| 33 | std::vector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64) override { return std::vector<Qt3DCore::QAspectJobPtr>(); } |
| 34 | Qt3DInput::QAbstractPhysicalDevice *createPhysicalDevice(const QString &) override { return nullptr; } |
| 35 | QList<Qt3DCore::QNodeId> physicalDevices() const override { return {}; } |
| 36 | QStringList deviceNames() const override { return QStringList(); } |
| 37 | |
| 38 | Qt3DInput::QAbstractPhysicalDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId deviceId) const override |
| 39 | { |
| 40 | if (m_device->id() == deviceId) |
| 41 | return new FakeBackendDevice(); |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | void onInitialize() override {} |
| 47 | Qt3DInput::QAbstractPhysicalDevice *m_device; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | } // anonymous |
| 52 | |
| 53 | class tst_Utils : public Qt3DCore::QBackendNodeTester |
| 54 | { |
| 55 | Q_OBJECT |
| 56 | |
| 57 | private Q_SLOTS: |
| 58 | |
| 59 | void checkPhysicalDeviceForValidAxisInput() |
| 60 | { |
| 61 | // GIVEN |
| 62 | Qt3DInput::QAnalogAxisInput analogAxisInput; |
| 63 | Qt3DInput::Input::InputHandler handler; |
| 64 | TestPhysicalDevice testPhysicalDevice; |
| 65 | FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); |
| 66 | |
| 67 | analogAxisInput.setSourceDevice(&testPhysicalDevice); |
| 68 | |
| 69 | // WHEN -> Create backend AnalogAxisInput |
| 70 | Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); |
| 71 | simulateInitializationSync(&analogAxisInput, backendAxisInput); |
| 72 | |
| 73 | // THEN |
| 74 | QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); |
| 75 | QCOMPARE(backendAxisInput->sourceDevice(), testPhysicalDevice.id()); |
| 76 | |
| 77 | // Create backend integration |
| 78 | handler.addInputDeviceIntegration(&fakeIntegration); |
| 79 | |
| 80 | // WHEN |
| 81 | Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); |
| 82 | |
| 83 | // THEN -> FakeIntegration returns something non null if it receives |
| 84 | // the same id as the device used to create it |
| 85 | QVERIFY(backendDevice != nullptr); |
| 86 | delete backendDevice; |
| 87 | } |
| 88 | |
| 89 | void checkProxyPhysicalDeviceForValidAxisInput() |
| 90 | { |
| 91 | // GIVEN |
| 92 | Qt3DInput::QAnalogAxisInput analogAxisInput; |
| 93 | Qt3DInput::Input::InputHandler handler; |
| 94 | TestProxy testProxyPhysicalDevice; |
| 95 | TestPhysicalDevice testPhysicalDevice; |
| 96 | FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); |
| 97 | |
| 98 | analogAxisInput.setSourceDevice(&testProxyPhysicalDevice); |
| 99 | |
| 100 | // WHEN -> Create backend AnalogAxisInput |
| 101 | Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); |
| 102 | simulateInitializationSync(&analogAxisInput, backendAxisInput); |
| 103 | |
| 104 | // THEN |
| 105 | QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); |
| 106 | QCOMPARE(backendAxisInput->sourceDevice(), testProxyPhysicalDevice.id()); |
| 107 | |
| 108 | // WHEN -> Create backend PhysicalProxiDevice |
| 109 | Qt3DInput::Input::PhysicalDeviceProxy *backendProxyDevice = handler.physicalDeviceProxyManager()->getOrCreateResource(testProxyPhysicalDevice.id()); |
| 110 | backendProxyDevice->setManager(handler.physicalDeviceProxyManager()); |
| 111 | simulateInitializationSync(&testProxyPhysicalDevice, backendProxyDevice); |
| 112 | backendProxyDevice->setDevice(&testPhysicalDevice); |
| 113 | |
| 114 | // THEN |
| 115 | QCOMPARE(backendProxyDevice->physicalDeviceId(), testPhysicalDevice.id()); |
| 116 | |
| 117 | // Create backend integration |
| 118 | handler.addInputDeviceIntegration(&fakeIntegration); |
| 119 | |
| 120 | // WHEN |
| 121 | Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); |
| 122 | |
| 123 | // THEN -> FakeIntegration returns something non null if it receives |
| 124 | // the same id as the device used to create it |
| 125 | QVERIFY(backendDevice != nullptr); |
| 126 | delete backendDevice; |
| 127 | } |
| 128 | |
| 129 | void checkNoPhysicalDeviceForInvalidAxisInput() |
| 130 | { |
| 131 | // GIVEN |
| 132 | Qt3DInput::QAnalogAxisInput analogAxisInput; |
| 133 | Qt3DInput::Input::InputHandler handler; |
| 134 | TestPhysicalDevice testPhysicalDevice; |
| 135 | TestPhysicalDevice testPhysicalDevice2; |
| 136 | FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); |
| 137 | |
| 138 | analogAxisInput.setSourceDevice(&testPhysicalDevice2); |
| 139 | |
| 140 | // WHEN -> Create backend AnalogAxisInput |
| 141 | Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); |
| 142 | simulateInitializationSync(&analogAxisInput, backendAxisInput); |
| 143 | |
| 144 | // THEN |
| 145 | QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); |
| 146 | QCOMPARE(backendAxisInput->sourceDevice(), testPhysicalDevice2.id()); |
| 147 | |
| 148 | // Create backend integration |
| 149 | handler.addInputDeviceIntegration(&fakeIntegration); |
| 150 | |
| 151 | // WHEN |
| 152 | Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); |
| 153 | |
| 154 | // THEN -> FakeIntegration returns something non null if it receives |
| 155 | // the same id as the device used to create it (testPhysicalDevice != testPhysicalDevice2) |
| 156 | QVERIFY(backendDevice == nullptr); |
| 157 | } |
| 158 | |
| 159 | void checkNoPysicalDeviceForInvalidProxyPhysicalDevice() |
| 160 | { |
| 161 | // GIVEN |
| 162 | Qt3DInput::QAnalogAxisInput analogAxisInput; |
| 163 | Qt3DInput::Input::InputHandler handler; |
| 164 | TestProxy testProxyPhysicalDevice; |
| 165 | TestPhysicalDevice testPhysicalDevice; |
| 166 | TestPhysicalDevice testPhysicalDevice2; |
| 167 | FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); |
| 168 | |
| 169 | analogAxisInput.setSourceDevice(&testProxyPhysicalDevice); |
| 170 | |
| 171 | // WHEN -> Create backend AnalogAxisInput |
| 172 | Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); |
| 173 | simulateInitializationSync(&analogAxisInput, backendAxisInput); |
| 174 | |
| 175 | // THEN |
| 176 | QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); |
| 177 | QCOMPARE(backendAxisInput->sourceDevice(), testProxyPhysicalDevice.id()); |
| 178 | |
| 179 | // WHEN -> Create backend PhysicalProxiDevice |
| 180 | Qt3DInput::Input::PhysicalDeviceProxy *backendProxyDevice = handler.physicalDeviceProxyManager()->getOrCreateResource(testProxyPhysicalDevice.id()); |
| 181 | backendProxyDevice->setManager(handler.physicalDeviceProxyManager()); |
| 182 | simulateInitializationSync(&testProxyPhysicalDevice, backendProxyDevice); |
| 183 | backendProxyDevice->setDevice(&testPhysicalDevice2); |
| 184 | |
| 185 | // THEN |
| 186 | QCOMPARE(backendProxyDevice->physicalDeviceId(), testPhysicalDevice2.id()); |
| 187 | |
| 188 | // Create backend integration |
| 189 | handler.addInputDeviceIntegration(&fakeIntegration); |
| 190 | |
| 191 | // WHEN |
| 192 | Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); |
| 193 | |
| 194 | // THEN -> FakeIntegration returns something non null if it receives |
| 195 | // the same id as the device used to create it (testPhysicalDevice != testPhysicalDevice2) |
| 196 | QVERIFY(backendDevice == nullptr); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | QTEST_MAIN(tst_Utils) |
| 201 | |
| 202 | #include "tst_utils.moc" |