Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add test for geometry generator labeling
  • Loading branch information
m-kuhn committed Mar 10, 2019
1 parent cf93a22 commit f4d5788
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
1 change: 0 additions & 1 deletion tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -956,7 +956,6 @@ void TestQgsLabelingEngine::testLabelBlockingRegion()

img = job3.renderedImage();
QVERIFY( imageCheck( QStringLiteral( "label_blocking_boundary_geometry" ), img, 20 ) );

}

QGSTEST_MAIN( TestQgsLabelingEngine )
Expand Down
116 changes: 115 additions & 1 deletion tests/src/core/testqgspallabeling.cpp
Expand Up @@ -18,6 +18,13 @@
#include <QStringList>

#include "qgspallabeling.h"
#include "qgsfontutils.h"
#include "qgsvectorlayer.h"
#include "qgsnullsymbolrenderer.h"
#include "qgsvectorlayerlabeling.h"
#include "qgsmapsettings.h"
#include "qgsmaprenderersequentialjob.h"
#include "qgsrenderchecker.h"

class TestQgsPalLabeling: public QObject
{
Expand All @@ -30,8 +37,11 @@ class TestQgsPalLabeling: public QObject
void cleanup();// will be called after every testfunction.
void wrapChar();//test wrapping text lines
void graphemes(); //test splitting strings to graphemes
bool imageCheck( const QString &testName, QImage &image, int mismatchCount );
void testGeometryGenerator();

private:
QString mReport;
};

void TestQgsPalLabeling::initTestCase()
Expand All @@ -40,7 +50,15 @@ void TestQgsPalLabeling::initTestCase()

void TestQgsPalLabeling::cleanupTestCase()
{

QgsApplication::exitQgis();
QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsPalLabeling::init()
Expand Down Expand Up @@ -177,5 +195,101 @@ void TestQgsPalLabeling::graphemes()
<< expected2Pt11 );
}

bool TestQgsPalLabeling::imageCheck( const QString &testName, QImage &image, int mismatchCount )
{
//draw background
QImage imageWithBackground( image.width(), image.height(), QImage::Format_RGB32 );
QgsRenderChecker::drawBackground( &imageWithBackground );
QPainter painter( &imageWithBackground );
painter.drawImage( 0, 0, image );
painter.end();

mReport += "<h2>" + testName + "</h2>\n";
QString tempDir = QDir::tempPath() + '/';
QString fileName = tempDir + testName + ".png";
imageWithBackground.save( fileName, "PNG" );
QgsRenderChecker checker;
checker.setControlPathPrefix( QStringLiteral( "pallabeling" ) );
checker.setControlName( "expected_" + testName );
checker.setRenderedImage( fileName );
checker.setColorTolerance( 2 );
bool resultFlag = checker.compareImages( testName, mismatchCount );
mReport += checker.report();
return resultFlag;
}

void TestQgsPalLabeling::testGeometryGenerator()
{
// test that no labels are drawn outside of the specified label boundary
QgsPalLayerSettings settings;

QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
format.setSize( 12 );
format.setNamedStyle( QStringLiteral( "Bold" ) );
format.setColor( QColor( 0, 0, 0 ) );
settings.setFormat( format );

settings.fieldName = QStringLiteral( "'X'" );
settings.isExpression = true;

settings.placement = QgsPalLayerSettings::OverPoint;
settings.geometryGeneratorEnabled = true;
settings.geometryGeneratorType = QgsWkbTypes::PointGeometry;
settings.geometryGenerator = "translate($geometry, 1, 0)";

std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326&field=id:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );

vl2->setRenderer( new QgsNullSymbolRenderer() );

// DEBUG HINT:
// Labels should be rendered with an offset of their original geometry. To debug, enable rendering the geometry itself.
// vl2->setRenderer( new QgsSingleSymbolRenderer( new QgsMarkerSymbol() ) );


QgsFeature f( vl2->fields(), 1 );

for ( int x = 0; x < 17; x += 3 )
{
for ( int y = 0; y < 10; y += 3 )
{
f.setGeometry( qgis::make_unique< QgsPoint >( x, y ) );
vl2->dataProvider()->addFeature( f );
}
}

vl2->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vl2->setLabelsEnabled( true );

// make a fake render context
QSize size( 640, 480 );
QgsMapSettings mapSettings;
QgsCoordinateReferenceSystem tgtCrs;
tgtCrs.createFromString( QStringLiteral( "EPSG:4326" ) );
mapSettings.setDestinationCrs( tgtCrs );

mapSettings.setOutputSize( size );
mapSettings.setExtent( vl2->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vl2.get() );
mapSettings.setOutputDpi( 96 );

QgsMapRendererSequentialJob job( mapSettings );
job.start();
job.waitForFinished();

QImage img = job.renderedImage();
QVERIFY( imageCheck( QStringLiteral( "geometry_generator_translated" ), img, 20 ) );

// with rotation
mapSettings.setRotation( 45 );
QgsMapRendererSequentialJob job2( mapSettings );
job2.start();
job2.waitForFinished();

img = job2.renderedImage();
QVERIFY( imageCheck( QStringLiteral( "rotated_geometry_generator_translated" ), img, 20 ) );
}


QGSTEST_MAIN( TestQgsPalLabeling )
#include "testqgspallabeling.moc"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f4d5788

Please sign in to comment.