Moved files to make zouba the only project.
[ptas] / 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 setFakeLocationLabel( const QString &label );
20   void setUseFakeLocation( bool useFake );
21   void updateLocation();
22
23   QGeoPositionInfoSource *gps();
24   Location               *liveLocation();
25   Location               *fakeLocation();
26   bool                    useFakeLocation();
27
28   bool                    m_gpsOn;
29   Location               *m_liveLocation;
30   Location               *m_fakeLocation;
31   QString                 m_fakeLocationLabel;
32   bool                    m_useFakeLocation;
33 };
34
35 MyGpsControllerPrivate::MyGpsControllerPrivate() :
36   m_gpsOn(false),
37   m_liveLocation( new Location( "livegps" ) ),
38   m_fakeLocation( new Location( "fakegps" ) ),
39   m_fakeLocationLabel(),
40   m_useFakeLocation(false)
41 {
42 }
43
44 MyGpsControllerPrivate::~MyGpsControllerPrivate()
45 {
46   delete m_liveLocation;
47   m_liveLocation = 0;
48   delete m_fakeLocation;
49   m_fakeLocation = 0;
50 }
51
52 void MyGpsControllerPrivate::init()
53 {
54 }
55
56 void MyGpsControllerPrivate::startGps()
57 {
58   m_gpsOn=true;
59 }
60
61 void MyGpsControllerPrivate::stopGps()
62 {
63   m_gpsOn=false;
64 }
65
66 QGeoPositionInfoSource *MyGpsControllerPrivate::gps()
67 {
68   return 0;
69 }
70
71 void MyGpsControllerPrivate::setGps( QGeoPositionInfoSource *gps )
72 {
73   Q_UNUSED( gps );
74 }
75
76 Location *MyGpsControllerPrivate::liveLocation()
77 {
78   return m_liveLocation;
79 }
80
81 Location *MyGpsControllerPrivate::fakeLocation()
82 {
83   return m_fakeLocation;
84 }
85
86 void MyGpsControllerPrivate::setFakeLocationLabel( const QString &label )
87 {
88   m_fakeLocationLabel = label;
89   m_fakeLocation->setLabel( label );
90 }
91
92 bool MyGpsControllerPrivate::useFakeLocation()
93 {
94   return m_useFakeLocation;
95 }
96
97 void MyGpsControllerPrivate::setUseFakeLocation( bool useFake )
98 {
99   m_useFakeLocation = useFake;
100 }
101
102 void MyGpsControllerPrivate::updateLocation()
103 {
104 }
105
106 void Ut_GpsController::init()
107 {
108   qRegisterMetaType<Location *>( "Location*" );
109
110   m_subject_p = new MyGpsControllerPrivate();
111   m_subject = new GpsController( m_subject_p ); // private ownership transferred
112 }
113
114 void Ut_GpsController::cleanup()
115 {
116     delete m_subject;
117     m_subject = 0;
118 }
119
120 void Ut_GpsController::initTestCase()
121 {
122 }
123
124 void Ut_GpsController::cleanupTestCase()
125 {
126 }
127
128 void Ut_GpsController::testGetGpsWithNoGpsUpdates()
129 {
130   QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
131
132   // this should start the gps,
133   // one signal to invalidate the previous display
134   // (which could be showing fake results, for example
135   m_subject->getGps();
136
137   QCOMPARE(m_subject_p->m_gpsOn, true);
138   QCOMPARE(spy.count(), 1);
139 }
140
141 void Ut_GpsController::testGetGpsWithGpsUpdates()
142 {
143   QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
144
145   // make test call
146   m_subject->getGps();
147
148   // check effect
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);
153 }
154
155 void Ut_GpsController::testFakeGps()
156 {
157   QSignalSpy spy(m_subject, SIGNAL(locationChanged(Location*)));
158
159   Location *gpsLocation = m_subject_p->m_liveLocation; // position from GPS
160
161   // make test call
162   m_subject->useFakeGps( "fakegps" );
163   m_subject->getGps();
164
165   // check effect
166   QList<QVariant> arguments;
167
168   // gps should be off
169   QCOMPARE(m_subject_p->m_gpsOn, false);
170
171   // should get two signals, one from useFakeGps() and one from getGps()
172   QVERIFY2(spy.count()==2, "Should receive two signals" );
173
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" ) );
181
182   // switch back to GPS
183   m_subject->useLiveGps();
184   m_subject->getGps();
185
186   // gps should be on
187   QCOMPARE(m_subject_p->m_gpsOn, true);
188
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" ) );
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_liveLocation);
204   QCOMPARE( arguments.at(0).value<Location*>()->label(), QString( "livegps" ) );
205 }
206
207 void Ut_GpsController::testLiveToFakeToLive()
208 {
209   m_subject_p->updateLocation(); // pretend GPS has given an update
210
211   m_subject->useFakeGps( "fakegps" );
212   m_subject->getGps();
213
214   // switch back to live GPS
215   m_subject->useLiveGps();
216   m_subject->getGps();
217
218   m_subject->useFakeGps( "fakegps" );
219   m_subject->getGps();
220 }
221
222 QTEST_APPLESS_MAIN(Ut_GpsController)