File indexing completed on 2024-05-12 05:17:31
0001 /* 0002 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KItinerary/LocationUtil> 0008 #include <KItinerary/Place> 0009 0010 #include <QObject> 0011 #include <QTest> 0012 0013 #define _(x) QStringLiteral(x) 0014 0015 using namespace KItinerary; 0016 0017 class LocationUtilTest : public QObject 0018 { 0019 Q_OBJECT 0020 private Q_SLOTS: 0021 void testLocationCompare() 0022 { 0023 Airport txlCoord; 0024 txlCoord.setGeo({52.5f, 13.28f}); 0025 Airport sxfCoord; 0026 sxfCoord.setGeo({52.3f, 13.52f}); 0027 0028 QVERIFY(LocationUtil::isSameLocation(txlCoord, sxfCoord, LocationUtil::CityLevel)); 0029 QVERIFY(LocationUtil::isSameLocation(sxfCoord, txlCoord, LocationUtil::CityLevel)); 0030 QVERIFY(LocationUtil::isSameLocation(txlCoord, txlCoord, LocationUtil::CityLevel)); 0031 QVERIFY(!LocationUtil::isSameLocation(txlCoord, {}, LocationUtil::CityLevel)); 0032 QVERIFY(!LocationUtil::isSameLocation({}, sxfCoord, LocationUtil::CityLevel)); 0033 0034 QVERIFY(!LocationUtil::isSameLocation(sxfCoord, txlCoord, LocationUtil::Exact)); 0035 QVERIFY(LocationUtil::isSameLocation(txlCoord, txlCoord, LocationUtil::Exact)); 0036 QVERIFY(!LocationUtil::isSameLocation(txlCoord, {}, LocationUtil::Exact)); 0037 QVERIFY(!LocationUtil::isSameLocation({}, sxfCoord, LocationUtil::Exact)); 0038 0039 PostalAddress addr; 0040 addr.setAddressCountry(_("DE")); 0041 addr.setAddressLocality(_("Berlin")); 0042 Airport txlAddress; 0043 txlAddress.setAddress(addr); 0044 Airport sxfAddress; 0045 addr.setAddressLocality(_("BERLIN")); 0046 sxfAddress.setAddress(addr); 0047 0048 QVERIFY(LocationUtil::isSameLocation(txlAddress, sxfAddress, LocationUtil::CityLevel)); 0049 QVERIFY(LocationUtil::isSameLocation(sxfAddress, txlAddress, LocationUtil::CityLevel)); 0050 QVERIFY(LocationUtil::isSameLocation(sxfAddress, sxfAddress, LocationUtil::CityLevel)); 0051 QVERIFY(!LocationUtil::isSameLocation(txlAddress, {}, LocationUtil::CityLevel)); 0052 QVERIFY(!LocationUtil::isSameLocation({}, sxfAddress, LocationUtil::CityLevel)); 0053 0054 QVERIFY(!LocationUtil::isSameLocation(txlAddress, sxfAddress, LocationUtil::Exact)); 0055 QVERIFY(!LocationUtil::isSameLocation(txlAddress, {}, LocationUtil::Exact)); 0056 QVERIFY(!LocationUtil::isSameLocation({}, sxfAddress, LocationUtil::Exact)); 0057 0058 Airport txlIata; 0059 txlIata.setIataCode(_("TXL")); 0060 QVERIFY(LocationUtil::isSameLocation(txlIata, txlIata, LocationUtil::CityLevel)); 0061 QVERIFY(LocationUtil::isSameLocation(txlIata, txlIata, LocationUtil::Exact)); 0062 QVERIFY(!LocationUtil::isSameLocation({}, txlIata, LocationUtil::CityLevel)); 0063 QVERIFY(!LocationUtil::isSameLocation(txlIata, {}, LocationUtil::Exact)); 0064 0065 Place txlName; 0066 txlName.setName(_("Berlin Tegel Airpot")); 0067 QVERIFY(LocationUtil::isSameLocation(txlName, txlName, LocationUtil::CityLevel)); 0068 QVERIFY(LocationUtil::isSameLocation(txlName, txlName, LocationUtil::Exact)); 0069 QVERIFY(!LocationUtil::isSameLocation({}, txlName, LocationUtil::CityLevel)); 0070 QVERIFY(!LocationUtil::isSameLocation(txlName, {}, LocationUtil::Exact)); 0071 0072 Airport txl; 0073 txl.setGeo(txlCoord.geo()); 0074 txl.setIataCode(_("TXL")); 0075 txl.setName(_("Berlin Tegel Airport")); 0076 Airport sxf; 0077 sxf.setGeo(sxfCoord.geo()); 0078 sxf.setIataCode(_("SXF")); 0079 sxf.setName(_("Berlin Schönefeld Airport")); 0080 QVERIFY(LocationUtil::isSameLocation(txl, sxf, LocationUtil::CityLevel)); 0081 QVERIFY(!LocationUtil::isSameLocation(txl, sxf, LocationUtil::Exact)); 0082 QVERIFY(!LocationUtil::isSameLocation(txl, {}, LocationUtil::CityLevel)); 0083 QVERIFY(!LocationUtil::isSameLocation({}, sxf, LocationUtil::Exact)); 0084 0085 0086 GeoCoordinates mnhCoord({49.47922, 8.46941}); 0087 GeoCoordinates lwhCoord({49.47716, 8.43406}); 0088 TrainStation mnh; 0089 mnh.setGeo(mnhCoord); 0090 TrainStation lwh; 0091 lwh.setGeo(lwhCoord); 0092 QVERIFY(LocationUtil::isSameLocation(mnh, lwh, LocationUtil::CityLevel)); // wrong, but we have no other information 0093 mnh.setName(_("Mannheim Hbf")); 0094 lwh.setName(_("Ludwigshafen (Rhein) Hauptbahnhof")); 0095 QVERIFY(!LocationUtil::isSameLocation(mnh, lwh, LocationUtil::CityLevel)); 0096 lwh.setName(_("Mannheim (Rhein) West")); 0097 QVERIFY(LocationUtil::isSameLocation(mnh, lwh, LocationUtil::CityLevel)); 0098 } 0099 0100 void testLocationNameCompare_data() 0101 { 0102 QTest::addColumn<QString>("lhsName"); 0103 QTest::addColumn<QString>("rhsName"); 0104 QTest::addColumn<bool>("cityEqual"); 0105 QTest::addColumn<bool>("exactEqual"); 0106 0107 QTest::newRow("empty") << QString() << QString() << false << false; 0108 QTest::newRow("equal") << QStringLiteral("Berlin") << QStringLiteral("Berlin") << true << true; 0109 QTest::newRow("not equal") << QStringLiteral("Nürnberg") << QStringLiteral("Valencia") << false << false; 0110 QTest::newRow("case-insensitive") << QStringLiteral("Berlin") << QStringLiteral("BERLiN") << true << true; 0111 QTest::newRow("diacritic") << QStringLiteral("Düsseldorf") << QStringLiteral("Dusseldorf") << true << true; 0112 QTest::newRow("diacritic 2") << QStringLiteral("Rīga") << QStringLiteral("Riga") << true << true; 0113 QTest::newRow("diacritic case-insensitive") << QStringLiteral("København H") << QStringLiteral("KOEBENHAVN H") << true << true; 0114 QTest::newRow("diacritic-transliteration-1") << QStringLiteral("Zürich") << QStringLiteral("ZUERICH") << true << true; 0115 QTest::newRow("diacritic-transliteration-2") << QStringLiteral("Győr") << QStringLiteral("GYOER") << true << true; 0116 0117 QTest::newRow("random-suffix-1") << QStringLiteral("Berlin") << QStringLiteral("Berlin (tief)") << true << false; 0118 QTest::newRow("random-suffix-2") << QStringLiteral("München Hbf") << QStringLiteral("München Hbf Gl.27-36") << true << false; 0119 QTest::newRow("random-suffix-3") << QStringLiteral("Berlin") << QStringLiteral("Berlin+City") << true << false; 0120 QTest::newRow("random-suffix-and-diacritic") << QStringLiteral("München Hbf") << QStringLiteral("MUNCHEN") << true << false; 0121 0122 QTest::newRow("space-insensitive") << QStringLiteral("Frankfurt(Main)Hbf") << QStringLiteral("Frankfurt (Main) Hbf") << true << true; 0123 } 0124 0125 void testLocationNameCompare() 0126 { 0127 QFETCH(QString, lhsName); 0128 QFETCH(QString, rhsName); 0129 QFETCH(bool, cityEqual); 0130 QFETCH(bool, exactEqual); 0131 0132 Place lhs; 0133 lhs.setName(lhsName); 0134 Place rhs; 0135 rhs.setName(rhsName); 0136 QCOMPARE(LocationUtil::isSameLocation(lhs, rhs, LocationUtil::CityLevel), cityEqual); 0137 QCOMPARE(LocationUtil::isSameLocation(rhs, lhs, LocationUtil::CityLevel), cityEqual); 0138 QCOMPARE(LocationUtil::isSameLocation(lhs, rhs, LocationUtil::Exact), exactEqual); 0139 QCOMPARE(LocationUtil::isSameLocation(rhs, lhs, LocationUtil::Exact), exactEqual); 0140 } 0141 0142 void testGeoUri() 0143 { 0144 Place p; 0145 QVERIFY(LocationUtil::geoUri(p).isEmpty()); 0146 0147 GeoCoordinates coord(45.5137, 9.21139); 0148 p.setGeo(coord); 0149 QCOMPARE(LocationUtil::geoUri(p), QUrl(QStringLiteral("geo:45.5137,9.21139"))); 0150 QCOMPARE(p.geoUri(), QUrl(QStringLiteral("geo:45.5137,9.21139"))); 0151 0152 PostalAddress addr; 0153 addr.setStreetAddress(QStringLiteral("Piazza della Scienza")); 0154 addr.setAddressLocality(QStringLiteral("Milan")); 0155 addr.setAddressCountry(QStringLiteral("IT")); 0156 p.setAddress(addr); 0157 QCOMPARE(LocationUtil::geoUri(p), QUrl(QStringLiteral("geo:45.5137,9.21139"))); 0158 0159 p.setGeo({}); 0160 QCOMPARE(LocationUtil::geoUri(p), QUrl(QStringLiteral("geo:0,0?q=Piazza della Scienza,MILAN,IT"))); 0161 } 0162 }; 0163 0164 QTEST_APPLESS_MAIN(LocationUtilTest) 0165 0166 #include "locationutiltest.moc"