Skip to content

Commit 67f1e10

Browse files
committed
More unit test for QgsBrowserModel
1 parent 4d18eee commit 67f1e10

File tree

1 file changed

+53
-102
lines changed

1 file changed

+53
-102
lines changed

tests/src/core/testqgsbrowsermodel.cpp

+53-102
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,8 @@ class TestQgsBrowserModel : public QObject
3636
void init() {} // will be called before each testfunction is executed.
3737
void cleanup() {} // will be called after every testfunction.
3838

39-
void testValid();
40-
void testDirItemChildren();
4139
void testModel();
4240

43-
private:
44-
QgsDirectoryItem *mDirItem = nullptr;
45-
QString mScanItemsSetting;
46-
bool isValidDirItem( QgsDirectoryItem *item );
4741
};
4842

4943
void TestQgsBrowserModel::initTestCase()
@@ -60,109 +54,13 @@ void TestQgsBrowserModel::initTestCase()
6054
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
6155
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
6256
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
63-
64-
//create a directory item that will be used in all tests...
65-
mDirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), TEST_DATA_DIR );
6657
}
6758

6859
void TestQgsBrowserModel::cleanupTestCase()
6960
{
70-
if ( mDirItem )
71-
delete mDirItem;
72-
7361
QgsApplication::exitQgis();
7462
}
7563

76-
bool TestQgsBrowserModel::isValidDirItem( QgsDirectoryItem *item )
77-
{
78-
return ( item && item->hasChildren() );
79-
}
80-
81-
void TestQgsBrowserModel::testValid()
82-
{
83-
if ( mDirItem )
84-
{
85-
QgsDebugMsg( QStringLiteral( "dirItem has %1 children" ).arg( mDirItem->rowCount() ) );
86-
}
87-
QVERIFY( isValidDirItem( mDirItem ) );
88-
}
89-
90-
void TestQgsBrowserModel::testDirItemChildren()
91-
{
92-
QgsSettings settings;
93-
QStringList tmpSettings;
94-
tmpSettings << QString() << QStringLiteral( "contents" ) << QStringLiteral( "extension" );
95-
Q_FOREACH ( const QString &tmpSetting, tmpSettings )
96-
{
97-
settings.setValue( QStringLiteral( "/qgis/scanItemsInBrowser2" ), tmpSetting );
98-
QgsDirectoryItem *dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), TEST_DATA_DIR );
99-
QVERIFY( isValidDirItem( dirItem ) );
100-
101-
QVector<QgsDataItem *> children = dirItem->createChildren();
102-
for ( int i = 0; i < children.size(); i++ )
103-
{
104-
QgsDataItem *dataItem = children[i];
105-
QgsLayerItem *layerItem = dynamic_cast<QgsLayerItem *>( dataItem );
106-
if ( ! layerItem )
107-
continue;
108-
109-
// test .vrt and .gz files are not loaded by gdal and ogr
110-
QFileInfo info( layerItem->path() );
111-
QString lFile = info.fileName();
112-
QString lProvider = layerItem->providerKey();
113-
QString errStr = QStringLiteral( "layer #%1 - %2 provider = %3 tmpSetting = %4" ).arg( i ).arg( lFile, lProvider, tmpSetting );
114-
115-
QgsDebugMsg( QStringLiteral( "testing child name=%1 provider=%2 path=%3 tmpSetting = %4" ).arg( layerItem->name(), lProvider, lFile, tmpSetting ) );
116-
117-
if ( lFile == QLatin1String( "landsat.tif" ) )
118-
{
119-
QVERIFY2( lProvider == "gdal", errStr.toLocal8Bit().constData() );
120-
}
121-
else if ( lFile == QLatin1String( "points.vrt" ) )
122-
{
123-
QVERIFY2( lProvider == "ogr", errStr.toLocal8Bit().constData() );
124-
}
125-
else if ( lFile == QLatin1String( "landsat.vrt" ) )
126-
{
127-
QVERIFY2( lProvider == "gdal", errStr.toLocal8Bit().constData() );
128-
}
129-
else if ( lFile == QLatin1String( "landsat_b1.tif.gz" ) )
130-
{
131-
QVERIFY2( lProvider == "gdal", errStr.toLocal8Bit().constData() );
132-
}
133-
else if ( lFile == QLatin1String( "points3.geojson.gz" ) )
134-
{
135-
QVERIFY2( lProvider == "ogr", errStr.toLocal8Bit().constData() );
136-
}
137-
138-
// test layerName() does not include extension for gdal and ogr items (bug #5621)
139-
QString lName = layerItem->layerName();
140-
errStr = QStringLiteral( "layer #%1 - %2 lName = %3 tmpSetting = %4" ).arg( i ).arg( lFile, lName, tmpSetting );
141-
142-
if ( lFile == QLatin1String( "landsat.tif" ) )
143-
{
144-
QVERIFY2( lName == "landsat", errStr.toLocal8Bit().constData() );
145-
}
146-
else if ( lFile == QLatin1String( "points.shp" ) )
147-
{
148-
QVERIFY2( lName == "points", errStr.toLocal8Bit().constData() );
149-
}
150-
else if ( lFile == QLatin1String( "landsat_b1.tif.gz" ) )
151-
{
152-
QVERIFY2( lName == "landsat_b1", errStr.toLocal8Bit().constData() );
153-
}
154-
else if ( lFile == QLatin1String( "points3.geojson.gz" ) )
155-
{
156-
QVERIFY2( lName == "points3", errStr.toLocal8Bit().constData() );
157-
}
158-
159-
}
160-
qDeleteAll( children );
161-
162-
delete dirItem;
163-
}
164-
}
165-
16664
void TestQgsBrowserModel::testModel()
16765
{
16866
QgsBrowserModel model;
@@ -173,6 +71,59 @@ void TestQgsBrowserModel::testModel()
17371
QVERIFY( !model.data( QModelIndex() ).isValid() );
17472
QVERIFY( !model.flags( QModelIndex() ) );
17573
QVERIFY( !model.hasChildren() );
74+
QVERIFY( !model.dataItem( QModelIndex() ) );
75+
76+
// add a root child
77+
QgsDataCollectionItem *rootItem1 = new QgsDataCollectionItem( nullptr, QStringLiteral( "Test" ), QStringLiteral( "root1" ) );
78+
model.connectItem( rootItem1 );
79+
model.mRootItems.append( rootItem1 );
80+
81+
QCOMPARE( model.rowCount(), 1 );
82+
QCOMPARE( model.columnCount(), 1 );
83+
QVERIFY( !model.data( QModelIndex() ).isValid() );
84+
QVERIFY( !model.flags( QModelIndex() ) );
85+
QVERIFY( model.hasChildren() );
86+
QModelIndex root1Index = model.index( 0, 0 );
87+
QVERIFY( root1Index.isValid() );
88+
QCOMPARE( model.rowCount( root1Index ), 0 );
89+
QCOMPARE( model.columnCount( root1Index ), 1 );
90+
// initially, we say the item has children, until it's populated and we know for sure
91+
QVERIFY( model.hasChildren( root1Index ) );
92+
rootItem1->setState( QgsDataItem::Populated );
93+
QVERIFY( !model.hasChildren( root1Index ) );
94+
QCOMPARE( model.data( root1Index ).toString(), QStringLiteral( "Test" ) );
95+
QCOMPARE( model.data( root1Index, QgsBrowserModel::PathRole ).toString(), QStringLiteral( "root1" ) );
96+
QCOMPARE( model.dataItem( root1Index ), rootItem1 );
97+
98+
// second root item
99+
QgsDataCollectionItem *rootItem2 = new QgsDataCollectionItem( nullptr, QStringLiteral( "Test2" ), QStringLiteral( "root2" ) );
100+
model.connectItem( rootItem2 );
101+
model.mRootItems.append( rootItem2 );
102+
103+
QCOMPARE( model.rowCount(), 2 );
104+
QVERIFY( model.hasChildren() );
105+
QModelIndex root2Index = model.index( 1, 0 );
106+
QVERIFY( root2Index.isValid() );
107+
QCOMPARE( model.rowCount( root2Index ), 0 );
108+
QCOMPARE( model.columnCount( root2Index ), 1 );
109+
QCOMPARE( model.data( root2Index ).toString(), QStringLiteral( "Test2" ) );
110+
QCOMPARE( model.data( root2Index, QgsBrowserModel::PathRole ).toString(), QStringLiteral( "root2" ) );
111+
QCOMPARE( model.dataItem( root2Index ), rootItem2 );
112+
113+
// child item
114+
QgsDataCollectionItem *childItem1 = new QgsDataCollectionItem( nullptr, QStringLiteral( "Child1" ), QStringLiteral( "child1" ) );
115+
model.connectItem( childItem1 );
116+
rootItem1->addChild( childItem1 );
117+
118+
QCOMPARE( model.rowCount(), 2 );
119+
QCOMPARE( model.columnCount(), 1 );
120+
QCOMPARE( model.rowCount( root1Index ), 1 );
121+
QCOMPARE( model.columnCount( root1Index ), 1 );
122+
QVERIFY( model.hasChildren( root1Index ) );
123+
QModelIndex child1Index = model.index( 0, 0, root1Index );
124+
QCOMPARE( model.data( child1Index ).toString(), QStringLiteral( "Child1" ) );
125+
QCOMPARE( model.data( child1Index, QgsBrowserModel::PathRole ).toString(), QStringLiteral( "child1" ) );
126+
QCOMPARE( model.dataItem( child1Index ), childItem1 );
176127

177128
}
178129

0 commit comments

Comments
 (0)