File indexing completed on 2024-05-12 05:13:45
0001 /* 0002 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org> 0003 SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #include "testhelper.h" 0007 0008 #include "journeysectionmodel.h" 0009 0010 #include <KPublicTransport/Stopover> 0011 0012 #include <QAbstractItemModelTester> 0013 #include <QJsonDocument> 0014 #include <QJsonObject> 0015 #include <QTest> 0016 #include <QTimeZone> 0017 0018 class JourneySectionModelTest : public QObject 0019 { 0020 Q_OBJECT 0021 private Q_SLOTS: 0022 void testJourneySectionModel() 0023 { 0024 JourneySectionModel model; 0025 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {10, 00}, QTimeZone("Europe/Berlin"))); 0026 QAbstractItemModelTester modelTester(&model); 0027 0028 const auto json = QJsonDocument::fromJson(Test::readFile(QLatin1StringView(SOURCE_DIR "/data/publictransport/db-wifi-journey.json"))).object(); 0029 const auto jny = KPublicTransport::Journey::fromJson(json); 0030 QVERIFY(!jny.sections().empty()); 0031 model.setJourneySection(jny.sections()[0]); 0032 QCOMPARE(model.rowCount(), 11); 0033 0034 for (auto i = 0; i < model.rowCount(); ++i) { 0035 auto idx = model.index(i, 0); 0036 model.setData(idx, 10.0f, JourneySectionModel::LeadingSegmentLengthRole); 0037 model.setData(idx, 20.0f, JourneySectionModel::TrailingSegmentLengthtRole); 0038 } 0039 0040 QCOMPARE(model.departureTrailingProgress(), 0.0f); 0041 QCOMPARE(model.departed(), false); 0042 QCOMPARE(model.arrivalLeadingProgress(), 0.0f); 0043 QCOMPARE(model.arrived(), false); 0044 for (auto i = 0; i < model.rowCount(); ++i) { 0045 auto idx = model.index(i, 0); 0046 QCOMPARE(model.data(idx, JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.0f); 0047 QCOMPARE(model.data(idx, JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0048 QCOMPARE(model.data(idx, JourneySectionModel::StopoverPassedRole).toBool(), false); 0049 } 0050 0051 model.setProperty("showProgress", true); 0052 auto idx = model.index(3, 0); 0053 QCOMPARE(idx.data(JourneySectionModel::StopoverRole).value<KPublicTransport::Stopover>().stopPoint().name(), QLatin1StringView("Hannover Hbf")); 0054 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0055 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0056 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.826531f); 0057 0058 idx = model.index(4, 0); 0059 QCOMPARE(idx.data(JourneySectionModel::StopoverRole).value<KPublicTransport::Stopover>().stopPoint().name(), QLatin1StringView("Bielefeld Hbf")); 0060 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), false); 0061 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.0f); 0062 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0063 0064 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {10, 15}, QTimeZone("Europe/Berlin"))); 0065 idx = model.index(3, 0); 0066 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0067 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0068 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 1.0f); 0069 0070 idx = model.index(4, 0); 0071 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), false); 0072 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.571429f); 0073 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0074 0075 model.setProperty("departureTrailingSegmentLength", 30.0f); 0076 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {7, 15}, QTimeZone("Europe/Berlin"))); // not departed yet 0077 idx = model.index(0, 0); 0078 QCOMPARE(model.departureTrailingProgress(), 0.0f); 0079 QCOMPARE(model.departed(), false); 0080 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), false); 0081 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.0f); 0082 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0083 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {7, 37}, QTimeZone("Europe/Berlin"))); // departed 0084 QCOMPARE(model.departureTrailingProgress(), 0.2222222f); 0085 QCOMPARE(model.departed(), true); 0086 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), false); 0087 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.0f); 0088 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0089 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {7, 41}, QTimeZone("Europe/Berlin"))); // close to arrival 0090 QCOMPARE(model.departureTrailingProgress(), 1.0f); 0091 QCOMPARE(model.departed(), true); 0092 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), false); 0093 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.333333f); 0094 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0095 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {7, 42}, QTimeZone("Europe/Berlin"))); // arrived 0096 QCOMPARE(model.departureTrailingProgress(), 1.0f); 0097 QCOMPARE(model.departed(), true); 0098 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0099 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0100 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0101 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {7, 46}, QTimeZone("Europe/Berlin"))); // departing 0102 QCOMPARE(model.departureTrailingProgress(), 1.0f); 0103 QCOMPARE(model.departed(), true); 0104 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0105 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0106 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0107 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {7, 50}, QTimeZone("Europe/Berlin"))); // departed 0108 QCOMPARE(model.departureTrailingProgress(), 1.0f); 0109 QCOMPARE(model.departed(), true); 0110 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0111 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0112 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.375f); 0113 0114 model.setProperty("arrivalLeadingSegmentLength", 40.0f); 0115 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {12, 0}, QTimeZone("Europe/Berlin"))); // arriving 0116 idx = model.index(10, 0); 0117 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), false); 0118 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 0.25f); 0119 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0120 QCOMPARE(model.arrivalLeadingProgress(), 0.0f); 0121 QCOMPARE(model.arrived(), false); 0122 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {12, 3}, QTimeZone("Europe/Berlin"))); // arrived 0123 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0124 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0125 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.0f); 0126 QCOMPARE(model.arrivalLeadingProgress(), 0.0f); 0127 QCOMPARE(model.arrived(), false); 0128 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {12, 4}, QTimeZone("Europe/Berlin"))); // departed 0129 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0130 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0131 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 0.428571f); 0132 QCOMPARE(model.arrivalLeadingProgress(), 0.0f); 0133 QCOMPARE(model.arrived(), false); 0134 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {12, 9}, QTimeZone("Europe/Berlin"))); // arriving 0135 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0136 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0137 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 1.0f); 0138 QCOMPARE(model.arrivalLeadingProgress(), 0.785714f); 0139 QCOMPARE(model.arrived(), false); 0140 model.setCurrentDateTime(QDateTime({2021, 12, 21}, {12, 10}, QTimeZone("Europe/Berlin"))); // arrived 0141 QCOMPARE(idx.data(JourneySectionModel::StopoverPassedRole).toBool(), true); 0142 QCOMPARE(idx.data(JourneySectionModel::LeadingSegmentProgressRole).toFloat(), 1.0f); 0143 QCOMPARE(idx.data(JourneySectionModel::TrailingSegmentProgressRole).toFloat(), 1.0f); 0144 QCOMPARE(model.arrivalLeadingProgress(), 1.0f); 0145 QCOMPARE(model.arrived(), true); 0146 } 0147 }; 0148 0149 QTEST_GUILESS_MAIN(JourneySectionModelTest) 0150 0151 #include "journeysectionmodeltest.moc"