summaryrefslogtreecommitdiffstats
path: root/tests/auto/qgeosatelliteinfosource/testqgeosatelliteinfosource.cpp
blob: 343a0b0730dccf8bdb290de94b77ff9d46f5e45d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
/*********************f*******************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//TESTED_COMPONENT=src/location

#include <QTest>
#include <QMetaType>
#include <QSignalSpy>
#include <QDebug>
#include <QTimer>

#include <qgeosatelliteinfosource.h>
#include <qgeosatelliteinfo.h>

#include "testqgeosatelliteinfosource_p.h"
#include "../qgeocoordinate/qlocationtestutils_p.h"
#ifdef TST_GYPSYMOCK_ENABLED
#include "qgeosatelliteinfosource_gypsy_p.h"
#include <QProcessEnvironment>
#include "../gypsymock/gypsymock.h"
#endif

Q_DECLARE_METATYPE(QList<QGeoSatelliteInfo>)

#define MAX_WAITING_TIME 50000

// Must provide a valid source, unless testing the source
// returned by QGeoSatelliteInfoSource::createDefaultSource() on a system
// that has no default source
#define CHECK_SOURCE_VALID { \
        if (!m_source) { \
            if (m_testingDefaultSource && QGeoSatelliteInfoSource::createDefaultSource(0) == 0) \
                QSKIP("No default satellite source on this system", SkipAll); \
            else \
                QFAIL("createTestSource() must return a valid source!"); \
        } \
    }

class MySatelliteSource : public QGeoSatelliteInfoSource
{
    Q_OBJECT
public:
    MySatelliteSource(QObject *parent = 0)
            : QGeoSatelliteInfoSource(parent) {
    }
    virtual void startUpdates() {}
    virtual void stopUpdates() {}
    virtual void requestUpdate(int) {}
};

#ifdef TST_GYPSYMOCK_ENABLED
// Mocked platform calls.
class TestSatelliteGypsyEngine : public SatelliteGypsyEngine
{
public:

    static bool gypsy_fix_status_available;
    static void set_gypsy_fix_status_available() {gypsy_fix_status_available = true;}
    static bool gconf_client_get_default_fail;
    static void set_fail_gconf_client_get_default() {gconf_client_get_default_fail = true;}
    static bool gconf_client_get_string_fail;
    static void set_fail_gconf_client_get_string() {gconf_client_get_string_fail = true;}
    static bool gypsy_control_get_default_fail;
    static void set_fail_gypsy_control_get_default() {gypsy_control_get_default_fail = true;}
    static bool gypsy_control_create_fail;
    static void set_fail_gypsy_control_create() {gypsy_control_create_fail = true;}
    static bool gypsy_devivce_new_fail;
    static void set_fail_gypsy_device_new() {gypsy_devivce_new_fail = true;}
    static bool gypsy_satellite_new_fail;
    static void set_fail_gypsy_satellite_new() {gypsy_satellite_new_fail = true;}
    static bool gypsy_device_start_fail;
    static void set_fail_gypsy_device_start() {gypsy_device_start_fail = true;}

    TestSatelliteGypsyEngine(QGeoSatelliteInfoSource *parent = 0):
        SatelliteGypsyEngine(parent), m_gypsymock(0)
    {
    }
    ~TestSatelliteGypsyEngine()
    {
        m_thread.quit();
        m_thread.wait();
        if (m_gypsymock) {
            delete m_gypsymock;
            m_gypsymock = 0;
        }
    }

    gulong eng_g_signal_connect(gpointer instance,
                                    const gchar* detailed_signal,
                                    GCallback c_handler,
                                    gpointer data)
    {
        Q_UNUSED(instance);
        //TRACE1("============= blocking call to connectSignal");
        QMetaObject::invokeMethod(m_gypsymock,
                                  "connectSignal",
                                  Qt::BlockingQueuedConnection,
                                  //Qt::QueuedConnection,
                                  Q_ARG(int, (int)detailed_signal),
                                  Q_ARG(int, (int)c_handler),
                                  Q_ARG(int, (int)data));
        //TRACE1("============= nonblocking call to connectSignal DONE");
        return 1; // dummy
    }
    guint eng_g_signal_handlers_disconnect_by_func (gpointer instance,
                                                        gpointer func,
                                                        gpointer data)
    {
        Q_UNUSED(instance);
        //TRACE1("============= blocking call to disconnectSignal");
        QMetaObject::invokeMethod(m_gypsymock,
                                  "disconnectSignal",
                                  //Qt::BlockingQueuedConnection,
                                  Qt::QueuedConnection,
                                  Q_ARG(int, (int)func),
                                  Q_ARG(int, (int)data));
        //TRACE1("============= blocking call to disconnectSignal DONE");
        return 1; // dummy
    }

    void eng_g_free(gpointer mem)
    {
        Q_UNUSED(mem);
        // todo crashes
        //free((char*)mem);
    }

    GypsyControl* eng_gypsy_control_get_default (void)
    {
        if (!gypsy_control_get_default_fail) {
            return (GypsyControl*)g_object_new (G_TYPE_OBJECT, NULL); // dummy
        } else {
            gypsy_control_get_default_fail = false;
            return NULL;
        }
    }
    char *eng_gypsy_control_create (GypsyControl *control, const char* device_name, GError **error)
    {
        // use root '/' here because it usually exists (sat source makes
        // existense checks)
        if (gypsy_control_create_fail || !control || QString::compare(QString::fromAscii(device_name), "/")) {
            if (error) {
                *error = g_error_new ((GQuark)1, // dummy
                                      2,         // dummy
                                      "=mock= No control or wrong device_name.");
            }
            gypsy_control_create_fail = false;
            return NULL;
        }
        return (char*)"/my/little/gypsycontrol";
    }

    GypsyDevice *eng_gypsy_device_new (const char* object_path)
    {
        // Create new mockend.
        if (gypsy_devivce_new_fail || !object_path || QString::compare(QString::fromAscii(object_path), "/my/little/gypsycontrol")) {
            TRACE1("No correctobject path given or mockend already existed.");
            gypsy_devivce_new_fail = false;
            return NULL;
        }

        if (m_gypsymock)
            delete m_gypsymock;
        m_gypsymock = new GypsyMock;
        m_gypsymock->moveToThread(&m_thread);
        m_thread.start();
        return (GypsyDevice*)g_object_new (G_TYPE_OBJECT, NULL); // dummy
    }
    GypsySatellite *eng_gypsy_satellite_new (const char *object_path)
    {
        if (gypsy_satellite_new_fail || !object_path || QString::compare(QString::fromAscii(object_path), "/my/little/gypsycontrol")) {
            gypsy_satellite_new_fail = false;
            return NULL;
        }
        return (GypsySatellite*)g_object_new (G_TYPE_OBJECT, NULL); // dummy
    }
    gboolean eng_gypsy_device_start (GypsyDevice *device, GError **error)
    {
        if (gypsy_device_start_fail || !device || !m_gypsymock) {
            if (error) {
                *error = g_error_new ((GQuark)1, // dummy
                                      2,         // dummy
                                      "=mock= No device provided or mockend not created at gypsy_device_start");
            }
            gypsy_device_start_fail = false;
            return false;
        }
        //TRACE1("============= blocking call to start");
        QMetaObject::invokeMethod(m_gypsymock,
                                  "start",
                                  Qt::BlockingQueuedConnection
                                  //Qt::QueuedConnection
                                  );
        //TRACE1("============= blocking call to start DONE");
        return true;
    }
    gboolean eng_gypsy_device_stop (GypsyDevice *device, GError **error)
    {
        if (!device || !m_gypsymock) {
            if (error) {
                *error = g_error_new ((GQuark)1, // dummy
                                      2,         // dummy
                                      "=mock= No device provided or mockend not created at gypsy_device_stop");
            }
            return false;
        }
        //TRACE1("============= blocking call to stop");
        QMetaObject::invokeMethod(m_gypsymock,
                                  "stop",
                                  Qt::BlockingQueuedConnection
                                  //Qt::QueuedConnection
                                  );
        //TRACE1("============= blocking call to DONE");
        return true;
    }
    GypsyDeviceFixStatus eng_gypsy_device_get_fix_status (GypsyDevice *device, GError **error)
    {
        if (!device) {
            if (error) {
                *error = g_error_new ((GQuark)1, // dummy
                                      2,         // dummy
                                      "=mock= No device provided for gypsy_device_get_fix_status");
            }
            return GYPSY_DEVICE_FIX_STATUS_INVALID;
        }
        if (gypsy_fix_status_available) {
            return GYPSY_DEVICE_FIX_STATUS_2D;
            gypsy_fix_status_available = false;
        } else {
            return GYPSY_DEVICE_FIX_STATUS_NONE;
        }
    }
    GPtrArray *eng_gypsy_satellite_get_satellites (GypsySatellite *satellite, GError **error)
    {  
        if (!satellite) {
            if (error) {
                *error = g_error_new ((GQuark)1, // dummy
                                      2,         // dummy
                                      "=mock= No satellite provided for gypsy_satellite_get_satellites");
            }
            return NULL;
        }
        return m_gypsymock->satelliteArray();
    }
    void eng_gypsy_satellite_free_satellite_array (GPtrArray *satellites)
    {
        for (uint i = 0; i < satellites->len; i++) {
            delete (GypsySatelliteDetails*)g_ptr_array_index(satellites, i);
        }
        g_ptr_array_free(satellites, true);
    }
    // GConf symbols (mockability due to gconf X11 requirement)
    GConfClient* eng_gconf_client_get_default(void)
    {
        if (!gconf_client_get_default_fail) {
            return (GConfClient*)g_object_new (G_TYPE_OBJECT, NULL); // dummy
        } else {
            gconf_client_get_default_fail = false;
            return NULL;
        }
    }
    gchar* eng_gconf_client_get_string(GConfClient* client, const gchar* key, GError** err)
    {
        // QString::compare return value is a bit 'unintuitive'
        TRACE1(key);
        if (gconf_client_get_string_fail || !client || QString::compare(QString::fromAscii(key),"/apps/geoclue/master/org.freedesktop.Geoclue.GPSDevice")) {
            if (err) {
                *err = g_error_new ((GQuark)1, // dummy
                                    2,       // dummy
                                    "=mock= No client or wrong key in gconf_client_get_string");
            }
            gconf_client_get_string_fail = false;
            return 0;
        }  else {
            return (gchar*)"/";
        }
    }
public:
    GypsyMock* m_gypsymock;
    QThread m_thread;
};

bool TestSatelliteGypsyEngine::gypsy_fix_status_available = false;
bool TestSatelliteGypsyEngine::gconf_client_get_default_fail = false;
bool TestSatelliteGypsyEngine::gconf_client_get_string_fail = false;
bool TestSatelliteGypsyEngine::gypsy_control_get_default_fail = false;
bool TestSatelliteGypsyEngine::gypsy_control_create_fail = false;
bool TestSatelliteGypsyEngine::gypsy_devivce_new_fail = false;
bool TestSatelliteGypsyEngine::gypsy_satellite_new_fail = false;
bool TestSatelliteGypsyEngine::gypsy_device_start_fail = false;

class TestQGeoSatelliteInfoSourceGypsy : public QGeoSatelliteInfoSourceGypsy
{
public:
    TestQGeoSatelliteInfoSourceGypsy(QObject *parent = 0) :
        QGeoSatelliteInfoSourceGypsy(parent) {}
    GypsyMock* mockEngine() {return ((TestSatelliteGypsyEngine*)m_engine)->m_gypsymock;}
protected:
    // override to mock platform calls
    void createEngine() { m_engine = new TestSatelliteGypsyEngine(this); }
};

#endif // TST_GYPSYMOCK_ENABLED

class DefaultSourceTest : public TestQGeoSatelliteInfoSource
{
    Q_OBJECT
protected:
    QGeoSatelliteInfoSource *createTestSource() {
#ifdef TST_GYPSYMOCK_ENABLED
        TestQGeoSatelliteInfoSourceGypsy *source = new TestQGeoSatelliteInfoSourceGypsy(0);
        int status = source->init();
        if (status == -1) {
            delete source;
            return 0;
        }
        return source;
#else
        return QGeoSatelliteInfoSource::createDefaultSource(0);

#endif
    }
};

TestQGeoSatelliteInfoSource::TestQGeoSatelliteInfoSource(QObject *parent)
        : QObject(parent)
{
    m_testingDefaultSource = false;
}

TestQGeoSatelliteInfoSource *TestQGeoSatelliteInfoSource::createDefaultSourceTest()
{
    DefaultSourceTest *test = new DefaultSourceTest;
    test->m_testingDefaultSource = true;
    return test;
}

void TestQGeoSatelliteInfoSource::base_initTestCase()
{
    qRegisterMetaType<QList<QGeoSatelliteInfo> >();
}

void TestQGeoSatelliteInfoSource::base_init()
{
    m_source = createTestSource();
    m_testSlot2Called = false;
}

void TestQGeoSatelliteInfoSource::base_cleanup()
{
    delete m_source;
    m_source = 0;
}

void TestQGeoSatelliteInfoSource::base_cleanupTestCase()
{
}

void TestQGeoSatelliteInfoSource::initTestCase()
{
    base_initTestCase();
}

void TestQGeoSatelliteInfoSource::init()
{
    base_init();
}

void TestQGeoSatelliteInfoSource::cleanup()
{
    base_cleanup();
}

void TestQGeoSatelliteInfoSource::cleanupTestCase()
{
    base_cleanupTestCase();
}

void TestQGeoSatelliteInfoSource::test_slot1()
{
}

void TestQGeoSatelliteInfoSource::test_slot2()
{
    m_testSlot2Called = true;
}

void TestQGeoSatelliteInfoSource::constructor_withParent()
{
    //QLocationTestUtils::uheap_mark();
    QObject *parent = new QObject();
    new MySatelliteSource(parent);
    delete parent;
    //QLocationTestUtils::uheap_mark_end();
}

void TestQGeoSatelliteInfoSource::constructor_noParent()
{
    //QLocationTestUtils::uheap_mark();
    MySatelliteSource *obj = new MySatelliteSource();
    delete obj;
    //QLocationTestUtils::uheap_mark_end();
}

void TestQGeoSatelliteInfoSource::createDefaultSource()
{
    //QLocationTestUtils::uheap_mark();
    QObject *parent = new QObject;
#ifdef TST_GYPSYMOCK_ENABLED
    QGeoSatelliteInfoSourceGypsy* source = new TestQGeoSatelliteInfoSourceGypsy(parent);
    int status = source->init();
    if (status == -1) {
        delete source;
        source = 0;
    }
#else
    QGeoSatelliteInfoSource *source = QGeoSatelliteInfoSource::createDefaultSource(parent);
#endif
#if defined(Q_OS_SYMBIAN)
    QVERIFY(source != 0);
#elif defined(Q_OS_WINCE)
    QVERIFY(source != 0);
#elif defined(Q_WS_MAEMO_5)
    QVERIFY(source != 0);
#elif defined(Q_WS_MAEMO_6)
    QVERIFY(source != 0);
#elif defined(TST_GYPSYMOCK_ENABLED)
    QVERIFY(source != 0);
#elif defined(Q_WS_MEEGO)
    Q_UNUSED(source);
    // with meego the source may or may not be present,
    // as it may be a usb/bt device
#else
    QVERIFY(source == 0);
#endif
    delete parent;
    //QLocationTestUtils::uheap_mark_end();
}

void TestQGeoSatelliteInfoSource::createDefaultSource_noParent()
{
    //QLocationTestUtils::uheap_mark();
#ifdef TST_GYPSYMOCK_ENABLED
    QGeoSatelliteInfoSourceGypsy *source = new TestQGeoSatelliteInfoSourceGypsy(0);
    int status = source->init();
    if (status == -1) {
        delete source;
        source = 0;
    }
#else
    QGeoSatelliteInfoSource *source = QGeoSatelliteInfoSource::createDefaultSource(0);
#endif
#if defined(Q_OS_SYMBIAN)
    QVERIFY(source != 0);
#elif defined(Q_OS_WINCE)
    QVERIFY(source != 0);
#elif defined(Q_WS_MAEMO_5)
    QVERIFY(source != 0);
#elif defined(Q_WS_MAEMO_6)
    QVERIFY(source != 0);
#elif defined(TST_GYPSYMOCK_ENABLED)
    QVERIFY(source != 0);
#elif defined(Q_WS_MEEGO)
    // with meego the source may or may not be present,
    // as it may be a usb/bt device
#else
    QVERIFY(source == 0);
#endif
    delete source;
    //QLocationTestUtils::uheap_mark_end();
}

void TestQGeoSatelliteInfoSource::startUpdates()
{

    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif
    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));

    m_source->startUpdates();

    for (int i = 0; i < 3; i++) {

        EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

        QTRY_VERIFY_WITH_TIMEOUT((spyView.count() > 0) && (spyUse.count() > 0), 12000);
        spyView.clear();
        spyUse.clear();
    }

    m_source->stopUpdates();
}

void TestQGeoSatelliteInfoSource::startUpdates_moreThanOnce()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif
    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));

    m_source->startUpdates();

    m_source->startUpdates(); // check there is no crash

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() > 0) && (spyUse.count() > 0), MAX_WAITING_TIME);

    m_source->startUpdates(); // check there is no crash

    m_source->stopUpdates();
}

void TestQGeoSatelliteInfoSource::stopUpdates()
{

    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));

    m_source->startUpdates();

    for (int i = 0; i < 2; i++) {

        EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

        QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 12000);
        spyView.clear();
        spyUse.clear();
    }

    m_source->stopUpdates();

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 0) && (spyUse.count() == 0), 12000);
}

void TestQGeoSatelliteInfoSource::stopUpdates_withoutStart()
{
    CHECK_SOURCE_VALID;

    m_source->stopUpdates(); // check there is no crash
}

void TestQGeoSatelliteInfoSource::requestUpdate()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QFETCH(int, timeout);
    QSignalSpy spy(m_source, SIGNAL(requestTimeout()));
    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    m_source->requestUpdate(timeout);
    // Geoclue may deliver update instantly if there is a satellite fix
    QTRY_VERIFY_WITH_TIMEOUT(!spy.isEmpty() || !spyView.isEmpty(), 10);
}

void TestQGeoSatelliteInfoSource::requestUpdate_data()
{
    QTest::addColumn<int>("timeout");
    QTest::newRow("less than zero") << -1;
    QTest::newRow("very small timeout") << 1;
}

void TestQGeoSatelliteInfoSource::requestUpdate_validTimeout()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyTimeout(m_source, SIGNAL(requestTimeout()));

    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT(
            (spyView.count() == 1) && (spyUse.count() == 1 && (spyTimeout.count()) == 0), 7000);
}

void TestQGeoSatelliteInfoSource::requestUpdate_defaultTimeout()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyTimeout(m_source, SIGNAL(requestTimeout()));

    m_source->requestUpdate(0);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT(
            (spyView.count() == 1) && (spyUse.count() == 1 && (spyTimeout.count()) == 0), 
            MAX_WAITING_TIME);
}

void TestQGeoSatelliteInfoSource::requestUpdate_repeatedCalls()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));

    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 7000);
    spyView.clear();
    spyUse.clear();

    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 7000);
}

void TestQGeoSatelliteInfoSource::requestUpdate_overlappingCalls()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));

    m_source->requestUpdate(7000);
    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

#if defined(Q_WS_MEEGO) && !defined(TST_GYPSYMOCK_ENABLED)
    // With Gypsy we migh actually get two updates instantly.
    // Not with mocked backend though.
    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 2) && (spyUse.count() == 2), 7000);
#else
    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 7000);
#endif
}

void TestQGeoSatelliteInfoSource::requestUpdate_overlappingCallsWithTimeout()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyTimeout(m_source,
                      SIGNAL(requestTimeout()));

    m_source->requestUpdate(0);
    m_source->requestUpdate(1);

    QTRY_COMPARE_WITH_TIMEOUT(spyTimeout.count(), 0, 7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

#if defined(Q_WS_MEEGO) && !defined(TST_GYPSYMOCK_ENABLED)
    // With Gypsy we migh actually get two updates instantly.
    // Not with mocked backend though.
    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 2) && (spyUse.count() == 2), 7000);
#else
    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 7000);
#endif
}

void TestQGeoSatelliteInfoSource::requestUpdateAfterStartUpdates()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyTimeout(m_source, SIGNAL(requestTimeout()));

    m_source->startUpdates();

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), MAX_WAITING_TIME);
    spyView.clear();
    spyUse.clear();

    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1)
                             && (spyTimeout.count() == 0), 7000);

    spyView.clear();
    spyUse.clear();

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 12000);

    m_source->stopUpdates();
}

void TestQGeoSatelliteInfoSource::requestUpdateBeforeStartUpdates()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    QSignalSpy spyView(m_source,
                       SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source,
                      SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyTimeout(m_source, SIGNAL(requestTimeout()));

    m_source->requestUpdate(7000);

    m_source->startUpdates();

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() > 0) && (spyUse.count() > 0)
                             && (spyTimeout.count() == 0), 7000);

    spyView.clear();
    spyUse.clear();

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((spyView.count() == 1) && (spyUse.count() == 1), 12000);

    m_source->stopUpdates();
}

void TestQGeoSatelliteInfoSource::removeSlotForRequestTimeout()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    bool i = connect(m_source, SIGNAL(requestTimeout()), this, SLOT(test_slot1()));
    QVERIFY(i==true);
    i = connect(m_source, SIGNAL(requestTimeout()), this, SLOT(test_slot2()));
    QVERIFY(i==true);
    i = disconnect(m_source, SIGNAL(requestTimeout()), this, SLOT(test_slot1()));
    QVERIFY(i==true);
    
    m_source->requestUpdate(-1);
    QTRY_VERIFY_WITH_TIMEOUT((m_testSlot2Called == true), 1000);
}

void TestQGeoSatelliteInfoSource::removeSlotForSatellitesInUseUpdated()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    bool i = connect(m_source, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)), this, SLOT(test_slot1()));
    QVERIFY(i == true);
    i = connect(m_source, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)), this, SLOT(test_slot2()));
    QVERIFY(i == true);
    i = disconnect(m_source, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)), this, SLOT(test_slot1()));
    QVERIFY(i == true);

    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((m_testSlot2Called == true), 7000);
}

void TestQGeoSatelliteInfoSource::removeSlotForSatellitesInViewUpdated()
{
    CHECK_SOURCE_VALID;
#if defined(Q_WS_MAEMO_6)
    QSKIP("Real GPS not suitable for autotesting, skipping the test.", SkipAll);
#endif

    bool i = connect(m_source, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)), this, SLOT(test_slot1()));
    QVERIFY(i == true);
    i = connect(m_source, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)), this, SLOT(test_slot2()));
    QVERIFY(i == true);
    i = disconnect(m_source, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)), this, SLOT(test_slot1()));
    QVERIFY(i == true);

    m_source->requestUpdate(7000);

    EXPECT_FAIL_WINCE_SEE_MOBILITY_337;

    QTRY_VERIFY_WITH_TIMEOUT((m_testSlot2Called == true), 7000);
}

#ifdef TST_GYPSYMOCK_ENABLED
void TestQGeoSatelliteInfoSource::updateValues()
{
    CHECK_SOURCE_VALID;

    // Test that correct values are received (values are from the .journal)
    QSignalSpy spyView(m_source, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    m_source->requestUpdate(1000);
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty(), 1100);
    QTRY_VERIFY_WITH_TIMEOUT(!spyUse.isEmpty(), 1100);

    QList<QGeoSatelliteInfo> viewList = spyView.first().at(0).value<QList<QGeoSatelliteInfo> >();
    QCOMPARE(viewList.count(), 6);
    QGeoSatelliteInfo viewSat = viewList.at(0);
    QCOMPARE(viewSat.prnNumber(), 13);
    QVERIFY(viewSat.hasAttribute(QGeoSatelliteInfo::Azimuth));
    QVERIFY(viewSat.hasAttribute(QGeoSatelliteInfo::Elevation));
    QVERIFY(qFuzzyCompare(viewSat.attribute(QGeoSatelliteInfo::Azimuth), 12.0));
    QVERIFY(qFuzzyCompare(viewSat.attribute(QGeoSatelliteInfo::Elevation), 1));

    QList<QGeoSatelliteInfo> useList = spyUse.first().at(0).value<QList<QGeoSatelliteInfo> >();
    QCOMPARE(useList.count(), 3);
    QGeoSatelliteInfo useSat = useList.at(1);
    QCOMPARE(useSat.prnNumber(), 33);
    QVERIFY(useSat.hasAttribute(QGeoSatelliteInfo::Azimuth));
    QVERIFY(useSat.hasAttribute(QGeoSatelliteInfo::Elevation));
    QVERIFY(qFuzzyCompare(useSat.attribute(QGeoSatelliteInfo::Azimuth), 32));
    QVERIFY(qFuzzyCompare(useSat.attribute(QGeoSatelliteInfo::Elevation), 3));
}

void TestQGeoSatelliteInfoSource::initGoneBad()
{
    // Verify failures during init are handled gracefully
    int status;
    TestQGeoSatelliteInfoSourceGypsy* bad_source;

    TestSatelliteGypsyEngine::set_fail_gconf_client_get_default();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gconf_client_get_string();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gypsy_control_get_default();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gypsy_control_create();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gypsy_device_new();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gypsy_satellite_new();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gypsy_satellite_new();
    TestSatelliteGypsyEngine::set_fail_gypsy_device_new();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

    TestSatelliteGypsyEngine::set_fail_gypsy_device_start();
    bad_source = new TestQGeoSatelliteInfoSourceGypsy(0);
    status = bad_source->init();
    QVERIFY(status == -1);
    delete bad_source;

}

void TestQGeoSatelliteInfoSource::badUpdates()
{
    CHECK_SOURCE_VALID;

    // Check that timeout is received if updates are stopped

    QSignalSpy spyView(m_source, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyUse(m_source, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)));
    QSignalSpy spyTimeout(m_source, SIGNAL(requestTimeout()));

    // Check basic updates work
    m_source->startUpdates();
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty(), 4000);
    m_source->stopUpdates();
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    m_source->requestUpdate(2000);
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty(), 2100);

    // Check timeout is sent when a requestUpdate is active
    GypsyMock* mockEngine = static_cast<TestQGeoSatelliteInfoSourceGypsy*>(m_source)->mockEngine();
    mockEngine->disableUpdates(true);
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    m_source->requestUpdate(2000);
    QTRY_VERIFY_WITH_TIMEOUT(!spyTimeout.isEmpty(), 2100);
    QVERIFY(spyView.isEmpty());

    // Check that update comes ok once enabled again
    mockEngine->disableUpdates(false);
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    m_source->requestUpdate(2000);
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty(), 2100);
    QVERIFY(spyTimeout.isEmpty());

    // Check that regular updates do not produce timeout,
    // and that it resumes automatically
    mockEngine->disableUpdates(true);
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    m_source->startUpdates();
    QTest::qWait(2000);
    QVERIFY(spyView.isEmpty() && spyUse.isEmpty() && spyTimeout.isEmpty());
    mockEngine->disableUpdates(false);
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty() && !spyUse.isEmpty() && spyTimeout.isEmpty(), 4000);

    // Stop updates while a updateRequest is active, it should not stop
    // the request
    m_source->requestUpdate(1000);
    m_source->stopUpdates();
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty(), 2100);
    QVERIFY(spyTimeout.isEmpty());
    // Wait a bit more to be sure no more updates are received
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    QTest::qWait(2000);
    QVERIFY(spyView.isEmpty());
    QVERIFY(spyTimeout.isEmpty());

    // Have requestUpdate timeout when there are active updates.
    // Timeout should not bother the regular updates.
    m_source->startUpdates();
    m_source->requestUpdate(5);
    QTRY_VERIFY_WITH_TIMEOUT(!spyTimeout.isEmpty(), 15);
    QVERIFY(spyUse.isEmpty());
    QTRY_VERIFY_WITH_TIMEOUT(!spyUse.isEmpty(), 2000);
    m_source->stopUpdates();

    // Test getting fix immediately
    TestSatelliteGypsyEngine::set_gypsy_fix_status_available();
    spyView.clear(); spyUse.clear(); spyTimeout.clear();
    m_source->requestUpdate(200);
    QTRY_VERIFY_WITH_TIMEOUT(!spyView.isEmpty(), 20);
    QVERIFY(spyTimeout.isEmpty());
}

#endif

#include "testqgeosatelliteinfosource.moc"