File indexing completed on 2022-10-04 14:00:19

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0004 //
0005 
0006 
0007 #include "MarbleModel.h"
0008 #include "PluginManager.h"
0009 #include "RenderPlugin.h"
0010 
0011 #include <QMetaType>
0012 #include <QTest>
0013 
0014 Q_DECLARE_METATYPE( const Marble::RenderPlugin * )
0015 
0016 namespace Marble
0017 {
0018 
0019 class RenderPluginTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023  private Q_SLOTS:
0024     void newInstance_data();
0025     void newInstance();
0026 
0027     void initialize_data();
0028     void initialize();
0029 
0030     void setVisible_data();
0031     void setVisible();
0032 
0033     void setEnabled_data();
0034     void setEnabled();
0035 
0036     void setSettingsVisible_data();
0037     void setSettingsVisible();
0038 
0039     void setSettingsEnabled_data();
0040     void setSettingsEnabled();
0041 
0042     void setSettingsAllEqual_data();
0043     void setSettingsAllEqual();
0044 
0045     void restoreDefaultSettings_data();
0046     void restoreDefaultSettings();
0047 
0048  private:
0049     MarbleModel m_model;
0050 };
0051 
0052 void RenderPluginTest::newInstance_data()
0053 {
0054     QTest::addColumn<const RenderPlugin *>( "factory" );
0055 
0056     foreach ( const RenderPlugin *factory, m_model.pluginManager()->renderPlugins() ) {
0057         QTest::newRow(factory->nameId().toLatin1().constData())
0058             << factory;
0059     }
0060 }
0061 
0062 void RenderPluginTest::newInstance()
0063 {
0064     QFETCH( const RenderPlugin *, factory );
0065 
0066     RenderPlugin *const instance = factory->newInstance( &m_model );
0067 
0068     const bool enabledByDefault = instance->enabled();
0069     const bool visibleByDefault = instance->visible();
0070 
0071     QVERIFY( instance->settings().contains( "enabled" ) );
0072     QVERIFY( instance->settings().contains( "visible" ) );
0073     QCOMPARE( instance->settings().value( "enabled", !enabledByDefault ).toBool(), enabledByDefault );
0074     QCOMPARE( instance->settings().value( "visible", !visibleByDefault ).toBool(), visibleByDefault );
0075 
0076     delete instance;
0077 }
0078 
0079 void RenderPluginTest::initialize_data()
0080 {
0081     QTest::addColumn<const RenderPlugin *>( "factory" );
0082 
0083     foreach ( const RenderPlugin *plugin, m_model.pluginManager()->renderPlugins() ) {
0084         QTest::newRow(plugin->nameId().toLatin1().constData())
0085             << plugin;
0086     }
0087 }
0088 
0089 void RenderPluginTest::initialize()
0090 {
0091     QFETCH( const RenderPlugin *, factory );
0092 
0093     RenderPlugin *const instance = factory->newInstance( &m_model );
0094 
0095     instance->initialize();
0096 
0097     // prevent infinite loops
0098     QVERIFY( instance->isInitialized() );
0099 }
0100 
0101 void RenderPluginTest::setVisible_data()
0102 {
0103     QTest::addColumn<const RenderPlugin *>( "factory" );
0104 
0105     foreach ( const RenderPlugin *factory, m_model.pluginManager()->renderPlugins() ) {
0106         QTest::newRow(factory->nameId().toLatin1().constData())
0107             << factory;
0108     }
0109 }
0110 
0111 void RenderPluginTest::setVisible()
0112 {
0113     QFETCH( const RenderPlugin *, factory );
0114 
0115     RenderPlugin *const instance = factory->newInstance( &m_model );
0116 
0117     const bool visibleByDefault = instance->visible();
0118 
0119     instance->setVisible( !visibleByDefault );
0120 
0121     QCOMPARE( instance->visible(), !visibleByDefault );
0122     QCOMPARE( instance->settings().value( "visible", visibleByDefault ).toBool(), !visibleByDefault );
0123 
0124     QHash<QString, QVariant> settings;
0125     settings.insert( "visible", !visibleByDefault );
0126 
0127     instance->setSettings( settings );
0128     instance->setVisible( visibleByDefault ); // should also overwrite setting "visible"
0129 
0130     QCOMPARE( instance->visible(), visibleByDefault );
0131     QCOMPARE( instance->settings().value( "visible", !visibleByDefault ).toBool(), visibleByDefault );
0132 
0133     delete instance;
0134 }
0135 
0136 void RenderPluginTest::setEnabled_data()
0137 {
0138     QTest::addColumn<const RenderPlugin *>( "factory" );
0139 
0140     foreach ( const RenderPlugin *factory, m_model.pluginManager()->renderPlugins() ) {
0141         QTest::newRow(factory->nameId().toLatin1().constData())
0142             << factory;
0143     }
0144 }
0145 
0146 void RenderPluginTest::setEnabled()
0147 {
0148     QFETCH( const RenderPlugin *, factory );
0149 
0150     RenderPlugin *const instance = factory->newInstance( &m_model );
0151 
0152     const bool enabledByDefault = instance->enabled();
0153 
0154     instance->setEnabled( !enabledByDefault );
0155 
0156     QCOMPARE( instance->enabled(), !enabledByDefault );
0157     QCOMPARE( instance->settings().value( "enabled", enabledByDefault ).toBool(), !enabledByDefault );
0158 
0159     QHash<QString, QVariant> settings;
0160     settings.insert( "enabled", !enabledByDefault );
0161 
0162     instance->setSettings( settings );
0163     instance->setEnabled( enabledByDefault ); // should also overwrite setting "enabled"
0164 
0165     QCOMPARE( instance->enabled(), enabledByDefault );
0166     QCOMPARE( instance->settings().value( "enabled", !enabledByDefault ).toBool(), enabledByDefault );
0167 
0168     delete instance;
0169 }
0170 
0171 void RenderPluginTest::setSettingsVisible_data()
0172 {
0173     QTest::addColumn<const RenderPlugin *>( "factory" );
0174 
0175     foreach ( const RenderPlugin *factory, m_model.pluginManager()->renderPlugins() ) {
0176         QTest::newRow(factory->nameId().toLatin1().constData())
0177             << factory;
0178     }
0179 }
0180 
0181 void RenderPluginTest::setSettingsVisible()
0182 {
0183     QFETCH( const RenderPlugin *, factory );
0184 
0185     RenderPlugin *const instance = factory->newInstance( &m_model );
0186 
0187     const bool visibleByDefault = instance->visible();
0188 
0189     QHash<QString, QVariant> settings = instance->settings();
0190 
0191     // visibile property should follow setting
0192     settings.insert( "visible", !visibleByDefault );
0193     instance->setSettings( settings );
0194 
0195     QCOMPARE( instance->settings().value( "visible", visibleByDefault ).toBool(), !visibleByDefault );
0196     QCOMPARE( instance->visible(), !visibleByDefault );
0197 
0198     settings.insert( "visible", visibleByDefault );
0199     instance->setSettings( settings );
0200 
0201     QCOMPARE( instance->settings().value( "visible", !visibleByDefault ).toBool(), visibleByDefault );
0202     QCOMPARE( instance->visible(), visibleByDefault );
0203 
0204     // restoreDefaultSettings() is triggered by the config dialog, but it shouldn't touch visible property
0205     instance->setVisible( !visibleByDefault );
0206     instance->restoreDefaultSettings();
0207 
0208     QCOMPARE( instance->visible(), !visibleByDefault );
0209 
0210     delete instance;
0211 }
0212 
0213 void RenderPluginTest::setSettingsEnabled_data()
0214 {
0215     QTest::addColumn<const RenderPlugin *>( "factory" );
0216 
0217     foreach ( const RenderPlugin *factory, m_model.pluginManager()->renderPlugins() ) {
0218         QTest::newRow(factory->nameId().toLatin1().constData())
0219             << factory;
0220     }
0221 }
0222 
0223 void RenderPluginTest::setSettingsEnabled()
0224 {
0225     QFETCH( const RenderPlugin *, factory );
0226 
0227     RenderPlugin *const instance = factory->newInstance( &m_model );
0228 
0229     const bool enabledByDefault = instance->enabled();
0230 
0231     QHash<QString, QVariant> settings = instance->settings();
0232 
0233     // enabled property should follow setting
0234     settings.insert( "enabled", !enabledByDefault );
0235     instance->setSettings( settings );
0236 
0237     QCOMPARE( instance->settings().value( "enabled", enabledByDefault ).toBool(), !enabledByDefault );
0238     QCOMPARE( instance->enabled(), !enabledByDefault );
0239 
0240     settings.insert( "enabled", enabledByDefault );
0241     instance->setSettings( settings );
0242 
0243     QCOMPARE( instance->settings().value( "enabled", !enabledByDefault ).toBool(), enabledByDefault );
0244     QCOMPARE( instance->enabled(), enabledByDefault );
0245 
0246     // restoreDefaultSettings() is triggered by the config dialog, but it shouldn't touch enabled property
0247     instance->setEnabled( !enabledByDefault );
0248     instance->restoreDefaultSettings();
0249 
0250     QCOMPARE( instance->enabled(), !enabledByDefault );
0251 
0252     delete instance;
0253 }
0254 
0255 void RenderPluginTest::setSettingsAllEqual_data()
0256 {
0257     QTest::addColumn<QVariant>( "result" );
0258     QTest::addColumn<QVariant>( "expected" );
0259 
0260     foreach ( const RenderPlugin *plugin, m_model.pluginManager()->renderPlugins() ) {
0261         const RenderPlugin *const expected = plugin->newInstance( &m_model );
0262 
0263         RenderPlugin *const result = plugin->newInstance( &m_model );
0264         result->setSettings( expected->settings() );
0265 
0266         const QHash<QString, QVariant> settings = expected->settings();
0267         QHash<QString, QVariant>::const_iterator itpoint = settings.begin();
0268         QHash<QString, QVariant>::const_iterator const endpoint = settings.end();
0269         for (; itpoint != endpoint; ++itpoint ) {
0270             const QString testName = QString( "%1 %2" ).arg( plugin->nameId() ).arg( itpoint.key() );
0271             QTest::newRow(testName.toLatin1().constData())
0272                 << result->settings().value(itpoint.key())
0273                 << expected->settings().value(itpoint.key());
0274         }
0275     }
0276 }
0277 
0278 void RenderPluginTest::setSettingsAllEqual()
0279 {
0280     QFETCH( QVariant, result );
0281     QFETCH( QVariant, expected );
0282 
0283     QCOMPARE( result, expected );
0284 }
0285 
0286 void RenderPluginTest::restoreDefaultSettings_data()
0287 {
0288     QTest::addColumn<QVariant>( "result" );
0289     QTest::addColumn<QVariant>( "expected" );
0290 
0291     foreach ( const RenderPlugin *plugin, m_model.pluginManager()->renderPlugins() ) {
0292         RenderPlugin *const result = plugin->newInstance( &m_model );
0293         result->initialize();
0294 
0295         RenderPlugin *const expected = plugin->newInstance( &m_model );
0296         expected->initialize();
0297         expected->restoreDefaultSettings();
0298 
0299         QHash<QString, QVariant> const & settings = expected->settings();
0300         QHash<QString, QVariant>::const_iterator itpoint = settings.begin();
0301         QHash<QString, QVariant>::const_iterator const endpoint = settings.end();
0302         for (; itpoint != endpoint; ++itpoint ) {
0303             const QString testName = QString( "%1 %2" ).arg( plugin->nameId() ).arg( itpoint.key() );
0304             QTest::newRow(testName.toLatin1().constData())
0305                 << result->settings().value(itpoint.key())
0306                 << expected->settings().value(itpoint.key());
0307         }
0308     }
0309 }
0310 
0311 void RenderPluginTest::restoreDefaultSettings()
0312 {
0313     QFETCH( QVariant, result );
0314     QFETCH( QVariant, expected );
0315 
0316     QCOMPARE( result, expected );
0317 }
0318 
0319 }
0320 
0321 QTEST_MAIN( Marble::RenderPluginTest )
0322 
0323 #include "RenderPluginTest.moc"