23 if ( qgetenv(
"QGIS_CONTINUOUS_INTEGRATION_RUN" ) == QStringLiteral(
"true" ) )
34 mControlPathPrefix = prefix;
39 mMapSettings = mapSettings;
46 mReport +=
"<h2>" + testName +
"</h2>\n";
49 if ( !QFile::exists( baseDir ) )
51 qDebug() <<
"Control image path " << baseDir <<
" does not exist!";
55 QStringList subDirs = QDir( baseDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
57 if ( subDirs.isEmpty() )
62 QVector<QgsDartMeasurement> dartMeasurements;
65 QString diffImageFile;
67 for (
const QString &suffix : std::as_const( subDirs ) )
69 if ( subDirs.count() > 1 )
71 qDebug() <<
"Checking subdir " << suffix;
83 if ( !mRenderedImage.isNull() )
98 mReport += checker.
report(
false );
100 if ( !mResult && diffImageFile.isEmpty() )
102 diffImageFile = checker.mDiffImageFile;
106 if ( !mResult && !mExpectFail && mIsCiRun )
108 const auto constDartMeasurements = dartMeasurements;
113 "If this is a rendering inconsistency, please add another control image folder, add an anomaly image or increase the color tolerance." );
116#if DUMP_BASE64_IMAGES
117 QFile fileSource( mRenderedImage );
118 fileSource.open( QIODevice::ReadOnly );
120 const QByteArray blob = fileSource.readAll();
121 const QByteArray encoded = blob.toBase64();
122 qDebug() <<
"Dumping rendered image " << mRenderedImage <<
" as base64\n";
123 qDebug() <<
"################################################################";
125 qDebug() <<
"################################################################";
126 qDebug() <<
"End dump";
130 if ( !mResult && !mExpectFail )
133 if ( !reportDir.exists() )
135 if ( !QDir().mkpath( reportDir.path() ) )
137 qDebug() <<
"!!!!! cannot create " << reportDir.path();
140 if ( QFile::exists( mRenderedImage ) )
142 QFileInfo fi( mRenderedImage );
143 const QString destPath = reportDir.filePath( fi.fileName() );
144 if ( QFile::exists( destPath ) )
145 QFile::remove( destPath );
147 if ( !QFile::copy( mRenderedImage, destPath ) )
149 qDebug() <<
"!!!!! could not copy " << mRenderedImage <<
" to " << destPath;
153 if ( !diffImageFile.isEmpty() && QFile::exists( diffImageFile ) )
155 QFileInfo fi( diffImageFile );
156 const QString destPath = reportDir.filePath( fi.fileName() );
157 if ( QFile::exists( destPath ) )
158 QFile::remove( destPath );
160 if ( !QFile::copy( diffImageFile, destPath ) )
162 qDebug() <<
"!!!!! could not copy " << diffImageFile <<
" to " << destPath;
172 return !mResult ? mReport : QString();
177 QString myDataDir( TEST_DATA_DIR );
178 QString myControlImageDir = myDataDir + QDir::separator() +
"control_images" +
179 QDir::separator() + mControlPathPrefix + QDir::separator() + mControlName + QDir::separator();
180 return myControlImageDir;
189QgsLayoutChecker::QgsLayoutChecker(
const QString &testName,
QgsLayout *layout )
190 : mTestName( testName )
193 , mDotsPerMeter( 96 / 25.4 * 1000 )
199bool QgsLayoutChecker::testLayout( QString &checkedReport,
int page,
int pixelDiff,
bool createReferenceImage )
209 setControlName(
"expected_" + mTestName );
212 if ( createReferenceImage )
218 QImage _outputImage( mSize, QImage::Format_RGB32 );
219 _outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
220 _outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
221 QPainter _p( &_outputImage );
223 _exporter.renderPage( &_p, page );
226 if ( ! QDir( controlImagePath() ).exists() )
228 QDir().mkdir( controlImagePath() );
230 _outputImage.save( controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png",
"PNG" );
231 qDebug( ) <<
"Reference image saved to : " + controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png";
235 QImage outputImage( mSize, QImage::Format_RGB32 );
236 outputImage.setDotsPerMeterX( mDotsPerMeter );
237 outputImage.setDotsPerMeterY( mDotsPerMeter );
238 drawBackground( &outputImage );
239 QPainter p( &outputImage );
241 exporter.renderPage( &p, page );
244 QString renderedFilePath = QDir::tempPath() +
'/' + QFileInfo( mTestName ).baseName() +
"_rendered.png";
245 outputImage.save( renderedFilePath,
"PNG" );
247 setRenderedImage( renderedFilePath );
249 bool testResult = runTest( mTestName, pixelDiff );
251 checkedReport += report();
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)
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.
QString report() const
Returns a report for this test.
QgsMultiRenderChecker()
Constructor for QgsMultiRenderChecker.
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.
void setMapSettings(const QgsMapSettings &mapSettings)
void setControlPathSuffix(const QString &name)
bool runTest(const QString &testName, unsigned int mismatchCount=0, QgsRenderChecker::Flags flags=QgsRenderChecker::Flags())
Render checker flags.
@ 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 setColorTolerance(unsigned int colorTolerance)
Set tolerance for color components used by runTest() and compareImages().