Skip to content

Commit

Permalink
More unit test for QgsBrowserModel
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 18, 2018
1 parent 4d18eee commit 67f1e10
Showing 1 changed file with 53 additions and 102 deletions.
155 changes: 53 additions & 102 deletions tests/src/core/testqgsbrowsermodel.cpp
Expand Up @@ -36,14 +36,8 @@ class TestQgsBrowserModel : public QObject
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.

void testValid();
void testDirItemChildren();
void testModel();

private:
QgsDirectoryItem *mDirItem = nullptr;
QString mScanItemsSetting;
bool isValidDirItem( QgsDirectoryItem *item );
};

void TestQgsBrowserModel::initTestCase()
Expand All @@ -60,109 +54,13 @@ void TestQgsBrowserModel::initTestCase()
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );

//create a directory item that will be used in all tests...
mDirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), TEST_DATA_DIR );
}

void TestQgsBrowserModel::cleanupTestCase()
{
if ( mDirItem )
delete mDirItem;

QgsApplication::exitQgis();
}

bool TestQgsBrowserModel::isValidDirItem( QgsDirectoryItem *item )
{
return ( item && item->hasChildren() );
}

void TestQgsBrowserModel::testValid()
{
if ( mDirItem )
{
QgsDebugMsg( QStringLiteral( "dirItem has %1 children" ).arg( mDirItem->rowCount() ) );
}
QVERIFY( isValidDirItem( mDirItem ) );
}

void TestQgsBrowserModel::testDirItemChildren()
{
QgsSettings settings;
QStringList tmpSettings;
tmpSettings << QString() << QStringLiteral( "contents" ) << QStringLiteral( "extension" );
Q_FOREACH ( const QString &tmpSetting, tmpSettings )
{
settings.setValue( QStringLiteral( "/qgis/scanItemsInBrowser2" ), tmpSetting );
QgsDirectoryItem *dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "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;

// test .vrt and .gz files are not loaded by gdal and ogr
QFileInfo info( layerItem->path() );
QString lFile = info.fileName();
QString lProvider = layerItem->providerKey();
QString errStr = QStringLiteral( "layer #%1 - %2 provider = %3 tmpSetting = %4" ).arg( i ).arg( lFile, lProvider, tmpSetting );

QgsDebugMsg( QStringLiteral( "testing child name=%1 provider=%2 path=%3 tmpSetting = %4" ).arg( layerItem->name(), lProvider, lFile, tmpSetting ) );

if ( lFile == QLatin1String( "landsat.tif" ) )
{
QVERIFY2( lProvider == "gdal", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "points.vrt" ) )
{
QVERIFY2( lProvider == "ogr", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "landsat.vrt" ) )
{
QVERIFY2( lProvider == "gdal", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "landsat_b1.tif.gz" ) )
{
QVERIFY2( lProvider == "gdal", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "points3.geojson.gz" ) )
{
QVERIFY2( lProvider == "ogr", errStr.toLocal8Bit().constData() );
}

// test layerName() does not include extension for gdal and ogr items (bug #5621)
QString lName = layerItem->layerName();
errStr = QStringLiteral( "layer #%1 - %2 lName = %3 tmpSetting = %4" ).arg( i ).arg( lFile, lName, tmpSetting );

if ( lFile == QLatin1String( "landsat.tif" ) )
{
QVERIFY2( lName == "landsat", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "points.shp" ) )
{
QVERIFY2( lName == "points", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "landsat_b1.tif.gz" ) )
{
QVERIFY2( lName == "landsat_b1", errStr.toLocal8Bit().constData() );
}
else if ( lFile == QLatin1String( "points3.geojson.gz" ) )
{
QVERIFY2( lName == "points3", errStr.toLocal8Bit().constData() );
}

}
qDeleteAll( children );

delete dirItem;
}
}

void TestQgsBrowserModel::testModel()
{
QgsBrowserModel model;
Expand All @@ -173,6 +71,59 @@ void TestQgsBrowserModel::testModel()
QVERIFY( !model.data( QModelIndex() ).isValid() );
QVERIFY( !model.flags( QModelIndex() ) );
QVERIFY( !model.hasChildren() );
QVERIFY( !model.dataItem( QModelIndex() ) );

// add a root child
QgsDataCollectionItem *rootItem1 = new QgsDataCollectionItem( nullptr, QStringLiteral( "Test" ), QStringLiteral( "root1" ) );
model.connectItem( rootItem1 );
model.mRootItems.append( rootItem1 );

QCOMPARE( model.rowCount(), 1 );
QCOMPARE( model.columnCount(), 1 );
QVERIFY( !model.data( QModelIndex() ).isValid() );
QVERIFY( !model.flags( QModelIndex() ) );
QVERIFY( model.hasChildren() );
QModelIndex root1Index = model.index( 0, 0 );
QVERIFY( root1Index.isValid() );
QCOMPARE( model.rowCount( root1Index ), 0 );
QCOMPARE( model.columnCount( root1Index ), 1 );
// initially, we say the item has children, until it's populated and we know for sure
QVERIFY( model.hasChildren( root1Index ) );
rootItem1->setState( QgsDataItem::Populated );
QVERIFY( !model.hasChildren( root1Index ) );
QCOMPARE( model.data( root1Index ).toString(), QStringLiteral( "Test" ) );
QCOMPARE( model.data( root1Index, QgsBrowserModel::PathRole ).toString(), QStringLiteral( "root1" ) );
QCOMPARE( model.dataItem( root1Index ), rootItem1 );

// second root item
QgsDataCollectionItem *rootItem2 = new QgsDataCollectionItem( nullptr, QStringLiteral( "Test2" ), QStringLiteral( "root2" ) );
model.connectItem( rootItem2 );
model.mRootItems.append( rootItem2 );

QCOMPARE( model.rowCount(), 2 );
QVERIFY( model.hasChildren() );
QModelIndex root2Index = model.index( 1, 0 );
QVERIFY( root2Index.isValid() );
QCOMPARE( model.rowCount( root2Index ), 0 );
QCOMPARE( model.columnCount( root2Index ), 1 );
QCOMPARE( model.data( root2Index ).toString(), QStringLiteral( "Test2" ) );
QCOMPARE( model.data( root2Index, QgsBrowserModel::PathRole ).toString(), QStringLiteral( "root2" ) );
QCOMPARE( model.dataItem( root2Index ), rootItem2 );

// child item
QgsDataCollectionItem *childItem1 = new QgsDataCollectionItem( nullptr, QStringLiteral( "Child1" ), QStringLiteral( "child1" ) );
model.connectItem( childItem1 );
rootItem1->addChild( childItem1 );

QCOMPARE( model.rowCount(), 2 );
QCOMPARE( model.columnCount(), 1 );
QCOMPARE( model.rowCount( root1Index ), 1 );
QCOMPARE( model.columnCount( root1Index ), 1 );
QVERIFY( model.hasChildren( root1Index ) );
QModelIndex child1Index = model.index( 0, 0, root1Index );
QCOMPARE( model.data( child1Index ).toString(), QStringLiteral( "Child1" ) );
QCOMPARE( model.data( child1Index, QgsBrowserModel::PathRole ).toString(), QStringLiteral( "child1" ) );
QCOMPARE( model.dataItem( child1Index ), childItem1 );

}

Expand Down

0 comments on commit 67f1e10

Please sign in to comment.