|
| 1 | +/*************************************************************************** |
| 2 | + testqgsbrowsermodel.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : October 2018 |
| 5 | + Copyright : (C) 2018 Nyall Dawson |
| 6 | + Email : nyall dot dawson 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 "qgstest.h" |
| 16 | + |
| 17 | +#include <QObject> |
| 18 | +#include <QString> |
| 19 | +#include <QStringList> |
| 20 | + |
| 21 | +//qgis includes... |
| 22 | +#include "qgsdataitem.h" |
| 23 | +#include "qgsvectorlayer.h" |
| 24 | +#include "qgsapplication.h" |
| 25 | +#include "qgslogger.h" |
| 26 | +#include "qgssettings.h" |
| 27 | +#include "qgsbrowsermodel.h" |
| 28 | + |
| 29 | +class TestQgsBrowserModel : public QObject |
| 30 | +{ |
| 31 | + Q_OBJECT |
| 32 | + |
| 33 | + private slots: |
| 34 | + void initTestCase();// will be called before the first testfunction is executed. |
| 35 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 36 | + void init() {} // will be called before each testfunction is executed. |
| 37 | + void cleanup() {} // will be called after every testfunction. |
| 38 | + |
| 39 | + void testValid(); |
| 40 | + void testDirItemChildren(); |
| 41 | + void testModel(); |
| 42 | + |
| 43 | + private: |
| 44 | + QgsDirectoryItem *mDirItem = nullptr; |
| 45 | + QString mScanItemsSetting; |
| 46 | + bool isValidDirItem( QgsDirectoryItem *item ); |
| 47 | +}; |
| 48 | + |
| 49 | +void TestQgsBrowserModel::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 QgsSettings environment |
| 60 | + QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) ); |
| 61 | + QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) ); |
| 62 | + 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 ); |
| 66 | +} |
| 67 | + |
| 68 | +void TestQgsBrowserModel::cleanupTestCase() |
| 69 | +{ |
| 70 | + if ( mDirItem ) |
| 71 | + delete mDirItem; |
| 72 | + |
| 73 | + QgsApplication::exitQgis(); |
| 74 | +} |
| 75 | + |
| 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 | + |
| 166 | +void TestQgsBrowserModel::testModel() |
| 167 | +{ |
| 168 | + QgsBrowserModel model; |
| 169 | + |
| 170 | + // empty |
| 171 | + QCOMPARE( model.rowCount(), 0 ); |
| 172 | + QCOMPARE( model.columnCount(), 1 ); |
| 173 | + QVERIFY( !model.data( QModelIndex() ).isValid() ); |
| 174 | + QVERIFY( !model.flags( QModelIndex() ) ); |
| 175 | + QVERIFY( !model.hasChildren() ); |
| 176 | + |
| 177 | +} |
| 178 | + |
| 179 | +QGSTEST_MAIN( TestQgsBrowserModel ) |
| 180 | +#include "testqgsbrowsermodel.moc" |
0 commit comments