|
| 1 | +/*************************************************************************** |
| 2 | + testqgsdatadefinedsizelegend.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : June 2017 |
| 5 | + Copyright : (C) 2017 by Martin Dobias |
| 6 | + Email : wonder dot sk at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgstest.h" |
| 17 | + |
| 18 | +#include "qgsdatadefinedsizelegend.h" |
| 19 | +#include "qgsfontutils.h" |
| 20 | +#include "qgsrenderchecker.h" |
| 21 | +#include "qgssymbol.h" |
| 22 | + |
| 23 | + |
| 24 | +static QString _fileNameForTest( const QString &testName ) |
| 25 | +{ |
| 26 | + return QDir::tempPath() + '/' + testName + ".png"; |
| 27 | +} |
| 28 | + |
| 29 | +static bool _verifyImage( const QString &testName, QString &report ) |
| 30 | +{ |
| 31 | + QgsRenderChecker checker; |
| 32 | + checker.setControlPathPrefix( QStringLiteral( "data_defined_size_legend" ) ); |
| 33 | + checker.setControlName( "expected_" + testName ); |
| 34 | + checker.setRenderedImage( _fileNameForTest( testName ) ); |
| 35 | + checker.setSizeTolerance( 3, 3 ); |
| 36 | + bool equal = checker.compareImages( testName, 500 ); |
| 37 | + report += checker.report(); |
| 38 | + return equal; |
| 39 | +} |
| 40 | + |
| 41 | +static QgsRenderContext _createRenderContext( double mupp, double dpi, double scale ) |
| 42 | +{ |
| 43 | + QgsRenderContext context; |
| 44 | + context.setScaleFactor( dpi / 25.4 ); |
| 45 | + context.setRendererScale( scale ); |
| 46 | + context.setMapToPixel( QgsMapToPixel( mupp ) ); |
| 47 | + return context; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +/** \ingroup UnitTests |
| 52 | + * This is a unit test for legend rendering when using data-defined size of markers. |
| 53 | + */ |
| 54 | +class TestQgsDataDefinedSizeLegend : public QObject |
| 55 | +{ |
| 56 | + Q_OBJECT |
| 57 | + |
| 58 | + public: |
| 59 | + |
| 60 | + private slots: |
| 61 | + void initTestCase();// will be called before the first testfunction is executed. |
| 62 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 63 | + |
| 64 | + void testBasic(); |
| 65 | + void testCrowded(); |
| 66 | + |
| 67 | + private: |
| 68 | + QString mReport; |
| 69 | +}; |
| 70 | + |
| 71 | +void TestQgsDataDefinedSizeLegend::initTestCase() |
| 72 | +{ |
| 73 | + // Runs once before any tests are run |
| 74 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 75 | + QgsApplication::init(); |
| 76 | + QgsApplication::initQgis(); |
| 77 | + QgsApplication::showSettings(); |
| 78 | + |
| 79 | + mReport += QLatin1String( "<h1>Data Defined Size Legend Tests</h1>\n" ); |
| 80 | +} |
| 81 | + |
| 82 | +void TestQgsDataDefinedSizeLegend::cleanupTestCase() |
| 83 | +{ |
| 84 | + QString myReportFile = QDir::tempPath() + "/qgistest.html"; |
| 85 | + QFile myFile( myReportFile ); |
| 86 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 87 | + { |
| 88 | + QTextStream myQTextStream( &myFile ); |
| 89 | + myQTextStream << mReport; |
| 90 | + myFile.close(); |
| 91 | + } |
| 92 | + |
| 93 | + QgsApplication::exitQgis(); |
| 94 | +} |
| 95 | + |
| 96 | +void TestQgsDataDefinedSizeLegend::testBasic() |
| 97 | +{ |
| 98 | + QgsDataDefinedSizeLegend settings; |
| 99 | + settings.setLegendType( QgsDataDefinedSizeLegend::LegendCollapsed ); |
| 100 | + settings.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Roman" ) ) ); |
| 101 | + |
| 102 | + QList<QgsDataDefinedSizeLegend::SizeClass> classes; |
| 103 | + classes << QgsDataDefinedSizeLegend::SizeClass( 3., QString( "3" ) ); |
| 104 | + classes << QgsDataDefinedSizeLegend::SizeClass( 15., QString( "15" ) ); |
| 105 | + classes << QgsDataDefinedSizeLegend::SizeClass( 30., QString( "30" ) ); |
| 106 | + settings.setClasses( classes ); |
| 107 | + |
| 108 | + QgsStringMap props; |
| 109 | + props["name"] = "circle"; |
| 110 | + props["color"] = "200,255,200"; |
| 111 | + props["outline_color"] = "0,255,0"; |
| 112 | + |
| 113 | + settings.setSymbol( QgsMarkerSymbol::createSimple( props ) ); // takes ownership |
| 114 | + |
| 115 | + QgsRenderContext context( _createRenderContext( 100, 96, 100 ) ); |
| 116 | + |
| 117 | + QImage imgBottom = settings.collapsedLegendImage( context, 1 ); |
| 118 | + imgBottom.save( _fileNameForTest( "basic_bottom" ) ); |
| 119 | + QVERIFY( _verifyImage( "basic_bottom", mReport ) ); |
| 120 | + |
| 121 | + settings.setVerticalAlignment( QgsDataDefinedSizeLegend::AlignCenter ); |
| 122 | + |
| 123 | + QImage imgCenter = settings.collapsedLegendImage( context, 1 ); |
| 124 | + imgCenter.save( _fileNameForTest( "basic_center" ) ); |
| 125 | + QVERIFY( _verifyImage( "basic_center", mReport ) ); |
| 126 | +} |
| 127 | + |
| 128 | +void TestQgsDataDefinedSizeLegend::testCrowded() |
| 129 | +{ |
| 130 | + QgsDataDefinedSizeLegend settings; |
| 131 | + settings.setLegendType( QgsDataDefinedSizeLegend::LegendCollapsed ); |
| 132 | + settings.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Roman" ) ) ); |
| 133 | + |
| 134 | + QList<QgsDataDefinedSizeLegend::SizeClass> classes; |
| 135 | + classes << QgsDataDefinedSizeLegend::SizeClass( 2., QString( "2" ) ); |
| 136 | + classes << QgsDataDefinedSizeLegend::SizeClass( 5., QString( "5" ) ); |
| 137 | + classes << QgsDataDefinedSizeLegend::SizeClass( 10., QString( "10" ) ); |
| 138 | + classes << QgsDataDefinedSizeLegend::SizeClass( 12., QString( "12" ) ); |
| 139 | + classes << QgsDataDefinedSizeLegend::SizeClass( 15., QString( "15" ) ); |
| 140 | + classes << QgsDataDefinedSizeLegend::SizeClass( 18., QString( "18" ) ); |
| 141 | + settings.setClasses( classes ); |
| 142 | + |
| 143 | + QgsStringMap props; |
| 144 | + props["name"] = "circle"; |
| 145 | + props["color"] = "200,255,200"; |
| 146 | + props["outline_color"] = "0,255,0"; |
| 147 | + |
| 148 | + settings.setSymbol( QgsMarkerSymbol::createSimple( props ) ); // takes ownership |
| 149 | + |
| 150 | + QgsRenderContext context( _createRenderContext( 100, 96, 100 ) ); |
| 151 | + |
| 152 | + QImage img = settings.collapsedLegendImage( context, 1 ); |
| 153 | + img.save( _fileNameForTest( "crowded" ) ); |
| 154 | + |
| 155 | + QVERIFY( _verifyImage( "crowded", mReport ) ); |
| 156 | +} |
| 157 | + |
| 158 | +QGSTEST_MAIN( TestQgsDataDefinedSizeLegend ) |
| 159 | +#include "testqgsdatadefinedsizelegend.moc" |
0 commit comments