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>
18 #include "kitemviews/kfileitemmodel.h"
21 void myMessageOutput(QtMsgType type
, const QMessageLogContext
&context
, const QString
&msg
)
31 fprintf(stderr
, "Critical: %s\n", msg
.toLocal8Bit().data());
34 fprintf(stderr
, "Fatal: %s\n", msg
.toLocal8Bit().data());
41 Q_DECLARE_METATYPE(KItemRange
)
42 Q_DECLARE_METATYPE(KItemRangeList
)
43 Q_DECLARE_METATYPE(QList
<int>)
45 class KFileItemModelTest
: public QObject
54 void testDefaultRoles();
55 void testDefaultSortRole();
56 void testDefaultGroupedSorting();
58 void testRemoveItems();
59 void testDirLoadingCompleted();
61 void testSetDataWithModifiedSortRole_data();
62 void testSetDataWithModifiedSortRole();
63 void testChangeSortRole();
64 void testResortAfterChangingName();
65 void testModelConsistencyWhenInsertingItems();
66 void testItemRangeConsistencyWhenInsertingItems();
67 void testExpandItems();
68 void testExpandParentItems();
69 void testMakeExpandedItemHidden();
70 void testRemoveFilteredExpandedItems();
72 void testIndexForKeyboardSearch();
73 void testNameFilter();
75 void testRefreshExpandedItem();
76 void testAddItemToFilteredExpandedFolder();
77 void testDeleteItemsWithExpandedFolderWithFilter();
78 void testRefreshItemsWithFilter();
79 void testRefreshExpandedFolderWithFilter();
80 void testRemoveHiddenItems();
81 void collapseParentOfHiddenItems();
82 void removeParentOfHiddenItems();
83 void testGeneralParentChildRelationships();
84 void testNameRoleGroups();
85 void testNameRoleGroupsWithExpandedItems();
86 void testInconsistentModel();
87 void testChangeRolesForFilteredItems();
88 void testChangeSortRoleWhileFiltering();
89 void testRefreshFilteredItems();
90 void testCollapseFolderWhileLoading();
91 void testCreateMimeData();
92 void testDeleteFileMoreThanOnce();
93 void testInsertAfterExpand();
94 void testCurrentDirRemoved();
97 QStringList
itemsInModel() const;
100 KFileItemModel
*m_model
;
104 void KFileItemModelTest::initTestCase()
106 QStandardPaths::setTestModeEnabled(true);
109 void KFileItemModelTest::init()
111 // The item-model tests result in a huge number of debugging
112 // output from kdelibs. Only show critical and fatal messages.
113 qInstallMessageHandler(myMessageOutput
);
115 qRegisterMetaType
<KItemRange
>("KItemRange");
116 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
117 qRegisterMetaType
<KFileItemList
>("KFileItemList");
119 m_testDir
= new TestDir();
120 m_model
= new KFileItemModel();
121 m_model
->m_dirLister
->setAutoUpdate(false);
123 // Reduce the timer interval to make the test run faster.
124 m_model
->m_resortAllItemsTimer
->setInterval(0);
127 void KFileItemModelTest::cleanup()
136 void KFileItemModelTest::testDefaultRoles()
138 const QSet
<QByteArray
> roles
= m_model
->roles();
139 QCOMPARE(roles
.count(), 4);
140 QVERIFY(roles
.contains("text"));
141 QVERIFY(roles
.contains("isDir"));
142 QVERIFY(roles
.contains("isLink"));
143 QVERIFY(roles
.contains("isHidden"));
146 void KFileItemModelTest::testDefaultSortRole()
148 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
149 QVERIFY(itemsInsertedSpy
.isValid());
151 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
153 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
155 m_model
->loadDirectory(m_testDir
->url());
156 QVERIFY(itemsInsertedSpy
.wait());
158 QCOMPARE(m_model
->count(), 3);
159 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
160 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
161 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
164 void KFileItemModelTest::testDefaultGroupedSorting()
166 QCOMPARE(m_model
->groupedSorting(), false);
169 void KFileItemModelTest::testNewItems()
171 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
173 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
175 m_model
->loadDirectory(m_testDir
->url());
176 QVERIFY(itemsInsertedSpy
.wait());
178 QCOMPARE(m_model
->count(), 3);
180 QVERIFY(m_model
->isConsistent());
183 void KFileItemModelTest::testRemoveItems()
185 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
186 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
188 m_testDir
->createFiles({"a.txt", "b.txt"});
189 m_model
->loadDirectory(m_testDir
->url());
190 QVERIFY(itemsInsertedSpy
.wait());
191 QCOMPARE(m_model
->count(), 2);
192 QVERIFY(m_model
->isConsistent());
194 m_testDir
->removeFile("a.txt");
195 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
196 QVERIFY(itemsRemovedSpy
.wait());
197 QCOMPARE(m_model
->count(), 1);
198 QVERIFY(m_model
->isConsistent());
201 void KFileItemModelTest::testDirLoadingCompleted()
203 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
204 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
205 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
207 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
209 m_model
->loadDirectory(m_testDir
->url());
210 QVERIFY(loadingCompletedSpy
.wait());
211 QCOMPARE(loadingCompletedSpy
.count(), 1);
212 QCOMPARE(itemsInsertedSpy
.count(), 1);
213 QCOMPARE(itemsRemovedSpy
.count(), 0);
214 QCOMPARE(m_model
->count(), 3);
216 m_testDir
->createFiles({"d.txt", "e.txt"});
217 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
218 QVERIFY(loadingCompletedSpy
.wait());
219 QCOMPARE(loadingCompletedSpy
.count(), 2);
220 QCOMPARE(itemsInsertedSpy
.count(), 2);
221 QCOMPARE(itemsRemovedSpy
.count(), 0);
222 QCOMPARE(m_model
->count(), 5);
224 m_testDir
->removeFile("a.txt");
225 m_testDir
->createFile("f.txt");
226 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
227 QVERIFY(loadingCompletedSpy
.wait());
228 QCOMPARE(loadingCompletedSpy
.count(), 3);
229 QCOMPARE(itemsInsertedSpy
.count(), 3);
230 QCOMPARE(itemsRemovedSpy
.count(), 1);
231 QCOMPARE(m_model
->count(), 5);
233 m_testDir
->removeFile("b.txt");
234 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
235 QVERIFY(itemsRemovedSpy
.wait());
236 QCOMPARE(loadingCompletedSpy
.count(), 4);
237 QCOMPARE(itemsInsertedSpy
.count(), 3);
238 QCOMPARE(itemsRemovedSpy
.count(), 2);
239 QCOMPARE(m_model
->count(), 4);
241 QVERIFY(m_model
->isConsistent());
244 void KFileItemModelTest::testSetData()
246 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
247 QVERIFY(itemsInsertedSpy
.isValid());
248 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
249 QVERIFY(itemsChangedSpy
.isValid());
251 m_testDir
->createFile("a.txt");
253 m_model
->loadDirectory(m_testDir
->url());
254 QVERIFY(itemsInsertedSpy
.wait());
256 QHash
<QByteArray
, QVariant
> values
;
257 values
.insert("customRole1", "Test1");
258 values
.insert("customRole2", "Test2");
260 m_model
->setData(0, values
);
261 QCOMPARE(itemsChangedSpy
.count(), 1);
263 values
= m_model
->data(0);
264 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
265 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
266 QVERIFY(m_model
->isConsistent());
269 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
271 QTest::addColumn
<int>("changedIndex");
272 QTest::addColumn
<int>("changedRating");
273 QTest::addColumn
<bool>("expectMoveSignal");
274 QTest::addColumn
<int>("ratingIndex0");
275 QTest::addColumn
<int>("ratingIndex1");
276 QTest::addColumn
<int>("ratingIndex2");
279 // Index 0 = rating 2
280 // Index 1 = rating 4
281 // Index 2 = rating 6
283 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
284 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
285 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
287 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
288 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
289 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
292 void KFileItemModelTest::testSetDataWithModifiedSortRole()
294 QFETCH(int, changedIndex
);
295 QFETCH(int, changedRating
);
296 QFETCH(bool, expectMoveSignal
);
297 QFETCH(int, ratingIndex0
);
298 QFETCH(int, ratingIndex1
);
299 QFETCH(int, ratingIndex2
);
301 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
302 QVERIFY(itemsInsertedSpy
.isValid());
303 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
304 QVERIFY(itemsMovedSpy
.isValid());
306 // Changing the value of a sort-role must result in
307 // a reordering of the items.
308 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
309 m_model
->setSortRole("rating");
310 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
312 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
314 m_model
->loadDirectory(m_testDir
->url());
315 QVERIFY(itemsInsertedSpy
.wait());
317 // Fill the "rating" role of each file:
322 QHash
<QByteArray
, QVariant
> ratingA
;
323 ratingA
.insert("rating", 2);
324 m_model
->setData(0, ratingA
);
326 QHash
<QByteArray
, QVariant
> ratingB
;
327 ratingB
.insert("rating", 4);
328 m_model
->setData(1, ratingB
);
330 QHash
<QByteArray
, QVariant
> ratingC
;
331 ratingC
.insert("rating", 6);
332 m_model
->setData(2, ratingC
);
334 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
335 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
336 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
338 // Now change the rating from a.txt. This usually results
339 // in reordering of the items.
340 QHash
<QByteArray
, QVariant
> rating
;
341 rating
.insert("rating", changedRating
);
342 m_model
->setData(changedIndex
, rating
);
344 if (expectMoveSignal
) {
345 QVERIFY(itemsMovedSpy
.wait());
348 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
349 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
350 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
351 QVERIFY(m_model
->isConsistent());
354 void KFileItemModelTest::testChangeSortRole()
356 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
357 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
358 QVERIFY(itemsMovedSpy
.isValid());
360 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
362 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
364 m_model
->loadDirectory(m_testDir
->url());
365 QVERIFY(itemsInsertedSpy
.wait());
366 QCOMPARE(itemsInModel(),
367 QStringList() << "a.txt"
371 // Simulate that KFileItemModelRolesUpdater determines the mime type.
372 // Resorting the files by 'type' will only work immediately if their
373 // mime types are known.
374 for (int index
= 0; index
< m_model
->count(); ++index
) {
375 m_model
->fileItem(index
).determineMimeType();
378 // Now: sort by type.
379 m_model
->setSortRole("type");
380 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
381 QVERIFY(!itemsMovedSpy
.isEmpty());
383 // The actual order of the files might depend on the translation of the
384 // result of KFileItem::mimeComment() in the user's language.
385 QStringList version1
;
390 QStringList version2
;
395 const bool ok1
= (itemsInModel() == version1
);
396 const bool ok2
= (itemsInModel() == version2
);
401 void KFileItemModelTest::testResortAfterChangingName()
403 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
404 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
405 QVERIFY(itemsMovedSpy
.isValid());
407 // We sort by size in a directory where all files have the same size.
408 // Therefore, the files are sorted by their names.
409 m_model
->setSortRole("size");
411 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
413 m_model
->loadDirectory(m_testDir
->url());
414 QVERIFY(itemsInsertedSpy
.wait());
415 QCOMPARE(itemsInModel(),
416 QStringList() << "a.txt"
420 // We rename a.txt to d.txt. Even though the size has not changed at all,
421 // the model must re-sort the items.
422 QHash
<QByteArray
, QVariant
> data
;
423 data
.insert("text", "d.txt");
424 m_model
->setData(0, data
);
426 QVERIFY(itemsMovedSpy
.wait());
427 QCOMPARE(itemsInModel(),
428 QStringList() << "b.txt"
432 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
433 const KFileItem fileItemD
= m_model
->fileItem(2);
434 KFileItem fileItemA
= fileItemD
;
435 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
436 urlA
.setPath(urlA
.path() + "a.txt");
437 fileItemA
.setUrl(urlA
);
439 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
441 QVERIFY(itemsMovedSpy
.wait());
442 QCOMPARE(itemsInModel(),
443 QStringList() << "a.txt"
448 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
450 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
452 // KFileItemModel prevents that inserting a punch of items sequentially
453 // results in an itemsInserted()-signal for each item. Instead internally
454 // a timeout is given that collects such operations and results in only
455 // one itemsInserted()-signal. However in this test we want to stress
456 // KFileItemModel to do a lot of insert operation and hence decrease
457 // the timeout to 1 millisecond.
458 m_testDir
->createFile("1");
459 m_model
->loadDirectory(m_testDir
->url());
460 QVERIFY(itemsInsertedSpy
.wait());
461 QCOMPARE(m_model
->count(), 1);
463 // Insert 10 items for 20 times. After each insert operation the model consistency
465 QSet
<int> insertedItems
;
466 for (int i
= 0; i
< 20; ++i
) {
467 itemsInsertedSpy
.clear();
469 for (int j
= 0; j
< 10; ++j
) {
470 int itemName
= QRandomGenerator::global()->generate();
471 while (insertedItems
.contains(itemName
)) {
472 itemName
= QRandomGenerator::global()->generate();
474 insertedItems
.insert(itemName
);
476 m_testDir
->createFile(QString::number(itemName
));
479 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
480 if (itemsInsertedSpy
.isEmpty()) {
481 QVERIFY(itemsInsertedSpy
.wait());
484 QVERIFY(m_model
->isConsistent());
487 QCOMPARE(m_model
->count(), 201);
490 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
492 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
494 m_testDir
->createFiles({"B", "E", "G"});
496 // Due to inserting the 3 items one item-range with index == 0 and
497 // count == 3 must be given
498 m_model
->loadDirectory(m_testDir
->url());
499 QVERIFY(itemsInsertedSpy
.wait());
501 QCOMPARE(itemsInsertedSpy
.count(), 1);
502 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
503 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
504 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
506 // The indexes of the item-ranges must always be related to the model before
507 // the items have been inserted. Having:
510 // and inserting A, C, D, F the resulting model will be:
513 // and the item-ranges must be:
514 // index: 0, count: 1 for A
515 // index: 1, count: 2 for B, C
516 // index: 2, count: 1 for G
518 m_testDir
->createFiles({"A", "C", "D", "F"});
520 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
521 QVERIFY(itemsInsertedSpy
.wait());
523 QCOMPARE(itemsInsertedSpy
.count(), 1);
524 arguments
= itemsInsertedSpy
.takeFirst();
525 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
526 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
529 void KFileItemModelTest::testExpandItems()
531 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
532 QVERIFY(itemsInsertedSpy
.isValid());
533 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
534 QVERIFY(itemsRemovedSpy
.isValid());
535 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
536 QVERIFY(loadingCompletedSpy
.isValid());
538 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
539 // Besides testing the basic item expansion functionality, the test makes sure that
540 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
541 // yields the correct result for "a/a/1" and "a/a-1/", which is non-trivial because they share the
542 // first three characters.
543 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
544 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
545 modelRoles
<< "isExpanded"
547 << "expandedParentsCount";
548 m_model
->setRoles(modelRoles
);
550 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
552 // Store the URLs of all folders in a set.
553 QSet
<QUrl
> allFolders
;
554 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a") << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
555 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
557 m_model
->loadDirectory(m_testDir
->url());
558 QVERIFY(itemsInsertedSpy
.wait());
560 // So far, the model contains only "a/"
561 QCOMPARE(m_model
->count(), 1);
562 QVERIFY(m_model
->isExpandable(0));
563 QVERIFY(!m_model
->isExpanded(0));
564 QVERIFY(m_model
->expandedDirectories().empty());
566 QCOMPARE(itemsInsertedSpy
.count(), 1);
567 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
568 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
570 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
571 m_model
->setExpanded(0, true);
572 QVERIFY(m_model
->isExpanded(0));
573 QVERIFY(itemsInsertedSpy
.wait());
574 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
575 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
577 QCOMPARE(itemsInsertedSpy
.count(), 1);
578 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
579 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
581 QVERIFY(m_model
->isExpandable(1));
582 QVERIFY(!m_model
->isExpanded(1));
583 QVERIFY(m_model
->isExpandable(2));
584 QVERIFY(!m_model
->isExpanded(2));
586 // Expand the folder "a/a/" -> "a/a/1" becomes visible
587 m_model
->setExpanded(1, true);
588 QVERIFY(m_model
->isExpanded(1));
589 QVERIFY(itemsInsertedSpy
.wait());
590 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
591 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a") << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
593 QCOMPARE(itemsInsertedSpy
.count(), 1);
594 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
595 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
597 QVERIFY(!m_model
->isExpandable(2));
598 QVERIFY(!m_model
->isExpanded(2));
600 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
601 m_model
->setExpanded(3, true);
602 QVERIFY(m_model
->isExpanded(3));
603 QVERIFY(itemsInsertedSpy
.wait());
604 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
605 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
607 QCOMPARE(itemsInsertedSpy
.count(), 1);
608 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
609 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
611 QVERIFY(!m_model
->isExpandable(4));
612 QVERIFY(!m_model
->isExpanded(4));
614 // Collapse the top-level folder -> all other items should disappear
615 m_model
->setExpanded(0, false);
616 QVERIFY(!m_model
->isExpanded(0));
617 QCOMPARE(m_model
->count(), 1);
618 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
620 QCOMPARE(itemsRemovedSpy
.count(), 1);
621 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
622 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
623 QVERIFY(m_model
->isConsistent());
625 // Clear the model, reload the folder and try to restore the expanded folders.
627 QCOMPARE(m_model
->count(), 0);
628 QVERIFY(m_model
->expandedDirectories().empty());
630 m_model
->loadDirectory(m_testDir
->url());
631 m_model
->restoreExpandedDirectories(allFolders
);
632 QVERIFY(loadingCompletedSpy
.wait());
633 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
634 QVERIFY(m_model
->isExpanded(0));
635 QVERIFY(m_model
->isExpanded(1));
636 QVERIFY(!m_model
->isExpanded(2));
637 QVERIFY(m_model
->isExpanded(3));
638 QVERIFY(!m_model
->isExpanded(4));
639 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
640 QVERIFY(m_model
->isConsistent());
642 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
643 // This is how DolphinView restores the expanded folders when navigating in history.
644 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
645 QVERIFY(loadingCompletedSpy
.wait());
646 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
647 m_model
->restoreExpandedDirectories(allFolders
);
648 m_model
->loadDirectory(m_testDir
->url());
649 QVERIFY(loadingCompletedSpy
.wait());
650 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
651 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
653 // Remove all expanded items by changing the roles
654 itemsRemovedSpy
.clear();
655 m_model
->setRoles(originalModelRoles
);
656 QVERIFY(!m_model
->isExpanded(0));
657 QCOMPARE(m_model
->count(), 1);
658 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
660 QCOMPARE(itemsRemovedSpy
.count(), 1);
661 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
662 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
663 QVERIFY(m_model
->isConsistent());
666 void KFileItemModelTest::testExpandParentItems()
668 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
669 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
670 QVERIFY(loadingCompletedSpy
.isValid());
672 // Create a tree structure of folders:
680 QSet
<QByteArray
> modelRoles
= m_model
->roles();
681 modelRoles
<< "isExpanded"
683 << "expandedParentsCount";
684 m_model
->setRoles(modelRoles
);
686 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
688 m_model
->loadDirectory(m_testDir
->url());
689 QVERIFY(itemsInsertedSpy
.wait());
691 // So far, the model contains only "a 1/" and "a2/".
692 QCOMPARE(m_model
->count(), 2);
693 QVERIFY(m_model
->expandedDirectories().empty());
695 // Expand the parents of "a2/b2/c2".
696 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
697 QVERIFY(loadingCompletedSpy
.wait());
699 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
700 // It's important that only the parents of "a1/b1/c1" are expanded.
701 QCOMPARE(m_model
->count(), 4);
702 QVERIFY(!m_model
->isExpanded(0));
703 QVERIFY(m_model
->isExpanded(1));
704 QVERIFY(m_model
->isExpanded(2));
705 QVERIFY(!m_model
->isExpanded(3));
707 // Expand the parents of "a 1/b1".
708 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
709 QVERIFY(loadingCompletedSpy
.wait());
711 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
712 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
713 QCOMPARE(m_model
->count(), 5);
714 QVERIFY(m_model
->isExpanded(0));
715 QVERIFY(!m_model
->isExpanded(1));
716 QVERIFY(m_model
->isExpanded(2));
717 QVERIFY(m_model
->isExpanded(3));
718 QVERIFY(!m_model
->isExpanded(4));
719 QVERIFY(m_model
->isConsistent());
722 m_model
->setExpanded(1, true);
723 QVERIFY(loadingCompletedSpy
.wait());
724 QCOMPARE(m_model
->count(), 6);
725 QVERIFY(m_model
->isExpanded(0));
726 QVERIFY(m_model
->isExpanded(1));
727 QVERIFY(!m_model
->isExpanded(2));
728 QVERIFY(m_model
->isExpanded(3));
729 QVERIFY(m_model
->isExpanded(4));
730 QVERIFY(!m_model
->isExpanded(5));
731 QVERIFY(m_model
->isConsistent());
733 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
734 m_model
->setExpanded(1, false);
735 QCOMPARE(m_model
->count(), 5);
736 QVERIFY(m_model
->isExpanded(0));
737 QVERIFY(!m_model
->isExpanded(1));
738 QVERIFY(m_model
->isExpanded(2));
739 QVERIFY(m_model
->isExpanded(3));
740 QVERIFY(!m_model
->isExpanded(4));
741 QVERIFY(m_model
->isConsistent());
745 * Renaming an expanded folder by prepending its name with a dot makes it
746 * hidden. Verify that this does not cause an inconsistent model state and
747 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
749 void KFileItemModelTest::testMakeExpandedItemHidden()
751 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
752 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
754 QSet
<QByteArray
> modelRoles
= m_model
->roles();
755 modelRoles
<< "isExpanded"
757 << "expandedParentsCount";
758 m_model
->setRoles(modelRoles
);
760 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
762 m_model
->loadDirectory(m_testDir
->url());
763 QVERIFY(itemsInsertedSpy
.wait());
765 // So far, the model contains only "1a/" and "1b".
766 QCOMPARE(m_model
->count(), 2);
767 m_model
->setExpanded(0, true);
768 QVERIFY(itemsInsertedSpy
.wait());
770 // Now "1a/2a" and "1a/2b" have appeared.
771 QCOMPARE(m_model
->count(), 4);
772 m_model
->setExpanded(1, true);
773 QVERIFY(itemsInsertedSpy
.wait());
774 QCOMPARE(m_model
->count(), 6);
776 // Rename "1a/2" and make it hidden.
777 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
778 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
780 KIO::SimpleJob
*job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
781 bool ok
= job
->exec();
783 QVERIFY(itemsRemovedSpy
.wait());
785 // "1a/2" and its subfolders have disappeared now.
786 QVERIFY(m_model
->isConsistent());
787 QCOMPARE(m_model
->count(), 3);
789 m_model
->setExpanded(0, false);
790 QCOMPARE(m_model
->count(), 2);
793 void KFileItemModelTest::testRemoveFilteredExpandedItems()
795 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
797 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
798 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
799 modelRoles
<< "isExpanded"
801 << "expandedParentsCount";
802 m_model
->setRoles(modelRoles
);
804 m_testDir
->createFiles({"folder/child", "file"});
806 m_model
->loadDirectory(m_testDir
->url());
807 QVERIFY(itemsInsertedSpy
.wait());
809 // So far, the model contains only "folder/" and "file".
810 QCOMPARE(m_model
->count(), 2);
811 QVERIFY(m_model
->isExpandable(0));
812 QVERIFY(!m_model
->isExpandable(1));
813 QVERIFY(!m_model
->isExpanded(0));
814 QVERIFY(!m_model
->isExpanded(1));
815 QCOMPARE(itemsInModel(),
816 QStringList() << "folder"
819 // Expand "folder" -> "folder/child" becomes visible.
820 m_model
->setExpanded(0, true);
821 QVERIFY(m_model
->isExpanded(0));
822 QVERIFY(itemsInsertedSpy
.wait());
823 QCOMPARE(itemsInModel(),
824 QStringList() << "folder"
828 // Add a name filter.
829 m_model
->setNameFilter("f");
830 QCOMPARE(itemsInModel(),
831 QStringList() << "folder"
834 m_model
->setNameFilter("fo");
835 QCOMPARE(itemsInModel(), QStringList() << "folder");
837 // Remove all expanded items by changing the roles
838 m_model
->setRoles(originalModelRoles
);
839 QVERIFY(!m_model
->isExpanded(0));
840 QCOMPARE(itemsInModel(), QStringList() << "folder");
842 // Remove the name filter and verify that "folder/child" does not reappear.
843 m_model
->setNameFilter(QString());
844 QCOMPARE(itemsInModel(),
845 QStringList() << "folder"
849 void KFileItemModelTest::testSorting()
851 // testDir structure is as follows
858 // │ │ ├─ 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));
887 m_testDir
->createFile(".f");
888 m_testDir
->createDir(".g");
890 m_model
->loadDirectory(m_testDir
->url());
891 QVERIFY(itemsInsertedSpy
.wait());
892 QCOMPARE(itemsInsertedSpy
.count(), 1);
893 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
894 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 5));
896 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
897 m_model
->setExpanded(index
, true);
898 QVERIFY(itemsInsertedSpy
.wait());
899 QCOMPARE(itemsInsertedSpy
.count(), 1);
900 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
901 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
903 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
904 m_model
->setExpanded(index
, true);
905 QVERIFY(itemsInsertedSpy
.wait());
906 QCOMPARE(itemsInsertedSpy
.count(), 1);
907 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
908 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1));
910 // Default: Sort by Name, ascending
911 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
912 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
913 QVERIFY(m_model
->sortDirectoriesFirst());
914 QVERIFY(!m_model
->showHiddenFiles());
915 QCOMPARE(itemsInModel(),
925 // Sort by Name, ascending, 'Sort Folders First' disabled
926 m_model
->setSortDirectoriesFirst(false);
927 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
928 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
929 QCOMPARE(itemsInModel(),
938 QCOMPARE(itemsMovedSpy
.count(), 1);
939 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
940 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
942 // Sort by Name, descending
943 m_model
->setSortDirectoriesFirst(true);
944 m_model
->setSortOrder(Qt::DescendingOrder
);
945 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
946 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
947 QCOMPARE(itemsInModel(),
956 QCOMPARE(itemsMovedSpy
.count(), 2);
957 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
958 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
959 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
960 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
962 // Sort by Date, descending
963 m_model
->setSortDirectoriesFirst(true);
964 m_model
->setSortRole("modificationtime");
965 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
966 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
967 QCOMPARE(itemsInModel(),
976 QCOMPARE(itemsMovedSpy
.count(), 1);
977 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
978 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 5 << 4 << 6);
980 // Sort by Date, ascending
981 m_model
->setSortOrder(Qt::AscendingOrder
);
982 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
983 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
984 QCOMPARE(itemsInModel(),
993 QCOMPARE(itemsMovedSpy
.count(), 1);
994 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
995 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
997 // Sort by Date, ascending, 'Sort Folders First' disabled
998 m_model
->setSortDirectoriesFirst(false);
999 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
1000 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1001 QVERIFY(!m_model
->sortDirectoriesFirst());
1002 QCOMPARE(itemsInModel(),
1003 QStringList() << "e"
1011 QCOMPARE(itemsMovedSpy
.count(), 1);
1012 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
1013 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
1015 // Sort by Name, ascending, 'Sort Folders First' disabled
1016 m_model
->setSortRole("text");
1017 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1018 QVERIFY(!m_model
->sortDirectoriesFirst());
1019 QCOMPARE(itemsInModel(),
1020 QStringList() << "a"
1028 QCOMPARE(itemsMovedSpy
.count(), 1);
1029 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1030 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
1032 // Sort by Size, ascending, 'Sort Folders First' disabled
1033 m_model
->setSortRole("size");
1034 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1035 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1036 QVERIFY(!m_model
->sortDirectoriesFirst());
1037 QCOMPARE(itemsInModel(),
1038 QStringList() << "c"
1046 QCOMPARE(itemsMovedSpy
.count(), 1);
1047 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
1048 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
1050 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
1051 m_model
->setSortDirectoriesFirst(true);
1052 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1053 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
1054 QVERIFY(m_model
->sortDirectoriesFirst());
1055 QCOMPARE(itemsInModel(),
1056 QStringList() << "c"
1064 QCOMPARE(itemsMovedSpy
.count(), 0);
1066 // Sort by Size, descending, 'Sort Folders First' enabled
1067 m_model
->setSortOrder(Qt::DescendingOrder
);
1068 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
1069 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
1070 QVERIFY(m_model
->sortDirectoriesFirst());
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 << 6 << 5 << 4);
1084 // 'Show Hidden Files' enabled
1085 m_model
->setShowHiddenFiles(true);
1086 QVERIFY(m_model
->showHiddenFiles());
1087 QVERIFY(!m_model
->sortHiddenLast());
1088 QCOMPARE(itemsInModel(),
1089 QStringList() << "c"
1099 QCOMPARE(itemsMovedSpy
.count(), 0);
1100 QCOMPARE(itemsInsertedSpy
.count(), 1);
1101 QCOMPARE(itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
1103 // 'Sort Hidden Files Last' enabled
1104 m_model
->setSortHiddenLast(true);
1105 QVERIFY(m_model
->sortHiddenLast());
1106 QCOMPARE(itemsInModel(),
1107 QStringList() << "c"
1117 QCOMPARE(itemsMovedSpy
.count(), 1);
1118 QCOMPARE(itemsInsertedSpy
.count(), 0);
1119 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 5));
1120 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 8 << 4 << 5 << 6 << 7);
1123 m_model
->setSortRole("text");
1124 QCOMPARE(itemsInModel(),
1125 QStringList() << "c"
1135 QCOMPARE(itemsMovedSpy
.count(), 1);
1136 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 2));
1137 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 5 << 4);
1140 m_model
->setSortOrder(Qt::AscendingOrder
);
1141 QCOMPARE(itemsInModel(),
1142 QStringList() << "c"
1152 QCOMPARE(itemsMovedSpy
.count(), 1);
1153 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
1154 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 7 << 6 << 5 << 4);
1156 // 'Sort Folders First' disabled
1157 m_model
->setSortDirectoriesFirst(false);
1158 QVERIFY(!m_model
->sortDirectoriesFirst());
1159 QCOMPARE(itemsInModel(),
1160 QStringList() << "a"
1170 QCOMPARE(itemsMovedSpy
.count(), 1);
1171 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 10));
1172 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int>>(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1175 void KFileItemModelTest::testIndexForKeyboardSearch()
1177 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1179 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1181 m_model
->loadDirectory(m_testDir
->url());
1182 QVERIFY(itemsInsertedSpy
.wait());
1184 // Search from index 0
1185 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
1186 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
1187 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
1188 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
1189 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
1190 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
1191 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
1192 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
1193 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
1194 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
1195 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
1197 // Start a search somewhere in the middle
1198 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
1199 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
1200 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
1201 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
1203 // Test searches that go past the last item back to index 0
1204 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
1205 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
1206 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
1207 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
1209 // Test searches that yield no result
1210 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
1211 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
1212 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
1213 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
1214 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
1216 // Test upper case searches (note that search is case insensitive)
1217 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
1218 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
1219 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
1220 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1222 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1225 void KFileItemModelTest::testNameFilter()
1227 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1229 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1231 m_model
->loadDirectory(m_testDir
->url());
1232 QVERIFY(itemsInsertedSpy
.wait());
1234 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1235 QCOMPARE(m_model
->count(), 3);
1237 m_model
->setNameFilter("A2"); // Shows only A2
1238 QCOMPARE(m_model
->count(), 1);
1240 m_model
->setNameFilter("A2"); // Shows only A1
1241 QCOMPARE(m_model
->count(), 1);
1243 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1244 QCOMPARE(m_model
->count(), 2);
1246 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1247 QCOMPARE(m_model
->count(), 2);
1249 m_model
->setNameFilter(QString()); // Shows again all items
1250 QCOMPARE(m_model
->count(), 5);
1254 * Verifies that we do not crash when adding a KFileItem with an empty path.
1255 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1256 * tried to always read the first character of the path, even if the path is empty.
1258 void KFileItemModelTest::testEmptyPath()
1260 QSet
<QByteArray
> roles
;
1261 roles
.insert("text");
1262 roles
.insert("isExpanded");
1263 roles
.insert("isExpandable");
1264 roles
.insert("expandedParentsCount");
1265 m_model
->setRoles(roles
);
1267 const QUrl emptyUrl
;
1268 QVERIFY(emptyUrl
.path().isEmpty());
1270 const QUrl
url("file:///test/");
1272 KFileItemList items
;
1273 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1274 m_model
->slotItemsAdded(emptyUrl
, items
);
1275 m_model
->slotCompleted();
1279 * Verifies that the 'isExpanded' state of folders does not change when the
1280 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1282 void KFileItemModelTest::testRefreshExpandedItem()
1284 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1285 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1286 QVERIFY(itemsChangedSpy
.isValid());
1288 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1289 modelRoles
<< "isExpanded"
1291 << "expandedParentsCount";
1292 m_model
->setRoles(modelRoles
);
1294 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1296 m_model
->loadDirectory(m_testDir
->url());
1297 QVERIFY(itemsInsertedSpy
.wait());
1298 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1300 m_model
->setExpanded(0, true);
1301 QVERIFY(itemsInsertedSpy
.wait());
1302 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1303 QVERIFY(m_model
->isExpanded(0));
1305 const KFileItem item
= m_model
->fileItem(0);
1306 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1307 QVERIFY(!itemsChangedSpy
.isEmpty());
1309 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1310 QVERIFY(m_model
->isExpanded(0));
1314 * Verifies that adding an item to an expanded folder that's filtered makes the parental chain visible.
1316 void KFileItemModelTest::testAddItemToFilteredExpandedFolder()
1318 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1319 QSignalSpy
fileItemsChangedSpy(m_model
, &KFileItemModel::fileItemsChanged
);
1321 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1322 modelRoles
<< "isExpanded"
1324 << "expandedParentsCount";
1325 m_model
->setRoles(modelRoles
);
1327 m_testDir
->createFile("a/b/file");
1329 m_model
->loadDirectory(m_testDir
->url());
1330 QVERIFY(itemsInsertedSpy
.wait());
1331 QCOMPARE(m_model
->count(), 1); // "a
1334 m_model
->setExpanded(0, true);
1335 QVERIFY(itemsInsertedSpy
.wait());
1338 m_model
->setExpanded(1, true);
1339 QVERIFY(itemsInsertedSpy
.wait());
1341 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1343 const QUrl urlB
= m_model
->fileItem(1).url();
1345 // Set a filter that matches ".txt" extension
1346 m_model
->setNameFilter("*.txt");
1347 QCOMPARE(m_model
->count(), 0); // Everything got hidden since we don't have a .txt file yet
1349 m_model
->slotItemsAdded(urlB
, KFileItemList() << KFileItem(QUrl("a/b/newItem.txt")));
1350 m_model
->slotCompleted();
1352 // Entire parental chain should now be shown
1353 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/newItem.txt"
1354 QCOMPARE(itemsInModel(),
1355 QStringList() << "a"
1359 // Items should be indented in hierarchy
1360 QCOMPARE(m_model
->expandedParentsCount(0), 0);
1361 QCOMPARE(m_model
->expandedParentsCount(1), 1);
1362 QCOMPARE(m_model
->expandedParentsCount(2), 2);
1366 * Verifies that deleting the last filter-passing child from expanded folders
1367 * makes the parental chain hidden.
1369 void KFileItemModelTest::testDeleteItemsWithExpandedFolderWithFilter()
1371 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1372 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1374 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1375 modelRoles
<< "isExpanded"
1377 << "expandedParentsCount";
1378 m_model
->setRoles(modelRoles
);
1380 m_testDir
->createFile("a/b/file");
1382 m_model
->loadDirectory(m_testDir
->url());
1383 QVERIFY(itemsInsertedSpy
.wait());
1384 QCOMPARE(m_model
->count(), 1); // "a
1387 m_model
->setExpanded(0, true);
1388 QVERIFY(itemsInsertedSpy
.wait());
1391 m_model
->setExpanded(1, true);
1392 QVERIFY(itemsInsertedSpy
.wait());
1394 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1396 // Set a filter that matches "file" extension
1397 m_model
->setNameFilter("file");
1398 QCOMPARE(m_model
->count(), 3); // Everything is still shown
1401 QCOMPARE(itemsRemovedSpy
.count(), 0);
1402 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(2));
1403 QCOMPARE(itemsRemovedSpy
.count(), 1);
1405 // Entire parental chain should now be filtered
1406 QCOMPARE(m_model
->count(), 0);
1407 QCOMPARE(m_model
->m_filteredItems
.size(), 2);
1411 * Verifies that the fileItemsChanged signal is raised with the correct index after renaming files with filter set.
1412 * The rename operation will cause one item to be filtered out and another item to be reordered.
1414 void KFileItemModelTest::testRefreshItemsWithFilter()
1416 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1417 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1418 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1419 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1421 // Creates three .txt files
1422 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt"});
1424 m_model
->loadDirectory(m_testDir
->url());
1425 QVERIFY(itemsInsertedSpy
.wait());
1427 QCOMPARE(m_model
->count(), 3); // "b.txt", "c.txt", "d.txt"
1429 // Set a filter that matches ".txt" extension
1430 m_model
->setNameFilter("*.txt");
1431 QCOMPARE(m_model
->count(), 3); // Still all items are shown
1432 QCOMPARE(itemsInModel(),
1433 QStringList() << "b.txt"
1437 // Objects used to rename
1438 const KFileItem fileItemC_txt
= m_model
->fileItem(1);
1439 KFileItem fileItemC_cfg
= fileItemC_txt
;
1440 fileItemC_cfg
.setUrl(QUrl("c.cfg"));
1442 const KFileItem fileItemD_txt
= m_model
->fileItem(2);
1443 KFileItem fileItemA_txt
= fileItemD_txt
;
1444 fileItemA_txt
.setUrl(QUrl("a.txt"));
1446 // Rename "c.txt" to "c.cfg"; and rename "d.txt" to "a.txt"
1447 QCOMPARE(itemsRemovedSpy
.count(), 0);
1448 QCOMPARE(itemsChangedSpy
.count(), 0);
1449 m_model
->slotRefreshItems({qMakePair(fileItemC_txt
, fileItemC_cfg
), qMakePair(fileItemD_txt
, fileItemA_txt
)});
1450 QCOMPARE(itemsRemovedSpy
.count(), 1);
1451 QCOMPARE(itemsChangedSpy
.count(), 1);
1452 QCOMPARE(m_model
->count(), 2); // Only "a.txt" and "b.txt". "c.cfg" got filtered out
1454 QList
<QVariant
> arguments
= itemsChangedSpy
.takeLast();
1455 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1457 // We started with the order "b.txt", "c.txt", "d.txt"
1458 // "d.txt" started with index "2"
1459 // "c.txt" got renamed and got filtered out
1460 // "d.txt" index shifted from index "2" to "1"
1461 // So we expect index "1" in this argument, meaning "d.txt" was renamed
1462 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1464 // Re-sorting is done asynchronously:
1465 QCOMPARE(itemsInModel(),
1466 QStringList() << "b.txt"
1467 << "a.txt"); // Files should still be in the incorrect order
1468 QVERIFY(itemsMovedSpy
.wait());
1469 QCOMPARE(itemsInModel(),
1470 QStringList() << "a.txt"
1471 << "b.txt"); // Files were re-sorted and should now be in the correct order
1475 * Verifies that parental chains are hidden and shown as needed while their children get filtered/unfiltered due to renaming.
1476 * Also verifies that the "isExpanded" and "expandedParentsCount" values are kept for expanded folders that get refreshed.
1478 void KFileItemModelTest::testRefreshExpandedFolderWithFilter()
1480 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1481 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1483 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1484 modelRoles
<< "isExpanded"
1486 << "expandedParentsCount";
1487 m_model
->setRoles(modelRoles
);
1489 m_testDir
->createFile("a/b/someFolder/someFile");
1491 m_model
->loadDirectory(m_testDir
->url());
1492 QVERIFY(itemsInsertedSpy
.wait());
1494 QCOMPARE(m_model
->count(), 1); // Only "a/"
1497 m_model
->setExpanded(0, true);
1498 QVERIFY(itemsInsertedSpy
.wait());
1501 m_model
->setExpanded(1, true);
1502 QVERIFY(itemsInsertedSpy
.wait());
1504 // Expand "a/b/someFolder/".
1505 m_model
->setExpanded(2, true);
1506 QVERIFY(itemsInsertedSpy
.wait());
1507 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/b/", "a/b/someFolder", "a/b/someFolder/someFile"
1509 // Set a filter that matches the expanded folder "someFolder"
1510 m_model
->setNameFilter("someFolder");
1511 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/b/someFolder"
1513 // Objects used to rename
1514 const KFileItem fileItemA
= m_model
->fileItem(0);
1515 KFileItem fileItemARenamed
= fileItemA
;
1516 fileItemARenamed
.setUrl(QUrl("a_renamed"));
1518 const KFileItem fileItemSomeFolder
= m_model
->fileItem(2);
1519 KFileItem fileItemRenamedFolder
= fileItemSomeFolder
;
1520 fileItemRenamedFolder
.setUrl(QUrl("/a_renamed/b/renamedFolder"));
1522 // Rename "a" to "a_renamed"
1523 // This way we test if the algorithm is sane as to NOT hide "a_renamed" since it will have visible children
1524 m_model
->slotRefreshItems({qMakePair(fileItemA
, fileItemARenamed
)});
1525 QCOMPARE(m_model
->count(), 3); // Entire parental chain must still be shown
1526 QCOMPARE(itemsInModel(),
1527 QStringList() << "a_renamed"
1531 // Rename "a_renamed" back to "a"; and "someFolder" to "renamedFolder"
1532 m_model
->slotRefreshItems({qMakePair(fileItemARenamed
, fileItemA
), qMakePair(fileItemSomeFolder
, fileItemRenamedFolder
)});
1533 QCOMPARE(m_model
->count(), 0); // Entire parental chain became hidden
1535 // Rename "renamedFolder" back to "someFolder". Filter is passing again
1536 m_model
->slotRefreshItems({qMakePair(fileItemRenamedFolder
, fileItemSomeFolder
)});
1537 QCOMPARE(m_model
->count(), 3); // Entire parental chain is shown again
1538 QCOMPARE(itemsInModel(),
1539 QStringList() << "a"
1543 // slotRefreshItems() should preserve "isExpanded" and "expandedParentsCount" values explicitly in this case
1544 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("isExpanded").toBool(), true);
1545 QCOMPARE(m_model
->m_itemData
.at(2)->values
.value("expandedParentsCount"), 2);
1549 * Verify that removing hidden files and folders from the model does not
1550 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1552 void KFileItemModelTest::testRemoveHiddenItems()
1554 m_testDir
->createDir(".a");
1555 m_testDir
->createDir(".b");
1556 m_testDir
->createDir("c");
1557 m_testDir
->createDir("d");
1558 m_testDir
->createFiles({".f", ".g", "h", "i"});
1560 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1561 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1563 m_model
->setShowHiddenFiles(true);
1564 m_model
->loadDirectory(m_testDir
->url());
1565 QVERIFY(itemsInsertedSpy
.wait());
1566 QCOMPARE(itemsInModel(),
1567 QStringList() << ".a"
1575 QCOMPARE(itemsInsertedSpy
.count(), 1);
1576 QCOMPARE(itemsRemovedSpy
.count(), 0);
1577 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1578 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1580 m_model
->setShowHiddenFiles(false);
1581 QCOMPARE(itemsInModel(),
1582 QStringList() << "c"
1586 QCOMPARE(itemsInsertedSpy
.count(), 0);
1587 QCOMPARE(itemsRemovedSpy
.count(), 1);
1588 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1589 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1591 m_model
->setShowHiddenFiles(true);
1592 QCOMPARE(itemsInModel(),
1593 QStringList() << ".a"
1601 QCOMPARE(itemsInsertedSpy
.count(), 1);
1602 QCOMPARE(itemsRemovedSpy
.count(), 0);
1603 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1604 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1607 QCOMPARE(itemsInModel(), QStringList());
1608 QCOMPARE(itemsInsertedSpy
.count(), 0);
1609 QCOMPARE(itemsRemovedSpy
.count(), 1);
1610 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1611 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1613 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1614 // Verify that this does not make the model crash.
1615 m_model
->setShowHiddenFiles(false);
1619 * Verify that filtered items are removed when their parent is collapsed.
1621 void KFileItemModelTest::collapseParentOfHiddenItems()
1623 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1624 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1626 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1627 modelRoles
<< "isExpanded"
1629 << "expandedParentsCount";
1630 m_model
->setRoles(modelRoles
);
1632 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1634 m_model
->loadDirectory(m_testDir
->url());
1635 QVERIFY(itemsInsertedSpy
.wait());
1636 QCOMPARE(m_model
->count(), 1); // Only "a/"
1639 m_model
->setExpanded(0, true);
1640 QVERIFY(itemsInsertedSpy
.wait());
1641 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1644 m_model
->setExpanded(1, true);
1645 QVERIFY(itemsInsertedSpy
.wait());
1646 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1649 m_model
->setExpanded(2, true);
1650 QVERIFY(itemsInsertedSpy
.wait());
1651 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"
1653 // Set a name filter that matches nothing -> nothing should remain.
1654 m_model
->setNameFilter("xyz");
1655 QCOMPARE(itemsRemovedSpy
.count(), 1);
1656 QCOMPARE(m_model
->count(), 0); //Everything is hidden
1657 QCOMPARE(itemsInModel(), QStringList());
1659 //Filter by the file names. Folder "d" will be hidden since it was collapsed
1660 m_model
->setNameFilter("1");
1661 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1662 QCOMPARE(m_model
->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1664 // Collapse the folder "a/".
1665 m_model
->setExpanded(0, false);
1666 QCOMPARE(itemsRemovedSpy
.count(), 2);
1667 QCOMPARE(m_model
->count(), 1);
1668 QCOMPARE(itemsInModel(), QStringList() << "a");
1670 // Remove the filter -> "a" should still appear (and we should not get a crash).
1671 m_model
->setNameFilter(QString());
1672 QCOMPARE(itemsRemovedSpy
.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1673 QCOMPARE(m_model
->count(), 1);
1674 QCOMPARE(itemsInModel(), QStringList() << "a");
1678 * Verify that filtered items are removed when their parent is deleted.
1680 void KFileItemModelTest::removeParentOfHiddenItems()
1682 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1683 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1685 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1686 modelRoles
<< "isExpanded"
1688 << "expandedParentsCount";
1689 m_model
->setRoles(modelRoles
);
1691 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1693 m_model
->loadDirectory(m_testDir
->url());
1694 QVERIFY(itemsInsertedSpy
.wait());
1695 QCOMPARE(m_model
->count(), 1); // Only "a/"
1698 m_model
->setExpanded(0, true);
1699 QVERIFY(itemsInsertedSpy
.wait());
1700 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1703 m_model
->setExpanded(1, true);
1704 QVERIFY(itemsInsertedSpy
.wait());
1705 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1708 m_model
->setExpanded(2, true);
1709 QVERIFY(itemsInsertedSpy
.wait());
1710 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"
1712 // Set a name filter that matches nothing -> nothing should remain.
1713 m_model
->setNameFilter("xyz");
1714 QCOMPARE(itemsRemovedSpy
.count(), 1);
1715 QCOMPARE(m_model
->count(), 0);
1716 QCOMPARE(itemsInModel(), QStringList());
1718 // Filter by "c". Folder "b" will also be shown because it is its parent.
1719 m_model
->setNameFilter("c");
1720 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1721 QCOMPARE(m_model
->count(), 3);
1722 QCOMPARE(itemsInModel(),
1723 QStringList() << "a"
1727 // Simulate the deletion of the directory "a/b/".
1728 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1729 QCOMPARE(itemsRemovedSpy
.count(), 2);
1730 QCOMPARE(m_model
->count(), 0); // "a" will be filtered out since it doesn't pass the filter and doesn't have visible children
1732 // Remove the filter -> only the file "a/1" should appear.
1733 m_model
->setNameFilter(QString());
1734 QCOMPARE(m_model
->count(), 2);
1735 QCOMPARE(itemsInModel(),
1736 QStringList() << "a"
1741 * Create a tree structure where parent-child relationships can not be
1742 * determined by parsing the URLs, and verify that KFileItemModel
1743 * handles them correctly.
1745 void KFileItemModelTest::testGeneralParentChildRelationships()
1747 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1748 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1750 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1751 modelRoles
<< "isExpanded"
1753 << "expandedParentsCount";
1754 m_model
->setRoles(modelRoles
);
1756 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1758 m_model
->loadDirectory(m_testDir
->url());
1759 QVERIFY(itemsInsertedSpy
.wait());
1760 QCOMPARE(itemsInModel(),
1761 QStringList() << "parent1"
1764 // Expand all folders.
1765 m_model
->setExpanded(0, true);
1766 QVERIFY(itemsInsertedSpy
.wait());
1767 QCOMPARE(itemsInModel(),
1768 QStringList() << "parent1"
1772 m_model
->setExpanded(1, true);
1773 QVERIFY(itemsInsertedSpy
.wait());
1774 QCOMPARE(itemsInModel(),
1775 QStringList() << "parent1"
1777 << "realGrandChild1"
1780 m_model
->setExpanded(3, true);
1781 QVERIFY(itemsInsertedSpy
.wait());
1782 QCOMPARE(itemsInModel(),
1783 QStringList() << "parent1"
1785 << "realGrandChild1"
1789 m_model
->setExpanded(4, true);
1790 QVERIFY(itemsInsertedSpy
.wait());
1791 QCOMPARE(itemsInModel(),
1792 QStringList() << "parent1"
1794 << "realGrandChild1"
1797 << "realGrandChild2");
1799 // Add some more children and grand-children.
1800 const QUrl parent1
= m_model
->fileItem(0).url();
1801 const QUrl parent2
= m_model
->fileItem(3).url();
1802 const QUrl realChild1
= m_model
->fileItem(1).url();
1803 const QUrl realChild2
= m_model
->fileItem(4).url();
1805 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1806 m_model
->slotCompleted();
1807 QCOMPARE(itemsInModel(),
1808 QStringList() << "parent1"
1810 << "realGrandChild1"
1814 << "realGrandChild2");
1816 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1817 m_model
->slotCompleted();
1818 QCOMPARE(itemsInModel(),
1819 QStringList() << "parent1"
1821 << "realGrandChild1"
1825 << "realGrandChild2"
1828 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1829 m_model
->slotCompleted();
1830 QCOMPARE(itemsInModel(),
1831 QStringList() << "parent1"
1834 << "realGrandChild1"
1838 << "realGrandChild2"
1841 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1842 m_model
->slotCompleted();
1843 QCOMPARE(itemsInModel(),
1844 QStringList() << "parent1"
1847 << "realGrandChild1"
1852 << "realGrandChild2"
1855 // Set a name filter that matches nothing -> nothing will remain.
1856 m_model
->setNameFilter("xyz");
1857 QCOMPARE(itemsInModel(), QStringList());
1858 QCOMPARE(itemsRemovedSpy
.count(), 1);
1859 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1860 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1861 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 10));
1863 // Set a name filter that matches only "realChild". Their prarents should still show.
1864 m_model
->setNameFilter("realChild");
1865 QCOMPARE(itemsInModel(),
1866 QStringList() << "parent1"
1870 QCOMPARE(itemsRemovedSpy
.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
1872 // Collapse "parent1".
1873 m_model
->setExpanded(0, false);
1874 QCOMPARE(itemsInModel(),
1875 QStringList() << "parent1"
1878 QCOMPARE(itemsRemovedSpy
.count(), 1);
1879 arguments
= itemsRemovedSpy
.takeFirst();
1880 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1881 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1883 // Remove "parent2".
1884 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1885 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1886 QCOMPARE(itemsRemovedSpy
.count(), 1);
1887 arguments
= itemsRemovedSpy
.takeFirst();
1888 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1889 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1891 // Clear filter, verify that no items reappear.
1892 m_model
->setNameFilter(QString());
1893 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1896 void KFileItemModelTest::testNameRoleGroups()
1898 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1899 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1900 QVERIFY(itemsMovedSpy
.isValid());
1901 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1902 QVERIFY(groupsChangedSpy
.isValid());
1904 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1906 m_model
->setGroupedSorting(true);
1907 m_model
->loadDirectory(m_testDir
->url());
1908 QVERIFY(itemsInsertedSpy
.wait());
1909 QCOMPARE(itemsInModel(),
1910 QStringList() << "b.txt"
1915 QList
<QPair
<int, QVariant
>> expectedGroups
;
1916 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1917 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1918 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1919 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1920 QCOMPARE(m_model
->groups(), expectedGroups
);
1922 // Rename d.txt to a.txt.
1923 QHash
<QByteArray
, QVariant
> data
;
1924 data
.insert("text", "a.txt");
1925 m_model
->setData(2, data
);
1926 QVERIFY(itemsMovedSpy
.wait());
1927 QCOMPARE(itemsInModel(),
1928 QStringList() << "a.txt"
1933 expectedGroups
.clear();
1934 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1935 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1936 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1937 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1938 QCOMPARE(m_model
->groups(), expectedGroups
);
1940 // Rename c.txt to d.txt.
1941 data
.insert("text", "d.txt");
1942 m_model
->setData(2, data
);
1943 QVERIFY(groupsChangedSpy
.wait());
1944 QCOMPARE(itemsInModel(),
1945 QStringList() << "a.txt"
1950 expectedGroups
.clear();
1951 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1952 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1953 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1954 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1955 QCOMPARE(m_model
->groups(), expectedGroups
);
1957 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1958 const KFileItem fileItemD
= m_model
->fileItem(2);
1959 KFileItem fileItemC
= fileItemD
;
1960 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1961 urlC
.setPath(urlC
.path() + "c.txt");
1962 fileItemC
.setUrl(urlC
);
1964 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1965 QVERIFY(groupsChangedSpy
.wait());
1966 QCOMPARE(itemsInModel(),
1967 QStringList() << "a.txt"
1972 expectedGroups
.clear();
1973 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1974 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1975 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1976 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1977 QCOMPARE(m_model
->groups(), expectedGroups
);
1980 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1982 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1984 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1985 modelRoles
<< "isExpanded"
1987 << "expandedParentsCount";
1988 m_model
->setRoles(modelRoles
);
1990 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1992 m_model
->setGroupedSorting(true);
1993 m_model
->loadDirectory(m_testDir
->url());
1994 QVERIFY(itemsInsertedSpy
.wait());
1995 QCOMPARE(itemsInModel(),
1996 QStringList() << "a"
1999 QList
<QPair
<int, QVariant
>> expectedGroups
;
2000 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2001 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
2002 QCOMPARE(m_model
->groups(), expectedGroups
);
2004 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
2005 expectedGroups
.clear();
2006 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
2007 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
2009 m_model
->setExpanded(0, true);
2010 QVERIFY(m_model
->isExpanded(0));
2011 QVERIFY(itemsInsertedSpy
.wait());
2012 QCOMPARE(itemsInModel(),
2013 QStringList() << "a"
2017 QCOMPARE(m_model
->groups(), expectedGroups
);
2019 m_model
->setExpanded(3, true);
2020 QVERIFY(m_model
->isExpanded(3));
2021 QVERIFY(itemsInsertedSpy
.wait());
2022 QCOMPARE(itemsInModel(),
2023 QStringList() << "a"
2029 QCOMPARE(m_model
->groups(), expectedGroups
);
2032 void KFileItemModelTest::testInconsistentModel()
2034 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2036 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2037 modelRoles
<< "isExpanded"
2039 << "expandedParentsCount";
2040 m_model
->setRoles(modelRoles
);
2042 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
2044 m_model
->loadDirectory(m_testDir
->url());
2045 QVERIFY(itemsInsertedSpy
.wait());
2046 QCOMPARE(itemsInModel(), QStringList() << "a");
2048 // Expand "a/" and "a/b/".
2049 m_model
->setExpanded(0, true);
2050 QVERIFY(m_model
->isExpanded(0));
2051 QVERIFY(itemsInsertedSpy
.wait());
2052 QCOMPARE(itemsInModel(),
2053 QStringList() << "a"
2056 m_model
->setExpanded(1, true);
2057 QVERIFY(m_model
->isExpanded(1));
2058 QVERIFY(itemsInsertedSpy
.wait());
2059 QCOMPARE(itemsInModel(),
2060 QStringList() << "a"
2065 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
2066 // Such a thing can in principle happen when performing a search, and there
2068 // (a) match the search string, and
2069 // (b) are children of a folder that matches the search string and is expanded.
2071 // Note that the first item in the list of added items must be new (i.e., not
2072 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
2073 // it receives items that are in the model already and ignore them.
2074 QUrl
url(m_model
->directory().url() + "/a2");
2075 KFileItem
newItem(url
);
2077 KFileItemList items
;
2078 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
2079 m_model
->slotItemsAdded(m_model
->directory(), items
);
2080 m_model
->slotCompleted();
2081 QCOMPARE(itemsInModel(),
2082 QStringList() << "a"
2090 m_model
->setExpanded(0, false);
2092 // Test that the right items have been removed, see
2093 // https://bugs.kde.org/show_bug.cgi?id=324371
2094 QCOMPARE(itemsInModel(),
2095 QStringList() << "a"
2100 // Test that resorting does not cause a crash, see
2101 // https://bugs.kde.org/show_bug.cgi?id=325359
2102 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
2103 m_model
->resortAllItems();
2106 void KFileItemModelTest::testChangeRolesForFilteredItems()
2108 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2110 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2111 modelRoles
<< "owner";
2112 m_model
->setRoles(modelRoles
);
2114 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
2116 m_model
->loadDirectory(m_testDir
->url());
2117 QVERIFY(itemsInsertedSpy
.wait());
2118 QCOMPARE(itemsInModel(),
2119 QStringList() << "a.txt"
2123 for (int index
= 0; index
< m_model
->count(); ++index
) {
2124 // All items should have the "text" and "owner" roles, but not "group".
2125 QVERIFY(m_model
->data(index
).contains("text"));
2126 QVERIFY(m_model
->data(index
).contains("owner"));
2127 QVERIFY(!m_model
->data(index
).contains("group"));
2130 // Add a filter, such that only "aaa.txt" remains in the model.
2131 m_model
->setNameFilter("aaa");
2132 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
2134 // Add the "group" role.
2135 modelRoles
<< "group";
2136 m_model
->setRoles(modelRoles
);
2138 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
2139 m_model
->setNameFilter("aa");
2140 QCOMPARE(itemsInModel(),
2141 QStringList() << "aa.txt"
2144 for (int index
= 0; index
< m_model
->count(); ++index
) {
2145 // All items should have the "text", "owner", and "group" roles.
2146 QVERIFY(m_model
->data(index
).contains("text"));
2147 QVERIFY(m_model
->data(index
).contains("owner"));
2148 QVERIFY(m_model
->data(index
).contains("group"));
2151 // Remove the "owner" role.
2152 modelRoles
.remove("owner");
2153 m_model
->setRoles(modelRoles
);
2155 // Clear the filter, and verify that all items have the expected roles
2156 m_model
->setNameFilter(QString());
2157 QCOMPARE(itemsInModel(),
2158 QStringList() << "a.txt"
2162 for (int index
= 0; index
< m_model
->count(); ++index
) {
2163 // All items should have the "text" and "group" roles, but now "owner".
2164 QVERIFY(m_model
->data(index
).contains("text"));
2165 QVERIFY(!m_model
->data(index
).contains("owner"));
2166 QVERIFY(m_model
->data(index
).contains("group"));
2170 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
2172 KFileItemList items
;
2174 KIO::UDSEntry entry
[3];
2176 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
2177 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
2179 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
2180 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
2182 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
2183 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
2185 for (int i
= 0; i
< 3; ++i
) {
2186 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
2187 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
2188 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
2189 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
2190 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
2191 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
2192 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
2195 m_model
->slotItemsAdded(m_testDir
->url(), items
);
2196 m_model
->slotCompleted();
2198 QCOMPARE(itemsInModel(),
2199 QStringList() << "a.txt"
2204 m_model
->setNameFilter("a");
2205 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
2208 m_model
->setSortRole("owner");
2210 // Clear the filter, and verify that the items are sorted correctly.
2211 m_model
->setNameFilter(QString());
2212 QCOMPARE(itemsInModel(),
2213 QStringList() << "c.txt"
2218 void KFileItemModelTest::testRefreshFilteredItems()
2220 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2222 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
2224 m_model
->loadDirectory(m_testDir
->url());
2225 QVERIFY(itemsInsertedSpy
.wait());
2226 QCOMPARE(itemsInModel(),
2227 QStringList() << "a.txt"
2232 const KFileItem fileItemC
= m_model
->fileItem(2);
2234 // Show only the .txt files.
2235 m_model
->setNameFilter(".txt");
2236 QCOMPARE(itemsInModel(),
2237 QStringList() << "a.txt"
2240 // Rename one of the .jpg files.
2241 KFileItem fileItemE
= fileItemC
;
2242 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
2243 urlE
.setPath(urlE
.path() + "/e.jpg");
2244 fileItemE
.setUrl(urlE
);
2246 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
2248 // Show all files again, and verify that the model has updated the file name.
2249 m_model
->setNameFilter(QString());
2250 QCOMPARE(itemsInModel(),
2251 QStringList() << "a.txt"
2257 void KFileItemModelTest::testCreateMimeData()
2259 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2261 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2262 modelRoles
<< "isExpanded"
2264 << "expandedParentsCount";
2265 m_model
->setRoles(modelRoles
);
2267 m_testDir
->createFile("a/1");
2269 m_model
->loadDirectory(m_testDir
->url());
2270 QVERIFY(itemsInsertedSpy
.wait());
2271 QCOMPARE(itemsInModel(), QStringList() << "a");
2274 m_model
->setExpanded(0, true);
2275 QVERIFY(itemsInsertedSpy
.wait());
2276 QCOMPARE(itemsInModel(),
2277 QStringList() << "a"
2280 // Verify that creating the MIME data for a child of an expanded folder does
2281 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
2283 selection
.insert(1);
2284 QMimeData
*mimeData
= m_model
->createMimeData(selection
);
2288 void KFileItemModelTest::testCollapseFolderWhileLoading()
2290 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2292 QSet
<QByteArray
> modelRoles
= m_model
->roles();
2293 modelRoles
<< "isExpanded"
2295 << "expandedParentsCount";
2296 m_model
->setRoles(modelRoles
);
2298 m_testDir
->createFile("a2/b/c1.txt");
2300 m_model
->loadDirectory(m_testDir
->url());
2301 QVERIFY(itemsInsertedSpy
.wait());
2302 QCOMPARE(itemsInModel(), QStringList() << "a2");
2305 m_model
->setExpanded(0, true);
2306 QVERIFY(m_model
->isExpanded(0));
2307 QVERIFY(itemsInsertedSpy
.wait());
2308 QCOMPARE(itemsInModel(),
2309 QStringList() << "a2"
2313 m_model
->setExpanded(1, true);
2314 QVERIFY(m_model
->isExpanded(1));
2315 QVERIFY(itemsInsertedSpy
.wait());
2316 QCOMPARE(itemsInModel(),
2317 QStringList() << "a2"
2321 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
2322 // signal is not emitted yet.
2323 const KFileItem fileItemC1
= m_model
->fileItem(2);
2324 KFileItem fileItemC2
= fileItemC1
;
2325 QUrl urlC2
= fileItemC2
.url();
2326 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
2327 urlC2
.setPath(urlC2
.path() + "c2.txt");
2328 fileItemC2
.setUrl(urlC2
);
2330 const QUrl urlB
= m_model
->fileItem(1).url();
2331 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
2332 QCOMPARE(itemsInModel(),
2333 QStringList() << "a2"
2337 // Collapse "a2/". This should also remove all its (indirect) children from
2338 // the model and from the model's m_pendingItemsToInsert member.
2339 m_model
->setExpanded(0, false);
2340 QCOMPARE(itemsInModel(), QStringList() << "a2");
2342 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
2343 // is still in m_pendingItemsToInsert, then we might get a crash, see
2344 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
2345 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
2346 // without parent in the model.
2347 m_model
->slotCompleted();
2348 QCOMPARE(itemsInModel(), QStringList() << "a2");
2350 // Expand "a2/" again.
2351 m_model
->setExpanded(0, true);
2352 QVERIFY(m_model
->isExpanded(0));
2353 QVERIFY(itemsInsertedSpy
.wait());
2354 QCOMPARE(itemsInModel(),
2355 QStringList() << "a2"
2358 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
2359 // completed() signal is not emitted yet.
2360 const KFileItem fileItemA2
= m_model
->fileItem(0);
2361 KFileItem fileItemA1
= fileItemA2
;
2362 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
2363 urlA1
.setPath(urlA1
.path() + "a1");
2364 fileItemA1
.setUrl(urlA1
);
2366 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
2367 QCOMPARE(itemsInModel(),
2368 QStringList() << "a2"
2371 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
2372 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
2373 // confuse the code which collapses the folder.
2374 m_model
->setExpanded(0, false);
2375 QCOMPARE(itemsInModel(),
2376 QStringList() << "a1"
2378 QVERIFY(!m_model
->isExpanded(0));
2379 QVERIFY(!m_model
->isExpanded(1));
2382 void KFileItemModelTest::testDeleteFileMoreThanOnce()
2384 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2386 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
2388 m_model
->loadDirectory(m_testDir
->url());
2389 QVERIFY(itemsInsertedSpy
.wait());
2390 QCOMPARE(itemsInModel(),
2391 QStringList() << "a.txt"
2396 const KFileItem fileItemB
= m_model
->fileItem(1);
2398 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
2400 list
<< fileItemB
<< fileItemB
;
2401 m_model
->slotItemsDeleted(list
);
2403 QVERIFY(m_model
->isConsistent());
2404 QCOMPARE(itemsInModel(),
2405 QStringList() << "a.txt"
2410 void KFileItemModelTest::testInsertAfterExpand()
2412 m_model
->m_dirLister
->setAutoUpdate(true);
2414 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
2415 QVERIFY(itemsInsertedSpy
.isValid());
2416 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
2417 QVERIFY(itemsRemovedSpy
.isValid());
2418 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2419 QVERIFY(loadingCompletedSpy
.isValid());
2421 // Test expanding subfolders in a folder with the items "a/", "a/a/"
2422 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
2423 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
2424 modelRoles
<< "isExpanded"
2426 << "expandedParentsCount";
2427 m_model
->setRoles(modelRoles
);
2429 m_testDir
->createFile("a/b/1");
2431 m_model
->loadDirectory(m_testDir
->url());
2432 QVERIFY(itemsInsertedSpy
.wait());
2434 // So far, the model contains only "a/"
2435 QCOMPARE(m_model
->count(), 1);
2436 QVERIFY(m_model
->isExpandable(0));
2437 QVERIFY(!m_model
->isExpanded(0));
2438 QVERIFY(m_model
->expandedDirectories().empty());
2440 QCOMPARE(itemsInsertedSpy
.count(), 1);
2442 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2443 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
2444 QCOMPARE(m_model
->expandedParentsCount(0), 0);
2446 itemsInsertedSpy
.clear();
2448 // Expand the folder "a/" -> "a/b" become visible
2449 m_model
->setExpanded(0, true);
2450 QVERIFY(m_model
->isExpanded(0));
2451 QVERIFY(itemsInsertedSpy
.wait());
2452 QCOMPARE(m_model
->count(), 2); // 3 items: "a/", "a/a/"
2453 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>({QUrl::fromLocalFile(m_testDir
->path() + "/a")}));
2455 QCOMPARE(itemsInsertedSpy
.count(), 1);
2457 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2458 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1)); // 1 new item "a/b" with index 1
2459 QCOMPARE(m_model
->expandedParentsCount(1), 1);
2461 itemsInsertedSpy
.clear();
2463 // Expand "a/b" -> "a/b/1" becomes visible
2464 m_model
->setExpanded(1, true);
2465 QVERIFY(itemsInsertedSpy
.wait());
2466 QCOMPARE(m_model
->expandedDirectories(), QSet({QUrl::fromLocalFile(m_testDir
->path() + "/a"), QUrl::fromLocalFile(m_testDir
->path() + "/a/b")}));
2468 QCOMPARE(itemsInsertedSpy
.count(), 1);
2470 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
2471 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/b/1" with index 2
2472 QCOMPARE(m_model
->expandedParentsCount(2), 2);
2474 itemsInsertedSpy
.clear();
2476 // Collapse "a" whilst leaving "b" expanded
2477 m_model
->setExpanded(0, false);
2479 // Insert additional files into "a/b/"
2480 m_testDir
->createFile("a/b/2");
2482 QVERIFY(!itemsInsertedSpy
.wait(5000));
2484 QCOMPARE(itemsInModel(), {"a"});
2486 m_model
->setExpanded(0, true);
2488 QTRY_COMPARE(itemsInModel(), QStringList({"a", "b", "1", "2"}));
2490 QCOMPARE(m_model
->expandedParentsCount(0), 0); // a
2491 QCOMPARE(m_model
->expandedParentsCount(1), 1); // a/b
2492 QCOMPARE(m_model
->expandedParentsCount(2), 2); // a/b/1
2493 QCOMPARE(m_model
->expandedParentsCount(3), 2); // a/b/2
2496 void KFileItemModelTest::testCurrentDirRemoved()
2498 m_model
->m_dirLister
->setAutoUpdate(true);
2499 QSignalSpy
currentDirectoryRemovedSpy(m_model
, &KFileItemModel::currentDirectoryRemoved
);
2500 QVERIFY(currentDirectoryRemovedSpy
.isValid());
2501 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
2502 QVERIFY(loadingCompletedSpy
.isValid());
2503 QSignalSpy
dirListerClearSpy(m_model
->m_dirLister
, &KCoreDirLister::clear
);
2504 QVERIFY(dirListerClearSpy
.isValid());
2506 m_testDir
->createFiles({"dir/a.txt", "dir/b.txt"});
2507 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/dir/"));
2508 QVERIFY(loadingCompletedSpy
.wait());
2509 QCOMPARE(m_model
->count(), 2);
2510 QVERIFY(m_model
->isConsistent());
2512 m_testDir
->removeDir("dir");
2513 QVERIFY(currentDirectoryRemovedSpy
.wait());
2515 // dirLister calls clear
2516 QCOMPARE(dirListerClearSpy
.count(), 2);
2517 QVERIFY(m_model
->isConsistent());
2518 QVERIFY(m_model
->m_itemData
.isEmpty());
2519 QCOMPARE(m_model
->count(), 0);
2522 QStringList
KFileItemModelTest::itemsInModel() const
2525 for (int i
= 0; i
< m_model
->count(); i
++) {
2526 items
<< m_model
->fileItem(i
).text();
2531 QTEST_MAIN(KFileItemModelTest
)
2533 #include "kfileitemmodeltest.moc"