~ubuntu-branches/ubuntu/trusty/kdepimlibs/trusty

« back to all changes in this revision

Viewing changes to akonadi/calendar/tests/todopurgertest.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg, Rohan Garg, Philip Muškovac
  • Date: 2013-11-23 17:36:44 UTC
  • mfrom: (1.1.102)
  • Revision ID: package-import@ubuntu.com-20131123173644-p5ow94192ezsny8g
Tags: 4:4.11.80-0ubuntu1
[ Rohan Garg ]
* New upstream beta release
  - Bump akonadi requirement to 1.10.45
  - Update install files
  - Update symbols

[ Philip Muškovac ]
* kdepimlibs-dev/-dbg breaks/replaces kdepim-runtime/-dbg (<< 4:4.11.80)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2013 Sérgio Martins <iamsergio@gmail.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
    02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "todopurgertest.h"
 
21
 
 
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>
 
32
 
 
33
#include <QTestEventLoop>
 
34
 
 
35
using namespace Akonadi;
 
36
using namespace KCalCore;
 
37
 
 
38
Q_DECLARE_METATYPE( QSet<QByteArray> )
 
39
 
 
40
void TodoPurgerTest::createTodo(const QString &uid, const QString &parentUid, bool completed, bool recurring )
 
41
{
 
42
    Item item;
 
43
    item.setMimeType(Todo::todoMimeType());
 
44
    Todo::Ptr todo = Todo::Ptr(new Todo());
 
45
    todo->setUid(uid);
 
46
 
 
47
    const KDateTime today = KDateTime::currentDateTime(KDateTime::UTC);
 
48
    const KDateTime yesterday = today.addDays(-1);
 
49
 
 
50
    todo->setDtStart(yesterday);
 
51
    todo->setRelatedTo(parentUid);
 
52
 
 
53
    if (recurring)
 
54
        todo->recurrence()->setDaily(1);
 
55
 
 
56
    if (recurring && completed) {
 
57
        todo->setCompleted(today);
 
58
    } else {
 
59
        todo->setCompleted(completed);
 
60
    }
 
61
 
 
62
    todo->setSummary(QLatin1String("summary"));
 
63
 
 
64
 
 
65
    item.setPayload<KCalCore::Incidence::Ptr>(todo);
 
66
    ItemCreateJob *job = new ItemCreateJob(item, m_collection, this);
 
67
    m_pendingCreations++;
 
68
    AKVERIFYEXEC(job);
 
69
}
 
70
 
 
71
void TodoPurgerTest::fetchCollection()
 
72
{
 
73
    CollectionFetchJob *job = new CollectionFetchJob(Collection::root(),
 
74
                                                     CollectionFetchJob::Recursive,
 
75
                                                     this);
 
76
    // Get list of collections
 
77
    job->fetchScope().setContentMimeTypes(QStringList() << QLatin1String("application/x-vnd.akonadi.calendar.todo"));
 
78
    AKVERIFYEXEC(job);
 
79
 
 
80
    // Find our collection
 
81
    Collection::List collections = job->collections();
 
82
    QVERIFY(!collections.isEmpty());
 
83
    m_collection = collections.first();
 
84
 
 
85
    QVERIFY(m_collection.isValid());
 
86
}
 
87
 
 
88
void TodoPurgerTest::initTestCase()
 
89
{
 
90
    AkonadiTest::checkTestIsIsolated();
 
91
 
 
92
    qRegisterMetaType<QSet<QByteArray> >("QSet<QByteArray>");
 
93
    fetchCollection();
 
94
 
 
95
    m_pendingCreations = 0;
 
96
    m_pendingDeletions = 0;
 
97
    m_calendar = new ETMCalendar();
 
98
    m_calendar->registerObserver(this);
 
99
    m_todoPurger = new TodoPurger(this);
 
100
 
 
101
    connect(m_todoPurger, SIGNAL(todosPurged(bool,int,int)), SLOT(onTodosPurged(bool,int,int)));
 
102
 
 
103
    connect(m_calendar, SIGNAL(collectionsAdded(Akonadi::Collection::List)),
 
104
            &QTestEventLoop::instance(), SLOT(exitLoop()));
 
105
 
 
106
    // Wait for the collection
 
107
    QTestEventLoop::instance().enterLoop(10);
 
108
    QVERIFY(!QTestEventLoop::instance().timeout());
 
109
 
 
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);
 
114
}
 
115
 
 
116
void TodoPurgerTest::cleanupTestCase()
 
117
{
 
118
    delete m_calendar;
 
119
}
 
120
 
 
121
void TodoPurgerTest::testPurge()
 
122
{
 
123
    createTree();
 
124
 
 
125
    m_pendingDeletions = 8;
 
126
    m_pendingPurgeSignal = true;
 
127
 
 
128
    m_todoPurger->purgeCompletedTodos();
 
129
 
 
130
    // Wait for deletions and purged signal
 
131
    QTestEventLoop::instance().enterLoop(10);
 
132
    QVERIFY(!QTestEventLoop::instance().timeout());
 
133
 
 
134
    QCOMPARE(m_numDeleted, 10);
 
135
    QCOMPARE(m_numIgnored, 2);
 
136
}
 
137
 
 
138
void TodoPurgerTest::calendarIncidenceAdded(const Incidence::Ptr &)
 
139
{
 
140
    --m_pendingCreations;
 
141
    if (m_pendingCreations == 0) {
 
142
        QTestEventLoop::instance().exitLoop();
 
143
    }
 
144
}
 
145
 
 
146
void TodoPurgerTest::calendarIncidenceDeleted(const Incidence::Ptr &)
 
147
{
 
148
    --m_pendingDeletions;
 
149
    if (m_pendingDeletions == 0 && !m_pendingPurgeSignal) {
 
150
        QTestEventLoop::instance().exitLoop();
 
151
    }
 
152
}
 
153
 
 
154
void TodoPurgerTest::onTodosPurged(bool success, int numDeleted, int numIgnored)
 
155
{
 
156
    QVERIFY(success);
 
157
    m_pendingPurgeSignal = false;
 
158
    m_numDeleted = numDeleted;
 
159
    m_numIgnored = numIgnored;
 
160
 
 
161
    if (m_pendingDeletions == 0) {
 
162
        QTestEventLoop::instance().enterLoop(10);
 
163
        QVERIFY(!QTestEventLoop::instance().timeout());
 
164
    }
 
165
}
 
166
 
 
167
void TodoPurgerTest::createTree()
 
168
{
 
169
    createTodo(tr("a"), QString(), true);  // Will be deleted
 
170
    createTodo(tr("b"), QString(), false); // Won't be deleted
 
171
 
 
172
    // Completed tree
 
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
 
177
 
 
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
 
183
 
 
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
 
189
 
 
190
    // Recurring uncomplete
 
191
    createTodo(tr("f"), QString(), false, true); // Won't be deleted
 
192
 
 
193
    // Recurring complete, this one is not deleted because recurrence didn't end
 
194
    createTodo(tr("g"), QString(), true, true); // Won't be deleted
 
195
 
 
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
 
200
 
 
201
 
 
202
    // Now wait for incidences do be created
 
203
    QTestEventLoop::instance().enterLoop(10);
 
204
    QVERIFY(!QTestEventLoop::instance().timeout());
 
205
}
 
206
 
 
207
QTEST_AKONADIMAIN(TodoPurgerTest, GUI)