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
8 #include <QRandomGenerator>
16 #include "kitemviews/kfileitemmodel.h"
17 #include "kitemviews/private/kfileitemmodeldirlister.h"
20 void myMessageOutput(QtMsgType type
, const QMessageLogContext
& context
, const QString
& msg
)
30 fprintf(stderr
, "Critical: %s\n", msg
.toLocal8Bit().data());
33 fprintf(stderr
, "Fatal: %s\n", msg
.toLocal8Bit().data());
40 Q_DECLARE_METATYPE(KItemRange
)
41 Q_DECLARE_METATYPE(KItemRangeList
)
42 Q_DECLARE_METATYPE(QList
<int>)
44 class KFileItemModelTest
: public QObject
52 void testDefaultRoles();
53 void testDefaultSortRole();
54 void testDefaultGroupedSorting();
56 void testRemoveItems();
57 void testDirLoadingCompleted();
59 void testSetDataWithModifiedSortRole_data();
60 void testSetDataWithModifiedSortRole();
61 void testChangeSortRole();
62 void testResortAfterChangingName();
63 void testModelConsistencyWhenInsertingItems();
64 void testItemRangeConsistencyWhenInsertingItems();
65 void testExpandItems();
66 void testExpandParentItems();
67 void testMakeExpandedItemHidden();
68 void testRemoveFilteredExpandedItems();
70 void testIndexForKeyboardSearch();
71 void testNameFilter();
73 void testRefreshExpandedItem();
74 void testRemoveHiddenItems();
75 void collapseParentOfHiddenItems();
76 void removeParentOfHiddenItems();
77 void testGeneralParentChildRelationships();
78 void testNameRoleGroups();
79 void testNameRoleGroupsWithExpandedItems();
80 void testInconsistentModel();
81 void testChangeRolesForFilteredItems();
82 void testChangeSortRoleWhileFiltering();
83 void testRefreshFilteredItems();
84 void testCollapseFolderWhileLoading();
85 void testCreateMimeData();
86 void testDeleteFileMoreThanOnce();
89 QStringList
itemsInModel() const;
92 KFileItemModel
* m_model
;
96 void KFileItemModelTest::init()
98 // The item-model tests result in a huge number of debugging
99 // output from kdelibs. Only show critical and fatal messages.
100 qInstallMessageHandler(myMessageOutput
);
102 qRegisterMetaType
<KItemRange
>("KItemRange");
103 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
104 qRegisterMetaType
<KFileItemList
>("KFileItemList");
106 m_testDir
= new TestDir();
107 m_model
= new KFileItemModel();
108 m_model
->m_dirLister
->setAutoUpdate(false);
110 // Reduce the timer interval to make the test run faster.
111 m_model
->m_resortAllItemsTimer
->setInterval(0);
114 void KFileItemModelTest::cleanup()
123 void KFileItemModelTest::testDefaultRoles()
125 const QSet
<QByteArray
> roles
= m_model
->roles();
126 QCOMPARE(roles
.count(), 4);
127 QVERIFY(roles
.contains("text"));
128 QVERIFY(roles
.contains("isDir"));
129 QVERIFY(roles
.contains("isLink"));
130 QVERIFY(roles
.contains("isHidden"));
133 void KFileItemModelTest::testDefaultSortRole()
135 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
136 QVERIFY(itemsInsertedSpy
.isValid());
138 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
140 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
142 m_model
->loadDirectory(m_testDir
->url());
143 QVERIFY(itemsInsertedSpy
.wait());
145 QCOMPARE(m_model
->count(), 3);
146 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
147 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
148 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
151 void KFileItemModelTest::testDefaultGroupedSorting()
153 QCOMPARE(m_model
->groupedSorting(), false);
156 void KFileItemModelTest::testNewItems()
158 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
160 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
162 m_model
->loadDirectory(m_testDir
->url());
163 QVERIFY(itemsInsertedSpy
.wait());
165 QCOMPARE(m_model
->count(), 3);
167 QVERIFY(m_model
->isConsistent());
170 void KFileItemModelTest::testRemoveItems()
172 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
173 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
175 m_testDir
->createFiles({"a.txt", "b.txt"});
176 m_model
->loadDirectory(m_testDir
->url());
177 QVERIFY(itemsInsertedSpy
.wait());
178 QCOMPARE(m_model
->count(), 2);
179 QVERIFY(m_model
->isConsistent());
181 m_testDir
->removeFile("a.txt");
182 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
183 QVERIFY(itemsRemovedSpy
.wait());
184 QCOMPARE(m_model
->count(), 1);
185 QVERIFY(m_model
->isConsistent());
188 void KFileItemModelTest::testDirLoadingCompleted()
190 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
191 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
192 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
194 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
196 m_model
->loadDirectory(m_testDir
->url());
197 QVERIFY(loadingCompletedSpy
.wait());
198 QCOMPARE(loadingCompletedSpy
.count(), 1);
199 QCOMPARE(itemsInsertedSpy
.count(), 1);
200 QCOMPARE(itemsRemovedSpy
.count(), 0);
201 QCOMPARE(m_model
->count(), 3);
203 m_testDir
->createFiles({"d.txt", "e.txt"});
204 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
205 QVERIFY(loadingCompletedSpy
.wait());
206 QCOMPARE(loadingCompletedSpy
.count(), 2);
207 QCOMPARE(itemsInsertedSpy
.count(), 2);
208 QCOMPARE(itemsRemovedSpy
.count(), 0);
209 QCOMPARE(m_model
->count(), 5);
211 m_testDir
->removeFile("a.txt");
212 m_testDir
->createFile("f.txt");
213 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
214 QVERIFY(loadingCompletedSpy
.wait());
215 QCOMPARE(loadingCompletedSpy
.count(), 3);
216 QCOMPARE(itemsInsertedSpy
.count(), 3);
217 QCOMPARE(itemsRemovedSpy
.count(), 1);
218 QCOMPARE(m_model
->count(), 5);
220 m_testDir
->removeFile("b.txt");
221 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
222 QVERIFY(itemsRemovedSpy
.wait());
223 QCOMPARE(loadingCompletedSpy
.count(), 4);
224 QCOMPARE(itemsInsertedSpy
.count(), 3);
225 QCOMPARE(itemsRemovedSpy
.count(), 2);
226 QCOMPARE(m_model
->count(), 4);
228 QVERIFY(m_model
->isConsistent());
231 void KFileItemModelTest::testSetData()
233 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
234 QVERIFY(itemsInsertedSpy
.isValid());
235 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
236 QVERIFY(itemsChangedSpy
.isValid());
238 m_testDir
->createFile("a.txt");
240 m_model
->loadDirectory(m_testDir
->url());
241 QVERIFY(itemsInsertedSpy
.wait());
243 QHash
<QByteArray
, QVariant
> values
;
244 values
.insert("customRole1", "Test1");
245 values
.insert("customRole2", "Test2");
247 m_model
->setData(0, values
);
248 QCOMPARE(itemsChangedSpy
.count(), 1);
250 values
= m_model
->data(0);
251 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
252 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
253 QVERIFY(m_model
->isConsistent());
256 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
258 QTest::addColumn
<int>("changedIndex");
259 QTest::addColumn
<int>("changedRating");
260 QTest::addColumn
<bool>("expectMoveSignal");
261 QTest::addColumn
<int>("ratingIndex0");
262 QTest::addColumn
<int>("ratingIndex1");
263 QTest::addColumn
<int>("ratingIndex2");
266 // Index 0 = rating 2
267 // Index 1 = rating 4
268 // Index 2 = rating 6
270 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
271 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
272 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
274 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
275 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
276 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
279 void KFileItemModelTest::testSetDataWithModifiedSortRole()
281 QFETCH(int, changedIndex
);
282 QFETCH(int, changedRating
);
283 QFETCH(bool, expectMoveSignal
);
284 QFETCH(int, ratingIndex0
);
285 QFETCH(int, ratingIndex1
);
286 QFETCH(int, ratingIndex2
);
288 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
289 QVERIFY(itemsInsertedSpy
.isValid());
290 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
291 QVERIFY(itemsMovedSpy
.isValid());
293 // Changing the value of a sort-role must result in
294 // a reordering of the items.
295 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
296 m_model
->setSortRole("rating");
297 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
299 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
301 m_model
->loadDirectory(m_testDir
->url());
302 QVERIFY(itemsInsertedSpy
.wait());
304 // Fill the "rating" role of each file:
309 QHash
<QByteArray
, QVariant
> ratingA
;
310 ratingA
.insert("rating", 2);
311 m_model
->setData(0, ratingA
);
313 QHash
<QByteArray
, QVariant
> ratingB
;
314 ratingB
.insert("rating", 4);
315 m_model
->setData(1, ratingB
);
317 QHash
<QByteArray
, QVariant
> ratingC
;
318 ratingC
.insert("rating", 6);
319 m_model
->setData(2, ratingC
);
321 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
322 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
323 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
325 // Now change the rating from a.txt. This usually results
326 // in reordering of the items.
327 QHash
<QByteArray
, QVariant
> rating
;
328 rating
.insert("rating", changedRating
);
329 m_model
->setData(changedIndex
, rating
);
331 if (expectMoveSignal
) {
332 QVERIFY(itemsMovedSpy
.wait());
335 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
336 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
337 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
338 QVERIFY(m_model
->isConsistent());
341 void KFileItemModelTest::testChangeSortRole()
343 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
344 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
345 QVERIFY(itemsMovedSpy
.isValid());
347 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
349 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
351 m_model
->loadDirectory(m_testDir
->url());
352 QVERIFY(itemsInsertedSpy
.wait());
353 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
355 // Simulate that KFileItemModelRolesUpdater determines the mime type.
356 // Resorting the files by 'type' will only work immediately if their
357 // mime types are known.
358 for (int index
= 0; index
< m_model
->count(); ++index
) {
359 m_model
->fileItem(index
).determineMimeType();
362 // Now: sort by type.
363 m_model
->setSortRole("type");
364 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
365 QVERIFY(!itemsMovedSpy
.isEmpty());
367 // The actual order of the files might depend on the translation of the
368 // result of KFileItem::mimeComment() in the user's language.
369 QStringList version1
;
370 version1
<< "b.jpg" << "a.txt" << "c.txt";
372 QStringList version2
;
373 version2
<< "a.txt" << "c.txt" << "b.jpg";
375 const bool ok1
= (itemsInModel() == version1
);
376 const bool ok2
= (itemsInModel() == version2
);
381 void KFileItemModelTest::testResortAfterChangingName()
383 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
384 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
385 QVERIFY(itemsMovedSpy
.isValid());
387 // We sort by size in a directory where all files have the same size.
388 // Therefore, the files are sorted by their names.
389 m_model
->setSortRole("size");
391 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
393 m_model
->loadDirectory(m_testDir
->url());
394 QVERIFY(itemsInsertedSpy
.wait());
395 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
397 // We rename a.txt to d.txt. Even though the size has not changed at all,
398 // the model must re-sort the items.
399 QHash
<QByteArray
, QVariant
> data
;
400 data
.insert("text", "d.txt");
401 m_model
->setData(0, data
);
403 QVERIFY(itemsMovedSpy
.wait());
404 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
406 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
407 const KFileItem fileItemD
= m_model
->fileItem(2);
408 KFileItem fileItemA
= fileItemD
;
409 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
410 urlA
.setPath(urlA
.path() + "a.txt");
411 fileItemA
.setUrl(urlA
);
413 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
415 QVERIFY(itemsMovedSpy
.wait());
416 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
419 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
421 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
423 // KFileItemModel prevents that inserting a punch of items sequentially
424 // results in an itemsInserted()-signal for each item. Instead internally
425 // a timeout is given that collects such operations and results in only
426 // one itemsInserted()-signal. However in this test we want to stress
427 // KFileItemModel to do a lot of insert operation and hence decrease
428 // the timeout to 1 millisecond.
429 m_testDir
->createFile("1");
430 m_model
->loadDirectory(m_testDir
->url());
431 QVERIFY(itemsInsertedSpy
.wait());
432 QCOMPARE(m_model
->count(), 1);
434 // Insert 10 items for 20 times. After each insert operation the model consistency
436 QSet
<int> insertedItems
;
437 for (int i
= 0; i
< 20; ++i
) {
438 itemsInsertedSpy
.clear();
440 for (int j
= 0; j
< 10; ++j
) {
441 int itemName
= QRandomGenerator::global()->generate();
442 while (insertedItems
.contains(itemName
)) {
443 itemName
= QRandomGenerator::global()->generate();
445 insertedItems
.insert(itemName
);
447 m_testDir
->createFile(QString::number(itemName
));
450 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
451 if (itemsInsertedSpy
.isEmpty()) {
452 QVERIFY(itemsInsertedSpy
.wait());
455 QVERIFY(m_model
->isConsistent());
458 QCOMPARE(m_model
->count(), 201);
461 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
463 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
465 m_testDir
->createFiles({"B", "E", "G"});
467 // Due to inserting the 3 items one item-range with index == 0 and
468 // count == 3 must be given
469 m_model
->loadDirectory(m_testDir
->url());
470 QVERIFY(itemsInsertedSpy
.wait());
472 QCOMPARE(itemsInsertedSpy
.count(), 1);
473 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
474 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
475 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
477 // The indexes of the item-ranges must always be related to the model before
478 // the items have been inserted. Having:
481 // and inserting A, C, D, F the resulting model will be:
484 // and the item-ranges must be:
485 // index: 0, count: 1 for A
486 // index: 1, count: 2 for B, C
487 // index: 2, count: 1 for G
489 m_testDir
->createFiles({"A", "C", "D", "F"});
491 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
492 QVERIFY(itemsInsertedSpy
.wait());
494 QCOMPARE(itemsInsertedSpy
.count(), 1);
495 arguments
= itemsInsertedSpy
.takeFirst();
496 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
497 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
500 void KFileItemModelTest::testExpandItems()
502 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
503 QVERIFY(itemsInsertedSpy
.isValid());
504 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
505 QVERIFY(itemsRemovedSpy
.isValid());
506 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
507 QVERIFY(loadingCompletedSpy
.isValid());
509 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
510 // Besides testing the basic item expansion functionality, the test makes sure that
511 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
512 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
513 // first three characters.
514 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
515 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
516 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
517 m_model
->setRoles(modelRoles
);
519 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
521 // Store the URLs of all folders in a set.
522 QSet
<QUrl
> allFolders
;
523 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a")
524 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
525 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
527 m_model
->loadDirectory(m_testDir
->url());
528 QVERIFY(itemsInsertedSpy
.wait());
530 // So far, the model contains only "a/"
531 QCOMPARE(m_model
->count(), 1);
532 QVERIFY(m_model
->isExpandable(0));
533 QVERIFY(!m_model
->isExpanded(0));
534 QVERIFY(m_model
->expandedDirectories().empty());
536 QCOMPARE(itemsInsertedSpy
.count(), 1);
537 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
538 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
540 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
541 m_model
->setExpanded(0, true);
542 QVERIFY(m_model
->isExpanded(0));
543 QVERIFY(itemsInsertedSpy
.wait());
544 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
545 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
547 QCOMPARE(itemsInsertedSpy
.count(), 1);
548 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
549 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
551 QVERIFY(m_model
->isExpandable(1));
552 QVERIFY(!m_model
->isExpanded(1));
553 QVERIFY(m_model
->isExpandable(2));
554 QVERIFY(!m_model
->isExpanded(2));
556 // Expand the folder "a/a/" -> "a/a/1" becomes visible
557 m_model
->setExpanded(1, true);
558 QVERIFY(m_model
->isExpanded(1));
559 QVERIFY(itemsInsertedSpy
.wait());
560 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
561 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a")
562 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
564 QCOMPARE(itemsInsertedSpy
.count(), 1);
565 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
566 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
568 QVERIFY(!m_model
->isExpandable(2));
569 QVERIFY(!m_model
->isExpanded(2));
571 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
572 m_model
->setExpanded(3, true);
573 QVERIFY(m_model
->isExpanded(3));
574 QVERIFY(itemsInsertedSpy
.wait());
575 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
576 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
578 QCOMPARE(itemsInsertedSpy
.count(), 1);
579 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
580 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
582 QVERIFY(!m_model
->isExpandable(4));
583 QVERIFY(!m_model
->isExpanded(4));
585 // Collapse the top-level folder -> all other items should disappear
586 m_model
->setExpanded(0, false);
587 QVERIFY(!m_model
->isExpanded(0));
588 QCOMPARE(m_model
->count(), 1);
589 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
591 QCOMPARE(itemsRemovedSpy
.count(), 1);
592 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
593 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
594 QVERIFY(m_model
->isConsistent());
596 // Clear the model, reload the folder and try to restore the expanded folders.
598 QCOMPARE(m_model
->count(), 0);
599 QVERIFY(m_model
->expandedDirectories().empty());
601 m_model
->loadDirectory(m_testDir
->url());
602 m_model
->restoreExpandedDirectories(allFolders
);
603 QVERIFY(loadingCompletedSpy
.wait());
604 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
605 QVERIFY(m_model
->isExpanded(0));
606 QVERIFY(m_model
->isExpanded(1));
607 QVERIFY(!m_model
->isExpanded(2));
608 QVERIFY(m_model
->isExpanded(3));
609 QVERIFY(!m_model
->isExpanded(4));
610 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
611 QVERIFY(m_model
->isConsistent());
613 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
614 // This is how DolphinView restores the expanded folders when navigating in history.
615 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
616 QVERIFY(loadingCompletedSpy
.wait());
617 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
618 m_model
->restoreExpandedDirectories(allFolders
);
619 m_model
->loadDirectory(m_testDir
->url());
620 QVERIFY(loadingCompletedSpy
.wait());
621 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
622 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
624 // Remove all expanded items by changing the roles
625 itemsRemovedSpy
.clear();
626 m_model
->setRoles(originalModelRoles
);
627 QVERIFY(!m_model
->isExpanded(0));
628 QCOMPARE(m_model
->count(), 1);
629 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
631 QCOMPARE(itemsRemovedSpy
.count(), 1);
632 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
633 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
634 QVERIFY(m_model
->isConsistent());
637 void KFileItemModelTest::testExpandParentItems()
639 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
640 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
641 QVERIFY(loadingCompletedSpy
.isValid());
643 // Create a tree structure of folders:
651 QSet
<QByteArray
> modelRoles
= m_model
->roles();
652 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
653 m_model
->setRoles(modelRoles
);
655 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
657 m_model
->loadDirectory(m_testDir
->url());
658 QVERIFY(itemsInsertedSpy
.wait());
660 // So far, the model contains only "a 1/" and "a2/".
661 QCOMPARE(m_model
->count(), 2);
662 QVERIFY(m_model
->expandedDirectories().empty());
664 // Expand the parents of "a2/b2/c2".
665 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
666 QVERIFY(loadingCompletedSpy
.wait());
668 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
669 // It's important that only the parents of "a1/b1/c1" are expanded.
670 QCOMPARE(m_model
->count(), 4);
671 QVERIFY(!m_model
->isExpanded(0));
672 QVERIFY(m_model
->isExpanded(1));
673 QVERIFY(m_model
->isExpanded(2));
674 QVERIFY(!m_model
->isExpanded(3));
676 // Expand the parents of "a 1/b1".
677 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
678 QVERIFY(loadingCompletedSpy
.wait());
680 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
681 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
682 QCOMPARE(m_model
->count(), 5);
683 QVERIFY(m_model
->isExpanded(0));
684 QVERIFY(!m_model
->isExpanded(1));
685 QVERIFY(m_model
->isExpanded(2));
686 QVERIFY(m_model
->isExpanded(3));
687 QVERIFY(!m_model
->isExpanded(4));
688 QVERIFY(m_model
->isConsistent());
691 m_model
->setExpanded(1, true);
692 QVERIFY(loadingCompletedSpy
.wait());
693 QCOMPARE(m_model
->count(), 6);
694 QVERIFY(m_model
->isExpanded(0));
695 QVERIFY(m_model
->isExpanded(1));
696 QVERIFY(!m_model
->isExpanded(2));
697 QVERIFY(m_model
->isExpanded(3));
698 QVERIFY(m_model
->isExpanded(4));
699 QVERIFY(!m_model
->isExpanded(5));
700 QVERIFY(m_model
->isConsistent());
702 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
703 m_model
->setExpanded(1, false);
704 QCOMPARE(m_model
->count(), 5);
705 QVERIFY(m_model
->isExpanded(0));
706 QVERIFY(!m_model
->isExpanded(1));
707 QVERIFY(m_model
->isExpanded(2));
708 QVERIFY(m_model
->isExpanded(3));
709 QVERIFY(!m_model
->isExpanded(4));
710 QVERIFY(m_model
->isConsistent());
714 * Renaming an expanded folder by prepending its name with a dot makes it
715 * hidden. Verify that this does not cause an inconsistent model state and
716 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
718 void KFileItemModelTest::testMakeExpandedItemHidden()
720 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
721 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
723 QSet
<QByteArray
> modelRoles
= m_model
->roles();
724 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
725 m_model
->setRoles(modelRoles
);
727 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
729 m_model
->loadDirectory(m_testDir
->url());
730 QVERIFY(itemsInsertedSpy
.wait());
732 // So far, the model contains only "1a/" and "1b".
733 QCOMPARE(m_model
->count(), 2);
734 m_model
->setExpanded(0, true);
735 QVERIFY(itemsInsertedSpy
.wait());
737 // Now "1a/2a" and "1a/2b" have appeared.
738 QCOMPARE(m_model
->count(), 4);
739 m_model
->setExpanded(1, true);
740 QVERIFY(itemsInsertedSpy
.wait());
741 QCOMPARE(m_model
->count(), 6);
743 // Rename "1a/2" and make it hidden.
744 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
745 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
747 KIO::SimpleJob
* job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
748 bool ok
= job
->exec();
750 QVERIFY(itemsRemovedSpy
.wait());
752 // "1a/2" and its subfolders have disappeared now.
753 QVERIFY(m_model
->isConsistent());
754 QCOMPARE(m_model
->count(), 3);
756 m_model
->setExpanded(0, false);
757 QCOMPARE(m_model
->count(), 2);
761 void KFileItemModelTest::testRemoveFilteredExpandedItems()
763 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
765 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
766 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
767 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
768 m_model
->setRoles(modelRoles
);
770 m_testDir
->createFiles({"folder/child", "file"});
772 m_model
->loadDirectory(m_testDir
->url());
773 QVERIFY(itemsInsertedSpy
.wait());
775 // So far, the model contains only "folder/" and "file".
776 QCOMPARE(m_model
->count(), 2);
777 QVERIFY(m_model
->isExpandable(0));
778 QVERIFY(!m_model
->isExpandable(1));
779 QVERIFY(!m_model
->isExpanded(0));
780 QVERIFY(!m_model
->isExpanded(1));
781 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
783 // Expand "folder" -> "folder/child" becomes visible.
784 m_model
->setExpanded(0, true);
785 QVERIFY(m_model
->isExpanded(0));
786 QVERIFY(itemsInsertedSpy
.wait());
787 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
789 // Add a name filter.
790 m_model
->setNameFilter("f");
791 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
793 m_model
->setNameFilter("fo");
794 QCOMPARE(itemsInModel(), QStringList() << "folder");
796 // Remove all expanded items by changing the roles
797 m_model
->setRoles(originalModelRoles
);
798 QVERIFY(!m_model
->isExpanded(0));
799 QCOMPARE(itemsInModel(), QStringList() << "folder");
801 // Remove the name filter and verify that "folder/child" does not reappear.
802 m_model
->setNameFilter(QString());
803 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
806 void KFileItemModelTest::testSorting()
808 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
809 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
810 QVERIFY(itemsMovedSpy
.isValid());
812 // Create some files with different sizes and modification times to check the different sorting options
813 QDateTime now
= QDateTime::currentDateTime();
815 QSet
<QByteArray
> roles
;
816 roles
.insert("text");
817 roles
.insert("isExpanded");
818 roles
.insert("isExpandable");
819 roles
.insert("expandedParentsCount");
820 m_model
->setRoles(roles
);
822 m_testDir
->createDir("c/c-2");
823 m_testDir
->createFile("c/c-2/c-3");
824 m_testDir
->createFile("c/c-1");
826 m_testDir
->createFile("a", "A file", now
.addDays(-3));
827 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
828 m_testDir
->createDir("c", now
.addDays(-2));
829 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
830 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
831 m_testDir
->createFile(".f");
833 m_model
->loadDirectory(m_testDir
->url());
834 QVERIFY(itemsInsertedSpy
.wait());
836 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
837 m_model
->setExpanded(index
, true);
838 QVERIFY(itemsInsertedSpy
.wait());
840 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
841 m_model
->setExpanded(index
, true);
842 QVERIFY(itemsInsertedSpy
.wait());
844 // Default: Sort by Name, ascending
845 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
846 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
847 QVERIFY(m_model
->sortDirectoriesFirst());
848 QVERIFY(!m_model
->showHiddenFiles());
849 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
851 // Sort by Name, ascending, 'Sort Folders First' disabled
852 m_model
->setSortDirectoriesFirst(false);
853 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
854 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
855 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
856 QCOMPARE(itemsMovedSpy
.count(), 1);
857 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
858 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
860 // Sort by Name, descending
861 m_model
->setSortDirectoriesFirst(true);
862 m_model
->setSortOrder(Qt::DescendingOrder
);
863 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
864 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
865 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
866 QCOMPARE(itemsMovedSpy
.count(), 2);
867 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
868 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
869 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
870 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
872 // Sort by Date, descending
873 m_model
->setSortDirectoriesFirst(true);
874 m_model
->setSortRole("modificationtime");
875 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
876 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
877 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
878 QCOMPARE(itemsMovedSpy
.count(), 1);
879 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
880 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
882 // Sort by Date, ascending
883 m_model
->setSortOrder(Qt::AscendingOrder
);
884 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
885 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
886 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
887 QCOMPARE(itemsMovedSpy
.count(), 1);
888 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
889 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
891 // Sort by Date, ascending, 'Sort Folders First' disabled
892 m_model
->setSortDirectoriesFirst(false);
893 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
894 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
895 QVERIFY(!m_model
->sortDirectoriesFirst());
896 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
897 QCOMPARE(itemsMovedSpy
.count(), 1);
898 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
899 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
901 // Sort by Name, ascending, 'Sort Folders First' disabled
902 m_model
->setSortRole("text");
903 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
904 QVERIFY(!m_model
->sortDirectoriesFirst());
905 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
906 QCOMPARE(itemsMovedSpy
.count(), 1);
907 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
908 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
910 // Sort by Size, ascending, 'Sort Folders First' disabled
911 m_model
->setSortRole("size");
912 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
913 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
914 QVERIFY(!m_model
->sortDirectoriesFirst());
915 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
916 QCOMPARE(itemsMovedSpy
.count(), 1);
917 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
918 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
920 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
921 m_model
->setSortDirectoriesFirst(true);
922 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
923 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
924 QVERIFY(m_model
->sortDirectoriesFirst());
925 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
926 QCOMPARE(itemsMovedSpy
.count(), 0);
928 // Sort by Size, descending, 'Sort Folders First' enabled
929 m_model
->setSortOrder(Qt::DescendingOrder
);
930 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
931 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
932 QVERIFY(m_model
->sortDirectoriesFirst());
933 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
934 QCOMPARE(itemsMovedSpy
.count(), 1);
935 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
936 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
938 // TODO: Sort by other roles; show/hide hidden files
941 void KFileItemModelTest::testIndexForKeyboardSearch()
943 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
945 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
947 m_model
->loadDirectory(m_testDir
->url());
948 QVERIFY(itemsInsertedSpy
.wait());
950 // Search from index 0
951 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
952 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
953 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
954 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
955 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
956 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
957 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
958 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
959 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
960 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
961 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
963 // Start a search somewhere in the middle
964 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
965 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
966 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
967 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
969 // Test searches that go past the last item back to index 0
970 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
971 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
972 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
973 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
975 // Test searches that yield no result
976 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
977 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
978 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
979 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
980 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
982 // Test upper case searches (note that search is case insensitive)
983 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
984 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
985 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
986 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
988 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
991 void KFileItemModelTest::testNameFilter()
993 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
995 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
997 m_model
->loadDirectory(m_testDir
->url());
998 QVERIFY(itemsInsertedSpy
.wait());
1000 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1001 QCOMPARE(m_model
->count(), 3);
1003 m_model
->setNameFilter("A2"); // Shows only A2
1004 QCOMPARE(m_model
->count(), 1);
1006 m_model
->setNameFilter("A2"); // Shows only A1
1007 QCOMPARE(m_model
->count(), 1);
1009 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1010 QCOMPARE(m_model
->count(), 2);
1012 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1013 QCOMPARE(m_model
->count(), 2);
1015 m_model
->setNameFilter(QString()); // Shows again all items
1016 QCOMPARE(m_model
->count(), 5);
1020 * Verifies that we do not crash when adding a KFileItem with an empty path.
1021 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1022 * tried to always read the first character of the path, even if the path is empty.
1024 void KFileItemModelTest::testEmptyPath()
1026 QSet
<QByteArray
> roles
;
1027 roles
.insert("text");
1028 roles
.insert("isExpanded");
1029 roles
.insert("isExpandable");
1030 roles
.insert("expandedParentsCount");
1031 m_model
->setRoles(roles
);
1033 const QUrl emptyUrl
;
1034 QVERIFY(emptyUrl
.path().isEmpty());
1036 const QUrl
url("file:///test/");
1038 KFileItemList items
;
1039 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1040 m_model
->slotItemsAdded(emptyUrl
, items
);
1041 m_model
->slotCompleted();
1045 * Verifies that the 'isExpanded' state of folders does not change when the
1046 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1048 void KFileItemModelTest::testRefreshExpandedItem()
1050 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1051 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1052 QVERIFY(itemsChangedSpy
.isValid());
1054 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1055 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1056 m_model
->setRoles(modelRoles
);
1058 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1060 m_model
->loadDirectory(m_testDir
->url());
1061 QVERIFY(itemsInsertedSpy
.wait());
1062 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1064 m_model
->setExpanded(0, true);
1065 QVERIFY(itemsInsertedSpy
.wait());
1066 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1067 QVERIFY(m_model
->isExpanded(0));
1069 const KFileItem item
= m_model
->fileItem(0);
1070 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1071 QVERIFY(!itemsChangedSpy
.isEmpty());
1073 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1074 QVERIFY(m_model
->isExpanded(0));
1078 * Verify that removing hidden files and folders from the model does not
1079 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1081 void KFileItemModelTest::testRemoveHiddenItems()
1083 m_testDir
->createDir(".a");
1084 m_testDir
->createDir(".b");
1085 m_testDir
->createDir("c");
1086 m_testDir
->createDir("d");
1087 m_testDir
->createFiles({".f", ".g", "h", "i"});
1089 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1090 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1092 m_model
->setShowHiddenFiles(true);
1093 m_model
->loadDirectory(m_testDir
->url());
1094 QVERIFY(itemsInsertedSpy
.wait());
1095 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1096 QCOMPARE(itemsInsertedSpy
.count(), 1);
1097 QCOMPARE(itemsRemovedSpy
.count(), 0);
1098 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1099 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1101 m_model
->setShowHiddenFiles(false);
1102 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1103 QCOMPARE(itemsInsertedSpy
.count(), 0);
1104 QCOMPARE(itemsRemovedSpy
.count(), 1);
1105 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1106 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1108 m_model
->setShowHiddenFiles(true);
1109 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1110 QCOMPARE(itemsInsertedSpy
.count(), 1);
1111 QCOMPARE(itemsRemovedSpy
.count(), 0);
1112 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1113 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1116 QCOMPARE(itemsInModel(), QStringList());
1117 QCOMPARE(itemsInsertedSpy
.count(), 0);
1118 QCOMPARE(itemsRemovedSpy
.count(), 1);
1119 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1120 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1122 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1123 // Verify that this does not make the model crash.
1124 m_model
->setShowHiddenFiles(false);
1128 * Verify that filtered items are removed when their parent is collapsed.
1130 void KFileItemModelTest::collapseParentOfHiddenItems()
1132 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1133 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1135 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1136 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1137 m_model
->setRoles(modelRoles
);
1139 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1141 m_model
->loadDirectory(m_testDir
->url());
1142 QVERIFY(itemsInsertedSpy
.wait());
1143 QCOMPARE(m_model
->count(), 1); // Only "a/"
1146 m_model
->setExpanded(0, true);
1147 QVERIFY(itemsInsertedSpy
.wait());
1148 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1151 m_model
->setExpanded(1, true);
1152 QVERIFY(itemsInsertedSpy
.wait());
1153 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1156 m_model
->setExpanded(2, true);
1157 QVERIFY(itemsInsertedSpy
.wait());
1158 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"
1160 // Set a name filter that matches nothing -> only the expanded folders remain.
1161 m_model
->setNameFilter("xyz");
1162 QCOMPARE(itemsRemovedSpy
.count(), 1);
1163 QCOMPARE(m_model
->count(), 3);
1164 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1166 // Collapse the folder "a/".
1167 m_model
->setExpanded(0, false);
1168 QCOMPARE(itemsRemovedSpy
.count(), 2);
1169 QCOMPARE(m_model
->count(), 1);
1170 QCOMPARE(itemsInModel(), QStringList() << "a");
1172 // Remove the filter -> no files should appear (and we should not get a crash).
1173 m_model
->setNameFilter(QString());
1174 QCOMPARE(m_model
->count(), 1);
1178 * Verify that filtered items are removed when their parent is deleted.
1180 void KFileItemModelTest::removeParentOfHiddenItems()
1182 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1183 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1185 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1186 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1187 m_model
->setRoles(modelRoles
);
1189 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1191 m_model
->loadDirectory(m_testDir
->url());
1192 QVERIFY(itemsInsertedSpy
.wait());
1193 QCOMPARE(m_model
->count(), 1); // Only "a/"
1196 m_model
->setExpanded(0, true);
1197 QVERIFY(itemsInsertedSpy
.wait());
1198 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1201 m_model
->setExpanded(1, true);
1202 QVERIFY(itemsInsertedSpy
.wait());
1203 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1206 m_model
->setExpanded(2, true);
1207 QVERIFY(itemsInsertedSpy
.wait());
1208 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"
1210 // Set a name filter that matches nothing -> only the expanded folders remain.
1211 m_model
->setNameFilter("xyz");
1212 QCOMPARE(itemsRemovedSpy
.count(), 1);
1213 QCOMPARE(m_model
->count(), 3);
1214 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1216 // Simulate the deletion of the directory "a/b/".
1217 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1218 QCOMPARE(itemsRemovedSpy
.count(), 2);
1219 QCOMPARE(m_model
->count(), 1);
1220 QCOMPARE(itemsInModel(), QStringList() << "a");
1222 // Remove the filter -> only the file "a/1" should appear.
1223 m_model
->setNameFilter(QString());
1224 QCOMPARE(m_model
->count(), 2);
1225 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1229 * Create a tree structure where parent-child relationships can not be
1230 * determined by parsing the URLs, and verify that KFileItemModel
1231 * handles them correctly.
1233 void KFileItemModelTest::testGeneralParentChildRelationships()
1235 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1236 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1238 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1239 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1240 m_model
->setRoles(modelRoles
);
1242 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1244 m_model
->loadDirectory(m_testDir
->url());
1245 QVERIFY(itemsInsertedSpy
.wait());
1246 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1248 // Expand all folders.
1249 m_model
->setExpanded(0, true);
1250 QVERIFY(itemsInsertedSpy
.wait());
1251 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1253 m_model
->setExpanded(1, true);
1254 QVERIFY(itemsInsertedSpy
.wait());
1255 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1257 m_model
->setExpanded(3, true);
1258 QVERIFY(itemsInsertedSpy
.wait());
1259 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1261 m_model
->setExpanded(4, true);
1262 QVERIFY(itemsInsertedSpy
.wait());
1263 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1265 // Add some more children and grand-children.
1266 const QUrl parent1
= m_model
->fileItem(0).url();
1267 const QUrl parent2
= m_model
->fileItem(3).url();
1268 const QUrl realChild1
= m_model
->fileItem(1).url();
1269 const QUrl realChild2
= m_model
->fileItem(4).url();
1271 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1272 m_model
->slotCompleted();
1273 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1275 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1276 m_model
->slotCompleted();
1277 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1279 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1280 m_model
->slotCompleted();
1281 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1283 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1284 m_model
->slotCompleted();
1285 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1287 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1288 m_model
->slotCompleted();
1289 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1291 // Set a name filter that matches nothing -> only expanded folders remain.
1292 m_model
->setNameFilter("xyz");
1293 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1294 QCOMPARE(itemsRemovedSpy
.count(), 1);
1295 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1296 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1297 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1299 // Collapse "parent1".
1300 m_model
->setExpanded(0, false);
1301 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1302 QCOMPARE(itemsRemovedSpy
.count(), 1);
1303 arguments
= itemsRemovedSpy
.takeFirst();
1304 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1305 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1307 // Remove "parent2".
1308 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1309 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1310 QCOMPARE(itemsRemovedSpy
.count(), 1);
1311 arguments
= itemsRemovedSpy
.takeFirst();
1312 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1313 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1315 // Clear filter, verify that no items reappear.
1316 m_model
->setNameFilter(QString());
1317 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1320 void KFileItemModelTest::testNameRoleGroups()
1322 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1323 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1324 QVERIFY(itemsMovedSpy
.isValid());
1325 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1326 QVERIFY(groupsChangedSpy
.isValid());
1328 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1330 m_model
->setGroupedSorting(true);
1331 m_model
->loadDirectory(m_testDir
->url());
1332 QVERIFY(itemsInsertedSpy
.wait());
1333 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1335 QList
<QPair
<int, QVariant
> > expectedGroups
;
1336 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1337 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1338 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1339 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1340 QCOMPARE(m_model
->groups(), expectedGroups
);
1342 // Rename d.txt to a.txt.
1343 QHash
<QByteArray
, QVariant
> data
;
1344 data
.insert("text", "a.txt");
1345 m_model
->setData(2, data
);
1346 QVERIFY(itemsMovedSpy
.wait());
1347 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1349 expectedGroups
.clear();
1350 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1351 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1352 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1353 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1354 QCOMPARE(m_model
->groups(), expectedGroups
);
1356 // Rename c.txt to d.txt.
1357 data
.insert("text", "d.txt");
1358 m_model
->setData(2, data
);
1359 QVERIFY(groupsChangedSpy
.wait());
1360 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1362 expectedGroups
.clear();
1363 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1364 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1365 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1366 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1367 QCOMPARE(m_model
->groups(), expectedGroups
);
1369 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1370 const KFileItem fileItemD
= m_model
->fileItem(2);
1371 KFileItem fileItemC
= fileItemD
;
1372 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1373 urlC
.setPath(urlC
.path() + "c.txt");
1374 fileItemC
.setUrl(urlC
);
1376 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1377 QVERIFY(groupsChangedSpy
.wait());
1378 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1380 expectedGroups
.clear();
1381 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1382 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1383 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1384 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1385 QCOMPARE(m_model
->groups(), expectedGroups
);
1388 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1390 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1392 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1393 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1394 m_model
->setRoles(modelRoles
);
1396 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1398 m_model
->setGroupedSorting(true);
1399 m_model
->loadDirectory(m_testDir
->url());
1400 QVERIFY(itemsInsertedSpy
.wait());
1401 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1403 QList
<QPair
<int, QVariant
> > expectedGroups
;
1404 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1405 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1406 QCOMPARE(m_model
->groups(), expectedGroups
);
1408 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1409 expectedGroups
.clear();
1410 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1411 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1413 m_model
->setExpanded(0, true);
1414 QVERIFY(m_model
->isExpanded(0));
1415 QVERIFY(itemsInsertedSpy
.wait());
1416 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1417 QCOMPARE(m_model
->groups(), expectedGroups
);
1419 m_model
->setExpanded(3, true);
1420 QVERIFY(m_model
->isExpanded(3));
1421 QVERIFY(itemsInsertedSpy
.wait());
1422 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1423 QCOMPARE(m_model
->groups(), expectedGroups
);
1426 void KFileItemModelTest::testInconsistentModel()
1428 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1430 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1431 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1432 m_model
->setRoles(modelRoles
);
1434 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1436 m_model
->loadDirectory(m_testDir
->url());
1437 QVERIFY(itemsInsertedSpy
.wait());
1438 QCOMPARE(itemsInModel(), QStringList() << "a");
1440 // Expand "a/" and "a/b/".
1441 m_model
->setExpanded(0, true);
1442 QVERIFY(m_model
->isExpanded(0));
1443 QVERIFY(itemsInsertedSpy
.wait());
1444 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1446 m_model
->setExpanded(1, true);
1447 QVERIFY(m_model
->isExpanded(1));
1448 QVERIFY(itemsInsertedSpy
.wait());
1449 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1451 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1452 // Such a thing can in principle happen when performing a search, and there
1454 // (a) match the search string, and
1455 // (b) are children of a folder that matches the search string and is expanded.
1457 // Note that the first item in the list of added items must be new (i.e., not
1458 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1459 // it receives items that are in the model already and ignore them.
1460 QUrl
url(m_model
->directory().url() + "/a2");
1461 KFileItem
newItem(url
);
1463 KFileItemList items
;
1464 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1465 m_model
->slotItemsAdded(m_model
->directory(), items
);
1466 m_model
->slotCompleted();
1467 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1469 m_model
->setExpanded(0, false);
1471 // Test that the right items have been removed, see
1472 // https://bugs.kde.org/show_bug.cgi?id=324371
1473 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1475 // Test that resorting does not cause a crash, see
1476 // https://bugs.kde.org/show_bug.cgi?id=325359
1477 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1478 m_model
->resortAllItems();
1482 void KFileItemModelTest::testChangeRolesForFilteredItems()
1484 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1486 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1487 modelRoles
<< "owner";
1488 m_model
->setRoles(modelRoles
);
1490 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1492 m_model
->loadDirectory(m_testDir
->url());
1493 QVERIFY(itemsInsertedSpy
.wait());
1494 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1496 for (int index
= 0; index
< m_model
->count(); ++index
) {
1497 // All items should have the "text" and "owner" roles, but not "group".
1498 QVERIFY(m_model
->data(index
).contains("text"));
1499 QVERIFY(m_model
->data(index
).contains("owner"));
1500 QVERIFY(!m_model
->data(index
).contains("group"));
1503 // Add a filter, such that only "aaa.txt" remains in the model.
1504 m_model
->setNameFilter("aaa");
1505 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1507 // Add the "group" role.
1508 modelRoles
<< "group";
1509 m_model
->setRoles(modelRoles
);
1511 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1512 m_model
->setNameFilter("aa");
1513 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1515 for (int index
= 0; index
< m_model
->count(); ++index
) {
1516 // All items should have the "text", "owner", and "group" roles.
1517 QVERIFY(m_model
->data(index
).contains("text"));
1518 QVERIFY(m_model
->data(index
).contains("owner"));
1519 QVERIFY(m_model
->data(index
).contains("group"));
1522 // Remove the "owner" role.
1523 modelRoles
.remove("owner");
1524 m_model
->setRoles(modelRoles
);
1526 // Clear the filter, and verify that all items have the expected roles
1527 m_model
->setNameFilter(QString());
1528 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1530 for (int index
= 0; index
< m_model
->count(); ++index
) {
1531 // All items should have the "text" and "group" roles, but now "owner".
1532 QVERIFY(m_model
->data(index
).contains("text"));
1533 QVERIFY(!m_model
->data(index
).contains("owner"));
1534 QVERIFY(m_model
->data(index
).contains("group"));
1538 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1540 KFileItemList items
;
1542 KIO::UDSEntry entry
[3];
1544 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1545 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
1547 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1548 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
1550 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1551 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
1553 for (int i
= 0; i
< 3; ++i
) {
1554 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1555 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1556 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
1557 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1558 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
1559 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1560 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
1563 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1564 m_model
->slotCompleted();
1566 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1569 m_model
->setNameFilter("a");
1570 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1573 m_model
->setSortRole("owner");
1575 // Clear the filter, and verify that the items are sorted correctly.
1576 m_model
->setNameFilter(QString());
1577 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1580 void KFileItemModelTest::testRefreshFilteredItems()
1582 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1584 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1586 m_model
->loadDirectory(m_testDir
->url());
1587 QVERIFY(itemsInsertedSpy
.wait());
1588 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1590 const KFileItem fileItemC
= m_model
->fileItem(2);
1592 // Show only the .txt files.
1593 m_model
->setNameFilter(".txt");
1594 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1596 // Rename one of the .jpg files.
1597 KFileItem fileItemE
= fileItemC
;
1598 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1599 urlE
.setPath(urlE
.path() + "/e.jpg");
1600 fileItemE
.setUrl(urlE
);
1602 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1604 // Show all files again, and verify that the model has updated the file name.
1605 m_model
->setNameFilter(QString());
1606 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1609 void KFileItemModelTest::testCreateMimeData()
1611 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1613 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1614 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1615 m_model
->setRoles(modelRoles
);
1617 m_testDir
->createFile("a/1");
1619 m_model
->loadDirectory(m_testDir
->url());
1620 QVERIFY(itemsInsertedSpy
.wait());
1621 QCOMPARE(itemsInModel(), QStringList() << "a");
1624 m_model
->setExpanded(0, true);
1625 QVERIFY(itemsInsertedSpy
.wait());
1626 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1628 // Verify that creating the MIME data for a child of an expanded folder does
1629 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1631 selection
.insert(1);
1632 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1636 void KFileItemModelTest::testCollapseFolderWhileLoading()
1638 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1640 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1641 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1642 m_model
->setRoles(modelRoles
);
1644 m_testDir
->createFile("a2/b/c1.txt");
1646 m_model
->loadDirectory(m_testDir
->url());
1647 QVERIFY(itemsInsertedSpy
.wait());
1648 QCOMPARE(itemsInModel(), QStringList() << "a2");
1651 m_model
->setExpanded(0, true);
1652 QVERIFY(m_model
->isExpanded(0));
1653 QVERIFY(itemsInsertedSpy
.wait());
1654 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1657 m_model
->setExpanded(1, true);
1658 QVERIFY(m_model
->isExpanded(1));
1659 QVERIFY(itemsInsertedSpy
.wait());
1660 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1662 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1663 // signal is not emitted yet.
1664 const KFileItem fileItemC1
= m_model
->fileItem(2);
1665 KFileItem fileItemC2
= fileItemC1
;
1666 QUrl urlC2
= fileItemC2
.url();
1667 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1668 urlC2
.setPath(urlC2
.path() + "c2.txt");
1669 fileItemC2
.setUrl(urlC2
);
1671 const QUrl urlB
= m_model
->fileItem(1).url();
1672 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1673 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1675 // Collapse "a2/". This should also remove all its (indirect) children from
1676 // the model and from the model's m_pendingItemsToInsert member.
1677 m_model
->setExpanded(0, false);
1678 QCOMPARE(itemsInModel(), QStringList() << "a2");
1680 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1681 // is still in m_pendingItemsToInsert, then we might get a crash, see
1682 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1683 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1684 // without parent in the model.
1685 m_model
->slotCompleted();
1686 QCOMPARE(itemsInModel(), QStringList() << "a2");
1688 // Expand "a2/" again.
1689 m_model
->setExpanded(0, true);
1690 QVERIFY(m_model
->isExpanded(0));
1691 QVERIFY(itemsInsertedSpy
.wait());
1692 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1694 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1695 // completed() signal is not emitted yet.
1696 const KFileItem fileItemA2
= m_model
->fileItem(0);
1697 KFileItem fileItemA1
= fileItemA2
;
1698 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1699 urlA1
.setPath(urlA1
.path() + "a1");
1700 fileItemA1
.setUrl(urlA1
);
1702 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1703 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1705 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1706 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1707 // confuse the code which collapses the folder.
1708 m_model
->setExpanded(0, false);
1709 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1710 QVERIFY(!m_model
->isExpanded(0));
1711 QVERIFY(!m_model
->isExpanded(1));
1714 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1716 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1718 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1720 m_model
->loadDirectory(m_testDir
->url());
1721 QVERIFY(itemsInsertedSpy
.wait());
1722 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1724 const KFileItem fileItemB
= m_model
->fileItem(1);
1726 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1728 list
<< fileItemB
<< fileItemB
;
1729 m_model
->slotItemsDeleted(list
);
1731 QVERIFY(m_model
->isConsistent());
1732 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1735 QStringList
KFileItemModelTest::itemsInModel() const
1738 for (int i
= 0; i
< m_model
->count(); i
++) {
1739 items
<< m_model
->fileItem(i
).text();
1744 QTEST_MAIN(KFileItemModelTest
)
1746 #include "kfileitemmodeltest.moc"