File indexing completed on 2024-04-21 04:41:06
0001 /* 0002 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KPublicTransport/Backend> 0008 #include <KPublicTransport/BackendModel> 0009 #include <KPublicTransport/CoverageArea> 0010 #include <KPublicTransport/Location> 0011 #include <KPublicTransport/Manager> 0012 0013 #include <QAbstractItemModelTester> 0014 #include <QSignalSpy> 0015 #include <QStandardPaths> 0016 #include <QTest> 0017 0018 using namespace KPublicTransport; 0019 0020 class BackendTest : public QObject 0021 { 0022 Q_OBJECT 0023 private: 0024 Manager m_ptMgr; // for the benchmark 0025 0026 private Q_SLOTS: 0027 void initTestCase() 0028 { 0029 qputenv("TZ", "UTC"); 0030 QStandardPaths::setTestModeEnabled(true); 0031 } 0032 0033 void testCoverageData() 0034 { 0035 Manager mgr; 0036 for (const auto &b : mgr.backends()) { 0037 QVERIFY(!b.identifier().isEmpty()); 0038 QVERIFY(!b.name().isEmpty()); 0039 QVERIFY(!b.coverageArea(CoverageArea::Realtime).isEmpty() || !b.coverageArea(CoverageArea::Regular).isEmpty() || !b.coverageArea(CoverageArea::Any).isEmpty()); 0040 0041 for (const auto &type : {CoverageArea::Realtime, CoverageArea::Regular, CoverageArea::Any}) { 0042 const auto c = b.coverageArea(type); 0043 if (c.isEmpty()) { 0044 continue; 0045 } 0046 0047 QCOMPARE(c.type(), type); 0048 QVERIFY(c.regions().size() > 0); 0049 } 0050 } 0051 } 0052 0053 void testCoverageHitDetection() 0054 { 0055 Manager mgr; 0056 auto b = *std::find_if(mgr.backends().begin(), mgr.backends().end(), [](const auto &b) { return b.identifier() == QLatin1String("de_db"); }); 0057 QCOMPARE(b.identifier(), QLatin1String("de_db")); 0058 auto c = b.coverageArea(CoverageArea::Realtime); 0059 QVERIFY(!c.isEmpty()); 0060 QVERIFY(!c.isGlobal()); 0061 0062 Location l1; 0063 l1.setCoordinate(52.5, 13.0); 0064 QVERIFY(c.coversLocation(l1)); 0065 l1.setCoordinate(13.0, 52.5); 0066 QVERIFY(!c.coversLocation(l1)); 0067 0068 Location l2; 0069 l2.setCountry(QStringLiteral("DE")); 0070 QVERIFY(c.coversLocation(l2)); 0071 l2.setCountry(QStringLiteral("NZ")); 0072 QVERIFY(!c.coversLocation(l2)); 0073 0074 b = *std::find_if(mgr.backends().begin(), mgr.backends().end(), [](const auto &b) { return b.identifier() == QLatin1String("un_gbfs"); }); 0075 QCOMPARE(b.identifier(), QLatin1String("un_gbfs")); 0076 c = b.coverageArea(CoverageArea::Realtime); 0077 QVERIFY(!c.isEmpty()); 0078 QVERIFY(c.isGlobal()); 0079 0080 QVERIFY(c.coversLocation(l1)); 0081 QVERIFY(c.coversLocation(l2)); 0082 } 0083 0084 void testCoverageBenchmark() 0085 { 0086 Location loc; 0087 loc.setCoordinate(52.5, 13.0); 0088 QBENCHMARK { 0089 for (const auto &b : m_ptMgr.backends()) { 0090 for (const auto type : { CoverageArea::Realtime, CoverageArea::Regular, CoverageArea::Any }) { 0091 const auto c = b.coverageArea(type); 0092 c.coversLocation(loc); 0093 } 0094 } 0095 } 0096 } 0097 0098 void testBackendModel_data() 0099 { 0100 QTest::addColumn<BackendModel::Mode>("mode"); 0101 QTest::newRow("flat") << BackendModel::Flat; 0102 QTest::newRow("groupByCountry") << BackendModel::GroupByCountry; 0103 } 0104 0105 void testBackendModel() 0106 { 0107 QFETCH(BackendModel::Mode, mode); 0108 0109 BackendModel model; 0110 QAbstractItemModelTester modelTest(&model); 0111 0112 Manager mgr; 0113 model.setManager(&mgr); 0114 model.setMode(mode); 0115 QVERIFY(model.rowCount() > 20); 0116 QSignalSpy dataChangedSpy(&model, &QAbstractItemModel::dataChanged); 0117 0118 mgr.setAllowInsecureBackends(false); 0119 for (auto i = 0; i < model.rowCount(); ++i) { 0120 const auto idx = model.index(i, 0); 0121 QVERIFY(!idx.data(BackendModel::IdentifierRole).toString().isEmpty()); 0122 QCOMPARE(idx.data(BackendModel::SecureRole).userType(), QMetaType::Bool); 0123 QVERIFY(!idx.data(BackendModel::NameRole).toString().isEmpty()); 0124 QVERIFY(!idx.data(BackendModel::DescriptionRole).toString().isEmpty()); 0125 QCOMPARE(idx.data(BackendModel::SecureRole), idx.data(BackendModel::ItemEnabledRole)); 0126 if (!idx.data(BackendModel::SecureRole).toBool()) { 0127 QVERIFY(!idx.data(BackendModel::BackendEnabledRole).toBool()); 0128 } 0129 QVERIFY(!idx.data(BackendModel::CountryCodeRole).toString().isEmpty()); 0130 } 0131 0132 QCOMPARE(dataChangedSpy.size(), 0); 0133 mgr.setAllowInsecureBackends(true); 0134 QCOMPARE(dataChangedSpy.size(), 1); 0135 for (auto i = 0; i < model.rowCount(); ++i) { 0136 const auto idx = model.index(i, 0); 0137 QVERIFY(idx.data(BackendModel::ItemEnabledRole).toBool()); 0138 QVERIFY(idx.data(BackendModel::BackendEnabledRole).toBool()); 0139 } 0140 0141 QCOMPARE(mgr.isBackendEnabled(QStringLiteral("un_navitia")), true); 0142 QCOMPARE(mgr.isBackendEnabled(QStringLiteral("fr_sncf")), true); 0143 bool navitiaToggled = false; 0144 bool sncfToggled = false; 0145 for (auto i = 0; i < model.rowCount(); ++i) { 0146 const auto idx = model.index(i, 0); 0147 if (idx.data(BackendModel::IdentifierRole).toString() == QLatin1String("un_navitia") && !navitiaToggled) { 0148 model.setData(idx, Qt::Unchecked, Qt::CheckStateRole); 0149 model.setData(idx, true, BackendModel::BackendEnabledRole); 0150 navitiaToggled = true; 0151 0152 QCOMPARE(idx.data(Qt::CheckStateRole).toInt(), Qt::Checked); 0153 QCOMPARE(idx.data(BackendModel::BackendEnabledRole).toBool(), true); 0154 } 0155 if (idx.data(BackendModel::IdentifierRole).toString() == QLatin1String("fr_sncf") && !sncfToggled) { 0156 model.setData(idx, false, BackendModel::BackendEnabledRole); 0157 sncfToggled = true; 0158 0159 QCOMPARE(idx.data(Qt::CheckStateRole).toInt(), Qt::Unchecked); 0160 QCOMPARE(idx.data(BackendModel::BackendEnabledRole).toBool(), false); 0161 } 0162 } 0163 QVERIFY(navitiaToggled); 0164 QVERIFY(sncfToggled); 0165 QCOMPARE(dataChangedSpy.size(), 4); 0166 QCOMPARE(mgr.isBackendEnabled(QStringLiteral("un_navitia")), true); 0167 QCOMPARE(mgr.isBackendEnabled(QStringLiteral("fr_sncf")), false); 0168 0169 QCOMPARE(mgr.enabledBackends(), QStringList(QStringLiteral("un_navitia"))); 0170 QCOMPARE(mgr.disabledBackends(), QStringList(QStringLiteral("fr_sncf"))); 0171 mgr.setBackendEnabled(QStringLiteral("un_navitia"), true); 0172 mgr.setBackendEnabled(QStringLiteral("fr_sncf"), false); 0173 QCOMPARE(mgr.enabledBackends(), QStringList(QStringLiteral("un_navitia"))); 0174 QCOMPARE(mgr.disabledBackends(), QStringList(QStringLiteral("fr_sncf"))); 0175 mgr.setBackendEnabled(QStringLiteral("un_navitia"), false); 0176 mgr.setBackendEnabled(QStringLiteral("fr_sncf"), true); 0177 QCOMPARE(mgr.disabledBackends(), QStringList(QStringLiteral("un_navitia"))); 0178 QCOMPARE(mgr.enabledBackends(), QStringList(QStringLiteral("fr_sncf"))); 0179 } 0180 }; 0181 0182 QTEST_GUILESS_MAIN(BackendTest) 0183 0184 #include "backendtest.moc"