File indexing completed on 2024-04-14 04:35:57

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <map/loader/mapdata.h>
0008 #include <map/loader/maploader.h>
0009 #include <map/content/platformmodel.h>
0010 
0011 #include <QTest>
0012 #include <QAbstractItemModelTester>
0013 #include <QSignalSpy>
0014 #include <QtPlugin>
0015 
0016 Q_IMPORT_PLUGIN(OSM_XmlIOPlugin)
0017 
0018 using namespace KOSMIndoorMap;
0019 
0020 class PlatformModelTest : public QObject
0021 {
0022     Q_OBJECT
0023 private Q_SLOTS:
0024     void testPlatformModel()
0025     {
0026         MapLoader loader;
0027         loader.loadFromFile(QStringLiteral(SOURCE_DIR "/data/platforms/hamburg-altona.osm"));
0028         QCOMPARE(loader.isLoading(), false);
0029         QCOMPARE(loader.hasError(), false);
0030 
0031         auto mapData = loader.takeData();
0032         QVERIFY(!mapData.dataSet().nodes.empty());
0033         QVERIFY(!mapData.dataSet().ways.empty());
0034         QVERIFY(!mapData.dataSet().relations.empty());
0035         QVERIFY(mapData.boundingBox().height() > 0);
0036         QVERIFY(mapData.boundingBox().width() > 0);
0037 
0038         PlatformModel model;
0039         QAbstractItemModelTester modelTest(&model);
0040         QSignalSpy platformChangeSpy(&model, &PlatformModel::platformIndexChanged);
0041 
0042         model.setMapData(mapData);
0043         Platform p;
0044         p.setMode(Platform::Rail);
0045         p.setName(QStringLiteral("10"));
0046         model.setArrivalPlatform(p);
0047         p.setName(QStringLiteral("7"));
0048         model.setDeparturePlatform(p);
0049         QVERIFY(platformChangeSpy.wait());
0050         QCOMPARE(model.rowCount(), 12);
0051 
0052         for (int i = 0; i < model.rowCount(); ++i) {
0053             const auto idx = model.index(i, 0);
0054             QVERIFY(!idx.data(Qt::DisplayRole).toString().isEmpty());
0055             QVERIFY(idx.data(PlatformModel::ElementRole).value<OSM::Element>().type() != OSM::Type::Null);
0056 
0057             const auto secCount = model.rowCount(idx);
0058             QVERIFY(secCount > 0 || !(idx.data(PlatformModel::DeparturePlatformRole).toBool() || idx.data(PlatformModel::ArrivalPlatformRole).toBool()));
0059             for (int j = 0; j < secCount; ++j) {
0060                 const auto secIdx = model.index(j, 0, idx);
0061                 QVERIFY(!secIdx.data(Qt::DisplayRole).toString().isEmpty());
0062                 const auto secElem = secIdx.data(PlatformModel::ElementRole).value<OSM::Element>();
0063                 QVERIFY(secElem.type() != OSM::Type::Null);
0064                 QVERIFY(secElem.tagValue("mx:arrival").isEmpty() || secElem.tagValue("mx:arrival") == "0");
0065                 QVERIFY(secElem.tagValue("mx:departure").isEmpty() || secElem.tagValue("mx:departure") == "0");
0066             }
0067         }
0068 
0069         QVERIFY(model.departurePlatformRow() >= 0);
0070         QVERIFY(model.arrivalPlatformRow() >= 0);
0071 
0072         // fuzzy platform matching
0073         p.setName(QStringLiteral("10 D-F"));
0074         model.setArrivalPlatform(p);
0075         p.setName(QStringLiteral("9A-C"));
0076         model.setDeparturePlatform(p);
0077         QVERIFY(platformChangeSpy.wait());
0078 
0079         QVERIFY(model.departurePlatformRow() >= 0);
0080         auto idx = model.index(model.departurePlatformRow(), 0);
0081         QVERIFY(idx.isValid());
0082         QCOMPARE(model.rowCount(idx), 5);
0083         for (int i = 0; i < model.rowCount(idx); ++i) {
0084             const auto secElem = model.index(i, 0, idx).data(PlatformModel::ElementRole).value<OSM::Element>();
0085             QVERIFY(secElem.type() != OSM::Type::Null);
0086             QCOMPARE(secElem.tagValue("mx:departure"), i < 3 ? "1" : "0");
0087         }
0088 
0089         QVERIFY(model.arrivalPlatformRow() >= 0);
0090         idx = model.index(model.arrivalPlatformRow(), 0);
0091         QVERIFY(idx.isValid());
0092         QCOMPARE(model.rowCount(idx), 5);
0093         for (int i = 0; i < model.rowCount(idx); ++i) {
0094             const auto secElem = model.index(i, 0, idx).data(PlatformModel::ElementRole).value<OSM::Element>();
0095             QVERIFY(secElem.type() != OSM::Type::Null);
0096             QCOMPARE(secElem.tagValue("mx:arrival"), i >= 3 ? "1" : "0");
0097         }
0098 
0099         // non-matching platforms
0100         p.setName(QStringLiteral("13"));
0101         model.setArrivalPlatform(p);
0102         p.setName(QStringLiteral("14 A-D"));
0103         model.setDeparturePlatform(p);
0104         QVERIFY(platformChangeSpy.wait());
0105         QCOMPARE(model.departurePlatformRow(), -1);
0106         QCOMPARE(model.arrivalPlatformRow(), -1);
0107     }
0108 };
0109 
0110 QTEST_GUILESS_MAIN(PlatformModelTest)
0111 
0112 #include "platformmodeltest.moc"