1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 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 <Qt3DCore/private/qnode_p.h> |
31 | #include <Qt3DCore/private/qscene_p.h> |
32 | #include <Qt3DCore/qentity.h> |
33 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
34 | |
35 | #include <Qt3DRender/qrenderstateset.h> |
36 | #include <Qt3DRender/private/qrenderstate_p.h> |
37 | #include <Qt3DRender/qrenderstate.h> |
38 | #include <Qt3DRender/private/qrenderstateset_p.h> |
39 | |
40 | #include "testpostmanarbiter.h" |
41 | |
42 | class MyStateSet; |
43 | class MyStateSetPrivate : public Qt3DRender::QRenderStatePrivate |
44 | { |
45 | public : |
46 | MyStateSetPrivate() |
47 | : QRenderStatePrivate(Qt3DRender::Render::DepthTestStateMask) |
48 | {} |
49 | }; |
50 | |
51 | |
52 | class MyStateSet : public Qt3DRender::QRenderState |
53 | { |
54 | Q_OBJECT |
55 | public: |
56 | explicit MyStateSet(Qt3DCore::QNode *parent = nullptr) |
57 | : Qt3DRender::QRenderState(*new MyStateSetPrivate(), parent) |
58 | {} |
59 | |
60 | private: |
61 | Q_DECLARE_PRIVATE(MyStateSet) |
62 | }; |
63 | |
64 | class tst_QRenderStateSet: public QObject |
65 | { |
66 | Q_OBJECT |
67 | |
68 | private Q_SLOTS: |
69 | |
70 | void checkSaneDefaults() |
71 | { |
72 | QScopedPointer<Qt3DRender::QRenderStateSet> defaultstateSet(new Qt3DRender::QRenderStateSet); |
73 | QVERIFY(defaultstateSet->renderStates().isEmpty()); |
74 | } |
75 | |
76 | void checkCloning_data() |
77 | { |
78 | QTest::addColumn<Qt3DRender::QRenderStateSet *>(name: "stateSet" ); |
79 | QTest::addColumn<QVector<Qt3DRender::QRenderState *> >(name: "states" ); |
80 | |
81 | Qt3DRender::QRenderStateSet *defaultConstructed = new Qt3DRender::QRenderStateSet(); |
82 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed << QVector<Qt3DRender::QRenderState *>(); |
83 | |
84 | Qt3DRender::QRenderStateSet *stateSetWithStates = new Qt3DRender::QRenderStateSet(); |
85 | Qt3DRender::QRenderState *state1 = new MyStateSet(); |
86 | Qt3DRender::QRenderState *state2 = new MyStateSet(); |
87 | QVector<Qt3DRender::QRenderState *> states = QVector<Qt3DRender::QRenderState *>() << state1 << state2; |
88 | stateSetWithStates->addRenderState(state: state1); |
89 | stateSetWithStates->addRenderState(state: state2); |
90 | QTest::newRow(dataTag: "stateSetWithStates" ) << stateSetWithStates << states; |
91 | } |
92 | |
93 | void checkCloning() |
94 | { |
95 | // GIVEN |
96 | QFETCH(Qt3DRender::QRenderStateSet*, stateSet); |
97 | QFETCH(QVector<Qt3DRender::QRenderState *>, states); |
98 | |
99 | // THEN |
100 | QCOMPARE(stateSet->renderStates(), states); |
101 | |
102 | // WHEN |
103 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(stateSet); |
104 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
105 | |
106 | // THEN |
107 | QCOMPARE(creationChanges.size(), 1 + states.size()); |
108 | |
109 | const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QRenderStateSetData> creationChangeData = |
110 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderStateSetData>>(src: creationChanges.first()); |
111 | const Qt3DRender::QRenderStateSetData &cloneData = creationChangeData->data; |
112 | |
113 | QCOMPARE(stateSet->id(), creationChangeData->subjectId()); |
114 | QCOMPARE(stateSet->isEnabled(), creationChangeData->isNodeEnabled()); |
115 | QCOMPARE(stateSet->metaObject(), creationChangeData->metaObject()); |
116 | QCOMPARE(stateSet->renderStates().count(), cloneData.renderStateIds.count()); |
117 | |
118 | for (int i = 0, m = states.count(); i < m; ++i) { |
119 | Qt3DRender::QRenderState *sOrig = states.at(i); |
120 | QCOMPARE(sOrig->id(), cloneData.renderStateIds.at(i)); |
121 | } |
122 | |
123 | delete stateSet; |
124 | } |
125 | |
126 | void checkPropertyUpdates() |
127 | { |
128 | // GIVEN |
129 | TestArbiter arbiter; |
130 | QScopedPointer<Qt3DRender::QRenderStateSet> stateSet(new Qt3DRender::QRenderStateSet()); |
131 | arbiter.setArbiterOnNode(stateSet.data()); |
132 | |
133 | // WHEN |
134 | Qt3DRender::QRenderState *state1 = new MyStateSet(); |
135 | stateSet->addRenderState(state: state1); |
136 | QCoreApplication::processEvents(); |
137 | |
138 | // THEN |
139 | QCOMPARE(arbiter.events.size(), 0); |
140 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
141 | QVERIFY(arbiter.dirtyNodes.contains(stateSet.data())); |
142 | |
143 | arbiter.dirtyNodes.clear(); |
144 | |
145 | // WHEN |
146 | stateSet->addRenderState(state: state1); |
147 | QCoreApplication::processEvents(); |
148 | |
149 | // THEN |
150 | QCOMPARE(arbiter.events.size(), 0); |
151 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
152 | // WHEN |
153 | stateSet->removeRenderState(state: state1); |
154 | QCoreApplication::processEvents(); |
155 | |
156 | // THEN |
157 | QCOMPARE(arbiter.events.size(), 0); |
158 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
159 | QVERIFY(arbiter.dirtyNodes.contains(stateSet.data())); |
160 | |
161 | arbiter.dirtyNodes.clear(); |
162 | } |
163 | |
164 | void checkRenderStateBookkeeping() |
165 | { |
166 | // GIVEN |
167 | QScopedPointer<Qt3DRender::QRenderStateSet> renderStateSet(new Qt3DRender::QRenderStateSet); |
168 | { |
169 | // WHEN |
170 | MyStateSet state; |
171 | renderStateSet->addRenderState(state: &state); |
172 | |
173 | // THEN |
174 | QCOMPARE(state.parent(), renderStateSet.data()); |
175 | QCOMPARE(renderStateSet->renderStates().size(), 1); |
176 | } |
177 | // THEN (Should not crash and parameter be unset) |
178 | QVERIFY(renderStateSet->renderStates().empty()); |
179 | |
180 | { |
181 | // WHEN |
182 | Qt3DRender::QRenderStateSet someOtherStateSet; |
183 | QScopedPointer<Qt3DRender::QRenderState> state(new MyStateSet(&someOtherStateSet)); |
184 | renderStateSet->addRenderState(state: state.data()); |
185 | |
186 | // THEN |
187 | QCOMPARE(state->parent(), &someOtherStateSet); |
188 | QCOMPARE(renderStateSet->renderStates().size(), 1); |
189 | |
190 | // WHEN |
191 | renderStateSet.reset(); |
192 | state.reset(); |
193 | |
194 | // THEN Should not crash when the state is destroyed (tests for failed removal of destruction helper) |
195 | } |
196 | } |
197 | }; |
198 | |
199 | QTEST_MAIN(tst_QRenderStateSet) |
200 | |
201 | #include "tst_qrenderstateset.moc" |
202 | |