Skip to content

Commit 4e6636a

Browse files
author
timlinux
committed
In renderchecker split runtest into two functions so that image based compares can be done in situations where a maprenderer is not needed. Added more tests for geometry functions (buffer, difference) and provide a visual report of geometry test results.
git-svn-id: http://svn.osgeo.org/qgis/trunk@8947 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c7c2666 commit 4e6636a

9 files changed

+217
-37
lines changed

tests/src/core/qgsrenderchecker.cpp

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
QgsRenderChecker::QgsRenderChecker( ) :
2626
mReport(""),
2727
mExpectedImageFile(""),
28+
mRenderedImageFile(""),
2829
mMismatchCount(0),
2930
mMatchTarget(0),
3031
mElapsedTime(0),
@@ -53,10 +54,7 @@ bool QgsRenderChecker::runTest( QString theTestName )
5354
// Now render our layers onto a pixmap
5455
//
5556
QImage myImage( myExpectedImage.width() , myExpectedImage.height(), QImage::Format_RGB32 );
56-
QImage myDifferenceImage( myExpectedImage.width() , myExpectedImage.height(), QImage::Format_RGB32);
57-
QString myResultDiffImage = QDir::tempPath() + QDir::separator() + theTestName + "_result_diff.png";
5857
myImage.fill ( qRgb( 152,219,249 ) );
59-
myDifferenceImage.fill ( qRgb( 152,219,249 ) );
6058
QPainter myPainter( &myImage );
6159
mpMapRenderer->setOutputSize( QSize ( myExpectedImage.width(),myExpectedImage.height() ),72 );
6260
QTime myTime;
@@ -68,51 +66,81 @@ bool QgsRenderChecker::runTest( QString theTestName )
6866
// Save the pixmap to disk so the user can make a
6967
// visual assessment if needed
7068
//
71-
QString myResultImage = QDir::tempPath() + QDir::separator() + theTestName + "_result.png";
72-
myImage.save (myResultImage);
69+
mRenderedImageFile = QDir::tempPath() + QDir::separator() + theTestName + "_result.png";
70+
myImage.save (mRenderedImageFile);
71+
return compareImages(theTestName);
72+
73+
}
74+
75+
76+
bool QgsRenderChecker::compareImages( QString theTestName )
77+
{
78+
if (mExpectedImageFile.isEmpty())
79+
{
80+
qDebug("QgsRenderChecker::runTest failed - Expected Image (control) File not set.");
81+
mReport= "<table>"
82+
"<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
83+
"<tr><td>Nothing rendered</td>\n<td>Failed because Expected "
84+
"Image File not set.</td></tr></table>\n";
85+
return false;
86+
}
87+
if (mRenderedImageFile.isEmpty())
88+
{
89+
qDebug("QgsRenderChecker::runTest failed - Rendered Image File not set.");
90+
mReport= "<table>"
91+
"<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
92+
"<tr><td>Nothing rendered</td>\n<td>Failed because Expected "
93+
"Image File not set.</td></tr></table>\n";
94+
return false;
95+
}
96+
//
97+
// Load /create the images
98+
//
99+
QImage myExpectedImage (mExpectedImageFile);
100+
QImage myResultImage (mRenderedImageFile);
101+
QImage myDifferenceImage( myExpectedImage.width() , myExpectedImage.height(), QImage::Format_RGB32);
102+
QString myResultDiffImage = QDir::tempPath() + QDir::separator() + theTestName + "_result_diff.png";
103+
myDifferenceImage.fill ( qRgb( 152,219,249 ) );
104+
73105
//
74106
// Set pixel count score and target
75107
//
76108
mMatchTarget = myExpectedImage.width() * myExpectedImage.height();
77-
int myPixelCount = myImage.width() * myImage.height();
109+
int myPixelCount = myResultImage.width() * myResultImage.height();
78110
//
79111
// Set the report with the result
80112
//
81113
mReport= "<table>";
82114
mReport += "<tr><td colspan=2>";
83115
mReport += "Test image and result image for " + theTestName + "<br>"
84116
"Expected size: " + QString::number(myExpectedImage.width()).toLocal8Bit() + "w x " +
85-
QString::number(myExpectedImage.width()).toLocal8Bit() + "h (" +
86-
QString::number(mMatchTarget).toLocal8Bit() + " pixels)<br>"
87-
"Actual size: " + QString::number(myImage.width()).toLocal8Bit() + "w x " +
88-
QString::number(myImage.width()).toLocal8Bit() + "h (" +
89-
QString::number(myPixelCount).toLocal8Bit() + " pixels)";
117+
QString::number(myExpectedImage.width()).toLocal8Bit() + "h (" +
118+
QString::number(mMatchTarget).toLocal8Bit() + " pixels)<br>"
119+
"Actual size: " + QString::number(myResultImage.width()).toLocal8Bit() + "w x " +
120+
QString::number(myResultImage.width()).toLocal8Bit() + "h (" +
121+
QString::number(myPixelCount).toLocal8Bit() + " pixels)";
90122
mReport += "</td></tr>";
91123
mReport += "<tr><td colspan = 2>\n";
92124
mReport += "Expected Duration : <= " + QString::number(mElapsedTimeTarget) +
93-
"ms (0 indicates not specified)<br>";
125+
"ms (0 indicates not specified)<br>";
94126
mReport += "Actual Duration : " + QString::number(mElapsedTime) + "ms<br>";
95127
QString myImagesString= "</td></tr>"
96128
"<tr><td>Test Result:</td><td>Expected Result:</td><td>Difference (all blue is good, any red is bad)</td></tr>\n"
97129
"<tr><td><img src=\"file://" +
98-
myResultImage +
130+
mRenderedImageFile +
99131
"\"></td>\n<td><img src=\"file://" +
100-
mExpectedImageFile +
132+
mExpectedImageFile +
101133
"\"></td><td><img src=\"file://" +
102-
myResultDiffImage +
134+
myResultDiffImage +
103135
"\"></td>\n</tr>\n</table>";
104136
//
105137
// Put the same info to debug too
106138
//
107-
qDebug ("Expected size: " + QString::number(myExpectedImage.width()).toLocal8Bit() + + "w x " +
108-
QString::number(myExpectedImage.width()).toLocal8Bit() + + "h");
109-
qDebug ("Actual size: " + QString::number(myImage.width()).toLocal8Bit() + + "w x " +
110-
QString::number(myImage.width()).toLocal8Bit() + + "h");
111-
//
112-
// Now load the renderered image and the expected image
113-
// and then iterate through them counting how many
114-
// dissimilar pixel values there are
115-
//
139+
140+
qDebug ("Expected size: " + QString::number(myExpectedImage.width()).toLocal8Bit() + "w x " +
141+
QString::number(myExpectedImage.width()).toLocal8Bit() + "h");
142+
qDebug ("Actual size: " + QString::number(myResultImage.width()).toLocal8Bit() + "w x " +
143+
QString::number(myResultImage.width()).toLocal8Bit() + "h");
116144

117145
if (mMatchTarget!= myPixelCount )
118146
{
@@ -123,13 +151,19 @@ bool QgsRenderChecker::runTest( QString theTestName )
123151
mReport += myImagesString;
124152
return false;
125153
}
154+
155+
//
156+
// Now iterate through them counting how many
157+
// dissimilar pixel values there are
158+
//
159+
126160
mMismatchCount = 0;
127161
for (int x = 0; x < myExpectedImage.width(); ++x)
128162
{
129163
for (int y = 0; y < myExpectedImage.height(); ++y)
130164
{
131165
QRgb myExpectedPixel = myExpectedImage.pixel(x,y);
132-
QRgb myActualPixel = myImage.pixel(x,y);
166+
QRgb myActualPixel = myResultImage.pixel(x,y);
133167
if (myExpectedPixel != myActualPixel)
134168
{
135169
++mMismatchCount;
@@ -141,14 +175,14 @@ bool QgsRenderChecker::runTest( QString theTestName )
141175
//save the diff image to disk
142176
//
143177
myDifferenceImage.save (myResultDiffImage);
144-
178+
145179
//
146180
// Send match result to debug
147181
//
148182
qDebug (QString::number(mMismatchCount).toLocal8Bit() + "/" +
149-
QString::number(mMatchTarget).toLocal8Bit() +
150-
" pixels mismatched");;
151-
183+
QString::number(mMatchTarget).toLocal8Bit() +
184+
" pixels mismatched");;
185+
152186
//
153187
// Send match result to report
154188
//
@@ -158,7 +192,7 @@ bool QgsRenderChecker::runTest( QString theTestName )
158192
" pixels mismatched";
159193
mReport += "</td></tr>";
160194

161-
195+
162196
if ( mMismatchCount==0 )
163197
{
164198
mReport += "<tr><td colspan = 3>\n";

tests/src/core/qgsrenderchecker.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,27 @@ class QgsRenderChecker
4444
int elapsedTime() { return mElapsedTime; };
4545
void setElapsedTimeTarget(int theTarget) { mElapsedTimeTarget = theTarget; };
4646
void setExpectedImage (QString theImageFileName) { mExpectedImageFile = theImageFileName; };
47+
void setRenderedImage (QString theImageFileName) { mRenderedImageFile = theImageFileName; };
4748
void setMapRenderer ( QgsMapRender * thepMapRenderer) { mpMapRenderer = thepMapRenderer; };
4849
/**
50+
* Test using renderer to generate the image to be compared.
4951
* @param theTestName - to be used as the basis for writing a file to
50-
* /tmp/theTestName.png
52+
* e.g. /tmp/theTestName.png
53+
* @note make sure to call setExpectedImage and setMapRenderer first
5154
*/
5255
bool runTest( QString theTestName );
5356

57+
/**
58+
* Test using two arbitary images (map renderer will not be used)
59+
* @param theTestName - to be used as the basis for writing a file to
60+
* e.g. /tmp/theTestName.png
61+
* @note: make sure to call setExpectedImage and setRenderedImage first.
62+
*/
63+
bool compareImages( QString theTestName );
5464
private:
5565
QString mReport;
5666
QString mExpectedImageFile;
67+
QString mRenderedImageFile;
5768
unsigned int mMismatchCount;
5869
unsigned int mMatchTarget;
5970
int mElapsedTime;

0 commit comments

Comments
 (0)