1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "qnmeapositioninfosourceproxyfactory.h"
30 #include "../qgeopositioninfosource/testqgeopositioninfosource_p.h"
31 #include "../utils/qlocationtestutils_p.h"
32 
33 #include <QtPositioning/qnmeapositioninfosource.h>
34 
35 #include <QTest>
36 #include <QDir>
37 #include <QDebug>
38 #include <QBuffer>
39 #include <QSignalSpy>
40 #include <QMetaType>
41 #include <QFile>
42 #include <QTemporaryFile>
43 #include <QHash>
44 #include <QTimer>
45 
46 QT_USE_NAMESPACE
Q_DECLARE_METATYPE(QNmeaPositionInfoSource::UpdateMode)47 Q_DECLARE_METATYPE(QNmeaPositionInfoSource::UpdateMode)
48 Q_DECLARE_METATYPE(QList<QDateTime>)
49 
50 class tst_QNmeaPositionInfoSource : public QObject
51 {
52     Q_OBJECT
53 
54 public:
55     enum UpdateTriggerMethod
56     {
57         StartUpdatesMethod,
58         RequestUpdatesMethod
59     };
60 
61     tst_QNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent = 0);
62 
63 private:
64     QList<QDateTime> createDateTimes(const QDateTime &dt, int count) const
65     {
66         QList<QDateTime> dateTimes;
67         int interval = 100;
68         for (int i=0; i<count; i++) {
69             dateTimes << dt.addMSecs(interval);
70             interval += 100;
71         }
72         return dateTimes;
73     }
74 
75     QList<QDateTime> createDateTimes(int count) const
76     {
77         return createDateTimes(QDateTime::currentDateTime().toUTC(), count);
78     }
79 
80 private slots:
81     void initTestCase();
82 
83     void constructor();
84 
85     void supportedPositioningMethods();
86 
87     void minimumUpdateInterval();
88 
89     void userEquivalentRangeError();
90 
91     void setUpdateInterval_delayedUpdate();
92 
93     void lastKnownPosition();
94 
95     void beginWithBufferedData();
96     void beginWithBufferedData_data();
97 
98     void startUpdates();
99     void startUpdates_data();
100 
101     void startUpdates_withTimeout();
102 
103     void startUpdates_expectLatestUpdateOnly();
104 
105     void startUpdates_waitForValidDateTime();
106     void startUpdates_waitForValidDateTime_data();
107 
108     void requestUpdate_waitForValidDateTime();
109     void requestUpdate_waitForValidDateTime_data();
110 
111     void requestUpdate();
112     void requestUpdate_after_start();
113 
114     void testWithBadNmea();
115     void testWithBadNmea_data();
116 
117 private:
118     QNmeaPositionInfoSource::UpdateMode m_mode;
119 };
120 
Q_DECLARE_METATYPE(tst_QNmeaPositionInfoSource::UpdateTriggerMethod)121 Q_DECLARE_METATYPE(tst_QNmeaPositionInfoSource::UpdateTriggerMethod)
122 
123 //---------------------------------------------------
124 
125 class Feeder : public QObject
126 {
127     Q_OBJECT
128 
129 public:
130     Feeder(QObject *parent)
131         : QObject(parent)
132     {
133     }
134 
135     void start(QNmeaPositionInfoSourceProxy *proxy)
136     {
137         m_proxy = proxy;
138         QTimer *timer = new QTimer;
139         QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
140         timer->setInterval(proxy->source()->minimumUpdateInterval()*2);
141         timer->start();
142     }
143 
144 public slots:
145     void timeout()
146     {
147         m_proxy->feedBytes(QLocationTestUtils::createRmcSentence(QDateTime::currentDateTime()).toLatin1());
148     }
149 
150 private:
151     QNmeaPositionInfoSourceProxy *m_proxy;
152 };
153 
154 //---------------------------------------------------
155 
156 
157 class UnlimitedNmeaStream : public QIODevice
158 {
159     Q_OBJECT
160 
161 public:
UnlimitedNmeaStream(QObject * parent)162     UnlimitedNmeaStream(QObject *parent) : QIODevice(parent) {}
163 
164 protected:
readData(char * data,qint64 maxSize)165     qint64 readData(char *data, qint64 maxSize)
166     {
167         QByteArray bytes = QLocationTestUtils::createRmcSentence(QDateTime::currentDateTime()).toLatin1();
168         qint64 sz = qMin(qint64(bytes.size()), maxSize);
169         memcpy(data, bytes.constData(), sz);
170         return sz;
171     }
172 
writeData(const char *,qint64)173     qint64 writeData(const char *, qint64)
174     {
175         return -1;
176     }
177 
bytesAvailable()178     qint64 bytesAvailable() const
179     {
180         return 1024 + QIODevice::bytesAvailable();
181     }
182 };
183