1/****************************************************************************
2**
3** Copyright (C) 2017 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 <Qt3DQuickScene2D/qscene2d.h>
31#include <Qt3DRender/qrendertargetoutput.h>
32#include <private/qscene2d_p.h>
33#include <QObject>
34#include <QSignalSpy>
35#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
36#include <Qt3DCore/qnodecreatedchange.h>
37#include "testpostmanarbiter.h"
38
39using namespace Qt3DRender::Quick;
40
41class tst_QScene2D : public QObject
42{
43 Q_OBJECT
44
45private Q_SLOTS:
46
47 void initTestCase()
48 {
49 qRegisterMetaType<Qt3DRender::Quick::QScene2D::RenderPolicy>(
50 typeName: "QScene2D::RenderPolicy");
51 }
52
53 void checkDefaultConstruction()
54 {
55 // GIVEN
56 Qt3DRender::Quick::QScene2D scene2d;
57
58 // THEN
59 QCOMPARE(scene2d.output(), nullptr);
60 QCOMPARE(scene2d.renderPolicy(), QScene2D::Continuous);
61 QCOMPARE(scene2d.item(), nullptr);
62 QCOMPARE(scene2d.isMouseEnabled(), true);
63 }
64
65 void checkPropertyChanges()
66 {
67 // GIVEN
68 Qt3DRender::Quick::QScene2D scene2d;
69 QScopedPointer<Qt3DRender::QRenderTargetOutput> output(new Qt3DRender::QRenderTargetOutput());
70 QScopedPointer<QQuickItem> item(new QQuickItem());
71
72 {
73 // WHEN
74 QSignalSpy spy(&scene2d, SIGNAL(outputChanged(Qt3DRender::QRenderTargetOutput*)));
75 Qt3DRender::QRenderTargetOutput *newValue = output.data();
76 scene2d.setOutput(newValue);
77
78 // THEN
79 QVERIFY(spy.isValid());
80 QCOMPARE(scene2d.output(), newValue);
81 QCOMPARE(spy.count(), 1);
82
83 // WHEN
84 spy.clear();
85 scene2d.setOutput(newValue);
86
87 // THEN
88 QCOMPARE(scene2d.output(), newValue);
89 QCOMPARE(spy.count(), 0);
90 }
91 {
92 // WHEN
93 QSignalSpy spy(&scene2d, SIGNAL(renderPolicyChanged(QScene2D::RenderPolicy)));
94 const QScene2D::RenderPolicy newValue = QScene2D::SingleShot;
95 scene2d.setRenderPolicy(newValue);
96
97 // THEN
98 QVERIFY(spy.isValid());
99 QCOMPARE(scene2d.renderPolicy(), newValue);
100 QCOMPARE(spy.count(), 1);
101
102 // WHEN
103 spy.clear();
104 scene2d.setRenderPolicy(newValue);
105
106 // THEN
107 QCOMPARE(scene2d.renderPolicy(), newValue);
108 QCOMPARE(spy.count(), 0);
109 }
110 {
111 // WHEN
112 QSignalSpy spy(&scene2d, SIGNAL(itemChanged(QQuickItem*)));
113 QQuickItem *newValue = item.data();
114 scene2d.setItem(newValue);
115
116 // THEN
117 QVERIFY(spy.isValid());
118 QCOMPARE(scene2d.item(), newValue);
119 QCOMPARE(spy.count(), 1);
120
121 // WHEN
122 spy.clear();
123 scene2d.setItem(newValue);
124
125 // THEN
126 QCOMPARE(scene2d.item(), newValue);
127 QCOMPARE(spy.count(), 0);
128 }
129
130 {
131 // WHEN
132 QSignalSpy spy(&scene2d, SIGNAL(mouseEnabledChanged(bool)));
133 bool newValue = false;
134 scene2d.setMouseEnabled(newValue);
135
136 // THEN
137 QVERIFY(spy.isValid());
138 QCOMPARE(scene2d.isMouseEnabled(), newValue);
139 QCOMPARE(spy.count(), 1);
140
141 // WHEN
142 spy.clear();
143 scene2d.setMouseEnabled(newValue);
144
145 // THEN
146 QCOMPARE(scene2d.isMouseEnabled(), newValue);
147 QCOMPARE(spy.count(), 0);
148 }
149 }
150
151 void checkCreationData()
152 {
153 // GIVEN
154 Qt3DRender::Quick::QScene2D scene2d;
155 QScopedPointer<Qt3DRender::QRenderTargetOutput> output(new Qt3DRender::QRenderTargetOutput());
156
157 scene2d.setOutput(output.data());
158 scene2d.setRenderPolicy(QScene2D::SingleShot);
159
160 // WHEN
161 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
162
163 {
164 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&scene2d);
165 creationChanges = creationChangeGenerator.creationChanges();
166 }
167
168 // THEN
169 {
170 QCOMPARE(creationChanges.size(), 1);
171
172 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<
173 Qt3DRender::Quick::QScene2DData>>(src: creationChanges.first());
174 const Qt3DRender::Quick::QScene2DData cloneData = creationChangeData->data;
175
176 QCOMPARE(scene2d.output()->id(), cloneData.output);
177 QCOMPARE(scene2d.renderPolicy(), cloneData.renderPolicy);
178 QCOMPARE(scene2d.id(), creationChangeData->subjectId());
179 QCOMPARE(scene2d.isEnabled(), true);
180 QCOMPARE(scene2d.isEnabled(), creationChangeData->isNodeEnabled());
181 QCOMPARE(scene2d.metaObject(), creationChangeData->metaObject());
182 QCOMPARE(scene2d.isMouseEnabled(), cloneData.mouseEnabled);
183 }
184
185 // WHEN
186 scene2d.setEnabled(false);
187
188 {
189 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&scene2d);
190 creationChanges = creationChangeGenerator.creationChanges();
191 }
192
193 // THEN
194 {
195 QCOMPARE(creationChanges.size(), 1);
196
197 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<
198 Qt3DRender::Quick::QScene2DData>>(src: creationChanges.first());
199 const Qt3DRender::Quick::QScene2DData cloneData = creationChangeData->data;
200
201 QCOMPARE(scene2d.output()->id(), cloneData.output);
202 QCOMPARE(scene2d.renderPolicy(), cloneData.renderPolicy);
203 QCOMPARE(scene2d.id(), creationChangeData->subjectId());
204 QCOMPARE(scene2d.isEnabled(), false);
205 QCOMPARE(scene2d.isEnabled(), creationChangeData->isNodeEnabled());
206 QCOMPARE(scene2d.metaObject(), creationChangeData->metaObject());
207 }
208 }
209
210 void checkOutputUpdate()
211 {
212 // GIVEN
213 TestArbiter arbiter;
214 Qt3DRender::Quick::QScene2D scene2d;
215 arbiter.setArbiterOnNode(&scene2d);
216 QScopedPointer<Qt3DRender::QRenderTargetOutput> output(new Qt3DRender::QRenderTargetOutput());
217
218 {
219 // WHEN
220 scene2d.setOutput(output.data());
221 QCoreApplication::processEvents();
222
223 // THEN
224 QCOMPARE(arbiter.events.size(), 0);
225 QCOMPARE(arbiter.dirtyNodes.size(), 1);
226 QCOMPARE(arbiter.dirtyNodes.front(), &scene2d);
227
228 arbiter.dirtyNodes.clear();
229 }
230
231 {
232 // WHEN
233 scene2d.setOutput(output.data());
234 QCoreApplication::processEvents();
235
236 // THEN
237 QCOMPARE(arbiter.events.size(), 0);
238 QCOMPARE(arbiter.dirtyNodes.size(), 0);
239 }
240
241 }
242
243 void checkRenderPolicyUpdate()
244 {
245 // GIVEN
246 TestArbiter arbiter;
247 Qt3DRender::Quick::QScene2D scene2d;
248 arbiter.setArbiterOnNode(&scene2d);
249
250 {
251 // WHEN
252 scene2d.setRenderPolicy(QScene2D::SingleShot);
253 QCoreApplication::processEvents();
254
255 // THEN
256 QCOMPARE(arbiter.events.size(), 0);
257 QCOMPARE(arbiter.dirtyNodes.size(), 1);
258 QCOMPARE(arbiter.dirtyNodes.front(), &scene2d);
259
260 arbiter.dirtyNodes.clear();
261 }
262
263 {
264 // WHEN
265 scene2d.setRenderPolicy(QScene2D::SingleShot);
266 QCoreApplication::processEvents();
267
268 // THEN
269 QCOMPARE(arbiter.events.size(), 0);
270 QCOMPARE(arbiter.dirtyNodes.size(), 0);
271 }
272
273 }
274
275 void checkMouseEnabledUpdate()
276 {
277 // GIVEN
278 TestArbiter arbiter;
279 Qt3DRender::Quick::QScene2D scene2d;
280 arbiter.setArbiterOnNode(&scene2d);
281
282 {
283 // WHEN
284 scene2d.setMouseEnabled(false);
285 QCoreApplication::processEvents();
286
287 // THEN
288 QCOMPARE(arbiter.events.size(), 0);
289 QCOMPARE(arbiter.dirtyNodes.size(), 1);
290 QCOMPARE(arbiter.dirtyNodes.front(), &scene2d);
291
292 arbiter.dirtyNodes.clear();
293 }
294
295 {
296 // WHEN
297 scene2d.setMouseEnabled(false);
298 QCoreApplication::processEvents();
299
300 // THEN
301 QCOMPARE(arbiter.events.size(), 0);
302 QCOMPARE(arbiter.dirtyNodes.size(), 0);
303 }
304
305 }
306
307};
308
309QTEST_MAIN(tst_QScene2D)
310
311#include "tst_qscene2d.moc"
312

source code of qt3d/tests/auto/render/qscene2d/tst_qscene2d.cpp