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

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "../src/map-quick/amenitymodel.h"
0007 #include "../src/map-quick/amenitysortfilterproxymodel.h"
0008 
0009 #include <KOSMIndoorMap/MapData>
0010 
0011 #include <osm/io.h>
0012 
0013 #include <QAbstractItemModelTester>
0014 #include <QFile>
0015 #include <QTest>
0016 #include <QtPlugin>
0017 
0018 Q_IMPORT_PLUGIN(OSM_XmlIOPlugin)
0019 
0020 using namespace KOSMIndoorMap;
0021 
0022 void initLocale()
0023 {
0024     qputenv("LC_ALL", "en_US.utf-8");
0025     qputenv("TZ", "UTC");
0026 
0027     Q_INIT_RESOURCE(assets);
0028 }
0029 
0030 Q_CONSTRUCTOR_FUNCTION(initLocale)
0031 
0032 class AmenityModelTest : public QObject
0033 {
0034     Q_OBJECT
0035 private Q_SLOTS:
0036     void testModel()
0037     {
0038         // verify the locale matches what the test data expects
0039         // this is a workaround for test failures on OBS
0040         if (QLocale().createSeparatedList({QStringLiteral("A"), QStringLiteral("B")}) != QLatin1String("A and B")) {
0041             QSKIP("locale doesn't behave as expected!");
0042         }
0043 
0044         const QString osmFile = QStringLiteral(SOURCE_DIR "/data/amenitymodel/amenitymodeltest.osm");
0045         QFile inFile(osmFile);
0046         QVERIFY(inFile.open(QFile::ReadOnly));
0047 
0048         OSM::DataSet dataSet;
0049         auto p = OSM::IO::readerForFileName(osmFile, &dataSet);
0050         p->read(&inFile);
0051         QVERIFY(!dataSet.nodes.empty());
0052 
0053         MapData mapData;
0054         mapData.setDataSet(std::move(dataSet));
0055         QVERIFY(!mapData.isEmpty());
0056 
0057         AmenityModel model;
0058         QAbstractItemModelTester modelTest(&model);
0059 
0060         AmenitySortFilterProxyModel proxyModel;
0061         QAbstractItemModelTester proxyModelTest(&proxyModel);
0062 
0063         model.setMapData(mapData);
0064 
0065         QCOMPARE(model.rowCount(), 4);
0066 
0067         auto idx = model.index(0, 0);
0068         QCOMPARE(idx.data(AmenityModel::NameRole).toString(), QLatin1String("Burger Place"));
0069         QCOMPARE(idx.data(AmenityModel::TypeNameRole).toString(), QLatin1String("Fast Food"));
0070         QCOMPARE(idx.data(AmenityModel::CuisineRole).toString(), QLatin1String("Burger and Chicken Wings"));
0071         QCOMPARE(idx.data(AmenityModel::GroupRole).toInt(), AmenityModel::FoodGroup);
0072         QVERIFY(idx.data(AmenityModel::IconSourceRole).toString().contains(QLatin1String("fast_food.svg")));
0073 
0074         idx = model.index(1, 0);
0075         QCOMPARE(idx.data(AmenityModel::NameRole).toString(), QLatin1String("My Supermarket"));
0076         QCOMPARE(idx.data(AmenityModel::TypeNameRole).toString(), QLatin1String("Supermarket"));
0077         QVERIFY(idx.data(AmenityModel::CuisineRole).toString().isEmpty());
0078         QCOMPARE(idx.data(AmenityModel::GroupRole).toInt(), AmenityModel::ShopGroup);
0079         QVERIFY(!idx.data(AmenityModel::OpeningHoursRole).toString().isEmpty());
0080         QVERIFY(idx.data(AmenityModel::IconSourceRole).toString().contains(QLatin1String("supermarket.svg")));
0081 
0082         idx = model.index(2, 0);
0083         QVERIFY(idx.data(AmenityModel::NameRole).toString().isEmpty());
0084         QCOMPARE(idx.data(AmenityModel::TypeNameRole).toString(), QLatin1String("Toilets"));
0085         QVERIFY(idx.data(AmenityModel::CuisineRole).toString().isEmpty());
0086         QCOMPARE(idx.data(AmenityModel::GroupRole).toInt(), AmenityModel::ToiletGroup);
0087 
0088         idx = model.index(3, 0);
0089         QVERIFY(idx.data(AmenityModel::NameRole).toString().isEmpty());
0090         QCOMPARE(idx.data(AmenityModel::FallbackNameRole).toString(), QLatin1String("Some Company"));
0091         QCOMPARE(idx.data(AmenityModel::TypeNameRole).toString(), QLatin1String("Car Rental"));
0092         QVERIFY(idx.data(AmenityModel::CuisineRole).toString().isEmpty());
0093         QCOMPARE(idx.data(AmenityModel::GroupRole).toInt(), AmenityModel::AmenityGroup);
0094     }
0095 
0096     void testProxyModel()
0097     {
0098         const QString osmFile = QStringLiteral(SOURCE_DIR "/data/amenitymodel/amenitymodeltest.osm");
0099         QFile inFile(osmFile);
0100         QVERIFY(inFile.open(QFile::ReadOnly));
0101 
0102         OSM::DataSet dataSet;
0103         auto p = OSM::IO::readerForFileName(osmFile, &dataSet);
0104         p->read(&inFile);
0105         QVERIFY(!dataSet.nodes.empty());
0106 
0107         MapData mapData;
0108         mapData.setDataSet(std::move(dataSet));
0109         QVERIFY(!mapData.isEmpty());
0110 
0111         AmenityModel model;
0112         QAbstractItemModelTester modelTest(&model);
0113 
0114         AmenitySortFilterProxyModel proxyModel;
0115         proxyModel.setSourceModel(&model);
0116         QAbstractItemModelTester proxyModelTest(&proxyModel);
0117 
0118         model.setMapData(mapData);
0119         QCOMPARE(proxyModel.rowCount(), 4);
0120 
0121         auto idx = model.index(0, 0);
0122         QCOMPARE(idx.data(AmenityModel::GroupRole).toInt(), AmenityModel::FoodGroup);
0123 
0124         proxyModel.setProperty("filterString", QLatin1String("burger"));
0125         QCOMPARE(proxyModel.rowCount(), 1);
0126         proxyModel.setProperty("filterString", QLatin1String("lounge"));
0127         QCOMPARE(proxyModel.rowCount(), 0);
0128         proxyModel.setProperty("filterString", QLatin1String("wings"));
0129         QCOMPARE(proxyModel.rowCount(), 1);
0130         proxyModel.setProperty("filterString", QLatin1String("food"));
0131         QCOMPARE(proxyModel.rowCount(), 1);
0132         proxyModel.setProperty("filterString", QLatin1String("toilet"));
0133         QCOMPARE(proxyModel.rowCount(), 1);
0134         proxyModel.setProperty("filterString", QLatin1String("market"));
0135         QCOMPARE(proxyModel.rowCount(), 1);
0136         proxyModel.setProperty("filterString", QLatin1String("company"));
0137         QCOMPARE(proxyModel.rowCount(), 1);
0138         proxyModel.setProperty("filterString", QString());
0139         QCOMPARE(proxyModel.rowCount(), 4);
0140     }
0141 };
0142 
0143 QTEST_GUILESS_MAIN(AmenityModelTest)
0144 
0145 #include "amenitymodeltest.moc"