27 mControlPathPrefix = prefix;
32 mMapSettings = mapSettings;
37 bool successful =
false;
41 QStringList subDirs = QDir( baseDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
43 if ( subDirs.isEmpty() )
48 QVector<QgsDartMeasurement> dartMeasurements;
50 for (
const QString &suffix : std::as_const( subDirs ) )
52 if ( subDirs.count() > 1 )
54 qDebug() <<
"Checking subdir " << suffix;
66 if ( !mRenderedImage.isNull() )
69 result = checker.
compareImages( testName, mismatchCount, mRenderedImage );
73 result = checker.
runTest( testName, mismatchCount );
81 mReport += checker.
report();
86 const auto constDartMeasurements = dartMeasurements;
91 "If this is a rendering inconsistency, please add another control image folder, add an anomaly image or increase the color tolerance." );
100 QString myDataDir( TEST_DATA_DIR );
101 QString myControlImageDir = myDataDir + QDir::separator() +
"control_images" +
102 QDir::separator() + mControlPathPrefix + QDir::separator() + mControlName + QDir::separator();
103 return myControlImageDir;
112 QgsLayoutChecker::QgsLayoutChecker(
const QString &testName,
QgsLayout *layout )
113 : mTestName( testName )
116 , mDotsPerMeter( 96 / 25.4 * 1000 )
122 bool QgsLayoutChecker::testLayout( QString &checkedReport,
int page,
int pixelDiff,
bool createReferenceImage )
132 setControlName(
"expected_" + mTestName );
135 if ( createReferenceImage )
141 QImage _outputImage( mSize, QImage::Format_RGB32 );
142 _outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
143 _outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
144 QPainter _p( &_outputImage );
146 _exporter.renderPage( &_p, page );
149 if ( ! QDir( controlImagePath() ).exists() )
151 QDir().mkdir( controlImagePath() );
153 _outputImage.save( controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png",
"PNG" );
154 qDebug( ) <<
"Reference image saved to : " + controlImagePath() + QDir::separator() +
"expected_" + mTestName +
".png";
158 QImage outputImage( mSize, QImage::Format_RGB32 );
159 outputImage.setDotsPerMeterX( mDotsPerMeter );
160 outputImage.setDotsPerMeterY( mDotsPerMeter );
161 drawBackground( &outputImage );
162 QPainter p( &outputImage );
164 exporter.renderPage( &p, page );
167 QString renderedFilePath = QDir::tempPath() +
'/' + QFileInfo( mTestName ).baseName() +
"_rendered.png";
168 outputImage.save( renderedFilePath,
"PNG" );
170 setRenderedImage( renderedFilePath );
172 bool testResult = runTest( mTestName, pixelDiff );
174 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.
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).
void setMapSettings(const QgsMapSettings &mapSettings)
void setControlPathSuffix(const QString &name)
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.
void setRenderedImage(const QString &imageFileName)
Sets the file name of the rendered image generated by the test.
bool runTest(const QString &testName, unsigned int mismatchCount=0)
Test using renderer to generate the image to be compared.
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().
QString report()
Returns the HTML report describing the results of the test run.
bool compareImages(const QString &testName, unsigned int mismatchCount=0, const QString &renderedImageFile=QString())
Test using two arbitrary images (map renderer will not be used)