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 "TestUtils.h"
0007 
0008 #include "AbstractDataPluginModel.h"
0009 
0010 #include "AbstractDataPluginItem.h"
0011 #include "MarbleModel.h"
0012 #include "ViewportParams.h"
0013 
0014 #include <QTimer>
0015 #include <QSignalSpy>
0016 
0017 using namespace Marble;
0018 
0019 class TestDataPluginItem : public AbstractDataPluginItem
0020 {
0021     Q_OBJECT
0022 
0023 public:
0024     TestDataPluginItem( QObject *parent = nullptr ) :
0025         AbstractDataPluginItem( parent ),
0026         m_initialized( false )
0027     {}
0028 
0029     void setInitialized( bool initialized ) { m_initialized = initialized; }
0030 
0031     bool initialized() const override { return m_initialized; }
0032     bool operator<( const AbstractDataPluginItem *other ) const override { return this < other; }
0033 
0034 private:
0035     bool m_initialized;
0036 };
0037 
0038 class TestDataPluginModel : public AbstractDataPluginModel
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     TestDataPluginModel( const MarbleModel *marbleModel, QObject *parent = nullptr ) :
0044         AbstractDataPluginModel( "test", marbleModel, parent )
0045     {}
0046 
0047 protected:
0048     void getAdditionalItems(const GeoDataLatLonAltBox &box, qint32 number) override
0049     {
0050         Q_UNUSED( box )
0051         Q_UNUSED( number )
0052     }
0053 };
0054 
0055 class AbstractDataPluginModelTest : public QObject
0056 {
0057     Q_OBJECT
0058 
0059 public:
0060     AbstractDataPluginModelTest( QObject *parent = nullptr ) :
0061         QObject( parent )
0062     {}
0063 
0064 private Q_SLOTS:
0065     void init_testcase();
0066 
0067     void defaultConstructor();
0068 
0069     void destructor();
0070 
0071     void addItemToList_data();
0072     void addItemToList();
0073 
0074     void addItemToList_keepExisting_data();
0075     void addItemToList_keepExisting();
0076 
0077     void switchMapTheme_data();
0078     void switchMapTheme();
0079 
0080     void setFavoriteItemsOnly_data();
0081     void setFavoriteItemsOnly();
0082 
0083     void itemsVersusInitialized_data();
0084     void itemsVersusInitialized();
0085 
0086     void itemsVersusAddedAngularResolution();
0087 
0088     void itemsVersusSetSticky();
0089 
0090  private:
0091     const MarbleModel m_marbleModel;
0092     static const ViewportParams fullViewport;
0093 };
0094 
0095 const ViewportParams AbstractDataPluginModelTest::fullViewport( Equirectangular, 0, 0, 100, QSize( 230, 230 ) );
0096 
0097 void AbstractDataPluginModelTest::init_testcase()
0098 {
0099     QCOMPARE( GeoDataLatLonBox( fullViewport.viewLatLonAltBox() ), GeoDataLatLonBox( 90, -90, 180, -180, GeoDataCoordinates::Degree ) );
0100 }
0101 
0102 void AbstractDataPluginModelTest::defaultConstructor()
0103 {
0104     const TestDataPluginModel model( &m_marbleModel );
0105 
0106     QCOMPARE( model.isFavoriteItemsOnly(), false );
0107 }
0108 
0109 void AbstractDataPluginModelTest::destructor()
0110 {
0111     QPointer<AbstractDataPluginItem> item( new TestDataPluginItem );
0112     item->setId( "foo" );
0113 
0114     QEventLoop loop;
0115     connect( item, SIGNAL(destroyed()), &loop, SLOT(quit()) );
0116 
0117     {
0118         TestDataPluginModel model( &m_marbleModel );
0119         model.addItemToList( item );
0120 
0121         QVERIFY( model.itemExists( "foo" ) );
0122     }
0123 
0124     QTimer::singleShot( 5000, &loop, SLOT(quit()) ); // watchdog timer
0125     loop.exec();
0126 
0127     QVERIFY( item.isNull() );
0128 }
0129 
0130 void AbstractDataPluginModelTest::addItemToList_data()
0131 {
0132     QTest::addColumn<bool>( "initialized" );
0133 
0134     const bool isInitialized = true;
0135 
0136     addRow() << isInitialized;
0137     addRow() << !isInitialized;
0138 }
0139 
0140 void AbstractDataPluginModelTest::addItemToList()
0141 {
0142     QFETCH( bool, initialized );
0143 
0144     TestDataPluginModel model( &m_marbleModel );
0145 
0146     QVERIFY( model.isFavoriteItemsOnly() == false );
0147     QVERIFY( !model.itemExists( "foo" ) );
0148     QVERIFY( model.findItem( "foo" ) == nullptr );
0149 
0150     TestDataPluginItem *item = new TestDataPluginItem();
0151     item->setInitialized( initialized );
0152     item->setId( "foo" );
0153 
0154     QSignalSpy itemsUpdatedSpy( &model, SIGNAL(itemsUpdated()) );
0155 
0156     model.addItemToList( item );
0157 
0158     QVERIFY( model.itemExists( "foo" ) );
0159     QCOMPARE( model.findItem( "foo" ), item );
0160     QCOMPARE( itemsUpdatedSpy.count() == 1, initialized );
0161     QCOMPARE( static_cast<bool>( model.items( &fullViewport, 1 ).contains( item ) ), initialized );
0162 }
0163 
0164 void AbstractDataPluginModelTest::addItemToList_keepExisting_data()
0165 {
0166     QTest::addColumn<bool>( "itemInitialized" );
0167     QTest::addColumn<bool>( "rejectedInitialized" );
0168 
0169     const bool isInitialized = true;
0170 
0171     addRow() << isInitialized << isInitialized;
0172     addRow() << isInitialized << !isInitialized;
0173     addRow() << !isInitialized << isInitialized;
0174     addRow() << !isInitialized << !isInitialized;
0175 }
0176 
0177 void AbstractDataPluginModelTest::addItemToList_keepExisting()
0178 {
0179     QFETCH( bool, itemInitialized );
0180     QFETCH( bool, rejectedInitialized );
0181 
0182     TestDataPluginModel model( &m_marbleModel );
0183 
0184     QPointer<TestDataPluginItem> item( new TestDataPluginItem() );
0185     item->setId( "foo" );
0186     item->setInitialized( itemInitialized );
0187     model.addItemToList( item );
0188 
0189     QPointer<TestDataPluginItem> rejectedItem( new TestDataPluginItem() );
0190     rejectedItem->setId( "foo" );
0191     rejectedItem->setInitialized( rejectedInitialized );
0192 
0193     QEventLoop loop;
0194     connect( rejectedItem.data(), SIGNAL(destroyed()), &loop, SLOT(quit()) );
0195 
0196     QSignalSpy itemsUpdatedSpy( &model, SIGNAL(itemsUpdated()) );
0197 
0198     model.addItemToList( rejectedItem );
0199 
0200     QTimer::singleShot( 5000, &loop, SLOT(quit()) ); // watchdog timer
0201     loop.exec();
0202 
0203     QVERIFY( !item.isNull() );
0204     QVERIFY( rejectedItem.isNull() );
0205     QCOMPARE( model.findItem( item->id() ), item.data() );
0206     QCOMPARE( itemsUpdatedSpy.count(), 0 );
0207 }
0208 
0209 void AbstractDataPluginModelTest::switchMapTheme_data()
0210 {
0211     QTest::addColumn<QString>( "mapThemeId" );
0212     QTest::addColumn<bool>( "planetChanged" );
0213 
0214     addRow() << "earth/bluemarble/bluemarble.dgml" << false;
0215     addRow() << "moon/clementine/clementine.dgml" << true;
0216 }
0217 
0218 void AbstractDataPluginModelTest::switchMapTheme()
0219 {
0220     QFETCH( QString, mapThemeId );
0221     QFETCH( bool, planetChanged );
0222 
0223     MarbleModel marbleModel;
0224 
0225     marbleModel.setMapThemeId( "earth/openstreetmap/openstreetmap.dgml" );
0226     QCOMPARE( marbleModel.mapThemeId(), QString( "earth/openstreetmap/openstreetmap.dgml" ) );
0227 
0228     TestDataPluginModel model( &marbleModel );
0229 
0230     TestDataPluginItem *const item = new TestDataPluginItem();
0231     item->setId( "foo" );
0232     model.addItemToList( item );
0233 
0234     QCOMPARE( model.findItem( "foo" ), item );
0235 
0236     marbleModel.setMapThemeId( mapThemeId );
0237     QCOMPARE( marbleModel.mapThemeId(), mapThemeId );
0238 
0239     if ( planetChanged ) {
0240         QCOMPARE( model.findItem( "foo" ), static_cast<AbstractDataPluginItem *>( nullptr ) );
0241     }
0242     else {
0243         QCOMPARE( model.findItem( "foo" ), item );
0244     }
0245 }
0246 
0247 void AbstractDataPluginModelTest::setFavoriteItemsOnly_data()
0248 {
0249     QTest::addColumn<bool>( "itemIsFavorite" );
0250     QTest::addColumn<bool>( "favoriteItemsOnly" );
0251 
0252     addRow() << true << true;
0253     addRow() << true << false;
0254     addRow() << false << true;
0255     addRow() << false << false;
0256 }
0257 
0258 void AbstractDataPluginModelTest::setFavoriteItemsOnly()
0259 {
0260     QFETCH( bool, itemIsFavorite );
0261     QFETCH( bool, favoriteItemsOnly );
0262 
0263     TestDataPluginItem *item = new TestDataPluginItem;
0264     item->setId( "foo" );
0265     item->setInitialized( true );
0266     item->setFavorite( itemIsFavorite );
0267 
0268     TestDataPluginModel model( &m_marbleModel );
0269     model.setFavoriteItemsOnly( favoriteItemsOnly );
0270     model.addItemToList( item );
0271 
0272     QVERIFY( model.findItem( item->id() ) == item );
0273 
0274     const bool visible = !favoriteItemsOnly || itemIsFavorite;
0275 
0276     QCOMPARE( static_cast<bool>( model.items( &fullViewport, 1 ).contains( item ) ), visible );
0277 }
0278 
0279 void AbstractDataPluginModelTest::itemsVersusInitialized_data()
0280 {
0281     QTest::addColumn<bool>( "initialized" );
0282 
0283     addRow() << true;
0284     addRow() << false;
0285 }
0286 
0287 void AbstractDataPluginModelTest::itemsVersusInitialized()
0288 {
0289     QFETCH( bool, initialized );
0290 
0291     TestDataPluginItem *item = new TestDataPluginItem;
0292     item->setInitialized( initialized );
0293 
0294     TestDataPluginModel model( &m_marbleModel );
0295     model.addItemToList( item );
0296 
0297     QCOMPARE( static_cast<bool>( model.items( &fullViewport, 1 ).contains( item ) ), initialized );
0298 }
0299 
0300 void AbstractDataPluginModelTest::itemsVersusAddedAngularResolution()
0301 {
0302     const ViewportParams zoomedViewport( Equirectangular, 0, 0, 10000, QSize( 230, 230 ) );
0303 
0304     TestDataPluginItem *item = new TestDataPluginItem;
0305     item->setInitialized( true );
0306 
0307     TestDataPluginModel model( &m_marbleModel );
0308     model.addItemToList( item );
0309 
0310     QVERIFY( model.items( &zoomedViewport, 1 ).contains( item ) ); // calls setAddedAngularResolution()
0311     QVERIFY( !model.items( &fullViewport, 1 ).contains( item ) ); // addedAngularResolution() is too low
0312 }
0313 
0314 void AbstractDataPluginModelTest::itemsVersusSetSticky()
0315 {
0316     const ViewportParams zoomedViewport( Equirectangular, 0, 0, 10000, QSize( 230, 230 ) );
0317 
0318     TestDataPluginItem *item = new TestDataPluginItem;
0319     item->setInitialized( true );
0320     item->setSticky( false );
0321 
0322     TestDataPluginModel model( &m_marbleModel );
0323     model.addItemToList( item );
0324 
0325     QVERIFY( model.items( &zoomedViewport, 1 ).contains( item ) );
0326     QVERIFY( !model.items( &fullViewport, 1 ).contains( item ) );
0327 
0328     item->setSticky( true );
0329 
0330     QVERIFY( model.items( &zoomedViewport, 1 ).contains( item ) );
0331     QVERIFY( model.items( &fullViewport, 1 ).contains( item ) );
0332 
0333     item->setSticky( false );
0334 
0335     QVERIFY( model.items( &zoomedViewport, 1 ).contains( item ) );
0336     QVERIFY( !model.items( &fullViewport, 1 ).contains( item ) );
0337 }
0338 
0339 QTEST_MAIN( AbstractDataPluginModelTest )
0340 
0341 #include "AbstractDataPluginModelTest.moc"