Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[tests] Add method for setting size tolerance to render checker,
add some size tolerance to legend renderer test
  • Loading branch information
nyalldawson committed Jul 10, 2015
1 parent 78c2db6 commit 405688e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsrenderchecker.sip
Expand Up @@ -62,6 +62,14 @@ class QgsRenderChecker
* @note added in 2.1
*/
void setColorTolerance( unsigned int theColorTolerance );

/** Sets the largest allowable difference in size between the rendered and the expected image.
* @param xTolerance x tolerance in pixels
* @param yTolerance y tolerance in pixels
* @note added in QGIS 2.12
*/
void setSizeTolerance( int xTolerance, int yTolerance );

/**
* Test using renderer to generate the image to be compared.
* @param theTestName - to be used as the basis for writing a file to
Expand Down
34 changes: 25 additions & 9 deletions src/core/qgsrenderchecker.cpp
Expand Up @@ -37,6 +37,8 @@ QgsRenderChecker::QgsRenderChecker()
, mExpectedImageFile( "" )
, mMismatchCount( 0 )
, mColorTolerance( 0 )
, mMaxSizeDifferenceX( 0 )
, mMaxSizeDifferenceY( 0 )
, mElapsedTimeTarget( 0 )
, mBufferDashMessages( false )
{
Expand Down Expand Up @@ -374,30 +376,44 @@ bool QgsRenderChecker::compareImages( QString theTestName,

if ( mMatchTarget != myPixelCount )
{
qDebug( "Test image and result image for %s are different dimensions - FAILING!", theTestName.toLocal8Bit().constData() );
mReport += "<tr><td colspan=3>";
mReport += "<font color=red>Expected image and result image for " + theTestName + " are different dimensions - FAILING!</font>";
mReport += "</td></tr>";
mReport += myImagesString;
delete maskImage;
return false;
qDebug( "Test image and result image for %s are different dimensions", theTestName.toLocal8Bit().constData() );

if ( qAbs( myExpectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX ||
qAbs( myExpectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
{
mReport += "<tr><td colspan=3>";
mReport += "<font color=red>Expected image and result image for " + theTestName + " are different dimensions - FAILING!</font>";
mReport += "</td></tr>";
mReport += myImagesString;
delete maskImage;
return false;
}
else
{
mReport += "<tr><td colspan=3>";
mReport += "Expected image and result image for " + theTestName + " are different dimensions, but within tolerance";
mReport += "</td></tr>";
}
}

//
// Now iterate through them counting how many
// dissimilar pixel values there are
//

int maxHeight = qMin( myExpectedImage.height(), myResultImage.height() );
int maxWidth = qMin( myExpectedImage.width(), myResultImage.width() );

mMismatchCount = 0;
int colorTolerance = ( int ) mColorTolerance;
for ( int y = 0; y < myExpectedImage.height(); ++y )
for ( int y = 0; y < maxHeight; ++y )
{
const QRgb* expectedScanline = ( const QRgb* )myExpectedImage.constScanLine( y );
const QRgb* resultScanline = ( const QRgb* )myResultImage.constScanLine( y );
const QRgb* maskScanline = hasMask ? ( const QRgb* )maskImage->constScanLine( y ) : 0;
QRgb* diffScanline = ( QRgb* )myDifferenceImage.scanLine( y );

for ( int x = 0; x < myExpectedImage.width(); ++x )
for ( int x = 0; x < maxWidth; ++x )
{
int maskTolerance = hasMask ? qRed( maskScanline[ x ] ) : 0;
int pixelTolerance = qMax( colorTolerance, maskTolerance );
Expand Down
17 changes: 14 additions & 3 deletions src/core/qgsrenderchecker.h
Expand Up @@ -41,11 +41,12 @@ class CORE_EXPORT QgsRenderChecker
QgsRenderChecker();

//! Destructor
~QgsRenderChecker() {};
~QgsRenderChecker() {}

QString controlImagePath() const;

QString report() { return mReport; };
QString report() { return mReport; }

float matchPercent()
{
return static_cast<float>( mMismatchCount ) /
Expand All @@ -55,7 +56,7 @@ class CORE_EXPORT QgsRenderChecker
unsigned int matchTarget() { return mMatchTarget; }
//only records time for actual render part
int elapsedTime() { return mElapsedTime; }
void setElapsedTimeTarget( int theTarget ) { mElapsedTimeTarget = theTarget; };
void setElapsedTimeTarget( int theTarget ) { mElapsedTimeTarget = theTarget; }

/** Base directory name for the control image (with control image path
* suffixed) the path to the image will be constructed like this:
Expand Down Expand Up @@ -96,6 +97,14 @@ class CORE_EXPORT QgsRenderChecker
* @note added in 2.1
*/
void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }

/** Sets the largest allowable difference in size between the rendered and the expected image.
* @param xTolerance x tolerance in pixels
* @param yTolerance y tolerance in pixels
* @note added in QGIS 2.12
*/
void setSizeTolerance( int xTolerance, int yTolerance ) { mMaxSizeDifferenceX = xTolerance; mMaxSizeDifferenceY = yTolerance; }

/**
* Test using renderer to generate the image to be compared.
* @param theTestName - to be used as the basis for writing a file to
Expand Down Expand Up @@ -173,6 +182,8 @@ class CORE_EXPORT QgsRenderChecker
QString mControlName;
unsigned int mMismatchCount;
unsigned int mColorTolerance;
int mMaxSizeDifferenceX;
int mMaxSizeDifferenceY;
int mElapsedTimeTarget;
QgsMapSettings mMapSettings;
QString mControlPathPrefix;
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgslegendrenderer.cpp
Expand Up @@ -67,6 +67,7 @@ static bool _verifyImage( const QString& testName, QString &report )
QgsRenderChecker checker;
checker.setControlName( "expected_" + testName );
checker.setRenderedImage( _fileNameForTest( testName ) );
checker.setSizeTolerance( 3, 3 );
bool equal = checker.compareImages( testName, 500 );
report += checker.report();
return equal;
Expand Down

0 comments on commit 405688e

Please sign in to comment.