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 <QtPositioning/qgeosatelliteinfo.h>
32
33#include <QMetaType>
34#include <QObject>
35#include <QDebug>
36#include <QTest>
37
38#include <float.h>
39#include <limits.h>
40
41QT_USE_NAMESPACE
42Q_DECLARE_METATYPE(QGeoSatelliteInfo)
43Q_DECLARE_METATYPE(QGeoSatelliteInfo::Attribute)
44
45QByteArray tst_qgeosatelliteinfo_debug;
46
47void tst_qgeosatelliteinfo_messageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg)
48{
49 switch (type) {
50 case QtDebugMsg :
51 tst_qgeosatelliteinfo_debug = msg.toLocal8Bit();
52 break;
53 default:
54 break;
55 }
56}
57
58
59QList<qreal> tst_qgeosatelliteinfo_qrealTestValues()
60{
61 QList<qreal> values;
62
63 if (qreal(DBL_MIN) == DBL_MIN)
64 values << DBL_MIN;
65
66 values << FLT_MIN;
67 values << -1.0 << 0.0 << 1.0;
68 values << FLT_MAX;
69
70 if (qreal(DBL_MAX) == DBL_MAX)
71 values << DBL_MAX;
72
73 return values;
74}
75
76QList<int> tst_qgeosatelliteinfo_intTestValues()
77{
78 QList<int> values;
79 values << INT_MIN << -100 << 0 << 100 << INT_MAX;
80 return values;
81}
82
83QList<QGeoSatelliteInfo::Attribute> tst_qgeosatelliteinfo_getAttributes()
84{
85 QList<QGeoSatelliteInfo::Attribute> attributes;
86 attributes << QGeoSatelliteInfo::Elevation
87 << QGeoSatelliteInfo::Azimuth;
88 return attributes;
89}
90
91
92class tst_QGeoSatelliteInfo : public QObject
93{
94 Q_OBJECT
95
96private:
97 QGeoSatelliteInfo updateWithAttribute(QGeoSatelliteInfo::Attribute attribute, qreal value)
98 {
99 QGeoSatelliteInfo info;
100 info.setAttribute(attribute, value);
101 return info;
102 }
103
104 void addTestData_update()
105 {
106 QTest::addColumn<QGeoSatelliteInfo>(name: "info");
107
108 QList<int> intValues = tst_qgeosatelliteinfo_intTestValues();
109
110 for (int i=0; i<intValues.count(); i++) {
111 QGeoSatelliteInfo info;
112 info.setSignalStrength(intValues[i]);
113 QTest::newRow(dataTag: "signal strength") << info;
114 }
115
116 for (int i=0; i<intValues.count(); i++) {
117 QGeoSatelliteInfo info;
118 info.setSatelliteIdentifier(intValues[i]);
119 QTest::newRow(dataTag: "satellite identifier") << info;
120 }
121
122 QGeoSatelliteInfo info;
123 info.setSatelliteSystem(QGeoSatelliteInfo::GPS);
124 QTest::newRow(dataTag: "satellite system") << info;
125 info.setSatelliteSystem(QGeoSatelliteInfo::GLONASS);
126 QTest::newRow(dataTag: "satellite system") << info;
127
128 QList<QGeoSatelliteInfo::Attribute> attributes = tst_qgeosatelliteinfo_getAttributes();
129 QList<qreal> qrealValues = tst_qgeosatelliteinfo_qrealTestValues();
130 for (int i=0; i<attributes.count(); i++) {
131 QTest::newRow(qPrintable(QString("Attribute %1 = %2").arg(attributes[i]).arg(qrealValues[i])))
132 << updateWithAttribute(attribute: attributes[i], value: qrealValues[i]);
133 }
134 }
135
136private slots:
137 void constructor()
138 {
139 QGeoSatelliteInfo info;
140 QCOMPARE(info.signalStrength(), -1);
141 QCOMPARE(info.satelliteIdentifier(), -1);
142 QCOMPARE(info.satelliteSystem(), QGeoSatelliteInfo::Undefined);
143 QList<QGeoSatelliteInfo::Attribute> attributes = tst_qgeosatelliteinfo_getAttributes();
144 for (int i=0; i<attributes.count(); i++)
145 QCOMPARE(info.attribute(attributes[i]), qreal(-1.0));
146 }
147 void constructor_copy()
148 {
149 QFETCH(QGeoSatelliteInfo, info);
150
151 QCOMPARE(QGeoSatelliteInfo(info), info);
152 }
153
154 void constructor_copy_data()
155 {
156 addTestData_update();
157 }
158
159 void operator_comparison()
160 {
161 QFETCH(QGeoSatelliteInfo, info);
162
163 QVERIFY(info == info);
164 QCOMPARE(info != info, false);
165 QCOMPARE(info == QGeoSatelliteInfo(), false);
166 QCOMPARE(info != QGeoSatelliteInfo(), true);
167
168 QVERIFY(QGeoSatelliteInfo() == QGeoSatelliteInfo());
169 }
170
171 void operator_comparison_data()
172 {
173 addTestData_update();
174 }
175
176 void operator_assign()
177 {
178 QFETCH(QGeoSatelliteInfo, info);
179
180 QGeoSatelliteInfo info2 = info;
181 QCOMPARE(info2, info);
182 }
183
184 void operator_assign_data()
185 {
186 addTestData_update();
187 }
188
189 void setSignalStrength()
190 {
191 QFETCH(int, signal);
192
193 QGeoSatelliteInfo info;
194 QCOMPARE(info.signalStrength(), -1);
195
196 info.setSignalStrength(signal);
197 QCOMPARE(info.signalStrength(), signal);
198 }
199
200 void setSignalStrength_data()
201 {
202 QTest::addColumn<int>(name: "signal");
203
204 QList<int> intValues = tst_qgeosatelliteinfo_intTestValues();
205 for (int i=0; i<intValues.count(); i++)
206 QTest::newRow(qPrintable(QString("%1").arg(intValues[i]))) << intValues[i];
207 }
208 void setSatelliteIdentifier()
209 {
210 QFETCH(int, satId);
211
212 QGeoSatelliteInfo info;
213 QCOMPARE(info.satelliteIdentifier(), -1);
214
215 info.setSatelliteIdentifier(satId);
216 QCOMPARE(info.satelliteIdentifier(), satId);
217 }
218
219 void setSatelliteIdentifier_data()
220 {
221 QTest::addColumn<int>(name: "satId");
222
223 QList<int> intValues = tst_qgeosatelliteinfo_intTestValues();
224 for (int i=0; i<intValues.count(); i++)
225 QTest::newRow(qPrintable(QString("%1").arg(intValues[i]))) << intValues[i];
226 }
227
228 void setSatelliteSystem()
229 {
230 QFETCH(int, system);
231
232 QGeoSatelliteInfo info;
233 QCOMPARE(info.satelliteSystem(), QGeoSatelliteInfo::Undefined);
234
235 info.setSatelliteSystem(static_cast<QGeoSatelliteInfo::SatelliteSystem>(system));
236 QCOMPARE(info.satelliteSystem(), static_cast<QGeoSatelliteInfo::SatelliteSystem>(system));
237 }
238
239 void setSatelliteSystem_data()
240 {
241 QTest::addColumn<int>(name: "system");
242
243 QTest::newRow(dataTag: "Sat system undefined")
244 << int(QGeoSatelliteInfo::Undefined);
245 QTest::newRow(dataTag: "Sat system GPS")
246 << int(QGeoSatelliteInfo::GPS);
247 QTest::newRow(dataTag: "Sat system GLONASS")
248 << int(QGeoSatelliteInfo::GLONASS);
249 }
250
251 void attribute()
252 {
253 QFETCH(QGeoSatelliteInfo::Attribute, attribute);
254 QFETCH(qreal, value);
255
256 QGeoSatelliteInfo u;
257 QCOMPARE(u.attribute(attribute), qreal(-1.0));
258
259 u.setAttribute(attribute, value);
260 QCOMPARE(u.attribute(attribute), value);
261 u.removeAttribute(attribute);
262 QCOMPARE(u.attribute(attribute), qreal(-1.0));
263 }
264
265 void attribute_data()
266 {
267 QTest::addColumn<QGeoSatelliteInfo::Attribute>(name: "attribute");
268 QTest::addColumn<qreal>(name: "value");
269
270 QList<QGeoSatelliteInfo::Attribute> props;
271 props << QGeoSatelliteInfo::Elevation
272 << QGeoSatelliteInfo::Azimuth;
273 for (int i=0; i<props.count(); i++) {
274 QTest::newRow(qPrintable(QString("Attribute %1 = -1.0").arg(props[i])))
275 << props[i]
276 << qreal(-1.0);
277 QTest::newRow(qPrintable(QString("Attribute %1 = 0.0").arg(props[i])))
278 << props[i]
279 << qreal(0.0);
280 QTest::newRow(qPrintable(QString("Attribute %1 = 1.0").arg(props[i])))
281 << props[i]
282 << qreal(1.0);
283 }
284 }
285
286 void hasAttribute()
287 {
288 QFETCH(QGeoSatelliteInfo::Attribute, attribute);
289 QFETCH(qreal, value);
290
291 QGeoSatelliteInfo u;
292 QVERIFY(!u.hasAttribute(attribute));
293
294 u.setAttribute(attribute, value);
295 QVERIFY(u.hasAttribute(attribute));
296
297 u.removeAttribute(attribute);
298 QVERIFY(!u.hasAttribute(attribute));
299 }
300
301 void hasAttribute_data()
302 {
303 attribute_data();
304 }
305
306 void removeAttribute()
307 {
308 QFETCH(QGeoSatelliteInfo::Attribute, attribute);
309 QFETCH(qreal, value);
310
311 QGeoSatelliteInfo u;
312 QVERIFY(!u.hasAttribute(attribute));
313
314 u.setAttribute(attribute, value);
315 QVERIFY(u.hasAttribute(attribute));
316
317 u.removeAttribute(attribute);
318 QVERIFY(!u.hasAttribute(attribute));
319
320 u.setAttribute(attribute, value);
321 QVERIFY(u.hasAttribute(attribute));
322 }
323
324 void removeAttribute_data()
325 {
326 attribute_data();
327 }
328
329 void datastream()
330 {
331 QFETCH(QGeoSatelliteInfo, info);
332
333 QByteArray ba;
334 QDataStream out(&ba, QIODevice::WriteOnly);
335 out << info;
336
337 QDataStream in(&ba, QIODevice::ReadOnly);
338 QGeoSatelliteInfo inInfo;
339 in >> inInfo;
340 QCOMPARE(inInfo, info);
341 }
342
343 void datastream_data()
344 {
345 addTestData_update();
346 }
347
348 void debug()
349 {
350 QFETCH(QGeoSatelliteInfo, info);
351 QFETCH(int, nextValue);
352 QFETCH(QByteArray, debugString);
353
354 qInstallMessageHandler(tst_qgeosatelliteinfo_messageHandler);
355 qDebug() << info << nextValue;
356 qInstallMessageHandler(0);
357 QCOMPARE(QString(tst_qgeosatelliteinfo_debug), QString(debugString));
358 }
359
360 void debug_data()
361 {
362 QTest::addColumn<QGeoSatelliteInfo>(name: "info");
363 QTest::addColumn<int>(name: "nextValue");
364 QTest::addColumn<QByteArray>(name: "debugString");
365
366 QGeoSatelliteInfo info;
367
368 QTest::newRow(dataTag: "uninitialized") << info << 45
369 << QByteArray("QGeoSatelliteInfo(system=0, satId=-1, signal-strength=-1) 45");
370
371 info = QGeoSatelliteInfo();
372 info.setSignalStrength(1);
373 QTest::newRow(dataTag: "with SignalStrength") << info << 60
374 << QByteArray("QGeoSatelliteInfo(system=0, satId=-1, signal-strength=1) 60");
375
376 info = QGeoSatelliteInfo();
377 info.setSatelliteIdentifier(1);
378 QTest::newRow(dataTag: "with SatelliteIdentifier") << info << -1
379 << QByteArray("QGeoSatelliteInfo(system=0, satId=1, signal-strength=-1) -1");
380
381 info = QGeoSatelliteInfo();
382 info.setSatelliteSystem(QGeoSatelliteInfo::GPS);
383 QTest::newRow(dataTag: "with System GPS") << info << 1
384 << QByteArray("QGeoSatelliteInfo(system=1, satId=-1, signal-strength=-1) 1");
385
386 info = QGeoSatelliteInfo();
387 info.setSatelliteSystem(QGeoSatelliteInfo::GLONASS);
388 QTest::newRow(dataTag: "with System GLONASS") << info << 56
389 << QByteArray("QGeoSatelliteInfo(system=2, satId=-1, signal-strength=-1) 56");
390
391 info = QGeoSatelliteInfo();
392 info.setAttribute(attribute: QGeoSatelliteInfo::Elevation, value: 1.1);
393 QTest::newRow(dataTag: "with Elevation") << info << 0
394 << QByteArray("QGeoSatelliteInfo(system=0, satId=-1, signal-strength=-1, Elevation=1.1) 0");
395
396 info = QGeoSatelliteInfo();
397 info.setAttribute(attribute: QGeoSatelliteInfo::Azimuth, value: 1.1);
398 QTest::newRow(dataTag: "with Azimuth") << info << 45
399 << QByteArray("QGeoSatelliteInfo(system=0, satId=-1, signal-strength=-1, Azimuth=1.1) 45");
400 }
401};
402
403
404QTEST_APPLESS_MAIN(tst_QGeoSatelliteInfo)
405#include "tst_qgeosatelliteinfo.moc"
406

source code of qtlocation/tests/auto/qgeosatelliteinfo/tst_qgeosatelliteinfo.cpp