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
41 QT_USE_NAMESPACE
42 Q_DECLARE_METATYPE(QGeoSatelliteInfo)
43 Q_DECLARE_METATYPE(QGeoSatelliteInfo::Attribute)
44
45 QByteArray tst_qgeosatelliteinfo_debug;
46
tst_qgeosatelliteinfo_messageHandler(QtMsgType type,const QMessageLogContext &,const QString & msg)47 void 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
tst_qgeosatelliteinfo_qrealTestValues()59 QList<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
tst_qgeosatelliteinfo_intTestValues()76 QList<int> tst_qgeosatelliteinfo_intTestValues()
77 {
78 QList<int> values;
79 values << INT_MIN << -100 << 0 << 100 << INT_MAX;
80 return values;
81 }
82
tst_qgeosatelliteinfo_getAttributes()83 QList<QGeoSatelliteInfo::Attribute> tst_qgeosatelliteinfo_getAttributes()
84 {
85 QList<QGeoSatelliteInfo::Attribute> attributes;
86 attributes << QGeoSatelliteInfo::Elevation
87 << QGeoSatelliteInfo::Azimuth;
88 return attributes;
89 }
90
91
92 class tst_QGeoSatelliteInfo : public QObject
93 {
94 Q_OBJECT
95
96 private:
updateWithAttribute(QGeoSatelliteInfo::Attribute attribute,qreal value)97 QGeoSatelliteInfo updateWithAttribute(QGeoSatelliteInfo::Attribute attribute, qreal value)
98 {
99 QGeoSatelliteInfo info;
100 info.setAttribute(attribute, value);
101 return info;
102 }
103
addTestData_update()104 void addTestData_update()
105 {
106 QTest::addColumn<QGeoSatelliteInfo>("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("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("satellite identifier") << info;
120 }
121
122 QGeoSatelliteInfo info;
123 info.setSatelliteSystem(QGeoSatelliteInfo::GPS);
124 QTest::newRow("satellite system") << info;
125 info.setSatelliteSystem(QGeoSatelliteInfo::GLONASS);
126 QTest::newRow("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(attributes[i], qrealValues[i]);
133 }
134 }
135
136 private slots:
constructor()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 }
constructor_copy()147 void constructor_copy()
148 {
149 QFETCH(QGeoSatelliteInfo, info);
150
151 QCOMPARE(QGeoSatelliteInfo(info), info);
152 }
153
constructor_copy_data()154 void constructor_copy_data()
155 {
156 addTestData_update();
157 }
158
operator_comparison()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
operator_comparison_data()171 void operator_comparison_data()
172 {
173 addTestData_update();
174 }
175
operator_assign()176 void operator_assign()
177 {
178 QFETCH(QGeoSatelliteInfo, info);
179
180 QGeoSatelliteInfo info2 = info;
181 QCOMPARE(info2, info);
182 }
183
operator_assign_data()184 void operator_assign_data()
185 {
186 addTestData_update();
187 }
188
setSignalStrength()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
setSignalStrength_data()200 void setSignalStrength_data()
201 {
202 QTest::addColumn<int>("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 }
setSatelliteIdentifier()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
setSatelliteIdentifier_data()219 void setSatelliteIdentifier_data()
220 {
221 QTest::addColumn<int>("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
setSatelliteSystem()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
setSatelliteSystem_data()239 void setSatelliteSystem_data()
240 {
241 QTest::addColumn<int>("system");
242
243 QTest::newRow("Sat system undefined")
244 << int(QGeoSatelliteInfo::Undefined);
245 QTest::newRow("Sat system GPS")
246 << int(QGeoSatelliteInfo::GPS);
247 QTest::newRow("Sat system GLONASS")
248 << int(QGeoSatelliteInfo::GLONASS);
249 }
250
attribute()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
attribute_data()265 void attribute_data()
266 {
267 QTest::addColumn<QGeoSatelliteInfo::Attribute>("attribute");
268 QTest::addColumn<qreal>("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
hasAttribute()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
hasAttribute_data()301 void hasAttribute_data()
302 {
303 attribute_data();
304 }
305
removeAttribute()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
removeAttribute_data()324 void removeAttribute_data()
325 {
326 attribute_data();
327 }
328
datastream()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
datastream_data()343 void datastream_data()
344 {
345 addTestData_update();
346 }
347
debug()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
debug_data()360 void debug_data()
361 {
362 QTest::addColumn<QGeoSatelliteInfo>("info");
363 QTest::addColumn<int>("nextValue");
364 QTest::addColumn<QByteArray>("debugString");
365
366 QGeoSatelliteInfo info;
367
368 QTest::newRow("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("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("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("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("with System GLONASS") << info << 56
389 << QByteArray("QGeoSatelliteInfo(system=2, satId=-1, signal-strength=-1) 56");
390
391 info = QGeoSatelliteInfo();
392 info.setAttribute(QGeoSatelliteInfo::Elevation, 1.1);
393 QTest::newRow("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(QGeoSatelliteInfo::Azimuth, 1.1);
398 QTest::newRow("with Azimuth") << info << 45
399 << QByteArray("QGeoSatelliteInfo(system=0, satId=-1, signal-strength=-1, Azimuth=1.1) 45");
400 }
401 };
402
403
404 QTEST_APPLESS_MAIN(tst_QGeoSatelliteInfo)
405 #include "tst_qgeosatelliteinfo.moc"
406