27 if ( qgetenv(
"QGIS_CONTINUOUS_INTEGRATION_RUN" ) == QStringLiteral(
"true" ) )
44 mSourceFunction = function;
50 mControlPathPrefix = prefix;
55 mMapSettings = mapSettings;
62 mReportHeader =
"<h2>" + testName +
"</h2>\n";
63 mMarkdownReportHeader = QStringLiteral(
"### %1\n\n" ).arg( testName );
66 if ( !QFile::exists( baseDir ) )
68 qDebug() <<
"Control image path " << baseDir <<
" does not exist!";
72 QStringList subDirs = QDir( baseDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
74 if ( subDirs.isEmpty() )
79 QVector<QgsDartMeasurement> dartMeasurements;
82 QString diffImageFile;
84 QMap< QString, int > variantMismatchCount;
85 QMap< QString, int > variantSize;
87 for (
const QString &suffix : std::as_const( subDirs ) )
100 if ( !mRenderedImage.isNull() )
115 mReport += checker.
report(
false );
116 if ( subDirs.count() > 1 )
117 mMarkdownReport += QStringLiteral(
"* " ) + checker.
markdownReport(
false );
121 if ( !mResult && diffImageFile.isEmpty() )
123 diffImageFile = checker.mDiffImageFile;
127 variantMismatchCount.insert( suffix, checker.
mismatchCount() );
128 variantSize.insert( suffix, checker.
matchTarget() );
132 if ( !mResult && !mExpectFail && mIsCiRun )
134 const auto constDartMeasurements = dartMeasurements;
139 "If this is a rendering inconsistency, please add another control image folder, add an anomaly image or increase the color tolerance." );
142#if DUMP_BASE64_IMAGES
143 QFile fileSource( mRenderedImage );
144 fileSource.open( QIODevice::ReadOnly );
146 const QByteArray blob = fileSource.readAll();
147 const QByteArray encoded = blob.toBase64();
148 qDebug() <<
"Dumping rendered image " << mRenderedImage <<
" as base64\n";
149 qDebug() <<
"################################################################";
151 qDebug() <<
"################################################################";
152 qDebug() <<
"End dump";
156 if ( !mResult && !mExpectFail )
158 for (
auto it = variantMismatchCount.constBegin(); it != variantMismatchCount.constEnd(); it++ )
160 if ( subDirs.size() > 1 )
162 qDebug() << QStringLiteral(
"Variant %1: %2/%3 pixels mismatched (%4 allowed)" ).arg( it.key() ).arg( it.value() ).arg( variantSize.value( it.key() ) ).arg( mismatchCount );
166 qDebug() << QStringLiteral(
"%1/%2 pixels mismatched (%4 allowed)" ).arg( it.value() ).arg( variantSize.value( it.key() ) ).arg( mismatchCount );
170 if ( !reportDir.exists() )
172 if ( !QDir().mkpath( reportDir.path() ) )
174 qDebug() <<
"!!!!! cannot create " << reportDir.path();
177 if ( QFile::exists( mRenderedImage ) )
179 QFileInfo fi( mRenderedImage );
180 const QString destPath = reportDir.filePath( fi.fileName() );
181 if ( QFile::exists( destPath ) )
182 QFile::remove( destPath );
184 if ( !QFile::copy( mRenderedImage, destPath ) )
186 qDebug() <<
"!!!!! could not copy " << mRenderedImage <<
" to " << destPath;
190 if ( !diffImageFile.isEmpty() && QFile::exists( diffImageFile ) )
192 QFileInfo fi( diffImageFile );
193 const QString destPath = reportDir.filePath( fi.fileName() );
194 if ( QFile::exists( destPath ) )
195 QFile::remove( destPath );
197 if ( !QFile::copy( diffImageFile, destPath ) )
199 qDebug() <<
"!!!!! could not copy " << diffImageFile <<
" to " << destPath;
212 QString
report = mReportHeader;
213 if ( mSourceLine >= 0 )
215 const QString githubSha = qgetenv(
"GITHUB_SHA" );
216 if ( !githubSha.isEmpty() )
218 const QString githubBlobUrl = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2#L%3" ).arg(
219 githubSha, mSourceFile ).arg( mSourceLine );
220 report += QStringLiteral(
"<b style=\"color: red\">Test failed in %1 at <a href=\"%2\">%3:%4</a></b>\n" ).arg(
223 mSourceFile ).arg( mSourceLine );
227 report += QStringLiteral(
"<b style=\"color: red\">Test failed in %1 at %2:%3</b>\n" ).arg( mSourceFunction, mSourceFile ).arg( mSourceLine );
240 QString
report = mMarkdownReportHeader;
242 if ( mSourceLine >= 0 )
244 const QString githubSha = qgetenv(
"GITHUB_SHA" );
246 if ( !githubSha.isEmpty() )
248 fileLink = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2#L%3" ).arg(
249 githubSha, mSourceFile ).arg( mSourceLine );
255 report += QStringLiteral(
"**Test failed at %1 at [%2:%3](%4)**\n\n" ).arg( mSourceFunction, mSourceFile ).arg( mSourceLine ).arg( fileLink );
257 report += mMarkdownReport;
263 QString myDataDir( TEST_DATA_DIR );
264 QString myControlImageDir = myDataDir + QDir::separator() +
"control_images" +
265 QDir::separator() + mControlPathPrefix + QDir::separator() + mControlName + QDir::separator();
266 return myControlImageDir;
275QgsLayoutChecker::QgsLayoutChecker(
const QString &testName,
QgsLayout *layout )
276 : mTestName( testName )
279 , mDotsPerMeter( 96 / 25.4 * 1000 )
285bool QgsLayoutChecker::testLayout( QString &checkedReport,
int page,
int pixelDiff,
bool createReferenceImage )
292 setControlName(
"expected_" + mTestName );
295 if ( createReferenceImage )
301 QImage _outputImage( mSize, QImage::Format_RGB32 );
302 _outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
303 _outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
304 QPainter _p( &_outputImage );
306 _exporter.renderPage( &_p, page );
309 if ( ! QDir( controlImagePath() ).exists() )
311 QDir().mkdir( controlImagePath() );
313 _outputImage.save( controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png",
"PNG" );
314 qDebug( ) <<
"Reference image saved to : " + controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png";
318 QImage outputImage( mSize, QImage::Format_RGB32 );
319 outputImage.setDotsPerMeterX( mDotsPerMeter );
320 outputImage.setDotsPerMeterY( mDotsPerMeter );
321 drawBackground( &outputImage );
322 QPainter p( &outputImage );
324 exporter.renderPage( &p, page );
327 QString renderedFilePath = QDir::tempPath() +
'/' + QFileInfo( mTestName ).baseName() +
"_rendered.png";
328 if ( QFile::exists( renderedFilePath ) )
329 QFile::remove( renderedFilePath );
331 outputImage.save( renderedFilePath,
"PNG" );
333 setRenderedImage( renderedFilePath );
335 bool testResult = runTest( mTestName, pixelDiff );
337 checkedReport += report();
Emits dart measurements for display in CDash reports.
Handles rendering and exports of layouts to various formats.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Contains configuration for rendering maps.
bool runTest(const QString &testName, unsigned int mismatchCount=0)
Test using renderer to generate the image to be compared.
void setControlName(const QString &name)
Base directory name for the control image (with control image path suffixed) the path to the image wi...
QString controlImagePath() const
Returns the path to the control images.
void setControlPathPrefix(const QString &prefix)
Sets the path prefix where the control images are kept.
void setColorTolerance(unsigned int colorTolerance)
Set tolerance for color components used by runTest() Default value is 0.
void setMapSettings(const QgsMapSettings &mapSettings)
Set the map settings to use to render the image.
void setFileFunctionLine(const QString &file, const QString &function, int line)
Sets the source file, function and line from where the test originates.
QString report() const
Returns a HTML report for this test.
QgsMultiRenderChecker()
Constructor for QgsMultiRenderChecker.
QString markdownReport() const
Returns a markdown report for this test.
Helper class for unit tests that need to write an image and compare it to an expected result or rende...
void setControlName(const QString &name)
Sets the base directory name for the control image (with control image path suffixed).
static QDir testReportDir()
Returns the directory to use for generating a test report.
static QString sourcePath()
Returns the path to the QGIS source code.
QString markdownReport(bool ignoreSuccess=true) const
Returns the markdown report describing the results of the test run.
unsigned int matchTarget() const
Returns the total number of pixels in the control image.
void setMapSettings(const QgsMapSettings &mapSettings)
void setControlPathSuffix(const QString &name)
bool runTest(const QString &testName, unsigned int mismatchCount=0, QgsRenderChecker::Flags flags=QgsRenderChecker::Flags())
Test using renderer to generate the image to be compared.
@ Silent
Don't output non-critical messages to console.
@ AvoidExportingRenderedImage
Avoids exporting rendered images to reports.
QString renderedImage() const
Returns the path of the rendered image generated by the test.
QVector< QgsDartMeasurement > dartMeasurements() const
Gets access to buffered dash messages.
void setControlPathPrefix(const QString &name)
Sets the path prefix where the control images are kept.
QString report(bool ignoreSuccess=true) const
Returns the HTML report describing the results of the test run.
bool compareImages(const QString &testName, unsigned int mismatchCount=0, const QString &renderedImageFile=QString(), QgsRenderChecker::Flags flags=QgsRenderChecker::Flags())
Test using two arbitrary images (map renderer will not be used).
void setRenderedImage(const QString &imageFileName)
Sets the file name of the rendered image generated by the test.
void setSizeTolerance(int xTolerance, int yTolerance)
Sets the largest allowable difference in size between the rendered and the expected image.
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
unsigned int mismatchCount() const
Returns the number of pixels which did not match the control image.
void setExpectFail(bool expectFail)
Sets whether the comparison is expected to fail.
void setColorTolerance(unsigned int colorTolerance)
Set tolerance for color components used by runTest() and compareImages().