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/qparameter.h>
31#include <Qt3DRender/private/qparameter_p.h>
32#include <Qt3DRender/private/parameter_p.h>
33#include <Qt3DRender/private/uniform_p.h>
34#include <Qt3DRender/private/stringtoint_p.h>
35#include "qbackendnodetester.h"
36#include "testrenderer.h"
37
38class tst_Parameter : public Qt3DCore::QBackendNodeTester
39{
40 Q_OBJECT
41
42private Q_SLOTS:
43
44 void checkInitialState()
45 {
46 // GIVEN
47 Qt3DRender::Render::Parameter backendParameter;
48
49 // THEN
50 QCOMPARE(backendParameter.isEnabled(), false);
51 QVERIFY(backendParameter.peerId().isNull());
52 QCOMPARE(backendParameter.name(), QString());
53 QCOMPARE(backendParameter.uniformValue(), Qt3DRender::Render::UniformValue());
54 QCOMPARE(backendParameter.nameId(), -1);
55 QCOMPARE(backendParameter.backendValue(), QVariant());
56 }
57
58 void checkCleanupState()
59 {
60 // GIVEN
61 TestRenderer renderer;
62 Qt3DRender::Render::Parameter backendParameter;
63 Qt3DRender::QParameter parameter;
64 parameter.setName(QStringLiteral("Cutlass"));
65 parameter.setValue(QVariant(QColor(Qt::blue)));
66
67 // WHEN
68 backendParameter.setRenderer(&renderer);
69 simulateInitializationSync(frontend: &parameter, backend: &backendParameter);
70 backendParameter.cleanup();
71
72 // THEN
73 QCOMPARE(backendParameter.isEnabled(), false);
74 QCOMPARE(backendParameter.name(), QString());
75 QCOMPARE(backendParameter.uniformValue(), Qt3DRender::Render::UniformValue());
76 QCOMPARE(backendParameter.nameId(), -1);
77 QCOMPARE(backendParameter.backendValue(), QVariant());
78 }
79
80 void checkInitializeFromPeer()
81 {
82 // GIVEN
83 TestRenderer renderer;
84 Qt3DRender::QParameter parameter;
85
86 parameter.setName(QStringLiteral("Chevelle"));
87 parameter.setValue(QVariant(383.0f));
88
89 {
90 // WHEN
91 Qt3DRender::Render::Parameter backendParameter;
92 backendParameter.setRenderer(&renderer);
93 simulateInitializationSync(frontend: &parameter, backend: &backendParameter);
94
95 // THEN
96 QCOMPARE(backendParameter.isEnabled(), true);
97 QCOMPARE(backendParameter.peerId(), parameter.id());
98 QCOMPARE(backendParameter.name(), QStringLiteral("Chevelle"));
99 QCOMPARE(backendParameter.uniformValue(), Qt3DRender::Render::UniformValue::fromVariant(parameter.value()));
100 QCOMPARE(backendParameter.nameId(), Qt3DRender::Render::StringToInt::lookupId(QStringLiteral("Chevelle")));
101 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ParameterDirty);
102 }
103 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
104 {
105 // WHEN
106 Qt3DRender::Render::Parameter backendParameter;
107 backendParameter.setRenderer(&renderer);
108 parameter.setEnabled(false);
109 simulateInitializationSync(frontend: &parameter, backend: &backendParameter);
110
111 // THEN
112 QCOMPARE(backendParameter.peerId(), parameter.id());
113 QCOMPARE(backendParameter.isEnabled(), false);
114 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ParameterDirty);
115 }
116 }
117
118 void checkSceneChangeEvents()
119 {
120 // GIVEN
121 Qt3DRender::Render::Parameter backendParameter;
122 TestRenderer renderer;
123 backendParameter.setRenderer(&renderer);
124
125 Qt3DRender::QParameter parameter;
126 simulateInitializationSync(frontend: &parameter, backend: &backendParameter);
127
128 {
129 // WHEN
130 const bool newValue = false;
131 parameter.setEnabled(newValue);
132 backendParameter.syncFromFrontEnd(frontEnd: &parameter, firstTime: false);
133
134 // THEN
135 QCOMPARE(backendParameter.isEnabled(), newValue);
136 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ParameterDirty);
137 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
138 }
139 {
140 // WHEN
141 const QString newValue = QStringLiteral("C7");
142 parameter.setName(newValue);
143 backendParameter.syncFromFrontEnd(frontEnd: &parameter, firstTime: false);
144
145 // THEN
146 QCOMPARE(backendParameter.name(), newValue);
147 QCOMPARE(backendParameter.nameId(), Qt3DRender::Render::StringToInt::lookupId(newValue));
148 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ParameterDirty);
149 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
150 }
151 {
152 // WHEN
153 const QVariant value = QVariant::fromValue(value: QVector3D(350.0f, 427.0f, 454.0f));
154 const Qt3DRender::Render::UniformValue newValue = Qt3DRender::Render::UniformValue::fromVariant(variant: value);
155 parameter.setValue(value);
156 backendParameter.syncFromFrontEnd(frontEnd: &parameter, firstTime: false);
157
158 // THEN
159 QCOMPARE(backendParameter.uniformValue(), newValue);
160 QCOMPARE(backendParameter.backendValue(), value);
161 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ParameterDirty);
162 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
163 }
164 }
165
166};
167
168QTEST_MAIN(tst_Parameter)
169
170#include "tst_parameter.moc"
171

source code of qt3d/tests/auto/render/parameter/tst_parameter.cpp