Skip to content

Commit a7fc705

Browse files
committedApr 23, 2023
Add tests for using ordered layers and symbol levels together
1 parent f7cd3f9 commit a7fc705

File tree

10 files changed

+147
-0
lines changed

10 files changed

+147
-0
lines changed
 

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(TESTS
8484
testqgsjsonutils.cpp
8585
testqgslabelingengine.cpp
8686
testqgslayerdefinition.cpp
87+
testqgslayeredsymbollevel.cpp
8788
testqgslayertree.cpp
8889
testqgslayout.cpp
8990
testqgslayoutatlas.cpp
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/***************************************************************************
2+
testqgslayeredsymbollevel.cpp
3+
--------------------------------------
4+
Date : April 2016
5+
Copyright : (C) 2016 by Nyall Dawson
6+
Email : nyall dot dawson 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+
#include "qgstest.h"
16+
#include <QObject>
17+
#include <QString>
18+
#include <QStringList>
19+
#include <QApplication>
20+
#include <QFileInfo>
21+
#include <QDir>
22+
#include <QDesktopServices>
23+
24+
//qgis includes...
25+
#include <qgsmapsettings.h>
26+
#include <qgsmaplayer.h>
27+
#include <qgsvectorlayer.h>
28+
#include <qgsapplication.h>
29+
#include <qgsproviderregistry.h>
30+
#include <qgsproject.h>
31+
#include <qgssinglesymbolrenderer.h>
32+
#include <qgsfeaturerequest.h>
33+
#include <qgslinesymbol.h>
34+
#include <qgslinesymbollayer.h>
35+
#include <qgssymbol.h>
36+
#include "qgslayoutitemmap.h"
37+
#include "qgsmultirenderchecker.h"
38+
#include "qgsexpressioncontextutils.h"
39+
#include "qgslayout.h"
40+
41+
/**
42+
* \ingroup UnitTests
43+
* This is a unit test for layered symbol level rendering.
44+
*/
45+
class TestQgsLayeredSymbolLevel : public QgsTest
46+
{
47+
Q_OBJECT
48+
public:
49+
TestQgsLayeredSymbolLevel() : QgsTest( QStringLiteral( "Layered Symbol Level Rendering Tests" ) ) {}
50+
51+
private slots:
52+
void initTestCase();// will be called before the first testfunction is executed.
53+
void cleanupTestCase();// will be called after the last testfunction was executed.
54+
55+
void render();
56+
57+
private:
58+
bool imageCheck( const QString &type );
59+
QgsMapSettings mMapSettings;
60+
QgsVectorLayer *mpRoadsLayer = nullptr;
61+
QString mTestDataDir;
62+
};
63+
64+
65+
void TestQgsLayeredSymbolLevel::initTestCase()
66+
{
67+
// init QGIS's paths - true means that all path will be inited from prefix
68+
QgsApplication::init();
69+
QgsApplication::initQgis();
70+
QgsApplication::showSettings();
71+
72+
//create some objects that will be used in all tests...
73+
const QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
74+
mTestDataDir = myDataDir + '/';
75+
76+
//
77+
//create a roads layer that will be used in all tests...
78+
//
79+
const QString myRoadsFileName = mTestDataDir + "layered_roads.shp";
80+
const QFileInfo myRoadsFileInfo( myRoadsFileName );
81+
mpRoadsLayer = new QgsVectorLayer( myRoadsFileInfo.filePath(),
82+
myRoadsFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
83+
84+
QgsVectorSimplifyMethod simplifyMethod;
85+
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
86+
mpRoadsLayer->setSimplifyMethod( simplifyMethod );
87+
88+
mMapSettings.setLayers( QList<QgsMapLayer *>() << mpRoadsLayer );
89+
90+
}
91+
void TestQgsLayeredSymbolLevel::cleanupTestCase()
92+
{
93+
delete mpRoadsLayer;
94+
95+
QgsApplication::exitQgis();
96+
}
97+
98+
void TestQgsLayeredSymbolLevel::render()
99+
{
100+
QgsSimpleLineSymbolLayer *lineSymbolLayer0 = new QgsSimpleLineSymbolLayer;
101+
lineSymbolLayer0->setColor( Qt::black );
102+
lineSymbolLayer0->setWidth( 3 );
103+
104+
QgsSimpleLineSymbolLayer *lineSymbolLayer1 = new QgsSimpleLineSymbolLayer;
105+
lineSymbolLayer1->setColor( Qt::yellow );
106+
lineSymbolLayer1->setWidth( 2 );
107+
108+
QgsLineSymbol *lineSymbol = new QgsLineSymbol( QgsSymbolLayerList() << lineSymbolLayer0 << lineSymbolLayer1 );
109+
110+
QgsSingleSymbolRenderer *renderer = new QgsSingleSymbolRenderer( lineSymbol );
111+
mpRoadsLayer->setRenderer( renderer );
112+
113+
renderer->setUsingSymbolLevels( true );
114+
QVERIFY( imageCheck( "with_levels_no_layers" ) );
115+
116+
renderer->setOrderBy( QgsFeatureRequest::OrderBy() << QgsFeatureRequest::OrderByClause( "layer", false ) );
117+
renderer->setOrderByEnabled( true );
118+
QVERIFY( imageCheck( "with_levels_with_layers" ) );
119+
120+
renderer->setUsingSymbolLevels( false );
121+
QVERIFY( imageCheck( "no_levels_with_layers" ) );
122+
}
123+
124+
bool TestQgsLayeredSymbolLevel::imageCheck( const QString &testType )
125+
{
126+
//use the QgsRenderChecker test utility class to
127+
//ensure the rendered output matches our control image
128+
mMapSettings.setExtent( mpRoadsLayer->extent() );
129+
mMapSettings.setOutputSize( QSize( 400, 400 ) );
130+
mMapSettings.setOutputDpi( 96 );
131+
QgsExpressionContext context;
132+
context << QgsExpressionContextUtils::mapSettingsScope( mMapSettings );
133+
mMapSettings.setExpressionContext( context );
134+
QgsMultiRenderChecker myChecker;
135+
myChecker.setControlPathPrefix( QStringLiteral( "layered_symbol_levels" ) );
136+
myChecker.setControlName( "expected_" + testType );
137+
myChecker.setMapSettings( mMapSettings );
138+
const bool myResultFlag = myChecker.runTest( testType, 0 );
139+
mReport += myChecker.report();
140+
return myResultFlag;
141+
}
142+
143+
QGSTEST_MAIN( TestQgsLayeredSymbolLevel )
144+
#include "testqgslayeredsymbollevel.moc"

‎tests/testdata/layered_roads.cpg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8

‎tests/testdata/layered_roads.dbf

146 Bytes
Binary file not shown.

‎tests/testdata/layered_roads.prj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]

‎tests/testdata/layered_roads.shp

500 Bytes
Binary file not shown.

‎tests/testdata/layered_roads.shx

132 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.