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 testNaturalSorting();
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();
96 void testSizeSortingAfterRefresh();
99 QStringList
itemsInModel() const;
102 KFileItemModel
*m_model
;
106 void KFileItemModelTest::initTestCase()
108 QStandardPaths::setTestModeEnabled(true);
111 void KFileItemModelTest::init()
113 // The item-model tests result in a huge number of debugging
114 // output from kdelibs. Only show critical and fatal messages.
115 qInstallMessageHandler(myMessageOutput
);
117 qRegisterMetaType
<KItemRange
>("KItemRange");
118 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
119 qRegisterMetaType
<KFileItemList
>("KFileItemList");
121 m_testDir
= new TestDir();
122 m_model
= new KFileItemModel();
123 m_model
->m_dirLister
->setAutoUpdate(false);
125 // Reduce the timer interval to make the test run faster.
126 m_model
->m_resortAllItemsTimer
->setInterval(0);
129 void KFileItemModelTest::cleanup()
138 void KFileItemModelTest::testDefaultRoles()
140 const QSet
<QByteArray
> roles
= m_model
->roles();
141 QCOMPARE(roles
.count(), 4);
142 QVERIFY(roles
.contains("text"));
143 QVERIFY(roles
.contains("isDir"));
144 QVERIFY(roles
.contains("isLink"));
145 QVERIFY(roles
.contains("isHidden"));
148 void KFileItemModelTest::testDefaultSortRole()
150 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
151 QVERIFY(itemsInsertedSpy
.isValid());
153 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
155 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
157 m_model
->loadDirectory(m_testDir
->url());
158 QVERIFY(itemsInsertedSpy
.wait());
160 QCOMPARE(m_model
->count(), 3);
161 QCOMPARE(m_model
->data(0).value("text").toString(), QStringLiteral("a.txt"));
162 QCOMPARE(m_model
->data(1).value("text").toString(), QStringLiteral("b.txt"));
163 QCOMPARE(m_model
->data(2).value("text").toString(), QStringLiteral("c.txt"));
166 void KFileItemModelTest::testDefaultGroupedSorting()
168 QCOMPARE(m_model
->groupedSorting(), true);
171 void KFileItemModelTest::testNewItems()
173 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
175 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
177 m_model
->loadDirectory(m_testDir
->url());
178 QVERIFY(itemsInsertedSpy
.wait());
180 QCOMPARE(m_model
->count(), 3);
182 QVERIFY(m_model
->isConsistent());
185 void KFileItemModelTest::testRemoveItems()
187 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
188 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
190 m_testDir
->createFiles({"a.txt", "b.txt"});
191 m_model
->loadDirectory(m_testDir
->url());
192 QVERIFY(itemsInsertedSpy
.wait());
193 QCOMPARE(m_model
->count(), 2);
194 QVERIFY(m_model
->isConsistent());
196 m_testDir
->removeFile("a.txt");
197 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
198 QVERIFY(itemsRemovedSpy
.wait());
199 QCOMPARE(m_model
->count(), 1);
200 QVERIFY(m_model
->isConsistent());
203 void KFileItemModelTest::testDirLoadingCompleted()
205 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
206 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
207 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
209 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
211 m_model
->loadDirectory(m_testDir
->url());
212 QVERIFY(loadingCompletedSpy
.wait());
213 QCOMPARE(loadingCompletedSpy
.count(), 1);
214 QCOMPARE(itemsInsertedSpy
.count(), 1);
215 QCOMPARE(itemsRemovedSpy
.count(), 0);
216 QCOMPARE(m_model
->count(), 3);
218 m_testDir
->createFiles({"d.txt", "e.txt"});
219 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
220 QVERIFY(loadingCompletedSpy
.wait());
221 QCOMPARE(loadingCompletedSpy
.count(), 2);
222 QCOMPARE(itemsInsertedSpy
.count(), 2);
223 QCOMPARE(itemsRemovedSpy
.count(), 0);
224 QCOMPARE(m_model
->count(), 5);
226 m_testDir
->removeFile("a.txt");
227 m_testDir
->createFile("f.txt");
228 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
229 QVERIFY(loadingCompletedSpy
.wait());
230 QCOMPARE(loadingCompletedSpy
.count(), 3);
231 QCOMPARE(itemsInsertedSpy
.count(), 3);
232 QCOMPARE(itemsRemovedSpy
.count(), 1);
233 QCOMPARE(m_model
->count(), 5);
235 m_testDir
->removeFile("b.txt");
236 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
237 QVERIFY(itemsRemovedSpy
.wait());
238 QCOMPARE(loadingCompletedSpy
.count(), 4);
239 QCOMPARE(itemsInsertedSpy
.count(), 3);
240 QCOMPARE(itemsRemovedSpy
.count(), 2);
241 QCOMPARE(m_model
->count(), 4);
243 QVERIFY(m_model
->isConsistent());
246 void KFileItemModelTest::testSetData()
248 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
249 QVERIFY(itemsInsertedSpy
.isValid());
250 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
251 QVERIFY(itemsChangedSpy
.isValid());
253 m_testDir
->createFile("a.txt");
255 m_model
->loadDirectory(m_testDir
->url());
256 QVERIFY(itemsInsertedSpy
.wait());
258 QHash
<QByteArray
, QVariant
> values
;
259 values
.insert("customRole1", "Test1");
260 values
.insert("customRole2", "Test2");
262 m_model
->setData(0, values
);
263 QCOMPARE(itemsChangedSpy
.count(), 1);
265 values
= m_model
->data(0);
266 QCOMPARE(values
.value("customRole1").toString(), QStringLiteral("Test1"));
267 QCOMPARE(values
.value("customRole2").toString(), QStringLiteral("Test2"));
268 QVERIFY(m_model
->isConsistent());
271 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
273 QTest::addColumn
<int>("changedIndex");
274 QTest::addColumn
<int>("changedRating");
275 QTest::addColumn
<bool>("expectMoveSignal");
276 QTest::addColumn
<int>("ratingIndex0");
277 QTest::addColumn
<int>("ratingIndex1");
278 QTest::addColumn
<int>("ratingIndex2");
281 // Index 0 = rating 2
282 // Index 1 = rating 4
283 // Index 2 = rating 6
285 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
286 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
287 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
289 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
290 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
291 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
294 void KFileItemModelTest::testSetDataWithModifiedSortRole()
296 QFETCH(int, changedIndex
);
297 QFETCH(int, changedRating
);
298 QFETCH(bool, expectMoveSignal
);
299 QFETCH(int, ratingIndex0
);
300 QFETCH(int, ratingIndex1
);
301 QFETCH(int, ratingIndex2
);
303 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
304 QVERIFY(itemsInsertedSpy
.isValid());
305 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
306 QVERIFY(itemsMovedSpy
.isValid());
308 // Changing the value of a sort-role must result in
309 // a reordering of the items.
310 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
311 m_model
->setSortRole("rating");
312 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
314 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
316 m_model
->loadDirectory(m_testDir
->url());
317 QVERIFY(itemsInsertedSpy
.wait());
319 // Fill the "rating" role of each file:
324 QHash
<QByteArray
, QVariant
> ratingA
;
325 ratingA
.insert("rating", 2);
326 m_model
->setData(0, ratingA
);
328 QHash
<QByteArray
, QVariant
> ratingB
;
329 ratingB
.insert("rating", 4);
330 m_model
->setData(1, ratingB
);
332 QHash
<QByteArray
, QVariant
> ratingC
;
333 ratingC
.insert("rating", 6);
334 m_model
->setData(2, ratingC
);
336 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
337 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
338 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
340 // Now change the rating from a.txt. This usually results
341 // in reordering of the items.
342 QHash
<QByteArray
, QVariant
> rating
;
343 rating
.insert("rating", changedRating
);
344 m_model
->setData(changedIndex
, rating
);
346 if (expectMoveSignal
) {
347 QVERIFY(itemsMovedSpy
.wait());
350 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
351 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
352 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
353 QVERIFY(m_model
->isConsistent());
356 void KFileItemModelTest::testChangeSortRole()
358 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
359 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
360 QVERIFY(itemsMovedSpy
.isValid());
362 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
364 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
366 m_model
->loadDirectory(m_testDir
->url());
367 QVERIFY(itemsInsertedSpy
.wait());
368 QCOMPARE(itemsInModel(),
369 QStringList() << "a.txt"
373 // Simulate that KFileItemModelRolesUpdater determines the mime type.
374 // Resorting the files by 'type' will only work immediately if their
375 // mime types are known.
376 for (int index
= 0; index
< m_model
->count(); ++index
) {
377 m_model
->fileItem(index
).determineMimeType();
380 // Now: sort by type.
381 m_model
->setSortRole("type");
382 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
383 QVERIFY(!itemsMovedSpy
.isEmpty());
385 // The actual order of the files might depend on the translation of the
386 // result of KFileItem::mimeComment() in the user's language.
387 QStringList version1
;
392 QStringList version2
;
397 const bool ok1
= (itemsInModel() == version1
);
398 const bool ok2
= (itemsInModel() == version2
);
403 void KFileItemModelTest::testResortAfterChangingName()
405 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
406 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
407 QVERIFY(itemsMovedSpy
.isValid());
409 // We sort by size in a directory where all files have the same size.
410 // Therefore, the files are sorted by their names.
411 m_model
->setSortRole("size");
413 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
415 m_model
->loadDirectory(m_testDir
->url());
416 QVERIFY(itemsInsertedSpy
.wait());
417 QCOMPARE(itemsInModel(),
418 QStringList() << "a.txt"
422 // We rename a.txt to d.txt. Even though the size has not changed at all,
423 // the model must re-sort the items.
424 QHash
<QByteArray
, QVariant
> data
;
425 data
.insert("text", "d.txt");
426 m_model
->setData(0, data
);
428 QVERIFY(itemsMovedSpy
.wait());
429 QCOMPARE(itemsInModel(),
430 QStringList() << "b.txt"
434 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
435 const KFileItem fileItemD
= m_model
->fileItem(2);
436 KFileItem fileItemA
= fileItemD
;
437 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
438 urlA
.setPath(urlA
.path() + "a.txt");
439 fileItemA
.setUrl(urlA
);
441 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
443 QVERIFY(itemsMovedSpy
.wait());
444 QCOMPARE(itemsInModel(),
445 QStringList() << "a.txt"
450 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
452 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
454 // KFileItemModel prevents that inserting a punch of items sequentially
455 // results in an itemsInserted()-signal for each item. Instead internally
456 // a timeout is given that collects such operations and results in only
457 // one itemsInserted()-signal. However in this test we want to stress
458 // KFileItemModel to do a lot of insert operation and hence decrease
459 // the timeout to 1 millisecond.
460 m_testDir
->createFile("1");
461 m_model
->loadDirectory(m_testDir
->url());
462 QVERIFY(itemsInsertedSpy
.wait());
463 QCOMPARE(m_model
->count(), 1);
465 // Insert 10 items for 20 times. After each insert operation the model consistency
467 QSet
<int> insertedItems
;
468 for (int i
= 0; i
< 20; ++i
) {
469 itemsInsertedSpy
.clear();
471 for (int j
= 0; j
< 10; ++j
) {
472 int itemName
= QRandomGenerator::global()->generate();
473 while (insertedItems
.contains(itemName
)) {
474 itemName
= QRandomGenerator::global()->generate();
476 insertedItems
.insert(itemName
);
478 m_testDir
->createFile(QString::number(itemName
));
481 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
482 if (itemsInsertedSpy
.isEmpty()) {
483 QVERIFY(itemsInsertedSpy
.wait());
486 QVERIFY(m_model
->isConsistent());
489 QCOMPARE(m_model
->count(), 201);
492 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
494 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
496 m_testDir
->createFiles({"B", "E", "G"});
498 // Due to inserting the 3 items one item-range with index == 0 and
499 // count == 3 must be given
500 m_model
->loadDirectory(m_testDir
->url());
501 QVERIFY(itemsInsertedSpy
.wait());
503 QCOMPARE(itemsInsertedSpy
.count(), 1);
504 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
505 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
506 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
508 // The indexes of the item-ranges must always be related to the model before
509 // the items have been inserted. Having:
512 // and inserting A, C, D, F the resulting model will be:
515 // and the item-ranges must be:
516 // index: 0, count: 1 for A
517 // index: 1, count: 2 for B, C
518 // index: 2, count: 1 for G
520 m_testDir
->createFiles({"A", "C", "D", "F"});
522 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
523 QVERIFY(itemsInsertedSpy
.wait());
525 QCOMPARE(itemsInsertedSpy
.count(), 1);
526 arguments
= itemsInsertedSpy
.takeFirst();
527 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
528 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
531 void KFileItemModelTest::testExpandItems()
533 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
534 QVERIFY(itemsInsertedSpy
.isValid());
535 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
536 QVERIFY(itemsRemovedSpy
.isValid());
537 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
538 QVERIFY(loadingCompletedSpy
.isValid());
540 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
541 // Besides testing the basic item expansion functionality, the test makes sure that
542 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
543 // yields the correct result for "a/a/1" and "a/a-1/", which is non-trivial because they share the
544 // first three characters.
545 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
546 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
547 modelRoles
<< "isExpanded"
549 << "expandedParentsCount";
550 m_model
->setRoles(modelRoles
);
552 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
554 // Store the URLs of all folders in a set.
555 QSet
<QUrl
> allFolders
;
556 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a") << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
557 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
559 m_model
->loadDirectory(m_testDir
->url());
560 QVERIFY(itemsInsertedSpy
.wait());
562 // So far, the model contains only "a/"
563 QCOMPARE(m_model
->count(), 1);
564 QVERIFY(m_model
->isExpandable(0));
565 QVERIFY(!m_model
->isExpanded(0));
566 QVERIFY(m_model
->expandedDirectories().empty());
568 QCOMPARE(itemsInsertedSpy
.count(), 1);
569 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
570 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
572 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
573 m_model
->setExpanded(0, true);
574 QVERIFY(m_model
->isExpanded(0));
575 QVERIFY(itemsInsertedSpy
.wait());
576 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
577 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
579 QCOMPARE(itemsInsertedSpy
.count(), 1);
580 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
581 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
583 QVERIFY(m_model
->isExpandable(1));
584 QVERIFY(!m_model
->isExpanded(1));
585 QVERIFY(m_model
->isExpandable(2));
586 QVERIFY(!m_model
->isExpanded(2));
588 // Expand the folder "a/a/" -> "a/a/1" becomes visible
589 m_model
->setExpanded(1, true);
590 QVERIFY(m_model
->isExpanded(1));
591 QVERIFY(itemsInsertedSpy
.wait());
592 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
593 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a") << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
595 QCOMPARE(itemsInsertedSpy
.count(), 1);
596 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
597 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
599 QVERIFY(!m_model
->isExpandable(2));
600 QVERIFY(!m_model
->isExpanded(2));
602 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
603 m_model
->setExpanded(3, true);
604 QVERIFY(m_model
->isExpanded(3));
605 QVERIFY(itemsInsertedSpy
.wait());
606 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
607 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
609 QCOMPARE(itemsInsertedSpy
.count(), 1);
610 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
611 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
613 QVERIFY(!m_model
->isExpandable(4));
614 QVERIFY(!m_model
->isExpanded(4));
616 // Collapse the top-level folder -> all other items should disappear
617 m_model
->setExpanded(0, false);
618 QVERIFY(!m_model
->isExpanded(0));
619 QCOMPARE(m_model
->count(), 1);
620 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
622 QCOMPARE(itemsRemovedSpy
.count(), 1);
623 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
624 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
625 QVERIFY(m_model
->isConsistent());
627 // Clear the model, reload the folder and try to restore the expanded folders.
629 QCOMPARE(m_model
->count(), 0);
630 QVERIFY(m_model
->expandedDirectories().empty());
632 m_model
->loadDirectory(m_testDir
->url());
633 m_model
->restoreExpandedDirectories(allFolders
);
634 QVERIFY(loadingCompletedSpy
.wait());
635 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
636 QVERIFY(m_model
->isExpanded(0));
637 QVERIFY(m_model
->isExpanded(1));
638 QVERIFY(!m_model
->isExpanded(2));
639 QVERIFY(m_model
->isExpanded(3));
640 QVERIFY(!m_model
->isExpanded(4));
641 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
642 QVERIFY(m_model
->isConsistent());
644 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
645 // This is how DolphinView restores the expanded folders when navigating in history.
646 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
647 QVERIFY(loadingCompletedSpy
.wait());
648 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
649 m_model
->restoreExpandedDirectories(allFolders
);
650 m_model
->loadDirectory(m_testDir
->url());
651 QVERIFY(loadingCompletedSpy
.wait());
652 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
653 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
655 // Remove all expanded items by changing the roles
656 itemsRemovedSpy
.clear();
657 m_model
->setRoles(originalModelRoles
);
658 QVERIFY(!m_model
->isExpanded(0));
659 QCOMPARE(m_model
->count(), 1);
660 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
662 QCOMPARE(itemsRemovedSpy
.count(), 1);
663 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
664 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
665 QVERIFY(m_model
->isConsistent());
668 void KFileItemModelTest::testExpandParentItems()
670 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
671 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
672 QVERIFY(loadingCompletedSpy
.isValid());
674 // Create a tree structure of folders:
682 QSet
<QByteArray
> modelRoles
= m_model
->roles();
683 modelRoles
<< "isExpanded"
685 << "expandedParentsCount";
686 m_model
->setRoles(modelRoles
);
688 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
690 m_model
->loadDirectory(m_testDir
->url());
691 QVERIFY(itemsInsertedSpy
.wait());
693 // So far, the model contains only "a 1/" and "a2/".
694 QCOMPARE(m_model
->count(), 2);
695 QVERIFY(m_model
->expandedDirectories().empty());
697 // Expand the parents of "a2/b2/c2".
698 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
699 QVERIFY(loadingCompletedSpy
.wait());
701 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
702 // It's important that only the parents of "a1/b1/c1" are expanded.
703 QCOMPARE(m_model
->count(), 4);
704 QVERIFY(!m_model
->isExpanded(0));
705 QVERIFY(m_model
->isExpanded(1));
706 QVERIFY(m_model
->isExpanded(2));
707 QVERIFY(!m_model
->isExpanded(3));
709 // Expand the parents of "a 1/b1".
710 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
711 QVERIFY(loadingCompletedSpy
.wait());
713 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
714 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
715 QCOMPARE(m_model
->count(), 5);
716 QVERIFY(m_model
->isExpanded(0));
717 QVERIFY(!m_model
->isExpanded(1));
718 QVERIFY(m_model
->isExpanded(2));
719 QVERIFY(m_model
->isExpanded(3));
720 QVERIFY(!m_model
->isExpanded(4));
721 QVERIFY(m_model
->isConsistent());
724 m_model
->setExpanded(1, true);
725 QVERIFY(loadingCompletedSpy
.wait());
726 QCOMPARE(m_model
->count(), 6);
727 QVERIFY(m_model
->isExpanded(0));
728 QVERIFY(m_model
->isExpanded(1));
729 QVERIFY(!m_model
->isExpanded(2));
730 QVERIFY(m_model
->isExpanded(3));
731 QVERIFY(m_model
->isExpanded(4));
732 QVERIFY(!m_model
->isExpanded(5));
733 QVERIFY(m_model
->isConsistent());
735 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
736 m_model
->setExpanded(1, false);
737 QCOMPARE(m_model
->count(), 5);
738 QVERIFY(m_model
->isExpanded(0));
739 QVERIFY(!m_model
->isExpanded(1));
740 QVERIFY(m_model
->isExpanded(2));
741 QVERIFY(m_model
->isExpanded(3));
742 QVERIFY(!m_model
->isExpanded(4));
743 QVERIFY(m_model
->isConsistent());
747 * Renaming an expanded folder by prepending its name with a dot makes it
748 * hidden. Verify that this does not cause an inconsistent model state and
749 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
751 void KFileItemModelTest::testMakeExpandedItemHidden()
753 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
754 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
756 QSet
<QByteArray
> modelRoles
= m_model
->roles();
757 modelRoles
<< "isExpanded"
759 << "expandedParentsCount";
760 m_model
->setRoles(modelRoles
);
762 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
764 m_model
->loadDirectory(m_testDir
->url());
765 QVERIFY(itemsInsertedSpy
.wait());
767 // So far, the model contains only "1a/" and "1b".
768 QCOMPARE(m_model
->count(), 2);
769 m_model
->setExpanded(0, true);
770 QVERIFY(itemsInsertedSpy
.wait());
772 // Now "1a/2a" and "1a/2b" have appeared.
773 QCOMPARE(m_model
->count(), 4);
774 m_model
->setExpanded(1, true);
775 QVERIFY(itemsInsertedSpy
.wait());
776 QCOMPARE(m_model
->count(), 6);
778 // Rename "1a/2" and make it hidden.
779 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
780 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
782 KIO::SimpleJob
*job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
783 bool ok
= job
->exec();
785 QVERIFY(itemsRemovedSpy
.wait());
787 // "1a/2" and its subfolders have disappeared now.
788 QVERIFY(m_model
->isConsistent());
789 QCOMPARE(m_model
->count(), 3);
791 m_model
->setExpanded(0, false);
792 QCOMPARE(m_model
->count(), 2);
795 void KFileItemModelTest::testRemoveFilteredExpandedItems()
797 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
799 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
800 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
801 modelRoles
<< "isExpanded"
803 << "expandedParentsCount";
804 m_model
->setRoles(modelRoles
);
806 m_testDir
->createFiles({"folder/child", "file"});
808 m_model
->loadDirectory(m_testDir
->url());
809 QVERIFY(itemsInsertedSpy
.wait());
811 // So far, the model contains only "folder/" and "file".
812 QCOMPARE(m_model
->count(), 2);
813 QVERIFY(m_model
->isExpandable(0));
814 QVERIFY(!m_model
->isExpandable(1));
815 QVERIFY(!m_model
->isExpanded(0));
816 QVERIFY(!m_model
->isExpanded(1));
817 QCOMPARE(itemsInModel(),
818 QStringList() << "folder"
821 // Expand "folder" -> "folder/child" becomes visible.
822 m_model
->setExpanded(0, true);
823 QVERIFY(m_model
->isExpanded(0));
824 QVERIFY(itemsInsertedSpy
.wait());
825 QCOMPARE(itemsInModel(),
826 QStringList() << "folder"
830 // Add a name filter.
831 m_model
->setNameFilter("f");
832 QCOMPARE(itemsInModel(),
833 QStringList() << "folder"
836 m_model
->setNameFilter("fo");
837 QCOMPARE(itemsInModel(), QStringList() << "folder");
839 // Remove all expanded items by changing the roles
840 m_model
->setRoles(originalModelRoles
);
841 QVERIFY(!m_model
->isExpanded(0));
842 QCOMPARE(itemsInModel(), QStringList() << "folder");
844 // Remove the name filter and verify that "folder/child" does not reappear.
845 m_model
->setNameFilter(QString());
846 QCOMPARE(itemsInModel(),
847 QStringList() << "folder"
851 void KFileItemModelTest::testSizeSortingAfterRefresh()
853 // testDir structure is as follows
859 // │ │ ├─ c-3
864 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
865 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
866 QVERIFY(itemsMovedSpy
.isValid());
868 // Create some files with different sizes and modification times to check the different sorting options
869 QDateTime now
= QDateTime::currentDateTime();
871 QSet
<QByteArray
> roles
;
872 roles
.insert("text");
873 roles
.insert("isExpanded");
874 roles
.insert("isExpandable");
875 roles
.insert("expandedParentsCount");
876 m_model
->setRoles(roles
);
878 m_testDir
->createDir("c/c-2");
879 m_testDir
->createFile("c/c-2/c-3");
880 m_testDir
->createFile("c/c-1");
882 m_testDir
->createFile("a", "A file", now
.addDays(-3));
883 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
884 m_testDir
->createDir("c", now
.addDays(-2));
885 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
886 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
888 m_model
->loadDirectory(m_testDir
->url());
889 QVERIFY(itemsInsertedSpy
.wait());
891 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
892 m_model
->setExpanded(index
, true);
893 QVERIFY(itemsInsertedSpy
.wait());
895 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
896 m_model
->setExpanded(index
, true);
897 QVERIFY(itemsInsertedSpy
.wait());
899 // Default: Sort by Name, ascending
900 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
901 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
902 QCOMPARE(itemsInModel(),
912 // Sort by Size, ascending, before refresh
913 m_model
->setSortRole("size");
914 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
915 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
916 QCOMPARE(itemsInModel(),
927 m_model
->refreshDirectory(m_model
->directory());
928 QVERIFY(itemsInsertedSpy
.wait());
930 // Expand folders again
931 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
932 m_model
->setExpanded(index
, true);
933 QVERIFY(itemsInsertedSpy
.wait());
935 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
936 m_model
->setExpanded(index
, true);
937 QVERIFY(itemsInsertedSpy
.wait());
939 // Sort by Size, ascending, after refresh
940 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
941 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
942 QCOMPARE(itemsInModel(),
953 void KFileItemModelTest::testSorting()
955 // testDir structure is as follows
962 // │ │ ├─ c-3
968 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
969 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
970 QVERIFY(itemsMovedSpy
.isValid());
972 // Create some files with different sizes and modification times to check the different sorting options
973 QDateTime now
= QDateTime::currentDateTime();
975 QSet
<QByteArray
> roles
;
976 roles
.insert("text");
977 roles
.insert("isExpanded");
978 roles
.insert("isExpandable");
979 roles
.insert("expandedParentsCount");
980 m_model
->setRoles(roles
);
982 m_testDir
->createDir("c/c-2");
983 m_testDir
->createFile("c/c-2/c-3");
984 m_testDir
->createFile("c/c-1");
986 m_testDir
->createFile("a", "A file", now
.addDays(-3));
987 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
988 m_testDir
->createDir("c", now
.addDays(-2));
989 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
990 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
991 m_testDir
->createFile(".f");
992 m_testDir
->createDir(".g");
994 m_model
->loadDirectory(m_testDir
->url());
995 QVERIFY(itemsInsertedSpy
.wait());
996 QCOMPARE(itemsInsertedSpy
.count(), 1);
997 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
998 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 5));
1000 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
1001 m_model
->setExpanded(index
, true);
1002 QVERIFY(itemsInsertedSpy
.wait());
1003 QCOMPARE(itemsInsertedSpy
.count(), 1);
1004 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1005 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1007 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
1008 m_model
->setExpanded(index
, true);
1009 QVERIFY(itemsInsertedSpy
.wait());
1010 QCOMPARE(itemsInsertedSpy
.count(), 1);
1011 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1012 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1));
1014 // Default: Sort by Name, ascending
1015 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1016 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1017 QVERIFY(m_model
->sortDirectoriesFirst());
1018 QVERIFY(!m_model
->showHiddenFiles());
1019 QCOMPARE(itemsInModel(),
1020 QStringList() << "c"
1029 // Sort by Name, ascending, 'Sort Folders First' disabled
1030 m_model
->setSortDirectoriesFirst(false);
1031 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1032 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1033 QCOMPARE(itemsInModel(),
1034 QStringList() << "a"
1042 QCOMPARE(itemsMovedSpy
.count(), 1);
1043 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1044 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
1046 // Sort by Name, descending
1047 m_model
->setSortDirectoriesFirst(true);
1048 m_model
->setSortOrder(Qt::DescendingOrder
);
1049 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1050 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1051 QCOMPARE(itemsInModel(),
1052 QStringList() << "c"
1060 QCOMPARE(itemsMovedSpy
.count(), 2);
1061 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1062 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
1063 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1064 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1066 // Sort by Date, descending
1067 m_model
->setSortDirectoriesFirst(true);
1068 m_model
->setSortRole("modificationtime");
1069 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1070 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1071 QCOMPARE(itemsInModel(),
1072 QStringList() << "c"
1080 QCOMPARE(itemsMovedSpy
.count(), 1);
1081 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1082 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 5 << 4 << 6);
1084 // Sort by Date, ascending
1085 m_model
->setSortOrder(Qt::AscendingOrder
);
1086 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1087 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1088 QCOMPARE(itemsInModel(),
1089 QStringList() << "c"
1097 QCOMPARE(itemsMovedSpy
.count(), 1);
1098 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1099 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1101 // Sort by Date, ascending, 'Sort Folders First' disabled
1102 m_model
->setSortDirectoriesFirst(false);
1103 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1104 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1105 QVERIFY(!m_model
->sortDirectoriesFirst());
1106 QCOMPARE(itemsInModel(),
1107 QStringList() << "e"
1115 QCOMPARE(itemsMovedSpy
.count(), 1);
1116 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1117 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
1119 // Sort by Name, ascending, 'Sort Folders First' disabled
1120 m_model
->setSortRole("text");
1121 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1122 QVERIFY(!m_model
->sortDirectoriesFirst());
1123 QCOMPARE(itemsInModel(),
1124 QStringList() << "a"
1132 QCOMPARE(itemsMovedSpy
.count(), 1);
1133 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1134 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
1136 // Sort by Size, ascending, 'Sort Folders First' disabled
1137 m_model
->setSortRole("size");
1138 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1139 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1140 QVERIFY(!m_model
->sortDirectoriesFirst());
1141 QCOMPARE(itemsInModel(),
1142 QStringList() << "c"
1150 QCOMPARE(itemsMovedSpy
.count(), 1);
1151 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1152 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
1154 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
1155 m_model
->setSortDirectoriesFirst(true);
1156 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1157 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1158 QVERIFY(m_model
->sortDirectoriesFirst());
1159 QCOMPARE(itemsInModel(),
1160 QStringList() << "c"
1168 QCOMPARE(itemsMovedSpy
.count(), 0);
1170 // Sort by Size, descending, 'Sort Folders First' enabled
1171 m_model
->setSortOrder(Qt::DescendingOrder
);
1172 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1173 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1174 QVERIFY(m_model
->sortDirectoriesFirst());
1175 QCOMPARE(itemsInModel(),
1176 QStringList() << "c"
1184 QCOMPARE(itemsMovedSpy
.count(), 1);
1185 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1186 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1188 // 'Show Hidden Files' enabled
1189 m_model
->setShowHiddenFiles(true);
1190 QVERIFY(m_model
->showHiddenFiles());
1191 QVERIFY(!m_model
->sortHiddenLast());
1192 QCOMPARE(itemsInModel(),
1193 QStringList() << "c"
1203 QCOMPARE(itemsMovedSpy
.count(), 0);
1204 QCOMPARE(itemsInsertedSpy
.count(), 1);
1205 QCOMPARE(itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
1207 // 'Sort Hidden Files Last' enabled
1208 m_model
->setSortHiddenLast(true);
1209 QVERIFY(m_model
->sortHiddenLast());
1210 QCOMPARE(itemsInModel(),
1211 QStringList() << "c"
1221 QCOMPARE(itemsMovedSpy
.count(), 1);
1222 QCOMPARE(itemsInsertedSpy
.count(), 0);
1223 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 5));
1224 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 8 << 4 << 5 << 6 << 7);
1227 m_model
->setSortRole("text");
1228 QCOMPARE(itemsInModel(),
1229 QStringList() << "c"
1239 QCOMPARE(itemsMovedSpy
.count(), 1);
1240 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 2));
1241 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 5 << 4);
1244 m_model
->setSortOrder(Qt::AscendingOrder
);
1245 QCOMPARE(itemsInModel(),
1246 QStringList() << "c"
1256 QCOMPARE(itemsMovedSpy
.count(), 1);
1257 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1258 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1260 // 'Sort Folders First' disabled
1261 m_model
->setSortDirectoriesFirst(false);
1262 QVERIFY(!m_model
->sortDirectoriesFirst());
1263 QCOMPARE(itemsInModel(),
1264 QStringList() << "a"
1274 QCOMPARE(itemsMovedSpy
.count(), 1);
1275 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 10));
1276 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1279 void KFileItemModelTest::testNaturalSorting()
1281 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1282 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1283 QVERIFY(itemsMovedSpy
.isValid());
1285 m_model
->setSortRole("text");
1286 m_model
->setShowHiddenFiles(true);
1288 m_testDir
->createFiles({".a", "a.txt", "b.txt", "a 1.txt", "a 2.txt", "a 10.txt", "a.tar", "b.tar"});
1290 m_model
->loadDirectory(m_testDir
->url());
1291 QVERIFY(itemsInsertedSpy
.wait());
1293 // Sort by Name, ascending, natural sorting enabled
1294 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1295 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1296 QCOMPARE(itemsInModel(),
1297 QStringList() << ".a"
1306 // Sort by Extension
1307 m_model
->setSortRole("extension");
1308 QCOMPARE(m_model
->sortRole(), QByteArray("extension"));
1309 QCOMPARE(itemsInModel(),
1310 QStringList() << ".a"
1318 QCOMPARE(itemsMovedSpy
.count(), 1);
1319 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(2, 5));
1320 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 3 << 4 << 5 << 6 << 2);
1322 // Disable natural sorting, refresh directory for the change to take effect
1323 m_model
->m_naturalSorting
= false;
1324 m_model
->refreshDirectory(m_model
->directory());
1325 QVERIFY(itemsInsertedSpy
.wait());
1327 // Sort by Extension, ascending, natural sorting disabled
1328 QCOMPARE(m_model
->sortRole(), QByteArray("extension"));
1329 QCOMPARE(itemsInModel(),
1330 QStringList() << ".a"
1340 m_model
->setSortRole("text");
1341 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
1342 QCOMPARE(itemsInModel(),
1343 QStringList() << ".a"
1351 QCOMPARE(itemsMovedSpy
.count(), 1);
1352 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(1, 6));
1353 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 6 << 1 << 2 << 3 << 5);
1356 void KFileItemModelTest::testIndexForKeyboardSearch()
1358 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1360 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11", "U", "Ü", "Üu", "Ž"});
1362 m_model
->loadDirectory(m_testDir
->url());
1363 QVERIFY(itemsInsertedSpy
.wait());
1365 // Search from index 0
1366 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
1367 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
1368 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
1369 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
1370 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
1371 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
1372 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
1373 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
1374 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
1375 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
1376 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
1377 QCOMPARE(m_model
->indexForKeyboardSearch("u", 0), 8);
1378 QCOMPARE(m_model
->indexForKeyboardSearch("üu", 0), 10);
1379 QCOMPARE(m_model
->indexForKeyboardSearch("ž", 0), 11);
1381 // Start a search somewhere in the middle
1382 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
1383 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
1384 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
1385 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
1387 // Test searches that go past the last item back to index 0
1388 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
1389 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
1390 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
1391 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
1393 // Test searches that yield no result
1394 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
1395 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
1396 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
1397 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
1398 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
1400 // Test upper case searches (note that search is case insensitive)
1401 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
1402 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
1403 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
1404 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1406 // Test searches that match items with marks
1407 QCOMPARE(m_model
->indexForKeyboardSearch("u", 9), 9);
1408 QCOMPARE(m_model
->indexForKeyboardSearch("u", 10), 10);
1409 QCOMPARE(m_model
->indexForKeyboardSearch("uu", 0), 10);
1410 QCOMPARE(m_model
->indexForKeyboardSearch("z", 0), 11);
1412 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1415 void KFileItemModelTest::testNameFilter()
1417 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1419 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1421 m_model
->loadDirectory(m_testDir
->url());
1422 QVERIFY(itemsInsertedSpy
.wait());
1424 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1425 QCOMPARE(m_model
->count(), 3);
1427 m_model
->setNameFilter("A2"); // Shows only A2
1428 QCOMPARE(m_model
->count(), 1);
1430 m_model
->setNameFilter("A2"); // Shows only A1
1431 QCOMPARE(m_model
->count(), 1);
1433 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1434 QCOMPARE(m_model
->count(), 2);
1436 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1437 QCOMPARE(m_model
->count(), 2);
1439 m_model
->setNameFilter(QString()); // Shows again all items
1440 QCOMPARE(m_model
->count(), 5);
1444 * Verifies that we do not crash when adding a KFileItem with an empty path.
1445 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1446 * tried to always read the first character of the path, even if the path is empty.
1448 void KFileItemModelTest::testEmptyPath()
1450 QSet
<QByteArray
> roles
;
1451 roles
.insert("text");
1452 roles
.insert("isExpanded");
1453 roles
.insert("isExpandable");
1454 roles
.insert("expandedParentsCount");
1455 m_model
->setRoles(roles
);
1457 const QUrl emptyUrl
;
1458 QVERIFY(emptyUrl
.path().isEmpty());
1460 const QUrl
url("file:///test/");
1462 KFileItemList items
;
1463 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1464 m_model
->slotItemsAdded(emptyUrl
, items
);
1465 m_model
->slotCompleted();
1469 * Verifies that the 'isExpanded' state of folders does not change when the
1470 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1472 void KFileItemModelTest::testRefreshExpandedItem()
1474 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1475 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1476 QVERIFY(itemsChangedSpy
.isValid());
1478 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1479 modelRoles
<< "isExpanded"
1481 << "expandedParentsCount";
1482 m_model
->setRoles(modelRoles
);
1484 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1486 m_model
->loadDirectory(m_testDir
->url());
1487 QVERIFY(itemsInsertedSpy
.wait());
1488 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1490 m_model
->setExpanded(0, true);
1491 QVERIFY(itemsInsertedSpy
.wait());
1492 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1493 QVERIFY(m_model
->isExpanded(0));
1495 const KFileItem item
= m_model
->fileItem(0);
1496 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1497 QVERIFY(!itemsChangedSpy
.isEmpty());
1499 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1500 QVERIFY(m_model
->isExpanded(0));
1504 * Verifies that adding an item to an expanded folder that's filtered makes the parental chain visible.
1506 void KFileItemModelTest::testAddItemToFilteredExpandedFolder()
1508 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1509 QSignalSpy
fileItemsChangedSpy(m_model
, &KFileItemModel::fileItemsChanged
);
1511 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1512 modelRoles
<< "isExpanded"
1514 << "expandedParentsCount";
1515 m_model
->setRoles(modelRoles
);
1517 m_testDir
->createFile("a/b/file");
1519 m_model
->loadDirectory(m_testDir
->url());
1520 QVERIFY(itemsInsertedSpy
.wait());
1521 QCOMPARE(m_model
->count(), 1); // "a
1524 m_model
->setExpanded(0, true);
1525 QVERIFY(itemsInsertedSpy
.wait());
1528 m_model
->setExpanded(1, true);
1529 QVERIFY(itemsInsertedSpy
.wait());
1531 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1533 const QUrl urlB
= m_model
->fileItem(1).url();
1535 // Set a filter that matches ".txt" extension
1536 m_model
->setNameFilter("*.txt");
1537 QCOMPARE(m_model
->count(), 0); // Everything got hidden since we don't have a .txt file yet
1539 m_model
->slotItemsAdded(urlB
, KFileItemList() << KFileItem(QUrl("a/b/newItem.txt")));
1540 m_model
->slotCompleted();
1542 // Entire parental chain should now be shown
1543 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/newItem.txt"
1544 QCOMPARE(itemsInModel(),
1545 QStringList() << "a"
1549 // Items should be indented in hierarchy
1550 QCOMPARE(m_model
->expandedParentsCount(0), 0);
1551 QCOMPARE(m_model
->expandedParentsCount(1), 1);
1552 QCOMPARE(m_model
->expandedParentsCount(2), 2);
1556 * Verifies that deleting the last filter-passing child from expanded folders
1557 * makes the parental chain hidden.
1559 void KFileItemModelTest::testDeleteItemsWithExpandedFolderWithFilter()
1561 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1562 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1564 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1565 modelRoles
<< "isExpanded"
1567 << "expandedParentsCount";
1568 m_model
->setRoles(modelRoles
);
1570 m_testDir
->createFile("a/b/file");
1572 m_model
->loadDirectory(m_testDir
->url());
1573 QVERIFY(itemsInsertedSpy
.wait());
1574 QCOMPARE(m_model
->count(), 1); // "a
1577 m_model
->setExpanded(0, true);
1578 QVERIFY(itemsInsertedSpy
.wait());
1581 m_model
->setExpanded(1, true);
1582 QVERIFY(itemsInsertedSpy
.wait());
1584 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1586 // Set a filter that matches "file" extension
1587 m_model
->setNameFilter("file");
1588 QCOMPARE(m_model
->count(), 3); // Everything is still shown
1591 QCOMPARE(itemsRemovedSpy
.count(), 0);
1592 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(2));
1593 QCOMPARE(itemsRemovedSpy
.count(), 1);
1595 // Entire parental chain should now be filtered
1596 QCOMPARE(m_model
->count(), 0);
1597 QCOMPARE(m_model
->m_filteredItems
.size(), 2);
1601 * Verifies that the fileItemsChanged signal is raised with the correct index after renaming files with filter set.
1602 * The rename operation will cause one item to be filtered out and another item to be reordered.
1604 void KFileItemModelTest::testRefreshItemsWithFilter()
1606 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1607 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1608 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1609 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1611 // Creates three .txt files
1612 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt"});
1614 m_model
->loadDirectory(m_testDir
->url());
1615 QVERIFY(itemsInsertedSpy
.wait());
1617 QCOMPARE(m_model
->count(), 3); // "b.txt", "c.txt", "d.txt"
1619 // Set a filter that matches ".txt" extension
1620 m_model
->setNameFilter("*.txt");
1621 QCOMPARE(m_model
->count(), 3); // Still all items are shown
1622 QCOMPARE(itemsInModel(),
1623 QStringList() << "b.txt"
1627 // Objects used to rename
1628 const KFileItem fileItemC_txt
= m_model
->fileItem(1);
1629 KFileItem fileItemC_cfg
= fileItemC_txt
;
1630 fileItemC_cfg
.setUrl(QUrl("c.cfg"));
1632 const KFileItem fileItemD_txt
= m_model
->fileItem(2);
1633 KFileItem fileItemA_txt
= fileItemD_txt
;
1634 fileItemA_txt
.setUrl(QUrl("a.txt"));
1636 // Rename "c.txt" to "c.cfg"; and rename "d.txt" to "a.txt"
1637 QCOMPARE(itemsRemovedSpy
.count(), 0);
1638 QCOMPARE(itemsChangedSpy
.count(), 0);
1639 m_model
->slotRefreshItems({qMakePair(fileItemC_txt
, fileItemC_cfg
), qMakePair(fileItemD_txt
, fileItemA_txt
)});
1640 QCOMPARE(itemsRemovedSpy
.count(), 1);
1641 QCOMPARE(itemsChangedSpy
.count(), 1);
1642 QCOMPARE(m_model
->count(), 2); // Only "a.txt" and "b.txt". "c.cfg" got filtered out
1644 QList
<QVariant
> arguments
= itemsChangedSpy
.takeLast();
1645 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1647 // We started with the order "b.txt", "c.txt", "d.txt"
1648 // "d.txt" started with index "2"
1649 // "c.txt" got renamed and got filtered out
1650 // "d.txt" index shifted from index "2" to "1"
1651 // So we expect index "1" in this argument, meaning "d.txt" was renamed
1652 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1654 // Re-sorting is done asynchronously:
1655 QCOMPARE(itemsInModel(),
1656 QStringList() << "b.txt"
1657 << "a.txt"); // Files should still be in the incorrect order
1658 QVERIFY(itemsMovedSpy
.wait());
1659 QCOMPARE(itemsInModel(),
1660 QStringList() << "a.txt"
1661 << "b.txt"); // Files were re-sorted and should now be in the correct order
1665 * Verifies that parental chains are hidden and shown as needed while their children get filtered/unfiltered due to renaming.
1666 * Also verifies that the "isExpanded" and "expandedParentsCount" values are kept for expanded folders that get refreshed.
1668 void KFileItemModelTest::testRefreshExpandedFolderWithFilter()
1670 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1671 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1673 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1674 modelRoles
<< "isExpanded"
1676 << "expandedParentsCount";
1677 m_model
->setRoles(modelRoles
);
1679 m_testDir
->createFile("a/b/someFolder/someFile");
1681 m_model
->loadDirectory(m_testDir
->url());
1682 QVERIFY(itemsInsertedSpy
.wait());
1684 QCOMPARE(m_model
->count(), 1); // Only "a/"
1687 m_model
->setExpanded(0, true);
1688 QVERIFY(itemsInsertedSpy
.wait());
1691 m_model
->setExpanded(1, true);
1692 QVERIFY(itemsInsertedSpy
.wait());
1694 // Expand "a/b/someFolder/".
1695 m_model
->setExpanded(2, true);
1696 QVERIFY(itemsInsertedSpy
.wait());
1697 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/b/", "a/b/someFolder", "a/b/someFolder/someFile"
1699 // Set a filter that matches the expanded folder "someFolder"
1700 m_model
->setNameFilter("someFolder");
1701 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/someFolder"
1703 // Objects used to rename
1704 const KFileItem fileItemA
= m_model
->fileItem(0);
1705 KFileItem fileItemARenamed
= fileItemA
;
1706 fileItemARenamed
.setUrl(QUrl("a_renamed"));
1708 const KFileItem fileItemSomeFolder
= m_model
->fileItem(2);
1709 KFileItem fileItemRenamedFolder
= fileItemSomeFolder
;
1710 fileItemRenamedFolder
.setUrl(QUrl("/a_renamed/b/renamedFolder"));
1712 // Rename "a" to "a_renamed"
1713 // This way we test if the algorithm is sane as to NOT hide "a_renamed" since it will have visible children
1714 m_model
->slotRefreshItems({qMakePair(fileItemA
, fileItemARenamed
)});
1715 QCOMPARE(m_model
->count(), 3); // Entire parental chain must still be shown
1716 QCOMPARE(itemsInModel(),
1717 QStringList() << "a_renamed"
1721 // Rename "a_renamed" back to "a"; and "someFolder" to "renamedFolder"
1722 m_model
->slotRefreshItems({qMakePair(fileItemARenamed
, fileItemA
), qMakePair(fileItemSomeFolder
, fileItemRenamedFolder
)});
1723 QCOMPARE(m_model
->count(), 0); // Entire parental chain became hidden
1725 // Rename "renamedFolder" back to "someFolder". Filter is passing again
1726 m_model
->slotRefreshItems({qMakePair(fileItemRenamedFolder
, fileItemSomeFolder
)});
1727 QCOMPARE(m_model
->count(), 3); // Entire parental chain is shown again
1728 QCOMPARE(itemsInModel(),
1729 QStringList() << "a"
1733 // slotRefreshItems() should preserve "isExpanded" and "expandedParentsCount" values explicitly in this case
1734 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("isExpanded").toBool(), true);
1735 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("expandedParentsCount"), 2);
1739 * Verify that removing hidden files and folders from the model does not
1740 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1742 void KFileItemModelTest::testRemoveHiddenItems()
1744 m_testDir
->createDir(".a");
1745 m_testDir
->createDir(".b");
1746 m_testDir
->createDir("c");
1747 m_testDir
->createDir("d");
1748 m_testDir
->createFiles({".f", ".g", "h", "i"});
1750 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1751 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1753 m_model
->setShowHiddenFiles(true);
1754 m_model
->loadDirectory(m_testDir
->url());
1755 QVERIFY(itemsInsertedSpy
.wait());
1756 QCOMPARE(itemsInModel(),
1757 QStringList() << ".a"
1765 QCOMPARE(itemsInsertedSpy
.count(), 1);
1766 QCOMPARE(itemsRemovedSpy
.count(), 0);
1767 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1768 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1770 m_model
->setShowHiddenFiles(false);
1771 QCOMPARE(itemsInModel(),
1772 QStringList() << "c"
1776 QCOMPARE(itemsInsertedSpy
.count(), 0);
1777 QCOMPARE(itemsRemovedSpy
.count(), 1);
1778 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1779 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1781 m_model
->setShowHiddenFiles(true);
1782 QCOMPARE(itemsInModel(),
1783 QStringList() << ".a"
1791 QCOMPARE(itemsInsertedSpy
.count(), 1);
1792 QCOMPARE(itemsRemovedSpy
.count(), 0);
1793 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1794 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1797 QCOMPARE(itemsInModel(), QStringList());
1798 QCOMPARE(itemsInsertedSpy
.count(), 0);
1799 QCOMPARE(itemsRemovedSpy
.count(), 1);
1800 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1801 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1803 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1804 // Verify that this does not make the model crash.
1805 m_model
->setShowHiddenFiles(false);
1809 * Verify that filtered items are removed when their parent is collapsed.
1811 void KFileItemModelTest::collapseParentOfHiddenItems()
1813 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1814 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1816 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1817 modelRoles
<< "isExpanded"
1819 << "expandedParentsCount";
1820 m_model
->setRoles(modelRoles
);
1822 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1824 m_model
->loadDirectory(m_testDir
->url());
1825 QVERIFY(itemsInsertedSpy
.wait());
1826 QCOMPARE(m_model
->count(), 1); // Only "a/"
1829 m_model
->setExpanded(0, true);
1830 QVERIFY(itemsInsertedSpy
.wait());
1831 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1834 m_model
->setExpanded(1, true);
1835 QVERIFY(itemsInsertedSpy
.wait());
1836 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1839 m_model
->setExpanded(2, true);
1840 QVERIFY(itemsInsertedSpy
.wait());
1841 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"
1843 // Set a name filter that matches nothing -> nothing should remain.
1844 m_model
->setNameFilter("xyz");
1845 QCOMPARE(itemsRemovedSpy
.count(), 1);
1846 QCOMPARE(m_model
->count(), 0); // Everything is hidden
1847 QCOMPARE(itemsInModel(), QStringList());
1849 // Filter by the file names. Folder "d" will be hidden since it was collapsed
1850 m_model
->setNameFilter("1");
1851 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1852 QCOMPARE(m_model
->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1854 // Collapse the folder "a/".
1855 m_model
->setExpanded(0, false);
1856 QCOMPARE(itemsRemovedSpy
.count(), 2);
1857 QCOMPARE(m_model
->count(), 1);
1858 QCOMPARE(itemsInModel(), QStringList() << "a");
1860 // Remove the filter -> "a" should still appear (and we should not get a crash).
1861 m_model
->setNameFilter(QString());
1862 QCOMPARE(itemsRemovedSpy
.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1863 QCOMPARE(m_model
->count(), 1);
1864 QCOMPARE(itemsInModel(), QStringList() << "a");
1868 * Verify that filtered items are removed when their parent is deleted.
1870 void KFileItemModelTest::removeParentOfHiddenItems()
1872 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1873 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1875 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1876 modelRoles
<< "isExpanded"
1878 << "expandedParentsCount";
1879 m_model
->setRoles(modelRoles
);
1881 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1883 m_model
->loadDirectory(m_testDir
->url());
1884 QVERIFY(itemsInsertedSpy
.wait());
1885 QCOMPARE(m_model
->count(), 1); // Only "a/"
1888 m_model
->setExpanded(0, true);
1889 QVERIFY(itemsInsertedSpy
.wait());
1890 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1893 m_model
->setExpanded(1, true);
1894 QVERIFY(itemsInsertedSpy
.wait());
1895 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1898 m_model
->setExpanded(2, true);
1899 QVERIFY(itemsInsertedSpy
.wait());
1900 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"
1902 // Set a name filter that matches nothing -> nothing should remain.
1903 m_model
->setNameFilter("xyz");
1904 QCOMPARE(itemsRemovedSpy
.count(), 1);
1905 QCOMPARE(m_model
->count(), 0);
1906 QCOMPARE(itemsInModel(), QStringList());
1908 // Filter by "c". Folder "b" will also be shown because it is its parent.
1909 m_model
->setNameFilter("c");
1910 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1911 QCOMPARE(m_model
->count(), 3);
1912 QCOMPARE(itemsInModel(),
1913 QStringList() << "a"
1917 // Simulate the deletion of the directory "a/b/".
1918 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1919 QCOMPARE(itemsRemovedSpy
.count(), 2);
1920 QCOMPARE(m_model
->count(), 0); // "a" will be filtered out since it doesn't pass the filter and doesn't have visible children
1922 // Remove the filter -> only the file "a/1" should appear.
1923 m_model
->setNameFilter(QString());
1924 QCOMPARE(m_model
->count(), 2);
1925 QCOMPARE(itemsInModel(),
1926 QStringList() << "a"
1931 * Create a tree structure where parent-child relationships can not be
1932 * determined by parsing the URLs, and verify that KFileItemModel
1933 * handles them correctly.
1935 void KFileItemModelTest::testGeneralParentChildRelationships()
1937 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1938 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1940 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1941 modelRoles
<< "isExpanded"
1943 << "expandedParentsCount";
1944 m_model
->setRoles(modelRoles
);
1946 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1948 m_model
->loadDirectory(m_testDir
->url());
1949 QVERIFY(itemsInsertedSpy
.wait());
1950 QCOMPARE(itemsInModel(),
1951 QStringList() << "parent1"
1954 // Expand all folders.
1955 m_model
->setExpanded(0, true);
1956 QVERIFY(itemsInsertedSpy
.wait());
1957 QCOMPARE(itemsInModel(),
1958 QStringList() << "parent1"
1962 m_model
->setExpanded(1, true);
1963 QVERIFY(itemsInsertedSpy
.wait());
1964 QCOMPARE(itemsInModel(),
1965 QStringList() << "parent1"
1967 << "realGrandChild1"
1970 m_model
->setExpanded(3, true);
1971 QVERIFY(itemsInsertedSpy
.wait());
1972 QCOMPARE(itemsInModel(),
1973 QStringList() << "parent1"
1975 << "realGrandChild1"
1979 m_model
->setExpanded(4, true);
1980 QVERIFY(itemsInsertedSpy
.wait());
1981 QCOMPARE(itemsInModel(),
1982 QStringList() << "parent1"
1984 << "realGrandChild1"
1987 << "realGrandChild2");
1989 // Add some more children and grand-children.
1990 const QUrl parent1
= m_model
->fileItem(0).url();
1991 const QUrl parent2
= m_model
->fileItem(3).url();
1992 const QUrl realChild1
= m_model
->fileItem(1).url();
1993 const QUrl realChild2
= m_model
->fileItem(4).url();
1995 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1996 m_model
->slotCompleted();
1997 QCOMPARE(itemsInModel(),
1998 QStringList() << "parent1"
2000 << "realGrandChild1"
2004 << "realGrandChild2");
2006 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
2007 m_model
->slotCompleted();
2008 QCOMPARE(itemsInModel(),
2009 QStringList() << "parent1"
2011 << "realGrandChild1"
2015 << "realGrandChild2"
2018 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
2019 m_model
->slotCompleted();
2020 QCOMPARE(itemsInModel(),
2021 QStringList() << "parent1"
2024 << "realGrandChild1"
2028 << "realGrandChild2"
2031 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
2032 m_model
->slotCompleted();
2033 QCOMPARE(itemsInModel(),
2034 QStringList() << "parent1"
2037 << "realGrandChild1"
2042 << "realGrandChild2"
2045 // Set a name filter that matches nothing -> nothing will remain.
2046 m_model
->setNameFilter("xyz");
2047 QCOMPARE(itemsInModel(), QStringList());
2048 QCOMPARE(itemsRemovedSpy
.count(), 1);
2049 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
2050 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
2051 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 10));
2053 // Set a name filter that matches only "realChild". Their prarents should still show.
2054 m_model
->setNameFilter("realChild");
2055 QCOMPARE(itemsInModel(),
2056 QStringList() << "parent1"
2060 QCOMPARE(itemsRemovedSpy
.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
2062 // Collapse "parent1".
2063 m_model
->setExpanded(0, false);
2064 QCOMPARE(itemsInModel(),
2065 QStringList() << "parent1"
2068 QCOMPARE(itemsRemovedSpy
.count(), 1);
2069 arguments
= itemsRemovedSpy
.takeFirst();
2070 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
2071 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
2073 // Remove "parent2".
2074 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
2075 QCOMPARE(itemsInModel(), QStringList() << "parent1");
2076 QCOMPARE(itemsRemovedSpy
.count(), 1);
2077 arguments
= itemsRemovedSpy
.takeFirst();
2078 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
2079 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
2081 // Clear filter, verify that no items reappear.
2082 m_model
->setNameFilter(QString());
2083 QCOMPARE(itemsInModel(), QStringList() << "parent1");
2086 void KFileItemModelTest::testNameRoleGroups()
2088 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2089 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
2090 QVERIFY(itemsMovedSpy
.isValid());
2091 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
2092 QVERIFY(groupsChangedSpy
.isValid());
2094 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
2096 m_model
->setGroupedSorting(true);
2097 m_model
->setGroupRole("text");
2098 m_model
->loadDirectory(m_testDir
->url());
2099 QVERIFY(itemsInsertedSpy
.wait());
2100 QCOMPARE(itemsInModel(),
2101 QStringList() << "b.txt"
2106 QList
<QPair
<int, QVariant
>> expectedGroups
;
2107 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
2108 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
2109 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
2110 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2111 QCOMPARE(m_model
->groups(), expectedGroups
);
2113 // Rename d.txt to a.txt.
2114 QHash
<QByteArray
, QVariant
> data
;
2115 data
.insert("text", "a.txt");
2116 m_model
->setData(2, data
);
2117 QVERIFY(itemsMovedSpy
.wait());
2118 QCOMPARE(itemsInModel(),
2119 QStringList() << "a.txt"
2124 expectedGroups
.clear();
2125 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2126 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
2127 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
2128 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2129 QCOMPARE(m_model
->groups(), expectedGroups
);
2131 // Rename c.txt to d.txt.
2132 data
.insert("text", "d.txt");
2133 m_model
->setData(2, data
);
2134 QVERIFY(groupsChangedSpy
.wait());
2135 QCOMPARE(itemsInModel(),
2136 QStringList() << "a.txt"
2141 expectedGroups
.clear();
2142 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2143 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
2144 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
2145 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2146 QCOMPARE(m_model
->groups(), expectedGroups
);
2148 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
2149 const KFileItem fileItemD
= m_model
->fileItem(2);
2150 KFileItem fileItemC
= fileItemD
;
2151 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
2152 urlC
.setPath(urlC
.path() + "c.txt");
2153 fileItemC
.setUrl(urlC
);
2155 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
2156 QVERIFY(groupsChangedSpy
.wait());
2157 QCOMPARE(itemsInModel(),
2158 QStringList() << "a.txt"
2163 expectedGroups
.clear();
2164 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2165 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
2166 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
2167 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
2168 QCOMPARE(m_model
->groups(), expectedGroups
);
2171 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
2173 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2175 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2176 modelRoles
<< "isExpanded"
2178 << "expandedParentsCount";
2179 m_model
->setRoles(modelRoles
);
2181 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
2183 m_model
->setGroupedSorting(true);
2184 m_model
->setGroupRole("text");
2186 m_model
->loadDirectory(m_testDir
->url());
2187 QVERIFY(itemsInsertedSpy
.wait());
2188 QCOMPARE(itemsInModel(),
2189 QStringList() << "a"
2192 QList
<QPair
<int, QVariant
>> expectedGroups
;
2193 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2194 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
2196 QCOMPARE(m_model
->groups(), expectedGroups
);
2198 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
2199 expectedGroups
.clear();
2200 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2201 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
2203 m_model
->setExpanded(0, true);
2204 QVERIFY(m_model
->isExpanded(0));
2205 QVERIFY(itemsInsertedSpy
.wait());
2206 QCOMPARE(itemsInModel(),
2207 QStringList() << "a"
2211 QCOMPARE(m_model
->groups(), expectedGroups
);
2213 m_model
->setExpanded(3, true);
2214 QVERIFY(m_model
->isExpanded(3));
2215 QVERIFY(itemsInsertedSpy
.wait());
2216 QCOMPARE(itemsInModel(),
2217 QStringList() << "a"
2223 QCOMPARE(m_model
->groups(), expectedGroups
);
2226 void KFileItemModelTest::testInconsistentModel()
2228 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2230 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2231 modelRoles
<< "isExpanded"
2233 << "expandedParentsCount";
2234 m_model
->setRoles(modelRoles
);
2236 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
2238 m_model
->loadDirectory(m_testDir
->url());
2239 QVERIFY(itemsInsertedSpy
.wait());
2240 QCOMPARE(itemsInModel(), QStringList() << "a");
2242 // Expand "a/" and "a/b/".
2243 m_model
->setExpanded(0, true);
2244 QVERIFY(m_model
->isExpanded(0));
2245 QVERIFY(itemsInsertedSpy
.wait());
2246 QCOMPARE(itemsInModel(),
2247 QStringList() << "a"
2250 m_model
->setExpanded(1, true);
2251 QVERIFY(m_model
->isExpanded(1));
2252 QVERIFY(itemsInsertedSpy
.wait());
2253 QCOMPARE(itemsInModel(),
2254 QStringList() << "a"
2259 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
2260 // Such a thing can in principle happen when performing a search, and there
2262 // (a) match the search string, and
2263 // (b) are children of a folder that matches the search string and is expanded.
2265 // Note that the first item in the list of added items must be new (i.e., not
2266 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
2267 // it receives items that are in the model already and ignore them.
2268 QUrl
url(m_model
->directory().url() + "/a2");
2269 KFileItem
newItem(url
);
2271 KFileItemList items
;
2272 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
2273 m_model
->slotItemsAdded(m_model
->directory(), items
);
2274 m_model
->slotCompleted();
2275 QCOMPARE(itemsInModel(),
2276 QStringList() << "a"
2284 m_model
->setExpanded(0, false);
2286 // Test that the right items have been removed, see
2287 // https://bugs.kde.org/show_bug.cgi?id=324371
2288 QCOMPARE(itemsInModel(),
2289 QStringList() << "a"
2294 // Test that resorting does not cause a crash, see
2295 // https://bugs.kde.org/show_bug.cgi?id=325359
2296 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
2297 m_model
->resortAllItems();
2300 void KFileItemModelTest::testChangeRolesForFilteredItems()
2302 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2304 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2305 modelRoles
<< "owner";
2306 m_model
->setRoles(modelRoles
);
2308 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
2310 m_model
->loadDirectory(m_testDir
->url());
2311 QVERIFY(itemsInsertedSpy
.wait());
2312 QCOMPARE(itemsInModel(),
2313 QStringList() << "a.txt"
2317 for (int index
= 0; index
< m_model
->count(); ++index
) {
2318 // All items should have the "text" and "owner" roles, but not "group".
2319 QVERIFY(m_model
->data(index
).contains("text"));
2320 QVERIFY(m_model
->data(index
).contains("owner"));
2321 QVERIFY(!m_model
->data(index
).contains("group"));
2324 // Add a filter, such that only "aaa.txt" remains in the model.
2325 m_model
->setNameFilter("aaa");
2326 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
2328 // Add the "group" role.
2329 modelRoles
<< "group";
2330 m_model
->setRoles(modelRoles
);
2332 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
2333 m_model
->setNameFilter("aa");
2334 QCOMPARE(itemsInModel(),
2335 QStringList() << "aa.txt"
2338 for (int index
= 0; index
< m_model
->count(); ++index
) {
2339 // All items should have the "text", "owner", and "group" roles.
2340 QVERIFY(m_model
->data(index
).contains("text"));
2341 QVERIFY(m_model
->data(index
).contains("owner"));
2342 QVERIFY(m_model
->data(index
).contains("group"));
2345 // Remove the "owner" role.
2346 modelRoles
.remove("owner");
2347 m_model
->setRoles(modelRoles
);
2349 // Clear the filter, and verify that all items have the expected roles
2350 m_model
->setNameFilter(QString());
2351 QCOMPARE(itemsInModel(),
2352 QStringList() << "a.txt"
2356 for (int index
= 0; index
< m_model
->count(); ++index
) {
2357 // All items should have the "text" and "group" roles, but now "owner".
2358 QVERIFY(m_model
->data(index
).contains("text"));
2359 QVERIFY(!m_model
->data(index
).contains("owner"));
2360 QVERIFY(m_model
->data(index
).contains("group"));
2364 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
2366 KFileItemList items
;
2368 KIO::UDSEntry entry
[3];
2370 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
2371 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
2373 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
2374 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
2376 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
2377 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
2379 for (int i
= 0; i
< 3; ++i
) {
2380 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
2381 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
2382 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
2383 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
2384 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
2385 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
2386 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
2389 m_model
->slotItemsAdded(m_testDir
->url(), items
);
2390 m_model
->slotCompleted();
2392 QCOMPARE(itemsInModel(),
2393 QStringList() << "a.txt"
2398 m_model
->setNameFilter("a");
2399 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
2402 m_model
->setSortRole("owner");
2404 // Clear the filter, and verify that the items are sorted correctly.
2405 m_model
->setNameFilter(QString());
2406 QCOMPARE(itemsInModel(),
2407 QStringList() << "c.txt"
2412 void KFileItemModelTest::testRefreshFilteredItems()
2414 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2416 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
2418 m_model
->loadDirectory(m_testDir
->url());
2419 QVERIFY(itemsInsertedSpy
.wait());
2420 QCOMPARE(itemsInModel(),
2421 QStringList() << "a.txt"
2426 const KFileItem fileItemC
= m_model
->fileItem(2);
2428 // Show only the .txt files.
2429 m_model
->setNameFilter(".txt");
2430 QCOMPARE(itemsInModel(),
2431 QStringList() << "a.txt"
2434 // Rename one of the .jpg files.
2435 KFileItem fileItemE
= fileItemC
;
2436 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
2437 urlE
.setPath(urlE
.path() + "/e.jpg");
2438 fileItemE
.setUrl(urlE
);
2440 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
2442 // Show all files again, and verify that the model has updated the file name.
2443 m_model
->setNameFilter(QString());
2444 QCOMPARE(itemsInModel(),
2445 QStringList() << "a.txt"
2451 void KFileItemModelTest::testCreateMimeData()
2453 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2455 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2456 modelRoles
<< "isExpanded"
2458 << "expandedParentsCount";
2459 m_model
->setRoles(modelRoles
);
2461 m_testDir
->createFile("a/1");
2463 m_model
->loadDirectory(m_testDir
->url());
2464 QVERIFY(itemsInsertedSpy
.wait());
2465 QCOMPARE(itemsInModel(), QStringList() << "a");
2468 m_model
->setExpanded(0, true);
2469 QVERIFY(itemsInsertedSpy
.wait());
2470 QCOMPARE(itemsInModel(),
2471 QStringList() << "a"
2474 // Verify that creating the MIME data for a child of an expanded folder does
2475 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
2477 selection
.insert(1);
2478 QMimeData
*mimeData
= m_model
->createMimeData(selection
);
2482 void KFileItemModelTest::testCollapseFolderWhileLoading()
2484 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2486 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2487 modelRoles
<< "isExpanded"
2489 << "expandedParentsCount";
2490 m_model
->setRoles(modelRoles
);
2492 m_testDir
->createFile("a2/b/c1.txt");
2494 m_model
->loadDirectory(m_testDir
->url());
2495 QVERIFY(itemsInsertedSpy
.wait());
2496 QCOMPARE(itemsInModel(), QStringList() << "a2");
2499 m_model
->setExpanded(0, true);
2500 QVERIFY(m_model
->isExpanded(0));
2501 QVERIFY(itemsInsertedSpy
.wait());
2502 QCOMPARE(itemsInModel(),
2503 QStringList() << "a2"
2507 m_model
->setExpanded(1, true);
2508 QVERIFY(m_model
->isExpanded(1));
2509 QVERIFY(itemsInsertedSpy
.wait());
2510 QCOMPARE(itemsInModel(),
2511 QStringList() << "a2"
2515 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
2516 // signal is not emitted yet.
2517 const KFileItem fileItemC1
= m_model
->fileItem(2);
2518 KFileItem fileItemC2
= fileItemC1
;
2519 QUrl urlC2
= fileItemC2
.url();
2520 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
2521 urlC2
.setPath(urlC2
.path() + "c2.txt");
2522 fileItemC2
.setUrl(urlC2
);
2524 const QUrl urlB
= m_model
->fileItem(1).url();
2525 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
2526 QCOMPARE(itemsInModel(),
2527 QStringList() << "a2"
2531 // Collapse "a2/". This should also remove all its (indirect) children from
2532 // the model and from the model's m_pendingItemsToInsert member.
2533 m_model
->setExpanded(0, false);
2534 QCOMPARE(itemsInModel(), QStringList() << "a2");
2536 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
2537 // is still in m_pendingItemsToInsert, then we might get a crash, see
2538 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
2539 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
2540 // without parent in the model.
2541 m_model
->slotCompleted();
2542 QCOMPARE(itemsInModel(), QStringList() << "a2");
2544 // Expand "a2/" again.
2545 m_model
->setExpanded(0, true);
2546 QVERIFY(m_model
->isExpanded(0));
2547 QVERIFY(itemsInsertedSpy
.wait());
2548 QCOMPARE(itemsInModel(),
2549 QStringList() << "a2"
2552 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
2553 // completed() signal is not emitted yet.
2554 const KFileItem fileItemA2
= m_model
->fileItem(0);
2555 KFileItem fileItemA1
= fileItemA2
;
2556 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
2557 urlA1
.setPath(urlA1
.path() + "a1");
2558 fileItemA1
.setUrl(urlA1
);
2560 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
2561 QCOMPARE(itemsInModel(),
2562 QStringList() << "a2"
2565 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
2566 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
2567 // confuse the code which collapses the folder.
2568 m_model
->setExpanded(0, false);
2569 QCOMPARE(itemsInModel(),
2570 QStringList() << "a1"
2572 QVERIFY(!m_model
->isExpanded(0));
2573 QVERIFY(!m_model
->isExpanded(1));
2576 void KFileItemModelTest::testDeleteFileMoreThanOnce()
2578 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2580 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
2582 m_model
->loadDirectory(m_testDir
->url());
2583 QVERIFY(itemsInsertedSpy
.wait());
2584 QCOMPARE(itemsInModel(),
2585 QStringList() << "a.txt"
2590 const KFileItem fileItemB
= m_model
->fileItem(1);
2592 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
2594 list
<< fileItemB
<< fileItemB
;
2595 m_model
->slotItemsDeleted(list
);
2597 QVERIFY(m_model
->isConsistent());
2598 QCOMPARE(itemsInModel(),
2599 QStringList() << "a.txt"
2604 void KFileItemModelTest::testInsertAfterExpand()
2606 m_model
->m_dirLister
->setAutoUpdate(true);
2608 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2609 QVERIFY(itemsInsertedSpy
.isValid());
2610 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
2611 QVERIFY(itemsRemovedSpy
.isValid());
2612 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2613 QVERIFY(loadingCompletedSpy
.isValid());
2615 // Test expanding subfolders in a folder with the items "a/", "a/a/"
2616 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
2617 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
2618 modelRoles
<< "isExpanded"
2620 << "expandedParentsCount";
2621 m_model
->setRoles(modelRoles
);
2623 m_testDir
->createFile("a/b/1");
2625 m_model
->loadDirectory(m_testDir
->url());
2626 QVERIFY(itemsInsertedSpy
.wait());
2628 // So far, the model contains only "a/"
2629 QCOMPARE(m_model
->count(), 1);
2630 QVERIFY(m_model
->isExpandable(0));
2631 QVERIFY(!m_model
->isExpanded(0));
2632 QVERIFY(m_model
->expandedDirectories().empty());
2634 QCOMPARE(itemsInsertedSpy
.count(), 1);
2636 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2637 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
2638 QCOMPARE(m_model
->expandedParentsCount(0), 0);
2640 itemsInsertedSpy
.clear();
2642 // Expand the folder "a/" -> "a/b" become visible
2643 m_model
->setExpanded(0, true);
2644 QVERIFY(m_model
->isExpanded(0));
2645 QVERIFY(itemsInsertedSpy
.wait());
2646 QCOMPARE(m_model
->count(), 2); // 3 items: "a/", "a/a/"
2647 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>({QUrl::fromLocalFile(m_testDir
->path() + "/a")}));
2649 QCOMPARE(itemsInsertedSpy
.count(), 1);
2651 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2652 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1)); // 1 new item "a/b" with index 1
2653 QCOMPARE(m_model
->expandedParentsCount(1), 1);
2655 itemsInsertedSpy
.clear();
2657 // Expand "a/b" -> "a/b/1" becomes visible
2658 m_model
->setExpanded(1, true);
2659 QVERIFY(itemsInsertedSpy
.wait());
2660 QCOMPARE(m_model
->expandedDirectories(), QSet({QUrl::fromLocalFile(m_testDir
->path() + "/a"), QUrl::fromLocalFile(m_testDir
->path() + "/a/b")}));
2662 QCOMPARE(itemsInsertedSpy
.count(), 1);
2664 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2665 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/b/1" with index 2
2666 QCOMPARE(m_model
->expandedParentsCount(2), 2);
2668 itemsInsertedSpy
.clear();
2670 // Collapse "a" whilst leaving "b" expanded
2671 m_model
->setExpanded(0, false);
2673 // Insert additional files into "a/b/"
2674 m_testDir
->createFile("a/b/2");
2676 QVERIFY(!itemsInsertedSpy
.wait(5000));
2678 QCOMPARE(itemsInModel(), {"a"});
2680 m_model
->setExpanded(0, true);
2682 QTRY_COMPARE(itemsInModel(), QStringList({"a", "b", "1", "2"}));
2684 QCOMPARE(m_model
->expandedParentsCount(0), 0); // a
2685 QCOMPARE(m_model
->expandedParentsCount(1), 1); // a/b
2686 QCOMPARE(m_model
->expandedParentsCount(2), 2); // a/b/1
2687 QCOMPARE(m_model
->expandedParentsCount(3), 2); // a/b/2
2690 void KFileItemModelTest::testCurrentDirRemoved()
2692 m_model
->m_dirLister
->setAutoUpdate(true);
2693 QSignalSpy
currentDirectoryRemovedSpy(m_model
, &KFileItemModel::currentDirectoryRemoved
);
2694 QVERIFY(currentDirectoryRemovedSpy
.isValid());
2695 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2696 QVERIFY(loadingCompletedSpy
.isValid());
2697 QSignalSpy
dirListerClearSpy(m_model
->m_dirLister
, &KCoreDirLister::clear
);
2698 QVERIFY(dirListerClearSpy
.isValid());
2700 m_testDir
->createFiles({"dir/a.txt", "dir/b.txt"});
2701 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/dir/"));
2702 QVERIFY(loadingCompletedSpy
.wait());
2703 QCOMPARE(m_model
->count(), 2);
2704 QVERIFY(m_model
->isConsistent());
2706 m_testDir
->removeDir("dir");
2707 QVERIFY(currentDirectoryRemovedSpy
.wait());
2709 // dirLister calls clear
2710 QCOMPARE(dirListerClearSpy
.count(), 2);
2711 QVERIFY(m_model
->isConsistent());
2712 QVERIFY(m_model
->m_itemData
.isEmpty());
2713 QCOMPARE(m_model
->count(), 0);
2716 QStringList
KFileItemModelTest::itemsInModel() const
2719 for (int i
= 0; i
< m_model
->count(); i
++) {
2720 items
<< m_model
->fileItem(i
).text();
2725 QTEST_MAIN(KFileItemModelTest
)
2727 #include "kfileitemmodeltest.moc"