Skip to content
Permalink
Browse files

Add test for QgsLayerDefinition (regression #18981)

  • Loading branch information
elpaso committed Jun 7, 2018
1 parent a389321 commit ad49cfb3a418384ffaa9c4240ad8123b27a1fa5d
Showing with 108 additions and 0 deletions.
  1. +1 −0 tests/src/core/CMakeLists.txt
  2. +107 −0 tests/src/core/testqgslayerdefinition.cpp
@@ -191,6 +191,7 @@ SET(TESTS
testziplayer.cpp
testqgsmeshlayer.cpp
testqgsmeshlayerrenderer.cpp
testqgslayerdefinition.cpp
)

IF(WITH_QTWEBKIT)
@@ -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.
You can’t perform that action at this time.