Skip to content

Commit

Permalink
test that styles are loaded from qml files outside zip
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Apr 25, 2012
1 parent 2bd054c commit a859c62
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 49 deletions.
172 changes: 123 additions & 49 deletions tests/src/core/testziplayer.cpp
Expand Up @@ -26,6 +26,8 @@
#include <qgsrasterlayer.h>
#include <qgsdataitem.h>
#include "qgsconfig.h"
#include <qgsrenderer.h>
#include <qgsuniquevaluerenderer.h>

#include <gdal.h>

Expand All @@ -43,9 +45,15 @@ class TestZipLayer: public QObject
int mMaxScanZipSetting;
int mScanZipSetting;

bool testPassthruVector( QString myFileName );
bool testPassthruRaster( QString myFileName );
// get map layer using Passthru
QgsMapLayer * getLayer( QString myPath, QString myName, QString myProviderKey );
bool testZipItemPassthru( QString myFileName, QString myProviderKey );
// 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 );
// get layer transparency to test for .qml loading
int getLayerTransparency( QString myFileName, QString myProviderKey, int myScanZipSetting = 1 );

private slots:

Expand All @@ -65,28 +73,57 @@ class TestZipLayer: public QObject
void testZipItemRaster();
void testZipItemVector();
void testZipItemAll();

// test that styles are loaded from .qml files outside zip files
void testZipItemVectorTransparency();
void testGipItemVectorTransparency();
void testZipItemRasterTransparency();
void testGZipItemRasterTransparency();
};

bool TestZipLayer::testPassthruVector( QString myFileName )

QgsMapLayer *TestZipLayer::getLayer( QString myPath, QString myName, QString myProviderKey )
{
QFileInfo myFileInfo( myFileName );
QgsVectorLayer * myVectorLayer;
myVectorLayer = new QgsVectorLayer( myFileInfo.filePath(),
myFileInfo.completeBaseName(), "ogr" );
bool ok = myVectorLayer->isValid();
delete myVectorLayer;
return ok;
if ( myName == "" )
{
QFileInfo myFileInfo( myPath );
myName = myFileInfo.completeBaseName();
}
QgsMapLayer *myLayer = NULL;

if ( myProviderKey == "ogr" )
{
myLayer = new QgsVectorLayer( myPath, myName, "ogr" );
}
else if ( myProviderKey == "gdal" )
{
myLayer = new QgsRasterLayer( myPath, myName, "gdal" );
}
// item should not have other provider key, but if it does will return NULL

return myLayer;
}

bool TestZipLayer::testPassthruRaster( QString myFileName )
QgsMapLayer *TestZipLayer::getZipLayer( QString myPath, QString myName )
{
QFileInfo myFileInfo( myFileName );
QgsRasterLayer * myRasterLayer;
myRasterLayer = new QgsRasterLayer( myFileInfo.filePath(),
myFileInfo.completeBaseName(), "gdal" );
bool ok = myRasterLayer->isValid();
delete myRasterLayer;
QgsMapLayer *myLayer = NULL;
QgsDirectoryItem *dirItem = new QgsDirectoryItem( NULL, "/", "" );
QgsDataItem* myItem = QgsZipItem::itemFromPath( dirItem, myPath, myName );
if ( myItem )
{
QgsLayerItem *layerItem = dynamic_cast<QgsLayerItem*>( myItem );
if ( layerItem )
myLayer = getLayer( layerItem->path(), layerItem->name(), layerItem->providerKey() );
}
delete dirItem;
return myLayer;
}

bool TestZipLayer::testZipItemPassthru( QString myFileName, QString myProviderKey )
{
QgsMapLayer * myLayer = getLayer( myFileName, "", myProviderKey );
bool ok = myLayer && myLayer->isValid();
if ( myLayer )
delete myLayer;
return ok;
}

Expand All @@ -111,33 +148,18 @@ bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
QgsDebugMsg( QString( "child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( layerItem->providerKey() ).arg( layerItem->path() ) );
if ( myChildName == "" || myChildName == item->name() )
{
QgsMapLayer* myLayer = NULL;
if ( layerItem->providerKey() == "ogr" )
{
myLayer = new QgsVectorLayer( item->path(), item->name(), "ogr" );
}
else if ( layerItem->providerKey() == "gdal" )
{
myLayer = new QgsRasterLayer( item->path(), item->name(), "gdal" );
}
else
{
// item should not have other provider key, but if it does the test will fail
ok = false;
QWARN( QString( "Invalid provider %1" ).arg( layerItem->providerKey() ).toLocal8Bit().data() );
break;
}
if ( myLayer != NULL )
QgsMapLayer* layer = getLayer( layerItem->path(), layerItem->name(), layerItem->providerKey() );
if ( layer != NULL )
{
// we got a layer, check if it is valid and exit
QgsDebugMsg( QString( "valid: %1" ).arg( myLayer->isValid() ) );
ok = myLayer->isValid();
delete myLayer;
// if no child name given in argument, then pass to next one (unless current child is invalid)
QgsDebugMsg( QString( "valid: %1" ).arg( layer->isValid() ) );
ok = layer->isValid();
delete layer;
if ( ! ok )
{
QWARN( QString( "Invalid item %1" ).arg( layerItem->path() ).toLocal8Bit().data() );
QWARN( QString( "Invalid layer %1" ).arg( layerItem->path() ).toLocal8Bit().data() );
}
// if no child name given, then pass to next one (unless current child is invalid)
if ( myChildName == "" )
{
if ( ! ok )
Expand All @@ -150,14 +172,14 @@ bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
}
else
{
QWARN( QString( "Invalid item %1" ).arg( layerItem->path() ).toLocal8Bit().data() );
QWARN( QString( "Invalid layer %1" ).arg( layerItem->path() ).toLocal8Bit().data() );
break;
}
}
}
else
{
QWARN( QString( "Invalid item %1" ).arg( layerItem->path() ).toLocal8Bit().data() );
QWARN( QString( "Invalid layer %1" ).arg( layerItem->path() ).toLocal8Bit().data() );
break;
}
}
Expand All @@ -166,6 +188,24 @@ bool TestZipLayer::testZipItem( QString myFileName, QString myChildName = "" )
return ok;
}

int TestZipLayer::getLayerTransparency( QString myFileName, QString myProviderKey, int myScanZipSetting )
{
int myTransparency = -1;
mSettings.setValue( "/qgis/scanZipInBrowser", myScanZipSetting );
QgsMapLayer * myLayer = NULL;
if ( myScanZipSetting == 1 )
myLayer = getLayer( myFileName, "", myProviderKey );
else
myLayer = getZipLayer( myFileName, "" );
if ( myLayer && myLayer->isValid() )
myTransparency = myLayer->getTransparency();
if ( myLayer )
delete myLayer;
return myTransparency;
}


// slots
void TestZipLayer::initTestCase()
{
QgsApplication::init();
Expand All @@ -192,7 +232,7 @@ void TestZipLayer::cleanupTestCase()

void TestZipLayer::testPassthruVectorZip()
{
QString myFileName = mDataDir + "points.zip";
QString myFileName = mDataDir + "points2.zip";
QgsDebugMsg( "GDAL: " + QString( GDAL_RELEASE_NAME ) );
#if GDAL_VERSION_NUM < 1800
myFileName = "/vsizip/" + myFileName + "/points.shp";
Expand All @@ -201,7 +241,7 @@ void TestZipLayer::testPassthruVectorZip()
for ( int i = 1 ; i <= mMaxScanZipSetting ; i++ )
{
mSettings.setValue( "/qgis/scanZipInBrowser", i );
QVERIFY( testPassthruVector( myFileName ) );
QVERIFY( testZipItemPassthru( myFileName, "ogr" ) );
}
}

Expand All @@ -210,7 +250,7 @@ void TestZipLayer::testPassthruVectorGzip()
for ( int i = 1 ; i <= mMaxScanZipSetting ; i++ )
{
mSettings.setValue( "/qgis/scanZipInBrowser", i );
QVERIFY( testPassthruVector( mDataDir + "points.geojson.gz" ) );
QVERIFY( testZipItemPassthru( mDataDir + "points3.geojson.gz", "ogr" ) );
}
}

Expand All @@ -219,7 +259,7 @@ void TestZipLayer::testPassthruRasterZip()
for ( int i = 1 ; i <= mMaxScanZipSetting ; i++ )
{
mSettings.setValue( "/qgis/scanZipInBrowser", i );
QVERIFY( testPassthruRaster( mDataDir + "landsat_b1.zip" ) );
QVERIFY( testZipItemPassthru( mDataDir + "landsat_b1.zip", "gdal" ) );
}
}

Expand All @@ -228,7 +268,7 @@ void TestZipLayer::testPassthruRasterGzip()
for ( int i = 1 ; i <= mMaxScanZipSetting ; i++ )
{
mSettings.setValue( "/qgis/scanZipInBrowser", i );
QVERIFY( testPassthruRaster( mDataDir + "landsat_b1.tif.gz" ) );
QVERIFY( testZipItemPassthru( mDataDir + "landsat_b1.tif.gz", "gdal" ) );
}
}

Expand Down Expand Up @@ -270,9 +310,43 @@ void TestZipLayer::testZipItemAll()
QVERIFY( testZipItem( mDataDir + "testzip.zip", "" ) );
}

QTEST_MAIN( TestZipLayer )
#include "moc_testziplayer.cxx"

void TestZipLayer::testZipItemVectorTransparency()
{
int myTarget = 250;
int myTransparency = getLayerTransparency( mDataDir + "points2.zip", "ogr", 1 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
myTransparency = getLayerTransparency( mDataDir + "points2.zip", "ogr", 2 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
}

void TestZipLayer::testGipItemVectorTransparency()
{
int myTarget = 250;
int myTransparency = getLayerTransparency( mDataDir + "points3.geojson.gz", "ogr", 1 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
myTransparency = getLayerTransparency( mDataDir + "points3.geojson.gz", "ogr", 2 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
}

void TestZipLayer::testZipItemRasterTransparency()
{
int myTarget = 250;
int myTransparency = getLayerTransparency( mDataDir + "landsat_b1.zip", "gdal", 1 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
myTransparency = getLayerTransparency( mDataDir + "landsat_b1.zip", "gdal", 2 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
}

void TestZipLayer::testGZipItemRasterTransparency()
{
int myTarget = 250;
int myTransparency = getLayerTransparency( mDataDir + "landsat_b1.tif.gz", "gdal", 1 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
myTransparency = getLayerTransparency( mDataDir + "landsat_b1.tif.gz", "gdal", 2 );
QVERIFY2(( myTransparency == myTarget ), QString( "Transparency is %1, should be %2" ).arg( myTransparency ).arg( myTarget ).toLocal8Bit().data() );
}


QTEST_MAIN( TestZipLayer )
#include "moc_testziplayer.cxx"
32 changes: 32 additions & 0 deletions tests/testdata/landsat_b1.qml
@@ -0,0 +1,32 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="1.9.90-Alpha" minimumScale="0" maximumScale="1e+08" hasScaleBasedVisibilityFlag="0">
<transparencyLevelInt>250</transparencyLevelInt>
<rasterproperties>
<mDrawingStyle>SingleBandGray</mDrawingStyle>
<mColorShadingAlgorithm>UndefinedShader</mColorShadingAlgorithm>
<mInvertColor boolean="false"/>
<mRedBandName>Not Set</mRedBandName>
<mGreenBandName>Not Set</mGreenBandName>
<mBlueBandName>Not Set</mBlueBandName>
<mGrayBandName>Band 1</mGrayBandName>
<mStandardDeviations>0</mStandardDeviations>
<mUserDefinedRGBMinimumMaximum boolean="false"/>
<mRGBMinimumMaximumEstimated boolean="true"/>
<mUserDefinedGrayMinimumMaximum boolean="false"/>
<mGrayMinimumMaximumEstimated boolean="true"/>
<mContrastEnhancementAlgorithm>StretchAndClipToMinimumMaximum</mContrastEnhancementAlgorithm>
<contrastEnhancementMinMaxValues>
<minMaxEntry>
<min>122</min>
<max>130</max>
</minMaxEntry>
</contrastEnhancementMinMaxValues>
<mNoDataValue mValidNoDataValue="true">-32768.000000</mNoDataValue>
<singleValuePixelList>
<pixelListEntry pixelValue="-32768.000000" percentTransparent="100"/>
</singleValuePixelList>
<threeValuePixelList>
<pixelListEntry red="-32768.000000" blue="-32768.000000" green="-32768.000000" percentTransparent="100"/>
</threeValuePixelList>
</rasterproperties>
</qgis>
32 changes: 32 additions & 0 deletions tests/testdata/landsat_b1.tif.qml
@@ -0,0 +1,32 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="1.9.90-Alpha" minimumScale="0" maximumScale="1e+08" hasScaleBasedVisibilityFlag="0">
<transparencyLevelInt>248</transparencyLevelInt>
<rasterproperties>
<mDrawingStyle>SingleBandGray</mDrawingStyle>
<mColorShadingAlgorithm>UndefinedShader</mColorShadingAlgorithm>
<mInvertColor boolean="false"/>
<mRedBandName></mRedBandName>
<mGreenBandName></mGreenBandName>
<mBlueBandName></mBlueBandName>
<mGrayBandName>Band 1</mGrayBandName>
<mStandardDeviations>0</mStandardDeviations>
<mUserDefinedRGBMinimumMaximum boolean="false"/>
<mRGBMinimumMaximumEstimated boolean="true"/>
<mUserDefinedGrayMinimumMaximum boolean="false"/>
<mGrayMinimumMaximumEstimated boolean="true"/>
<mContrastEnhancementAlgorithm>StretchAndClipToMinimumMaximum</mContrastEnhancementAlgorithm>
<contrastEnhancementMinMaxValues>
<minMaxEntry>
<min>122</min>
<max>130</max>
</minMaxEntry>
</contrastEnhancementMinMaxValues>
<mNoDataValue mValidNoDataValue="true">-32768.000000</mNoDataValue>
<singleValuePixelList>
<pixelListEntry pixelValue="-32768.000000" percentTransparent="100"/>
</singleValuePixelList>
<threeValuePixelList>
<pixelListEntry red="-32768.000000" blue="-32768.000000" green="-32768.000000" percentTransparent="100"/>
</threeValuePixelList>
</rasterproperties>
</qgis>
Binary file modified tests/testdata/landsat_b1.zip
Binary file not shown.
Binary file removed tests/testdata/points.zip
Binary file not shown.

0 comments on commit a859c62

Please sign in to comment.