| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 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 | #include <QtTest/QTest> |
| 30 | #include <qbackendnodetester.h> |
| 31 | #include <Qt3DCore/private/qnode_p.h> |
| 32 | #include <Qt3DCore/private/qscene_p.h> |
| 33 | #include <Qt3DInput/private/abstractaxisinput_p.h> |
| 34 | #include <Qt3DInput/QAbstractAxisInput> |
| 35 | #include <Qt3DInput/private/qabstractaxisinput_p.h> |
| 36 | #include "testdevice.h" |
| 37 | |
| 38 | class DummyAxisInput : public Qt3DInput::QAbstractAxisInput |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | public: |
| 42 | explicit DummyAxisInput(QNode *parent = nullptr) |
| 43 | : Qt3DInput::QAbstractAxisInput(*new Qt3DInput::QAbstractAxisInputPrivate, parent) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | Qt3DCore::QNodeCreatedChangeBasePtr createNodeCreationChange() const override |
| 49 | { |
| 50 | auto creationChange = Qt3DCore::QNodeCreatedChangePtr<Qt3DInput::QAbstractAxisInputData>::create(arguments: this); |
| 51 | auto &data = creationChange->data; |
| 52 | data.sourceDeviceId = qIdForNode(node: sourceDevice()); |
| 53 | return creationChange; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | class DummyAxisInputBackend : public Qt3DInput::Input::AbstractAxisInput |
| 58 | { |
| 59 | public: |
| 60 | explicit DummyAxisInputBackend() |
| 61 | : AbstractAxisInput() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | float process(Qt3DInput::Input::InputHandler *inputHandler, qint64 currentTime) override |
| 66 | { |
| 67 | Q_UNUSED(inputHandler); |
| 68 | Q_UNUSED(currentTime); |
| 69 | return 0.0f; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | class tst_AbstractAxisInput : public Qt3DCore::QBackendNodeTester |
| 75 | { |
| 76 | Q_OBJECT |
| 77 | |
| 78 | private Q_SLOTS: |
| 79 | |
| 80 | void checkPeerPropertyMirroring() |
| 81 | { |
| 82 | // GIVEN |
| 83 | DummyAxisInputBackend backendAxisInput; |
| 84 | DummyAxisInput axisInput; |
| 85 | TestDevice sourceDevice; |
| 86 | |
| 87 | axisInput.setSourceDevice(&sourceDevice); |
| 88 | |
| 89 | // WHEN |
| 90 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 91 | |
| 92 | // THEN |
| 93 | QCOMPARE(backendAxisInput.peerId(), axisInput.id()); |
| 94 | QCOMPARE(backendAxisInput.isEnabled(), axisInput.isEnabled()); |
| 95 | QCOMPARE(backendAxisInput.sourceDevice(), sourceDevice.id()); |
| 96 | } |
| 97 | |
| 98 | void checkInitialAndCleanedUpState() |
| 99 | { |
| 100 | // GIVEN |
| 101 | DummyAxisInputBackend backendAxisInput; |
| 102 | |
| 103 | // THEN |
| 104 | QVERIFY(backendAxisInput.peerId().isNull()); |
| 105 | QCOMPARE(backendAxisInput.isEnabled(), false); |
| 106 | QCOMPARE(backendAxisInput.sourceDevice(), Qt3DCore::QNodeId()); |
| 107 | |
| 108 | // GIVEN |
| 109 | DummyAxisInput axisInput; |
| 110 | TestDevice sourceDevice; |
| 111 | |
| 112 | axisInput.setSourceDevice(&sourceDevice); |
| 113 | |
| 114 | // WHEN |
| 115 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 116 | backendAxisInput.cleanup(); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(backendAxisInput.isEnabled(), false); |
| 120 | QCOMPARE(backendAxisInput.sourceDevice(), Qt3DCore::QNodeId()); |
| 121 | } |
| 122 | |
| 123 | void checkPropertyChanges() |
| 124 | { |
| 125 | // GIVEN |
| 126 | DummyAxisInput axisInput; |
| 127 | DummyAxisInputBackend backendAxisInput; |
| 128 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
| 129 | |
| 130 | // WHEN |
| 131 | axisInput.setEnabled(false); |
| 132 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
| 133 | |
| 134 | // THEN |
| 135 | QCOMPARE(backendAxisInput.isEnabled(), false); |
| 136 | |
| 137 | // WHEN |
| 138 | TestDevice device; |
| 139 | axisInput.setSourceDevice(&device); |
| 140 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
| 141 | |
| 142 | // THEN |
| 143 | QCOMPARE(backendAxisInput.sourceDevice(), device.id()); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | QTEST_APPLESS_MAIN(tst_AbstractAxisInput) |
| 148 | |
| 149 | #include "tst_abstractaxisinput.moc" |
| 150 | |