File: testrecurtodo.cpp

package info (click to toggle)
kcalcore 4%3A18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,704 kB
  • sloc: cpp: 22,533; perl: 136; sh: 11; makefile: 5
file content (263 lines) | stat: -rw-r--r-- 9,746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
  This file is part of the kcalcore library.

  Copyright (c) 2011 Sérgio Martins <iamsergio@gmail.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/
#include "testrecurtodo.h"
#include "todo.h"
#include "utils.h"

#include <QDebug>
#include <QTest>

QTEST_MAIN(RecurTodoTest)

using namespace KCalCore;

void RecurTodoTest::testAllDay()
{
    qputenv("TZ", "GMT");
    const QDate currentDate = QDate::currentDate();
    const QDateTime currentUtcDateTime = QDateTime::currentDateTimeUtc();

    const QDate dueDate(QDate::currentDate());
    Todo *todo = new Todo();
    todo->setDtStart(QDateTime(dueDate.addDays(-1), {}));
    todo->setDtDue(QDateTime(dueDate, {}));
    todo->setSummary(QStringLiteral("All day event"));
    todo->setAllDay(true);

    QCOMPARE(todo->dtStart().daysTo(todo->dtDue()), 1);

    Recurrence *recurrence = todo->recurrence();
    recurrence->unsetRecurs();
    recurrence->setDaily(1);
    QCOMPARE(todo->dtDue(), QDateTime(dueDate, {}));
    todo->setCompleted(currentUtcDateTime);
    QVERIFY(todo->recurs());
    QVERIFY(todo->percentComplete() == 0);
    const QDate newStartDate = todo->dtStart().date();
    const QDate newDueDate = todo->dtDue().date();
    QCOMPARE(newStartDate, currentDate);
    QCOMPARE(newStartDate.daysTo(newDueDate), 1);

    todo->setCompleted(currentUtcDateTime);

    QCOMPARE(newDueDate, currentDate.addDays(1));
    QCOMPARE(todo->dtDue(true /*first ocurrence*/).date(), dueDate);
}

void RecurTodoTest::testRecurrenceStart()
{
    qputenv("TZ", "GMT");
    const QDateTime currentDateTime = QDateTime::currentDateTime();
    const QDate currentDate = currentDateTime.date();
    const QTime currentTimeWithMS = currentDateTime.time();

    const QDate fourDaysAgo(currentDate.addDays(-4));
    const QDate treeDaysAgo(currentDate.addDays(-3));
    const QTime currentTime(currentTimeWithMS.hour(), currentTimeWithMS.minute(), currentTimeWithMS.second());

    Todo *todo = new Todo();
    Recurrence *recurrence = todo->recurrence();
    recurrence->unsetRecurs();
    recurrence->setDaily(1);
    todo->setDtStart(QDateTime(fourDaysAgo, currentTime));
    const QDateTime originalDtDue(treeDaysAgo, currentTime);
    todo->setDtDue(originalDtDue);
    todo->setSummary(QStringLiteral("Not an all day event"));
    QVERIFY(!todo->allDay());
    QVERIFY(recurrence->startDateTime().isValid());
}

void RecurTodoTest::testNonAllDay()
{
    qputenv("TZ", "GMT");
    const QDateTime currentDateTime = QDateTime::currentDateTime();
    const QDate currentDate = currentDateTime.date();
    const QTime currentTimeWithMS = currentDateTime.time();

    const QDate fourDaysAgo(currentDate.addDays(-4));
    const QDate treeDaysAgo(currentDate.addDays(-3));
    const QTime currentTime(currentTimeWithMS.hour(), currentTimeWithMS.minute(), currentTimeWithMS.second());

    Todo *todo = new Todo();
    todo->setDtStart(QDateTime(fourDaysAgo, currentTime));
    const QDateTime originalDtDue(treeDaysAgo, currentTime);
    todo->setDtDue(originalDtDue);
    todo->setSummary(QStringLiteral("Not an all day event"));
    QVERIFY(!todo->allDay());
    Recurrence *recurrence = todo->recurrence();
    recurrence->unsetRecurs();
    recurrence->setDaily(1);
    QVERIFY(recurrence->startDateTime().isValid());
    QCOMPARE(todo->dtDue(), originalDtDue);
    todo->setCompleted(QDateTime::currentDateTimeUtc());
    QVERIFY(todo->recurs());
    QVERIFY(todo->percentComplete() == 0);

    const bool equal = todo->dtStart() == QDateTime(currentDate, currentTime, todo->dtStart().timeZone()).addDays(1);
    if (!equal) {
        qDebug() << "Test Failed. dtDue = " << todo->dtDue().toString() << "OriginalDtDue:" << originalDtDue.toString()
                 <<  "QDateTime:"
                 << QDateTime(currentDate, currentTime, todo->dtDue().timeZone()).addDays(1).toString();
    }

    QVERIFY(equal);

    todo->setCompleted(QDateTime::currentDateTimeUtc());
    QCOMPARE(todo->dtStart(), QDateTime(currentDate, currentTime, todo->dtStart().timeZone()).addDays(2));
    QCOMPARE(todo->dtDue(true /*first ocurrence*/), QDateTime(treeDaysAgo, currentTime));
}

void RecurTodoTest::testIsAllDay()
{
    KCalCore::Todo::Ptr todo(new KCalCore::Todo());
    todo->setUid(QStringLiteral("todo"));
    todo->setDtStart(QDateTime(QDate(2013, 03, 10), QTime(10, 0, 0), Qt::UTC));
    todo->setDtDue(QDateTime(QDate(2013, 03, 10), QTime(10, 0, 0), Qt::UTC));
    todo->recurrence()->setDaily(1);
    todo->recurrence()->setDuration(2);
    QCOMPARE(todo->allDay(), false);
    QCOMPARE(todo->recurrence()->allDay(), false);

    KCalCore::Todo::Ptr allDay(new KCalCore::Todo());
    allDay->setUid(QStringLiteral("todo"));
    allDay->setDtStart(QDateTime(QDate(2013, 03, 10), {}, Qt::UTC));
    allDay->setDtDue(QDateTime(QDate(2013, 03, 10), {}, Qt::UTC));
    allDay->setAllDay(true);
    allDay->recurrence()->setDaily(1);
    allDay->recurrence()->setDuration(2);
    QCOMPARE(allDay->allDay(), true);
    QCOMPARE(allDay->recurrence()->allDay(), true);
}

void RecurTodoTest::testHasDueDate()
{
    KCalCore::Todo::Ptr todo(new KCalCore::Todo());
    todo->setUid(QStringLiteral("todo"));
    todo->setDtStart(QDateTime(QDate(2013, 03, 10), QTime(10, 0, 0), Qt::UTC));
    todo->recurrence()->setDaily(1);
    todo->recurrence()->setDuration(2);
    QVERIFY(!todo->hasDueDate());
}

void RecurTodoTest::testRecurTodo_data()
{
    QTest::addColumn<QDateTime>("dtstart");
    QTest::addColumn<QDateTime>("dtdue");

    // Can't use QDateTime::currentDateTimeUtc() due to milliseconds mismatching
    const QDateTime today = QDateTime::fromSecsSinceEpoch(QDateTime::currentSecsSinceEpoch(), Qt::UTC);
    const QDateTime tomorrow = today.addDays(1);
    const QDateTime invalid;

    QTest::newRow("valid dtstart") << today << invalid;
    QTest::newRow("valid dtstart and dtdue") << today << tomorrow;
    QTest::newRow("valid dtdue") << invalid << today;
}

void RecurTodoTest::testRecurTodo()
{
    QFETCH(QDateTime, dtstart);
    QFETCH(QDateTime, dtdue);

    KCalCore::Todo::Ptr todo(new KCalCore::Todo());
    todo->setUid(QStringLiteral("todo"));
    todo->setDtStart(dtstart);
    todo->setDtDue(dtdue);
    todo->recurrence()->setDaily(1);

    const bool legacyMode = !dtstart.isValid();
    QCOMPARE(todo->percentComplete(), 0);

    // Recur it
    todo->setCompleted(QDateTime::currentDateTimeUtc());
    QCOMPARE(todo->percentComplete(), 0);

    if (legacyMode) {
        QVERIFY(todo->dtDue().isValid());
        QVERIFY(!todo->dtStart().isValid());
        QCOMPARE(todo->dtDue(), dtdue.addDays(1));

        QCOMPARE(todo->dtDue(/**first=*/ true), dtdue);
    } else {
        QVERIFY(todo->dtStart().isValid());
        QVERIFY(!(todo->dtDue().isValid() ^ dtdue.isValid()));
        QCOMPARE(todo->dtStart(), dtstart.addDays(1));

        if (dtdue.isValid()) {
            const int delta = dtstart.daysTo(dtdue);
            QCOMPARE(todo->dtStart().daysTo(todo->dtDue()), delta);
        }

        QCOMPARE(todo->dtStart(/**first=*/ true), dtstart);
    }
}

void RecurTodoTest::testDtStart()
{
    QDateTime start(QDate(2013, 03, 10), QTime(10, 0, 0), Qt::UTC);
    KCalCore::Todo::Ptr todo(new KCalCore::Todo());
    todo->setUid(QStringLiteral("todo"));
    todo->setDtStart(start);
    todo->recurrence()->setDaily(1);
    todo->recurrence()->setDuration(2);
    QCOMPARE(todo->dtStart(), start);

    KCalCore::Todo::Ptr todoWithDue(new KCalCore::Todo());
    todoWithDue->setUid(QStringLiteral("todoWithDue"));
    todoWithDue->setDtStart(start);
    todoWithDue->setDtDue(QDateTime(start).addSecs(60));
    todoWithDue->recurrence()->setDaily(1);
    todoWithDue->recurrence()->setDuration(2);
    QCOMPARE(todoWithDue->dtStart(), start);
}

void RecurTodoTest::testRecurrenceBasedOnDtStart()
{
    const QDateTime dtstart(QDate(2013, 03, 10), QTime(10, 0, 0), Qt::UTC);
    const QDateTime dtdue(QDate(2013, 03, 10), QTime(11, 0, 0), Qt::UTC);

    KCalCore::Todo::Ptr todo(new KCalCore::Todo());
    todo->setUid(QStringLiteral("todo"));
    todo->setDtStart(dtstart);
    todo->setDtDue(dtdue);
    todo->recurrence()->setDaily(1);
    todo->recurrence()->setDuration(3);

    QCOMPARE(todo->recurrence()->getNextDateTime(dtstart), dtstart.addDays(1));
    QCOMPARE(todo->recurrence()->getNextDateTime(dtstart.addDays(1)), dtstart.addDays(2));
    QCOMPARE(todo->recurrence()->getNextDateTime(dtstart.addDays(2)), QDateTime());
}

//For backwards compatibility only
void RecurTodoTest::testRecurrenceBasedOnDue()
{
    const QDateTime dtdue(QDate(2013, 03, 10), QTime(11, 0, 0), Qt::UTC);

    KCalCore::Todo::Ptr todo(new KCalCore::Todo());
    todo->setUid(QStringLiteral("todo"));
    todo->setDtDue(dtdue);
    todo->recurrence()->setDaily(1);
    todo->recurrence()->setDuration(3);

    QCOMPARE(todo->recurrence()->getNextDateTime(dtdue), dtdue.addDays(1));
    QCOMPARE(todo->recurrence()->getNextDateTime(dtdue.addDays(1)), dtdue.addDays(2));
    QCOMPARE(todo->recurrence()->getNextDateTime(dtdue.addDays(2)), QDateTime());
}