]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Use new "edit-copy-path" icon for "Copy Location" menu item
[dolphin.git] / src / tests / kfileitemmodeltest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include <QRandomGenerator>
9 #include <QTest>
10 #include <QSignalSpy>
11 #include <QStandardPaths>
12 #include <QTimer>
13 #include <QMimeData>
14
15 #include <KDirLister>
16 #include <kio/job.h>
17
18 #include "kitemviews/kfileitemmodel.h"
19 #include "testdir.h"
20
21 void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
22 {
23 Q_UNUSED(context)
24
25 switch (type) {
26 case QtDebugMsg:
27 break;
28 case QtWarningMsg:
29 break;
30 case QtCriticalMsg:
31 fprintf(stderr, "Critical: %s\n", msg.toLocal8Bit().data());
32 break;
33 case QtFatalMsg:
34 fprintf(stderr, "Fatal: %s\n", msg.toLocal8Bit().data());
35 abort();
36 default:
37 break;
38 }
39 }
40
41 Q_DECLARE_METATYPE(KItemRange)
42 Q_DECLARE_METATYPE(KItemRangeList)
43 Q_DECLARE_METATYPE(QList<int>)
44
45 class KFileItemModelTest : public QObject
46 {
47 Q_OBJECT
48
49 private Q_SLOTS:
50 void init();
51 void initTestCase();
52 void cleanup();
53
54 void testDefaultRoles();
55 void testDefaultSortRole();
56 void testDefaultGroupedSorting();
57 void testNewItems();
58 void testRemoveItems();
59 void testDirLoadingCompleted();
60 void testSetData();
61 void testSetDataWithModifiedSortRole_data();
62 void testSetDataWithModifiedSortRole();
63 void testChangeSortRole();
64 void testResortAfterChangingName();
65 void testModelConsistencyWhenInsertingItems();
66 void testItemRangeConsistencyWhenInsertingItems();
67 void testExpandItems();
68 void testExpandParentItems();
69 void testMakeExpandedItemHidden();
70 void testRemoveFilteredExpandedItems();
71 void testSorting();
72 void testIndexForKeyboardSearch();
73 void testNameFilter();
74 void testEmptyPath();
75 void testRefreshExpandedItem();
76 void testRemoveHiddenItems();
77 void collapseParentOfHiddenItems();
78 void removeParentOfHiddenItems();
79 void testGeneralParentChildRelationships();
80 void testNameRoleGroups();
81 void testNameRoleGroupsWithExpandedItems();
82 void testInconsistentModel();
83 void testChangeRolesForFilteredItems();
84 void testChangeSortRoleWhileFiltering();
85 void testRefreshFilteredItems();
86 void testCollapseFolderWhileLoading();
87 void testCreateMimeData();
88 void testDeleteFileMoreThanOnce();
89
90 private:
91 QStringList itemsInModel() const;
92
93 private:
94 KFileItemModel* m_model;
95 TestDir* m_testDir;
96 };
97
98 void KFileItemModelTest::initTestCase()
99 {
100 QStandardPaths::setTestModeEnabled(true);
101 }
102
103 void KFileItemModelTest::init()
104 {
105 // The item-model tests result in a huge number of debugging
106 // output from kdelibs. Only show critical and fatal messages.
107 qInstallMessageHandler(myMessageOutput);
108
109 qRegisterMetaType<KItemRange>("KItemRange");
110 qRegisterMetaType<KItemRangeList>("KItemRangeList");
111 qRegisterMetaType<KFileItemList>("KFileItemList");
112
113 m_testDir = new TestDir();
114 m_model = new KFileItemModel();
115 m_model->m_dirLister->setAutoUpdate(false);
116
117 // Reduce the timer interval to make the test run faster.
118 m_model->m_resortAllItemsTimer->setInterval(0);
119 }
120
121 void KFileItemModelTest::cleanup()
122 {
123 delete m_model;
124 m_model = nullptr;
125
126 delete m_testDir;
127 m_testDir = nullptr;
128 }
129
130 void KFileItemModelTest::testDefaultRoles()
131 {
132 const QSet<QByteArray> roles = m_model->roles();
133 QCOMPARE(roles.count(), 4);
134 QVERIFY(roles.contains("text"));
135 QVERIFY(roles.contains("isDir"));
136 QVERIFY(roles.contains("isLink"));
137 QVERIFY(roles.contains("isHidden"));
138 }
139
140 void KFileItemModelTest::testDefaultSortRole()
141 {
142 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
143 QVERIFY(itemsInsertedSpy.isValid());
144
145 QCOMPARE(m_model->sortRole(), QByteArray("text"));
146
147 m_testDir->createFiles({"c.txt", "a.txt", "b.txt"});
148
149 m_model->loadDirectory(m_testDir->url());
150 QVERIFY(itemsInsertedSpy.wait());
151
152 QCOMPARE(m_model->count(), 3);
153 QCOMPARE(m_model->data(0).value("text").toString(), QString("a.txt"));
154 QCOMPARE(m_model->data(1).value("text").toString(), QString("b.txt"));
155 QCOMPARE(m_model->data(2).value("text").toString(), QString("c.txt"));
156 }
157
158 void KFileItemModelTest::testDefaultGroupedSorting()
159 {
160 QCOMPARE(m_model->groupedSorting(), false);
161 }
162
163 void KFileItemModelTest::testNewItems()
164 {
165 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
166
167 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
168
169 m_model->loadDirectory(m_testDir->url());
170 QVERIFY(itemsInsertedSpy.wait());
171
172 QCOMPARE(m_model->count(), 3);
173
174 QVERIFY(m_model->isConsistent());
175 }
176
177 void KFileItemModelTest::testRemoveItems()
178 {
179 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
180 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
181
182 m_testDir->createFiles({"a.txt", "b.txt"});
183 m_model->loadDirectory(m_testDir->url());
184 QVERIFY(itemsInsertedSpy.wait());
185 QCOMPARE(m_model->count(), 2);
186 QVERIFY(m_model->isConsistent());
187
188 m_testDir->removeFile("a.txt");
189 m_model->m_dirLister->updateDirectory(m_testDir->url());
190 QVERIFY(itemsRemovedSpy.wait());
191 QCOMPARE(m_model->count(), 1);
192 QVERIFY(m_model->isConsistent());
193 }
194
195 void KFileItemModelTest::testDirLoadingCompleted()
196 {
197 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
198 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
199 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
200
201 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
202
203 m_model->loadDirectory(m_testDir->url());
204 QVERIFY(loadingCompletedSpy.wait());
205 QCOMPARE(loadingCompletedSpy.count(), 1);
206 QCOMPARE(itemsInsertedSpy.count(), 1);
207 QCOMPARE(itemsRemovedSpy.count(), 0);
208 QCOMPARE(m_model->count(), 3);
209
210 m_testDir->createFiles({"d.txt", "e.txt"});
211 m_model->m_dirLister->updateDirectory(m_testDir->url());
212 QVERIFY(loadingCompletedSpy.wait());
213 QCOMPARE(loadingCompletedSpy.count(), 2);
214 QCOMPARE(itemsInsertedSpy.count(), 2);
215 QCOMPARE(itemsRemovedSpy.count(), 0);
216 QCOMPARE(m_model->count(), 5);
217
218 m_testDir->removeFile("a.txt");
219 m_testDir->createFile("f.txt");
220 m_model->m_dirLister->updateDirectory(m_testDir->url());
221 QVERIFY(loadingCompletedSpy.wait());
222 QCOMPARE(loadingCompletedSpy.count(), 3);
223 QCOMPARE(itemsInsertedSpy.count(), 3);
224 QCOMPARE(itemsRemovedSpy.count(), 1);
225 QCOMPARE(m_model->count(), 5);
226
227 m_testDir->removeFile("b.txt");
228 m_model->m_dirLister->updateDirectory(m_testDir->url());
229 QVERIFY(itemsRemovedSpy.wait());
230 QCOMPARE(loadingCompletedSpy.count(), 4);
231 QCOMPARE(itemsInsertedSpy.count(), 3);
232 QCOMPARE(itemsRemovedSpy.count(), 2);
233 QCOMPARE(m_model->count(), 4);
234
235 QVERIFY(m_model->isConsistent());
236 }
237
238 void KFileItemModelTest::testSetData()
239 {
240 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
241 QVERIFY(itemsInsertedSpy.isValid());
242 QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged);
243 QVERIFY(itemsChangedSpy.isValid());
244
245 m_testDir->createFile("a.txt");
246
247 m_model->loadDirectory(m_testDir->url());
248 QVERIFY(itemsInsertedSpy.wait());
249
250 QHash<QByteArray, QVariant> values;
251 values.insert("customRole1", "Test1");
252 values.insert("customRole2", "Test2");
253
254 m_model->setData(0, values);
255 QCOMPARE(itemsChangedSpy.count(), 1);
256
257 values = m_model->data(0);
258 QCOMPARE(values.value("customRole1").toString(), QString("Test1"));
259 QCOMPARE(values.value("customRole2").toString(), QString("Test2"));
260 QVERIFY(m_model->isConsistent());
261 }
262
263 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
264 {
265 QTest::addColumn<int>("changedIndex");
266 QTest::addColumn<int>("changedRating");
267 QTest::addColumn<bool>("expectMoveSignal");
268 QTest::addColumn<int>("ratingIndex0");
269 QTest::addColumn<int>("ratingIndex1");
270 QTest::addColumn<int>("ratingIndex2");
271
272 // Default setup:
273 // Index 0 = rating 2
274 // Index 1 = rating 4
275 // Index 2 = rating 6
276
277 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
278 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
279 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
280
281 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
282 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
283 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
284 }
285
286 void KFileItemModelTest::testSetDataWithModifiedSortRole()
287 {
288 QFETCH(int, changedIndex);
289 QFETCH(int, changedRating);
290 QFETCH(bool, expectMoveSignal);
291 QFETCH(int, ratingIndex0);
292 QFETCH(int, ratingIndex1);
293 QFETCH(int, ratingIndex2);
294
295 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
296 QVERIFY(itemsInsertedSpy.isValid());
297 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
298 QVERIFY(itemsMovedSpy.isValid());
299
300 // Changing the value of a sort-role must result in
301 // a reordering of the items.
302 QCOMPARE(m_model->sortRole(), QByteArray("text"));
303 m_model->setSortRole("rating");
304 QCOMPARE(m_model->sortRole(), QByteArray("rating"));
305
306 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
307
308 m_model->loadDirectory(m_testDir->url());
309 QVERIFY(itemsInsertedSpy.wait());
310
311 // Fill the "rating" role of each file:
312 // a.txt -> 2
313 // b.txt -> 4
314 // c.txt -> 6
315
316 QHash<QByteArray, QVariant> ratingA;
317 ratingA.insert("rating", 2);
318 m_model->setData(0, ratingA);
319
320 QHash<QByteArray, QVariant> ratingB;
321 ratingB.insert("rating", 4);
322 m_model->setData(1, ratingB);
323
324 QHash<QByteArray, QVariant> ratingC;
325 ratingC.insert("rating", 6);
326 m_model->setData(2, ratingC);
327
328 QCOMPARE(m_model->data(0).value("rating").toInt(), 2);
329 QCOMPARE(m_model->data(1).value("rating").toInt(), 4);
330 QCOMPARE(m_model->data(2).value("rating").toInt(), 6);
331
332 // Now change the rating from a.txt. This usually results
333 // in reordering of the items.
334 QHash<QByteArray, QVariant> rating;
335 rating.insert("rating", changedRating);
336 m_model->setData(changedIndex, rating);
337
338 if (expectMoveSignal) {
339 QVERIFY(itemsMovedSpy.wait());
340 }
341
342 QCOMPARE(m_model->data(0).value("rating").toInt(), ratingIndex0);
343 QCOMPARE(m_model->data(1).value("rating").toInt(), ratingIndex1);
344 QCOMPARE(m_model->data(2).value("rating").toInt(), ratingIndex2);
345 QVERIFY(m_model->isConsistent());
346 }
347
348 void KFileItemModelTest::testChangeSortRole()
349 {
350 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
351 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
352 QVERIFY(itemsMovedSpy.isValid());
353
354 QCOMPARE(m_model->sortRole(), QByteArray("text"));
355
356 m_testDir->createFiles({"a.txt", "b.jpg", "c.txt"});
357
358 m_model->loadDirectory(m_testDir->url());
359 QVERIFY(itemsInsertedSpy.wait());
360 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
361
362 // Simulate that KFileItemModelRolesUpdater determines the mime type.
363 // Resorting the files by 'type' will only work immediately if their
364 // mime types are known.
365 for (int index = 0; index < m_model->count(); ++index) {
366 m_model->fileItem(index).determineMimeType();
367 }
368
369 // Now: sort by type.
370 m_model->setSortRole("type");
371 QCOMPARE(m_model->sortRole(), QByteArray("type"));
372 QVERIFY(!itemsMovedSpy.isEmpty());
373
374 // The actual order of the files might depend on the translation of the
375 // result of KFileItem::mimeComment() in the user's language.
376 QStringList version1;
377 version1 << "b.jpg" << "a.txt" << "c.txt";
378
379 QStringList version2;
380 version2 << "a.txt" << "c.txt" << "b.jpg";
381
382 const bool ok1 = (itemsInModel() == version1);
383 const bool ok2 = (itemsInModel() == version2);
384
385 QVERIFY(ok1 || ok2);
386 }
387
388 void KFileItemModelTest::testResortAfterChangingName()
389 {
390 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
391 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
392 QVERIFY(itemsMovedSpy.isValid());
393
394 // We sort by size in a directory where all files have the same size.
395 // Therefore, the files are sorted by their names.
396 m_model->setSortRole("size");
397
398 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
399
400 m_model->loadDirectory(m_testDir->url());
401 QVERIFY(itemsInsertedSpy.wait());
402 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
403
404 // We rename a.txt to d.txt. Even though the size has not changed at all,
405 // the model must re-sort the items.
406 QHash<QByteArray, QVariant> data;
407 data.insert("text", "d.txt");
408 m_model->setData(0, data);
409
410 QVERIFY(itemsMovedSpy.wait());
411 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
412
413 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
414 const KFileItem fileItemD = m_model->fileItem(2);
415 KFileItem fileItemA = fileItemD;
416 QUrl urlA = fileItemA.url().adjusted(QUrl::RemoveFilename);
417 urlA.setPath(urlA.path() + "a.txt");
418 fileItemA.setUrl(urlA);
419
420 m_model->slotRefreshItems({qMakePair(fileItemD, fileItemA)});
421
422 QVERIFY(itemsMovedSpy.wait());
423 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
424 }
425
426 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
427 {
428 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
429
430 // KFileItemModel prevents that inserting a punch of items sequentially
431 // results in an itemsInserted()-signal for each item. Instead internally
432 // a timeout is given that collects such operations and results in only
433 // one itemsInserted()-signal. However in this test we want to stress
434 // KFileItemModel to do a lot of insert operation and hence decrease
435 // the timeout to 1 millisecond.
436 m_testDir->createFile("1");
437 m_model->loadDirectory(m_testDir->url());
438 QVERIFY(itemsInsertedSpy.wait());
439 QCOMPARE(m_model->count(), 1);
440
441 // Insert 10 items for 20 times. After each insert operation the model consistency
442 // is checked.
443 QSet<int> insertedItems;
444 for (int i = 0; i < 20; ++i) {
445 itemsInsertedSpy.clear();
446
447 for (int j = 0; j < 10; ++j) {
448 int itemName = QRandomGenerator::global()->generate();
449 while (insertedItems.contains(itemName)) {
450 itemName = QRandomGenerator::global()->generate();
451 }
452 insertedItems.insert(itemName);
453
454 m_testDir->createFile(QString::number(itemName));
455 }
456
457 m_model->m_dirLister->updateDirectory(m_testDir->url());
458 if (itemsInsertedSpy.isEmpty()) {
459 QVERIFY(itemsInsertedSpy.wait());
460 }
461
462 QVERIFY(m_model->isConsistent());
463 }
464
465 QCOMPARE(m_model->count(), 201);
466 }
467
468 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
469 {
470 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
471
472 m_testDir->createFiles({"B", "E", "G"});
473
474 // Due to inserting the 3 items one item-range with index == 0 and
475 // count == 3 must be given
476 m_model->loadDirectory(m_testDir->url());
477 QVERIFY(itemsInsertedSpy.wait());
478
479 QCOMPARE(itemsInsertedSpy.count(), 1);
480 QList<QVariant> arguments = itemsInsertedSpy.takeFirst();
481 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
482 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 3));
483
484 // The indexes of the item-ranges must always be related to the model before
485 // the items have been inserted. Having:
486 // 0 1 2
487 // B E G
488 // and inserting A, C, D, F the resulting model will be:
489 // 0 1 2 3 4 5 6
490 // A B C D E F G
491 // and the item-ranges must be:
492 // index: 0, count: 1 for A
493 // index: 1, count: 2 for B, C
494 // index: 2, count: 1 for G
495
496 m_testDir->createFiles({"A", "C", "D", "F"});
497
498 m_model->m_dirLister->updateDirectory(m_testDir->url());
499 QVERIFY(itemsInsertedSpy.wait());
500
501 QCOMPARE(itemsInsertedSpy.count(), 1);
502 arguments = itemsInsertedSpy.takeFirst();
503 itemRangeList = arguments.at(0).value<KItemRangeList>();
504 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
505 }
506
507 void KFileItemModelTest::testExpandItems()
508 {
509 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
510 QVERIFY(itemsInsertedSpy.isValid());
511 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
512 QVERIFY(itemsRemovedSpy.isValid());
513 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
514 QVERIFY(loadingCompletedSpy.isValid());
515
516 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
517 // Besides testing the basic item expansion functionality, the test makes sure that
518 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
519 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
520 // first three characters.
521 QSet<QByteArray> originalModelRoles = m_model->roles();
522 QSet<QByteArray> modelRoles = originalModelRoles;
523 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
524 m_model->setRoles(modelRoles);
525
526 m_testDir->createFiles({"a/a/1", "a/a-1/1"});
527
528 // Store the URLs of all folders in a set.
529 QSet<QUrl> allFolders;
530 allFolders << QUrl::fromLocalFile(m_testDir->path() + "/a")
531 << QUrl::fromLocalFile(m_testDir->path() + "/a/a")
532 << QUrl::fromLocalFile(m_testDir->path() + "/a/a-1");
533
534 m_model->loadDirectory(m_testDir->url());
535 QVERIFY(itemsInsertedSpy.wait());
536
537 // So far, the model contains only "a/"
538 QCOMPARE(m_model->count(), 1);
539 QVERIFY(m_model->isExpandable(0));
540 QVERIFY(!m_model->isExpanded(0));
541 QVERIFY(m_model->expandedDirectories().empty());
542
543 QCOMPARE(itemsInsertedSpy.count(), 1);
544 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
545 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
546
547 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
548 m_model->setExpanded(0, true);
549 QVERIFY(m_model->isExpanded(0));
550 QVERIFY(itemsInsertedSpy.wait());
551 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
552 QCOMPARE(m_model->expandedDirectories(), QSet<QUrl>() << QUrl::fromLocalFile(m_testDir->path() + "/a"));
553
554 QCOMPARE(itemsInsertedSpy.count(), 1);
555 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
556 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
557
558 QVERIFY(m_model->isExpandable(1));
559 QVERIFY(!m_model->isExpanded(1));
560 QVERIFY(m_model->isExpandable(2));
561 QVERIFY(!m_model->isExpanded(2));
562
563 // Expand the folder "a/a/" -> "a/a/1" becomes visible
564 m_model->setExpanded(1, true);
565 QVERIFY(m_model->isExpanded(1));
566 QVERIFY(itemsInsertedSpy.wait());
567 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
568 QCOMPARE(m_model->expandedDirectories(), QSet<QUrl>() << QUrl::fromLocalFile(m_testDir->path() + "/a")
569 << QUrl::fromLocalFile(m_testDir->path() + "/a/a"));
570
571 QCOMPARE(itemsInsertedSpy.count(), 1);
572 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
573 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
574
575 QVERIFY(!m_model->isExpandable(2));
576 QVERIFY(!m_model->isExpanded(2));
577
578 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
579 m_model->setExpanded(3, true);
580 QVERIFY(m_model->isExpanded(3));
581 QVERIFY(itemsInsertedSpy.wait());
582 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
583 QCOMPARE(m_model->expandedDirectories(), allFolders);
584
585 QCOMPARE(itemsInsertedSpy.count(), 1);
586 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
587 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
588
589 QVERIFY(!m_model->isExpandable(4));
590 QVERIFY(!m_model->isExpanded(4));
591
592 // Collapse the top-level folder -> all other items should disappear
593 m_model->setExpanded(0, false);
594 QVERIFY(!m_model->isExpanded(0));
595 QCOMPARE(m_model->count(), 1);
596 QVERIFY(!m_model->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir->path() + "/a"))); // TODO: Make sure that child URLs are also removed
597
598 QCOMPARE(itemsRemovedSpy.count(), 1);
599 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
600 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
601 QVERIFY(m_model->isConsistent());
602
603 // Clear the model, reload the folder and try to restore the expanded folders.
604 m_model->clear();
605 QCOMPARE(m_model->count(), 0);
606 QVERIFY(m_model->expandedDirectories().empty());
607
608 m_model->loadDirectory(m_testDir->url());
609 m_model->restoreExpandedDirectories(allFolders);
610 QVERIFY(loadingCompletedSpy.wait());
611 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
612 QVERIFY(m_model->isExpanded(0));
613 QVERIFY(m_model->isExpanded(1));
614 QVERIFY(!m_model->isExpanded(2));
615 QVERIFY(m_model->isExpanded(3));
616 QVERIFY(!m_model->isExpanded(4));
617 QCOMPARE(m_model->expandedDirectories(), allFolders);
618 QVERIFY(m_model->isConsistent());
619
620 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
621 // This is how DolphinView restores the expanded folders when navigating in history.
622 m_model->loadDirectory(QUrl::fromLocalFile(m_testDir->path() + "/a/a/"));
623 QVERIFY(loadingCompletedSpy.wait());
624 QCOMPARE(m_model->count(), 1); // 1 item: "1"
625 m_model->restoreExpandedDirectories(allFolders);
626 m_model->loadDirectory(m_testDir->url());
627 QVERIFY(loadingCompletedSpy.wait());
628 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
629 QCOMPARE(m_model->expandedDirectories(), allFolders);
630
631 // Remove all expanded items by changing the roles
632 itemsRemovedSpy.clear();
633 m_model->setRoles(originalModelRoles);
634 QVERIFY(!m_model->isExpanded(0));
635 QCOMPARE(m_model->count(), 1);
636 QVERIFY(!m_model->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir->path() + "/a")));
637
638 QCOMPARE(itemsRemovedSpy.count(), 1);
639 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
640 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
641 QVERIFY(m_model->isConsistent());
642 }
643
644 void KFileItemModelTest::testExpandParentItems()
645 {
646 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
647 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
648 QVERIFY(loadingCompletedSpy.isValid());
649
650 // Create a tree structure of folders:
651 // a 1/
652 // a 1/b1/
653 // a 1/b1/c1/
654 // a2/
655 // a2/b2/
656 // a2/b2/c2/
657 // a2/b2/c2/d2/
658 QSet<QByteArray> modelRoles = m_model->roles();
659 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
660 m_model->setRoles(modelRoles);
661
662 m_testDir->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
663
664 m_model->loadDirectory(m_testDir->url());
665 QVERIFY(itemsInsertedSpy.wait());
666
667 // So far, the model contains only "a 1/" and "a2/".
668 QCOMPARE(m_model->count(), 2);
669 QVERIFY(m_model->expandedDirectories().empty());
670
671 // Expand the parents of "a2/b2/c2".
672 m_model->expandParentDirectories(QUrl::fromLocalFile(m_testDir->path() + "a2/b2/c2"));
673 QVERIFY(loadingCompletedSpy.wait());
674
675 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
676 // It's important that only the parents of "a1/b1/c1" are expanded.
677 QCOMPARE(m_model->count(), 4);
678 QVERIFY(!m_model->isExpanded(0));
679 QVERIFY(m_model->isExpanded(1));
680 QVERIFY(m_model->isExpanded(2));
681 QVERIFY(!m_model->isExpanded(3));
682
683 // Expand the parents of "a 1/b1".
684 m_model->expandParentDirectories(QUrl::fromLocalFile(m_testDir->path() + "a 1/b1"));
685 QVERIFY(loadingCompletedSpy.wait());
686
687 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
688 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
689 QCOMPARE(m_model->count(), 5);
690 QVERIFY(m_model->isExpanded(0));
691 QVERIFY(!m_model->isExpanded(1));
692 QVERIFY(m_model->isExpanded(2));
693 QVERIFY(m_model->isExpanded(3));
694 QVERIFY(!m_model->isExpanded(4));
695 QVERIFY(m_model->isConsistent());
696
697 // Expand "a 1/b1/".
698 m_model->setExpanded(1, true);
699 QVERIFY(loadingCompletedSpy.wait());
700 QCOMPARE(m_model->count(), 6);
701 QVERIFY(m_model->isExpanded(0));
702 QVERIFY(m_model->isExpanded(1));
703 QVERIFY(!m_model->isExpanded(2));
704 QVERIFY(m_model->isExpanded(3));
705 QVERIFY(m_model->isExpanded(4));
706 QVERIFY(!m_model->isExpanded(5));
707 QVERIFY(m_model->isConsistent());
708
709 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
710 m_model->setExpanded(1, false);
711 QCOMPARE(m_model->count(), 5);
712 QVERIFY(m_model->isExpanded(0));
713 QVERIFY(!m_model->isExpanded(1));
714 QVERIFY(m_model->isExpanded(2));
715 QVERIFY(m_model->isExpanded(3));
716 QVERIFY(!m_model->isExpanded(4));
717 QVERIFY(m_model->isConsistent());
718 }
719
720 /**
721 * Renaming an expanded folder by prepending its name with a dot makes it
722 * hidden. Verify that this does not cause an inconsistent model state and
723 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
724 */
725 void KFileItemModelTest::testMakeExpandedItemHidden()
726 {
727 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
728 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
729
730 QSet<QByteArray> modelRoles = m_model->roles();
731 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
732 m_model->setRoles(modelRoles);
733
734 m_testDir->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
735
736 m_model->loadDirectory(m_testDir->url());
737 QVERIFY(itemsInsertedSpy.wait());
738
739 // So far, the model contains only "1a/" and "1b".
740 QCOMPARE(m_model->count(), 2);
741 m_model->setExpanded(0, true);
742 QVERIFY(itemsInsertedSpy.wait());
743
744 // Now "1a/2a" and "1a/2b" have appeared.
745 QCOMPARE(m_model->count(), 4);
746 m_model->setExpanded(1, true);
747 QVERIFY(itemsInsertedSpy.wait());
748 QCOMPARE(m_model->count(), 6);
749
750 // Rename "1a/2" and make it hidden.
751 const QUrl oldUrl = QUrl::fromLocalFile(m_model->fileItem(0).url().path() + "/2a");
752 const QUrl newUrl = QUrl::fromLocalFile(m_model->fileItem(0).url().path() + "/.2a");
753
754 KIO::SimpleJob* job = KIO::rename(oldUrl, newUrl, KIO::HideProgressInfo);
755 bool ok = job->exec();
756 QVERIFY(ok);
757 QVERIFY(itemsRemovedSpy.wait());
758
759 // "1a/2" and its subfolders have disappeared now.
760 QVERIFY(m_model->isConsistent());
761 QCOMPARE(m_model->count(), 3);
762
763 m_model->setExpanded(0, false);
764 QCOMPARE(m_model->count(), 2);
765
766 }
767
768 void KFileItemModelTest::testRemoveFilteredExpandedItems()
769 {
770 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
771
772 QSet<QByteArray> originalModelRoles = m_model->roles();
773 QSet<QByteArray> modelRoles = originalModelRoles;
774 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
775 m_model->setRoles(modelRoles);
776
777 m_testDir->createFiles({"folder/child", "file"});
778
779 m_model->loadDirectory(m_testDir->url());
780 QVERIFY(itemsInsertedSpy.wait());
781
782 // So far, the model contains only "folder/" and "file".
783 QCOMPARE(m_model->count(), 2);
784 QVERIFY(m_model->isExpandable(0));
785 QVERIFY(!m_model->isExpandable(1));
786 QVERIFY(!m_model->isExpanded(0));
787 QVERIFY(!m_model->isExpanded(1));
788 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
789
790 // Expand "folder" -> "folder/child" becomes visible.
791 m_model->setExpanded(0, true);
792 QVERIFY(m_model->isExpanded(0));
793 QVERIFY(itemsInsertedSpy.wait());
794 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
795
796 // Add a name filter.
797 m_model->setNameFilter("f");
798 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
799
800 m_model->setNameFilter("fo");
801 QCOMPARE(itemsInModel(), QStringList() << "folder");
802
803 // Remove all expanded items by changing the roles
804 m_model->setRoles(originalModelRoles);
805 QVERIFY(!m_model->isExpanded(0));
806 QCOMPARE(itemsInModel(), QStringList() << "folder");
807
808 // Remove the name filter and verify that "folder/child" does not reappear.
809 m_model->setNameFilter(QString());
810 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
811 }
812
813 void KFileItemModelTest::testSorting()
814 {
815 // testDir structure is as follows
816 // ./
817 // ├─ .g/
818 // ├─ a
819 // ├─ b
820 // ├─ c/
821 // │ ├─ c-2/
822 // │ │ ├─ c-3
823 // │ ├─ c-1
824 // ├─ .f
825 // ├─ d
826 // ├─ e
827
828 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
829 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
830 QVERIFY(itemsMovedSpy.isValid());
831
832 // Create some files with different sizes and modification times to check the different sorting options
833 QDateTime now = QDateTime::currentDateTime();
834
835 QSet<QByteArray> roles;
836 roles.insert("text");
837 roles.insert("isExpanded");
838 roles.insert("isExpandable");
839 roles.insert("expandedParentsCount");
840 m_model->setRoles(roles);
841
842 m_testDir->createDir("c/c-2");
843 m_testDir->createFile("c/c-2/c-3");
844 m_testDir->createFile("c/c-1");
845
846 m_testDir->createFile("a", "A file", now.addDays(-3));
847 m_testDir->createFile("b", "A larger file", now.addDays(0));
848 m_testDir->createDir("c", now.addDays(-2));
849 m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
850 m_testDir->createFile("e", "An even larger file", now.addDays(-4));
851 m_testDir->createFile(".f");
852 m_testDir->createDir(".g");
853
854 m_model->loadDirectory(m_testDir->url());
855 QVERIFY(itemsInsertedSpy.wait());
856 QCOMPARE(itemsInsertedSpy.count(), 1);
857 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
858 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 5));
859
860 int index = m_model->index(QUrl(m_testDir->url().url() + "/c"));
861 m_model->setExpanded(index, true);
862 QVERIFY(itemsInsertedSpy.wait());
863 QCOMPARE(itemsInsertedSpy.count(), 1);
864 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
865 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
866
867 index = m_model->index(QUrl(m_testDir->url().url() + "/c/c-2"));
868 m_model->setExpanded(index, true);
869 QVERIFY(itemsInsertedSpy.wait());
870 QCOMPARE(itemsInsertedSpy.count(), 1);
871 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
872 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1));
873
874 // Default: Sort by Name, ascending
875 QCOMPARE(m_model->sortRole(), QByteArray("text"));
876 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
877 QVERIFY(m_model->sortDirectoriesFirst());
878 QVERIFY(!m_model->showHiddenFiles());
879 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
880
881 // Sort by Name, ascending, 'Sort Folders First' disabled
882 m_model->setSortDirectoriesFirst(false);
883 QCOMPARE(m_model->sortRole(), QByteArray("text"));
884 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
885 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
886 QCOMPARE(itemsMovedSpy.count(), 1);
887 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 6));
888 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
889
890 // Sort by Name, descending
891 m_model->setSortDirectoriesFirst(true);
892 m_model->setSortOrder(Qt::DescendingOrder);
893 QCOMPARE(m_model->sortRole(), QByteArray("text"));
894 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
895 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
896 QCOMPARE(itemsMovedSpy.count(), 2);
897 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 6));
898 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2);
899 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
900 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
901
902 // Sort by Date, descending
903 m_model->setSortDirectoriesFirst(true);
904 m_model->setSortRole("modificationtime");
905 QCOMPARE(m_model->sortRole(), QByteArray("modificationtime"));
906 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
907 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
908 QCOMPARE(itemsMovedSpy.count(), 1);
909 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
910 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 5 << 4 << 6);
911
912 // Sort by Date, ascending
913 m_model->setSortOrder(Qt::AscendingOrder);
914 QCOMPARE(m_model->sortRole(), QByteArray("modificationtime"));
915 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
916 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
917 QCOMPARE(itemsMovedSpy.count(), 1);
918 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
919 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
920
921 // Sort by Date, ascending, 'Sort Folders First' disabled
922 m_model->setSortDirectoriesFirst(false);
923 QCOMPARE(m_model->sortRole(), QByteArray("modificationtime"));
924 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
925 QVERIFY(!m_model->sortDirectoriesFirst());
926 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
927 QCOMPARE(itemsMovedSpy.count(), 1);
928 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 6));
929 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
930
931 // Sort by Name, ascending, 'Sort Folders First' disabled
932 m_model->setSortRole("text");
933 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
934 QVERIFY(!m_model->sortDirectoriesFirst());
935 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
936 QCOMPARE(itemsMovedSpy.count(), 1);
937 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 8));
938 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
939
940 // Sort by Size, ascending, 'Sort Folders First' disabled
941 m_model->setSortRole("size");
942 QCOMPARE(m_model->sortRole(), QByteArray("size"));
943 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
944 QVERIFY(!m_model->sortDirectoriesFirst());
945 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
946 QCOMPARE(itemsMovedSpy.count(), 1);
947 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 8));
948 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
949
950 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
951 m_model->setSortDirectoriesFirst(true);
952 QCOMPARE(m_model->sortRole(), QByteArray("size"));
953 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
954 QVERIFY(m_model->sortDirectoriesFirst());
955 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
956 QCOMPARE(itemsMovedSpy.count(), 0);
957
958 // Sort by Size, descending, 'Sort Folders First' enabled
959 m_model->setSortOrder(Qt::DescendingOrder);
960 QCOMPARE(m_model->sortRole(), QByteArray("size"));
961 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
962 QVERIFY(m_model->sortDirectoriesFirst());
963 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
964 QCOMPARE(itemsMovedSpy.count(), 1);
965 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
966 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
967
968 // 'Show Hidden Files' enabled
969 m_model->setShowHiddenFiles(true);
970 QVERIFY(m_model->showHiddenFiles());
971 QVERIFY(!m_model->sortHiddenLast());
972 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << ".g" << "d" << "e" << "b" << "a" << ".f");
973 QCOMPARE(itemsMovedSpy.count(), 0);
974 QCOMPARE(itemsInsertedSpy.count(), 1);
975 QCOMPARE(itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
976
977 // 'Sort Hidden Files Last' enabled
978 m_model->setSortHiddenLast(true);
979 QVERIFY(m_model->sortHiddenLast());
980 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a" << ".g" << ".f");
981 QCOMPARE(itemsMovedSpy.count(), 1);
982 QCOMPARE(itemsInsertedSpy.count(), 0);
983 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 5));
984 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 8 << 4 << 5 << 6 << 7);
985
986 // Sort by Name
987 m_model->setSortRole("text");
988 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a" << ".g" << ".f");
989 QCOMPARE(itemsMovedSpy.count(), 1);
990 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 2));
991 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 5 << 4);
992
993 // Sort ascending
994 m_model->setSortOrder(Qt::AscendingOrder);
995 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e" << ".g" << ".f");
996 QCOMPARE(itemsMovedSpy.count(), 1);
997 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
998 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
999
1000 // 'Sort Folders First' disabled
1001 m_model->setSortDirectoriesFirst(false);
1002 QVERIFY(!m_model->sortDirectoriesFirst());
1003 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e" << ".f" << ".g");
1004 QCOMPARE(itemsMovedSpy.count(), 1);
1005 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 10));
1006 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1007
1008 }
1009
1010 void KFileItemModelTest::testIndexForKeyboardSearch()
1011 {
1012 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1013
1014 m_testDir->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1015
1016 m_model->loadDirectory(m_testDir->url());
1017 QVERIFY(itemsInsertedSpy.wait());
1018
1019 // Search from index 0
1020 QCOMPARE(m_model->indexForKeyboardSearch("a", 0), 0);
1021 QCOMPARE(m_model->indexForKeyboardSearch("aa", 0), 1);
1022 QCOMPARE(m_model->indexForKeyboardSearch("i", 0), 2);
1023 QCOMPARE(m_model->indexForKeyboardSearch("image", 0), 2);
1024 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 0), 2);
1025 QCOMPARE(m_model->indexForKeyboardSearch("image.png", 0), 3);
1026 QCOMPARE(m_model->indexForKeyboardSearch("t", 0), 4);
1027 QCOMPARE(m_model->indexForKeyboardSearch("text", 0), 4);
1028 QCOMPARE(m_model->indexForKeyboardSearch("text1", 0), 5);
1029 QCOMPARE(m_model->indexForKeyboardSearch("text2", 0), 6);
1030 QCOMPARE(m_model->indexForKeyboardSearch("text11", 0), 7);
1031
1032 // Start a search somewhere in the middle
1033 QCOMPARE(m_model->indexForKeyboardSearch("a", 1), 1);
1034 QCOMPARE(m_model->indexForKeyboardSearch("i", 3), 3);
1035 QCOMPARE(m_model->indexForKeyboardSearch("t", 5), 5);
1036 QCOMPARE(m_model->indexForKeyboardSearch("text1", 6), 7);
1037
1038 // Test searches that go past the last item back to index 0
1039 QCOMPARE(m_model->indexForKeyboardSearch("a", 2), 0);
1040 QCOMPARE(m_model->indexForKeyboardSearch("i", 7), 2);
1041 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 3), 2);
1042 QCOMPARE(m_model->indexForKeyboardSearch("text2", 7), 6);
1043
1044 // Test searches that yield no result
1045 QCOMPARE(m_model->indexForKeyboardSearch("aaa", 0), -1);
1046 QCOMPARE(m_model->indexForKeyboardSearch("b", 0), -1);
1047 QCOMPARE(m_model->indexForKeyboardSearch("image.svg", 0), -1);
1048 QCOMPARE(m_model->indexForKeyboardSearch("text3", 0), -1);
1049 QCOMPARE(m_model->indexForKeyboardSearch("text3", 5), -1);
1050
1051 // Test upper case searches (note that search is case insensitive)
1052 QCOMPARE(m_model->indexForKeyboardSearch("A", 0), 0);
1053 QCOMPARE(m_model->indexForKeyboardSearch("aA", 0), 1);
1054 QCOMPARE(m_model->indexForKeyboardSearch("TexT", 5), 5);
1055 QCOMPARE(m_model->indexForKeyboardSearch("IMAGE", 4), 2);
1056
1057 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1058 }
1059
1060 void KFileItemModelTest::testNameFilter()
1061 {
1062 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1063
1064 m_testDir->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1065
1066 m_model->loadDirectory(m_testDir->url());
1067 QVERIFY(itemsInsertedSpy.wait());
1068
1069 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
1070 QCOMPARE(m_model->count(), 3);
1071
1072 m_model->setNameFilter("A2"); // Shows only A2
1073 QCOMPARE(m_model->count(), 1);
1074
1075 m_model->setNameFilter("A2"); // Shows only A1
1076 QCOMPARE(m_model->count(), 1);
1077
1078 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1079 QCOMPARE(m_model->count(), 2);
1080
1081 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1082 QCOMPARE(m_model->count(), 2);
1083
1084 m_model->setNameFilter(QString()); // Shows again all items
1085 QCOMPARE(m_model->count(), 5);
1086 }
1087
1088 /**
1089 * Verifies that we do not crash when adding a KFileItem with an empty path.
1090 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1091 * tried to always read the first character of the path, even if the path is empty.
1092 */
1093 void KFileItemModelTest::testEmptyPath()
1094 {
1095 QSet<QByteArray> roles;
1096 roles.insert("text");
1097 roles.insert("isExpanded");
1098 roles.insert("isExpandable");
1099 roles.insert("expandedParentsCount");
1100 m_model->setRoles(roles);
1101
1102 const QUrl emptyUrl;
1103 QVERIFY(emptyUrl.path().isEmpty());
1104
1105 const QUrl url("file:///test/");
1106
1107 KFileItemList items;
1108 items << KFileItem(emptyUrl, QString(), KFileItem::Unknown) << KFileItem(url, QString(), KFileItem::Unknown);
1109 m_model->slotItemsAdded(emptyUrl, items);
1110 m_model->slotCompleted();
1111 }
1112
1113 /**
1114 * Verifies that the 'isExpanded' state of folders does not change when the
1115 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1116 */
1117 void KFileItemModelTest::testRefreshExpandedItem()
1118 {
1119 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1120 QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged);
1121 QVERIFY(itemsChangedSpy.isValid());
1122
1123 QSet<QByteArray> modelRoles = m_model->roles();
1124 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1125 m_model->setRoles(modelRoles);
1126
1127 m_testDir->createFiles({"a/1", "a/2", "3", "4"});
1128
1129 m_model->loadDirectory(m_testDir->url());
1130 QVERIFY(itemsInsertedSpy.wait());
1131 QCOMPARE(m_model->count(), 3); // "a/", "3", "4"
1132
1133 m_model->setExpanded(0, true);
1134 QVERIFY(itemsInsertedSpy.wait());
1135 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1136 QVERIFY(m_model->isExpanded(0));
1137
1138 const KFileItem item = m_model->fileItem(0);
1139 m_model->slotRefreshItems({qMakePair(item, item)});
1140 QVERIFY(!itemsChangedSpy.isEmpty());
1141
1142 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1143 QVERIFY(m_model->isExpanded(0));
1144 }
1145
1146 /**
1147 * Verify that removing hidden files and folders from the model does not
1148 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1149 */
1150 void KFileItemModelTest::testRemoveHiddenItems()
1151 {
1152 m_testDir->createDir(".a");
1153 m_testDir->createDir(".b");
1154 m_testDir->createDir("c");
1155 m_testDir->createDir("d");
1156 m_testDir->createFiles({".f", ".g", "h", "i"});
1157
1158 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1159 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1160
1161 m_model->setShowHiddenFiles(true);
1162 m_model->loadDirectory(m_testDir->url());
1163 QVERIFY(itemsInsertedSpy.wait());
1164 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1165 QCOMPARE(itemsInsertedSpy.count(), 1);
1166 QCOMPARE(itemsRemovedSpy.count(), 0);
1167 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
1168 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
1169
1170 m_model->setShowHiddenFiles(false);
1171 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1172 QCOMPARE(itemsInsertedSpy.count(), 0);
1173 QCOMPARE(itemsRemovedSpy.count(), 1);
1174 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
1175 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1176
1177 m_model->setShowHiddenFiles(true);
1178 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1179 QCOMPARE(itemsInsertedSpy.count(), 1);
1180 QCOMPARE(itemsRemovedSpy.count(), 0);
1181 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
1182 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1183
1184 m_model->clear();
1185 QCOMPARE(itemsInModel(), QStringList());
1186 QCOMPARE(itemsInsertedSpy.count(), 0);
1187 QCOMPARE(itemsRemovedSpy.count(), 1);
1188 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
1189 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
1190
1191 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1192 // Verify that this does not make the model crash.
1193 m_model->setShowHiddenFiles(false);
1194 }
1195
1196 /**
1197 * Verify that filtered items are removed when their parent is collapsed.
1198 */
1199 void KFileItemModelTest::collapseParentOfHiddenItems()
1200 {
1201 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1202 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1203
1204 QSet<QByteArray> modelRoles = m_model->roles();
1205 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1206 m_model->setRoles(modelRoles);
1207
1208 m_testDir->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1209
1210 m_model->loadDirectory(m_testDir->url());
1211 QVERIFY(itemsInsertedSpy.wait());
1212 QCOMPARE(m_model->count(), 1); // Only "a/"
1213
1214 // Expand "a/".
1215 m_model->setExpanded(0, true);
1216 QVERIFY(itemsInsertedSpy.wait());
1217 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1218
1219 // Expand "a/b/".
1220 m_model->setExpanded(1, true);
1221 QVERIFY(itemsInsertedSpy.wait());
1222 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1223
1224 // Expand "a/b/c/".
1225 m_model->setExpanded(2, true);
1226 QVERIFY(itemsInsertedSpy.wait());
1227 QCOMPARE(m_model->count(), 7); // 7 items: "a/", "a/b/", "a/b/c", "a/b/c/d/", "a/b/c/1", "a/b/1", "a/1"
1228
1229 // Set a name filter that matches nothing -> nothing should remain.
1230 m_model->setNameFilter("xyz");
1231 QCOMPARE(itemsRemovedSpy.count(), 1);
1232 QCOMPARE(m_model->count(), 0); //Everything is hidden
1233 QCOMPARE(itemsInModel(), QStringList());
1234
1235 //Filter by the file names. Folder "d" will be hidden since it was collapsed
1236 m_model->setNameFilter("1");
1237 QCOMPARE(itemsRemovedSpy.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1238 QCOMPARE(m_model->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1239
1240 // Collapse the folder "a/".
1241 m_model->setExpanded(0, false);
1242 QCOMPARE(itemsRemovedSpy.count(), 2);
1243 QCOMPARE(m_model->count(), 1);
1244 QCOMPARE(itemsInModel(), QStringList() << "a");
1245
1246 // Remove the filter -> "a" should still appear (and we should not get a crash).
1247 m_model->setNameFilter(QString());
1248 QCOMPARE(itemsRemovedSpy.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1249 QCOMPARE(m_model->count(), 1);
1250 QCOMPARE(itemsInModel(), QStringList() << "a");
1251 }
1252
1253 /**
1254 * Verify that filtered items are removed when their parent is deleted.
1255 */
1256 void KFileItemModelTest::removeParentOfHiddenItems()
1257 {
1258 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1259 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1260
1261 QSet<QByteArray> modelRoles = m_model->roles();
1262 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1263 m_model->setRoles(modelRoles);
1264
1265 m_testDir->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1266
1267 m_model->loadDirectory(m_testDir->url());
1268 QVERIFY(itemsInsertedSpy.wait());
1269 QCOMPARE(m_model->count(), 1); // Only "a/"
1270
1271 // Expand "a/".
1272 m_model->setExpanded(0, true);
1273 QVERIFY(itemsInsertedSpy.wait());
1274 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1275
1276 // Expand "a/b/".
1277 m_model->setExpanded(1, true);
1278 QVERIFY(itemsInsertedSpy.wait());
1279 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1280
1281 // Expand "a/b/c/".
1282 m_model->setExpanded(2, true);
1283 QVERIFY(itemsInsertedSpy.wait());
1284 QCOMPARE(m_model->count(), 7); // 7 items: "a/", "a/b/", "a/b/c", "a/b/c/d/", "a/b/c/1", "a/b/1", "a/1"
1285
1286 // Set a name filter that matches nothing -> nothing should remain.
1287 m_model->setNameFilter("xyz");
1288 QCOMPARE(itemsRemovedSpy.count(), 1);
1289 QCOMPARE(m_model->count(), 0);
1290 QCOMPARE(itemsInModel(), QStringList());
1291
1292 // Filter by "c". Folder "b" will also be shown because it is its parent.
1293 m_model->setNameFilter("c");
1294 QCOMPARE(itemsRemovedSpy.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1295 QCOMPARE(m_model->count(), 3);
1296 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1297
1298 // Simulate the deletion of the directory "a/b/".
1299 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1300 QCOMPARE(itemsRemovedSpy.count(), 2);
1301 QCOMPARE(m_model->count(), 1);
1302 QCOMPARE(itemsInModel(), QStringList() << "a");
1303
1304 // Remove the filter -> only the file "a/1" should appear.
1305 m_model->setNameFilter(QString());
1306 QCOMPARE(m_model->count(), 2);
1307 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1308 }
1309
1310 /**
1311 * Create a tree structure where parent-child relationships can not be
1312 * determined by parsing the URLs, and verify that KFileItemModel
1313 * handles them correctly.
1314 */
1315 void KFileItemModelTest::testGeneralParentChildRelationships()
1316 {
1317 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1318 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1319
1320 QSet<QByteArray> modelRoles = m_model->roles();
1321 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1322 m_model->setRoles(modelRoles);
1323
1324 m_testDir->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1325
1326 m_model->loadDirectory(m_testDir->url());
1327 QVERIFY(itemsInsertedSpy.wait());
1328 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1329
1330 // Expand all folders.
1331 m_model->setExpanded(0, true);
1332 QVERIFY(itemsInsertedSpy.wait());
1333 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1334
1335 m_model->setExpanded(1, true);
1336 QVERIFY(itemsInsertedSpy.wait());
1337 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1338
1339 m_model->setExpanded(3, true);
1340 QVERIFY(itemsInsertedSpy.wait());
1341 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1342
1343 m_model->setExpanded(4, true);
1344 QVERIFY(itemsInsertedSpy.wait());
1345 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1346
1347 // Add some more children and grand-children.
1348 const QUrl parent1 = m_model->fileItem(0).url();
1349 const QUrl parent2 = m_model->fileItem(3).url();
1350 const QUrl realChild1 = m_model->fileItem(1).url();
1351 const QUrl realChild2 = m_model->fileItem(4).url();
1352
1353 m_model->slotItemsAdded(parent1, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown));
1354 m_model->slotCompleted();
1355 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1356
1357 m_model->slotItemsAdded(parent2, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown));
1358 m_model->slotCompleted();
1359 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1360
1361 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown));
1362 m_model->slotCompleted();
1363 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1364
1365 m_model->slotItemsAdded(realChild2, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown));
1366 m_model->slotCompleted();
1367 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1368
1369 // Set a name filter that matches nothing -> nothing will remain.
1370 m_model->setNameFilter("xyz");
1371 QCOMPARE(itemsInModel(), QStringList());
1372 QCOMPARE(itemsRemovedSpy.count(), 1);
1373 QList<QVariant> arguments = itemsRemovedSpy.takeFirst();
1374 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
1375 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 10));
1376
1377 // Set a name filter that matches only "realChild". Their prarents should still show.
1378 m_model->setNameFilter("realChild");
1379 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1380 QCOMPARE(itemsRemovedSpy.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
1381
1382 // Collapse "parent1".
1383 m_model->setExpanded(0, false);
1384 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1385 QCOMPARE(itemsRemovedSpy.count(), 1);
1386 arguments = itemsRemovedSpy.takeFirst();
1387 itemRangeList = arguments.at(0).value<KItemRangeList>();
1388 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1));
1389
1390 // Remove "parent2".
1391 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1392 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1393 QCOMPARE(itemsRemovedSpy.count(), 1);
1394 arguments = itemsRemovedSpy.takeFirst();
1395 itemRangeList = arguments.at(0).value<KItemRangeList>();
1396 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
1397
1398 // Clear filter, verify that no items reappear.
1399 m_model->setNameFilter(QString());
1400 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1401 }
1402
1403 void KFileItemModelTest::testNameRoleGroups()
1404 {
1405 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1406 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
1407 QVERIFY(itemsMovedSpy.isValid());
1408 QSignalSpy groupsChangedSpy(m_model, &KFileItemModel::groupsChanged);
1409 QVERIFY(groupsChangedSpy.isValid());
1410
1411 m_testDir->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1412
1413 m_model->setGroupedSorting(true);
1414 m_model->loadDirectory(m_testDir->url());
1415 QVERIFY(itemsInsertedSpy.wait());
1416 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1417
1418 QList<QPair<int, QVariant> > expectedGroups;
1419 expectedGroups << QPair<int, QVariant>(0, QLatin1String("B"));
1420 expectedGroups << QPair<int, QVariant>(1, QLatin1String("C"));
1421 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1422 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1423 QCOMPARE(m_model->groups(), expectedGroups);
1424
1425 // Rename d.txt to a.txt.
1426 QHash<QByteArray, QVariant> data;
1427 data.insert("text", "a.txt");
1428 m_model->setData(2, data);
1429 QVERIFY(itemsMovedSpy.wait());
1430 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1431
1432 expectedGroups.clear();
1433 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1434 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1435 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1436 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1437 QCOMPARE(m_model->groups(), expectedGroups);
1438
1439 // Rename c.txt to d.txt.
1440 data.insert("text", "d.txt");
1441 m_model->setData(2, data);
1442 QVERIFY(groupsChangedSpy.wait());
1443 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1444
1445 expectedGroups.clear();
1446 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1447 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1448 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1449 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1450 QCOMPARE(m_model->groups(), expectedGroups);
1451
1452 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1453 const KFileItem fileItemD = m_model->fileItem(2);
1454 KFileItem fileItemC = fileItemD;
1455 QUrl urlC = fileItemC.url().adjusted(QUrl::RemoveFilename);
1456 urlC.setPath(urlC.path() + "c.txt");
1457 fileItemC.setUrl(urlC);
1458
1459 m_model->slotRefreshItems({qMakePair(fileItemD, fileItemC)});
1460 QVERIFY(groupsChangedSpy.wait());
1461 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1462
1463 expectedGroups.clear();
1464 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1465 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1466 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1467 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1468 QCOMPARE(m_model->groups(), expectedGroups);
1469 }
1470
1471 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1472 {
1473 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1474
1475 QSet<QByteArray> modelRoles = m_model->roles();
1476 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1477 m_model->setRoles(modelRoles);
1478
1479 m_testDir->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1480
1481 m_model->setGroupedSorting(true);
1482 m_model->loadDirectory(m_testDir->url());
1483 QVERIFY(itemsInsertedSpy.wait());
1484 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1485
1486 QList<QPair<int, QVariant> > expectedGroups;
1487 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1488 expectedGroups << QPair<int, QVariant>(1, QLatin1String("D"));
1489 QCOMPARE(m_model->groups(), expectedGroups);
1490
1491 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1492 expectedGroups.clear();
1493 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1494 expectedGroups << QPair<int, QVariant>(3, QLatin1String("D"));
1495
1496 m_model->setExpanded(0, true);
1497 QVERIFY(m_model->isExpanded(0));
1498 QVERIFY(itemsInsertedSpy.wait());
1499 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1500 QCOMPARE(m_model->groups(), expectedGroups);
1501
1502 m_model->setExpanded(3, true);
1503 QVERIFY(m_model->isExpanded(3));
1504 QVERIFY(itemsInsertedSpy.wait());
1505 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1506 QCOMPARE(m_model->groups(), expectedGroups);
1507 }
1508
1509 void KFileItemModelTest::testInconsistentModel()
1510 {
1511 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1512
1513 QSet<QByteArray> modelRoles = m_model->roles();
1514 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1515 m_model->setRoles(modelRoles);
1516
1517 m_testDir->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1518
1519 m_model->loadDirectory(m_testDir->url());
1520 QVERIFY(itemsInsertedSpy.wait());
1521 QCOMPARE(itemsInModel(), QStringList() << "a");
1522
1523 // Expand "a/" and "a/b/".
1524 m_model->setExpanded(0, true);
1525 QVERIFY(m_model->isExpanded(0));
1526 QVERIFY(itemsInsertedSpy.wait());
1527 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1528
1529 m_model->setExpanded(1, true);
1530 QVERIFY(m_model->isExpanded(1));
1531 QVERIFY(itemsInsertedSpy.wait());
1532 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1533
1534 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1535 // Such a thing can in principle happen when performing a search, and there
1536 // are files which
1537 // (a) match the search string, and
1538 // (b) are children of a folder that matches the search string and is expanded.
1539 //
1540 // Note that the first item in the list of added items must be new (i.e., not
1541 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1542 // it receives items that are in the model already and ignore them.
1543 QUrl url(m_model->directory().url() + "/a2");
1544 KFileItem newItem(url);
1545
1546 KFileItemList items;
1547 items << newItem << m_model->fileItem(2) << m_model->fileItem(3);
1548 m_model->slotItemsAdded(m_model->directory(), items);
1549 m_model->slotCompleted();
1550 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1551
1552 m_model->setExpanded(0, false);
1553
1554 // Test that the right items have been removed, see
1555 // https://bugs.kde.org/show_bug.cgi?id=324371
1556 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1557
1558 // Test that resorting does not cause a crash, see
1559 // https://bugs.kde.org/show_bug.cgi?id=325359
1560 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1561 m_model->resortAllItems();
1562
1563 }
1564
1565 void KFileItemModelTest::testChangeRolesForFilteredItems()
1566 {
1567 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1568
1569 QSet<QByteArray> modelRoles = m_model->roles();
1570 modelRoles << "owner";
1571 m_model->setRoles(modelRoles);
1572
1573 m_testDir->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1574
1575 m_model->loadDirectory(m_testDir->url());
1576 QVERIFY(itemsInsertedSpy.wait());
1577 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1578
1579 for (int index = 0; index < m_model->count(); ++index) {
1580 // All items should have the "text" and "owner" roles, but not "group".
1581 QVERIFY(m_model->data(index).contains("text"));
1582 QVERIFY(m_model->data(index).contains("owner"));
1583 QVERIFY(!m_model->data(index).contains("group"));
1584 }
1585
1586 // Add a filter, such that only "aaa.txt" remains in the model.
1587 m_model->setNameFilter("aaa");
1588 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1589
1590 // Add the "group" role.
1591 modelRoles << "group";
1592 m_model->setRoles(modelRoles);
1593
1594 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1595 m_model->setNameFilter("aa");
1596 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1597
1598 for (int index = 0; index < m_model->count(); ++index) {
1599 // All items should have the "text", "owner", and "group" roles.
1600 QVERIFY(m_model->data(index).contains("text"));
1601 QVERIFY(m_model->data(index).contains("owner"));
1602 QVERIFY(m_model->data(index).contains("group"));
1603 }
1604
1605 // Remove the "owner" role.
1606 modelRoles.remove("owner");
1607 m_model->setRoles(modelRoles);
1608
1609 // Clear the filter, and verify that all items have the expected roles
1610 m_model->setNameFilter(QString());
1611 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1612
1613 for (int index = 0; index < m_model->count(); ++index) {
1614 // All items should have the "text" and "group" roles, but now "owner".
1615 QVERIFY(m_model->data(index).contains("text"));
1616 QVERIFY(!m_model->data(index).contains("owner"));
1617 QVERIFY(m_model->data(index).contains("group"));
1618 }
1619 }
1620
1621 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1622 {
1623 KFileItemList items;
1624
1625 KIO::UDSEntry entry[3];
1626
1627 entry[0].fastInsert(KIO::UDSEntry::UDS_NAME, "a.txt");
1628 entry[0].fastInsert(KIO::UDSEntry::UDS_USER, "user-b");
1629
1630 entry[1].fastInsert(KIO::UDSEntry::UDS_NAME, "b.txt");
1631 entry[1].fastInsert(KIO::UDSEntry::UDS_USER, "user-c");
1632
1633 entry[2].fastInsert(KIO::UDSEntry::UDS_NAME, "c.txt");
1634 entry[2].fastInsert(KIO::UDSEntry::UDS_USER, "user-a");
1635
1636 for (int i = 0; i < 3; ++i) {
1637 entry[i].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1638 entry[i].fastInsert(KIO::UDSEntry::UDS_ACCESS, 07777);
1639 entry[i].fastInsert(KIO::UDSEntry::UDS_SIZE, 0);
1640 entry[i].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0);
1641 entry[i].fastInsert(KIO::UDSEntry::UDS_GROUP, "group");
1642 entry[i].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME, 0);
1643 items.append(KFileItem(entry[i], m_testDir->url(), false, true));
1644 }
1645
1646 m_model->slotItemsAdded(m_testDir->url(), items);
1647 m_model->slotCompleted();
1648
1649 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1650
1651 // Add a filter.
1652 m_model->setNameFilter("a");
1653 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1654
1655 // Sort by "owner".
1656 m_model->setSortRole("owner");
1657
1658 // Clear the filter, and verify that the items are sorted correctly.
1659 m_model->setNameFilter(QString());
1660 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1661 }
1662
1663 void KFileItemModelTest::testRefreshFilteredItems()
1664 {
1665 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1666
1667 m_testDir->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1668
1669 m_model->loadDirectory(m_testDir->url());
1670 QVERIFY(itemsInsertedSpy.wait());
1671 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1672
1673 const KFileItem fileItemC = m_model->fileItem(2);
1674
1675 // Show only the .txt files.
1676 m_model->setNameFilter(".txt");
1677 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1678
1679 // Rename one of the .jpg files.
1680 KFileItem fileItemE = fileItemC;
1681 QUrl urlE = fileItemE.url().adjusted(QUrl::RemoveFilename);
1682 urlE.setPath(urlE.path() + "/e.jpg");
1683 fileItemE.setUrl(urlE);
1684
1685 m_model->slotRefreshItems({qMakePair(fileItemC, fileItemE)});
1686
1687 // Show all files again, and verify that the model has updated the file name.
1688 m_model->setNameFilter(QString());
1689 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1690 }
1691
1692 void KFileItemModelTest::testCreateMimeData()
1693 {
1694 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1695
1696 QSet<QByteArray> modelRoles = m_model->roles();
1697 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1698 m_model->setRoles(modelRoles);
1699
1700 m_testDir->createFile("a/1");
1701
1702 m_model->loadDirectory(m_testDir->url());
1703 QVERIFY(itemsInsertedSpy.wait());
1704 QCOMPARE(itemsInModel(), QStringList() << "a");
1705
1706 // Expand "a/".
1707 m_model->setExpanded(0, true);
1708 QVERIFY(itemsInsertedSpy.wait());
1709 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1710
1711 // Verify that creating the MIME data for a child of an expanded folder does
1712 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1713 KItemSet selection;
1714 selection.insert(1);
1715 QMimeData* mimeData = m_model->createMimeData(selection);
1716 delete mimeData;
1717 }
1718
1719 void KFileItemModelTest::testCollapseFolderWhileLoading()
1720 {
1721 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1722
1723 QSet<QByteArray> modelRoles = m_model->roles();
1724 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1725 m_model->setRoles(modelRoles);
1726
1727 m_testDir->createFile("a2/b/c1.txt");
1728
1729 m_model->loadDirectory(m_testDir->url());
1730 QVERIFY(itemsInsertedSpy.wait());
1731 QCOMPARE(itemsInModel(), QStringList() << "a2");
1732
1733 // Expand "a2/".
1734 m_model->setExpanded(0, true);
1735 QVERIFY(m_model->isExpanded(0));
1736 QVERIFY(itemsInsertedSpy.wait());
1737 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1738
1739 // Expand "a2/b/".
1740 m_model->setExpanded(1, true);
1741 QVERIFY(m_model->isExpanded(1));
1742 QVERIFY(itemsInsertedSpy.wait());
1743 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1744
1745 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1746 // signal is not emitted yet.
1747 const KFileItem fileItemC1 = m_model->fileItem(2);
1748 KFileItem fileItemC2 = fileItemC1;
1749 QUrl urlC2 = fileItemC2.url();
1750 urlC2 = urlC2.adjusted(QUrl::RemoveFilename);
1751 urlC2.setPath(urlC2.path() + "c2.txt");
1752 fileItemC2.setUrl(urlC2);
1753
1754 const QUrl urlB = m_model->fileItem(1).url();
1755 m_model->slotItemsAdded(urlB, KFileItemList() << fileItemC2);
1756 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1757
1758 // Collapse "a2/". This should also remove all its (indirect) children from
1759 // the model and from the model's m_pendingItemsToInsert member.
1760 m_model->setExpanded(0, false);
1761 QCOMPARE(itemsInModel(), QStringList() << "a2");
1762
1763 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1764 // is still in m_pendingItemsToInsert, then we might get a crash, see
1765 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1766 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1767 // without parent in the model.
1768 m_model->slotCompleted();
1769 QCOMPARE(itemsInModel(), QStringList() << "a2");
1770
1771 // Expand "a2/" again.
1772 m_model->setExpanded(0, true);
1773 QVERIFY(m_model->isExpanded(0));
1774 QVERIFY(itemsInsertedSpy.wait());
1775 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1776
1777 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1778 // completed() signal is not emitted yet.
1779 const KFileItem fileItemA2 = m_model->fileItem(0);
1780 KFileItem fileItemA1 = fileItemA2;
1781 QUrl urlA1 = fileItemA1.url().adjusted(QUrl::RemoveFilename);
1782 urlA1.setPath(urlA1.path() + "a1");
1783 fileItemA1.setUrl(urlA1);
1784
1785 m_model->slotItemsAdded(m_model->directory(), KFileItemList() << fileItemA1);
1786 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1787
1788 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1789 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1790 // confuse the code which collapses the folder.
1791 m_model->setExpanded(0, false);
1792 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1793 QVERIFY(!m_model->isExpanded(0));
1794 QVERIFY(!m_model->isExpanded(1));
1795 }
1796
1797 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1798 {
1799 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1800
1801 m_testDir->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1802
1803 m_model->loadDirectory(m_testDir->url());
1804 QVERIFY(itemsInsertedSpy.wait());
1805 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1806
1807 const KFileItem fileItemB = m_model->fileItem(1);
1808
1809 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1810 KFileItemList list;
1811 list << fileItemB << fileItemB;
1812 m_model->slotItemsDeleted(list);
1813
1814 QVERIFY(m_model->isConsistent());
1815 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1816 }
1817
1818 QStringList KFileItemModelTest::itemsInModel() const
1819 {
1820 QStringList items;
1821 for (int i = 0; i < m_model->count(); i++) {
1822 items << m_model->fileItem(i).text();
1823 }
1824 return items;
1825 }
1826
1827 QTEST_MAIN(KFileItemModelTest)
1828
1829 #include "kfileitemmodeltest.moc"