2
Copyright (c) 2013 Sérgio Martins <iamsergio@gmail.com>
4
This library is free software; you can redistribute it and/or modify it
5
under the terms of the GNU Library General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or (at your
7
option) any later version.
9
This library is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12
License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to the
16
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
#include "todopurgertest.h"
22
#include "../etmcalendar.h"
23
#include "../todopurger.h"
24
#include <akonadi/itemcreatejob.h>
25
#include <akonadi/qtest_akonadi.h>
26
#include <akonadi/collectionfetchjob.h>
27
#include <akonadi/collectionfetchscope.h>
28
#include <akonadi/collectionmodifyjob.h>
29
#include <akonadi/itemdeletejob.h>
30
#include <akonadi/itemmodifyjob.h>
31
#include <KCheckableProxyModel>
33
#include <QTestEventLoop>
35
using namespace Akonadi;
36
using namespace KCalCore;
38
Q_DECLARE_METATYPE( QSet<QByteArray> )
40
void TodoPurgerTest::createTodo(const QString &uid, const QString &parentUid, bool completed, bool recurring )
43
item.setMimeType(Todo::todoMimeType());
44
Todo::Ptr todo = Todo::Ptr(new Todo());
47
const KDateTime today = KDateTime::currentDateTime(KDateTime::UTC);
48
const KDateTime yesterday = today.addDays(-1);
50
todo->setDtStart(yesterday);
51
todo->setRelatedTo(parentUid);
54
todo->recurrence()->setDaily(1);
56
if (recurring && completed) {
57
todo->setCompleted(today);
59
todo->setCompleted(completed);
62
todo->setSummary(QLatin1String("summary"));
65
item.setPayload<KCalCore::Incidence::Ptr>(todo);
66
ItemCreateJob *job = new ItemCreateJob(item, m_collection, this);
71
void TodoPurgerTest::fetchCollection()
73
CollectionFetchJob *job = new CollectionFetchJob(Collection::root(),
74
CollectionFetchJob::Recursive,
76
// Get list of collections
77
job->fetchScope().setContentMimeTypes(QStringList() << QLatin1String("application/x-vnd.akonadi.calendar.todo"));
80
// Find our collection
81
Collection::List collections = job->collections();
82
QVERIFY(!collections.isEmpty());
83
m_collection = collections.first();
85
QVERIFY(m_collection.isValid());
88
void TodoPurgerTest::initTestCase()
90
AkonadiTest::checkTestIsIsolated();
92
qRegisterMetaType<QSet<QByteArray> >("QSet<QByteArray>");
95
m_pendingCreations = 0;
96
m_pendingDeletions = 0;
97
m_calendar = new ETMCalendar();
98
m_calendar->registerObserver(this);
99
m_todoPurger = new TodoPurger(this);
101
connect(m_todoPurger, SIGNAL(todosPurged(bool,int,int)), SLOT(onTodosPurged(bool,int,int)));
103
connect(m_calendar, SIGNAL(collectionsAdded(Akonadi::Collection::List)),
104
&QTestEventLoop::instance(), SLOT(exitLoop()));
106
// Wait for the collection
107
QTestEventLoop::instance().enterLoop(10);
108
QVERIFY(!QTestEventLoop::instance().timeout());
110
KCheckableProxyModel *checkable = m_calendar->checkableProxyModel();
111
const QModelIndex firstIndex = checkable->index(0, 0);
112
QVERIFY(firstIndex.isValid());
113
checkable->setData(firstIndex, Qt::Checked, Qt::CheckStateRole);
116
void TodoPurgerTest::cleanupTestCase()
121
void TodoPurgerTest::testPurge()
125
m_pendingDeletions = 8;
126
m_pendingPurgeSignal = true;
128
m_todoPurger->purgeCompletedTodos();
130
// Wait for deletions and purged signal
131
QTestEventLoop::instance().enterLoop(10);
132
QVERIFY(!QTestEventLoop::instance().timeout());
134
QCOMPARE(m_numDeleted, 10);
135
QCOMPARE(m_numIgnored, 2);
138
void TodoPurgerTest::calendarIncidenceAdded(const Incidence::Ptr &)
140
--m_pendingCreations;
141
if (m_pendingCreations == 0) {
142
QTestEventLoop::instance().exitLoop();
146
void TodoPurgerTest::calendarIncidenceDeleted(const Incidence::Ptr &)
148
--m_pendingDeletions;
149
if (m_pendingDeletions == 0 && !m_pendingPurgeSignal) {
150
QTestEventLoop::instance().exitLoop();
154
void TodoPurgerTest::onTodosPurged(bool success, int numDeleted, int numIgnored)
157
m_pendingPurgeSignal = false;
158
m_numDeleted = numDeleted;
159
m_numIgnored = numIgnored;
161
if (m_pendingDeletions == 0) {
162
QTestEventLoop::instance().enterLoop(10);
163
QVERIFY(!QTestEventLoop::instance().timeout());
167
void TodoPurgerTest::createTree()
169
createTodo(tr("a"), QString(), true); // Will be deleted
170
createTodo(tr("b"), QString(), false); // Won't be deleted
173
createTodo(tr("c"), QString(), true); // Will be deleted
174
createTodo(tr("c1"), tr("c"), true); // Will be deleted
175
createTodo(tr("c1.1"), tr("c1"), true); // Will be deleted
176
createTodo(tr("c1.2"), tr("c1"), true); // Will be deleted
178
// Root completed but children not completed
179
createTodo(tr("d"), QString(), true); // Will be ignored (uncomplete children)
180
createTodo(tr("d1"), tr("d"), false); // Won't be deleted
181
createTodo(tr("d1.1"), tr("d1"), false); // Won't be deleted
182
createTodo(tr("d1.2"), tr("d1"), false); // Won't be deleted
184
// Root uncomplete with children complete
185
createTodo(tr("e"), QString(), false); // Won't be deleted
186
createTodo(tr("e1"), tr("e"), true); // Will be deleted
187
createTodo(tr("e1.1"), tr("e1"), true); // Will be deleted
188
createTodo(tr("e1.2"), tr("e1"), true); // Will be deleted
190
// Recurring uncomplete
191
createTodo(tr("f"), QString(), false, true); // Won't be deleted
193
// Recurring complete, this one is not deleted because recurrence didn't end
194
createTodo(tr("g"), QString(), true, true); // Won't be deleted
196
createTodo(tr("h"), QString(), true); // Will be ignored (uncomplete children)
197
createTodo(tr("h1"), tr("h"), false); // Won't be deleted
198
createTodo(tr("h1.1"), tr("h1"), true); // Will be deleted
199
createTodo(tr("h1.2"), tr("h1"), true); // Will be deleted
202
// Now wait for incidences do be created
203
QTestEventLoop::instance().enterLoop(10);
204
QVERIFY(!QTestEventLoop::instance().timeout());
207
QTEST_AKONADIMAIN(TodoPurgerTest, GUI)