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>
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 testRemoveHiddenItems();
77 void collapseParentOfHiddenItems();
78 void removeParentOfHiddenItems();
79 void testGeneralParentChildRelationships();
80 void testNameRoleGroups();
81 void testNameRoleGroupsWithExpandedItems();
82 void testInconsistentModel();
83 void testChangeRolesForFilteredItems();
84 void testChangeSortRoleWhileFiltering();
85 void testRefreshFilteredItems();
86 void testCollapseFolderWhileLoading();
87 void testCreateMimeData();
88 void testDeleteFileMoreThanOnce();
91 QStringList
itemsInModel() const;
94 KFileItemModel
* m_model
;
98 void KFileItemModelTest::initTestCase()
100 QStandardPaths::setTestModeEnabled(true);
103 void KFileItemModelTest::init()
105 // The item-model tests result in a huge number of debugging
106 // output from kdelibs. Only show critical and fatal messages.
107 qInstallMessageHandler(myMessageOutput
);
109 qRegisterMetaType
<KItemRange
>("KItemRange");
110 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
111 qRegisterMetaType
<KFileItemList
>("KFileItemList");
113 m_testDir
= new TestDir();
114 m_model
= new KFileItemModel();
115 m_model
->m_dirLister
->setAutoUpdate(false);
117 // Reduce the timer interval to make the test run faster.
118 m_model
->m_resortAllItemsTimer
->setInterval(0);
121 void KFileItemModelTest::cleanup()
130 void KFileItemModelTest::testDefaultRoles()
132 const QSet
<QByteArray
> roles
= m_model
->roles();
133 QCOMPARE(roles
.count(), 4);
134 QVERIFY(roles
.contains("text"));
135 QVERIFY(roles
.contains("isDir"));
136 QVERIFY(roles
.contains("isLink"));
137 QVERIFY(roles
.contains("isHidden"));
140 void KFileItemModelTest::testDefaultSortRole()
142 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
143 QVERIFY(itemsInsertedSpy
.isValid());
145 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
147 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
149 m_model
->loadDirectory(m_testDir
->url());
150 QVERIFY(itemsInsertedSpy
.wait());
152 QCOMPARE(m_model
->count(), 3);
153 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
154 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
155 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
158 void KFileItemModelTest::testDefaultGroupedSorting()
160 QCOMPARE(m_model
->groupedSorting(), false);
163 void KFileItemModelTest::testNewItems()
165 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
167 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
169 m_model
->loadDirectory(m_testDir
->url());
170 QVERIFY(itemsInsertedSpy
.wait());
172 QCOMPARE(m_model
->count(), 3);
174 QVERIFY(m_model
->isConsistent());
177 void KFileItemModelTest::testRemoveItems()
179 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
180 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
182 m_testDir
->createFiles({"a.txt", "b.txt"});
183 m_model
->loadDirectory(m_testDir
->url());
184 QVERIFY(itemsInsertedSpy
.wait());
185 QCOMPARE(m_model
->count(), 2);
186 QVERIFY(m_model
->isConsistent());
188 m_testDir
->removeFile("a.txt");
189 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
190 QVERIFY(itemsRemovedSpy
.wait());
191 QCOMPARE(m_model
->count(), 1);
192 QVERIFY(m_model
->isConsistent());
195 void KFileItemModelTest::testDirLoadingCompleted()
197 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
198 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
199 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
201 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
203 m_model
->loadDirectory(m_testDir
->url());
204 QVERIFY(loadingCompletedSpy
.wait());
205 QCOMPARE(loadingCompletedSpy
.count(), 1);
206 QCOMPARE(itemsInsertedSpy
.count(), 1);
207 QCOMPARE(itemsRemovedSpy
.count(), 0);
208 QCOMPARE(m_model
->count(), 3);
210 m_testDir
->createFiles({"d.txt", "e.txt"});
211 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
212 QVERIFY(loadingCompletedSpy
.wait());
213 QCOMPARE(loadingCompletedSpy
.count(), 2);
214 QCOMPARE(itemsInsertedSpy
.count(), 2);
215 QCOMPARE(itemsRemovedSpy
.count(), 0);
216 QCOMPARE(m_model
->count(), 5);
218 m_testDir
->removeFile("a.txt");
219 m_testDir
->createFile("f.txt");
220 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
221 QVERIFY(loadingCompletedSpy
.wait());
222 QCOMPARE(loadingCompletedSpy
.count(), 3);
223 QCOMPARE(itemsInsertedSpy
.count(), 3);
224 QCOMPARE(itemsRemovedSpy
.count(), 1);
225 QCOMPARE(m_model
->count(), 5);
227 m_testDir
->removeFile("b.txt");
228 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
229 QVERIFY(itemsRemovedSpy
.wait());
230 QCOMPARE(loadingCompletedSpy
.count(), 4);
231 QCOMPARE(itemsInsertedSpy
.count(), 3);
232 QCOMPARE(itemsRemovedSpy
.count(), 2);
233 QCOMPARE(m_model
->count(), 4);
235 QVERIFY(m_model
->isConsistent());
238 void KFileItemModelTest::testSetData()
240 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
241 QVERIFY(itemsInsertedSpy
.isValid());
242 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
243 QVERIFY(itemsChangedSpy
.isValid());
245 m_testDir
->createFile("a.txt");
247 m_model
->loadDirectory(m_testDir
->url());
248 QVERIFY(itemsInsertedSpy
.wait());
250 QHash
<QByteArray
, QVariant
> values
;
251 values
.insert("customRole1", "Test1");
252 values
.insert("customRole2", "Test2");
254 m_model
->setData(0, values
);
255 QCOMPARE(itemsChangedSpy
.count(), 1);
257 values
= m_model
->data(0);
258 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
259 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
260 QVERIFY(m_model
->isConsistent());
263 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
265 QTest::addColumn
<int>("changedIndex");
266 QTest::addColumn
<int>("changedRating");
267 QTest::addColumn
<bool>("expectMoveSignal");
268 QTest::addColumn
<int>("ratingIndex0");
269 QTest::addColumn
<int>("ratingIndex1");
270 QTest::addColumn
<int>("ratingIndex2");
273 // Index 0 = rating 2
274 // Index 1 = rating 4
275 // Index 2 = rating 6
277 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
278 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
279 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
281 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
282 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
283 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
286 void KFileItemModelTest::testSetDataWithModifiedSortRole()
288 QFETCH(int, changedIndex
);
289 QFETCH(int, changedRating
);
290 QFETCH(bool, expectMoveSignal
);
291 QFETCH(int, ratingIndex0
);
292 QFETCH(int, ratingIndex1
);
293 QFETCH(int, ratingIndex2
);
295 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
296 QVERIFY(itemsInsertedSpy
.isValid());
297 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
298 QVERIFY(itemsMovedSpy
.isValid());
300 // Changing the value of a sort-role must result in
301 // a reordering of the items.
302 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
303 m_model
->setSortRole("rating");
304 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
306 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
308 m_model
->loadDirectory(m_testDir
->url());
309 QVERIFY(itemsInsertedSpy
.wait());
311 // Fill the "rating" role of each file:
316 QHash
<QByteArray
, QVariant
> ratingA
;
317 ratingA
.insert("rating", 2);
318 m_model
->setData(0, ratingA
);
320 QHash
<QByteArray
, QVariant
> ratingB
;
321 ratingB
.insert("rating", 4);
322 m_model
->setData(1, ratingB
);
324 QHash
<QByteArray
, QVariant
> ratingC
;
325 ratingC
.insert("rating", 6);
326 m_model
->setData(2, ratingC
);
328 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
329 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
330 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
332 // Now change the rating from a.txt. This usually results
333 // in reordering of the items.
334 QHash
<QByteArray
, QVariant
> rating
;
335 rating
.insert("rating", changedRating
);
336 m_model
->setData(changedIndex
, rating
);
338 if (expectMoveSignal
) {
339 QVERIFY(itemsMovedSpy
.wait());
342 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
343 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
344 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
345 QVERIFY(m_model
->isConsistent());
348 void KFileItemModelTest::testChangeSortRole()
350 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
351 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
352 QVERIFY(itemsMovedSpy
.isValid());
354 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
356 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
358 m_model
->loadDirectory(m_testDir
->url());
359 QVERIFY(itemsInsertedSpy
.wait());
360 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
362 // Simulate that KFileItemModelRolesUpdater determines the mime type.
363 // Resorting the files by 'type' will only work immediately if their
364 // mime types are known.
365 for (int index
= 0; index
< m_model
->count(); ++index
) {
366 m_model
->fileItem(index
).determineMimeType();
369 // Now: sort by type.
370 m_model
->setSortRole("type");
371 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
372 QVERIFY(!itemsMovedSpy
.isEmpty());
374 // The actual order of the files might depend on the translation of the
375 // result of KFileItem::mimeComment() in the user's language.
376 QStringList version1
;
377 version1
<< "b.jpg" << "a.txt" << "c.txt";
379 QStringList version2
;
380 version2
<< "a.txt" << "c.txt" << "b.jpg";
382 const bool ok1
= (itemsInModel() == version1
);
383 const bool ok2
= (itemsInModel() == version2
);
388 void KFileItemModelTest::testResortAfterChangingName()
390 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
391 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
392 QVERIFY(itemsMovedSpy
.isValid());
394 // We sort by size in a directory where all files have the same size.
395 // Therefore, the files are sorted by their names.
396 m_model
->setSortRole("size");
398 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
400 m_model
->loadDirectory(m_testDir
->url());
401 QVERIFY(itemsInsertedSpy
.wait());
402 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
404 // We rename a.txt to d.txt. Even though the size has not changed at all,
405 // the model must re-sort the items.
406 QHash
<QByteArray
, QVariant
> data
;
407 data
.insert("text", "d.txt");
408 m_model
->setData(0, data
);
410 QVERIFY(itemsMovedSpy
.wait());
411 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
413 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
414 const KFileItem fileItemD
= m_model
->fileItem(2);
415 KFileItem fileItemA
= fileItemD
;
416 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
417 urlA
.setPath(urlA
.path() + "a.txt");
418 fileItemA
.setUrl(urlA
);
420 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
422 QVERIFY(itemsMovedSpy
.wait());
423 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
426 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
428 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
430 // KFileItemModel prevents that inserting a punch of items sequentially
431 // results in an itemsInserted()-signal for each item. Instead internally
432 // a timeout is given that collects such operations and results in only
433 // one itemsInserted()-signal. However in this test we want to stress
434 // KFileItemModel to do a lot of insert operation and hence decrease
435 // the timeout to 1 millisecond.
436 m_testDir
->createFile("1");
437 m_model
->loadDirectory(m_testDir
->url());
438 QVERIFY(itemsInsertedSpy
.wait());
439 QCOMPARE(m_model
->count(), 1);
441 // Insert 10 items for 20 times. After each insert operation the model consistency
443 QSet
<int> insertedItems
;
444 for (int i
= 0; i
< 20; ++i
) {
445 itemsInsertedSpy
.clear();
447 for (int j
= 0; j
< 10; ++j
) {
448 int itemName
= QRandomGenerator::global()->generate();
449 while (insertedItems
.contains(itemName
)) {
450 itemName
= QRandomGenerator::global()->generate();
452 insertedItems
.insert(itemName
);
454 m_testDir
->createFile(QString::number(itemName
));
457 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
458 if (itemsInsertedSpy
.isEmpty()) {
459 QVERIFY(itemsInsertedSpy
.wait());
462 QVERIFY(m_model
->isConsistent());
465 QCOMPARE(m_model
->count(), 201);
468 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
470 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
472 m_testDir
->createFiles({"B", "E", "G"});
474 // Due to inserting the 3 items one item-range with index == 0 and
475 // count == 3 must be given
476 m_model
->loadDirectory(m_testDir
->url());
477 QVERIFY(itemsInsertedSpy
.wait());
479 QCOMPARE(itemsInsertedSpy
.count(), 1);
480 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
481 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
482 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
484 // The indexes of the item-ranges must always be related to the model before
485 // the items have been inserted. Having:
488 // and inserting A, C, D, F the resulting model will be:
491 // and the item-ranges must be:
492 // index: 0, count: 1 for A
493 // index: 1, count: 2 for B, C
494 // index: 2, count: 1 for G
496 m_testDir
->createFiles({"A", "C", "D", "F"});
498 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
499 QVERIFY(itemsInsertedSpy
.wait());
501 QCOMPARE(itemsInsertedSpy
.count(), 1);
502 arguments
= itemsInsertedSpy
.takeFirst();
503 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
504 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
507 void KFileItemModelTest::testExpandItems()
509 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
510 QVERIFY(itemsInsertedSpy
.isValid());
511 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
512 QVERIFY(itemsRemovedSpy
.isValid());
513 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
514 QVERIFY(loadingCompletedSpy
.isValid());
516 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
517 // Besides testing the basic item expansion functionality, the test makes sure that
518 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
519 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
520 // first three characters.
521 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
522 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
523 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
524 m_model
->setRoles(modelRoles
);
526 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
528 // Store the URLs of all folders in a set.
529 QSet
<QUrl
> allFolders
;
530 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a")
531 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
532 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
534 m_model
->loadDirectory(m_testDir
->url());
535 QVERIFY(itemsInsertedSpy
.wait());
537 // So far, the model contains only "a/"
538 QCOMPARE(m_model
->count(), 1);
539 QVERIFY(m_model
->isExpandable(0));
540 QVERIFY(!m_model
->isExpanded(0));
541 QVERIFY(m_model
->expandedDirectories().empty());
543 QCOMPARE(itemsInsertedSpy
.count(), 1);
544 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
545 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
547 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
548 m_model
->setExpanded(0, true);
549 QVERIFY(m_model
->isExpanded(0));
550 QVERIFY(itemsInsertedSpy
.wait());
551 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
552 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
554 QCOMPARE(itemsInsertedSpy
.count(), 1);
555 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
556 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
558 QVERIFY(m_model
->isExpandable(1));
559 QVERIFY(!m_model
->isExpanded(1));
560 QVERIFY(m_model
->isExpandable(2));
561 QVERIFY(!m_model
->isExpanded(2));
563 // Expand the folder "a/a/" -> "a/a/1" becomes visible
564 m_model
->setExpanded(1, true);
565 QVERIFY(m_model
->isExpanded(1));
566 QVERIFY(itemsInsertedSpy
.wait());
567 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
568 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a")
569 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
571 QCOMPARE(itemsInsertedSpy
.count(), 1);
572 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
573 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
575 QVERIFY(!m_model
->isExpandable(2));
576 QVERIFY(!m_model
->isExpanded(2));
578 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
579 m_model
->setExpanded(3, true);
580 QVERIFY(m_model
->isExpanded(3));
581 QVERIFY(itemsInsertedSpy
.wait());
582 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
583 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
585 QCOMPARE(itemsInsertedSpy
.count(), 1);
586 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
587 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
589 QVERIFY(!m_model
->isExpandable(4));
590 QVERIFY(!m_model
->isExpanded(4));
592 // Collapse the top-level folder -> all other items should disappear
593 m_model
->setExpanded(0, false);
594 QVERIFY(!m_model
->isExpanded(0));
595 QCOMPARE(m_model
->count(), 1);
596 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
598 QCOMPARE(itemsRemovedSpy
.count(), 1);
599 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
600 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
601 QVERIFY(m_model
->isConsistent());
603 // Clear the model, reload the folder and try to restore the expanded folders.
605 QCOMPARE(m_model
->count(), 0);
606 QVERIFY(m_model
->expandedDirectories().empty());
608 m_model
->loadDirectory(m_testDir
->url());
609 m_model
->restoreExpandedDirectories(allFolders
);
610 QVERIFY(loadingCompletedSpy
.wait());
611 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
612 QVERIFY(m_model
->isExpanded(0));
613 QVERIFY(m_model
->isExpanded(1));
614 QVERIFY(!m_model
->isExpanded(2));
615 QVERIFY(m_model
->isExpanded(3));
616 QVERIFY(!m_model
->isExpanded(4));
617 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
618 QVERIFY(m_model
->isConsistent());
620 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
621 // This is how DolphinView restores the expanded folders when navigating in history.
622 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
623 QVERIFY(loadingCompletedSpy
.wait());
624 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
625 m_model
->restoreExpandedDirectories(allFolders
);
626 m_model
->loadDirectory(m_testDir
->url());
627 QVERIFY(loadingCompletedSpy
.wait());
628 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
629 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
631 // Remove all expanded items by changing the roles
632 itemsRemovedSpy
.clear();
633 m_model
->setRoles(originalModelRoles
);
634 QVERIFY(!m_model
->isExpanded(0));
635 QCOMPARE(m_model
->count(), 1);
636 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
638 QCOMPARE(itemsRemovedSpy
.count(), 1);
639 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
640 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
641 QVERIFY(m_model
->isConsistent());
644 void KFileItemModelTest::testExpandParentItems()
646 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
647 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
648 QVERIFY(loadingCompletedSpy
.isValid());
650 // Create a tree structure of folders:
658 QSet
<QByteArray
> modelRoles
= m_model
->roles();
659 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
660 m_model
->setRoles(modelRoles
);
662 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
664 m_model
->loadDirectory(m_testDir
->url());
665 QVERIFY(itemsInsertedSpy
.wait());
667 // So far, the model contains only "a 1/" and "a2/".
668 QCOMPARE(m_model
->count(), 2);
669 QVERIFY(m_model
->expandedDirectories().empty());
671 // Expand the parents of "a2/b2/c2".
672 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
673 QVERIFY(loadingCompletedSpy
.wait());
675 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
676 // It's important that only the parents of "a1/b1/c1" are expanded.
677 QCOMPARE(m_model
->count(), 4);
678 QVERIFY(!m_model
->isExpanded(0));
679 QVERIFY(m_model
->isExpanded(1));
680 QVERIFY(m_model
->isExpanded(2));
681 QVERIFY(!m_model
->isExpanded(3));
683 // Expand the parents of "a 1/b1".
684 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
685 QVERIFY(loadingCompletedSpy
.wait());
687 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
688 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
689 QCOMPARE(m_model
->count(), 5);
690 QVERIFY(m_model
->isExpanded(0));
691 QVERIFY(!m_model
->isExpanded(1));
692 QVERIFY(m_model
->isExpanded(2));
693 QVERIFY(m_model
->isExpanded(3));
694 QVERIFY(!m_model
->isExpanded(4));
695 QVERIFY(m_model
->isConsistent());
698 m_model
->setExpanded(1, true);
699 QVERIFY(loadingCompletedSpy
.wait());
700 QCOMPARE(m_model
->count(), 6);
701 QVERIFY(m_model
->isExpanded(0));
702 QVERIFY(m_model
->isExpanded(1));
703 QVERIFY(!m_model
->isExpanded(2));
704 QVERIFY(m_model
->isExpanded(3));
705 QVERIFY(m_model
->isExpanded(4));
706 QVERIFY(!m_model
->isExpanded(5));
707 QVERIFY(m_model
->isConsistent());
709 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
710 m_model
->setExpanded(1, false);
711 QCOMPARE(m_model
->count(), 5);
712 QVERIFY(m_model
->isExpanded(0));
713 QVERIFY(!m_model
->isExpanded(1));
714 QVERIFY(m_model
->isExpanded(2));
715 QVERIFY(m_model
->isExpanded(3));
716 QVERIFY(!m_model
->isExpanded(4));
717 QVERIFY(m_model
->isConsistent());
721 * Renaming an expanded folder by prepending its name with a dot makes it
722 * hidden. Verify that this does not cause an inconsistent model state and
723 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
725 void KFileItemModelTest::testMakeExpandedItemHidden()
727 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
728 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
730 QSet
<QByteArray
> modelRoles
= m_model
->roles();
731 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
732 m_model
->setRoles(modelRoles
);
734 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
736 m_model
->loadDirectory(m_testDir
->url());
737 QVERIFY(itemsInsertedSpy
.wait());
739 // So far, the model contains only "1a/" and "1b".
740 QCOMPARE(m_model
->count(), 2);
741 m_model
->setExpanded(0, true);
742 QVERIFY(itemsInsertedSpy
.wait());
744 // Now "1a/2a" and "1a/2b" have appeared.
745 QCOMPARE(m_model
->count(), 4);
746 m_model
->setExpanded(1, true);
747 QVERIFY(itemsInsertedSpy
.wait());
748 QCOMPARE(m_model
->count(), 6);
750 // Rename "1a/2" and make it hidden.
751 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
752 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
754 KIO::SimpleJob
* job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
755 bool ok
= job
->exec();
757 QVERIFY(itemsRemovedSpy
.wait());
759 // "1a/2" and its subfolders have disappeared now.
760 QVERIFY(m_model
->isConsistent());
761 QCOMPARE(m_model
->count(), 3);
763 m_model
->setExpanded(0, false);
764 QCOMPARE(m_model
->count(), 2);
768 void KFileItemModelTest::testRemoveFilteredExpandedItems()
770 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
772 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
773 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
774 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
775 m_model
->setRoles(modelRoles
);
777 m_testDir
->createFiles({"folder/child", "file"});
779 m_model
->loadDirectory(m_testDir
->url());
780 QVERIFY(itemsInsertedSpy
.wait());
782 // So far, the model contains only "folder/" and "file".
783 QCOMPARE(m_model
->count(), 2);
784 QVERIFY(m_model
->isExpandable(0));
785 QVERIFY(!m_model
->isExpandable(1));
786 QVERIFY(!m_model
->isExpanded(0));
787 QVERIFY(!m_model
->isExpanded(1));
788 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
790 // Expand "folder" -> "folder/child" becomes visible.
791 m_model
->setExpanded(0, true);
792 QVERIFY(m_model
->isExpanded(0));
793 QVERIFY(itemsInsertedSpy
.wait());
794 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
796 // Add a name filter.
797 m_model
->setNameFilter("f");
798 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
800 m_model
->setNameFilter("fo");
801 QCOMPARE(itemsInModel(), QStringList() << "folder");
803 // Remove all expanded items by changing the roles
804 m_model
->setRoles(originalModelRoles
);
805 QVERIFY(!m_model
->isExpanded(0));
806 QCOMPARE(itemsInModel(), QStringList() << "folder");
808 // Remove the name filter and verify that "folder/child" does not reappear.
809 m_model
->setNameFilter(QString());
810 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
813 void KFileItemModelTest::testSorting()
815 // testDir structure is as follows
822 // │ │ ├─ c-3
828 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
829 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
830 QVERIFY(itemsMovedSpy
.isValid());
832 // Create some files with different sizes and modification times to check the different sorting options
833 QDateTime now
= QDateTime::currentDateTime();
835 QSet
<QByteArray
> roles
;
836 roles
.insert("text");
837 roles
.insert("isExpanded");
838 roles
.insert("isExpandable");
839 roles
.insert("expandedParentsCount");
840 m_model
->setRoles(roles
);
842 m_testDir
->createDir("c/c-2");
843 m_testDir
->createFile("c/c-2/c-3");
844 m_testDir
->createFile("c/c-1");
846 m_testDir
->createFile("a", "A file", now
.addDays(-3));
847 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
848 m_testDir
->createDir("c", now
.addDays(-2));
849 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
850 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
851 m_testDir
->createFile(".f");
852 m_testDir
->createDir(".g");
854 m_model
->loadDirectory(m_testDir
->url());
855 QVERIFY(itemsInsertedSpy
.wait());
856 QCOMPARE(itemsInsertedSpy
.count(), 1);
857 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
858 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 5));
860 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
861 m_model
->setExpanded(index
, true);
862 QVERIFY(itemsInsertedSpy
.wait());
863 QCOMPARE(itemsInsertedSpy
.count(), 1);
864 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
865 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
867 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
868 m_model
->setExpanded(index
, true);
869 QVERIFY(itemsInsertedSpy
.wait());
870 QCOMPARE(itemsInsertedSpy
.count(), 1);
871 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
872 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1));
874 // Default: Sort by Name, ascending
875 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
876 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
877 QVERIFY(m_model
->sortDirectoriesFirst());
878 QVERIFY(!m_model
->showHiddenFiles());
879 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
881 // Sort by Name, ascending, 'Sort Folders First' disabled
882 m_model
->setSortDirectoriesFirst(false);
883 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
884 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
885 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
886 QCOMPARE(itemsMovedSpy
.count(), 1);
887 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
888 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
890 // Sort by Name, descending
891 m_model
->setSortDirectoriesFirst(true);
892 m_model
->setSortOrder(Qt::DescendingOrder
);
893 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
894 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
895 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
896 QCOMPARE(itemsMovedSpy
.count(), 2);
897 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
898 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
899 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
900 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
902 // Sort by Date, descending
903 m_model
->setSortDirectoriesFirst(true);
904 m_model
->setSortRole("modificationtime");
905 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
906 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
907 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
908 QCOMPARE(itemsMovedSpy
.count(), 1);
909 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
910 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
912 // Sort by Date, ascending
913 m_model
->setSortOrder(Qt::AscendingOrder
);
914 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
915 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
916 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
917 QCOMPARE(itemsMovedSpy
.count(), 1);
918 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
919 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
921 // Sort by Date, ascending, 'Sort Folders First' disabled
922 m_model
->setSortDirectoriesFirst(false);
923 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
924 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
925 QVERIFY(!m_model
->sortDirectoriesFirst());
926 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
927 QCOMPARE(itemsMovedSpy
.count(), 1);
928 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
929 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
931 // Sort by Name, ascending, 'Sort Folders First' disabled
932 m_model
->setSortRole("text");
933 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
934 QVERIFY(!m_model
->sortDirectoriesFirst());
935 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
936 QCOMPARE(itemsMovedSpy
.count(), 1);
937 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
938 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
940 // Sort by Size, ascending, 'Sort Folders First' disabled
941 m_model
->setSortRole("size");
942 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
943 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
944 QVERIFY(!m_model
->sortDirectoriesFirst());
945 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
946 QCOMPARE(itemsMovedSpy
.count(), 1);
947 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
948 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
950 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
951 m_model
->setSortDirectoriesFirst(true);
952 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
953 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
954 QVERIFY(m_model
->sortDirectoriesFirst());
955 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
956 QCOMPARE(itemsMovedSpy
.count(), 0);
958 // Sort by Size, descending, 'Sort Folders First' enabled
959 m_model
->setSortOrder(Qt::DescendingOrder
);
960 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
961 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
962 QVERIFY(m_model
->sortDirectoriesFirst());
963 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
964 QCOMPARE(itemsMovedSpy
.count(), 1);
965 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
966 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
968 // 'Show Hidden Files' enabled
969 m_model
->setShowHiddenFiles(true);
970 QVERIFY(m_model
->showHiddenFiles());
971 QVERIFY(!m_model
->sortHiddenLast());
972 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << ".g" << "d" << "e" << "b" << "a" << ".f");
973 QCOMPARE(itemsMovedSpy
.count(), 0);
974 QCOMPARE(itemsInsertedSpy
.count(), 1);
975 QCOMPARE(itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
977 // 'Sort Hidden Files Last' enabled
978 m_model
->setSortHiddenLast(true);
979 QVERIFY(m_model
->sortHiddenLast());
980 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a" << ".g" << ".f");
981 QCOMPARE(itemsMovedSpy
.count(), 1);
982 QCOMPARE(itemsInsertedSpy
.count(), 0);
983 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 5));
984 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 8 << 4 << 5 << 6 << 7);
987 m_model
->setSortRole("text");
988 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a" << ".g" << ".f");
989 QCOMPARE(itemsMovedSpy
.count(), 1);
990 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 2));
991 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 5 << 4);
994 m_model
->setSortOrder(Qt::AscendingOrder
);
995 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e" << ".g" << ".f");
996 QCOMPARE(itemsMovedSpy
.count(), 1);
997 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
998 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
1000 // 'Sort Folders First' disabled
1001 m_model
->setSortDirectoriesFirst(false);
1002 QVERIFY(!m_model
->sortDirectoriesFirst());
1003 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e" << ".f" << ".g");
1004 QCOMPARE(itemsMovedSpy
.count(), 1);
1005 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 10));
1006 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1010 void KFileItemModelTest::testIndexForKeyboardSearch()
1012 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1014 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1016 m_model
->loadDirectory(m_testDir
->url());
1017 QVERIFY(itemsInsertedSpy
.wait());
1019 // Search from index 0
1020 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
1021 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
1022 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
1023 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
1024 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
1025 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
1026 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
1027 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
1028 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
1029 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
1030 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
1032 // Start a search somewhere in the middle
1033 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
1034 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
1035 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
1036 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
1038 // Test searches that go past the last item back to index 0
1039 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
1040 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
1041 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
1042 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
1044 // Test searches that yield no result
1045 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
1046 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
1047 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
1048 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
1049 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
1051 // Test upper case searches (note that search is case insensitive)
1052 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
1053 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
1054 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
1055 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1057 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1060 void KFileItemModelTest::testNameFilter()
1062 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1064 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1066 m_model
->loadDirectory(m_testDir
->url());
1067 QVERIFY(itemsInsertedSpy
.wait());
1069 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1070 QCOMPARE(m_model
->count(), 3);
1072 m_model
->setNameFilter("A2"); // Shows only A2
1073 QCOMPARE(m_model
->count(), 1);
1075 m_model
->setNameFilter("A2"); // Shows only A1
1076 QCOMPARE(m_model
->count(), 1);
1078 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1079 QCOMPARE(m_model
->count(), 2);
1081 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1082 QCOMPARE(m_model
->count(), 2);
1084 m_model
->setNameFilter(QString()); // Shows again all items
1085 QCOMPARE(m_model
->count(), 5);
1089 * Verifies that we do not crash when adding a KFileItem with an empty path.
1090 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1091 * tried to always read the first character of the path, even if the path is empty.
1093 void KFileItemModelTest::testEmptyPath()
1095 QSet
<QByteArray
> roles
;
1096 roles
.insert("text");
1097 roles
.insert("isExpanded");
1098 roles
.insert("isExpandable");
1099 roles
.insert("expandedParentsCount");
1100 m_model
->setRoles(roles
);
1102 const QUrl emptyUrl
;
1103 QVERIFY(emptyUrl
.path().isEmpty());
1105 const QUrl
url("file:///test/");
1107 KFileItemList items
;
1108 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1109 m_model
->slotItemsAdded(emptyUrl
, items
);
1110 m_model
->slotCompleted();
1114 * Verifies that the 'isExpanded' state of folders does not change when the
1115 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1117 void KFileItemModelTest::testRefreshExpandedItem()
1119 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1120 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1121 QVERIFY(itemsChangedSpy
.isValid());
1123 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1124 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1125 m_model
->setRoles(modelRoles
);
1127 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1129 m_model
->loadDirectory(m_testDir
->url());
1130 QVERIFY(itemsInsertedSpy
.wait());
1131 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1133 m_model
->setExpanded(0, true);
1134 QVERIFY(itemsInsertedSpy
.wait());
1135 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1136 QVERIFY(m_model
->isExpanded(0));
1138 const KFileItem item
= m_model
->fileItem(0);
1139 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1140 QVERIFY(!itemsChangedSpy
.isEmpty());
1142 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1143 QVERIFY(m_model
->isExpanded(0));
1147 * Verify that removing hidden files and folders from the model does not
1148 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1150 void KFileItemModelTest::testRemoveHiddenItems()
1152 m_testDir
->createDir(".a");
1153 m_testDir
->createDir(".b");
1154 m_testDir
->createDir("c");
1155 m_testDir
->createDir("d");
1156 m_testDir
->createFiles({".f", ".g", "h", "i"});
1158 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1159 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1161 m_model
->setShowHiddenFiles(true);
1162 m_model
->loadDirectory(m_testDir
->url());
1163 QVERIFY(itemsInsertedSpy
.wait());
1164 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1165 QCOMPARE(itemsInsertedSpy
.count(), 1);
1166 QCOMPARE(itemsRemovedSpy
.count(), 0);
1167 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1168 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1170 m_model
->setShowHiddenFiles(false);
1171 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1172 QCOMPARE(itemsInsertedSpy
.count(), 0);
1173 QCOMPARE(itemsRemovedSpy
.count(), 1);
1174 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1175 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1177 m_model
->setShowHiddenFiles(true);
1178 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1179 QCOMPARE(itemsInsertedSpy
.count(), 1);
1180 QCOMPARE(itemsRemovedSpy
.count(), 0);
1181 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1182 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1185 QCOMPARE(itemsInModel(), QStringList());
1186 QCOMPARE(itemsInsertedSpy
.count(), 0);
1187 QCOMPARE(itemsRemovedSpy
.count(), 1);
1188 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1189 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1191 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1192 // Verify that this does not make the model crash.
1193 m_model
->setShowHiddenFiles(false);
1197 * Verify that filtered items are removed when their parent is collapsed.
1199 void KFileItemModelTest::collapseParentOfHiddenItems()
1201 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1202 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1204 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1205 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1206 m_model
->setRoles(modelRoles
);
1208 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1210 m_model
->loadDirectory(m_testDir
->url());
1211 QVERIFY(itemsInsertedSpy
.wait());
1212 QCOMPARE(m_model
->count(), 1); // Only "a/"
1215 m_model
->setExpanded(0, true);
1216 QVERIFY(itemsInsertedSpy
.wait());
1217 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1220 m_model
->setExpanded(1, true);
1221 QVERIFY(itemsInsertedSpy
.wait());
1222 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1225 m_model
->setExpanded(2, true);
1226 QVERIFY(itemsInsertedSpy
.wait());
1227 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"
1229 // Set a name filter that matches nothing -> nothing should remain.
1230 m_model
->setNameFilter("xyz");
1231 QCOMPARE(itemsRemovedSpy
.count(), 1);
1232 QCOMPARE(m_model
->count(), 0); //Everything is hidden
1233 QCOMPARE(itemsInModel(), QStringList());
1235 //Filter by the file names. Folder "d" will be hidden since it was collapsed
1236 m_model
->setNameFilter("1");
1237 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1238 QCOMPARE(m_model
->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1240 // Collapse the folder "a/".
1241 m_model
->setExpanded(0, false);
1242 QCOMPARE(itemsRemovedSpy
.count(), 2);
1243 QCOMPARE(m_model
->count(), 1);
1244 QCOMPARE(itemsInModel(), QStringList() << "a");
1246 // Remove the filter -> "a" should still appear (and we should not get a crash).
1247 m_model
->setNameFilter(QString());
1248 QCOMPARE(itemsRemovedSpy
.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1249 QCOMPARE(m_model
->count(), 1);
1250 QCOMPARE(itemsInModel(), QStringList() << "a");
1254 * Verify that filtered items are removed when their parent is deleted.
1256 void KFileItemModelTest::removeParentOfHiddenItems()
1258 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1259 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1261 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1262 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1263 m_model
->setRoles(modelRoles
);
1265 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1267 m_model
->loadDirectory(m_testDir
->url());
1268 QVERIFY(itemsInsertedSpy
.wait());
1269 QCOMPARE(m_model
->count(), 1); // Only "a/"
1272 m_model
->setExpanded(0, true);
1273 QVERIFY(itemsInsertedSpy
.wait());
1274 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1277 m_model
->setExpanded(1, true);
1278 QVERIFY(itemsInsertedSpy
.wait());
1279 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1282 m_model
->setExpanded(2, true);
1283 QVERIFY(itemsInsertedSpy
.wait());
1284 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"
1286 // Set a name filter that matches nothing -> nothing should remain.
1287 m_model
->setNameFilter("xyz");
1288 QCOMPARE(itemsRemovedSpy
.count(), 1);
1289 QCOMPARE(m_model
->count(), 0);
1290 QCOMPARE(itemsInModel(), QStringList());
1292 // Filter by "c". Folder "b" will also be shown because it is its parent.
1293 m_model
->setNameFilter("c");
1294 QCOMPARE(itemsRemovedSpy
.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1295 QCOMPARE(m_model
->count(), 3);
1296 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1298 // Simulate the deletion of the directory "a/b/".
1299 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1300 QCOMPARE(itemsRemovedSpy
.count(), 2);
1301 QCOMPARE(m_model
->count(), 1);
1302 QCOMPARE(itemsInModel(), QStringList() << "a");
1304 // Remove the filter -> only the file "a/1" should appear.
1305 m_model
->setNameFilter(QString());
1306 QCOMPARE(m_model
->count(), 2);
1307 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1311 * Create a tree structure where parent-child relationships can not be
1312 * determined by parsing the URLs, and verify that KFileItemModel
1313 * handles them correctly.
1315 void KFileItemModelTest::testGeneralParentChildRelationships()
1317 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1318 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1320 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1321 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1322 m_model
->setRoles(modelRoles
);
1324 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1326 m_model
->loadDirectory(m_testDir
->url());
1327 QVERIFY(itemsInsertedSpy
.wait());
1328 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1330 // Expand all folders.
1331 m_model
->setExpanded(0, true);
1332 QVERIFY(itemsInsertedSpy
.wait());
1333 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1335 m_model
->setExpanded(1, true);
1336 QVERIFY(itemsInsertedSpy
.wait());
1337 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1339 m_model
->setExpanded(3, true);
1340 QVERIFY(itemsInsertedSpy
.wait());
1341 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1343 m_model
->setExpanded(4, true);
1344 QVERIFY(itemsInsertedSpy
.wait());
1345 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1347 // Add some more children and grand-children.
1348 const QUrl parent1
= m_model
->fileItem(0).url();
1349 const QUrl parent2
= m_model
->fileItem(3).url();
1350 const QUrl realChild1
= m_model
->fileItem(1).url();
1351 const QUrl realChild2
= m_model
->fileItem(4).url();
1353 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1354 m_model
->slotCompleted();
1355 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1357 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1358 m_model
->slotCompleted();
1359 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1361 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1362 m_model
->slotCompleted();
1363 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1365 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1366 m_model
->slotCompleted();
1367 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1369 // Set a name filter that matches nothing -> nothing will remain.
1370 m_model
->setNameFilter("xyz");
1371 QCOMPARE(itemsInModel(), QStringList());
1372 QCOMPARE(itemsRemovedSpy
.count(), 1);
1373 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1374 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1375 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 10));
1377 // Set a name filter that matches only "realChild". Their prarents should still show.
1378 m_model
->setNameFilter("realChild");
1379 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1380 QCOMPARE(itemsRemovedSpy
.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
1382 // Collapse "parent1".
1383 m_model
->setExpanded(0, false);
1384 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1385 QCOMPARE(itemsRemovedSpy
.count(), 1);
1386 arguments
= itemsRemovedSpy
.takeFirst();
1387 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1388 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1390 // Remove "parent2".
1391 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1392 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1393 QCOMPARE(itemsRemovedSpy
.count(), 1);
1394 arguments
= itemsRemovedSpy
.takeFirst();
1395 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1396 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1398 // Clear filter, verify that no items reappear.
1399 m_model
->setNameFilter(QString());
1400 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1403 void KFileItemModelTest::testNameRoleGroups()
1405 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1406 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1407 QVERIFY(itemsMovedSpy
.isValid());
1408 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1409 QVERIFY(groupsChangedSpy
.isValid());
1411 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1413 m_model
->setGroupedSorting(true);
1414 m_model
->loadDirectory(m_testDir
->url());
1415 QVERIFY(itemsInsertedSpy
.wait());
1416 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1418 QList
<QPair
<int, QVariant
> > expectedGroups
;
1419 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1420 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1421 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1422 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1423 QCOMPARE(m_model
->groups(), expectedGroups
);
1425 // Rename d.txt to a.txt.
1426 QHash
<QByteArray
, QVariant
> data
;
1427 data
.insert("text", "a.txt");
1428 m_model
->setData(2, data
);
1429 QVERIFY(itemsMovedSpy
.wait());
1430 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1432 expectedGroups
.clear();
1433 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1434 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1435 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1436 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1437 QCOMPARE(m_model
->groups(), expectedGroups
);
1439 // Rename c.txt to d.txt.
1440 data
.insert("text", "d.txt");
1441 m_model
->setData(2, data
);
1442 QVERIFY(groupsChangedSpy
.wait());
1443 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1445 expectedGroups
.clear();
1446 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1447 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1448 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1449 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1450 QCOMPARE(m_model
->groups(), expectedGroups
);
1452 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1453 const KFileItem fileItemD
= m_model
->fileItem(2);
1454 KFileItem fileItemC
= fileItemD
;
1455 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1456 urlC
.setPath(urlC
.path() + "c.txt");
1457 fileItemC
.setUrl(urlC
);
1459 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1460 QVERIFY(groupsChangedSpy
.wait());
1461 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1463 expectedGroups
.clear();
1464 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1465 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1466 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1467 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1468 QCOMPARE(m_model
->groups(), expectedGroups
);
1471 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1473 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1475 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1476 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1477 m_model
->setRoles(modelRoles
);
1479 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1481 m_model
->setGroupedSorting(true);
1482 m_model
->loadDirectory(m_testDir
->url());
1483 QVERIFY(itemsInsertedSpy
.wait());
1484 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1486 QList
<QPair
<int, QVariant
> > expectedGroups
;
1487 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1488 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1489 QCOMPARE(m_model
->groups(), expectedGroups
);
1491 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1492 expectedGroups
.clear();
1493 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1494 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1496 m_model
->setExpanded(0, true);
1497 QVERIFY(m_model
->isExpanded(0));
1498 QVERIFY(itemsInsertedSpy
.wait());
1499 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1500 QCOMPARE(m_model
->groups(), expectedGroups
);
1502 m_model
->setExpanded(3, true);
1503 QVERIFY(m_model
->isExpanded(3));
1504 QVERIFY(itemsInsertedSpy
.wait());
1505 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1506 QCOMPARE(m_model
->groups(), expectedGroups
);
1509 void KFileItemModelTest::testInconsistentModel()
1511 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1513 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1514 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1515 m_model
->setRoles(modelRoles
);
1517 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1519 m_model
->loadDirectory(m_testDir
->url());
1520 QVERIFY(itemsInsertedSpy
.wait());
1521 QCOMPARE(itemsInModel(), QStringList() << "a");
1523 // Expand "a/" and "a/b/".
1524 m_model
->setExpanded(0, true);
1525 QVERIFY(m_model
->isExpanded(0));
1526 QVERIFY(itemsInsertedSpy
.wait());
1527 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1529 m_model
->setExpanded(1, true);
1530 QVERIFY(m_model
->isExpanded(1));
1531 QVERIFY(itemsInsertedSpy
.wait());
1532 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1534 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1535 // Such a thing can in principle happen when performing a search, and there
1537 // (a) match the search string, and
1538 // (b) are children of a folder that matches the search string and is expanded.
1540 // Note that the first item in the list of added items must be new (i.e., not
1541 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1542 // it receives items that are in the model already and ignore them.
1543 QUrl
url(m_model
->directory().url() + "/a2");
1544 KFileItem
newItem(url
);
1546 KFileItemList items
;
1547 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1548 m_model
->slotItemsAdded(m_model
->directory(), items
);
1549 m_model
->slotCompleted();
1550 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1552 m_model
->setExpanded(0, false);
1554 // Test that the right items have been removed, see
1555 // https://bugs.kde.org/show_bug.cgi?id=324371
1556 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1558 // Test that resorting does not cause a crash, see
1559 // https://bugs.kde.org/show_bug.cgi?id=325359
1560 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1561 m_model
->resortAllItems();
1565 void KFileItemModelTest::testChangeRolesForFilteredItems()
1567 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1569 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1570 modelRoles
<< "owner";
1571 m_model
->setRoles(modelRoles
);
1573 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1575 m_model
->loadDirectory(m_testDir
->url());
1576 QVERIFY(itemsInsertedSpy
.wait());
1577 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1579 for (int index
= 0; index
< m_model
->count(); ++index
) {
1580 // All items should have the "text" and "owner" roles, but not "group".
1581 QVERIFY(m_model
->data(index
).contains("text"));
1582 QVERIFY(m_model
->data(index
).contains("owner"));
1583 QVERIFY(!m_model
->data(index
).contains("group"));
1586 // Add a filter, such that only "aaa.txt" remains in the model.
1587 m_model
->setNameFilter("aaa");
1588 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1590 // Add the "group" role.
1591 modelRoles
<< "group";
1592 m_model
->setRoles(modelRoles
);
1594 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1595 m_model
->setNameFilter("aa");
1596 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1598 for (int index
= 0; index
< m_model
->count(); ++index
) {
1599 // All items should have the "text", "owner", and "group" roles.
1600 QVERIFY(m_model
->data(index
).contains("text"));
1601 QVERIFY(m_model
->data(index
).contains("owner"));
1602 QVERIFY(m_model
->data(index
).contains("group"));
1605 // Remove the "owner" role.
1606 modelRoles
.remove("owner");
1607 m_model
->setRoles(modelRoles
);
1609 // Clear the filter, and verify that all items have the expected roles
1610 m_model
->setNameFilter(QString());
1611 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1613 for (int index
= 0; index
< m_model
->count(); ++index
) {
1614 // All items should have the "text" and "group" roles, but now "owner".
1615 QVERIFY(m_model
->data(index
).contains("text"));
1616 QVERIFY(!m_model
->data(index
).contains("owner"));
1617 QVERIFY(m_model
->data(index
).contains("group"));
1621 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1623 KFileItemList items
;
1625 KIO::UDSEntry entry
[3];
1627 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1628 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
1630 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1631 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
1633 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1634 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
1636 for (int i
= 0; i
< 3; ++i
) {
1637 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1638 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1639 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
1640 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1641 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
1642 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1643 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
1646 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1647 m_model
->slotCompleted();
1649 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1652 m_model
->setNameFilter("a");
1653 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1656 m_model
->setSortRole("owner");
1658 // Clear the filter, and verify that the items are sorted correctly.
1659 m_model
->setNameFilter(QString());
1660 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1663 void KFileItemModelTest::testRefreshFilteredItems()
1665 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1667 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1669 m_model
->loadDirectory(m_testDir
->url());
1670 QVERIFY(itemsInsertedSpy
.wait());
1671 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1673 const KFileItem fileItemC
= m_model
->fileItem(2);
1675 // Show only the .txt files.
1676 m_model
->setNameFilter(".txt");
1677 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1679 // Rename one of the .jpg files.
1680 KFileItem fileItemE
= fileItemC
;
1681 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1682 urlE
.setPath(urlE
.path() + "/e.jpg");
1683 fileItemE
.setUrl(urlE
);
1685 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1687 // Show all files again, and verify that the model has updated the file name.
1688 m_model
->setNameFilter(QString());
1689 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1692 void KFileItemModelTest::testCreateMimeData()
1694 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1696 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1697 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1698 m_model
->setRoles(modelRoles
);
1700 m_testDir
->createFile("a/1");
1702 m_model
->loadDirectory(m_testDir
->url());
1703 QVERIFY(itemsInsertedSpy
.wait());
1704 QCOMPARE(itemsInModel(), QStringList() << "a");
1707 m_model
->setExpanded(0, true);
1708 QVERIFY(itemsInsertedSpy
.wait());
1709 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1711 // Verify that creating the MIME data for a child of an expanded folder does
1712 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1714 selection
.insert(1);
1715 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1719 void KFileItemModelTest::testCollapseFolderWhileLoading()
1721 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1723 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1724 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1725 m_model
->setRoles(modelRoles
);
1727 m_testDir
->createFile("a2/b/c1.txt");
1729 m_model
->loadDirectory(m_testDir
->url());
1730 QVERIFY(itemsInsertedSpy
.wait());
1731 QCOMPARE(itemsInModel(), QStringList() << "a2");
1734 m_model
->setExpanded(0, true);
1735 QVERIFY(m_model
->isExpanded(0));
1736 QVERIFY(itemsInsertedSpy
.wait());
1737 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1740 m_model
->setExpanded(1, true);
1741 QVERIFY(m_model
->isExpanded(1));
1742 QVERIFY(itemsInsertedSpy
.wait());
1743 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1745 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1746 // signal is not emitted yet.
1747 const KFileItem fileItemC1
= m_model
->fileItem(2);
1748 KFileItem fileItemC2
= fileItemC1
;
1749 QUrl urlC2
= fileItemC2
.url();
1750 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1751 urlC2
.setPath(urlC2
.path() + "c2.txt");
1752 fileItemC2
.setUrl(urlC2
);
1754 const QUrl urlB
= m_model
->fileItem(1).url();
1755 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1756 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1758 // Collapse "a2/". This should also remove all its (indirect) children from
1759 // the model and from the model's m_pendingItemsToInsert member.
1760 m_model
->setExpanded(0, false);
1761 QCOMPARE(itemsInModel(), QStringList() << "a2");
1763 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1764 // is still in m_pendingItemsToInsert, then we might get a crash, see
1765 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1766 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1767 // without parent in the model.
1768 m_model
->slotCompleted();
1769 QCOMPARE(itemsInModel(), QStringList() << "a2");
1771 // Expand "a2/" again.
1772 m_model
->setExpanded(0, true);
1773 QVERIFY(m_model
->isExpanded(0));
1774 QVERIFY(itemsInsertedSpy
.wait());
1775 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1777 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1778 // completed() signal is not emitted yet.
1779 const KFileItem fileItemA2
= m_model
->fileItem(0);
1780 KFileItem fileItemA1
= fileItemA2
;
1781 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1782 urlA1
.setPath(urlA1
.path() + "a1");
1783 fileItemA1
.setUrl(urlA1
);
1785 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1786 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1788 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1789 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1790 // confuse the code which collapses the folder.
1791 m_model
->setExpanded(0, false);
1792 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1793 QVERIFY(!m_model
->isExpanded(0));
1794 QVERIFY(!m_model
->isExpanded(1));
1797 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1799 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1801 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1803 m_model
->loadDirectory(m_testDir
->url());
1804 QVERIFY(itemsInsertedSpy
.wait());
1805 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1807 const KFileItem fileItemB
= m_model
->fileItem(1);
1809 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1811 list
<< fileItemB
<< fileItemB
;
1812 m_model
->slotItemsDeleted(list
);
1814 QVERIFY(m_model
->isConsistent());
1815 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1818 QStringList
KFileItemModelTest::itemsInModel() const
1821 for (int i
= 0; i
< m_model
->count(); i
++) {
1822 items
<< m_model
->fileItem(i
).text();
1827 QTEST_MAIN(KFileItemModelTest
)
1829 #include "kfileitemmodeltest.moc"