Skip to content

Commit eb9ab8a

Browse files
committedMay 17, 2016
add unit test for extent
1 parent b930a4b commit eb9ab8a

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
 

‎tests/src/gui/testqgsmapcanvas.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <qgsmaprenderer.h>
2222
#include <qgsmaplayerregistry.h>
2323
#include <qgsrenderchecker.h>
24+
#include <qgsvectordataprovider.h>
2425

2526
namespace QTest
2627
{
@@ -47,6 +48,7 @@ class TestQgsMapCanvas : public QObject
4748
void testMapRendererInteraction();
4849
void testPanByKeyboard();
4950
void testMagnification();
51+
void testMagnificationExtent();
5052

5153
private:
5254
QgsMapCanvas* mCanvas;
@@ -251,5 +253,96 @@ void TestQgsMapCanvas::testMagnification()
251253
QCOMPARE( checker.compareImages( "map_magnification", 100 ), true );
252254
}
253255

256+
void compareExtent( const QgsRectangle &initialExtent,
257+
const QgsRectangle &extent )
258+
{
259+
QVERIFY( qgsDoubleNear( initialExtent.xMinimum(), extent.xMinimum(), 0.00000000001 ) );
260+
QVERIFY( qgsDoubleNear( initialExtent.xMaximum(), extent.xMaximum(), 0.00000000001 ) );
261+
QVERIFY( qgsDoubleNear( initialExtent.yMinimum(), extent.yMinimum(), 0.00000000001 ) );
262+
QVERIFY( qgsDoubleNear( initialExtent.yMaximum(), extent.yMaximum(), 0.00000000001 ) );
263+
}
264+
265+
void TestQgsMapCanvas::testMagnificationExtent()
266+
{
267+
// build vector layer
268+
QString testDataDir = QString( TEST_DATA_DIR ) + '/';
269+
QString myPointsFileName = testDataDir + "points.shp";
270+
QFileInfo myPointFileInfo( myPointsFileName );
271+
QgsVectorLayer *layer = new QgsVectorLayer( myPointFileInfo.filePath(),
272+
myPointFileInfo.completeBaseName(), "ogr" );
273+
274+
// prepare map canvas
275+
QList<QgsMapCanvasLayer> layers;
276+
layers.append( layer );
277+
mCanvas->setLayerSet( layers );
278+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << layer );
279+
280+
// zoomToFullExtent
281+
mCanvas->zoomToFullExtent();
282+
QgsRectangle initialExtent = mCanvas->extent();
283+
284+
mCanvas->setMagnificationFactor( 4.0 );
285+
mCanvas->setMagnificationFactor( 1.0 );
286+
287+
compareExtent( mCanvas->extent(), initialExtent );
288+
289+
// setExtent with layer extent
290+
mCanvas->setExtent( layer->extent() );
291+
initialExtent = mCanvas->extent();
292+
293+
mCanvas->setMagnificationFactor( 4.0 );
294+
mCanvas->setMagnificationFactor( 1.0 );
295+
296+
compareExtent( mCanvas->extent(), initialExtent );
297+
298+
// zoomToSelected
299+
QgsFeature f1( layer->dataProvider()->fields(), 1 );
300+
QgsFeature f2( layer->dataProvider()->fields(), 2 );
301+
QgsFeatureIds ids;
302+
ids << f1.id() << f2.id();
303+
layer->setSelectedFeatures( ids );
304+
305+
mCanvas->zoomToSelected( layer );
306+
initialExtent = mCanvas->extent();
307+
308+
mCanvas->setMagnificationFactor( 4.0 );
309+
mCanvas->setMagnificationFactor( 1.0 );
310+
311+
compareExtent( mCanvas->extent(), initialExtent );
312+
313+
// zoomToFeatureIds
314+
mCanvas->zoomToFeatureIds( layer, ids );
315+
initialExtent = mCanvas->extent();
316+
317+
mCanvas->setMagnificationFactor( 4.0 );
318+
mCanvas->setMagnificationFactor( 1.0 );
319+
320+
compareExtent( mCanvas->extent(), initialExtent );
321+
322+
// zoomIn / zoomOut
323+
initialExtent = mCanvas->extent();
324+
mCanvas->zoomIn();
325+
326+
mCanvas->setMagnificationFactor( 4.0 );
327+
mCanvas->zoomIn();
328+
mCanvas->zoomOut();
329+
mCanvas->setMagnificationFactor( 1.0 );
330+
331+
mCanvas->zoomOut();
332+
333+
compareExtent( mCanvas->extent(), initialExtent );
334+
335+
// zoomScale
336+
initialExtent = mCanvas->extent();
337+
double scale = mCanvas->scale();
338+
mCanvas->zoomScale( 6.052017*10e7 );
339+
340+
mCanvas->setMagnificationFactor( 4.0 );
341+
mCanvas->setMagnificationFactor( 1.0 );
342+
343+
mCanvas->zoomScale( scale );
344+
compareExtent( mCanvas->extent(), initialExtent );
345+
}
346+
254347
QTEST_MAIN( TestQgsMapCanvas )
255348
#include "testqgsmapcanvas.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.