24
24
#include < qgsvectorlayerdiagramprovider.h>
25
25
#include < qgsvectorlayerlabeling.h>
26
26
#include < qgsvectorlayerlabelprovider.h>
27
+ #include " qgsrenderchecker.h"
28
+ #include " qgsfontutils.h"
27
29
28
30
class TestQgsLabelingEngineV2 : public QObject
29
31
{
30
32
Q_OBJECT
31
33
public:
32
34
TestQgsLabelingEngineV2 () : vl( 0 ) {}
35
+
33
36
private slots:
34
37
void initTestCase ();
35
38
void cleanupTestCase ();
39
+ void init ();// will be called before each testfunction is executed.
40
+ void cleanup ();// will be called after every testfunction.
36
41
void testBasic ();
37
42
void testDiagrams ();
38
43
void testRuleBased ();
39
44
40
45
private:
41
46
QgsVectorLayer* vl;
47
+
48
+ QString mReport ;
49
+
50
+ void setDefaultLabelParams ( QgsVectorLayer* layer );
51
+ bool imageCheck ( const QString& testName, QImage &image, int mismatchCount );
42
52
};
43
53
44
54
void TestQgsLabelingEngineV2::initTestCase ()
45
55
{
56
+ mReport += " <h1>Labeling Engine V2 Tests</h1>\n " ;
57
+
46
58
QgsApplication::init ();
47
59
QgsApplication::initQgis ();
48
60
QgsApplication::showSettings ();
61
+ QgsFontUtils::loadStandardTestFonts ( QStringList () << " Bold" );
62
+ }
49
63
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
+ }
51
76
77
+ void TestQgsLabelingEngineV2::init ()
78
+ {
79
+ QString filename = QString ( TEST_DATA_DIR ) + " /points.shp" ;
52
80
vl = new QgsVectorLayer ( filename, " points" , " ogr" );
53
81
Q_ASSERT ( vl->isValid () );
54
82
QgsMapLayerRegistry::instance ()->addMapLayer ( vl );
55
83
}
56
84
57
- void TestQgsLabelingEngineV2::cleanupTestCase ()
85
+ void TestQgsLabelingEngineV2::cleanup ()
58
86
{
59
- QgsApplication::exitQgis ();
87
+ QgsMapLayerRegistry::instance ()->removeMapLayer ( vl->id ( ) );
88
+ vl = 0 ;
60
89
}
61
90
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
+ }
62
99
63
100
void TestQgsLabelingEngineV2::testBasic ()
64
101
{
@@ -67,6 +104,7 @@ void TestQgsLabelingEngineV2::testBasic()
67
104
mapSettings.setOutputSize ( size );
68
105
mapSettings.setExtent ( vl->extent () );
69
106
mapSettings.setLayers ( QStringList () << vl->id () );
107
+ mapSettings.setOutputDpi ( 96 );
70
108
71
109
// first render the map and labeling separately
72
110
@@ -83,6 +121,7 @@ void TestQgsLabelingEngineV2::testBasic()
83
121
vl->setCustomProperty ( " labeling" , " pal" );
84
122
vl->setCustomProperty ( " labeling/enabled" , true );
85
123
vl->setCustomProperty ( " labeling/fieldName" , " Class" );
124
+ setDefaultLabelParams ( vl );
86
125
87
126
QgsLabelingEngineV2 engine;
88
127
engine.setMapSettings ( mapSettings );
@@ -92,6 +131,8 @@ void TestQgsLabelingEngineV2::testBasic()
92
131
93
132
p.end ();
94
133
134
+ QVERIFY ( imageCheck ( " labeling_basic" , img, 0 ) );
135
+
95
136
// now let's test the variant when integrated into rendering loop
96
137
97
138
job.start ();
@@ -100,7 +141,7 @@ void TestQgsLabelingEngineV2::testBasic()
100
141
101
142
vl->setCustomProperty ( " labeling/enabled" , false );
102
143
103
- QCOMPARE ( img , img2 );
144
+ QVERIFY ( imageCheck ( " labeling_basic " , img2, 0 ) );
104
145
}
105
146
106
147
void TestQgsLabelingEngineV2::testDiagrams ()
@@ -110,6 +151,7 @@ void TestQgsLabelingEngineV2::testDiagrams()
110
151
mapSettings.setOutputSize ( size );
111
152
mapSettings.setExtent ( vl->extent () );
112
153
mapSettings.setLayers ( QStringList () << vl->id () );
154
+ mapSettings.setOutputDpi ( 96 );
113
155
114
156
// first render the map and diagrams separately
115
157
@@ -134,14 +176,15 @@ void TestQgsLabelingEngineV2::testDiagrams()
134
176
135
177
p.end ();
136
178
179
+ QVERIFY ( imageCheck ( " labeling_point_diagrams" , img, 0 ) );
180
+
137
181
// now let's test the variant when integrated into rendering loop
138
182
job.start ();
139
183
job.waitForFinished ();
140
184
QImage img2 = job.renderedImage ();
141
185
142
- QCOMPARE ( img, img2 );
143
-
144
186
vl->loadDefaultStyle ( res );
187
+ QVERIFY ( imageCheck ( " labeling_point_diagrams" , img2, 0 ) );
145
188
}
146
189
147
190
@@ -152,6 +195,7 @@ void TestQgsLabelingEngineV2::testRuleBased()
152
195
mapSettings.setOutputSize ( size );
153
196
mapSettings.setExtent ( vl->extent () );
154
197
mapSettings.setLayers ( QStringList () << vl->id () );
198
+ mapSettings.setOutputDpi ( 96 );
155
199
156
200
// set up most basic rule-based labeling for layer
157
201
QgsRuleBasedLabeling::Rule* root = new QgsRuleBasedLabeling::Rule ( 0 );
@@ -162,6 +206,12 @@ void TestQgsLabelingEngineV2::testRuleBased()
162
206
s1.obstacle = false ;
163
207
s1.dist = 2 ;
164
208
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
+
165
215
root->appendChild ( new QgsRuleBasedLabeling::Rule ( new QgsPalLayerSettings ( s1 ) ) );
166
216
167
217
QgsPalLayerSettings s2;
@@ -170,18 +220,22 @@ void TestQgsLabelingEngineV2::testRuleBased()
170
220
s2.obstacle = false ;
171
221
s2.dist = 2 ;
172
222
s2.textColor = Qt::red;
223
+ s2.textFont = QgsFontUtils::getStandardTestFont ( " Bold" );
224
+ s2.placement = QgsPalLayerSettings::OverPoint;
225
+ s2.quadOffset = QgsPalLayerSettings::QuadrantBelowRight;
226
+ s2.displayAll = true ;
173
227
s2.setDataDefinedProperty ( QgsPalLayerSettings::Size, true , true , " 18" , QString () );
174
228
175
229
root->appendChild ( new QgsRuleBasedLabeling::Rule ( new QgsPalLayerSettings ( s2 ), 0 , 0 , " Class = 'Jet'" ) );
176
230
177
231
vl->setLabeling ( new QgsRuleBasedLabeling ( root ) );
232
+ setDefaultLabelParams ( vl );
178
233
179
234
QgsMapRendererSequentialJob job ( mapSettings );
180
235
job.start ();
181
236
job.waitForFinished ();
182
237
QImage img = job.renderedImage ();
183
-
184
- img.save ( " /tmp/rules.png" );
238
+ QVERIFY ( imageCheck ( " labeling_rulebased" , img, 0 ) );
185
239
186
240
// test read/write rules
187
241
QDomDocument doc, doc2, doc3;
@@ -213,5 +267,28 @@ void TestQgsLabelingEngineV2::testRuleBased()
213
267
214
268
}
215
269
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
+
216
293
QTEST_MAIN ( TestQgsLabelingEngineV2 )
217
294
#include " testqgslabelingenginev2.moc"
0 commit comments