Skip to content

Commit 0fb2499

Browse files
author
timlinux
committedJan 21, 2008
Updated these tests to use the qgsrenderchecker class to validate results.
srcMakelist modified to gui tests will build git-svn-id: http://svn.osgeo.org/qgis/trunk@8017 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 3521311 commit 0fb2499

File tree

5 files changed

+106
-134
lines changed

5 files changed

+106
-134
lines changed
 

‎tests/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
IF (ENABLE_TESTS)
22
SUBDIRS(core)
3+
SUBDIRS(gui)
34
ENDIF (ENABLE_TESTS)

‎tests/src/core/CMakeLists.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Standard includes and utils to compile into all tests.
2+
SET (util_SRCS qgsrenderchecker.cpp)
3+
4+
15
#####################################################
26
# Don't forget to include output directory, otherwise
37
# the UI file won't be wrapped!
@@ -64,7 +68,7 @@ ENDIF (APPLE)
6468
#
6569
# QgsApplication test
6670
#
67-
SET(qgis_applicationtest_SRCS testqgsapplication.cpp)
71+
SET(qgis_applicationtest_SRCS testqgsapplication.cpp ${util_SRCS})
6872
SET(qgis_applicationtest_MOC_CPPS testqgsapplication.cpp)
6973
QT4_WRAP_CPP(qgis_applicationtest_MOC_SRCS ${qgis_applicationtest_MOC_CPPS})
7074
ADD_CUSTOM_TARGET(qgis_applicationtestmoc ALL DEPENDS ${qgis_applicationtest_MOC_SRCS})
@@ -85,7 +89,7 @@ ENDIF (APPLE)
8589
#
8690
# QgsFileWriter test
8791
#
88-
SET(qgis_filewritertest_SRCS testqgsvectorfilewriter.cpp)
92+
SET(qgis_filewritertest_SRCS testqgsvectorfilewriter.cpp ${util_SRCS})
8993
SET(qgis_filewritertest_MOC_CPPS testqgsvectorfilewriter.cpp)
9094
QT4_WRAP_CPP(qgis_filewritertest_MOC_SRCS ${qgis_filewritertest_MOC_CPPS})
9195
ADD_CUSTOM_TARGET(qgis_filewritertestmoc ALL DEPENDS ${qgis_filewritertest_MOC_SRCS})
@@ -106,7 +110,7 @@ ENDIF (APPLE)
106110
#
107111
# QgsRasterLayer test
108112
#
109-
SET(qgis_rasterlayertest_SRCS testqgsrasterlayer.cpp)
113+
SET(qgis_rasterlayertest_SRCS testqgsrasterlayer.cpp ${util_SRCS})
110114
SET(qgis_rasterlayertest_MOC_CPPS testqgsrasterlayer.cpp)
111115
QT4_WRAP_CPP(qgis_rasterlayertest_MOC_SRCS ${qgis_rasterlayertest_MOC_CPPS})
112116
ADD_CUSTOM_TARGET(qgis_rasterlayertestmoc ALL DEPENDS ${qgis_rasterlayertest_MOC_SRCS})
@@ -127,7 +131,7 @@ ENDIF (APPLE)
127131
#
128132
# QgsMapLayer test
129133
#
130-
SET(qgis_maplayertest_SRCS testqgsmaplayer.cpp)
134+
SET(qgis_maplayertest_SRCS testqgsmaplayer.cpp ${util_SRCS})
131135
SET(qgis_maplayertest_MOC_CPPS testqgsmaplayer.cpp)
132136
QT4_WRAP_CPP(qgis_maplayertest_MOC_SRCS ${qgis_maplayertest_MOC_CPPS})
133137
ADD_CUSTOM_TARGET(qgis_maplayertestmoc ALL DEPENDS ${qgis_maplayertest_MOC_SRCS})
@@ -148,7 +152,7 @@ ENDIF (APPLE)
148152
#
149153
# Qgs Renderers test (qsinglesymbol renderer etc)
150154
#
151-
SET(qgis_rendererstest_SRCS testqgsrenderers.cpp)
155+
SET(qgis_rendererstest_SRCS testqgsrenderers.cpp ${util_SRCS})
152156
SET(qgis_rendererstest_MOC_CPPS testqgsrenderers.cpp)
153157
QT4_WRAP_CPP(qgis_rendererstest_MOC_SRCS ${qgis_rendererstest_MOC_CPPS})
154158
ADD_CUSTOM_TARGET(qgis_rendererstestmoc ALL DEPENDS ${qgis_rendererstest_MOC_SRCS})
@@ -169,7 +173,7 @@ ENDIF (APPLE)
169173
#
170174
# QgsMapRender test
171175
#
172-
SET(qgis_maprendertest_SRCS testqgsmaprender.cpp)
176+
SET(qgis_maprendertest_SRCS testqgsmaprender.cpp ${util_SRCS})
173177
SET(qgis_maprendertest_MOC_CPPS testqgsmaprender.cpp)
174178
QT4_WRAP_CPP(qgis_maprendertest_MOC_SRCS ${qgis_maprendertest_MOC_CPPS})
175179
ADD_CUSTOM_TARGET(qgis_maprendertestmoc ALL DEPENDS ${qgis_maprendertest_MOC_SRCS})

‎tests/src/core/testqgsmaprender.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
testqgsvectorfilewriter.cpp
33
--------------------------------------
44
Date : Sun Sep 16 12:22:54 AKDT 2007
5-
Copyright : (C) 2007 by Gary E. Sherman
6-
Email : sherman at mrcc dot com
5+
Copyright : (C) 2007 by Tim Sutton
6+
Email : tim @ linfiniti.com
77
***************************************************************************
88
* *
99
* This program is free software; you can redistribute it and/or modify *
@@ -23,6 +23,7 @@
2323
#include <iostream>
2424

2525
#include <QApplication>
26+
#include <QDesktopServices>
2627

2728
//qgis includes...
2829
#include <qgsvectorlayer.h> //defines QgsFieldMap
@@ -41,6 +42,9 @@
4142
#include <qgsproviderregistry.h>
4243
#include <qgsmaplayerregistry.h>
4344

45+
//qgs unit test utility class
46+
#include "qgsrenderchecker.h"
47+
4448
/** \ingroup UnitTests
4549
* This is a unit test for the QgsMapRender class.
4650
* It will do some performance testing too
@@ -51,6 +55,7 @@ class TestQgsMapRender: public QObject
5155
Q_OBJECT;
5256
private slots:
5357
void initTestCase();// will be called before the first testfunction is executed.
58+
void cleanupTestCase();// will be called after the last testfunction was executed.
5459
void init(){};// will be called before each testfunction is executed.
5560
void cleanup(){};// will be called after every testfunction.
5661

@@ -64,6 +69,7 @@ class TestQgsMapRender: public QObject
6469
QgsFieldMap mFields;
6570
QgsMapRender * mpMapRenderer;
6671
QgsMapLayer * mpPolysLayer;
72+
QString mReport;
6773
};
6874

6975
void TestQgsMapRender::initTestCase()
@@ -84,7 +90,7 @@ void TestQgsMapRender::initTestCase()
8490

8591
//create some objects that will be used in all tests...
8692
mEncoding = "UTF-8";
87-
QgsField myField1("Field1",QVariant::String,"String",10,0,"Field 1 comment");
93+
QgsField myField1("Value",QVariant::Int,"int",10,0,"Value on lon");
8894
mFields.insert(0, myField1);
8995
mSRS = QgsSpatialRefSys(GEOWKT);
9096
//
@@ -94,6 +100,9 @@ void TestQgsMapRender::initTestCase()
94100
QString myTestDataDir = myDataDir + QDir::separator();
95101
QString myTmpDir = QDir::tempPath() + QDir::separator() ;
96102
QString myFileName = myTmpDir + "maprender_testdata.shp";
103+
//copy over the default qml for our generated layer
104+
QString myQmlFileName = myTestDataDir + "maprender_testdata.qml";
105+
QFile::copy(myQmlFileName, myTmpDir + "maprender_testdata.qml");
97106
qDebug ( "Checking test dataset exists...");
98107
qDebug ( myFileName );
99108
if (!QFile::exists(myFileName))
@@ -131,7 +140,7 @@ void TestQgsMapRender::initTestCase()
131140
QgsFeature myFeature;
132141
myFeature.setTypeName("WKBPolygon");
133142
myFeature.setGeometry(mypPolygonGeometry);
134-
myFeature.addAttribute(0,"HelloWorld");
143+
myFeature.addAttribute(0,i);
135144
//
136145
// Write the feature to the filewriter
137146
// and check for errors
@@ -167,32 +176,37 @@ void TestQgsMapRender::initTestCase()
167176
QStringList myLayers;
168177
myLayers << mpPolysLayer->getLayerID();
169178
mpMapRenderer->setLayerSet(myLayers);
179+
mReport += "<h1>Map Render Tests</h1>\n";
170180
}
171181

172-
void TestQgsMapRender::performanceTest()
182+
183+
void TestQgsMapRender::cleanupTestCase()
173184
{
185+
QString myReportFile = QDir::tempPath() + QDir::separator() + "maprendertest.html";
186+
QFile myFile ( myReportFile);
187+
if ( myFile.open ( QIODevice::WriteOnly ) )
188+
{
189+
QTextStream myQTextStream ( &myFile );
190+
myQTextStream << mReport;
191+
myFile.close();
192+
QDesktopServices::openUrl("file://"+myReportFile);
193+
}
194+
195+
}
174196

175-
//
176-
// Now render our layers onto a pixmap
177-
//
178-
QPixmap myPixmap( 1800,900 );
179-
myPixmap.fill ( QColor ( "#98dbf9" ) );
180-
QPainter myPainter( &myPixmap );
181-
mpMapRenderer->setOutputSize( QSize ( 1800,900 ),72 );
197+
198+
199+
void TestQgsMapRender::performanceTest()
200+
{
182201
mpMapRenderer->setExtent(mpPolysLayer->extent());
183-
qDebug ("Extents set to:");
184-
qDebug (mpPolysLayer->extent().stringRep());
185-
QTime myTime;
186-
myTime.start();
187-
mpMapRenderer->render( &myPainter );
188-
qDebug ("Elapsed time in ms for render job: " +
189-
QString::number ( myTime.elapsed() ).toLocal8Bit());
190-
myPainter.end();
191-
//
192-
// Save the pixmap to disk so the user can make a
193-
// visual assessment if needed
194-
//
195-
myPixmap.save (QDir::tempPath() + QDir::separator() + "maprender_result.png");
202+
QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt
203+
QString myTestDataDir = myDataDir + QDir::separator();
204+
QgsRenderChecker myChecker;
205+
myChecker.setExpectedImage ( myTestDataDir + "expected_maprender.png" );
206+
myChecker.setMapRenderer ( mpMapRenderer );
207+
bool myResultFlag = myChecker.runTest("maprender");
208+
mReport += myChecker.report();
209+
QVERIFY(myResultFlag);
196210
}
197211

198212

‎tests/src/core/testqgsrasterlayer.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QPainter>
2525
#include <QSettings>
2626
#include <QTime>
27+
#include <QDesktopServices>
2728

2829

2930
//qgis includes...
@@ -33,6 +34,10 @@
3334
#include <qgsapplication.h>
3435
#include <qgsmaprender.h>
3536

37+
//qgis unit test includes
38+
#include <qgsrenderchecker.h>
39+
40+
3641
/** \ingroup UnitTests
3742
* This is a unit test for the QgsRasterLayer class.
3843
*/
@@ -41,19 +46,21 @@ class TestQgsRasterLayer: public QObject
4146
Q_OBJECT;
4247
private slots:
4348
void initTestCase();// will be called before the first testfunction is executed.
44-
void cleanupTestCase(){};// will be called after the last testfunction was executed.
49+
void cleanupTestCase();// will be called after the last testfunction was executed.
4550
void init(){};// will be called before each testfunction is executed.
4651
void cleanup(){};// will be called after every testfunction.
4752

4853
void isValid();
4954
void pseudoColor();
5055
void checkDimensions();
5156
private:
52-
void render(QString theFileName);
57+
bool render(QString theFileName);
5358
QgsRasterLayer * mpRasterLayer;
5459
QgsMapRender * mpMapRenderer;
60+
QString mReport;
5561
};
5662

63+
//runs before all tests
5764
void TestQgsRasterLayer::initTestCase()
5865
{
5966
// init QGIS's paths - true means that all path will be inited from prefix
@@ -82,12 +89,27 @@ void TestQgsRasterLayer::initTestCase()
8289
QStringList myLayers;
8390
myLayers << mpRasterLayer->getLayerID();
8491
mpMapRenderer->setLayerSet(myLayers);
92+
mReport += "<h1>Raster Layer Tests</h1>\n";
93+
}
94+
//runs after all tests
95+
void TestQgsRasterLayer::cleanupTestCase()
96+
{
97+
QString myReportFile = QDir::tempPath() + QDir::separator() + "rastertest.html";
98+
QFile myFile ( myReportFile);
99+
if ( myFile.open ( QIODevice::WriteOnly ) )
100+
{
101+
QTextStream myQTextStream ( &myFile );
102+
myQTextStream << mReport;
103+
myFile.close();
104+
QDesktopServices::openUrl("file://"+myReportFile);
105+
}
106+
85107
}
86108

87109
void TestQgsRasterLayer::isValid()
88110
{
89111
QVERIFY ( mpRasterLayer->isValid() );
90-
render("raster_test.png");
112+
QVERIFY ( render("raster") );
91113
}
92114

93115
void TestQgsRasterLayer::pseudoColor()
@@ -98,7 +120,7 @@ void TestQgsRasterLayer::pseudoColor()
98120
QgsContrastEnhancement::STRETCH_TO_MINMAX, false);
99121
mpRasterLayer->setMinimumValue(mpRasterLayer->getGrayBandName(),0.0, false);
100122
mpRasterLayer->setMaximumValue(mpRasterLayer->getGrayBandName(),0.0);
101-
render("raster_pseudo_test.png");
123+
QVERIFY(render("raster_pseudo"));
102124
}
103125

104126
void TestQgsRasterLayer::checkDimensions()
@@ -110,30 +132,18 @@ void TestQgsRasterLayer::checkDimensions()
110132
QVERIFY ( mpRasterLayer->getRasterBandStats(1).elementCount == 100 );
111133
}
112134

113-
void TestQgsRasterLayer::render(QString theFileName)
135+
bool TestQgsRasterLayer::render(QString theTestType)
114136
{
115-
116-
//
117-
// Now render our layers onto a pixmap
118-
//
119-
QPixmap myPixmap( 100,100 );
120-
myPixmap.fill ( QColor ( "#98dbf9" ) );
121-
QPainter myPainter( &myPixmap );
122-
mpMapRenderer->setOutputSize( QSize ( 100,100 ),72 );
137+
mReport += "<h2>" + theTestType + "</h2>\n";
123138
mpMapRenderer->setExtent(mpRasterLayer->extent());
124-
qDebug ("Extents set to:");
125-
qDebug (mpRasterLayer->extent().stringRep());
126-
QTime myTime;
127-
myTime.start();
128-
mpMapRenderer->render( &myPainter );
129-
qDebug ("Elapsed time in ms for render job: " +
130-
QString::number ( myTime.elapsed() ).toLocal8Bit());
131-
myPainter.end();
132-
//
133-
// Save the pixmap to disk so the user can make a
134-
// visual assessment if needed
135-
//
136-
myPixmap.save (QDir::tempPath() + QDir::separator() + theFileName);
139+
QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt
140+
QString myTestDataDir = myDataDir + QDir::separator();
141+
QgsRenderChecker myChecker;
142+
myChecker.setExpectedImage ( myTestDataDir + "expected_" + theTestType + ".png" );
143+
myChecker.setMapRenderer ( mpMapRenderer );
144+
bool myResultFlag = myChecker.runTest(theTestType);
145+
mReport += "\n\n\n" + myChecker.report();
146+
return myResultFlag;
137147
}
138148

139149
QTEST_MAIN(TestQgsRasterLayer)

‎tests/src/core/testqgsrenderers.cpp

Lines changed: 20 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/***************************************************************************
2-
testqgsvectorfilewriter.cpp
2+
testqgsrenderers.cpp
33
--------------------------------------
4-
Date : Sun Sep 16 12:22:54 AKDT 2007
5-
Copyright : (C) 2007 by Gary E. Sherman
6-
Email : sherman at mrcc dot com
4+
Date : 20 Jan 2008
5+
Copyright : (C) 2008 by Tim Sutton
6+
Email : tim @ linfiniti.com
77
***************************************************************************
88
* *
99
* This program is free software; you can redistribute it and/or modify *
@@ -15,10 +15,6 @@
1515
#include <QtTest>
1616
#include <QObject>
1717
#include <QString>
18-
#include <QPainter>
19-
#include <QPixmap>
20-
#include <QByteArray>
21-
#include <QBuffer>
2218
#include <QStringList>
2319
#include <QObject>
2420
#include <QApplication>
@@ -34,6 +30,8 @@
3430
#include <qgsapplication.h>
3531
#include <qgsproviderregistry.h>
3632
#include <qgsmaplayerregistry.h>
33+
//qgis test includes
34+
#include "qgsrenderchecker.h"
3735

3836
/** \ingroup UnitTests
3937
* This is a unit test for the different renderers for vector layers.
@@ -134,6 +132,7 @@ void TestQgsRenderers::initTestCase()
134132
myLayers << mpPolysLayer->getLayerID();
135133
myLayers << mpLinesLayer->getLayerID();
136134
mpMapRenderer->setLayerSet(myLayers);
135+
mReport+= "<h1>Vector Renderer Tests</h1>\n";
137136
}
138137
void TestQgsRenderers::cleanupTestCase()
139138
{
@@ -151,28 +150,28 @@ void TestQgsRenderers::cleanupTestCase()
151150

152151
void TestQgsRenderers::singleSymbol()
153152
{
154-
mReport+= "<h1>Single symbol renderer test</h1>\n";
153+
mReport+= "<h2>Single symbol renderer test</h2>\n";
155154
QVERIFY ( setQml("single") );
156155
QVERIFY ( imageCheck("single"));
157156
}
158157

159158
void TestQgsRenderers::uniqueValue()
160159
{
161-
mReport+= "<h1>Unique value symbol renderer test</h1>\n";
160+
mReport+= "<h2>Unique value symbol renderer test</h2>\n";
162161
QVERIFY ( setQml("uniquevalue") );
163162
QVERIFY ( imageCheck("uniquevalue"));
164163
}
165164

166165
void TestQgsRenderers::graduatedSymbol()
167166
{
168-
mReport+= "<h1>Graduated symbol renderer test</h1>\n";
167+
mReport+= "<h2>Graduated symbol renderer test</h2>\n";
169168
QVERIFY ( setQml("graduated") );
170169
QVERIFY ( imageCheck("graduated"));
171170
}
172171

173172
void TestQgsRenderers::continuousSymbol()
174173
{
175-
mReport+= "<h1>Continuous symbol renderer test</h1>\n";
174+
mReport+= "<h2>Continuous symbol renderer test</h2>\n";
176175
QVERIFY ( setQml("continuous") );
177176
QVERIFY ( imageCheck("continuous"));
178177
}
@@ -217,72 +216,16 @@ bool TestQgsRenderers::setQml (QString theType)
217216

218217
bool TestQgsRenderers::imageCheck(QString theTestType)
219218
{
220-
//
221-
// Now render our layers onto a pixmap
222-
//
223-
QPixmap myPixmap( 800,800 );
224-
myPixmap.fill ( QColor ( "#98dbf9" ) );
225-
QPainter myPainter( &myPixmap );
226-
mpMapRenderer->setOutputSize( QSize ( 800,800 ),72 );
219+
//use the QgsRenderChecker test utility class to
220+
//ensure the rendered output matches our control image
227221
mpMapRenderer->setExtent(mpPointsLayer->extent());
228-
mpMapRenderer->render( &myPainter );
229-
myPainter.end();
230-
//
231-
// Save the pixmap to disk so the user can make a
232-
// visual assessment if needed
233-
//
234-
myPixmap.save (QDir::tempPath() + QDir::separator() + theTestType + ".png");
235-
//
236-
// Load the expected result pixmap
237-
//
238-
QPixmap myExpectedPixmap (mTestDataDir + "expected_" + theTestType + ".png");
239-
mReport+= "<table>"
240-
"<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
241-
"<tr><td><img src=\"" +
242-
QDir::tempPath() + QDir::separator() + theTestType + ".png" +
243-
"\"></td>\n<td><img src=\"" +
244-
mTestDataDir + "expected_" + theTestType + ".png" +
245-
"\"></td></tr></table>\n";
246-
//
247-
// Now load the renderered image and the expected image
248-
// each into a byte array, and then iterate through them
249-
// counting how many dissimilar pixel values there are
250-
//
251-
QByteArray myResultBytes;
252-
QBuffer myResultBuffer(&myResultBytes);
253-
myResultBuffer.open(QIODevice::WriteOnly);
254-
myPixmap.save(&myResultBuffer, "PNG"); // writes pixmap into bytes in PNG format
255-
256-
QByteArray myExpectedBytes;
257-
QBuffer myExpectedBuffer(&myExpectedBytes);
258-
myExpectedBuffer.open(QIODevice::WriteOnly);
259-
myExpectedPixmap.save(&myExpectedBuffer, "PNG"); // writes pixmap into bytes in PNG format
260-
261-
if (myExpectedBytes.size() != myResultBytes.size())
262-
{
263-
qDebug ("Test image and result image for " + theTestType + " are different - FAILING!");
264-
return false;
265-
}
266-
int myMismatchCount = 0;
267-
for (int i = 0; i < myExpectedBytes.size(); ++i)
268-
{
269-
if (myExpectedBytes.at(i) != myResultBytes.at(i))
270-
{
271-
++myMismatchCount;
272-
}
273-
}
274-
qDebug (QString::number(myMismatchCount).toLocal8Bit() + "/" +
275-
QString::number(myExpectedBytes.size()).toLocal8Bit() +
276-
" bytes mismatched");;
277-
278-
if ( myMismatchCount==0 )
279-
{
280-
return true;
281-
}
282-
else
283-
{
284-
return false;
285-
}
222+
QString myExpectedImage = mTestDataDir + "expected_" + theTestType + ".png";
223+
QgsRenderChecker myChecker;
224+
myChecker.setExpectedImage ( myExpectedImage );
225+
myChecker.setMapRenderer ( mpMapRenderer );
226+
bool myResultFlag = myChecker.runTest( theTestType );
227+
mReport += myChecker.report();
228+
return myResultFlag;
286229
}
287230

288231
QTEST_MAIN(TestQgsRenderers)

0 commit comments

Comments
 (0)
Please sign in to comment.