2424#include < qgsvectorlayerdiagramprovider.h>
2525#include < qgsvectorlayerlabeling.h>
2626#include < qgsvectorlayerlabelprovider.h>
27+ #include " qgsrenderchecker.h"
28+ #include " qgsfontutils.h"
2729
2830class TestQgsLabelingEngineV2 : public QObject
2931{
3032 Q_OBJECT
3133 public:
3234 TestQgsLabelingEngineV2 () : vl( 0 ) {}
35+
3336 private slots:
3437 void initTestCase ();
3538 void cleanupTestCase ();
39+ void init ();// will be called before each testfunction is executed.
40+ void cleanup ();// will be called after every testfunction.
3641 void testBasic ();
3742 void testDiagrams ();
3843 void testRuleBased ();
3944
4045 private:
4146 QgsVectorLayer* vl;
47+
48+ QString mReport ;
49+
50+ void setDefaultLabelParams ( QgsVectorLayer* layer );
51+ bool imageCheck ( const QString& testName, QImage &image, int mismatchCount );
4252};
4353
4454void TestQgsLabelingEngineV2::initTestCase ()
4555{
56+ mReport += " <h1>Labeling Engine V2 Tests</h1>\n " ;
57+
4658 QgsApplication::init ();
4759 QgsApplication::initQgis ();
4860 QgsApplication::showSettings ();
61+ QgsFontUtils::loadStandardTestFonts ( QStringList () << " Bold" );
62+ }
4963
50- QString filename = QString ( TEST_DATA_DIR ) + " /points.shp" ;
64+ void TestQgsLabelingEngineV2::cleanupTestCase ()
65+ {
66+ QgsApplication::exitQgis ();
67+ QString myReportFile = QDir::tempPath () + " /qgistest.html" ;
68+ QFile myFile ( myReportFile );
69+ if ( myFile.open ( QIODevice::WriteOnly | QIODevice::Append ) )
70+ {
71+ QTextStream myQTextStream ( &myFile );
72+ myQTextStream << mReport ;
73+ myFile.close ();
74+ }
75+ }
5176
77+ void TestQgsLabelingEngineV2::init ()
78+ {
79+ QString filename = QString ( TEST_DATA_DIR ) + " /points.shp" ;
5280 vl = new QgsVectorLayer ( filename, " points" , " ogr" );
5381 Q_ASSERT ( vl->isValid () );
5482 QgsMapLayerRegistry::instance ()->addMapLayer ( vl );
5583}
5684
57- void TestQgsLabelingEngineV2::cleanupTestCase ()
85+ void TestQgsLabelingEngineV2::cleanup ()
5886{
59- QgsApplication::exitQgis ();
87+ QgsMapLayerRegistry::instance ()->removeMapLayer ( vl->id ( ) );
88+ vl = 0 ;
6089}
6190
91+ void TestQgsLabelingEngineV2::setDefaultLabelParams ( QgsVectorLayer* layer )
92+ {
93+ layer->setCustomProperty ( " labeling/fontFamily" , QgsFontUtils::getStandardTestFont ( " Bold" ).family () );
94+ layer->setCustomProperty ( " labeling/namedStyle" , " Bold" );
95+ layer->setCustomProperty ( " labeling/textColorR" , " 200" );
96+ layer->setCustomProperty ( " labeling/textColorG" , " 0" );
97+ layer->setCustomProperty ( " labeling/textColorB" , " 200" );
98+ }
6299
63100void TestQgsLabelingEngineV2::testBasic ()
64101{
@@ -67,6 +104,7 @@ void TestQgsLabelingEngineV2::testBasic()
67104 mapSettings.setOutputSize ( size );
68105 mapSettings.setExtent ( vl->extent () );
69106 mapSettings.setLayers ( QStringList () << vl->id () );
107+ mapSettings.setOutputDpi ( 96 );
70108
71109 // first render the map and labeling separately
72110
@@ -83,6 +121,7 @@ void TestQgsLabelingEngineV2::testBasic()
83121 vl->setCustomProperty ( " labeling" , " pal" );
84122 vl->setCustomProperty ( " labeling/enabled" , true );
85123 vl->setCustomProperty ( " labeling/fieldName" , " Class" );
124+ setDefaultLabelParams ( vl );
86125
87126 QgsLabelingEngineV2 engine;
88127 engine.setMapSettings ( mapSettings );
@@ -92,6 +131,8 @@ void TestQgsLabelingEngineV2::testBasic()
92131
93132 p.end ();
94133
134+ QVERIFY ( imageCheck ( " labeling_basic" , img, 0 ) );
135+
95136 // now let's test the variant when integrated into rendering loop
96137
97138 job.start ();
@@ -100,7 +141,7 @@ void TestQgsLabelingEngineV2::testBasic()
100141
101142 vl->setCustomProperty ( " labeling/enabled" , false );
102143
103- QCOMPARE ( img , img2 );
144+ QVERIFY ( imageCheck ( " labeling_basic " , img2, 0 ) );
104145}
105146
106147void TestQgsLabelingEngineV2::testDiagrams ()
@@ -110,6 +151,7 @@ void TestQgsLabelingEngineV2::testDiagrams()
110151 mapSettings.setOutputSize ( size );
111152 mapSettings.setExtent ( vl->extent () );
112153 mapSettings.setLayers ( QStringList () << vl->id () );
154+ mapSettings.setOutputDpi ( 96 );
113155
114156 // first render the map and diagrams separately
115157
@@ -134,14 +176,15 @@ void TestQgsLabelingEngineV2::testDiagrams()
134176
135177 p.end ();
136178
179+ QVERIFY ( imageCheck ( " labeling_point_diagrams" , img, 0 ) );
180+
137181 // now let's test the variant when integrated into rendering loop
138182 job.start ();
139183 job.waitForFinished ();
140184 QImage img2 = job.renderedImage ();
141185
142- QCOMPARE ( img, img2 );
143-
144186 vl->loadDefaultStyle ( res );
187+ QVERIFY ( imageCheck ( " labeling_point_diagrams" , img2, 0 ) );
145188}
146189
147190
@@ -152,6 +195,7 @@ void TestQgsLabelingEngineV2::testRuleBased()
152195 mapSettings.setOutputSize ( size );
153196 mapSettings.setExtent ( vl->extent () );
154197 mapSettings.setLayers ( QStringList () << vl->id () );
198+ mapSettings.setOutputDpi ( 96 );
155199
156200 // set up most basic rule-based labeling for layer
157201 QgsRuleBasedLabeling::Rule* root = new QgsRuleBasedLabeling::Rule ( 0 );
@@ -162,6 +206,12 @@ void TestQgsLabelingEngineV2::testRuleBased()
162206 s1.obstacle = false ;
163207 s1.dist = 2 ;
164208 s1.distInMapUnits = false ;
209+ s1.textColor = QColor ( 200 , 0 , 200 );
210+ s1.textFont = QgsFontUtils::getStandardTestFont ( " Bold" );
211+ s1.placement = QgsPalLayerSettings::OverPoint;
212+ s1.quadOffset = QgsPalLayerSettings::QuadrantAboveLeft;
213+ s1.displayAll = true ;
214+
165215 root->appendChild ( new QgsRuleBasedLabeling::Rule ( new QgsPalLayerSettings ( s1 ) ) );
166216
167217 QgsPalLayerSettings s2;
@@ -170,18 +220,22 @@ void TestQgsLabelingEngineV2::testRuleBased()
170220 s2.obstacle = false ;
171221 s2.dist = 2 ;
172222 s2.textColor = Qt::red;
223+ s2.textFont = QgsFontUtils::getStandardTestFont ( " Bold" );
224+ s2.placement = QgsPalLayerSettings::OverPoint;
225+ s2.quadOffset = QgsPalLayerSettings::QuadrantBelowRight;
226+ s2.displayAll = true ;
173227 s2.setDataDefinedProperty ( QgsPalLayerSettings::Size, true , true , " 18" , QString () );
174228
175229 root->appendChild ( new QgsRuleBasedLabeling::Rule ( new QgsPalLayerSettings ( s2 ), 0 , 0 , " Class = 'Jet'" ) );
176230
177231 vl->setLabeling ( new QgsRuleBasedLabeling ( root ) );
232+ setDefaultLabelParams ( vl );
178233
179234 QgsMapRendererSequentialJob job ( mapSettings );
180235 job.start ();
181236 job.waitForFinished ();
182237 QImage img = job.renderedImage ();
183-
184- img.save ( " /tmp/rules.png" );
238+ QVERIFY ( imageCheck ( " labeling_rulebased" , img, 0 ) );
185239
186240 // test read/write rules
187241 QDomDocument doc, doc2, doc3;
@@ -213,5 +267,28 @@ void TestQgsLabelingEngineV2::testRuleBased()
213267
214268}
215269
270+ bool TestQgsLabelingEngineV2::imageCheck ( const QString& testName, QImage &image, int mismatchCount )
271+ {
272+ // draw background
273+ QImage imageWithBackground ( image.width (), image.height (), QImage::Format_RGB32 );
274+ QgsRenderChecker::drawBackground ( &imageWithBackground );
275+ QPainter painter ( &imageWithBackground );
276+ painter.drawImage ( 0 , 0 , image );
277+ painter.end ();
278+
279+ mReport += " <h2>" + testName + " </h2>\n " ;
280+ QString tempDir = QDir::tempPath () + " /" ;
281+ QString fileName = tempDir + testName + " .png" ;
282+ imageWithBackground.save ( fileName, " PNG" );
283+ QgsRenderChecker checker;
284+ checker.setControlPathPrefix ( " labelingenginev2" );
285+ checker.setControlName ( " expected_" + testName );
286+ checker.setRenderedImage ( fileName );
287+ checker.setColorTolerance ( 2 );
288+ bool resultFlag = checker.compareImages ( testName, mismatchCount );
289+ mReport += checker.report ();
290+ return resultFlag;
291+ }
292+
216293QTEST_MAIN ( TestQgsLabelingEngineV2 )
217294#include " testqgslabelingenginev2.moc"
0 commit comments