1
/* This file is part of Zanshin Todo.
3
Copyright 2008 Kevin Ottens <ervin@kde.org>
4
Copyright 2008, 2009 Mario Bensi <nef@ipsquad.net>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License as
8
published by the Free Software Foundation; either version 2 of
9
the License or (at your option) version 3 or any later version
10
accepted by the membership of KDE e.V. (or its successor approved
11
by the membership of KDE e.V.), which shall act as a proxy
12
defined in Section 14 of version 3 of the license.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25
#include "modeltestbase.h"
27
#include <akonadi/collection.h>
28
#include <akonadi/item.h>
29
#include <akonadi/itemcreatejob.h>
30
#include <akonadi/itemdeletejob.h>
31
#include <akonadi/qtest_akonadi.h>
33
#include <boost/shared_ptr.hpp>
35
#include <kcal/todo.h>
37
#include <QtGui/QSortFilterProxyModel>
39
#include "todoflatmodel.h"
41
typedef boost::shared_ptr<KCal::Incidence> IncidencePtr;
43
class TodoFlatModelTest : public ModelTestBase
49
void testInitialState_data();
50
void testInitialState();
51
void testItemModification_data();
52
void testItemModification();
53
void testSingleRemoved();
54
void testRowTypeDuringInsert();
56
void onInsertRows(const QModelIndex &parent, int begin, int end);
58
TodoFlatModel m_model;
59
QSortFilterProxyModel m_sortedModel;
62
QTEST_AKONADIMAIN(TodoFlatModelTest, GUI)
64
void TodoFlatModelTest::initTestCase()
66
ModelTestBase::initTestCase();
67
m_model.setCollection(m_collection);
70
m_sortedModel.setSourceModel(&m_model);
71
m_sortedModel.sort(TodoFlatModel::RemoteId);
74
void TodoFlatModelTest::testInitialState_data()
76
QTest::addColumn<int>("row");
77
QTest::addColumn<QString>("remoteId");
78
QTest::addColumn<QString>("summary");
79
QTest::addColumn<QStringList>("categories");
80
QTest::addColumn<QString>("parentRemoteId");
81
QTest::addColumn<QString>("dueDate");
82
QTest::addColumn<bool>("isImportant");
84
QTest::newRow("0") << 0 << "fake-01" << "Becoming Astronaut" << QStringList() << "fake-12" << "" << false;
85
QTest::newRow("1") << 1 << "fake-02" << "Look at the stars" << QStringList() << "fake-01" << "" << false;
86
QTest::newRow("2") << 2 << "fake-03" << "Walk around with the dog"
87
<< (QStringList() << "Errands") << "fake-10" << "2008-12-25" << false;
88
QTest::newRow("3") << 3 << "fake-04" << "Second Folder" << QStringList() << "" << "" << false;
89
QTest::newRow("4") << 4 << "fake-05" << "Feed the dog"
90
<< (QStringList() << "Home") << "fake-10" << "" << false;
91
QTest::newRow("5") << 5 << "fake-06" << "Read magazine"
92
<< (QStringList() << "Office" << "Computer") << "fake-11" << "" << false;
93
QTest::newRow("6") << 6 << "fake-07" << "Learn the constellations" << QStringList() << "fake-01" << "" << false;
94
QTest::newRow("7") << 7 << "fake-08" << "Choose a puppy" << QStringList() << "fake-10" << "" << false;
95
QTest::newRow("8") << 8 << "fake-09" << "Listen new age album 2" << QStringList() << "fake-11" << "" << false;
96
QTest::newRow("9") << 9 << "fake-10" << "Pet Project" << QStringList() << "fake-04" << "" << false;
97
QTest::newRow("10") << 10 << "fake-11" << "Becoming more relaxed" << QStringList() << "fake-12" << "" << false;
98
QTest::newRow("11") << 11 << "fake-12" << "First Folder" << QStringList() << "" << "" << false;
99
QTest::newRow("12") << 12 << "fake-14" << "Choose a kitty" << QStringList() << "" << "" << false;
102
void TodoFlatModelTest::testInitialState()
105
QFETCH(QString, remoteId);
106
QFETCH(QString, summary);
107
QFETCH(QStringList, categories);
108
QFETCH(QString, parentRemoteId);
109
QFETCH(QString, dueDate);
110
QFETCH(bool, isImportant);
112
QCOMPARE(m_sortedModel.rowCount(), 13);
113
QCOMPARE(m_sortedModel.columnCount(), 7);
115
QModelIndex index = m_sortedModel.index(row, TodoFlatModel::RemoteId);
116
QCOMPARE(m_sortedModel.data(index).toString(), remoteId);
117
QCOMPARE(m_sortedModel.rowCount(index), 0);
118
QCOMPARE(m_sortedModel.columnCount(index), 0);
120
index = m_sortedModel.index(row, TodoFlatModel::Summary);
121
QCOMPARE(m_sortedModel.data(index).toString(), summary);
122
QCOMPARE(m_sortedModel.rowCount(index), 0);
123
QCOMPARE(m_sortedModel.columnCount(index), 0);
125
index = m_sortedModel.index(row, TodoFlatModel::Categories);
126
QCOMPARE(m_sortedModel.data(index).toStringList(), categories);
127
QCOMPARE(m_sortedModel.rowCount(index), 0);
128
QCOMPARE(m_sortedModel.columnCount(index), 0);
130
index = m_sortedModel.index(row, TodoFlatModel::ParentRemoteId);
131
QCOMPARE(m_sortedModel.data(index).toString(), parentRemoteId);
132
QCOMPARE(m_sortedModel.rowCount(index), 0);
133
QCOMPARE(m_sortedModel.columnCount(index), 0);
135
index = m_sortedModel.index(row, TodoFlatModel::DueDate);
136
QCOMPARE(m_sortedModel.data(index).toString(), dueDate);
137
QCOMPARE(m_sortedModel.rowCount(index), 0);
138
QCOMPARE(m_sortedModel.columnCount(index), 0);
141
void TodoFlatModelTest::testItemModification_data()
143
QTest::addColumn<int>("row");
144
QTest::addColumn<int>("column");
145
QTest::addColumn<QString>("newData");
146
QTest::addColumn<bool>("expected");
148
QTest::newRow("Changing Summary") << 2 << (int)TodoFlatModel::Summary << "Walk around with the CAT" << true;
149
QTest::newRow("Changing Summary (bis)") << 2 << (int)TodoFlatModel::Summary << "Walk around with the dog" << true;
150
QTest::newRow("New Parent") << 1 << (int)TodoFlatModel::ParentRemoteId << "fake-10" << true;
151
QTest::newRow("Wrong Parent") << 1 << (int)TodoFlatModel::ParentRemoteId << "zonzog" << false;
152
QTest::newRow("New Categories") << 3 << (int)TodoFlatModel::Categories << "Computer, Office" << true;
153
QTest::newRow("New Due Date") << 6 << (int)TodoFlatModel::DueDate << "2009-03-14" << true;
154
QTest::newRow("Wrong Due Date") << 6 << (int)TodoFlatModel::DueDate << "2009-42-21" << false;
155
QTest::newRow("Cycle Prevention") << 11 << (int)TodoFlatModel::ParentRemoteId << "fake-02" << false;
158
void TodoFlatModelTest::testItemModification()
162
QFETCH(QString, newData);
163
QFETCH(bool, expected);
165
QSignalSpy spy(&m_sortedModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
167
QModelIndex index = m_sortedModel.index(row, column);
168
bool result = m_sortedModel.setData(index, newData);
169
QCOMPARE(result, expected);
171
flushNotifications();
174
QCOMPARE(spy.count(), 0);
178
QCOMPARE(m_sortedModel.data(index).toStringList().join(", "), newData);
180
QCOMPARE(spy.count(), 1);
181
QVariantList signal = spy.takeFirst();
182
QCOMPARE(signal.count(), 2);
183
QCOMPARE(signal.at(0).value<QModelIndex>(), m_sortedModel.index(row, 0));
184
QCOMPARE(signal.at(1).value<QModelIndex>(), m_sortedModel.index(row, TodoFlatModel::LastColumn));
187
void TodoFlatModelTest::testSingleRemoved()
189
Akonadi::Item item = m_model.itemForIndex(m_sortedModel.mapToSource(m_sortedModel.index(1, 0)));
191
int count = m_model.rowCount();
193
QSignalSpy spy(&m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
195
Akonadi::ItemDeleteJob *job = new Akonadi::ItemDeleteJob(item);
196
QVERIFY(job->exec());
198
flushNotifications();
200
QCOMPARE(m_model.rowCount(), count - 1);
202
QCOMPARE(spy.count(), 1);
203
QVariantList signal = spy.takeFirst();
204
QCOMPARE(signal.count(), 3);
205
QCOMPARE(signal.at(0).value<QModelIndex>(), QModelIndex());
206
QCOMPARE(signal.at(1).toInt(), 1);
207
QCOMPARE(signal.at(1).toInt(), 1);
211
void TodoFlatModelTest::testRowTypeDuringInsert()
213
int count = m_model.rowCount();
215
connect(&m_model, SIGNAL(rowsInserted(QModelIndex,int,int)),
216
this, SLOT(onInsertRows(QModelIndex, int, int)));
218
KCal::Todo *todo = new KCal::Todo();
219
todo->setSummary("foo folder");
220
todo->addComment("X-Zanshin-Folder");
221
IncidencePtr incidence(todo);
224
item.setMimeType("application/x-vnd.akonadi.calendar.todo");
225
item.setPayload<IncidencePtr>(incidence);
227
Akonadi::Collection collection = m_model.collection();
228
Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob(item, collection);
229
QVERIFY(job->exec());
231
flushNotifications();
233
QCOMPARE(m_model.rowCount(), count+1);
235
disconnect(&m_model, SIGNAL(rowsInserted(QModelIndex,int,int)),
236
this, SLOT(onInsertRows(QModelIndex, int, int)));
239
void TodoFlatModelTest::onInsertRows(const QModelIndex &parent, int begin, int end)
241
QVERIFY(!parent.isValid());
244
QModelIndex index = m_model.index(begin, TodoFlatModel::RowType);
245
QCOMPARE(m_model.data(index).toInt(), (int)TodoFlatModel::FolderTodo);
248
#include "todoflatmodeltest.moc"