File indexing completed on 2024-05-05 12:10:31
0001 /* 0002 This file is part of the kcalcore library. 0003 SPDX-FileCopyrightText: 2007 Allen Winter <winter@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "testfilestorage.h" 0009 #include "filestorage.h" 0010 #include "memorycalendar.h" 0011 0012 #include <QTest> 0013 #include <QTimeZone> 0014 QTEST_MAIN(FileStorageTest) 0015 0016 using namespace KCalendarCore; 0017 0018 void FileStorageTest::testValidity() 0019 { 0020 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0021 FileStorage fs(cal, QStringLiteral("fred.ics")); 0022 QCOMPARE(fs.fileName(), QStringLiteral("fred.ics")); 0023 QCOMPARE(fs.calendar().data(), cal.data()); 0024 cal->close(); 0025 } 0026 0027 void FileStorageTest::testSave() 0028 { 0029 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0030 FileStorage fs(cal, QStringLiteral("fred.ics")); 0031 0032 QDate dt = QDate::currentDate(); 0033 0034 Event::Ptr event1 = Event::Ptr(new Event()); 0035 event1->setUid(QStringLiteral("1")); 0036 event1->setDtStart(QDateTime(dt, {})); 0037 event1->setDtEnd(QDateTime(dt, {}).addDays(1)); 0038 event1->setSummary(QStringLiteral("Event1 Summary")); 0039 event1->setDescription(QStringLiteral("This is a description of the first event")); 0040 event1->setLocation(QStringLiteral("the place")); 0041 cal->addEvent(event1); 0042 0043 Event::Ptr event2 = Event::Ptr(new Event()); 0044 event2->setUid(QStringLiteral("2")); 0045 event2->setDtStart(QDateTime(dt, {}).addDays(1)); 0046 event2->setDtEnd(QDateTime(dt, {}).addDays(2)); 0047 event2->setSummary(QStringLiteral("Event2 Summary")); 0048 event2->setDescription(QStringLiteral("This is a description of the second event")); 0049 event2->setLocation(QStringLiteral("the other place")); 0050 cal->addEvent(event2); 0051 0052 QVERIFY(fs.open()); 0053 QVERIFY(fs.save()); 0054 QVERIFY(fs.close()); 0055 cal->close(); 0056 QFile::remove(QStringLiteral("fred.ics")); 0057 } 0058 0059 void FileStorageTest::testSaveLoadSave() 0060 { 0061 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0062 FileStorage fs(cal, QStringLiteral("fred.ics")); 0063 0064 QDate dt = QDate::currentDate(); 0065 0066 Event::Ptr event1 = Event::Ptr(new Event()); 0067 event1->setUid(QStringLiteral("1")); 0068 event1->setDtStart(QDateTime(dt, {})); 0069 event1->setDtEnd(QDateTime(dt, {}).addDays(1)); 0070 event1->setSummary(QStringLiteral("Event1 Summary")); 0071 event1->setDescription(QStringLiteral("This is a description of the first event")); 0072 event1->setLocation(QStringLiteral("the place")); 0073 cal->addEvent(event1); 0074 0075 Event::Ptr event2 = Event::Ptr(new Event()); 0076 event2->setUid(QStringLiteral("2")); 0077 event2->setDtStart(QDateTime(dt, {}).addDays(1)); 0078 event2->setDtEnd(QDateTime(dt, {}).addDays(2)); 0079 event2->setSummary(QStringLiteral("Event2 Summary")); 0080 event2->setDescription(QStringLiteral("This is a description of the second event")); 0081 event2->setLocation(QStringLiteral("the other place")); 0082 cal->addEvent(event2); 0083 0084 QVERIFY(fs.open()); 0085 QVERIFY(fs.save()); 0086 QVERIFY(fs.close()); 0087 QVERIFY(fs.open()); 0088 QVERIFY(fs.load()); 0089 Event::Ptr e = fs.calendar()->incidence(QStringLiteral("1")).staticCast<Event>(); 0090 QVERIFY(e != nullptr); 0091 QVERIFY(fs.close()); 0092 QFile::remove(QStringLiteral("fred.ics")); 0093 0094 QVERIFY(fs.open()); 0095 QVERIFY(fs.save()); 0096 QVERIFY(fs.close()); 0097 QFile::remove(QStringLiteral("fred.ics")); 0098 } 0099 0100 void FileStorageTest::testSpecialChars() 0101 { 0102 const QDate currentDate = QDate::currentDate(); 0103 const QString uid(QStringLiteral("12345")); 0104 0105 Event::Ptr event = Event::Ptr(new Event()); 0106 event->setUid(uid); 0107 event->setDtStart(QDateTime(currentDate, {})); 0108 event->setDtEnd(QDateTime(currentDate.addDays(1), {})); 0109 0110 const QChar latin1_umlaut[] = {(QChar)0xFC, QLatin1Char('\0')}; 0111 0112 event->setSummary(QString(latin1_umlaut)); 0113 0114 // Save to file: 0115 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0116 FileStorage fs(cal, QStringLiteral("bart.ics")); 0117 cal->addEvent(event); 0118 0119 QVERIFY(fs.open()); 0120 QVERIFY(fs.save()); 0121 QVERIFY(fs.close()); 0122 0123 // Load again: 0124 MemoryCalendar::Ptr otherCalendar(new MemoryCalendar(QTimeZone::utc())); 0125 FileStorage otherFs(otherCalendar, QStringLiteral("bart.ics")); 0126 QVERIFY(otherFs.open()); 0127 QVERIFY(otherFs.load()); 0128 0129 Event::Ptr otherEvent = otherCalendar->incidence(uid).staticCast<Event>(); 0130 QVERIFY(otherFs.close()); 0131 0132 QVERIFY(otherEvent); 0133 0134 // Make sure the retrieved incidence is equal to the original one 0135 QVERIFY(otherEvent->summary() == event->summary()); 0136 QVERIFY(otherEvent->summary().toLatin1().count() == 1 0137 && strcmp(otherEvent->summary().toLatin1().constData(), QString(latin1_umlaut).toLatin1().constData()) == 0); 0138 0139 // Make sure bart.ics is in UTF-8 0140 QFile file(QStringLiteral("bart.ics")); 0141 QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); 0142 0143 const QByteArray bytesFromFile = file.readAll(); 0144 const QChar utf_umlaut[] = {(QChar)0xC3, (QChar)0XBC, QLatin1Char('\0')}; 0145 0146 QVERIFY(bytesFromFile.contains(QString(utf_umlaut).toLatin1().constData())); 0147 QVERIFY(!bytesFromFile.contains(QString(latin1_umlaut).toLatin1().constData())); 0148 0149 file.close(); 0150 0151 file.remove(); 0152 } 0153 0154 #include "moc_testfilestorage.cpp"