File indexing completed on 2024-04-28 04:40:49

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "../src/map-quick/osmelementinformationmodel.h"
0008 
0009 #include <osm/io.h>
0010 
0011 #include <QAbstractItemModelTester>
0012 #include <QDir>
0013 #include <QFile>
0014 #include <QJsonArray>
0015 #include <QJsonDocument>
0016 #include <QJsonObject>
0017 #include <QProcess>
0018 #include <QTest>
0019 #include <QtPlugin>
0020 
0021 Q_IMPORT_PLUGIN(OSM_XmlIOPlugin)
0022 
0023 using namespace KOSMIndoorMap;
0024 
0025 void initLocale()
0026 {
0027     qputenv("LC_ALL", "en_US.utf-8");
0028     qputenv("TZ", "UTC");
0029 }
0030 
0031 Q_CONSTRUCTOR_FUNCTION(initLocale)
0032 
0033 class OSMElementInfoModelTest : public QObject
0034 {
0035     Q_OBJECT
0036 private Q_SLOTS:
0037     void testModel_data()
0038     {
0039         QTest::addColumn<QString>("osmFile");
0040         QTest::addColumn<QString>("modelFile");
0041 
0042         QDir dir(QStringLiteral(SOURCE_DIR "/data/osminfomodel/"));
0043         const auto lst = dir.entryList(QStringList(QStringLiteral("*.xml")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0044         for (const auto &file : lst) {
0045             const auto base = file.left(file.size() - 4);
0046             QTest::newRow(base.toLatin1().constData()) << (dir.path() + QLatin1Char('/') + file) << (dir.path() + QLatin1Char('/') + base + QLatin1String(".json"));
0047         }
0048     }
0049 
0050     void testModel()
0051     {
0052         // verify the locale matches what the test data expects
0053         // this is a workaround for test failures on OBS
0054         if (QLocale().createSeparatedList({QStringLiteral("A"), QStringLiteral("B")}) != QLatin1String("A and B")) {
0055             QSKIP("locale doesn't behave as expected!");
0056         }
0057 
0058         QFETCH(QString, osmFile);
0059         QFETCH(QString, modelFile);
0060 
0061         QFile inFile(osmFile);
0062         QVERIFY(inFile.open(QFile::ReadOnly));
0063 
0064         OSM::DataSet dataSet;
0065         auto p = OSM::IO::readerForFileName(osmFile, &dataSet);
0066         p->read(&inFile);
0067         QCOMPARE(dataSet.nodes.size(), 1);
0068 
0069         OSMElementInformationModel model;
0070         QAbstractItemModelTester modelTest(&model);
0071         model.setElement(OSMElement(OSM::Element(&dataSet.nodes[0])));
0072 
0073         QJsonObject top;
0074         top.insert(QStringLiteral("name"), model.name());
0075         if (!model.category().isEmpty()) {
0076             top.insert(QStringLiteral("category"), model.category());
0077         }
0078         QJsonArray modelContent;
0079         for (int row = 0; row < model.rowCount(); ++row) {
0080             const auto idx = model.index(row);
0081             QJsonObject modelRow;
0082             for (auto role : {OSMElementInformationModel::KeyLabelRole, OSMElementInformationModel::ValueRole, OSMElementInformationModel::ValueUrlRole, OSMElementInformationModel::CategoryLabelRole}) {
0083                 if (!idx.data(role).toString().isEmpty()) {
0084                     modelRow.insert(QString::fromUtf8(model.roleNames().value(role)), idx.data(role).toString());
0085                 }
0086             }
0087             modelContent.push_back(modelRow);
0088         }
0089         top.insert(QStringLiteral("content"), modelContent);
0090 
0091         model.clear();
0092         QCOMPARE(model.rowCount(), 0);
0093 
0094         QFile refFile(modelFile);
0095         QVERIFY(refFile.open(QFile::ReadOnly));
0096         const auto refContent = QJsonDocument::fromJson(refFile.readAll()).object();
0097         if (top != refContent) {
0098             QFile failFile(modelFile + QLatin1String(".fail"));
0099             QVERIFY(failFile.open(QFile::WriteOnly));
0100             failFile.write(QJsonDocument(top).toJson());
0101             failFile.close();
0102 
0103             QProcess proc;
0104             proc.setProcessChannelMode(QProcess::ForwardedChannels);
0105             proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), modelFile, failFile.fileName()});
0106             QVERIFY(proc.waitForFinished());
0107         }
0108         QCOMPARE(top, refContent);
0109     }
0110 };
0111 
0112 QTEST_GUILESS_MAIN(OSMElementInfoModelTest)
0113 
0114 #include "osmelementinfomodeltest.moc"