| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 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 <Qt3DCore/private/qnode_p.h> | 
| 31 | #include <Qt3DCore/private/qscene_p.h> | 
| 32 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> | 
| 33 |  | 
| 34 | #include <Qt3DRender/qabstractlight.h> | 
| 35 | #include <Qt3DRender/private/qabstractlight_p.h> | 
| 36 | #include <Qt3DRender/qpointlight.h> | 
| 37 | #include <Qt3DRender/qdirectionallight.h> | 
| 38 | #include <Qt3DRender/qspotlight.h> | 
| 39 | #include <Qt3DRender/private/qpointlight_p.h> | 
| 40 | #include <Qt3DRender/private/qdirectionallight_p.h> | 
| 41 | #include <Qt3DRender/private/qspotlight_p.h> | 
| 42 |  | 
| 43 | #include "testpostmanarbiter.h" | 
| 44 |  | 
| 45 | class DummyLight : public Qt3DRender::QAbstractLight | 
| 46 | { | 
| 47 |     Q_OBJECT | 
| 48 |  | 
| 49 | public: | 
| 50 |     explicit DummyLight(Qt3DCore::QNode *parent = nullptr) | 
| 51 |         : QAbstractLight(*new Qt3DRender::QAbstractLightPrivate(QAbstractLight::PointLight), parent) | 
| 52 |     {} | 
| 53 | }; | 
| 54 |  | 
| 55 |  | 
| 56 | // We need to call QNode::clone which is protected | 
| 57 | // So we sublcass QNode instead of QObject | 
| 58 | class tst_QAbstractLight: public Qt3DCore::QNode | 
| 59 | { | 
| 60 |     Q_OBJECT | 
| 61 |  | 
| 62 | private Q_SLOTS: | 
| 63 |     // TO DO: Test should be rewritten to query the properties from the attached QShaderData | 
| 64 |  | 
| 65 | //    void checkLightCloning() | 
| 66 | //    { | 
| 67 | //        // GIVEN | 
| 68 | //        DummyLight light; | 
| 69 | //        light.setColor(Qt::red); | 
| 70 | //        light.setIntensity(0.5f); | 
| 71 |  | 
| 72 | //        // WHEN | 
| 73 | //        Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(material); | 
| 74 | //        QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); | 
| 75 |  | 
| 76 | //        // THEN | 
| 77 | //        QVERIFY(creationChanges.size() >= 1); | 
| 78 |  | 
| 79 | //        const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QA> creationChangeData = | 
| 80 | //                qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QMaterialData>>(creationChanges.first()); | 
| 81 | //        const Qt3DRender::QMaterialData &cloneData = creationChangeData->data; | 
| 82 |  | 
| 83 |  | 
| 84 | //        QScopedPointer<Qt3DRender::QAbstractLight> lightClone(static_cast<Qt3DRender::QAbstractLight *>(QNode::clone(&light))); | 
| 85 | //        QVERIFY(lightClone.data()); | 
| 86 | //        QCOMPARE(light.color(), lightClone->color()); | 
| 87 | //        QCOMPARE(light.intensity(), lightClone->intensity()); | 
| 88 | //    } | 
| 89 |  | 
| 90 | //    void checkPointLightCloning() | 
| 91 | //    { | 
| 92 | //        Qt3DRender::QPointLight pointLight; | 
| 93 | //        QCOMPARE(pointLight.type(), Qt3DRender::QAbstractLight::PointLight); | 
| 94 | //        pointLight.setColor(Qt::green); | 
| 95 | //        pointLight.setIntensity(0.5f); | 
| 96 | //        pointLight.setConstantAttenuation(0.5f); | 
| 97 | //        pointLight.setLinearAttenuation(0.0f);      // No actual event triggered as 0.0f is default | 
| 98 | //        pointLight.setQuadraticAttenuation(1.0f); | 
| 99 |  | 
| 100 | //        QScopedPointer<Qt3DRender::QPointLight> pointLightClone(static_cast<Qt3DRender::QPointLight *>(QNode::clone(&pointLight))); | 
| 101 | //        QVERIFY(pointLightClone.data()); | 
| 102 | //        QCOMPARE(pointLightClone->type(), Qt3DRender::QAbstractLight::PointLight); | 
| 103 | //        QCOMPARE(pointLight.color(), pointLightClone->color()); | 
| 104 | //        QCOMPARE(pointLight.intensity(), pointLightClone->intensity()); | 
| 105 | //        QCOMPARE(pointLight.constantAttenuation(), pointLightClone->constantAttenuation()); | 
| 106 | //        QCOMPARE(pointLight.linearAttenuation(), pointLightClone->linearAttenuation()); | 
| 107 | //        QCOMPARE(pointLight.quadraticAttenuation(), pointLightClone->quadraticAttenuation()); | 
| 108 | //    } | 
| 109 |  | 
| 110 | //    void checkDirectionalLightCloning() | 
| 111 | //    { | 
| 112 | //        Qt3DRender::QDirectionalLight dirLight; | 
| 113 | //        QCOMPARE(dirLight.type(), Qt3DRender::QAbstractLight::DirectionalLight); | 
| 114 | //        dirLight.setColor(Qt::blue); | 
| 115 | //        dirLight.setIntensity(0.5f); | 
| 116 | //        dirLight.setWorldDirection(QVector3D(0, 0, -1)); | 
| 117 |  | 
| 118 | //        QScopedPointer<Qt3DRender::QDirectionalLight> dirLightClone(static_cast<Qt3DRender::QDirectionalLight *>(QNode::clone(&dirLight))); | 
| 119 | //        QVERIFY(dirLightClone.data()); | 
| 120 | //        QCOMPARE(dirLightClone->type(), Qt3DRender::QAbstractLight::DirectionalLight); | 
| 121 | //        QCOMPARE(dirLight.color(), dirLightClone->color()); | 
| 122 | //        QCOMPARE(dirLight.intensity(), dirLightClone->intensity()); | 
| 123 | //        QCOMPARE(dirLight.worldDirection(), dirLightClone->worldDirection()); | 
| 124 | //    } | 
| 125 |  | 
| 126 | //    void checkSpotLightCloning() | 
| 127 | //    { | 
| 128 | //        Qt3DRender::QSpotLight spotLight; | 
| 129 | //        QCOMPARE(spotLight.type(), Qt3DRender::QAbstractLight::SpotLight); | 
| 130 | //        spotLight.setColor(Qt::lightGray); | 
| 131 | //        spotLight.setIntensity(0.5f); | 
| 132 | //        spotLight.setLocalDirection(QVector3D(0, 0, -1)); | 
| 133 | //        spotLight.setCutOffAngle(0.75f); | 
| 134 |  | 
| 135 | //        QScopedPointer<Qt3DRender::QSpotLight> spotLightClone(static_cast<Qt3DRender::QSpotLight *>(QNode::clone(&spotLight))); | 
| 136 | //        QVERIFY(spotLightClone.data()); | 
| 137 | //        QCOMPARE(spotLightClone->type(), Qt3DRender::QAbstractLight::SpotLight); | 
| 138 | //        QCOMPARE(spotLight.color(), spotLightClone->color()); | 
| 139 | //        QCOMPARE(spotLight.intensity(), spotLightClone->intensity()); | 
| 140 | //        QCOMPARE(spotLight.localDirection(), spotLightClone->localDirection()); | 
| 141 | //        QCOMPARE(spotLight.cutOffAngle(), spotLightClone->cutOffAngle()); | 
| 142 | //    } | 
| 143 |  | 
| 144 |     void checkLightPropertyUpdates() | 
| 145 |     { | 
| 146 |         TestArbiter arbiter; | 
| 147 |         QScopedPointer<Qt3DRender::QAbstractLight> light(new DummyLight); | 
| 148 |         arbiter.setArbiterOnNode(light.data()); | 
| 149 |  | 
| 150 |         light->setColor(Qt::red); | 
| 151 |         light->setIntensity(0.8f); // change from the default of 0.5f | 
| 152 |  | 
| 153 |         QCOMPARE(arbiter.events.size(), 0); | 
| 154 |         QCOMPARE(arbiter.dirtyNodes.size(), 2); | 
| 155 |         QVERIFY(arbiter.dirtyNodes[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); | 
| 156 |         QCOMPARE(arbiter.dirtyNodes[1], light.data()); | 
| 157 |  | 
| 158 |         arbiter.dirtyNodes.clear(); | 
| 159 |  | 
| 160 |         QCOMPARE(arbiter.events.size(), 0); | 
| 161 |  | 
| 162 |         arbiter.events.clear(); | 
| 163 |     } | 
| 164 |  | 
| 165 |     void checkPointLightPropertyUpdates() | 
| 166 |     { | 
| 167 |         TestArbiter arbiter; | 
| 168 |         QScopedPointer<Qt3DRender::QPointLight> pointLight(new Qt3DRender::QPointLight); | 
| 169 |         arbiter.setArbiterOnNode(pointLight.data()); | 
| 170 |  | 
| 171 |         pointLight->setColor(Qt::green); | 
| 172 |         pointLight->setIntensity(0.8f); | 
| 173 |         pointLight->setConstantAttenuation(0.5f); | 
| 174 |         pointLight->setLinearAttenuation(0.0f);     // No actual event triggered as 0.0f is default | 
| 175 |         pointLight->setQuadraticAttenuation(1.0f); | 
| 176 |  | 
| 177 |         QCOMPARE(arbiter.events.size(), 0); | 
| 178 |         QCOMPARE(arbiter.dirtyNodes.size(), 2); | 
| 179 |         QVERIFY(arbiter.dirtyNodes[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); | 
| 180 |         QCOMPARE(arbiter.dirtyNodes[1], pointLight.data()); | 
| 181 |  | 
| 182 |         arbiter.dirtyNodes.clear(); | 
| 183 |     } | 
| 184 |  | 
| 185 |     void checkDirectionalLightPropertyUpdates() | 
| 186 |     { | 
| 187 |         TestArbiter arbiter; | 
| 188 |         QScopedPointer<Qt3DRender::QDirectionalLight> dirLight(new Qt3DRender::QDirectionalLight); | 
| 189 |         arbiter.setArbiterOnNode(dirLight.data()); | 
| 190 |  | 
| 191 |         dirLight->setColor(Qt::blue); | 
| 192 |         dirLight->setIntensity(0.8f); | 
| 193 |         dirLight->setWorldDirection(QVector3D(0.5f, 0.0f, -1.0f)); | 
| 194 |  | 
| 195 |         QCOMPARE(arbiter.dirtyNodes.size(), 2); | 
| 196 |         QVERIFY(arbiter.dirtyNodes[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); | 
| 197 |         QCOMPARE(arbiter.dirtyNodes[1], dirLight.data()); | 
| 198 |  | 
| 199 |         arbiter.dirtyNodes.clear(); | 
| 200 |     } | 
| 201 |  | 
| 202 |     void checkSpotLightPropertyUpdates() | 
| 203 |     { | 
| 204 |         TestArbiter arbiter; | 
| 205 |         QScopedPointer<Qt3DRender::QSpotLight> spotLight(new Qt3DRender::QSpotLight); | 
| 206 |         arbiter.setArbiterOnNode(spotLight.data()); | 
| 207 |  | 
| 208 |         spotLight->setColor(Qt::lightGray); | 
| 209 |         spotLight->setIntensity(0.8f); | 
| 210 |         spotLight->setLocalDirection(QVector3D(0.5f, 0.0f, -1.0f)); | 
| 211 |         spotLight->setCutOffAngle(0.75f); | 
| 212 |  | 
| 213 |         QCOMPARE(arbiter.dirtyNodes.size(), 2); | 
| 214 |         QVERIFY(arbiter.dirtyNodes[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); | 
| 215 |         QCOMPARE(arbiter.dirtyNodes[1], spotLight.data()); | 
| 216 |  | 
| 217 |         arbiter.dirtyNodes.clear(); | 
| 218 |     } | 
| 219 | }; | 
| 220 |  | 
| 221 | QTEST_MAIN(tst_QAbstractLight) | 
| 222 |  | 
| 223 | #include "tst_qabstractlight.moc" | 
| 224 |  |