keepassxc

Форк
0
/
TestEntryModel.cpp 
378 строк · 11.3 Кб
1
/*
2
 *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "TestEntryModel.h"
19

20
#include <QSignalSpy>
21
#include <QTest>
22

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"
33
#include "modeltest.h"
34

35
QTEST_GUILESS_MAIN(TestEntryModel)
36

37
void TestEntryModel::initTestCase()
38
{
39
    qRegisterMetaType<QModelIndex>("QModelIndex");
40
    QVERIFY(Crypto::init());
41
}
42

43
void TestEntryModel::test()
44
{
45
    auto group1 = new Group();
46
    auto group2 = new Group();
47

48
    auto entry1 = new Entry();
49
    entry1->setGroup(group1);
50
    entry1->setTitle("testTitle1");
51

52
    auto entry2 = new Entry();
53
    entry2->setGroup(group1);
54
    entry2->setTitle("testTitle2");
55

56
    auto model = new EntryModel(this);
57

58
    QSignalSpy spyAboutToBeMoved(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
59
    QSignalSpy spyMoved(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
60

61
    auto modelTest = new ModelTest(model, this);
62

63
    model->setGroup(group1);
64

65
    QCOMPARE(model->rowCount(), 2);
66

67
    QSignalSpy spyDataChanged(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
68
    entry1->setTitle("changed");
69
    QCOMPARE(spyDataChanged.count(), 1);
70

71
    QModelIndex index1 = model->index(0, 1);
72
    QModelIndex index2 = model->index(1, 1);
73

74
    QCOMPARE(model->data(index1).toString(), entry1->title());
75
    QCOMPARE(model->data(index2).toString(), entry2->title());
76

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)));
81

82
    auto entry3 = new Entry();
83
    entry3->setGroup(group1);
84

85
    QCOMPARE(spyAboutToBeMoved.count(), 0);
86
    QCOMPARE(spyMoved.count(), 0);
87

88
    entry1->moveDown();
89
    QCOMPARE(spyAboutToBeMoved.count(), 1);
90
    QCOMPARE(spyMoved.count(), 1);
91

92
    entry1->moveDown();
93
    QCOMPARE(spyAboutToBeMoved.count(), 2);
94
    QCOMPARE(spyMoved.count(), 2);
95

96
    entry1->moveDown();
97
    QCOMPARE(spyAboutToBeMoved.count(), 2);
98
    QCOMPARE(spyMoved.count(), 2);
99

100
    entry3->moveUp();
101
    QCOMPARE(spyAboutToBeMoved.count(), 3);
102
    QCOMPARE(spyMoved.count(), 3);
103

104
    entry3->moveUp();
105
    QCOMPARE(spyAboutToBeMoved.count(), 3);
106
    QCOMPARE(spyMoved.count(), 3);
107

108
    QCOMPARE(spyAboutToAdd.count(), 1);
109
    QCOMPARE(spyAdded.count(), 1);
110
    QCOMPARE(spyAboutToRemove.count(), 0);
111
    QCOMPARE(spyRemoved.count(), 0);
112

113
    entry2->setGroup(group2);
114

115
    QCOMPARE(spyAboutToAdd.count(), 1);
116
    QCOMPARE(spyAdded.count(), 1);
117
    QCOMPARE(spyAboutToRemove.count(), 1);
118
    QCOMPARE(spyRemoved.count(), 1);
119

120
    QSignalSpy spyReset(model, SIGNAL(modelReset()));
121
    model->setGroup(group2);
122
    QCOMPARE(spyReset.count(), 1);
123

124
    delete group1;
125
    delete group2;
126

127
    delete modelTest;
128
    delete model;
129
}
130

131
void TestEntryModel::testAttachmentsModel()
132
{
133
    auto entryAttachments = new EntryAttachments(this);
134

135
    auto model = new EntryAttachmentsModel(this);
136
    auto modelTest = new ModelTest(model, this);
137

138
    QCOMPARE(model->rowCount(), 0);
139
    model->setEntryAttachments(entryAttachments);
140
    QCOMPARE(model->rowCount(), 0);
141

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)));
147

148
    entryAttachments->set("first", QByteArray("123"));
149

150
    entryAttachments->set("2nd", QByteArray("456"));
151
    entryAttachments->set("2nd", QByteArray("7890"));
152

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);
156

157
    entryAttachments->remove("first");
158

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);
164

165
    QSignalSpy spyReset(model, SIGNAL(modelReset()));
166
    entryAttachments->clear();
167
    model->setEntryAttachments(nullptr);
168
    QCOMPARE(spyReset.count(), 2);
169
    QCOMPARE(model->rowCount(), 0);
170

171
    delete modelTest;
172
    delete model;
173
    delete entryAttachments;
174
}
175

176
void TestEntryModel::testAttributesModel()
177
{
178
    auto entryAttributes = new EntryAttributes(this);
179

180
    auto model = new EntryAttributesModel(this);
181
    auto modelTest = new ModelTest(model, this);
182

183
    QCOMPARE(model->rowCount(), 0);
184
    model->setEntryAttributes(entryAttributes);
185
    QCOMPARE(model->rowCount(), 0);
186

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)));
192

193
    entryAttributes->set("first", "123");
194

195
    entryAttributes->set("2nd", "456");
196
    entryAttributes->set("2nd", "789");
197

198
    QCOMPARE(model->data(model->index(0, 0)).toString(), QString("2nd"));
199

200
    entryAttributes->remove("first");
201

202
    // make sure these don't generate messages
203
    entryAttributes->set("Title", "test");
204
    entryAttributes->set("Notes", "test");
205

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);
211

212
    // test attribute protection
213
    QString value = entryAttributes->value("2nd");
214
    entryAttributes->set("2nd", value, true);
215
    QVERIFY(entryAttributes->isProtected("2nd"));
216
    QCOMPARE(entryAttributes->value("2nd"), value);
217

218
    QSignalSpy spyReset(model, SIGNAL(modelReset()));
219
    entryAttributes->clear();
220
    model->setEntryAttributes(nullptr);
221
    QCOMPARE(spyReset.count(), 2);
222
    QCOMPARE(model->rowCount(), 0);
223

224
    delete modelTest;
225
    delete model;
226
}
227

228
void TestEntryModel::testDefaultIconModel()
229
{
230
    auto model = new DefaultIconModel(this);
231
    auto modelTest = new ModelTest(model, this);
232

233
    QCOMPARE(model->rowCount(), databaseIcons()->count());
234

235
    delete modelTest;
236
    delete model;
237
}
238

239
void TestEntryModel::testCustomIconModel()
240
{
241
    auto model = new CustomIconModel(this);
242
    auto modelTest = new ModelTest(model, this);
243

244
    QCOMPARE(model->rowCount(), 0);
245

246
    QHash<QUuid, QPixmap> icons;
247
    QList<QUuid> iconsOrder;
248

249
    QUuid iconUuid = QUuid::fromRfc4122(QByteArray(16, '2'));
250
    icons.insert(iconUuid, QPixmap());
251
    iconsOrder << iconUuid;
252

253
    QUuid iconUuid2 = QUuid::fromRfc4122(QByteArray(16, '1'));
254
    icons.insert(iconUuid2, QPixmap());
255
    iconsOrder << iconUuid2;
256

257
    model->setIcons(icons, iconsOrder);
258
    QCOMPARE(model->uuidFromIndex(model->index(0, 0)), iconUuid);
259
    QCOMPARE(model->uuidFromIndex(model->index(1, 0)), iconUuid2);
260

261
    delete modelTest;
262
    delete model;
263
}
264

265
void TestEntryModel::testAutoTypeAssociationsModel()
266
{
267
    auto model = new AutoTypeAssociationsModel(this);
268
    auto modelTest = new ModelTest(model, this);
269

270
    QCOMPARE(model->rowCount(), 0);
271

272
    auto associations = new AutoTypeAssociations(this);
273
    model->setAutoTypeAssociations(associations);
274

275
    QCOMPARE(model->rowCount(), 0);
276

277
    AutoTypeAssociations::Association assoc;
278
    assoc.window = "1";
279
    assoc.sequence = "2";
280
    associations->add(assoc);
281

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"));
285

286
    assoc.window = "3";
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"));
291

292
    associations->add(assoc);
293
    associations->remove(0);
294
    QCOMPARE(model->rowCount(), 1);
295

296
    delete modelTest;
297
    delete model;
298
    delete associations;
299
}
300

301
void TestEntryModel::testProxyModel()
302
{
303
    auto modelSource = new EntryModel(this);
304
    auto modelProxy = new SortFilterHideProxyModel(this);
305
    modelProxy->setSourceModel(modelSource);
306

307
    auto modelTest = new ModelTest(modelProxy, this);
308

309
    auto db = new Database();
310
    auto entry = new Entry();
311
    entry->setTitle("Test Title");
312
    entry->setGroup(db->rootGroup());
313

314
    modelSource->setGroup(db->rootGroup());
315

316
    // Test hiding and showing a column
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());
322

323
    int oldSpyColumnRemoveSize = spyColumnRemove.size();
324
    modelProxy->hideColumn(0, true);
325
    QCOMPARE(spyColumnRemove.size(), oldSpyColumnRemoveSize);
326

327
    modelProxy->hideColumn(100, true);
328
    QCOMPARE(spyColumnRemove.size(), oldSpyColumnRemoveSize);
329

330
    QList<Entry*> entryList;
331
    entryList << entry;
332
    modelSource->setEntries(entryList);
333

334
    QSignalSpy spyColumnInsert(modelProxy, SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)));
335
    modelProxy->hideColumn(0, false);
336
    QCOMPARE(modelProxy->columnCount(), columnCount);
337
    QVERIFY(!spyColumnInsert.isEmpty());
338

339
    int oldSpyColumnInsertSize = spyColumnInsert.size();
340
    modelProxy->hideColumn(0, false);
341
    QCOMPARE(spyColumnInsert.size(), oldSpyColumnInsertSize);
342

343
    delete modelTest;
344
    delete modelProxy;
345
    delete modelSource;
346
    delete db;
347
}
348

349
void TestEntryModel::testDatabaseDelete()
350
{
351
    auto model = new EntryModel(this);
352
    auto modelTest = new ModelTest(model, this);
353

354
    auto db1 = new Database();
355
    auto group1 = new Group();
356
    group1->setParent(db1->rootGroup());
357

358
    auto entry1 = new Entry();
359
    entry1->setGroup(group1);
360

361
    auto db2 = new Database();
362
    auto entry2 = new Entry();
363
    entry2->setGroup(db2->rootGroup());
364

365
    model->setEntries(QList<Entry*>() << entry1 << entry2);
366

367
    QCOMPARE(model->rowCount(), 2);
368

369
    delete db1;
370
    QCOMPARE(model->rowCount(), 1);
371

372
    delete entry2;
373
    QCOMPARE(model->rowCount(), 0);
374

375
    delete db2;
376
    delete modelTest;
377
    delete model;
378
}
379

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.