Skip to content
Permalink
Browse files

Added unit tests

  • Loading branch information
wonder-sk committed Jun 19, 2017
1 parent e3270ed commit c9c42169ce9b16e64df7e810fc34fb2a5cf0abd6
@@ -0,0 +1,159 @@
/***************************************************************************
testqgsdatadefinedsizelegend.cpp
--------------------------------------
Date : June 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot 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 "qgstest.h"

#include "qgsdatadefinedsizelegend.h"
#include "qgsfontutils.h"
#include "qgsrenderchecker.h"
#include "qgssymbol.h"


static QString _fileNameForTest( const QString &testName )
{
return QDir::tempPath() + '/' + testName + ".png";
}

static bool _verifyImage( const QString &testName, QString &report )
{
QgsRenderChecker checker;
checker.setControlPathPrefix( QStringLiteral( "data_defined_size_legend" ) );
checker.setControlName( "expected_" + testName );
checker.setRenderedImage( _fileNameForTest( testName ) );
checker.setSizeTolerance( 3, 3 );
bool equal = checker.compareImages( testName, 500 );
report += checker.report();
return equal;
}

static QgsRenderContext _createRenderContext( double mupp, double dpi, double scale )
{
QgsRenderContext context;
context.setScaleFactor( dpi / 25.4 );
context.setRendererScale( scale );
context.setMapToPixel( QgsMapToPixel( mupp ) );
return context;
}


/** \ingroup UnitTests
* This is a unit test for legend rendering when using data-defined size of markers.
*/
class TestQgsDataDefinedSizeLegend : public QObject
{
Q_OBJECT

public:

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 testBasic();
void testCrowded();

private:
QString mReport;
};

void TestQgsDataDefinedSizeLegend::initTestCase()
{
// Runs once before any tests are run
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();

mReport += QLatin1String( "<h1>Data Defined Size Legend Tests</h1>\n" );
}

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

QgsApplication::exitQgis();
}

void TestQgsDataDefinedSizeLegend::testBasic()
{
QgsDataDefinedSizeLegend settings;
settings.setLegendType( QgsDataDefinedSizeLegend::LegendCollapsed );
settings.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Roman" ) ) );

QList<QgsDataDefinedSizeLegend::SizeClass> classes;
classes << QgsDataDefinedSizeLegend::SizeClass( 3., QString( "3" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 15., QString( "15" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 30., QString( "30" ) );
settings.setClasses( classes );

QgsStringMap props;
props["name"] = "circle";
props["color"] = "200,255,200";
props["outline_color"] = "0,255,0";

settings.setSymbol( QgsMarkerSymbol::createSimple( props ) ); // takes ownership

QgsRenderContext context( _createRenderContext( 100, 96, 100 ) );

QImage imgBottom = settings.collapsedLegendImage( context, 1 );
imgBottom.save( _fileNameForTest( "basic_bottom" ) );
QVERIFY( _verifyImage( "basic_bottom", mReport ) );

settings.setVerticalAlignment( QgsDataDefinedSizeLegend::AlignCenter );

QImage imgCenter = settings.collapsedLegendImage( context, 1 );
imgCenter.save( _fileNameForTest( "basic_center" ) );
QVERIFY( _verifyImage( "basic_center", mReport ) );
}

void TestQgsDataDefinedSizeLegend::testCrowded()
{
QgsDataDefinedSizeLegend settings;
settings.setLegendType( QgsDataDefinedSizeLegend::LegendCollapsed );
settings.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Roman" ) ) );

QList<QgsDataDefinedSizeLegend::SizeClass> classes;
classes << QgsDataDefinedSizeLegend::SizeClass( 2., QString( "2" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 5., QString( "5" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 10., QString( "10" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 12., QString( "12" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 15., QString( "15" ) );
classes << QgsDataDefinedSizeLegend::SizeClass( 18., QString( "18" ) );
settings.setClasses( classes );

QgsStringMap props;
props["name"] = "circle";
props["color"] = "200,255,200";
props["outline_color"] = "0,255,0";

settings.setSymbol( QgsMarkerSymbol::createSimple( props ) ); // takes ownership

QgsRenderContext context( _createRenderContext( 100, 96, 100 ) );

QImage img = settings.collapsedLegendImage( context, 1 );
img.save( _fileNameForTest( "crowded" ) );

QVERIFY( _verifyImage( "crowded", mReport ) );
}

QGSTEST_MAIN( TestQgsDataDefinedSizeLegend )
#include "testqgsdatadefinedsizelegend.moc"
@@ -127,6 +127,7 @@ class TestQgsLegendRenderer : public QObject
void testFilterByExpression();
void testDiagramAttributeLegend();
void testDiagramSizeLegend();
void testDataDefinedSizeCollapsed();

private:
QgsLayerTree *mRoot = nullptr;
@@ -726,5 +727,67 @@ void TestQgsLegendRenderer::testDiagramSizeLegend()
QgsProject::instance()->removeMapLayer( vl4 );
}


void TestQgsLegendRenderer::testDataDefinedSizeCollapsed()
{
QString testName = QStringLiteral( "legend_data_defined_size_collapsed" );

QgsVectorLayer *vlDataDefinedSize = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "Point Layer" ), QStringLiteral( "memory" ) );
{
QgsVectorDataProvider *pr = vlDataDefinedSize->dataProvider();
QList<QgsField> attrs;
attrs << QgsField( QStringLiteral( "test_attr" ), QVariant::Int );
pr->addAttributes( attrs );

QgsFields fields;
fields.append( attrs.back() );

QgsGeometry g = QgsGeometry::fromPoint( QgsPointXY( 1.0, 1.0 ) );

QList<QgsFeature> features;
QgsFeature f1( fields, 1 );
f1.setAttribute( 0, 100 );
f1.setGeometry( g );
QgsFeature f2( fields, 2 );
f2.setAttribute( 0, 200 );
f2.setGeometry( g );
QgsFeature f3( fields, 3 );
f3.setAttribute( 0, 300 );
f3.setGeometry( g );
features << f1 << f2 << f3;
pr->addFeatures( features );
vlDataDefinedSize->updateFields();
}

QgsStringMap props;
props["name"] = "circle";
props["color"] = "200,200,200";
props["outline_color"] = "0,0,0";
QgsMarkerSymbol *symbol = QgsMarkerSymbol::createSimple( props );
QgsProperty ddsProperty = QgsProperty::fromField( "test_attr" );
ddsProperty.setTransformer( new QgsSizeScaleTransformer( QgsSizeScaleTransformer::Linear, 100, 300, 10, 30 ) ); // takes ownership
symbol->setDataDefinedSize( ddsProperty );

QgsDataDefinedSizeLegend *ddsLegend = new QgsDataDefinedSizeLegend();
ddsLegend->setLegendType( QgsDataDefinedSizeLegend::LegendCollapsed );

QgsSingleSymbolRenderer *r = new QgsSingleSymbolRenderer( symbol ); // takes ownership
r->setDataDefinedSizeLegend( ddsLegend );
vlDataDefinedSize->setRenderer( r );

QgsLayerTree *root = new QgsLayerTree();
root->addLayer( vlDataDefinedSize );

QgsLayerTreeModel legendModel( root );

QgsLegendSettings settings;
_setStandardTestFont( settings );
_renderLegend( testName, &legendModel, settings );
QVERIFY( _verifyImage( testName, mReport ) );

delete root;
}


QGSTEST_MAIN( TestQgsLegendRenderer )
#include "testqgslegendrenderer.moc"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit c9c4216

Please sign in to comment.
You can’t perform that action at this time.