[tests] Add method for setting size tolerance to render checker,

add some size tolerance to legend renderer test
This commit is contained in:
Nyall Dawson 2015-07-10 16:52:52 +10:00
parent 78c2db627f
commit 405688ea49
4 changed files with 48 additions and 12 deletions

View File

@ -62,6 +62,14 @@ class QgsRenderChecker
* @note added in 2.1 * @note added in 2.1
*/ */
void setColorTolerance( unsigned int theColorTolerance ); 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. * Test using renderer to generate the image to be compared.
* @param theTestName - to be used as the basis for writing a file to * @param theTestName - to be used as the basis for writing a file to

View File

@ -37,6 +37,8 @@ QgsRenderChecker::QgsRenderChecker()
, mExpectedImageFile( "" ) , mExpectedImageFile( "" )
, mMismatchCount( 0 ) , mMismatchCount( 0 )
, mColorTolerance( 0 ) , mColorTolerance( 0 )
, mMaxSizeDifferenceX( 0 )
, mMaxSizeDifferenceY( 0 )
, mElapsedTimeTarget( 0 ) , mElapsedTimeTarget( 0 )
, mBufferDashMessages( false ) , mBufferDashMessages( false )
{ {
@ -374,7 +376,11 @@ bool QgsRenderChecker::compareImages( QString theTestName,
if ( mMatchTarget != myPixelCount ) if ( mMatchTarget != myPixelCount )
{ {
qDebug( "Test image and result image for %s are different dimensions - FAILING!", theTestName.toLocal8Bit().constData() ); 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 += "<tr><td colspan=3>";
mReport += "<font color=red>Expected image and result image for " + theTestName + " are different dimensions - FAILING!</font>"; mReport += "<font color=red>Expected image and result image for " + theTestName + " are different dimensions - FAILING!</font>";
mReport += "</td></tr>"; mReport += "</td></tr>";
@ -382,22 +388,32 @@ bool QgsRenderChecker::compareImages( QString theTestName,
delete maskImage; delete maskImage;
return false; 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 // Now iterate through them counting how many
// dissimilar pixel values there are // dissimilar pixel values there are
// //
int maxHeight = qMin( myExpectedImage.height(), myResultImage.height() );
int maxWidth = qMin( myExpectedImage.width(), myResultImage.width() );
mMismatchCount = 0; mMismatchCount = 0;
int colorTolerance = ( int ) mColorTolerance; 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* expectedScanline = ( const QRgb* )myExpectedImage.constScanLine( y );
const QRgb* resultScanline = ( const QRgb* )myResultImage.constScanLine( y ); const QRgb* resultScanline = ( const QRgb* )myResultImage.constScanLine( y );
const QRgb* maskScanline = hasMask ? ( const QRgb* )maskImage->constScanLine( y ) : 0; const QRgb* maskScanline = hasMask ? ( const QRgb* )maskImage->constScanLine( y ) : 0;
QRgb* diffScanline = ( QRgb* )myDifferenceImage.scanLine( y ); 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 maskTolerance = hasMask ? qRed( maskScanline[ x ] ) : 0;
int pixelTolerance = qMax( colorTolerance, maskTolerance ); int pixelTolerance = qMax( colorTolerance, maskTolerance );

View File

@ -41,11 +41,12 @@ class CORE_EXPORT QgsRenderChecker
QgsRenderChecker(); QgsRenderChecker();
//! Destructor //! Destructor
~QgsRenderChecker() {}; ~QgsRenderChecker() {}
QString controlImagePath() const; QString controlImagePath() const;
QString report() { return mReport; }; QString report() { return mReport; }
float matchPercent() float matchPercent()
{ {
return static_cast<float>( mMismatchCount ) / return static_cast<float>( mMismatchCount ) /
@ -55,7 +56,7 @@ class CORE_EXPORT QgsRenderChecker
unsigned int matchTarget() { return mMatchTarget; } unsigned int matchTarget() { return mMatchTarget; }
//only records time for actual render part //only records time for actual render part
int elapsedTime() { return mElapsedTime; } 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 /** Base directory name for the control image (with control image path
* suffixed) the path to the image will be constructed like this: * suffixed) the path to the image will be constructed like this:
@ -96,6 +97,14 @@ class CORE_EXPORT QgsRenderChecker
* @note added in 2.1 * @note added in 2.1
*/ */
void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; } 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. * Test using renderer to generate the image to be compared.
* @param theTestName - to be used as the basis for writing a file to * @param theTestName - to be used as the basis for writing a file to
@ -173,6 +182,8 @@ class CORE_EXPORT QgsRenderChecker
QString mControlName; QString mControlName;
unsigned int mMismatchCount; unsigned int mMismatchCount;
unsigned int mColorTolerance; unsigned int mColorTolerance;
int mMaxSizeDifferenceX;
int mMaxSizeDifferenceY;
int mElapsedTimeTarget; int mElapsedTimeTarget;
QgsMapSettings mMapSettings; QgsMapSettings mMapSettings;
QString mControlPathPrefix; QString mControlPathPrefix;

View File

@ -67,6 +67,7 @@ static bool _verifyImage( const QString& testName, QString &report )
QgsRenderChecker checker; QgsRenderChecker checker;
checker.setControlName( "expected_" + testName ); checker.setControlName( "expected_" + testName );
checker.setRenderedImage( _fileNameForTest( testName ) ); checker.setRenderedImage( _fileNameForTest( testName ) );
checker.setSizeTolerance( 3, 3 );
bool equal = checker.compareImages( testName, 500 ); bool equal = checker.compareImages( testName, 500 );
report += checker.report(); report += checker.report();
return equal; return equal;