diff options
Diffstat (limited to 'tests/auto/gui')
47 files changed, 4199 insertions, 245 deletions
diff --git a/tests/auto/gui/gui.pro b/tests/auto/gui/gui.pro index 2fd3024afe..d7cda11513 100644 --- a/tests/auto/gui/gui.pro +++ b/tests/auto/gui/gui.pro @@ -9,8 +9,11 @@ SUBDIRS = \ painting \ qopenglconfig \ qopengl \ + qvulkan \ text \ util \ itemmodels \ !qtConfig(opengl): SUBDIRS -= qopengl qopenglconfig + +!qtConfig(vulkan): SUBDIRS -= qvulkan diff --git a/tests/auto/gui/image/qicon/tst_qicon.cpp b/tests/auto/gui/image/qicon/tst_qicon.cpp index 175179699d..bf8f7ade9e 100644 --- a/tests/auto/gui/image/qicon/tst_qicon.cpp +++ b/tests/auto/gui/image/qicon/tst_qicon.cpp @@ -530,16 +530,6 @@ void tst_QIcon::streamAvailableSizes() } } - -static inline bool operator<(const QSize &lhs, const QSize &rhs) -{ - if (lhs.width() < rhs.width()) - return true; - else if (lhs.width() == lhs.width()) - return lhs.height() < lhs.height(); - return false; -} - #ifndef QT_NO_WIDGETS void tst_QIcon::task184901_badCache() { diff --git a/tests/auto/gui/image/qiconhighdpi/icons/misc/button.9.png b/tests/auto/gui/image/qiconhighdpi/icons/misc/button.9.png Binary files differnew file mode 100644 index 0000000000..1a560a1d74 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/icons/misc/button.9.png diff --git a/tests/auto/gui/image/qiconhighdpi/icons/misc/button@2x.9.png b/tests/auto/gui/image/qiconhighdpi/icons/misc/button@2x.9.png Binary files differnew file mode 100644 index 0000000000..f010dc55c7 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/icons/misc/button@2x.9.png diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp index ce7f68a0a6..51892cca04 100644 --- a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp @@ -39,6 +39,7 @@ private slots: void initTestCase(); void fromTheme_data(); void fromTheme(); + void ninePatch(); }; tst_QIconHighDpi::tst_QIconHighDpi() @@ -182,6 +183,24 @@ void tst_QIconHighDpi::fromTheme() QCOMPARE(pixmap.devicePixelRatio(), expectedDpr); } +void tst_QIconHighDpi::ninePatch() +{ + const QIcon icon(":/icons/misc/button.9.png"); + const int dpr = qCeil(qApp->devicePixelRatio()); + + switch (dpr) { + case 1: + QCOMPARE(icon.availableSizes().size(), 1); + QCOMPARE(icon.availableSizes().at(0), QSize(42, 42)); + break; + case 2: + QCOMPARE(icon.availableSizes().size(), 2); + QCOMPARE(icon.availableSizes().at(0), QSize(42, 42)); + QCOMPARE(icon.availableSizes().at(1), QSize(82, 82)); + break; + } +} + int main(int argc, char *argv[]) { QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc index 80b5e38ee6..5cc1c6d9b1 100644 --- a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc @@ -4,5 +4,7 @@ <file>icons/testtheme/22x22/actions/appointment-new.png</file> <file>icons/testtheme/index.theme</file> <file>icons/testtheme/22x22@2/actions/appointment-new.png</file> + <file>icons/misc/button.9.png</file> + <file>icons/misc/button@2x.9.png</file> </qresource> </RCC> diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 73e11e7cc7..54eb8ab99c 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -33,6 +33,7 @@ #include <qimagereader.h> #include <qlist.h> #include <qmatrix.h> +#include <qrandom.h> #include <stdio.h> #include <qpainter.h> @@ -219,69 +220,71 @@ private slots: void toCGImage(); #endif + void hugeQImage(); + private: const QString m_prefix; }; -static QString formatToString(QImage::Format format) +static QLatin1String formatToString(QImage::Format format) { switch (format) { case QImage::Format_Invalid: - return QStringLiteral("Invalid"); + return QLatin1String("Invalid"); case QImage::Format_Mono: - return QStringLiteral("Mono"); + return QLatin1String("Mono"); case QImage::Format_MonoLSB: - return QStringLiteral("MonoLSB"); + return QLatin1String("MonoLSB"); case QImage::Format_Indexed8: - return QStringLiteral("Indexed8"); + return QLatin1String("Indexed8"); case QImage::Format_RGB32: - return QStringLiteral("RGB32"); + return QLatin1String("RGB32"); case QImage::Format_ARGB32: - return QStringLiteral("ARGB32"); + return QLatin1String("ARGB32"); case QImage::Format_ARGB32_Premultiplied: - return QStringLiteral("ARGB32pm"); + return QLatin1String("ARGB32pm"); case QImage::Format_RGB16: - return QStringLiteral("RGB16"); + return QLatin1String("RGB16"); case QImage::Format_ARGB8565_Premultiplied: - return QStringLiteral("ARGB8565pm"); + return QLatin1String("ARGB8565pm"); case QImage::Format_RGB666: - return QStringLiteral("RGB666"); + return QLatin1String("RGB666"); case QImage::Format_ARGB6666_Premultiplied: - return QStringLiteral("ARGB6666pm"); + return QLatin1String("ARGB6666pm"); case QImage::Format_RGB555: - return QStringLiteral("RGB555"); + return QLatin1String("RGB555"); case QImage::Format_ARGB8555_Premultiplied: - return QStringLiteral("ARGB8555pm"); + return QLatin1String("ARGB8555pm"); case QImage::Format_RGB888: - return QStringLiteral("RGB888"); + return QLatin1String("RGB888"); case QImage::Format_RGB444: - return QStringLiteral("RGB444"); + return QLatin1String("RGB444"); case QImage::Format_ARGB4444_Premultiplied: - return QStringLiteral("ARGB4444pm"); + return QLatin1String("ARGB4444pm"); case QImage::Format_RGBX8888: - return QStringLiteral("RGBx88888"); + return QLatin1String("RGBx88888"); case QImage::Format_RGBA8888: - return QStringLiteral("RGBA88888"); + return QLatin1String("RGBA88888"); case QImage::Format_RGBA8888_Premultiplied: - return QStringLiteral("RGBA88888pm"); + return QLatin1String("RGBA88888pm"); case QImage::Format_BGR30: - return QStringLiteral("BGR30"); + return QLatin1String("BGR30"); case QImage::Format_A2BGR30_Premultiplied: - return QStringLiteral("A2BGR30pm"); + return QLatin1String("A2BGR30pm"); case QImage::Format_RGB30: - return QStringLiteral("RGB30"); + return QLatin1String("RGB30"); case QImage::Format_A2RGB30_Premultiplied: - return QStringLiteral("A2RGB30pm"); + return QLatin1String("A2RGB30pm"); case QImage::Format_Alpha8: - return QStringLiteral("Alpha8"); + return QLatin1String("Alpha8"); case QImage::Format_Grayscale8: - return QStringLiteral("Grayscale8"); + return QLatin1String("Grayscale8"); default: break; }; Q_UNREACHABLE(); qWarning("Unhandled image format"); - return QStringLiteral("unknown"); + return QLatin1String("unknown"); } tst_QImage::tst_QImage() @@ -315,8 +318,7 @@ void tst_QImage::create() { bool cr = true; QT_TRY { - //QImage image(7000000, 7000000, 8, 256, QImage::IgnoreEndian); - QImage image(7000000, 7000000, QImage::Format_Indexed8); + QImage image(700000000, 70000000, QImage::Format_Indexed8); image.setColorCount(256); cr = !image.isNull(); } QT_CATCH (...) { @@ -1753,7 +1755,7 @@ void tst_QImage::smoothScale2() static inline int rand8() { - return int(256. * (qrand() / (RAND_MAX + 1.0))); + return QRandomGenerator::global()->bounded(256); } void tst_QImage::smoothScale3_data() @@ -2334,7 +2336,7 @@ void tst_QImage::rgbSwapped_data() QTest::addColumn<QImage::Format>("format"); for (int i = QImage::Format_Indexed8; i < QImage::Format_Alpha8; ++i) { - QTest::newRow(qPrintable(formatToString(QImage::Format(i)))) << QImage::Format(i); + QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i); } } @@ -2379,7 +2381,7 @@ void tst_QImage::rgbSwapped() QCOMPARE(image, imageSwappedTwice); - QCOMPARE(memcmp(image.constBits(), imageSwappedTwice.constBits(), image.byteCount()), 0); + QCOMPARE(memcmp(image.constBits(), imageSwappedTwice.constBits(), image.sizeInBytes()), 0); } void tst_QImage::mirrored_data() @@ -2491,7 +2493,7 @@ void tst_QImage::mirrored() QCOMPARE(image, imageMirroredTwice); if (format != QImage::Format_Mono && format != QImage::Format_MonoLSB) - QCOMPARE(memcmp(image.constBits(), imageMirroredTwice.constBits(), image.byteCount()), 0); + QCOMPARE(memcmp(image.constBits(), imageMirroredTwice.constBits(), image.sizeInBytes()), 0); else { for (int i = 0; i < image.height(); ++i) for (int j = 0; j < image.width(); ++j) @@ -2553,8 +2555,8 @@ void tst_QImage::inplaceRgbSwapped() QImage dataSwapped; { QVERIFY(!orig.isNull()); - volatileData = new uchar[orig.byteCount()]; - memcpy(volatileData, orig.constBits(), orig.byteCount()); + volatileData = new uchar[orig.sizeInBytes()]; + memcpy(volatileData, orig.constBits(), orig.sizeInBytes()); QImage dataImage; if (rw) @@ -2589,11 +2591,12 @@ void tst_QImage::inplaceMirrored_data() continue; if (i == QImage::Format_RGB444 || i == QImage::Format_ARGB4444_Premultiplied) continue; - QTest::newRow(qPrintable(formatToString(QImage::Format(i)) + QStringLiteral(", vertical"))) + const auto fmt = formatToString(QImage::Format(i)); + QTest::addRow("%s, vertical", fmt.data()) << QImage::Format(i) << true << false; - QTest::newRow(qPrintable(formatToString(QImage::Format(i)) + QStringLiteral(", horizontal"))) + QTest::addRow("%s, horizontal", fmt.data()) << QImage::Format(i) << false << true; - QTest::newRow(qPrintable(formatToString(QImage::Format(i)) + QStringLiteral(", horizontal+vertical"))) + QTest::addRow("%s, horizontal+vertical", fmt.data()) << QImage::Format(i) << true << true; } } @@ -2664,8 +2667,8 @@ void tst_QImage::inplaceMirrored() QImage dataSwapped; { QVERIFY(!orig.isNull()); - volatileData = new uchar[orig.byteCount()]; - memcpy(volatileData, orig.constBits(), orig.byteCount()); + volatileData = new uchar[orig.sizeInBytes()]; + memcpy(volatileData, orig.constBits(), orig.sizeInBytes()); QImage dataImage; if (rw) @@ -2757,12 +2760,12 @@ void tst_QImage::genericRgbConversion_data() QTest::addColumn<QImage::Format>("dest_format"); for (int i = QImage::Format_RGB32; i < QImage::Format_Alpha8; ++i) { - const QString formatI = formatToString(QImage::Format(i)); + const QLatin1String formatI = formatToString(QImage::Format(i)); for (int j = QImage::Format_RGB32; j < QImage::Format_Alpha8; ++j) { if (i == j) continue; - const QString test = formatI + QLatin1String(" -> ") + formatToString(QImage::Format(j)); - QTest::newRow(qPrintable(test)) << QImage::Format(i) << QImage::Format(j); + QTest::addRow("%s -> %s", formatI.data(), formatToString(QImage::Format(j)).data()) + << QImage::Format(i) << QImage::Format(j); } } } @@ -2799,8 +2802,8 @@ void tst_QImage::inplaceRgbConversion_data() for (int j = QImage::Format_RGB32; j < QImage::Format_Alpha8; ++j) { if (i == j) continue; - QString test = QString::fromLatin1("%1 -> %2").arg(formatToString(QImage::Format(i))).arg(formatToString(QImage::Format(j))); - QTest::newRow(qPrintable(test)) << QImage::Format(i) << QImage::Format(j); + QTest::addRow("%s -> %s", formatToString(QImage::Format(i)).data(), formatToString(QImage::Format(j)).data()) + << QImage::Format(i) << QImage::Format(j); } } } @@ -2966,7 +2969,7 @@ void tst_QImage::invertPixelsRGB_data() QTest::addColumn<QImage::Format>("image_format"); for (int i = QImage::Format_RGB32; i < QImage::Format_Alpha8; ++i) { - QTest::newRow(qPrintable(formatToString(QImage::Format(i)))) << QImage::Format(i); + QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i); } } @@ -3143,10 +3146,10 @@ void tst_QImage::rgb30Repremul_data() { QTest::addColumn<uint>("color"); for (int i = 255; i > 0; i -= 15) { - QTest::newRow(qPrintable(QStringLiteral("100% red=") + QString::number(i))) << qRgba(i, 0, 0, 0xff); - QTest::newRow(qPrintable(QStringLiteral("75% red=") + QString::number(i))) << qRgba(i, 0, 0, 0xc0); - QTest::newRow(qPrintable(QStringLiteral("50% red=") + QString::number(i))) << qRgba(i, 0, 0, 0x80); - QTest::newRow(qPrintable(QStringLiteral("37.5% red=") + QString::number(i))) << qRgba(i, 0, 0, 0x60); + QTest::addRow("100%% red=%d", i) << qRgba(i, 0, 0, 0xff); + QTest::addRow("75%% red=%d", i) << qRgba(i, 0, 0, 0xc0); + QTest::addRow("50%% red=%d", i) << qRgba(i, 0, 0, 0x80); + QTest::addRow("37.5%% red=%d", i) << qRgba(i, 0, 0, 0x60); } } @@ -3406,7 +3409,7 @@ void tst_QImage::toCGImage_data() QImage::Format_RGBA8888, QImage::Format_RGBX8888, QImage::Format_ARGB32_Premultiplied }; for (int i = QImage::Format_Invalid; i < QImage::Format_Grayscale8; ++i) { - QTest::newRow(qPrintable(formatToString(QImage::Format(i)))) + QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i) << supported.contains(QImage::Format(i)); } } @@ -3428,6 +3431,32 @@ void tst_QImage::toCGImage() #endif +void tst_QImage::hugeQImage() +{ +#if Q_PROCESSOR_WORDSIZE < 8 + QSKIP("Test only makes sense on 64-bit machines"); +#else + QImage image(25000, 25000, QImage::Format_RGB32); + + QVERIFY(!image.isNull()); + QCOMPARE(image.height(), 25000); + QCOMPARE(image.width(), 25000); + QCOMPARE(image.sizeInBytes(), qssize_t(25000)*25000*4); + QCOMPARE(image.bytesPerLine(), 25000 * 4); + + QCOMPARE(image.constScanLine(24990), image.constBits() + qssize_t(25000)*24990*4); + + image.setPixel(20000, 24990, 0xffaabbcc); + QCOMPARE(image.pixel(20000, 24990), 0xffaabbcc); + QCOMPARE((reinterpret_cast<const unsigned int *>(image.constScanLine(24990)))[20000], 0xffaabbcc); + + QImage canvas(100, 100, QImage::Format_RGB32); + QPainter painter(&canvas); + painter.drawImage(0,0, image, 19950, 24900, 100, 100); + painter.end(); + QCOMPARE(reinterpret_cast<const unsigned int *>(canvas.constScanLine(90))[50], 0xffaabbcc); +#endif +} QTEST_GUILESS_MAIN(tst_QImage) #include "tst_qimage.moc" diff --git a/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp b/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp index d5c624833c..a53c2ddb5b 100644 --- a/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp +++ b/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp @@ -77,6 +77,9 @@ private slots: void saveWithNoFormat(); void saveToTemporaryFile(); + + void writeEmpty(); + private: QTemporaryDir m_temporaryDir; QString prefix; @@ -463,7 +466,7 @@ void tst_QImageWriter::saveWithNoFormat() SKIP_IF_UNSUPPORTED(format); QImage niceImage(64, 64, QImage::Format_ARGB32); - memset(niceImage.bits(), 0, niceImage.byteCount()); + memset(niceImage.bits(), 0, niceImage.sizeInBytes()); QImageWriter writer(fileName /* , 0 - no format! */); if (error != 0) { @@ -529,5 +532,18 @@ void tst_QImageWriter::saveToTemporaryFile() } } +void tst_QImageWriter::writeEmpty() +{ + // check writing a null QImage errors gracefully + QTemporaryDir dir; + QVERIFY2(dir.isValid(), qPrintable(dir.errorString())); + QString fileName(dir.path() + QLatin1String("/testimage.bmp")); + QVERIFY(!QFileInfo(fileName).exists()); + QImageWriter writer(fileName); + QVERIFY(!writer.write(QImage())); + QCOMPARE(writer.error(), QImageWriter::InvalidImageError); + QVERIFY(!QFileInfo(fileName).exists()); +} + QTEST_MAIN(tst_QImageWriter) #include "tst_qimagewriter.moc" diff --git a/tests/auto/gui/kernel/kernel.pro b/tests/auto/gui/kernel/kernel.pro index 559395a9ae..46786262c0 100644 --- a/tests/auto/gui/kernel/kernel.pro +++ b/tests/auto/gui/kernel/kernel.pro @@ -2,6 +2,7 @@ TEMPLATE=subdirs SUBDIRS=\ qbackingstore \ qclipboard \ + qcursor \ qdrag \ qevent \ qfileopenevent \ diff --git a/tests/auto/gui/kernel/noqteventloop/tst_noqteventloop.cpp b/tests/auto/gui/kernel/noqteventloop/tst_noqteventloop.cpp index 37d97cd3db..4fca9a07fc 100644 --- a/tests/auto/gui/kernel/noqteventloop/tst_noqteventloop.cpp +++ b/tests/auto/gui/kernel/noqteventloop/tst_noqteventloop.cpp @@ -64,7 +64,7 @@ public: m_received.clear(); } - bool event(QEvent *event) + bool event(QEvent *event) override { m_received[event->type()]++; return QWindow::event(event); diff --git a/tests/auto/gui/kernel/qcursor/qcursor.pro b/tests/auto/gui/kernel/qcursor/qcursor.pro new file mode 100644 index 0000000000..16e7d7c41c --- /dev/null +++ b/tests/auto/gui/kernel/qcursor/qcursor.pro @@ -0,0 +1,6 @@ +QT += testlib +TARGET = tst_qcursor +CONFIG += testcase +CONFIG -= app_bundle + +SOURCES += tst_qcursor.cpp diff --git a/tests/auto/gui/kernel/qcursor/tst_qcursor.cpp b/tests/auto/gui/kernel/qcursor/tst_qcursor.cpp new file mode 100644 index 0000000000..d505f5a655 --- /dev/null +++ b/tests/auto/gui/kernel/qcursor/tst_qcursor.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QTest> +#include <qcursor.h> +#include <qpixmap.h> +#include <qbitmap.h> + +class tst_QCursor : public QObject +{ + Q_OBJECT + +private slots: + void equality(); +}; + +#define VERIFY_EQUAL(lhs, rhs) \ + QVERIFY(lhs == rhs); \ + QVERIFY(rhs == lhs); \ + QVERIFY(!(rhs != lhs)); \ + QVERIFY(!(lhs != rhs)) + +#define VERIFY_DIFFERENT(lhs, rhs) \ + QVERIFY(lhs != rhs); \ + QVERIFY(rhs != lhs); \ + QVERIFY(!(rhs == lhs)); \ + QVERIFY(!(lhs == rhs)) + +void tst_QCursor::equality() +{ + VERIFY_EQUAL(QCursor(), QCursor()); + VERIFY_EQUAL(QCursor(Qt::CrossCursor), QCursor(Qt::CrossCursor)); + VERIFY_DIFFERENT(QCursor(Qt::CrossCursor), QCursor()); + + // Shape + QCursor shapeCursor(Qt::WaitCursor); + VERIFY_EQUAL(shapeCursor, shapeCursor); + QCursor shapeCursorCopy(shapeCursor); + VERIFY_EQUAL(shapeCursor, shapeCursorCopy); + shapeCursorCopy.setShape(Qt::DragMoveCursor); + VERIFY_DIFFERENT(shapeCursor, shapeCursorCopy); + shapeCursorCopy.setShape(shapeCursor.shape()); + VERIFY_EQUAL(shapeCursor, shapeCursorCopy); + + // Pixmap + QPixmap pixmap(16, 16); + QCursor pixmapCursor(pixmap); + VERIFY_EQUAL(pixmapCursor, pixmapCursor); + VERIFY_EQUAL(pixmapCursor, QCursor(pixmapCursor)); + VERIFY_EQUAL(pixmapCursor, QCursor(pixmap)); + VERIFY_DIFFERENT(pixmapCursor, QCursor()); + VERIFY_DIFFERENT(pixmapCursor, QCursor(pixmap, 5, 5)); + VERIFY_DIFFERENT(pixmapCursor, QCursor(QPixmap(16, 16))); + VERIFY_DIFFERENT(pixmapCursor, shapeCursor); + + // Bitmap & mask + QBitmap bitmap(16, 16); + QBitmap mask(16, 16); + QCursor bitmapCursor(bitmap, mask); + VERIFY_EQUAL(bitmapCursor, bitmapCursor); + VERIFY_EQUAL(bitmapCursor, QCursor(bitmapCursor)); + VERIFY_EQUAL(bitmapCursor, QCursor(bitmap, mask)); + VERIFY_DIFFERENT(bitmapCursor, QCursor()); + VERIFY_DIFFERENT(bitmapCursor, QCursor(bitmap, mask, 5, 5)); + VERIFY_DIFFERENT(bitmapCursor, QCursor(bitmap, QBitmap(16, 16))); + VERIFY_DIFFERENT(bitmapCursor, QCursor(QBitmap(16, 16), mask)); + VERIFY_DIFFERENT(bitmapCursor, shapeCursor); + VERIFY_DIFFERENT(bitmapCursor, pixmapCursor); + + // Empty pixmap + QPixmap emptyPixmap; + QCursor emptyPixmapCursor(emptyPixmap); + QCOMPARE(emptyPixmapCursor.shape(), Qt::ArrowCursor); + VERIFY_EQUAL(emptyPixmapCursor, QCursor()); + VERIFY_EQUAL(emptyPixmapCursor, QCursor(emptyPixmap, 5, 5)); + VERIFY_DIFFERENT(emptyPixmapCursor, shapeCursor); + VERIFY_DIFFERENT(emptyPixmapCursor, pixmapCursor); + VERIFY_DIFFERENT(emptyPixmapCursor, bitmapCursor); + + // Empty bitmap & mask + QBitmap emptyBitmap; + QCursor emptyBitmapCursor(emptyBitmap, emptyBitmap); + QCOMPARE(emptyBitmapCursor.shape(), Qt::ArrowCursor); + VERIFY_EQUAL(emptyBitmapCursor, QCursor()); + VERIFY_EQUAL(emptyBitmapCursor, QCursor(emptyBitmap, emptyBitmap, 5, 5)); + VERIFY_EQUAL(emptyBitmapCursor, QCursor(emptyBitmap, mask)); + VERIFY_EQUAL(emptyBitmapCursor, QCursor(bitmap, emptyBitmap)); + VERIFY_EQUAL(emptyBitmapCursor, emptyPixmapCursor); + VERIFY_DIFFERENT(emptyBitmapCursor, shapeCursor); + VERIFY_DIFFERENT(emptyBitmapCursor, pixmapCursor); + VERIFY_DIFFERENT(emptyBitmapCursor, bitmapCursor); +} + +#undef VERIFY_EQUAL +#undef VERIFY_DIFFERENT + +QTEST_MAIN(tst_QCursor) +#include "tst_qcursor.moc" diff --git a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp index a935258fb8..6ba488aaa7 100644 --- a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp +++ b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp @@ -227,7 +227,7 @@ void tst_QGuiApplication::focusObject() QOpenGLContext context; context.create(); context.makeCurrent(&window1); - QTest::qWaitForWindowExposed(&window1); // Buffer swap only succeeds with exposed window + QVERIFY(QTest::qWaitForWindowExposed(&window1)); // Buffer swap only succeeds with exposed window context.swapBuffers(&window1); #endif @@ -392,7 +392,7 @@ void tst_QGuiApplication::changeFocusWindow() QOpenGLContext context; context.create(); context.makeCurrent(&window1); - QTest::qWaitForWindowExposed(&window1); // Buffer swap only succeeds with exposed window + QVERIFY(QTest::qWaitForWindowExposed(&window1)); // Buffer swap only succeeds with exposed window context.swapBuffers(&window1); #endif FocusChangeWindow window2; @@ -406,7 +406,7 @@ void tst_QGuiApplication::changeFocusWindow() #if defined(Q_OS_QNX) // We either need to create a eglSurface or a create a backing store // and then post the window in order for screen to show the window context.makeCurrent(&window2); - QTest::qWaitForWindowExposed(&window2); // Buffer swap only succeeds with exposed window + QVERIFY(QTest::qWaitForWindowExposed(&window2)); // Buffer swap only succeeds with exposed window context.swapBuffers(&window2); #endif QVERIFY(QTest::qWaitForWindowExposed(&window1)); diff --git a/tests/auto/gui/kernel/qguitimer/qguitimer.pro b/tests/auto/gui/kernel/qguitimer/qguitimer.pro index 8a71e48007..9c58f0e22c 100644 --- a/tests/auto/gui/kernel/qguitimer/qguitimer.pro +++ b/tests/auto/gui/kernel/qguitimer/qguitimer.pro @@ -1,4 +1,4 @@ CONFIG += testcase TARGET = tst_qguitimer -QT = core gui testlib +QT = core core-private gui testlib SOURCES += ../../../corelib/kernel/qtimer/tst_qtimer.cpp diff --git a/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp b/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp index 6394a956bd..f8b6bf064a 100644 --- a/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp +++ b/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp @@ -36,14 +36,18 @@ #include <QLibraryInfo> #ifdef Q_OS_MAC -#ifdef Q_OS_OSX -#include <Carbon/Carbon.h> -#endif struct MacSpecialKey { int key; ushort macSymbol; }; +// Unicode code points for the glyphs associated with these keys +// Defined by Carbon headers but not anywhere in Cocoa +static const int kShiftUnicode = 0x21E7; +static const int kControlUnicode = 0x2303; +static const int kOptionUnicode = 0x2325; +static const int kCommandUnicode = 0x2318; + static const int NumEntries = 21; static const MacSpecialKey entries[NumEntries] = { { Qt::Key_Escape, 0x238B }, @@ -61,12 +65,10 @@ static const MacSpecialKey entries[NumEntries] = { { Qt::Key_Down, 0x2193 }, { Qt::Key_PageUp, 0x21DE }, { Qt::Key_PageDown, 0x21DF }, -#ifdef Q_OS_OSX { Qt::Key_Shift, kShiftUnicode }, { Qt::Key_Control, kCommandUnicode }, { Qt::Key_Meta, kControlUnicode }, { Qt::Key_Alt, kOptionUnicode }, -#endif { Qt::Key_CapsLock, 0x21EA }, }; diff --git a/tests/auto/gui/kernel/qopenglwindow/tst_qopenglwindow.cpp b/tests/auto/gui/kernel/qopenglwindow/tst_qopenglwindow.cpp index 58dee6f6ca..d882dc3888 100644 --- a/tests/auto/gui/kernel/qopenglwindow/tst_qopenglwindow.cpp +++ b/tests/auto/gui/kernel/qopenglwindow/tst_qopenglwindow.cpp @@ -62,7 +62,7 @@ void tst_QOpenGLWindow::create() w.resize(640, 480); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w)); QVERIFY(w.isValid()); } @@ -111,7 +111,7 @@ void tst_QOpenGLWindow::basic() w.reset(); w.resize(640, 480); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w)); // Check that the virtuals are invoked. QCOMPARE(w.initCount, 1); @@ -170,7 +170,7 @@ void tst_QOpenGLWindow::painter() PainterWindow w; w.resize(400, 400); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w)); QCOMPARE(w.img.size(), w.size() * w.devicePixelRatio()); QVERIFY(w.img.pixel(QPoint(5, 5) * w.devicePixelRatio()) == qRgb(0, 0, 255)); @@ -212,7 +212,7 @@ void tst_QOpenGLWindow::partial() PartialPainterWindow w(u); w.resize(800, 400); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w)); // Add a couple of small blue rects. for (int i = 0; i < 10; ++i) { @@ -285,7 +285,7 @@ void tst_QOpenGLWindow::underOver() PaintUnderOverWindow w; w.resize(400, 400); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w)); // under -> paint -> over -> under -> paint -> ... is the only acceptable sequence QCOMPARE(w.m_state, PaintUnderOverWindow::PaintOver); diff --git a/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp b/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp index 41fcdf9f30..3421622fe3 100644 --- a/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp +++ b/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp @@ -46,7 +46,7 @@ void tst_QRasterWindow::create() w.resize(640, 480); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w)); } class PainterWindow : public QRasterWindow @@ -70,7 +70,7 @@ void tst_QRasterWindow::basic() w.reset(); w.resize(400, 400); w.show(); - QTest::qWaitForWindowExposed(&w); + QVERIFY(QTest::qWaitForWindowExposed(&w));; QVERIFY(w.paintCount >= 1); diff --git a/tests/auto/gui/kernel/qwindow/BLACKLIST b/tests/auto/gui/kernel/qwindow/BLACKLIST index 1a4885b895..3e03d9e236 100644 --- a/tests/auto/gui/kernel/qwindow/BLACKLIST +++ b/tests/auto/gui/kernel/qwindow/BLACKLIST @@ -19,5 +19,9 @@ ubuntu-16.04 osx [modalWindowModallity] osx +[visibility] +osx-10.11 ci +osx-10.12 ci + [testInputEvents] rhel-7.4 diff --git a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp index 6452b9ecd0..039d095ce6 100644 --- a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp +++ b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp @@ -269,12 +269,21 @@ class Window : public QWindow { public: Window(const Qt::WindowFlags flags = Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint) + : QWindow(), lastReceivedWindowState(windowState()) { reset(); setFlags(flags); #if defined(Q_OS_QNX) setSurfaceType(QSurface::OpenGLSurface); #endif + +#if !defined(Q_OS_MACOS) + // FIXME: All platforms should send window-state change events, regardless + // of the sync/async nature of the the underlying platform, but they don't. + connect(this, &QWindow::windowStateChanged, [=]() { + lastReceivedWindowState = windowState(); + }); +#endif } void reset() @@ -299,6 +308,10 @@ public: case QEvent::Move: m_framePositionsOnMove << framePosition(); break; + + case QEvent::WindowStateChange: + lastReceivedWindowState = windowState(); + default: break; } @@ -327,6 +340,8 @@ public: } QVector<QPoint> m_framePositionsOnMove; + Qt::WindowStates lastReceivedWindowState; + private: QHash<QEvent::Type, int> m_received; QVector<QEvent::Type> m_order; @@ -475,7 +490,7 @@ void tst_QWindow::positioning() window.showNormal(); QCoreApplication::processEvents(); - QTest::qWaitForWindowExposed(&window); + QVERIFY(QTest::qWaitForWindowExposed(&window)); QMargins originalMargins = window.frameMargins(); @@ -487,17 +502,15 @@ void tst_QWindow::positioning() window.reset(); window.setWindowState(Qt::WindowFullScreen); - QCoreApplication::processEvents(); + QTRY_COMPARE(window.lastReceivedWindowState, Qt::WindowFullScreen); QTRY_VERIFY(window.received(QEvent::Resize) > 0); - QTest::qWait(2000); window.reset(); window.setWindowState(Qt::WindowNoState); - QCoreApplication::processEvents(); + QTRY_COMPARE(window.lastReceivedWindowState, Qt::WindowNoState); QTRY_VERIFY(window.received(QEvent::Resize) > 0); - QTest::qWait(2000); QTRY_COMPARE(originalPos, window.position()); QTRY_COMPARE(originalFramePos, window.framePosition()); @@ -1470,7 +1483,7 @@ void tst_QWindow::activateAndClose() window.showNormal(); #if defined(Q_OS_QNX) // We either need to create a eglSurface or a create a backing store // and then post the window in order for screen to show the window - QTest::qWaitForWindowExposed(&window); + QVERIFY(QTest::qWaitForWindowExposed(&window)); QOpenGLContext context; context.create(); context.makeCurrent(&window); @@ -1753,7 +1766,7 @@ void tst_QWindow::visibility() { qRegisterMetaType<Qt::WindowModality>("QWindow::Visibility"); - QWindow window; + Window window; QSignalSpy spy(&window, SIGNAL(visibilityChanged(QWindow::Visibility))); window.setVisibility(QWindow::AutomaticVisibility); @@ -1774,11 +1787,13 @@ void tst_QWindow::visibility() QCOMPARE(window.windowState(), Qt::WindowFullScreen); QCOMPARE(window.visibility(), QWindow::FullScreen); QCOMPARE(spy.count(), 1); + QTRY_COMPARE(window.lastReceivedWindowState, Qt::WindowFullScreen); spy.clear(); window.setWindowState(Qt::WindowNoState); QCOMPARE(window.visibility(), QWindow::Windowed); QCOMPARE(spy.count(), 1); + QTRY_COMPARE(window.lastReceivedWindowState, Qt::WindowNoState); spy.clear(); window.setVisible(false); @@ -1791,16 +1806,27 @@ void tst_QWindow::mask() { QRegion mask = QRect(10, 10, 800 - 20, 600 - 20); - QWindow window; - window.resize(800, 600); - window.setMask(mask); + { + QWindow window; + window.resize(800, 600); + QCOMPARE(window.mask(), QRegion()); - QCOMPARE(window.mask(), QRegion()); + window.create(); + window.setMask(mask); + QCOMPARE(window.mask(), mask); + } - window.create(); - window.setMask(mask); + { + QWindow window; + window.resize(800, 600); + QCOMPARE(window.mask(), QRegion()); + + window.setMask(mask); + QCOMPARE(window.mask(), mask); + window.create(); + QCOMPARE(window.mask(), mask); + } - QCOMPARE(window.mask(), mask); } void tst_QWindow::initialSize() @@ -1845,6 +1871,9 @@ void tst_QWindow::modalDialog() if (!QGuiApplication::platformName().compare(QLatin1String("wayland"), Qt::CaseInsensitive)) QSKIP("Wayland: This fails. Figure out why."); + if (QGuiApplication::platformName() == QLatin1String("cocoa")) + QSKIP("Test fails due to QTBUG-61965, and is slow due to QTBUG-61964"); + QWindow normalWindow; normalWindow.setFramePosition(m_availableTopLeft + QPoint(80, 80)); normalWindow.resize(m_testWindowSize); diff --git a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp index c2c04b69c5..96d983c8f7 100644 --- a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp +++ b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp @@ -2278,8 +2278,8 @@ void tst_QMatrixNxN::rotate4x4_data() float y = 2.0f; float z = -6.0f; float angle = -45.0f; - float c = std::cos(angle * M_PI / 180.0f); - float s = std::sin(angle * M_PI / 180.0f); + float c = std::cos(qDegreesToRadians(angle)); + float s = std::sin(qDegreesToRadians(angle)); float len = std::sqrt(x * x + y * y + z * z); float xu = x / len; float yu = y / len; diff --git a/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp b/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp index 53af65e010..097dd111d3 100644 --- a/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp +++ b/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp @@ -815,7 +815,7 @@ void tst_QQuaternion::fromAxisAndAngle() // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56 // to calculate the answer we expect to get. QVector3D vector = QVector3D(x1, y1, z1).normalized(); - const float a = (angle * M_PI / 180.0) / 2.0; + const float a = qDegreesToRadians(angle) / 2.0; const float sin_a = std::sin(a); const float cos_a = std::cos(a); QQuaternion result(cos_a, diff --git a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp index 1ce7e797fc..6809aea086 100644 --- a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp +++ b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp @@ -545,19 +545,32 @@ void tst_QColor::setNamedColor_data() QColor bySetNamedColor; \ bySetNamedColor.setNamedColor(expr); \ auto byCtor = QColor(expr); \ - QTest::newRow(e.name + QByteArrayLiteral(#expr)) \ + QTest::addRow("%s: %s", e.name, #expr) \ << byCtor << bySetNamedColor << expected; \ } while (0) \ /*end*/ - ROW(QLatin1String(e.name)); - ROW(QString(QLatin1String(e.name))); + const auto l1 = QLatin1String(e.name); + const auto l1UpperBA = QByteArray(e.name).toUpper(); + const auto l1Upper = QLatin1String(l1UpperBA); + const auto l1SpaceBA = QByteArray(e.name).insert(1, ' '); + const auto l1Space = QLatin1String(l1SpaceBA); + + const auto u16 = QString(l1); + const auto u16Upper = u16.toUpper(); + const auto u16Space = QString(u16).insert(1, ' '); + + ROW(l1); + ROW(u16); + ROW(QStringView(u16)); // name should be case insensitive - ROW(QLatin1String(QByteArray(e.name).toUpper())); - ROW(QString(e.name).toUpper()); + ROW(l1Upper); + ROW(u16Upper); + ROW(QStringView(u16Upper)); // spaces should be ignored - ROW(QLatin1String(QByteArray(e.name).insert(1, ' '))); - ROW(QString(e.name).insert(1, ' ')); + ROW(l1Space); + ROW(u16Space); + ROW(QStringView(u16Space)); #undef ROW } } diff --git a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp index 6b6869c2ba..b8243a2b54 100644 --- a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp +++ b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp @@ -45,6 +45,7 @@ #include <qdesktopwidget.h> #endif #include <qpixmap.h> +#include <qrandom.h> #include <private/qdrawhelper_p.h> #include <qpainter.h> @@ -399,51 +400,6 @@ void tst_QPainter::cleanupTestCase() QFile::remove(QLatin1String("foo.png")); } -static const char* const maskSource_data[] = { -"16 13 6 1", -". c None", -"d c #000000", -"# c #999999", -"c c #cccccc", -"b c #ffff00", -"a c #ffffff", -"...#####........", -"..#aaaaa#.......", -".#abcbcba######.", -".#acbcbcaaaaaa#d", -".#abcbcbcbcbcb#d", -"#############b#d", -"#aaaaaaaaaa##c#d", -"#abcbcbcbcbbd##d", -".#abcbcbcbcbcd#d", -".#acbcbcbcbcbd#d", -"..#acbcbcbcbb#dd", -"..#############d", -"...ddddddddddddd"}; - -static const char* const maskResult_data[] = { -"16 13 6 1", -". c #ff0000", -"d c #000000", -"# c #999999", -"c c #cccccc", -"b c #ffff00", -"a c #ffffff", -"...#####........", -"..#aaaaa#.......", -".#abcbcba######.", -".#acbcbcaaaaaa#d", -".#abcbcbcbcbcb#d", -"#############b#d", -"#aaaaaaaaaa##c#d", -"#abcbcbcbcbbd##d", -".#abcbcbcbcbcd#d", -".#acbcbcbcbcbd#d", -"..#acbcbcbcbb#dd", -"..#############d", -"...ddddddddddddd"}; - - #ifndef QT_NO_WIDGETS void tst_QPainter::drawPixmap_comp_data() { @@ -3097,7 +3053,7 @@ void tst_QPainter::fpe_steepSlopes_data() qreal randf() { - return rand() / (RAND_MAX + 1.0); + return QRandomGenerator::global()->bounded(1.0); } QPointF randInRect(const QRectF &rect) @@ -3559,11 +3515,9 @@ void tst_QPainter::drawImage_data() continue; for (int odd_x = 0; odd_x <= 1; ++odd_x) { for (int odd_width = 0; odd_width <= 1; ++odd_width) { - QString description = - QString("srcFormat %1, dstFormat %2, odd x: %3, odd width: %4") - .arg(srcFormat).arg(dstFormat).arg(odd_x).arg(odd_width); - - QTest::newRow(qPrintable(description)) << (10 + odd_x) << 10 << (20 + odd_width) << 20 + QTest::addRow("srcFormat %d, dstFormat %d, odd x: %d, odd width: %d", + srcFormat, dstFormat, odd_x, odd_width) + << (10 + odd_x) << 10 << (20 + odd_width) << 20 << QImage::Format(srcFormat) << QImage::Format(dstFormat); } diff --git a/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp b/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp index 14ba9c5c84..93035af7d3 100644 --- a/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp +++ b/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp @@ -35,6 +35,7 @@ #include <qpolygon.h> #include <qdebug.h> #include <qpainter.h> +#include <qrandom.h> #include <math.h> @@ -423,8 +424,8 @@ void tst_QPathClipper::clip() static inline QPointF randomPointInRect(const QRectF &rect) { - qreal rx = qrand() / (RAND_MAX + 1.); - qreal ry = qrand() / (RAND_MAX + 1.); + qreal rx = QRandomGenerator::global()->bounded(1.0); + qreal ry = QRandomGenerator::global()->bounded(1.0); return QPointF(rect.left() + rx * rect.width(), rect.top() + ry * rect.height()); diff --git a/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp b/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp index 13b6e28f5f..bf3e5dfb52 100644 --- a/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp +++ b/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp @@ -49,6 +49,8 @@ private slots: void boundingRectF(); void makeEllipse(); void swap(); + void intersections_data(); + void intersections(); }; tst_QPolygon::tst_QPolygon() @@ -159,5 +161,45 @@ void tst_QPolygon::swap() QCOMPARE(p2.count(),3); } +void tst_QPolygon::intersections_data() +{ + QTest::addColumn<QPolygon>("poly1"); + QTest::addColumn<QPolygon>("poly2"); + QTest::addColumn<bool>("result"); + + QTest::newRow("empty intersects nothing") + << QPolygon() + << QPolygon(QVector<QPoint>() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << false; + QTest::newRow("identical triangles") + << QPolygon(QVector<QPoint>() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << QPolygon(QVector<QPoint>() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << true; + QTest::newRow("not intersecting") + << QPolygon(QVector<QPoint>() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << QPolygon(QVector<QPoint>() << QPoint(0,20) << QPoint(10,12) << QPoint(-10,12)) + << false; + QTest::newRow("clean intersection of squares") + << QPolygon(QVector<QPoint>() << QPoint(0,0) << QPoint(0,10) << QPoint(10,10) << QPoint(10,0)) + << QPolygon(QVector<QPoint>() << QPoint(5,5) << QPoint(5,15) << QPoint(15,15) << QPoint(15,5)) + << true; + QTest::newRow("clean contains of squares") + << QPolygon(QVector<QPoint>() << QPoint(0,0) << QPoint(0,10) << QPoint(10,10) << QPoint(10,0)) + << QPolygon(QVector<QPoint>() << QPoint(5,5) << QPoint(5,8) << QPoint(8,8) << QPoint(8,5)) + << true; +} + +void tst_QPolygon::intersections() +{ + QFETCH(QPolygon, poly1); + QFETCH(QPolygon, poly2); + QFETCH(bool, result); + + QCOMPARE(poly2.intersects(poly1), poly1.intersects(poly2)); + QCOMPARE(poly2.intersected(poly1).isEmpty(), poly1.intersected(poly2).isEmpty()); + QCOMPARE(!poly1.intersected(poly2).isEmpty(), poly1.intersects(poly2)); + QCOMPARE(poly1.intersects(poly2), result); +} + QTEST_APPLESS_MAIN(tst_QPolygon) #include "tst_qpolygon.moc" diff --git a/tests/auto/gui/painting/qtransform/tst_qtransform.cpp b/tests/auto/gui/painting/qtransform/tst_qtransform.cpp index 0c089cdaa3..0a6a95daca 100644 --- a/tests/auto/gui/painting/qtransform/tst_qtransform.cpp +++ b/tests/auto/gui/painting/qtransform/tst_qtransform.cpp @@ -29,7 +29,6 @@ #include <QtTest/QtTest> #include "qtransform.h" -#include <math.h> #include <qpolygon.h> #include <qdebug.h> @@ -67,10 +66,6 @@ private: Q_DECLARE_METATYPE(QTransform) -#if defined(Q_OS_WIN) && !defined(M_PI) -#define M_PI 3.14159265897932384626433832795f -#endif - void tst_QTransform::mapRect_data() { mapping_data(); diff --git a/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp b/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp index a79526c434..da88a868f3 100644 --- a/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp +++ b/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp @@ -114,131 +114,96 @@ void tst_QWMatrix::mapping_data() << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( -300, -400, 300, 400 ) ); -#if defined(Q_OS_WIN) && !defined(M_PI) -#define M_PI 3.14159265897932384626433832795f -#endif + const auto rotate = [](qreal degrees) { + const qreal rad = qDegreesToRadians(degrees); + return QMatrix(std::cos(rad), -std::sin(rad), + std::sin(rad), std::cos(rad), 0, 0); + }; // rotations - float deg = 0.; - QTest::newRow( "rot 0 a" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 0 a" ) << rotate(0.) << QRect( 0, 0, 30, 40 ) << QPolygon ( QRect( 0, 0, 30, 40 ) ); - deg = 0.00001f; - QTest::newRow( "rot 0 b" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 0 b" ) << rotate(0.00001f) << QRect( 0, 0, 30, 40 ) << QPolygon ( QRect( 0, 0, 30, 40 ) ); - deg = 0.; - QTest::newRow( "rot 0 c" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 0 c" ) << rotate(0.) << QRect( 10, 20, 30, 40 ) << QPolygon ( QRect( 10, 20, 30, 40 ) ); - deg = 0.00001f; - QTest::newRow( "rot 0 d" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 0 d" ) << rotate(0.00001f) << QRect( 10, 20, 30, 40 ) << QPolygon ( QRect( 10, 20, 30, 40 ) ); #if 0 - // rotations - deg = 90.; - QTest::newRow( "rotscale 90 a" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + const auto rotScale = [](qreal degrees, qreal scale) { + const qreal rad = qDegreesToRadians(degrees); + return QMatrix(scale * std::cos(rad), -scale * std::sin(rad), + scale * std::sin(rad), scale * std::cos(rad), 0, 0); + }; + // rotations with scaling + QTest::newRow( "rotscale 90 a" ) << rotScale(90., 10) << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( 0, -299, 400, 300 ) ); - deg = 90.00001; - QTest::newRow( "rotscale 90 b" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 90 b" ) << rotScale(90.00001, 10) << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( 0, -299, 400, 300 ) ); - deg = 90.; - QTest::newRow( "rotscale 90 c" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 90 c" ) << rotScale(90., 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( 200, -399, 400, 300 ) ); - deg = 90.00001; - QTest::newRow( "rotscale 90 d" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 90 d" ) << rotScale(90.00001, 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( 200, -399, 400, 300 ) ); - deg = 180.; - QTest::newRow( "rotscale 180 a" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 180 a" ) << rotScale(180., 10) << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( -299, -399, 300, 400 ) ); - deg = 180.000001; - QTest::newRow( "rotscale 180 b" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 180 b" ) << rotScale(180.000001, 10) << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( -299, -399, 300, 400 ) ); - deg = 180.; - QTest::newRow( "rotscale 180 c" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 180 c" ) << rotScale(180., 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( -399, -599, 300, 400 ) ); - deg = 180.000001; - QTest::newRow( "rotscale 180 d" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 180 d" ) << rotScale(180.000001, 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( -399, -599, 300, 400 ) ); - deg = 270.; - QTest::newRow( "rotscale 270 a" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 270 a" ) << rotScale(270., 10) << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( -399, 00, 400, 300 ) ); - deg = 270.0000001; - QTest::newRow( "rotscale 270 b" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 270 b" ) << rotScale(270.0000001, 10) << QRect( 0, 0, 30, 40 ) << QPolygon( QRect( -399, 00, 400, 300 ) ); - deg = 270.; - QTest::newRow( "rotscale 270 c" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 270 c" ) << rotScale(270., 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( -599, 100, 400, 300 ) ); - deg = 270.000001; - QTest::newRow( "rotscale 270 d" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rotscale 270 d" ) << rotScale(270.000001, 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( -599, 100, 400, 300 ) ); // rotations that are not multiples of 90 degrees. mapRect returns the bounding rect here. - deg = 45; - QTest::newRow( "rot 45 a" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 45 a" ) << rotate(45) << QRect( 0, 0, 10, 10 ) << QPolygon( QRect( 0, -7, 14, 14 ) ); - QTest::newRow( "rot 45 b" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 45 b" ) << rotate(45) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( 21, -14, 49, 49 ) ); - QTest::newRow( "rot 45 c" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 45 c" ) << rotScale(45, 10) << QRect( 0, 0, 10, 10 ) << QPolygon( QRect( 0, -70, 141, 141 ) ); - QTest::newRow( "rot 45 d" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot 45 d" ) << rotScale(45, 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( 212, -141, 495, 495 ) ); - deg = -45; - QTest::newRow( "rot -45 a" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot -45 a" ) << rotate(-45) << QRect( 0, 0, 10, 10 ) << QPolygon( QRect( -7, 0, 14, 14 ) ); - QTest::newRow( "rot -45 b" ) << QMatrix( std::cos( M_PI*deg/180. ), -std::sin( M_PI*deg/180. ), - std::sin( M_PI*deg/180. ), std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot -45 b" ) << rotate(-45) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( -35, 21, 49, 49 ) ); - QTest::newRow( "rot -45 c" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot -45 c" ) << rotScale(-45, 10) << QRect( 0, 0, 10, 10 ) << QPolygon( QRect( -70, 0, 141, 141 ) ); - QTest::newRow( "rot -45 d" ) << QMatrix( 10*std::cos( M_PI*deg/180. ), -10*std::sin( M_PI*deg/180. ), - 10*std::sin( M_PI*deg/180. ), 10*std::cos( M_PI*deg/180. ), 0, 0 ) + QTest::newRow( "rot -45 d" ) << rotScale(-45, 10) << QRect( 10, 20, 30, 40 ) << QPolygon( QRect( -353, 212, 495, 495 ) ); #endif diff --git a/tests/auto/gui/qopengl/tst_qopengl.cpp b/tests/auto/gui/qopengl/tst_qopengl.cpp index 6d9456fa69..82b3657b56 100644 --- a/tests/auto/gui/qopengl/tst_qopengl.cpp +++ b/tests/auto/gui/qopengl/tst_qopengl.cpp @@ -1413,7 +1413,7 @@ void tst_QOpenGL::glxContextWrap() window->setSurfaceType(QWindow::OpenGLSurface); window->setGeometry(0, 0, 10, 10); window->show(); - QTest::qWaitForWindowExposed(window); + QVERIFY(QTest::qWaitForWindowExposed(window)); QPlatformNativeInterface *nativeIf = QGuiApplicationPrivate::instance()->platformIntegration()->nativeInterface(); QVERIFY(nativeIf); @@ -1457,7 +1457,7 @@ void tst_QOpenGL::wglContextWrap() window->setSurfaceType(QWindow::OpenGLSurface); window->setGeometry(0, 0, 256, 256); window->show(); - QTest::qWaitForWindowExposed(window.data()); + QVERIFY(QTest::qWaitForWindowExposed(window.data())); QVariant v = ctx->nativeHandle(); QVERIFY(!v.isNull()); diff --git a/tests/auto/gui/qvulkan/qvulkan.pro b/tests/auto/gui/qvulkan/qvulkan.pro new file mode 100644 index 0000000000..0db990a2d6 --- /dev/null +++ b/tests/auto/gui/qvulkan/qvulkan.pro @@ -0,0 +1,9 @@ +############################################################ +# Project file for autotest for gui/vulkan functionality +############################################################ + +CONFIG += testcase +TARGET = tst_qvulkan +QT += gui-private core-private testlib + +SOURCES += tst_qvulkan.cpp diff --git a/tests/auto/gui/qvulkan/tst_qvulkan.cpp b/tests/auto/gui/qvulkan/tst_qvulkan.cpp new file mode 100644 index 0000000000..cc4bc43f92 --- /dev/null +++ b/tests/auto/gui/qvulkan/tst_qvulkan.cpp @@ -0,0 +1,435 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QVulkanInstance> +#include <QtGui/QVulkanFunctions> +#include <QtGui/QVulkanWindow> + +#include <QtTest/QtTest> + +#include <QSignalSpy> + +class tst_QVulkan : public QObject +{ + Q_OBJECT + +private slots: + void vulkanInstance(); + void vulkanCheckSupported(); + void vulkanPlainWindow(); + void vulkanVersionRequest(); + void vulkanWindow(); + void vulkanWindowRenderer(); + void vulkanWindowGrab(); +}; + +void tst_QVulkan::vulkanInstance() +{ + QVulkanInstance inst; + if (!inst.create()) + QSKIP("Vulkan init failed; skip"); + + QVERIFY(inst.isValid()); + QVERIFY(inst.vkInstance() != VK_NULL_HANDLE); + QVERIFY(inst.functions()); + QVERIFY(!inst.flags().testFlag(QVulkanInstance::NoDebugOutputRedirect)); + + inst.destroy(); + + QVERIFY(!inst.isValid()); + QVERIFY(inst.handle() == nullptr); + + inst.setFlags(QVulkanInstance::NoDebugOutputRedirect); + // pass a bogus layer and extension + inst.setExtensions(QByteArrayList() << "abcdefg" << "notanextension"); + inst.setLayers(QByteArrayList() << "notalayer"); + QVERIFY(inst.create()); + + QVERIFY(inst.isValid()); + QVERIFY(inst.vkInstance() != VK_NULL_HANDLE); + QVERIFY(inst.handle() != nullptr); + QVERIFY(inst.functions()); + QVERIFY(inst.flags().testFlag(QVulkanInstance::NoDebugOutputRedirect)); + QVERIFY(!inst.extensions().contains("abcdefg")); + QVERIFY(!inst.extensions().contains("notanextension")); + QVERIFY(!inst.extensions().contains("notalayer")); + // at least the surface extensions should be there however + QVERIFY(inst.extensions().contains("VK_KHR_surface")); + + QVERIFY(inst.getInstanceProcAddr("vkGetDeviceQueue")); +} + +void tst_QVulkan::vulkanCheckSupported() +{ + // Test the early calls to supportedLayers/extensions that need the library + // and some basics, but do not initialize the instance. + QVulkanInstance inst; + QVERIFY(!inst.isValid()); + + QVulkanInfoVector<QVulkanLayer> vl = inst.supportedLayers(); + qDebug() << vl; + QVERIFY(!inst.isValid()); + + QVulkanInfoVector<QVulkanExtension> ve = inst.supportedExtensions(); + qDebug() << ve; + QVERIFY(!inst.isValid()); + + if (inst.create()) { // skip the rest when Vulkan is not supported at all + QVERIFY(!ve.isEmpty()); + QVERIFY(ve == inst.supportedExtensions()); + } +} + +void tst_QVulkan::vulkanPlainWindow() +{ + QVulkanInstance inst; + if (!inst.create()) + QSKIP("Vulkan init failed; skip"); + + QWindow w; + w.setSurfaceType(QSurface::VulkanSurface); + w.setVulkanInstance(&inst); + w.resize(1024, 768); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + + QCOMPARE(w.vulkanInstance(), &inst); + + VkSurfaceKHR surface = QVulkanInstance::surfaceForWindow(&w); + QVERIFY(surface != VK_NULL_HANDLE); + + // exercise supportsPresent (and QVulkanFunctions) a bit + QVulkanFunctions *f = inst.functions(); + VkPhysicalDevice physDev; + uint32_t count = 1; + VkResult err = f->vkEnumeratePhysicalDevices(inst.vkInstance(), &count, &physDev); + if (err != VK_SUCCESS) + QSKIP("No physical devices; skip"); + + VkPhysicalDeviceProperties physDevProps; + f->vkGetPhysicalDeviceProperties(physDev, &physDevProps); + qDebug("Device name: %s Driver version: %d.%d.%d", physDevProps.deviceName, + VK_VERSION_MAJOR(physDevProps.driverVersion), VK_VERSION_MINOR(physDevProps.driverVersion), + VK_VERSION_PATCH(physDevProps.driverVersion)); + + bool supports = inst.supportsPresent(physDev, 0, &w); + qDebug("queue family 0 supports presenting to window = %d", supports); +} + +void tst_QVulkan::vulkanVersionRequest() +{ + QVulkanInstance inst; + if (!inst.create()) + QSKIP("Vulkan init failed; skip"); + + // Now that we know Vulkan is functional, check the requested apiVersion is + // passed to vkCreateInstance as expected. + + inst.destroy(); + + inst.setApiVersion(QVersionNumber(10, 0, 0)); + QVERIFY(!inst.create()); + QCOMPARE(inst.errorCode(), VK_ERROR_INCOMPATIBLE_DRIVER); +} + +static void waitForUnexposed(QWindow *w) +{ + QElapsedTimer timer; + timer.start(); + while (w->isExposed()) { + int remaining = 5000 - int(timer.elapsed()); + if (remaining <= 0) + break; + QCoreApplication::processEvents(QEventLoop::AllEvents, remaining); + QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete); + QTest::qSleep(10); + } +} + +void tst_QVulkan::vulkanWindow() +{ + QVulkanInstance inst; + if (!inst.create()) + QSKIP("Vulkan init failed; skip"); + + // First let's forget to set the instance. + QVulkanWindow w; + QVERIFY(!w.isValid()); + w.resize(1024, 768); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + QVERIFY(!w.isValid()); + + // Now set it. A simple hide - show should be enough to correct, this, no + // need for a full destroy - create. + w.hide(); + waitForUnexposed(&w); + w.setVulkanInstance(&inst); + QVector<VkPhysicalDeviceProperties> pdevs = w.availablePhysicalDevices(); + if (pdevs.isEmpty()) + QSKIP("No Vulkan physical devices; skip"); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + QVERIFY(w.isValid()); + QCOMPARE(w.vulkanInstance(), &inst); + QVulkanInfoVector<QVulkanExtension> exts = w.supportedDeviceExtensions(); + + // Now destroy and recreate. + w.destroy(); + waitForUnexposed(&w); + QVERIFY(!w.isValid()); + // check that flags can be set between a destroy() - show() + w.setFlags(QVulkanWindow::PersistentResources); + // supported lists can be queried before expose too + QVERIFY(w.supportedDeviceExtensions() == exts); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + QVERIFY(w.isValid()); + QVERIFY(w.flags().testFlag(QVulkanWindow::PersistentResources)); + + QVERIFY(w.physicalDevice() != VK_NULL_HANDLE); + QVERIFY(w.physicalDeviceProperties() != nullptr); + QVERIFY(w.device() != VK_NULL_HANDLE); + QVERIFY(w.graphicsQueue() != VK_NULL_HANDLE); + QVERIFY(w.graphicsCommandPool() != VK_NULL_HANDLE); + QVERIFY(w.defaultRenderPass() != VK_NULL_HANDLE); + + QVERIFY(w.concurrentFrameCount() > 0); + QVERIFY(w.concurrentFrameCount() <= QVulkanWindow::MAX_CONCURRENT_FRAME_COUNT); +} + +class TestVulkanRenderer; + +class TestVulkanWindow : public QVulkanWindow +{ +public: + QVulkanWindowRenderer *createRenderer() override; + +private: + TestVulkanRenderer *m_renderer = nullptr; +}; + +struct TestVulkan { + int preInitResCount = 0; + int initResCount = 0; + int initSwcResCount = 0; + int releaseResCount = 0; + int releaseSwcResCount = 0; + int startNextFrameCount = 0; +} testVulkan; + +class TestVulkanRenderer : public QVulkanWindowRenderer +{ +public: + TestVulkanRenderer(QVulkanWindow *w) : m_window(w) { } + + void preInitResources() override; + void initResources() override; + void initSwapChainResources() override; + void releaseSwapChainResources() override; + void releaseResources() override; + + void startNextFrame() override; + +private: + QVulkanWindow *m_window; + QVulkanDeviceFunctions *m_devFuncs; +}; + +void TestVulkanRenderer::preInitResources() +{ + if (testVulkan.initResCount) { + qWarning("initResources called before preInitResources?!"); + testVulkan.preInitResCount = -1; + return; + } + + // Ensure the physical device and the surface are available at this stage. + VkPhysicalDevice physDev = m_window->physicalDevice(); + if (physDev == VK_NULL_HANDLE) { + qWarning("No physical device in preInitResources"); + testVulkan.preInitResCount = -1; + return; + } + VkSurfaceKHR surface = m_window->vulkanInstance()->surfaceForWindow(m_window); + if (surface == VK_NULL_HANDLE) { + qWarning("No surface in preInitResources"); + testVulkan.preInitResCount = -1; + return; + } + + ++testVulkan.preInitResCount; +} + +void TestVulkanRenderer::initResources() +{ + m_devFuncs = m_window->vulkanInstance()->deviceFunctions(m_window->device()); + ++testVulkan.initResCount; +} + +void TestVulkanRenderer::initSwapChainResources() +{ + ++testVulkan.initSwcResCount; +} + +void TestVulkanRenderer::releaseSwapChainResources() +{ + ++testVulkan.releaseSwcResCount; +} + +void TestVulkanRenderer::releaseResources() +{ + ++testVulkan.releaseResCount; +} + +void TestVulkanRenderer::startNextFrame() +{ + ++testVulkan.startNextFrameCount; + + VkClearColorValue clearColor = { 0, 1, 0, 1 }; + VkClearDepthStencilValue clearDS = { 1, 0 }; + VkClearValue clearValues[2]; + memset(clearValues, 0, sizeof(clearValues)); + clearValues[0].color = clearColor; + clearValues[1].depthStencil = clearDS; + + VkRenderPassBeginInfo rpBeginInfo; + memset(&rpBeginInfo, 0, sizeof(rpBeginInfo)); + rpBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; + rpBeginInfo.renderPass = m_window->defaultRenderPass(); + rpBeginInfo.framebuffer = m_window->currentFramebuffer(); + const QSize sz = m_window->swapChainImageSize(); + rpBeginInfo.renderArea.extent.width = sz.width(); + rpBeginInfo.renderArea.extent.height = sz.height(); + rpBeginInfo.clearValueCount = 2; + rpBeginInfo.pClearValues = clearValues; + VkCommandBuffer cmdBuf = m_window->currentCommandBuffer(); + m_devFuncs->vkCmdBeginRenderPass(cmdBuf, &rpBeginInfo, VK_SUBPASS_CONTENTS_INLINE); + + m_devFuncs->vkCmdEndRenderPass(cmdBuf); + + m_window->frameReady(); +} + +QVulkanWindowRenderer *TestVulkanWindow::createRenderer() +{ + Q_ASSERT(!m_renderer); + m_renderer = new TestVulkanRenderer(this); + return m_renderer; +} + +void tst_QVulkan::vulkanWindowRenderer() +{ + QVulkanInstance inst; + if (!inst.create()) + QSKIP("Vulkan init failed; skip"); + + testVulkan = TestVulkan(); + + TestVulkanWindow w; + w.setVulkanInstance(&inst); + w.resize(1024, 768); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + + if (w.availablePhysicalDevices().isEmpty()) + QSKIP("No Vulkan physical devices; skip"); + + QVERIFY(testVulkan.preInitResCount == 1); + QVERIFY(testVulkan.initResCount == 1); + QVERIFY(testVulkan.initSwcResCount == 1); + // this has to be QTRY due to the async update in QVulkanWindowPrivate::ensureStarted() + QTRY_VERIFY(testVulkan.startNextFrameCount >= 1); + + QVERIFY(!w.swapChainImageSize().isEmpty()); + QVERIFY(w.colorFormat() != VK_FORMAT_UNDEFINED); + QVERIFY(w.depthStencilFormat() != VK_FORMAT_UNDEFINED); + + w.destroy(); + waitForUnexposed(&w); + QVERIFY(testVulkan.releaseSwcResCount == 1); + QVERIFY(testVulkan.releaseResCount == 1); +} + +void tst_QVulkan::vulkanWindowGrab() +{ + QVulkanInstance inst; + inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation"); + if (!inst.create()) + QSKIP("Vulkan init failed; skip"); + + testVulkan = TestVulkan(); + + TestVulkanWindow w; + w.setVulkanInstance(&inst); + w.resize(1024, 768); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + + if (w.availablePhysicalDevices().isEmpty()) + QSKIP("No Vulkan physical devices; skip"); + + if (!w.supportsGrab()) + QSKIP("No grab support; skip"); + + QVERIFY(!w.swapChainImageSize().isEmpty()); + + QImage img1 = w.grab(); + QImage img2 = w.grab(); + QImage img3 = w.grab(); + + QVERIFY(!img1.isNull()); + QVERIFY(!img2.isNull()); + QVERIFY(!img3.isNull()); + + QCOMPARE(img1.size(), w.swapChainImageSize()); + QCOMPARE(img2.size(), w.swapChainImageSize()); + QCOMPARE(img3.size(), w.swapChainImageSize()); + + QRgb a = img1.pixel(10, 20); + QRgb b = img2.pixel(5, 5); + QRgb c = img3.pixel(50, 30); + + QCOMPARE(a, b); + QCOMPARE(b, c); + QRgb refPixel = qRgb(0, 255, 0); + + int redFuzz = qAbs(qRed(a) - qRed(refPixel)); + int greenFuzz = qAbs(qGreen(a) - qGreen(refPixel)); + int blueFuzz = qAbs(qBlue(a) - qBlue(refPixel)); + + QVERIFY(redFuzz <= 1); + QVERIFY(blueFuzz <= 1); + QVERIFY(greenFuzz <= 1); + + w.destroy(); +} + +QTEST_MAIN(tst_QVulkan) + +#include "tst_qvulkan.moc" diff --git a/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp b/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp index 8667caa1ef..0a422fca17 100644 --- a/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp +++ b/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp @@ -47,7 +47,11 @@ private slots: void elidedText(); void veryNarrowElidedText(); void averageCharWidth(); + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) void bypassShaping(); +#endif + void elidedMultiLength(); void elidedMultiLengthF(); void inFontUcs4(); @@ -187,6 +191,7 @@ void tst_QFontMetrics::averageCharWidth() QVERIFY(fmf.averageCharWidth() != 0); } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) void tst_QFontMetrics::bypassShaping() { QFont f; @@ -201,6 +206,7 @@ void tst_QFontMetrics::bypassShaping() // This assertion is needed in Qt WebKit's WebCore::Font::offsetForPositionForSimpleText QCOMPARE(textWidth, charsWidth); } +#endif template<class FontMetrics, typename PrimitiveType> void elidedMultiLength_helper() { diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 764b99646b..2f3da2c196 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -187,6 +187,7 @@ private slots: void cssInheritance(); void lineHeightType(); + void cssLineHeightMultiplier(); private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); void buildRegExpData(); @@ -3398,6 +3399,33 @@ void tst_QTextDocument::lineHeightType() { QTextDocument td; + td.setHtml("<html><head><style type=\"text/css\">body { -qt-line-height-type: fixed; line-height: 10; -qt-line-height-type: fixed; }</style></head><body>Foobar</body></html>"); + QTextBlock block = td.begin(); + QTextBlockFormat format = block.blockFormat(); + QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::FixedHeight)); + QCOMPARE(format.lineHeight(), 10.0); + } + + { + QTextDocument td; + td.setHtml("<html><head><style type=\"text/css\">body { -qt-line-height-type: proportional; line-height: 3; }</style></head><body>Foobar</body></html>"); + QTextBlock block = td.begin(); + QTextBlockFormat format = block.blockFormat(); + QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight)); + QCOMPARE(format.lineHeight(), 3.0); + } + + { + QTextDocument td; + td.setHtml("<html><head><style type=\"text/css\">body { line-height: 2.5; -qt-line-height-type: proportional; }</style></head><body>Foobar</body></html>"); + QTextBlock block = td.begin(); + QTextBlockFormat format = block.blockFormat(); + QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight)); + QCOMPARE(format.lineHeight(), 2.5); + } + + { + QTextDocument td; td.setHtml("<html><head><style type=\"text/css\">body { line-height: 33; -qt-line-height-type: minimum; }</style></head><body>Foobar</body></html>"); QTextBlock block = td.begin(); QTextBlockFormat format = block.blockFormat(); @@ -3424,5 +3452,26 @@ void tst_QTextDocument::lineHeightType() } } +void tst_QTextDocument::cssLineHeightMultiplier() +{ + { + QTextDocument td; + td.setHtml("<html><head><style type=\"text/css\">body { line-height: 10; }</style></head><body>Foobar</body></html>"); + QTextBlock block = td.begin(); + QTextBlockFormat format = block.blockFormat(); + QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight)); + QCOMPARE(format.lineHeight(), 1000.0); + } + + { + QTextDocument td; + td.setHtml("<html><head><style type=\"text/css\">body {line-height: 1.38; }</style></head><body>Foobar</body></html>"); + QTextBlock block = td.begin(); + QTextBlockFormat format = block.blockFormat(); + QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight)); + QCOMPARE(format.lineHeight(), 138.0); + } +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc" diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp index b8af5271ea..b68a014bff 100644 --- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp +++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp @@ -1312,7 +1312,7 @@ void tst_QTextLayout::testDefaultTabs() QCOMPARE(line.cursorToX(31), 480.); QTextOption option = layout.textOption(); - option.setTabStop(90); + option.setTabStopDistance(90); layout.setTextOption(option); layout.beginLayout(); line = layout.createLine(); @@ -1351,7 +1351,7 @@ void tst_QTextLayout::testTabs() layout.setCacheEnabled(true); QTextOption option = layout.textOption(); - option.setTabStop(150); + option.setTabStopDistance(150); layout.setTextOption(option); layout.beginLayout(); diff --git a/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp b/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp index ca01e83dbe..39f5e9ecc3 100644 --- a/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp +++ b/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp @@ -145,7 +145,7 @@ void tst_QTextPieceTable::insertion3() { QString compare; for (int i = 0; i < 20000; ++i) { - int pos = rand() % (i+1); + int pos = QRandomGenerator::global()->bounded(i+1); QChar c((unsigned short)(i & 0xff) + 1); QString str; str += c; @@ -159,7 +159,7 @@ void tst_QTextPieceTable::insertion4() { QString compare; for (int i = 0; i < 20000; ++i) { - int pos = rand() % (i+1); + int pos = QRandomGenerator::global()->generate() % (i+1); QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; @@ -178,7 +178,7 @@ void tst_QTextPieceTable::insertion5() { QString compare; for (int i = 0; i < 20000; ++i) { - int pos = rand() % (i+1); + int pos = QRandomGenerator::global()->generate() % (i+1); QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; @@ -236,8 +236,8 @@ void tst_QTextPieceTable::removal3() QString compare; int l = 0; for (int i = 0; i < 20000; ++i) { - bool remove = l && (rand() % 2); - int pos = rand() % (remove ? l : (l+1)); + bool remove = l && (QRandomGenerator::global()->bounded(2)); + int pos = QRandomGenerator::global()->bounded(remove ? l : (l+1)); QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; @@ -263,8 +263,8 @@ void tst_QTextPieceTable::removal4() QString compare; int l = 0; for (int i = 0; i < 20000; ++i) { - bool remove = l && (rand() % 2); - int pos = (l > 1) ? rand() % (remove ? l-1 : l) : 0; + bool remove = l && (QRandomGenerator::global()->bounded(2)); + int pos = (l > 1) ? QRandomGenerator::global()->bounded(remove ? l-1 : l) : 0; QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; if (c != 'a') { @@ -472,13 +472,12 @@ void tst_QTextPieceTable::undoRedo10() void tst_QTextPieceTable::undoRedo11() { - srand(3); const int loops = 20; QString compare; int l = 0; for (int i = 0; i < loops; ++i) { - bool remove = l && (rand() % 2); - int pos = (l > 1) ? rand() % (remove ? l-1 : l) : 0; + bool remove = l && (QRandomGenerator::global()->bounded(2)); + int pos = (l > 1) ? QRandomGenerator::global()->bounded(remove ? l-1 : l) : 0; QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; diff --git a/tests/auto/gui/text/qtextscriptengine/BLACKLIST b/tests/auto/gui/text/qtextscriptengine/BLACKLIST new file mode 100644 index 0000000000..52eb9086a9 --- /dev/null +++ b/tests/auto/gui/text/qtextscriptengine/BLACKLIST @@ -0,0 +1,2 @@ +[thaiWithZWJ] +rhel-7.2 diff --git a/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp b/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp index ee50b98733..0371f51961 100644 --- a/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp +++ b/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp @@ -78,6 +78,9 @@ private slots: void thaiIsolatedSaraAm(); void thaiWithZWJ(); void thaiMultipleVowels(); + + void shapingDisabledDevanagari(); + void shapingDisabledLatin(); private: bool haveTestFonts; }; @@ -1280,5 +1283,62 @@ void tst_QTextScriptEngine::thaiMultipleVowels() // If we haven't crashed at this point, then the test has passed. } +void tst_QTextScriptEngine::shapingDisabledLatin() +{ + QString s("fi"); + + QFont font("Calibri"); + font.setStyleStrategy(QFont::PreferNoShaping); + + QTextLayout layout(s); + layout.setFont(font); + layout.beginLayout(); + layout.createLine(); + layout.endLayout(); + + QList<QGlyphRun> runs = layout.glyphRuns(); + + QCOMPARE(runs.size(), 1); + QCOMPARE(runs.first().glyphIndexes().size(), 2); +} + +void tst_QTextScriptEngine::shapingDisabledDevanagari() +{ + QString s; + s += QChar(0x0915); // KA + s += QChar(0x094D); // VIRAMA + s += QChar(0x0915); // KA + + + QList<QGlyphRun> normalRuns; + { + QTextLayout layout(s); + layout.beginLayout(); + layout.createLine(); + layout.endLayout(); + + normalRuns = layout.glyphRuns(); + } + + QFont font; + font.setStyleStrategy(QFont::PreferNoShaping); + + QList<QGlyphRun> noShapingRuns; + { + QTextLayout layout(s); + layout.setFont(font); + layout.beginLayout(); + layout.createLine(); + layout.endLayout(); + + noShapingRuns = layout.glyphRuns(); + } + + // Even though shaping is disabled, Devanagari requires it, so the flag should be ignored. + QCOMPARE(normalRuns.size(), 1); + QCOMPARE(noShapingRuns.size(), 1); + QCOMPARE(noShapingRuns.first().glyphIndexes().size(), normalRuns.first().glyphIndexes().size()); +} + QTEST_MAIN(tst_QTextScriptEngine) #include "tst_qtextscriptengine.moc" diff --git a/tests/auto/gui/util/qshadergenerator/qshadergenerator.pro b/tests/auto/gui/util/qshadergenerator/qshadergenerator.pro new file mode 100644 index 0000000000..c1f610e029 --- /dev/null +++ b/tests/auto/gui/util/qshadergenerator/qshadergenerator.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadergenerator.cpp +TARGET = tst_qshadergenerator diff --git a/tests/auto/gui/util/qshadergenerator/tst_qshadergenerator.cpp b/tests/auto/gui/util/qshadergenerator/tst_qshadergenerator.cpp new file mode 100644 index 0000000000..d0a0225055 --- /dev/null +++ b/tests/auto/gui/util/qshadergenerator/tst_qshadergenerator.cpp @@ -0,0 +1,895 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtCore/qmetaobject.h> +#include <QtGui/private/qshadergenerator_p.h> +#include <QtGui/private/qshaderlanguage_p.h> + +namespace +{ + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports, const QStringList &layers = QStringList()) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + node.setLayers(layers); + for (const auto &port : ports) + node.addPort(port); + return node; + } + + QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, + const QUuid &targetUuid, const QString &targetName, + const QStringList &layers = QStringList()) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + edge.layers = layers; + return edge; + } + + QShaderGraph createGraph() + { + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + auto graph = QShaderGraph(); + + auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + worldPosition.setParameter("name", "worldPosition"); + worldPosition.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;", + QByteArrayList() << "varying highp vec3 $name;")); + worldPosition.addRule(openGL3, QShaderNode::Rule("vec3 $value = $name;", + QByteArrayList() << "in vec3 $name;")); + + auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", + QByteArrayList() << "varying highp vec2 texCoord;")); + texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + + auto lightIntensity = createNode({ + createPort(QShaderNodePort::Output, "lightIntensity") + }); + lightIntensity.addRule(openGLES2, QShaderNode::Rule("highp float $lightIntensity = lightIntensity;", + QByteArrayList() << "uniform highp float lightIntensity;")); + lightIntensity.addRule(openGL3, QShaderNode::Rule("float $lightIntensity = lightIntensity;", + QByteArrayList() << "uniform float lightIntensity;")); + + auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", + QByteArrayList() << "uniform highp float exposure;")); + exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", + QByteArrayList() << "uniform float exposure;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); + sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); + + auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + + auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + + graph.addNode(worldPosition); + graph.addNode(texture); + graph.addNode(texCoord); + graph.addNode(lightIntensity); + graph.addNode(exposure); + graph.addNode(fragColor); + graph.addNode(sampleTexture); + graph.addNode(lightFunction); + graph.addNode(exposureFunction); + + graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); + graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); + + graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position")); + graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); + graph.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity")); + + graph.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); + graph.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); + + graph.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); + + return graph; + } +} + +class tst_QShaderGenerator : public QObject +{ + Q_OBJECT +private slots: + void shouldHaveDefaultState(); + void shouldGenerateShaderCode_data(); + void shouldGenerateShaderCode(); + void shouldGenerateVersionCommands_data(); + void shouldGenerateVersionCommands(); + void shouldProcessLanguageQualifierAndTypeEnums_data(); + void shouldProcessLanguageQualifierAndTypeEnums(); + void shouldGenerateDifferentCodeDependingOnActiveLayers(); +}; + +void tst_QShaderGenerator::shouldHaveDefaultState() +{ + // GIVEN + auto generator = QShaderGenerator(); + + // THEN + QVERIFY(generator.graph.nodes().isEmpty()); + QVERIFY(generator.graph.edges().isEmpty()); + QVERIFY(!generator.format.isValid()); +} + +void tst_QShaderGenerator::shouldGenerateShaderCode_data() +{ + QTest::addColumn<QShaderGraph>("graph"); + QTest::addColumn<QShaderFormat>("format"); + QTest::addColumn<QByteArray>("expectedCode"); + + const auto graph = createGraph(); + + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + const auto openGL32 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2); + const auto openGL4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + const auto versionGLES2 = QByteArrayList() << "#version 100" << ""; + const auto versionGL3 = QByteArrayList() << "#version 130" << ""; + const auto versionGL32 = QByteArrayList() << "#version 150 core" << ""; + const auto versionGL4 = QByteArrayList() << "#version 400 core" << ""; + + const auto es2Code = QByteArrayList() << "varying highp vec3 worldPosition;" + << "uniform sampler2D texture;" + << "varying highp vec2 texCoord;" + << "uniform highp float lightIntensity;" + << "uniform highp float exposure;" + << "#pragma include es2/lightmodel.frag.inc" + << "" + << "void main()" + << "{" + << " highp vec2 v2 = texCoord;" + << " sampler2D v1 = texture;" + << " highp float v3 = lightIntensity;" + << " highp vec4 v5 = texture2D(v1, v2);" + << " highp vec3 v0 = worldPosition;" + << " highp float v4 = exposure;" + << " highp vec4 v6 = lightModel(v5, v0, v3);" + << " highp vec4 v7 = v6 * pow(2.0, v4);" + << " gl_fragColor = v7;" + << "}" + << ""; + + const auto gl3Code = QByteArrayList() << "in vec3 worldPosition;" + << "uniform sampler2D texture;" + << "in vec2 texCoord;" + << "uniform float lightIntensity;" + << "uniform float exposure;" + << "out vec4 fragColor;" + << "#pragma include gl3/lightmodel.frag.inc" + << "" + << "void main()" + << "{" + << " vec2 v2 = texCoord;" + << " sampler2D v1 = texture;" + << " float v3 = lightIntensity;" + << " vec4 v5 = texture2D(v1, v2);" + << " vec3 v0 = worldPosition;" + << " float v4 = exposure;" + << " vec4 v6 = lightModel(v5, v0, v3);" + << " vec4 v7 = v6 * pow(2.0, v4);" + << " fragColor = v7;" + << "}" + << ""; + + QTest::newRow("EmptyGraphAndFormat") << QShaderGraph() << QShaderFormat() << QByteArrayLiteral("\nvoid main()\n{\n}\n"); + QTest::newRow("LightExposureGraphAndES2") << graph << openGLES2 << (versionGLES2 + es2Code).join('\n'); + QTest::newRow("LightExposureGraphAndGL3") << graph << openGL3 << (versionGL3 + gl3Code).join('\n'); + QTest::newRow("LightExposureGraphAndGL32") << graph << openGL32 << (versionGL32 + gl3Code).join('\n'); + QTest::newRow("LightExposureGraphAndGL4") << graph << openGL4 << (versionGL4 + gl3Code).join('\n'); +} + +void tst_QShaderGenerator::shouldGenerateShaderCode() +{ + // GIVEN + QFETCH(QShaderGraph, graph); + QFETCH(QShaderFormat, format); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = format; + + // WHEN + const auto code = generator.createShaderCode(); + + // THEN + QFETCH(QByteArray, expectedCode); + QCOMPARE(code, expectedCode); +} + +void tst_QShaderGenerator::shouldGenerateVersionCommands_data() +{ + QTest::addColumn<QShaderFormat>("format"); + QTest::addColumn<QByteArray>("version"); + + QTest::newRow("GLES2") << createFormat(QShaderFormat::OpenGLES, 2, 0) << QByteArrayLiteral("#version 100"); + QTest::newRow("GLES3") << createFormat(QShaderFormat::OpenGLES, 3, 0) << QByteArrayLiteral("#version 300 es"); + + QTest::newRow("GL20") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) << QByteArrayLiteral("#version 110"); + QTest::newRow("GL21") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 1) << QByteArrayLiteral("#version 120"); + QTest::newRow("GL30") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 0) << QByteArrayLiteral("#version 130"); + QTest::newRow("GL31") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 1) << QByteArrayLiteral("#version 140"); + QTest::newRow("GL32") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 2) << QByteArrayLiteral("#version 150"); + QTest::newRow("GL33") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 3) << QByteArrayLiteral("#version 330"); + QTest::newRow("GL40") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 0) << QByteArrayLiteral("#version 400"); + QTest::newRow("GL41") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 1) << QByteArrayLiteral("#version 410"); + QTest::newRow("GL42") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 2) << QByteArrayLiteral("#version 420"); + QTest::newRow("GL43") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 3) << QByteArrayLiteral("#version 430"); + + QTest::newRow("GL20core") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) << QByteArrayLiteral("#version 110"); + QTest::newRow("GL21core") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 1) << QByteArrayLiteral("#version 120"); + QTest::newRow("GL30core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) << QByteArrayLiteral("#version 130"); + QTest::newRow("GL31core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) << QByteArrayLiteral("#version 140"); + QTest::newRow("GL32core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2) << QByteArrayLiteral("#version 150 core"); + QTest::newRow("GL33core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 3) << QByteArrayLiteral("#version 330 core"); + QTest::newRow("GL40core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0) << QByteArrayLiteral("#version 400 core"); + QTest::newRow("GL41core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 1) << QByteArrayLiteral("#version 410 core"); + QTest::newRow("GL42core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 2) << QByteArrayLiteral("#version 420 core"); + QTest::newRow("GL43core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 3) << QByteArrayLiteral("#version 430 core"); + + QTest::newRow("GL20compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) << QByteArrayLiteral("#version 110"); + QTest::newRow("GL21compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 1) << QByteArrayLiteral("#version 120"); + QTest::newRow("GL30compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 0) << QByteArrayLiteral("#version 130"); + QTest::newRow("GL31compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 1) << QByteArrayLiteral("#version 140"); + QTest::newRow("GL32compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 2) << QByteArrayLiteral("#version 150 compatibility"); + QTest::newRow("GL33compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 3) << QByteArrayLiteral("#version 330 compatibility"); + QTest::newRow("GL40compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 0) << QByteArrayLiteral("#version 400 compatibility"); + QTest::newRow("GL41compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 1) << QByteArrayLiteral("#version 410 compatibility"); + QTest::newRow("GL42compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 2) << QByteArrayLiteral("#version 420 compatibility"); + QTest::newRow("GL43compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 3) << QByteArrayLiteral("#version 430 compatibility"); +} + +void tst_QShaderGenerator::shouldGenerateVersionCommands() +{ + // GIVEN + QFETCH(QShaderFormat, format); + + auto generator = QShaderGenerator(); + generator.format = format; + + // WHEN + const auto code = generator.createShaderCode(); + + // THEN + QFETCH(QByteArray, version); + const auto expectedCode = (QByteArrayList() << version + << "" + << "" + << "void main()" + << "{" + << "}" + << "").join('\n'); + QCOMPARE(code, expectedCode); +} + + +namespace { + QString toGlsl(QShaderLanguage::StorageQualifier qualifier, const QShaderFormat &format) + { + if (format.version().majorVersion() <= 2) { + // Note we're assuming fragment shader only here, it'd be different + // values for vertex shader, will need to be fixed properly at some + // point but isn't necessary yet (this problem already exists in past + // commits anyway) + switch (qualifier) { + case QShaderLanguage::Const: + return "const"; + case QShaderLanguage::Input: + return "varying"; + case QShaderLanguage::Output: + return ""; // Although fragment shaders for <=2 only have fixed outputs + case QShaderLanguage::Uniform: + return "uniform"; + } + } else { + switch (qualifier) { + case QShaderLanguage::Const: + return "const"; + case QShaderLanguage::Input: + return "in"; + case QShaderLanguage::Output: + return "out"; + case QShaderLanguage::Uniform: + return "uniform"; + } + } + + Q_UNREACHABLE(); + } + + QString toGlsl(QShaderLanguage::VariableType type) + { + switch (type) { + case QShaderLanguage::Bool: + return "bool"; + case QShaderLanguage::Int: + return "int"; + case QShaderLanguage::Uint: + return "uint"; + case QShaderLanguage::Float: + return "float"; + case QShaderLanguage::Double: + return "double"; + case QShaderLanguage::Vec2: + return "vec2"; + case QShaderLanguage::Vec3: + return "vec3"; + case QShaderLanguage::Vec4: + return "vec4"; + case QShaderLanguage::DVec2: + return "dvec2"; + case QShaderLanguage::DVec3: + return "dvec3"; + case QShaderLanguage::DVec4: + return "dvec4"; + case QShaderLanguage::BVec2: + return "bvec2"; + case QShaderLanguage::BVec3: + return "bvec3"; + case QShaderLanguage::BVec4: + return "bvec4"; + case QShaderLanguage::IVec2: + return "ivec2"; + case QShaderLanguage::IVec3: + return "ivec3"; + case QShaderLanguage::IVec4: + return "ivec4"; + case QShaderLanguage::UVec2: + return "uvec2"; + case QShaderLanguage::UVec3: + return "uvec3"; + case QShaderLanguage::UVec4: + return "uvec4"; + case QShaderLanguage::Mat2: + return "mat2"; + case QShaderLanguage::Mat3: + return "mat3"; + case QShaderLanguage::Mat4: + return "mat4"; + case QShaderLanguage::Mat2x2: + return "mat2x2"; + case QShaderLanguage::Mat2x3: + return "mat2x3"; + case QShaderLanguage::Mat2x4: + return "mat2x4"; + case QShaderLanguage::Mat3x2: + return "mat3x2"; + case QShaderLanguage::Mat3x3: + return "mat3x3"; + case QShaderLanguage::Mat3x4: + return "mat3x4"; + case QShaderLanguage::Mat4x2: + return "mat4x2"; + case QShaderLanguage::Mat4x3: + return "mat4x3"; + case QShaderLanguage::Mat4x4: + return "mat4x4"; + case QShaderLanguage::DMat2: + return "dmat2"; + case QShaderLanguage::DMat3: + return "dmat3"; + case QShaderLanguage::DMat4: + return "dmat4"; + case QShaderLanguage::DMat2x2: + return "dmat2x2"; + case QShaderLanguage::DMat2x3: + return "dmat2x3"; + case QShaderLanguage::DMat2x4: + return "dmat2x4"; + case QShaderLanguage::DMat3x2: + return "dmat3x2"; + case QShaderLanguage::DMat3x3: + return "dmat3x3"; + case QShaderLanguage::DMat3x4: + return "dmat3x4"; + case QShaderLanguage::DMat4x2: + return "dmat4x2"; + case QShaderLanguage::DMat4x3: + return "dmat4x3"; + case QShaderLanguage::DMat4x4: + return "dmat4x4"; + case QShaderLanguage::Sampler1D: + return "sampler1D"; + case QShaderLanguage::Sampler2D: + return "sampler2D"; + case QShaderLanguage::Sampler3D: + return "sampler3D"; + case QShaderLanguage::SamplerCube: + return "samplerCube"; + case QShaderLanguage::Sampler2DRect: + return "sampler2DRect"; + case QShaderLanguage::Sampler2DMs: + return "sampler2DMS"; + case QShaderLanguage::SamplerBuffer: + return "samplerBuffer"; + case QShaderLanguage::Sampler1DArray: + return "sampler1DArray"; + case QShaderLanguage::Sampler2DArray: + return "sampler2DArray"; + case QShaderLanguage::Sampler2DMsArray: + return "sampler2DMSArray"; + case QShaderLanguage::SamplerCubeArray: + return "samplerCubeArray"; + case QShaderLanguage::Sampler1DShadow: + return "sampler1DShadow"; + case QShaderLanguage::Sampler2DShadow: + return "sampler2DShadow"; + case QShaderLanguage::Sampler2DRectShadow: + return "sampler2DRectShadow"; + case QShaderLanguage::Sampler1DArrayShadow: + return "sampler1DArrayShadow"; + case QShaderLanguage::Sampler2DArrayShadow: + return "sample2DArrayShadow"; + case QShaderLanguage::SamplerCubeShadow: + return "samplerCubeShadow"; + case QShaderLanguage::SamplerCubeArrayShadow: + return "samplerCubeArrayShadow"; + case QShaderLanguage::ISampler1D: + return "isampler1D"; + case QShaderLanguage::ISampler2D: + return "isampler2D"; + case QShaderLanguage::ISampler3D: + return "isampler3D"; + case QShaderLanguage::ISamplerCube: + return "isamplerCube"; + case QShaderLanguage::ISampler2DRect: + return "isampler2DRect"; + case QShaderLanguage::ISampler2DMs: + return "isampler2DMS"; + case QShaderLanguage::ISamplerBuffer: + return "isamplerBuffer"; + case QShaderLanguage::ISampler1DArray: + return "isampler1DArray"; + case QShaderLanguage::ISampler2DArray: + return "isampler2DArray"; + case QShaderLanguage::ISampler2DMsArray: + return "isampler2DMSArray"; + case QShaderLanguage::ISamplerCubeArray: + return "isamplerCubeArray"; + case QShaderLanguage::USampler1D: + return "usampler1D"; + case QShaderLanguage::USampler2D: + return "usampler2D"; + case QShaderLanguage::USampler3D: + return "usampler3D"; + case QShaderLanguage::USamplerCube: + return "usamplerCube"; + case QShaderLanguage::USampler2DRect: + return "usampler2DRect"; + case QShaderLanguage::USampler2DMs: + return "usampler2DMS"; + case QShaderLanguage::USamplerBuffer: + return "usamplerBuffer"; + case QShaderLanguage::USampler1DArray: + return "usampler1DArray"; + case QShaderLanguage::USampler2DArray: + return "usampler2DArray"; + case QShaderLanguage::USampler2DMsArray: + return "usampler2DMSArray"; + case QShaderLanguage::USamplerCubeArray: + return "usamplerCubeArray"; + } + + Q_UNREACHABLE(); + } +} + +void tst_QShaderGenerator::shouldProcessLanguageQualifierAndTypeEnums_data() +{ + QTest::addColumn<QShaderGraph>("graph"); + QTest::addColumn<QShaderFormat>("format"); + QTest::addColumn<QByteArray>("expectedCode"); + + const auto es2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto es3 = createFormat(QShaderFormat::OpenGLES, 3, 0); + const auto gl2 = createFormat(QShaderFormat::OpenGLNoProfile, 2, 0); + const auto gl3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + const auto qualifierEnum = QMetaEnum::fromType<QShaderLanguage::StorageQualifier>(); + const auto typeEnum = QMetaEnum::fromType<QShaderLanguage::VariableType>(); + + for (int qualifierIndex = 0; qualifierIndex < qualifierEnum.keyCount(); qualifierIndex++) { + const auto qualifierName = qualifierEnum.key(qualifierIndex); + const auto qualifierValue = static_cast<QShaderLanguage::StorageQualifier>(qualifierEnum.value(qualifierIndex)); + + for (int typeIndex = 0; typeIndex < typeEnum.keyCount(); typeIndex++) { + const auto typeName = typeEnum.key(typeIndex); + const auto typeValue = static_cast<QShaderLanguage::VariableType>(typeEnum.value(typeIndex)); + + auto graph = QShaderGraph(); + + auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + worldPosition.setParameter("name", "worldPosition"); + worldPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(qualifierValue)); + worldPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(typeValue)); + worldPosition.addRule(es2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + worldPosition.addRule(gl2, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + worldPosition.addRule(gl3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(es2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(gl2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(gl3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + graph.addNode(worldPosition); + graph.addNode(fragColor); + + graph.addEdge(createEdge(worldPosition.uuid(), "value", fragColor.uuid(), "fragColor")); + + const auto gl2Code = (QByteArrayList() << "#version 110" + << "" + << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl2)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "" + << "void main()" + << "{" + << QStringLiteral(" %1 v0 = worldPosition;").arg(toGlsl(typeValue)).toUtf8() + << " gl_fragColor = v0;" + << "}" + << "").join("\n"); + const auto gl3Code = (QByteArrayList() << "#version 130" + << "" + << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl3)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << QStringLiteral(" %1 v0 = worldPosition;").arg(toGlsl(typeValue)).toUtf8() + << " fragColor = v0;" + << "}" + << "").join("\n"); + const auto gl4Code = (QByteArrayList() << "#version 400 core" + << "" + << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl4)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << QStringLiteral(" %1 v0 = worldPosition;").arg(toGlsl(typeValue)).toUtf8() + << " fragColor = v0;" + << "}" + << "").join("\n"); + const auto es2Code = (QByteArrayList() << "#version 100" + << "" + << QStringLiteral("%1 highp %2 worldPosition;").arg(toGlsl(qualifierValue, es2)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "" + << "void main()" + << "{" + << QStringLiteral(" highp %1 v0 = worldPosition;").arg(toGlsl(typeValue)).toUtf8() + << " gl_fragColor = v0;" + << "}" + << "").join("\n"); + const auto es3Code = (QByteArrayList() << "#version 300 es" + << "" + << QStringLiteral("%1 highp %2 worldPosition;").arg(toGlsl(qualifierValue, es3)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "" + << "void main()" + << "{" + << QStringLiteral(" highp %1 v0 = worldPosition;").arg(toGlsl(typeValue)).toUtf8() + << " gl_fragColor = v0;" + << "}" + << "").join("\n"); + + QTest::addRow("%s %s ES2", qualifierName, typeName) << graph << es2 << es2Code; + QTest::addRow("%s %s ES3", qualifierName, typeName) << graph << es3 << es3Code; + QTest::addRow("%s %s GL2", qualifierName, typeName) << graph << gl2 << gl2Code; + QTest::addRow("%s %s GL3", qualifierName, typeName) << graph << gl3 << gl3Code; + QTest::addRow("%s %s GL4", qualifierName, typeName) << graph << gl4 << gl4Code; + } + } +} + +void tst_QShaderGenerator::shouldProcessLanguageQualifierAndTypeEnums() +{ + // GIVEN + QFETCH(QShaderGraph, graph); + QFETCH(QShaderFormat, format); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = format; + + // WHEN + const auto code = generator.createShaderCode(); + + // THEN + QFETCH(QByteArray, expectedCode); + QCOMPARE(code, expectedCode); +} + +void tst_QShaderGenerator::shouldGenerateDifferentCodeDependingOnActiveLayers() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }, { + "diffuseTexture", + "normalTexture" + }); + texCoord.addRule(gl4, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + auto diffuseUniform = createNode({ + createPort(QShaderNodePort::Output, "color") + }, {"diffuseUniform"}); + diffuseUniform.addRule(gl4, QShaderNode::Rule("vec4 $color = diffuseUniform;", + QByteArrayList() << "uniform vec4 diffuseUniform;")); + auto diffuseTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }, {"diffuseTexture"}); + diffuseTexture.addRule(gl4, QShaderNode::Rule("vec4 $color = texture2D(diffuseTexture, $coord);", + QByteArrayList() << "uniform sampler2D diffuseTexture;")); + auto normalUniform = createNode({ + createPort(QShaderNodePort::Output, "normal") + }, {"normalUniform"}); + normalUniform.addRule(gl4, QShaderNode::Rule("vec3 $normal = normalUniform;", + QByteArrayList() << "uniform vec3 normalUniform;")); + auto normalTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "normal") + }, {"normalTexture"}); + normalTexture.addRule(gl4, QShaderNode::Rule("vec3 $normal = texture2D(normalTexture, $coord).rgb;", + QByteArrayList() << "uniform sampler2D normalTexture;")); + auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "color"), + createPort(QShaderNodePort::Input, "normal"), + createPort(QShaderNodePort::Output, "output") + }); + lightFunction.addRule(gl4, QShaderNode::Rule("vec4 $output = lightModel($color, $normal);", + QByteArrayList() << "#pragma include gl4/lightmodel.frag.inc")); + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(gl4, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(texCoord); + res.addNode(diffuseUniform); + res.addNode(diffuseTexture); + res.addNode(normalUniform); + res.addNode(normalTexture); + res.addNode(lightFunction); + res.addNode(fragColor); + + res.addEdge(createEdge(diffuseUniform.uuid(), "color", lightFunction.uuid(), "color", {"diffuseUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", diffuseTexture.uuid(), "coord", {"diffuseTexture"})); + res.addEdge(createEdge(diffuseTexture.uuid(), "color", lightFunction.uuid(), "color", {"diffuseTexture"})); + + res.addEdge(createEdge(normalUniform.uuid(), "normal", lightFunction.uuid(), "normal", {"normalUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", normalTexture.uuid(), "coord", {"normalTexture"})); + res.addEdge(createEdge(normalTexture.uuid(), "normal", lightFunction.uuid(), "normal", {"normalTexture"})); + + res.addEdge(createEdge(lightFunction.uuid(), "output", fragColor.uuid(), "fragColor")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseUniform", "normalUniform"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "uniform vec4 diffuseUniform;" + << "uniform vec3 normalUniform;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " vec3 v1 = normalUniform;" + << " vec4 v0 = diffuseUniform;" + << " vec4 v2 = lightModel(v0, v1);" + << " fragColor = v2;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseUniform", "normalTexture"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec2 texCoord;" + << "uniform vec4 diffuseUniform;" + << "uniform sampler2D normalTexture;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " vec2 v0 = texCoord;" + << " vec3 v2 = texture2D(normalTexture, v0).rgb;" + << " vec4 v1 = diffuseUniform;" + << " vec4 v3 = lightModel(v1, v2);" + << " fragColor = v3;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseTexture", "normalUniform"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec2 texCoord;" + << "uniform sampler2D diffuseTexture;" + << "uniform vec3 normalUniform;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " vec2 v0 = texCoord;" + << " vec3 v2 = normalUniform;" + << " vec4 v1 = texture2D(diffuseTexture, v0);" + << " vec4 v3 = lightModel(v1, v2);" + << " fragColor = v3;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseTexture", "normalTexture"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec2 texCoord;" + << "uniform sampler2D diffuseTexture;" + << "uniform sampler2D normalTexture;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " vec2 v0 = texCoord;" + << " vec3 v2 = texture2D(normalTexture, v0).rgb;" + << " vec4 v1 = texture2D(diffuseTexture, v0);" + << " vec4 v3 = lightModel(v1, v2);" + << " fragColor = v3;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } +} + +QTEST_MAIN(tst_QShaderGenerator) + +#include "tst_qshadergenerator.moc" diff --git a/tests/auto/gui/util/qshadergraph/qshadergraph.pro b/tests/auto/gui/util/qshadergraph/qshadergraph.pro new file mode 100644 index 0000000000..ec54941c77 --- /dev/null +++ b/tests/auto/gui/util/qshadergraph/qshadergraph.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadergraph.cpp +TARGET = tst_qshadergraph diff --git a/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp b/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp new file mode 100644 index 0000000000..ce2d38c24f --- /dev/null +++ b/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp @@ -0,0 +1,779 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtGui/private/qshadergraph_p.h> + +namespace +{ + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports, const QStringList &layers = QStringList()) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + node.setLayers(layers); + for (const auto &port : ports) + node.addPort(port); + return node; + } + + QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, + const QUuid &targetUuid, const QString &targetName, + const QStringList &layers = QStringList()) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + edge.layers = layers; + return edge; + } + + QShaderGraph::Statement createStatement(const QShaderNode &node, + const QVector<int> &inputs = QVector<int>(), + const QVector<int> &outputs = QVector<int>()) + { + auto statement = QShaderGraph::Statement(); + statement.node = node; + statement.inputs = inputs; + statement.outputs = outputs; + return statement; + } + + void debugStatement(const QString &prefix, const QShaderGraph::Statement &statement) + { + qDebug() << prefix << statement.inputs << statement.uuid().toString() << statement.outputs; + } + + void dumpStatementsIfNeeded(const QVector<QShaderGraph::Statement> &statements, const QVector<QShaderGraph::Statement> &expected) + { + if (statements != expected) { + for (int i = 0; i < qMax(statements.size(), expected.size()); i++) { + qDebug() << "----" << i << "----"; + if (i < statements.size()) + debugStatement("A:", statements.at(i)); + if (i < expected.size()) + debugStatement("E:", expected.at(i)); + qDebug() << "-----------"; + } + } + } +} + +class tst_QShaderGraph : public QObject +{ + Q_OBJECT +private slots: + void shouldHaveEdgeDefaultState(); + void shouldTestEdgesEquality_data(); + void shouldTestEdgesEquality(); + void shouldManipulateStatementMembers(); + void shouldTestStatementsEquality_data(); + void shouldTestStatementsEquality(); + void shouldFindIndexFromPortNameInStatements_data(); + void shouldFindIndexFromPortNameInStatements(); + void shouldManageNodeList(); + void shouldManageEdgeList(); + void shouldSerializeGraphForCodeGeneration(); + void shouldHandleUnboundPortsDuringGraphSerialization(); + void shouldSurviveCyclesDuringGraphSerialization(); + void shouldDealWithEdgesJumpingOverLayers(); + void shouldGenerateDifferentStatementsDependingOnActiveLayers(); +}; + +void tst_QShaderGraph::shouldHaveEdgeDefaultState() +{ + // GIVEN + auto edge = QShaderGraph::Edge(); + + // THEN + QVERIFY(edge.sourceNodeUuid.isNull()); + QVERIFY(edge.sourcePortName.isEmpty()); + QVERIFY(edge.targetNodeUuid.isNull()); + QVERIFY(edge.targetPortName.isEmpty()); +} + +void tst_QShaderGraph::shouldTestEdgesEquality_data() +{ + QTest::addColumn<QShaderGraph::Edge>("left"); + QTest::addColumn<QShaderGraph::Edge>("right"); + QTest::addColumn<bool>("expected"); + + const auto sourceUuid1 = QUuid::createUuid(); + const auto sourceUuid2 = QUuid::createUuid(); + const auto targetUuid1 = QUuid::createUuid(); + const auto targetUuid2 = QUuid::createUuid(); + + QTest::newRow("Equals") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << true; + QTest::newRow("SourceUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid2, "foo", targetUuid1, "bar") + << false; + QTest::newRow("SourceName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "bleh", targetUuid1, "bar") + << false; + QTest::newRow("TargetUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "foo", targetUuid2, "bar") + << false; + QTest::newRow("TargetName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "foo", targetUuid1, "bleh") + << false; +} + +void tst_QShaderGraph::shouldTestEdgesEquality() +{ + // GIVEN + QFETCH(QShaderGraph::Edge, left); + QFETCH(QShaderGraph::Edge, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderGraph::shouldManipulateStatementMembers() +{ + // GIVEN + auto statement = QShaderGraph::Statement(); + + // THEN (default state) + QVERIFY(statement.inputs.isEmpty()); + QVERIFY(statement.outputs.isEmpty()); + QVERIFY(statement.node.uuid().isNull()); + QVERIFY(statement.uuid().isNull()); + + // WHEN + const auto node = createNode({}); + statement.node = node; + + // THEN + QCOMPARE(statement.uuid(), node.uuid()); + + // WHEN + statement.node = QShaderNode(); + + // THEN + QVERIFY(statement.uuid().isNull()); +} + +void tst_QShaderGraph::shouldTestStatementsEquality_data() +{ + QTest::addColumn<QShaderGraph::Statement>("left"); + QTest::addColumn<QShaderGraph::Statement>("right"); + QTest::addColumn<bool>("expected"); + + const auto node1 = createNode({}); + const auto node2 = createNode({}); + + QTest::newRow("EqualNodes") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node1, {1, 2}, {3, 4}) + << true; + QTest::newRow("EqualInvalids") << createStatement(QShaderNode(), {1, 2}, {3, 4}) + << createStatement(QShaderNode(), {1, 2}, {3, 4}) + << true; + QTest::newRow("Nodes") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node2, {1, 2}, {3, 4}) + << false; + QTest::newRow("Inputs") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node1, {1, 2, 0}, {3, 4}) + << false; + QTest::newRow("Outputs") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node1, {1, 2}, {3, 0, 4}) + << false; +} + +void tst_QShaderGraph::shouldTestStatementsEquality() +{ + // GIVEN + QFETCH(QShaderGraph::Statement, left); + QFETCH(QShaderGraph::Statement, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderGraph::shouldFindIndexFromPortNameInStatements_data() +{ + QTest::addColumn<QShaderGraph::Statement>("statement"); + QTest::addColumn<QString>("portName"); + QTest::addColumn<int>("expectedInputIndex"); + QTest::addColumn<int>("expectedOutputIndex"); + + const auto inputNodeStatement = createStatement(createNode({ + createPort(QShaderNodePort::Output, "input") + })); + const auto outputNodeStatement = createStatement(createNode({ + createPort(QShaderNodePort::Input, "output") + })); + const auto functionNodeStatement = createStatement(createNode({ + createPort(QShaderNodePort::Input, "input1"), + createPort(QShaderNodePort::Output, "output1"), + createPort(QShaderNodePort::Input, "input2"), + createPort(QShaderNodePort::Output, "output2"), + createPort(QShaderNodePort::Output, "output3"), + createPort(QShaderNodePort::Input, "input3") + })); + + QTest::newRow("Invalid") << QShaderGraph::Statement() << "foo" << -1 << -1; + QTest::newRow("InputNodeWrongName") << inputNodeStatement << "foo" << -1 << -1; + QTest::newRow("InputNodeExistingName") << inputNodeStatement << "input" << -1 << 0; + QTest::newRow("OutputNodeWrongName") << outputNodeStatement << "foo" << -1 << -1; + QTest::newRow("OutputNodeExistingName") << outputNodeStatement << "output" << 0 << -1; + QTest::newRow("FunctionNodeWrongName") << functionNodeStatement << "foo" << -1 << -1; + QTest::newRow("FunctionNodeInput1") << functionNodeStatement << "input1" << 0 << -1; + QTest::newRow("FunctionNodeOutput1") << functionNodeStatement << "output1" << -1 << 0; + QTest::newRow("FunctionNodeInput2") << functionNodeStatement << "input2" << 1 << -1; + QTest::newRow("FunctionNodeOutput2") << functionNodeStatement << "output2" << -1 << 1; + QTest::newRow("FunctionNodeInput3") << functionNodeStatement << "input3" << 2 << -1; + QTest::newRow("FunctionNodeOutput3") << functionNodeStatement << "output3" << -1 << 2; +} + +void tst_QShaderGraph::shouldFindIndexFromPortNameInStatements() +{ + // GIVEN + QFETCH(QShaderGraph::Statement, statement); + QFETCH(QString, portName); + QFETCH(int, expectedInputIndex); + QFETCH(int, expectedOutputIndex); + + // WHEN + const auto inputIndex = statement.portIndex(QShaderNodePort::Input, portName); + const auto outputIndex = statement.portIndex(QShaderNodePort::Output, portName); + + // THEN + QCOMPARE(inputIndex, expectedInputIndex); + QCOMPARE(outputIndex, expectedOutputIndex); +} + +void tst_QShaderGraph::shouldManageNodeList() +{ + // GIVEN + const auto node1 = createNode({createPort(QShaderNodePort::Output, "node1")}); + const auto node2 = createNode({createPort(QShaderNodePort::Output, "node2")}); + + auto graph = QShaderGraph(); + + // THEN (default state) + QVERIFY(graph.nodes().isEmpty()); + + // WHEN + graph.addNode(node1); + + // THEN + QCOMPARE(graph.nodes().size(), 1); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + + // WHEN + graph.addNode(node2); + + // THEN + QCOMPARE(graph.nodes().size(), 2); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid()); + QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name); + + + // WHEN + graph.removeNode(node2); + + // THEN + QCOMPARE(graph.nodes().size(), 1); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + + // WHEN + graph.addNode(node2); + + // THEN + QCOMPARE(graph.nodes().size(), 2); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid()); + QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name); + + // WHEN + const auto node1bis = [node1] { + auto res = node1; + auto port = res.ports().at(0); + port.name = QStringLiteral("node1bis"); + res.addPort(port); + return res; + }(); + graph.addNode(node1bis); + + // THEN + QCOMPARE(graph.nodes().size(), 2); + QCOMPARE(graph.nodes().at(0).uuid(), node2.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node2.ports().at(0).name); + QCOMPARE(graph.nodes().at(1).uuid(), node1bis.uuid()); + QCOMPARE(graph.nodes().at(1).ports().at(0).name, node1bis.ports().at(0).name); +} + +void tst_QShaderGraph::shouldManageEdgeList() +{ + // GIVEN + const auto edge1 = createEdge(QUuid::createUuid(), "foo", QUuid::createUuid(), "bar"); + const auto edge2 = createEdge(QUuid::createUuid(), "baz", QUuid::createUuid(), "boo"); + + auto graph = QShaderGraph(); + + // THEN (default state) + QVERIFY(graph.edges().isEmpty()); + + // WHEN + graph.addEdge(edge1); + + // THEN + QCOMPARE(graph.edges().size(), 1); + QCOMPARE(graph.edges().at(0), edge1); + + // WHEN + graph.addEdge(edge2); + + // THEN + QCOMPARE(graph.edges().size(), 2); + QCOMPARE(graph.edges().at(0), edge1); + QCOMPARE(graph.edges().at(1), edge2); + + + // WHEN + graph.removeEdge(edge2); + + // THEN + QCOMPARE(graph.edges().size(), 1); + QCOMPARE(graph.edges().at(0), edge1); + + // WHEN + graph.addEdge(edge2); + + // THEN + QCOMPARE(graph.edges().size(), 2); + QCOMPARE(graph.edges().at(0), edge1); + QCOMPARE(graph.edges().at(1), edge2); + + // WHEN + graph.addEdge(edge1); + + // THEN + QCOMPARE(graph.edges().size(), 2); + QCOMPARE(graph.edges().at(0), edge1); + QCOMPARE(graph.edges().at(1), edge2); +} + +void tst_QShaderGraph::shouldSerializeGraphForCodeGeneration() +{ + // GIVEN + const auto input1 = createNode({ + createPort(QShaderNodePort::Output, "input1Value") + }); + const auto input2 = createNode({ + createPort(QShaderNodePort::Output, "input2Value") + }); + const auto output1 = createNode({ + createPort(QShaderNodePort::Input, "output1Value") + }); + const auto output2 = createNode({ + createPort(QShaderNodePort::Input, "output2Value") + }); + const auto function1 = createNode({ + createPort(QShaderNodePort::Input, "function1Input"), + createPort(QShaderNodePort::Output, "function1Output") + }); + const auto function2 = createNode({ + createPort(QShaderNodePort::Input, "function2Input1"), + createPort(QShaderNodePort::Input, "function2Input2"), + createPort(QShaderNodePort::Output, "function2Output") + }); + const auto function3 = createNode({ + createPort(QShaderNodePort::Input, "function3Input1"), + createPort(QShaderNodePort::Input, "function3Input2"), + createPort(QShaderNodePort::Output, "function3Output1"), + createPort(QShaderNodePort::Output, "function3Output2") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input1); + res.addNode(input2); + res.addNode(output1); + res.addNode(output2); + res.addNode(function1); + res.addNode(function2); + res.addNode(function3); + res.addEdge(createEdge(input1.uuid(), "input1Value", function1.uuid(), "function1Input")); + res.addEdge(createEdge(input1.uuid(), "input1Value", function2.uuid(), "function2Input1")); + res.addEdge(createEdge(input2.uuid(), "input2Value", function2.uuid(), "function2Input2")); + res.addEdge(createEdge(function1.uuid(), "function1Output", function3.uuid(), "function3Input1")); + res.addEdge(createEdge(function2.uuid(), "function2Output", function3.uuid(), "function3Input2")); + res.addEdge(createEdge(function3.uuid(), "function3Output1", output1.uuid(), "output1Value")); + res.addEdge(createEdge(function3.uuid(), "function3Output2", output2.uuid(), "output2Value")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(input2, {}, {1}) + << createStatement(input1, {}, {0}) + << createStatement(function2, {0, 1}, {3}) + << createStatement(function1, {0}, {2}) + << createStatement(function3, {2, 3}, {4, 5}) + << createStatement(output2, {5}, {}) + << createStatement(output1, {4}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldHandleUnboundPortsDuringGraphSerialization() +{ + // GIVEN + const auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + const auto unboundInput = createNode({ + createPort(QShaderNodePort::Output, "unbound") + }); + const auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + const auto unboundOutput = createNode({ + createPort(QShaderNodePort::Input, "unbound") + }); + const auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput1"), + createPort(QShaderNodePort::Input, "functionInput2"), + createPort(QShaderNodePort::Input, "functionInput3"), + createPort(QShaderNodePort::Output, "functionOutput1"), + createPort(QShaderNodePort::Output, "functionOutput2"), + createPort(QShaderNodePort::Output, "functionOutput3") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input); + res.addNode(unboundInput); + res.addNode(output); + res.addNode(unboundOutput); + res.addNode(function); + res.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput2")); + res.addEdge(createEdge(function.uuid(), "functionOutput2", output.uuid(), "output")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + // Note that no edge leads to the unbound input + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(input, {}, {0}) + << createStatement(function, {-1, 0, -1}, {2, 3, 4}) + << createStatement(unboundOutput, {-1}, {}) + << createStatement(output, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldSurviveCyclesDuringGraphSerialization() +{ + // GIVEN + const auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + const auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + const auto function1 = createNode({ + createPort(QShaderNodePort::Input, "function1Input1"), + createPort(QShaderNodePort::Input, "function1Input2"), + createPort(QShaderNodePort::Output, "function1Output") + }); + const auto function2 = createNode({ + createPort(QShaderNodePort::Input, "function2Input"), + createPort(QShaderNodePort::Output, "function2Output") + }); + const auto function3 = createNode({ + createPort(QShaderNodePort::Input, "function3Input"), + createPort(QShaderNodePort::Output, "function3Output") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input); + res.addNode(output); + res.addNode(function1); + res.addNode(function2); + res.addNode(function3); + res.addEdge(createEdge(input.uuid(), "input", function1.uuid(), "function1Input1")); + res.addEdge(createEdge(function1.uuid(), "function1Output", function2.uuid(), "function2Input")); + res.addEdge(createEdge(function2.uuid(), "function2Output", function3.uuid(), "function3Input")); + res.addEdge(createEdge(function3.uuid(), "function3Output", function1.uuid(), "function1Input2")); + res.addEdge(createEdge(function2.uuid(), "function2Output", output.uuid(), "output")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + // Obviously will lead to a compile failure later on since it cuts everything beyond the cycle + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(output, {2}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldDealWithEdgesJumpingOverLayers() +{ + // GIVEN + const auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "worldPosition") + }); + const auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + const auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + const auto lightIntensity = createNode({ + createPort(QShaderNodePort::Output, "lightIntensity") + }); + const auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + const auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + const auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + const auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + const auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(worldPosition); + res.addNode(texture); + res.addNode(texCoord); + res.addNode(lightIntensity); + res.addNode(exposure); + res.addNode(fragColor); + res.addNode(sampleTexture); + res.addNode(lightFunction); + res.addNode(exposureFunction); + + res.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); + + res.addEdge(createEdge(worldPosition.uuid(), "worldPosition", lightFunction.uuid(), "position")); + res.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); + res.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity")); + + res.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); + res.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); + + res.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); + + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {2}) + << createStatement(texture, {}, {1}) + << createStatement(lightIntensity, {}, {3}) + << createStatement(sampleTexture, {1, 2}, {5}) + << createStatement(worldPosition, {}, {0}) + << createStatement(exposure, {}, {4}) + << createStatement(lightFunction, {5, 0, 3}, {6}) + << createStatement(exposureFunction, {6, 4}, {7}) + << createStatement(fragColor, {7}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldGenerateDifferentStatementsDependingOnActiveLayers() +{ + // GIVEN + const auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }, { + "diffuseTexture", + "normalTexture" + }); + const auto diffuseUniform = createNode({ + createPort(QShaderNodePort::Output, "color") + }, {"diffuseUniform"}); + const auto diffuseTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }, {"diffuseTexture"}); + const auto normalUniform = createNode({ + createPort(QShaderNodePort::Output, "normal") + }, {"normalUniform"}); + const auto normalTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "normal") + }, {"normalTexture"}); + const auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "color"), + createPort(QShaderNodePort::Input, "normal"), + createPort(QShaderNodePort::Output, "output") + }); + const auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(texCoord); + res.addNode(diffuseUniform); + res.addNode(diffuseTexture); + res.addNode(normalUniform); + res.addNode(normalTexture); + res.addNode(lightFunction); + res.addNode(fragColor); + + res.addEdge(createEdge(diffuseUniform.uuid(), "color", lightFunction.uuid(), "color", {"diffuseUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", diffuseTexture.uuid(), "coord", {"diffuseTexture"})); + res.addEdge(createEdge(diffuseTexture.uuid(), "color", lightFunction.uuid(), "color", {"diffuseTexture"})); + + res.addEdge(createEdge(normalUniform.uuid(), "normal", lightFunction.uuid(), "normal", {"normalUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", normalTexture.uuid(), "coord", {"normalTexture"})); + res.addEdge(createEdge(normalTexture.uuid(), "normal", lightFunction.uuid(), "normal", {"normalTexture"})); + + res.addEdge(createEdge(lightFunction.uuid(), "output", fragColor.uuid(), "fragColor")); + + return res; + }(); + + { + // WHEN + const auto statements = graph.createStatements({"diffuseUniform", "normalUniform"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(normalUniform, {}, {1}) + << createStatement(diffuseUniform, {}, {0}) + << createStatement(lightFunction, {0, 1}, {2}) + << createStatement(fragColor, {2}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } + + { + // WHEN + const auto statements = graph.createStatements({"diffuseUniform", "normalTexture"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {0}) + << createStatement(normalTexture, {0}, {2}) + << createStatement(diffuseUniform, {}, {1}) + << createStatement(lightFunction, {1, 2}, {3}) + << createStatement(fragColor, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } + + { + // WHEN + const auto statements = graph.createStatements({"diffuseTexture", "normalUniform"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {0}) + << createStatement(normalUniform, {}, {2}) + << createStatement(diffuseTexture, {0}, {1}) + << createStatement(lightFunction, {1, 2}, {3}) + << createStatement(fragColor, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } + + { + // WHEN + const auto statements = graph.createStatements({"diffuseTexture", "normalTexture"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {0}) + << createStatement(normalTexture, {0}, {2}) + << createStatement(diffuseTexture, {0}, {1}) + << createStatement(lightFunction, {1, 2}, {3}) + << createStatement(fragColor, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } +} + +QTEST_MAIN(tst_QShaderGraph) + +#include "tst_qshadergraph.moc" diff --git a/tests/auto/gui/util/qshadergraphloader/qshadergraphloader.pro b/tests/auto/gui/util/qshadergraphloader/qshadergraphloader.pro new file mode 100644 index 0000000000..e80a93f9e8 --- /dev/null +++ b/tests/auto/gui/util/qshadergraphloader/qshadergraphloader.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadergraphloader.cpp +TARGET = tst_qshadergraphloader diff --git a/tests/auto/gui/util/qshadergraphloader/tst_qshadergraphloader.cpp b/tests/auto/gui/util/qshadergraphloader/tst_qshadergraphloader.cpp new file mode 100644 index 0000000000..761e03a195 --- /dev/null +++ b/tests/auto/gui/util/qshadergraphloader/tst_qshadergraphloader.cpp @@ -0,0 +1,625 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtCore/qbuffer.h> + +#include <QtGui/private/qshadergraphloader_p.h> +#include <QtGui/private/qshaderlanguage_p.h> + +using QBufferPointer = QSharedPointer<QBuffer>; +Q_DECLARE_METATYPE(QBufferPointer); + +using PrototypeHash = QHash<QString, QShaderNode>; +Q_DECLARE_METATYPE(PrototypeHash); + +namespace +{ + QBufferPointer createBuffer(const QByteArray &data, QIODevice::OpenMode openMode = QIODevice::ReadOnly) + { + auto buffer = QBufferPointer::create(); + buffer->setData(data); + if (openMode != QIODevice::NotOpen) + buffer->open(openMode); + return buffer; + } + + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports, const QStringList &layers = QStringList()) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + node.setLayers(layers); + for (const auto &port : ports) + node.addPort(port); + return node; + } + + QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, + const QUuid &targetUuid, const QString &targetName, + const QStringList &layers = QStringList()) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + edge.layers = layers; + return edge; + } + + QShaderGraph createGraph() + { + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + auto graph = QShaderGraph(); + + auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + worldPosition.setUuid(QUuid("{00000000-0000-0000-0000-000000000001}")); + worldPosition.setParameter("name", "worldPosition"); + worldPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Input)); + worldPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3)); + worldPosition.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + worldPosition.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + + auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + texture.setUuid(QUuid("{00000000-0000-0000-0000-000000000002}")); + texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + texCoord.setUuid(QUuid("{00000000-0000-0000-0000-000000000003}")); + texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", + QByteArrayList() << "varying highp vec2 texCoord;")); + texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + + auto lightIntensity = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + lightIntensity.setUuid(QUuid("{00000000-0000-0000-0000-000000000004}")); + lightIntensity.setParameter("name", "defaultName"); + lightIntensity.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); + lightIntensity.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Float)); + lightIntensity.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + lightIntensity.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + + auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + exposure.setUuid(QUuid("{00000000-0000-0000-0000-000000000005}")); + exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", + QByteArrayList() << "uniform highp float exposure;")); + exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", + QByteArrayList() << "uniform float exposure;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.setUuid(QUuid("{00000000-0000-0000-0000-000000000006}")); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + sampleTexture.setUuid(QUuid("{00000000-0000-0000-0000-000000000007}")); + sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); + sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); + + auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightFunction.setUuid(QUuid("{00000000-0000-0000-0000-000000000008}")); + lightFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + + auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + exposureFunction.setUuid(QUuid("{00000000-0000-0000-0000-000000000009}")); + exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + + graph.addNode(worldPosition); + graph.addNode(texture); + graph.addNode(texCoord); + graph.addNode(lightIntensity); + graph.addNode(exposure); + graph.addNode(fragColor); + graph.addNode(sampleTexture); + graph.addNode(lightFunction); + graph.addNode(exposureFunction); + + graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); + graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); + + graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position")); + graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); + graph.addEdge(createEdge(lightIntensity.uuid(), "value", lightFunction.uuid(), "lightIntensity")); + + graph.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); + graph.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); + + graph.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); + + return graph; + } + + void debugStatement(const QString &prefix, const QShaderGraph::Statement &statement) + { + qDebug() << prefix << statement.inputs << statement.uuid().toString() << statement.outputs; + } + + void dumpStatementsIfNeeded(const QVector<QShaderGraph::Statement> &statements, const QVector<QShaderGraph::Statement> &expected) + { + if (statements != expected) { + for (int i = 0; i < qMax(statements.size(), expected.size()); i++) { + qDebug() << "----" << i << "----"; + if (i < statements.size()) + debugStatement("A:", statements.at(i)); + if (i < expected.size()) + debugStatement("E:", expected.at(i)); + qDebug() << "-----------"; + } + } + } +} + +class tst_QShaderGraphLoader : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateLoaderMembers(); + void shouldLoadFromJsonStream_data(); + void shouldLoadFromJsonStream(); +}; + +void tst_QShaderGraphLoader::shouldManipulateLoaderMembers() +{ + // GIVEN + auto loader = QShaderGraphLoader(); + + // THEN (default state) + QCOMPARE(loader.status(), QShaderGraphLoader::Null); + QVERIFY(!loader.device()); + QVERIFY(loader.graph().nodes().isEmpty()); + QVERIFY(loader.graph().edges().isEmpty()); + QVERIFY(loader.prototypes().isEmpty()); + + // WHEN + auto device1 = createBuffer(QByteArray("..........."), QIODevice::NotOpen); + loader.setDevice(device1.data()); + + // THEN + QCOMPARE(loader.status(), QShaderGraphLoader::Error); + QCOMPARE(loader.device(), device1.data()); + QVERIFY(loader.graph().nodes().isEmpty()); + QVERIFY(loader.graph().edges().isEmpty()); + + // WHEN + auto device2 = createBuffer(QByteArray("..........."), QIODevice::ReadOnly); + loader.setDevice(device2.data()); + + // THEN + QCOMPARE(loader.status(), QShaderGraphLoader::Waiting); + QCOMPARE(loader.device(), device2.data()); + QVERIFY(loader.graph().nodes().isEmpty()); + QVERIFY(loader.graph().edges().isEmpty()); + + + // WHEN + const auto prototypes = [this]{ + auto res = QHash<QString, QShaderNode>(); + res.insert("foo", createNode({})); + return res; + }(); + loader.setPrototypes(prototypes); + + // THEN + QCOMPARE(loader.prototypes().size(), prototypes.size()); + QVERIFY(loader.prototypes().contains("foo")); + QCOMPARE(loader.prototypes().value("foo").uuid(), prototypes.value("foo").uuid()); +} + +void tst_QShaderGraphLoader::shouldLoadFromJsonStream_data() +{ + QTest::addColumn<QBufferPointer>("device"); + QTest::addColumn<PrototypeHash>("prototypes"); + QTest::addColumn<QShaderGraph>("graph"); + QTest::addColumn<QShaderGraphLoader::Status>("status"); + + QTest::newRow("empty") << createBuffer("", QIODevice::ReadOnly) << PrototypeHash() + << QShaderGraph() << QShaderGraphLoader::Error; + + const auto smallJson = "{" + " \"nodes\": [" + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"type\": \"MyInput\"," + " \"layers\": [\"foo\", \"bar\"]" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"type\": \"MyOutput\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"type\": \"MyFunction\"" + " }" + " ]," + " \"edges\": [" + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"sourcePort\": \"input\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"targetPort\": \"functionInput\"," + " \"layers\": [\"bar\", \"baz\"]" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"sourcePort\": \"functionOutput\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"targetPort\": \"output\"" + " }" + " ]" + "}"; + + const auto smallProtos = [this]{ + auto protos = PrototypeHash(); + + auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + protos.insert("MyInput", input); + + auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + protos.insert("MyOutput", output); + + auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput"), + createPort(QShaderNodePort::Output, "functionOutput") + }); + protos.insert("MyFunction", function); + return protos; + }(); + + const auto smallGraph = [this]{ + auto graph = QShaderGraph(); + + auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }, {"foo", "bar"}); + input.setUuid(QUuid("{00000000-0000-0000-0000-000000000001}")); + auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + output.setUuid(QUuid("{00000000-0000-0000-0000-000000000002}")); + auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput"), + createPort(QShaderNodePort::Output, "functionOutput") + }); + function.setUuid(QUuid("{00000000-0000-0000-0000-000000000003}")); + + graph.addNode(input); + graph.addNode(output); + graph.addNode(function); + graph.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput", {"bar", "baz"})); + graph.addEdge(createEdge(function.uuid(), "functionOutput", output.uuid(), "output")); + + return graph; + }(); + + QTest::newRow("TwoNodesOneEdge") << createBuffer(smallJson) << smallProtos << smallGraph << QShaderGraphLoader::Ready; + QTest::newRow("NotOpen") << createBuffer(smallJson, QIODevice::NotOpen) << smallProtos << QShaderGraph() << QShaderGraphLoader::Error; + QTest::newRow("NoPrototype") << createBuffer(smallJson) << PrototypeHash() << QShaderGraph() << QShaderGraphLoader::Error; + + const auto complexJson = "{" + " \"nodes\": [" + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"type\": \"inputValue\"," + " \"parameters\": {" + " \"name\": \"worldPosition\"," + " \"qualifier\": {" + " \"type\": \"QShaderLanguage::StorageQualifier\"," + " \"value\": \"QShaderLanguage::Input\"" + " }," + " \"type\": {" + " \"type\": \"QShaderLanguage::VariableType\"," + " \"value\": \"QShaderLanguage::Vec3\"" + " }" + " }" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"type\": \"texture\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"type\": \"texCoord\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000004}\"," + " \"type\": \"inputValue\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000005}\"," + " \"type\": \"exposure\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000006}\"," + " \"type\": \"fragColor\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"type\": \"sampleTexture\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"type\": \"lightModel\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"type\": \"exposureFunction\"" + " }" + " ]," + " \"edges\": [" + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"sourcePort\": \"texture\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"targetPort\": \"sampler\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"sourcePort\": \"texCoord\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"targetPort\": \"coord\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"sourcePort\": \"value\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"targetPort\": \"position\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"sourcePort\": \"color\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"targetPort\": \"baseColor\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000004}\"," + " \"sourcePort\": \"value\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"targetPort\": \"lightIntensity\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"sourcePort\": \"outputColor\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"targetPort\": \"inputColor\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000005}\"," + " \"sourcePort\": \"exposure\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"targetPort\": \"exposure\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"sourcePort\": \"outputColor\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000006}\"," + " \"targetPort\": \"fragColor\"" + " }" + " ]" + "}"; + + const auto complexProtos = [this]{ + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + auto protos = PrototypeHash(); + + auto inputValue = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + inputValue.setParameter("name", "defaultName"); + inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); + inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Float)); + inputValue.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + inputValue.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + protos.insert("inputValue", inputValue); + + auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + protos.insert("texture", texture); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", + QByteArrayList() << "varying highp vec2 texCoord;")); + texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + protos.insert("texCoord", texCoord); + + auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", + QByteArrayList() << "uniform highp float exposure;")); + exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", + QByteArrayList() << "uniform float exposure;")); + protos.insert("exposure", exposure); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + protos.insert("fragColor", fragColor); + + auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); + sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); + protos.insert("sampleTexture", sampleTexture); + + auto lightModel = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightModel.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightModel.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + protos.insert("lightModel", lightModel); + + auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + protos.insert("exposureFunction", exposureFunction); + + return protos; + }(); + + const auto complexGraph = createGraph(); + + QTest::newRow("ComplexGraph") << createBuffer(complexJson) << complexProtos << complexGraph << QShaderGraphLoader::Ready; +} + +void tst_QShaderGraphLoader::shouldLoadFromJsonStream() +{ + // GIVEN + QFETCH(QBufferPointer, device); + QFETCH(PrototypeHash, prototypes); + + auto loader = QShaderGraphLoader(); + + // WHEN + loader.setPrototypes(prototypes); + loader.setDevice(device.data()); + loader.load(); + + // THEN + QFETCH(QShaderGraphLoader::Status, status); + QCOMPARE(loader.status(), status); + + QFETCH(QShaderGraph, graph); + const auto statements = loader.graph().createStatements({"foo", "bar", "baz"}); + const auto expected = graph.createStatements({"foo", "bar", "baz"}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + + const auto sortedParameters = [](const QShaderNode &node) { + auto res = node.parameterNames(); + res.sort(); + return res; + }; + + for (int i = 0; i < statements.size(); i++) { + const auto actualNode = statements.at(i).node; + const auto expectedNode = expected.at(i).node; + + QCOMPARE(actualNode.layers(), expectedNode.layers()); + QCOMPARE(actualNode.ports(), expectedNode.ports()); + QCOMPARE(sortedParameters(actualNode), sortedParameters(expectedNode)); + for (const auto &name : expectedNode.parameterNames()) { + QCOMPARE(actualNode.parameter(name), expectedNode.parameter(name)); + } + QCOMPARE(actualNode.availableFormats(), expectedNode.availableFormats()); + for (const auto &format : expectedNode.availableFormats()) { + QCOMPARE(actualNode.rule(format), expectedNode.rule(format)); + } + } +} + +QTEST_MAIN(tst_QShaderGraphLoader) + +#include "tst_qshadergraphloader.moc" diff --git a/tests/auto/gui/util/qshadernodes/qshadernodes.pro b/tests/auto/gui/util/qshadernodes/qshadernodes.pro new file mode 100644 index 0000000000..5ab8b73a51 --- /dev/null +++ b/tests/auto/gui/util/qshadernodes/qshadernodes.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadernodes.cpp +TARGET = tst_qshadernodes diff --git a/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp b/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp new file mode 100644 index 0000000000..9eb738a1b2 --- /dev/null +++ b/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp @@ -0,0 +1,548 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtGui/private/qshaderformat_p.h> +#include <QtGui/private/qshadernode_p.h> +#include <QtGui/private/qshadernodeport_p.h> + +namespace +{ + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion, + const QStringList &extensions = QStringList(), + const QString &vendor = QString()) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + format.setExtensions(extensions); + format.setVendor(vendor); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction direction, const QString &name) + { + auto port = QShaderNodePort(); + port.direction = direction; + port.name = name; + return port; + } +} + +class tst_QShaderNodes : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateFormatMembers(); + void shouldVerifyFormatsEquality_data(); + void shouldVerifyFormatsEquality(); + void shouldVerifyFormatsCompatibilities_data(); + void shouldVerifyFormatsCompatibilities(); + + void shouldHaveDefaultPortState(); + void shouldVerifyPortsEquality_data(); + void shouldVerifyPortsEquality(); + + void shouldManipulateNodeMembers(); + void shouldHandleNodeRulesSupportAndOrder(); +}; + +void tst_QShaderNodes::shouldManipulateFormatMembers() +{ + // GIVEN + auto format = QShaderFormat(); + + // THEN (default state) + QCOMPARE(format.api(), QShaderFormat::NoApi); + QCOMPARE(format.version().majorVersion(), 0); + QCOMPARE(format.version().minorVersion(), 0); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(!format.isValid()); + + // WHEN + format.setApi(QShaderFormat::OpenGLES); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 0); + QCOMPARE(format.version().minorVersion(), 0); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(!format.isValid()); + + // WHEN + format.setVersion(QVersionNumber(3)); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 0); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(format.isValid()); + + // WHEN + format.setVersion(QVersionNumber(3, 2)); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 2); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(format.isValid()); + + // WHEN + format.setExtensions({"foo", "bar"}); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 2); + QCOMPARE(format.extensions(), QStringList({"bar", "foo"})); + QCOMPARE(format.vendor(), QString()); + QVERIFY(format.isValid()); + + // WHEN + format.setVendor(QStringLiteral("KDAB")); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 2); + QCOMPARE(format.extensions(), QStringList({"bar", "foo"})); + QCOMPARE(format.vendor(), QStringLiteral("KDAB")); + QVERIFY(format.isValid()); +} + +void tst_QShaderNodes::shouldVerifyFormatsEquality_data() +{ + QTest::addColumn<QShaderFormat>("left"); + QTest::addColumn<QShaderFormat>("right"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("Equals") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << true; + QTest::newRow("Apis") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLNoProfile, 3, 0, {"foo", "bar"}, "KDAB") + << false; + QTest::newRow("Major") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0, {"foo", "bar"}, "KDAB") + << false; + QTest::newRow("Minor") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1, {"foo", "bar"}, "KDAB") + << false; + QTest::newRow("Extensions") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo"}, "KDAB") + << false; + QTest::newRow("Vendor") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}) + << false; +} + +void tst_QShaderNodes::shouldVerifyFormatsEquality() +{ + // GIVEN + QFETCH(QShaderFormat, left); + QFETCH(QShaderFormat, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderNodes::shouldVerifyFormatsCompatibilities_data() +{ + QTest::addColumn<QShaderFormat>("reference"); + QTest::addColumn<QShaderFormat>("tested"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("NoProfileVsES") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLES, 2, 0) + << true; + QTest::newRow("CoreProfileVsES") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLES, 2, 0) + << false; + QTest::newRow("CompatProfileVsES") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLES, 2, 0) + << true; + + QTest::newRow("ESVsNoProfile") << createFormat(QShaderFormat::OpenGLES, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << false; + QTest::newRow("ESVsCoreProfile") << createFormat(QShaderFormat::OpenGLES, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << false; + QTest::newRow("ESVsCompatProfile") << createFormat(QShaderFormat::OpenGLES, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << false; + + QTest::newRow("CoreVsNoProfile") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << false; + QTest::newRow("CoreVsCompat") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << false; + QTest::newRow("CoreVsCore") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + + QTest::newRow("NoProfileVsCore") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + QTest::newRow("NoProvileVsCompat") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << true; + QTest::newRow("NoProfileVsNoProfile") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << true; + + QTest::newRow("CompatVsCore") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + QTest::newRow("CompatVsCompat") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << true; + QTest::newRow("CompatVsNoProfile") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << true; + + QTest::newRow("MajorForwardCompat_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + QTest::newRow("MajorForwardCompat_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 4) + << true; + QTest::newRow("MajorForwardCompat_3") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << false; + QTest::newRow("MajorForwardCompat_4") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 4) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << false; + + QTest::newRow("MinorForwardCompat_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << true; + QTest::newRow("MinorForwardCompat_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) + << false; + + QTest::newRow("Extensions_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo"}) + << true; + QTest::newRow("Extensions_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo"}) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}) + << false; + + QTest::newRow("Vendor_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}) + << true; + QTest::newRow("Vendor_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << false; + QTest::newRow("Vendor_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << true; +} + +void tst_QShaderNodes::shouldVerifyFormatsCompatibilities() +{ + // GIVEN + QFETCH(QShaderFormat, reference); + QFETCH(QShaderFormat, tested); + + // WHEN + const auto supported = reference.supports(tested); + + // THEN + QFETCH(bool, expected); + QCOMPARE(supported, expected); +} + +void tst_QShaderNodes::shouldHaveDefaultPortState() +{ + // GIVEN + auto port = QShaderNodePort(); + + // THEN + QCOMPARE(port.direction, QShaderNodePort::Output); + QVERIFY(port.name.isEmpty()); +} + +void tst_QShaderNodes::shouldVerifyPortsEquality_data() +{ + QTest::addColumn<QShaderNodePort>("left"); + QTest::addColumn<QShaderNodePort>("right"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("Equals") << createPort(QShaderNodePort::Input, "foo") + << createPort(QShaderNodePort::Input, "foo") + << true; + QTest::newRow("Direction") << createPort(QShaderNodePort::Input, "foo") + << createPort(QShaderNodePort::Output, "foo") + << false; + QTest::newRow("Name") << createPort(QShaderNodePort::Input, "foo") + << createPort(QShaderNodePort::Input, "bar") + << false; +} + +void tst_QShaderNodes::shouldVerifyPortsEquality() +{ + // GIVEN + QFETCH(QShaderNodePort, left); + QFETCH(QShaderNodePort, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderNodes::shouldManipulateNodeMembers() +{ + // GIVEN + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"}); + const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + + auto node = QShaderNode(); + + // THEN (default state) + QCOMPARE(node.type(), QShaderNode::Invalid); + QVERIFY(node.uuid().isNull()); + QVERIFY(node.layers().isEmpty()); + QVERIFY(node.ports().isEmpty()); + QVERIFY(node.parameterNames().isEmpty()); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + const auto uuid = QUuid::createUuid(); + node.setUuid(uuid); + + // THEN + QCOMPARE(node.uuid(), uuid); + + // WHEN + node.setLayers({"foo", "bar"}); + + // THEN + QCOMPARE(node.layers(), QStringList({"foo", "bar"})); + + // WHEN + auto firstPort = QShaderNodePort(); + firstPort.direction = QShaderNodePort::Input; + firstPort.name = QStringLiteral("foo"); + node.addPort(firstPort); + + // THEN + QCOMPARE(node.type(), QShaderNode::Output); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), firstPort); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + auto secondPort = QShaderNodePort(); + secondPort.direction = QShaderNodePort::Output; + secondPort.name = QStringLiteral("bar"); + node.addPort(secondPort); + + // THEN + QCOMPARE(node.type(), QShaderNode::Function); + QCOMPARE(node.ports().size(), 2); + QCOMPARE(node.ports().at(0), firstPort); + QCOMPARE(node.ports().at(1), secondPort); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.removePort(firstPort); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.setParameter(QStringLiteral("baz"), 42); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + auto parameterNames = node.parameterNames(); + parameterNames.sort(); + QCOMPARE(parameterNames.size(), 1); + QCOMPARE(parameterNames.at(0), QStringLiteral("baz")); + QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42)); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.setParameter(QStringLiteral("bleh"), QStringLiteral("value")); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + parameterNames = node.parameterNames(); + parameterNames.sort(); + QCOMPARE(parameterNames.size(), 2); + QCOMPARE(parameterNames.at(0), QStringLiteral("baz")); + QCOMPARE(parameterNames.at(1), QStringLiteral("bleh")); + QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42)); + QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value"))); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.clearParameter(QStringLiteral("baz")); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + parameterNames = node.parameterNames(); + parameterNames.sort(); + QCOMPARE(parameterNames.size(), 1); + QCOMPARE(parameterNames.at(0), QStringLiteral("bleh")); + QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant()); + QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value"))); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.addRule(openGLES2, es2Rule); + node.addRule(openGL3, gl3Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + + // WHEN + node.removeRule(openGLES2); + + // THEN + QCOMPARE(node.availableFormats().size(), 1); + QCOMPARE(node.availableFormats().at(0), openGL3); + QCOMPARE(node.rule(openGL3), gl3Rule); + + // WHEN + node.addRule(openGLES2, es2Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGL3); + QCOMPARE(node.availableFormats().at(1), openGLES2); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + + // WHEN + node.addRule(openGL3, gl3bisRule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3bisRule); +} + +void tst_QShaderNodes::shouldHandleNodeRulesSupportAndOrder() +{ + // GIVEN + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + const auto openGL32 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2); + const auto openGL4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"}); + const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + const auto gl32Rule = QShaderNode::Rule(QByteArrayLiteral("gl32"), {"#pragma include gl32/foo.inc", "#pragma include gl32/bar.inc"}); + const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + + auto node = QShaderNode(); + + // WHEN + node.addRule(openGLES2, es2Rule); + node.addRule(openGL3, gl3Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + QCOMPARE(node.rule(openGL32), gl3Rule); + QCOMPARE(node.rule(openGL4), gl3Rule); + + // WHEN + node.addRule(openGL32, gl32Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 3); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.availableFormats().at(2), openGL32); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + QCOMPARE(node.rule(openGL32), gl32Rule); + QCOMPARE(node.rule(openGL4), gl32Rule); + + // WHEN + node.addRule(openGL3, gl3bisRule); + + // THEN + QCOMPARE(node.availableFormats().size(), 3); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL32); + QCOMPARE(node.availableFormats().at(2), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3bisRule); + QCOMPARE(node.rule(openGL32), gl3bisRule); + QCOMPARE(node.rule(openGL4), gl3bisRule); +} + +QTEST_MAIN(tst_QShaderNodes) + +#include "tst_qshadernodes.moc" diff --git a/tests/auto/gui/util/qshadernodesloader/qshadernodesloader.pro b/tests/auto/gui/util/qshadernodesloader/qshadernodesloader.pro new file mode 100644 index 0000000000..b9c26a2942 --- /dev/null +++ b/tests/auto/gui/util/qshadernodesloader/qshadernodesloader.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadernodesloader.cpp +TARGET = tst_qshadernodesloader diff --git a/tests/auto/gui/util/qshadernodesloader/tst_qshadernodesloader.cpp b/tests/auto/gui/util/qshadernodesloader/tst_qshadernodesloader.cpp new file mode 100644 index 0000000000..4782e40ed8 --- /dev/null +++ b/tests/auto/gui/util/qshadernodesloader/tst_qshadernodesloader.cpp @@ -0,0 +1,323 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtCore/qbuffer.h> + +#include <QtGui/private/qshadernodesloader_p.h> +#include <QtGui/private/qshaderlanguage_p.h> + +using QBufferPointer = QSharedPointer<QBuffer>; +Q_DECLARE_METATYPE(QBufferPointer); + +using NodeHash = QHash<QString, QShaderNode>; +Q_DECLARE_METATYPE(NodeHash); + +namespace +{ + QBufferPointer createBuffer(const QByteArray &data, QIODevice::OpenMode openMode = QIODevice::ReadOnly) + { + auto buffer = QBufferPointer::create(); + buffer->setData(data); + if (openMode != QIODevice::NotOpen) + buffer->open(openMode); + return buffer; + } + + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion, + const QStringList &extensions = QStringList(), + const QString &vendor = QString()) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + format.setExtensions(extensions); + format.setVendor(vendor); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports) + { + auto node = QShaderNode(); + for (const auto &port : ports) + node.addPort(port); + return node; + } +} + +class tst_QShaderNodesLoader : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateLoaderMembers(); + void shouldLoadFromJsonStream_data(); + void shouldLoadFromJsonStream(); +}; + +void tst_QShaderNodesLoader::shouldManipulateLoaderMembers() +{ + // GIVEN + auto loader = QShaderNodesLoader(); + + // THEN (default state) + QCOMPARE(loader.status(), QShaderNodesLoader::Null); + QVERIFY(!loader.device()); + QVERIFY(loader.nodes().isEmpty()); + + // WHEN + auto device1 = createBuffer(QByteArray("..........."), QIODevice::NotOpen); + loader.setDevice(device1.data()); + + // THEN + QCOMPARE(loader.status(), QShaderNodesLoader::Error); + QCOMPARE(loader.device(), device1.data()); + QVERIFY(loader.nodes().isEmpty()); + + // WHEN + auto device2 = createBuffer(QByteArray("..........."), QIODevice::ReadOnly); + loader.setDevice(device2.data()); + + // THEN + QCOMPARE(loader.status(), QShaderNodesLoader::Waiting); + QCOMPARE(loader.device(), device2.data()); + QVERIFY(loader.nodes().isEmpty()); +} + +void tst_QShaderNodesLoader::shouldLoadFromJsonStream_data() +{ + QTest::addColumn<QBufferPointer>("device"); + QTest::addColumn<NodeHash>("nodes"); + QTest::addColumn<QShaderNodesLoader::Status>("status"); + + QTest::newRow("empty") << createBuffer("", QIODevice::ReadOnly) << NodeHash() << QShaderNodesLoader::Error; + + const auto smallJson = "{" + " \"inputValue\": {" + " \"outputs\": [" + " \"value\"" + " ]," + " \"parameters\": {" + " \"name\": \"defaultName\"," + " \"qualifier\": {" + " \"type\": \"QShaderLanguage::StorageQualifier\"," + " \"value\": \"QShaderLanguage::Uniform\"" + " }," + " \"type\": {" + " \"type\": \"QShaderLanguage::VariableType\"," + " \"value\": \"QShaderLanguage::Vec3\"" + " }," + " \"defaultValue\": {" + " \"type\": \"float\"," + " \"value\": \"1.25\"" + " }" + " }," + " \"rules\": [" + " {" + " \"format\": {" + " \"api\": \"OpenGLES\"," + " \"major\": 2," + " \"minor\": 0" + " }," + " \"substitution\": \"highp vec3 $value = $name;\"," + " \"headerSnippets\": [ \"varying highp vec3 $name;\" ]" + " }," + " {" + " \"format\": {" + " \"api\": \"OpenGLCompatibilityProfile\"," + " \"major\": 2," + " \"minor\": 1" + " }," + " \"substitution\": \"vec3 $value = $name;\"," + " \"headerSnippets\": [ \"in vec3 $name;\" ]" + " }" + " ]" + " }," + " \"fragColor\": {" + " \"inputs\": [" + " \"fragColor\"" + " ]," + " \"rules\": [" + " {" + " \"format\": {" + " \"api\": \"OpenGLES\"," + " \"major\": 2," + " \"minor\": 0" + " }," + " \"substitution\": \"gl_fragColor = $fragColor;\"" + " }," + " {" + " \"format\": {" + " \"api\": \"OpenGLNoProfile\"," + " \"major\": 4," + " \"minor\": 0" + " }," + " \"substitution\": \"fragColor = $fragColor;\"," + " \"headerSnippets\": [ \"out vec4 fragColor;\" ]" + " }" + " ]" + " }," + " \"lightModel\": {" + " \"inputs\": [" + " \"baseColor\"," + " \"position\"," + " \"lightIntensity\"" + " ]," + " \"outputs\": [" + " \"outputColor\"" + " ]," + " \"rules\": [" + " {" + " \"format\": {" + " \"api\": \"OpenGLES\"," + " \"major\": 2," + " \"minor\": 0," + " \"extensions\": [ \"ext1\", \"ext2\" ]," + " \"vendor\": \"kdab\"" + " }," + " \"substitution\": \"highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);\"," + " \"headerSnippets\": [ \"#pragma include es2/lightmodel.frag.inc\" ]" + " }," + " {" + " \"format\": {" + " \"api\": \"OpenGLCoreProfile\"," + " \"major\": 3," + " \"minor\": 3" + " }," + " \"substitution\": \"vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);\"," + " \"headerSnippets\": [ \"#pragma include gl3/lightmodel.frag.inc\" ]" + " }" + " ]" + " }" + "}"; + + const auto smallProtos = [this]{ + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGLES2Extended = createFormat(QShaderFormat::OpenGLES, 2, 0, {"ext1", "ext2"}, "kdab"); + const auto openGL2 = createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 1); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 3); + const auto openGL4 = createFormat(QShaderFormat::OpenGLNoProfile, 4, 0); + + auto protos = NodeHash(); + + auto inputValue = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + inputValue.setParameter("name", "defaultName"); + inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); + inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3)); + inputValue.setParameter("defaultValue", QVariant(1.25f)); + inputValue.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;", + QByteArrayList() << "varying highp vec3 $name;")); + inputValue.addRule(openGL2, QShaderNode::Rule("vec3 $value = $name;", + QByteArrayList() << "in vec3 $name;")); + protos.insert("inputValue", inputValue); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL4, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + protos.insert(QStringLiteral("fragColor"), fragColor); + + auto lightModel = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightModel.addRule(openGLES2Extended, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightModel.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + protos.insert("lightModel", lightModel); + + return protos; + }(); + + QTest::newRow("NotOpen") << createBuffer(smallJson, QIODevice::NotOpen) << NodeHash() << QShaderNodesLoader::Error; + QTest::newRow("CorrectJSON") << createBuffer(smallJson) << smallProtos << QShaderNodesLoader::Ready; +} + +void tst_QShaderNodesLoader::shouldLoadFromJsonStream() +{ + // GIVEN + QFETCH(QBufferPointer, device); + + auto loader = QShaderNodesLoader(); + + // WHEN + loader.setDevice(device.data()); + loader.load(); + + // THEN + QFETCH(QShaderNodesLoader::Status, status); + QCOMPARE(loader.status(), status); + + QFETCH(NodeHash, nodes); + const auto sortedKeys = [](const NodeHash &nodes) { + auto res = nodes.keys(); + res.sort(); + return res; + }; + const auto sortedParameters = [](const QShaderNode &node) { + auto res = node.parameterNames(); + res.sort(); + return res; + }; + QCOMPARE(sortedKeys(loader.nodes()), sortedKeys(nodes)); + for (const auto &key : nodes.keys()) { + const auto actual = loader.nodes().value(key); + const auto expected = nodes.value(key); + + QVERIFY(actual.uuid().isNull()); + QCOMPARE(actual.ports(), expected.ports()); + QCOMPARE(sortedParameters(actual), sortedParameters(expected)); + for (const auto &name : expected.parameterNames()) { + QCOMPARE(actual.parameter(name), expected.parameter(name)); + } + QCOMPARE(actual.availableFormats(), expected.availableFormats()); + for (const auto &format : expected.availableFormats()) { + QCOMPARE(actual.rule(format), expected.rule(format)); + } + } +} + +QTEST_MAIN(tst_QShaderNodesLoader) + +#include "tst_qshadernodesloader.moc" diff --git a/tests/auto/gui/util/util.pro b/tests/auto/gui/util/util.pro index f2c4515dc2..940e892e5f 100644 --- a/tests/auto/gui/util/util.pro +++ b/tests/auto/gui/util/util.pro @@ -5,4 +5,9 @@ SUBDIRS= \ qintvalidator \ qregexpvalidator \ qregularexpressionvalidator \ + qshadergenerator \ + qshadergraph \ + qshadergraphloader \ + qshadernodes \ + qshadernodesloader \ |