1 /***************************************************************************
2     testqgssinglebandpsueodcolorrendererwidget
3      --------------------------------------
4     Date                 : May 2020
5     Copyright            : (C) 2020 Julien Cabieces
6     Email                : julien.cabieces@oslandia.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 
19 #include <qgsapplication.h>
20 #include <qgsrasterlayer.h>
21 #include "qgssinglebandpseudocolorrendererwidget.h"
22 #include "qgssinglebandpseudocolorrenderer.h"
23 #include "qgsrastershader.h"
24 
25 /**
26  * \ingroup UnitTests
27  * This is a unit test to verify that raster histogram works
28  */
29 class TestQgsSingleBandPseudoColorRendererWidget : public QObject
30 {
31     Q_OBJECT
32 
33   public:
34 
TestQgsSingleBandPseudoColorRendererWidget()35     TestQgsSingleBandPseudoColorRendererWidget() {}
36 
37   private:
38 
39     QgsRasterLayer *mRasterLayer = nullptr;
40 
41   private slots:
42 
43     // init / cleanup
44     void initTestCase();// will be called before the first testfunction is executed.
45     void cleanupTestCase();// will be called after the last testfunction was executed.
46 
47     // tests
48     void testEditLabel();
49 };
50 
51 
52 // slots
53 
initTestCase()54 void TestQgsSingleBandPseudoColorRendererWidget::initTestCase()
55 {
56   QgsApplication::init();
57   QgsApplication::initQgis();
58 
59   const QString mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
60   const QString myLandsatFileName = mTestDataDir + "landsat.tif";
61 
62   mRasterLayer = new QgsRasterLayer( myLandsatFileName, "landsat" );
63   QVERIFY( mRasterLayer->isValid() );
64 }
65 
cleanupTestCase()66 void TestQgsSingleBandPseudoColorRendererWidget::cleanupTestCase()
67 {
68   delete mRasterLayer;
69 }
70 
testEditLabel()71 void TestQgsSingleBandPseudoColorRendererWidget::testEditLabel()
72 {
73   // related to  https://github.com/qgis/QGIS/issues/36172
74 
75   QgsRasterShader *rasterShader = new QgsRasterShader();
76   QgsColorRampShader *colorRampShader = new QgsColorRampShader();
77   colorRampShader->setColorRampType( QgsColorRampShader::Interpolated );
78 
79   const double min = 122.123456;
80   const double max = 129.123456;
81 
82   // configure pseudo band color renderer
83   QList<QgsColorRampShader::ColorRampItem> colorRampItems;
84   QgsColorRampShader::ColorRampItem firstItem;
85   firstItem.value = 0.0;
86   firstItem.label = "zero";
87   firstItem.color = QColor( 0, 0, 255 );
88   colorRampItems.append( firstItem );
89 
90   colorRampShader->setColorRampItemList( colorRampItems );
91   rasterShader->setRasterShaderFunction( colorRampShader );
92 
93   QgsSingleBandPseudoColorRenderer *rasterRenderer = new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), 1, rasterShader );
94   rasterRenderer->setClassificationMin( min );
95   rasterRenderer->setClassificationMax( max );
96   mRasterLayer->dataProvider()->setNoDataValue( 1, 126 );
97   rasterRenderer->setNodataColor( QColor( 255, 0, 255 ) );
98   mRasterLayer->setRenderer( rasterRenderer );
99 
100   QgsSingleBandPseudoColorRendererWidget widget( mRasterLayer );
101 
102   // force loading min/max with the same exact values
103   // it should not triggers classification and we should get the initial ramp item
104   widget.loadMinMax( 1, min, max );
105 
106   QgsSingleBandPseudoColorRenderer *newRasterRenderer = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( widget.renderer() );
107   QVERIFY( newRasterRenderer );
108   QVERIFY( rasterRenderer->shader() );
109 
110   QgsColorRampShader *newColorRampShader = dynamic_cast<QgsColorRampShader *>( newRasterRenderer->shader()->rasterShaderFunction() );
111   QVERIFY( newColorRampShader );
112 
113   const QList<QgsColorRampShader::ColorRampItem> newColorRampItems = newColorRampShader->colorRampItemList();
114   QCOMPARE( newColorRampItems.at( 0 ).label, QStringLiteral( "zero" ) );
115 
116   QCOMPARE( widget.mMinLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->minimum() ) );
117   QCOMPARE( widget.mMaxLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->maximum() ) );
118   QCOMPARE( widget.mMinLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->shader().minimumValue() ) );
119   QCOMPARE( widget.mMaxLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->shader().maximumValue() ) );
120 
121   // change the min/max
122   widget.loadMinMax( 1, min + 1.0, max - 1.0 );
123 
124   QCOMPARE( widget.mMinLineEdit->text(), widget.displayValueWithMaxPrecision( min + 1.0 ) );
125   QCOMPARE( widget.mMaxLineEdit->text(), widget.displayValueWithMaxPrecision( max - 1.0 ) );
126   QCOMPARE( widget.mMinLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->minimum() ) );
127   QCOMPARE( widget.mMaxLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->maximum() ) );
128   QCOMPARE( widget.mMinLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->shader().minimumValue() ) );
129   QCOMPARE( widget.mMaxLineEdit->text(), widget.displayValueWithMaxPrecision( widget.mColorRampShaderWidget->shader().maximumValue() ) );
130 }
131 
132 
133 
134 QGSTEST_MAIN( TestQgsSingleBandPseudoColorRendererWidget )
135 #include "testqgssinglebandpseudocolorrendererwidget.moc"
136