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 <qbackendnodetester.h>
31#include <Qt3DRender/private/material_p.h>
32
33#include <Qt3DRender/QMaterial>
34#include <Qt3DRender/QParameter>
35#include <Qt3DRender/QEffect>
36#include "testrenderer.h"
37
38using namespace Qt3DCore;
39using namespace Qt3DRender;
40using namespace Qt3DRender::Render;
41
42class tst_RenderMaterial : public Qt3DCore::QBackendNodeTester
43{
44 Q_OBJECT
45
46public:
47 tst_RenderMaterial() {}
48
49private slots:
50 void shouldHaveInitialState();
51 void shouldHavePropertiesMirroringFromItsPeer_data();
52 void shouldHavePropertiesMirroringFromItsPeer();
53 void shouldHandleParametersPropertyChange();
54 void shouldHandleEnablePropertyChange();
55 void shouldHandleEffectPropertyChange();
56};
57
58
59void tst_RenderMaterial::shouldHaveInitialState()
60{
61 // GIVEN
62 Material backend;
63
64 // THEN
65 QVERIFY(backend.parameters().isEmpty());
66 QVERIFY(backend.effect().isNull());
67 QVERIFY(!backend.isEnabled());
68}
69
70void tst_RenderMaterial::shouldHavePropertiesMirroringFromItsPeer_data()
71{
72 QTest::addColumn<QMaterial *>(name: "frontendMaterial");
73
74 QMaterial *emptyMaterial = new QMaterial();
75 QTest::newRow(dataTag: "emptyMaterial") << emptyMaterial;
76
77 QMaterial *simpleMaterial = new QMaterial();
78 simpleMaterial->addParameter(parameter: new QParameter());
79 simpleMaterial->setEffect(new QEffect());
80 QTest::newRow(dataTag: "simpleMaterial") << simpleMaterial;
81
82 QMaterial *manyParametersMaterial = new QMaterial();
83 manyParametersMaterial->addParameter(parameter: new QParameter());
84 manyParametersMaterial->addParameter(parameter: new QParameter());
85 manyParametersMaterial->addParameter(parameter: new QParameter());
86 manyParametersMaterial->addParameter(parameter: new QParameter());
87 simpleMaterial->setEffect(new QEffect());
88 QTest::newRow(dataTag: "manyParametersMaterial") << manyParametersMaterial;
89
90 QMaterial *disabledSimpleMaterial = new QMaterial();
91 disabledSimpleMaterial->setEnabled(false);
92 disabledSimpleMaterial->addParameter(parameter: new QParameter());
93 disabledSimpleMaterial->setEffect(new QEffect());
94 QTest::newRow(dataTag: "simpleDisabledMaterial") << disabledSimpleMaterial;
95
96 QMaterial *manyParametersDisabledMaterial = new QMaterial();
97 manyParametersDisabledMaterial->setEnabled(false);
98 manyParametersDisabledMaterial->setEffect(new QEffect());
99 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
100 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
101 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
102 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
103 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
104 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
105 manyParametersDisabledMaterial->addParameter(parameter: new QParameter());
106 QTest::newRow(dataTag: "manyParametersDisabledMaterial") << manyParametersDisabledMaterial;
107}
108
109void tst_RenderMaterial::shouldHavePropertiesMirroringFromItsPeer()
110{
111 // WHEN
112 QFETCH(QMaterial *, frontendMaterial);
113 TestRenderer renderer;
114 Material backend;
115
116 // GIVEN
117 backend.setRenderer(&renderer);
118 simulateInitializationSync(frontend: frontendMaterial, backend: &backend);
119
120 // THEN
121 QVERIFY(backend.isEnabled() == frontendMaterial->isEnabled());
122 QCOMPARE(backend.effect(), frontendMaterial->effect() ? frontendMaterial->effect()->id() : QNodeId());
123 QCOMPARE(backend.parameters().count(), frontendMaterial->parameters().count());
124 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::MaterialDirty);
125
126 int c = 0;
127 const auto frontendMaterialParameters = frontendMaterial->parameters();
128 for (QParameter *p : frontendMaterialParameters)
129 QCOMPARE(p->id(), backend.parameters().at(c++));
130
131 delete frontendMaterial;
132}
133
134void tst_RenderMaterial::shouldHandleParametersPropertyChange()
135{
136 // GIVEN
137 QParameter *parameter = new QParameter();
138 Material backend;
139 TestRenderer renderer;
140 backend.setRenderer(&renderer);
141
142 QMaterial material;
143 simulateInitializationSync(frontend: &material, backend: &backend);
144
145 // WHEN
146 material.addParameter(parameter);
147 backend.syncFromFrontEnd(frontEnd: &material, firstTime: false);
148
149 // THEN
150 QCOMPARE(backend.parameters().count(), 1);
151 QCOMPARE(backend.parameters().first(), parameter->id());
152 QVERIFY(renderer.dirtyBits() != 0);
153
154 // WHEN
155 material.removeParameter(parameter);
156 backend.syncFromFrontEnd(frontEnd: &material, firstTime: false);
157
158 // THEN
159 QVERIFY(backend.parameters().isEmpty());
160}
161
162void tst_RenderMaterial::shouldHandleEnablePropertyChange()
163{
164 // GIVEN
165 Material backend;
166 TestRenderer renderer;
167 backend.setRenderer(&renderer);
168
169 QMaterial material;
170 simulateInitializationSync(frontend: &material, backend: &backend);
171
172 // WHEN
173 material.setEnabled(false);
174 backend.syncFromFrontEnd(frontEnd: &material, firstTime: false);
175
176 // THEN
177 QVERIFY(!backend.isEnabled());
178 QVERIFY(renderer.dirtyBits() != 0);
179
180 // WHEN
181 material.setEnabled(true);
182 backend.syncFromFrontEnd(frontEnd: &material, firstTime: false);
183
184 // THEN
185 QVERIFY(backend.isEnabled());
186
187}
188
189void tst_RenderMaterial::shouldHandleEffectPropertyChange()
190{
191 // GIVEN
192 Material backend;
193 TestRenderer renderer;
194 backend.setRenderer(&renderer);
195
196 QMaterial material;
197 simulateInitializationSync(frontend: &material, backend: &backend);
198
199 // WHEN
200 QEffect effect;
201 material.setEffect(&effect);
202 backend.syncFromFrontEnd(frontEnd: &material, firstTime: false);
203
204 // THEN
205 QCOMPARE(backend.effect(), effect.id());
206 QVERIFY(renderer.dirtyBits() != 0);
207}
208
209QTEST_APPLESS_MAIN(tst_RenderMaterial)
210
211#include "tst_material.moc"
212

source code of qt3d/tests/auto/render/material/tst_material.cpp