|
| 1 | +/*************************************************************************** |
| 2 | + testqgsfilefiledownloader.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 07.06.2018 |
| 5 | + Copyright : (C) 2018 Alessandro Pasotti |
| 6 | + Email : elpaso at itopen dot it |
| 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 | + |
| 16 | + |
| 17 | +#include "qgstest.h" |
| 18 | +#include <QObject> |
| 19 | +#include <QTemporaryFile> |
| 20 | +#include <QTemporaryDir> |
| 21 | + |
| 22 | +#include <qgsapplication.h> |
| 23 | +#include <qgsproject.h> |
| 24 | +#include <qgslayertree.h> |
| 25 | +#include <qgslayerdefinition.h> |
| 26 | + |
| 27 | +class TestQgsLayerDefinition: public QObject |
| 28 | +{ |
| 29 | + Q_OBJECT |
| 30 | + public: |
| 31 | + TestQgsLayerDefinition() = default; |
| 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 | + /** |
| 40 | + * test that findLayers() skips invalid layers |
| 41 | + */ |
| 42 | + void testFindLayers(); |
| 43 | + |
| 44 | + /** |
| 45 | + * test that export does not crash: regression #18981 |
| 46 | + * https://issues.qgis.org/issues/18981 - Save QLR crashes QGIS 3 |
| 47 | + */ |
| 48 | + void testExportDoesNotCrash(); |
| 49 | + |
| 50 | + private: |
| 51 | + QTemporaryFile *mTempFile; |
| 52 | +}; |
| 53 | + |
| 54 | + |
| 55 | +void TestQgsLayerDefinition::initTestCase() |
| 56 | +{ |
| 57 | + QgsApplication::init(); |
| 58 | + QgsApplication::initQgis(); |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +void TestQgsLayerDefinition::cleanupTestCase() |
| 63 | +{ |
| 64 | + QgsApplication::exitQgis(); |
| 65 | +} |
| 66 | + |
| 67 | +void TestQgsLayerDefinition::init() |
| 68 | +{ |
| 69 | + mTempFile = new QTemporaryFile(); |
| 70 | + QVERIFY( mTempFile->open() ); |
| 71 | + mTempFile->close(); |
| 72 | + QString errorMessage; |
| 73 | + const QString path = QString( TEST_DATA_DIR + QStringLiteral( "/bug_18981_broken.qlr" ) ); |
| 74 | + QgsLayerDefinition::loadLayerDefinition( path, QgsProject::instance(), QgsProject::instance()->layerTreeRoot(), errorMessage ); |
| 75 | + QVERIFY( errorMessage.isEmpty() ); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +void TestQgsLayerDefinition::cleanup() |
| 80 | +{ |
| 81 | + delete mTempFile; |
| 82 | +} |
| 83 | + |
| 84 | +void TestQgsLayerDefinition::testFindLayers() |
| 85 | +{ |
| 86 | + QCOMPARE( QgsProject::instance()->layerTreeRoot()->findLayers().count(), 1 ); |
| 87 | + QCOMPARE( QgsProject::instance()->layerTreeRoot()->findLayers().at( 0 )->name(), QStringLiteral( "NewMemory" ) ); |
| 88 | +} |
| 89 | + |
| 90 | +void TestQgsLayerDefinition::testExportDoesNotCrash() |
| 91 | +{ |
| 92 | + QString errorMessage; |
| 93 | + QVERIFY( QgsLayerDefinition::exportLayerDefinition( mTempFile->fileName(), QgsProject::instance()->layerTreeRoot()->children(), errorMessage ) ); |
| 94 | + QVERIFY( errorMessage.isEmpty() ); |
| 95 | + // Reload |
| 96 | + const QString path = QString( TEST_DATA_DIR + QStringLiteral( "/bug_18981_broken.qlr" ) ); |
| 97 | + QgsProject::instance()->removeAllMapLayers(); |
| 98 | + QgsLayerDefinition::loadLayerDefinition( path, QgsProject::instance(), QgsProject::instance()->layerTreeRoot(), errorMessage ); |
| 99 | + testFindLayers(); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +QGSTEST_MAIN( TestQgsLayerDefinition ) |
| 105 | +#include "testqgslayerdefinition.moc" |
| 106 | + |
| 107 | + |
0 commit comments