| 1 | // Copyright (C) 2014 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/qchangearbiter_p.h> |
| 6 | #include <Qt3DCore/private/qscene_p.h> |
| 7 | #include <Qt3DCore/qnode.h> |
| 8 | #include <Qt3DCore/qentity.h> |
| 9 | #include <Qt3DCore/qcomponent.h> |
| 10 | #include <Qt3DCore/qbackendnode.h> |
| 11 | #include <Qt3DCore/private/qnode_p.h> |
| 12 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 13 | #include <QThread> |
| 14 | #include <QWaitCondition> |
| 15 | #include <testarbiter.h> |
| 16 | |
| 17 | class tst_QChangeArbiter : public QObject |
| 18 | { |
| 19 | Q_OBJECT |
| 20 | |
| 21 | private slots: |
| 22 | void recordsDirtyNodes(); |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | // used to test property change notifications |
| 27 | class PropertyTestNode : public Qt3DCore::QNode |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | Q_PROPERTY(int prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed) |
| 31 | Q_PROPERTY(float prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed) |
| 32 | |
| 33 | public: |
| 34 | explicit PropertyTestNode(Qt3DCore::QNode *parent = 0) : Qt3DCore::QNode(parent) |
| 35 | {} |
| 36 | |
| 37 | int prop1() const { return m_prop1; } |
| 38 | void setProp1(int v) |
| 39 | { |
| 40 | if (m_prop1 != v) { |
| 41 | m_prop1 = v; |
| 42 | Q_EMIT prop1Changed(v); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | float prop2() const { return m_prop2; } |
| 47 | void setProp2(float v) |
| 48 | { |
| 49 | if (m_prop2 != v) { |
| 50 | m_prop2 = v; |
| 51 | Q_EMIT prop2Changed(v); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | Q_SIGNALS: |
| 56 | void prop1Changed(int v); |
| 57 | void prop2Changed(float v); |
| 58 | |
| 59 | private: |
| 60 | int m_prop1 = 0; |
| 61 | float m_prop2 = 0.0f; |
| 62 | }; |
| 63 | |
| 64 | void tst_QChangeArbiter::recordsDirtyNodes() |
| 65 | { |
| 66 | // GIVEN |
| 67 | QScopedPointer<TestArbiter> arbiter(new TestArbiter()); |
| 68 | QScopedPointer<Qt3DCore::QScene> scene(new Qt3DCore::QScene()); |
| 69 | arbiter->setScene(scene.data()); |
| 70 | scene->setArbiter(arbiter.data()); |
| 71 | |
| 72 | // WHEN |
| 73 | auto *root = new PropertyTestNode(); |
| 74 | auto *child = new PropertyTestNode(root); |
| 75 | |
| 76 | root->setProp1(883); |
| 77 | child->setProp2(1584); |
| 78 | |
| 79 | // THEN |
| 80 | QCOMPARE(arbiter->dirtyNodes().size(), 0); |
| 81 | |
| 82 | // WHEN |
| 83 | Qt3DCore::QNodePrivate::get(root)->setArbiter(arbiter.data()); |
| 84 | root->setProp1(884); |
| 85 | child->setProp2(1585); |
| 86 | |
| 87 | // THEN |
| 88 | QCOMPARE(arbiter->dirtyNodes().size(), 1); |
| 89 | QCOMPARE(arbiter->dirtyNodes().front(), root); |
| 90 | |
| 91 | arbiter->clear(); |
| 92 | |
| 93 | // WHEN |
| 94 | Qt3DCore::QNodePrivate::get(child)->setArbiter(arbiter.data()); |
| 95 | child->setProp2(1586); |
| 96 | |
| 97 | // THEN |
| 98 | QCOMPARE(arbiter->dirtyNodes().size(), 1); |
| 99 | QCOMPARE(arbiter->dirtyNodes().front(), child); |
| 100 | |
| 101 | arbiter->clear(); |
| 102 | |
| 103 | // WHEN |
| 104 | root->setProp1(887); |
| 105 | child->setProp2(1587); |
| 106 | |
| 107 | // THEN |
| 108 | QCOMPARE(arbiter->dirtyNodes().size(), 2); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | QTEST_MAIN(tst_QChangeArbiter) |
| 113 | |
| 114 | #include "tst_qchangearbiter.moc" |