Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 29, 2020
1 parent c923546 commit 2204399
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/src/analysis/testqgsprocessingalgs.cpp
Expand Up @@ -131,6 +131,8 @@ class TestQgsProcessingAlgs: public QObject

void saveLog();
void setProjectVariable();
void exportLayoutPdf();
void exportLayoutPng();

private:

Expand Down Expand Up @@ -3691,5 +3693,77 @@ void TestQgsProcessingAlgs::setProjectVariable()
QCOMPARE( scope->variable( QStringLiteral( "my_var" ) ).toInt(), 13 );
}

void TestQgsProcessingAlgs::exportLayoutPdf()
{
QgsProject p;
QgsPrintLayout *layout = new QgsPrintLayout( &p );
layout->initializeDefaults();
layout->setName( QStringLiteral( "my layout" ) );
p.layoutManager()->addLayout( layout );

std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:printlayouttopdf" ) ) );
QVERIFY( alg != nullptr );

const QString outputPdf = QDir::tempPath() + "/my_layout.pdf";
if ( QFile::exists( outputPdf ) )
QFile::remove( outputPdf );

QVariantMap parameters;
parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "missing" ) );
parameters.insert( QStringLiteral( "OUTPUT" ), outputPdf );

bool ok = false;
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
context->setProject( &p );
QgsProcessingFeedback feedback;
QVariantMap results;
results = alg->run( parameters, *context, &feedback, &ok );
// invalid layout name
QVERIFY( !ok );
QVERIFY( !QFile::exists( outputPdf ) );

parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "my layout" ) );
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );

QVERIFY( QFile::exists( outputPdf ) );
}

void TestQgsProcessingAlgs::exportLayoutPng()
{
QgsProject p;
QgsPrintLayout *layout = new QgsPrintLayout( &p );
layout->initializeDefaults();
layout->setName( QStringLiteral( "my layout" ) );
p.layoutManager()->addLayout( layout );

std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:printlayouttoimage" ) ) );
QVERIFY( alg != nullptr );

const QString outputPdf = QDir::tempPath() + "/my_layout.png";
if ( QFile::exists( outputPdf ) )
QFile::remove( outputPdf );

QVariantMap parameters;
parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "missing" ) );
parameters.insert( QStringLiteral( "OUTPUT" ), outputPdf );

bool ok = false;
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
context->setProject( &p );
QgsProcessingFeedback feedback;
QVariantMap results;
results = alg->run( parameters, *context, &feedback, &ok );
// invalid layout name
QVERIFY( !ok );
QVERIFY( !QFile::exists( outputPdf ) );

parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "my layout" ) );
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );

QVERIFY( QFile::exists( outputPdf ) );
}

QGSTEST_MAIN( TestQgsProcessingAlgs )
#include "testqgsprocessingalgs.moc"

0 comments on commit 2204399

Please sign in to comment.