|
| 1 | +/*************************************************************************** |
| 2 | + testprocesinggui.cpp |
| 3 | + --------------------------- |
| 4 | + begin : April 2018 |
| 5 | + copyright : (C) 2018 by Matthias Kuhn |
| 6 | + email : matthias@opengis.ch |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include <QObject> |
| 19 | +#include "qgstest.h" |
| 20 | +#include "qgsgui.h" |
| 21 | +#include "qgsprocessingguiregistry.h" |
| 22 | +#include "qgsprocessingregistry.h" |
| 23 | +#include "qgsprocessingalgorithmconfigurationwidget.h" |
| 24 | +#include "qgsnativealgorithms.h" |
| 25 | +#include "qgsxmlutils.h" |
| 26 | + |
| 27 | +class TestProcessingGui : public QObject |
| 28 | +{ |
| 29 | + Q_OBJECT |
| 30 | + public: |
| 31 | + TestProcessingGui() = 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 | + void testSetGetConfig(); |
| 39 | + void testFilterAlgorithmConfig(); |
| 40 | +}; |
| 41 | + |
| 42 | +void TestProcessingGui::initTestCase() |
| 43 | +{ |
| 44 | + QgsApplication::init(); |
| 45 | + QgsApplication::initQgis(); |
| 46 | + QgsApplication::processingRegistry()->addProvider( new QgsNativeAlgorithms( QgsApplication::processingRegistry() ) ); |
| 47 | +} |
| 48 | + |
| 49 | +void TestProcessingGui::cleanupTestCase() |
| 50 | +{ |
| 51 | + QgsApplication::exitQgis(); |
| 52 | +} |
| 53 | +void TestProcessingGui::init() |
| 54 | +{ |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +void TestProcessingGui::cleanup() |
| 59 | +{ |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +void TestProcessingGui::testSetGetConfig() |
| 64 | +{ |
| 65 | + const QList< const QgsProcessingAlgorithm * > algorithms = QgsApplication::instance()->processingRegistry()->algorithms(); |
| 66 | + |
| 67 | + // Find all defined widgets for native algorithms |
| 68 | + // and get the default configuration (that is, we create a widget |
| 69 | + // and get the configuration it returns without being modified in any way) |
| 70 | + // We then set and get this configuration and validate that it matches the original one. |
| 71 | + for ( const QgsProcessingAlgorithm *algorithm : algorithms ) |
| 72 | + { |
| 73 | + std::unique_ptr<QgsProcessingAlgorithmConfigurationWidget> configWidget( QgsGui::instance()->processingGuiRegistry()->algorithmConfigurationWidget( algorithm ) ); |
| 74 | + |
| 75 | + if ( configWidget ) |
| 76 | + { |
| 77 | + const QVariantMap defaultConfig = configWidget->configuration(); |
| 78 | + configWidget->setConfiguration( defaultConfig ); |
| 79 | + const QVariantMap defaultControlConfig = configWidget->configuration(); |
| 80 | + QDomDocument defaultConfigDoc; |
| 81 | + QDomDocument defaultConfigControlDoc; |
| 82 | + QgsXmlUtils::writeVariant( defaultConfig, defaultConfigDoc ); |
| 83 | + QgsXmlUtils::writeVariant( defaultControlConfig, defaultConfigControlDoc ); |
| 84 | + QCOMPARE( defaultConfigDoc.toString(), defaultConfigControlDoc.toString() ); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void TestProcessingGui::testFilterAlgorithmConfig() |
| 90 | +{ |
| 91 | + const QgsProcessingAlgorithm *algorithm = QgsApplication::instance()->processingRegistry()->algorithmById( QStringLiteral( "native:filter" ) ); |
| 92 | + std::unique_ptr<QgsProcessingAlgorithmConfigurationWidget> configWidget( QgsGui::instance()->processingGuiRegistry()->algorithmConfigurationWidget( algorithm ) ); |
| 93 | + |
| 94 | + QVariantMap config; |
| 95 | + QVariantList outputs; |
| 96 | + QVariantMap output; |
| 97 | + output.insert( QStringLiteral( "name" ), QStringLiteral( "test" ) ); |
| 98 | + output.insert( QStringLiteral( "expression" ), QStringLiteral( "I am an expression" ) ); |
| 99 | + output.insert( QStringLiteral( "isModelOutput" ), true ); |
| 100 | + outputs.append( output ); |
| 101 | + config.insert( QStringLiteral( "outputs" ), outputs ); |
| 102 | + configWidget->setConfiguration( config ); |
| 103 | + |
| 104 | + QVariantMap configControl = configWidget->configuration(); |
| 105 | + |
| 106 | + QDomDocument configDoc; |
| 107 | + QDomDocument configControlDoc; |
| 108 | + QgsXmlUtils::writeVariant( config, configDoc ); |
| 109 | + QgsXmlUtils::writeVariant( configControl, configControlDoc ); |
| 110 | + QCOMPARE( configDoc.toString(), configControlDoc.toString() ); |
| 111 | +} |
| 112 | + |
| 113 | +QGSTEST_MAIN( TestProcessingGui ) |
| 114 | +#include "testprocessinggui.moc" |
0 commit comments