@@ -56,6 +56,12 @@ static void _setStandardTestFont( QgsLegendSettings &settings, const QString &st
56
56
}
57
57
}
58
58
59
+ static QImage _base64ToImage ( const QString &base64 )
60
+ {
61
+ const QByteArray bytearray = QByteArray::fromBase64 ( base64.toStdString ().c_str () );
62
+ return QImage::fromData ( bytearray, " PNG" );
63
+ }
64
+
59
65
static void _renderLegend ( const QString &testName, QgsLayerTreeModel *legendModel, QgsLegendSettings &settings )
60
66
{
61
67
settings.setTitle ( QStringLiteral ( " Legend" ) );
@@ -77,14 +83,23 @@ static void _renderLegend( const QString &testName, QgsLayerTreeModel *legendMod
77
83
img.save ( _fileNameForTest ( testName ) );
78
84
}
79
85
80
- static bool _verifyImage ( const QString &testName, QString &report )
86
+ static QJsonObject _renderJsonLegend ( QgsLayerTreeModel *legendModel, const QgsLegendSettings &settings )
87
+ {
88
+ QgsLegendRenderer legendRenderer ( legendModel, settings );
89
+
90
+ QJsonObject json;
91
+ legendRenderer.drawLegend ( json );
92
+ return json;
93
+ }
94
+
95
+ static bool _verifyImage ( const QString &testName, QString &report, int diff = 500 )
81
96
{
82
97
QgsRenderChecker checker;
83
98
checker.setControlPathPrefix ( QStringLiteral ( " legend" ) );
84
99
checker.setControlName ( " expected_" + testName );
85
100
checker.setRenderedImage ( _fileNameForTest ( testName ) );
86
101
checker.setSizeTolerance ( 3 , 3 );
87
- bool equal = checker.compareImages ( testName, 500 );
102
+ bool equal = checker.compareImages ( testName, diff );
88
103
report += checker.report ();
89
104
return equal;
90
105
}
@@ -125,6 +140,8 @@ class TestQgsLegendRenderer : public QObject
125
140
void testDataDefinedSizeCollapsed ();
126
141
void testTextOnSymbol ();
127
142
143
+ void testBasicJson ();
144
+
128
145
private:
129
146
QgsLayerTree *mRoot = nullptr ;
130
147
QgsVectorLayer *mVL1 = nullptr ; // line
@@ -829,6 +846,87 @@ void TestQgsLegendRenderer::testTextOnSymbol()
829
846
delete root;
830
847
}
831
848
849
+ void TestQgsLegendRenderer::testBasicJson ()
850
+ {
851
+ QgsLayerTreeModel legendModel ( mRoot );
852
+
853
+ QgsLegendSettings settings;
854
+ settings.setTitle ( QStringLiteral ( " Legend" ) );
855
+ _setStandardTestFont ( settings );
856
+ const QJsonObject json = _renderJsonLegend ( &legendModel, settings );
857
+
858
+ QCOMPARE ( json[ " title" ], " Legend" );
859
+
860
+ const QJsonArray root = json[" nodes" ].toArray ();
861
+
862
+ const QJsonObject grp1 = root[0 ].toObject ();
863
+ QCOMPARE ( grp1[" title" ], " Line + Polygon" );
864
+ QCOMPARE ( grp1[" type" ], " group" );
865
+
866
+ const QJsonArray grp1_nodes = grp1[" nodes" ].toArray ();
867
+
868
+ const QJsonObject line_layer = grp1_nodes[0 ].toObject ();
869
+ QCOMPARE ( line_layer[" title" ].toString (), " Line Layer" );
870
+ QCOMPARE ( line_layer[" type" ].toString (), " layer" );
871
+ const QImage line_layer_icon = _base64ToImage ( line_layer[" icon" ].toString () );
872
+ QString test_name = " line_layer_icon" ;
873
+ line_layer_icon.save ( _fileNameForTest ( test_name ) );
874
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
875
+
876
+ const QJsonObject polygon_layer = grp1_nodes[1 ].toObject ();
877
+ QCOMPARE ( polygon_layer[" title" ].toString (), " Polygon Layer" );
878
+ QCOMPARE ( polygon_layer[" type" ].toString (), " layer" );
879
+ const QImage polygon_layer_icon = _base64ToImage ( polygon_layer[" icon" ].toString () );
880
+ test_name = " polygon_layer_icon" ;
881
+ polygon_layer_icon.save ( _fileNameForTest ( test_name ) );
882
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
883
+
884
+ const QJsonObject point_layer = root[1 ].toObject ();
885
+ QCOMPARE ( point_layer[" title" ].toString (), " Point Layer" );
886
+ QCOMPARE ( point_layer[" type" ].toString (), " layer" );
887
+ const QJsonArray point_layer_symbols = point_layer[" symbols" ].toArray ();
888
+
889
+ const QJsonObject point_layer_symbol_red = point_layer_symbols[0 ].toObject ();
890
+ QCOMPARE ( point_layer_symbol_red[" title" ].toString (), " Red" );
891
+ const QImage point_layer_icon_red = _base64ToImage ( point_layer_symbol_red[" icon" ].toString () );
892
+ test_name = " point_layer_icon_red" ;
893
+ point_layer_icon_red.save ( _fileNameForTest ( test_name ) );
894
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
895
+
896
+ const QJsonObject point_layer_symbol_green = point_layer_symbols[1 ].toObject ();
897
+ QCOMPARE ( point_layer_symbol_green[" title" ].toString (), " Green" );
898
+ const QImage point_layer_icon_green = _base64ToImage ( point_layer_symbol_green[" icon" ].toString () );
899
+ test_name = " point_layer_icon_green" ;
900
+ point_layer_icon_green.save ( _fileNameForTest ( test_name ) );
901
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
902
+
903
+ const QJsonObject point_layer_symbol_blue = point_layer_symbols[2 ].toObject ();
904
+ QCOMPARE ( point_layer_symbol_blue[" title" ].toString (), " Blue" );
905
+ const QImage point_layer_icon_blue = _base64ToImage ( point_layer_symbol_blue[" icon" ].toString () );
906
+ test_name = " point_layer_icon_blue" ;
907
+ point_layer_icon_blue.save ( _fileNameForTest ( test_name ) );
908
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
909
+
910
+ const QJsonObject raster_layer = root[2 ].toObject ();
911
+ QCOMPARE ( raster_layer[" title" ].toString (), " Raster Layer" );
912
+ QCOMPARE ( raster_layer[" type" ].toString (), " layer" );
913
+ const QJsonArray raster_layer_symbols = raster_layer[" symbols" ].toArray ();
914
+
915
+ const QJsonObject raster_layer_symbol_1 = raster_layer_symbols[0 ].toObject ();
916
+ QCOMPARE ( raster_layer_symbol_1[" title" ].toString (), " 1" );
917
+ const QImage raster_layer_icon_1 = _base64ToImage ( raster_layer_symbol_1[" icon" ].toString () );
918
+ test_name = " raster_layer_icon_1" ;
919
+ raster_layer_icon_1.save ( _fileNameForTest ( test_name ) );
920
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
921
+
922
+ const QJsonObject raster_layer_symbol_2 = raster_layer_symbols[1 ].toObject ();
923
+ QCOMPARE ( raster_layer_symbol_2[" title" ].toString (), " 2" );
924
+ const QImage raster_layer_icon_2 = _base64ToImage ( raster_layer_symbol_2[" icon" ].toString () );
925
+ test_name = " raster_layer_icon_2" ;
926
+ raster_layer_icon_2.save ( _fileNameForTest ( test_name ) );
927
+ QVERIFY ( _verifyImage ( test_name, mReport , 50 ) );
928
+ }
929
+
832
930
833
931
QGSTEST_MAIN ( TestQgsLegendRenderer )
834
932
#include " testqgslegendrenderer.moc"
0 commit comments