File indexing completed on 2025-01-19 04:46:59
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 * 0006 */ 0007 0008 #include "testdataparser.h" 0009 #include <QDir> 0010 #include <QJsonArray> 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QTimeZone> 0014 0015 #include <QTest> 0016 0017 #include <KCalendarCore/ICalFormat> 0018 #include <KCalendarCore/MemoryCalendar> 0019 0020 #include <CalendarEvents/CalendarEventsPlugin> 0021 0022 TestDataParser::TestDataParser(const QString &testData, bool uniqueEventData) 0023 : mTestData(testData) 0024 , mUniqueEventData(uniqueEventData) 0025 { 0026 parse(); 0027 } 0028 0029 TestDataParser::~TestDataParser() = default; 0030 0031 QStringList TestDataParser::allTestData() 0032 { 0033 QDir testdir(QStringLiteral(PIMEVENT_DATADIR "/data")); 0034 const auto data = testdir.entryInfoList({QStringLiteral("*.json")}, QDir::Files); 0035 QStringList testcases; 0036 testcases.reserve(data.count()); 0037 for (const auto &fi : data) { 0038 testcases << fi.baseName(); 0039 } 0040 return testcases; 0041 } 0042 0043 QDate TestDataParser::rangeStart() const 0044 { 0045 return mRangeStart; 0046 } 0047 0048 QDate TestDataParser::rangeEnd() const 0049 { 0050 return mRangeEnd; 0051 } 0052 0053 qint64 TestDataParser::akonadiId() const 0054 { 0055 return mAkonadiId; 0056 } 0057 0058 QList<CalendarEvents::EventData> TestDataParser::eventData() const 0059 { 0060 return mEventData; 0061 } 0062 0063 KCalendarCore::Incidence::Ptr TestDataParser::incidence() const 0064 { 0065 return mIncidence; 0066 } 0067 0068 QDateTime TestDataParser::parseDateTime(const QJsonObject &dateTime) 0069 { 0070 const auto date = QDate::fromString(dateTime[QStringLiteral("date")].toString(), Qt::ISODate); 0071 const auto time = QTime::fromString(dateTime[QStringLiteral("time")].toString(), Qt::ISODate); 0072 if (dateTime.contains(QLatin1StringView("tz"))) { 0073 return QDateTime(date, time, QTimeZone(dateTime[QStringLiteral("tz")].toString().toLatin1())).toLocalTime(); 0074 } else { 0075 return QDateTime(date, time, Qt::LocalTime); 0076 } 0077 } 0078 0079 void TestDataParser::parse() 0080 { 0081 QFile icalFile(QStringLiteral(PIMEVENT_DATADIR "/data/%1.ics").arg(mTestData)); 0082 QVERIFY(icalFile.exists()); 0083 QVERIFY(icalFile.open(QIODevice::ReadOnly)); 0084 0085 auto calendar = KCalendarCore::MemoryCalendar::Ptr::create(QTimeZone::systemTimeZone()); 0086 KCalendarCore::ICalFormat format; 0087 QVERIFY(format.load(calendar, icalFile.fileName())); 0088 QVERIFY(!calendar->incidences().isEmpty()); 0089 mIncidence = calendar->incidences().at(0); 0090 QVERIFY(mIncidence); 0091 0092 QFile jsonFile(QStringLiteral(PIMEVENT_DATADIR "/data/%1.json").arg(mTestData)); 0093 QVERIFY(jsonFile.exists()); 0094 QVERIFY(jsonFile.open(QIODevice::ReadOnly)); 0095 0096 const QByteArray json = jsonFile.readAll(); 0097 const QJsonDocument jsonDoc = QJsonDocument::fromJson(json); 0098 const QJsonObject doc = jsonDoc.object(); 0099 mRangeStart = QDate::fromString(doc[QStringLiteral("rangeStart")].toString(), Qt::ISODate); 0100 mRangeEnd = QDate::fromString(doc[QStringLiteral("rangeEnd")].toString(), Qt::ISODate); 0101 mAkonadiId = doc[QStringLiteral("akonadiId")].toInt(); 0102 0103 const QJsonArray array = doc[QStringLiteral("eventData")].toArray(); 0104 for (auto iter = array.constBegin(), end = array.constEnd(); iter != end; ++iter) { 0105 CalendarEvents::EventData eventData; 0106 const QJsonObject obj = iter->toObject(); 0107 eventData.setTitle(obj[QStringLiteral("summary")].toString()); 0108 eventData.setDescription(obj[QStringLiteral("description")].toString()); 0109 const QString type = obj[QStringLiteral("type")].toString(); 0110 if (type == QLatin1StringView("Event")) { 0111 eventData.setEventType(CalendarEvents::EventData::Event); 0112 } else { 0113 eventData.setEventType(CalendarEvents::EventData::Todo); 0114 } 0115 eventData.setIsAllDay(obj[QStringLiteral("allDay")].toBool()); 0116 eventData.setIsMinor(obj[QStringLiteral("isMinor")].toBool()); 0117 QDateTime startDateTime = parseDateTime(obj[QStringLiteral("startDateTime")].toObject()); 0118 if (eventData.isAllDay()) { 0119 startDateTime.setTime(QTime(0, 0, 0, Qt::LocalTime)); 0120 } 0121 eventData.setStartDateTime(startDateTime); 0122 QDateTime endDateTime = parseDateTime(obj[QStringLiteral("endDateTime")].toObject()); 0123 if (eventData.isAllDay()) { 0124 endDateTime.setTime(QTime(0, 0, 0, Qt::LocalTime)); 0125 } 0126 eventData.setEndDateTime(endDateTime); 0127 eventData.setUid(obj[QStringLiteral("uid")].toString()); 0128 0129 if (mUniqueEventData) { 0130 mEventData.push_back(eventData); 0131 } else { 0132 QDate d = startDateTime.date(); 0133 const QDate dateEnd = endDateTime.date(); 0134 while (d <= dateEnd) { 0135 mEventData.push_back(eventData); 0136 d = d.addDays(1); 0137 } 0138 } 0139 } 0140 QVERIFY(!mEventData.isEmpty()); 0141 }