1 /***************************************************************************
2      testqgsmeshlayerpropertiesdialog.cpp
3      ------------------------------------
4     Date                 : January 2019
5     Copyright            : (C) 2019 by Peter Petrik
6     Email                : zilolv 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 "qgsapplication.h"
17 #include "qgsmeshlayer.h"
18 #include "qgsmeshdataprovider.h"
19 #include "qgsmeshlayerproperties.h"
20 #include "qgsmeshrendereractivedatasetwidget.h"
21 #include "qgsfeedback.h"
22 #include "qgis.h"
23 #include "qgsmapcanvas.h"
24 
25 #include <QTemporaryFile>
26 
27 /**
28  * \ingroup UnitTests
29  * This is a unit test for the mesh layer properties dialog
30  */
31 class TestQgsMeshLayerPropertiesDialog : public QObject
32 {
33     Q_OBJECT
34   public:
35     TestQgsMeshLayerPropertiesDialog();
36 
37   private slots:
38     void initTestCase();// will be called before the first testfunction is executed.
39     void cleanupTestCase();// will be called after the last testfunction was executed.
init()40     void init() {} // will be called before each testfunction is executed.
cleanup()41     void cleanup() {} // will be called after every testfunction.
42 
43     void testInvalidLayer();
44     void testCrs();
45     void testDatasetGroupTree();
46 
47   private:
48     QgsMeshLayer *mpMeshLayer = nullptr;
49 };
50 
51 TestQgsMeshLayerPropertiesDialog::TestQgsMeshLayerPropertiesDialog() = default;
52 
53 //runs before all tests
initTestCase()54 void TestQgsMeshLayerPropertiesDialog::initTestCase()
55 {
56   qDebug() << "TestQgisAppClipboard::initTestCase()";
57   // init QGIS's paths - true means that all path will be inited from prefix
58   QgsApplication::init();
59   QgsApplication::initQgis();
60 
61   const QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/mesh/" );
62   const QString uri( testDataDir + "/quad_and_triangle.2dm" );
63   mpMeshLayer = new QgsMeshLayer( uri, "Triangle and Quad MDAL", "mdal" );
64 
65   QgsProject::instance()->addMapLayers(
66     QList<QgsMapLayer *>() << mpMeshLayer );
67 }
68 
69 //runs after all tests
cleanupTestCase()70 void TestQgsMeshLayerPropertiesDialog::cleanupTestCase()
71 {
72   QgsApplication::exitQgis();
73 }
74 
testInvalidLayer()75 void TestQgsMeshLayerPropertiesDialog::testInvalidLayer()
76 {
77   QgsMeshLayer invalidLayer;
78   QgsMapCanvas mapCanvas;
79   const std::unique_ptr< QgsMeshLayerProperties > dialog = std::make_unique< QgsMeshLayerProperties > ( &invalidLayer,
80       &mapCanvas );
81 
82   QVERIFY( dialog );
83 }
84 
testCrs()85 void TestQgsMeshLayerPropertiesDialog::testCrs()
86 {
87   QgsMapCanvas mapCanvas;
88   std::unique_ptr< QgsMeshLayerProperties > dialog = std::make_unique< QgsMeshLayerProperties > ( mpMeshLayer,
89       &mapCanvas );
90   QCOMPARE( dialog->mCrsSelector->crs(), mpMeshLayer->crs() );
91   const QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromEpsgId( 27700 );
92   dialog->mCrsSelector->setCrs( crs );
93   QCOMPARE( crs, mpMeshLayer->crs() );
94 }
95 
testDatasetGroupTree()96 void TestQgsMeshLayerPropertiesDialog::testDatasetGroupTree()
97 {
98   const QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/mesh/" );
99   const QString uri( testDataDir + "/trap_steady_05_3D.nc" );
100   QgsMeshLayer meshLayer( uri, "", "mdal" );
101 
102   QgsMeshRendererSettings rendererSettings = meshLayer.rendererSettings();
103   rendererSettings.setActiveScalarDatasetGroup( 1 );
104   meshLayer.setRendererSettings( rendererSettings );
105   QCOMPARE( meshLayer.rendererSettings().activeScalarDatasetGroup(), 1 );
106 
107   QgsMeshRendererActiveDatasetWidget activeDatasetWidget;
108   activeDatasetWidget.setLayer( &meshLayer );
109   activeDatasetWidget.syncToLayer();
110 
111   QCOMPARE( activeDatasetWidget.activeScalarDatasetGroup(), 1 );
112 
113   std::unique_ptr<QgsMeshDatasetGroupTreeItem> rootItem( meshLayer.datasetGroupTreeRootItem()->clone() );
114   rootItem->child( 1 )->setIsEnabled( false );
115   meshLayer.setDatasetGroupTreeRootItem( rootItem.get() );
116 
117   QCOMPARE( activeDatasetWidget.activeScalarDatasetGroup(), 0 );
118 
119 }
120 
121 QGSTEST_MAIN( TestQgsMeshLayerPropertiesDialog )
122 #include "testqgsmeshlayerpropertiesdialog.moc"
123