diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2016-08-29 16:50:37 +0200 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2016-11-19 18:32:45 +0000 |
commit | 0a4cf4c09fa25fa67b12e77227dfd639dd3708dc (patch) | |
tree | ee6d89b6a3cfc095388379494195e9a7cfe30e34 /tests/auto/input | |
parent | 3fe2b66230da2f35be3533d13a5dd6e65376b2aa (diff) |
Add unit tests for Input::Utils
Change-Id: I5efe9a3db29732d169023ef8ccb435837c1ff3d1
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests/auto/input')
-rw-r--r-- | tests/auto/input/input.pro | 3 | ||||
-rw-r--r-- | tests/auto/input/utils/tst_utils.cpp | 228 | ||||
-rw-r--r-- | tests/auto/input/utils/utils.pro | 11 |
3 files changed, 241 insertions, 1 deletions
diff --git a/tests/auto/input/input.pro b/tests/auto/input/input.pro index 357432461..f469d07ac 100644 --- a/tests/auto/input/input.pro +++ b/tests/auto/input/input.pro @@ -26,5 +26,6 @@ qtConfig(private_tests) { physicaldeviceproxy \ loadproxydevicejob \ qmousedevice \ - mousedevice + mousedevice \ + utils } diff --git a/tests/auto/input/utils/tst_utils.cpp b/tests/auto/input/utils/tst_utils.cpp new file mode 100644 index 000000000..766408ce3 --- /dev/null +++ b/tests/auto/input/utils/tst_utils.cpp @@ -0,0 +1,228 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Paul Lemire <paul.lemire350@gmail.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QTest> +#include <Qt3DInput/private/utils_p.h> +#include <Qt3DInput/private/axis_p.h> +#include <Qt3DInput/qanalogaxisinput.h> +#include <Qt3DCore/qpropertyupdatedchange.h> +#include "qbackendnodetester.h" +#include "testdeviceproxy.h" + +namespace { + +class FakeBackendDevice : public Qt3DInput::QAbstractPhysicalDeviceBackendNode +{ + // QAbstractPhysicalDeviceBackendNode interface +public: + FakeBackendDevice() + : Qt3DInput::QAbstractPhysicalDeviceBackendNode(Qt3DCore::QBackendNode::ReadOnly) + {} + float axisValue(int) const { return 0.0f; } + bool isButtonPressed(int) const { return false; } +}; + +class FakeInputDeviceIntegration : public Qt3DInput::QInputDeviceIntegration +{ +public: + explicit FakeInputDeviceIntegration(Qt3DInput::QAbstractPhysicalDevice *device) + : Qt3DInput::QInputDeviceIntegration() + , m_device(device) + {} + + QVector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64) Q_DECL_OVERRIDE { return QVector<Qt3DCore::QAspectJobPtr>(); } + Qt3DInput::QAbstractPhysicalDevice *createPhysicalDevice(const QString &) Q_DECL_OVERRIDE { return nullptr; } + QVector<Qt3DCore::QNodeId> physicalDevices() const Q_DECL_OVERRIDE { return QVector<Qt3DCore::QNodeId>(); } + QStringList deviceNames() const Q_DECL_OVERRIDE { return QStringList(); } + + Qt3DInput::QAbstractPhysicalDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId deviceId) const Q_DECL_OVERRIDE + { + if (m_device->id() == deviceId) + return new FakeBackendDevice(); + return nullptr; + } + +private: + void onInitialize() Q_DECL_OVERRIDE {} + Qt3DInput::QAbstractPhysicalDevice *m_device; +}; + + +} // anonymous + +class tst_Utils : public Qt3DCore::QBackendNodeTester +{ + Q_OBJECT + +private Q_SLOTS: + + void checkPhysicalDeviceForValidAxisInput() + { + // GIVEN + Qt3DInput::QAnalogAxisInput analogAxisInput; + Qt3DInput::Input::InputHandler handler; + TestPhysicalDevice testPhysicalDevice; + FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); + + analogAxisInput.setSourceDevice(&testPhysicalDevice); + + // WHEN -> Create backend AnalogAxisInput + Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); + simulateInitialization(&analogAxisInput, backendAxisInput); + + // THEN + QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); + QCOMPARE(backendAxisInput->sourceDevice(), testPhysicalDevice.id()); + + // Create backend integration + handler.addInputDeviceIntegration(&fakeIntegration); + + // WHEN + Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); + + // THEN -> FakeIntegration returns something non null if it receives + // the same id as the device used to create it + QVERIFY(backendDevice != nullptr); + delete backendDevice; + } + + void checkProxyPhysicalDeviceForValidAxisInput() + { + // GIVEN + Qt3DInput::QAnalogAxisInput analogAxisInput; + Qt3DInput::Input::InputHandler handler; + TestProxy testProxyPhysicalDevice; + TestPhysicalDevice testPhysicalDevice; + FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); + + analogAxisInput.setSourceDevice(&testProxyPhysicalDevice); + + // WHEN -> Create backend AnalogAxisInput + Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); + simulateInitialization(&analogAxisInput, backendAxisInput); + + // THEN + QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); + QCOMPARE(backendAxisInput->sourceDevice(), testProxyPhysicalDevice.id()); + + // WHEN -> Create backend PhysicalProxiDevice + Qt3DInput::Input::PhysicalDeviceProxy *backendProxyDevice = handler.physicalDeviceProxyManager()->getOrCreateResource(testProxyPhysicalDevice.id()); + backendProxyDevice->setManager(handler.physicalDeviceProxyManager()); + simulateInitialization(&testProxyPhysicalDevice, backendProxyDevice); + backendProxyDevice->setDevice(&testPhysicalDevice); + + // THEN + QCOMPARE(backendProxyDevice->physicalDeviceId(), testPhysicalDevice.id()); + + // Create backend integration + handler.addInputDeviceIntegration(&fakeIntegration); + + // WHEN + Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); + + // THEN -> FakeIntegration returns something non null if it receives + // the same id as the device used to create it + QVERIFY(backendDevice != nullptr); + delete backendDevice; + } + + void checkNoPhysicalDeviceForInvalidAxisInput() + { + // GIVEN + Qt3DInput::QAnalogAxisInput analogAxisInput; + Qt3DInput::Input::InputHandler handler; + TestPhysicalDevice testPhysicalDevice; + TestPhysicalDevice testPhysicalDevice2; + FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); + + analogAxisInput.setSourceDevice(&testPhysicalDevice2); + + // WHEN -> Create backend AnalogAxisInput + Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); + simulateInitialization(&analogAxisInput, backendAxisInput); + + // THEN + QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); + QCOMPARE(backendAxisInput->sourceDevice(), testPhysicalDevice2.id()); + + // Create backend integration + handler.addInputDeviceIntegration(&fakeIntegration); + + // WHEN + Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); + + // THEN -> FakeIntegration returns something non null if it receives + // the same id as the device used to create it (testPhysicalDevice != testPhysicalDevice2) + QVERIFY(backendDevice == nullptr); + } + + void checkNoPysicalDeviceForInvalidProxyPhysicalDevice() + { + // GIVEN + Qt3DInput::QAnalogAxisInput analogAxisInput; + Qt3DInput::Input::InputHandler handler; + TestProxy testProxyPhysicalDevice; + TestPhysicalDevice testPhysicalDevice; + TestPhysicalDevice testPhysicalDevice2; + FakeInputDeviceIntegration fakeIntegration(&testPhysicalDevice); + + analogAxisInput.setSourceDevice(&testProxyPhysicalDevice); + + // WHEN -> Create backend AnalogAxisInput + Qt3DInput::Input::AnalogAxisInput *backendAxisInput = handler.analogAxisInputManager()->getOrCreateResource(analogAxisInput.id()); + simulateInitialization(&analogAxisInput, backendAxisInput); + + // THEN + QCOMPARE(backendAxisInput->axis(), analogAxisInput.axis()); + QCOMPARE(backendAxisInput->sourceDevice(), testProxyPhysicalDevice.id()); + + // WHEN -> Create backend PhysicalProxiDevice + Qt3DInput::Input::PhysicalDeviceProxy *backendProxyDevice = handler.physicalDeviceProxyManager()->getOrCreateResource(testProxyPhysicalDevice.id()); + backendProxyDevice->setManager(handler.physicalDeviceProxyManager()); + simulateInitialization(&testProxyPhysicalDevice, backendProxyDevice); + backendProxyDevice->setDevice(&testPhysicalDevice2); + + // THEN + QCOMPARE(backendProxyDevice->physicalDeviceId(), testPhysicalDevice2.id()); + + // Create backend integration + handler.addInputDeviceIntegration(&fakeIntegration); + + // WHEN + Qt3DInput::QAbstractPhysicalDeviceBackendNode *backendDevice = Qt3DInput::Input::Utils::physicalDeviceForInput(backendAxisInput, &handler); + + // THEN -> FakeIntegration returns something non null if it receives + // the same id as the device used to create it (testPhysicalDevice != testPhysicalDevice2) + QVERIFY(backendDevice == nullptr); + } +}; + +QTEST_MAIN(tst_Utils) + +#include "tst_utils.moc" diff --git a/tests/auto/input/utils/utils.pro b/tests/auto/input/utils/utils.pro new file mode 100644 index 000000000..7ee2c27d8 --- /dev/null +++ b/tests/auto/input/utils/utils.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +TARGET = tst_utils + +QT += 3dcore 3dcore-private 3dinput 3dinput-private testlib + +CONFIG += testcase + +SOURCES += tst_utils.cpp + +include(../commons/commons.pri) |