bbe467c61046579180cfc67eabd9c8531ac8a185
[ptas] / zouba / tests / ut_gpscontroller / ut_gpscontroller.cpp
1 #include <QObject>
2 #include <QtDebug>
3 #include "ut_gpscontroller.h"
4
5 #include "gpscontroller.h"
6 #include "gpscontroller_p.h"
7
8 class MyGpsControllerPrivate: public GpsControllerPrivate
9 {
10 public:
11   MyGpsControllerPrivate();
12   ~MyGpsControllerPrivate();
13
14   void init();
15   void startGps();
16   void stopGps();
17
18   void setGps( QGeoPositionInfoSource *gps );
19   void setCurrentLocation( Location *location );
20   void setUseFakeLocation( bool useFake );
21   void updateLocation();
22
23   QGeoPositionInfoSource *gps();
24   Location               *currentLocation();
25   bool                    useFakeLocation();
26
27   bool                    m_gpsOn;
28   Location               *m_currentLocation;
29   bool                    m_useFakeLocation;
30 };
31
32 MyGpsControllerPrivate::MyGpsControllerPrivate() :
33   m_gpsOn(false),
34   m_currentLocation(0),
35   m_useFakeLocation(false)
36 {
37 }
38
39 MyGpsControllerPrivate::~MyGpsControllerPrivate()
40 {
41   delete m_currentLocation;
42   m_currentLocation=0;
43 }
44
45 void MyGpsControllerPrivate::init()
46 {
47 }
48
49 void MyGpsControllerPrivate::startGps()
50 {
51   m_gpsOn=true;
52 }
53
54 void MyGpsControllerPrivate::stopGps()
55 {
56   m_gpsOn=false;
57 }
58
59 QGeoPositionInfoSource *MyGpsControllerPrivate::gps()
60 {
61   return 0;
62 }
63
64 void MyGpsControllerPrivate::setGps( QGeoPositionInfoSource *gps )
65 {
66   Q_UNUSED( gps );
67 }
68
69 Location *MyGpsControllerPrivate::currentLocation()
70 {
71   return m_currentLocation;
72 }
73
74 void MyGpsControllerPrivate::setCurrentLocation( Location *location )
75 {
76   m_currentLocation = location;
77 }
78
79 bool MyGpsControllerPrivate::useFakeLocation()
80 {
81   return m_useFakeLocation;
82 }
83
84 void MyGpsControllerPrivate::setUseFakeLocation( bool useFake )
85 {
86   m_useFakeLocation = useFake;
87 }
88
89 void MyGpsControllerPrivate::updateLocation()
90 {
91   if ( m_currentLocation && m_currentLocation->label() == "livegps" ) {
92     delete m_currentLocation;
93   }
94   m_currentLocation = new Location( "livegps" );
95 }
96
97 void Ut_GpsController::init()
98 {
99   qRegisterMetaType<Location *>( "Location*" );
100
101   m_subject_p = new MyGpsControllerPrivate();
102   m_subject = new GpsController( m_subject_p ); // private ownership transferred
103 }
104
105 void Ut_GpsController::cleanup()
106 {
107     delete m_subject;
108     m_subject = 0;
109 }
110
111 void Ut_GpsController::initTestCase()
112 {
113 }
114
115 void Ut_GpsController::cleanupTestCase()
116 {
117 }
118
119 void Ut_GpsController::testGetGpsWithNoGpsUpdates()
120 {
121   QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
122
123   // this should start the gps,
124   // but since there's been no update from the GPS, there should be
125   // no signal
126   m_subject->getGps();
127
128   QCOMPARE(m_subject_p->m_gpsOn, true);
129   QCOMPARE(spy.count(), 0);
130 }
131
132 void Ut_GpsController::testGetGpsWithGpsUpdates()
133 {
134   QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
135
136   m_subject_p->updateLocation(); // pretend GPS has given an update
137
138   // make test call
139   m_subject->getGps();
140
141   // check effect
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);
147 }
148
149 void Ut_GpsController::testFakeGps()
150 {
151   QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
152
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");
156
157   // make test call
158   m_subject->useFakeGps( fakeLocation ); // ownership -> m_subject
159   m_subject->getGps();
160
161   // check effect
162   QList<QVariant> arguments;
163
164   // gps should be off
165   QCOMPARE(m_subject_p->m_gpsOn, false);
166
167   // should get two signals, one from useFakeGps() and one from getGps()
168   QVERIFY2(spy.count()==2, "Should receive two signals" );
169
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 );
178
179   // should not be the gpsLocation or zero
180   QVERIFY(m_subject_p->m_currentLocation != gpsLocation);
181   QVERIFY(m_subject_p->m_currentLocation != 0);
182
183   // switch back to GPS
184   m_subject->useLiveGps();
185   m_subject->getGps();
186
187   // gps should be on
188   QCOMPARE(m_subject_p->m_gpsOn, true);
189
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);
195
196   // fake a GPS update
197   m_subject_p->updateLocation(); // pretend GPS has given an update
198
199   // get GPS location
200   m_subject->getGps();
201
202   // check effect
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);
208 }
209
210 void Ut_GpsController::testLiveToFakeToLive()
211 {
212   m_subject_p->updateLocation(); // pretend GPS has given an update
213   Location *fakeLocation = new Location();
214
215   m_subject->useFakeGps( fakeLocation ); // ownership -> m_subject
216   m_subject->getGps();
217
218   // switch back to live GPS
219   m_subject->useLiveGps();
220   m_subject->getGps();
221
222   // fake a GPS update
223   m_subject_p->updateLocation(); // pretend GPS has given an update
224
225   // get GPS location
226   m_subject->getGps();
227
228   m_subject->useFakeGps( fakeLocation ); // ownership -> m_subject
229   m_subject->getGps();
230
231   // fake a GPS update
232   m_subject_p->updateLocation(); // pretend GPS has given an update
233
234   // get GPS location
235   m_subject->getGps();
236 }
237
238 QTEST_APPLESS_MAIN(Ut_GpsController)