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

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0004 //
0005 
0006 #include "MarbleModel.h"
0007 #include "PluginManager.h"
0008 #include "AbstractFloatItem.h"
0009 
0010 #include <QMetaType>
0011 #include <QTest>
0012 
0013 Q_DECLARE_METATYPE( const Marble::AbstractFloatItem * )
0014 
0015 using namespace Marble;
0016 
0017 class NullFloatItem : public AbstractFloatItem
0018 {
0019  public:
0020     NullFloatItem() :
0021         AbstractFloatItem( nullptr )
0022     {}
0023 
0024     NullFloatItem( const MarbleModel *model ) :
0025         AbstractFloatItem( model )
0026     {}
0027 
0028     QString name() const override { return QStringLiteral("Null Float Item"); }
0029     QString nameId() const override { return QStringLiteral("null"); }
0030     QString version() const override { return QStringLiteral("0.0"); }
0031     QString description() const override { return QStringLiteral("A null float item just for testing."); }
0032     QIcon icon() const override { return QIcon(); }
0033     QString copyrightYears() const override { return QStringLiteral("2013"); }
0034     QVector<PluginAuthor> pluginAuthors() const override
0035     {
0036         return QVector<PluginAuthor>() << PluginAuthor(QStringLiteral("Bernhard Beschow"), QStringLiteral("bbeschow@cs.tu-berlin.de"));
0037     }
0038     void initialize() override {}
0039     bool isInitialized() const override { return true; }
0040     QStringList backendTypes() const override { return QStringList(QStringLiteral("null")); }
0041     QString guiString() const override { return QStringLiteral("Null"); }
0042     RenderPlugin *newInstance( const MarbleModel * ) const override { return nullptr; }
0043 };
0044 
0045 class AbstractFloatItemTest : public QObject
0046 {
0047     Q_OBJECT
0048 
0049  public:
0050     AbstractFloatItemTest();
0051 
0052  private Q_SLOTS:
0053     void defaultConstructor();
0054 
0055     void newInstance_data();
0056     void newInstance();
0057 
0058     void setSettings_data();
0059     void setSettings();
0060 
0061     void setPosition_data();
0062     void setPosition();
0063 
0064  private:
0065     MarbleModel m_model;
0066     QList<const AbstractFloatItem *> m_factories;
0067 };
0068 
0069 AbstractFloatItemTest::AbstractFloatItemTest()
0070 {
0071     foreach ( const RenderPlugin *plugin, m_model.pluginManager()->renderPlugins() ) {
0072         const AbstractFloatItem *const factory = qobject_cast<const AbstractFloatItem *>( plugin );
0073         if ( !factory )
0074             continue;
0075 
0076         m_factories << factory;
0077     }
0078 }
0079 
0080 void AbstractFloatItemTest::defaultConstructor()
0081 {
0082     const NullFloatItem item;
0083 
0084     QCOMPARE( item.renderPolicy(), QString( "ALWAYS") );
0085     QCOMPARE( item.renderPosition(), QStringList( "FLOAT_ITEM" ) );
0086     QCOMPARE( item.cacheMode(), AbstractFloatItem::ItemCoordinateCache );
0087     QCOMPARE( item.frame(), AbstractFloatItem::RectFrame );
0088     QCOMPARE( item.padding(), 4. );
0089     QCOMPARE( item.contentSize(), QSizeF( 150, 50 ) );
0090     QCOMPARE( item.position(), QPointF( 10, 10 ) );
0091     QCOMPARE( item.visible(), true );
0092     QCOMPARE( item.positionLocked(), true );
0093     QVERIFY( item.settings().contains( "position" ) );
0094 }
0095 
0096 void AbstractFloatItemTest::newInstance_data()
0097 {
0098     QTest::addColumn<const AbstractFloatItem *>( "factory" );
0099 
0100     foreach ( const AbstractFloatItem *factory, m_factories ) {
0101         QTest::newRow(factory->nameId().toLatin1().constData())
0102             << factory;
0103     }
0104 }
0105 
0106 void AbstractFloatItemTest::newInstance()
0107 {
0108     QFETCH( const AbstractFloatItem *, factory );
0109 
0110     RenderPlugin *const instance = factory->newInstance( &m_model );
0111 
0112     QVERIFY( qobject_cast<AbstractFloatItem *>( instance ) != 0 );
0113     QVERIFY( instance->settings().contains( "position" ) );
0114 
0115     delete instance;
0116 }
0117 
0118 void AbstractFloatItemTest::setSettings_data()
0119 {
0120     QTest::addColumn<const AbstractFloatItem *>( "factory" );
0121 
0122     foreach ( const AbstractFloatItem *factory, m_factories ) {
0123         QTest::newRow(factory->nameId().toLatin1().constData())
0124             << factory;
0125     }
0126 }
0127 
0128 void AbstractFloatItemTest::setSettings()
0129 {
0130     QFETCH( const AbstractFloatItem *, factory );
0131 
0132     AbstractFloatItem *const instance = qobject_cast<AbstractFloatItem *>( factory->newInstance( &m_model ) );
0133 
0134     QVERIFY( instance != nullptr );
0135 
0136     const QPointF position( 1.318, 4.005 );
0137 
0138     instance->restoreDefaultSettings();
0139 
0140     QVERIFY( instance->settings().value( "position", position ).toPointF() != position );
0141     QVERIFY( instance->position() != position );
0142 
0143     QHash<QString, QVariant> settings;
0144     settings.insert( "position", position );
0145 
0146     instance->setSettings( settings );
0147 
0148     QCOMPARE( instance->settings().value( "position", QPointF( 0, 0 ) ).toPointF(), position );
0149     QCOMPARE( instance->position(), position );
0150 
0151     delete instance;
0152 }
0153 
0154 void AbstractFloatItemTest::setPosition_data()
0155 {
0156     QTest::addColumn<const AbstractFloatItem *>( "factory" );
0157 
0158     foreach ( const AbstractFloatItem *factory, m_factories ) {
0159         QTest::newRow(factory->nameId().toLatin1().constData())
0160             << factory;
0161     }
0162 }
0163 
0164 void AbstractFloatItemTest::setPosition()
0165 {
0166     QFETCH( const AbstractFloatItem *, factory );
0167 
0168     AbstractFloatItem *const instance = qobject_cast<AbstractFloatItem *>( factory->newInstance( &m_model ) );
0169 
0170     QVERIFY( instance != nullptr );
0171 
0172     const QPointF position( 1.318, 4.005 );
0173 
0174     QVERIFY( instance->position() != position );
0175     QVERIFY( instance->settings().value( "position", position ).toPointF() != position );
0176 
0177     QHash<QString, QVariant> settings;
0178     settings.insert( "position", QPointF( 0, 0 ) );
0179 
0180     instance->setSettings( settings );
0181     instance->setPosition( position ); // position of settings should be overwritten
0182 
0183     QCOMPARE( instance->position(), position );
0184     QCOMPARE( instance->settings().value( "position", QPointF( 1, 0 ) ).toPointF(), position );
0185 
0186     delete instance;
0187 }
0188 
0189 QTEST_MAIN( AbstractFloatItemTest )
0190 
0191 #include "AbstractFloatItemTest.moc"