1 /***************************************************************************
2      testqgisapp.cpp
3      --------------------------------------
4     Date                 : 6 october 2018
5     Copyright            : (C) 2018 by Even Rouault
6     Email                : even.rouault at spatialys.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 
17 #include <QApplication>
18 #include "gdal.h"
19 
20 #include "qgisapp.h"
21 #include "qgsproject.h"
22 #include "qgsmaplayerstore.h"
23 
24 /**
25  * \ingroup UnitTests
26  * This is a unit test for the QgisApp
27  */
28 class TestQgisApp : public QObject
29 {
30     Q_OBJECT
31 
32   public:
33     TestQgisApp();
34 
35   private slots:
36     void initTestCase();// will be called before the first testfunction is executed.
37     void cleanupTestCase();// will be called after the last testfunction was executed.
38     void init(); // will be called before each testfunction is executed.
39     void cleanup(); // will be called after every testfunction.
40 
41     void addVectorLayerShp();
42     void addVectorLayerGeopackageSingleLayer();
43     void addVectorLayerGeopackageSingleLayerAlreadyLayername();
44     void addVectorLayerInvalid();
45     void addEmbeddedGroup();
46 
47   private:
48     QgisApp *mQgisApp = nullptr;
49     QString mTestDataDir;
50 };
51 
52 TestQgisApp::TestQgisApp() = default;
53 
54 //runs before all tests
initTestCase()55 void TestQgisApp::initTestCase()
56 {
57   // Set up the QgsSettings environment
58   QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
59   QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
60   QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
61 
62   qDebug() << "TestQgisApp::initTestCase()";
63   // init QGIS's paths - true means that all path will be inited from prefix
64   QgsApplication::init();
65   QgsApplication::initQgis();
66   mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
67   mQgisApp = new QgisApp();
68 }
69 
70 //runs after all tests
cleanupTestCase()71 void TestQgisApp::cleanupTestCase()
72 {
73   QgsApplication::exitQgis();
74 }
75 
init()76 void TestQgisApp::init()
77 {
78   GDALDriverH hDriver = GDALGetDriverByName( "GPKG" );
79   QVERIFY( hDriver );
80   GDALDatasetH hDS = GDALCreate( hDriver, "/vsimem/test.gpkg", 0, 0, 0, GDT_Unknown, nullptr );
81   QVERIFY( hDS );
82   GDALDatasetCreateLayer( hDS, "my_layer", nullptr, wkbPoint, nullptr );
83   GDALClose( hDS );
84 }
85 
cleanup()86 void TestQgisApp::cleanup()
87 {
88   GDALDriverH hDriver = GDALGetDriverByName( "GPKG" );
89   QVERIFY( hDriver );
90   GDALDeleteDataset( hDriver, "/vsimem/test.gpkg" );
91 }
92 
addVectorLayerShp()93 void TestQgisApp::addVectorLayerShp()
94 {
95   const QString filePath = mTestDataDir + QStringLiteral( "points.shp" );
96   QgsVectorLayer *layer = mQgisApp->addVectorLayer( filePath, "test", QStringLiteral( "ogr" ) );
97   QVERIFY( layer->isValid() );
98 
99   // No need for |layerName= for shapefiles
100   QVERIFY( layer->source().endsWith( QLatin1String( "points.shp" ) ) );
101 
102   // cleanup
103   QgsProject::instance()->layerStore()->removeMapLayers( QStringList() <<  layer->id() );
104 }
105 
addVectorLayerGeopackageSingleLayer()106 void TestQgisApp::addVectorLayerGeopackageSingleLayer()
107 {
108   const QString filePath = QLatin1String( "/vsimem/test.gpkg" );
109   QgsVectorLayer *layer = mQgisApp->addVectorLayer( filePath, "test", QStringLiteral( "ogr" ) );
110   QVERIFY( layer->isValid() );
111 
112   // Need for |layerName= for geopackage
113   QVERIFY( layer->source().endsWith( QLatin1String( "/vsimem/test.gpkg|layername=my_layer" ) ) );
114 
115   // cleanup
116   QgsProject::instance()->layerStore()->removeMapLayers( QStringList() <<  layer->id() );
117 }
118 
addVectorLayerGeopackageSingleLayerAlreadyLayername()119 void TestQgisApp::addVectorLayerGeopackageSingleLayerAlreadyLayername()
120 {
121   const QString filePath = QLatin1String( "/vsimem/test.gpkg|layername=my_layer" );
122   QgsVectorLayer *layer = mQgisApp->addVectorLayer( filePath, "test", QStringLiteral( "ogr" ) );
123   QVERIFY( layer->isValid() );
124 
125   // Need for |layerName= for geopackage
126   QVERIFY( layer->source().endsWith( QLatin1String( "/vsimem/test.gpkg|layername=my_layer" ) ) );
127 
128   // cleanup
129   QgsProject::instance()->layerStore()->removeMapLayers( QStringList() <<  layer->id() );
130 }
131 
addVectorLayerInvalid()132 void TestQgisApp::addVectorLayerInvalid()
133 {
134   QgsVectorLayer *layer = mQgisApp->addVectorLayer( "i_do_not_exist", "test", QStringLiteral( "ogr" ) );
135   QVERIFY( !layer );
136 
137   layer = mQgisApp->addVectorLayer( "/vsimem/test.gpkg|layername=invalid_layer_name", "test", QStringLiteral( "ogr" ) );
138   QVERIFY( !layer );
139 }
140 
addEmbeddedGroup()141 void TestQgisApp::addEmbeddedGroup()
142 {
143   const QString projectPath = QString( TEST_DATA_DIR ) + QStringLiteral( "/embedded_groups/joins1.qgs" );
144 
145   QCOMPARE( QgsProject::instance()->layers<QgsVectorLayer *>().count(), 0 );
146 
147   mQgisApp->addEmbeddedItems( projectPath, QStringList() << QStringLiteral( "GROUP" ), QStringList() );
148 
149   QgsVectorLayer *vl = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QStringLiteral( "polys_with_id_32002f94_eebe_40a5_a182_44198ba1bc5a" ) );
150   QCOMPARE( vl->fields().count(), 5 );
151 
152   // cleanup
153   QgsProject::instance()->clear();
154 }
155 
156 
157 
158 QGSTEST_MAIN( TestQgisApp )
159 #include "testqgisapp.moc"
160