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 #include <QtCore/QString>
30 #include <QtTest/QtTest>
31 
32 #include <QtPositioning/qgeoaddress.h>
33 
34 QT_USE_NAMESPACE
35 
36 class tst_QGeoAddress : public QObject
37 {
38     Q_OBJECT
39 
40 public:
41     tst_QGeoAddress();
42 
43 private Q_SLOTS:
44     void constructorTest();
45     void textTest();
46 //TODO: there are various field we don't have yet in QGeoAddress
47 //       will need to either remove or enable these tests
48 //    void additionalDataTest();
49 //    void alternativeAttributesTest();
50     void cityTest();
51     void countryCodeTest();
52     void countryTest();
53     void countyTest();
54     void districtTest();
55 //    void floorTest();
56 //    void houseNumberTest();
57 //    void labelTest();
58     void postalCodeTest();
59     void stateTest();
60     void streetTest();
61 //    void suiteTest();
62     void generatedText();
63     void generatedText_data();
64     void operatorsTest();
65     void emptyClearTest();
66 };
67 
tst_QGeoAddress()68 tst_QGeoAddress::tst_QGeoAddress()
69 {
70 }
71 
constructorTest()72 void tst_QGeoAddress::constructorTest()
73 {
74     QGeoAddress testObj;
75 
76     testObj.setStreet("testId");
77     QGeoAddress *testObjPtr = new QGeoAddress(testObj);
78     QVERIFY2(testObjPtr != NULL, "Copy constructor - null");
79     QVERIFY2(*testObjPtr == testObj, "Copy constructor - compare");
80     delete testObjPtr;
81 }
82 
textTest()83 void tst_QGeoAddress::textTest()
84 {
85     QGeoAddress address;
86     QVERIFY(address.text().isEmpty());
87     address.setText(QStringLiteral("123 Fake Street\nSpringfield"));
88     QCOMPARE(address.text(), QStringLiteral("123 Fake Street\nSpringfield"));
89 }
90 
cityTest()91 void tst_QGeoAddress::cityTest()
92 {
93     QGeoAddress testObj;
94     QVERIFY2(testObj.city() == QString(), "Wrong default value");
95     testObj.setCity("testText");
96     QVERIFY2(testObj.city() == "testText", "Wrong value returned");
97 }
98 
countryCodeTest()99 void tst_QGeoAddress::countryCodeTest()
100 {
101     QGeoAddress testObj;
102     QVERIFY2(testObj.countryCode() == QString(), "Wrong default value");
103     testObj.setCountryCode("testText");
104     QVERIFY2(testObj.countryCode() == "testText", "Wrong value returned");
105 }
106 
countryTest()107 void tst_QGeoAddress::countryTest()
108 {
109     QGeoAddress testObj;
110     QVERIFY2(testObj.country() == QString(), "Wrong default value");
111     testObj.setCountry("testText");
112     QVERIFY2(testObj.country() == "testText", "Wrong value returned");
113 }
114 
countyTest()115 void tst_QGeoAddress::countyTest()
116 {
117     QGeoAddress testObj;
118     QVERIFY2(testObj.county() == QString(), "Wrong default value");
119     testObj.setCounty("testText");
120     QVERIFY2(testObj.county() == "testText", "Wrong value returned");
121 }
122 
districtTest()123 void tst_QGeoAddress::districtTest()
124 {
125     QGeoAddress testObj;
126     QVERIFY2(testObj.district() == QString(), "Wrong default value");
127     testObj.setDistrict("testText");
128     QVERIFY2(testObj.district() == "testText", "Wrong value returned");
129 }
130 
131 //  TODO: currently don't have floor in QGeoAddress
132 //void tst_QGeoAddress::floorTest()
133 //{
134 //    QGeoAddress testObj;
135 //    QVERIFY2(testObj.floor() == QString(), "Wrong default value");
136 //    testObj.setFloor("testText");
137 //    QVERIFY2(testObj.floor() == "testText", "Wrong value returned");
138 //}
139 
140 //TODO: Atm not sure if we will have house number in API.
141 //void tst_QGeoAddress::houseNumberTest()
142 //{
143 //    QGeoAddress testObj;
144 //    QVERIFY2(testObj.houseNumber() == QString(), "Wrong default value");
145 //    testObj.setHouseNumber("testText");
146 //    QVERIFY2(testObj.houseNumber() == "testText", "Wrong value returned");
147 //}
148 
149 //void tst_QGeoAddress::labelTest()
150 //{
151 //    QGeoAddress testObj;
152 //    QVERIFY2(testObj.label() == QString(), "Wrong default value");
153 //    testObj.setLabel("testText");
154 //    QVERIFY2(testObj.label() == "testText", "Wrong value returned");
155 //}
156 
postalCodeTest()157 void tst_QGeoAddress::postalCodeTest()
158 {
159     QGeoAddress testObj;
160     QVERIFY2(testObj.postalCode() == QString(), "Wrong default value");
161     testObj.setPostalCode("testText");
162     QVERIFY2(testObj.postalCode() == "testText", "Wrong value returned");
163 }
164 
stateTest()165 void tst_QGeoAddress::stateTest()
166 {
167     QGeoAddress testObj;
168     QVERIFY2(testObj.state() == QString(), "Wrong default value");
169     testObj.setState("testText");
170     QVERIFY2(testObj.state() == "testText", "Wrong value returned");
171 }
172 
streetTest()173 void tst_QGeoAddress::streetTest()
174 {
175     QGeoAddress testObj;
176     QVERIFY2(testObj.street() == QString(), "Wrong default value");
177     testObj.setStreet("testText");
178     QVERIFY2(testObj.street() == "testText", "Wrong value returned");
179 }
180 
generatedText()181 void tst_QGeoAddress::generatedText()
182 {
183     QFETCH(QString, countryCode);
184     QFETCH(QString, expectedPostalCodeOnly);
185     QFETCH(QString, expectedFullAddress);
186 
187     QGeoAddress streetOnly;
188     streetOnly.setStreet("street");
189     streetOnly.setCountryCode(countryCode);
190 
191     QCOMPARE(streetOnly.text(), QStringLiteral("street"));
192 
193     QGeoAddress cityOnly;
194     cityOnly.setCity("city");
195     cityOnly.setCountryCode(countryCode);
196     if (countryCode == QLatin1String("CYM") || countryCode == QLatin1String("IRL"))
197         QCOMPARE(cityOnly.text(), QString());
198     else
199         QCOMPARE(cityOnly.text(), QStringLiteral("city"));
200 
201     QGeoAddress postalCodeOnly;
202     postalCodeOnly.setPostalCode("postcode");
203     postalCodeOnly.setCountryCode(countryCode);
204     QCOMPARE(postalCodeOnly.text(), expectedPostalCodeOnly);
205 
206     QGeoAddress fullAddress;
207     fullAddress.setStreet("street");
208     fullAddress.setDistrict("district");
209     fullAddress.setPostalCode("postcode");
210     fullAddress.setCity("city");
211     fullAddress.setState("state");
212     fullAddress.setCountry("country");
213     fullAddress.setCountryCode(countryCode);
214 
215     QCOMPARE(fullAddress.text(), expectedFullAddress);
216 }
217 
generatedText_data()218 void tst_QGeoAddress::generatedText_data()
219 {
220     QTest::addColumn<QString>("countryCode");
221     QTest::addColumn<QString>("expectedPostalCodeOnly");
222     QTest::addColumn<QString>("expectedFullAddress");
223 
224     QTest::newRow("Albania") << QString::fromLatin1("ALB")
225                              << QString::fromLatin1("postcode") /* postal code only */
226                              << QString::fromLatin1("street<br/>"   /* full address */
227                                                     "postcode, city<br/>"
228                                                     "country");
229 
230     QTest::newRow("Andorra") << QString::fromLatin1("AND")
231                              << QString::fromLatin1("postcode")
232                              << QString::fromLatin1("street<br/>"
233                                                     "postcode city<br/>"
234                                                     "country");
235     QTest::newRow("United Arab Emirates") << QString::fromLatin1("ARE")
236                                           << QString()
237                                           << QString::fromLatin1("street<br/>"
238                                                                  "district city<br/>"
239                                                                  "country");
240     QTest::newRow("Australia") << QString::fromLatin1("AUS")
241                                << QString::fromLatin1("postcode")
242                                << QString::fromLatin1("street<br/>"
243                                                       "district state postcode<br/>"
244                                                       "country");
245     QTest::newRow("Austria") << QString::fromLatin1("AUT")
246                              << QString::fromLatin1("postcode")
247                              << QString::fromLatin1("street<br/>"
248                                                     "postcode city<br/>"
249                                                     "country");
250     QTest::newRow("Bahamas") << QString::fromLatin1("BHS")
251                              << QString()
252                                      << QString::fromLatin1("street<br/>"
253                                                             "district city<br/>"
254                                                             "country");
255     QTest::newRow("Bahrain") << QString::fromLatin1("BHR")
256                              << QString()
257                              << QString::fromLatin1("street<br/>"
258                                                     "district, city, state<br/>"
259                                                     "country");
260     QTest::newRow("Brazil") << QString::fromLatin1("BRA")
261                             << QString::fromLatin1("postcode")
262                             << QString::fromLatin1("street<br/>"
263                                                    "district city-state postcode<br/>"
264                                                    "country");
265     QTest::newRow("Brunei Darussalam") << QString::fromLatin1("BRN")
266                                        << QString::fromLatin1("postcode")
267                                        << QString::fromLatin1("street<br/>"
268                                                               "district city postcode<br/>"
269                                                               "country");
270     QTest::newRow("Canada") << QString::fromLatin1("CAN")
271                             << QString::fromLatin1("postcode")
272                             << QString::fromLatin1("street<br/>"
273                                                    "city, state postcode<br/>"
274                                                    "country");
275     QTest::newRow("China") << QString::fromLatin1("CHN")
276                            << QString::fromLatin1("postcode")
277                            << QString::fromLatin1("street, city<br/>"
278                                                   "postcode state<br/>"
279                                                   "country");
280     QTest::newRow("Chile") << QString::fromLatin1("CHL")
281                            << QString::fromLatin1("postcode")
282                            << QString::fromLatin1("street<br/>"
283                                                   "postcode district, city, state<br/>"
284                                                   "country");
285     QTest::newRow("Cayman Islands") << QString::fromLatin1("CYM")
286                                     << QString::fromLatin1("postcode")
287                                     << QString::fromLatin1("street<br/>"
288                                                            "state postcode<br/>"
289                                                            "country");
290     QTest::newRow("France") << QString::fromLatin1("FRA")
291                             << QString::fromLatin1("postcode")
292                             << QString::fromLatin1("street<br/>"
293                                                    "postcode city<br/>"
294                                                    "country");
295 
296     QTest::newRow("United Kingdom") << QString::fromLatin1("GBR")
297                             << QString::fromLatin1("postcode")
298                             << QString::fromLatin1("street<br/>"
299                                                    "district, city, postcode<br/>"
300                                                    "country");
301     QTest::newRow("Gibraltar") << QString::fromLatin1("GIB")
302                                << QString()
303                                << QString::fromLatin1("street<br/>"
304                                                       "city<br/>"
305                                                       "country");
306     QTest::newRow("Guadeloupe") << QString::fromLatin1("GLP")
307                                 << QString::fromLatin1("postcode")
308                                 << QString::fromLatin1("street<br/>"
309                                                        "postcode city<br/>"
310                                                        "country");
311     QTest::newRow("French Guiana") << QString::fromLatin1("GUF")
312                                    << QString::fromLatin1("postcode")
313                                    << QString::fromLatin1("street<br/>"
314                                                           "postcode city<br/>"
315                                                           "country");
316     QTest::newRow("Hong Kong") << QString::fromLatin1("HKG")
317                                << QString()
318                                << QString::fromLatin1("street<br/>"
319                                                       "district<br/>"
320                                                       "city");
321     QTest::newRow("India") << QString::fromLatin1("IND")
322                            << QString::fromLatin1("postcode")
323                            << QString::fromLatin1("street<br/>"
324                                                   "city postcode state<br/>"
325                                                   "country");
326     QTest::newRow("Indonesia") << QString::fromLatin1("IDN")
327                                << QString::fromLatin1("postcode")
328                                << QString::fromLatin1("street<br/>"
329                                                       "city, postcode<br/>"
330                                                       "country");
331     QTest::newRow("Ireland") << QString::fromLatin1("IRL")
332                              << QString()
333                              << QString::fromLatin1("street<br/>"
334                                                     "district, state<br/>"
335                                                     "country");
336     QTest::newRow("Italy") << QString::fromLatin1("ITA")
337                            << QString::fromLatin1("postcode")
338                            << QString::fromLatin1("street<br/>"
339                                                   "postcode city<br/>"
340                                                   "country");
341     QTest::newRow("Jersey") << QString::fromLatin1("JEY")
342                            << QString::fromLatin1("postcode")
343                            << QString::fromLatin1("street<br/>"
344                                                   "city, postcode<br/>"
345                                                   "country");
346     QTest::newRow("Jordan") << QString::fromLatin1("JOR")
347                             << QString::fromLatin1("postcode")
348                             << QString::fromLatin1("street<br/>"
349                                                    "district city postcode<br/>"
350                                                    "country");
351     QTest::newRow("Kuwait") << QString::fromLatin1("KWT")
352                             << QString::fromLatin1("postcode")
353                             << QString::fromLatin1("street<br/>"
354                                                    "postcode, district, city<br/>"
355                                                    "country");
356     QTest::newRow("Latvia") << QString::fromLatin1("LVA")
357                             << QString::fromLatin1("postcode")
358                             << QString::fromLatin1("street<br/>"
359                                                    "city, postcode<br/>"
360                                                    "country");
361     QTest::newRow("Lebanon") << QString::fromLatin1("LBN")
362                             << QString::fromLatin1("postcode")
363                             << QString::fromLatin1("street<br/>"
364                                                    "district city postcode<br/>"
365                                                    "country");
366     QTest::newRow("Luxembourg") << QString::fromLatin1("LUX")
367                                 << QString::fromLatin1("postcode")
368                                 << QString::fromLatin1("street<br/>"
369                                                        "postcode city<br/>"
370                                                        "country");
371     QTest::newRow("Malta") << QString::fromLatin1("MLT")
372                            << QString::fromLatin1("postcode")
373                            << QString::fromLatin1("street<br/>"
374                                                   "city postcode<br/>"
375                                                   "country");
376     QTest::newRow("Monaco") << QString::fromLatin1("MCO")
377                             << QString::fromLatin1("postcode")
378                             << QString::fromLatin1("street<br/>"
379                                                    "postcode city<br/>"
380                                                    "country");
381     QTest::newRow("Mexico") <<  QString::fromLatin1("MEX")
382                             << QString::fromLatin1("postcode")
383                             << QString::fromLatin1("street<br/>"
384                                                    "district<br/>"
385                                                    "postcode city, state<br/>"
386                                                    "country");
387     QTest::newRow("Martinique") << QString::fromLatin1("MTQ")
388                                 << QString::fromLatin1("postcode")
389                                 << QString::fromLatin1("street<br/>"
390                                                        "postcode, city<br/>"
391                                                        "country");
392     QTest::newRow("Malaysia") << QString::fromLatin1("MYS")
393                                 << QString::fromLatin1("postcode")
394                                 << QString::fromLatin1("street<br/>"
395                                                        "postcode city<br/>"
396                                                        "state<br/>"
397                                                        "country");
398     QTest::newRow("New Zealand") << QString::fromLatin1("NZL")
399                                  << QString::fromLatin1("postcode")
400                                  << QString::fromLatin1("street<br/>"
401                                                         "district city postcode<br/>"
402                                                         "country");
403     QTest::newRow("Oman") << QString::fromLatin1("OMN")
404                           << QString::fromLatin1("postcode")
405                           << QString::fromLatin1("street<br/>"
406                                                  "district, postcode, city, country");
407     QTest::newRow("Puerto Rico") << QString::fromLatin1("PRI")
408                                  << QString::fromLatin1("postcode")
409                                  << QString::fromLatin1("street<br/>"
410                                                         "district, city, state, postcode<br/>"
411                                                         "country");
412     QTest::newRow("Qatar") << QString::fromLatin1("QAT")
413                            << QString()
414                            << QString::fromLatin1("street<br/>"
415                                                   "district city, country");
416     QTest::newRow("Reunion") << QString::fromLatin1("REU")
417                              << QString::fromLatin1("postcode")
418                              << QString::fromLatin1("street<br/>"
419                                                     "postcode city<br/>"
420                                                     "country");
421     QTest::newRow("Russian Federation") << QString::fromLatin1("RUS")
422                              << QString::fromLatin1("postcode")
423                              << QString::fromLatin1("street<br/>"
424                                                     "postcode city<br/>"
425                                                     "country");
426     QTest::newRow("Saudi Arabia") << QString::fromLatin1("SAU")
427                                   << QString::fromLatin1("postcode")
428                                   << QString::fromLatin1("street district<br/>"
429                                                          "city postcode<br/>"
430                                                          "country");
431     QTest::newRow("Singapore") << QString::fromLatin1("SGP")
432                                << QString::fromLatin1("postcode")
433                                << QString::fromLatin1("street<br/>"
434                                                       "city postcode<br/>"
435                                                       "country");
436     QTest::newRow("Marino") << QString::fromLatin1("SMR")
437                             << QString::fromLatin1("postcode")
438                             << QString::fromLatin1("street<br/>"
439                                                    "postcode city<br/>"
440                                                    "country");
441     QTest::newRow("Taiwan") << QString::fromLatin1("TWN")
442                             << QString()
443                             << QString::fromLatin1("street, district, city<br/>"
444                                                    "country");
445     QTest::newRow("Thailand") << QString::fromLatin1("THA")
446                               << QString("postcode")
447                               << QString::fromLatin1("street<br/>"
448                                                      "district, city postcode<br/>"
449                                                      "country");
450     QTest::newRow("Turkey") <<  QString::fromLatin1("TUR")
451                             << QString("postcode")
452                             << QString::fromLatin1("street<br/>"
453                                                    "postcode district, city<br/>"
454                                                    "country");
455     QTest::newRow("Ukraine") << QString::fromLatin1("UKR")
456                             << QString::fromLatin1("postcode")
457                             << QString::fromLatin1("street<br/>"
458                                                    "city postcode<br/>"
459                                                    "country");
460     QTest::newRow("United States") << QString::fromLatin1("USA")
461                                    << QString::fromLatin1("postcode")
462                                    << QString::fromLatin1("street<br/>"
463                                                           "city, state postcode<br/>"
464                                                           "country");
465     QTest::newRow("Virgin Islands, US") << QString::fromLatin1("VIR")
466                                         << QString("postcode")
467                                         << QString::fromLatin1("street<br/>"
468                                                                "city, state postcode<br/>"
469                                                                "country");
470     QTest::newRow("Vatican City State") << QString::fromLatin1("VAT")
471                                         << QString::fromLatin1("postcode")
472                                         << QString::fromLatin1("street<br/>"
473                                                                "postcode city<br/>"
474                                                                "country");
475     QTest::newRow("Venezuela") << QString::fromLatin1("VEN")
476                                << QString::fromLatin1("postcode")
477                                << QString::fromLatin1("street<br/>"
478                                                       "city postcode, state<br/>"
479                                                       "country");
480     QTest::newRow("South Africa") << QString::fromLatin1("ZAF")
481                                << QString()
482                                << QString::fromLatin1("street<br/>"
483                                                       "district, city<br/>"
484                                                       "country");
485     QTest::newRow("Finland") << QString::fromLatin1("FIN")
486                              << QString::fromLatin1("postcode")
487                              << QString::fromLatin1("street<br/>"
488                                                     "postcode city<br/>"
489                                                     "country");
490 }
491 
492 // TODO: curenlty we don't have suite in QGeoAddress
493 //       will need to either remove or enable
494 //void tst_QGeoAddress::suiteTest()
495 //{
496 //    QGeoAddress testObj;
497 //    QVERIFY2(testObj.suite() == QString(), "Wrong default value");
498 //    testObj.setSuite("testText");
499 //    QVERIFY2(testObj.suite() == "testText", "Wrong value returned");
500 //}
501 
operatorsTest()502 void tst_QGeoAddress::operatorsTest()
503 {
504     QGeoAddress testObj;
505     testObj.setStreet("testValue");
506     QGeoAddress testObj2;
507     testObj2 = testObj;
508     QVERIFY2(testObj == testObj2, "Not copied correctly");
509     testObj2.setCountry("testValue2");
510     QVERIFY2(testObj != testObj2, "Object should be different");
511 }
512 
emptyClearTest()513 void tst_QGeoAddress::emptyClearTest()
514 {
515     QGeoAddress testObj;
516     QVERIFY(testObj.isEmpty());
517 
518     testObj.setCountry(QStringLiteral("country"));
519     QVERIFY(!testObj.isEmpty());
520     testObj.clear();
521 
522     testObj.setCountryCode(QStringLiteral("countryCode"));
523     QVERIFY(!testObj.isEmpty());
524     testObj.clear();
525 
526     testObj.setState(QStringLiteral("state"));
527     QVERIFY(!testObj.isEmpty());
528     testObj.clear();
529 
530     testObj.setCounty(QStringLiteral("county"));
531     QVERIFY(!testObj.isEmpty());
532     testObj.clear();
533 
534     testObj.setCity(QStringLiteral("city"));
535     QVERIFY(!testObj.isEmpty());
536     testObj.clear();
537 
538     testObj.setDistrict(QStringLiteral("district"));
539     QVERIFY(!testObj.isEmpty());
540     testObj.clear();
541 
542     testObj.setPostalCode(QStringLiteral("postalCode"));
543     QVERIFY(!testObj.isEmpty());
544     testObj.clear();
545 
546     testObj.setStreet(QStringLiteral("street"));
547     QVERIFY(!testObj.isEmpty());
548     testObj.clear();
549 
550     testObj.setText(QStringLiteral("formatted address"));
551     QVERIFY(!testObj.isEmpty());
552     testObj.clear();
553 
554     QVERIFY(testObj.isEmpty());
555 }
556 
557 QTEST_APPLESS_MAIN(tst_QGeoAddress)
558 
559 #include "tst_qgeoaddress.moc"
560