File indexing completed on 2024-05-12 05:15:00
0001 /* 0002 This file is part of the kcalcore library. 0003 0004 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> 0005 SPDX-FileContributor: Sérgio Martins <sergio.martins@kdab.com> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "testdndfactory.h" 0011 0012 #include "dndfactory.h" 0013 0014 #include <KCalendarCore/MemoryCalendar> 0015 0016 #include <QTest> 0017 #include <QTimeZone> 0018 0019 QTEST_MAIN(DndFactoryTest) // clipboard() needs GUI 0020 0021 using namespace KCalendarCore; 0022 using namespace KCalUtils; 0023 0024 void DndFactoryTest::testPasteAllDayEvent() 0025 { 0026 MemoryCalendar::Ptr calendar(new MemoryCalendar(QTimeZone::systemTimeZone())); 0027 0028 DndFactory factory(calendar); 0029 0030 Event::Ptr allDayEvent(new Event()); 0031 allDayEvent->setSummary(QStringLiteral("Summary 1")); 0032 allDayEvent->setDtStart(QDateTime(QDate(2010, 8, 8), {})); 0033 allDayEvent->setDtEnd(QDateTime(QDate(2010, 8, 9), {})); 0034 allDayEvent->setAllDay(true); 0035 const QString originalUid = allDayEvent->uid(); 0036 const bool originalIsAllDay = allDayEvent->allDay(); 0037 0038 Incidence::List incidencesToPaste; 0039 incidencesToPaste.append(allDayEvent); 0040 0041 QVERIFY(factory.copyIncidences(incidencesToPaste)); 0042 0043 Incidence::List pastedIncidences = factory.pasteIncidences(); 0044 QVERIFY(pastedIncidences.size() == 1); 0045 0046 Incidence::Ptr incidence = pastedIncidences.first(); 0047 0048 QVERIFY(incidence->type() == Incidence::TypeEvent); 0049 0050 // check if a new uid was generated. 0051 QVERIFY(incidence->uid() != originalUid); 0052 0053 // we passed an invalid KDateTime to pasteIncidences() so dates don't change. 0054 QVERIFY(incidence->allDay() == originalIsAllDay); 0055 0056 Event::Ptr pastedEvent = incidence.staticCast<Event>(); 0057 0058 QCOMPARE(pastedEvent->dtStart(), allDayEvent->dtStart()); 0059 QCOMPARE(pastedEvent->dtEnd(), allDayEvent->dtEnd()); 0060 QCOMPARE(pastedEvent->summary(), allDayEvent->summary()); 0061 } 0062 0063 void DndFactoryTest::testPasteAllDayEvent2() 0064 { 0065 MemoryCalendar::Ptr calendar(new MemoryCalendar(QTimeZone::systemTimeZone())); 0066 0067 DndFactory factory(calendar); 0068 0069 Event::Ptr allDayEvent(new Event()); 0070 allDayEvent->setSummary(QStringLiteral("Summary 2")); 0071 allDayEvent->setDtStart(QDateTime(QDate(2010, 8, 8), {})); 0072 allDayEvent->setDtEnd(QDateTime(QDate(2010, 8, 9), {})); 0073 allDayEvent->setAllDay(true); 0074 const QString originalUid = allDayEvent->uid(); 0075 0076 Incidence::List incidencesToPaste; 0077 incidencesToPaste.append(allDayEvent); 0078 0079 QVERIFY(factory.copyIncidences(incidencesToPaste)); 0080 const QDateTime newDateTime(QDate(2011, 1, 1).startOfDay()); 0081 const uint originalLength = allDayEvent->dtStart().secsTo(allDayEvent->dtEnd()); 0082 0083 // paste at the new time 0084 Incidence::List pastedIncidences = factory.pasteIncidences(newDateTime); 0085 0086 // we only copied one incidence 0087 QVERIFY(pastedIncidences.size() == 1); 0088 0089 Incidence::Ptr incidence = pastedIncidences.first(); 0090 0091 QVERIFY(incidence->type() == Incidence::TypeEvent); 0092 0093 // check if a new uid was generated. 0094 QVERIFY(incidence->uid() != originalUid); 0095 0096 // the new dateTime didn't have time component 0097 QVERIFY(incidence->allDay()); 0098 0099 Event::Ptr pastedEvent = incidence.staticCast<Event>(); 0100 const uint newLength = pastedEvent->dtStart().secsTo(pastedEvent->dtEnd()); 0101 #if 0 0102 qDebug() << "originalLength was " << originalLength << "; and newLength is " 0103 << newLength << "; old dtStart was " << allDayEvent->dtStart() 0104 << " and old dtEnd was " << allDayEvent->dtEnd() << endl 0105 << "; new dtStart is " << pastedEvent->dtStart() 0106 << " and new dtEnd is " << pastedEvent->dtEnd(); 0107 #endif 0108 QCOMPARE(newLength, originalLength); 0109 QCOMPARE(newDateTime, pastedEvent->dtStart()); 0110 QCOMPARE(allDayEvent->summary(), pastedEvent->summary()); 0111 } 0112 0113 void DndFactoryTest::testPasteTodo() 0114 { 0115 MemoryCalendar::Ptr calendar(new MemoryCalendar(QTimeZone::systemTimeZone())); 0116 0117 DndFactory factory(calendar); 0118 0119 Todo::Ptr todo(new Todo()); 0120 todo->setSummary(QStringLiteral("Summary 1")); 0121 todo->setDtDue(QDateTime(QDate(2010, 8, 9), {})); 0122 0123 Incidence::List incidencesToPaste; 0124 incidencesToPaste.append(todo); 0125 0126 QVERIFY(factory.copyIncidences(incidencesToPaste)); 0127 0128 const QDateTime newDateTime(QDate(2011, 1, 1), QTime(10, 10)); 0129 0130 Incidence::List pastedIncidences = factory.pasteIncidences(newDateTime); 0131 QVERIFY(pastedIncidences.size() == 1); 0132 0133 Incidence::Ptr incidence = pastedIncidences.first(); 0134 0135 QVERIFY(incidence->type() == Incidence::TypeTodo); 0136 0137 // check if a new uid was generated. 0138 QVERIFY(incidence->uid() != todo->uid()); 0139 0140 Todo::Ptr pastedTodo = incidence.staticCast<Todo>(); 0141 0142 QCOMPARE(newDateTime, pastedTodo->dtDue()); 0143 QCOMPARE(todo->summary(), pastedTodo->summary()); 0144 } 0145 0146 #include "moc_testdndfactory.cpp"