Skip to content

Commit

Permalink
Add test for rendering of adjacent tile images
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jan 7, 2016
1 parent 15a8bc9 commit 886fd72
Show file tree
Hide file tree
Showing 9 changed files with 1,187 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class QgsVectorLayerDiagramProvider;
* @note added in 2.4
* @note not available in Python bindings
*/
class QgsVectorLayerRenderer : public QgsMapLayerRenderer
class CORE_EXPORT QgsVectorLayerRenderer : public QgsMapLayerRenderer
{
public:
QgsVectorLayerRenderer( QgsVectorLayer* layer, QgsRenderContext& context );
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@ ADD_QGIS_TEST(vectorlayercachetest testqgsvectorlayercache.cpp )
ADD_QGIS_TEST(vectorlayerjoinbuffer testqgsvectorlayerjoinbuffer.cpp )
ADD_QGIS_TEST(vectorlayertest testqgsvectorlayer.cpp)
ADD_QGIS_TEST(ziplayertest testziplayer.cpp)
ADD_QGIS_TEST(adjacenttiles testqgsadjacenttiles.cpp)
ADD_DEPENDENCIES(qgis_coordinatereferencesystemtest synccrsdb)
171 changes: 171 additions & 0 deletions tests/src/core/testqgsadjacenttiles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/***************************************************************************
testadjacenttiles.cpp
--------------------------------------
Date : Jan 7 2016
Copyright : (C) 2016 by Marco Hugentobler
Email : marco at sourcepole dot ch
***************************************************************************
* *
* 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/QtTest>
#include <QObject>

#include "qgsapplication.h"
#include "qgsmaplayerregistry.h"
#include <qgsrenderchecker.h>
#include "qgsvectorlayer.h"
#include "qgsvectorlayerrenderer.h"

/** \ingroup UnitTests
* This unit test checks if rendering of adjacent tiles (e.g. to render images for tile caches)
* does not result in border effects
*/
class TestQgsAdjacentTiles : public QObject
{
Q_OBJECT
public:
TestQgsAdjacentTiles() {}
~TestQgsAdjacentTiles() {}

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 testFourAdjacentTiles_data();
void testFourAdjacentTiles();
};

void TestQgsAdjacentTiles::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
}

void TestQgsAdjacentTiles::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsAdjacentTiles::testFourAdjacentTiles_data()
{
QTest::addColumn<QStringList>( "bboxList" );
QTest::addColumn<QString>( "resultFile" );
QTest::addColumn<QString>( "shapeFile" );
QTest::addColumn<QString>( "qmlFile" );

QString shapeFile = TEST_DATA_DIR + QString( "/france_parts.shp" );
QString qmlFile = TEST_DATA_DIR + QString( "/adjacent_tiles/line_pattern_30_degree.qml" );
QString resultFile = TEST_DATA_DIR + QString( "/adjacent_tiles/testFourAdjacentTiles1_expected.png" );

QStringList bboxList1;
bboxList1 << "-1.5,48,-0.5,49";
bboxList1 << "-0.5,48,0.5,49";
bboxList1 << "-1.5,47,-0.5,48";
bboxList1 << "-0.5,47,0.5,48";

QTest::newRow( "testFourAdjacentTiles1" ) << bboxList1 << resultFile << shapeFile << qmlFile;

qmlFile = TEST_DATA_DIR + QString( "/adjacent_tiles/point_pattern_simple_marker.qml" );
resultFile = TEST_DATA_DIR + QString( "/adjacent_tiles/testFourAdjacentTiles2_expected.png" );

QTest::newRow( "testFourAdjacentTiles2" ) << bboxList1 << resultFile << shapeFile << qmlFile;

shapeFile = TEST_DATA_DIR + QString( "/lines.shp" );
qmlFile = TEST_DATA_DIR + QString( "/adjacent_tiles/simple_line_dashed.qml" );
resultFile = TEST_DATA_DIR + QString( "/adjacent_tiles/testFourAdjacentTiles3_expected.png" );

QStringList bboxList2;
bboxList2 << "-105,35,-95,45";
bboxList2 << "-95,35,-85,45";
bboxList2 << "-105,25,-95,35";
bboxList2 << "-95,25,-85,35";

QTest::newRow( "testFourAdjacentTiles3" ) << bboxList2 << resultFile << shapeFile << qmlFile;
}

void TestQgsAdjacentTiles::testFourAdjacentTiles()
{
QFETCH( QStringList, bboxList );
QFETCH( QString, resultFile );
QFETCH( QString, shapeFile );
QFETCH( QString, qmlFile );

QVERIFY( bboxList.size() == 4 );

//create maplayer, set QML and add to maplayer registry
QgsVectorLayer* vectorLayer = new QgsVectorLayer( shapeFile, "testshape", "ogr" );

//todo: read QML
QFile symbologyFile( qmlFile );
if ( !symbologyFile.open( QIODevice::ReadOnly ) )
{
QFAIL( "Open symbology file failed" );
}

QDomDocument qmlDoc;
if ( !qmlDoc.setContent( &symbologyFile ) )
{
QFAIL( "QML file not valid" );
}

QString errorMsg;
if ( !vectorLayer->readSymbology( qmlDoc.documentElement(), errorMsg ) )
{
QFAIL( errorMsg.toLocal8Bit().data() );
}

QImage globalImage( 512, 512, QImage::Format_ARGB32_Premultiplied );
globalImage.fill( Qt::white );
QPainter globalPainter( &globalImage );

for ( int i = 0; i < 4; ++i )
{
QImage img( 256, 256, QImage::Format_ARGB32_Premultiplied );
img.fill( Qt::white );
QPainter p( &img );

QgsRenderContext renderContext;
renderContext.setPainter( &p );
renderContext.setFlag( QgsRenderContext::RenderMapTile, true );

//extent
QStringList rectCoords = bboxList.at( i ).split( "," );
if ( rectCoords.size() != 4 )
{
QFAIL( "bbox string invalid" );
}
QgsRectangle rect( rectCoords[0].toDouble(), rectCoords[1].toDouble(), rectCoords[2].toDouble(), rectCoords[3].toDouble() );
renderContext.setExtent( rect );

//maptopixel
QgsMapToPixel mapToPixel( rect.width() / 256.0, rect.center().x(), rect.center().y(), 256, 256, 0 );
renderContext.setMapToPixel( mapToPixel );

QgsVectorLayerRenderer renderer( vectorLayer, renderContext );
if ( !renderer.render() )
{
QFAIL( "Rendering failed" );
}

int globalImageX = ( i % 2 ) * 256;
int globalImageY = ( i < 2 ) ? 0 : 256;
globalPainter.drawImage( globalImageX, globalImageY, img );
}

delete vectorLayer;

QString renderedImagePath = QDir::tempPath() + "/" + QTest::currentDataTag() + QString( ".png" );
globalImage.save( renderedImagePath );

QgsRenderChecker checker;
checker.setControlName( QTest::currentDataTag() );
QVERIFY( checker.compareImages( QTest::currentDataTag(), 0, renderedImagePath ) );
}

QTEST_MAIN( TestQgsAdjacentTiles )
#include "testqgsadjacenttiles.moc"
Loading

0 comments on commit 886fd72

Please sign in to comment.