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 <Qt3DRender/qrendertarget.h>
31#include <Qt3DRender/qrendertargetoutput.h>
32#include <Qt3DRender/private/qrendertarget_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
39class tst_QRenderTarget : public QObject
40{
41 Q_OBJECT
42
43private Q_SLOTS:
44
45 void checkDefaultConstruction()
46 {
47 // GIVEN
48 Qt3DRender::QRenderTarget renderTarget;
49
50 // THEN
51 QVERIFY(renderTarget.outputs().empty());
52 }
53
54 void checkPropertyChanges()
55 {
56 // GIVEN
57 Qt3DRender::QRenderTarget renderTarget;
58
59 // WHEN
60 Qt3DRender::QRenderTargetOutput output;
61
62 renderTarget.addOutput(output: &output);
63
64 // THEN
65 QCOMPARE(renderTarget.outputs().size(), 1);
66
67 // WHEN
68 renderTarget.addOutput(output: &output);
69
70 // THEN
71 QCOMPARE(renderTarget.outputs().size(), 1);
72
73 // WHEN
74 renderTarget.removeOutput(output: &output);
75
76 // THEN
77 QCOMPARE(renderTarget.outputs().size(), 0);
78 }
79
80 void checkRenderTargetOutputBookkeeping()
81 {
82 // GIVEN
83 Qt3DRender::QRenderTarget renderTarget;
84
85 {
86 // WHEN
87 Qt3DRender::QRenderTargetOutput output;
88 renderTarget.addOutput(output: &output);
89
90 // THEN
91 QCOMPARE(renderTarget.outputs().size(), 1);
92 }
93
94 // THEN -> should not crash
95 QCOMPARE(renderTarget.outputs().size(), 0);
96 }
97
98 void checkCreationData()
99 {
100 // GIVEN
101 Qt3DRender::QRenderTarget renderTarget;
102 Qt3DRender::QRenderTargetOutput output;
103
104 renderTarget.addOutput(output: &output);
105
106
107 // WHEN
108 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
109
110 {
111 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderTarget);
112 creationChanges = creationChangeGenerator.creationChanges();
113 }
114
115 // THEN
116 {
117 QCOMPARE(creationChanges.size(), 2); // Target + Output
118
119 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderTargetData>>(src: creationChanges.first());
120 const Qt3DRender::QRenderTargetData cloneData = creationChangeData->data;
121
122 QCOMPARE(renderTarget.id(), creationChangeData->subjectId());
123 QCOMPARE(renderTarget.isEnabled(), true);
124 QCOMPARE(renderTarget.isEnabled(), creationChangeData->isNodeEnabled());
125 QCOMPARE(renderTarget.metaObject(), creationChangeData->metaObject());
126 QCOMPARE(renderTarget.outputs().size(), cloneData.outputIds.size());
127 QCOMPARE(output.id(), cloneData.outputIds.first());
128 }
129
130 // WHEN
131 renderTarget.setEnabled(false);
132
133 {
134 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&renderTarget);
135 creationChanges = creationChangeGenerator.creationChanges();
136 }
137
138 // THEN
139 {
140 QCOMPARE(creationChanges.size(), 2); // Target + Output
141
142 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QRenderTargetData>>(src: creationChanges.first());
143 const Qt3DRender::QRenderTargetData cloneData = creationChangeData->data;
144
145 QCOMPARE(renderTarget.id(), creationChangeData->subjectId());
146 QCOMPARE(renderTarget.isEnabled(), false);
147 QCOMPARE(renderTarget.isEnabled(), creationChangeData->isNodeEnabled());
148 QCOMPARE(renderTarget.metaObject(), creationChangeData->metaObject());
149 QCOMPARE(renderTarget.outputs().size(), cloneData.outputIds.size());
150 QCOMPARE(output.id(), cloneData.outputIds.first());
151 }
152 }
153
154 void checkRenderTargetOutputUpdate()
155 {
156 // GIVEN
157 TestArbiter arbiter;
158 Qt3DRender::QRenderTarget renderTarget;
159 Qt3DRender::QRenderTargetOutput renderTargetOutput;
160 arbiter.setArbiterOnNode(&renderTarget);
161
162 {
163 // WHEN
164 renderTarget.addOutput(output: &renderTargetOutput);
165 QCoreApplication::processEvents();
166
167 // THEN
168 QCOMPARE(arbiter.events.size(), 0);
169 QCOMPARE(arbiter.dirtyNodes.size(), 1);
170 QCOMPARE(arbiter.dirtyNodes.front(), &renderTarget);
171
172 arbiter.dirtyNodes.clear();
173 }
174
175 {
176 // WHEN
177 renderTarget.removeOutput(output: &renderTargetOutput);
178 QCoreApplication::processEvents();
179
180 // THEN
181 QCOMPARE(arbiter.events.size(), 0);
182 QCOMPARE(arbiter.dirtyNodes.size(), 1);
183 QCOMPARE(arbiter.dirtyNodes.front(), &renderTarget);
184
185 arbiter.dirtyNodes.clear();
186 }
187 }
188};
189
190QTEST_MAIN(tst_QRenderTarget)
191
192#include "tst_qrendertarget.moc"
193

source code of qt3d/tests/auto/render/qrendertarget/tst_qrendertarget.cpp