Commit 9ee19ffc authored by Volker Krause's avatar Volker Krause
Browse files

Fall back to expiration date when the relevance date field is not set

parent f0236a66
......@@ -2,3 +2,4 @@ add_definitions(-DSOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
ecm_add_test(pkpasstest.cpp LINK_LIBRARIES Qt5::Test KPkPass)
ecm_add_test(pkpassmanagertest.cpp LINK_LIBRARIES Qt5::Test itinerary)
ecm_add_test(timelinemodeltest.cpp LINK_LIBRARIES Qt5::Test itinerary)
/*
Copyright (C) 2018 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pkpassmanager.h>
#include <timelinemodel.h>
#include <QUrl>
#include <QtTest/qtest.h>
#include <QSignalSpy>
#include <QStandardPaths>
class TimelineModelTest : public QObject
{
Q_OBJECT
private:
void clearPasses(PkPassManager *mgr)
{
for (const auto id : mgr->passes())
mgr->removePass(id);
}
private slots:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void testModel()
{
PkPassManager mgr;
clearPasses(&mgr);
TimelineModel model;
model.setPkPassManager(&mgr);
QSignalSpy insertSpy(&model, &TimelineModel::rowsInserted);
QVERIFY(insertSpy.isValid());
QSignalSpy updateSpy(&model, &TimelineModel::dataChanged);
QVERIFY(updateSpy.isValid());
QCOMPARE(model.rowCount(), 0);
mgr.importPass(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/boardingpass-v1.pkpass")));
QCOMPARE(insertSpy.size(), 1);
QVERIFY(updateSpy.isEmpty());
QCOMPARE(model.rowCount(), 1);
mgr.importPass(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/boardingpass-v2.pkpass")));
QCOMPARE(insertSpy.size(), 1);
QCOMPARE(updateSpy.size(), 1);
}
};
QTEST_GUILESS_MAIN(TimelineModelTest)
#include "timelinemodeltest.moc"
......@@ -151,7 +151,7 @@ void PkPassManager::updatePass(const QString& passId)
auto p = pass(passId);
if (!p || p->webServiceUrl().isEmpty() || p->authenticationToken().isEmpty())
return;
if (p->relevantDate() < QDateTime::currentDateTimeUtc()) // TODO check expiration date and voided property
if (relevantDate(p) < QDateTime::currentDateTimeUtc()) // TODO check expiration date and voided property
return;
QUrl url(p->webServiceUrl());
......@@ -180,3 +180,11 @@ void PkPassManager::updatePasses()
for (const auto &passId : passes())
updatePass(passId);
}
QDateTime PkPassManager::relevantDate(KPkPass::Pass *pass)
{
const auto dt = pass->relevantDate();
if (dt.isValid())
return dt;
return pass->expirationDate();
}
......@@ -45,6 +45,8 @@ public:
void updatePass(const QString &passId);
Q_INVOKABLE void updatePasses();
static QDateTime relevantDate(KPkPass::Pass *pass);
signals:
void passAdded(const QString &passId);
void passUpdated(const QString &passId, const QStringList &changes);
......
......@@ -36,7 +36,7 @@ void TimelineModel::setPkPassManager(PkPassManager* mgr)
m_mgr = mgr;
m_passes = mgr->passes();
std::sort(m_passes.begin(), m_passes.end(), [this](const QString &lhs, const QString &rhs) {
return m_mgr->pass(lhs)->relevantDate() > m_mgr->pass(rhs)->relevantDate();
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
});
connect(mgr, &PkPassManager::passAdded, this, &TimelineModel::passAdded);
connect(mgr, &PkPassManager::passUpdated, this, &TimelineModel::passUpdated);
......@@ -61,7 +61,7 @@ QVariant TimelineModel::data(const QModelIndex& index, int role) const
case PassIdRole:
return m_passes.at(index.row());
case SectionHeader:
return QLocale().toString(m_mgr->pass(m_passes.at(index.row()))->relevantDate().date(), QLocale::ShortFormat);
return QLocale().toString(PkPassManager::relevantDate(m_mgr->pass(m_passes.at(index.row()))).date(), QLocale::ShortFormat);
}
return {};
}
......@@ -78,7 +78,7 @@ QHash<int, QByteArray> TimelineModel::roleNames() const
void TimelineModel::passAdded(const QString &passId)
{
auto it = std::lower_bound(m_passes.begin(), m_passes.end(), passId, [this](const QString &lhs, const QString &rhs) {
return m_mgr->pass(lhs)->relevantDate() > m_mgr->pass(rhs)->relevantDate();
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
});
auto index = std::distance(m_passes.begin(), it);
beginInsertRows({}, index, index);
......@@ -89,7 +89,7 @@ void TimelineModel::passAdded(const QString &passId)
void TimelineModel::passUpdated(const QString &passId)
{
auto it = std::lower_bound(m_passes.begin(), m_passes.end(), passId, [this](const QString &lhs, const QString &rhs) {
return m_mgr->pass(lhs)->relevantDate() > m_mgr->pass(rhs)->relevantDate();
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
});
auto row = std::distance(m_passes.begin(), it);
emit dataChanged(index(row, 0), index(row, 0));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment