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 <QObject>
31#include <Qt3DRender/qgraphicsapifilter.h>
32#include <qopenglcontext.h>
33#include <QtCore/qsharedpointer.h>
34#include <QSignalSpy>
35
36class tst_QGraphicsApiFilter : public QObject
37{
38 Q_OBJECT
39public:
40 tst_QGraphicsApiFilter() : QObject()
41 {
42 qRegisterMetaType<Qt3DRender::QGraphicsApiFilter::Api>();
43 qRegisterMetaType<Qt3DRender::QGraphicsApiFilter::OpenGLProfile>();
44 }
45
46private Q_SLOTS:
47 void defaultConstruction()
48 {
49 // WHEN
50 Qt3DRender::QGraphicsApiFilter apiFilter;
51
52 // THEN
53 const auto api = (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL)
54 ? Qt3DRender::QGraphicsApiFilter::OpenGL
55 : Qt3DRender::QGraphicsApiFilter::OpenGLES;
56 QCOMPARE(apiFilter.api(), api);
57 QCOMPARE(apiFilter.profile(), Qt3DRender::QGraphicsApiFilter::NoProfile);
58 QCOMPARE(apiFilter.majorVersion(), 0);
59 QCOMPARE(apiFilter.minorVersion(), 0);
60 QCOMPARE(apiFilter.extensions(), QStringList());
61 QCOMPARE(apiFilter.vendor(), QString());
62 }
63
64 void properties()
65 {
66 // GIVEN
67 Qt3DRender::QGraphicsApiFilter apiFilter;
68
69 {
70 // WHEN
71 QSignalSpy spy(&apiFilter, SIGNAL(apiChanged(Qt3DRender::QGraphicsApiFilter::Api)));
72 apiFilter.setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
73
74 // THEN
75 QCOMPARE(apiFilter.api(), Qt3DRender::QGraphicsApiFilter::OpenGL);
76 bool shouldEmitSignal = (QOpenGLContext::openGLModuleType() != QOpenGLContext::LibGL);
77 QCOMPARE(spy.isEmpty(), !shouldEmitSignal);
78 }
79
80 {
81 // WHEN
82 QSignalSpy spy(&apiFilter, SIGNAL(profileChanged(Qt3DRender::QGraphicsApiFilter::OpenGLProfile)));
83 apiFilter.setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
84
85 // THEN
86 QCOMPARE(apiFilter.profile(), Qt3DRender::QGraphicsApiFilter::CoreProfile);
87 QCOMPARE(spy.count(), 1);
88 }
89
90 {
91 // WHEN
92 QSignalSpy spy(&apiFilter, SIGNAL(majorVersionChanged(int)));
93 apiFilter.setMajorVersion(4);
94
95 // THEN
96 QCOMPARE(apiFilter.majorVersion(), 4);
97 QCOMPARE(spy.count(), 1);
98 }
99
100 {
101 // WHEN
102 QSignalSpy spy(&apiFilter, SIGNAL(minorVersionChanged(int)));
103 apiFilter.setMinorVersion(5);
104
105 // THEN
106 QCOMPARE(apiFilter.minorVersion(), 5);
107 QCOMPARE(spy.count(), 1);
108 }
109
110 {
111 // WHEN
112 QSignalSpy spy(&apiFilter, SIGNAL(extensionsChanged(QStringList)));
113 const auto extensions = (QStringList() << QLatin1String("extension1") << QLatin1String("extension2"));
114 apiFilter.setExtensions(extensions);
115
116 // THEN
117 QCOMPARE(apiFilter.extensions(), extensions);
118 QCOMPARE(spy.count(), 1);
119 }
120
121 {
122 // WHEN
123 QSignalSpy spy(&apiFilter, SIGNAL(vendorChanged(QString)));
124 const QLatin1String vendor("Triangles McTriangleFace");
125 apiFilter.setVendor(vendor);
126
127 // THEN
128 QCOMPARE(apiFilter.vendor(), vendor);
129 QCOMPARE(spy.count(), 1);
130 }
131 }
132
133 void shouldDetermineCompatibility_data()
134 {
135 QTest::addColumn<QSharedPointer<Qt3DRender::QGraphicsApiFilter>>(name: "required");
136 QTest::addColumn<QSharedPointer<Qt3DRender::QGraphicsApiFilter>>(name: "actual");
137 QTest::addColumn<bool>(name: "expected");
138
139 auto required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
140 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
141 required->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
142 required->setMajorVersion(4);
143 required->setMinorVersion(5);
144 auto actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
145 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
146 actual->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
147 actual->setMajorVersion(4);
148 actual->setMinorVersion(5);
149 bool expected = true;
150 QTest::newRow(dataTag: "exact_match") << required << actual << expected;
151
152 required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
153 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
154 required->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
155 required->setMajorVersion(3);
156 required->setMinorVersion(2);
157 actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
158 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
159 actual->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
160 actual->setMajorVersion(4);
161 actual->setMinorVersion(5);
162 expected = true;
163 QTest::newRow(dataTag: "actual_is_higher_version") << required << actual << expected;
164
165 required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
166 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
167 required->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
168 required->setMajorVersion(4);
169 required->setMinorVersion(5);
170 actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
171 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
172 actual->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
173 actual->setMajorVersion(3);
174 actual->setMinorVersion(2);
175 expected = false;
176 QTest::newRow(dataTag: "actual_is_lower_version") << required << actual << expected;
177
178 required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
179 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
180 required->setProfile(Qt3DRender::QGraphicsApiFilter::CompatibilityProfile);
181 required->setMajorVersion(4);
182 required->setMinorVersion(5);
183 actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
184 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
185 actual->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
186 actual->setMajorVersion(4);
187 actual->setMinorVersion(5);
188 expected = false;
189 QTest::newRow(dataTag: "wrong_profile") << required << actual << expected;
190
191 required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
192 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
193 required->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
194 required->setMajorVersion(3);
195 required->setMinorVersion(2);
196 actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
197 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGLES);
198 actual->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
199 actual->setMajorVersion(3);
200 actual->setMinorVersion(2);
201 expected = false;
202 QTest::newRow(dataTag: "wrong_api") << required << actual << expected;
203
204 required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
205 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
206 required->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
207 required->setMajorVersion(2);
208 required->setMinorVersion(0);
209 actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
210 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
211 actual->setProfile(Qt3DRender::QGraphicsApiFilter::CompatibilityProfile);
212 actual->setMajorVersion(3);
213 actual->setMinorVersion(2);
214 expected = true;
215 QTest::newRow(dataTag: "gl_3_2_compatibility_can_use_gl_2_0") << required << actual << expected;
216
217 required = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
218 required->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
219 required->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
220 required->setMajorVersion(2);
221 required->setMinorVersion(0);
222 actual = QSharedPointer<Qt3DRender::QGraphicsApiFilter>::create();
223 actual->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
224 actual->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
225 actual->setMajorVersion(3);
226 actual->setMinorVersion(2);
227 expected = false;
228 QTest::newRow(dataTag: "gl_3_2_core_cant_use_gl_2_0") << required << actual << expected;
229 }
230
231 void shouldDetermineCompatibility()
232 {
233 // GIVEN
234 QFETCH(QSharedPointer<Qt3DRender::QGraphicsApiFilter>, required);
235 QFETCH(QSharedPointer<Qt3DRender::QGraphicsApiFilter>, actual);
236 QFETCH(bool, expected);
237
238 // WHEN
239 const auto isCompatible = (*actual == *required);
240
241 // THEN
242 QCOMPARE(isCompatible, expected);
243 }
244
245 void checkEnumTypeValues()
246 {
247 // THEN
248 QCOMPARE(int(Qt3DRender::QGraphicsApiFilter::OpenGL), 1);
249 QCOMPARE(int(Qt3DRender::QGraphicsApiFilter::OpenGLES), 2);
250 QCOMPARE(int(Qt3DRender::QGraphicsApiFilter::Vulkan), 3);
251 QCOMPARE(int(Qt3DRender::QGraphicsApiFilter::DirectX), 4);
252 }
253
254 // TODO: Add equality test in 5.8 when we can add new api to
255 // test for compatibility and properly use operator == to really
256 // test for equality.
257};
258
259
260QTEST_MAIN(tst_QGraphicsApiFilter)
261
262#include "tst_qgraphicsapifilter.moc"
263

source code of qt3d/tests/auto/render/qgraphicsapifilter/tst_qgraphicsapifilter.cpp