File indexing completed on 2024-04-21 03:52:39

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Glen Ditchfield <GJDitchfield@acm.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "testdateserialization.h"
0008 #include "icalformat.h"
0009 #include "memorycalendar.h"
0010 
0011 #include <QTest>
0012 
0013 QTEST_MAIN(TestDateSerialization)
0014 
0015 using namespace KCalendarCore;
0016 
0017 // Check that serialization and deserialization of a minimal recurring todo
0018 // preserves the start and due dates of the todo and its first occurrence.
0019 // See bug 345498.
0020 void TestDateSerialization::testNewRecurringTodo()
0021 {
0022     QDateTime startDate = QDate(2015, 3, 24).startOfDay();
0023     QDateTime dueDate{startDate.addDays(1)};
0024 
0025     Todo::Ptr todo(new Todo);
0026     todo->setDtStart(startDate);
0027     todo->setDtDue(dueDate, true);
0028     todo->setAllDay(true);
0029     todo->recurrence()->setMonthly(1);
0030 
0031     MemoryCalendar::Ptr cal{new MemoryCalendar(QTimeZone::utc())};
0032     cal->addIncidence(todo);
0033 
0034     ICalFormat format;
0035     const QString result = format.toString(cal);
0036 
0037     Incidence::Ptr i = format.fromString(result);
0038     QVERIFY(i);
0039     QVERIFY(i->type() == IncidenceBase::IncidenceType::TypeTodo);
0040     Todo::Ptr newTodo = i.staticCast<Todo>();
0041     QCOMPARE(newTodo->dtStart(true), startDate);
0042     QCOMPARE(newTodo->dtStart(false), startDate);
0043     QCOMPARE(newTodo->dtDue(true), dueDate);
0044     QCOMPARE(newTodo->dtDue(false), dueDate);
0045 }
0046 
0047 // Check that serialization and deserialization of a minimal recurring todo
0048 // that has been completed once preserves the start and due dates of the todo
0049 // and correctly calculates the start and due dates of the next occurrence.
0050 // See bug 345565.
0051 void TestDateSerialization::testTodoCompletedOnce()
0052 {
0053     QDateTime startDate = QDate(QDate::currentDate().year(), QDate::currentDate().month(), 1).startOfDay();
0054     QDateTime dueDate{startDate.addDays(1)};
0055 
0056     Todo::Ptr todo(new Todo);
0057     todo->setDtStart(startDate);
0058     todo->setDtDue(dueDate, true);
0059     todo->setAllDay(true);
0060     todo->recurrence()->setMonthly(1);
0061 
0062     MemoryCalendar::Ptr cal{new MemoryCalendar(QTimeZone::utc())};
0063     cal->addIncidence(todo);
0064 
0065     ICalFormat format;
0066     QString result = format.toString(cal);
0067 
0068     Incidence::Ptr i = format.fromString(result);
0069     QVERIFY(i);
0070     QVERIFY(i->type() == IncidenceBase::IncidenceType::TypeTodo);
0071     todo = i.staticCast<Todo>();
0072     todo->setCompleted(dueDate);
0073 
0074     cal = MemoryCalendar::Ptr{new MemoryCalendar(QTimeZone::utc())};
0075     cal->addIncidence(todo);
0076     result = format.toString(cal);
0077 
0078     QCOMPARE(todo->dtStart(true), startDate);
0079     QCOMPARE(todo->dtStart(false), startDate.addMonths(1));
0080     QCOMPARE(todo->dtDue(true), dueDate);
0081     QCOMPARE(todo->dtDue(false), dueDate.addMonths(1));
0082 }
0083 
0084 // Check that QDateTime values with UTC offsets are handled correctly
0085 void TestDateSerialization::testUTCOffset()
0086 {
0087     QDateTime startDate({2022, 3, 6}, {10, 25}, QTimeZone::fromSecondsAheadOfUtc(3600));
0088 
0089     Event::Ptr event(new Event);
0090     event->setDtStart(startDate);
0091 
0092     MemoryCalendar::Ptr cal{new MemoryCalendar(QTimeZone::utc())};
0093     cal->addIncidence(event);
0094 
0095     ICalFormat format;
0096     QString result = format.toString(cal);
0097 
0098     Incidence::Ptr i = format.fromString(result);
0099     QVERIFY(i);
0100     QCOMPARE(i->dtStart(), startDate);
0101     // Qt::OffsetFromUTC is turned into Qt::Timezone with a "UTC+/-X" timezone right now
0102     // but Qt::OffsetFromUTC would be equally correct here should this ever change in the future
0103     QCOMPARE(i->dtStart().timeSpec(), Qt::TimeZone);
0104     QCOMPARE(i->dtStart().timeZone().offsetFromUtc(i->dtStart()), 3600);
0105 }
0106 
0107 #include "moc_testdateserialization.cpp"