1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite 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
30#include <QtTest/QtTest>
31
32#include <QtGui/private/qshaderformat_p.h>
33#include <QtGui/private/qshadernode_p.h>
34#include <QtGui/private/qshadernodeport_p.h>
35
36namespace
37{
38 QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion,
39 const QStringList &extensions = QStringList(),
40 const QString &vendor = QString())
41 {
42 auto format = QShaderFormat();
43 format.setApi(api);
44 format.setVersion(QVersionNumber(majorVersion, minorVersion));
45 format.setExtensions(extensions);
46 format.setVendor(vendor);
47 return format;
48 }
49
50 QShaderNodePort createPort(QShaderNodePort::Direction direction, const QString &name)
51 {
52 auto port = QShaderNodePort();
53 port.direction = direction;
54 port.name = name;
55 return port;
56 }
57}
58
59class tst_QShaderNodes : public QObject
60{
61 Q_OBJECT
62private slots:
63 void shouldManipulateFormatMembers();
64 void shouldVerifyFormatsEquality_data();
65 void shouldVerifyFormatsEquality();
66 void shouldVerifyFormatsCompatibilities_data();
67 void shouldVerifyFormatsCompatibilities();
68
69 void shouldHaveDefaultPortState();
70 void shouldVerifyPortsEquality_data();
71 void shouldVerifyPortsEquality();
72
73 void shouldManipulateNodeMembers();
74 void shouldHandleNodeRulesSupportAndOrder();
75};
76
77void tst_QShaderNodes::shouldManipulateFormatMembers()
78{
79 // GIVEN
80 auto format = QShaderFormat();
81
82 // THEN (default state)
83 QCOMPARE(format.api(), QShaderFormat::NoApi);
84 QCOMPARE(format.version().majorVersion(), 0);
85 QCOMPARE(format.version().minorVersion(), 0);
86 QCOMPARE(format.extensions(), QStringList());
87 QCOMPARE(format.vendor(), QString());
88 QVERIFY(!format.isValid());
89
90 // WHEN
91 format.setApi(QShaderFormat::OpenGLES);
92
93 // THEN
94 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
95 QCOMPARE(format.version().majorVersion(), 0);
96 QCOMPARE(format.version().minorVersion(), 0);
97 QCOMPARE(format.extensions(), QStringList());
98 QCOMPARE(format.vendor(), QString());
99 QVERIFY(!format.isValid());
100
101 // WHEN
102 format.setVersion(QVersionNumber(3));
103
104 // THEN
105 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
106 QCOMPARE(format.version().majorVersion(), 3);
107 QCOMPARE(format.version().minorVersion(), 0);
108 QCOMPARE(format.extensions(), QStringList());
109 QCOMPARE(format.vendor(), QString());
110 QVERIFY(format.isValid());
111
112 // WHEN
113 format.setVersion(QVersionNumber(3, 2));
114
115 // THEN
116 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
117 QCOMPARE(format.version().majorVersion(), 3);
118 QCOMPARE(format.version().minorVersion(), 2);
119 QCOMPARE(format.extensions(), QStringList());
120 QCOMPARE(format.vendor(), QString());
121 QVERIFY(format.isValid());
122
123 // WHEN
124 format.setExtensions({"foo", "bar"});
125
126 // THEN
127 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
128 QCOMPARE(format.version().majorVersion(), 3);
129 QCOMPARE(format.version().minorVersion(), 2);
130 QCOMPARE(format.extensions(), QStringList({"bar", "foo"}));
131 QCOMPARE(format.vendor(), QString());
132 QVERIFY(format.isValid());
133
134 // WHEN
135 format.setVendor(QStringLiteral("KDAB"));
136
137 // THEN
138 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
139 QCOMPARE(format.version().majorVersion(), 3);
140 QCOMPARE(format.version().minorVersion(), 2);
141 QCOMPARE(format.extensions(), QStringList({"bar", "foo"}));
142 QCOMPARE(format.vendor(), QStringLiteral("KDAB"));
143 QVERIFY(format.isValid());
144}
145
146void tst_QShaderNodes::shouldVerifyFormatsEquality_data()
147{
148 QTest::addColumn<QShaderFormat>(name: "left");
149 QTest::addColumn<QShaderFormat>(name: "right");
150 QTest::addColumn<bool>(name: "expected");
151
152 QTest::newRow(dataTag: "Equals") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
153 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
154 << true;
155 QTest::newRow(dataTag: "Apis") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
156 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
157 << false;
158 QTest::newRow(dataTag: "Major") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
159 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
160 << false;
161 QTest::newRow(dataTag: "Minor") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
162 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 1, extensions: {"foo", "bar"}, vendor: "KDAB")
163 << false;
164 QTest::newRow(dataTag: "Extensions") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
165 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo"}, vendor: "KDAB")
166 << false;
167 QTest::newRow(dataTag: "Vendor") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
168 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"})
169 << false;
170}
171
172void tst_QShaderNodes::shouldVerifyFormatsEquality()
173{
174 // GIVEN
175 QFETCH(QShaderFormat, left);
176 QFETCH(QShaderFormat, right);
177
178 // WHEN
179 const auto equal = (left == right);
180 const auto notEqual = (left != right);
181
182 // THEN
183 QFETCH(bool, expected);
184 QCOMPARE(equal, expected);
185 QCOMPARE(notEqual, !expected);
186}
187
188void tst_QShaderNodes::shouldVerifyFormatsCompatibilities_data()
189{
190 QTest::addColumn<QShaderFormat>(name: "reference");
191 QTest::addColumn<QShaderFormat>(name: "tested");
192 QTest::addColumn<bool>(name: "expected");
193
194 QTest::newRow(dataTag: "NoProfileVsES") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
195 << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
196 << true;
197 QTest::newRow(dataTag: "CoreProfileVsES") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
198 << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
199 << false;
200 QTest::newRow(dataTag: "CompatProfileVsES") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
201 << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
202 << true;
203
204 QTest::newRow(dataTag: "ESVsNoProfile") << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
205 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
206 << false;
207 QTest::newRow(dataTag: "ESVsCoreProfile") << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
208 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
209 << false;
210 QTest::newRow(dataTag: "ESVsCompatProfile") << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
211 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
212 << false;
213
214 QTest::newRow(dataTag: "CoreVsNoProfile") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
215 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
216 << false;
217 QTest::newRow(dataTag: "CoreVsCompat") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
218 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
219 << false;
220 QTest::newRow(dataTag: "CoreVsCore") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
221 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
222 << true;
223
224 QTest::newRow(dataTag: "NoProfileVsCore") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
225 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
226 << true;
227 QTest::newRow(dataTag: "NoProvileVsCompat") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
228 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
229 << true;
230 QTest::newRow(dataTag: "NoProfileVsNoProfile") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
231 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
232 << true;
233
234 QTest::newRow(dataTag: "CompatVsCore") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
235 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
236 << true;
237 QTest::newRow(dataTag: "CompatVsCompat") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
238 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
239 << true;
240 QTest::newRow(dataTag: "CompatVsNoProfile") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
241 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
242 << true;
243
244 QTest::newRow(dataTag: "MajorForwardCompat_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
245 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
246 << true;
247 QTest::newRow(dataTag: "MajorForwardCompat_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
248 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 4)
249 << true;
250 QTest::newRow(dataTag: "MajorForwardCompat_3") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
251 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
252 << false;
253 QTest::newRow(dataTag: "MajorForwardCompat_4") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 4)
254 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
255 << false;
256
257 QTest::newRow(dataTag: "MinorForwardCompat_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 1)
258 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
259 << true;
260 QTest::newRow(dataTag: "MinorForwardCompat_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
261 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 1)
262 << false;
263
264 QTest::newRow(dataTag: "Extensions_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"})
265 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo"})
266 << true;
267 QTest::newRow(dataTag: "Extensions_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo"})
268 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"})
269 << false;
270
271 QTest::newRow(dataTag: "Vendor_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
272 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {})
273 << true;
274 QTest::newRow(dataTag: "Vendor_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {})
275 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
276 << false;
277 QTest::newRow(dataTag: "Vendor_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
278 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
279 << true;
280}
281
282void tst_QShaderNodes::shouldVerifyFormatsCompatibilities()
283{
284 // GIVEN
285 QFETCH(QShaderFormat, reference);
286 QFETCH(QShaderFormat, tested);
287
288 // WHEN
289 const auto supported = reference.supports(other: tested);
290
291 // THEN
292 QFETCH(bool, expected);
293 QCOMPARE(supported, expected);
294}
295
296void tst_QShaderNodes::shouldHaveDefaultPortState()
297{
298 // GIVEN
299 auto port = QShaderNodePort();
300
301 // THEN
302 QCOMPARE(port.direction, QShaderNodePort::Output);
303 QVERIFY(port.name.isEmpty());
304}
305
306void tst_QShaderNodes::shouldVerifyPortsEquality_data()
307{
308 QTest::addColumn<QShaderNodePort>(name: "left");
309 QTest::addColumn<QShaderNodePort>(name: "right");
310 QTest::addColumn<bool>(name: "expected");
311
312 QTest::newRow(dataTag: "Equals") << createPort(direction: QShaderNodePort::Input, name: "foo")
313 << createPort(direction: QShaderNodePort::Input, name: "foo")
314 << true;
315 QTest::newRow(dataTag: "Direction") << createPort(direction: QShaderNodePort::Input, name: "foo")
316 << createPort(direction: QShaderNodePort::Output, name: "foo")
317 << false;
318 QTest::newRow(dataTag: "Name") << createPort(direction: QShaderNodePort::Input, name: "foo")
319 << createPort(direction: QShaderNodePort::Input, name: "bar")
320 << false;
321}
322
323void tst_QShaderNodes::shouldVerifyPortsEquality()
324{
325 // GIVEN
326 QFETCH(QShaderNodePort, left);
327 QFETCH(QShaderNodePort, right);
328
329 // WHEN
330 const auto equal = (left == right);
331 const auto notEqual = (left != right);
332
333 // THEN
334 QFETCH(bool, expected);
335 QCOMPARE(equal, expected);
336 QCOMPARE(notEqual, !expected);
337}
338
339void tst_QShaderNodes::shouldManipulateNodeMembers()
340{
341 // GIVEN
342 const auto openGLES2 = createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0);
343 const auto openGL3 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0);
344
345 const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"});
346 const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
347 const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
348
349 auto node = QShaderNode();
350
351 // THEN (default state)
352 QCOMPARE(node.type(), QShaderNode::Invalid);
353 QVERIFY(node.uuid().isNull());
354 QVERIFY(node.layers().isEmpty());
355 QVERIFY(node.ports().isEmpty());
356 QVERIFY(node.parameterNames().isEmpty());
357 QVERIFY(node.availableFormats().isEmpty());
358
359 // WHEN
360 const auto uuid = QUuid::createUuid();
361 node.setUuid(uuid);
362
363 // THEN
364 QCOMPARE(node.uuid(), uuid);
365
366 // WHEN
367 node.setLayers({"foo", "bar"});
368
369 // THEN
370 QCOMPARE(node.layers(), QStringList({"foo", "bar"}));
371
372 // WHEN
373 auto firstPort = QShaderNodePort();
374 firstPort.direction = QShaderNodePort::Input;
375 firstPort.name = QStringLiteral("foo");
376 node.addPort(port: firstPort);
377
378 // THEN
379 QCOMPARE(node.type(), QShaderNode::Output);
380 QCOMPARE(node.ports().size(), 1);
381 QCOMPARE(node.ports().at(0), firstPort);
382 QVERIFY(node.availableFormats().isEmpty());
383
384 // WHEN
385 auto secondPort = QShaderNodePort();
386 secondPort.direction = QShaderNodePort::Output;
387 secondPort.name = QStringLiteral("bar");
388 node.addPort(port: secondPort);
389
390 // THEN
391 QCOMPARE(node.type(), QShaderNode::Function);
392 QCOMPARE(node.ports().size(), 2);
393 QCOMPARE(node.ports().at(0), firstPort);
394 QCOMPARE(node.ports().at(1), secondPort);
395 QVERIFY(node.availableFormats().isEmpty());
396
397 // WHEN
398 node.removePort(port: firstPort);
399
400 // THEN
401 QCOMPARE(node.type(), QShaderNode::Input);
402 QCOMPARE(node.ports().size(), 1);
403 QCOMPARE(node.ports().at(0), secondPort);
404 QVERIFY(node.availableFormats().isEmpty());
405
406 // WHEN
407 node.setParameter(QStringLiteral("baz"), value: 42);
408
409 // THEN
410 QCOMPARE(node.type(), QShaderNode::Input);
411 QCOMPARE(node.ports().size(), 1);
412 QCOMPARE(node.ports().at(0), secondPort);
413 auto parameterNames = node.parameterNames();
414 parameterNames.sort();
415 QCOMPARE(parameterNames.size(), 1);
416 QCOMPARE(parameterNames.at(0), QStringLiteral("baz"));
417 QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42));
418 QVERIFY(node.availableFormats().isEmpty());
419
420 // WHEN
421 node.setParameter(QStringLiteral("bleh"), QStringLiteral("value"));
422
423 // THEN
424 QCOMPARE(node.type(), QShaderNode::Input);
425 QCOMPARE(node.ports().size(), 1);
426 QCOMPARE(node.ports().at(0), secondPort);
427 parameterNames = node.parameterNames();
428 parameterNames.sort();
429 QCOMPARE(parameterNames.size(), 2);
430 QCOMPARE(parameterNames.at(0), QStringLiteral("baz"));
431 QCOMPARE(parameterNames.at(1), QStringLiteral("bleh"));
432 QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42));
433 QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value")));
434 QVERIFY(node.availableFormats().isEmpty());
435
436 // WHEN
437 node.clearParameter(QStringLiteral("baz"));
438
439 // THEN
440 QCOMPARE(node.type(), QShaderNode::Input);
441 QCOMPARE(node.ports().size(), 1);
442 QCOMPARE(node.ports().at(0), secondPort);
443 parameterNames = node.parameterNames();
444 parameterNames.sort();
445 QCOMPARE(parameterNames.size(), 1);
446 QCOMPARE(parameterNames.at(0), QStringLiteral("bleh"));
447 QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant());
448 QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value")));
449 QVERIFY(node.availableFormats().isEmpty());
450
451 // WHEN
452 node.addRule(format: openGLES2, rule: es2Rule);
453 node.addRule(format: openGL3, rule: gl3Rule);
454
455 // THEN
456 QCOMPARE(node.availableFormats().size(), 2);
457 QCOMPARE(node.availableFormats().at(0), openGLES2);
458 QCOMPARE(node.availableFormats().at(1), openGL3);
459 QCOMPARE(node.rule(openGLES2), es2Rule);
460 QCOMPARE(node.rule(openGL3), gl3Rule);
461
462 // WHEN
463 node.removeRule(format: openGLES2);
464
465 // THEN
466 QCOMPARE(node.availableFormats().size(), 1);
467 QCOMPARE(node.availableFormats().at(0), openGL3);
468 QCOMPARE(node.rule(openGL3), gl3Rule);
469
470 // WHEN
471 node.addRule(format: openGLES2, rule: es2Rule);
472
473 // THEN
474 QCOMPARE(node.availableFormats().size(), 2);
475 QCOMPARE(node.availableFormats().at(0), openGL3);
476 QCOMPARE(node.availableFormats().at(1), openGLES2);
477 QCOMPARE(node.rule(openGLES2), es2Rule);
478 QCOMPARE(node.rule(openGL3), gl3Rule);
479
480 // WHEN
481 node.addRule(format: openGL3, rule: gl3bisRule);
482
483 // THEN
484 QCOMPARE(node.availableFormats().size(), 2);
485 QCOMPARE(node.availableFormats().at(0), openGLES2);
486 QCOMPARE(node.availableFormats().at(1), openGL3);
487 QCOMPARE(node.rule(openGLES2), es2Rule);
488 QCOMPARE(node.rule(openGL3), gl3bisRule);
489}
490
491void tst_QShaderNodes::shouldHandleNodeRulesSupportAndOrder()
492{
493 // GIVEN
494 const auto openGLES2 = createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0);
495 const auto openGL3 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0);
496 const auto openGL32 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 2);
497 const auto openGL4 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 4, minorVersion: 0);
498
499 const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"});
500 const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
501 const auto gl32Rule = QShaderNode::Rule(QByteArrayLiteral("gl32"), {"#pragma include gl32/foo.inc", "#pragma include gl32/bar.inc"});
502 const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
503
504 auto node = QShaderNode();
505
506 // WHEN
507 node.addRule(format: openGLES2, rule: es2Rule);
508 node.addRule(format: openGL3, rule: gl3Rule);
509
510 // THEN
511 QCOMPARE(node.availableFormats().size(), 2);
512 QCOMPARE(node.availableFormats().at(0), openGLES2);
513 QCOMPARE(node.availableFormats().at(1), openGL3);
514 QCOMPARE(node.rule(openGLES2), es2Rule);
515 QCOMPARE(node.rule(openGL3), gl3Rule);
516 QCOMPARE(node.rule(openGL32), gl3Rule);
517 QCOMPARE(node.rule(openGL4), gl3Rule);
518
519 // WHEN
520 node.addRule(format: openGL32, rule: gl32Rule);
521
522 // THEN
523 QCOMPARE(node.availableFormats().size(), 3);
524 QCOMPARE(node.availableFormats().at(0), openGLES2);
525 QCOMPARE(node.availableFormats().at(1), openGL3);
526 QCOMPARE(node.availableFormats().at(2), openGL32);
527 QCOMPARE(node.rule(openGLES2), es2Rule);
528 QCOMPARE(node.rule(openGL3), gl3Rule);
529 QCOMPARE(node.rule(openGL32), gl32Rule);
530 QCOMPARE(node.rule(openGL4), gl32Rule);
531
532 // WHEN
533 node.addRule(format: openGL3, rule: gl3bisRule);
534
535 // THEN
536 QCOMPARE(node.availableFormats().size(), 3);
537 QCOMPARE(node.availableFormats().at(0), openGLES2);
538 QCOMPARE(node.availableFormats().at(1), openGL32);
539 QCOMPARE(node.availableFormats().at(2), openGL3);
540 QCOMPARE(node.rule(openGLES2), es2Rule);
541 QCOMPARE(node.rule(openGL3), gl3bisRule);
542 QCOMPARE(node.rule(openGL32), gl3bisRule);
543 QCOMPARE(node.rule(openGL4), gl3bisRule);
544}
545
546QTEST_MAIN(tst_QShaderNodes)
547
548#include "tst_qshadernodes.moc"
549

source code of qtbase/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp