Skip to content

Commit 07ffbfa

Browse files
committed
Add a bunch of tests for legend grouping into columns
(cherry-picked from 570e693)
1 parent 3dbf97f commit 07ffbfa

File tree

10 files changed

+62
-0
lines changed

10 files changed

+62
-0
lines changed

tests/src/core/testqgslegendrenderer.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class TestQgsLegendRenderer : public QObject
119119
void testThreeColumns();
120120
void testFilterByMap();
121121
void testFilterByMapSameSymbol();
122+
void testColumns_data();
123+
void testColumns();
122124
void testRasterBorder();
123125
void testFilterByPolygon();
124126
void testFilterByExpression();
@@ -132,6 +134,7 @@ class TestQgsLegendRenderer : public QObject
132134
QgsVectorLayer* mVL3; // point
133135
QgsRasterLayer* mRL;
134136
QString mReport;
137+
bool _testLegendColumns( int itemCount, int columnCount, const QString& testName );
135138
};
136139

137140

@@ -454,6 +457,65 @@ void TestQgsLegendRenderer::testFilterByMapSameSymbol()
454457
QgsMapLayerRegistry::instance()->removeMapLayer( vl4 );
455458
}
456459

460+
bool TestQgsLegendRenderer::_testLegendColumns( int itemCount, int columnCount, const QString& testName )
461+
{
462+
QgsFillSymbolV2* sym = new QgsFillSymbolV2();
463+
sym->setColor( Qt::cyan );
464+
465+
QgsLayerTreeGroup* root = new QgsLayerTreeGroup();
466+
467+
QList< QgsVectorLayer* > layers;
468+
for ( int i = 1; i <= itemCount; ++i )
469+
{
470+
QgsVectorLayer* vl = new QgsVectorLayer( "Polygon", QString( "Layer %1" ).arg( i ), "memory" );
471+
QgsMapLayerRegistry::instance()->addMapLayer( vl );
472+
vl->setRendererV2( new QgsSingleSymbolRendererV2( sym->clone() ) );
473+
root->addLayer( vl );
474+
layers << vl;
475+
}
476+
delete sym;
477+
478+
QgsLayerTreeModel legendModel( root );
479+
QgsLegendSettings settings;
480+
settings.setColumnCount( columnCount );
481+
_setStandardTestFont( settings, "Bold" );
482+
_renderLegend( testName, &legendModel, settings );
483+
bool result = _verifyImage( testName, mReport );
484+
485+
Q_FOREACH ( QgsVectorLayer* l, layers )
486+
{
487+
QgsMapLayerRegistry::instance()->removeMapLayer( l );
488+
}
489+
return result;
490+
}
491+
492+
void TestQgsLegendRenderer::testColumns_data()
493+
{
494+
QTest::addColumn<QString>( "testName" );
495+
QTest::addColumn<int>( "items" );
496+
QTest::addColumn<int>( "columns" );
497+
498+
QTest::newRow( "2 items, 2 columns" ) << "legend_2_by_2" << 2 << 2;
499+
QTest::newRow( "3 items, 2 columns" ) << "legend_3_by_2" << 3 << 2;
500+
QTest::newRow( "4 items, 2 columns" ) << "legend_4_by_2" << 4 << 2;
501+
QTest::newRow( "5 items, 2 columns" ) << "legend_5_by_2" << 5 << 2;
502+
QTest::newRow( "3 items, 3 columns" ) << "legend_3_by_3" << 3 << 3;
503+
QTest::newRow( "4 items, 3 columns" ) << "legend_4_by_3" << 4 << 3;
504+
QTest::newRow( "5 items, 3 columns" ) << "legend_5_by_3" << 5 << 3;
505+
QTest::newRow( "6 items, 3 columns" ) << "legend_6_by_3" << 6 << 3;
506+
QTest::newRow( "7 items, 3 columns" ) << "legend_7_by_3" << 7 << 3;
507+
}
508+
509+
void TestQgsLegendRenderer::testColumns()
510+
{
511+
//test rendering legend with different combinations of columns and items
512+
513+
QFETCH( QString, testName );
514+
QFETCH( int, items );
515+
QFETCH( int, columns );
516+
QVERIFY( _testLegendColumns( items, columns, testName ) );
517+
}
518+
457519
void TestQgsLegendRenderer::testRasterBorder()
458520
{
459521
QString testName = "legend_raster_border";
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)