Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add test for QgsLayerDefinition (regression #18981)
  • Loading branch information
elpaso committed Jun 7, 2018
1 parent a389321 commit ad49cfb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -191,6 +191,7 @@ SET(TESTS
testziplayer.cpp
testqgsmeshlayer.cpp
testqgsmeshlayerrenderer.cpp
testqgslayerdefinition.cpp
)

IF(WITH_QTWEBKIT)
Expand Down
107 changes: 107 additions & 0 deletions tests/src/core/testqgslayerdefinition.cpp
@@ -0,0 +1,107 @@
/***************************************************************************
testqgsfilefiledownloader.cpp
--------------------------------------
Date : 07.06.2018
Copyright : (C) 2018 Alessandro Pasotti
Email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgstest.h"
#include <QObject>
#include <QTemporaryFile>
#include <QTemporaryDir>

#include <qgsapplication.h>
#include <qgsproject.h>
#include <qgslayertree.h>
#include <qgslayerdefinition.h>

class TestQgsLayerDefinition: public QObject
{
Q_OBJECT
public:
TestQgsLayerDefinition() = default;

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

/**
* test that findLayers() skips invalid layers
*/
void testFindLayers();

/**
* test that export does not crash: regression #18981
* https://issues.qgis.org/issues/18981 - Save QLR crashes QGIS 3
*/
void testExportDoesNotCrash();

private:
QTemporaryFile *mTempFile;
};


void TestQgsLayerDefinition::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

}

void TestQgsLayerDefinition::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsLayerDefinition::init()
{
mTempFile = new QTemporaryFile();
QVERIFY( mTempFile->open() );
mTempFile->close();
QString errorMessage;
const QString path = QString( TEST_DATA_DIR + QStringLiteral( "/bug_18981_broken.qlr" ) );
QgsLayerDefinition::loadLayerDefinition( path, QgsProject::instance(), QgsProject::instance()->layerTreeRoot(), errorMessage );
QVERIFY( errorMessage.isEmpty() );
}


void TestQgsLayerDefinition::cleanup()
{
delete mTempFile;
}

void TestQgsLayerDefinition::testFindLayers()
{
QCOMPARE( QgsProject::instance()->layerTreeRoot()->findLayers().count(), 1 );
QCOMPARE( QgsProject::instance()->layerTreeRoot()->findLayers().at( 0 )->name(), QStringLiteral( "NewMemory" ) );
}

void TestQgsLayerDefinition::testExportDoesNotCrash()
{
QString errorMessage;
QVERIFY( QgsLayerDefinition::exportLayerDefinition( mTempFile->fileName(), QgsProject::instance()->layerTreeRoot()->children(), errorMessage ) );
QVERIFY( errorMessage.isEmpty() );
// Reload
const QString path = QString( TEST_DATA_DIR + QStringLiteral( "/bug_18981_broken.qlr" ) );
QgsProject::instance()->removeAllMapLayers();
QgsLayerDefinition::loadLayerDefinition( path, QgsProject::instance(), QgsProject::instance()->layerTreeRoot(), errorMessage );
testFindLayers();
}



QGSTEST_MAIN( TestQgsLayerDefinition )
#include "testqgslayerdefinition.moc"


0 comments on commit ad49cfb

Please sign in to comment.