1 /***************************************************************************
2      testqgsmeshcalculator.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 "qgisapp.h"
17 #include "qgsapplication.h"
18 #include "qgsvectorlayer.h"
19 #include "qgsmeshlayer.h"
20 #include "qgsmeshdataprovider.h"
21 #include "qgsmeshcalculatordialog.h"
22 #include "qgsfeedback.h"
23 
24 #include <QTemporaryFile>
25 
26 /**
27  * \ingroup UnitTests
28  * This is a unit test for the mesh calculator
29  */
30 class TestQgsMeshCalculatorDialog : public QObject
31 {
32     Q_OBJECT
33   public:
34     TestQgsMeshCalculatorDialog();
35 
36   private slots:
37     void initTestCase();// will be called before the first testfunction is executed.
38     void cleanupTestCase();// will be called after the last testfunction was executed.
init()39     void init() {} // will be called before each testfunction is executed.
cleanup()40     void cleanup() {} // will be called after every testfunction.
41 
42     void testCalc();
43 
44   private:
45     QgisApp *mQgisApp = nullptr;
46     QgsMeshLayer *mpMeshLayer = nullptr;
47 };
48 
49 TestQgsMeshCalculatorDialog::TestQgsMeshCalculatorDialog() = default;
50 
51 //runs before all tests
initTestCase()52 void TestQgsMeshCalculatorDialog::initTestCase()
53 {
54   qDebug() << "TestQgisAppClipboard::initTestCase()";
55   // init QGIS's paths - true means that all path will be inited from prefix
56   QgsApplication::init();
57   QgsApplication::initQgis();
58   mQgisApp = new QgisApp();
59 
60   QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/mesh/" );
61   QString uri( testDataDir + "/quad_and_triangle.2dm" );
62   mpMeshLayer = new QgsMeshLayer( uri, "Triangle and Quad MDAL", "mdal" );
63   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_vertex_scalar.dat" );
64   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_vertex_scalar2.dat" );
65   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_vertex_scalar_max.dat" );
66   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_vertex_vector.dat" );
67   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_vertex_vector2.dat" );
68   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_vertex_vector_max.dat" );
69   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_els_face_scalar.dat" );
70   mpMeshLayer->dataProvider()->addDataset( testDataDir + "/quad_and_triangle_els_face_vector.dat" );
71 
72   QgsProject::instance()->addMapLayers(
73     QList<QgsMapLayer *>() << mpMeshLayer );
74 }
75 
76 //runs after all tests
cleanupTestCase()77 void TestQgsMeshCalculatorDialog::cleanupTestCase()
78 {
79   QgsApplication::exitQgis();
80 }
81 
testCalc()82 void TestQgsMeshCalculatorDialog::testCalc()
83 {
84   if ( !QgsTest::runFlakyTests() )
85     QSKIP( "This test is disabled on Travis CI environment" );
86 
87   std::unique_ptr< QgsMeshCalculatorDialog > dialog( new QgsMeshCalculatorDialog( mpMeshLayer ) );
88 
89   int groupCount = mpMeshLayer->dataProvider()->datasetGroupCount();
90 
91   QTemporaryFile tmpFile;
92   tmpFile.open(); // fileName is not available until open
93   QString tmpName = tmpFile.fileName();
94   tmpFile.close();
95 
96   // this next part is fragile, and may need to be modified if the dialog changes:
97   dialog->mOutputDatasetFileWidget->setFilePath( tmpName );
98   dialog->mExpressionTextEdit->setText( QStringLiteral( "\"VertexScalarDataset\" * 2 " ) );
99   dialog->accept();
100   std::unique_ptr<QgsMeshCalculator> calculator = dialog->calculator();
101 
102   QgsFeedback feedback;
103   QgsMeshCalculator::Result res = calculator->processCalculation( &feedback );
104   QCOMPARE( res, QgsMeshCalculator::Success );
105 
106   // check result
107   int newGroupCount = mpMeshLayer->dataProvider()->datasetGroupCount();
108   QCOMPARE( groupCount + 1, newGroupCount );
109 }
110 
111 QGSTEST_MAIN( TestQgsMeshCalculatorDialog )
112 #include "testqgsmeshcalculatordialog.moc"
113