| 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/qmousedevice.h> | 
| 32 | #include <Qt3DInput/qmouseevent.h> | 
| 33 | #include <Qt3DInput/private/qmousedevice_p.h> | 
| 34 | #include <QObject> | 
| 35 | #include <QSignalSpy> | 
| 36 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> | 
| 37 | #include <Qt3DCore/qnodecreatedchange.h> | 
| 38 | #include "testpostmanarbiter.h" | 
| 39 |  | 
| 40 | class tst_QMouseDevice : public QObject | 
| 41 | { | 
| 42 |     Q_OBJECT | 
| 43 |  | 
| 44 | private Q_SLOTS: | 
| 45 |  | 
| 46 |     void checkDefaultConstruction() | 
| 47 |     { | 
| 48 |         // GIVEN | 
| 49 |         Qt3DInput::QMouseDevice mouseDevice; | 
| 50 |  | 
| 51 |         // THEN | 
| 52 |         QCOMPARE(mouseDevice.sensitivity(), 0.1f); | 
| 53 |         QCOMPARE(mouseDevice.updateAxesContinuously(), false); | 
| 54 |         QCOMPARE(mouseDevice.axisCount(), 4); | 
| 55 |         QCOMPARE(mouseDevice.buttonCount(), 3); | 
| 56 |         QCOMPARE(mouseDevice.axisNames(), QStringList() | 
| 57 |                  << QStringLiteral("X" ) | 
| 58 |                  << QStringLiteral("Y" ) | 
| 59 |                  << QStringLiteral("WheelX" ) | 
| 60 |                  << QStringLiteral("WheelY" )); | 
| 61 |         QCOMPARE(mouseDevice.buttonNames(), QStringList() | 
| 62 |                  << QStringLiteral("Left" ) | 
| 63 |                  << QStringLiteral("Right" ) | 
| 64 |                  << QStringLiteral("Center" )); | 
| 65 |  | 
| 66 |         QVERIFY(mouseDevice.axisIdentifier(QStringLiteral("X" )) == Qt3DInput::QMouseDevice::X); | 
| 67 |         QVERIFY(mouseDevice.axisIdentifier(QStringLiteral("Y" )) == Qt3DInput::QMouseDevice::Y); | 
| 68 |         QVERIFY(mouseDevice.axisIdentifier(QStringLiteral("WheelX" )) == Qt3DInput::QMouseDevice::WheelX); | 
| 69 |         QVERIFY(mouseDevice.axisIdentifier(QStringLiteral("WheelY" )) == Qt3DInput::QMouseDevice::WheelY); | 
| 70 |  | 
| 71 |         QVERIFY(mouseDevice.buttonIdentifier(QStringLiteral("Left" )) == Qt3DInput::QMouseEvent::LeftButton); | 
| 72 |         QVERIFY(mouseDevice.buttonIdentifier(QStringLiteral("Right" )) == Qt3DInput::QMouseEvent::RightButton); | 
| 73 |         QVERIFY(mouseDevice.buttonIdentifier(QStringLiteral("Center" )) == Qt3DInput::QMouseEvent::MiddleButton); | 
| 74 |     } | 
| 75 |  | 
| 76 |     void checkPropertyChanges() | 
| 77 |     { | 
| 78 |         // GIVEN | 
| 79 |         Qt3DInput::QMouseDevice mouseDevice; | 
| 80 |  | 
| 81 |         { | 
| 82 |             // WHEN | 
| 83 |             QSignalSpy spy(&mouseDevice, SIGNAL(sensitivityChanged(float))); | 
| 84 |             const float newValue = 0.5f; | 
| 85 |             mouseDevice.setSensitivity(newValue); | 
| 86 |  | 
| 87 |             // THEN | 
| 88 |             QVERIFY(spy.isValid()); | 
| 89 |             QCOMPARE(mouseDevice.sensitivity(), newValue); | 
| 90 |             QCOMPARE(spy.count(), 1); | 
| 91 |  | 
| 92 |             // WHEN | 
| 93 |             spy.clear(); | 
| 94 |             mouseDevice.setSensitivity(newValue); | 
| 95 |  | 
| 96 |             // THEN | 
| 97 |             QCOMPARE(mouseDevice.sensitivity(), newValue); | 
| 98 |             QCOMPARE(spy.count(), 0); | 
| 99 |         } | 
| 100 |         { | 
| 101 |             // WHEN | 
| 102 |             QSignalSpy spy(&mouseDevice, SIGNAL(updateAxesContinuouslyChanged(bool))); | 
| 103 |             const bool newValue = true; | 
| 104 |             mouseDevice.setUpdateAxesContinuously(newValue); | 
| 105 |  | 
| 106 |             // THEN | 
| 107 |             QVERIFY(spy.isValid()); | 
| 108 |             QCOMPARE(mouseDevice.updateAxesContinuously(), newValue); | 
| 109 |             QCOMPARE(spy.count(), 1); | 
| 110 |  | 
| 111 |             // WHEN | 
| 112 |             spy.clear(); | 
| 113 |             mouseDevice.setUpdateAxesContinuously(newValue); | 
| 114 |  | 
| 115 |             // THEN | 
| 116 |             QCOMPARE(mouseDevice.updateAxesContinuously(), newValue); | 
| 117 |             QCOMPARE(spy.count(), 0); | 
| 118 |         } | 
| 119 |     } | 
| 120 |  | 
| 121 |     void checkCreationData() | 
| 122 |     { | 
| 123 |         // GIVEN | 
| 124 |         Qt3DInput::QMouseDevice mouseDevice; | 
| 125 |  | 
| 126 |         mouseDevice.setSensitivity(0.8f); | 
| 127 |  | 
| 128 |         // WHEN | 
| 129 |         QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; | 
| 130 |  | 
| 131 |         { | 
| 132 |             Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&mouseDevice); | 
| 133 |             creationChanges = creationChangeGenerator.creationChanges(); | 
| 134 |         } | 
| 135 |  | 
| 136 |         // THEN | 
| 137 |         { | 
| 138 |             QCOMPARE(creationChanges.size(), 1); | 
| 139 |  | 
| 140 |             const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QMouseDeviceData>>(src: creationChanges.first()); | 
| 141 |             const Qt3DInput::QMouseDeviceData cloneData = creationChangeData->data; | 
| 142 |  | 
| 143 |             QCOMPARE(mouseDevice.sensitivity(), cloneData.sensitivity); | 
| 144 |             QCOMPARE(mouseDevice.id(), creationChangeData->subjectId()); | 
| 145 |             QCOMPARE(mouseDevice.isEnabled(), true); | 
| 146 |             QCOMPARE(mouseDevice.isEnabled(), creationChangeData->isNodeEnabled()); | 
| 147 |             QCOMPARE(mouseDevice.metaObject(), creationChangeData->metaObject()); | 
| 148 |         } | 
| 149 |  | 
| 150 |         // WHEN | 
| 151 |         mouseDevice.setEnabled(false); | 
| 152 |  | 
| 153 |         { | 
| 154 |             Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&mouseDevice); | 
| 155 |             creationChanges = creationChangeGenerator.creationChanges(); | 
| 156 |         } | 
| 157 |  | 
| 158 |         // THEN | 
| 159 |         { | 
| 160 |             QCOMPARE(creationChanges.size(), 1); | 
| 161 |  | 
| 162 |             const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QMouseDeviceData>>(src: creationChanges.first()); | 
| 163 |             const Qt3DInput::QMouseDeviceData cloneData = creationChangeData->data; | 
| 164 |  | 
| 165 |             QCOMPARE(mouseDevice.sensitivity(), cloneData.sensitivity); | 
| 166 |             QCOMPARE(mouseDevice.id(), creationChangeData->subjectId()); | 
| 167 |             QCOMPARE(mouseDevice.isEnabled(), false); | 
| 168 |             QCOMPARE(mouseDevice.isEnabled(), creationChangeData->isNodeEnabled()); | 
| 169 |             QCOMPARE(mouseDevice.metaObject(), creationChangeData->metaObject()); | 
| 170 |         } | 
| 171 |     } | 
| 172 |  | 
| 173 |     void checkSensitivityUpdate() | 
| 174 |     { | 
| 175 |         // GIVEN | 
| 176 |         TestArbiter arbiter; | 
| 177 |         Qt3DInput::QMouseDevice mouseDevice; | 
| 178 |         arbiter.setArbiterOnNode(&mouseDevice); | 
| 179 |  | 
| 180 |         { | 
| 181 |             // WHEN | 
| 182 |             mouseDevice.setSensitivity(0.7f); | 
| 183 |             // THEN | 
| 184 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 185 |             QCOMPARE(arbiter.dirtyNodes.front(), &mouseDevice); | 
| 186 |         } | 
| 187 |  | 
| 188 |         { | 
| 189 |             // WHEN | 
| 190 |             mouseDevice.setSensitivity(0.7f); | 
| 191 |  | 
| 192 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 193 |             QCOMPARE(arbiter.dirtyNodes.front(), &mouseDevice); | 
| 194 |         } | 
| 195 |  | 
| 196 |     } | 
| 197 |  | 
| 198 |     void checkUpdateAxesContinuouslyUpdate() | 
| 199 |     { | 
| 200 |         // GIVEN | 
| 201 |         TestArbiter arbiter; | 
| 202 |         Qt3DInput::QMouseDevice mouseDevice; | 
| 203 |         arbiter.setArbiterOnNode(&mouseDevice); | 
| 204 |  | 
| 205 |         { | 
| 206 |             // WHEN | 
| 207 |             mouseDevice.setUpdateAxesContinuously(true); | 
| 208 |             // THEN | 
| 209 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 210 |             QCOMPARE(arbiter.dirtyNodes.front(), &mouseDevice); | 
| 211 |         } | 
| 212 |  | 
| 213 |         { | 
| 214 |             // WHEN | 
| 215 |             mouseDevice.setSensitivity(true); | 
| 216 |  | 
| 217 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 218 |             QCOMPARE(arbiter.dirtyNodes.front(), &mouseDevice); | 
| 219 |         } | 
| 220 |  | 
| 221 |     } | 
| 222 |  | 
| 223 | }; | 
| 224 |  | 
| 225 | QTEST_MAIN(tst_QMouseDevice) | 
| 226 |  | 
| 227 | #include "tst_qmousedevice.moc" | 
| 228 |  |