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 setCurrentLocation( Location *location );
20 void setUseFakeLocation( bool useFake );
21 void updateLocation();
23 QGeoPositionInfoSource *gps();
24 Location *currentLocation();
25 bool useFakeLocation();
28 Location *m_currentLocation;
29 bool m_useFakeLocation;
32 MyGpsControllerPrivate::MyGpsControllerPrivate() :
35 m_useFakeLocation(false)
39 MyGpsControllerPrivate::~MyGpsControllerPrivate()
41 delete m_currentLocation;
45 void MyGpsControllerPrivate::init()
49 void MyGpsControllerPrivate::startGps()
54 void MyGpsControllerPrivate::stopGps()
59 QGeoPositionInfoSource *MyGpsControllerPrivate::gps()
64 void MyGpsControllerPrivate::setGps( QGeoPositionInfoSource *gps )
69 Location *MyGpsControllerPrivate::currentLocation()
71 return m_currentLocation;
74 void MyGpsControllerPrivate::setCurrentLocation( Location *location )
76 m_currentLocation = location;
79 bool MyGpsControllerPrivate::useFakeLocation()
81 return m_useFakeLocation;
84 void MyGpsControllerPrivate::setUseFakeLocation( bool useFake )
86 m_useFakeLocation = useFake;
89 void MyGpsControllerPrivate::updateLocation()
91 if ( m_currentLocation && m_currentLocation->label() == "livegps" ) {
92 delete m_currentLocation;
94 m_currentLocation = new Location( "livegps" );
97 void Ut_GpsController::init()
99 qRegisterMetaType<Location *>( "Location*" );
101 m_subject_p = new MyGpsControllerPrivate();
102 m_subject = new GpsController( m_subject_p ); // private ownership transferred
105 void Ut_GpsController::cleanup()
111 void Ut_GpsController::initTestCase()
115 void Ut_GpsController::cleanupTestCase()
119 void Ut_GpsController::testGetGpsWithNoGpsUpdates()
121 QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
123 // this should start the gps,
124 // but since there's been no update from the GPS, there should be
128 QCOMPARE(m_subject_p->m_gpsOn, true);
129 QCOMPARE(spy.count(), 0);
132 void Ut_GpsController::testGetGpsWithGpsUpdates()
134 QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
136 m_subject_p->updateLocation(); // pretend GPS has given an update
142 QCOMPARE(m_subject_p->m_gpsOn, true);
143 QCOMPARE(spy.count(), 1);
144 QList<QVariant> arguments = spy.takeFirst();
145 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation);
146 QVERIFY(m_subject_p->m_currentLocation != 0);
149 void Ut_GpsController::testFakeGps()
151 QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
153 m_subject_p->updateLocation(); // pretend GPS has given an update
154 Location *gpsLocation = m_subject_p->m_currentLocation; // position from GPS
155 Location *fakeLocation = new Location("fakegps");
158 m_subject->useFakeGps( fakeLocation ); // ownership -> m_subject
162 QList<QVariant> arguments;
165 QCOMPARE(m_subject_p->m_gpsOn, false);
167 // should get two signals, one from useFakeGps() and one from getGps()
168 QVERIFY2(spy.count()==2, "Should receive two signals" );
170 // both args should be the fake gps position supplied to useFakeGps()
171 arguments = spy.takeFirst();
172 QCOMPARE( arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation );
173 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "fakegps" ) );
174 arguments = spy.takeFirst();
175 QCOMPARE( arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation );
176 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "fakegps" ) );
177 QCOMPARE( m_subject_p->m_currentLocation, fakeLocation );
179 // should not be the gpsLocation or zero
180 QVERIFY(m_subject_p->m_currentLocation != gpsLocation);
181 QVERIFY(m_subject_p->m_currentLocation != 0);
183 // switch back to GPS
184 m_subject->useLiveGps();
188 QCOMPARE(m_subject_p->m_gpsOn, true);
190 QVERIFY2(spy.count()==1, "should get a locationChanged signal from getGps" );
191 arguments = spy.takeFirst();
192 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation);
193 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "livegps" ) );
194 QVERIFY(m_subject_p->m_currentLocation != fakeLocation);
197 m_subject_p->updateLocation(); // pretend GPS has given an update
203 QCOMPARE(spy.count(), 1);
204 arguments = spy.takeFirst();
205 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation);
206 QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "livegps" ) );
207 QVERIFY(m_subject_p->m_currentLocation != 0);
210 void Ut_GpsController::testLiveToFakeToLive()
212 m_subject_p->updateLocation(); // pretend GPS has given an update
213 Location *fakeLocation = new Location();
215 m_subject->useFakeGps( fakeLocation ); // ownership -> m_subject
218 // switch back to live GPS
219 m_subject->useLiveGps();
223 m_subject_p->updateLocation(); // pretend GPS has given an update
228 m_subject->useFakeGps( fakeLocation ); // ownership -> m_subject
232 m_subject_p->updateLocation(); // pretend GPS has given an update
238 QTEST_APPLESS_MAIN(Ut_GpsController)