testtodo.cpp 10.4 KB
Newer Older
Allen Winter's avatar
Allen Winter committed
1 2 3
/*
  This file is part of the kcalcore library.

4 5 6
  SPDX-FileCopyrightText: 2006, 2008 Allen Winter <winter@kde.org>

  SPDX-License-Identifier: LGPL-2.0-or-later
Allen Winter's avatar
Allen Winter committed
7 8
*/
#include "testtodo.h"
Christophe Giboudeaux's avatar
Christophe Giboudeaux committed
9
#include "todo.h"
10
#include "event.h"
11
#include "attachment.h"
Allen Winter's avatar
Allen Winter committed
12

Allen Winter's avatar
Allen Winter committed
13
#include <QTest>
Laurent Montel's avatar
Laurent Montel committed
14
QTEST_MAIN(TodoTest)
Allen Winter's avatar
Allen Winter committed
15

Volker Krause's avatar
Volker Krause committed
16
using namespace KCalendarCore;
Allen Winter's avatar
Allen Winter committed
17

18 19
void TodoTest::initTestCase()
{
Adriaan de Groot's avatar
Adriaan de Groot committed
20
    qputenv("TZ", "UTC");
21 22
}

Allen Winter's avatar
Allen Winter committed
23 24
void TodoTest::testValidity()
{
25 26
    QDate dt = QDate::currentDate();
    Todo *todo = new Todo();
27 28
    todo->setDtStart(QDateTime(dt, {}));
    todo->setDtDue(QDateTime(dt, {}).addDays(1));
Laurent Montel's avatar
Laurent Montel committed
29 30 31
    todo->setSummary(QStringLiteral("To-do1 Summary"));
    todo->setDescription(QStringLiteral("This is a description of the first to-do"));
    todo->setLocation(QStringLiteral("the place"));
32
    todo->setPercentComplete(5);
33 34 35 36

    QCOMPARE(todo->summary(), QStringLiteral("To-do1 Summary"));
    QCOMPARE(todo->location(), QStringLiteral("the place"));
    QCOMPARE(todo->percentComplete(), 5);
Allen Winter's avatar
Allen Winter committed
37 38 39 40
}

void TodoTest::testCompare()
{
41 42
    QDate dt = QDate::currentDate();
    Todo todo1;
43 44
    todo1.setDtStart(QDateTime(dt, {}));
    todo1.setDtDue(QDateTime(dt, {}).addDays(1));
Laurent Montel's avatar
Laurent Montel committed
45 46 47
    todo1.setSummary(QStringLiteral("To-do1 Summary"));
    todo1.setDescription(QStringLiteral("This is a description of the first to-do"));
    todo1.setLocation(QStringLiteral("the place"));
48 49 50
    todo1.setCompleted(true);

    Todo todo2;
51 52
    todo2.setDtStart(QDateTime(dt, {}).addDays(1));
    todo2.setDtDue(QDateTime(dt, {}).addDays(2));
Laurent Montel's avatar
Laurent Montel committed
53 54 55
    todo2.setSummary(QStringLiteral("To-do2 Summary"));
    todo2.setDescription(QStringLiteral("This is a description of the second to-do"));
    todo2.setLocation(QStringLiteral("the other place"));
56 57 58
    todo2.setCompleted(false);

    QVERIFY(!(todo1 == todo2));
59 60
    QCOMPARE(todo1.dtDue(), todo2.dtStart());
    QCOMPARE(todo2.summary(), QStringLiteral("To-do2 Summary"));
61
    QVERIFY(!(todo1.isCompleted() == todo2.isCompleted()));
Allen Winter's avatar
Allen Winter committed
62 63 64 65
}

void TodoTest::testClone()
{
66 67
    QDate dt = QDate::currentDate();
    Todo todo1;
68 69
    todo1.setDtStart(QDateTime(dt, {}));
    todo1.setDtDue(QDateTime(dt, {}).addDays(1));
Laurent Montel's avatar
Laurent Montel committed
70 71 72
    todo1.setSummary(QStringLiteral("Todo1 Summary"));
    todo1.setDescription(QStringLiteral("This is a description of the first todo"));
    todo1.setLocation(QStringLiteral("the place"));
73 74

    Todo *todo2 = todo1.clone();
75 76 77 78 79 80
    QCOMPARE(todo1.summary(), todo2->summary());
    QCOMPARE(todo1.dtStart(), todo2->dtStart());
    QCOMPARE(todo1.dtDue(), todo2->dtDue());
    QCOMPARE(todo1.description(), todo2->description());
    QCOMPARE(todo1.location(), todo2->location());
    QCOMPARE(todo1.isCompleted(), todo2->isCompleted());
Allen Winter's avatar
Allen Winter committed
81 82
}

83 84
void TodoTest::testCopyIncidence()
{
Laurent Montel's avatar
Laurent Montel committed
85 86
    QDate dt = QDate::currentDate();
    Event event;
87
    event.setDtStart(QDateTime(dt, {}));
Laurent Montel's avatar
Laurent Montel committed
88 89 90
    event.setSummary(QStringLiteral("Event1 Summary"));
    event.setDescription(QStringLiteral("This is a description of the first event"));
    event.setLocation(QStringLiteral("the place"));
Laurent Montel's avatar
Laurent Montel committed
91 92 93 94 95 96 97

    Todo todo(event);
    QCOMPARE(todo.uid(), event.uid());
    QCOMPARE(todo.dtStart(), event.dtStart());
    QCOMPARE(todo.summary(), event.summary());
    QCOMPARE(todo.description(), event.description());
    QCOMPARE(todo.location(), event.location());
98 99
}

Allen Winter's avatar
Allen Winter committed
100 101
void TodoTest::testAssign()
{
102 103
    QDate dt = QDate::currentDate();
    Todo todo1;
104 105
    todo1.setDtStart(QDateTime(dt, {}));
    todo1.setDtDue(QDateTime(dt, {}).addDays(1));
Laurent Montel's avatar
Laurent Montel committed
106 107 108
    todo1.setSummary(QStringLiteral("Todo1 Summary"));
    todo1.setDescription(QStringLiteral("This is a description of the first todo"));
    todo1.setLocation(QStringLiteral("the place"));
109 110 111

    Todo todo2 = todo1;
    QVERIFY(todo1 == todo2);
Allen Winter's avatar
Allen Winter committed
112 113
}

Laurent Montel's avatar
Laurent Montel committed
114 115
void TodoTest::testSetCompleted()
{
116
    Todo todo1, todo2, todo3;
Laurent Montel's avatar
Laurent Montel committed
117 118 119
    todo1.setSummary(QStringLiteral("Todo Summary"));
    todo2.setSummary(QStringLiteral("Todo Summary"));
    todo3.setSummary(QStringLiteral("Todo Summary"));
120
    QDateTime today = QDateTime::currentDateTimeUtc();
Allen Winter's avatar
Allen Winter committed
121

122
    // due yesterday
123
    QDateTime originalDueDate = today.addDays(-1);
Allen Winter's avatar
Allen Winter committed
124

125 126 127 128
    todo1.setDtStart(originalDueDate);
    todo1.setDtDue(originalDueDate);
    todo1.recurrence()->setDaily(1);
    todo1.setCompleted(today);
Allen Winter's avatar
Allen Winter committed
129

130
    todo2.setCompleted(true);
Allen Winter's avatar
Allen Winter committed
131

132 133
    todo3.setStatus(Incidence::StatusCompleted);

134 135 136
    QVERIFY(originalDueDate != todo1.dtDue());
    QVERIFY(!todo1.isCompleted());
    QVERIFY(todo2.isCompleted());
137 138
    QCOMPARE(todo2.status(), Incidence::StatusCompleted);
    QVERIFY(todo3.isCompleted());
139 140
    todo2.setCompleted(false);
    QVERIFY(!todo2.isCompleted());
Allen Winter's avatar
Allen Winter committed
141 142
}

Laurent Montel's avatar
Laurent Montel committed
143 144
void TodoTest::testStatus()
{
145 146
    QDateTime today = QDateTime::currentDateTimeUtc();
    QDateTime yesterday = today.addDays(-1);
147 148

    Todo todo1;
Albert Astals Cid's avatar
Albert Astals Cid committed
149
    todo1.setAllDay(true);
150 151 152 153 154 155 156 157 158 159 160
    todo1.setDtStart(yesterday);
    todo1.setDtDue(today);
    todo1.setPercentComplete(50);
    QVERIFY(todo1.isInProgress(false));
    QVERIFY(!todo1.isNotStarted(false));
    QVERIFY(!todo1.isOverdue());
    todo1.setPercentComplete(100);
    QVERIFY(todo1.isCompleted());

    Todo todo2 = todo1;
    todo2.setPercentComplete(33);
161
    todo2.setDtDue(QDateTime());
162
    QVERIFY(todo2.isOpenEnded());
Allen Winter's avatar
Allen Winter committed
163
}
164 165 166

void TodoTest::testSerializer_data()
{
Volker Krause's avatar
Volker Krause committed
167
    QTest::addColumn<KCalendarCore::Todo::Ptr>("todo");
168

169 170
    QDateTime today = QDateTime::currentDateTimeUtc();
    QDateTime yesterday = today.addDays(-1);
171 172 173 174 175 176 177 178

    Todo::Ptr todo1 = Todo::Ptr(new Todo());
    Todo::Ptr todo2 = Todo::Ptr(new Todo());
    Todo::Ptr todo3 = Todo::Ptr(new Todo());
    Todo::Ptr todo4 = Todo::Ptr(new Todo());
    Todo::Ptr todo5 = Todo::Ptr(new Todo());
    Todo::Ptr todo6 = Todo::Ptr(new Todo());

Laurent Montel's avatar
Laurent Montel committed
179 180
    todo1->setSummary(QStringLiteral("Summary"), false);
    todo1->setDescription(QStringLiteral("description"), false);
181
    todo1->setCreated(yesterday);
182 183 184 185
    todo1->setRevision(50);
    todo1->setDtDue(yesterday);
    todo1->setDtStart(today);
    todo1->setPercentComplete(50);
Laurent Montel's avatar
Laurent Montel committed
186
    todo1->setLocation(QStringLiteral("<b>location</b>"), false);
187

Laurent Montel's avatar
Laurent Montel committed
188 189 190
    todo2->setDescription(QStringLiteral("<b>description</b>"), true);
    todo2->setSummary(QStringLiteral("<b>Summary2</b>"), true);
    todo2->setLocation(QStringLiteral("<b>location</b>"), true);
191 192 193 194 195
    todo2->setDtDue(yesterday);
    todo2->setPercentComplete(100);

    todo3->setDtStart(today);
    todo3->setPercentComplete(100);
Allen Winter's avatar
Allen Winter committed
196 197 198 199 200 201
    todo3->setCategories(QStringList()
                         << QStringLiteral("a") << QStringLiteral("b")
                         << QStringLiteral("c") << QStringLiteral("d"));
    todo3->setResources(QStringList()
                        << QStringLiteral("a") << QStringLiteral("b")
                        << QStringLiteral("c") << QStringLiteral("d"));
202 203 204 205 206 207
    todo3->setPriority(5);

    QVERIFY(!todo4->dirtyFields().contains(IncidenceBase::FieldRecurrence));
    todo4->recurrence()->setDaily(1);
    QVERIFY(todo4->dirtyFields().contains(IncidenceBase::FieldRecurrence));

208
    Attachment attachment(QStringLiteral("http://www.kde.org"));
209 210 211 212 213 214
    todo4->addAttachment(attachment);

    todo5->recurrence()->setDaily(1);
    todo5->setCompleted(today);
    todo5->setStatus(Incidence::StatusDraft);
    todo5->setSecrecy(Incidence::SecrecyPrivate);
Laurent Montel's avatar
Laurent Montel committed
215
    todo5->setRelatedTo(QStringLiteral("uid1"), Incidence::RelTypeParent);
216 217 218
    todo5->setHasGeo(true);
    todo5->setGeoLatitude(40);
    todo5->setGeoLongitude(40);
Laurent Montel's avatar
Laurent Montel committed
219
    todo5->setOrganizer(QStringLiteral("organizer@mail.com"));
220 221 222

    todo6->recurrence()->setDaily(1);
    todo6->setCompleted(today);
223
    todo6->setRecurrenceId(yesterday);
224 225
    todo6->setStatus(Incidence::StatusDraft);
    todo6->setSecrecy(Incidence::SecrecyPrivate);
Laurent Montel's avatar
Laurent Montel committed
226
    todo6->setRelatedTo(QStringLiteral("uid1"), Incidence::RelTypeParent);
227 228 229
    todo6->setHasGeo(true);
    todo6->setGeoLatitude(40);
    todo6->setGeoLongitude(40);
Laurent Montel's avatar
Laurent Montel committed
230
    todo6->setUid(QStringLiteral("uid22"));
231
    todo6->setLastModified(today);
Laurent Montel's avatar
Laurent Montel committed
232
    todo6->addContact(QStringLiteral("addContact"));
233 234 235 236 237 238 239 240 241

    // Remaining properties tested in testevent.cpp

    QTest::newRow("todo1") << todo1;
    QTest::newRow("todo2") << todo2;
    QTest::newRow("todo3") << todo3;
    QTest::newRow("todo4") << todo4;
    QTest::newRow("todo5") << todo5;
    QTest::newRow("todo6") << todo6;
242 243 244 245
}

void TodoTest::testSerializer()
{
Volker Krause's avatar
Volker Krause committed
246 247
    QFETCH(KCalendarCore::Todo::Ptr, todo);
    IncidenceBase::Ptr incidenceBase = todo.staticCast<KCalendarCore::IncidenceBase>();
248 249 250 251 252 253

    QByteArray array;
    QDataStream stream(&array, QIODevice::WriteOnly);
    stream << incidenceBase;

    Todo::Ptr todo2 = Todo::Ptr(new Todo());
Volker Krause's avatar
Volker Krause committed
254
    IncidenceBase::Ptr incidenceBase2 = todo2.staticCast<KCalendarCore::IncidenceBase>();
255 256 257 258
    QVERIFY(*todo != *todo2);
    QDataStream stream2(&array, QIODevice::ReadOnly);
    stream2 >> incidenceBase2;
    QVERIFY(*todo == *todo2);
259
}
260 261 262

void TodoTest::testRoles()
{
263 264
    const QDateTime today = QDateTime::currentDateTimeUtc();
    const QDateTime yesterday = today.addDays(-1);
265 266 267 268 269
    Todo todo;
    todo.setDtStart(today.addDays(-1));
    todo.setDtDue(today);
    QCOMPARE(todo.dateTime(Incidence::RoleDisplayStart), today);
    QCOMPARE(todo.dateTime(Incidence::RoleDisplayEnd), today);
270
    todo.setDtDue(QDateTime());
271 272 273
    QCOMPARE(todo.dateTime(Incidence::RoleDisplayStart), yesterday);
    QCOMPARE(todo.dateTime(Incidence::RoleDisplayEnd), yesterday);
}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

void TodoTest::testIconNameOneoff()
{
    const QDateTime now = QDateTime::currentDateTime();
    Todo todo;
    todo.setDtStart(now);

    QCOMPARE(todo.iconName(), QLatin1String("view-calendar-tasks"));
    todo.setCompleted(now);
    QCOMPARE(todo.iconName(), QLatin1String("task-complete"));
}

void TodoTest::testIconNameRecurringNeverDue()
{
    const QDateTime now = QDateTime::currentDateTime();
    Todo todo;
    todo.setDtStart(now);
    todo.recurrence()->setDaily(1);

    QCOMPARE(todo.iconName(now), QLatin1String("view-calendar-tasks"));

    todo.setCompleted(now);
    QCOMPARE(todo.iconName(now), QLatin1String("task-complete"));
    QCOMPARE(todo.iconName(now.addDays(1)), QLatin1String("view-calendar-tasks"));
}

void TodoTest::testIconNameRecurringDue()
{
    const QDateTime now = QDateTime::currentDateTime();
    const QDateTime later = now.addSecs(3600);
    Todo todo;
    todo.setDtStart(now);
    todo.setDtDue(later, /*first=*/true);
    todo.recurrence()->setDaily(1);

    QCOMPARE(todo.iconName(now), QLatin1String("view-calendar-tasks"));
    QCOMPARE(todo.iconName(later), QLatin1String("view-calendar-tasks")); // Legacy case

    todo.setCompleted(now);
    QCOMPARE(todo.iconName(now), QLatin1String("task-complete"));
    QCOMPARE(todo.iconName(later), QLatin1String("task-complete")); // Legacy case
    QCOMPARE(todo.iconName(now.addDays(1)), QLatin1String("view-calendar-tasks"));
}