1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 Paul Lemire paul.lemire350@gmail.com |
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 <Qt3DRender/private/framegraphnode_p.h> |
30 | #include <QtTest/QTest> |
31 | #include <Qt3DRender/private/managers_p.h> |
32 | #include <Qt3DRender/private/nodemanagers_p.h> |
33 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
34 | #include "testrenderer.h" |
35 | #include "qbackendnodetester.h" |
36 | |
37 | class MyFrameGraphNode : public Qt3DRender::Render::FrameGraphNode |
38 | { |
39 | public: |
40 | void setEnabled(bool enabled) |
41 | { |
42 | FrameGraphNode::setEnabled(enabled); |
43 | } |
44 | }; |
45 | |
46 | class MyQFrameGraphNode : public Qt3DRender::QFrameGraphNode |
47 | { |
48 | Q_OBJECT |
49 | public: |
50 | MyQFrameGraphNode(Qt3DCore::QNode *parent = nullptr) |
51 | : Qt3DRender::QFrameGraphNode(parent) |
52 | {} |
53 | }; |
54 | |
55 | class tst_FrameGraphNode : public Qt3DCore::QBackendNodeTester |
56 | { |
57 | Q_OBJECT |
58 | public: |
59 | tst_FrameGraphNode(QObject *parent = nullptr) |
60 | : Qt3DCore::QBackendNodeTester(parent) |
61 | {} |
62 | |
63 | ~tst_FrameGraphNode() |
64 | {} |
65 | |
66 | void setIdInternal(Qt3DRender::Render::FrameGraphNode *node, Qt3DCore::QNodeId id) |
67 | { |
68 | Qt3DCore::QBackendNodePrivate::get(n: node)->m_peerId = id; |
69 | } |
70 | |
71 | private Q_SLOTS: |
72 | |
73 | void checkInitialState() |
74 | { |
75 | // GIVEN |
76 | MyFrameGraphNode n; |
77 | |
78 | // THEN |
79 | QCOMPARE(n.nodeType(), Qt3DRender::Render::FrameGraphNode::InvalidNodeType); |
80 | QVERIFY(!n.isEnabled()); |
81 | QVERIFY(n.peerId().isNull()); |
82 | QVERIFY(n.manager() == nullptr); |
83 | QVERIFY(n.parentId().isNull()); |
84 | QVERIFY(n.childrenIds().empty()); |
85 | } |
86 | |
87 | void checkPropertyChanges() |
88 | { |
89 | // GIVEN |
90 | QScopedPointer<MyFrameGraphNode> n(new MyFrameGraphNode()); |
91 | |
92 | // WHEN |
93 | n->setEnabled(true); |
94 | // THEN |
95 | QCOMPARE(n->isEnabled(), true); |
96 | |
97 | // WHEN |
98 | QScopedPointer<Qt3DRender::Render::FrameGraphManager> manager(new Qt3DRender::Render::FrameGraphManager()); |
99 | n->setFrameGraphManager(manager.data()); |
100 | // THEN |
101 | QCOMPARE(n->manager(), manager.data()); |
102 | |
103 | // WHEN |
104 | const Qt3DCore::QNodeId parentId = Qt3DCore::QNodeId::createId(); |
105 | // THEN |
106 | QVERIFY(!parentId.isNull()); |
107 | |
108 | // WHEN |
109 | n->setParentId(parentId); |
110 | |
111 | // THEN |
112 | QCOMPARE(n->parentId(), parentId); |
113 | } |
114 | |
115 | void checkParentChange() |
116 | { |
117 | // GIVEN |
118 | QScopedPointer<Qt3DRender::Render::FrameGraphManager> manager(new Qt3DRender::Render::FrameGraphManager()); |
119 | const Qt3DCore::QNodeId parentId = Qt3DCore::QNodeId::createId(); |
120 | const Qt3DCore::QNodeId childId = Qt3DCore::QNodeId::createId(); |
121 | Qt3DRender::Render::FrameGraphNode *parent1 = new MyFrameGraphNode(); |
122 | Qt3DRender::Render::FrameGraphNode *child = new MyFrameGraphNode(); |
123 | |
124 | setIdInternal(node: parent1, id: parentId); |
125 | setIdInternal(node: child, id: childId); |
126 | |
127 | manager->appendNode(id: parentId, node: parent1); |
128 | manager->appendNode(id: childId, node: child); |
129 | |
130 | parent1->setFrameGraphManager(manager.data()); |
131 | child->setFrameGraphManager(manager.data()); |
132 | |
133 | // THEN |
134 | QVERIFY(parent1->childrenIds().isEmpty()); |
135 | QVERIFY(child->parentId().isNull()); |
136 | |
137 | // WHEN |
138 | child->setParentId(parentId); |
139 | |
140 | // THEN |
141 | QCOMPARE(child->parentId(), parentId); |
142 | QCOMPARE(child->parent(), parent1); |
143 | QCOMPARE(parent1->childrenIds().count(), 1); |
144 | QCOMPARE(parent1->childrenIds().first(), childId); |
145 | QCOMPARE(parent1->children().count(), parent1->childrenIds().count()); |
146 | QCOMPARE(parent1->children().first(), child); |
147 | |
148 | // WHEN |
149 | child->setParentId(Qt3DCore::QNodeId()); |
150 | |
151 | // THEN |
152 | QVERIFY(child->parentId().isNull()); |
153 | QVERIFY(child->parent() == nullptr); |
154 | QCOMPARE(parent1->childrenIds().count(), 0); |
155 | QCOMPARE(parent1->children().count(), parent1->childrenIds().count()); |
156 | } |
157 | |
158 | void checkSetParent() |
159 | { |
160 | // GIVEN |
161 | QScopedPointer<Qt3DRender::Render::FrameGraphManager> manager(new Qt3DRender::Render::FrameGraphManager()); |
162 | const Qt3DCore::QNodeId parent1Id = Qt3DCore::QNodeId::createId(); |
163 | const Qt3DCore::QNodeId parent2Id = Qt3DCore::QNodeId::createId(); |
164 | const Qt3DCore::QNodeId childId = Qt3DCore::QNodeId::createId(); |
165 | |
166 | Qt3DRender::Render::FrameGraphNode *parent1 = new MyFrameGraphNode(); |
167 | Qt3DRender::Render::FrameGraphNode *parent2 = new MyFrameGraphNode(); |
168 | Qt3DRender::Render::FrameGraphNode *child = new MyFrameGraphNode(); |
169 | |
170 | setIdInternal(node: parent1, id: parent1Id); |
171 | setIdInternal(node: parent2, id: parent2Id); |
172 | setIdInternal(node: child, id: childId); |
173 | |
174 | manager->appendNode(id: parent1Id, node: parent1); |
175 | manager->appendNode(id: parent2Id, node: parent2); |
176 | manager->appendNode(id: childId, node: child); |
177 | |
178 | parent1->setFrameGraphManager(manager.data()); |
179 | parent2->setFrameGraphManager(manager.data()); |
180 | child->setFrameGraphManager(manager.data()); |
181 | |
182 | // THEN |
183 | QCOMPARE(parent1->peerId(), parent1Id); |
184 | QCOMPARE(parent2->peerId(), parent2Id); |
185 | QCOMPARE(child->peerId(), childId); |
186 | |
187 | QVERIFY(child->parentId().isNull()); |
188 | QCOMPARE(parent1->childrenIds().size(), 0); |
189 | QCOMPARE(parent2->childrenIds().size(), 0); |
190 | |
191 | // WHEN |
192 | child->setParentId(parent1Id); |
193 | |
194 | // THEN |
195 | QCOMPARE(child->parentId(), parent1Id); |
196 | QCOMPARE(parent1->childrenIds().size(), 1); |
197 | QCOMPARE(parent2->childrenIds().size(), 0); |
198 | |
199 | // WHEN |
200 | child->setParentId(parent2Id); |
201 | |
202 | // THEN |
203 | QCOMPARE(child->parentId(), parent2Id); |
204 | QCOMPARE(parent1->childrenIds().size(), 0); |
205 | QCOMPARE(parent2->childrenIds().size(), 1); |
206 | |
207 | // WHEN |
208 | child->setParentId(Qt3DCore::QNodeId()); |
209 | |
210 | // THEN |
211 | QVERIFY(child->parentId().isNull()); |
212 | QCOMPARE(parent1->childrenIds().size(), 0); |
213 | QCOMPARE(parent2->childrenIds().size(), 0); |
214 | } |
215 | |
216 | void checkSceneChangeEvents() |
217 | { |
218 | // GIVEN |
219 | Qt3DRender::QFrameGraphNode *frontendFGNode = new MyQFrameGraphNode(); |
220 | Qt3DRender::QFrameGraphNode *frontendFGChild = new MyQFrameGraphNode(); |
221 | Qt3DRender::Render::FrameGraphNode *backendFGNode = new MyFrameGraphNode(); |
222 | Qt3DRender::Render::FrameGraphNode *backendFGChild = new MyFrameGraphNode(); |
223 | |
224 | QScopedPointer<Qt3DRender::Render::FrameGraphManager> manager(new Qt3DRender::Render::FrameGraphManager()); |
225 | |
226 | TestRenderer renderer; |
227 | |
228 | backendFGNode->setRenderer(&renderer); |
229 | backendFGChild->setRenderer(&renderer); |
230 | |
231 | setIdInternal(node: backendFGNode, id: frontendFGNode->id()); |
232 | setIdInternal(node: backendFGChild, id: frontendFGChild->id()); |
233 | |
234 | manager->appendNode(id: frontendFGNode->id(), node: backendFGNode); |
235 | manager->appendNode(id: frontendFGChild->id(), node: backendFGChild); |
236 | |
237 | backendFGNode->setFrameGraphManager(manager.data()); |
238 | backendFGChild->setFrameGraphManager(manager.data()); |
239 | |
240 | simulateInitializationSync(frontend: frontendFGNode, backend: backendFGNode); |
241 | simulateInitializationSync(frontend: frontendFGChild, backend: backendFGChild); |
242 | |
243 | QCOMPARE(backendFGNode->childrenIds().size(), 0); |
244 | |
245 | { |
246 | // WHEN |
247 | renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty); |
248 | frontendFGChild->setParent(frontendFGNode); |
249 | backendFGChild->syncFromFrontEnd(frontEnd: frontendFGChild, firstTime: false); |
250 | |
251 | // THEN |
252 | QCOMPARE(backendFGNode->childrenIds().size(), 1); |
253 | QCOMPARE(backendFGChild->parentId(), frontendFGNode->id()); |
254 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::FrameGraphDirty); |
255 | } |
256 | { |
257 | // WHEN |
258 | renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty); |
259 | frontendFGChild->setParent(Q_NODE_NULLPTR); |
260 | backendFGChild->syncFromFrontEnd(frontEnd: frontendFGChild, firstTime: false); |
261 | |
262 | // THEN |
263 | QCOMPARE(backendFGNode->childrenIds().size(), 0); |
264 | QVERIFY(backendFGChild->parentId().isNull()); |
265 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::FrameGraphDirty); |
266 | } |
267 | } |
268 | |
269 | void checCleanupMarksDirty() |
270 | { |
271 | // GIVEN |
272 | TestRenderer renderer; |
273 | Qt3DRender::Render::FrameGraphNode *backendFGNode = new MyFrameGraphNode(); |
274 | |
275 | backendFGNode->setRenderer(&renderer); |
276 | |
277 | // THEN |
278 | QVERIFY(renderer.dirtyBits() == 0); |
279 | |
280 | // WHEN |
281 | backendFGNode->cleanup(); |
282 | |
283 | // THEN |
284 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::FrameGraphDirty); |
285 | } |
286 | |
287 | }; |
288 | |
289 | QTEST_MAIN(tst_FrameGraphNode) |
290 | |
291 | #include "tst_framegraphnode.moc" |
292 | |