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 #include "GeoDataTrack.h"
0007 #include "GeoDataPlacemark.h"
0008 #include "GeoDataAccuracy.h"
0009 #include "PositionProviderPlugin.h"
0010 #include "PluginManager.h"
0011 #include "MarbleModel.h"
0012 #include "TestUtils.h"
0013 
0014 #include <QSignalSpy>
0015 
0016 Q_DECLARE_METATYPE( Marble::GeoDataPlacemark * )
0017 
0018 namespace Marble
0019 {
0020 
0021 class PlacemarkPositionProviderPluginTest : public QObject
0022 {
0023     Q_OBJECT
0024 
0025  public:
0026     PlacemarkPositionProviderPluginTest();
0027 
0028     static PositionProviderPlugin *createUninitializedPlugin( const PluginManager *pluginManager );
0029     static PositionProviderPlugin *createInitializedPlugin( const MarbleModel *model );
0030 
0031  private Q_SLOTS:
0032     void initialize_data();
0033     void initialize();
0034 
0035     void setTrackedPlacemark_afterInitialize_data();
0036     void setTrackedPlacemark_afterInitialize();
0037 
0038     void setClockDateTime();
0039 
0040  private:
0041     const QDateTime m_minTime;
0042     const QDateTime m_maxTime;
0043     GeoDataPlacemark m_placemark1;
0044     GeoDataPlacemark m_placemark2;
0045 };
0046 
0047 PlacemarkPositionProviderPluginTest::PlacemarkPositionProviderPluginTest() :
0048     m_minTime( QDate( 2000, 1, 1 ), QTime( 0, 0 ) ),
0049     m_maxTime( QDate( 2000, 1, 2 ), QTime( 23, 59 ) )
0050 {
0051     qRegisterMetaType<GeoDataCoordinates>( "GeoDataCoordinates" );
0052     qRegisterMetaType<GeoDataAccuracy>( "GeoDataAccuracy" );
0053     qRegisterMetaType<PositionProviderStatus>( "PositionProviderStatus" );
0054 
0055     GeoDataTrack *track = new GeoDataTrack;
0056     track->setInterpolate( true );
0057     track->addPoint( m_minTime, GeoDataCoordinates() );
0058     track->addPoint( m_maxTime, GeoDataCoordinates() );
0059     m_placemark1.setGeometry( track );
0060 
0061     m_placemark2 = m_placemark1;
0062 }
0063 
0064 PositionProviderPlugin *PlacemarkPositionProviderPluginTest::createUninitializedPlugin( const PluginManager *pluginManager )
0065 {
0066     foreach ( const PositionProviderPlugin *plugin, pluginManager->positionProviderPlugins() ) {
0067         if (plugin->nameId() == QLatin1String("Placemark")) {
0068             PositionProviderPlugin *instance = plugin->newInstance();
0069             return instance;
0070         }
0071     }
0072 
0073     return nullptr;
0074 }
0075 
0076 PositionProviderPlugin *PlacemarkPositionProviderPluginTest::createInitializedPlugin( const MarbleModel *model )
0077 {
0078     PositionProviderPlugin *const plugin = createUninitializedPlugin( model->pluginManager() );
0079 
0080     if ( plugin == nullptr ) {
0081         return nullptr;
0082     }
0083 
0084     plugin->initialize();
0085 
0086     return plugin;
0087 }
0088 
0089 void PlacemarkPositionProviderPluginTest::initialize_data()
0090 {
0091     QTest::addColumn<GeoDataPlacemark *>( "placemark" );
0092 
0093     GeoDataPlacemark *const nullPlacemark = nullptr;
0094 
0095     addRow() << nullPlacemark;
0096     addRow() << &m_placemark1;
0097 }
0098 
0099 void PlacemarkPositionProviderPluginTest::initialize()
0100 {
0101     QFETCH( GeoDataPlacemark *, placemark );
0102     const PositionProviderStatus expectedStatus = placemark ? PositionProviderStatusAvailable : PositionProviderStatusUnavailable;
0103     const int expectedStatusCount = placemark ? 1 : 0;
0104     const int expectedPositionCount = placemark ? 1 : 0;
0105 
0106     MarbleModel model;
0107     model.setClockDateTime( m_minTime ); // FIXME crashes when this line is removed
0108 
0109     model.setTrackedPlacemark( placemark );
0110     QVERIFY( model.trackedPlacemark() == placemark );
0111 
0112     PositionProviderPlugin *const plugin = createUninitializedPlugin( model.pluginManager() );
0113     QVERIFY2( plugin != nullptr, "Need a PlacemarkPositionProviderPlugin!" );
0114 
0115     QCOMPARE( plugin->status(), PositionProviderStatusUnavailable );
0116 
0117     QSignalSpy statusChangedSpy( plugin, SIGNAL(statusChanged(PositionProviderStatus)) );
0118     QSignalSpy positionChangedSpy( plugin, SIGNAL(positionChanged(GeoDataCoordinates,GeoDataAccuracy)) );
0119 
0120     plugin->initialize();
0121 
0122     QCOMPARE( plugin->status(), expectedStatus );
0123     QCOMPARE( statusChangedSpy.count(), expectedStatusCount );
0124     QCOMPARE( positionChangedSpy.count(), expectedPositionCount );
0125 
0126     delete plugin;
0127 }
0128 
0129 void PlacemarkPositionProviderPluginTest::setTrackedPlacemark_afterInitialize_data()
0130 {
0131     QTest::addColumn<GeoDataPlacemark *>( "initialPlacemark" );
0132     QTest::addColumn<GeoDataPlacemark *>( "newPlacemark" );
0133     QTest::addColumn<int>( "expectedStatusCount" );
0134 
0135     GeoDataPlacemark *const nullPlacemark = nullptr;
0136 
0137     addRow() << nullPlacemark << nullPlacemark << 0;
0138     addRow() << nullPlacemark << &m_placemark1 << 1;
0139     addRow() << &m_placemark1 << nullPlacemark << 1;
0140     addRow() << &m_placemark1 << &m_placemark2 << 2;
0141 }
0142 
0143 void PlacemarkPositionProviderPluginTest::setTrackedPlacemark_afterInitialize()
0144 {
0145     QFETCH( GeoDataPlacemark *, initialPlacemark );
0146     QFETCH( GeoDataPlacemark *, newPlacemark );
0147     QFETCH( int, expectedStatusCount );
0148     const PositionProviderStatus expectedStatus = newPlacemark ? PositionProviderStatusAvailable : PositionProviderStatusUnavailable;
0149     const int expectedPositionCount = newPlacemark ? 1 : 0;
0150 
0151     MarbleModel model;
0152     model.setClockDateTime( m_minTime ); // FIXME crashes when this line is removed
0153 
0154     model.setTrackedPlacemark( initialPlacemark );
0155     QVERIFY( model.trackedPlacemark() == initialPlacemark );
0156 
0157     PositionProviderPlugin *const plugin = createInitializedPlugin( &model );
0158     QVERIFY2( plugin != nullptr, "Need a PlacemarkPositionProviderPlugin!" );
0159 
0160     QSignalSpy statusChangedSpy( plugin, SIGNAL(statusChanged(PositionProviderStatus)) );
0161     QSignalSpy positionChangedSpy( plugin, SIGNAL(positionChanged(GeoDataCoordinates,GeoDataAccuracy)) );
0162 
0163     model.setTrackedPlacemark( newPlacemark );
0164 
0165     QCOMPARE( plugin->status(), expectedStatus );
0166     QCOMPARE( statusChangedSpy.count(), expectedStatusCount );
0167     QCOMPARE( positionChangedSpy.count(), expectedPositionCount );
0168 
0169     delete plugin;
0170 }
0171 
0172 void PlacemarkPositionProviderPluginTest::setClockDateTime()
0173 {
0174     MarbleModel model;
0175 
0176     model.setClockDateTime( m_minTime ); // FIXME crashes when this line is removed
0177 
0178     model.setTrackedPlacemark( &m_placemark1 );
0179 
0180     PositionProviderPlugin *const plugin = createInitializedPlugin( &model );
0181     QVERIFY2( plugin != nullptr, "Need a PlacemarkPositionProviderPlugin!" );
0182 
0183     QSignalSpy positionChangedSpy( plugin, SIGNAL(positionChanged(GeoDataCoordinates,GeoDataAccuracy)) );
0184 
0185     model.setClockDateTime( m_minTime );
0186 
0187     QCOMPARE( positionChangedSpy.count(), 1 );
0188 
0189     model.setClockDateTime( m_minTime ); // FIXME crashes when using m_maxTime
0190 
0191     QCOMPARE( positionChangedSpy.count(), 2 );
0192 
0193     delete plugin;
0194 }
0195 
0196 }
0197 
0198 QTEST_MAIN( Marble::PlacemarkPositionProviderPluginTest )
0199 
0200 #include "PlacemarkPositionProviderPluginTest.moc"