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//TESTED_COMPONENT=src/location
30
31#include "qnmeapositioninfosourceproxyfactory.h"
32#include "../qgeopositioninfosource/testqgeopositioninfosource_p.h"
33#include "../utils/qlocationtestutils_p.h"
34
35#include <QtPositioning/qnmeapositioninfosource.h>
36#include <QSignalSpy>
37#include <QTest>
38
39Q_DECLARE_METATYPE(QNmeaPositionInfoSource::UpdateMode)
40
41class DummyNmeaPositionInfoSource : public QNmeaPositionInfoSource
42{
43 Q_OBJECT
44
45public:
46 DummyNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent = 0);
47
48protected:
49 virtual bool parsePosInfoFromNmeaData(const char *data,
50 int size,
51 QGeoPositionInfo *posInfo,
52 bool *hasFix);
53
54private:
55 int callCount;
56};
57
58DummyNmeaPositionInfoSource::DummyNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent) :
59 QNmeaPositionInfoSource(mode, parent),
60 callCount(0)
61{
62}
63
64bool DummyNmeaPositionInfoSource::parsePosInfoFromNmeaData(const char* data,
65 int size,
66 QGeoPositionInfo *posInfo,
67 bool *hasFix)
68{
69 Q_UNUSED(data);
70 Q_UNUSED(size);
71
72 posInfo->setCoordinate(QGeoCoordinate(callCount * 1.0, callCount * 1.0, callCount * 1.0));
73 posInfo->setTimestamp(QDateTime::currentDateTime().toUTC());
74 *hasFix = true;
75 ++callCount;
76
77 return true;
78}
79
80class tst_DummyNmeaPositionInfoSource : public QObject
81{
82 Q_OBJECT
83
84public:
85 tst_DummyNmeaPositionInfoSource();
86
87private slots:
88 void initTestCase();
89 void testOverloadedParseFunction();
90};
91
92
93tst_DummyNmeaPositionInfoSource::tst_DummyNmeaPositionInfoSource() {}
94
95void tst_DummyNmeaPositionInfoSource::initTestCase()
96{
97
98}
99
100void tst_DummyNmeaPositionInfoSource::testOverloadedParseFunction()
101{
102 DummyNmeaPositionInfoSource source(QNmeaPositionInfoSource::RealTimeMode);
103 QNmeaPositionInfoSourceProxyFactory factory;
104 QNmeaPositionInfoSourceProxy *proxy = static_cast<QNmeaPositionInfoSourceProxy*>(factory.createProxy(source: &source));
105
106 QSignalSpy spy(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo)));
107
108 QGeoPositionInfo pos;
109
110 proxy->source()->startUpdates();
111
112 proxy->feedBytes(bytes: QString("The parser converts\n").toLatin1());
113
114 QTRY_VERIFY_WITH_TIMEOUT((spy.count() == 1), 10000);
115 pos = spy.at(i: 0).at(i: 0).value<QGeoPositionInfo>();
116
117 QVERIFY((pos.coordinate().latitude() == 0.0)
118 && (pos.coordinate().longitude() == 0.0)
119 && (pos.coordinate().altitude() == 0.0));
120
121 spy.clear();
122
123 proxy->feedBytes(bytes: QString("any data it receives\n").toLatin1());
124
125 QTRY_VERIFY_WITH_TIMEOUT((spy.count() == 1), 10000);
126 pos = spy.at(i: 0).at(i: 0).value<QGeoPositionInfo>();
127
128 QVERIFY((pos.coordinate().latitude() == 1.0)
129 && (pos.coordinate().longitude() == 1.0)
130 && (pos.coordinate().altitude() == 1.0));
131
132 spy.clear();
133
134 proxy->feedBytes(bytes: QString("into positions\n").toLatin1());
135
136 QTRY_VERIFY_WITH_TIMEOUT((spy.count() == 1), 10000);
137 pos = spy.at(i: 0).at(i: 0).value<QGeoPositionInfo>();
138
139 QVERIFY((pos.coordinate().latitude() == 2.0)
140 && (pos.coordinate().longitude() == 2.0)
141 && (pos.coordinate().altitude() == 2.0));
142
143 spy.clear();
144}
145
146#include "tst_dummynmeapositioninfosource.moc"
147
148QTEST_GUILESS_MAIN(tst_DummyNmeaPositionInfoSource);
149

source code of qtlocation/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/tst_dummynmeapositioninfosource.cpp