3 #include "ut_gpscontroller.h"
5 #include "gpscontroller.h"
6 #include "gpscontroller_p.h"
8 class MyGpsControllerPrivate: public GpsControllerPrivate
11 MyGpsControllerPrivate();
12 ~MyGpsControllerPrivate();
18 void setGps( QGeoPositionInfoSource *gps );
19 void setFakeLocationLabel( const QString &label );
20 void setUseFakeLocation( bool useFake );
21 void updateLocation();
23 QGeoPositionInfoSource *gps();
24 Location *liveLocation();
25 Location *fakeLocation();
26 bool useFakeLocation();
29 Location *m_liveLocation;
30 Location *m_fakeLocation;
31 QString m_fakeLocationLabel;
32 bool m_useFakeLocation;
35 MyGpsControllerPrivate::MyGpsControllerPrivate() :
37 m_liveLocation( new Location( "livegps" ) ),
38 m_fakeLocation( new Location( "fakegps" ) ),
39 m_fakeLocationLabel(),
40 m_useFakeLocation(false)
44 MyGpsControllerPrivate::~MyGpsControllerPrivate()
46 delete m_liveLocation;
48 delete m_fakeLocation;
52 void MyGpsControllerPrivate::init()
56 void MyGpsControllerPrivate::startGps()
61 void MyGpsControllerPrivate::stopGps()
66 QGeoPositionInfoSource *MyGpsControllerPrivate::gps()
71 void MyGpsControllerPrivate::setGps( QGeoPositionInfoSource *gps )
76 Location *MyGpsControllerPrivate::liveLocation()
78 return m_liveLocation;
81 Location *MyGpsControllerPrivate::fakeLocation()
83 return m_fakeLocation;
86 void MyGpsControllerPrivate::setFakeLocationLabel( const QString &label )
88 m_fakeLocationLabel = label;
89 m_fakeLocation->setLabel( label );
92 bool MyGpsControllerPrivate::useFakeLocation()
94 return m_useFakeLocation;
97 void MyGpsControllerPrivate::setUseFakeLocation( bool useFake )
99 m_useFakeLocation = useFake;
102 void MyGpsControllerPrivate::updateLocation()
106 void Ut_GpsController::init()
108 qRegisterMetaType<Location *>( "Location*" );
110 m_subject_p = new MyGpsControllerPrivate();
111 m_subject = new GpsController( m_subject_p ); // private ownership transferred
114 void Ut_GpsController::cleanup()
120 void Ut_GpsController::initTestCase()
124 void Ut_GpsController::cleanupTestCase()
128 void Ut_GpsController::testGetGpsWithNoGpsUpdates()
130 QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
132 // this should start the gps,
133 // one signal to invalidate the previous display
134 // (which could be showing fake results, for example
137 QCOMPARE(m_subject_p->m_gpsOn, true);
138 QCOMPARE(spy.count(), 1);
141 void Ut_GpsController::testGetGpsWithGpsUpdates()
143 QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
149 QCOMPARE(m_subject_p->m_gpsOn, true);
150 QCOMPARE(spy.count(), 1);
151 QList<QVariant> arguments = spy.takeFirst();
152 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_liveLocation);
155 void Ut_GpsController::testFakeGps()
157 QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
159 Location *gpsLocation = m_subject_p->m_liveLocation; // position from GPS
162 m_subject->useFakeGps( "fakegps" );
166 QList<QVariant> arguments;
169 QCOMPARE(m_subject_p->m_gpsOn, false);
171 // should get two signals, one from useFakeGps() and one from getGps()
172 QVERIFY2(spy.count()==2, "Should receive two signals" );
174 // both args should be the fake gps position supplied to useFakeGps()
175 arguments = spy.takeFirst();
176 QCOMPARE( arguments.at(0).value<Location*>(), m_subject_p->m_fakeLocation );
177 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "fakegps" ) );
178 arguments = spy.takeFirst();
179 QCOMPARE( arguments.at(0).value<Location*>(), m_subject_p->m_fakeLocation );
180 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "fakegps" ) );
182 // switch back to GPS
183 m_subject->useLiveGps();
187 QCOMPARE(m_subject_p->m_gpsOn, true);
189 QVERIFY2(spy.count()==2, "should get two locationChanged signals" );
190 arguments = spy.takeFirst();
191 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_liveLocation);
192 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "livegps" ) );
193 arguments = spy.takeFirst();
194 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_liveLocation);
195 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "livegps" ) );
201 QCOMPARE(spy.count(), 1);
202 arguments = spy.takeFirst();
203 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_liveLocation);
204 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "livegps" ) );
207 void Ut_GpsController::testLiveToFakeToLive()
209 m_subject_p->updateLocation(); // pretend GPS has given an update
211 m_subject->useFakeGps( "fakegps" );
214 // switch back to live GPS
215 m_subject->useLiveGps();
218 m_subject->useFakeGps( "fakegps" );
222 QTEST_APPLESS_MAIN(Ut_GpsController)