Skip to content

Commit 77ede9c

Browse files
committed
Boost unit test coverage of paint effects
1 parent 18c856d commit 77ede9c

File tree

7 files changed

+253
-1
lines changed

7 files changed

+253
-1
lines changed

tests/src/core/testqgspainteffect.cpp

Lines changed: 253 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
#include "qgspainteffectregistry.h"
3030
#include "qgsvectorcolorrampv2.h"
3131
#include "qgssymbollayerv2utils.h"
32+
#include "qgsmapsettings.h"
33+
#include "qgsvectorlayer.h"
34+
#include "qgsapplication.h"
35+
#include "qgsmaplayerregistry.h"
36+
#include "qgssymbolv2.h"
37+
#include "qgssinglesymbolrendererv2.h"
38+
#include "qgsfillsymbollayerv2.h"
39+
#include "qgslinesymbollayerv2.h"
40+
#include "qgsmarkersymbollayerv2.h"
41+
#include "qgscomposition.h"
42+
#include "qgscomposermap.h"
3243

3344
//qgis test includes
3445
#include "qgsmultirenderchecker.h"
@@ -91,21 +102,38 @@ class TestQgsPaintEffect: public QObject
91102

92103
void stack();
93104

105+
//rendering
106+
void layerEffectPolygon();
107+
void layerEffectLine();
108+
void layerEffectMarker();
109+
void vectorLayerEffect();
110+
void mapUnits();
111+
void composer();
112+
94113
private:
95-
bool imageCheck( QString testName , QImage &image, int mismatchCount );
114+
bool imageCheck( QString testName , QImage &image, int mismatchCount = 0 );
115+
bool mapRenderCheck( QString testName, QgsMapSettings &mapSettings, int mismatchCount = 0 );
116+
96117
QString mReport;
118+
QString mTestDataDir;
97119

98120
QPicture* mPicture;
99121
};
100122

101123

102124
void TestQgsPaintEffect::initTestCase()
103125
{
126+
QgsApplication::init();
127+
QgsApplication::initQgis();
128+
104129
mReport += "<h1>Paint Effect Tests</h1>\n";
105130
mPicture = 0;
106131

107132
QgsPaintEffectRegistry* registry = QgsPaintEffectRegistry::instance();
108133
registry->addEffectType( new QgsPaintEffectMetadata( "Dummy", "Dummy effect", DummyPaintEffect::create ) );
134+
135+
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
136+
mTestDataDir = myDataDir + QDir::separator();
109137
}
110138

111139
void TestQgsPaintEffect::cleanupTestCase()
@@ -118,6 +146,7 @@ void TestQgsPaintEffect::cleanupTestCase()
118146
myQTextStream << mReport;
119147
myFile.close();
120148
}
149+
QgsApplication::exitQgis();
121150
}
122151

123152
void TestQgsPaintEffect::init()
@@ -638,6 +667,218 @@ void TestQgsPaintEffect::stack()
638667
QVERIFY( result );
639668
}
640669

670+
void TestQgsPaintEffect::layerEffectPolygon()
671+
{
672+
// test rendering a polygon symbol layer with a paint effect
673+
674+
QString polysFileName = mTestDataDir + "polys.shp";
675+
QFileInfo polyFileInfo( polysFileName );
676+
QgsVectorLayer* polysLayer = new QgsVectorLayer( polyFileInfo.filePath(),
677+
polyFileInfo.completeBaseName(), "ogr" );
678+
QgsVectorSimplifyMethod simplifyMethod;
679+
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
680+
polysLayer->setSimplifyMethod( simplifyMethod );
681+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << polysLayer );
682+
683+
QgsMapSettings ms;
684+
QgsSimpleFillSymbolLayerV2* fill = new QgsSimpleFillSymbolLayerV2;
685+
fill->setColor( QColor( 255, 0, 0 ) );
686+
QgsDropShadowEffect* effect = new QgsDropShadowEffect();
687+
fill->setPaintEffect( effect );
688+
689+
QgsFillSymbolV2* fillSymbol = new QgsFillSymbolV2();
690+
fillSymbol->changeSymbolLayer( 0, fill );
691+
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( fillSymbol );
692+
693+
polysLayer->setRendererV2( renderer );
694+
ms.setLayers( QStringList() << polysLayer->id() );
695+
ms.setExtent( polysLayer->extent() );
696+
697+
mReport += "<h2>Paint effect symbol layer test (polygon)</h2>\n";
698+
bool result = mapRenderCheck( "painteffect_poly", ms );
699+
QVERIFY( result );
700+
}
701+
702+
void TestQgsPaintEffect::layerEffectLine()
703+
{
704+
// test rendering a line symbol layer with a paint effect
705+
QString linesFileName = mTestDataDir + "lines.shp";
706+
QFileInfo lineFileInfo( linesFileName );
707+
QgsVectorLayer* lineLayer = new QgsVectorLayer( lineFileInfo.filePath(),
708+
lineFileInfo.completeBaseName(), "ogr" );
709+
QgsVectorSimplifyMethod simplifyMethod;
710+
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
711+
lineLayer->setSimplifyMethod( simplifyMethod );
712+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << lineLayer );
713+
714+
QgsMapSettings ms;
715+
QgsSimpleLineSymbolLayerV2* line = new QgsSimpleLineSymbolLayerV2;
716+
line->setColor( QColor( 255, 0, 0 ) );
717+
line->setWidth( 1.0 );
718+
QgsDropShadowEffect* effect = new QgsDropShadowEffect();
719+
line->setPaintEffect( effect );
720+
721+
QgsLineSymbolV2* lineSymbol = new QgsLineSymbolV2();
722+
lineSymbol->changeSymbolLayer( 0, line );
723+
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( lineSymbol );
724+
725+
lineLayer->setRendererV2( renderer );
726+
ms.setLayers( QStringList() << lineLayer->id() );
727+
ms.setExtent( lineLayer->extent() );
728+
729+
mReport += "<h2>Paint effect symbol layer test (line)</h2>\n";
730+
bool result = mapRenderCheck( "painteffect_line", ms );
731+
QVERIFY( result );
732+
}
733+
734+
void TestQgsPaintEffect::layerEffectMarker()
735+
{
736+
// test rendering a marker symbol layer with a paint effect
737+
QString pointFileName = mTestDataDir + "points.shp";
738+
QFileInfo pointFileInfo( pointFileName );
739+
QgsVectorLayer* pointLayer = new QgsVectorLayer( pointFileInfo.filePath(),
740+
pointFileInfo.completeBaseName(), "ogr" );
741+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << pointLayer );
742+
743+
QgsMapSettings ms;
744+
QgsSimpleMarkerSymbolLayerV2* marker = new QgsSimpleMarkerSymbolLayerV2;
745+
marker->setColor( QColor( 255, 0, 0 ) );
746+
QgsDropShadowEffect* effect = new QgsDropShadowEffect();
747+
marker->setPaintEffect( effect );
748+
749+
QgsMarkerSymbolV2* markerSymbol = new QgsMarkerSymbolV2();
750+
markerSymbol->changeSymbolLayer( 0, marker );
751+
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( markerSymbol );
752+
753+
pointLayer->setRendererV2( renderer );
754+
ms.setLayers( QStringList() << pointLayer->id() );
755+
ms.setExtent( pointLayer->extent() );
756+
757+
mReport += "<h2>Paint effect symbol layer test (point)</h2>\n";
758+
bool result = mapRenderCheck( "painteffect_marker", ms );
759+
QVERIFY( result );
760+
}
761+
762+
void TestQgsPaintEffect::vectorLayerEffect()
763+
{
764+
// test rendering a whole vector layer with a layer-wide effect
765+
QString polysFileName = mTestDataDir + "polys.shp";
766+
QFileInfo polyFileInfo( polysFileName );
767+
QgsVectorLayer* polysLayer = new QgsVectorLayer( polyFileInfo.filePath(),
768+
polyFileInfo.completeBaseName(), "ogr" );
769+
QgsVectorSimplifyMethod simplifyMethod;
770+
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
771+
polysLayer->setSimplifyMethod( simplifyMethod );
772+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << polysLayer );
773+
774+
QgsMapSettings ms;
775+
QgsSimpleFillSymbolLayerV2* fill = new QgsSimpleFillSymbolLayerV2;
776+
fill->setColor( QColor( 255, 0, 0 ) );
777+
778+
QgsFillSymbolV2* fillSymbol = new QgsFillSymbolV2();
779+
fillSymbol->changeSymbolLayer( 0, fill );
780+
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( fillSymbol );
781+
782+
QgsOuterGlowEffect* effect = new QgsOuterGlowEffect();
783+
effect->setSpread( 30.0 );
784+
effect->setColor( QColor( 255, 0, 0 ) );
785+
renderer->setPaintEffect( effect );
786+
787+
polysLayer->setRendererV2( renderer );
788+
789+
ms.setLayers( QStringList() << polysLayer->id() );
790+
ms.setExtent( polysLayer->extent() );
791+
792+
mReport += "<h2>Paint effect layer test</h2>\n";
793+
bool result = mapRenderCheck( "painteffect_layer", ms );
794+
QVERIFY( result );
795+
}
796+
797+
void TestQgsPaintEffect::mapUnits()
798+
{
799+
//test rendering an effect which utilises map units
800+
QString linesFileName = mTestDataDir + "lines.shp";
801+
QFileInfo lineFileInfo( linesFileName );
802+
QgsVectorLayer* lineLayer = new QgsVectorLayer( lineFileInfo.filePath(),
803+
lineFileInfo.completeBaseName(), "ogr" );
804+
QgsVectorSimplifyMethod simplifyMethod;
805+
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
806+
lineLayer->setSimplifyMethod( simplifyMethod );
807+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << lineLayer );
808+
809+
QgsMapSettings ms;
810+
QgsSimpleLineSymbolLayerV2* line = new QgsSimpleLineSymbolLayerV2;
811+
line->setColor( QColor( 255, 0, 0 ) );
812+
line->setWidth( 1.0 );
813+
814+
QgsLineSymbolV2* lineSymbol = new QgsLineSymbolV2();
815+
lineSymbol->changeSymbolLayer( 0, line );
816+
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( lineSymbol );
817+
QgsOuterGlowEffect* effect = new QgsOuterGlowEffect();
818+
effect->setColor( QColor( 255, 0, 0 ) );
819+
effect->setSpread( 3 );
820+
effect->setSpreadUnit( QgsSymbolV2::MapUnit );
821+
renderer->setPaintEffect( effect );
822+
823+
lineLayer->setRendererV2( renderer );
824+
ms.setLayers( QStringList() << lineLayer->id() );
825+
ms.setExtent( lineLayer->extent() );
826+
827+
mReport += "<h2>Paint effect map units test</h2>\n";
828+
bool result = mapRenderCheck( "painteffect_mapunits", ms );
829+
QVERIFY( result );
830+
}
831+
832+
void TestQgsPaintEffect::composer()
833+
{
834+
//test rendering an effect inside a composer (tests DPI scaling of effects)
835+
836+
QString linesFileName = mTestDataDir + "lines.shp";
837+
QFileInfo lineFileInfo( linesFileName );
838+
QgsVectorLayer* lineLayer = new QgsVectorLayer( lineFileInfo.filePath(),
839+
lineFileInfo.completeBaseName(), "ogr" );
840+
QgsVectorSimplifyMethod simplifyMethod;
841+
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
842+
lineLayer->setSimplifyMethod( simplifyMethod );
843+
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << lineLayer );
844+
845+
QgsMapSettings ms;
846+
QgsSimpleLineSymbolLayerV2* line = new QgsSimpleLineSymbolLayerV2;
847+
line->setColor( QColor( 255, 0, 0 ) );
848+
line->setWidth( 1.0 );
849+
850+
QgsLineSymbolV2* lineSymbol = new QgsLineSymbolV2();
851+
lineSymbol->changeSymbolLayer( 0, line );
852+
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( lineSymbol );
853+
QgsEffectStack* effect = new QgsEffectStack();
854+
effect->appendEffect( new QgsDropShadowEffect() );
855+
effect->appendEffect( new QgsDrawSourceEffect() );
856+
renderer->setPaintEffect( effect );
857+
858+
lineLayer->setRendererV2( renderer );
859+
ms.setLayers( QStringList() << lineLayer->id() );
860+
ms.setCrsTransformEnabled( false );
861+
862+
QgsComposition* composition = new QgsComposition( ms );
863+
composition->setPaperSize( 50, 50 );
864+
QgsComposerMap* composerMap = new QgsComposerMap( composition, 1, 1, 48, 48 );
865+
composerMap->setFrameEnabled( true );
866+
composition->addComposerMap( composerMap );
867+
composerMap->setNewExtent( lineLayer->extent() );
868+
869+
QImage outputImage( 591, 591, QImage::Format_RGB32 );
870+
composition->setPlotStyle( QgsComposition::Print );
871+
outputImage.setDotsPerMeterX( 300 / 25.4 * 1000 );
872+
outputImage.setDotsPerMeterY( 300 / 25.4 * 1000 );
873+
QgsMultiRenderChecker::drawBackground( &outputImage );
874+
QPainter p( &outputImage );
875+
composition->renderPage( &p, 0 );
876+
p.end();
877+
878+
bool result = imageCheck( "painteffect_composer", outputImage );
879+
QVERIFY( result );
880+
}
881+
641882

642883
//
643884
// Private helper functions not called directly by CTest
@@ -665,5 +906,16 @@ bool TestQgsPaintEffect::imageCheck( QString testName, QImage &image, int mismat
665906
return resultFlag;
666907
}
667908

909+
bool TestQgsPaintEffect::mapRenderCheck( QString testName, QgsMapSettings& mapSettings, int mismatchCount )
910+
{
911+
QgsMultiRenderChecker checker;
912+
checker.setControlName( "expected_" + testName );
913+
checker.setMapSettings( mapSettings );
914+
checker.setColorTolerance( 20 );
915+
bool result = checker.runTest( testName, mismatchCount );
916+
mReport += checker.report();
917+
return result;
918+
}
919+
668920
QTEST_MAIN( TestQgsPaintEffect )
669921
#include "testqgspainteffect.moc"
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)