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 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
816 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
817 QVERIFY(itemsMovedSpy
.isValid());
819 // Create some files with different sizes and modification times to check the different sorting options
820 QDateTime now
= QDateTime::currentDateTime();
822 QSet
<QByteArray
> roles
;
823 roles
.insert("text");
824 roles
.insert("isExpanded");
825 roles
.insert("isExpandable");
826 roles
.insert("expandedParentsCount");
827 m_model
->setRoles(roles
);
829 m_testDir
->createDir("c/c-2");
830 m_testDir
->createFile("c/c-2/c-3");
831 m_testDir
->createFile("c/c-1");
833 m_testDir
->createFile("a", "A file", now
.addDays(-3));
834 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
835 m_testDir
->createDir("c", now
.addDays(-2));
836 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
837 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
838 m_testDir
->createFile(".f");
840 m_model
->loadDirectory(m_testDir
->url());
841 QVERIFY(itemsInsertedSpy
.wait());
843 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
844 m_model
->setExpanded(index
, true);
845 QVERIFY(itemsInsertedSpy
.wait());
847 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
848 m_model
->setExpanded(index
, true);
849 QVERIFY(itemsInsertedSpy
.wait());
851 // Default: Sort by Name, ascending
852 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
853 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
854 QVERIFY(m_model
->sortDirectoriesFirst());
855 QVERIFY(!m_model
->showHiddenFiles());
856 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
858 // Sort by Name, ascending, 'Sort Folders First' disabled
859 m_model
->setSortDirectoriesFirst(false);
860 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
861 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
862 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
863 QCOMPARE(itemsMovedSpy
.count(), 1);
864 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
865 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
867 // Sort by Name, descending
868 m_model
->setSortDirectoriesFirst(true);
869 m_model
->setSortOrder(Qt::DescendingOrder
);
870 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
871 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
872 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
873 QCOMPARE(itemsMovedSpy
.count(), 2);
874 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
875 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
876 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
877 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
879 // Sort by Date, descending
880 m_model
->setSortDirectoriesFirst(true);
881 m_model
->setSortRole("modificationtime");
882 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
883 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
884 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
885 QCOMPARE(itemsMovedSpy
.count(), 1);
886 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
887 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
889 // Sort by Date, ascending
890 m_model
->setSortOrder(Qt::AscendingOrder
);
891 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
892 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
893 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
894 QCOMPARE(itemsMovedSpy
.count(), 1);
895 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
896 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
898 // Sort by Date, ascending, 'Sort Folders First' disabled
899 m_model
->setSortDirectoriesFirst(false);
900 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
901 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
902 QVERIFY(!m_model
->sortDirectoriesFirst());
903 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
904 QCOMPARE(itemsMovedSpy
.count(), 1);
905 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
906 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
908 // Sort by Name, ascending, 'Sort Folders First' disabled
909 m_model
->setSortRole("text");
910 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
911 QVERIFY(!m_model
->sortDirectoriesFirst());
912 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
913 QCOMPARE(itemsMovedSpy
.count(), 1);
914 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
915 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
917 // Sort by Size, ascending, 'Sort Folders First' disabled
918 m_model
->setSortRole("size");
919 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
920 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
921 QVERIFY(!m_model
->sortDirectoriesFirst());
922 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
923 QCOMPARE(itemsMovedSpy
.count(), 1);
924 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
925 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
927 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
928 m_model
->setSortDirectoriesFirst(true);
929 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
930 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
931 QVERIFY(m_model
->sortDirectoriesFirst());
932 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
933 QCOMPARE(itemsMovedSpy
.count(), 0);
935 // Sort by Size, descending, 'Sort Folders First' enabled
936 m_model
->setSortOrder(Qt::DescendingOrder
);
937 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
938 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
939 QVERIFY(m_model
->sortDirectoriesFirst());
940 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
941 QCOMPARE(itemsMovedSpy
.count(), 1);
942 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
943 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
945 // TODO: Sort by other roles; show/hide hidden files
948 void KFileItemModelTest::testIndexForKeyboardSearch()
950 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
952 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
954 m_model
->loadDirectory(m_testDir
->url());
955 QVERIFY(itemsInsertedSpy
.wait());
957 // Search from index 0
958 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
959 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
960 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
961 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
962 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
963 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
964 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
965 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
966 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
967 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
968 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
970 // Start a search somewhere in the middle
971 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
972 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
973 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
974 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
976 // Test searches that go past the last item back to index 0
977 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
978 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
979 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
980 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
982 // Test searches that yield no result
983 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
984 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
985 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
986 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
987 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
989 // Test upper case searches (note that search is case insensitive)
990 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
991 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
992 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
993 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
995 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
998 void KFileItemModelTest::testNameFilter()
1000 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1002 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1004 m_model
->loadDirectory(m_testDir
->url());
1005 QVERIFY(itemsInsertedSpy
.wait());
1007 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1008 QCOMPARE(m_model
->count(), 3);
1010 m_model
->setNameFilter("A2"); // Shows only A2
1011 QCOMPARE(m_model
->count(), 1);
1013 m_model
->setNameFilter("A2"); // Shows only A1
1014 QCOMPARE(m_model
->count(), 1);
1016 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1017 QCOMPARE(m_model
->count(), 2);
1019 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1020 QCOMPARE(m_model
->count(), 2);
1022 m_model
->setNameFilter(QString()); // Shows again all items
1023 QCOMPARE(m_model
->count(), 5);
1027 * Verifies that we do not crash when adding a KFileItem with an empty path.
1028 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1029 * tried to always read the first character of the path, even if the path is empty.
1031 void KFileItemModelTest::testEmptyPath()
1033 QSet
<QByteArray
> roles
;
1034 roles
.insert("text");
1035 roles
.insert("isExpanded");
1036 roles
.insert("isExpandable");
1037 roles
.insert("expandedParentsCount");
1038 m_model
->setRoles(roles
);
1040 const QUrl emptyUrl
;
1041 QVERIFY(emptyUrl
.path().isEmpty());
1043 const QUrl
url("file:///test/");
1045 KFileItemList items
;
1046 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1047 m_model
->slotItemsAdded(emptyUrl
, items
);
1048 m_model
->slotCompleted();
1052 * Verifies that the 'isExpanded' state of folders does not change when the
1053 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1055 void KFileItemModelTest::testRefreshExpandedItem()
1057 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1058 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1059 QVERIFY(itemsChangedSpy
.isValid());
1061 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1062 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1063 m_model
->setRoles(modelRoles
);
1065 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1067 m_model
->loadDirectory(m_testDir
->url());
1068 QVERIFY(itemsInsertedSpy
.wait());
1069 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1071 m_model
->setExpanded(0, true);
1072 QVERIFY(itemsInsertedSpy
.wait());
1073 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1074 QVERIFY(m_model
->isExpanded(0));
1076 const KFileItem item
= m_model
->fileItem(0);
1077 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1078 QVERIFY(!itemsChangedSpy
.isEmpty());
1080 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1081 QVERIFY(m_model
->isExpanded(0));
1085 * Verify that removing hidden files and folders from the model does not
1086 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1088 void KFileItemModelTest::testRemoveHiddenItems()
1090 m_testDir
->createDir(".a");
1091 m_testDir
->createDir(".b");
1092 m_testDir
->createDir("c");
1093 m_testDir
->createDir("d");
1094 m_testDir
->createFiles({".f", ".g", "h", "i"});
1096 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1097 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1099 m_model
->setShowHiddenFiles(true);
1100 m_model
->loadDirectory(m_testDir
->url());
1101 QVERIFY(itemsInsertedSpy
.wait());
1102 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1103 QCOMPARE(itemsInsertedSpy
.count(), 1);
1104 QCOMPARE(itemsRemovedSpy
.count(), 0);
1105 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1106 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1108 m_model
->setShowHiddenFiles(false);
1109 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1110 QCOMPARE(itemsInsertedSpy
.count(), 0);
1111 QCOMPARE(itemsRemovedSpy
.count(), 1);
1112 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1113 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1115 m_model
->setShowHiddenFiles(true);
1116 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1117 QCOMPARE(itemsInsertedSpy
.count(), 1);
1118 QCOMPARE(itemsRemovedSpy
.count(), 0);
1119 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1120 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1123 QCOMPARE(itemsInModel(), QStringList());
1124 QCOMPARE(itemsInsertedSpy
.count(), 0);
1125 QCOMPARE(itemsRemovedSpy
.count(), 1);
1126 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1127 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1129 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1130 // Verify that this does not make the model crash.
1131 m_model
->setShowHiddenFiles(false);
1135 * Verify that filtered items are removed when their parent is collapsed.
1137 void KFileItemModelTest::collapseParentOfHiddenItems()
1139 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1140 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1142 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1143 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1144 m_model
->setRoles(modelRoles
);
1146 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1148 m_model
->loadDirectory(m_testDir
->url());
1149 QVERIFY(itemsInsertedSpy
.wait());
1150 QCOMPARE(m_model
->count(), 1); // Only "a/"
1153 m_model
->setExpanded(0, true);
1154 QVERIFY(itemsInsertedSpy
.wait());
1155 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1158 m_model
->setExpanded(1, true);
1159 QVERIFY(itemsInsertedSpy
.wait());
1160 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1163 m_model
->setExpanded(2, true);
1164 QVERIFY(itemsInsertedSpy
.wait());
1165 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"
1167 // Set a name filter that matches nothing -> only the expanded folders remain.
1168 m_model
->setNameFilter("xyz");
1169 QCOMPARE(itemsRemovedSpy
.count(), 1);
1170 QCOMPARE(m_model
->count(), 3);
1171 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1173 // Collapse the folder "a/".
1174 m_model
->setExpanded(0, false);
1175 QCOMPARE(itemsRemovedSpy
.count(), 2);
1176 QCOMPARE(m_model
->count(), 1);
1177 QCOMPARE(itemsInModel(), QStringList() << "a");
1179 // Remove the filter -> no files should appear (and we should not get a crash).
1180 m_model
->setNameFilter(QString());
1181 QCOMPARE(m_model
->count(), 1);
1185 * Verify that filtered items are removed when their parent is deleted.
1187 void KFileItemModelTest::removeParentOfHiddenItems()
1189 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1190 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1192 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1193 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1194 m_model
->setRoles(modelRoles
);
1196 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1198 m_model
->loadDirectory(m_testDir
->url());
1199 QVERIFY(itemsInsertedSpy
.wait());
1200 QCOMPARE(m_model
->count(), 1); // Only "a/"
1203 m_model
->setExpanded(0, true);
1204 QVERIFY(itemsInsertedSpy
.wait());
1205 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1208 m_model
->setExpanded(1, true);
1209 QVERIFY(itemsInsertedSpy
.wait());
1210 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1213 m_model
->setExpanded(2, true);
1214 QVERIFY(itemsInsertedSpy
.wait());
1215 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"
1217 // Set a name filter that matches nothing -> only the expanded folders remain.
1218 m_model
->setNameFilter("xyz");
1219 QCOMPARE(itemsRemovedSpy
.count(), 1);
1220 QCOMPARE(m_model
->count(), 3);
1221 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1223 // Simulate the deletion of the directory "a/b/".
1224 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1225 QCOMPARE(itemsRemovedSpy
.count(), 2);
1226 QCOMPARE(m_model
->count(), 1);
1227 QCOMPARE(itemsInModel(), QStringList() << "a");
1229 // Remove the filter -> only the file "a/1" should appear.
1230 m_model
->setNameFilter(QString());
1231 QCOMPARE(m_model
->count(), 2);
1232 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1236 * Create a tree structure where parent-child relationships can not be
1237 * determined by parsing the URLs, and verify that KFileItemModel
1238 * handles them correctly.
1240 void KFileItemModelTest::testGeneralParentChildRelationships()
1242 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1243 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1245 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1246 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1247 m_model
->setRoles(modelRoles
);
1249 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1251 m_model
->loadDirectory(m_testDir
->url());
1252 QVERIFY(itemsInsertedSpy
.wait());
1253 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1255 // Expand all folders.
1256 m_model
->setExpanded(0, true);
1257 QVERIFY(itemsInsertedSpy
.wait());
1258 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1260 m_model
->setExpanded(1, true);
1261 QVERIFY(itemsInsertedSpy
.wait());
1262 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1264 m_model
->setExpanded(3, true);
1265 QVERIFY(itemsInsertedSpy
.wait());
1266 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1268 m_model
->setExpanded(4, true);
1269 QVERIFY(itemsInsertedSpy
.wait());
1270 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1272 // Add some more children and grand-children.
1273 const QUrl parent1
= m_model
->fileItem(0).url();
1274 const QUrl parent2
= m_model
->fileItem(3).url();
1275 const QUrl realChild1
= m_model
->fileItem(1).url();
1276 const QUrl realChild2
= m_model
->fileItem(4).url();
1278 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1279 m_model
->slotCompleted();
1280 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1282 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1283 m_model
->slotCompleted();
1284 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1286 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1287 m_model
->slotCompleted();
1288 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1290 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1291 m_model
->slotCompleted();
1292 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1294 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1295 m_model
->slotCompleted();
1296 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1298 // Set a name filter that matches nothing -> only expanded folders remain.
1299 m_model
->setNameFilter("xyz");
1300 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1301 QCOMPARE(itemsRemovedSpy
.count(), 1);
1302 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1303 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1304 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1306 // Collapse "parent1".
1307 m_model
->setExpanded(0, false);
1308 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1309 QCOMPARE(itemsRemovedSpy
.count(), 1);
1310 arguments
= itemsRemovedSpy
.takeFirst();
1311 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1312 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1314 // Remove "parent2".
1315 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1316 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1317 QCOMPARE(itemsRemovedSpy
.count(), 1);
1318 arguments
= itemsRemovedSpy
.takeFirst();
1319 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1320 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1322 // Clear filter, verify that no items reappear.
1323 m_model
->setNameFilter(QString());
1324 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1327 void KFileItemModelTest::testNameRoleGroups()
1329 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1330 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1331 QVERIFY(itemsMovedSpy
.isValid());
1332 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1333 QVERIFY(groupsChangedSpy
.isValid());
1335 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1337 m_model
->setGroupedSorting(true);
1338 m_model
->loadDirectory(m_testDir
->url());
1339 QVERIFY(itemsInsertedSpy
.wait());
1340 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1342 QList
<QPair
<int, QVariant
> > expectedGroups
;
1343 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1344 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1345 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1346 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1347 QCOMPARE(m_model
->groups(), expectedGroups
);
1349 // Rename d.txt to a.txt.
1350 QHash
<QByteArray
, QVariant
> data
;
1351 data
.insert("text", "a.txt");
1352 m_model
->setData(2, data
);
1353 QVERIFY(itemsMovedSpy
.wait());
1354 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1356 expectedGroups
.clear();
1357 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1358 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1359 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1360 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1361 QCOMPARE(m_model
->groups(), expectedGroups
);
1363 // Rename c.txt to d.txt.
1364 data
.insert("text", "d.txt");
1365 m_model
->setData(2, data
);
1366 QVERIFY(groupsChangedSpy
.wait());
1367 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1369 expectedGroups
.clear();
1370 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1371 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1372 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1373 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1374 QCOMPARE(m_model
->groups(), expectedGroups
);
1376 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1377 const KFileItem fileItemD
= m_model
->fileItem(2);
1378 KFileItem fileItemC
= fileItemD
;
1379 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1380 urlC
.setPath(urlC
.path() + "c.txt");
1381 fileItemC
.setUrl(urlC
);
1383 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1384 QVERIFY(groupsChangedSpy
.wait());
1385 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1387 expectedGroups
.clear();
1388 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1389 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1390 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1391 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1392 QCOMPARE(m_model
->groups(), expectedGroups
);
1395 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1397 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1399 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1400 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1401 m_model
->setRoles(modelRoles
);
1403 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1405 m_model
->setGroupedSorting(true);
1406 m_model
->loadDirectory(m_testDir
->url());
1407 QVERIFY(itemsInsertedSpy
.wait());
1408 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1410 QList
<QPair
<int, QVariant
> > expectedGroups
;
1411 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1412 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1413 QCOMPARE(m_model
->groups(), expectedGroups
);
1415 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1416 expectedGroups
.clear();
1417 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1418 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1420 m_model
->setExpanded(0, true);
1421 QVERIFY(m_model
->isExpanded(0));
1422 QVERIFY(itemsInsertedSpy
.wait());
1423 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1424 QCOMPARE(m_model
->groups(), expectedGroups
);
1426 m_model
->setExpanded(3, true);
1427 QVERIFY(m_model
->isExpanded(3));
1428 QVERIFY(itemsInsertedSpy
.wait());
1429 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1430 QCOMPARE(m_model
->groups(), expectedGroups
);
1433 void KFileItemModelTest::testInconsistentModel()
1435 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1437 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1438 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1439 m_model
->setRoles(modelRoles
);
1441 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1443 m_model
->loadDirectory(m_testDir
->url());
1444 QVERIFY(itemsInsertedSpy
.wait());
1445 QCOMPARE(itemsInModel(), QStringList() << "a");
1447 // Expand "a/" and "a/b/".
1448 m_model
->setExpanded(0, true);
1449 QVERIFY(m_model
->isExpanded(0));
1450 QVERIFY(itemsInsertedSpy
.wait());
1451 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1453 m_model
->setExpanded(1, true);
1454 QVERIFY(m_model
->isExpanded(1));
1455 QVERIFY(itemsInsertedSpy
.wait());
1456 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1458 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1459 // Such a thing can in principle happen when performing a search, and there
1461 // (a) match the search string, and
1462 // (b) are children of a folder that matches the search string and is expanded.
1464 // Note that the first item in the list of added items must be new (i.e., not
1465 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1466 // it receives items that are in the model already and ignore them.
1467 QUrl
url(m_model
->directory().url() + "/a2");
1468 KFileItem
newItem(url
);
1470 KFileItemList items
;
1471 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1472 m_model
->slotItemsAdded(m_model
->directory(), items
);
1473 m_model
->slotCompleted();
1474 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1476 m_model
->setExpanded(0, false);
1478 // Test that the right items have been removed, see
1479 // https://bugs.kde.org/show_bug.cgi?id=324371
1480 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1482 // Test that resorting does not cause a crash, see
1483 // https://bugs.kde.org/show_bug.cgi?id=325359
1484 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1485 m_model
->resortAllItems();
1489 void KFileItemModelTest::testChangeRolesForFilteredItems()
1491 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1493 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1494 modelRoles
<< "owner";
1495 m_model
->setRoles(modelRoles
);
1497 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1499 m_model
->loadDirectory(m_testDir
->url());
1500 QVERIFY(itemsInsertedSpy
.wait());
1501 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1503 for (int index
= 0; index
< m_model
->count(); ++index
) {
1504 // All items should have the "text" and "owner" roles, but not "group".
1505 QVERIFY(m_model
->data(index
).contains("text"));
1506 QVERIFY(m_model
->data(index
).contains("owner"));
1507 QVERIFY(!m_model
->data(index
).contains("group"));
1510 // Add a filter, such that only "aaa.txt" remains in the model.
1511 m_model
->setNameFilter("aaa");
1512 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1514 // Add the "group" role.
1515 modelRoles
<< "group";
1516 m_model
->setRoles(modelRoles
);
1518 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1519 m_model
->setNameFilter("aa");
1520 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1522 for (int index
= 0; index
< m_model
->count(); ++index
) {
1523 // All items should have the "text", "owner", and "group" roles.
1524 QVERIFY(m_model
->data(index
).contains("text"));
1525 QVERIFY(m_model
->data(index
).contains("owner"));
1526 QVERIFY(m_model
->data(index
).contains("group"));
1529 // Remove the "owner" role.
1530 modelRoles
.remove("owner");
1531 m_model
->setRoles(modelRoles
);
1533 // Clear the filter, and verify that all items have the expected roles
1534 m_model
->setNameFilter(QString());
1535 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1537 for (int index
= 0; index
< m_model
->count(); ++index
) {
1538 // All items should have the "text" and "group" roles, but now "owner".
1539 QVERIFY(m_model
->data(index
).contains("text"));
1540 QVERIFY(!m_model
->data(index
).contains("owner"));
1541 QVERIFY(m_model
->data(index
).contains("group"));
1545 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1547 KFileItemList items
;
1549 KIO::UDSEntry entry
[3];
1551 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1552 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
1554 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1555 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
1557 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1558 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
1560 for (int i
= 0; i
< 3; ++i
) {
1561 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1562 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1563 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
1564 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1565 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
1566 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1567 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
1570 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1571 m_model
->slotCompleted();
1573 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1576 m_model
->setNameFilter("a");
1577 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1580 m_model
->setSortRole("owner");
1582 // Clear the filter, and verify that the items are sorted correctly.
1583 m_model
->setNameFilter(QString());
1584 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1587 void KFileItemModelTest::testRefreshFilteredItems()
1589 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1591 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1593 m_model
->loadDirectory(m_testDir
->url());
1594 QVERIFY(itemsInsertedSpy
.wait());
1595 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1597 const KFileItem fileItemC
= m_model
->fileItem(2);
1599 // Show only the .txt files.
1600 m_model
->setNameFilter(".txt");
1601 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1603 // Rename one of the .jpg files.
1604 KFileItem fileItemE
= fileItemC
;
1605 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1606 urlE
.setPath(urlE
.path() + "/e.jpg");
1607 fileItemE
.setUrl(urlE
);
1609 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1611 // Show all files again, and verify that the model has updated the file name.
1612 m_model
->setNameFilter(QString());
1613 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1616 void KFileItemModelTest::testCreateMimeData()
1618 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1620 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1621 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1622 m_model
->setRoles(modelRoles
);
1624 m_testDir
->createFile("a/1");
1626 m_model
->loadDirectory(m_testDir
->url());
1627 QVERIFY(itemsInsertedSpy
.wait());
1628 QCOMPARE(itemsInModel(), QStringList() << "a");
1631 m_model
->setExpanded(0, true);
1632 QVERIFY(itemsInsertedSpy
.wait());
1633 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1635 // Verify that creating the MIME data for a child of an expanded folder does
1636 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1638 selection
.insert(1);
1639 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1643 void KFileItemModelTest::testCollapseFolderWhileLoading()
1645 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1647 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1648 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1649 m_model
->setRoles(modelRoles
);
1651 m_testDir
->createFile("a2/b/c1.txt");
1653 m_model
->loadDirectory(m_testDir
->url());
1654 QVERIFY(itemsInsertedSpy
.wait());
1655 QCOMPARE(itemsInModel(), QStringList() << "a2");
1658 m_model
->setExpanded(0, true);
1659 QVERIFY(m_model
->isExpanded(0));
1660 QVERIFY(itemsInsertedSpy
.wait());
1661 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1664 m_model
->setExpanded(1, true);
1665 QVERIFY(m_model
->isExpanded(1));
1666 QVERIFY(itemsInsertedSpy
.wait());
1667 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1669 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1670 // signal is not emitted yet.
1671 const KFileItem fileItemC1
= m_model
->fileItem(2);
1672 KFileItem fileItemC2
= fileItemC1
;
1673 QUrl urlC2
= fileItemC2
.url();
1674 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1675 urlC2
.setPath(urlC2
.path() + "c2.txt");
1676 fileItemC2
.setUrl(urlC2
);
1678 const QUrl urlB
= m_model
->fileItem(1).url();
1679 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1680 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1682 // Collapse "a2/". This should also remove all its (indirect) children from
1683 // the model and from the model's m_pendingItemsToInsert member.
1684 m_model
->setExpanded(0, false);
1685 QCOMPARE(itemsInModel(), QStringList() << "a2");
1687 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1688 // is still in m_pendingItemsToInsert, then we might get a crash, see
1689 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1690 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1691 // without parent in the model.
1692 m_model
->slotCompleted();
1693 QCOMPARE(itemsInModel(), QStringList() << "a2");
1695 // Expand "a2/" again.
1696 m_model
->setExpanded(0, true);
1697 QVERIFY(m_model
->isExpanded(0));
1698 QVERIFY(itemsInsertedSpy
.wait());
1699 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1701 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1702 // completed() signal is not emitted yet.
1703 const KFileItem fileItemA2
= m_model
->fileItem(0);
1704 KFileItem fileItemA1
= fileItemA2
;
1705 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1706 urlA1
.setPath(urlA1
.path() + "a1");
1707 fileItemA1
.setUrl(urlA1
);
1709 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1710 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1712 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1713 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1714 // confuse the code which collapses the folder.
1715 m_model
->setExpanded(0, false);
1716 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1717 QVERIFY(!m_model
->isExpanded(0));
1718 QVERIFY(!m_model
->isExpanded(1));
1721 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1723 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1725 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1727 m_model
->loadDirectory(m_testDir
->url());
1728 QVERIFY(itemsInsertedSpy
.wait());
1729 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1731 const KFileItem fileItemB
= m_model
->fileItem(1);
1733 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1735 list
<< fileItemB
<< fileItemB
;
1736 m_model
->slotItemsDeleted(list
);
1738 QVERIFY(m_model
->isConsistent());
1739 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1742 QStringList
KFileItemModelTest::itemsInModel() const
1745 for (int i
= 0; i
< m_model
->count(); i
++) {
1746 items
<< m_model
->fileItem(i
).text();
1751 QTEST_MAIN(KFileItemModelTest
)
1753 #include "kfileitemmodeltest.moc"