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 for (
const QString &suffix : std::as_const( subDirs ) )
83 if ( subDirs.count() > 1 )
85 qDebug() <<
"Checking subdir " << suffix;
98 if ( !mRenderedImage.isNull() )
113 mReport += checker.
report(
false );
114 if ( subDirs.count() > 1 )
115 mMarkdownReport += QStringLiteral(
"* " ) + checker.
markdownReport(
false );
119 if ( !mResult && diffImageFile.isEmpty() )
121 diffImageFile = checker.mDiffImageFile;
125 if ( !mResult && !mExpectFail && mIsCiRun )
127 const auto constDartMeasurements = dartMeasurements;
132 "If this is a rendering inconsistency, please add another control image folder, add an anomaly image or increase the color tolerance." );
135#if DUMP_BASE64_IMAGES
136 QFile fileSource( mRenderedImage );
137 fileSource.open( QIODevice::ReadOnly );
139 const QByteArray blob = fileSource.readAll();
140 const QByteArray encoded = blob.toBase64();
141 qDebug() <<
"Dumping rendered image " << mRenderedImage <<
" as base64\n";
142 qDebug() <<
"################################################################";
144 qDebug() <<
"################################################################";
145 qDebug() <<
"End dump";
149 if ( !mResult && !mExpectFail )
152 if ( !reportDir.exists() )
154 if ( !QDir().mkpath( reportDir.path() ) )
156 qDebug() <<
"!!!!! cannot create " << reportDir.path();
159 if ( QFile::exists( mRenderedImage ) )
161 QFileInfo fi( mRenderedImage );
162 const QString destPath = reportDir.filePath( fi.fileName() );
163 if ( QFile::exists( destPath ) )
164 QFile::remove( destPath );
166 if ( !QFile::copy( mRenderedImage, destPath ) )
168 qDebug() <<
"!!!!! could not copy " << mRenderedImage <<
" to " << destPath;
172 if ( !diffImageFile.isEmpty() && QFile::exists( diffImageFile ) )
174 QFileInfo fi( diffImageFile );
175 const QString destPath = reportDir.filePath( fi.fileName() );
176 if ( QFile::exists( destPath ) )
177 QFile::remove( destPath );
179 if ( !QFile::copy( diffImageFile, destPath ) )
181 qDebug() <<
"!!!!! could not copy " << diffImageFile <<
" to " << destPath;
194 QString
report = mReportHeader;
195 if ( mSourceLine >= 0 )
197 const QString githubSha = qgetenv(
"GITHUB_SHA" );
198 if ( !githubSha.isEmpty() )
200 const QString githubBlobUrl = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2#L%3" ).arg(
201 githubSha, mSourceFile ).arg( mSourceLine );
202 report += QStringLiteral(
"<b style=\"color: red\">Test failed in %1 at <a href=\"%2\">%3:%4</a></b>\n" ).arg(
205 mSourceFile ).arg( mSourceLine );
209 report += QStringLiteral(
"<b style=\"color: red\">Test failed in %1 at %2:%3</b>\n" ).arg( mSourceFunction, mSourceFile ).arg( mSourceLine );
222 QString
report = mMarkdownReportHeader;
224 if ( mSourceLine >= 0 )
226 const QString githubSha = qgetenv(
"GITHUB_SHA" );
228 if ( !githubSha.isEmpty() )
230 fileLink = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2#L%3" ).arg(
231 githubSha, mSourceFile ).arg( mSourceLine );
237 report += QStringLiteral(
"**Test failed at %1 at [%2:%3](%4)**\n\n" ).arg( mSourceFunction, mSourceFile ).arg( mSourceLine ).arg( fileLink );
239 report += mMarkdownReport;
245 QString myDataDir( TEST_DATA_DIR );
246 QString myControlImageDir = myDataDir + QDir::separator() +
"control_images" +
247 QDir::separator() + mControlPathPrefix + QDir::separator() + mControlName + QDir::separator();
248 return myControlImageDir;
257QgsLayoutChecker::QgsLayoutChecker(
const QString &testName,
QgsLayout *layout )
258 : mTestName( testName )
261 , mDotsPerMeter( 96 / 25.4 * 1000 )
267bool QgsLayoutChecker::testLayout( QString &checkedReport,
int page,
int pixelDiff,
bool createReferenceImage )
274 setControlName(
"expected_" + mTestName );
277 if ( createReferenceImage )
283 QImage _outputImage( mSize, QImage::Format_RGB32 );
284 _outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
285 _outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
286 QPainter _p( &_outputImage );
288 _exporter.renderPage( &_p, page );
291 if ( ! QDir( controlImagePath() ).exists() )
293 QDir().mkdir( controlImagePath() );
295 _outputImage.save( controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png",
"PNG" );
296 qDebug( ) <<
"Reference image saved to : " + controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png";
300 QImage outputImage( mSize, QImage::Format_RGB32 );
301 outputImage.setDotsPerMeterX( mDotsPerMeter );
302 outputImage.setDotsPerMeterY( mDotsPerMeter );
303 drawBackground( &outputImage );
304 QPainter p( &outputImage );
306 exporter.renderPage( &p, page );
309 QString renderedFilePath = QDir::tempPath() +
'/' + QFileInfo( mTestName ).baseName() +
"_rendered.png";
310 if ( QFile::exists( renderedFilePath ) )
311 QFile::remove( renderedFilePath );
313 outputImage.save( renderedFilePath,
"PNG" );
315 setRenderedImage( renderedFilePath );
317 bool testResult = runTest( mTestName, pixelDiff );
319 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.
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.
@ 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.
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().