| 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 | #include <QtTest/QTest> |
| 30 | #include <Qt3DRender/qparameter.h> |
| 31 | #include <Qt3DRender/private/qparameter_p.h> |
| 32 | #include <QObject> |
| 33 | #include <QSignalSpy> |
| 34 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
| 35 | #include <Qt3DCore/qnodecreatedchange.h> |
| 36 | #include <Qt3DCore/qentity.h> |
| 37 | #include "testpostmanarbiter.h" |
| 38 | |
| 39 | class tst_QParameter : public QObject |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | |
| 43 | private Q_SLOTS: |
| 44 | |
| 45 | void checkDefaultConstruction() |
| 46 | { |
| 47 | // GIVEN |
| 48 | Qt3DRender::QParameter parameter; |
| 49 | |
| 50 | // THEN |
| 51 | QCOMPARE(parameter.name(), QString()); |
| 52 | QCOMPARE(parameter.value(), QVariant()); |
| 53 | } |
| 54 | |
| 55 | void checkPropertyChanges() |
| 56 | { |
| 57 | // GIVEN |
| 58 | Qt3DRender::QParameter parameter; |
| 59 | |
| 60 | { |
| 61 | // WHEN |
| 62 | QSignalSpy spy(¶meter, SIGNAL(nameChanged(QString))); |
| 63 | const QString newValue = QStringLiteral("SomeName" ); |
| 64 | parameter.setName(newValue); |
| 65 | |
| 66 | // THEN |
| 67 | QVERIFY(spy.isValid()); |
| 68 | QCOMPARE(parameter.name(), newValue); |
| 69 | QCOMPARE(spy.count(), 1); |
| 70 | |
| 71 | // WHEN |
| 72 | spy.clear(); |
| 73 | parameter.setName(newValue); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(parameter.name(), newValue); |
| 77 | QCOMPARE(spy.count(), 0); |
| 78 | } |
| 79 | { |
| 80 | // WHEN |
| 81 | QSignalSpy spy(¶meter, SIGNAL(valueChanged(QVariant))); |
| 82 | const QVariant newValue(454.0f); |
| 83 | parameter.setValue(newValue); |
| 84 | |
| 85 | // THEN |
| 86 | QVERIFY(spy.isValid()); |
| 87 | QCOMPARE(parameter.value(), newValue); |
| 88 | QCOMPARE(spy.count(), 1); |
| 89 | |
| 90 | // WHEN |
| 91 | spy.clear(); |
| 92 | parameter.setValue(newValue); |
| 93 | |
| 94 | // THEN |
| 95 | QCOMPARE(parameter.value(), newValue); |
| 96 | QCOMPARE(spy.count(), 0); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void checkCreationData() |
| 101 | { |
| 102 | // GIVEN |
| 103 | Qt3DRender::QParameter parameter; |
| 104 | |
| 105 | parameter.setName(QStringLiteral("TwinCam" )); |
| 106 | parameter.setValue(QVariant(427)); |
| 107 | |
| 108 | // WHEN |
| 109 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
| 110 | |
| 111 | { |
| 112 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(¶meter); |
| 113 | creationChanges = creationChangeGenerator.creationChanges(); |
| 114 | } |
| 115 | |
| 116 | // THEN |
| 117 | { |
| 118 | QCOMPARE(creationChanges.size(), 1); |
| 119 | |
| 120 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QParameterData>>(src: creationChanges.first()); |
| 121 | const Qt3DRender::QParameterData cloneData = creationChangeData->data; |
| 122 | |
| 123 | QCOMPARE(parameter.name(), cloneData.name); |
| 124 | QCOMPARE(parameter.value(), cloneData.backendValue); |
| 125 | QCOMPARE(parameter.id(), creationChangeData->subjectId()); |
| 126 | QCOMPARE(parameter.isEnabled(), true); |
| 127 | QCOMPARE(parameter.isEnabled(), creationChangeData->isNodeEnabled()); |
| 128 | QCOMPARE(parameter.metaObject(), creationChangeData->metaObject()); |
| 129 | } |
| 130 | |
| 131 | // WHEN |
| 132 | parameter.setEnabled(false); |
| 133 | |
| 134 | { |
| 135 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(¶meter); |
| 136 | creationChanges = creationChangeGenerator.creationChanges(); |
| 137 | } |
| 138 | |
| 139 | // THEN |
| 140 | { |
| 141 | QCOMPARE(creationChanges.size(), 1); |
| 142 | |
| 143 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QParameterData>>(src: creationChanges.first()); |
| 144 | const Qt3DRender::QParameterData cloneData = creationChangeData->data; |
| 145 | |
| 146 | QCOMPARE(parameter.name(), cloneData.name); |
| 147 | QCOMPARE(parameter.value(), cloneData.backendValue); |
| 148 | QCOMPARE(parameter.id(), creationChangeData->subjectId()); |
| 149 | QCOMPARE(parameter.isEnabled(), false); |
| 150 | QCOMPARE(parameter.isEnabled(), creationChangeData->isNodeEnabled()); |
| 151 | QCOMPARE(parameter.metaObject(), creationChangeData->metaObject()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void checkNameUpdate() |
| 156 | { |
| 157 | // GIVEN |
| 158 | TestArbiter arbiter; |
| 159 | Qt3DRender::QParameter parameter; |
| 160 | arbiter.setArbiterOnNode(¶meter); |
| 161 | |
| 162 | { |
| 163 | // WHEN |
| 164 | parameter.setName(QStringLiteral("Bruce" )); |
| 165 | QCoreApplication::processEvents(); |
| 166 | |
| 167 | // THEN |
| 168 | QCOMPARE(arbiter.events.size(), 0); |
| 169 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 170 | QCOMPARE(arbiter.dirtyNodes.front(), ¶meter); |
| 171 | |
| 172 | arbiter.dirtyNodes.clear(); |
| 173 | } |
| 174 | |
| 175 | { |
| 176 | // WHEN |
| 177 | parameter.setName(QStringLiteral("Bruce" )); |
| 178 | QCoreApplication::processEvents(); |
| 179 | |
| 180 | // THEN |
| 181 | QCOMPARE(arbiter.events.size(), 0); |
| 182 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | void checkValueUpdate() |
| 188 | { |
| 189 | // GIVEN |
| 190 | TestArbiter arbiter; |
| 191 | Qt3DRender::QParameter parameter; |
| 192 | arbiter.setArbiterOnNode(¶meter); |
| 193 | |
| 194 | { |
| 195 | // WHEN |
| 196 | parameter.setValue(QVariant(383.0f)); |
| 197 | QCoreApplication::processEvents(); |
| 198 | |
| 199 | // THEN |
| 200 | QCOMPARE(arbiter.events.size(), 0); |
| 201 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 202 | QCOMPARE(arbiter.dirtyNodes.front(), ¶meter); |
| 203 | |
| 204 | arbiter.dirtyNodes.clear(); |
| 205 | } |
| 206 | |
| 207 | { |
| 208 | // WHEN |
| 209 | parameter.setValue(QVariant(383.0f)); |
| 210 | QCoreApplication::processEvents(); |
| 211 | |
| 212 | // THEN |
| 213 | QCOMPARE(arbiter.events.size(), 0); |
| 214 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 215 | } |
| 216 | |
| 217 | // WHEN -> QNode -> QNodeId |
| 218 | { |
| 219 | Qt3DCore::QEntity testEntity; |
| 220 | |
| 221 | { |
| 222 | // WHEN |
| 223 | parameter.setValue(QVariant::fromValue(value: &testEntity)); |
| 224 | QCoreApplication::processEvents(); |
| 225 | |
| 226 | // THEN |
| 227 | QCOMPARE(arbiter.events.size(), 0); |
| 228 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 229 | QCOMPARE(arbiter.dirtyNodes.front(), ¶meter); |
| 230 | |
| 231 | arbiter.dirtyNodes.clear(); |
| 232 | } |
| 233 | |
| 234 | { |
| 235 | // WHEN |
| 236 | parameter.setValue(QVariant::fromValue(value: &testEntity)); |
| 237 | QCoreApplication::processEvents(); |
| 238 | |
| 239 | // THEN |
| 240 | QCOMPARE(arbiter.events.size(), 0); |
| 241 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | void checkBookeeping() |
| 249 | { |
| 250 | // GIVEN |
| 251 | Qt3DRender::QParameter parameter; |
| 252 | QSignalSpy spy(¶meter, SIGNAL(valueChanged(QVariant))); |
| 253 | |
| 254 | { |
| 255 | // WHEN |
| 256 | QScopedPointer<Qt3DCore::QNode> node(new Qt3DCore::QNode()); |
| 257 | parameter.setValue(QVariant::fromValue(value: node.data())); |
| 258 | |
| 259 | // THEN |
| 260 | QCOMPARE(spy.count(), 1); |
| 261 | QCOMPARE(parameter.value(), QVariant::fromValue(node.data())); |
| 262 | spy.clear(); |
| 263 | } |
| 264 | |
| 265 | // THEN |
| 266 | QCOMPARE(spy.count(), 1); |
| 267 | QCOMPARE(parameter.value(), QVariant()); |
| 268 | } |
| 269 | |
| 270 | }; |
| 271 | |
| 272 | QTEST_MAIN(tst_QParameter) |
| 273 | |
| 274 | #include "tst_qparameter.moc" |
| 275 | |