1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
28 #include "kitemviews/kfileitemmodel.h"
29 #include "kitemviews/private/kfileitemmodeldirlister.h"
32 void myMessageOutput(QtMsgType type
, const QMessageLogContext
& context
, const QString
& msg
)
42 fprintf(stderr
, "Critical: %s\n", msg
.toLocal8Bit().data());
45 fprintf(stderr
, "Fatal: %s\n", msg
.toLocal8Bit().data());
52 Q_DECLARE_METATYPE(KItemRange
)
53 Q_DECLARE_METATYPE(KItemRangeList
)
54 Q_DECLARE_METATYPE(QList
<int>)
56 class KFileItemModelTest
: public QObject
64 void testDefaultRoles();
65 void testDefaultSortRole();
66 void testDefaultGroupedSorting();
68 void testRemoveItems();
69 void testDirLoadingCompleted();
71 void testSetDataWithModifiedSortRole_data();
72 void testSetDataWithModifiedSortRole();
73 void testChangeSortRole();
74 void testResortAfterChangingName();
75 void testModelConsistencyWhenInsertingItems();
76 void testItemRangeConsistencyWhenInsertingItems();
77 void testExpandItems();
78 void testExpandParentItems();
79 void testMakeExpandedItemHidden();
80 void testRemoveFilteredExpandedItems();
82 void testIndexForKeyboardSearch();
83 void testNameFilter();
85 void testRefreshExpandedItem();
86 void testRemoveHiddenItems();
87 void collapseParentOfHiddenItems();
88 void removeParentOfHiddenItems();
89 void testGeneralParentChildRelationships();
90 void testNameRoleGroups();
91 void testNameRoleGroupsWithExpandedItems();
92 void testInconsistentModel();
93 void testChangeRolesForFilteredItems();
94 void testChangeSortRoleWhileFiltering();
95 void testRefreshFilteredItems();
96 void testCollapseFolderWhileLoading();
97 void testCreateMimeData();
98 void testDeleteFileMoreThanOnce();
101 QStringList
itemsInModel() const;
104 KFileItemModel
* m_model
;
108 void KFileItemModelTest::init()
110 // The item-model tests result in a huge number of debugging
111 // output from kdelibs. Only show critical and fatal messages.
112 qInstallMessageHandler(myMessageOutput
);
114 qRegisterMetaType
<KItemRange
>("KItemRange");
115 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
116 qRegisterMetaType
<KFileItemList
>("KFileItemList");
118 m_testDir
= new TestDir();
119 m_model
= new KFileItemModel();
120 m_model
->m_dirLister
->setAutoUpdate(false);
122 // Reduce the timer interval to make the test run faster.
123 m_model
->m_resortAllItemsTimer
->setInterval(0);
126 void KFileItemModelTest::cleanup()
135 void KFileItemModelTest::testDefaultRoles()
137 const QSet
<QByteArray
> roles
= m_model
->roles();
138 QCOMPARE(roles
.count(), 4);
139 QVERIFY(roles
.contains("text"));
140 QVERIFY(roles
.contains("isDir"));
141 QVERIFY(roles
.contains("isLink"));
142 QVERIFY(roles
.contains("isHidden"));
145 void KFileItemModelTest::testDefaultSortRole()
147 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
148 QVERIFY(itemsInsertedSpy
.isValid());
150 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
152 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
154 m_model
->loadDirectory(m_testDir
->url());
155 QVERIFY(itemsInsertedSpy
.wait());
157 QCOMPARE(m_model
->count(), 3);
158 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
159 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
160 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
163 void KFileItemModelTest::testDefaultGroupedSorting()
165 QCOMPARE(m_model
->groupedSorting(), false);
168 void KFileItemModelTest::testNewItems()
170 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
172 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
174 m_model
->loadDirectory(m_testDir
->url());
175 QVERIFY(itemsInsertedSpy
.wait());
177 QCOMPARE(m_model
->count(), 3);
179 QVERIFY(m_model
->isConsistent());
182 void KFileItemModelTest::testRemoveItems()
184 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
185 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
187 m_testDir
->createFiles({"a.txt", "b.txt"});
188 m_model
->loadDirectory(m_testDir
->url());
189 QVERIFY(itemsInsertedSpy
.wait());
190 QCOMPARE(m_model
->count(), 2);
191 QVERIFY(m_model
->isConsistent());
193 m_testDir
->removeFile("a.txt");
194 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
195 QVERIFY(itemsRemovedSpy
.wait());
196 QCOMPARE(m_model
->count(), 1);
197 QVERIFY(m_model
->isConsistent());
200 void KFileItemModelTest::testDirLoadingCompleted()
202 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
203 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
204 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
206 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
208 m_model
->loadDirectory(m_testDir
->url());
209 QVERIFY(loadingCompletedSpy
.wait());
210 QCOMPARE(loadingCompletedSpy
.count(), 1);
211 QCOMPARE(itemsInsertedSpy
.count(), 1);
212 QCOMPARE(itemsRemovedSpy
.count(), 0);
213 QCOMPARE(m_model
->count(), 3);
215 m_testDir
->createFiles({"d.txt", "e.txt"});
216 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
217 QVERIFY(loadingCompletedSpy
.wait());
218 QCOMPARE(loadingCompletedSpy
.count(), 2);
219 QCOMPARE(itemsInsertedSpy
.count(), 2);
220 QCOMPARE(itemsRemovedSpy
.count(), 0);
221 QCOMPARE(m_model
->count(), 5);
223 m_testDir
->removeFile("a.txt");
224 m_testDir
->createFile("f.txt");
225 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
226 QVERIFY(loadingCompletedSpy
.wait());
227 QCOMPARE(loadingCompletedSpy
.count(), 3);
228 QCOMPARE(itemsInsertedSpy
.count(), 3);
229 QCOMPARE(itemsRemovedSpy
.count(), 1);
230 QCOMPARE(m_model
->count(), 5);
232 m_testDir
->removeFile("b.txt");
233 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
234 QVERIFY(itemsRemovedSpy
.wait());
235 QCOMPARE(loadingCompletedSpy
.count(), 4);
236 QCOMPARE(itemsInsertedSpy
.count(), 3);
237 QCOMPARE(itemsRemovedSpy
.count(), 2);
238 QCOMPARE(m_model
->count(), 4);
240 QVERIFY(m_model
->isConsistent());
243 void KFileItemModelTest::testSetData()
245 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
246 QVERIFY(itemsInsertedSpy
.isValid());
247 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
248 QVERIFY(itemsChangedSpy
.isValid());
250 m_testDir
->createFile("a.txt");
252 m_model
->loadDirectory(m_testDir
->url());
253 QVERIFY(itemsInsertedSpy
.wait());
255 QHash
<QByteArray
, QVariant
> values
;
256 values
.insert("customRole1", "Test1");
257 values
.insert("customRole2", "Test2");
259 m_model
->setData(0, values
);
260 QCOMPARE(itemsChangedSpy
.count(), 1);
262 values
= m_model
->data(0);
263 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
264 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
265 QVERIFY(m_model
->isConsistent());
268 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
270 QTest::addColumn
<int>("changedIndex");
271 QTest::addColumn
<int>("changedRating");
272 QTest::addColumn
<bool>("expectMoveSignal");
273 QTest::addColumn
<int>("ratingIndex0");
274 QTest::addColumn
<int>("ratingIndex1");
275 QTest::addColumn
<int>("ratingIndex2");
278 // Index 0 = rating 2
279 // Index 1 = rating 4
280 // Index 2 = rating 6
282 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
283 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
284 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
286 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
287 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
288 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
291 void KFileItemModelTest::testSetDataWithModifiedSortRole()
293 QFETCH(int, changedIndex
);
294 QFETCH(int, changedRating
);
295 QFETCH(bool, expectMoveSignal
);
296 QFETCH(int, ratingIndex0
);
297 QFETCH(int, ratingIndex1
);
298 QFETCH(int, ratingIndex2
);
300 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
301 QVERIFY(itemsInsertedSpy
.isValid());
302 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
303 QVERIFY(itemsMovedSpy
.isValid());
305 // Changing the value of a sort-role must result in
306 // a reordering of the items.
307 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
308 m_model
->setSortRole("rating");
309 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
311 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
313 m_model
->loadDirectory(m_testDir
->url());
314 QVERIFY(itemsInsertedSpy
.wait());
316 // Fill the "rating" role of each file:
321 QHash
<QByteArray
, QVariant
> ratingA
;
322 ratingA
.insert("rating", 2);
323 m_model
->setData(0, ratingA
);
325 QHash
<QByteArray
, QVariant
> ratingB
;
326 ratingB
.insert("rating", 4);
327 m_model
->setData(1, ratingB
);
329 QHash
<QByteArray
, QVariant
> ratingC
;
330 ratingC
.insert("rating", 6);
331 m_model
->setData(2, ratingC
);
333 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
334 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
335 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
337 // Now change the rating from a.txt. This usually results
338 // in reordering of the items.
339 QHash
<QByteArray
, QVariant
> rating
;
340 rating
.insert("rating", changedRating
);
341 m_model
->setData(changedIndex
, rating
);
343 if (expectMoveSignal
) {
344 QVERIFY(itemsMovedSpy
.wait());
347 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
348 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
349 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
350 QVERIFY(m_model
->isConsistent());
353 void KFileItemModelTest::testChangeSortRole()
355 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
356 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
357 QVERIFY(itemsMovedSpy
.isValid());
359 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
361 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
363 m_model
->loadDirectory(m_testDir
->url());
364 QVERIFY(itemsInsertedSpy
.wait());
365 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
367 // Simulate that KFileItemModelRolesUpdater determines the mime type.
368 // Resorting the files by 'type' will only work immediately if their
369 // mime types are known.
370 for (int index
= 0; index
< m_model
->count(); ++index
) {
371 m_model
->fileItem(index
).determineMimeType();
374 // Now: sort by type.
375 m_model
->setSortRole("type");
376 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
377 QVERIFY(!itemsMovedSpy
.isEmpty());
379 // The actual order of the files might depend on the translation of the
380 // result of KFileItem::mimeComment() in the user's language.
381 QStringList version1
;
382 version1
<< "b.jpg" << "a.txt" << "c.txt";
384 QStringList version2
;
385 version2
<< "a.txt" << "c.txt" << "b.jpg";
387 const bool ok1
= (itemsInModel() == version1
);
388 const bool ok2
= (itemsInModel() == version2
);
393 void KFileItemModelTest::testResortAfterChangingName()
395 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
396 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
397 QVERIFY(itemsMovedSpy
.isValid());
399 // We sort by size in a directory where all files have the same size.
400 // Therefore, the files are sorted by their names.
401 m_model
->setSortRole("size");
403 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
405 m_model
->loadDirectory(m_testDir
->url());
406 QVERIFY(itemsInsertedSpy
.wait());
407 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
409 // We rename a.txt to d.txt. Even though the size has not changed at all,
410 // the model must re-sort the items.
411 QHash
<QByteArray
, QVariant
> data
;
412 data
.insert("text", "d.txt");
413 m_model
->setData(0, data
);
415 QVERIFY(itemsMovedSpy
.wait());
416 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
418 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
419 const KFileItem fileItemD
= m_model
->fileItem(2);
420 KFileItem fileItemA
= fileItemD
;
421 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
422 urlA
.setPath(urlA
.path() + "a.txt");
423 fileItemA
.setUrl(urlA
);
425 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
427 QVERIFY(itemsMovedSpy
.wait());
428 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
431 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
433 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
435 // KFileItemModel prevents that inserting a punch of items sequentially
436 // results in an itemsInserted()-signal for each item. Instead internally
437 // a timeout is given that collects such operations and results in only
438 // one itemsInserted()-signal. However in this test we want to stress
439 // KFileItemModel to do a lot of insert operation and hence decrease
440 // the timeout to 1 millisecond.
441 m_testDir
->createFile("1");
442 m_model
->loadDirectory(m_testDir
->url());
443 QVERIFY(itemsInsertedSpy
.wait());
444 QCOMPARE(m_model
->count(), 1);
446 // Insert 10 items for 20 times. After each insert operation the model consistency
448 QSet
<int> insertedItems
;
449 for (int i
= 0; i
< 20; ++i
) {
450 itemsInsertedSpy
.clear();
452 for (int j
= 0; j
< 10; ++j
) {
453 int itemName
= qrand();
454 while (insertedItems
.contains(itemName
)) {
457 insertedItems
.insert(itemName
);
459 m_testDir
->createFile(QString::number(itemName
));
462 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
463 if (itemsInsertedSpy
.isEmpty()) {
464 QVERIFY(itemsInsertedSpy
.wait());
467 QVERIFY(m_model
->isConsistent());
470 QCOMPARE(m_model
->count(), 201);
473 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
475 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
477 m_testDir
->createFiles({"B", "E", "G"});
479 // Due to inserting the 3 items one item-range with index == 0 and
480 // count == 3 must be given
481 m_model
->loadDirectory(m_testDir
->url());
482 QVERIFY(itemsInsertedSpy
.wait());
484 QCOMPARE(itemsInsertedSpy
.count(), 1);
485 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
486 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
487 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
489 // The indexes of the item-ranges must always be related to the model before
490 // the items have been inserted. Having:
493 // and inserting A, C, D, F the resulting model will be:
496 // and the item-ranges must be:
497 // index: 0, count: 1 for A
498 // index: 1, count: 2 for B, C
499 // index: 2, count: 1 for G
501 m_testDir
->createFiles({"A", "C", "D", "F"});
503 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
504 QVERIFY(itemsInsertedSpy
.wait());
506 QCOMPARE(itemsInsertedSpy
.count(), 1);
507 arguments
= itemsInsertedSpy
.takeFirst();
508 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
509 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
512 void KFileItemModelTest::testExpandItems()
514 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
515 QVERIFY(itemsInsertedSpy
.isValid());
516 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
517 QVERIFY(itemsRemovedSpy
.isValid());
518 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
519 QVERIFY(loadingCompletedSpy
.isValid());
521 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
522 // Besides testing the basic item expansion functionality, the test makes sure that
523 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
524 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
525 // first three characters.
526 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
527 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
528 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
529 m_model
->setRoles(modelRoles
);
531 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
533 // Store the URLs of all folders in a set.
534 QSet
<QUrl
> allFolders
;
535 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a")
536 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
537 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
539 m_model
->loadDirectory(m_testDir
->url());
540 QVERIFY(itemsInsertedSpy
.wait());
542 // So far, the model contains only "a/"
543 QCOMPARE(m_model
->count(), 1);
544 QVERIFY(m_model
->isExpandable(0));
545 QVERIFY(!m_model
->isExpanded(0));
546 QVERIFY(m_model
->expandedDirectories().empty());
548 QCOMPARE(itemsInsertedSpy
.count(), 1);
549 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
550 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
552 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
553 m_model
->setExpanded(0, true);
554 QVERIFY(m_model
->isExpanded(0));
555 QVERIFY(itemsInsertedSpy
.wait());
556 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
557 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
559 QCOMPARE(itemsInsertedSpy
.count(), 1);
560 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
561 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
563 QVERIFY(m_model
->isExpandable(1));
564 QVERIFY(!m_model
->isExpanded(1));
565 QVERIFY(m_model
->isExpandable(2));
566 QVERIFY(!m_model
->isExpanded(2));
568 // Expand the folder "a/a/" -> "a/a/1" becomes visible
569 m_model
->setExpanded(1, true);
570 QVERIFY(m_model
->isExpanded(1));
571 QVERIFY(itemsInsertedSpy
.wait());
572 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
573 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a")
574 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
576 QCOMPARE(itemsInsertedSpy
.count(), 1);
577 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
578 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
580 QVERIFY(!m_model
->isExpandable(2));
581 QVERIFY(!m_model
->isExpanded(2));
583 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
584 m_model
->setExpanded(3, true);
585 QVERIFY(m_model
->isExpanded(3));
586 QVERIFY(itemsInsertedSpy
.wait());
587 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
588 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
590 QCOMPARE(itemsInsertedSpy
.count(), 1);
591 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
592 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
594 QVERIFY(!m_model
->isExpandable(4));
595 QVERIFY(!m_model
->isExpanded(4));
597 // Collapse the top-level folder -> all other items should disappear
598 m_model
->setExpanded(0, false);
599 QVERIFY(!m_model
->isExpanded(0));
600 QCOMPARE(m_model
->count(), 1);
601 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
603 QCOMPARE(itemsRemovedSpy
.count(), 1);
604 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
605 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
606 QVERIFY(m_model
->isConsistent());
608 // Clear the model, reload the folder and try to restore the expanded folders.
610 QCOMPARE(m_model
->count(), 0);
611 QVERIFY(m_model
->expandedDirectories().empty());
613 m_model
->loadDirectory(m_testDir
->url());
614 m_model
->restoreExpandedDirectories(allFolders
);
615 QVERIFY(loadingCompletedSpy
.wait());
616 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
617 QVERIFY(m_model
->isExpanded(0));
618 QVERIFY(m_model
->isExpanded(1));
619 QVERIFY(!m_model
->isExpanded(2));
620 QVERIFY(m_model
->isExpanded(3));
621 QVERIFY(!m_model
->isExpanded(4));
622 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
623 QVERIFY(m_model
->isConsistent());
625 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
626 // This is how DolphinView restores the expanded folders when navigating in history.
627 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
628 QVERIFY(loadingCompletedSpy
.wait());
629 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
630 m_model
->restoreExpandedDirectories(allFolders
);
631 m_model
->loadDirectory(m_testDir
->url());
632 QVERIFY(loadingCompletedSpy
.wait());
633 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
634 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
636 // Remove all expanded items by changing the roles
637 itemsRemovedSpy
.clear();
638 m_model
->setRoles(originalModelRoles
);
639 QVERIFY(!m_model
->isExpanded(0));
640 QCOMPARE(m_model
->count(), 1);
641 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
643 QCOMPARE(itemsRemovedSpy
.count(), 1);
644 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
645 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
646 QVERIFY(m_model
->isConsistent());
649 void KFileItemModelTest::testExpandParentItems()
651 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
652 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
653 QVERIFY(loadingCompletedSpy
.isValid());
655 // Create a tree structure of folders:
663 QSet
<QByteArray
> modelRoles
= m_model
->roles();
664 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
665 m_model
->setRoles(modelRoles
);
667 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
669 m_model
->loadDirectory(m_testDir
->url());
670 QVERIFY(itemsInsertedSpy
.wait());
672 // So far, the model contains only "a 1/" and "a2/".
673 QCOMPARE(m_model
->count(), 2);
674 QVERIFY(m_model
->expandedDirectories().empty());
676 // Expand the parents of "a2/b2/c2".
677 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
678 QVERIFY(loadingCompletedSpy
.wait());
680 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
681 // It's important that only the parents of "a1/b1/c1" are expanded.
682 QCOMPARE(m_model
->count(), 4);
683 QVERIFY(!m_model
->isExpanded(0));
684 QVERIFY(m_model
->isExpanded(1));
685 QVERIFY(m_model
->isExpanded(2));
686 QVERIFY(!m_model
->isExpanded(3));
688 // Expand the parents of "a 1/b1".
689 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
690 QVERIFY(loadingCompletedSpy
.wait());
692 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
693 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
694 QCOMPARE(m_model
->count(), 5);
695 QVERIFY(m_model
->isExpanded(0));
696 QVERIFY(!m_model
->isExpanded(1));
697 QVERIFY(m_model
->isExpanded(2));
698 QVERIFY(m_model
->isExpanded(3));
699 QVERIFY(!m_model
->isExpanded(4));
700 QVERIFY(m_model
->isConsistent());
703 m_model
->setExpanded(1, true);
704 QVERIFY(loadingCompletedSpy
.wait());
705 QCOMPARE(m_model
->count(), 6);
706 QVERIFY(m_model
->isExpanded(0));
707 QVERIFY(m_model
->isExpanded(1));
708 QVERIFY(!m_model
->isExpanded(2));
709 QVERIFY(m_model
->isExpanded(3));
710 QVERIFY(m_model
->isExpanded(4));
711 QVERIFY(!m_model
->isExpanded(5));
712 QVERIFY(m_model
->isConsistent());
714 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
715 m_model
->setExpanded(1, false);
716 QCOMPARE(m_model
->count(), 5);
717 QVERIFY(m_model
->isExpanded(0));
718 QVERIFY(!m_model
->isExpanded(1));
719 QVERIFY(m_model
->isExpanded(2));
720 QVERIFY(m_model
->isExpanded(3));
721 QVERIFY(!m_model
->isExpanded(4));
722 QVERIFY(m_model
->isConsistent());
726 * Renaming an expanded folder by prepending its name with a dot makes it
727 * hidden. Verify that this does not cause an inconsistent model state and
728 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
730 void KFileItemModelTest::testMakeExpandedItemHidden()
732 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
733 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
735 QSet
<QByteArray
> modelRoles
= m_model
->roles();
736 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
737 m_model
->setRoles(modelRoles
);
739 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
741 m_model
->loadDirectory(m_testDir
->url());
742 QVERIFY(itemsInsertedSpy
.wait());
744 // So far, the model contains only "1a/" and "1b".
745 QCOMPARE(m_model
->count(), 2);
746 m_model
->setExpanded(0, true);
747 QVERIFY(itemsInsertedSpy
.wait());
749 // Now "1a/2a" and "1a/2b" have appeared.
750 QCOMPARE(m_model
->count(), 4);
751 m_model
->setExpanded(1, true);
752 QVERIFY(itemsInsertedSpy
.wait());
753 QCOMPARE(m_model
->count(), 6);
755 // Rename "1a/2" and make it hidden.
756 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
757 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
759 KIO::SimpleJob
* job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
760 bool ok
= job
->exec();
762 QVERIFY(itemsRemovedSpy
.wait());
764 // "1a/2" and its subfolders have disappeared now.
765 QVERIFY(m_model
->isConsistent());
766 QCOMPARE(m_model
->count(), 3);
768 m_model
->setExpanded(0, false);
769 QCOMPARE(m_model
->count(), 2);
773 void KFileItemModelTest::testRemoveFilteredExpandedItems()
775 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
777 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
778 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
779 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
780 m_model
->setRoles(modelRoles
);
782 m_testDir
->createFiles({"folder/child", "file"});
784 m_model
->loadDirectory(m_testDir
->url());
785 QVERIFY(itemsInsertedSpy
.wait());
787 // So far, the model contains only "folder/" and "file".
788 QCOMPARE(m_model
->count(), 2);
789 QVERIFY(m_model
->isExpandable(0));
790 QVERIFY(!m_model
->isExpandable(1));
791 QVERIFY(!m_model
->isExpanded(0));
792 QVERIFY(!m_model
->isExpanded(1));
793 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
795 // Expand "folder" -> "folder/child" becomes visible.
796 m_model
->setExpanded(0, true);
797 QVERIFY(m_model
->isExpanded(0));
798 QVERIFY(itemsInsertedSpy
.wait());
799 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
801 // Add a name filter.
802 m_model
->setNameFilter("f");
803 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
805 m_model
->setNameFilter("fo");
806 QCOMPARE(itemsInModel(), QStringList() << "folder");
808 // Remove all expanded items by changing the roles
809 m_model
->setRoles(originalModelRoles
);
810 QVERIFY(!m_model
->isExpanded(0));
811 QCOMPARE(itemsInModel(), QStringList() << "folder");
813 // Remove the name filter and verify that "folder/child" does not reappear.
814 m_model
->setNameFilter(QString());
815 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
818 void KFileItemModelTest::testSorting()
820 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
821 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
822 QVERIFY(itemsMovedSpy
.isValid());
824 // Create some files with different sizes and modification times to check the different sorting options
825 QDateTime now
= QDateTime::currentDateTime();
827 QSet
<QByteArray
> roles
;
828 roles
.insert("text");
829 roles
.insert("isExpanded");
830 roles
.insert("isExpandable");
831 roles
.insert("expandedParentsCount");
832 m_model
->setRoles(roles
);
834 m_testDir
->createDir("c/c-2");
835 m_testDir
->createFile("c/c-2/c-3");
836 m_testDir
->createFile("c/c-1");
838 m_testDir
->createFile("a", "A file", now
.addDays(-3));
839 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
840 m_testDir
->createDir("c", now
.addDays(-2));
841 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
842 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
843 m_testDir
->createFile(".f");
845 m_model
->loadDirectory(m_testDir
->url());
846 QVERIFY(itemsInsertedSpy
.wait());
848 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
849 m_model
->setExpanded(index
, true);
850 QVERIFY(itemsInsertedSpy
.wait());
852 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
853 m_model
->setExpanded(index
, true);
854 QVERIFY(itemsInsertedSpy
.wait());
856 // Default: Sort by Name, ascending
857 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
858 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
859 QVERIFY(m_model
->sortDirectoriesFirst());
860 QVERIFY(!m_model
->showHiddenFiles());
861 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
863 // Sort by Name, ascending, 'Sort Folders First' disabled
864 m_model
->setSortDirectoriesFirst(false);
865 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
866 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
867 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
868 QCOMPARE(itemsMovedSpy
.count(), 1);
869 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
870 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
872 // Sort by Name, descending
873 m_model
->setSortDirectoriesFirst(true);
874 m_model
->setSortOrder(Qt::DescendingOrder
);
875 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
876 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
877 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
878 QCOMPARE(itemsMovedSpy
.count(), 2);
879 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
880 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
881 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
882 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
884 // Sort by Date, descending
885 m_model
->setSortDirectoriesFirst(true);
886 m_model
->setSortRole("modificationtime");
887 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
888 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
889 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
890 QCOMPARE(itemsMovedSpy
.count(), 1);
891 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
892 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
894 // Sort by Date, ascending
895 m_model
->setSortOrder(Qt::AscendingOrder
);
896 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
897 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
898 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
899 QCOMPARE(itemsMovedSpy
.count(), 1);
900 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
901 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
903 // Sort by Date, ascending, 'Sort Folders First' disabled
904 m_model
->setSortDirectoriesFirst(false);
905 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
906 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
907 QVERIFY(!m_model
->sortDirectoriesFirst());
908 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
909 QCOMPARE(itemsMovedSpy
.count(), 1);
910 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
911 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
913 // Sort by Name, ascending, 'Sort Folders First' disabled
914 m_model
->setSortRole("text");
915 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
916 QVERIFY(!m_model
->sortDirectoriesFirst());
917 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
918 QCOMPARE(itemsMovedSpy
.count(), 1);
919 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
920 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
922 // Sort by Size, ascending, 'Sort Folders First' disabled
923 m_model
->setSortRole("size");
924 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
925 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
926 QVERIFY(!m_model
->sortDirectoriesFirst());
927 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
928 QCOMPARE(itemsMovedSpy
.count(), 1);
929 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
930 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
932 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
933 m_model
->setSortDirectoriesFirst(true);
934 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
935 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
936 QVERIFY(m_model
->sortDirectoriesFirst());
937 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
938 QCOMPARE(itemsMovedSpy
.count(), 0);
940 // Sort by Size, descending, 'Sort Folders First' enabled
941 m_model
->setSortOrder(Qt::DescendingOrder
);
942 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
943 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
944 QVERIFY(m_model
->sortDirectoriesFirst());
945 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
946 QCOMPARE(itemsMovedSpy
.count(), 1);
947 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
948 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
950 // TODO: Sort by other roles; show/hide hidden files
953 void KFileItemModelTest::testIndexForKeyboardSearch()
955 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
957 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
959 m_model
->loadDirectory(m_testDir
->url());
960 QVERIFY(itemsInsertedSpy
.wait());
962 // Search from index 0
963 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
964 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
965 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
966 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
967 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
968 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
969 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
970 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
971 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
972 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
973 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
975 // Start a search somewhere in the middle
976 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
977 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
978 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
979 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
981 // Test searches that go past the last item back to index 0
982 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
983 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
984 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
985 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
987 // Test searches that yield no result
988 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
989 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
990 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
991 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
992 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
994 // Test upper case searches (note that search is case insensitive)
995 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
996 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
997 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
998 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1000 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1003 void KFileItemModelTest::testNameFilter()
1005 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1007 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1009 m_model
->loadDirectory(m_testDir
->url());
1010 QVERIFY(itemsInsertedSpy
.wait());
1012 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1013 QCOMPARE(m_model
->count(), 3);
1015 m_model
->setNameFilter("A2"); // Shows only A2
1016 QCOMPARE(m_model
->count(), 1);
1018 m_model
->setNameFilter("A2"); // Shows only A1
1019 QCOMPARE(m_model
->count(), 1);
1021 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1022 QCOMPARE(m_model
->count(), 2);
1024 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1025 QCOMPARE(m_model
->count(), 2);
1027 m_model
->setNameFilter(QString()); // Shows again all items
1028 QCOMPARE(m_model
->count(), 5);
1032 * Verifies that we do not crash when adding a KFileItem with an empty path.
1033 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1034 * tried to always read the first character of the path, even if the path is empty.
1036 void KFileItemModelTest::testEmptyPath()
1038 QSet
<QByteArray
> roles
;
1039 roles
.insert("text");
1040 roles
.insert("isExpanded");
1041 roles
.insert("isExpandable");
1042 roles
.insert("expandedParentsCount");
1043 m_model
->setRoles(roles
);
1045 const QUrl emptyUrl
;
1046 QVERIFY(emptyUrl
.path().isEmpty());
1048 const QUrl
url("file:///test/");
1050 KFileItemList items
;
1051 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1052 m_model
->slotItemsAdded(emptyUrl
, items
);
1053 m_model
->slotCompleted();
1057 * Verifies that the 'isExpanded' state of folders does not change when the
1058 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1060 void KFileItemModelTest::testRefreshExpandedItem()
1062 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1063 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1064 QVERIFY(itemsChangedSpy
.isValid());
1066 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1067 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1068 m_model
->setRoles(modelRoles
);
1070 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1072 m_model
->loadDirectory(m_testDir
->url());
1073 QVERIFY(itemsInsertedSpy
.wait());
1074 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1076 m_model
->setExpanded(0, true);
1077 QVERIFY(itemsInsertedSpy
.wait());
1078 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1079 QVERIFY(m_model
->isExpanded(0));
1081 const KFileItem item
= m_model
->fileItem(0);
1082 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1083 QVERIFY(!itemsChangedSpy
.isEmpty());
1085 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1086 QVERIFY(m_model
->isExpanded(0));
1090 * Verify that removing hidden files and folders from the model does not
1091 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1093 void KFileItemModelTest::testRemoveHiddenItems()
1095 m_testDir
->createDir(".a");
1096 m_testDir
->createDir(".b");
1097 m_testDir
->createDir("c");
1098 m_testDir
->createDir("d");
1099 m_testDir
->createFiles({".f", ".g", "h", "i"});
1101 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1102 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1104 m_model
->setShowHiddenFiles(true);
1105 m_model
->loadDirectory(m_testDir
->url());
1106 QVERIFY(itemsInsertedSpy
.wait());
1107 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1108 QCOMPARE(itemsInsertedSpy
.count(), 1);
1109 QCOMPARE(itemsRemovedSpy
.count(), 0);
1110 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1111 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1113 m_model
->setShowHiddenFiles(false);
1114 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1115 QCOMPARE(itemsInsertedSpy
.count(), 0);
1116 QCOMPARE(itemsRemovedSpy
.count(), 1);
1117 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1118 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1120 m_model
->setShowHiddenFiles(true);
1121 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1122 QCOMPARE(itemsInsertedSpy
.count(), 1);
1123 QCOMPARE(itemsRemovedSpy
.count(), 0);
1124 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1125 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1128 QCOMPARE(itemsInModel(), QStringList());
1129 QCOMPARE(itemsInsertedSpy
.count(), 0);
1130 QCOMPARE(itemsRemovedSpy
.count(), 1);
1131 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1132 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1134 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1135 // Verify that this does not make the model crash.
1136 m_model
->setShowHiddenFiles(false);
1140 * Verify that filtered items are removed when their parent is collapsed.
1142 void KFileItemModelTest::collapseParentOfHiddenItems()
1144 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1145 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1147 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1148 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1149 m_model
->setRoles(modelRoles
);
1151 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1153 m_model
->loadDirectory(m_testDir
->url());
1154 QVERIFY(itemsInsertedSpy
.wait());
1155 QCOMPARE(m_model
->count(), 1); // Only "a/"
1158 m_model
->setExpanded(0, true);
1159 QVERIFY(itemsInsertedSpy
.wait());
1160 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1163 m_model
->setExpanded(1, true);
1164 QVERIFY(itemsInsertedSpy
.wait());
1165 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1168 m_model
->setExpanded(2, true);
1169 QVERIFY(itemsInsertedSpy
.wait());
1170 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"
1172 // Set a name filter that matches nothing -> only the expanded folders remain.
1173 m_model
->setNameFilter("xyz");
1174 QCOMPARE(itemsRemovedSpy
.count(), 1);
1175 QCOMPARE(m_model
->count(), 3);
1176 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1178 // Collapse the folder "a/".
1179 m_model
->setExpanded(0, false);
1180 QCOMPARE(itemsRemovedSpy
.count(), 2);
1181 QCOMPARE(m_model
->count(), 1);
1182 QCOMPARE(itemsInModel(), QStringList() << "a");
1184 // Remove the filter -> no files should appear (and we should not get a crash).
1185 m_model
->setNameFilter(QString());
1186 QCOMPARE(m_model
->count(), 1);
1190 * Verify that filtered items are removed when their parent is deleted.
1192 void KFileItemModelTest::removeParentOfHiddenItems()
1194 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1195 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1197 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1198 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1199 m_model
->setRoles(modelRoles
);
1201 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1203 m_model
->loadDirectory(m_testDir
->url());
1204 QVERIFY(itemsInsertedSpy
.wait());
1205 QCOMPARE(m_model
->count(), 1); // Only "a/"
1208 m_model
->setExpanded(0, true);
1209 QVERIFY(itemsInsertedSpy
.wait());
1210 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1213 m_model
->setExpanded(1, true);
1214 QVERIFY(itemsInsertedSpy
.wait());
1215 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1218 m_model
->setExpanded(2, true);
1219 QVERIFY(itemsInsertedSpy
.wait());
1220 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"
1222 // Set a name filter that matches nothing -> only the expanded folders remain.
1223 m_model
->setNameFilter("xyz");
1224 QCOMPARE(itemsRemovedSpy
.count(), 1);
1225 QCOMPARE(m_model
->count(), 3);
1226 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1228 // Simulate the deletion of the directory "a/b/".
1229 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1230 QCOMPARE(itemsRemovedSpy
.count(), 2);
1231 QCOMPARE(m_model
->count(), 1);
1232 QCOMPARE(itemsInModel(), QStringList() << "a");
1234 // Remove the filter -> only the file "a/1" should appear.
1235 m_model
->setNameFilter(QString());
1236 QCOMPARE(m_model
->count(), 2);
1237 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1241 * Create a tree structure where parent-child relationships can not be
1242 * determined by parsing the URLs, and verify that KFileItemModel
1243 * handles them correctly.
1245 void KFileItemModelTest::testGeneralParentChildRelationships()
1247 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1248 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1250 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1251 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1252 m_model
->setRoles(modelRoles
);
1254 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1256 m_model
->loadDirectory(m_testDir
->url());
1257 QVERIFY(itemsInsertedSpy
.wait());
1258 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1260 // Expand all folders.
1261 m_model
->setExpanded(0, true);
1262 QVERIFY(itemsInsertedSpy
.wait());
1263 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1265 m_model
->setExpanded(1, true);
1266 QVERIFY(itemsInsertedSpy
.wait());
1267 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1269 m_model
->setExpanded(3, true);
1270 QVERIFY(itemsInsertedSpy
.wait());
1271 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1273 m_model
->setExpanded(4, true);
1274 QVERIFY(itemsInsertedSpy
.wait());
1275 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1277 // Add some more children and grand-children.
1278 const QUrl parent1
= m_model
->fileItem(0).url();
1279 const QUrl parent2
= m_model
->fileItem(3).url();
1280 const QUrl realChild1
= m_model
->fileItem(1).url();
1281 const QUrl realChild2
= m_model
->fileItem(4).url();
1283 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1284 m_model
->slotCompleted();
1285 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1287 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1288 m_model
->slotCompleted();
1289 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1291 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1292 m_model
->slotCompleted();
1293 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1295 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1296 m_model
->slotCompleted();
1297 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1299 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1300 m_model
->slotCompleted();
1301 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1303 // Set a name filter that matches nothing -> only expanded folders remain.
1304 m_model
->setNameFilter("xyz");
1305 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1306 QCOMPARE(itemsRemovedSpy
.count(), 1);
1307 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1308 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1309 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1311 // Collapse "parent1".
1312 m_model
->setExpanded(0, false);
1313 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1314 QCOMPARE(itemsRemovedSpy
.count(), 1);
1315 arguments
= itemsRemovedSpy
.takeFirst();
1316 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1317 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1319 // Remove "parent2".
1320 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1321 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1322 QCOMPARE(itemsRemovedSpy
.count(), 1);
1323 arguments
= itemsRemovedSpy
.takeFirst();
1324 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1325 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1327 // Clear filter, verify that no items reappear.
1328 m_model
->setNameFilter(QString());
1329 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1332 void KFileItemModelTest::testNameRoleGroups()
1334 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1335 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1336 QVERIFY(itemsMovedSpy
.isValid());
1337 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1338 QVERIFY(groupsChangedSpy
.isValid());
1340 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1342 m_model
->setGroupedSorting(true);
1343 m_model
->loadDirectory(m_testDir
->url());
1344 QVERIFY(itemsInsertedSpy
.wait());
1345 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1347 QList
<QPair
<int, QVariant
> > expectedGroups
;
1348 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1349 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1350 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1351 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1352 QCOMPARE(m_model
->groups(), expectedGroups
);
1354 // Rename d.txt to a.txt.
1355 QHash
<QByteArray
, QVariant
> data
;
1356 data
.insert("text", "a.txt");
1357 m_model
->setData(2, data
);
1358 QVERIFY(itemsMovedSpy
.wait());
1359 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1361 expectedGroups
.clear();
1362 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1363 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1364 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1365 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1366 QCOMPARE(m_model
->groups(), expectedGroups
);
1368 // Rename c.txt to d.txt.
1369 data
.insert("text", "d.txt");
1370 m_model
->setData(2, data
);
1371 QVERIFY(groupsChangedSpy
.wait());
1372 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1374 expectedGroups
.clear();
1375 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1376 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1377 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1378 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1379 QCOMPARE(m_model
->groups(), expectedGroups
);
1381 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1382 const KFileItem fileItemD
= m_model
->fileItem(2);
1383 KFileItem fileItemC
= fileItemD
;
1384 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1385 urlC
.setPath(urlC
.path() + "c.txt");
1386 fileItemC
.setUrl(urlC
);
1388 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1389 QVERIFY(groupsChangedSpy
.wait());
1390 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1392 expectedGroups
.clear();
1393 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1394 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1395 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1396 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1397 QCOMPARE(m_model
->groups(), expectedGroups
);
1400 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1402 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1404 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1405 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1406 m_model
->setRoles(modelRoles
);
1408 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1410 m_model
->setGroupedSorting(true);
1411 m_model
->loadDirectory(m_testDir
->url());
1412 QVERIFY(itemsInsertedSpy
.wait());
1413 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1415 QList
<QPair
<int, QVariant
> > expectedGroups
;
1416 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1417 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1418 QCOMPARE(m_model
->groups(), expectedGroups
);
1420 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1421 expectedGroups
.clear();
1422 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1423 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1425 m_model
->setExpanded(0, true);
1426 QVERIFY(m_model
->isExpanded(0));
1427 QVERIFY(itemsInsertedSpy
.wait());
1428 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1429 QCOMPARE(m_model
->groups(), expectedGroups
);
1431 m_model
->setExpanded(3, true);
1432 QVERIFY(m_model
->isExpanded(3));
1433 QVERIFY(itemsInsertedSpy
.wait());
1434 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1435 QCOMPARE(m_model
->groups(), expectedGroups
);
1438 void KFileItemModelTest::testInconsistentModel()
1440 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1442 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1443 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1444 m_model
->setRoles(modelRoles
);
1446 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1448 m_model
->loadDirectory(m_testDir
->url());
1449 QVERIFY(itemsInsertedSpy
.wait());
1450 QCOMPARE(itemsInModel(), QStringList() << "a");
1452 // Expand "a/" and "a/b/".
1453 m_model
->setExpanded(0, true);
1454 QVERIFY(m_model
->isExpanded(0));
1455 QVERIFY(itemsInsertedSpy
.wait());
1456 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1458 m_model
->setExpanded(1, true);
1459 QVERIFY(m_model
->isExpanded(1));
1460 QVERIFY(itemsInsertedSpy
.wait());
1461 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1463 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1464 // Such a thing can in principle happen when performing a search, and there
1466 // (a) match the search string, and
1467 // (b) are children of a folder that matches the search string and is expanded.
1469 // Note that the first item in the list of added items must be new (i.e., not
1470 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1471 // it receives items that are in the model already and ignore them.
1472 QUrl
url(m_model
->directory().url() + "/a2");
1473 KFileItem
newItem(url
);
1475 KFileItemList items
;
1476 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1477 m_model
->slotItemsAdded(m_model
->directory(), items
);
1478 m_model
->slotCompleted();
1479 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1481 m_model
->setExpanded(0, false);
1483 // Test that the right items have been removed, see
1484 // https://bugs.kde.org/show_bug.cgi?id=324371
1485 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1487 // Test that resorting does not cause a crash, see
1488 // https://bugs.kde.org/show_bug.cgi?id=325359
1489 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1490 m_model
->resortAllItems();
1494 void KFileItemModelTest::testChangeRolesForFilteredItems()
1496 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1498 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1499 modelRoles
<< "owner";
1500 m_model
->setRoles(modelRoles
);
1502 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1504 m_model
->loadDirectory(m_testDir
->url());
1505 QVERIFY(itemsInsertedSpy
.wait());
1506 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1508 for (int index
= 0; index
< m_model
->count(); ++index
) {
1509 // All items should have the "text" and "owner" roles, but not "group".
1510 QVERIFY(m_model
->data(index
).contains("text"));
1511 QVERIFY(m_model
->data(index
).contains("owner"));
1512 QVERIFY(!m_model
->data(index
).contains("group"));
1515 // Add a filter, such that only "aaa.txt" remains in the model.
1516 m_model
->setNameFilter("aaa");
1517 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1519 // Add the "group" role.
1520 modelRoles
<< "group";
1521 m_model
->setRoles(modelRoles
);
1523 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1524 m_model
->setNameFilter("aa");
1525 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1527 for (int index
= 0; index
< m_model
->count(); ++index
) {
1528 // All items should have the "text", "owner", and "group" roles.
1529 QVERIFY(m_model
->data(index
).contains("text"));
1530 QVERIFY(m_model
->data(index
).contains("owner"));
1531 QVERIFY(m_model
->data(index
).contains("group"));
1534 // Remove the "owner" role.
1535 modelRoles
.remove("owner");
1536 m_model
->setRoles(modelRoles
);
1538 // Clear the filter, and verify that all items have the expected roles
1539 m_model
->setNameFilter(QString());
1540 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1542 for (int index
= 0; index
< m_model
->count(); ++index
) {
1543 // All items should have the "text" and "group" roles, but now "owner".
1544 QVERIFY(m_model
->data(index
).contains("text"));
1545 QVERIFY(!m_model
->data(index
).contains("owner"));
1546 QVERIFY(m_model
->data(index
).contains("group"));
1550 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1552 KFileItemList items
;
1554 KIO::UDSEntry entry
[3];
1556 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1557 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
1559 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1560 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
1562 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1563 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
1565 for (int i
= 0; i
< 3; ++i
) {
1566 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1567 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1568 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
1569 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1570 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
1571 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1572 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
1575 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1576 m_model
->slotCompleted();
1578 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1581 m_model
->setNameFilter("a");
1582 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1585 m_model
->setSortRole("owner");
1587 // Clear the filter, and verify that the items are sorted correctly.
1588 m_model
->setNameFilter(QString());
1589 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1592 void KFileItemModelTest::testRefreshFilteredItems()
1594 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1596 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1598 m_model
->loadDirectory(m_testDir
->url());
1599 QVERIFY(itemsInsertedSpy
.wait());
1600 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1602 const KFileItem fileItemC
= m_model
->fileItem(2);
1604 // Show only the .txt files.
1605 m_model
->setNameFilter(".txt");
1606 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1608 // Rename one of the .jpg files.
1609 KFileItem fileItemE
= fileItemC
;
1610 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1611 urlE
.setPath(urlE
.path() + "/e.jpg");
1612 fileItemE
.setUrl(urlE
);
1614 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1616 // Show all files again, and verify that the model has updated the file name.
1617 m_model
->setNameFilter(QString());
1618 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1621 void KFileItemModelTest::testCreateMimeData()
1623 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1625 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1626 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1627 m_model
->setRoles(modelRoles
);
1629 m_testDir
->createFile("a/1");
1631 m_model
->loadDirectory(m_testDir
->url());
1632 QVERIFY(itemsInsertedSpy
.wait());
1633 QCOMPARE(itemsInModel(), QStringList() << "a");
1636 m_model
->setExpanded(0, true);
1637 QVERIFY(itemsInsertedSpy
.wait());
1638 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1640 // Verify that creating the MIME data for a child of an expanded folder does
1641 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1643 selection
.insert(1);
1644 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1648 void KFileItemModelTest::testCollapseFolderWhileLoading()
1650 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1652 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1653 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1654 m_model
->setRoles(modelRoles
);
1656 m_testDir
->createFile("a2/b/c1.txt");
1658 m_model
->loadDirectory(m_testDir
->url());
1659 QVERIFY(itemsInsertedSpy
.wait());
1660 QCOMPARE(itemsInModel(), QStringList() << "a2");
1663 m_model
->setExpanded(0, true);
1664 QVERIFY(m_model
->isExpanded(0));
1665 QVERIFY(itemsInsertedSpy
.wait());
1666 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1669 m_model
->setExpanded(1, true);
1670 QVERIFY(m_model
->isExpanded(1));
1671 QVERIFY(itemsInsertedSpy
.wait());
1672 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1674 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1675 // signal is not emitted yet.
1676 const KFileItem fileItemC1
= m_model
->fileItem(2);
1677 KFileItem fileItemC2
= fileItemC1
;
1678 QUrl urlC2
= fileItemC2
.url();
1679 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1680 urlC2
.setPath(urlC2
.path() + "c2.txt");
1681 fileItemC2
.setUrl(urlC2
);
1683 const QUrl urlB
= m_model
->fileItem(1).url();
1684 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1685 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1687 // Collapse "a2/". This should also remove all its (indirect) children from
1688 // the model and from the model's m_pendingItemsToInsert member.
1689 m_model
->setExpanded(0, false);
1690 QCOMPARE(itemsInModel(), QStringList() << "a2");
1692 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1693 // is still in m_pendingItemsToInsert, then we might get a crash, see
1694 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1695 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1696 // without parent in the model.
1697 m_model
->slotCompleted();
1698 QCOMPARE(itemsInModel(), QStringList() << "a2");
1700 // Expand "a2/" again.
1701 m_model
->setExpanded(0, true);
1702 QVERIFY(m_model
->isExpanded(0));
1703 QVERIFY(itemsInsertedSpy
.wait());
1704 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1706 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1707 // completed() signal is not emitted yet.
1708 const KFileItem fileItemA2
= m_model
->fileItem(0);
1709 KFileItem fileItemA1
= fileItemA2
;
1710 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1711 urlA1
.setPath(urlA1
.path() + "a1");
1712 fileItemA1
.setUrl(urlA1
);
1714 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1715 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1717 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1718 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1719 // confuse the code which collapses the folder.
1720 m_model
->setExpanded(0, false);
1721 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1722 QVERIFY(!m_model
->isExpanded(0));
1723 QVERIFY(!m_model
->isExpanded(1));
1726 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1728 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1730 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1732 m_model
->loadDirectory(m_testDir
->url());
1733 QVERIFY(itemsInsertedSpy
.wait());
1734 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1736 const KFileItem fileItemB
= m_model
->fileItem(1);
1738 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1740 list
<< fileItemB
<< fileItemB
;
1741 m_model
->slotItemsDeleted(list
);
1743 QVERIFY(m_model
->isConsistent());
1744 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1747 QStringList
KFileItemModelTest::itemsInModel() const
1750 for (int i
= 0; i
< m_model
->count(); i
++) {
1751 items
<< m_model
->fileItem(i
).text();
1756 QTEST_MAIN(KFileItemModelTest
)
1758 #include "kfileitemmodeltest.moc"