diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2016-08-25 10:02:26 +0200 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2016-11-18 18:44:59 +0000 |
commit | 53597bea63dab181fdf5a039edcbb8c29e46997e (patch) | |
tree | c53912fca87e67fb86bea17965cda2ea30eea895 /tests/auto/input | |
parent | 8fddd77fe815f7928fa1429a592fb202b11236b8 (diff) |
Add unit tests for QAbstractPhysicalDeviceProxy
Change-Id: I5652f532503dd7c637bdc10c2af83c444ea6be23
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests/auto/input')
5 files changed, 290 insertions, 2 deletions
diff --git a/tests/auto/input/commons/commons.pri b/tests/auto/input/commons/commons.pri index ccc14075b..4913f5bbb 100644 --- a/tests/auto/input/commons/commons.pri +++ b/tests/auto/input/commons/commons.pri @@ -1,4 +1,5 @@ -HEADERS += $$PWD/testdevice.h +HEADERS += $$PWD/testdevice.h \ + $$PWD/testdeviceproxy.h INCLUDEPATH += $$PWD diff --git a/tests/auto/input/commons/testdeviceproxy.h b/tests/auto/input/commons/testdeviceproxy.h new file mode 100644 index 000000000..0a6af9876 --- /dev/null +++ b/tests/auto/input/commons/testdeviceproxy.h @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** 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$ +** +****************************************************************************/ + + +#ifndef TESTDEVICEPROXY_H +#define TESTDEVICEPROXY_H + +#include <Qt3DInput/private/qabstractphysicaldeviceproxy_p.h> +#include <Qt3DInput/private/qabstractphysicaldeviceproxy_p_p.h> + +class TestPhysicalDevice : public Qt3DInput::QAbstractPhysicalDevice +{ + Q_OBJECT +public: + explicit TestPhysicalDevice(Qt3DCore::QNode *parent = nullptr) + : Qt3DInput::QAbstractPhysicalDevice(parent) + {} + + ~TestPhysicalDevice() + { + } + + enum AxisValues { + X = 0, + Y, + Z + }; + + enum ButtonValues { + Left = 0, + Right + }; + + int axisCount() const Q_DECL_FINAL { return 3; } + int buttonCount() const Q_DECL_FINAL { return 2; } + QStringList axisNames() const Q_DECL_FINAL { return QStringList() << QStringLiteral("x") << QStringLiteral("y") << QStringLiteral("z"); } + QStringList buttonNames() const Q_DECL_FINAL { return QStringList() << QStringLiteral("Left") << QStringLiteral("Right"); } + + int axisIdentifier(const QString &name) const Q_DECL_FINAL + { + if (name == QLatin1String("x")) + return TestPhysicalDevice::X; + if (name == QLatin1String("y")) + return TestPhysicalDevice::Y; + if (name == QLatin1String("z")) + return TestPhysicalDevice::Z; + return -1; + } + int buttonIdentifier(const QString &name) const Q_DECL_FINAL + { + if (name == QLatin1String("Left")) + return TestPhysicalDevice::Left; + if (name == QLatin1String("Right")) + return TestPhysicalDevice::Right; + return -1; + } +}; + +class TestProxyPrivate : public Qt3DInput::QAbstractPhysicalDeviceProxyPrivate +{ +public: + TestProxyPrivate() + : Qt3DInput::QAbstractPhysicalDeviceProxyPrivate(QStringLiteral("TestProxy")) + {} +}; + +class TestProxy : public Qt3DInput::QAbstractPhysicalDeviceProxy +{ + Q_OBJECT +public: + TestProxy() + : Qt3DInput::QAbstractPhysicalDeviceProxy(*new TestProxyPrivate) + {} + + Qt3DInput::QAbstractPhysicalDevice *device() const + { + Q_D(const TestProxy); + return d->m_device; + } + + void simulateSceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) + { + Qt3DInput::QAbstractPhysicalDeviceProxy::sceneChangeEvent(change); + } + +private: + Q_DECLARE_PRIVATE(TestProxy) +}; + +#endif // TESTDEVICEPROXY_H diff --git a/tests/auto/input/input.pro b/tests/auto/input/input.pro index 310469821..c16484013 100644 --- a/tests/auto/input/input.pro +++ b/tests/auto/input/input.pro @@ -21,5 +21,6 @@ qtConfig(private_tests) { inputsequence \ inputchord \ qabstractphysicaldevicebackendnode \ - logicaldevice + logicaldevice \ + qabstractphysicaldeviceproxy } diff --git a/tests/auto/input/qabstractphysicaldeviceproxy/qabstractphysicaldeviceproxy.pro b/tests/auto/input/qabstractphysicaldeviceproxy/qabstractphysicaldeviceproxy.pro new file mode 100644 index 000000000..6aa43f276 --- /dev/null +++ b/tests/auto/input/qabstractphysicaldeviceproxy/qabstractphysicaldeviceproxy.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +TARGET = tst_qabstractphysicaldeviceproxy + +QT += 3dcore 3dcore-private 3dinput 3dinput-private testlib + +CONFIG += testcase + +SOURCES += tst_qabstractphysicaldeviceproxy.cpp + +include(../commons/commons.pri) diff --git a/tests/auto/input/qabstractphysicaldeviceproxy/tst_qabstractphysicaldeviceproxy.cpp b/tests/auto/input/qabstractphysicaldeviceproxy/tst_qabstractphysicaldeviceproxy.cpp new file mode 100644 index 000000000..b4dfef05e --- /dev/null +++ b/tests/auto/input/qabstractphysicaldeviceproxy/tst_qabstractphysicaldeviceproxy.cpp @@ -0,0 +1,160 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** 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 <Qt3DCore/qpropertyupdatedchange.h> +#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> +#include <Qt3DCore/qnodecreatedchange.h> +#include "testdeviceproxy.h" + +class tst_QAbstractPhysicalDeviceProxy : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + + void checkDefaultConstruction() + { + // GIVEN + TestProxy abstractPhysicalDeviceProxy; + + // THEN + QCOMPARE(abstractPhysicalDeviceProxy.deviceName(), QLatin1String("TestProxy")); + QCOMPARE(abstractPhysicalDeviceProxy.status(), Qt3DInput::QAbstractPhysicalDeviceProxy::NotFound); + QCOMPARE(abstractPhysicalDeviceProxy.axisCount(), 0); + QCOMPARE(abstractPhysicalDeviceProxy.buttonCount(), 0); + QCOMPARE(abstractPhysicalDeviceProxy.axisNames(), QStringList()); + QCOMPARE(abstractPhysicalDeviceProxy.buttonNames(), QStringList()); + QVERIFY(abstractPhysicalDeviceProxy.device() == nullptr); + } + + + void checkDeviceLoading() + { + // GIVEN + TestProxy abstractPhysicalDeviceProxy; + + // WHEN + TestPhysicalDevice *device = new TestPhysicalDevice(); + auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(Qt3DCore::QNodeId()); + change->setPropertyName("device"); + change->setValue(QVariant::fromValue(device)); + + abstractPhysicalDeviceProxy.simulateSceneChangeEvent(change); + + // THEN + QCOMPARE(abstractPhysicalDeviceProxy.deviceName(), QLatin1String("TestProxy")); + QCOMPARE(abstractPhysicalDeviceProxy.status(), Qt3DInput::QAbstractPhysicalDeviceProxy::Ready); + QCOMPARE(abstractPhysicalDeviceProxy.axisCount(), device->axisCount()); + QCOMPARE(abstractPhysicalDeviceProxy.buttonCount(), device->buttonCount()); + QCOMPARE(abstractPhysicalDeviceProxy.axisNames(), device->axisNames()); + QCOMPARE(abstractPhysicalDeviceProxy.buttonNames(), device->buttonNames()); + QVERIFY(abstractPhysicalDeviceProxy.device() == device); + } + + void checkDeviceBookkeeping() + { + // GIVEN + TestProxy *abstractPhysicalDeviceProxy = new TestProxy(); + + // WHEN + TestPhysicalDevice *device = new TestPhysicalDevice(); + auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(Qt3DCore::QNodeId()); + change->setPropertyName("device"); + change->setValue(QVariant::fromValue(device)); + + abstractPhysicalDeviceProxy->simulateSceneChangeEvent(change); + + // THEN + QVERIFY(abstractPhysicalDeviceProxy->device() == device); + + // WHEN + delete device; + + // THEN -> should not crash + QVERIFY(abstractPhysicalDeviceProxy->device() == nullptr); + } + + void checkCreationData() + { + // GIVEN + TestProxy abstractPhysicalDeviceProxy; + + + // WHEN + QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; + + { + Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&abstractPhysicalDeviceProxy); + creationChanges = creationChangeGenerator.creationChanges(); + } + + // THEN + { + QCOMPARE(creationChanges.size(), 1); + + const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QAbstractPhysicalDeviceProxyData>>(creationChanges.first()); + const Qt3DInput::QAbstractPhysicalDeviceProxyData cloneData = creationChangeData->data; + + QCOMPARE(abstractPhysicalDeviceProxy.deviceName(), cloneData.deviceName); + QCOMPARE(abstractPhysicalDeviceProxy.id(), creationChangeData->subjectId()); + QCOMPARE(abstractPhysicalDeviceProxy.isEnabled(), true); + QCOMPARE(abstractPhysicalDeviceProxy.isEnabled(), creationChangeData->isNodeEnabled()); + QCOMPARE(abstractPhysicalDeviceProxy.metaObject(), creationChangeData->metaObject()); + } + + // WHEN + abstractPhysicalDeviceProxy.setEnabled(false); + + { + Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&abstractPhysicalDeviceProxy); + creationChanges = creationChangeGenerator.creationChanges(); + } + + // THEN + { + QCOMPARE(creationChanges.size(), 1); + + const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QAbstractPhysicalDeviceProxyData>>(creationChanges.first()); + const Qt3DInput::QAbstractPhysicalDeviceProxyData cloneData = creationChangeData->data; + + QCOMPARE(abstractPhysicalDeviceProxy.deviceName(), cloneData.deviceName); + QCOMPARE(abstractPhysicalDeviceProxy.id(), creationChangeData->subjectId()); + QCOMPARE(abstractPhysicalDeviceProxy.isEnabled(), false); + QCOMPARE(abstractPhysicalDeviceProxy.isEnabled(), creationChangeData->isNodeEnabled()); + QCOMPARE(abstractPhysicalDeviceProxy.metaObject(), creationChangeData->metaObject()); + } + } + +}; + +QTEST_MAIN(tst_QAbstractPhysicalDeviceProxy) + +#include "tst_qabstractphysicaldeviceproxy.moc" |