|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 #include <qgeopositioninfo.h> |
|
42 |
|
43 #include <QMetaType> |
|
44 #include <QObject> |
|
45 #include <QDebug> |
|
46 #include <QTest> |
|
47 |
|
48 #include <float.h> |
|
49 |
|
50 QTM_USE_NAMESPACE |
|
51 Q_DECLARE_METATYPE(QGeoCoordinate) |
|
52 Q_DECLARE_METATYPE(QGeoPositionInfo) |
|
53 Q_DECLARE_METATYPE(QGeoPositionInfo::Attribute) |
|
54 |
|
55 QByteArray tst_qgeopositioninfo_debug; |
|
56 |
|
57 void tst_qgeopositioninfo_messageHandler(QtMsgType type, const char *msg) |
|
58 { |
|
59 switch(type) { |
|
60 case QtDebugMsg : |
|
61 tst_qgeopositioninfo_debug = QByteArray(msg); |
|
62 break; |
|
63 default: |
|
64 break; |
|
65 } |
|
66 } |
|
67 |
|
68 QList<qreal> tst_qgeopositioninfo_qrealTestValues() |
|
69 { |
|
70 QList<qreal> values; |
|
71 |
|
72 // the following platforms use float for qreal |
|
73 #if !defined(QT_NO_FPU) && !defined(QT_ARCH_ARM) && !defined(QT_ARCH_WINDOWSCE) && !defined(QT_ARCH_SYMBIAN) |
|
74 if (qreal(DBL_MIN) == DBL_MIN) |
|
75 values << DBL_MIN; |
|
76 #endif |
|
77 |
|
78 values << FLT_MIN; |
|
79 values << -1.0 << 0.0 << 1.0; |
|
80 values << FLT_MAX; |
|
81 |
|
82 // the following platforms use float for qreal |
|
83 #if !defined(QT_NO_FPU) && !defined(QT_ARCH_ARM) && !defined(QT_ARCH_WINDOWSCE) && !defined(QT_ARCH_SYMBIAN) |
|
84 if (qreal(DBL_MAX) == DBL_MAX) |
|
85 values << DBL_MAX; |
|
86 #endif |
|
87 |
|
88 return values; |
|
89 } |
|
90 |
|
91 QList<QGeoPositionInfo::Attribute> tst_qgeopositioninfo_getAttributes() |
|
92 { |
|
93 QList<QGeoPositionInfo::Attribute> attributes; |
|
94 attributes << QGeoPositionInfo::Direction |
|
95 << QGeoPositionInfo::GroundSpeed |
|
96 << QGeoPositionInfo::VerticalSpeed |
|
97 << QGeoPositionInfo::MagneticVariation |
|
98 << QGeoPositionInfo::HorizontalAccuracy |
|
99 << QGeoPositionInfo::VerticalAccuracy; |
|
100 return attributes; |
|
101 } |
|
102 |
|
103 |
|
104 class tst_QGeoPositionInfo : public QObject |
|
105 { |
|
106 Q_OBJECT |
|
107 |
|
108 private: |
|
109 QGeoPositionInfo infoWithAttribute(QGeoPositionInfo::Attribute attribute, qreal value) |
|
110 { |
|
111 QGeoPositionInfo info; |
|
112 info.setAttribute(attribute, value); |
|
113 return info; |
|
114 } |
|
115 |
|
116 void addTestData_info() |
|
117 { |
|
118 QTest::addColumn<QGeoPositionInfo>("info"); |
|
119 |
|
120 QTest::newRow("invalid") << QGeoPositionInfo(); |
|
121 |
|
122 QTest::newRow("coord") << QGeoPositionInfo(QGeoCoordinate(-27.3422,150.2342), QDateTime()); |
|
123 QTest::newRow("datetime") << QGeoPositionInfo(QGeoCoordinate(), QDateTime::currentDateTime()); |
|
124 |
|
125 QList<QGeoPositionInfo::Attribute> attributes = tst_qgeopositioninfo_getAttributes(); |
|
126 QList<qreal> values = tst_qgeopositioninfo_qrealTestValues(); |
|
127 for (int i=0; i<attributes.count(); i++) { |
|
128 for (int j=0; j<values.count(); j++) { |
|
129 QTest::newRow(qPrintable(QString("Attribute %1 = %2").arg(attributes[i]).arg(values[j]))) |
|
130 << infoWithAttribute(attributes[i], values[j]); |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 private slots: |
|
136 void constructor() |
|
137 { |
|
138 QGeoPositionInfo info; |
|
139 QVERIFY(!info.isValid()); |
|
140 QVERIFY(!info.coordinate().isValid()); |
|
141 QVERIFY(info.timestamp().isNull()); |
|
142 } |
|
143 |
|
144 void constructor_coord_dateTime() |
|
145 { |
|
146 QFETCH(QGeoCoordinate, coord); |
|
147 QFETCH(QDateTime, dateTime); |
|
148 QFETCH(bool, valid); |
|
149 |
|
150 QGeoPositionInfo info(coord, dateTime); |
|
151 QCOMPARE(info.coordinate(), coord); |
|
152 QCOMPARE(info.timestamp(), dateTime); |
|
153 QCOMPARE(info.isValid(), valid); |
|
154 } |
|
155 |
|
156 void constructor_coord_dateTime_data() |
|
157 { |
|
158 QTest::addColumn<QGeoCoordinate>("coord"); |
|
159 QTest::addColumn<QDateTime>("dateTime"); |
|
160 QTest::addColumn<bool>("valid"); |
|
161 |
|
162 QTest::newRow("both null") << QGeoCoordinate() << QDateTime() << false; |
|
163 QTest::newRow("both valid") << QGeoCoordinate(1,1) << QDateTime::currentDateTime() << true; |
|
164 QTest::newRow("valid coord") << QGeoCoordinate(1,1) << QDateTime() << false; |
|
165 QTest::newRow("valid datetime") << QGeoCoordinate() << QDateTime::currentDateTime() << false; |
|
166 QTest::newRow("valid time but not date == invalid") |
|
167 << QGeoCoordinate() << QDateTime(QDate(), QTime::currentTime()) << false; |
|
168 QTest::newRow("valid date but not time == valid due to QDateTime constructor") |
|
169 << QGeoCoordinate() << QDateTime(QDate::currentDate(), QTime()) << false; |
|
170 } |
|
171 |
|
172 void constructor_copy() |
|
173 { |
|
174 QFETCH(QGeoPositionInfo, info); |
|
175 |
|
176 QCOMPARE(QGeoPositionInfo(info), info); |
|
177 } |
|
178 |
|
179 void constructor_copy_data() |
|
180 { |
|
181 addTestData_info(); |
|
182 } |
|
183 |
|
184 void operator_assign() |
|
185 { |
|
186 QFETCH(QGeoPositionInfo, info); |
|
187 |
|
188 QGeoPositionInfo info2 = info; |
|
189 QCOMPARE(info2, info); |
|
190 } |
|
191 |
|
192 void operator_assign_data() |
|
193 { |
|
194 addTestData_info(); |
|
195 } |
|
196 |
|
197 void operator_equals() |
|
198 { |
|
199 QFETCH(QGeoPositionInfo, info); |
|
200 |
|
201 QVERIFY(info == info); |
|
202 if (info.isValid()) |
|
203 QCOMPARE(info == QGeoPositionInfo(), false); |
|
204 } |
|
205 |
|
206 void operator_equals_data() |
|
207 { |
|
208 addTestData_info(); |
|
209 } |
|
210 |
|
211 void operator_notEquals() |
|
212 { |
|
213 QFETCH(QGeoPositionInfo, info); |
|
214 |
|
215 QCOMPARE(info != info, false); |
|
216 if (info.isValid()) |
|
217 QCOMPARE(info != QGeoPositionInfo(), true); |
|
218 } |
|
219 |
|
220 void operator_notEquals_data() |
|
221 { |
|
222 addTestData_info(); |
|
223 } |
|
224 |
|
225 void setDateTime() |
|
226 { |
|
227 QFETCH(QDateTime, dateTime); |
|
228 |
|
229 QGeoPositionInfo info; |
|
230 info.setTimestamp(dateTime); |
|
231 QCOMPARE(info.timestamp(), dateTime); |
|
232 } |
|
233 |
|
234 void setDateTime_data() |
|
235 { |
|
236 QTest::addColumn<QDateTime>("dateTime"); |
|
237 QTest::newRow("invalid") << QDateTime(); |
|
238 QTest::newRow("now") << QDateTime::currentDateTime(); |
|
239 } |
|
240 |
|
241 void dateTime() |
|
242 { |
|
243 QGeoPositionInfo info; |
|
244 QVERIFY(info.timestamp().isNull()); |
|
245 } |
|
246 |
|
247 void setCoordinate() |
|
248 { |
|
249 |
|
250 QFETCH(QGeoCoordinate, coord); |
|
251 |
|
252 QGeoPositionInfo info; |
|
253 info.setCoordinate(coord); |
|
254 QCOMPARE(info.coordinate(), coord); |
|
255 } |
|
256 |
|
257 void setCoordinate_data() |
|
258 { |
|
259 QTest::addColumn<QGeoCoordinate>("coord"); |
|
260 |
|
261 QTest::newRow("invalid") << QGeoCoordinate(); |
|
262 QTest::newRow("valid") << QGeoCoordinate(30,30); |
|
263 } |
|
264 |
|
265 void attribute() |
|
266 { |
|
267 QFETCH(QGeoPositionInfo::Attribute, attribute); |
|
268 QFETCH(qreal, value); |
|
269 |
|
270 QGeoPositionInfo info; |
|
271 QCOMPARE(info.attribute(attribute), qreal(-1.0)); |
|
272 |
|
273 info.setAttribute(attribute, value); |
|
274 QCOMPARE(info.attribute(attribute), value); |
|
275 |
|
276 info.removeAttribute(attribute); |
|
277 QCOMPARE(info.attribute(attribute), qreal(-1.0)); |
|
278 } |
|
279 |
|
280 void attribute_data() |
|
281 { |
|
282 QTest::addColumn<QGeoPositionInfo::Attribute>("attribute"); |
|
283 QTest::addColumn<qreal>("value"); |
|
284 |
|
285 QList<QGeoPositionInfo::Attribute> attributes = tst_qgeopositioninfo_getAttributes(); |
|
286 QList<qreal> values = tst_qgeopositioninfo_qrealTestValues(); |
|
287 for (int i=0; i<attributes.count(); i++) { |
|
288 for (int j=0; j<values.count(); j++) { |
|
289 QTest::newRow(qPrintable(QString("Attribute %1 = %2").arg(attributes[i]).arg(values[j]))) |
|
290 << attributes[i] << values[j]; |
|
291 } |
|
292 } |
|
293 } |
|
294 |
|
295 void hasAttribute() |
|
296 { |
|
297 QFETCH(QGeoPositionInfo::Attribute, attribute); |
|
298 QFETCH(qreal, value); |
|
299 |
|
300 QGeoPositionInfo info; |
|
301 QVERIFY(!info.hasAttribute(attribute)); |
|
302 |
|
303 info.setAttribute(attribute, value); |
|
304 QVERIFY(info.hasAttribute(attribute)); |
|
305 |
|
306 info.removeAttribute(attribute); |
|
307 QVERIFY(!info.hasAttribute(attribute)); |
|
308 } |
|
309 |
|
310 void hasAttribute_data() |
|
311 { |
|
312 attribute_data(); |
|
313 } |
|
314 |
|
315 void removeAttribute() |
|
316 { |
|
317 QFETCH(QGeoPositionInfo::Attribute, attribute); |
|
318 QFETCH(qreal, value); |
|
319 |
|
320 QGeoPositionInfo info; |
|
321 QVERIFY(!info.hasAttribute(attribute)); |
|
322 |
|
323 info.setAttribute(attribute, value); |
|
324 QVERIFY(info.hasAttribute(attribute)); |
|
325 |
|
326 info.removeAttribute(attribute); |
|
327 QVERIFY(!info.hasAttribute(attribute)); |
|
328 |
|
329 info.setAttribute(attribute, value); |
|
330 QVERIFY(info.hasAttribute(attribute)); |
|
331 } |
|
332 |
|
333 void removeAttribute_data() |
|
334 { |
|
335 attribute_data(); |
|
336 } |
|
337 |
|
338 void datastream() |
|
339 { |
|
340 QFETCH(QGeoPositionInfo, info); |
|
341 |
|
342 QByteArray ba; |
|
343 QDataStream out(&ba, QIODevice::WriteOnly); |
|
344 out << info; |
|
345 |
|
346 QDataStream in(&ba, QIODevice::ReadOnly); |
|
347 QGeoPositionInfo inInfo; |
|
348 in >> inInfo; |
|
349 QCOMPARE(inInfo, info); |
|
350 } |
|
351 |
|
352 void datastream_data() |
|
353 { |
|
354 addTestData_info(); |
|
355 } |
|
356 |
|
357 void debug() |
|
358 { |
|
359 QFETCH(QGeoPositionInfo, info); |
|
360 QFETCH(QByteArray, debugStringEnd); |
|
361 |
|
362 qInstallMsgHandler(tst_qgeopositioninfo_messageHandler); |
|
363 qDebug() << info; |
|
364 qInstallMsgHandler(0); |
|
365 |
|
366 // use endsWith() so we don't depend on QDateTime's debug() implementation |
|
367 QVERIFY(tst_qgeopositioninfo_debug.endsWith(debugStringEnd)); |
|
368 } |
|
369 |
|
370 void debug_data() |
|
371 { |
|
372 QTest::addColumn<QGeoPositionInfo>("info"); |
|
373 QTest::addColumn<QByteArray>("debugStringEnd"); |
|
374 |
|
375 QTest::newRow("no values") << QGeoPositionInfo() |
|
376 << QString("QGeoCoordinate(?, ?))").toLatin1(); |
|
377 |
|
378 QGeoCoordinate coord(1, 1); |
|
379 QTest::newRow("coord, time") << QGeoPositionInfo(coord, QDateTime::currentDateTime()) |
|
380 << QByteArray("QGeoCoordinate(1, 1))"); |
|
381 |
|
382 QGeoPositionInfo info; |
|
383 info.setAttribute(QGeoPositionInfo::Direction, 1.1); |
|
384 info.setAttribute(QGeoPositionInfo::GroundSpeed, 2.1); |
|
385 info.setAttribute(QGeoPositionInfo::VerticalSpeed, 3.1); |
|
386 info.setAttribute(QGeoPositionInfo::MagneticVariation, 4.1); |
|
387 info.setAttribute(QGeoPositionInfo::HorizontalAccuracy, 5.1); |
|
388 info.setAttribute(QGeoPositionInfo::VerticalAccuracy, 6.1); |
|
389 QTest::newRow("all attributes") << info |
|
390 << QByteArray("QGeoCoordinate(?, ?), Direction=1.1, GroundSpeed=2.1, VerticalSpeed=3.1, MagneticVariation=4.1, HorizontalAccuracy=5.1, VerticalAccuracy=6.1)"); |
|
391 } |
|
392 }; |
|
393 |
|
394 |
|
395 QTEST_MAIN(tst_QGeoPositionInfo) |
|
396 #include "tst_qgeopositioninfo.moc" |