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
46 private:
47 QgisApp *mQgisApp = nullptr;
48 QString mTestDataDir;
49 };
50
51 TestQgisApp::TestQgisApp() = default;
52
53 //runs before all tests
initTestCase()54 void TestQgisApp::initTestCase()
55 {
56 // Set up the QgsSettings environment
57 QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
58 QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
59 QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
60
61 qDebug() << "TestQgisApp::initTestCase()";
62 // init QGIS's paths - true means that all path will be inited from prefix
63 QgsApplication::init();
64 QgsApplication::initQgis();
65 mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
66 mQgisApp = new QgisApp();
67 }
68
69 //runs after all tests
cleanupTestCase()70 void TestQgisApp::cleanupTestCase()
71 {
72 QgsApplication::exitQgis();
73 }
74
init()75 void TestQgisApp::init()
76 {
77 GDALDriverH hDriver = GDALGetDriverByName( "GPKG" );
78 QVERIFY( hDriver );
79 GDALDatasetH hDS = GDALCreate( hDriver, "/vsimem/test.gpkg", 0, 0, 0, GDT_Unknown, nullptr );
80 QVERIFY( hDS );
81 GDALDatasetCreateLayer( hDS, "my_layer", nullptr, wkbPoint, nullptr );
82 GDALClose( hDS );
83 }
84
cleanup()85 void TestQgisApp::cleanup()
86 {
87 GDALDriverH hDriver = GDALGetDriverByName( "GPKG" );
88 QVERIFY( hDriver );
89 GDALDeleteDataset( hDriver, "/vsimem/test.gpkg" );
90 }
91
addVectorLayerShp()92 void TestQgisApp::addVectorLayerShp()
93 {
94 QString filePath = mTestDataDir + QStringLiteral( "points.shp" );
95 QgsVectorLayer *layer = mQgisApp->addVectorLayer( filePath, "test", QStringLiteral( "ogr" ) );
96 QVERIFY( layer->isValid() );
97
98 // No need for |layerName= for shapefiles
99 QVERIFY( layer->source().endsWith( QLatin1String( "points.shp" ) ) );
100
101 // cleanup
102 QgsProject::instance()->layerStore()->removeMapLayers( QStringList() << layer->id() );
103 }
104
addVectorLayerGeopackageSingleLayer()105 void TestQgisApp::addVectorLayerGeopackageSingleLayer()
106 {
107 QString filePath = QLatin1String( "/vsimem/test.gpkg" );
108 QgsVectorLayer *layer = mQgisApp->addVectorLayer( filePath, "test", QStringLiteral( "ogr" ) );
109 QVERIFY( layer->isValid() );
110
111 // Need for |layerName= for geopackage
112 QVERIFY( layer->source().endsWith( QLatin1String( "/vsimem/test.gpkg|layername=my_layer" ) ) );
113
114 // cleanup
115 QgsProject::instance()->layerStore()->removeMapLayers( QStringList() << layer->id() );
116 }
117
addVectorLayerGeopackageSingleLayerAlreadyLayername()118 void TestQgisApp::addVectorLayerGeopackageSingleLayerAlreadyLayername()
119 {
120 QString filePath = QLatin1String( "/vsimem/test.gpkg|layername=my_layer" );
121 QgsVectorLayer *layer = mQgisApp->addVectorLayer( filePath, "test", QStringLiteral( "ogr" ) );
122 QVERIFY( layer->isValid() );
123
124 // Need for |layerName= for geopackage
125 QVERIFY( layer->source().endsWith( QLatin1String( "/vsimem/test.gpkg|layername=my_layer" ) ) );
126
127 // cleanup
128 QgsProject::instance()->layerStore()->removeMapLayers( QStringList() << layer->id() );
129 }
130
addVectorLayerInvalid()131 void TestQgisApp::addVectorLayerInvalid()
132 {
133 QgsVectorLayer *layer = mQgisApp->addVectorLayer( "i_do_not_exist", "test", QStringLiteral( "ogr" ) );
134 QVERIFY( !layer );
135
136 layer = mQgisApp->addVectorLayer( "/vsimem/test.gpkg|layername=invalid_layer_name", "test", QStringLiteral( "ogr" ) );
137 QVERIFY( !layer );
138 }
139
140 QGSTEST_MAIN( TestQgisApp )
141 #include "testqgisapp.moc"
142