Changes: added ut_gpscontroller
[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_useFakeLocation ) {
92     delete m_currentLocation;
93     m_currentLocation = new Location();
94   }
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();
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   arguments = spy.takeFirst();
174   QCOMPARE(arguments.at(0).value<Location*>(), m_subject_p->m_currentLocation);
175   QCOMPARE(m_subject_p->m_currentLocation, fakeLocation);
176
177   // should not be the gpsLocation or zero
178   QVERIFY(m_subject_p->m_currentLocation != gpsLocation);
179   QVERIFY(m_subject_p->m_currentLocation != 0);
180
181   // switch back to GPS
182   m_subject->useLiveGps();
183   m_subject->getGps();
184
185   // gps should be on
186   QCOMPARE(m_subject_p->m_gpsOn, true);
187
188   // should be zero
189   QVERIFY(m_subject_p->m_currentLocation == 0);
190
191   // should not get any signals because useFakeGps sets the location to 0
192   QVERIFY2(spy.count()==0, "should not receive any signals" );
193
194   // fake a GPS update
195   m_subject_p->updateLocation(); // pretend GPS has given an update
196
197   // get GPS location
198   m_subject->getGps();
199
200   // check effect
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);
205 }
206
207 QTEST_APPLESS_MAIN(Ut_GpsController)