mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
add tests for QgsDataItem - verify that .gz and .vrt files are not recognized as both gdal and ogr items (bug #5636)
This commit is contained in:
parent
db164dfbb7
commit
589460b2dc
@ -95,3 +95,4 @@ ADD_QGIS_TEST(searchstringtest testqgssearchstring.cpp)
|
||||
ADD_QGIS_TEST(vectorlayertest testqgsvectorlayer.cpp)
|
||||
ADD_QGIS_TEST(rulebasedrenderertest testqgsrulebasedrenderer.cpp)
|
||||
ADD_QGIS_TEST(ziplayertest testziplayer.cpp)
|
||||
ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)
|
||||
|
144
tests/src/core/testqgsdataitem.cpp
Normal file
144
tests/src/core/testqgsdataitem.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
/***************************************************************************
|
||||
testqgsdataitem.cpp
|
||||
--------------------------------------
|
||||
Date : Thu May 24 10:44:50 BRT 2012
|
||||
Copyright : (C) 2012 Etienne Tourigny
|
||||
Email : etourigny.dev at gmail dot com
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
#include <QtTest>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QSettings>
|
||||
|
||||
//qgis includes...
|
||||
#include <qgsdataitem.h>
|
||||
#include <qgsvectorlayer.h>
|
||||
#include <qgsapplication.h>
|
||||
#include <qgslogger.h>
|
||||
|
||||
/** \ingroup UnitTests
|
||||
* This is a unit test for the QgsDataItem class.
|
||||
*/
|
||||
class TestQgsDataItem: public QObject
|
||||
{
|
||||
Q_OBJECT;
|
||||
|
||||
private slots:
|
||||
void initTestCase();// will be called before the first testfunction is executed.
|
||||
void cleanupTestCase();;// will be called after the last testfunction was executed.
|
||||
void init() {};// will be called before each testfunction is executed.
|
||||
void cleanup() {};// will be called after every testfunction.
|
||||
|
||||
void testValid();
|
||||
void testDirItemChildren();
|
||||
|
||||
private:
|
||||
QgsDirectoryItem* mDirItem;
|
||||
int mScanItemsSetting;
|
||||
bool isValidDirItem( QgsDirectoryItem *item );
|
||||
};
|
||||
|
||||
void TestQgsDataItem::initTestCase()
|
||||
{
|
||||
//
|
||||
// Runs once before any tests are run
|
||||
//
|
||||
// init QGIS's paths - true means that all path will be inited from prefix
|
||||
QgsApplication::init();
|
||||
QgsApplication::initQgis();
|
||||
QgsApplication::showSettings();
|
||||
|
||||
// Set up the QSettings environment
|
||||
QCoreApplication::setOrganizationName( "QuantumGIS" );
|
||||
QCoreApplication::setOrganizationDomain( "qgis.org" );
|
||||
QCoreApplication::setApplicationName( "QGIS-TEST" );
|
||||
// save current scanItemsSetting value
|
||||
QSettings settings;
|
||||
mScanItemsSetting = settings.value( "/qgis/scanItemsInBrowser", 0 ).toInt();
|
||||
|
||||
//create a directory item that will be used in all tests...
|
||||
mDirItem = new QgsDirectoryItem( 0, "Test", TEST_DATA_DIR );
|
||||
}
|
||||
|
||||
void TestQgsDataItem::cleanupTestCase()
|
||||
{
|
||||
// restore scanItemsSetting
|
||||
QSettings settings;
|
||||
settings.setValue( "/qgis/scanItemsInBrowser", mScanItemsSetting );
|
||||
if ( mDirItem )
|
||||
delete mDirItem;
|
||||
}
|
||||
|
||||
bool TestQgsDataItem::isValidDirItem( QgsDirectoryItem *item )
|
||||
{
|
||||
return ( item && item->hasChildren() );
|
||||
}
|
||||
|
||||
void TestQgsDataItem::testValid()
|
||||
{
|
||||
if ( mDirItem )
|
||||
QgsDebugMsg( QString( "dirItem has %1 children" ).arg( mDirItem->rowCount() ) );
|
||||
QVERIFY( isValidDirItem( mDirItem ) );
|
||||
}
|
||||
|
||||
void TestQgsDataItem::testDirItemChildren()
|
||||
{
|
||||
QSettings settings;
|
||||
// test scanItems setting=1 to test .vrt and .gz files are not loaded by gdal and ogr
|
||||
for ( int iSetting = 0 ; iSetting <= 1 ; iSetting++ )
|
||||
{
|
||||
settings.setValue( "/qgis/scanItemsInBrowser", iSetting );
|
||||
QgsDirectoryItem* dirItem = new QgsDirectoryItem( 0, "Test", TEST_DATA_DIR );
|
||||
QVERIFY( isValidDirItem( dirItem ) );
|
||||
|
||||
QVector<QgsDataItem*> children = dirItem->createChildren();
|
||||
for ( int i = 0; i < children.size(); i++ )
|
||||
{
|
||||
QgsDataItem* dataItem = children[i];
|
||||
QgsLayerItem* layerItem = dynamic_cast<QgsLayerItem*>( dataItem );
|
||||
if ( ! layerItem )
|
||||
continue;
|
||||
QFileInfo info( layerItem->path() );
|
||||
QString lFile = info.fileName();
|
||||
QString lProvider = layerItem->providerKey();
|
||||
QString errStr = QString( "layer #%1 - %2 provider = %3 iSetting = %4" ).arg( i ).arg( lFile ).arg( lProvider ).arg( iSetting );
|
||||
const char* err = errStr.toLocal8Bit().constData();
|
||||
|
||||
QgsDebugMsg( QString( "child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( lProvider ).arg( lFile ) );
|
||||
|
||||
if ( lFile == "landsat.tif" )
|
||||
{
|
||||
QVERIFY2( lProvider == "gdal", err );
|
||||
}
|
||||
else if ( lFile == "points.vrt" )
|
||||
{
|
||||
QVERIFY2( lProvider == "ogr", err );
|
||||
}
|
||||
else if ( lFile == "landsat.vrt" )
|
||||
{
|
||||
QVERIFY2( lProvider == "gdal", err );
|
||||
}
|
||||
else if ( lFile == "landsat_b1.tif.gz" )
|
||||
{
|
||||
QVERIFY2( lProvider == "gdal", err );
|
||||
}
|
||||
else if ( lFile == "points3.geojson.gz" )
|
||||
{
|
||||
QVERIFY2( lProvider == "ogr", err );
|
||||
}
|
||||
}
|
||||
if ( dirItem )
|
||||
delete dirItem;
|
||||
}
|
||||
}
|
||||
|
||||
QTEST_MAIN( TestQgsDataItem )
|
||||
#include "moc_testqgsdataitem.cxx"
|
@ -50,7 +50,7 @@ class TestZipLayer: public QObject
|
||||
// get map layer using QgsZipItem (only 1 child)
|
||||
QgsMapLayer * getZipLayer( QString myPath, QString myName );
|
||||
// test item(s) in zip item (supply name or test all)
|
||||
bool testZipItem( QString myFileName, QString myChildName );
|
||||
bool testZipItem( QString myFileName, QString myChildName = "", QString myDriverName = "" );
|
||||
// get layer transparency to test for .qml loading
|
||||
int getLayerTransparency( QString myFileName, QString myProviderKey, int myScanZipSetting = 1 );
|
||||
|
||||
@ -79,6 +79,8 @@ class TestZipLayer: public QObject
|
||||
void testGZipItemRasterTransparency();
|
||||
//make sure items inside subfolders can be read
|
||||
void testZipItemSubfolder();
|
||||
//make sure .vrt items are loaded by proper provider (gdal/ogr)
|
||||
void testZipItemVRT();
|
||||
};
|
||||
|
||||
|
||||
@ -128,13 +130,15 @@ bool TestZipLayer::testZipItemPassthru( QString myFileName, QString myProviderKe
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
|
||||
bool TestZipLayer::testZipItem( QString myFileName, QString myChildName, QString myProviderName )
|
||||
{
|
||||
QgsDebugMsg( QString( "\n=======================================\nfile = %1 name = %2" ).arg( myFileName ).arg( myChildName ) );
|
||||
QgsDebugMsg( QString( "\n=======================================\nfile = %1 name = %2 provider = %3"
|
||||
).arg( myFileName ).arg( myChildName ).arg( myProviderName ) );
|
||||
QFileInfo myFileInfo( myFileName );
|
||||
QgsZipItem *myZipItem = new QgsZipItem( NULL, myFileInfo.fileName(), myFileName );
|
||||
myZipItem->populate();
|
||||
bool ok = false;
|
||||
QString driverName;
|
||||
QVector<QgsDataItem*> myChildren = myZipItem->children();
|
||||
|
||||
if ( myChildren.size() > 0 )
|
||||
@ -168,6 +172,16 @@ bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
|
||||
}
|
||||
else
|
||||
{
|
||||
//verify correct provider was used
|
||||
if ( myProviderName != "" )
|
||||
{
|
||||
ok = ( myProviderName == layerItem->providerKey() );
|
||||
if ( ! ok )
|
||||
{
|
||||
QWARN( QString( "Layer %1 opened by provider %2, expecting %3"
|
||||
).arg( layerItem->path() ).arg( layerItem->providerKey() ).arg( myProviderName ).toLocal8Bit().data() );
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -388,5 +402,19 @@ void TestZipLayer::testZipItemSubfolder()
|
||||
QVERIFY( testZipItem( mDataDir + "testzip.zip", "folder/folder2/landsat_b2.tif" ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TestZipLayer::testZipItemVRT()
|
||||
{
|
||||
QSettings settings;
|
||||
for ( int i = 2 ; i <= mMaxScanZipSetting ; i++ )
|
||||
{
|
||||
settings.setValue( "/qgis/scanZipInBrowser", i );
|
||||
QVERIFY( i == settings.value( "/qgis/scanZipInBrowser" ).toInt() );
|
||||
QVERIFY( testZipItem( mDataDir + "testzip.zip", "landsat.vrt", "gdal" ) );
|
||||
QVERIFY( testZipItem( mDataDir + "testzip.zip", "points.vrt", "ogr" ) );
|
||||
}
|
||||
}
|
||||
|
||||
QTEST_MAIN( TestZipLayer )
|
||||
#include "moc_testziplayer.cxx"
|
||||
|
5
tests/testdata/points.vrt
vendored
Normal file
5
tests/testdata/points.vrt
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<OGRVRTDataSource>
|
||||
<OGRVRTLayer name="points">
|
||||
<SrcDataSource relativeToVRT="1">points.shp</SrcDataSource>
|
||||
</OGRVRTLayer>
|
||||
</OGRVRTDataSource>
|
BIN
tests/testdata/testzip.zip
vendored
BIN
tests/testdata/testzip.zip
vendored
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user