2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include <QRandomGenerator>
11 #include <QStandardPaths>
16 #include <KIO/SimpleJob>
18 #include "kitemviews/kfileitemmodel.h"
21 void myMessageOutput(QtMsgType type
, const QMessageLogContext
&context
, const QString
&msg
)
31 fprintf(stderr
, "Critical: %s\n", msg
.toLocal8Bit().data());
34 fprintf(stderr
, "Fatal: %s\n", msg
.toLocal8Bit().data());
41 Q_DECLARE_METATYPE(KItemRange
)
42 Q_DECLARE_METATYPE(KItemRangeList
)
43 Q_DECLARE_METATYPE(QList
<int>)
45 class KFileItemModelTest
: public QObject
54 void testDefaultRoles();
55 void testDefaultSortRole();
56 void testDefaultGroupedSorting();
58 void testRemoveItems();
59 void testDirLoadingCompleted();
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();
72 void testIndexForKeyboardSearch();
73 void testNameFilter();
75 void testRefreshExpandedItem();
76 void testAddItemToFilteredExpandedFolder();
77 void testDeleteItemsWithExpandedFolderWithFilter();
78 void testRefreshItemsWithFilter();
79 void testRefreshExpandedFolderWithFilter();
80 void testRemoveHiddenItems();
81 void collapseParentOfHiddenItems();
82 void removeParentOfHiddenItems();
83 void testGeneralParentChildRelationships();
84 void testNameRoleGroups();
85 void testNameRoleGroupsWithExpandedItems();
86 void testInconsistentModel();
87 void testChangeRolesForFilteredItems();
88 void testChangeSortRoleWhileFiltering();
89 void testRefreshFilteredItems();
90 void testCollapseFolderWhileLoading();
91 void testCreateMimeData();
92 void testDeleteFileMoreThanOnce();
93 void testInsertAfterExpand();
94 void testCurrentDirRemoved();
95 void testSizeSortingAfterRefresh();
98 QStringList
itemsInModel() const;
101 KFileItemModel
*m_model
;
105 void KFileItemModelTest::initTestCase()
107 QStandardPaths::setTestModeEnabled(true);
110 void KFileItemModelTest::init()
112 // The item-model tests result in a huge number of debugging
113 // output from kdelibs. Only show critical and fatal messages.
114 qInstallMessageHandler(myMessageOutput
);
116 qRegisterMetaType
<KItemRange
>("KItemRange");
117 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
118 qRegisterMetaType
<KFileItemList
>("KFileItemList");
120 m_testDir
= new TestDir();
121 m_model
= new KFileItemModel();
122 m_model
->m_dirLister
->setAutoUpdate(false);
124 // Reduce the timer interval to make the test run faster.
125 m_model
->m_resortAllItemsTimer
->setInterval(0);
128 void KFileItemModelTest::cleanup()
137 void KFileItemModelTest::testDefaultRoles()
139 const QSet
<QByteArray
> roles
= m_model
->roles();
140 QCOMPARE(roles
.count(), 4);
141 QVERIFY(roles
.contains("text"));
142 QVERIFY(roles
.contains("isDir"));
143 QVERIFY(roles
.contains("isLink"));
144 QVERIFY(roles
.contains("isHidden"));
147 void KFileItemModelTest::testDefaultSortRole()
149 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
150 QVERIFY(itemsInsertedSpy
.isValid());
152 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
154 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
156 m_model
->loadDirectory(m_testDir
->url());
157 QVERIFY(itemsInsertedSpy
.wait());
159 QCOMPARE(m_model
->count(), 3);
160 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
161 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
162 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
165 void KFileItemModelTest::testDefaultGroupedSorting()
167 QCOMPARE(m_model
->groupedSorting(), false);
170 void KFileItemModelTest::testNewItems()
172 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
174 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
176 m_model
->loadDirectory(m_testDir
->url());
177 QVERIFY(itemsInsertedSpy
.wait());
179 QCOMPARE(m_model
->count(), 3);
181 QVERIFY(m_model
->isConsistent());
184 void KFileItemModelTest::testRemoveItems()
186 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
187 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
189 m_testDir
->createFiles({"a.txt", "b.txt"});
190 m_model
->loadDirectory(m_testDir
->url());
191 QVERIFY(itemsInsertedSpy
.wait());
192 QCOMPARE(m_model
->count(), 2);
193 QVERIFY(m_model
->isConsistent());
195 m_testDir
->removeFile("a.txt");
196 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
197 QVERIFY(itemsRemovedSpy
.wait());
198 QCOMPARE(m_model
->count(), 1);
199 QVERIFY(m_model
->isConsistent());
202 void KFileItemModelTest::testDirLoadingCompleted()
204 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
205 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
206 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
208 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
210 m_model
->loadDirectory(m_testDir
->url());
211 QVERIFY(loadingCompletedSpy
.wait());
212 QCOMPARE(loadingCompletedSpy
.count(), 1);
213 QCOMPARE(itemsInsertedSpy
.count(), 1);
214 QCOMPARE(itemsRemovedSpy
.count(), 0);
215 QCOMPARE(m_model
->count(), 3);
217 m_testDir
->createFiles({"d.txt", "e.txt"});
218 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
219 QVERIFY(loadingCompletedSpy
.wait());
220 QCOMPARE(loadingCompletedSpy
.count(), 2);
221 QCOMPARE(itemsInsertedSpy
.count(), 2);
222 QCOMPARE(itemsRemovedSpy
.count(), 0);
223 QCOMPARE(m_model
->count(), 5);
225 m_testDir
->removeFile("a.txt");
226 m_testDir
->createFile("f.txt");
227 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
228 QVERIFY(loadingCompletedSpy
.wait());
229 QCOMPARE(loadingCompletedSpy
.count(), 3);
230 QCOMPARE(itemsInsertedSpy
.count(), 3);
231 QCOMPARE(itemsRemovedSpy
.count(), 1);
232 QCOMPARE(m_model
->count(), 5);
234 m_testDir
->removeFile("b.txt");
235 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
236 QVERIFY(itemsRemovedSpy
.wait());
237 QCOMPARE(loadingCompletedSpy
.count(), 4);
238 QCOMPARE(itemsInsertedSpy
.count(), 3);
239 QCOMPARE(itemsRemovedSpy
.count(), 2);
240 QCOMPARE(m_model
->count(), 4);
242 QVERIFY(m_model
->isConsistent());
245 void KFileItemModelTest::testSetData()
247 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
248 QVERIFY(itemsInsertedSpy
.isValid());
249 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
250 QVERIFY(itemsChangedSpy
.isValid());
252 m_testDir
->createFile("a.txt");
254 m_model
->loadDirectory(m_testDir
->url());
255 QVERIFY(itemsInsertedSpy
.wait());
257 QHash
<QByteArray
, QVariant
> values
;
258 values
.insert("customRole1", "Test1");
259 values
.insert("customRole2", "Test2");
261 m_model
->setData(0, values
);
262 QCOMPARE(itemsChangedSpy
.count(), 1);
264 values
= m_model
->data(0);
265 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
266 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
267 QVERIFY(m_model
->isConsistent());
270 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
272 QTest::addColumn
<int>("changedIndex");
273 QTest::addColumn
<int>("changedRating");
274 QTest::addColumn
<bool>("expectMoveSignal");
275 QTest::addColumn
<int>("ratingIndex0");
276 QTest::addColumn
<int>("ratingIndex1");
277 QTest::addColumn
<int>("ratingIndex2");
280 // Index 0 = rating 2
281 // Index 1 = rating 4
282 // Index 2 = rating 6
284 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
285 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
286 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
288 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
289 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
290 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
293 void KFileItemModelTest::testSetDataWithModifiedSortRole()
295 QFETCH(int, changedIndex
);
296 QFETCH(int, changedRating
);
297 QFETCH(bool, expectMoveSignal
);
298 QFETCH(int, ratingIndex0
);
299 QFETCH(int, ratingIndex1
);
300 QFETCH(int, ratingIndex2
);
302 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
303 QVERIFY(itemsInsertedSpy
.isValid());
304 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
305 QVERIFY(itemsMovedSpy
.isValid());
307 // Changing the value of a sort-role must result in
308 // a reordering of the items.
309 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
310 m_model
->setSortRole("rating");
311 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
313 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
315 m_model
->loadDirectory(m_testDir
->url());
316 QVERIFY(itemsInsertedSpy
.wait());
318 // Fill the "rating" role of each file:
323 QHash
<QByteArray
, QVariant
> ratingA
;
324 ratingA
.insert("rating", 2);
325 m_model
->setData(0, ratingA
);
327 QHash
<QByteArray
, QVariant
> ratingB
;
328 ratingB
.insert("rating", 4);
329 m_model
->setData(1, ratingB
);
331 QHash
<QByteArray
, QVariant
> ratingC
;
332 ratingC
.insert("rating", 6);
333 m_model
->setData(2, ratingC
);
335 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
336 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
337 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
339 // Now change the rating from a.txt. This usually results
340 // in reordering of the items.
341 QHash
<QByteArray
, QVariant
> rating
;
342 rating
.insert("rating", changedRating
);
343 m_model
->setData(changedIndex
, rating
);
345 if (expectMoveSignal
) {
346 QVERIFY(itemsMovedSpy
.wait());
349 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
350 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
351 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
352 QVERIFY(m_model
->isConsistent());
355 void KFileItemModelTest::testChangeSortRole()
357 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
358 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
359 QVERIFY(itemsMovedSpy
.isValid());
361 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
363 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
365 m_model
->loadDirectory(m_testDir
->url());
366 QVERIFY(itemsInsertedSpy
.wait());
367 QCOMPARE(itemsInModel(),
368 QStringList() << "a.txt"
372 // Simulate that KFileItemModelRolesUpdater determines the mime type.
373 // Resorting the files by 'type' will only work immediately if their
374 // mime types are known.
375 for (int index
= 0; index
< m_model
->count(); ++index
) {
376 m_model
->fileItem(index
).determineMimeType();
379 // Now: sort by type.
380 m_model
->setSortRole("type");
381 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
382 QVERIFY(!itemsMovedSpy
.isEmpty());
384 // The actual order of the files might depend on the translation of the
385 // result of KFileItem::mimeComment() in the user's language.
386 QStringList version1
;
391 QStringList version2
;
396 const bool ok1
= (itemsInModel() == version1
);
397 const bool ok2
= (itemsInModel() == version2
);
402 void KFileItemModelTest::testResortAfterChangingName()
404 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
405 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
406 QVERIFY(itemsMovedSpy
.isValid());
408 // We sort by size in a directory where all files have the same size.
409 // Therefore, the files are sorted by their names.
410 m_model
->setSortRole("size");
412 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
414 m_model
->loadDirectory(m_testDir
->url());
415 QVERIFY(itemsInsertedSpy
.wait());
416 QCOMPARE(itemsInModel(),
417 QStringList() << "a.txt"
421 // We rename a.txt to d.txt. Even though the size has not changed at all,
422 // the model must re-sort the items.
423 QHash
<QByteArray
, QVariant
> data
;
424 data
.insert("text", "d.txt");
425 m_model
->setData(0, data
);
427 QVERIFY(itemsMovedSpy
.wait());
428 QCOMPARE(itemsInModel(),
429 QStringList() << "b.txt"
433 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
434 const KFileItem fileItemD
= m_model
->fileItem(2);
435 KFileItem fileItemA
= fileItemD
;
436 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
437 urlA
.setPath(urlA
.path() + "a.txt");
438 fileItemA
.setUrl(urlA
);
440 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
442 QVERIFY(itemsMovedSpy
.wait());
443 QCOMPARE(itemsInModel(),
444 QStringList() << "a.txt"
449 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
451 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
453 // KFileItemModel prevents that inserting a punch of items sequentially
454 // results in an itemsInserted()-signal for each item. Instead internally
455 // a timeout is given that collects such operations and results in only
456 // one itemsInserted()-signal. However in this test we want to stress
457 // KFileItemModel to do a lot of insert operation and hence decrease
458 // the timeout to 1 millisecond.
459 m_testDir
->createFile("1");
460 m_model
->loadDirectory(m_testDir
->url());
461 QVERIFY(itemsInsertedSpy
.wait());
462 QCOMPARE(m_model
->count(), 1);
464 // Insert 10 items for 20 times. After each insert operation the model consistency
466 QSet
<int> insertedItems
;
467 for (int i
= 0; i
< 20; ++i
) {
468 itemsInsertedSpy
.clear();
470 for (int j
= 0; j
< 10; ++j
) {
471 int itemName
= QRandomGenerator::global()->generate();
472 while (insertedItems
.contains(itemName
)) {
473 itemName
= QRandomGenerator::global()->generate();
475 insertedItems
.insert(itemName
);
477 m_testDir
->createFile(QString::number(itemName
));
480 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
481 if (itemsInsertedSpy
.isEmpty()) {
482 QVERIFY(itemsInsertedSpy
.wait());
485 QVERIFY(m_model
->isConsistent());
488 QCOMPARE(m_model
->count(), 201);
491 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
493 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
495 m_testDir
->createFiles({"B", "E", "G"});
497 // Due to inserting the 3 items one item-range with index == 0 and
498 // count == 3 must be given
499 m_model
->loadDirectory(m_testDir
->url());
500 QVERIFY(itemsInsertedSpy
.wait());
502 QCOMPARE(itemsInsertedSpy
.count(), 1);
503 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
504 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
505 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
507 // The indexes of the item-ranges must always be related to the model before
508 // the items have been inserted. Having:
511 // and inserting A, C, D, F the resulting model will be:
514 // and the item-ranges must be:
515 // index: 0, count: 1 for A
516 // index: 1, count: 2 for B, C
517 // index: 2, count: 1 for G
519 m_testDir
->createFiles({"A", "C", "D", "F"});
521 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
522 QVERIFY(itemsInsertedSpy
.wait());
524 QCOMPARE(itemsInsertedSpy
.count(), 1);
525 arguments
= itemsInsertedSpy
.takeFirst();
526 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
527 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
530 void KFileItemModelTest::testExpandItems()
532 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
533 QVERIFY(itemsInsertedSpy
.isValid());
534 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
535 QVERIFY(itemsRemovedSpy
.isValid());
536 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
537 QVERIFY(loadingCompletedSpy
.isValid());
539 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
540 // Besides testing the basic item expansion functionality, the test makes sure that
541 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
542 // yields the correct result for "a/a/1" and "a/a-1/", which is non-trivial because they share the
543 // first three characters.
544 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
545 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
546 modelRoles
<< "isExpanded"
548 << "expandedParentsCount";
549 m_model
->setRoles(modelRoles
);
551 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
553 // Store the URLs of all folders in a set.
554 QSet
<QUrl
> allFolders
;
555 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a") << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
556 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
558 m_model
->loadDirectory(m_testDir
->url());
559 QVERIFY(itemsInsertedSpy
.wait());
561 // So far, the model contains only "a/"
562 QCOMPARE(m_model
->count(), 1);
563 QVERIFY(m_model
->isExpandable(0));
564 QVERIFY(!m_model
->isExpanded(0));
565 QVERIFY(m_model
->expandedDirectories().empty());
567 QCOMPARE(itemsInsertedSpy
.count(), 1);
568 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
569 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
571 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
572 m_model
->setExpanded(0, true);
573 QVERIFY(m_model
->isExpanded(0));
574 QVERIFY(itemsInsertedSpy
.wait());
575 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
576 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
578 QCOMPARE(itemsInsertedSpy
.count(), 1);
579 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
580 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
582 QVERIFY(m_model
->isExpandable(1));
583 QVERIFY(!m_model
->isExpanded(1));
584 QVERIFY(m_model
->isExpandable(2));
585 QVERIFY(!m_model
->isExpanded(2));
587 // Expand the folder "a/a/" -> "a/a/1" becomes visible
588 m_model
->setExpanded(1, true);
589 QVERIFY(m_model
->isExpanded(1));
590 QVERIFY(itemsInsertedSpy
.wait());
591 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
592 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a") << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
594 QCOMPARE(itemsInsertedSpy
.count(), 1);
595 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
596 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
598 QVERIFY(!m_model
->isExpandable(2));
599 QVERIFY(!m_model
->isExpanded(2));
601 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
602 m_model
->setExpanded(3, true);
603 QVERIFY(m_model
->isExpanded(3));
604 QVERIFY(itemsInsertedSpy
.wait());
605 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
606 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
608 QCOMPARE(itemsInsertedSpy
.count(), 1);
609 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
610 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
612 QVERIFY(!m_model
->isExpandable(4));
613 QVERIFY(!m_model
->isExpanded(4));
615 // Collapse the top-level folder -> all other items should disappear
616 m_model
->setExpanded(0, false);
617 QVERIFY(!m_model
->isExpanded(0));
618 QCOMPARE(m_model
->count(), 1);
619 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
621 QCOMPARE(itemsRemovedSpy
.count(), 1);
622 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
623 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
624 QVERIFY(m_model
->isConsistent());
626 // Clear the model, reload the folder and try to restore the expanded folders.
628 QCOMPARE(m_model
->count(), 0);
629 QVERIFY(m_model
->expandedDirectories().empty());
631 m_model
->loadDirectory(m_testDir
->url());
632 m_model
->restoreExpandedDirectories(allFolders
);
633 QVERIFY(loadingCompletedSpy
.wait());
634 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
635 QVERIFY(m_model
->isExpanded(0));
636 QVERIFY(m_model
->isExpanded(1));
637 QVERIFY(!m_model
->isExpanded(2));
638 QVERIFY(m_model
->isExpanded(3));
639 QVERIFY(!m_model
->isExpanded(4));
640 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
641 QVERIFY(m_model
->isConsistent());
643 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
644 // This is how DolphinView restores the expanded folders when navigating in history.
645 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
646 QVERIFY(loadingCompletedSpy
.wait());
647 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
648 m_model
->restoreExpandedDirectories(allFolders
);
649 m_model
->loadDirectory(m_testDir
->url());
650 QVERIFY(loadingCompletedSpy
.wait());
651 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
652 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
654 // Remove all expanded items by changing the roles
655 itemsRemovedSpy
.clear();
656 m_model
->setRoles(originalModelRoles
);
657 QVERIFY(!m_model
->isExpanded(0));
658 QCOMPARE(m_model
->count(), 1);
659 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
661 QCOMPARE(itemsRemovedSpy
.count(), 1);
662 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
663 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
664 QVERIFY(m_model
->isConsistent());
667 void KFileItemModelTest::testExpandParentItems()
669 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
670 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
671 QVERIFY(loadingCompletedSpy
.isValid());
673 // Create a tree structure of folders:
681 QSet
<QByteArray
> modelRoles
= m_model
->roles();
682 modelRoles
<< "isExpanded"
684 << "expandedParentsCount";
685 m_model
->setRoles(modelRoles
);
687 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
689 m_model
->loadDirectory(m_testDir
->url());
690 QVERIFY(itemsInsertedSpy
.wait());
692 // So far, the model contains only "a 1/" and "a2/".
693 QCOMPARE(m_model
->count(), 2);
694 QVERIFY(m_model
->expandedDirectories().empty());
696 // Expand the parents of "a2/b2/c2".
697 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
698 QVERIFY(loadingCompletedSpy
.wait());
700 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
701 // It's important that only the parents of "a1/b1/c1" are expanded.
702 QCOMPARE(m_model
->count(), 4);
703 QVERIFY(!m_model
->isExpanded(0));
704 QVERIFY(m_model
->isExpanded(1));
705 QVERIFY(m_model
->isExpanded(2));
706 QVERIFY(!m_model
->isExpanded(3));
708 // Expand the parents of "a 1/b1".
709 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
710 QVERIFY(loadingCompletedSpy
.wait());
712 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
713 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
714 QCOMPARE(m_model
->count(), 5);
715 QVERIFY(m_model
->isExpanded(0));
716 QVERIFY(!m_model
->isExpanded(1));
717 QVERIFY(m_model
->isExpanded(2));
718 QVERIFY(m_model
->isExpanded(3));
719 QVERIFY(!m_model
->isExpanded(4));
720 QVERIFY(m_model
->isConsistent());
723 m_model
->setExpanded(1, true);
724 QVERIFY(loadingCompletedSpy
.wait());
725 QCOMPARE(m_model
->count(), 6);
726 QVERIFY(m_model
->isExpanded(0));
727 QVERIFY(m_model
->isExpanded(1));
728 QVERIFY(!m_model
->isExpanded(2));
729 QVERIFY(m_model
->isExpanded(3));
730 QVERIFY(m_model
->isExpanded(4));
731 QVERIFY(!m_model
->isExpanded(5));
732 QVERIFY(m_model
->isConsistent());
734 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
735 m_model
->setExpanded(1, false);
736 QCOMPARE(m_model
->count(), 5);
737 QVERIFY(m_model
->isExpanded(0));
738 QVERIFY(!m_model
->isExpanded(1));
739 QVERIFY(m_model
->isExpanded(2));
740 QVERIFY(m_model
->isExpanded(3));
741 QVERIFY(!m_model
->isExpanded(4));
742 QVERIFY(m_model
->isConsistent());
746 * Renaming an expanded folder by prepending its name with a dot makes it
747 * hidden. Verify that this does not cause an inconsistent model state and
748 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
750 void KFileItemModelTest::testMakeExpandedItemHidden()
752 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
753 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
755 QSet
<QByteArray
> modelRoles
= m_model
->roles();
756 modelRoles
<< "isExpanded"
758 << "expandedParentsCount";
759 m_model
->setRoles(modelRoles
);
761 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
763 m_model
->loadDirectory(m_testDir
->url());
764 QVERIFY(itemsInsertedSpy
.wait());
766 // So far, the model contains only "1a/" and "1b".
767 QCOMPARE(m_model
->count(), 2);
768 m_model
->setExpanded(0, true);
769 QVERIFY(itemsInsertedSpy
.wait());
771 // Now "1a/2a" and "1a/2b" have appeared.
772 QCOMPARE(m_model
->count(), 4);
773 m_model
->setExpanded(1, true);
774 QVERIFY(itemsInsertedSpy
.wait());
775 QCOMPARE(m_model
->count(), 6);
777 // Rename "1a/2" and make it hidden.
778 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
779 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
781 KIO::SimpleJob
*job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
782 bool ok
= job
->exec();
784 QVERIFY(itemsRemovedSpy
.wait());
786 // "1a/2" and its subfolders have disappeared now.
787 QVERIFY(m_model
->isConsistent());
788 QCOMPARE(m_model
->count(), 3);
790 m_model
->setExpanded(0, false);
791 QCOMPARE(m_model
->count(), 2);
794 void KFileItemModelTest::testRemoveFilteredExpandedItems()
796 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
798 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
799 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
800 modelRoles
<< "isExpanded"
802 << "expandedParentsCount";
803 m_model
->setRoles(modelRoles
);
805 m_testDir
->createFiles({"folder/child", "file"});
807 m_model
->loadDirectory(m_testDir
->url());
808 QVERIFY(itemsInsertedSpy
.wait());
810 // So far, the model contains only "folder/" and "file".
811 QCOMPARE(m_model
->count(), 2);
812 QVERIFY(m_model
->isExpandable(0));
813 QVERIFY(!m_model
->isExpandable(1));
814 QVERIFY(!m_model
->isExpanded(0));
815 QVERIFY(!m_model
->isExpanded(1));
816 QCOMPARE(itemsInModel(),
817 QStringList() << "folder"
820 // Expand "folder" -> "folder/child" becomes visible.
821 m_model
->setExpanded(0, true);
822 QVERIFY(m_model
->isExpanded(0));
823 QVERIFY(itemsInsertedSpy
.wait());
824 QCOMPARE(itemsInModel(),
825 QStringList() << "folder"
829 // Add a name filter.
830 m_model
->setNameFilter("f");
831 QCOMPARE(itemsInModel(),
832 QStringList() << "folder"
835 m_model
->setNameFilter("fo");
836 QCOMPARE(itemsInModel(), QStringList() << "folder");
838 // Remove all expanded items by changing the roles
839 m_model
->setRoles(originalModelRoles
);
840 QVERIFY(!m_model
->isExpanded(0));
841 QCOMPARE(itemsInModel(), QStringList() << "folder");
843 // Remove the name filter and verify that "folder/child" does not reappear.
844 m_model
->setNameFilter(QString());
845 QCOMPARE(itemsInModel(),
846 QStringList() << "folder"
850 void KFileItemModelTest::testSizeSortingAfterRefresh()
852 // testDir structure is as follows
858 // │ │ ├─ c-3
863 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
864 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
865 QVERIFY(itemsMovedSpy
.isValid());
867 // Create some files with different sizes and modification times to check the different sorting options
868 QDateTime now
= QDateTime::currentDateTime();
870 QSet
<QByteArray
> roles
;
871 roles
.insert("text");
872 roles
.insert("isExpanded");
873 roles
.insert("isExpandable");
874 roles
.insert("expandedParentsCount");
875 m_model
->setRoles(roles
);
877 m_testDir
->createDir("c/c-2");
878 m_testDir
->createFile("c/c-2/c-3");
879 m_testDir
->createFile("c/c-1");
881 m_testDir
->createFile("a", "A file", now
.addDays(-3));
882 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
883 m_testDir
->createDir("c", now
.addDays(-2));
884 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
885 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
887 m_model
->loadDirectory(m_testDir
->url());
888 QVERIFY(itemsInsertedSpy
.wait());
890 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
891 m_model
->setExpanded(index
, true);
892 QVERIFY(itemsInsertedSpy
.wait());
894 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
895 m_model
->setExpanded(index
, true);
896 QVERIFY(itemsInsertedSpy
.wait());
898 // Default: Sort by Name, ascending
899 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
900 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
901 QCOMPARE(itemsInModel(),
911 // Sort by Size, ascending, before refresh
912 m_model
->setSortRole("size");
913 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
914 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
915 QCOMPARE(itemsInModel(),
926 m_model
->refreshDirectory(m_model
->directory());
927 QVERIFY(itemsInsertedSpy
.wait());
929 // Expand folders again
930 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
931 m_model
->setExpanded(index
, true);
932 QVERIFY(itemsInsertedSpy
.wait());
934 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
935 m_model
->setExpanded(index
, true);
936 QVERIFY(itemsInsertedSpy
.wait());
938 // Sort by Size, ascending, after refresh
939 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
940 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
941 QCOMPARE(itemsInModel(),
952 void KFileItemModelTest::testSorting()
954 // testDir structure is as follows
961 // │ │ ├─ c-3
967 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
968 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
969 QVERIFY(itemsMovedSpy
.isValid());
971 // Create some files with different sizes and modification times to check the different sorting options
972 QDateTime now
= QDateTime::currentDateTime();
974 QSet
<QByteArray
> roles
;
975 roles
.insert("text");
976 roles
.insert("isExpanded");
977 roles
.insert("isExpandable");
978 roles
.insert("expandedParentsCount");
979 m_model
->setRoles(roles
);
981 m_testDir
->createDir("c/c-2");
982 m_testDir
->createFile("c/c-2/c-3");
983 m_testDir
->createFile("c/c-1");
985 m_testDir
->createFile("a", "A file", now
.addDays(-3));
986 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
987 m_testDir
->createDir("c", now
.addDays(-2));
988 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
989 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
990 m_testDir
->createFile(".f");
991 m_testDir
->createDir(".g");
993 m_model
->loadDirectory(m_testDir
->url());
994 QVERIFY(itemsInsertedSpy
.wait());
995 QCOMPARE(itemsInsertedSpy
.count(), 1);
996 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
997 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 5));
999 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
1000 m_model
->setExpanded(index
, true);
1001 QVERIFY(itemsInsertedSpy
.wait());
1002 QCOMPARE(itemsInsertedSpy
.count(), 1);
1003 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1004 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1006 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
1007 m_model
->setExpanded(index
, true);
1008 QVERIFY(itemsInsertedSpy
.wait());
1009 QCOMPARE(itemsInsertedSpy
.count(), 1);
1010 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1011 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1));
1013 // Default: Sort by Name, ascending
1014 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1015 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1016 QVERIFY(m_model
->sortDirectoriesFirst());
1017 QVERIFY(!m_model
->showHiddenFiles());
1018 QCOMPARE(itemsInModel(),
1019 QStringList() << "c"
1028 // Sort by Name, ascending, 'Sort Folders First' disabled
1029 m_model
->setSortDirectoriesFirst(false);
1030 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1031 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1032 QCOMPARE(itemsInModel(),
1033 QStringList() << "a"
1041 QCOMPARE(itemsMovedSpy
.count(), 1);
1042 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1043 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
1045 // Sort by Name, descending
1046 m_model
->setSortDirectoriesFirst(true);
1047 m_model
->setSortOrder(Qt::DescendingOrder
);
1048 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1049 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1050 QCOMPARE(itemsInModel(),
1051 QStringList() << "c"
1059 QCOMPARE(itemsMovedSpy
.count(), 2);
1060 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1061 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
1062 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1063 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1065 // Sort by Date, descending
1066 m_model
->setSortDirectoriesFirst(true);
1067 m_model
->setSortRole("modificationtime");
1068 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1069 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1070 QCOMPARE(itemsInModel(),
1071 QStringList() << "c"
1079 QCOMPARE(itemsMovedSpy
.count(), 1);
1080 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1081 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 5 << 4 << 6);
1083 // Sort by Date, ascending
1084 m_model
->setSortOrder(Qt::AscendingOrder
);
1085 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1086 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1087 QCOMPARE(itemsInModel(),
1088 QStringList() << "c"
1096 QCOMPARE(itemsMovedSpy
.count(), 1);
1097 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1098 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1100 // Sort by Date, ascending, 'Sort Folders First' disabled
1101 m_model
->setSortDirectoriesFirst(false);
1102 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1103 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1104 QVERIFY(!m_model
->sortDirectoriesFirst());
1105 QCOMPARE(itemsInModel(),
1106 QStringList() << "e"
1114 QCOMPARE(itemsMovedSpy
.count(), 1);
1115 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1116 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
1118 // Sort by Name, ascending, 'Sort Folders First' disabled
1119 m_model
->setSortRole("text");
1120 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1121 QVERIFY(!m_model
->sortDirectoriesFirst());
1122 QCOMPARE(itemsInModel(),
1123 QStringList() << "a"
1131 QCOMPARE(itemsMovedSpy
.count(), 1);
1132 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1133 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
1135 // Sort by Size, ascending, 'Sort Folders First' disabled
1136 m_model
->setSortRole("size");
1137 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1138 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1139 QVERIFY(!m_model
->sortDirectoriesFirst());
1140 QCOMPARE(itemsInModel(),
1141 QStringList() << "c"
1149 QCOMPARE(itemsMovedSpy
.count(), 1);
1150 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1151 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
1153 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
1154 m_model
->setSortDirectoriesFirst(true);
1155 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1156 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1157 QVERIFY(m_model
->sortDirectoriesFirst());
1158 QCOMPARE(itemsInModel(),
1159 QStringList() << "c"
1167 QCOMPARE(itemsMovedSpy
.count(), 0);
1169 // Sort by Size, descending, 'Sort Folders First' enabled
1170 m_model
->setSortOrder(Qt::DescendingOrder
);
1171 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1172 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1173 QVERIFY(m_model
->sortDirectoriesFirst());
1174 QCOMPARE(itemsInModel(),
1175 QStringList() << "c"
1183 QCOMPARE(itemsMovedSpy
.count(), 1);
1184 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1185 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1187 // 'Show Hidden Files' enabled
1188 m_model
->setShowHiddenFiles(true);
1189 QVERIFY(m_model
->showHiddenFiles());
1190 QVERIFY(!m_model
->sortHiddenLast());
1191 QCOMPARE(itemsInModel(),
1192 QStringList() << "c"
1202 QCOMPARE(itemsMovedSpy
.count(), 0);
1203 QCOMPARE(itemsInsertedSpy
.count(), 1);
1204 QCOMPARE(itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
1206 // 'Sort Hidden Files Last' enabled
1207 m_model
->setSortHiddenLast(true);
1208 QVERIFY(m_model
->sortHiddenLast());
1209 QCOMPARE(itemsInModel(),
1210 QStringList() << "c"
1220 QCOMPARE(itemsMovedSpy
.count(), 1);
1221 QCOMPARE(itemsInsertedSpy
.count(), 0);
1222 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 5));
1223 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 8 << 4 << 5 << 6 << 7);
1226 m_model
->setSortRole("text");
1227 QCOMPARE(itemsInModel(),
1228 QStringList() << "c"
1238 QCOMPARE(itemsMovedSpy
.count(), 1);
1239 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 2));
1240 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 5 << 4);
1243 m_model
->setSortOrder(Qt::AscendingOrder
);
1244 QCOMPARE(itemsInModel(),
1245 QStringList() << "c"
1255 QCOMPARE(itemsMovedSpy
.count(), 1);
1256 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1257 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1259 // 'Sort Folders First' disabled
1260 m_model
->setSortDirectoriesFirst(false);
1261 QVERIFY(!m_model
->sortDirectoriesFirst());
1262 QCOMPARE(itemsInModel(),
1263 QStringList() << "a"
1273 QCOMPARE(itemsMovedSpy
.count(), 1);
1274 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 10));
1275 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1278 void KFileItemModelTest::testIndexForKeyboardSearch()
1280 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1282 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1284 m_model
->loadDirectory(m_testDir
->url());
1285 QVERIFY(itemsInsertedSpy
.wait());
1287 // Search from index 0
1288 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
1289 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
1290 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
1291 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
1292 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
1293 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
1294 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
1295 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
1296 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
1297 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
1298 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
1300 // Start a search somewhere in the middle
1301 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
1302 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
1303 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
1304 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
1306 // Test searches that go past the last item back to index 0
1307 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
1308 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
1309 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
1310 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
1312 // Test searches that yield no result
1313 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
1314 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
1315 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
1316 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
1317 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
1319 // Test upper case searches (note that search is case insensitive)
1320 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
1321 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
1322 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
1323 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1325 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1328 void KFileItemModelTest::testNameFilter()
1330 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1332 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1334 m_model
->loadDirectory(m_testDir
->url());
1335 QVERIFY(itemsInsertedSpy
.wait());
1337 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1338 QCOMPARE(m_model
->count(), 3);
1340 m_model
->setNameFilter("A2"); // Shows only A2
1341 QCOMPARE(m_model
->count(), 1);
1343 m_model
->setNameFilter("A2"); // Shows only A1
1344 QCOMPARE(m_model
->count(), 1);
1346 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1347 QCOMPARE(m_model
->count(), 2);
1349 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1350 QCOMPARE(m_model
->count(), 2);
1352 m_model
->setNameFilter(QString()); // Shows again all items
1353 QCOMPARE(m_model
->count(), 5);
1357 * Verifies that we do not crash when adding a KFileItem with an empty path.
1358 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1359 * tried to always read the first character of the path, even if the path is empty.
1361 void KFileItemModelTest::testEmptyPath()
1363 QSet
<QByteArray
> roles
;
1364 roles
.insert("text");
1365 roles
.insert("isExpanded");
1366 roles
.insert("isExpandable");
1367 roles
.insert("expandedParentsCount");
1368 m_model
->setRoles(roles
);
1370 const QUrl emptyUrl
;
1371 QVERIFY(emptyUrl
.path().isEmpty());
1373 const QUrl
url("file:///test/");
1375 KFileItemList items
;
1376 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1377 m_model
->slotItemsAdded(emptyUrl
, items
);
1378 m_model
->slotCompleted();
1382 * Verifies that the 'isExpanded' state of folders does not change when the
1383 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1385 void KFileItemModelTest::testRefreshExpandedItem()
1387 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1388 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1389 QVERIFY(itemsChangedSpy
.isValid());
1391 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1392 modelRoles
<< "isExpanded"
1394 << "expandedParentsCount";
1395 m_model
->setRoles(modelRoles
);
1397 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1399 m_model
->loadDirectory(m_testDir
->url());
1400 QVERIFY(itemsInsertedSpy
.wait());
1401 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1403 m_model
->setExpanded(0, true);
1404 QVERIFY(itemsInsertedSpy
.wait());
1405 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1406 QVERIFY(m_model
->isExpanded(0));
1408 const KFileItem item
= m_model
->fileItem(0);
1409 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1410 QVERIFY(!itemsChangedSpy
.isEmpty());
1412 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1413 QVERIFY(m_model
->isExpanded(0));
1417 * Verifies that adding an item to an expanded folder that's filtered makes the parental chain visible.
1419 void KFileItemModelTest::testAddItemToFilteredExpandedFolder()
1421 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1422 QSignalSpy
fileItemsChangedSpy(m_model
, &KFileItemModel::fileItemsChanged
);
1424 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1425 modelRoles
<< "isExpanded"
1427 << "expandedParentsCount";
1428 m_model
->setRoles(modelRoles
);
1430 m_testDir
->createFile("a/b/file");
1432 m_model
->loadDirectory(m_testDir
->url());
1433 QVERIFY(itemsInsertedSpy
.wait());
1434 QCOMPARE(m_model
->count(), 1); // "a
1437 m_model
->setExpanded(0, true);
1438 QVERIFY(itemsInsertedSpy
.wait());
1441 m_model
->setExpanded(1, true);
1442 QVERIFY(itemsInsertedSpy
.wait());
1444 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1446 const QUrl urlB
= m_model
->fileItem(1).url();
1448 // Set a filter that matches ".txt" extension
1449 m_model
->setNameFilter("*.txt");
1450 QCOMPARE(m_model
->count(), 0); // Everything got hidden since we don't have a .txt file yet
1452 m_model
->slotItemsAdded(urlB
, KFileItemList() << KFileItem(QUrl("a/b/newItem.txt")));
1453 m_model
->slotCompleted();
1455 // Entire parental chain should now be shown
1456 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/newItem.txt"
1457 QCOMPARE(itemsInModel(),
1458 QStringList() << "a"
1462 // Items should be indented in hierarchy
1463 QCOMPARE(m_model
->expandedParentsCount(0), 0);
1464 QCOMPARE(m_model
->expandedParentsCount(1), 1);
1465 QCOMPARE(m_model
->expandedParentsCount(2), 2);
1469 * Verifies that deleting the last filter-passing child from expanded folders
1470 * makes the parental chain hidden.
1472 void KFileItemModelTest::testDeleteItemsWithExpandedFolderWithFilter()
1474 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1475 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1477 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1478 modelRoles
<< "isExpanded"
1480 << "expandedParentsCount";
1481 m_model
->setRoles(modelRoles
);
1483 m_testDir
->createFile("a/b/file");
1485 m_model
->loadDirectory(m_testDir
->url());
1486 QVERIFY(itemsInsertedSpy
.wait());
1487 QCOMPARE(m_model
->count(), 1); // "a
1490 m_model
->setExpanded(0, true);
1491 QVERIFY(itemsInsertedSpy
.wait());
1494 m_model
->setExpanded(1, true);
1495 QVERIFY(itemsInsertedSpy
.wait());
1497 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1499 // Set a filter that matches "file" extension
1500 m_model
->setNameFilter("file");
1501 QCOMPARE(m_model
->count(), 3); // Everything is still shown
1504 QCOMPARE(itemsRemovedSpy
.count(), 0);
1505 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(2));
1506 QCOMPARE(itemsRemovedSpy
.count(), 1);
1508 // Entire parental chain should now be filtered
1509 QCOMPARE(m_model
->count(), 0);
1510 QCOMPARE(m_model
->m_filteredItems
.size(), 2);
1514 * Verifies that the fileItemsChanged signal is raised with the correct index after renaming files with filter set.
1515 * The rename operation will cause one item to be filtered out and another item to be reordered.
1517 void KFileItemModelTest::testRefreshItemsWithFilter()
1519 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1520 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1521 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1522 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1524 // Creates three .txt files
1525 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt"});
1527 m_model
->loadDirectory(m_testDir
->url());
1528 QVERIFY(itemsInsertedSpy
.wait());
1530 QCOMPARE(m_model
->count(), 3); // "b.txt", "c.txt", "d.txt"
1532 // Set a filter that matches ".txt" extension
1533 m_model
->setNameFilter("*.txt");
1534 QCOMPARE(m_model
->count(), 3); // Still all items are shown
1535 QCOMPARE(itemsInModel(),
1536 QStringList() << "b.txt"
1540 // Objects used to rename
1541 const KFileItem fileItemC_txt
= m_model
->fileItem(1);
1542 KFileItem fileItemC_cfg
= fileItemC_txt
;
1543 fileItemC_cfg
.setUrl(QUrl("c.cfg"));
1545 const KFileItem fileItemD_txt
= m_model
->fileItem(2);
1546 KFileItem fileItemA_txt
= fileItemD_txt
;
1547 fileItemA_txt
.setUrl(QUrl("a.txt"));
1549 // Rename "c.txt" to "c.cfg"; and rename "d.txt" to "a.txt"
1550 QCOMPARE(itemsRemovedSpy
.count(), 0);
1551 QCOMPARE(itemsChangedSpy
.count(), 0);
1552 m_model
->slotRefreshItems({qMakePair(fileItemC_txt
, fileItemC_cfg
), qMakePair(fileItemD_txt
, fileItemA_txt
)});
1553 QCOMPARE(itemsRemovedSpy
.count(), 1);
1554 QCOMPARE(itemsChangedSpy
.count(), 1);
1555 QCOMPARE(m_model
->count(), 2); // Only "a.txt" and "b.txt". "c.cfg" got filtered out
1557 QList
<QVariant
> arguments
= itemsChangedSpy
.takeLast();
1558 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1560 // We started with the order "b.txt", "c.txt", "d.txt"
1561 // "d.txt" started with index "2"
1562 // "c.txt" got renamed and got filtered out
1563 // "d.txt" index shifted from index "2" to "1"
1564 // So we expect index "1" in this argument, meaning "d.txt" was renamed
1565 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1567 // Re-sorting is done asynchronously:
1568 QCOMPARE(itemsInModel(),
1569 QStringList() << "b.txt"
1570 << "a.txt"); // Files should still be in the incorrect order
1571 QVERIFY(itemsMovedSpy
.wait());
1572 QCOMPARE(itemsInModel(),
1573 QStringList() << "a.txt"
1574 << "b.txt"); // Files were re-sorted and should now be in the correct order
1578 * Verifies that parental chains are hidden and shown as needed while their children get filtered/unfiltered due to renaming.
1579 * Also verifies that the "isExpanded" and "expandedParentsCount" values are kept for expanded folders that get refreshed.
1581 void KFileItemModelTest::testRefreshExpandedFolderWithFilter()
1583 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1584 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1586 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1587 modelRoles
<< "isExpanded"
1589 << "expandedParentsCount";
1590 m_model
->setRoles(modelRoles
);
1592 m_testDir
->createFile("a/b/someFolder/someFile");
1594 m_model
->loadDirectory(m_testDir
->url());
1595 QVERIFY(itemsInsertedSpy
.wait());
1597 QCOMPARE(m_model
->count(), 1); // Only "a/"
1600 m_model
->setExpanded(0, true);
1601 QVERIFY(itemsInsertedSpy
.wait());
1604 m_model
->setExpanded(1, true);
1605 QVERIFY(itemsInsertedSpy
.wait());
1607 // Expand "a/b/someFolder/".
1608 m_model
->setExpanded(2, true);
1609 QVERIFY(itemsInsertedSpy
.wait());
1610 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/b/", "a/b/someFolder", "a/b/someFolder/someFile"
1612 // Set a filter that matches the expanded folder "someFolder"
1613 m_model
->setNameFilter("someFolder");
1614 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/someFolder"
1616 // Objects used to rename
1617 const KFileItem fileItemA
= m_model
->fileItem(0);
1618 KFileItem fileItemARenamed
= fileItemA
;
1619 fileItemARenamed
.setUrl(QUrl("a_renamed"));
1621 const KFileItem fileItemSomeFolder
= m_model
->fileItem(2);
1622 KFileItem fileItemRenamedFolder
= fileItemSomeFolder
;
1623 fileItemRenamedFolder
.setUrl(QUrl("/a_renamed/b/renamedFolder"));
1625 // Rename "a" to "a_renamed"
1626 // This way we test if the algorithm is sane as to NOT hide "a_renamed" since it will have visible children
1627 m_model
->slotRefreshItems({qMakePair(fileItemA
, fileItemARenamed
)});
1628 QCOMPARE(m_model
->count(), 3); // Entire parental chain must still be shown
1629 QCOMPARE(itemsInModel(),
1630 QStringList() << "a_renamed"
1634 // Rename "a_renamed" back to "a"; and "someFolder" to "renamedFolder"
1635 m_model
->slotRefreshItems({qMakePair(fileItemARenamed
, fileItemA
), qMakePair(fileItemSomeFolder
, fileItemRenamedFolder
)});
1636 QCOMPARE(m_model
->count(), 0); // Entire parental chain became hidden
1638 // Rename "renamedFolder" back to "someFolder". Filter is passing again
1639 m_model
->slotRefreshItems({qMakePair(fileItemRenamedFolder
, fileItemSomeFolder
)});
1640 QCOMPARE(m_model
->count(), 3); // Entire parental chain is shown again
1641 QCOMPARE(itemsInModel(),
1642 QStringList() << "a"
1646 // slotRefreshItems() should preserve "isExpanded" and "expandedParentsCount" values explicitly in this case
1647 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("isExpanded").toBool(), true);
1648 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("expandedParentsCount"), 2);
1652 * Verify that removing hidden files and folders from the model does not
1653 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1655 void KFileItemModelTest::testRemoveHiddenItems()
1657 m_testDir
->createDir(".a");
1658 m_testDir
->createDir(".b");
1659 m_testDir
->createDir("c");
1660 m_testDir
->createDir("d");
1661 m_testDir
->createFiles({".f", ".g", "h", "i"});
1663 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1664 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1666 m_model
->setShowHiddenFiles(true);
1667 m_model
->loadDirectory(m_testDir
->url());
1668 QVERIFY(itemsInsertedSpy
.wait());
1669 QCOMPARE(itemsInModel(),
1670 QStringList() << ".a"
1678 QCOMPARE(itemsInsertedSpy
.count(), 1);
1679 QCOMPARE(itemsRemovedSpy
.count(), 0);
1680 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1681 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1683 m_model
->setShowHiddenFiles(false);
1684 QCOMPARE(itemsInModel(),
1685 QStringList() << "c"
1689 QCOMPARE(itemsInsertedSpy
.count(), 0);
1690 QCOMPARE(itemsRemovedSpy
.count(), 1);
1691 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1692 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1694 m_model
->setShowHiddenFiles(true);
1695 QCOMPARE(itemsInModel(),
1696 QStringList() << ".a"
1704 QCOMPARE(itemsInsertedSpy
.count(), 1);
1705 QCOMPARE(itemsRemovedSpy
.count(), 0);
1706 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1707 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1710 QCOMPARE(itemsInModel(), QStringList());
1711 QCOMPARE(itemsInsertedSpy
.count(), 0);
1712 QCOMPARE(itemsRemovedSpy
.count(), 1);
1713 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1714 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1716 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1717 // Verify that this does not make the model crash.
1718 m_model
->setShowHiddenFiles(false);
1722 * Verify that filtered items are removed when their parent is collapsed.
1724 void KFileItemModelTest::collapseParentOfHiddenItems()
1726 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1727 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1729 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1730 modelRoles
<< "isExpanded"
1732 << "expandedParentsCount";
1733 m_model
->setRoles(modelRoles
);
1735 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1737 m_model
->loadDirectory(m_testDir
->url());
1738 QVERIFY(itemsInsertedSpy
.wait());
1739 QCOMPARE(m_model
->count(), 1); // Only "a/"
1742 m_model
->setExpanded(0, true);
1743 QVERIFY(itemsInsertedSpy
.wait());
1744 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1747 m_model
->setExpanded(1, true);
1748 QVERIFY(itemsInsertedSpy
.wait());
1749 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1752 m_model
->setExpanded(2, true);
1753 QVERIFY(itemsInsertedSpy
.wait());
1754 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"
1756 // Set a name filter that matches nothing -> nothing should remain.
1757 m_model
->setNameFilter("xyz");
1758 QCOMPARE(itemsRemovedSpy
.count(), 1);
1759 QCOMPARE(m_model
->count(), 0); // Everything is hidden
1760 QCOMPARE(itemsInModel(), QStringList());
1762 // Filter by the file names. Folder "d" will be hidden since it was collapsed
1763 m_model
->setNameFilter("1");
1764 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1765 QCOMPARE(m_model
->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1767 // Collapse the folder "a/".
1768 m_model
->setExpanded(0, false);
1769 QCOMPARE(itemsRemovedSpy
.count(), 2);
1770 QCOMPARE(m_model
->count(), 1);
1771 QCOMPARE(itemsInModel(), QStringList() << "a");
1773 // Remove the filter -> "a" should still appear (and we should not get a crash).
1774 m_model
->setNameFilter(QString());
1775 QCOMPARE(itemsRemovedSpy
.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1776 QCOMPARE(m_model
->count(), 1);
1777 QCOMPARE(itemsInModel(), QStringList() << "a");
1781 * Verify that filtered items are removed when their parent is deleted.
1783 void KFileItemModelTest::removeParentOfHiddenItems()
1785 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1786 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1788 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1789 modelRoles
<< "isExpanded"
1791 << "expandedParentsCount";
1792 m_model
->setRoles(modelRoles
);
1794 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1796 m_model
->loadDirectory(m_testDir
->url());
1797 QVERIFY(itemsInsertedSpy
.wait());
1798 QCOMPARE(m_model
->count(), 1); // Only "a/"
1801 m_model
->setExpanded(0, true);
1802 QVERIFY(itemsInsertedSpy
.wait());
1803 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1806 m_model
->setExpanded(1, true);
1807 QVERIFY(itemsInsertedSpy
.wait());
1808 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1811 m_model
->setExpanded(2, true);
1812 QVERIFY(itemsInsertedSpy
.wait());
1813 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"
1815 // Set a name filter that matches nothing -> nothing should remain.
1816 m_model
->setNameFilter("xyz");
1817 QCOMPARE(itemsRemovedSpy
.count(), 1);
1818 QCOMPARE(m_model
->count(), 0);
1819 QCOMPARE(itemsInModel(), QStringList());
1821 // Filter by "c". Folder "b" will also be shown because it is its parent.
1822 m_model
->setNameFilter("c");
1823 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1824 QCOMPARE(m_model
->count(), 3);
1825 QCOMPARE(itemsInModel(),
1826 QStringList() << "a"
1830 // Simulate the deletion of the directory "a/b/".
1831 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1832 QCOMPARE(itemsRemovedSpy
.count(), 2);
1833 QCOMPARE(m_model
->count(), 0); // "a" will be filtered out since it doesn't pass the filter and doesn't have visible children
1835 // Remove the filter -> only the file "a/1" should appear.
1836 m_model
->setNameFilter(QString());
1837 QCOMPARE(m_model
->count(), 2);
1838 QCOMPARE(itemsInModel(),
1839 QStringList() << "a"
1844 * Create a tree structure where parent-child relationships can not be
1845 * determined by parsing the URLs, and verify that KFileItemModel
1846 * handles them correctly.
1848 void KFileItemModelTest::testGeneralParentChildRelationships()
1850 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1851 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1853 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1854 modelRoles
<< "isExpanded"
1856 << "expandedParentsCount";
1857 m_model
->setRoles(modelRoles
);
1859 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1861 m_model
->loadDirectory(m_testDir
->url());
1862 QVERIFY(itemsInsertedSpy
.wait());
1863 QCOMPARE(itemsInModel(),
1864 QStringList() << "parent1"
1867 // Expand all folders.
1868 m_model
->setExpanded(0, true);
1869 QVERIFY(itemsInsertedSpy
.wait());
1870 QCOMPARE(itemsInModel(),
1871 QStringList() << "parent1"
1875 m_model
->setExpanded(1, true);
1876 QVERIFY(itemsInsertedSpy
.wait());
1877 QCOMPARE(itemsInModel(),
1878 QStringList() << "parent1"
1880 << "realGrandChild1"
1883 m_model
->setExpanded(3, true);
1884 QVERIFY(itemsInsertedSpy
.wait());
1885 QCOMPARE(itemsInModel(),
1886 QStringList() << "parent1"
1888 << "realGrandChild1"
1892 m_model
->setExpanded(4, true);
1893 QVERIFY(itemsInsertedSpy
.wait());
1894 QCOMPARE(itemsInModel(),
1895 QStringList() << "parent1"
1897 << "realGrandChild1"
1900 << "realGrandChild2");
1902 // Add some more children and grand-children.
1903 const QUrl parent1
= m_model
->fileItem(0).url();
1904 const QUrl parent2
= m_model
->fileItem(3).url();
1905 const QUrl realChild1
= m_model
->fileItem(1).url();
1906 const QUrl realChild2
= m_model
->fileItem(4).url();
1908 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1909 m_model
->slotCompleted();
1910 QCOMPARE(itemsInModel(),
1911 QStringList() << "parent1"
1913 << "realGrandChild1"
1917 << "realGrandChild2");
1919 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1920 m_model
->slotCompleted();
1921 QCOMPARE(itemsInModel(),
1922 QStringList() << "parent1"
1924 << "realGrandChild1"
1928 << "realGrandChild2"
1931 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1932 m_model
->slotCompleted();
1933 QCOMPARE(itemsInModel(),
1934 QStringList() << "parent1"
1937 << "realGrandChild1"
1941 << "realGrandChild2"
1944 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1945 m_model
->slotCompleted();
1946 QCOMPARE(itemsInModel(),
1947 QStringList() << "parent1"
1950 << "realGrandChild1"
1955 << "realGrandChild2"
1958 // Set a name filter that matches nothing -> nothing will remain.
1959 m_model
->setNameFilter("xyz");
1960 QCOMPARE(itemsInModel(), QStringList());
1961 QCOMPARE(itemsRemovedSpy
.count(), 1);
1962 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1963 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1964 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 10));
1966 // Set a name filter that matches only "realChild". Their prarents should still show.
1967 m_model
->setNameFilter("realChild");
1968 QCOMPARE(itemsInModel(),
1969 QStringList() << "parent1"
1973 QCOMPARE(itemsRemovedSpy
.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
1975 // Collapse "parent1".
1976 m_model
->setExpanded(0, false);
1977 QCOMPARE(itemsInModel(),
1978 QStringList() << "parent1"
1981 QCOMPARE(itemsRemovedSpy
.count(), 1);
1982 arguments
= itemsRemovedSpy
.takeFirst();
1983 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1984 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1986 // Remove "parent2".
1987 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1988 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1989 QCOMPARE(itemsRemovedSpy
.count(), 1);
1990 arguments
= itemsRemovedSpy
.takeFirst();
1991 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1992 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1994 // Clear filter, verify that no items reappear.
1995 m_model
->setNameFilter(QString());
1996 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1999 void KFileItemModelTest::testNameRoleGroups()
2001 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2002 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
2003 QVERIFY(itemsMovedSpy
.isValid());
2004 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
2005 QVERIFY(groupsChangedSpy
.isValid());
2007 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
2009 m_model
->setGroupedSorting(true);
2010 m_model
->loadDirectory(m_testDir
->url());
2011 QVERIFY(itemsInsertedSpy
.wait());
2012 QCOMPARE(itemsInModel(),
2013 QStringList() << "b.txt"
2018 QList
<QPair
<int, QVariant
>> expectedGroups
;
2019 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
2020 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
2021 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
2022 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2023 QCOMPARE(m_model
->groups(), expectedGroups
);
2025 // Rename d.txt to a.txt.
2026 QHash
<QByteArray
, QVariant
> data
;
2027 data
.insert("text", "a.txt");
2028 m_model
->setData(2, data
);
2029 QVERIFY(itemsMovedSpy
.wait());
2030 QCOMPARE(itemsInModel(),
2031 QStringList() << "a.txt"
2036 expectedGroups
.clear();
2037 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2038 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
2039 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
2040 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2041 QCOMPARE(m_model
->groups(), expectedGroups
);
2043 // Rename c.txt to d.txt.
2044 data
.insert("text", "d.txt");
2045 m_model
->setData(2, data
);
2046 QVERIFY(groupsChangedSpy
.wait());
2047 QCOMPARE(itemsInModel(),
2048 QStringList() << "a.txt"
2053 expectedGroups
.clear();
2054 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2055 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
2056 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
2057 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2058 QCOMPARE(m_model
->groups(), expectedGroups
);
2060 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
2061 const KFileItem fileItemD
= m_model
->fileItem(2);
2062 KFileItem fileItemC
= fileItemD
;
2063 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
2064 urlC
.setPath(urlC
.path() + "c.txt");
2065 fileItemC
.setUrl(urlC
);
2067 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
2068 QVERIFY(groupsChangedSpy
.wait());
2069 QCOMPARE(itemsInModel(),
2070 QStringList() << "a.txt"
2075 expectedGroups
.clear();
2076 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2077 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
2078 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
2079 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2080 QCOMPARE(m_model
->groups(), expectedGroups
);
2083 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
2085 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2087 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2088 modelRoles
<< "isExpanded"
2090 << "expandedParentsCount";
2091 m_model
->setRoles(modelRoles
);
2093 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
2095 m_model
->setGroupedSorting(true);
2096 m_model
->loadDirectory(m_testDir
->url());
2097 QVERIFY(itemsInsertedSpy
.wait());
2098 QCOMPARE(itemsInModel(),
2099 QStringList() << "a"
2102 QList
<QPair
<int, QVariant
>> expectedGroups
;
2103 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2104 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
2105 QCOMPARE(m_model
->groups(), expectedGroups
);
2107 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
2108 expectedGroups
.clear();
2109 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2110 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
2112 m_model
->setExpanded(0, true);
2113 QVERIFY(m_model
->isExpanded(0));
2114 QVERIFY(itemsInsertedSpy
.wait());
2115 QCOMPARE(itemsInModel(),
2116 QStringList() << "a"
2120 QCOMPARE(m_model
->groups(), expectedGroups
);
2122 m_model
->setExpanded(3, true);
2123 QVERIFY(m_model
->isExpanded(3));
2124 QVERIFY(itemsInsertedSpy
.wait());
2125 QCOMPARE(itemsInModel(),
2126 QStringList() << "a"
2132 QCOMPARE(m_model
->groups(), expectedGroups
);
2135 void KFileItemModelTest::testInconsistentModel()
2137 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2139 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2140 modelRoles
<< "isExpanded"
2142 << "expandedParentsCount";
2143 m_model
->setRoles(modelRoles
);
2145 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
2147 m_model
->loadDirectory(m_testDir
->url());
2148 QVERIFY(itemsInsertedSpy
.wait());
2149 QCOMPARE(itemsInModel(), QStringList() << "a");
2151 // Expand "a/" and "a/b/".
2152 m_model
->setExpanded(0, true);
2153 QVERIFY(m_model
->isExpanded(0));
2154 QVERIFY(itemsInsertedSpy
.wait());
2155 QCOMPARE(itemsInModel(),
2156 QStringList() << "a"
2159 m_model
->setExpanded(1, true);
2160 QVERIFY(m_model
->isExpanded(1));
2161 QVERIFY(itemsInsertedSpy
.wait());
2162 QCOMPARE(itemsInModel(),
2163 QStringList() << "a"
2168 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
2169 // Such a thing can in principle happen when performing a search, and there
2171 // (a) match the search string, and
2172 // (b) are children of a folder that matches the search string and is expanded.
2174 // Note that the first item in the list of added items must be new (i.e., not
2175 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
2176 // it receives items that are in the model already and ignore them.
2177 QUrl
url(m_model
->directory().url() + "/a2");
2178 KFileItem
newItem(url
);
2180 KFileItemList items
;
2181 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
2182 m_model
->slotItemsAdded(m_model
->directory(), items
);
2183 m_model
->slotCompleted();
2184 QCOMPARE(itemsInModel(),
2185 QStringList() << "a"
2193 m_model
->setExpanded(0, false);
2195 // Test that the right items have been removed, see
2196 // https://bugs.kde.org/show_bug.cgi?id=324371
2197 QCOMPARE(itemsInModel(),
2198 QStringList() << "a"
2203 // Test that resorting does not cause a crash, see
2204 // https://bugs.kde.org/show_bug.cgi?id=325359
2205 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
2206 m_model
->resortAllItems();
2209 void KFileItemModelTest::testChangeRolesForFilteredItems()
2211 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2213 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2214 modelRoles
<< "owner";
2215 m_model
->setRoles(modelRoles
);
2217 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
2219 m_model
->loadDirectory(m_testDir
->url());
2220 QVERIFY(itemsInsertedSpy
.wait());
2221 QCOMPARE(itemsInModel(),
2222 QStringList() << "a.txt"
2226 for (int index
= 0; index
< m_model
->count(); ++index
) {
2227 // All items should have the "text" and "owner" roles, but not "group".
2228 QVERIFY(m_model
->data(index
).contains("text"));
2229 QVERIFY(m_model
->data(index
).contains("owner"));
2230 QVERIFY(!m_model
->data(index
).contains("group"));
2233 // Add a filter, such that only "aaa.txt" remains in the model.
2234 m_model
->setNameFilter("aaa");
2235 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
2237 // Add the "group" role.
2238 modelRoles
<< "group";
2239 m_model
->setRoles(modelRoles
);
2241 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
2242 m_model
->setNameFilter("aa");
2243 QCOMPARE(itemsInModel(),
2244 QStringList() << "aa.txt"
2247 for (int index
= 0; index
< m_model
->count(); ++index
) {
2248 // All items should have the "text", "owner", and "group" roles.
2249 QVERIFY(m_model
->data(index
).contains("text"));
2250 QVERIFY(m_model
->data(index
).contains("owner"));
2251 QVERIFY(m_model
->data(index
).contains("group"));
2254 // Remove the "owner" role.
2255 modelRoles
.remove("owner");
2256 m_model
->setRoles(modelRoles
);
2258 // Clear the filter, and verify that all items have the expected roles
2259 m_model
->setNameFilter(QString());
2260 QCOMPARE(itemsInModel(),
2261 QStringList() << "a.txt"
2265 for (int index
= 0; index
< m_model
->count(); ++index
) {
2266 // All items should have the "text" and "group" roles, but now "owner".
2267 QVERIFY(m_model
->data(index
).contains("text"));
2268 QVERIFY(!m_model
->data(index
).contains("owner"));
2269 QVERIFY(m_model
->data(index
).contains("group"));
2273 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
2275 KFileItemList items
;
2277 KIO::UDSEntry entry
[3];
2279 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
2280 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
2282 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
2283 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
2285 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
2286 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
2288 for (int i
= 0; i
< 3; ++i
) {
2289 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
2290 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
2291 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
2292 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
2293 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
2294 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
2295 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
2298 m_model
->slotItemsAdded(m_testDir
->url(), items
);
2299 m_model
->slotCompleted();
2301 QCOMPARE(itemsInModel(),
2302 QStringList() << "a.txt"
2307 m_model
->setNameFilter("a");
2308 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
2311 m_model
->setSortRole("owner");
2313 // Clear the filter, and verify that the items are sorted correctly.
2314 m_model
->setNameFilter(QString());
2315 QCOMPARE(itemsInModel(),
2316 QStringList() << "c.txt"
2321 void KFileItemModelTest::testRefreshFilteredItems()
2323 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2325 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
2327 m_model
->loadDirectory(m_testDir
->url());
2328 QVERIFY(itemsInsertedSpy
.wait());
2329 QCOMPARE(itemsInModel(),
2330 QStringList() << "a.txt"
2335 const KFileItem fileItemC
= m_model
->fileItem(2);
2337 // Show only the .txt files.
2338 m_model
->setNameFilter(".txt");
2339 QCOMPARE(itemsInModel(),
2340 QStringList() << "a.txt"
2343 // Rename one of the .jpg files.
2344 KFileItem fileItemE
= fileItemC
;
2345 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
2346 urlE
.setPath(urlE
.path() + "/e.jpg");
2347 fileItemE
.setUrl(urlE
);
2349 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
2351 // Show all files again, and verify that the model has updated the file name.
2352 m_model
->setNameFilter(QString());
2353 QCOMPARE(itemsInModel(),
2354 QStringList() << "a.txt"
2360 void KFileItemModelTest::testCreateMimeData()
2362 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2364 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2365 modelRoles
<< "isExpanded"
2367 << "expandedParentsCount";
2368 m_model
->setRoles(modelRoles
);
2370 m_testDir
->createFile("a/1");
2372 m_model
->loadDirectory(m_testDir
->url());
2373 QVERIFY(itemsInsertedSpy
.wait());
2374 QCOMPARE(itemsInModel(), QStringList() << "a");
2377 m_model
->setExpanded(0, true);
2378 QVERIFY(itemsInsertedSpy
.wait());
2379 QCOMPARE(itemsInModel(),
2380 QStringList() << "a"
2383 // Verify that creating the MIME data for a child of an expanded folder does
2384 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
2386 selection
.insert(1);
2387 QMimeData
*mimeData
= m_model
->createMimeData(selection
);
2391 void KFileItemModelTest::testCollapseFolderWhileLoading()
2393 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2395 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2396 modelRoles
<< "isExpanded"
2398 << "expandedParentsCount";
2399 m_model
->setRoles(modelRoles
);
2401 m_testDir
->createFile("a2/b/c1.txt");
2403 m_model
->loadDirectory(m_testDir
->url());
2404 QVERIFY(itemsInsertedSpy
.wait());
2405 QCOMPARE(itemsInModel(), QStringList() << "a2");
2408 m_model
->setExpanded(0, true);
2409 QVERIFY(m_model
->isExpanded(0));
2410 QVERIFY(itemsInsertedSpy
.wait());
2411 QCOMPARE(itemsInModel(),
2412 QStringList() << "a2"
2416 m_model
->setExpanded(1, true);
2417 QVERIFY(m_model
->isExpanded(1));
2418 QVERIFY(itemsInsertedSpy
.wait());
2419 QCOMPARE(itemsInModel(),
2420 QStringList() << "a2"
2424 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
2425 // signal is not emitted yet.
2426 const KFileItem fileItemC1
= m_model
->fileItem(2);
2427 KFileItem fileItemC2
= fileItemC1
;
2428 QUrl urlC2
= fileItemC2
.url();
2429 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
2430 urlC2
.setPath(urlC2
.path() + "c2.txt");
2431 fileItemC2
.setUrl(urlC2
);
2433 const QUrl urlB
= m_model
->fileItem(1).url();
2434 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
2435 QCOMPARE(itemsInModel(),
2436 QStringList() << "a2"
2440 // Collapse "a2/". This should also remove all its (indirect) children from
2441 // the model and from the model's m_pendingItemsToInsert member.
2442 m_model
->setExpanded(0, false);
2443 QCOMPARE(itemsInModel(), QStringList() << "a2");
2445 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
2446 // is still in m_pendingItemsToInsert, then we might get a crash, see
2447 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
2448 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
2449 // without parent in the model.
2450 m_model
->slotCompleted();
2451 QCOMPARE(itemsInModel(), QStringList() << "a2");
2453 // Expand "a2/" again.
2454 m_model
->setExpanded(0, true);
2455 QVERIFY(m_model
->isExpanded(0));
2456 QVERIFY(itemsInsertedSpy
.wait());
2457 QCOMPARE(itemsInModel(),
2458 QStringList() << "a2"
2461 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
2462 // completed() signal is not emitted yet.
2463 const KFileItem fileItemA2
= m_model
->fileItem(0);
2464 KFileItem fileItemA1
= fileItemA2
;
2465 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
2466 urlA1
.setPath(urlA1
.path() + "a1");
2467 fileItemA1
.setUrl(urlA1
);
2469 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
2470 QCOMPARE(itemsInModel(),
2471 QStringList() << "a2"
2474 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
2475 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
2476 // confuse the code which collapses the folder.
2477 m_model
->setExpanded(0, false);
2478 QCOMPARE(itemsInModel(),
2479 QStringList() << "a1"
2481 QVERIFY(!m_model
->isExpanded(0));
2482 QVERIFY(!m_model
->isExpanded(1));
2485 void KFileItemModelTest::testDeleteFileMoreThanOnce()
2487 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2489 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
2491 m_model
->loadDirectory(m_testDir
->url());
2492 QVERIFY(itemsInsertedSpy
.wait());
2493 QCOMPARE(itemsInModel(),
2494 QStringList() << "a.txt"
2499 const KFileItem fileItemB
= m_model
->fileItem(1);
2501 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
2503 list
<< fileItemB
<< fileItemB
;
2504 m_model
->slotItemsDeleted(list
);
2506 QVERIFY(m_model
->isConsistent());
2507 QCOMPARE(itemsInModel(),
2508 QStringList() << "a.txt"
2513 void KFileItemModelTest::testInsertAfterExpand()
2515 m_model
->m_dirLister
->setAutoUpdate(true);
2517 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2518 QVERIFY(itemsInsertedSpy
.isValid());
2519 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
2520 QVERIFY(itemsRemovedSpy
.isValid());
2521 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2522 QVERIFY(loadingCompletedSpy
.isValid());
2524 // Test expanding subfolders in a folder with the items "a/", "a/a/"
2525 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
2526 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
2527 modelRoles
<< "isExpanded"
2529 << "expandedParentsCount";
2530 m_model
->setRoles(modelRoles
);
2532 m_testDir
->createFile("a/b/1");
2534 m_model
->loadDirectory(m_testDir
->url());
2535 QVERIFY(itemsInsertedSpy
.wait());
2537 // So far, the model contains only "a/"
2538 QCOMPARE(m_model
->count(), 1);
2539 QVERIFY(m_model
->isExpandable(0));
2540 QVERIFY(!m_model
->isExpanded(0));
2541 QVERIFY(m_model
->expandedDirectories().empty());
2543 QCOMPARE(itemsInsertedSpy
.count(), 1);
2545 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2546 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
2547 QCOMPARE(m_model
->expandedParentsCount(0), 0);
2549 itemsInsertedSpy
.clear();
2551 // Expand the folder "a/" -> "a/b" become visible
2552 m_model
->setExpanded(0, true);
2553 QVERIFY(m_model
->isExpanded(0));
2554 QVERIFY(itemsInsertedSpy
.wait());
2555 QCOMPARE(m_model
->count(), 2); // 3 items: "a/", "a/a/"
2556 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>({QUrl::fromLocalFile(m_testDir
->path() + "/a")}));
2558 QCOMPARE(itemsInsertedSpy
.count(), 1);
2560 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2561 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1)); // 1 new item "a/b" with index 1
2562 QCOMPARE(m_model
->expandedParentsCount(1), 1);
2564 itemsInsertedSpy
.clear();
2566 // Expand "a/b" -> "a/b/1" becomes visible
2567 m_model
->setExpanded(1, true);
2568 QVERIFY(itemsInsertedSpy
.wait());
2569 QCOMPARE(m_model
->expandedDirectories(), QSet({QUrl::fromLocalFile(m_testDir
->path() + "/a"), QUrl::fromLocalFile(m_testDir
->path() + "/a/b")}));
2571 QCOMPARE(itemsInsertedSpy
.count(), 1);
2573 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2574 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/b/1" with index 2
2575 QCOMPARE(m_model
->expandedParentsCount(2), 2);
2577 itemsInsertedSpy
.clear();
2579 // Collapse "a" whilst leaving "b" expanded
2580 m_model
->setExpanded(0, false);
2582 // Insert additional files into "a/b/"
2583 m_testDir
->createFile("a/b/2");
2585 QVERIFY(!itemsInsertedSpy
.wait(5000));
2587 QCOMPARE(itemsInModel(), {"a"});
2589 m_model
->setExpanded(0, true);
2591 QTRY_COMPARE(itemsInModel(), QStringList({"a", "b", "1", "2"}));
2593 QCOMPARE(m_model
->expandedParentsCount(0), 0); // a
2594 QCOMPARE(m_model
->expandedParentsCount(1), 1); // a/b
2595 QCOMPARE(m_model
->expandedParentsCount(2), 2); // a/b/1
2596 QCOMPARE(m_model
->expandedParentsCount(3), 2); // a/b/2
2599 void KFileItemModelTest::testCurrentDirRemoved()
2601 m_model
->m_dirLister
->setAutoUpdate(true);
2602 QSignalSpy
currentDirectoryRemovedSpy(m_model
, &KFileItemModel::currentDirectoryRemoved
);
2603 QVERIFY(currentDirectoryRemovedSpy
.isValid());
2604 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2605 QVERIFY(loadingCompletedSpy
.isValid());
2606 QSignalSpy
dirListerClearSpy(m_model
->m_dirLister
, &KCoreDirLister::clear
);
2607 QVERIFY(dirListerClearSpy
.isValid());
2609 m_testDir
->createFiles({"dir/a.txt", "dir/b.txt"});
2610 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/dir/"));
2611 QVERIFY(loadingCompletedSpy
.wait());
2612 QCOMPARE(m_model
->count(), 2);
2613 QVERIFY(m_model
->isConsistent());
2615 m_testDir
->removeDir("dir");
2616 QVERIFY(currentDirectoryRemovedSpy
.wait());
2618 // dirLister calls clear
2619 QCOMPARE(dirListerClearSpy
.count(), 2);
2620 QVERIFY(m_model
->isConsistent());
2621 QVERIFY(m_model
->m_itemData
.isEmpty());
2622 QCOMPARE(m_model
->count(), 0);
2625 QStringList
KFileItemModelTest::itemsInModel() const
2628 for (int i
= 0; i
< m_model
->count(); i
++) {
2629 items
<< m_model
->fileItem(i
).text();
2634 QTEST_MAIN(KFileItemModelTest
)
2636 #include "kfileitemmodeltest.moc"