24 if ( qgetenv(
"QGIS_CONTINUOUS_INTEGRATION_RUN" ) == QStringLiteral(
"true" ) )
41 mSourceFunction = function;
47 mControlPathPrefix = prefix;
52 mMapSettings = mapSettings;
59 mReportHeader =
"<h2>" + testName +
"</h2>\n";
60 mMarkdownReportHeader = QStringLiteral(
"### %1\n\n" ).arg( testName );
63 if ( !QFile::exists( baseDir ) )
65 qDebug() <<
"Control image path " << baseDir <<
" does not exist!";
69 QStringList subDirs = QDir( baseDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
71 if ( subDirs.isEmpty() )
76 QVector<QgsDartMeasurement> dartMeasurements;
79 QString diffImageFile;
81 QMap< QString, int > variantMismatchCount;
82 QMap< QString, int > variantSize;
84 for (
const QString &suffix : std::as_const( subDirs ) )
97 if ( !mRenderedImage.isNull() )
112 mReport += checker.
report(
false );
113 if ( subDirs.count() > 1 )
114 mMarkdownReport += QStringLiteral(
"* " ) + checker.
markdownReport(
false );
118 if ( !mResult && diffImageFile.isEmpty() )
120 diffImageFile = checker.mDiffImageFile;
124 variantMismatchCount.insert( suffix, checker.
mismatchCount() );
125 variantSize.insert( suffix, checker.
matchTarget() );
129 if ( !mResult && !mExpectFail && mIsCiRun )
131 const auto constDartMeasurements = dartMeasurements;
136 "If this is a rendering inconsistency, please add another control image folder, add an anomaly image or increase the color tolerance." );
139#if DUMP_BASE64_IMAGES
140 QFile fileSource( mRenderedImage );
141 fileSource.open( QIODevice::ReadOnly );
143 const QByteArray blob = fileSource.readAll();
144 const QByteArray encoded = blob.toBase64();
145 qDebug() <<
"Dumping rendered image " << mRenderedImage <<
" as base64\n";
146 qDebug() <<
"################################################################";
148 qDebug() <<
"################################################################";
149 qDebug() <<
"End dump";
153 if ( !mResult && !mExpectFail )
155 for (
auto it = variantMismatchCount.constBegin(); it != variantMismatchCount.constEnd(); it++ )
157 if ( subDirs.size() > 1 )
159 qDebug() << QStringLiteral(
"Variant %1: %2/%3 pixels mismatched (%4 allowed)" ).arg( it.key() ).arg( it.value() ).arg( variantSize.value( it.key() ) ).arg( mismatchCount );
163 qDebug() << QStringLiteral(
"%1/%2 pixels mismatched (%4 allowed)" ).arg( it.value() ).arg( variantSize.value( it.key() ) ).arg( mismatchCount );
167 if ( !reportDir.exists() )
169 if ( !QDir().mkpath( reportDir.path() ) )
171 qDebug() <<
"!!!!! cannot create " << reportDir.path();
174 if ( QFile::exists( mRenderedImage ) )
176 QFileInfo fi( mRenderedImage );
177 const QString destPath = reportDir.filePath( fi.fileName() );
178 if ( QFile::exists( destPath ) )
179 QFile::remove( destPath );
181 if ( !QFile::copy( mRenderedImage, destPath ) )
183 qDebug() <<
"!!!!! could not copy " << mRenderedImage <<
" to " << destPath;
187 if ( !diffImageFile.isEmpty() && QFile::exists( diffImageFile ) )
189 QFileInfo fi( diffImageFile );
190 const QString destPath = reportDir.filePath( fi.fileName() );
191 if ( QFile::exists( destPath ) )
192 QFile::remove( destPath );
194 if ( !QFile::copy( diffImageFile, destPath ) )
196 qDebug() <<
"!!!!! could not copy " << diffImageFile <<
" to " << destPath;
209 QString
report = mReportHeader;
210 if ( mSourceLine >= 0 )
212 const QString githubSha = qgetenv(
"GITHUB_SHA" );
213 if ( !githubSha.isEmpty() )
215 const QString githubBlobUrl = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2#L%3" ).arg(
216 githubSha, mSourceFile ).arg( mSourceLine );
217 report += QStringLiteral(
"<b style=\"color: red\">Test failed in %1 at <a href=\"%2\">%3:%4</a></b>\n" ).arg(
220 mSourceFile ).arg( mSourceLine );
224 report += QStringLiteral(
"<b style=\"color: red\">Test failed in %1 at %2:%3</b>\n" ).arg( mSourceFunction, mSourceFile ).arg( mSourceLine );
237 QString
report = mMarkdownReportHeader;
239 if ( mSourceLine >= 0 )
241 const QString githubSha = qgetenv(
"GITHUB_SHA" );
243 if ( !githubSha.isEmpty() )
245 fileLink = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2#L%3" ).arg(
246 githubSha, mSourceFile ).arg( mSourceLine );
252 report += QStringLiteral(
"**Test failed at %1 at [%2:%3](%4)**\n\n" ).arg( mSourceFunction, mSourceFile ).arg( mSourceLine ).arg( fileLink );
254 report += mMarkdownReport;
260 QString myDataDir( TEST_DATA_DIR );
261 QString myControlImageDir = myDataDir + QDir::separator() +
"control_images" +
262 QDir::separator() + mControlPathPrefix + QDir::separator() + mControlName + QDir::separator();
263 return myControlImageDir;
272QgsLayoutChecker::QgsLayoutChecker(
const QString &testName,
QgsLayout *layout )
273 : mTestName( testName )
276 , mDotsPerMeter( 96 / 25.4 * 1000 )
282bool QgsLayoutChecker::testLayout( QString &checkedReport,
int page,
int pixelDiff,
bool createReferenceImage )
289 setControlName(
"expected_" + mTestName );
292 if ( createReferenceImage )
298 QImage _outputImage( mSize, QImage::Format_RGB32 );
299 _outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
300 _outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
301 QPainter _p( &_outputImage );
303 _exporter.renderPage( &_p, page );
306 if ( ! QDir( controlImagePath() ).exists() )
308 QDir().mkdir( controlImagePath() );
310 _outputImage.save( controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png",
"PNG" );
311 qDebug( ) <<
"Reference image saved to : " + controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png";
315 QImage outputImage( mSize, QImage::Format_RGB32 );
316 outputImage.setDotsPerMeterX( mDotsPerMeter );
317 outputImage.setDotsPerMeterY( mDotsPerMeter );
318 drawBackground( &outputImage );
319 QPainter p( &outputImage );
321 exporter.renderPage( &p, page );
324 QString renderedFilePath = QDir::tempPath() +
'/' + QFileInfo( mTestName ).baseName() +
"_rendered.png";
325 if ( QFile::exists( renderedFilePath ) )
326 QFile::remove( renderedFilePath );
328 outputImage.save( renderedFilePath,
"PNG" );
330 setRenderedImage( renderedFilePath );
332 bool testResult = runTest( mTestName, pixelDiff );
334 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.
The QgsMapSettings class contains configuration for rendering of the map.
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.
This is a helper class for unit tests that need to write an image and compare it to an expected resul...
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().