Skip to content

Commit 589460b

Browse files
committed
add tests for QgsDataItem - verify that .gz and .vrt files are not recognized as both gdal and ogr items (bug #5636)
1 parent db164df commit 589460b

File tree

5 files changed

+181
-3
lines changed

5 files changed

+181
-3
lines changed

tests/src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ ADD_QGIS_TEST(searchstringtest testqgssearchstring.cpp)
9595
ADD_QGIS_TEST(vectorlayertest testqgsvectorlayer.cpp)
9696
ADD_QGIS_TEST(rulebasedrenderertest testqgsrulebasedrenderer.cpp)
9797
ADD_QGIS_TEST(ziplayertest testziplayer.cpp)
98+
ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)

tests/src/core/testqgsdataitem.cpp

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/***************************************************************************
2+
testqgsdataitem.cpp
3+
--------------------------------------
4+
Date : Thu May 24 10:44:50 BRT 2012
5+
Copyright : (C) 2012 Etienne Tourigny
6+
Email : etourigny.dev 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 <QtTest>
16+
#include <QObject>
17+
#include <QString>
18+
#include <QStringList>
19+
#include <QSettings>
20+
21+
//qgis includes...
22+
#include <qgsdataitem.h>
23+
#include <qgsvectorlayer.h>
24+
#include <qgsapplication.h>
25+
#include <qgslogger.h>
26+
27+
/** \ingroup UnitTests
28+
* This is a unit test for the QgsDataItem class.
29+
*/
30+
class TestQgsDataItem: public QObject
31+
{
32+
Q_OBJECT;
33+
34+
private slots:
35+
void initTestCase();// will be called before the first testfunction is executed.
36+
void cleanupTestCase();;// will be called after the last testfunction was executed.
37+
void init() {};// will be called before each testfunction is executed.
38+
void cleanup() {};// will be called after every testfunction.
39+
40+
void testValid();
41+
void testDirItemChildren();
42+
43+
private:
44+
QgsDirectoryItem* mDirItem;
45+
int mScanItemsSetting;
46+
bool isValidDirItem( QgsDirectoryItem *item );
47+
};
48+
49+
void TestQgsDataItem::initTestCase()
50+
{
51+
//
52+
// Runs once before any tests are run
53+
//
54+
// init QGIS's paths - true means that all path will be inited from prefix
55+
QgsApplication::init();
56+
QgsApplication::initQgis();
57+
QgsApplication::showSettings();
58+
59+
// Set up the QSettings environment
60+
QCoreApplication::setOrganizationName( "QuantumGIS" );
61+
QCoreApplication::setOrganizationDomain( "qgis.org" );
62+
QCoreApplication::setApplicationName( "QGIS-TEST" );
63+
// save current scanItemsSetting value
64+
QSettings settings;
65+
mScanItemsSetting = settings.value( "/qgis/scanItemsInBrowser", 0 ).toInt();
66+
67+
//create a directory item that will be used in all tests...
68+
mDirItem = new QgsDirectoryItem( 0, "Test", TEST_DATA_DIR );
69+
}
70+
71+
void TestQgsDataItem::cleanupTestCase()
72+
{
73+
// restore scanItemsSetting
74+
QSettings settings;
75+
settings.setValue( "/qgis/scanItemsInBrowser", mScanItemsSetting );
76+
if ( mDirItem )
77+
delete mDirItem;
78+
}
79+
80+
bool TestQgsDataItem::isValidDirItem( QgsDirectoryItem *item )
81+
{
82+
return ( item && item->hasChildren() );
83+
}
84+
85+
void TestQgsDataItem::testValid()
86+
{
87+
if ( mDirItem )
88+
QgsDebugMsg( QString( "dirItem has %1 children" ).arg( mDirItem->rowCount() ) );
89+
QVERIFY( isValidDirItem( mDirItem ) );
90+
}
91+
92+
void TestQgsDataItem::testDirItemChildren()
93+
{
94+
QSettings settings;
95+
// test scanItems setting=1 to test .vrt and .gz files are not loaded by gdal and ogr
96+
for ( int iSetting = 0 ; iSetting <= 1 ; iSetting++ )
97+
{
98+
settings.setValue( "/qgis/scanItemsInBrowser", iSetting );
99+
QgsDirectoryItem* dirItem = new QgsDirectoryItem( 0, "Test", TEST_DATA_DIR );
100+
QVERIFY( isValidDirItem( dirItem ) );
101+
102+
QVector<QgsDataItem*> children = dirItem->createChildren();
103+
for ( int i = 0; i < children.size(); i++ )
104+
{
105+
QgsDataItem* dataItem = children[i];
106+
QgsLayerItem* layerItem = dynamic_cast<QgsLayerItem*>( dataItem );
107+
if ( ! layerItem )
108+
continue;
109+
QFileInfo info( layerItem->path() );
110+
QString lFile = info.fileName();
111+
QString lProvider = layerItem->providerKey();
112+
QString errStr = QString( "layer #%1 - %2 provider = %3 iSetting = %4" ).arg( i ).arg( lFile ).arg( lProvider ).arg( iSetting );
113+
const char* err = errStr.toLocal8Bit().constData();
114+
115+
QgsDebugMsg( QString( "child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( lProvider ).arg( lFile ) );
116+
117+
if ( lFile == "landsat.tif" )
118+
{
119+
QVERIFY2( lProvider == "gdal", err );
120+
}
121+
else if ( lFile == "points.vrt" )
122+
{
123+
QVERIFY2( lProvider == "ogr", err );
124+
}
125+
else if ( lFile == "landsat.vrt" )
126+
{
127+
QVERIFY2( lProvider == "gdal", err );
128+
}
129+
else if ( lFile == "landsat_b1.tif.gz" )
130+
{
131+
QVERIFY2( lProvider == "gdal", err );
132+
}
133+
else if ( lFile == "points3.geojson.gz" )
134+
{
135+
QVERIFY2( lProvider == "ogr", err );
136+
}
137+
}
138+
if ( dirItem )
139+
delete dirItem;
140+
}
141+
}
142+
143+
QTEST_MAIN( TestQgsDataItem )
144+
#include "moc_testqgsdataitem.cxx"

tests/src/core/testziplayer.cpp

+31-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class TestZipLayer: public QObject
5050
// get map layer using QgsZipItem (only 1 child)
5151
QgsMapLayer * getZipLayer( QString myPath, QString myName );
5252
// test item(s) in zip item (supply name or test all)
53-
bool testZipItem( QString myFileName, QString myChildName );
53+
bool testZipItem( QString myFileName, QString myChildName = "", QString myDriverName = "" );
5454
// get layer transparency to test for .qml loading
5555
int getLayerTransparency( QString myFileName, QString myProviderKey, int myScanZipSetting = 1 );
5656

@@ -79,6 +79,8 @@ class TestZipLayer: public QObject
7979
void testGZipItemRasterTransparency();
8080
//make sure items inside subfolders can be read
8181
void testZipItemSubfolder();
82+
//make sure .vrt items are loaded by proper provider (gdal/ogr)
83+
void testZipItemVRT();
8284
};
8385

8486

@@ -128,13 +130,15 @@ bool TestZipLayer::testZipItemPassthru( QString myFileName, QString myProviderKe
128130
return ok;
129131
}
130132

131-
bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
133+
bool TestZipLayer::testZipItem( QString myFileName, QString myChildName, QString myProviderName )
132134
{
133-
QgsDebugMsg( QString( "\n=======================================\nfile = %1 name = %2" ).arg( myFileName ).arg( myChildName ) );
135+
QgsDebugMsg( QString( "\n=======================================\nfile = %1 name = %2 provider = %3"
136+
).arg( myFileName ).arg( myChildName ).arg( myProviderName ) );
134137
QFileInfo myFileInfo( myFileName );
135138
QgsZipItem *myZipItem = new QgsZipItem( NULL, myFileInfo.fileName(), myFileName );
136139
myZipItem->populate();
137140
bool ok = false;
141+
QString driverName;
138142
QVector<QgsDataItem*> myChildren = myZipItem->children();
139143

140144
if ( myChildren.size() > 0 )
@@ -168,6 +172,16 @@ bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
168172
}
169173
else
170174
{
175+
//verify correct provider was used
176+
if ( myProviderName != "" )
177+
{
178+
ok = ( myProviderName == layerItem->providerKey() );
179+
if ( ! ok )
180+
{
181+
QWARN( QString( "Layer %1 opened by provider %2, expecting %3"
182+
).arg( layerItem->path() ).arg( layerItem->providerKey() ).arg( myProviderName ).toLocal8Bit().data() );
183+
}
184+
}
171185
break;
172186
}
173187
}
@@ -388,5 +402,19 @@ void TestZipLayer::testZipItemSubfolder()
388402
QVERIFY( testZipItem( mDataDir + "testzip.zip", "folder/folder2/landsat_b2.tif" ) );
389403
}
390404
}
405+
406+
407+
void TestZipLayer::testZipItemVRT()
408+
{
409+
QSettings settings;
410+
for ( int i = 2 ; i <= mMaxScanZipSetting ; i++ )
411+
{
412+
settings.setValue( "/qgis/scanZipInBrowser", i );
413+
QVERIFY( i == settings.value( "/qgis/scanZipInBrowser" ).toInt() );
414+
QVERIFY( testZipItem( mDataDir + "testzip.zip", "landsat.vrt", "gdal" ) );
415+
QVERIFY( testZipItem( mDataDir + "testzip.zip", "points.vrt", "ogr" ) );
416+
}
417+
}
418+
391419
QTEST_MAIN( TestZipLayer )
392420
#include "moc_testziplayer.cxx"

tests/testdata/points.vrt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<OGRVRTDataSource>
2+
<OGRVRTLayer name="points">
3+
<SrcDataSource relativeToVRT="1">points.shp</SrcDataSource>
4+
</OGRVRTLayer>
5+
</OGRVRTDataSource>

tests/testdata/testzip.zip

2.25 KB
Binary file not shown.

0 commit comments

Comments
 (0)