summaryrefslogtreecommitdiff
path: root/tests/auto/qnmeasatelliteinfosource/dummy/tst_dummynmeasatelliteinfosource.cpp
blob: fadd2d7728ca0811606b1255c0f7f13d2abb82cc (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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QTest>
#include <QSignalSpy>
#include <QtPositioning/QNmeaSatelliteInfoSource>
#include "../../utils/qnmeaproxyfactory.h"

class DummyNmeaSatelliteInfoSource : public QNmeaSatelliteInfoSource
{
    Q_OBJECT

public:
    DummyNmeaSatelliteInfoSource(QObject *parent = 0);

protected:
    QGeoSatelliteInfo::SatelliteSystem parseSatellitesInUseFromNmea(const char *data, int size,
                                                                    QList<int> &pnrsInUse) override;
    SatelliteInfoParseStatus parseSatelliteInfoFromNmea(const char *data, int size,
                                                        QList<QGeoSatelliteInfo> &infos,
                                                        QGeoSatelliteInfo::SatelliteSystem &system) override;

private:
    int callCount;
};

DummyNmeaSatelliteInfoSource::DummyNmeaSatelliteInfoSource(QObject *parent)
    : QNmeaSatelliteInfoSource(QNmeaSatelliteInfoSource::UpdateMode::RealTimeMode, parent)
{
}

QGeoSatelliteInfo::SatelliteSystem
DummyNmeaSatelliteInfoSource::parseSatellitesInUseFromNmea(const char *data, int size,
                                                           QList<int> &pnrsInUse)
{
    // expected format: "USE:num1;num2;num3\n"
    // example: "USE:1;3;4;7\n"
    if (!data || !size)
        return QGeoSatelliteInfo::Undefined;

    QString str = QLatin1String(data, size).toString();
    if (!str.startsWith("USE:"))
        return QGeoSatelliteInfo::Undefined;

    const QStringList sl = str.mid(4).split(";", Qt::SkipEmptyParts);

    if (sl.empty())
        return QGeoSatelliteInfo::Undefined;

    for (const auto &str : sl) {
        bool ok = false;
        int value = str.toInt(&ok);
        if (ok) {
            pnrsInUse.push_back(value);
        }
    }
    return QGeoSatelliteInfo::GPS;
}

QNmeaSatelliteInfoSource::SatelliteInfoParseStatus
DummyNmeaSatelliteInfoSource::parseSatelliteInfoFromNmea(const char *data, int size,
                                                         QList<QGeoSatelliteInfo> &infos,
                                                         QGeoSatelliteInfo::SatelliteSystem &system)
{
    // expected format: "INFO:system,identifier;system,identifier;system,identifier\n"
    // example: "INFO:1,5;1,7;1,15\n"
    if (!data || !size)
        return NotParsed;

    QString str = QLatin1String(data, size).toString();
    if (!str.startsWith("INFO:"))
        return NotParsed;

    QStringList sat_infos = str.mid(5).split(";", Qt::SkipEmptyParts);

    if (sat_infos.empty())
        return NotParsed;

    for (const auto &sat_info : sat_infos) {
        QStringList parameters = sat_info.split(",", Qt::SkipEmptyParts);
        if (parameters.size() == 2) {
            QGeoSatelliteInfo info;
            info.setSatelliteSystem(
                    static_cast<QGeoSatelliteInfo::SatelliteSystem>(parameters[0].toInt()));
            info.setSatelliteIdentifier(parameters[1].toInt());
            infos.push_back(info);
        }
    }

    system = infos.isEmpty() ? QGeoSatelliteInfo::Undefined : infos.front().satelliteSystem();

    return FullyParsed;
}

class tst_DummyNmeaSatelliteInfoSource : public QObject
{
    Q_OBJECT

private slots:
    void testOverloadedParseFunction();
};

void tst_DummyNmeaSatelliteInfoSource::testOverloadedParseFunction()
{
    DummyNmeaSatelliteInfoSource source;
    QNmeaProxyFactory factory;
    QScopedPointer<QNmeaSatelliteInfoSourceProxy> proxy(
            factory.createSatelliteInfoSourceProxy(&source));

    QSignalSpy inUseSpy(proxy->source(), &QNmeaSatelliteInfoSource::satellitesInUseUpdated);
    QSignalSpy inViewSpy(proxy->source(), &QNmeaSatelliteInfoSource::satellitesInViewUpdated);

    proxy->source()->startUpdates();

    // first we need to send all satellites
    proxy->feedBytes("INFO:1,5;1,7;1,15\n");
    // then - used ones
    proxy->feedBytes("USE:5;15\n");

    QTRY_VERIFY_WITH_TIMEOUT(inUseSpy.count() == 1, 10000);
    QTRY_VERIFY_WITH_TIMEOUT(inViewSpy.count() == 1, 10000);

    QGeoSatelliteInfo info_1_5;
    info_1_5.setSatelliteSystem(QGeoSatelliteInfo::GPS);
    info_1_5.setSatelliteIdentifier(5);

    QGeoSatelliteInfo info_1_7;
    info_1_7.setSatelliteSystem(QGeoSatelliteInfo::GPS);
    info_1_7.setSatelliteIdentifier(7);

    QGeoSatelliteInfo info_1_15;
    info_1_15.setSatelliteSystem(QGeoSatelliteInfo::GPS);
    info_1_15.setSatelliteIdentifier(15);

    const QList<QGeoSatelliteInfo> desiredInView = { info_1_5, info_1_7, info_1_15 };
    const QList<QGeoSatelliteInfo> desiredInUse = { info_1_5, info_1_15 };

    const QList<QGeoSatelliteInfo> inViewList =
            inViewSpy.at(0).at(0).value<QList<QGeoSatelliteInfo>>();
    const QList<QGeoSatelliteInfo> inUseList =
            inUseSpy.at(0).at(0).value<QList<QGeoSatelliteInfo>>();

    QCOMPARE(inViewList, desiredInView);
    QCOMPARE(inUseList, desiredInUse);
}

#include "tst_dummynmeasatelliteinfosource.moc"

QTEST_GUILESS_MAIN(tst_DummyNmeaSatelliteInfoSource);