diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2024-11-15 12:05:30 +0800 |
|---|---|---|
| committer | Mitch Curtis <mitch.curtis@qt.io> | 2024-11-15 16:21:32 +0800 |
| commit | f9fea69596ae22e512b94a5714af68c6fbb667c6 (patch) | |
| tree | 8c6b2a48725f841b03d52ad20a5fcec65a5e4eb8 /src/quicktestutils/quick/visualtestutils.cpp | |
| parent | a5412bece857c5e2742a9cc2dc2e65539416a17b (diff) | |
QQuickVisualTestUtils::compareImages: save images on all failures
I didn't save images on failure for size and format mismatches because
I thought it wouldn't be useful, but it turns out it is.
Move the image-saving code into a lambda and call it for all failures.
Amends 426555aa581ce688538975176cf1c31f6ffddb24.
Change-Id: I6b2ccd8fd0a2559c88a8c525e6cd4bad1946e651
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
Diffstat (limited to 'src/quicktestutils/quick/visualtestutils.cpp')
| -rw-r--r-- | src/quicktestutils/quick/visualtestutils.cpp | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/src/quicktestutils/quick/visualtestutils.cpp b/src/quicktestutils/quick/visualtestutils.cpp index f708b815e2..9ce3a6df2a 100644 --- a/src/quicktestutils/quick/visualtestutils.cpp +++ b/src/quicktestutils/quick/visualtestutils.cpp @@ -141,17 +141,50 @@ bool QQuickVisualTestUtils::isDelegateVisible(QQuickItem *item) */ bool QQuickVisualTestUtils::compareImages(const QImage &ia, const QImage &ib, QString *errorMessage) { + auto maybeSaveImagesForDebugging = [ia, ib](QDebug &debug) { + if (!lcCompareImages().isDebugEnabled()) + return; + + const QDir saveDir(QCoreApplication::applicationDirPath()); + QString imageFileNamePrefix = QString::fromUtf8("%1-%2").arg( + QString::fromUtf8(QTest::currentAppName()), + QString::fromUtf8(QTest::currentTestFunction())); + if (QTest::currentDataTag()) + imageFileNamePrefix.append(QStringLiteral("-") + QString::fromUtf8(QTest::currentDataTag())); + + const QString actualImageFilePath = saveDir.filePath(imageFileNamePrefix + QLatin1String("-actual.png")); + const bool actualImageSaved = ia.save(actualImageFilePath); + if (!actualImageSaved) + qWarning() << "Failed to save actual image to" << actualImageFilePath; + + const QString expectedImageFilePath = saveDir.filePath(imageFileNamePrefix + QLatin1String("-expected.png")); + const bool expectedImageSaved = ib.save(expectedImageFilePath); + if (!expectedImageSaved) + qWarning() << "Failed to save expected image to" << expectedImageFilePath; + + if (actualImageSaved && expectedImageSaved) { + debug.noquote() << "\nActual image saved to:" << actualImageFilePath; + debug << "\nExpected image saved to:" << expectedImageFilePath; + } + }; + if (ia.size() != ib.size()) { - QDebug(errorMessage) << "Images are of different size:" << ia.size() << ib.size() + QDebug debug(errorMessage); + debug << "Images are of different size:" << ia.size() << ib.size() << "DPR:" << ia.devicePixelRatio() << ib.devicePixelRatio(); + maybeSaveImagesForDebugging(debug); return false; } if (ia.format() != ib.format()) { - QDebug(errorMessage) << "Images are of different formats:" << ia.format() << ib.format(); + QDebug debug(errorMessage); + debug << "Images are of different formats:" << ia.format() << ib.format(); + maybeSaveImagesForDebugging(debug); return false; } if (ia.depth() != 32) { - QDebug(errorMessage) << "This function only supports bit depths of 32 - depth of images is:" << ia.depth(); + QDebug debug(errorMessage); + debug << "This function only supports bit depths of 32 - depth of images is:" << ia.depth(); + maybeSaveImagesForDebugging(debug); return false; } @@ -173,30 +206,7 @@ bool QQuickVisualTestUtils::compareImages(const QImage &ia, const QImage &ib, QS QDebug debug(errorMessage); debug << "Mismatch at:" << x << y << ':' << Qt::hex << Qt::showbase << a << b; - - if (lcCompareImages().isDebugEnabled()) { - const QDir saveDir(QCoreApplication::applicationDirPath()); - QString imageFileNamePrefix = QString::fromUtf8("%1-%2").arg( - QString::fromUtf8(QTest::currentAppName()), - QString::fromUtf8(QTest::currentTestFunction())); - if (QTest::currentDataTag()) - imageFileNamePrefix.append(QStringLiteral("-") + QString::fromUtf8(QTest::currentDataTag())); - - const QString actualImageFilePath = saveDir.filePath(imageFileNamePrefix + QLatin1String("-actual.png")); - const bool actualImageSaved = ia.save(actualImageFilePath); - if (!actualImageSaved) - qWarning() << "Failed to save actual image to" << actualImageFilePath; - - const QString expectedImageFilePath = saveDir.filePath(imageFileNamePrefix + QLatin1String("-expected.png")); - const bool expectedImageSaved = ib.save(expectedImageFilePath); - if (!expectedImageSaved) - qWarning() << "Failed to save expected image to" << expectedImageFilePath; - - if (actualImageSaved && expectedImageSaved) { - debug.noquote() << "\nActual image saved to:" << actualImageFilePath; - debug << "\nExpected image saved to:" << expectedImageFilePath; - } - } + maybeSaveImagesForDebugging(debug); return false; } } |
