18
#include "TestEntryModel.h"
23
#include "core/Entry.h"
24
#include "core/Group.h"
25
#include "crypto/Crypto.h"
26
#include "gui/DatabaseIcons.h"
27
#include "gui/IconModels.h"
28
#include "gui/SortFilterHideProxyModel.h"
29
#include "gui/entry/AutoTypeAssociationsModel.h"
30
#include "gui/entry/EntryAttachmentsModel.h"
31
#include "gui/entry/EntryAttributesModel.h"
32
#include "gui/entry/EntryModel.h"
35
QTEST_GUILESS_MAIN(TestEntryModel)
37
void TestEntryModel::initTestCase()
39
qRegisterMetaType<QModelIndex>("QModelIndex");
40
QVERIFY(Crypto::init());
43
void TestEntryModel::test()
45
auto group1 = new Group();
46
auto group2 = new Group();
48
auto entry1 = new Entry();
49
entry1->setGroup(group1);
50
entry1->setTitle("testTitle1");
52
auto entry2 = new Entry();
53
entry2->setGroup(group1);
54
entry2->setTitle("testTitle2");
56
auto model = new EntryModel(this);
58
QSignalSpy spyAboutToBeMoved(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
59
QSignalSpy spyMoved(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
61
auto modelTest = new ModelTest(model, this);
63
model->setGroup(group1);
65
QCOMPARE(model->rowCount(), 2);
67
QSignalSpy spyDataChanged(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
68
entry1->setTitle("changed");
69
QCOMPARE(spyDataChanged.count(), 1);
71
QModelIndex index1 = model->index(0, 1);
72
QModelIndex index2 = model->index(1, 1);
74
QCOMPARE(model->data(index1).toString(), entry1->title());
75
QCOMPARE(model->data(index2).toString(), entry2->title());
77
QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
78
QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
79
QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
80
QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
82
auto entry3 = new Entry();
83
entry3->setGroup(group1);
85
QCOMPARE(spyAboutToBeMoved.count(), 0);
86
QCOMPARE(spyMoved.count(), 0);
89
QCOMPARE(spyAboutToBeMoved.count(), 1);
90
QCOMPARE(spyMoved.count(), 1);
93
QCOMPARE(spyAboutToBeMoved.count(), 2);
94
QCOMPARE(spyMoved.count(), 2);
97
QCOMPARE(spyAboutToBeMoved.count(), 2);
98
QCOMPARE(spyMoved.count(), 2);
101
QCOMPARE(spyAboutToBeMoved.count(), 3);
102
QCOMPARE(spyMoved.count(), 3);
105
QCOMPARE(spyAboutToBeMoved.count(), 3);
106
QCOMPARE(spyMoved.count(), 3);
108
QCOMPARE(spyAboutToAdd.count(), 1);
109
QCOMPARE(spyAdded.count(), 1);
110
QCOMPARE(spyAboutToRemove.count(), 0);
111
QCOMPARE(spyRemoved.count(), 0);
113
entry2->setGroup(group2);
115
QCOMPARE(spyAboutToAdd.count(), 1);
116
QCOMPARE(spyAdded.count(), 1);
117
QCOMPARE(spyAboutToRemove.count(), 1);
118
QCOMPARE(spyRemoved.count(), 1);
120
QSignalSpy spyReset(model, SIGNAL(modelReset()));
121
model->setGroup(group2);
122
QCOMPARE(spyReset.count(), 1);
131
void TestEntryModel::testAttachmentsModel()
133
auto entryAttachments = new EntryAttachments(this);
135
auto model = new EntryAttachmentsModel(this);
136
auto modelTest = new ModelTest(model, this);
138
QCOMPARE(model->rowCount(), 0);
139
model->setEntryAttachments(entryAttachments);
140
QCOMPARE(model->rowCount(), 0);
142
QSignalSpy spyDataChanged(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
143
QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
144
QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
145
QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
146
QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
148
entryAttachments->set("first", QByteArray("123"));
150
entryAttachments->set("2nd", QByteArray("456"));
151
entryAttachments->set("2nd", QByteArray("7890"));
153
const int firstRow = 0;
154
QCOMPARE(model->data(model->index(firstRow, EntryAttachmentsModel::NameColumn)).toString(), QString("2nd"));
155
QCOMPARE(model->data(model->index(firstRow, EntryAttachmentsModel::SizeColumn), Qt::EditRole).toInt(), 4);
157
entryAttachments->remove("first");
159
QCOMPARE(spyDataChanged.count(), 1);
160
QCOMPARE(spyAboutToAdd.count(), 2);
161
QCOMPARE(spyAdded.count(), 2);
162
QCOMPARE(spyAboutToRemove.count(), 1);
163
QCOMPARE(spyRemoved.count(), 1);
165
QSignalSpy spyReset(model, SIGNAL(modelReset()));
166
entryAttachments->clear();
167
model->setEntryAttachments(nullptr);
168
QCOMPARE(spyReset.count(), 2);
169
QCOMPARE(model->rowCount(), 0);
173
delete entryAttachments;
176
void TestEntryModel::testAttributesModel()
178
auto entryAttributes = new EntryAttributes(this);
180
auto model = new EntryAttributesModel(this);
181
auto modelTest = new ModelTest(model, this);
183
QCOMPARE(model->rowCount(), 0);
184
model->setEntryAttributes(entryAttributes);
185
QCOMPARE(model->rowCount(), 0);
187
QSignalSpy spyDataChanged(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
188
QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
189
QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
190
QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
191
QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
193
entryAttributes->set("first", "123");
195
entryAttributes->set("2nd", "456");
196
entryAttributes->set("2nd", "789");
198
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("2nd"));
200
entryAttributes->remove("first");
203
entryAttributes->set("Title", "test");
204
entryAttributes->set("Notes", "test");
206
QCOMPARE(spyDataChanged.count(), 1);
207
QCOMPARE(spyAboutToAdd.count(), 2);
208
QCOMPARE(spyAdded.count(), 2);
209
QCOMPARE(spyAboutToRemove.count(), 1);
210
QCOMPARE(spyRemoved.count(), 1);
213
QString value = entryAttributes->value("2nd");
214
entryAttributes->set("2nd", value, true);
215
QVERIFY(entryAttributes->isProtected("2nd"));
216
QCOMPARE(entryAttributes->value("2nd"), value);
218
QSignalSpy spyReset(model, SIGNAL(modelReset()));
219
entryAttributes->clear();
220
model->setEntryAttributes(nullptr);
221
QCOMPARE(spyReset.count(), 2);
222
QCOMPARE(model->rowCount(), 0);
228
void TestEntryModel::testDefaultIconModel()
230
auto model = new DefaultIconModel(this);
231
auto modelTest = new ModelTest(model, this);
233
QCOMPARE(model->rowCount(), databaseIcons()->count());
239
void TestEntryModel::testCustomIconModel()
241
auto model = new CustomIconModel(this);
242
auto modelTest = new ModelTest(model, this);
244
QCOMPARE(model->rowCount(), 0);
246
QHash<QUuid, QPixmap> icons;
247
QList<QUuid> iconsOrder;
249
QUuid iconUuid = QUuid::fromRfc4122(QByteArray(16, '2'));
250
icons.insert(iconUuid, QPixmap());
251
iconsOrder << iconUuid;
253
QUuid iconUuid2 = QUuid::fromRfc4122(QByteArray(16, '1'));
254
icons.insert(iconUuid2, QPixmap());
255
iconsOrder << iconUuid2;
257
model->setIcons(icons, iconsOrder);
258
QCOMPARE(model->uuidFromIndex(model->index(0, 0)), iconUuid);
259
QCOMPARE(model->uuidFromIndex(model->index(1, 0)), iconUuid2);
265
void TestEntryModel::testAutoTypeAssociationsModel()
267
auto model = new AutoTypeAssociationsModel(this);
268
auto modelTest = new ModelTest(model, this);
270
QCOMPARE(model->rowCount(), 0);
272
auto associations = new AutoTypeAssociations(this);
273
model->setAutoTypeAssociations(associations);
275
QCOMPARE(model->rowCount(), 0);
277
AutoTypeAssociations::Association assoc;
279
assoc.sequence = "2";
280
associations->add(assoc);
282
QCOMPARE(model->rowCount(), 1);
283
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("1"));
284
QCOMPARE(model->data(model->index(0, 1)).toString(), QString("2"));
287
assoc.sequence = "4";
288
associations->update(0, assoc);
289
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("3"));
290
QCOMPARE(model->data(model->index(0, 1)).toString(), QString("4"));
292
associations->add(assoc);
293
associations->remove(0);
294
QCOMPARE(model->rowCount(), 1);
301
void TestEntryModel::testProxyModel()
303
auto modelSource = new EntryModel(this);
304
auto modelProxy = new SortFilterHideProxyModel(this);
305
modelProxy->setSourceModel(modelSource);
307
auto modelTest = new ModelTest(modelProxy, this);
309
auto db = new Database();
310
auto entry = new Entry();
311
entry->setTitle("Test Title");
312
entry->setGroup(db->rootGroup());
314
modelSource->setGroup(db->rootGroup());
317
auto columnCount = modelProxy->columnCount();
318
QSignalSpy spyColumnRemove(modelProxy, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)));
319
modelProxy->hideColumn(0, true);
320
QCOMPARE(modelProxy->columnCount(), columnCount - 1);
321
QVERIFY(!spyColumnRemove.isEmpty());
323
int oldSpyColumnRemoveSize = spyColumnRemove.size();
324
modelProxy->hideColumn(0, true);
325
QCOMPARE(spyColumnRemove.size(), oldSpyColumnRemoveSize);
327
modelProxy->hideColumn(100, true);
328
QCOMPARE(spyColumnRemove.size(), oldSpyColumnRemoveSize);
330
QList<Entry*> entryList;
332
modelSource->setEntries(entryList);
334
QSignalSpy spyColumnInsert(modelProxy, SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)));
335
modelProxy->hideColumn(0, false);
336
QCOMPARE(modelProxy->columnCount(), columnCount);
337
QVERIFY(!spyColumnInsert.isEmpty());
339
int oldSpyColumnInsertSize = spyColumnInsert.size();
340
modelProxy->hideColumn(0, false);
341
QCOMPARE(spyColumnInsert.size(), oldSpyColumnInsertSize);
349
void TestEntryModel::testDatabaseDelete()
351
auto model = new EntryModel(this);
352
auto modelTest = new ModelTest(model, this);
354
auto db1 = new Database();
355
auto group1 = new Group();
356
group1->setParent(db1->rootGroup());
358
auto entry1 = new Entry();
359
entry1->setGroup(group1);
361
auto db2 = new Database();
362
auto entry2 = new Entry();
363
entry2->setGroup(db2->rootGroup());
365
model->setEntries(QList<Entry*>() << entry1 << entry2);
367
QCOMPARE(model->rowCount(), 2);
370
QCOMPARE(model->rowCount(), 1);
373
QCOMPARE(model->rowCount(), 0);