1/****************************************************************************
2**
3** Copyright (C) 2016 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/qentity.h>
31
32#include <Qt3DRender/qrendersettings.h>
33#include <Qt3DRender/qrendersurfaceselector.h>
34#include <Qt3DRender/private/qrendersurfaceselector_p.h>
35#include <QSignalSpy>
36#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
37#include <Qt3DCore/qnodecreatedchange.h>
38#include "testpostmanarbiter.h"
39
40class tst_QRenderSurfaceSelector: public QObject
41{
42 Q_OBJECT
43
44private Q_SLOTS:
45 void shouldFindInstanceInEntityTree_data()
46 {
47 QTest::addColumn<QSharedPointer<Qt3DCore::QEntity>>(name: "root");
48 QTest::addColumn<Qt3DRender::QRenderSurfaceSelector*>(name: "expected");
49
50 auto root = QSharedPointer<Qt3DCore::QEntity>::create();
51 auto settings = new Qt3DRender::QRenderSettings;
52 root->addComponent(comp: settings);
53 auto selector = new Qt3DRender::QRenderSurfaceSelector;
54 settings->setActiveFrameGraph(selector);
55 QTest::newRow(dataTag: "simplest_tree") << root << selector;
56
57 root = QSharedPointer<Qt3DCore::QEntity>::create();
58 settings = new Qt3DRender::QRenderSettings;
59 root->addComponent(comp: settings);
60 settings->setActiveFrameGraph(new Qt3DRender::QFrameGraphNode);
61 selector = nullptr;
62 QTest::newRow(dataTag: "no_selector") << root << selector;
63
64 root = QSharedPointer<Qt3DCore::QEntity>::create();
65 settings = new Qt3DRender::QRenderSettings;
66 root->addComponent(comp: settings);
67 selector = nullptr;
68 QTest::newRow(dataTag: "no_framegraph") << root << selector;
69
70 root = QSharedPointer<Qt3DCore::QEntity>::create();
71 selector = nullptr;
72 QTest::newRow(dataTag: "no_rendersettings") << root << selector;
73
74 root = QSharedPointer<Qt3DCore::QEntity>::create();
75 auto entity = new Qt3DCore::QEntity(root.data());
76 entity = new Qt3DCore::QEntity(entity);
77 settings = new Qt3DRender::QRenderSettings;
78 entity->addComponent(comp: settings);
79 selector = new Qt3DRender::QRenderSurfaceSelector;
80 settings->setActiveFrameGraph(selector);
81 QTest::newRow(dataTag: "in_subentity") << root << selector;
82
83 root = QSharedPointer<Qt3DCore::QEntity>::create();
84 entity = new Qt3DCore::QEntity(root.data());
85 entity = new Qt3DCore::QEntity(entity);
86 settings = new Qt3DRender::QRenderSettings;
87 entity->addComponent(comp: settings);
88 auto node = new Qt3DRender::QFrameGraphNode;
89 settings->setActiveFrameGraph(node);
90 node = new Qt3DRender::QFrameGraphNode(node);
91 selector = new Qt3DRender::QRenderSurfaceSelector(node);
92 QTest::newRow(dataTag: "in_deeper_framegraph") << root << selector;
93 }
94
95 void shouldFindInstanceInEntityTree()
96 {
97 // GIVEN
98 QFETCH(QSharedPointer<Qt3DCore::QEntity>, root);
99
100 // WHEN
101 auto selector = Qt3DRender::QRenderSurfaceSelectorPrivate::find(rootObject: root.data());
102
103 // THEN
104 QFETCH(Qt3DRender::QRenderSurfaceSelector*, expected);
105 QCOMPARE(selector, expected);
106 }
107
108 void checkDefaultConstruction()
109 {
110 // GIVEN
111 Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector;
112
113 // THEN
114 QVERIFY(renderSurfaceSelector.surface() == nullptr);
115 QCOMPARE(renderSurfaceSelector.externalRenderTargetSize(), QSize());
116 QCOMPARE(renderSurfaceSelector.surfacePixelRatio(), 1.0f);
117 }
118
119 void checkPropertyChanges()
120 {
121 // GIVEN
122 Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector;
123
124 {
125 // WHEN
126 QSignalSpy spy(&renderSurfaceSelector, SIGNAL(surfaceChanged(QObject *)));
127 QWindow newValue;
128 renderSurfaceSelector.setSurface(&newValue);
129
130 // THEN
131 QVERIFY(spy.isValid());
132 QCOMPARE(renderSurfaceSelector.surface(), &newValue);
133 QCOMPARE(spy.count(), 1);
134
135 // WHEN
136 spy.clear();
137 renderSurfaceSelector.setSurface(&newValue);
138
139 // THEN
140 QCOMPARE(renderSurfaceSelector.surface(), &newValue);
141 QCOMPARE(spy.count(), 0);
142
143 // Prevents crashes with temporary window being destroyed
144 renderSurfaceSelector.setSurface(nullptr);
145 QCoreApplication::processEvents();
146 }
147 {
148 // WHEN
149 QSignalSpy spy(&renderSurfaceSelector, SIGNAL(externalRenderTargetSizeChanged(QSize)));
150 const QSize newValue(512, 512);
151 renderSurfaceSelector.setExternalRenderTargetSize(newValue);
152
153 // THEN
154 QVERIFY(spy.isValid());
155 QCOMPARE(renderSurfaceSelector.externalRenderTargetSize(), newValue);
156 QCOMPARE(spy.count(), 1);
157
158 // WHEN
159 spy.clear();
160 renderSurfaceSelector.setExternalRenderTargetSize(newValue);
161
162 // THEN
163 QCOMPARE(renderSurfaceSelector.externalRenderTargetSize(), newValue);
164 QCOMPARE(spy.count(), 0);
165 }
166 {
167 // WHEN
168 QSignalSpy spy(&renderSurfaceSelector, SIGNAL(surfacePixelRatioChanged(float)));
169 const float newValue = 15.0f;
170 renderSurfaceSelector.setSurfacePixelRatio(newValue);
171
172 // THEN
173 QVERIFY(spy.isValid());
174 QCOMPARE(renderSurfaceSelector.surfacePixelRatio(), newValue);
175 QCOMPARE(spy.count(), 1);
176
177 // WHEN
178 spy.clear();
179 renderSurfaceSelector.setSurfacePixelRatio(newValue);
180
181 // THEN
182 QCOMPARE(renderSurfaceSelector.surfacePixelRatio(), newValue);
183 QCOMPARE(spy.count(), 0);
184 }
185 }
186
187 void checkCreationData()
188 {
189 // GIVEN
190 QWindow newValue;
191 Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector;
192
193 renderSurfaceSelector.setSurface(&newValue);
194 renderSurfaceSelector.setExternalRenderTargetSize(QSize(128, 128));
195 renderSurfaceSelector.setSurfacePixelRatio(25.0f);
196
197 // WHEN
198 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
199
200 {
201 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderSurfaceSelector);
202 creationChanges = creationChangeGenerator.creationChanges();
203 }
204
205 // THEN
206 {
207 QCOMPARE(creationChanges.size(), 1);
208
209 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderSurfaceSelectorData>>(src: creationChanges.first());
210 const Qt3DRender::QRenderSurfaceSelectorData cloneData = creationChangeData->data;
211
212 QCOMPARE(renderSurfaceSelector.surface(), cloneData.surface.data());
213 QCOMPARE(renderSurfaceSelector.externalRenderTargetSize(), cloneData.externalRenderTargetSize);
214 QCOMPARE(renderSurfaceSelector.surfacePixelRatio(), cloneData.surfacePixelRatio);
215 QCOMPARE(renderSurfaceSelector.id(), creationChangeData->subjectId());
216 QCOMPARE(renderSurfaceSelector.isEnabled(), true);
217 QCOMPARE(renderSurfaceSelector.isEnabled(), creationChangeData->isNodeEnabled());
218 QCOMPARE(renderSurfaceSelector.metaObject(), creationChangeData->metaObject());
219 }
220
221 // WHEN
222 renderSurfaceSelector.setEnabled(false);
223
224 {
225 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderSurfaceSelector);
226 creationChanges = creationChangeGenerator.creationChanges();
227 }
228
229 // THEN
230 {
231 QCOMPARE(creationChanges.size(), 1);
232
233 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderSurfaceSelectorData>>(src: creationChanges.first());
234 const Qt3DRender::QRenderSurfaceSelectorData cloneData = creationChangeData->data;
235
236 QCOMPARE(renderSurfaceSelector.surface(), cloneData.surface.data());
237 QCOMPARE(renderSurfaceSelector.externalRenderTargetSize(), cloneData.externalRenderTargetSize);
238 QCOMPARE(renderSurfaceSelector.surfacePixelRatio(), cloneData.surfacePixelRatio);
239 QCOMPARE(renderSurfaceSelector.id(), creationChangeData->subjectId());
240 QCOMPARE(renderSurfaceSelector.isEnabled(), false);
241 QCOMPARE(renderSurfaceSelector.isEnabled(), creationChangeData->isNodeEnabled());
242 QCOMPARE(renderSurfaceSelector.metaObject(), creationChangeData->metaObject());
243 }
244
245 // Prevents crashes with temporary window being destroyed
246 renderSurfaceSelector.setSurface(nullptr);
247 }
248
249 void checkSurfaceUpdate()
250 {
251 // GIVEN
252 QWindow newWindow;
253 TestArbiter arbiter;
254 Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector;
255 arbiter.setArbiterOnNode(&renderSurfaceSelector);
256
257 {
258 // WHEN
259 renderSurfaceSelector.setSurface(&newWindow);
260 QCoreApplication::processEvents();
261
262 // THEN
263 QCOMPARE(arbiter.events.size(), 0);
264 QCOMPARE(arbiter.dirtyNodes.size(), 1);
265 QCOMPARE(arbiter.dirtyNodes.front(), &renderSurfaceSelector);
266
267 arbiter.dirtyNodes.clear();
268 }
269
270 {
271 // WHEN
272 renderSurfaceSelector.setSurface(&newWindow);
273 QCoreApplication::processEvents();
274
275 // THEN
276 QCOMPARE(arbiter.events.size(), 0);
277 QCOMPARE(arbiter.dirtyNodes.size(), 0);
278 }
279
280 // Prevents crashes with temporary window being destroyed
281 renderSurfaceSelector.setSurface(nullptr);
282 }
283
284 void checkExternalRenderTargetSizeUpdate()
285 {
286 // GIVEN
287 TestArbiter arbiter;
288 Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector;
289 arbiter.setArbiterOnNode(&renderSurfaceSelector);
290
291 {
292 // WHEN
293 renderSurfaceSelector.setExternalRenderTargetSize(QSize(454, 454));
294 QCoreApplication::processEvents();
295
296 // THEN
297 QCOMPARE(arbiter.events.size(), 0);
298 QCOMPARE(arbiter.dirtyNodes.size(), 1);
299 QCOMPARE(arbiter.dirtyNodes.front(), &renderSurfaceSelector);
300
301 arbiter.dirtyNodes.clear();
302 }
303
304 {
305 // WHEN
306 renderSurfaceSelector.setExternalRenderTargetSize(QSize(454, 454));
307 QCoreApplication::processEvents();
308
309 // THEN
310 QCOMPARE(arbiter.events.size(), 0);
311 QCOMPARE(arbiter.dirtyNodes.size(), 0);
312 }
313
314 }
315
316 void checkSurfacePixelRatioUpdate()
317 {
318 // GIVEN
319 TestArbiter arbiter;
320 Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector;
321 arbiter.setArbiterOnNode(&renderSurfaceSelector);
322
323 {
324 // WHEN
325 renderSurfaceSelector.setSurfacePixelRatio(99.0f);
326 QCoreApplication::processEvents();
327
328 // THEN
329 QCOMPARE(arbiter.events.size(), 0);
330 QCOMPARE(arbiter.dirtyNodes.size(), 1);
331 QCOMPARE(arbiter.dirtyNodes.front(), &renderSurfaceSelector);
332
333 arbiter.dirtyNodes.clear();
334 }
335
336 {
337 // WHEN
338 renderSurfaceSelector.setSurfacePixelRatio(99.0f);
339 QCoreApplication::processEvents();
340
341 // THEN
342 QCOMPARE(arbiter.events.size(), 0);
343 QCOMPARE(arbiter.dirtyNodes.size(), 0);
344 }
345
346 }
347
348};
349
350QTEST_MAIN(tst_QRenderSurfaceSelector)
351
352#include "tst_qrendersurfaceselector.moc"
353

source code of qt3d/tests/auto/render/qrendersurfaceselector/tst_qrendersurfaceselector.cpp