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>
17 #include "kitemviews/kfileitemmodel.h"
18 #include "kitemviews/private/kfileitemmodeldirlister.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
821 // │ │ ├─ 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 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a" << ".g" << ".f");
972 QCOMPARE(itemsMovedSpy
.count(), 0);
973 QCOMPARE(itemsInsertedSpy
.count(), 1);
974 QCOMPARE(itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>(), KItemRangeList() << KItemRange(8, 2));
977 m_model
->setSortRole("text");
978 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a" << ".g" << ".f");
979 QCOMPARE(itemsMovedSpy
.count(), 1);
980 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 2));
981 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 5 << 4);
984 m_model
->setSortOrder(Qt::AscendingOrder
);
985 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e" << ".g" << ".f");
986 QCOMPARE(itemsMovedSpy
.count(), 1);
987 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
988 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
990 // 'Sort Folders First' disabled
991 m_model
->setSortDirectoriesFirst(false);
992 QVERIFY(!m_model
->sortDirectoriesFirst());
993 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e" << ".f" << ".g");
994 QCOMPARE(itemsMovedSpy
.count(), 1);
995 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 10));
996 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1000 void KFileItemModelTest::testIndexForKeyboardSearch()
1002 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1004 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1006 m_model
->loadDirectory(m_testDir
->url());
1007 QVERIFY(itemsInsertedSpy
.wait());
1009 // Search from index 0
1010 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
1011 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
1012 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
1013 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
1014 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
1015 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
1016 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
1017 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
1018 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
1019 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
1020 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
1022 // Start a search somewhere in the middle
1023 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
1024 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
1025 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
1026 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
1028 // Test searches that go past the last item back to index 0
1029 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
1030 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
1031 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
1032 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
1034 // Test searches that yield no result
1035 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
1036 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
1037 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
1038 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
1039 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
1041 // Test upper case searches (note that search is case insensitive)
1042 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
1043 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
1044 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
1045 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1047 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1050 void KFileItemModelTest::testNameFilter()
1052 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1054 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1056 m_model
->loadDirectory(m_testDir
->url());
1057 QVERIFY(itemsInsertedSpy
.wait());
1059 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1060 QCOMPARE(m_model
->count(), 3);
1062 m_model
->setNameFilter("A2"); // Shows only A2
1063 QCOMPARE(m_model
->count(), 1);
1065 m_model
->setNameFilter("A2"); // Shows only A1
1066 QCOMPARE(m_model
->count(), 1);
1068 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1069 QCOMPARE(m_model
->count(), 2);
1071 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1072 QCOMPARE(m_model
->count(), 2);
1074 m_model
->setNameFilter(QString()); // Shows again all items
1075 QCOMPARE(m_model
->count(), 5);
1079 * Verifies that we do not crash when adding a KFileItem with an empty path.
1080 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1081 * tried to always read the first character of the path, even if the path is empty.
1083 void KFileItemModelTest::testEmptyPath()
1085 QSet
<QByteArray
> roles
;
1086 roles
.insert("text");
1087 roles
.insert("isExpanded");
1088 roles
.insert("isExpandable");
1089 roles
.insert("expandedParentsCount");
1090 m_model
->setRoles(roles
);
1092 const QUrl emptyUrl
;
1093 QVERIFY(emptyUrl
.path().isEmpty());
1095 const QUrl
url("file:///test/");
1097 KFileItemList items
;
1098 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1099 m_model
->slotItemsAdded(emptyUrl
, items
);
1100 m_model
->slotCompleted();
1104 * Verifies that the 'isExpanded' state of folders does not change when the
1105 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1107 void KFileItemModelTest::testRefreshExpandedItem()
1109 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1110 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1111 QVERIFY(itemsChangedSpy
.isValid());
1113 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1114 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1115 m_model
->setRoles(modelRoles
);
1117 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1119 m_model
->loadDirectory(m_testDir
->url());
1120 QVERIFY(itemsInsertedSpy
.wait());
1121 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1123 m_model
->setExpanded(0, true);
1124 QVERIFY(itemsInsertedSpy
.wait());
1125 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1126 QVERIFY(m_model
->isExpanded(0));
1128 const KFileItem item
= m_model
->fileItem(0);
1129 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1130 QVERIFY(!itemsChangedSpy
.isEmpty());
1132 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1133 QVERIFY(m_model
->isExpanded(0));
1137 * Verify that removing hidden files and folders from the model does not
1138 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1140 void KFileItemModelTest::testRemoveHiddenItems()
1142 m_testDir
->createDir(".a");
1143 m_testDir
->createDir(".b");
1144 m_testDir
->createDir("c");
1145 m_testDir
->createDir("d");
1146 m_testDir
->createFiles({".f", ".g", "h", "i"});
1148 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1149 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1151 m_model
->setShowHiddenFiles(true);
1152 m_model
->loadDirectory(m_testDir
->url());
1153 QVERIFY(itemsInsertedSpy
.wait());
1154 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i" << ".a" << ".b" <<".f" << ".g");
1155 QCOMPARE(itemsInsertedSpy
.count(), 1);
1156 QCOMPARE(itemsRemovedSpy
.count(), 0);
1157 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1158 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1160 m_model
->setShowHiddenFiles(false);
1161 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1162 QCOMPARE(itemsInsertedSpy
.count(), 0);
1163 QCOMPARE(itemsRemovedSpy
.count(), 1);
1164 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1165 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 4));
1167 m_model
->setShowHiddenFiles(true);
1168 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i" << ".a" << ".b" <<".f" << ".g");
1169 QCOMPARE(itemsInsertedSpy
.count(), 1);
1170 QCOMPARE(itemsRemovedSpy
.count(), 0);
1171 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1172 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 4));
1175 QCOMPARE(itemsInModel(), QStringList());
1176 QCOMPARE(itemsInsertedSpy
.count(), 0);
1177 QCOMPARE(itemsRemovedSpy
.count(), 1);
1178 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1179 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1181 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1182 // Verify that this does not make the model crash.
1183 m_model
->setShowHiddenFiles(false);
1187 * Verify that filtered items are removed when their parent is collapsed.
1189 void KFileItemModelTest::collapseParentOfHiddenItems()
1191 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1192 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1194 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1195 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1196 m_model
->setRoles(modelRoles
);
1198 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1200 m_model
->loadDirectory(m_testDir
->url());
1201 QVERIFY(itemsInsertedSpy
.wait());
1202 QCOMPARE(m_model
->count(), 1); // Only "a/"
1205 m_model
->setExpanded(0, true);
1206 QVERIFY(itemsInsertedSpy
.wait());
1207 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1210 m_model
->setExpanded(1, true);
1211 QVERIFY(itemsInsertedSpy
.wait());
1212 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1215 m_model
->setExpanded(2, true);
1216 QVERIFY(itemsInsertedSpy
.wait());
1217 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"
1219 // Set a name filter that matches nothing -> only the expanded folders remain.
1220 m_model
->setNameFilter("xyz");
1221 QCOMPARE(itemsRemovedSpy
.count(), 1);
1222 QCOMPARE(m_model
->count(), 3);
1223 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1225 // Collapse the folder "a/".
1226 m_model
->setExpanded(0, false);
1227 QCOMPARE(itemsRemovedSpy
.count(), 2);
1228 QCOMPARE(m_model
->count(), 1);
1229 QCOMPARE(itemsInModel(), QStringList() << "a");
1231 // Remove the filter -> no files should appear (and we should not get a crash).
1232 m_model
->setNameFilter(QString());
1233 QCOMPARE(m_model
->count(), 1);
1237 * Verify that filtered items are removed when their parent is deleted.
1239 void KFileItemModelTest::removeParentOfHiddenItems()
1241 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1242 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1244 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1245 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1246 m_model
->setRoles(modelRoles
);
1248 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1250 m_model
->loadDirectory(m_testDir
->url());
1251 QVERIFY(itemsInsertedSpy
.wait());
1252 QCOMPARE(m_model
->count(), 1); // Only "a/"
1255 m_model
->setExpanded(0, true);
1256 QVERIFY(itemsInsertedSpy
.wait());
1257 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1260 m_model
->setExpanded(1, true);
1261 QVERIFY(itemsInsertedSpy
.wait());
1262 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1265 m_model
->setExpanded(2, true);
1266 QVERIFY(itemsInsertedSpy
.wait());
1267 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"
1269 // Set a name filter that matches nothing -> only the expanded folders remain.
1270 m_model
->setNameFilter("xyz");
1271 QCOMPARE(itemsRemovedSpy
.count(), 1);
1272 QCOMPARE(m_model
->count(), 3);
1273 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1275 // Simulate the deletion of the directory "a/b/".
1276 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1277 QCOMPARE(itemsRemovedSpy
.count(), 2);
1278 QCOMPARE(m_model
->count(), 1);
1279 QCOMPARE(itemsInModel(), QStringList() << "a");
1281 // Remove the filter -> only the file "a/1" should appear.
1282 m_model
->setNameFilter(QString());
1283 QCOMPARE(m_model
->count(), 2);
1284 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1288 * Create a tree structure where parent-child relationships can not be
1289 * determined by parsing the URLs, and verify that KFileItemModel
1290 * handles them correctly.
1292 void KFileItemModelTest::testGeneralParentChildRelationships()
1294 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1295 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1297 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1298 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1299 m_model
->setRoles(modelRoles
);
1301 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1303 m_model
->loadDirectory(m_testDir
->url());
1304 QVERIFY(itemsInsertedSpy
.wait());
1305 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1307 // Expand all folders.
1308 m_model
->setExpanded(0, true);
1309 QVERIFY(itemsInsertedSpy
.wait());
1310 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1312 m_model
->setExpanded(1, true);
1313 QVERIFY(itemsInsertedSpy
.wait());
1314 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1316 m_model
->setExpanded(3, true);
1317 QVERIFY(itemsInsertedSpy
.wait());
1318 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1320 m_model
->setExpanded(4, true);
1321 QVERIFY(itemsInsertedSpy
.wait());
1322 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1324 // Add some more children and grand-children.
1325 const QUrl parent1
= m_model
->fileItem(0).url();
1326 const QUrl parent2
= m_model
->fileItem(3).url();
1327 const QUrl realChild1
= m_model
->fileItem(1).url();
1328 const QUrl realChild2
= m_model
->fileItem(4).url();
1330 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1331 m_model
->slotCompleted();
1332 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1334 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1335 m_model
->slotCompleted();
1336 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1338 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1339 m_model
->slotCompleted();
1340 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1342 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1343 m_model
->slotCompleted();
1344 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1346 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1347 m_model
->slotCompleted();
1348 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1350 // Set a name filter that matches nothing -> only expanded folders remain.
1351 m_model
->setNameFilter("xyz");
1352 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1353 QCOMPARE(itemsRemovedSpy
.count(), 1);
1354 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1355 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1356 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1358 // Collapse "parent1".
1359 m_model
->setExpanded(0, false);
1360 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1361 QCOMPARE(itemsRemovedSpy
.count(), 1);
1362 arguments
= itemsRemovedSpy
.takeFirst();
1363 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1364 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1366 // Remove "parent2".
1367 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1368 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1369 QCOMPARE(itemsRemovedSpy
.count(), 1);
1370 arguments
= itemsRemovedSpy
.takeFirst();
1371 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1372 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1374 // Clear filter, verify that no items reappear.
1375 m_model
->setNameFilter(QString());
1376 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1379 void KFileItemModelTest::testNameRoleGroups()
1381 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1382 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1383 QVERIFY(itemsMovedSpy
.isValid());
1384 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1385 QVERIFY(groupsChangedSpy
.isValid());
1387 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1389 m_model
->setGroupedSorting(true);
1390 m_model
->loadDirectory(m_testDir
->url());
1391 QVERIFY(itemsInsertedSpy
.wait());
1392 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1394 QList
<QPair
<int, QVariant
> > expectedGroups
;
1395 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1396 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1397 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1398 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1399 QCOMPARE(m_model
->groups(), expectedGroups
);
1401 // Rename d.txt to a.txt.
1402 QHash
<QByteArray
, QVariant
> data
;
1403 data
.insert("text", "a.txt");
1404 m_model
->setData(2, data
);
1405 QVERIFY(itemsMovedSpy
.wait());
1406 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1408 expectedGroups
.clear();
1409 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1410 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1411 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1412 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1413 QCOMPARE(m_model
->groups(), expectedGroups
);
1415 // Rename c.txt to d.txt.
1416 data
.insert("text", "d.txt");
1417 m_model
->setData(2, data
);
1418 QVERIFY(groupsChangedSpy
.wait());
1419 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1421 expectedGroups
.clear();
1422 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1423 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1424 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1425 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1426 QCOMPARE(m_model
->groups(), expectedGroups
);
1428 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1429 const KFileItem fileItemD
= m_model
->fileItem(2);
1430 KFileItem fileItemC
= fileItemD
;
1431 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1432 urlC
.setPath(urlC
.path() + "c.txt");
1433 fileItemC
.setUrl(urlC
);
1435 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1436 QVERIFY(groupsChangedSpy
.wait());
1437 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1439 expectedGroups
.clear();
1440 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1441 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1442 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1443 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1444 QCOMPARE(m_model
->groups(), expectedGroups
);
1447 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1449 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1451 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1452 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1453 m_model
->setRoles(modelRoles
);
1455 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1457 m_model
->setGroupedSorting(true);
1458 m_model
->loadDirectory(m_testDir
->url());
1459 QVERIFY(itemsInsertedSpy
.wait());
1460 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1462 QList
<QPair
<int, QVariant
> > expectedGroups
;
1463 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1464 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1465 QCOMPARE(m_model
->groups(), expectedGroups
);
1467 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1468 expectedGroups
.clear();
1469 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1470 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1472 m_model
->setExpanded(0, true);
1473 QVERIFY(m_model
->isExpanded(0));
1474 QVERIFY(itemsInsertedSpy
.wait());
1475 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1476 QCOMPARE(m_model
->groups(), expectedGroups
);
1478 m_model
->setExpanded(3, true);
1479 QVERIFY(m_model
->isExpanded(3));
1480 QVERIFY(itemsInsertedSpy
.wait());
1481 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1482 QCOMPARE(m_model
->groups(), expectedGroups
);
1485 void KFileItemModelTest::testInconsistentModel()
1487 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1489 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1490 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1491 m_model
->setRoles(modelRoles
);
1493 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1495 m_model
->loadDirectory(m_testDir
->url());
1496 QVERIFY(itemsInsertedSpy
.wait());
1497 QCOMPARE(itemsInModel(), QStringList() << "a");
1499 // Expand "a/" and "a/b/".
1500 m_model
->setExpanded(0, true);
1501 QVERIFY(m_model
->isExpanded(0));
1502 QVERIFY(itemsInsertedSpy
.wait());
1503 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1505 m_model
->setExpanded(1, true);
1506 QVERIFY(m_model
->isExpanded(1));
1507 QVERIFY(itemsInsertedSpy
.wait());
1508 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1510 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1511 // Such a thing can in principle happen when performing a search, and there
1513 // (a) match the search string, and
1514 // (b) are children of a folder that matches the search string and is expanded.
1516 // Note that the first item in the list of added items must be new (i.e., not
1517 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1518 // it receives items that are in the model already and ignore them.
1519 QUrl
url(m_model
->directory().url() + "/a2");
1520 KFileItem
newItem(url
);
1522 KFileItemList items
;
1523 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1524 m_model
->slotItemsAdded(m_model
->directory(), items
);
1525 m_model
->slotCompleted();
1526 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1528 m_model
->setExpanded(0, false);
1530 // Test that the right items have been removed, see
1531 // https://bugs.kde.org/show_bug.cgi?id=324371
1532 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1534 // Test that resorting does not cause a crash, see
1535 // https://bugs.kde.org/show_bug.cgi?id=325359
1536 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1537 m_model
->resortAllItems();
1541 void KFileItemModelTest::testChangeRolesForFilteredItems()
1543 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1545 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1546 modelRoles
<< "owner";
1547 m_model
->setRoles(modelRoles
);
1549 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1551 m_model
->loadDirectory(m_testDir
->url());
1552 QVERIFY(itemsInsertedSpy
.wait());
1553 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1555 for (int index
= 0; index
< m_model
->count(); ++index
) {
1556 // All items should have the "text" and "owner" roles, but not "group".
1557 QVERIFY(m_model
->data(index
).contains("text"));
1558 QVERIFY(m_model
->data(index
).contains("owner"));
1559 QVERIFY(!m_model
->data(index
).contains("group"));
1562 // Add a filter, such that only "aaa.txt" remains in the model.
1563 m_model
->setNameFilter("aaa");
1564 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1566 // Add the "group" role.
1567 modelRoles
<< "group";
1568 m_model
->setRoles(modelRoles
);
1570 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1571 m_model
->setNameFilter("aa");
1572 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1574 for (int index
= 0; index
< m_model
->count(); ++index
) {
1575 // All items should have the "text", "owner", and "group" roles.
1576 QVERIFY(m_model
->data(index
).contains("text"));
1577 QVERIFY(m_model
->data(index
).contains("owner"));
1578 QVERIFY(m_model
->data(index
).contains("group"));
1581 // Remove the "owner" role.
1582 modelRoles
.remove("owner");
1583 m_model
->setRoles(modelRoles
);
1585 // Clear the filter, and verify that all items have the expected roles
1586 m_model
->setNameFilter(QString());
1587 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1589 for (int index
= 0; index
< m_model
->count(); ++index
) {
1590 // All items should have the "text" and "group" roles, but now "owner".
1591 QVERIFY(m_model
->data(index
).contains("text"));
1592 QVERIFY(!m_model
->data(index
).contains("owner"));
1593 QVERIFY(m_model
->data(index
).contains("group"));
1597 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1599 KFileItemList items
;
1601 KIO::UDSEntry entry
[3];
1603 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1604 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
1606 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1607 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
1609 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1610 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
1612 for (int i
= 0; i
< 3; ++i
) {
1613 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1614 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1615 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
1616 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1617 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
1618 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1619 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
1622 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1623 m_model
->slotCompleted();
1625 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1628 m_model
->setNameFilter("a");
1629 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1632 m_model
->setSortRole("owner");
1634 // Clear the filter, and verify that the items are sorted correctly.
1635 m_model
->setNameFilter(QString());
1636 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1639 void KFileItemModelTest::testRefreshFilteredItems()
1641 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1643 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1645 m_model
->loadDirectory(m_testDir
->url());
1646 QVERIFY(itemsInsertedSpy
.wait());
1647 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1649 const KFileItem fileItemC
= m_model
->fileItem(2);
1651 // Show only the .txt files.
1652 m_model
->setNameFilter(".txt");
1653 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1655 // Rename one of the .jpg files.
1656 KFileItem fileItemE
= fileItemC
;
1657 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1658 urlE
.setPath(urlE
.path() + "/e.jpg");
1659 fileItemE
.setUrl(urlE
);
1661 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1663 // Show all files again, and verify that the model has updated the file name.
1664 m_model
->setNameFilter(QString());
1665 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1668 void KFileItemModelTest::testCreateMimeData()
1670 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1672 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1673 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1674 m_model
->setRoles(modelRoles
);
1676 m_testDir
->createFile("a/1");
1678 m_model
->loadDirectory(m_testDir
->url());
1679 QVERIFY(itemsInsertedSpy
.wait());
1680 QCOMPARE(itemsInModel(), QStringList() << "a");
1683 m_model
->setExpanded(0, true);
1684 QVERIFY(itemsInsertedSpy
.wait());
1685 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1687 // Verify that creating the MIME data for a child of an expanded folder does
1688 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1690 selection
.insert(1);
1691 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1695 void KFileItemModelTest::testCollapseFolderWhileLoading()
1697 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1699 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1700 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1701 m_model
->setRoles(modelRoles
);
1703 m_testDir
->createFile("a2/b/c1.txt");
1705 m_model
->loadDirectory(m_testDir
->url());
1706 QVERIFY(itemsInsertedSpy
.wait());
1707 QCOMPARE(itemsInModel(), QStringList() << "a2");
1710 m_model
->setExpanded(0, true);
1711 QVERIFY(m_model
->isExpanded(0));
1712 QVERIFY(itemsInsertedSpy
.wait());
1713 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1716 m_model
->setExpanded(1, true);
1717 QVERIFY(m_model
->isExpanded(1));
1718 QVERIFY(itemsInsertedSpy
.wait());
1719 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1721 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1722 // signal is not emitted yet.
1723 const KFileItem fileItemC1
= m_model
->fileItem(2);
1724 KFileItem fileItemC2
= fileItemC1
;
1725 QUrl urlC2
= fileItemC2
.url();
1726 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1727 urlC2
.setPath(urlC2
.path() + "c2.txt");
1728 fileItemC2
.setUrl(urlC2
);
1730 const QUrl urlB
= m_model
->fileItem(1).url();
1731 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1732 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1734 // Collapse "a2/". This should also remove all its (indirect) children from
1735 // the model and from the model's m_pendingItemsToInsert member.
1736 m_model
->setExpanded(0, false);
1737 QCOMPARE(itemsInModel(), QStringList() << "a2");
1739 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1740 // is still in m_pendingItemsToInsert, then we might get a crash, see
1741 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1742 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1743 // without parent in the model.
1744 m_model
->slotCompleted();
1745 QCOMPARE(itemsInModel(), QStringList() << "a2");
1747 // Expand "a2/" again.
1748 m_model
->setExpanded(0, true);
1749 QVERIFY(m_model
->isExpanded(0));
1750 QVERIFY(itemsInsertedSpy
.wait());
1751 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1753 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1754 // completed() signal is not emitted yet.
1755 const KFileItem fileItemA2
= m_model
->fileItem(0);
1756 KFileItem fileItemA1
= fileItemA2
;
1757 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1758 urlA1
.setPath(urlA1
.path() + "a1");
1759 fileItemA1
.setUrl(urlA1
);
1761 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1762 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1764 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1765 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1766 // confuse the code which collapses the folder.
1767 m_model
->setExpanded(0, false);
1768 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1769 QVERIFY(!m_model
->isExpanded(0));
1770 QVERIFY(!m_model
->isExpanded(1));
1773 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1775 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1777 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1779 m_model
->loadDirectory(m_testDir
->url());
1780 QVERIFY(itemsInsertedSpy
.wait());
1781 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1783 const KFileItem fileItemB
= m_model
->fileItem(1);
1785 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1787 list
<< fileItemB
<< fileItemB
;
1788 m_model
->slotItemsDeleted(list
);
1790 QVERIFY(m_model
->isConsistent());
1791 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1794 QStringList
KFileItemModelTest::itemsInModel() const
1797 for (int i
= 0; i
< m_model
->count(); i
++) {
1798 items
<< m_model
->fileItem(i
).text();
1803 QTEST_MAIN(KFileItemModelTest
)
1805 #include "kfileitemmodeltest.moc"