| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DCore/private/qnode_p.h> |
| 6 | #include <Qt3DCore/private/qscene_p.h> |
| 7 | |
| 8 | #include <Qt3DCore/QAttribute> |
| 9 | #include <Qt3DCore/private/qattribute_p.h> |
| 10 | #include <Qt3DCore/QBuffer> |
| 11 | |
| 12 | #include "testarbiter.h" |
| 13 | |
| 14 | class tst_QAttribute: public QObject |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | public: |
| 18 | tst_QAttribute() |
| 19 | { |
| 20 | qRegisterMetaType<Qt3DCore::QBuffer*>("Qt3DCore::QBuffer*"); |
| 21 | } |
| 22 | |
| 23 | private Q_SLOTS: |
| 24 | void shouldHaveDefaultAttributeNames() |
| 25 | { |
| 26 | // GIVEN |
| 27 | Qt3DCore::QAttribute attribute; |
| 28 | |
| 29 | // THEN |
| 30 | QCOMPARE(Qt3DCore::QAttribute::defaultPositionAttributeName(), QStringLiteral("vertexPosition")); |
| 31 | QCOMPARE(Qt3DCore::QAttribute::defaultNormalAttributeName(), QStringLiteral("vertexNormal")); |
| 32 | QCOMPARE(Qt3DCore::QAttribute::defaultColorAttributeName(), QStringLiteral("vertexColor")); |
| 33 | QCOMPARE(Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName(), QStringLiteral("vertexTexCoord")); |
| 34 | QCOMPARE(Qt3DCore::QAttribute::defaultTangentAttributeName(), QStringLiteral("vertexTangent")); |
| 35 | QCOMPARE(Qt3DCore::QAttribute::defaultJointIndicesAttributeName(), QStringLiteral("vertexJointIndices")); |
| 36 | QCOMPARE(Qt3DCore::QAttribute::defaultJointWeightsAttributeName(), QStringLiteral("vertexJointWeights")); |
| 37 | |
| 38 | QCOMPARE(attribute.property("defaultPositionAttributeName").toString(), |
| 39 | Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 40 | QCOMPARE(attribute.property("defaultNormalAttributeName").toString(), |
| 41 | Qt3DCore::QAttribute::defaultNormalAttributeName()); |
| 42 | QCOMPARE(attribute.property("defaultColorAttributeName").toString(), |
| 43 | Qt3DCore::QAttribute::defaultColorAttributeName()); |
| 44 | QCOMPARE(attribute.property("defaultTextureCoordinateAttributeName").toString(), |
| 45 | Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName()); |
| 46 | QCOMPARE(attribute.property("defaultTangentAttributeName").toString(), |
| 47 | Qt3DCore::QAttribute::defaultTangentAttributeName()); |
| 48 | QCOMPARE(attribute.property("defaultJointIndicesAttributeName").toString(), |
| 49 | Qt3DCore::QAttribute::defaultJointIndicesAttributeName()); |
| 50 | QCOMPARE(attribute.property("defaultJointWeightsAttributeName").toString(), |
| 51 | Qt3DCore::QAttribute::defaultJointWeightsAttributeName()); |
| 52 | } |
| 53 | |
| 54 | void checkPropertyUpdates() |
| 55 | { |
| 56 | // GIVEN |
| 57 | TestArbiter arbiter; |
| 58 | QScopedPointer<Qt3DCore::QAttribute> attribute(new Qt3DCore::QAttribute()); |
| 59 | arbiter.setArbiterOnNode(attribute.data()); |
| 60 | |
| 61 | // WHEN |
| 62 | attribute->setVertexBaseType(Qt3DCore::QAttribute::Double); |
| 63 | |
| 64 | // THEN |
| 65 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 66 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 67 | |
| 68 | arbiter.clear(); |
| 69 | |
| 70 | // WHEN |
| 71 | attribute->setVertexSize(4); |
| 72 | |
| 73 | // THEN |
| 74 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 75 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 76 | |
| 77 | arbiter.clear(); |
| 78 | |
| 79 | // WHEN |
| 80 | attribute->setName(QStringLiteral("Duntov")); |
| 81 | |
| 82 | // THEN |
| 83 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 84 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 85 | |
| 86 | arbiter.clear(); |
| 87 | |
| 88 | // WHEN |
| 89 | attribute->setCount(883); |
| 90 | |
| 91 | // THEN |
| 92 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 93 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 94 | |
| 95 | arbiter.clear(); |
| 96 | |
| 97 | // WHEN |
| 98 | attribute->setByteStride(1340); |
| 99 | |
| 100 | // THEN |
| 101 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 102 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 103 | |
| 104 | arbiter.clear(); |
| 105 | |
| 106 | // WHEN |
| 107 | attribute->setByteOffset(1584); |
| 108 | |
| 109 | // THEN |
| 110 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 111 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 112 | |
| 113 | arbiter.clear(); |
| 114 | |
| 115 | // WHEN |
| 116 | attribute->setDivisor(1450); |
| 117 | |
| 118 | // THEN |
| 119 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 120 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 121 | |
| 122 | arbiter.clear(); |
| 123 | |
| 124 | // WHEN |
| 125 | attribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute); |
| 126 | |
| 127 | // THEN |
| 128 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 129 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 130 | |
| 131 | arbiter.clear(); |
| 132 | |
| 133 | // WHEN |
| 134 | Qt3DCore::QBuffer buf; |
| 135 | attribute->setBuffer(&buf); |
| 136 | |
| 137 | // THEN |
| 138 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 139 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 140 | |
| 141 | arbiter.clear(); |
| 142 | |
| 143 | // WHEN |
| 144 | Qt3DCore::QBuffer buf2; |
| 145 | attribute->setBuffer(&buf2); |
| 146 | |
| 147 | // THEN |
| 148 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 149 | QCOMPARE(arbiter.dirtyNodes().front(), attribute.data()); |
| 150 | |
| 151 | arbiter.clear(); |
| 152 | } |
| 153 | |
| 154 | void checkBufferBookkeeping() |
| 155 | { |
| 156 | // GIVEN |
| 157 | QScopedPointer<Qt3DCore::QAttribute> attribute(new Qt3DCore::QAttribute); |
| 158 | { |
| 159 | // WHEN |
| 160 | Qt3DCore::QBuffer buf; |
| 161 | attribute->setBuffer(&buf); |
| 162 | |
| 163 | // THEN |
| 164 | QCOMPARE(buf.parent(), attribute.data()); |
| 165 | QCOMPARE(attribute->buffer(), &buf); |
| 166 | } |
| 167 | // THEN (Should not crash and parameter be unset) |
| 168 | QVERIFY(attribute->buffer() == nullptr); |
| 169 | |
| 170 | { |
| 171 | // WHEN |
| 172 | Qt3DCore::QAttribute someOtherAttribute; |
| 173 | QScopedPointer<Qt3DCore::QBuffer> buf(new Qt3DCore::QBuffer(&someOtherAttribute)); |
| 174 | attribute->setBuffer(buf.data()); |
| 175 | |
| 176 | // THEN |
| 177 | QCOMPARE(buf->parent(), &someOtherAttribute); |
| 178 | QCOMPARE(attribute->buffer(), buf.data()); |
| 179 | |
| 180 | // WHEN |
| 181 | attribute.reset(); |
| 182 | buf.reset(); |
| 183 | |
| 184 | // THEN Should not crash when the buffer is destroyed (tests for failed removal of destruction helper) |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | QTEST_MAIN(tst_QAttribute) |
| 190 | |
| 191 | #include "tst_qattribute.moc" |