| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 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 | |
| 30 | #include <QtTest/QTest> |
| 31 | #include <Qt3DInput/qlogicaldevice.h> |
| 32 | #include <Qt3DInput/qaction.h> |
| 33 | #include <Qt3DInput/qaxis.h> |
| 34 | #include <Qt3DInput/private/qlogicaldevice_p.h> |
| 35 | #include <Qt3DInput/private/logicaldevice_p.h> |
| 36 | #include "qbackendnodetester.h" |
| 37 | |
| 38 | class tst_LogicalDevice : public Qt3DCore::QBackendNodeTester |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | private Q_SLOTS: |
| 43 | |
| 44 | void checkInitialState() |
| 45 | { |
| 46 | // GIVEN |
| 47 | Qt3DInput::Input::LogicalDevice backendLogicalDevice; |
| 48 | |
| 49 | // THEN |
| 50 | QVERIFY(backendLogicalDevice.peerId().isNull()); |
| 51 | QCOMPARE(backendLogicalDevice.isEnabled(), false); |
| 52 | QVERIFY(backendLogicalDevice.axes().empty()); |
| 53 | QVERIFY(backendLogicalDevice.actions().empty()); |
| 54 | } |
| 55 | |
| 56 | void checkCleanupState() |
| 57 | { |
| 58 | // GIVEN |
| 59 | Qt3DInput::QLogicalDevice logicalDevice; |
| 60 | Qt3DInput::Input::LogicalDevice backendLogicalDevice; |
| 61 | simulateInitializationSync(frontend: &logicalDevice, backend: &backendLogicalDevice); |
| 62 | |
| 63 | // WHEN |
| 64 | backendLogicalDevice.setEnabled(true); |
| 65 | |
| 66 | // WHEN |
| 67 | Qt3DInput::QAxis newAxisValue; |
| 68 | Qt3DInput::QAction newActionValue; |
| 69 | logicalDevice.addAxis(axis: &newAxisValue); |
| 70 | logicalDevice.addAction(action: &newActionValue); |
| 71 | backendLogicalDevice.syncFromFrontEnd(frontEnd: &logicalDevice, firstTime: false); |
| 72 | |
| 73 | // THEN |
| 74 | QCOMPARE(backendLogicalDevice.axes().size(), 1); |
| 75 | QCOMPARE(backendLogicalDevice.actions().size(), 1); |
| 76 | |
| 77 | // WHEN |
| 78 | backendLogicalDevice.cleanup(); |
| 79 | |
| 80 | // THEN |
| 81 | QCOMPARE(backendLogicalDevice.isEnabled(), false); |
| 82 | QCOMPARE(backendLogicalDevice.axes().size(), 0); |
| 83 | QCOMPARE(backendLogicalDevice.actions().size(), 0); |
| 84 | } |
| 85 | |
| 86 | void checkInitializeFromPeer() |
| 87 | { |
| 88 | // GIVEN |
| 89 | Qt3DInput::QLogicalDevice logicalDevice; |
| 90 | |
| 91 | Qt3DInput::QAction *action = new Qt3DInput::QAction(&logicalDevice); |
| 92 | Qt3DInput::QAxis *axis = new Qt3DInput::QAxis(&logicalDevice); |
| 93 | logicalDevice.addAction(action); |
| 94 | logicalDevice.addAxis(axis); |
| 95 | |
| 96 | { |
| 97 | // WHEN |
| 98 | Qt3DInput::Input::LogicalDevice backendLogicalDevice; |
| 99 | simulateInitializationSync(frontend: &logicalDevice, backend: &backendLogicalDevice); |
| 100 | |
| 101 | // THEN |
| 102 | QCOMPARE(backendLogicalDevice.isEnabled(), true); |
| 103 | QCOMPARE(backendLogicalDevice.axes().size(), 1); |
| 104 | QCOMPARE(backendLogicalDevice.axes().first(), axis->id()); |
| 105 | QCOMPARE(backendLogicalDevice.actions().size(), 1); |
| 106 | QCOMPARE(backendLogicalDevice.actions().first(), action->id()); |
| 107 | QCOMPARE(backendLogicalDevice.peerId(), logicalDevice.id()); |
| 108 | } |
| 109 | { |
| 110 | // WHEN |
| 111 | Qt3DInput::Input::LogicalDevice backendLogicalDevice; |
| 112 | logicalDevice.setEnabled(false); |
| 113 | simulateInitializationSync(frontend: &logicalDevice, backend: &backendLogicalDevice); |
| 114 | |
| 115 | // THEN |
| 116 | QCOMPARE(backendLogicalDevice.isEnabled(), false); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void checkSceneChangeEvents() |
| 121 | { |
| 122 | // GIVEN |
| 123 | Qt3DInput::QLogicalDevice logicalDevice; |
| 124 | Qt3DInput::Input::LogicalDevice backendLogicalDevice; |
| 125 | simulateInitializationSync(frontend: &logicalDevice, backend: &backendLogicalDevice); |
| 126 | |
| 127 | { |
| 128 | // WHEN |
| 129 | const bool newValue = false; |
| 130 | logicalDevice.setEnabled(newValue); |
| 131 | backendLogicalDevice.syncFromFrontEnd(frontEnd: &logicalDevice, firstTime: false); |
| 132 | |
| 133 | // THEN |
| 134 | QCOMPARE(backendLogicalDevice.isEnabled(), newValue); |
| 135 | } |
| 136 | { |
| 137 | // WHEN |
| 138 | Qt3DInput::QAxis newValue; |
| 139 | logicalDevice.addAxis(axis: &newValue); |
| 140 | backendLogicalDevice.syncFromFrontEnd(frontEnd: &logicalDevice, firstTime: false); |
| 141 | |
| 142 | // THEN |
| 143 | QCOMPARE(backendLogicalDevice.axes().size(), 1); |
| 144 | QCOMPARE(backendLogicalDevice.axes().first(), newValue.id()); |
| 145 | |
| 146 | // WHEN |
| 147 | logicalDevice.removeAxis(axis: &newValue); |
| 148 | backendLogicalDevice.syncFromFrontEnd(frontEnd: &logicalDevice, firstTime: false); |
| 149 | |
| 150 | // THEN |
| 151 | QCOMPARE(backendLogicalDevice.axes().size(), 0); |
| 152 | } |
| 153 | { |
| 154 | // WHEN |
| 155 | Qt3DInput::QAction newValue; |
| 156 | logicalDevice.addAction(action: &newValue); |
| 157 | backendLogicalDevice.syncFromFrontEnd(frontEnd: &logicalDevice, firstTime: false); |
| 158 | |
| 159 | // THEN |
| 160 | QCOMPARE(backendLogicalDevice.actions().size(), 1); |
| 161 | QCOMPARE(backendLogicalDevice.actions().first(), newValue.id()); |
| 162 | |
| 163 | // WHEN |
| 164 | logicalDevice.removeAction(action: &newValue); |
| 165 | backendLogicalDevice.syncFromFrontEnd(frontEnd: &logicalDevice, firstTime: false); |
| 166 | |
| 167 | // THEN |
| 168 | QCOMPARE(backendLogicalDevice.actions().size(), 0); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | }; |
| 173 | |
| 174 | QTEST_APPLESS_MAIN(tst_LogicalDevice) |
| 175 | |
| 176 | #include "tst_logicaldevice.moc" |
| 177 | |