diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2016-08-25 14:57:09 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2016-08-25 14:57:09 +0100 |
commit | b8372b52809378f860dfb0a9a9a97b30b5ad1a08 (patch) | |
tree | 271b70645f97f7fbb97c04c2d8337b781d042bcd /tests/auto/input | |
parent | 7a3db1facaac54053af5c8956ec04fcd8762a311 (diff) | |
parent | 150af04197682ccdbab509a95758fcdf957f63b2 (diff) |
Merge branch '5.7' into 5.8
Conflicts:
tests/auto/input/input.pro
Change-Id: Ic89c7d9093c95bc1c5ca50f04ed34c00b5f261f4
Diffstat (limited to 'tests/auto/input')
-rw-r--r-- | tests/auto/input/abstractaxisinput/abstractaxisinput.pro | 1 | ||||
-rw-r--r-- | tests/auto/input/actioninput/actioninput.pro | 11 | ||||
-rw-r--r-- | tests/auto/input/actioninput/tst_actioninput.cpp | 179 | ||||
-rw-r--r-- | tests/auto/input/analogaxisinput/analogaxisinput.pro | 1 | ||||
-rw-r--r-- | tests/auto/input/buttonaxisinput/buttonaxisinput.pro | 1 | ||||
-rw-r--r-- | tests/auto/input/commons/commons.pri | 3 | ||||
-rw-r--r-- | tests/auto/input/commons/testdevice.h | 107 | ||||
-rw-r--r-- | tests/auto/input/input.pro | 5 | ||||
-rw-r--r-- | tests/auto/input/inputchord/inputchord.pro | 11 | ||||
-rw-r--r-- | tests/auto/input/inputchord/tst_inputchord.cpp | 281 | ||||
-rw-r--r-- | tests/auto/input/inputsequence/inputsequence.pro | 11 | ||||
-rw-r--r-- | tests/auto/input/inputsequence/tst_inputsequence.cpp | 400 | ||||
-rw-r--r-- | tests/auto/input/keyboardhandler/keyboardhandler.pro | 1 |
13 files changed, 1007 insertions, 5 deletions
diff --git a/tests/auto/input/abstractaxisinput/abstractaxisinput.pro b/tests/auto/input/abstractaxisinput/abstractaxisinput.pro index 3f9c521de..574b9af9e 100644 --- a/tests/auto/input/abstractaxisinput/abstractaxisinput.pro +++ b/tests/auto/input/abstractaxisinput/abstractaxisinput.pro @@ -8,5 +8,4 @@ CONFIG += testcase SOURCES += tst_abstractaxisinput.cpp -include(../../core/common/common.pri) include(../commons/commons.pri) diff --git a/tests/auto/input/actioninput/actioninput.pro b/tests/auto/input/actioninput/actioninput.pro new file mode 100644 index 000000000..1091f2b76 --- /dev/null +++ b/tests/auto/input/actioninput/actioninput.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +TARGET = tst_actioninput + +QT += core-private 3dcore 3dcore-private 3dinput 3dinput-private testlib + +CONFIG += testcase + +SOURCES += tst_actioninput.cpp + +include(../commons/commons.pri) diff --git a/tests/auto/input/actioninput/tst_actioninput.cpp b/tests/auto/input/actioninput/tst_actioninput.cpp new file mode 100644 index 000000000..2db4cf760 --- /dev/null +++ b/tests/auto/input/actioninput/tst_actioninput.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +** +** 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 <qbackendnodetester.h> +#include "testdevice.h" + +#include <Qt3DCore/QPropertyUpdatedChange> +#include <Qt3DInput/private/actioninput_p.h> +#include <Qt3DInput/private/inputhandler_p.h> +#include <Qt3DInput/QActionInput> + +class tst_ActionInput : public Qt3DCore::QBackendNodeTester +{ + Q_OBJECT +private Q_SLOTS: + void shouldMirrorPeerProperties() + { + // GIVEN + Qt3DInput::Input::ActionInput backendActionInput; + Qt3DInput::QActionInput actionInput; + TestDevice sourceDevice; + + actionInput.setButtons(QVector<int>() << (1 << 8)); + actionInput.setSourceDevice(&sourceDevice); + + // WHEN + simulateInitialization(&actionInput, &backendActionInput); + + // THEN + QCOMPARE(backendActionInput.peerId(), actionInput.id()); + QCOMPARE(backendActionInput.isEnabled(), actionInput.isEnabled()); + QCOMPARE(backendActionInput.buttons(), actionInput.buttons()); + QCOMPARE(backendActionInput.sourceDevice(), sourceDevice.id()); + } + + void shouldHaveInitialAndCleanedUpStates() + { + // GIVEN + Qt3DInput::Input::ActionInput backendActionInput; + + // THEN + QVERIFY(backendActionInput.peerId().isNull()); + QVERIFY(backendActionInput.buttons().isEmpty()); + QCOMPARE(backendActionInput.isEnabled(), false); + QCOMPARE(backendActionInput.sourceDevice(), Qt3DCore::QNodeId()); + + // GIVEN + Qt3DInput::QActionInput actionInput; + TestDevice sourceDevice; + + actionInput.setButtons(QVector<int>() << (1 << 8)); + actionInput.setSourceDevice(&sourceDevice); + + // WHEN + simulateInitialization(&actionInput, &backendActionInput); + backendActionInput.cleanup(); + + // THEN + QVERIFY(backendActionInput.buttons().isEmpty()); + QCOMPARE(backendActionInput.isEnabled(), false); + QCOMPARE(backendActionInput.sourceDevice(), Qt3DCore::QNodeId()); + } + + void shouldHandlePropertyChanges() + { + // GIVEN + Qt3DInput::Input::ActionInput backendActionInput; + + // WHEN + Qt3DCore::QPropertyUpdatedChangePtr updateChange(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setValue(QVariant::fromValue(QVector<int>() << 64)); + updateChange->setPropertyName("buttons"); + backendActionInput.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendActionInput.buttons(), QVector<int>() << 64); + + // WHEN + updateChange.reset(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setPropertyName("enabled"); + updateChange->setValue(true); + backendActionInput.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendActionInput.isEnabled(), true); + + // WHEN + TestDevice device; + updateChange.reset(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setPropertyName("sourceDevice"); + updateChange->setValue(QVariant::fromValue(device.id())); + backendActionInput.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendActionInput.sourceDevice(), device.id()); + } + + void shouldDealWithKeyPresses() + { + // GIVEN + TestDeviceIntegration deviceIntegration; + TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); + TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); + Qt3DInput::Input::InputHandler handler; + handler.addInputDeviceIntegration(&deviceIntegration); + + Qt3DInput::Input::ActionInput backendActionInput; + Qt3DInput::QActionInput actionInput; + actionInput.setButtons(QVector<int>() << Qt::Key_Space << Qt::Key_Return); + actionInput.setSourceDevice(device); + simulateInitialization(&actionInput, &backendActionInput); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Up, true); + + // THEN + QCOMPARE(backendActionInput.process(&handler, 1000000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Space, true); + + // THEN + QCOMPARE(backendActionInput.process(&handler, 1000000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Return, true); + + // THEN + QCOMPARE(backendActionInput.process(&handler, 1000000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Space, false); + + // THEN + QCOMPARE(backendActionInput.process(&handler, 1000000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Up, false); + + // THEN + QCOMPARE(backendActionInput.process(&handler, 1000000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Return, false); + + // THEN + QCOMPARE(backendActionInput.process(&handler, 1000000000), false); + } +}; + +QTEST_APPLESS_MAIN(tst_ActionInput) + +#include "tst_actioninput.moc" diff --git a/tests/auto/input/analogaxisinput/analogaxisinput.pro b/tests/auto/input/analogaxisinput/analogaxisinput.pro index 7bd0a9865..16d237bf9 100644 --- a/tests/auto/input/analogaxisinput/analogaxisinput.pro +++ b/tests/auto/input/analogaxisinput/analogaxisinput.pro @@ -8,5 +8,4 @@ CONFIG += testcase SOURCES += tst_analogaxisinput.cpp -include(../../core/common/common.pri) include(../commons/commons.pri) diff --git a/tests/auto/input/buttonaxisinput/buttonaxisinput.pro b/tests/auto/input/buttonaxisinput/buttonaxisinput.pro index f012ca1d3..1aa492aa4 100644 --- a/tests/auto/input/buttonaxisinput/buttonaxisinput.pro +++ b/tests/auto/input/buttonaxisinput/buttonaxisinput.pro @@ -8,5 +8,4 @@ CONFIG += testcase SOURCES += tst_buttonaxisinput.cpp -include(../../core/common/common.pri) include(../commons/commons.pri) diff --git a/tests/auto/input/commons/commons.pri b/tests/auto/input/commons/commons.pri index cc65e8a41..ccc14075b 100644 --- a/tests/auto/input/commons/commons.pri +++ b/tests/auto/input/commons/commons.pri @@ -1,3 +1,6 @@ HEADERS += $$PWD/testdevice.h INCLUDEPATH += $$PWD + +include(../../core/common/common.pri) + diff --git a/tests/auto/input/commons/testdevice.h b/tests/auto/input/commons/testdevice.h index c58835a38..c6fc4995b 100644 --- a/tests/auto/input/commons/testdevice.h +++ b/tests/auto/input/commons/testdevice.h @@ -28,6 +28,9 @@ #include <Qt3DCore/private/qnode_p.h> #include <Qt3DInput/QAbstractPhysicalDevice> +#include <Qt3DInput/private/qabstractphysicaldevicebackendnode_p.h> +#include <Qt3DInput/private/qinputdeviceintegration_p.h> +#include <qbackendnodetester.h> class TestDevice : public Qt3DInput::QAbstractPhysicalDevice { @@ -43,4 +46,108 @@ public: QStringList buttonNames() const Q_DECL_FINAL { return QStringList(); } int axisIdentifier(const QString &name) const Q_DECL_FINAL { Q_UNUSED(name) return 0; } int buttonIdentifier(const QString &name) const Q_DECL_FINAL { Q_UNUSED(name) return 0; } + +private: + friend class TestDeviceBackendNode; +}; + +class TestDeviceBackendNode : public Qt3DInput::QAbstractPhysicalDeviceBackendNode +{ +public: + explicit TestDeviceBackendNode(TestDevice *device) + : Qt3DInput::QAbstractPhysicalDeviceBackendNode(ReadOnly) + { + Qt3DCore::QBackendNodeTester().simulateInitialization(device, this); + } + + float axisValue(int axisIdentifier) const Q_DECL_FINAL + { + return m_axisValues.value(axisIdentifier); + } + + void setAxisValue(int axisIdentifier, float value) + { + m_axisValues.insert(axisIdentifier, value); + } + + bool isButtonPressed(int buttonIdentifier) const Q_DECL_FINAL + { + return m_buttonStates.value(buttonIdentifier); + } + + void setButtonPressed(int buttonIdentifier, bool pressed) + { + m_buttonStates.insert(buttonIdentifier, pressed); + } + +private: + QHash<int, float> m_axisValues; + QHash<int, bool> m_buttonStates; +}; + +class TestDeviceIntegration : public Qt3DInput::QInputDeviceIntegration +{ + Q_OBJECT +public: + explicit TestDeviceIntegration(QObject *parent = nullptr) + : Qt3DInput::QInputDeviceIntegration(parent), + m_devicesParent(new Qt3DCore::QNode) + { + } + + ~TestDeviceIntegration() + { + qDeleteAll(m_deviceBackendNodes); + } + + QVector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64 time) Q_DECL_FINAL + { + Q_UNUSED(time); + return QVector<Qt3DCore::QAspectJobPtr>(); + } + + TestDevice *createPhysicalDevice(const QString &name) Q_DECL_FINAL + { + Q_ASSERT(!deviceNames().contains(name)); + auto device = new TestDevice(m_devicesParent.data()); // Avoids unwanted reparenting + device->setObjectName(name); + m_devices.append(device); + m_deviceBackendNodes.append(new TestDeviceBackendNode(device)); + return device; + } + + QVector<Qt3DCore::QNodeId> physicalDevices() const Q_DECL_FINAL + { + QVector<Qt3DCore::QNodeId> ids; + std::transform(m_devices.constBegin(), m_devices.constEnd(), + std::back_inserter(ids), + [] (TestDevice *device) { return device->id(); }); + return ids; + } + + TestDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId id) const Q_DECL_FINAL + { + auto it = std::find_if(m_deviceBackendNodes.constBegin(), m_deviceBackendNodes.constEnd(), + [id] (TestDeviceBackendNode *node) { return node->peerId() == id; }); + if (it == m_deviceBackendNodes.constEnd()) + return nullptr; + else + return *it; + } + + QStringList deviceNames() const Q_DECL_FINAL + { + QStringList names; + std::transform(m_devices.constBegin(), m_devices.constEnd(), + std::back_inserter(names), + [] (TestDevice *device) { return device->objectName(); }); + return names; + } + +private: + void onInitialize() Q_DECL_FINAL {} + + QScopedPointer<Qt3DCore::QNode> m_devicesParent; + QVector<TestDevice*> m_devices; + QVector<TestDeviceBackendNode*> m_deviceBackendNodes; }; diff --git a/tests/auto/input/input.pro b/tests/auto/input/input.pro index fa447730d..05668c036 100644 --- a/tests/auto/input/input.pro +++ b/tests/auto/input/input.pro @@ -13,8 +13,11 @@ qtConfig(private_tests) { axis \ action \ abstractaxisinput \ + actioninput \ analogaxisinput \ buttonaxisinput \ keyboardhandler \ - qaxisaccumulator + qaxisaccumulator \ + inputsequence \ + inputchord } diff --git a/tests/auto/input/inputchord/inputchord.pro b/tests/auto/input/inputchord/inputchord.pro new file mode 100644 index 000000000..09bbbb3ec --- /dev/null +++ b/tests/auto/input/inputchord/inputchord.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +TARGET = tst_inputchord + +QT += core-private 3dcore 3dcore-private 3dinput 3dinput-private testlib + +CONFIG += testcase + +SOURCES += tst_inputchord.cpp + +include(../commons/commons.pri) diff --git a/tests/auto/input/inputchord/tst_inputchord.cpp b/tests/auto/input/inputchord/tst_inputchord.cpp new file mode 100644 index 000000000..a840d7dee --- /dev/null +++ b/tests/auto/input/inputchord/tst_inputchord.cpp @@ -0,0 +1,281 @@ +/**************************************************************************** +** +** 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 <qbackendnodetester.h> +#include "testdevice.h" + +#include <Qt3DCore/QPropertyUpdatedChange> +#include <Qt3DCore/QPropertyNodeAddedChange> +#include <Qt3DCore/QPropertyNodeRemovedChange> +#include <Qt3DInput/private/actioninput_p.h> +#include <Qt3DInput/private/inputchord_p.h> +#include <Qt3DInput/private/inputhandler_p.h> +#include <Qt3DInput/private/inputmanagers_p.h> +#include <Qt3DInput/QActionInput> +#include <Qt3DInput/QInputChord> + +class tst_InputChord : public Qt3DCore::QBackendNodeTester +{ + Q_OBJECT +private Q_SLOTS: + void shouldMirrorPeerProperties() + { + // GIVEN + Qt3DInput::Input::InputChord backendInputChord; + Qt3DInput::QInputChord inputChord; + Qt3DInput::QActionInput actionInput; + + inputChord.setTimeout(250); + inputChord.addChord(&actionInput); + + // WHEN + simulateInitialization(&inputChord, &backendInputChord); + + // THEN + QCOMPARE(backendInputChord.peerId(), inputChord.id()); + QCOMPARE(backendInputChord.isEnabled(), inputChord.isEnabled()); + QCOMPARE(backendInputChord.timeout(), inputChord.timeout() * 1000000); + QCOMPARE(backendInputChord.chords().size(), inputChord.chords().size()); + + const int inputsCount = backendInputChord.chords().size(); + if (inputsCount > 0) { + for (int i = 0; i < inputsCount; ++i) + QCOMPARE(backendInputChord.chords().at(i), inputChord.chords().at(i)->id()); + } + } + + void shouldHaveInitialAndCleanedUpStates() + { + // GIVEN + Qt3DInput::Input::InputChord backendInputChord; + + // THEN + QVERIFY(backendInputChord.peerId().isNull()); + QCOMPARE(backendInputChord.isEnabled(), false); + QCOMPARE(backendInputChord.timeout(), 0); + QCOMPARE(backendInputChord.chords().size(), 0); + + // GIVEN + Qt3DInput::QInputChord inputChord; + Qt3DInput::QActionInput actionInput; + + inputChord.setTimeout(250); + inputChord.addChord(&actionInput); + + // WHEN + simulateInitialization(&inputChord, &backendInputChord); + backendInputChord.cleanup(); + + // THEN + QCOMPARE(backendInputChord.isEnabled(), false); + QCOMPARE(backendInputChord.timeout(), 0); + QCOMPARE(backendInputChord.chords().size(), 0); + } + + void shouldHandlePropertyChanges() + { + // GIVEN + Qt3DInput::Input::InputChord backendInputChord; + + // WHEN + Qt3DCore::QPropertyUpdatedChangePtr updateChange(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setValue(250); + updateChange->setPropertyName("timeout"); + backendInputChord.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendInputChord.timeout(), 250000000); + + // WHEN + updateChange.reset(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setPropertyName("enabled"); + updateChange->setValue(true); + backendInputChord.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendInputChord.isEnabled(), true); + + // WHEN + Qt3DInput::QActionInput input; + const Qt3DCore::QNodeId inputId = input.id(); + const auto nodeAddedChange = Qt3DCore::QPropertyNodeAddedChangePtr::create(Qt3DCore::QNodeId(), &input); + nodeAddedChange->setPropertyName("chord"); + backendInputChord.sceneChangeEvent(nodeAddedChange); + + // THEN + QCOMPARE(backendInputChord.chords().size(), 1); + QCOMPARE(backendInputChord.chords().first(), inputId); + + // WHEN + const auto nodeRemovedChange = Qt3DCore::QPropertyNodeRemovedChangePtr::create(Qt3DCore::QNodeId(), &input); + nodeRemovedChange->setPropertyName("chord"); + backendInputChord.sceneChangeEvent(nodeRemovedChange); + + // THEN + QCOMPARE(backendInputChord.chords().size(), 0); + } + + void shouldActivateWhenAllArePressed() + { + // GIVEN + TestDeviceIntegration deviceIntegration; + TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); + TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); + Qt3DInput::Input::InputHandler handler; + handler.addInputDeviceIntegration(&deviceIntegration); + + auto firstInput = new Qt3DInput::QActionInput; + firstInput->setButtons(QVector<int>() << Qt::Key_Q << Qt::Key_W); + firstInput->setSourceDevice(device); + auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); + simulateInitialization(firstInput, backendFirstInput); + + auto secondInput = new Qt3DInput::QActionInput; + secondInput->setButtons(QVector<int>() << Qt::Key_A << Qt::Key_S); + secondInput->setSourceDevice(device); + auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); + simulateInitialization(secondInput, backendSecondInput); + + Qt3DInput::Input::InputChord backendInputChord; + Qt3DInput::QInputChord inputChord; + inputChord.setTimeout(300); + inputChord.addChord(firstInput); + inputChord.addChord(secondInput); + simulateInitialization(&inputChord, &backendInputChord); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Up, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1000000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Up, false); + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1100000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_A, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1200000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1300000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_W, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1400000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_W, false); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1500000000), true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1600000000), true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1700000000), true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1800000000), true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1900000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, false); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 2000000000), false); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 2100000000), false); + } + + void shouldRespectChordTimeout() + { + // GIVEN + TestDeviceIntegration deviceIntegration; + TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); + TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); + Qt3DInput::Input::InputHandler handler; + handler.addInputDeviceIntegration(&deviceIntegration); + + auto firstInput = new Qt3DInput::QActionInput; + firstInput->setButtons(QVector<int>() << Qt::Key_Q); + firstInput->setSourceDevice(device); + auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); + simulateInitialization(firstInput, backendFirstInput); + + auto secondInput = new Qt3DInput::QActionInput; + secondInput->setButtons(QVector<int>() << Qt::Key_W); + secondInput->setSourceDevice(device); + auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); + simulateInitialization(secondInput, backendSecondInput); + + Qt3DInput::Input::InputChord backendInputChord; + Qt3DInput::QInputChord inputChord; + inputChord.setTimeout(300); + inputChord.addChord(firstInput); + inputChord.addChord(secondInput); + simulateInitialization(&inputChord, &backendInputChord); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1000000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_W, true); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1400000000), false); // Too late + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1600000000), false); + + // THEN + QCOMPARE(backendInputChord.process(&handler, 1800000000), false); + } +}; + +QTEST_APPLESS_MAIN(tst_InputChord) + +#include "tst_inputchord.moc" diff --git a/tests/auto/input/inputsequence/inputsequence.pro b/tests/auto/input/inputsequence/inputsequence.pro new file mode 100644 index 000000000..1fdb21f5b --- /dev/null +++ b/tests/auto/input/inputsequence/inputsequence.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +TARGET = tst_inputsequence + +QT += core-private 3dcore 3dcore-private 3dinput 3dinput-private testlib + +CONFIG += testcase + +SOURCES += tst_inputsequence.cpp + +include(../commons/commons.pri) diff --git a/tests/auto/input/inputsequence/tst_inputsequence.cpp b/tests/auto/input/inputsequence/tst_inputsequence.cpp new file mode 100644 index 000000000..6b4a39b68 --- /dev/null +++ b/tests/auto/input/inputsequence/tst_inputsequence.cpp @@ -0,0 +1,400 @@ +/**************************************************************************** +** +** 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 <qbackendnodetester.h> +#include "testdevice.h" + +#include <Qt3DCore/QPropertyUpdatedChange> +#include <Qt3DCore/QPropertyNodeAddedChange> +#include <Qt3DCore/QPropertyNodeRemovedChange> +#include <Qt3DInput/private/actioninput_p.h> +#include <Qt3DInput/private/inputhandler_p.h> +#include <Qt3DInput/private/inputmanagers_p.h> +#include <Qt3DInput/private/inputsequence_p.h> +#include <Qt3DInput/QActionInput> +#include <Qt3DInput/QInputSequence> + +class tst_InputSequence : public Qt3DCore::QBackendNodeTester +{ + Q_OBJECT +private Q_SLOTS: + void shouldMirrorPeerProperties() + { + // GIVEN + Qt3DInput::Input::InputSequence backendInputSequence; + Qt3DInput::QInputSequence inputSequence; + Qt3DInput::QActionInput actionInput; + + inputSequence.setTimeout(250); + inputSequence.setButtonInterval(100); + inputSequence.addSequence(&actionInput); + + // WHEN + simulateInitialization(&inputSequence, &backendInputSequence); + + // THEN + QCOMPARE(backendInputSequence.peerId(), inputSequence.id()); + QCOMPARE(backendInputSequence.isEnabled(), inputSequence.isEnabled()); + QCOMPARE(backendInputSequence.timeout(), inputSequence.timeout() * 1000000); + QCOMPARE(backendInputSequence.buttonInterval(), inputSequence.buttonInterval() * 1000000); + QCOMPARE(backendInputSequence.sequences().size(), inputSequence.sequences().size()); + + const int inputsCount = backendInputSequence.sequences().size(); + if (inputsCount > 0) { + for (int i = 0; i < inputsCount; ++i) + QCOMPARE(backendInputSequence.sequences().at(i), inputSequence.sequences().at(i)->id()); + } + } + + void shouldHaveInitialAndCleanedUpStates() + { + // GIVEN + Qt3DInput::Input::InputSequence backendInputSequence; + + // THEN + QVERIFY(backendInputSequence.peerId().isNull()); + QCOMPARE(backendInputSequence.isEnabled(), false); + QCOMPARE(backendInputSequence.timeout(), 0); + QCOMPARE(backendInputSequence.buttonInterval(), 0); + QCOMPARE(backendInputSequence.sequences().size(), 0); + + // GIVEN + Qt3DInput::QInputSequence inputSequence; + Qt3DInput::QActionInput actionInput; + + inputSequence.setTimeout(250); + inputSequence.setButtonInterval(100); + inputSequence.addSequence(&actionInput); + + // WHEN + simulateInitialization(&inputSequence, &backendInputSequence); + backendInputSequence.cleanup(); + + // THEN + QCOMPARE(backendInputSequence.isEnabled(), false); + QCOMPARE(backendInputSequence.timeout(), 0); + QCOMPARE(backendInputSequence.buttonInterval(), 0); + QCOMPARE(backendInputSequence.sequences().size(), 0); + } + + void shouldHandlePropertyChanges() + { + // GIVEN + Qt3DInput::Input::InputSequence backendInputSequence; + + // WHEN + Qt3DCore::QPropertyUpdatedChangePtr updateChange(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setValue(250); + updateChange->setPropertyName("timeout"); + backendInputSequence.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendInputSequence.timeout(), 250000000); + + // WHEN + updateChange.reset(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setValue(150); + updateChange->setPropertyName("buttonInterval"); + backendInputSequence.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendInputSequence.buttonInterval(), 150000000); + + // WHEN + updateChange.reset(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId())); + updateChange->setPropertyName("enabled"); + updateChange->setValue(true); + backendInputSequence.sceneChangeEvent(updateChange); + + // THEN + QCOMPARE(backendInputSequence.isEnabled(), true); + + // WHEN + Qt3DInput::QActionInput input; + const Qt3DCore::QNodeId inputId = input.id(); + const auto nodeAddedChange = Qt3DCore::QPropertyNodeAddedChangePtr::create(Qt3DCore::QNodeId(), &input); + nodeAddedChange->setPropertyName("sequence"); + backendInputSequence.sceneChangeEvent(nodeAddedChange); + + // THEN + QCOMPARE(backendInputSequence.sequences().size(), 1); + QCOMPARE(backendInputSequence.sequences().first(), inputId); + + // WHEN + const auto nodeRemovedChange = Qt3DCore::QPropertyNodeRemovedChangePtr::create(Qt3DCore::QNodeId(), &input); + nodeRemovedChange->setPropertyName("sequence"); + backendInputSequence.sceneChangeEvent(nodeRemovedChange); + + // THEN + QCOMPARE(backendInputSequence.sequences().size(), 0); + } + + void shouldActivateWhenSequenceIsConsumedInOrderOnly() + { + // GIVEN + TestDeviceIntegration deviceIntegration; + TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); + TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); + Qt3DInput::Input::InputHandler handler; + handler.addInputDeviceIntegration(&deviceIntegration); + + auto firstInput = new Qt3DInput::QActionInput; + firstInput->setButtons(QVector<int>() << Qt::Key_Q << Qt::Key_A); + firstInput->setSourceDevice(device); + auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); + simulateInitialization(firstInput, backendFirstInput); + + auto secondInput = new Qt3DInput::QActionInput; + secondInput->setButtons(QVector<int>() << Qt::Key_S << Qt::Key_W); + secondInput->setSourceDevice(device); + auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); + simulateInitialization(secondInput, backendSecondInput); + + auto thirdInput = new Qt3DInput::QActionInput; + thirdInput->setButtons(QVector<int>() << Qt::Key_D << Qt::Key_E); + thirdInput->setSourceDevice(device); + auto backendThirdInput = handler.actionInputManager()->getOrCreateResource(thirdInput->id()); + simulateInitialization(thirdInput, backendThirdInput); + + Qt3DInput::Input::InputSequence backendInputSequence; + Qt3DInput::QInputSequence inputSequence; + inputSequence.setButtonInterval(150); + inputSequence.setTimeout(450); + inputSequence.addSequence(firstInput); + inputSequence.addSequence(secondInput); + inputSequence.addSequence(thirdInput); + simulateInitialization(&inputSequence, &backendInputSequence); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Up, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1000000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Up, false); + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, false); + deviceBackend->setButtonPressed(Qt::Key_S, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1200000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, false); + deviceBackend->setButtonPressed(Qt::Key_E, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1300000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_E, false); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1400000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1500000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, false); + deviceBackend->setButtonPressed(Qt::Key_S, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1600000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, false); + deviceBackend->setButtonPressed(Qt::Key_E, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1700000000), true); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_E, false); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1800000000), false); + + + // Now out of order + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1900000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, false); + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 2000000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, false); + deviceBackend->setButtonPressed(Qt::Key_D, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 2100000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_D, false); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 22000000000), false); + } + + void shouldRespectSequenceTimeout() + { + // GIVEN + TestDeviceIntegration deviceIntegration; + TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); + TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); + Qt3DInput::Input::InputHandler handler; + handler.addInputDeviceIntegration(&deviceIntegration); + + auto firstInput = new Qt3DInput::QActionInput; + firstInput->setButtons(QVector<int>() << Qt::Key_Q << Qt::Key_A); + firstInput->setSourceDevice(device); + auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); + simulateInitialization(firstInput, backendFirstInput); + + auto secondInput = new Qt3DInput::QActionInput; + secondInput->setButtons(QVector<int>() << Qt::Key_S << Qt::Key_W); + secondInput->setSourceDevice(device); + auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); + simulateInitialization(secondInput, backendSecondInput); + + auto thirdInput = new Qt3DInput::QActionInput; + thirdInput->setButtons(QVector<int>() << Qt::Key_D << Qt::Key_E); + thirdInput->setSourceDevice(device); + auto backendThirdInput = handler.actionInputManager()->getOrCreateResource(thirdInput->id()); + simulateInitialization(thirdInput, backendThirdInput); + + Qt3DInput::Input::InputSequence backendInputSequence; + Qt3DInput::QInputSequence inputSequence; + inputSequence.setButtonInterval(250); + inputSequence.setTimeout(450); + inputSequence.addSequence(firstInput); + inputSequence.addSequence(secondInput); + inputSequence.addSequence(thirdInput); + simulateInitialization(&inputSequence, &backendInputSequence); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, false); + deviceBackend->setButtonPressed(Qt::Key_S, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1300000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, false); + deviceBackend->setButtonPressed(Qt::Key_E, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1600000000), false); // Too late + } + + void shouldRespectSequenceButtonInterval() + { + // GIVEN + TestDeviceIntegration deviceIntegration; + TestDevice *device = deviceIntegration.createPhysicalDevice("keyboard"); + TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(device->id()); + Qt3DInput::Input::InputHandler handler; + handler.addInputDeviceIntegration(&deviceIntegration); + + auto firstInput = new Qt3DInput::QActionInput; + firstInput->setButtons(QVector<int>() << Qt::Key_Q << Qt::Key_A); + firstInput->setSourceDevice(device); + auto backendFirstInput = handler.actionInputManager()->getOrCreateResource(firstInput->id()); + simulateInitialization(firstInput, backendFirstInput); + + auto secondInput = new Qt3DInput::QActionInput; + secondInput->setButtons(QVector<int>() << Qt::Key_S << Qt::Key_W); + secondInput->setSourceDevice(device); + auto backendSecondInput = handler.actionInputManager()->getOrCreateResource(secondInput->id()); + simulateInitialization(secondInput, backendSecondInput); + + auto thirdInput = new Qt3DInput::QActionInput; + thirdInput->setButtons(QVector<int>() << Qt::Key_D << Qt::Key_E); + thirdInput->setSourceDevice(device); + auto backendThirdInput = handler.actionInputManager()->getOrCreateResource(thirdInput->id()); + simulateInitialization(thirdInput, backendThirdInput); + + Qt3DInput::Input::InputSequence backendInputSequence; + Qt3DInput::QInputSequence inputSequence; + inputSequence.setButtonInterval(100); + inputSequence.setTimeout(450); + inputSequence.addSequence(firstInput); + inputSequence.addSequence(secondInput); + inputSequence.addSequence(thirdInput); + simulateInitialization(&inputSequence, &backendInputSequence); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1100000000), false); + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_Q, false); + deviceBackend->setButtonPressed(Qt::Key_S, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1250000000), false); // Too late + + // WHEN + deviceBackend->setButtonPressed(Qt::Key_S, false); + deviceBackend->setButtonPressed(Qt::Key_E, true); + + // THEN + QCOMPARE(backendInputSequence.process(&handler, 1300000000), false); + } +}; + +QTEST_APPLESS_MAIN(tst_InputSequence) + +#include "tst_inputsequence.moc" diff --git a/tests/auto/input/keyboardhandler/keyboardhandler.pro b/tests/auto/input/keyboardhandler/keyboardhandler.pro index f3f7b6c63..2e28c5045 100644 --- a/tests/auto/input/keyboardhandler/keyboardhandler.pro +++ b/tests/auto/input/keyboardhandler/keyboardhandler.pro @@ -8,5 +8,4 @@ CONFIG += testcase SOURCES += tst_keyboardhandler.cpp -include(../../core/common/common.pri) include(../commons/commons.pri) |