File indexing completed on 2024-04-21 03:52:40
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 } 0025 0026 void FileStorageTest::testSave() 0027 { 0028 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0029 FileStorage fs(cal, QStringLiteral("fred.ics")); 0030 0031 QDate dt = QDate::currentDate(); 0032 0033 Event::Ptr event1 = Event::Ptr(new Event()); 0034 event1->setUid(QStringLiteral("1")); 0035 event1->setDtStart(QDateTime(dt, {})); 0036 event1->setDtEnd(QDateTime(dt, {}).addDays(1)); 0037 event1->setSummary(QStringLiteral("Event1 Summary")); 0038 event1->setDescription(QStringLiteral("This is a description of the first event")); 0039 event1->setLocation(QStringLiteral("the place")); 0040 cal->addEvent(event1); 0041 0042 Event::Ptr event2 = Event::Ptr(new Event()); 0043 event2->setUid(QStringLiteral("2")); 0044 event2->setDtStart(QDateTime(dt, {}).addDays(1)); 0045 event2->setDtEnd(QDateTime(dt, {}).addDays(2)); 0046 event2->setSummary(QStringLiteral("Event2 Summary")); 0047 event2->setDescription(QStringLiteral("This is a description of the second event")); 0048 event2->setLocation(QStringLiteral("the other place")); 0049 cal->addEvent(event2); 0050 0051 QVERIFY(fs.open()); 0052 QVERIFY(fs.save()); 0053 QVERIFY(fs.close()); 0054 QFile::remove(QStringLiteral("fred.ics")); 0055 } 0056 0057 void FileStorageTest::testSaveLoadSave() 0058 { 0059 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0060 FileStorage fs(cal, QStringLiteral("fred.ics")); 0061 0062 QDate dt = QDate::currentDate(); 0063 0064 Event::Ptr event1 = Event::Ptr(new Event()); 0065 event1->setUid(QStringLiteral("1")); 0066 event1->setDtStart(QDateTime(dt, {})); 0067 event1->setDtEnd(QDateTime(dt, {}).addDays(1)); 0068 event1->setSummary(QStringLiteral("Event1 Summary")); 0069 event1->setDescription(QStringLiteral("This is a description of the first event")); 0070 event1->setLocation(QStringLiteral("the place")); 0071 cal->addEvent(event1); 0072 0073 Event::Ptr event2 = Event::Ptr(new Event()); 0074 event2->setUid(QStringLiteral("2")); 0075 event2->setDtStart(QDateTime(dt, {}).addDays(1)); 0076 event2->setDtEnd(QDateTime(dt, {}).addDays(2)); 0077 event2->setSummary(QStringLiteral("Event2 Summary")); 0078 event2->setDescription(QStringLiteral("This is a description of the second event")); 0079 event2->setLocation(QStringLiteral("the other place")); 0080 cal->addEvent(event2); 0081 0082 QVERIFY(fs.open()); 0083 QVERIFY(fs.save()); 0084 QVERIFY(fs.close()); 0085 QVERIFY(fs.open()); 0086 QVERIFY(fs.load()); 0087 Event::Ptr e = fs.calendar()->incidence(QStringLiteral("1")).staticCast<Event>(); 0088 QVERIFY(e != nullptr); 0089 QVERIFY(fs.close()); 0090 QFile::remove(QStringLiteral("fred.ics")); 0091 0092 QVERIFY(fs.open()); 0093 QVERIFY(fs.save()); 0094 QVERIFY(fs.close()); 0095 QFile::remove(QStringLiteral("fred.ics")); 0096 } 0097 0098 void FileStorageTest::testSpecialChars() 0099 { 0100 const QDate currentDate = QDate::currentDate(); 0101 const QString uid(QStringLiteral("12345")); 0102 0103 Event::Ptr event = Event::Ptr(new Event()); 0104 event->setUid(uid); 0105 event->setDtStart(QDateTime(currentDate, {})); 0106 event->setDtEnd(QDateTime(currentDate.addDays(1), {})); 0107 0108 const QChar latin1_umlaut[] = {(QChar)0xFC, QLatin1Char('\0')}; 0109 0110 event->setSummary(QString(latin1_umlaut)); 0111 0112 // Save to file: 0113 MemoryCalendar::Ptr cal(new MemoryCalendar(QTimeZone::utc())); 0114 FileStorage fs(cal, QStringLiteral("bart.ics")); 0115 cal->addEvent(event); 0116 0117 QVERIFY(fs.open()); 0118 QVERIFY(fs.save()); 0119 QVERIFY(fs.close()); 0120 0121 // Load again: 0122 MemoryCalendar::Ptr otherCalendar(new MemoryCalendar(QTimeZone::utc())); 0123 FileStorage otherFs(otherCalendar, QStringLiteral("bart.ics")); 0124 QVERIFY(otherFs.open()); 0125 QVERIFY(otherFs.load()); 0126 0127 Event::Ptr otherEvent = otherCalendar->incidence(uid).staticCast<Event>(); 0128 QVERIFY(otherFs.close()); 0129 0130 QVERIFY(otherEvent); 0131 0132 // Make sure the retrieved incidence is equal to the original one 0133 QVERIFY(otherEvent->summary() == event->summary()); 0134 QVERIFY(otherEvent->summary().toLatin1().size() == 1 0135 && strcmp(otherEvent->summary().toLatin1().constData(), QString(latin1_umlaut).toLatin1().constData()) == 0); 0136 0137 // Make sure bart.ics is in UTF-8 0138 QFile file(QStringLiteral("bart.ics")); 0139 QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); 0140 0141 const QByteArray bytesFromFile = file.readAll(); 0142 const QChar utf_umlaut[] = {(QChar)0xC3, (QChar)0XBC, QLatin1Char('\0')}; 0143 0144 QVERIFY(bytesFromFile.contains(QString(utf_umlaut).toLatin1().constData())); 0145 QVERIFY(!bytesFromFile.contains(QString(latin1_umlaut).toLatin1().constData())); 0146 0147 file.close(); 0148 0149 file.remove(); 0150 } 0151 0152 #include "moc_testfilestorage.cpp"