| 1 | // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DAnimation/qchannelmapping.h> |
| 6 | #include <Qt3DAnimation/private/qabstractchannelmapping_p.h> |
| 7 | #include <Qt3DAnimation/private/qchannelmapping_p.h> |
| 8 | #include <Qt3DCore/qentity.h> |
| 9 | #include <QObject> |
| 10 | #include <QSignalSpy> |
| 11 | #include <QQuaternion> |
| 12 | #include <testarbiter.h> |
| 13 | |
| 14 | class tst_QTargetEntity : public Qt3DCore::QEntity |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | Q_PROPERTY(QQuaternion rotation MEMBER m_rotation NOTIFY rotationChanged) |
| 18 | Q_PROPERTY(QVector3D translation MEMBER m_translation NOTIFY translationChanged) |
| 19 | Q_PROPERTY(QVector3D scale MEMBER m_scale NOTIFY scaleChanged) |
| 20 | Q_PROPERTY(float floatProperty MEMBER m_floatProperty NOTIFY floatPropertyChanged) |
| 21 | Q_PROPERTY(QVector2D vec2Property MEMBER m_vec2Property NOTIFY vec2PropertyChanged) |
| 22 | Q_PROPERTY(QVector3D vec3Property MEMBER m_vec3Property NOTIFY vec3PropertyChanged) |
| 23 | Q_PROPERTY(QVector4D vec4Property MEMBER m_vec4Property NOTIFY vec4PropertyChanged) |
| 24 | Q_PROPERTY(QQuaternion quaternionProperty MEMBER m_quaternionProperty NOTIFY quaternionPropertyChanged) |
| 25 | Q_PROPERTY(QVariantList listProperty MEMBER m_listProperty NOTIFY listPropertyChanged) |
| 26 | Q_PROPERTY(QList<float> vecProperty MEMBER m_vecProperty NOTIFY vecPropertyChanged) |
| 27 | |
| 28 | signals: |
| 29 | void rotationChanged(); |
| 30 | void translationChanged(); |
| 31 | void scaleChanged(); |
| 32 | void floatPropertyChanged(); |
| 33 | void vec2PropertyChanged(); |
| 34 | void vec3PropertyChanged(); |
| 35 | void vec4PropertyChanged(); |
| 36 | void quaternionPropertyChanged(); |
| 37 | void listPropertyChanged(); |
| 38 | void vecPropertyChanged(); |
| 39 | |
| 40 | private: |
| 41 | QQuaternion m_rotation; |
| 42 | QVector3D m_translation; |
| 43 | QVector3D m_scale; |
| 44 | float m_floatProperty; |
| 45 | QVector2D m_vec2Property; |
| 46 | QVector3D m_vec3Property; |
| 47 | QVector4D m_vec4Property; |
| 48 | QQuaternion m_quaternionProperty; |
| 49 | QVariantList m_listProperty; |
| 50 | QList<float> m_vecProperty; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | class tst_QChannelMapping : public QObject |
| 55 | { |
| 56 | Q_OBJECT |
| 57 | |
| 58 | private Q_SLOTS: |
| 59 | void checkDefaultConstruction() |
| 60 | { |
| 61 | // GIVEN |
| 62 | Qt3DAnimation::QChannelMapping mapping; |
| 63 | |
| 64 | // THEN |
| 65 | QCOMPARE(mapping.channelName(), QString()); |
| 66 | QCOMPARE(mapping.target(), static_cast<Qt3DCore::QNode *>(nullptr)); |
| 67 | QCOMPARE(mapping.property(), QString()); |
| 68 | |
| 69 | const Qt3DAnimation::QChannelMappingPrivate *d = |
| 70 | static_cast<const Qt3DAnimation::QChannelMappingPrivate *>( |
| 71 | Qt3DAnimation::QChannelMappingPrivate::get(&mapping)); |
| 72 | |
| 73 | QCOMPARE(d->m_type, static_cast<int>(QMetaType::UnknownType)); |
| 74 | QCOMPARE(d->m_componentCount, 0); |
| 75 | } |
| 76 | |
| 77 | void checkPropertyChanges() |
| 78 | { |
| 79 | // GIVEN |
| 80 | Qt3DAnimation::QChannelMapping mapping; |
| 81 | |
| 82 | { |
| 83 | // WHEN |
| 84 | QSignalSpy spy(&mapping, SIGNAL(channelNameChanged(QString))); |
| 85 | const QString newValue(QStringLiteral("Rotation")); |
| 86 | mapping.setChannelName(newValue); |
| 87 | |
| 88 | // THEN |
| 89 | QVERIFY(spy.isValid()); |
| 90 | QCOMPARE(mapping.channelName(), newValue); |
| 91 | QCOMPARE(spy.size(), 1); |
| 92 | |
| 93 | // WHEN |
| 94 | spy.clear(); |
| 95 | mapping.setChannelName(newValue); |
| 96 | |
| 97 | // THEN |
| 98 | QCOMPARE(mapping.channelName(), newValue); |
| 99 | QCOMPARE(spy.size(), 0); |
| 100 | } |
| 101 | |
| 102 | { |
| 103 | // WHEN |
| 104 | QSignalSpy spy(&mapping, SIGNAL(targetChanged(Qt3DCore::QNode*))); |
| 105 | auto newValue = new Qt3DCore::QEntity(); |
| 106 | mapping.setTarget(newValue); |
| 107 | |
| 108 | // THEN |
| 109 | QVERIFY(spy.isValid()); |
| 110 | QCOMPARE(mapping.target(), newValue); |
| 111 | QCOMPARE(newValue->parent(), &mapping); |
| 112 | QCOMPARE(spy.size(), 1); |
| 113 | |
| 114 | // WHEN |
| 115 | spy.clear(); |
| 116 | mapping.setTarget(newValue); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(mapping.target(), newValue); |
| 120 | QCOMPARE(spy.size(), 0); |
| 121 | } |
| 122 | |
| 123 | { |
| 124 | // WHEN |
| 125 | QSignalSpy spy(&mapping, SIGNAL(propertyChanged(QString))); |
| 126 | const QString newValue(QStringLiteral("rotation")); |
| 127 | mapping.setProperty(newValue); |
| 128 | |
| 129 | // THEN |
| 130 | QVERIFY(spy.isValid()); |
| 131 | QCOMPARE(mapping.property(), newValue); |
| 132 | QCOMPARE(spy.size(), 1); |
| 133 | |
| 134 | // WHEN |
| 135 | spy.clear(); |
| 136 | mapping.setProperty(newValue); |
| 137 | |
| 138 | // THEN |
| 139 | QCOMPARE(mapping.property(), newValue); |
| 140 | QCOMPARE(spy.size(), 0); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void checkPropertyUpdateChanges() |
| 145 | { |
| 146 | // GIVEN |
| 147 | TestArbiter arbiter; |
| 148 | Qt3DAnimation::QChannelMapping mapping; |
| 149 | QScopedPointer<Qt3DCore::QEntity> target(new tst_QTargetEntity()); |
| 150 | arbiter.setArbiterOnNode(&mapping); |
| 151 | |
| 152 | { |
| 153 | // WHEN |
| 154 | mapping.setChannelName(QStringLiteral("Scale")); |
| 155 | |
| 156 | // THEN |
| 157 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 158 | QCOMPARE(arbiter.dirtyNodes().front(), &mapping); |
| 159 | |
| 160 | arbiter.clear(); |
| 161 | |
| 162 | // WHEN |
| 163 | mapping.setChannelName(QStringLiteral("Scale")); |
| 164 | |
| 165 | // THEN |
| 166 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 167 | } |
| 168 | |
| 169 | { |
| 170 | // WHEN |
| 171 | mapping.setTarget(target.data()); |
| 172 | |
| 173 | // THEN |
| 174 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 175 | QCOMPARE(arbiter.dirtyNodes().front(), &mapping); |
| 176 | |
| 177 | arbiter.clear(); |
| 178 | |
| 179 | // WHEN |
| 180 | mapping.setTarget(target.data()); |
| 181 | |
| 182 | // THEN |
| 183 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 184 | } |
| 185 | |
| 186 | { |
| 187 | // WHEN |
| 188 | target->setProperty("scale", QVector3D(1.0f, 0.0f, 0.0f)); |
| 189 | mapping.setProperty(QStringLiteral("scale")); |
| 190 | QCoreApplication::processEvents(); |
| 191 | |
| 192 | // THEN |
| 193 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 194 | QCOMPARE(arbiter.dirtyNodes().front(), &mapping); |
| 195 | |
| 196 | arbiter.clear(); |
| 197 | |
| 198 | // WHEN |
| 199 | mapping.setProperty(QStringLiteral("scale")); |
| 200 | QCoreApplication::processEvents(); |
| 201 | |
| 202 | // THEN |
| 203 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void checkPropertyUpdateNameTypeAndComponentCount_data() |
| 208 | { |
| 209 | QTest::addColumn<QByteArray>("propertyName"); |
| 210 | QTest::addColumn<QVariant>("value"); |
| 211 | QTest::addColumn<int>("expectedType"); |
| 212 | QTest::addColumn<int>("expectedComponentCount"); |
| 213 | |
| 214 | QTest::newRow("float") << QByteArrayLiteral("floatProperty") << QVariant(1.0f) << static_cast<int>(QMetaType::Float) << 1; |
| 215 | QTest::newRow("vec2") << QByteArrayLiteral("vec2Property") << QVariant(QVector2D(1.0f, 1.0f)) << static_cast<int>(QMetaType::QVector2D) << 2; |
| 216 | QTest::newRow("vec3") << QByteArrayLiteral("vec3Property") << QVariant(QVector3D(1.0f, 1.0f, 1.0f)) << static_cast<int>(QMetaType::QVector3D) << 3; |
| 217 | QTest::newRow("vec4") << QByteArrayLiteral("vec4Property") << QVariant(QVector4D(1.0f, 1.0f, 1.0f, 1.0f)) << static_cast<int>(QMetaType::QVector4D) << 4; |
| 218 | QTest::newRow("quaternion") << QByteArrayLiteral("quaternionProperty") << QVariant(QQuaternion(1.0f, 1.0f, 1.0f, 1.0f)) << static_cast<int>(QMetaType::QQuaternion) << 4; |
| 219 | |
| 220 | QVariantList list = QVariantList() << QVariant(1.0f) << QVariant(1.0) << QVariant(1.0f) << QVariant(1.0f) << QVariant(1.0f); |
| 221 | QTest::newRow("variantlist") << QByteArrayLiteral("listProperty") << QVariant::fromValue(list) << static_cast<int>(QMetaType::QVariantList) << 5; |
| 222 | |
| 223 | QVector<float> vec(8); |
| 224 | QTest::newRow("vector") << QByteArrayLiteral("vecProperty") << QVariant::fromValue(vec) << qMetaTypeId<decltype(vec)>() << 8; |
| 225 | } |
| 226 | |
| 227 | void checkPropertyUpdateNameTypeAndComponentCount() |
| 228 | { |
| 229 | // GIVEN |
| 230 | QFETCH(QByteArray, propertyName); |
| 231 | QFETCH(QVariant, value); |
| 232 | QFETCH(int, expectedType); |
| 233 | QFETCH(int, expectedComponentCount); |
| 234 | |
| 235 | Q_UNUSED(expectedType); |
| 236 | Q_UNUSED(expectedComponentCount); |
| 237 | |
| 238 | TestArbiter arbiter; |
| 239 | Qt3DAnimation::QChannelMapping mapping; |
| 240 | QScopedPointer<Qt3DCore::QEntity> target(new tst_QTargetEntity()); |
| 241 | mapping.setTarget(target.data()); |
| 242 | arbiter.setArbiterOnNode(&mapping); |
| 243 | |
| 244 | { |
| 245 | // WHEN |
| 246 | target->setProperty(propertyName.constData(), value); |
| 247 | |
| 248 | // THEN |
| 249 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 250 | QCOMPARE(arbiter.dirtyNodes().front(), target.data()); |
| 251 | |
| 252 | arbiter.clear(); |
| 253 | |
| 254 | // THEN |
| 255 | mapping.setProperty(QString::fromLatin1(propertyName)); |
| 256 | |
| 257 | // THEN |
| 258 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | }; |
| 263 | |
| 264 | QTEST_MAIN(tst_QChannelMapping) |
| 265 | |
| 266 | #include "tst_qchannelmapping.moc" |