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 "GeoDataTreeModel.h"
0008 #include "GeoDataAccuracy.h"
0009 #include "PositionProviderPlugin.h"
0010 #include "PositionTracking.h"
0011 #include "TestUtils.h"
0012 
0013 #include <QSignalSpy>
0014 
0015 class FakeProvider : public Marble::PositionProviderPlugin
0016 {
0017 public:
0018     FakeProvider() :
0019         m_status( Marble::PositionProviderStatusUnavailable ),
0020         m_position(),
0021         m_accuracy(),
0022         m_speed( 0.0 ),
0023         m_direction( 0.0 ),
0024         m_timestamp()
0025     {}
0026 
0027     QString name() const override           { return "fake plugin"; }
0028     QString guiString() const override      { return "fake"; }
0029     QString nameId() const override         { return "fakeplugin"; }
0030     QString version() const override        { return "1.0"; }
0031     QString description() const override    { return "plugin for testing"; }
0032     QIcon icon() const override             { return QIcon(); }
0033     QString copyrightYears() const override { return "2012"; }
0034     QVector<Marble::PluginAuthor> pluginAuthors() const override { return QVector<Marble::PluginAuthor>(); }
0035     void initialize() override {}
0036     bool isInitialized() const override     { return true; }
0037 
0038     Marble::PositionProviderStatus status() const override { return m_status; }
0039     Marble::GeoDataCoordinates position() const override { return m_position; }
0040     Marble::GeoDataAccuracy accuracy() const override { return m_accuracy; }
0041     qreal speed() const override { return m_speed; }
0042     qreal direction() const override { return m_direction; }
0043     QDateTime timestamp() const override { return m_timestamp; }
0044 
0045     Marble::PositionProviderPlugin *newInstance() const override { return nullptr; }
0046 
0047     void setStatus( Marble::PositionProviderStatus status );
0048     void setPosition( const Marble::GeoDataCoordinates &position,
0049                       const Marble::GeoDataAccuracy &accuracy,
0050                       qreal speed,
0051                       qreal direction,
0052                       const QDateTime &timestamp );
0053 
0054 private:
0055     Marble::PositionProviderStatus m_status;
0056     Marble::GeoDataCoordinates m_position;
0057     Marble::GeoDataAccuracy m_accuracy;
0058     qreal m_speed;
0059     qreal m_direction;
0060     QDateTime m_timestamp;
0061 };
0062 
0063 void FakeProvider::setStatus( Marble::PositionProviderStatus status )
0064 {
0065     const Marble::PositionProviderStatus oldStatus = m_status;
0066 
0067     m_status = status;
0068 
0069     if ( oldStatus != m_status ) {
0070         emit statusChanged( m_status );
0071     }
0072 }
0073 
0074 void FakeProvider::setPosition( const Marble::GeoDataCoordinates &position,
0075                                 const Marble::GeoDataAccuracy &accuracy,
0076                                 qreal speed,
0077                                 qreal direction,
0078                                 const QDateTime &timestamp )
0079 {
0080     m_position = position;
0081     m_accuracy = accuracy;
0082     m_speed = speed;
0083     m_direction = direction;
0084     m_timestamp = timestamp;
0085 
0086     emit positionChanged( m_position, m_accuracy );
0087 }
0088 
0089 namespace Marble
0090 {
0091 
0092 class PositionTrackingTest : public QObject
0093 {
0094     Q_OBJECT
0095 
0096  public:
0097     PositionTrackingTest();
0098 
0099  private Q_SLOTS:
0100     void construct();
0101 
0102     void statusChanged_data();
0103     void statusChanged();
0104 
0105     void setPositionProviderPlugin();
0106 
0107     void clearTrack();
0108 };
0109 
0110 PositionTrackingTest::PositionTrackingTest()
0111 {
0112     qRegisterMetaType<GeoDataCoordinates>( "GeoDataCoordinates" );
0113     qRegisterMetaType<PositionProviderStatus>( "PositionProviderStatus" );
0114 }
0115 
0116 void PositionTrackingTest::construct()
0117 {
0118     GeoDataTreeModel treeModel;
0119     const PositionTracking tracking( &treeModel );
0120 
0121     QCOMPARE( const_cast<PositionTracking &>( tracking ).positionProviderPlugin(), static_cast<PositionProviderPlugin *>( nullptr ) );
0122     QCOMPARE( tracking.speed(), qreal( 0 ) );
0123     QCOMPARE( tracking.direction(), qreal( 0 ) );
0124     QCOMPARE( tracking.timestamp(), QDateTime() );
0125     QCOMPARE( tracking.accuracy(), GeoDataAccuracy() );
0126     QCOMPARE( tracking.trackVisible(), true );
0127     QCOMPARE( tracking.currentLocation(), GeoDataCoordinates() );
0128     QCOMPARE( tracking.status(), PositionProviderStatusUnavailable );
0129     QCOMPARE( tracking.isTrackEmpty(), true );
0130 
0131     QCOMPARE( treeModel.rowCount(), 1 );
0132     const QModelIndex indexPositionTracking = treeModel.index( 0, 0 );
0133     QCOMPARE( treeModel.data( indexPositionTracking, Qt::DisplayRole ).toString(), QString( "Position Tracking" ) );
0134     QCOMPARE( treeModel.rowCount( indexPositionTracking ), 2 );
0135     const QModelIndex indexCurrentPosition = treeModel.index( 0, 0, indexPositionTracking );
0136     QCOMPARE( treeModel.data( indexCurrentPosition, Qt::DisplayRole ).toString(), QString( "Current Position" ) );
0137     const QModelIndex indexCurrentTrack = treeModel.index( 1, 0, indexPositionTracking );
0138     QCOMPARE( treeModel.data( indexCurrentTrack, Qt::DisplayRole ).toString(), QString( "Current Track" ) );
0139 }
0140 
0141 void PositionTrackingTest::statusChanged_data()
0142 {
0143     QTest::addColumn<PositionProviderStatus>( "finalStatus" );
0144 
0145     addRow() << PositionProviderStatusError;
0146     addRow() << PositionProviderStatusUnavailable;
0147     addRow() << PositionProviderStatusAcquiring;
0148     addRow() << PositionProviderStatusAvailable;
0149 }
0150 
0151 void PositionTrackingTest::statusChanged()
0152 {
0153     QFETCH( PositionProviderStatus, finalStatus );
0154     const int expectedStatusChangedCount = ( finalStatus == PositionProviderStatusUnavailable ) ? 0 : 1;
0155 
0156     GeoDataTreeModel treeModel;
0157     PositionTracking tracking( &treeModel );
0158 
0159     QSignalSpy statusChangedSpy( &tracking, SIGNAL(statusChanged(PositionProviderStatus)) );
0160 
0161     FakeProvider provider;
0162     provider.setStatus( finalStatus );
0163 
0164     tracking.setPositionProviderPlugin( &provider );
0165 
0166     QCOMPARE( tracking.status(), finalStatus );
0167     QCOMPARE( statusChangedSpy.count(), expectedStatusChangedCount );
0168 }
0169 
0170 void PositionTrackingTest::setPositionProviderPlugin()
0171 {
0172     const GeoDataCoordinates coordinates( 1.2, 0.9 );
0173     const GeoDataAccuracy accuracy( GeoDataAccuracy::Detailed, 10.0, 22.0 );
0174     const qreal speed = 32.8;
0175     const qreal direction = 49.7;
0176     const QDateTime timestamp( QDate( 1, 3, 1994 ) );
0177 
0178     GeoDataTreeModel treeModel;
0179     PositionTracking tracking( &treeModel );
0180 
0181     QSignalSpy gpsLocationSpy( &tracking, SIGNAL(gpsLocation(GeoDataCoordinates,qreal)) );
0182 
0183     QPointer<FakeProvider> provider( new FakeProvider );
0184     provider->setStatus( PositionProviderStatusAvailable );
0185     provider->setPosition( coordinates, accuracy, speed, direction, timestamp );
0186 
0187     tracking.setPositionProviderPlugin( provider );
0188 
0189     QCOMPARE( tracking.currentLocation(), coordinates );
0190     QCOMPARE( tracking.accuracy(), accuracy );
0191     QCOMPARE( tracking.speed(), speed );
0192     QCOMPARE( tracking.direction(), direction );
0193     QCOMPARE( tracking.timestamp(), timestamp );
0194     QCOMPARE( gpsLocationSpy.count(), 1 );
0195 
0196     tracking.setPositionProviderPlugin( nullptr );
0197 
0198     QVERIFY( provider.isNull() );
0199 }
0200 
0201 void PositionTrackingTest::clearTrack()
0202 {
0203     const GeoDataCoordinates position( 2.1, 0.8 );
0204     const GeoDataAccuracy accuracy( GeoDataAccuracy::Detailed, 10.0, 22.0 );
0205     const qreal speed = 32.8;
0206     const qreal direction = 49.7;
0207     const QDateTime timestamp( QDate( 1, 3, 1994 ) );
0208 
0209     GeoDataTreeModel treeModel;
0210     PositionTracking tracking( &treeModel );
0211 
0212     FakeProvider provider;
0213     tracking.setPositionProviderPlugin( &provider );
0214 
0215     tracking.clearTrack();
0216 
0217     QVERIFY( tracking.isTrackEmpty() );
0218 
0219     provider.setStatus( PositionProviderStatusAvailable );
0220 
0221     provider.setPosition( position, accuracy, speed, direction, timestamp );
0222 
0223     QVERIFY( !tracking.isTrackEmpty() );
0224 
0225     tracking.clearTrack();
0226 
0227     QVERIFY( tracking.isTrackEmpty() );
0228 }
0229 
0230 }
0231 
0232 QTEST_MAIN( Marble::PositionTrackingTest )
0233 
0234 #include "PositionTrackingTest.moc"