/
testqgsrenderers.cpp
229 lines (209 loc) · 7.43 KB
/
testqgsrenderers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***************************************************************************
testqgsrenderers.cpp
--------------------------------------
Date : 20 Jan 2008
Copyright : (C) 2008 by Tim Sutton
Email : tim @ linfiniti.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QObject>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QDesktopServices>
#include <iostream>
//qgis includes...
#include <qgsmaprenderer.h>
#include <qgsmaplayer.h>
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmaplayerregistry.h>
//qgis test includes
#include "qgsrenderchecker.h"
/** \ingroup UnitTests
* This is a unit test for the different renderers for vector layers.
*/
class TestQgsRenderers: public QObject
{
Q_OBJECT;
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {};// will be called before each testfunction is executed.
void cleanup() {};// will be called after every testfunction.
void singleSymbol();
// void uniqueValue();
// void graduatedSymbol();
// void continuousSymbol();
private:
bool mTestHasError;
bool setQml( QString theType ); //uniquevalue / continuous / single /
bool imageCheck( QString theType ); //as above
QgsMapRenderer * mpMapRenderer;
QgsMapLayer * mpPointsLayer;
QgsMapLayer * mpLinesLayer;
QgsMapLayer * mpPolysLayer;
QString mTestDataDir;
QString mReport;
};
void TestQgsRenderers::initTestCase()
{
mTestHasError = false;
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();
//create some objects that will be used in all tests...
//
//create a point layer that will be used in all tests...
//
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
mTestDataDir = myDataDir + QDir::separator();
QString myPointsFileName = mTestDataDir + "points.shp";
QFileInfo myPointFileInfo( myPointsFileName );
mpPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
myPointFileInfo.completeBaseName(), "ogr" );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << mpPointsLayer );
//
//create a poly layer that will be used in all tests...
//
QString myPolysFileName = mTestDataDir + "polys.shp";
QFileInfo myPolyFileInfo( myPolysFileName );
mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(),
myPolyFileInfo.completeBaseName(), "ogr" );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << mpPolysLayer );
//
// Create a line layer that will be used in all tests...
//
QString myLinesFileName = mTestDataDir + "lines.shp";
QFileInfo myLineFileInfo( myLinesFileName );
mpLinesLayer = new QgsVectorLayer( myLineFileInfo.filePath(),
myLineFileInfo.completeBaseName(), "ogr" );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << mpLinesLayer );
//
// We only need maprender instead of mapcanvas
// since maprender does not require a qui
// and is more light weight
//
mpMapRenderer = new QgsMapRenderer();
QStringList myLayers;
myLayers << mpPointsLayer->id();
myLayers << mpPolysLayer->id();
myLayers << mpLinesLayer->id();
mpMapRenderer->setLayerSet( myLayers );
mReport += "<h1>Vector Renderer Tests</h1>\n";
}
void TestQgsRenderers::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
//QDesktopServices::openUrl( "file:///" + myReportFile );
}
}
void TestQgsRenderers::singleSymbol()
{
mReport += "<h2>Single symbol renderer test</h2>\n";
QVERIFY( setQml( "single" ) );
QVERIFY( imageCheck( "single" ) );
}
// TODO: update tests and enable
/*
void TestQgsRenderers::uniqueValue()
{
mReport += "<h2>Unique value symbol renderer test</h2>\n";
QVERIFY( setQml( "uniquevalue" ) );
QVERIFY( imageCheck( "uniquevalue" ) );
}
void TestQgsRenderers::graduatedSymbol()
{
mReport += "<h2>Graduated symbol renderer test</h2>\n";
QVERIFY( setQml( "graduated" ) );
QVERIFY( imageCheck( "graduated" ) );
}
void TestQgsRenderers::continuousSymbol()
{
mReport += "<h2>Continuous symbol renderer test</h2>\n";
QVERIFY( setQml( "continuous" ) );
QVERIFY( imageCheck( "continuous" ) );
}
*/
//
// Private helper functions not called directly by CTest
//
bool TestQgsRenderers::setQml( QString theType )
{
//load a qml style and apply to our layer
//the style will correspond to the renderer
//type we are testing
if ( ! mpPointsLayer->isValid() )
{
return false;
}
QString myFileName = mTestDataDir + "points_" + theType + "_symbol.qml";
bool myStyleFlag = false;
QString error = mpPointsLayer->loadNamedStyle( myFileName, myStyleFlag );
if ( !myStyleFlag )
{
qDebug( "%s", error.toLocal8Bit().constData() );
return false;
}
else
{
myStyleFlag = false; //ready for next test
}
myFileName = mTestDataDir + "polys_" + theType + "_symbol.qml";
mpPolysLayer->loadNamedStyle( myFileName, myStyleFlag );
if ( !myStyleFlag )
{
return false;
}
else
{
myStyleFlag = false; //ready for next test
}
myFileName = mTestDataDir + "lines_" + theType + "_symbol.qml";
mpLinesLayer->loadNamedStyle( myFileName, myStyleFlag );
return myStyleFlag;
}
bool TestQgsRenderers::imageCheck( QString theTestType )
{
//use the QgsRenderChecker test utility class to
//ensure the rendered output matches our control image
// mpPointsLayer->extent() was giving wrong extent in QGIS 2.0 (xmin shifted,
// the same wrong value is reported by ogrinfo). Since QGIS 2.1, the provider
// gives correct extent. Forced to fixed extend however to avoid problems in future.
QgsRectangle extent( -118.8888888888887720, 22.8002070393376783, -83.3333333333331581, 46.8719806763287536 );
mpMapRenderer->setExtent( extent );
mpMapRenderer->rendererContext()->setForceVectorOutput( true );
QgsRenderChecker myChecker;
myChecker.setControlName( "expected_" + theTestType );
myChecker.setMapRenderer( mpMapRenderer );
myChecker.setColorTolerance( 15 );
bool myResultFlag = myChecker.runTest( theTestType );
mReport += myChecker.report();
return myResultFlag;
}
QTEST_MAIN( TestQgsRenderers )
#include "moc_testqgsrenderers.cxx"