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_useFakeLocation ) {
92 delete m_currentLocation;
93 m_currentLocation = new Location();
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();
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 arguments = spy.takeFirst();
174 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation);
175 QCOMPARE(m_subject_p->m_currentLocation, fakeLocation);
177 // should not be the gpsLocation or zero
178 QVERIFY(m_subject_p->m_currentLocation != gpsLocation);
179 QVERIFY(m_subject_p->m_currentLocation != 0);
181 // switch back to GPS
182 m_subject->useLiveGps();
186 QCOMPARE(m_subject_p->m_gpsOn, true);
189 QVERIFY(m_subject_p->m_currentLocation == 0);
191 // should not get any signals because useFakeGps sets the location to 0
192 QVERIFY2(spy.count()==0, "should not receive any signals" );
195 m_subject_p->updateLocation(); // pretend GPS has given an update
201 QCOMPARE(spy.count(), 1);
202 arguments = spy.takeFirst();
203 QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation);
204 QVERIFY(m_subject_p->m_currentLocation != 0);
207 QTEST_APPLESS_MAIN(Ut_GpsController)