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>
17 #include <kio_version.h>
19 #include "kitemviews/kfileitemmodel.h"
22 void myMessageOutput(QtMsgType type
, const QMessageLogContext
&context
, const QString
&msg
)
32 fprintf(stderr
, "Critical: %s\n", msg
.toLocal8Bit().data());
35 fprintf(stderr
, "Fatal: %s\n", msg
.toLocal8Bit().data());
42 Q_DECLARE_METATYPE(KItemRange
)
43 Q_DECLARE_METATYPE(KItemRangeList
)
44 Q_DECLARE_METATYPE(QList
<int>)
46 class KFileItemModelTest
: public QObject
55 void testDefaultRoles();
56 void testDefaultSortRole();
57 void testDefaultGroupedSorting();
59 void testRemoveItems();
60 void testDirLoadingCompleted();
62 void testSetDataWithModifiedSortRole_data();
63 void testSetDataWithModifiedSortRole();
64 void testChangeSortRole();
65 void testResortAfterChangingName();
66 void testModelConsistencyWhenInsertingItems();
67 void testItemRangeConsistencyWhenInsertingItems();
68 void testExpandItems();
69 void testExpandParentItems();
70 void testMakeExpandedItemHidden();
71 void testRemoveFilteredExpandedItems();
73 void testIndexForKeyboardSearch();
74 void testNameFilter();
76 void testRefreshExpandedItem();
77 void testAddItemToFilteredExpandedFolder();
78 void testDeleteItemsWithExpandedFolderWithFilter();
79 void testRefreshItemsWithFilter();
80 void testRefreshExpandedFolderWithFilter();
81 void testRemoveHiddenItems();
82 void collapseParentOfHiddenItems();
83 void removeParentOfHiddenItems();
84 void testGeneralParentChildRelationships();
85 void testNameRoleGroups();
86 void testNameRoleGroupsWithExpandedItems();
87 void testInconsistentModel();
88 void testChangeRolesForFilteredItems();
89 void testChangeSortRoleWhileFiltering();
90 void testRefreshFilteredItems();
91 void testCollapseFolderWhileLoading();
92 void testCreateMimeData();
93 void testDeleteFileMoreThanOnce();
94 void testInsertAfterExpand();
95 void testCurrentDirRemoved();
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::testSorting()
852 // testDir structure is as follows
859 // │ │ ├─ c-3
865 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
866 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
867 QVERIFY(itemsMovedSpy
.isValid());
869 // Create some files with different sizes and modification times to check the different sorting options
870 QDateTime now
= QDateTime::currentDateTime();
872 QSet
<QByteArray
> roles
;
873 roles
.insert("text");
874 roles
.insert("isExpanded");
875 roles
.insert("isExpandable");
876 roles
.insert("expandedParentsCount");
877 m_model
->setRoles(roles
);
879 m_testDir
->createDir("c/c-2");
880 m_testDir
->createFile("c/c-2/c-3");
881 m_testDir
->createFile("c/c-1");
883 m_testDir
->createFile("a", "A file", now
.addDays(-3));
884 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
885 m_testDir
->createDir("c", now
.addDays(-2));
886 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
887 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
888 m_testDir
->createFile(".f");
889 m_testDir
->createDir(".g");
891 m_model
->loadDirectory(m_testDir
->url());
892 QVERIFY(itemsInsertedSpy
.wait());
893 QCOMPARE(itemsInsertedSpy
.count(), 1);
894 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
895 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 5));
897 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
898 m_model
->setExpanded(index
, true);
899 QVERIFY(itemsInsertedSpy
.wait());
900 QCOMPARE(itemsInsertedSpy
.count(), 1);
901 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
902 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
904 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
905 m_model
->setExpanded(index
, true);
906 QVERIFY(itemsInsertedSpy
.wait());
907 QCOMPARE(itemsInsertedSpy
.count(), 1);
908 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
909 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1));
911 // Default: Sort by Name, ascending
912 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
913 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
914 QVERIFY(m_model
->sortDirectoriesFirst());
915 QVERIFY(!m_model
->showHiddenFiles());
916 QCOMPARE(itemsInModel(),
926 // Sort by Name, ascending, 'Sort Folders First' disabled
927 m_model
->setSortDirectoriesFirst(false);
928 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
929 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
930 QCOMPARE(itemsInModel(),
939 QCOMPARE(itemsMovedSpy
.count(), 1);
940 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
941 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
943 // Sort by Name, descending
944 m_model
->setSortDirectoriesFirst(true);
945 m_model
->setSortOrder(Qt::DescendingOrder
);
946 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
947 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
948 QCOMPARE(itemsInModel(),
957 QCOMPARE(itemsMovedSpy
.count(), 2);
958 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
959 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
960 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
961 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
963 // Sort by Date, descending
964 m_model
->setSortDirectoriesFirst(true);
965 m_model
->setSortRole("modificationtime");
966 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
967 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
968 QCOMPARE(itemsInModel(),
977 QCOMPARE(itemsMovedSpy
.count(), 1);
978 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
979 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 5 << 4 << 6);
981 // Sort by Date, ascending
982 m_model
->setSortOrder(Qt::AscendingOrder
);
983 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
984 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
985 QCOMPARE(itemsInModel(),
994 QCOMPARE(itemsMovedSpy
.count(), 1);
995 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
996 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
998 // Sort by Date, ascending, 'Sort Folders First' disabled
999 m_model
->setSortDirectoriesFirst(false);
1000 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1001 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1002 QVERIFY(!m_model
->sortDirectoriesFirst());
1003 QCOMPARE(itemsInModel(),
1004 QStringList() << "e"
1012 QCOMPARE(itemsMovedSpy
.count(), 1);
1013 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1014 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
1016 // Sort by Name, ascending, 'Sort Folders First' disabled
1017 m_model
->setSortRole("text");
1018 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1019 QVERIFY(!m_model
->sortDirectoriesFirst());
1020 QCOMPARE(itemsInModel(),
1021 QStringList() << "a"
1029 QCOMPARE(itemsMovedSpy
.count(), 1);
1030 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1031 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
1033 // Sort by Size, ascending, 'Sort Folders First' disabled
1034 m_model
->setSortRole("size");
1035 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1036 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1037 QVERIFY(!m_model
->sortDirectoriesFirst());
1038 QCOMPARE(itemsInModel(),
1039 QStringList() << "c"
1047 QCOMPARE(itemsMovedSpy
.count(), 1);
1048 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1049 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
1051 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
1052 m_model
->setSortDirectoriesFirst(true);
1053 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1054 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1055 QVERIFY(m_model
->sortDirectoriesFirst());
1056 QCOMPARE(itemsInModel(),
1057 QStringList() << "c"
1065 QCOMPARE(itemsMovedSpy
.count(), 0);
1067 // Sort by Size, descending, 'Sort Folders First' enabled
1068 m_model
->setSortOrder(Qt::DescendingOrder
);
1069 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1070 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1071 QVERIFY(m_model
->sortDirectoriesFirst());
1072 QCOMPARE(itemsInModel(),
1073 QStringList() << "c"
1081 QCOMPARE(itemsMovedSpy
.count(), 1);
1082 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1083 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1085 // 'Show Hidden Files' enabled
1086 m_model
->setShowHiddenFiles(true);
1087 QVERIFY(m_model
->showHiddenFiles());
1088 QVERIFY(!m_model
->sortHiddenLast());
1089 QCOMPARE(itemsInModel(),
1090 QStringList() << "c"
1100 QCOMPARE(itemsMovedSpy
.count(), 0);
1101 QCOMPARE(itemsInsertedSpy
.count(), 1);
1102 QCOMPARE(itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
1104 // 'Sort Hidden Files Last' enabled
1105 m_model
->setSortHiddenLast(true);
1106 QVERIFY(m_model
->sortHiddenLast());
1107 QCOMPARE(itemsInModel(),
1108 QStringList() << "c"
1118 QCOMPARE(itemsMovedSpy
.count(), 1);
1119 QCOMPARE(itemsInsertedSpy
.count(), 0);
1120 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 5));
1121 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 8 << 4 << 5 << 6 << 7);
1124 m_model
->setSortRole("text");
1125 QCOMPARE(itemsInModel(),
1126 QStringList() << "c"
1136 QCOMPARE(itemsMovedSpy
.count(), 1);
1137 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 2));
1138 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 5 << 4);
1141 m_model
->setSortOrder(Qt::AscendingOrder
);
1142 QCOMPARE(itemsInModel(),
1143 QStringList() << "c"
1153 QCOMPARE(itemsMovedSpy
.count(), 1);
1154 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1155 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1157 // 'Sort Folders First' disabled
1158 m_model
->setSortDirectoriesFirst(false);
1159 QVERIFY(!m_model
->sortDirectoriesFirst());
1160 QCOMPARE(itemsInModel(),
1161 QStringList() << "a"
1171 QCOMPARE(itemsMovedSpy
.count(), 1);
1172 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 10));
1173 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1176 void KFileItemModelTest::testIndexForKeyboardSearch()
1178 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1180 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1182 m_model
->loadDirectory(m_testDir
->url());
1183 QVERIFY(itemsInsertedSpy
.wait());
1185 // Search from index 0
1186 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
1187 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
1188 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
1189 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
1190 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
1191 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
1192 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
1193 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
1194 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
1195 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
1196 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
1198 // Start a search somewhere in the middle
1199 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
1200 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
1201 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
1202 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
1204 // Test searches that go past the last item back to index 0
1205 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
1206 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
1207 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
1208 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
1210 // Test searches that yield no result
1211 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
1212 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
1213 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
1214 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
1215 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
1217 // Test upper case searches (note that search is case insensitive)
1218 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
1219 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
1220 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
1221 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1223 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1226 void KFileItemModelTest::testNameFilter()
1228 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1230 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1232 m_model
->loadDirectory(m_testDir
->url());
1233 QVERIFY(itemsInsertedSpy
.wait());
1235 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1236 QCOMPARE(m_model
->count(), 3);
1238 m_model
->setNameFilter("A2"); // Shows only A2
1239 QCOMPARE(m_model
->count(), 1);
1241 m_model
->setNameFilter("A2"); // Shows only A1
1242 QCOMPARE(m_model
->count(), 1);
1244 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1245 QCOMPARE(m_model
->count(), 2);
1247 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1248 QCOMPARE(m_model
->count(), 2);
1250 m_model
->setNameFilter(QString()); // Shows again all items
1251 QCOMPARE(m_model
->count(), 5);
1255 * Verifies that we do not crash when adding a KFileItem with an empty path.
1256 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1257 * tried to always read the first character of the path, even if the path is empty.
1259 void KFileItemModelTest::testEmptyPath()
1261 QSet
<QByteArray
> roles
;
1262 roles
.insert("text");
1263 roles
.insert("isExpanded");
1264 roles
.insert("isExpandable");
1265 roles
.insert("expandedParentsCount");
1266 m_model
->setRoles(roles
);
1268 const QUrl emptyUrl
;
1269 QVERIFY(emptyUrl
.path().isEmpty());
1271 const QUrl
url("file:///test/");
1273 KFileItemList items
;
1274 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1275 m_model
->slotItemsAdded(emptyUrl
, items
);
1276 m_model
->slotCompleted();
1280 * Verifies that the 'isExpanded' state of folders does not change when the
1281 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1283 void KFileItemModelTest::testRefreshExpandedItem()
1285 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1286 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1287 QVERIFY(itemsChangedSpy
.isValid());
1289 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1290 modelRoles
<< "isExpanded"
1292 << "expandedParentsCount";
1293 m_model
->setRoles(modelRoles
);
1295 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1297 m_model
->loadDirectory(m_testDir
->url());
1298 QVERIFY(itemsInsertedSpy
.wait());
1299 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1301 m_model
->setExpanded(0, true);
1302 QVERIFY(itemsInsertedSpy
.wait());
1303 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1304 QVERIFY(m_model
->isExpanded(0));
1306 const KFileItem item
= m_model
->fileItem(0);
1307 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1308 QVERIFY(!itemsChangedSpy
.isEmpty());
1310 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1311 QVERIFY(m_model
->isExpanded(0));
1315 * Verifies that adding an item to an expanded folder that's filtered makes the parental chain visible.
1317 void KFileItemModelTest::testAddItemToFilteredExpandedFolder()
1319 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1320 QSignalSpy
fileItemsChangedSpy(m_model
, &KFileItemModel::fileItemsChanged
);
1322 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1323 modelRoles
<< "isExpanded"
1325 << "expandedParentsCount";
1326 m_model
->setRoles(modelRoles
);
1328 m_testDir
->createFile("a/b/file");
1330 m_model
->loadDirectory(m_testDir
->url());
1331 QVERIFY(itemsInsertedSpy
.wait());
1332 QCOMPARE(m_model
->count(), 1); // "a
1335 m_model
->setExpanded(0, true);
1336 QVERIFY(itemsInsertedSpy
.wait());
1339 m_model
->setExpanded(1, true);
1340 QVERIFY(itemsInsertedSpy
.wait());
1342 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1344 const QUrl urlB
= m_model
->fileItem(1).url();
1346 // Set a filter that matches ".txt" extension
1347 m_model
->setNameFilter("*.txt");
1348 QCOMPARE(m_model
->count(), 0); // Everything got hidden since we don't have a .txt file yet
1350 m_model
->slotItemsAdded(urlB
, KFileItemList() << KFileItem(QUrl("a/b/newItem.txt")));
1351 m_model
->slotCompleted();
1353 // Entire parental chain should now be shown
1354 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/newItem.txt"
1355 QCOMPARE(itemsInModel(),
1356 QStringList() << "a"
1360 // Items should be indented in hierarchy
1361 QCOMPARE(m_model
->expandedParentsCount(0), 0);
1362 QCOMPARE(m_model
->expandedParentsCount(1), 1);
1363 QCOMPARE(m_model
->expandedParentsCount(2), 2);
1367 * Verifies that deleting the last filter-passing child from expanded folders
1368 * makes the parental chain hidden.
1370 void KFileItemModelTest::testDeleteItemsWithExpandedFolderWithFilter()
1372 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1373 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1375 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1376 modelRoles
<< "isExpanded"
1378 << "expandedParentsCount";
1379 m_model
->setRoles(modelRoles
);
1381 m_testDir
->createFile("a/b/file");
1383 m_model
->loadDirectory(m_testDir
->url());
1384 QVERIFY(itemsInsertedSpy
.wait());
1385 QCOMPARE(m_model
->count(), 1); // "a
1388 m_model
->setExpanded(0, true);
1389 QVERIFY(itemsInsertedSpy
.wait());
1392 m_model
->setExpanded(1, true);
1393 QVERIFY(itemsInsertedSpy
.wait());
1395 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1397 // Set a filter that matches "file" extension
1398 m_model
->setNameFilter("file");
1399 QCOMPARE(m_model
->count(), 3); // Everything is still shown
1402 QCOMPARE(itemsRemovedSpy
.count(), 0);
1403 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(2));
1404 QCOMPARE(itemsRemovedSpy
.count(), 1);
1406 // Entire parental chain should now be filtered
1407 QCOMPARE(m_model
->count(), 0);
1408 QCOMPARE(m_model
->m_filteredItems
.size(), 2);
1412 * Verifies that the fileItemsChanged signal is raised with the correct index after renaming files with filter set.
1413 * The rename operation will cause one item to be filtered out and another item to be reordered.
1415 void KFileItemModelTest::testRefreshItemsWithFilter()
1417 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1418 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1419 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1420 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1422 // Creates three .txt files
1423 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt"});
1425 m_model
->loadDirectory(m_testDir
->url());
1426 QVERIFY(itemsInsertedSpy
.wait());
1428 QCOMPARE(m_model
->count(), 3); // "b.txt", "c.txt", "d.txt"
1430 // Set a filter that matches ".txt" extension
1431 m_model
->setNameFilter("*.txt");
1432 QCOMPARE(m_model
->count(), 3); // Still all items are shown
1433 QCOMPARE(itemsInModel(),
1434 QStringList() << "b.txt"
1438 // Objects used to rename
1439 const KFileItem fileItemC_txt
= m_model
->fileItem(1);
1440 KFileItem fileItemC_cfg
= fileItemC_txt
;
1441 fileItemC_cfg
.setUrl(QUrl("c.cfg"));
1443 const KFileItem fileItemD_txt
= m_model
->fileItem(2);
1444 KFileItem fileItemA_txt
= fileItemD_txt
;
1445 fileItemA_txt
.setUrl(QUrl("a.txt"));
1447 // Rename "c.txt" to "c.cfg"; and rename "d.txt" to "a.txt"
1448 QCOMPARE(itemsRemovedSpy
.count(), 0);
1449 QCOMPARE(itemsChangedSpy
.count(), 0);
1450 m_model
->slotRefreshItems({qMakePair(fileItemC_txt
, fileItemC_cfg
), qMakePair(fileItemD_txt
, fileItemA_txt
)});
1451 QCOMPARE(itemsRemovedSpy
.count(), 1);
1452 QCOMPARE(itemsChangedSpy
.count(), 1);
1453 QCOMPARE(m_model
->count(), 2); // Only "a.txt" and "b.txt". "c.cfg" got filtered out
1455 QList
<QVariant
> arguments
= itemsChangedSpy
.takeLast();
1456 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1458 // We started with the order "b.txt", "c.txt", "d.txt"
1459 // "d.txt" started with index "2"
1460 // "c.txt" got renamed and got filtered out
1461 // "d.txt" index shifted from index "2" to "1"
1462 // So we expect index "1" in this argument, meaning "d.txt" was renamed
1463 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1465 // Re-sorting is done asynchronously:
1466 QCOMPARE(itemsInModel(),
1467 QStringList() << "b.txt"
1468 << "a.txt"); // Files should still be in the incorrect order
1469 QVERIFY(itemsMovedSpy
.wait());
1470 QCOMPARE(itemsInModel(),
1471 QStringList() << "a.txt"
1472 << "b.txt"); // Files were re-sorted and should now be in the correct order
1476 * Verifies that parental chains are hidden and shown as needed while their children get filtered/unfiltered due to renaming.
1477 * Also verifies that the "isExpanded" and "expandedParentsCount" values are kept for expanded folders that get refreshed.
1479 void KFileItemModelTest::testRefreshExpandedFolderWithFilter()
1481 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1482 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1484 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1485 modelRoles
<< "isExpanded"
1487 << "expandedParentsCount";
1488 m_model
->setRoles(modelRoles
);
1490 m_testDir
->createFile("a/b/someFolder/someFile");
1492 m_model
->loadDirectory(m_testDir
->url());
1493 QVERIFY(itemsInsertedSpy
.wait());
1495 QCOMPARE(m_model
->count(), 1); // Only "a/"
1498 m_model
->setExpanded(0, true);
1499 QVERIFY(itemsInsertedSpy
.wait());
1502 m_model
->setExpanded(1, true);
1503 QVERIFY(itemsInsertedSpy
.wait());
1505 // Expand "a/b/someFolder/".
1506 m_model
->setExpanded(2, true);
1507 QVERIFY(itemsInsertedSpy
.wait());
1508 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/b/", "a/b/someFolder", "a/b/someFolder/someFile"
1510 // Set a filter that matches the expanded folder "someFolder"
1511 m_model
->setNameFilter("someFolder");
1512 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/someFolder"
1514 // Objects used to rename
1515 const KFileItem fileItemA
= m_model
->fileItem(0);
1516 KFileItem fileItemARenamed
= fileItemA
;
1517 fileItemARenamed
.setUrl(QUrl("a_renamed"));
1519 const KFileItem fileItemSomeFolder
= m_model
->fileItem(2);
1520 KFileItem fileItemRenamedFolder
= fileItemSomeFolder
;
1521 fileItemRenamedFolder
.setUrl(QUrl("/a_renamed/b/renamedFolder"));
1523 // Rename "a" to "a_renamed"
1524 // This way we test if the algorithm is sane as to NOT hide "a_renamed" since it will have visible children
1525 m_model
->slotRefreshItems({qMakePair(fileItemA
, fileItemARenamed
)});
1526 QCOMPARE(m_model
->count(), 3); // Entire parental chain must still be shown
1527 QCOMPARE(itemsInModel(),
1528 QStringList() << "a_renamed"
1532 // Rename "a_renamed" back to "a"; and "someFolder" to "renamedFolder"
1533 m_model
->slotRefreshItems({qMakePair(fileItemARenamed
, fileItemA
), qMakePair(fileItemSomeFolder
, fileItemRenamedFolder
)});
1534 QCOMPARE(m_model
->count(), 0); // Entire parental chain became hidden
1536 // Rename "renamedFolder" back to "someFolder". Filter is passing again
1537 m_model
->slotRefreshItems({qMakePair(fileItemRenamedFolder
, fileItemSomeFolder
)});
1538 QCOMPARE(m_model
->count(), 3); // Entire parental chain is shown again
1539 QCOMPARE(itemsInModel(),
1540 QStringList() << "a"
1544 // slotRefreshItems() should preserve "isExpanded" and "expandedParentsCount" values explicitly in this case
1545 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("isExpanded").toBool(), true);
1546 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("expandedParentsCount"), 2);
1550 * Verify that removing hidden files and folders from the model does not
1551 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1553 void KFileItemModelTest::testRemoveHiddenItems()
1555 m_testDir
->createDir(".a");
1556 m_testDir
->createDir(".b");
1557 m_testDir
->createDir("c");
1558 m_testDir
->createDir("d");
1559 m_testDir
->createFiles({".f", ".g", "h", "i"});
1561 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1562 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1564 m_model
->setShowHiddenFiles(true);
1565 m_model
->loadDirectory(m_testDir
->url());
1566 QVERIFY(itemsInsertedSpy
.wait());
1567 QCOMPARE(itemsInModel(),
1568 QStringList() << ".a"
1576 QCOMPARE(itemsInsertedSpy
.count(), 1);
1577 QCOMPARE(itemsRemovedSpy
.count(), 0);
1578 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1579 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1581 m_model
->setShowHiddenFiles(false);
1582 QCOMPARE(itemsInModel(),
1583 QStringList() << "c"
1587 QCOMPARE(itemsInsertedSpy
.count(), 0);
1588 QCOMPARE(itemsRemovedSpy
.count(), 1);
1589 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1590 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1592 m_model
->setShowHiddenFiles(true);
1593 QCOMPARE(itemsInModel(),
1594 QStringList() << ".a"
1602 QCOMPARE(itemsInsertedSpy
.count(), 1);
1603 QCOMPARE(itemsRemovedSpy
.count(), 0);
1604 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1605 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1608 QCOMPARE(itemsInModel(), QStringList());
1609 QCOMPARE(itemsInsertedSpy
.count(), 0);
1610 QCOMPARE(itemsRemovedSpy
.count(), 1);
1611 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1612 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1614 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1615 // Verify that this does not make the model crash.
1616 m_model
->setShowHiddenFiles(false);
1620 * Verify that filtered items are removed when their parent is collapsed.
1622 void KFileItemModelTest::collapseParentOfHiddenItems()
1624 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1625 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1627 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1628 modelRoles
<< "isExpanded"
1630 << "expandedParentsCount";
1631 m_model
->setRoles(modelRoles
);
1633 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1635 m_model
->loadDirectory(m_testDir
->url());
1636 QVERIFY(itemsInsertedSpy
.wait());
1637 QCOMPARE(m_model
->count(), 1); // Only "a/"
1640 m_model
->setExpanded(0, true);
1641 QVERIFY(itemsInsertedSpy
.wait());
1642 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1645 m_model
->setExpanded(1, true);
1646 QVERIFY(itemsInsertedSpy
.wait());
1647 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1650 m_model
->setExpanded(2, true);
1651 QVERIFY(itemsInsertedSpy
.wait());
1652 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"
1654 // Set a name filter that matches nothing -> nothing should remain.
1655 m_model
->setNameFilter("xyz");
1656 QCOMPARE(itemsRemovedSpy
.count(), 1);
1657 QCOMPARE(m_model
->count(), 0); //Everything is hidden
1658 QCOMPARE(itemsInModel(), QStringList());
1660 //Filter by the file names. Folder "d" will be hidden since it was collapsed
1661 m_model
->setNameFilter("1");
1662 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1663 QCOMPARE(m_model
->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1665 // Collapse the folder "a/".
1666 m_model
->setExpanded(0, false);
1667 QCOMPARE(itemsRemovedSpy
.count(), 2);
1668 QCOMPARE(m_model
->count(), 1);
1669 QCOMPARE(itemsInModel(), QStringList() << "a");
1671 // Remove the filter -> "a" should still appear (and we should not get a crash).
1672 m_model
->setNameFilter(QString());
1673 QCOMPARE(itemsRemovedSpy
.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1674 QCOMPARE(m_model
->count(), 1);
1675 QCOMPARE(itemsInModel(), QStringList() << "a");
1679 * Verify that filtered items are removed when their parent is deleted.
1681 void KFileItemModelTest::removeParentOfHiddenItems()
1683 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1684 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1686 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1687 modelRoles
<< "isExpanded"
1689 << "expandedParentsCount";
1690 m_model
->setRoles(modelRoles
);
1692 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1694 m_model
->loadDirectory(m_testDir
->url());
1695 QVERIFY(itemsInsertedSpy
.wait());
1696 QCOMPARE(m_model
->count(), 1); // Only "a/"
1699 m_model
->setExpanded(0, true);
1700 QVERIFY(itemsInsertedSpy
.wait());
1701 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1704 m_model
->setExpanded(1, true);
1705 QVERIFY(itemsInsertedSpy
.wait());
1706 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1709 m_model
->setExpanded(2, true);
1710 QVERIFY(itemsInsertedSpy
.wait());
1711 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"
1713 // Set a name filter that matches nothing -> nothing should remain.
1714 m_model
->setNameFilter("xyz");
1715 QCOMPARE(itemsRemovedSpy
.count(), 1);
1716 QCOMPARE(m_model
->count(), 0);
1717 QCOMPARE(itemsInModel(), QStringList());
1719 // Filter by "c". Folder "b" will also be shown because it is its parent.
1720 m_model
->setNameFilter("c");
1721 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1722 QCOMPARE(m_model
->count(), 3);
1723 QCOMPARE(itemsInModel(),
1724 QStringList() << "a"
1728 // Simulate the deletion of the directory "a/b/".
1729 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1730 QCOMPARE(itemsRemovedSpy
.count(), 2);
1731 QCOMPARE(m_model
->count(), 0); // "a" will be filtered out since it doesn't pass the filter and doesn't have visible children
1733 // Remove the filter -> only the file "a/1" should appear.
1734 m_model
->setNameFilter(QString());
1735 QCOMPARE(m_model
->count(), 2);
1736 QCOMPARE(itemsInModel(),
1737 QStringList() << "a"
1742 * Create a tree structure where parent-child relationships can not be
1743 * determined by parsing the URLs, and verify that KFileItemModel
1744 * handles them correctly.
1746 void KFileItemModelTest::testGeneralParentChildRelationships()
1748 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1749 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1751 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1752 modelRoles
<< "isExpanded"
1754 << "expandedParentsCount";
1755 m_model
->setRoles(modelRoles
);
1757 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1759 m_model
->loadDirectory(m_testDir
->url());
1760 QVERIFY(itemsInsertedSpy
.wait());
1761 QCOMPARE(itemsInModel(),
1762 QStringList() << "parent1"
1765 // Expand all folders.
1766 m_model
->setExpanded(0, true);
1767 QVERIFY(itemsInsertedSpy
.wait());
1768 QCOMPARE(itemsInModel(),
1769 QStringList() << "parent1"
1773 m_model
->setExpanded(1, true);
1774 QVERIFY(itemsInsertedSpy
.wait());
1775 QCOMPARE(itemsInModel(),
1776 QStringList() << "parent1"
1778 << "realGrandChild1"
1781 m_model
->setExpanded(3, true);
1782 QVERIFY(itemsInsertedSpy
.wait());
1783 QCOMPARE(itemsInModel(),
1784 QStringList() << "parent1"
1786 << "realGrandChild1"
1790 m_model
->setExpanded(4, true);
1791 QVERIFY(itemsInsertedSpy
.wait());
1792 QCOMPARE(itemsInModel(),
1793 QStringList() << "parent1"
1795 << "realGrandChild1"
1798 << "realGrandChild2");
1800 // Add some more children and grand-children.
1801 const QUrl parent1
= m_model
->fileItem(0).url();
1802 const QUrl parent2
= m_model
->fileItem(3).url();
1803 const QUrl realChild1
= m_model
->fileItem(1).url();
1804 const QUrl realChild2
= m_model
->fileItem(4).url();
1806 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1807 m_model
->slotCompleted();
1808 QCOMPARE(itemsInModel(),
1809 QStringList() << "parent1"
1811 << "realGrandChild1"
1815 << "realGrandChild2");
1817 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1818 m_model
->slotCompleted();
1819 QCOMPARE(itemsInModel(),
1820 QStringList() << "parent1"
1822 << "realGrandChild1"
1826 << "realGrandChild2"
1829 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1830 m_model
->slotCompleted();
1831 QCOMPARE(itemsInModel(),
1832 QStringList() << "parent1"
1835 << "realGrandChild1"
1839 << "realGrandChild2"
1842 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1843 m_model
->slotCompleted();
1844 QCOMPARE(itemsInModel(),
1845 QStringList() << "parent1"
1848 << "realGrandChild1"
1853 << "realGrandChild2"
1856 // Set a name filter that matches nothing -> nothing will remain.
1857 m_model
->setNameFilter("xyz");
1858 QCOMPARE(itemsInModel(), QStringList());
1859 QCOMPARE(itemsRemovedSpy
.count(), 1);
1860 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1861 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1862 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 10));
1864 // Set a name filter that matches only "realChild". Their prarents should still show.
1865 m_model
->setNameFilter("realChild");
1866 QCOMPARE(itemsInModel(),
1867 QStringList() << "parent1"
1871 QCOMPARE(itemsRemovedSpy
.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
1873 // Collapse "parent1".
1874 m_model
->setExpanded(0, false);
1875 QCOMPARE(itemsInModel(),
1876 QStringList() << "parent1"
1879 QCOMPARE(itemsRemovedSpy
.count(), 1);
1880 arguments
= itemsRemovedSpy
.takeFirst();
1881 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1882 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1884 // Remove "parent2".
1885 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1886 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1887 QCOMPARE(itemsRemovedSpy
.count(), 1);
1888 arguments
= itemsRemovedSpy
.takeFirst();
1889 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1890 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1892 // Clear filter, verify that no items reappear.
1893 m_model
->setNameFilter(QString());
1894 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1897 void KFileItemModelTest::testNameRoleGroups()
1899 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1900 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1901 QVERIFY(itemsMovedSpy
.isValid());
1902 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1903 QVERIFY(groupsChangedSpy
.isValid());
1905 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1907 m_model
->setGroupedSorting(true);
1908 m_model
->loadDirectory(m_testDir
->url());
1909 QVERIFY(itemsInsertedSpy
.wait());
1910 QCOMPARE(itemsInModel(),
1911 QStringList() << "b.txt"
1916 QList
<QPair
<int, QVariant
>> expectedGroups
;
1917 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1918 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1919 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1920 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1921 QCOMPARE(m_model
->groups(), expectedGroups
);
1923 // Rename d.txt to a.txt.
1924 QHash
<QByteArray
, QVariant
> data
;
1925 data
.insert("text", "a.txt");
1926 m_model
->setData(2, data
);
1927 QVERIFY(itemsMovedSpy
.wait());
1928 QCOMPARE(itemsInModel(),
1929 QStringList() << "a.txt"
1934 expectedGroups
.clear();
1935 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1936 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1937 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1938 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1939 QCOMPARE(m_model
->groups(), expectedGroups
);
1941 // Rename c.txt to d.txt.
1942 data
.insert("text", "d.txt");
1943 m_model
->setData(2, data
);
1944 QVERIFY(groupsChangedSpy
.wait());
1945 QCOMPARE(itemsInModel(),
1946 QStringList() << "a.txt"
1951 expectedGroups
.clear();
1952 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1953 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1954 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1955 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1956 QCOMPARE(m_model
->groups(), expectedGroups
);
1958 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1959 const KFileItem fileItemD
= m_model
->fileItem(2);
1960 KFileItem fileItemC
= fileItemD
;
1961 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1962 urlC
.setPath(urlC
.path() + "c.txt");
1963 fileItemC
.setUrl(urlC
);
1965 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1966 QVERIFY(groupsChangedSpy
.wait());
1967 QCOMPARE(itemsInModel(),
1968 QStringList() << "a.txt"
1973 expectedGroups
.clear();
1974 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1975 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1976 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1977 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1978 QCOMPARE(m_model
->groups(), expectedGroups
);
1981 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1983 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1985 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1986 modelRoles
<< "isExpanded"
1988 << "expandedParentsCount";
1989 m_model
->setRoles(modelRoles
);
1991 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1993 m_model
->setGroupedSorting(true);
1994 m_model
->loadDirectory(m_testDir
->url());
1995 QVERIFY(itemsInsertedSpy
.wait());
1996 QCOMPARE(itemsInModel(),
1997 QStringList() << "a"
2000 QList
<QPair
<int, QVariant
>> expectedGroups
;
2001 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2002 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
2003 QCOMPARE(m_model
->groups(), expectedGroups
);
2005 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
2006 expectedGroups
.clear();
2007 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2008 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
2010 m_model
->setExpanded(0, true);
2011 QVERIFY(m_model
->isExpanded(0));
2012 QVERIFY(itemsInsertedSpy
.wait());
2013 QCOMPARE(itemsInModel(),
2014 QStringList() << "a"
2018 QCOMPARE(m_model
->groups(), expectedGroups
);
2020 m_model
->setExpanded(3, true);
2021 QVERIFY(m_model
->isExpanded(3));
2022 QVERIFY(itemsInsertedSpy
.wait());
2023 QCOMPARE(itemsInModel(),
2024 QStringList() << "a"
2030 QCOMPARE(m_model
->groups(), expectedGroups
);
2033 void KFileItemModelTest::testInconsistentModel()
2035 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2037 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2038 modelRoles
<< "isExpanded"
2040 << "expandedParentsCount";
2041 m_model
->setRoles(modelRoles
);
2043 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
2045 m_model
->loadDirectory(m_testDir
->url());
2046 QVERIFY(itemsInsertedSpy
.wait());
2047 QCOMPARE(itemsInModel(), QStringList() << "a");
2049 // Expand "a/" and "a/b/".
2050 m_model
->setExpanded(0, true);
2051 QVERIFY(m_model
->isExpanded(0));
2052 QVERIFY(itemsInsertedSpy
.wait());
2053 QCOMPARE(itemsInModel(),
2054 QStringList() << "a"
2057 m_model
->setExpanded(1, true);
2058 QVERIFY(m_model
->isExpanded(1));
2059 QVERIFY(itemsInsertedSpy
.wait());
2060 QCOMPARE(itemsInModel(),
2061 QStringList() << "a"
2066 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
2067 // Such a thing can in principle happen when performing a search, and there
2069 // (a) match the search string, and
2070 // (b) are children of a folder that matches the search string and is expanded.
2072 // Note that the first item in the list of added items must be new (i.e., not
2073 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
2074 // it receives items that are in the model already and ignore them.
2075 QUrl
url(m_model
->directory().url() + "/a2");
2076 KFileItem
newItem(url
);
2078 KFileItemList items
;
2079 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
2080 m_model
->slotItemsAdded(m_model
->directory(), items
);
2081 m_model
->slotCompleted();
2082 QCOMPARE(itemsInModel(),
2083 QStringList() << "a"
2091 m_model
->setExpanded(0, false);
2093 // Test that the right items have been removed, see
2094 // https://bugs.kde.org/show_bug.cgi?id=324371
2095 QCOMPARE(itemsInModel(),
2096 QStringList() << "a"
2101 // Test that resorting does not cause a crash, see
2102 // https://bugs.kde.org/show_bug.cgi?id=325359
2103 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
2104 m_model
->resortAllItems();
2107 void KFileItemModelTest::testChangeRolesForFilteredItems()
2109 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2111 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2112 modelRoles
<< "owner";
2113 m_model
->setRoles(modelRoles
);
2115 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
2117 m_model
->loadDirectory(m_testDir
->url());
2118 QVERIFY(itemsInsertedSpy
.wait());
2119 QCOMPARE(itemsInModel(),
2120 QStringList() << "a.txt"
2124 for (int index
= 0; index
< m_model
->count(); ++index
) {
2125 // All items should have the "text" and "owner" roles, but not "group".
2126 QVERIFY(m_model
->data(index
).contains("text"));
2127 QVERIFY(m_model
->data(index
).contains("owner"));
2128 QVERIFY(!m_model
->data(index
).contains("group"));
2131 // Add a filter, such that only "aaa.txt" remains in the model.
2132 m_model
->setNameFilter("aaa");
2133 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
2135 // Add the "group" role.
2136 modelRoles
<< "group";
2137 m_model
->setRoles(modelRoles
);
2139 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
2140 m_model
->setNameFilter("aa");
2141 QCOMPARE(itemsInModel(),
2142 QStringList() << "aa.txt"
2145 for (int index
= 0; index
< m_model
->count(); ++index
) {
2146 // All items should have the "text", "owner", and "group" roles.
2147 QVERIFY(m_model
->data(index
).contains("text"));
2148 QVERIFY(m_model
->data(index
).contains("owner"));
2149 QVERIFY(m_model
->data(index
).contains("group"));
2152 // Remove the "owner" role.
2153 modelRoles
.remove("owner");
2154 m_model
->setRoles(modelRoles
);
2156 // Clear the filter, and verify that all items have the expected roles
2157 m_model
->setNameFilter(QString());
2158 QCOMPARE(itemsInModel(),
2159 QStringList() << "a.txt"
2163 for (int index
= 0; index
< m_model
->count(); ++index
) {
2164 // All items should have the "text" and "group" roles, but now "owner".
2165 QVERIFY(m_model
->data(index
).contains("text"));
2166 QVERIFY(!m_model
->data(index
).contains("owner"));
2167 QVERIFY(m_model
->data(index
).contains("group"));
2171 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
2173 KFileItemList items
;
2175 KIO::UDSEntry entry
[3];
2177 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
2178 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
2180 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
2181 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
2183 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
2184 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
2186 for (int i
= 0; i
< 3; ++i
) {
2187 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
2188 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
2189 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
2190 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
2191 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
2192 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
2193 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
2196 m_model
->slotItemsAdded(m_testDir
->url(), items
);
2197 m_model
->slotCompleted();
2199 QCOMPARE(itemsInModel(),
2200 QStringList() << "a.txt"
2205 m_model
->setNameFilter("a");
2206 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
2209 m_model
->setSortRole("owner");
2211 // Clear the filter, and verify that the items are sorted correctly.
2212 m_model
->setNameFilter(QString());
2213 QCOMPARE(itemsInModel(),
2214 QStringList() << "c.txt"
2219 void KFileItemModelTest::testRefreshFilteredItems()
2221 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2223 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
2225 m_model
->loadDirectory(m_testDir
->url());
2226 QVERIFY(itemsInsertedSpy
.wait());
2227 QCOMPARE(itemsInModel(),
2228 QStringList() << "a.txt"
2233 const KFileItem fileItemC
= m_model
->fileItem(2);
2235 // Show only the .txt files.
2236 m_model
->setNameFilter(".txt");
2237 QCOMPARE(itemsInModel(),
2238 QStringList() << "a.txt"
2241 // Rename one of the .jpg files.
2242 KFileItem fileItemE
= fileItemC
;
2243 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
2244 urlE
.setPath(urlE
.path() + "/e.jpg");
2245 fileItemE
.setUrl(urlE
);
2247 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
2249 // Show all files again, and verify that the model has updated the file name.
2250 m_model
->setNameFilter(QString());
2251 QCOMPARE(itemsInModel(),
2252 QStringList() << "a.txt"
2258 void KFileItemModelTest::testCreateMimeData()
2260 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2262 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2263 modelRoles
<< "isExpanded"
2265 << "expandedParentsCount";
2266 m_model
->setRoles(modelRoles
);
2268 m_testDir
->createFile("a/1");
2270 m_model
->loadDirectory(m_testDir
->url());
2271 QVERIFY(itemsInsertedSpy
.wait());
2272 QCOMPARE(itemsInModel(), QStringList() << "a");
2275 m_model
->setExpanded(0, true);
2276 QVERIFY(itemsInsertedSpy
.wait());
2277 QCOMPARE(itemsInModel(),
2278 QStringList() << "a"
2281 // Verify that creating the MIME data for a child of an expanded folder does
2282 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
2284 selection
.insert(1);
2285 QMimeData
*mimeData
= m_model
->createMimeData(selection
);
2289 void KFileItemModelTest::testCollapseFolderWhileLoading()
2291 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2293 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2294 modelRoles
<< "isExpanded"
2296 << "expandedParentsCount";
2297 m_model
->setRoles(modelRoles
);
2299 m_testDir
->createFile("a2/b/c1.txt");
2301 m_model
->loadDirectory(m_testDir
->url());
2302 QVERIFY(itemsInsertedSpy
.wait());
2303 QCOMPARE(itemsInModel(), QStringList() << "a2");
2306 m_model
->setExpanded(0, true);
2307 QVERIFY(m_model
->isExpanded(0));
2308 QVERIFY(itemsInsertedSpy
.wait());
2309 QCOMPARE(itemsInModel(),
2310 QStringList() << "a2"
2314 m_model
->setExpanded(1, true);
2315 QVERIFY(m_model
->isExpanded(1));
2316 QVERIFY(itemsInsertedSpy
.wait());
2317 QCOMPARE(itemsInModel(),
2318 QStringList() << "a2"
2322 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
2323 // signal is not emitted yet.
2324 const KFileItem fileItemC1
= m_model
->fileItem(2);
2325 KFileItem fileItemC2
= fileItemC1
;
2326 QUrl urlC2
= fileItemC2
.url();
2327 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
2328 urlC2
.setPath(urlC2
.path() + "c2.txt");
2329 fileItemC2
.setUrl(urlC2
);
2331 const QUrl urlB
= m_model
->fileItem(1).url();
2332 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
2333 QCOMPARE(itemsInModel(),
2334 QStringList() << "a2"
2338 // Collapse "a2/". This should also remove all its (indirect) children from
2339 // the model and from the model's m_pendingItemsToInsert member.
2340 m_model
->setExpanded(0, false);
2341 QCOMPARE(itemsInModel(), QStringList() << "a2");
2343 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
2344 // is still in m_pendingItemsToInsert, then we might get a crash, see
2345 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
2346 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
2347 // without parent in the model.
2348 m_model
->slotCompleted();
2349 QCOMPARE(itemsInModel(), QStringList() << "a2");
2351 // Expand "a2/" again.
2352 m_model
->setExpanded(0, true);
2353 QVERIFY(m_model
->isExpanded(0));
2354 QVERIFY(itemsInsertedSpy
.wait());
2355 QCOMPARE(itemsInModel(),
2356 QStringList() << "a2"
2359 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
2360 // completed() signal is not emitted yet.
2361 const KFileItem fileItemA2
= m_model
->fileItem(0);
2362 KFileItem fileItemA1
= fileItemA2
;
2363 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
2364 urlA1
.setPath(urlA1
.path() + "a1");
2365 fileItemA1
.setUrl(urlA1
);
2367 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
2368 QCOMPARE(itemsInModel(),
2369 QStringList() << "a2"
2372 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
2373 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
2374 // confuse the code which collapses the folder.
2375 m_model
->setExpanded(0, false);
2376 QCOMPARE(itemsInModel(),
2377 QStringList() << "a1"
2379 QVERIFY(!m_model
->isExpanded(0));
2380 QVERIFY(!m_model
->isExpanded(1));
2383 void KFileItemModelTest::testDeleteFileMoreThanOnce()
2385 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2387 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
2389 m_model
->loadDirectory(m_testDir
->url());
2390 QVERIFY(itemsInsertedSpy
.wait());
2391 QCOMPARE(itemsInModel(),
2392 QStringList() << "a.txt"
2397 const KFileItem fileItemB
= m_model
->fileItem(1);
2399 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
2401 list
<< fileItemB
<< fileItemB
;
2402 m_model
->slotItemsDeleted(list
);
2404 QVERIFY(m_model
->isConsistent());
2405 QCOMPARE(itemsInModel(),
2406 QStringList() << "a.txt"
2411 void KFileItemModelTest::testInsertAfterExpand()
2413 m_model
->m_dirLister
->setAutoUpdate(true);
2415 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2416 QVERIFY(itemsInsertedSpy
.isValid());
2417 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
2418 QVERIFY(itemsRemovedSpy
.isValid());
2419 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2420 QVERIFY(loadingCompletedSpy
.isValid());
2422 // Test expanding subfolders in a folder with the items "a/", "a/a/"
2423 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
2424 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
2425 modelRoles
<< "isExpanded"
2427 << "expandedParentsCount";
2428 m_model
->setRoles(modelRoles
);
2430 m_testDir
->createFile("a/b/1");
2432 m_model
->loadDirectory(m_testDir
->url());
2433 QVERIFY(itemsInsertedSpy
.wait());
2435 // So far, the model contains only "a/"
2436 QCOMPARE(m_model
->count(), 1);
2437 QVERIFY(m_model
->isExpandable(0));
2438 QVERIFY(!m_model
->isExpanded(0));
2439 QVERIFY(m_model
->expandedDirectories().empty());
2441 QCOMPARE(itemsInsertedSpy
.count(), 1);
2443 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2444 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
2445 QCOMPARE(m_model
->expandedParentsCount(0), 0);
2447 itemsInsertedSpy
.clear();
2449 // Expand the folder "a/" -> "a/b" become visible
2450 m_model
->setExpanded(0, true);
2451 QVERIFY(m_model
->isExpanded(0));
2452 QVERIFY(itemsInsertedSpy
.wait());
2453 QCOMPARE(m_model
->count(), 2); // 3 items: "a/", "a/a/"
2454 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>({QUrl::fromLocalFile(m_testDir
->path() + "/a")}));
2456 QCOMPARE(itemsInsertedSpy
.count(), 1);
2458 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2459 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1)); // 1 new item "a/b" with index 1
2460 QCOMPARE(m_model
->expandedParentsCount(1), 1);
2462 itemsInsertedSpy
.clear();
2464 // Expand "a/b" -> "a/b/1" becomes visible
2465 m_model
->setExpanded(1, true);
2466 QVERIFY(itemsInsertedSpy
.wait());
2467 QCOMPARE(m_model
->expandedDirectories(), QSet({QUrl::fromLocalFile(m_testDir
->path() + "/a"), QUrl::fromLocalFile(m_testDir
->path() + "/a/b")}));
2469 QCOMPARE(itemsInsertedSpy
.count(), 1);
2471 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2472 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/b/1" with index 2
2473 QCOMPARE(m_model
->expandedParentsCount(2), 2);
2475 itemsInsertedSpy
.clear();
2477 // Collapse "a" whilst leaving "b" expanded
2478 m_model
->setExpanded(0, false);
2480 // Insert additional files into "a/b/"
2481 m_testDir
->createFile("a/b/2");
2482 #if KIO_VERSION < QT_VERSION_CHECK(5, 92, 0)
2483 QEXPECT_FAIL("", "Requires new API from frameworks", Abort
);
2486 QVERIFY(!itemsInsertedSpy
.wait(5000));
2488 QCOMPARE(itemsInModel(), {"a"});
2490 m_model
->setExpanded(0, true);
2492 QTRY_COMPARE(itemsInModel(), QStringList({"a", "b", "1", "2"}));
2494 QCOMPARE(m_model
->expandedParentsCount(0), 0); // a
2495 QCOMPARE(m_model
->expandedParentsCount(1), 1); // a/b
2496 QCOMPARE(m_model
->expandedParentsCount(2), 2); // a/b/1
2497 QCOMPARE(m_model
->expandedParentsCount(3), 2); // a/b/2
2500 void KFileItemModelTest::testCurrentDirRemoved()
2502 m_model
->m_dirLister
->setAutoUpdate(true);
2503 QSignalSpy
currentDirectoryRemovedSpy(m_model
, &KFileItemModel::currentDirectoryRemoved
);
2504 QVERIFY(currentDirectoryRemovedSpy
.isValid());
2505 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2506 QVERIFY(loadingCompletedSpy
.isValid());
2507 QSignalSpy
dirListerClearSpy(m_model
->m_dirLister
, &KCoreDirLister::clear
);
2508 QVERIFY(dirListerClearSpy
.isValid());
2510 m_testDir
->createFiles({"dir/a.txt", "dir/b.txt"});
2511 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/dir/"));
2512 QVERIFY(loadingCompletedSpy
.wait());
2513 QCOMPARE(m_model
->count(), 2);
2514 QVERIFY(m_model
->isConsistent());
2516 m_testDir
->removeDir("dir");
2517 QVERIFY(currentDirectoryRemovedSpy
.wait());
2519 // dirLister calls clear
2520 QCOMPARE(dirListerClearSpy
.count(), 2);
2521 QVERIFY(m_model
->isConsistent());
2522 QVERIFY(m_model
->m_itemData
.isEmpty());
2523 QCOMPARE(m_model
->count(), 0);
2526 QStringList
KFileItemModelTest::itemsInModel() const
2529 for (int i
= 0; i
< m_model
->count(); i
++) {
2530 items
<< m_model
->fileItem(i
).text();
2535 QTEST_MAIN(KFileItemModelTest
)
2537 #include "kfileitemmodeltest.moc"