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 ***************************************************************************/
21 #include <QRandomGenerator>
29 #include "kitemviews/kfileitemmodel.h"
30 #include "kitemviews/private/kfileitemmodeldirlister.h"
33 void myMessageOutput(QtMsgType type
, const QMessageLogContext
& context
, const QString
& msg
)
43 fprintf(stderr
, "Critical: %s\n", msg
.toLocal8Bit().data());
46 fprintf(stderr
, "Fatal: %s\n", msg
.toLocal8Bit().data());
53 Q_DECLARE_METATYPE(KItemRange
)
54 Q_DECLARE_METATYPE(KItemRangeList
)
55 Q_DECLARE_METATYPE(QList
<int>)
57 class KFileItemModelTest
: public QObject
65 void testDefaultRoles();
66 void testDefaultSortRole();
67 void testDefaultGroupedSorting();
69 void testRemoveItems();
70 void testDirLoadingCompleted();
72 void testSetDataWithModifiedSortRole_data();
73 void testSetDataWithModifiedSortRole();
74 void testChangeSortRole();
75 void testResortAfterChangingName();
76 void testModelConsistencyWhenInsertingItems();
77 void testItemRangeConsistencyWhenInsertingItems();
78 void testExpandItems();
79 void testExpandParentItems();
80 void testMakeExpandedItemHidden();
81 void testRemoveFilteredExpandedItems();
83 void testIndexForKeyboardSearch();
84 void testNameFilter();
86 void testRefreshExpandedItem();
87 void testRemoveHiddenItems();
88 void collapseParentOfHiddenItems();
89 void removeParentOfHiddenItems();
90 void testGeneralParentChildRelationships();
91 void testNameRoleGroups();
92 void testNameRoleGroupsWithExpandedItems();
93 void testInconsistentModel();
94 void testChangeRolesForFilteredItems();
95 void testChangeSortRoleWhileFiltering();
96 void testRefreshFilteredItems();
97 void testCollapseFolderWhileLoading();
98 void testCreateMimeData();
99 void testDeleteFileMoreThanOnce();
102 QStringList
itemsInModel() const;
105 KFileItemModel
* m_model
;
109 void KFileItemModelTest::init()
111 // The item-model tests result in a huge number of debugging
112 // output from kdelibs. Only show critical and fatal messages.
113 qInstallMessageHandler(myMessageOutput
);
115 qRegisterMetaType
<KItemRange
>("KItemRange");
116 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
117 qRegisterMetaType
<KFileItemList
>("KFileItemList");
119 m_testDir
= new TestDir();
120 m_model
= new KFileItemModel();
121 m_model
->m_dirLister
->setAutoUpdate(false);
123 // Reduce the timer interval to make the test run faster.
124 m_model
->m_resortAllItemsTimer
->setInterval(0);
127 void KFileItemModelTest::cleanup()
136 void KFileItemModelTest::testDefaultRoles()
138 const QSet
<QByteArray
> roles
= m_model
->roles();
139 QCOMPARE(roles
.count(), 4);
140 QVERIFY(roles
.contains("text"));
141 QVERIFY(roles
.contains("isDir"));
142 QVERIFY(roles
.contains("isLink"));
143 QVERIFY(roles
.contains("isHidden"));
146 void KFileItemModelTest::testDefaultSortRole()
148 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
149 QVERIFY(itemsInsertedSpy
.isValid());
151 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
153 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
155 m_model
->loadDirectory(m_testDir
->url());
156 QVERIFY(itemsInsertedSpy
.wait());
158 QCOMPARE(m_model
->count(), 3);
159 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
160 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
161 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
164 void KFileItemModelTest::testDefaultGroupedSorting()
166 QCOMPARE(m_model
->groupedSorting(), false);
169 void KFileItemModelTest::testNewItems()
171 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
173 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
175 m_model
->loadDirectory(m_testDir
->url());
176 QVERIFY(itemsInsertedSpy
.wait());
178 QCOMPARE(m_model
->count(), 3);
180 QVERIFY(m_model
->isConsistent());
183 void KFileItemModelTest::testRemoveItems()
185 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
186 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
188 m_testDir
->createFiles({"a.txt", "b.txt"});
189 m_model
->loadDirectory(m_testDir
->url());
190 QVERIFY(itemsInsertedSpy
.wait());
191 QCOMPARE(m_model
->count(), 2);
192 QVERIFY(m_model
->isConsistent());
194 m_testDir
->removeFile("a.txt");
195 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
196 QVERIFY(itemsRemovedSpy
.wait());
197 QCOMPARE(m_model
->count(), 1);
198 QVERIFY(m_model
->isConsistent());
201 void KFileItemModelTest::testDirLoadingCompleted()
203 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
204 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
205 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
207 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
209 m_model
->loadDirectory(m_testDir
->url());
210 QVERIFY(loadingCompletedSpy
.wait());
211 QCOMPARE(loadingCompletedSpy
.count(), 1);
212 QCOMPARE(itemsInsertedSpy
.count(), 1);
213 QCOMPARE(itemsRemovedSpy
.count(), 0);
214 QCOMPARE(m_model
->count(), 3);
216 m_testDir
->createFiles({"d.txt", "e.txt"});
217 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
218 QVERIFY(loadingCompletedSpy
.wait());
219 QCOMPARE(loadingCompletedSpy
.count(), 2);
220 QCOMPARE(itemsInsertedSpy
.count(), 2);
221 QCOMPARE(itemsRemovedSpy
.count(), 0);
222 QCOMPARE(m_model
->count(), 5);
224 m_testDir
->removeFile("a.txt");
225 m_testDir
->createFile("f.txt");
226 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
227 QVERIFY(loadingCompletedSpy
.wait());
228 QCOMPARE(loadingCompletedSpy
.count(), 3);
229 QCOMPARE(itemsInsertedSpy
.count(), 3);
230 QCOMPARE(itemsRemovedSpy
.count(), 1);
231 QCOMPARE(m_model
->count(), 5);
233 m_testDir
->removeFile("b.txt");
234 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
235 QVERIFY(itemsRemovedSpy
.wait());
236 QCOMPARE(loadingCompletedSpy
.count(), 4);
237 QCOMPARE(itemsInsertedSpy
.count(), 3);
238 QCOMPARE(itemsRemovedSpy
.count(), 2);
239 QCOMPARE(m_model
->count(), 4);
241 QVERIFY(m_model
->isConsistent());
244 void KFileItemModelTest::testSetData()
246 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
247 QVERIFY(itemsInsertedSpy
.isValid());
248 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
249 QVERIFY(itemsChangedSpy
.isValid());
251 m_testDir
->createFile("a.txt");
253 m_model
->loadDirectory(m_testDir
->url());
254 QVERIFY(itemsInsertedSpy
.wait());
256 QHash
<QByteArray
, QVariant
> values
;
257 values
.insert("customRole1", "Test1");
258 values
.insert("customRole2", "Test2");
260 m_model
->setData(0, values
);
261 QCOMPARE(itemsChangedSpy
.count(), 1);
263 values
= m_model
->data(0);
264 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
265 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
266 QVERIFY(m_model
->isConsistent());
269 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
271 QTest::addColumn
<int>("changedIndex");
272 QTest::addColumn
<int>("changedRating");
273 QTest::addColumn
<bool>("expectMoveSignal");
274 QTest::addColumn
<int>("ratingIndex0");
275 QTest::addColumn
<int>("ratingIndex1");
276 QTest::addColumn
<int>("ratingIndex2");
279 // Index 0 = rating 2
280 // Index 1 = rating 4
281 // Index 2 = rating 6
283 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
284 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
285 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
287 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
288 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
289 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
292 void KFileItemModelTest::testSetDataWithModifiedSortRole()
294 QFETCH(int, changedIndex
);
295 QFETCH(int, changedRating
);
296 QFETCH(bool, expectMoveSignal
);
297 QFETCH(int, ratingIndex0
);
298 QFETCH(int, ratingIndex1
);
299 QFETCH(int, ratingIndex2
);
301 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
302 QVERIFY(itemsInsertedSpy
.isValid());
303 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
304 QVERIFY(itemsMovedSpy
.isValid());
306 // Changing the value of a sort-role must result in
307 // a reordering of the items.
308 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
309 m_model
->setSortRole("rating");
310 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
312 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
314 m_model
->loadDirectory(m_testDir
->url());
315 QVERIFY(itemsInsertedSpy
.wait());
317 // Fill the "rating" role of each file:
322 QHash
<QByteArray
, QVariant
> ratingA
;
323 ratingA
.insert("rating", 2);
324 m_model
->setData(0, ratingA
);
326 QHash
<QByteArray
, QVariant
> ratingB
;
327 ratingB
.insert("rating", 4);
328 m_model
->setData(1, ratingB
);
330 QHash
<QByteArray
, QVariant
> ratingC
;
331 ratingC
.insert("rating", 6);
332 m_model
->setData(2, ratingC
);
334 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
335 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
336 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
338 // Now change the rating from a.txt. This usually results
339 // in reordering of the items.
340 QHash
<QByteArray
, QVariant
> rating
;
341 rating
.insert("rating", changedRating
);
342 m_model
->setData(changedIndex
, rating
);
344 if (expectMoveSignal
) {
345 QVERIFY(itemsMovedSpy
.wait());
348 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
349 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
350 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
351 QVERIFY(m_model
->isConsistent());
354 void KFileItemModelTest::testChangeSortRole()
356 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
357 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
358 QVERIFY(itemsMovedSpy
.isValid());
360 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
362 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
364 m_model
->loadDirectory(m_testDir
->url());
365 QVERIFY(itemsInsertedSpy
.wait());
366 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
368 // Simulate that KFileItemModelRolesUpdater determines the mime type.
369 // Resorting the files by 'type' will only work immediately if their
370 // mime types are known.
371 for (int index
= 0; index
< m_model
->count(); ++index
) {
372 m_model
->fileItem(index
).determineMimeType();
375 // Now: sort by type.
376 m_model
->setSortRole("type");
377 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
378 QVERIFY(!itemsMovedSpy
.isEmpty());
380 // The actual order of the files might depend on the translation of the
381 // result of KFileItem::mimeComment() in the user's language.
382 QStringList version1
;
383 version1
<< "b.jpg" << "a.txt" << "c.txt";
385 QStringList version2
;
386 version2
<< "a.txt" << "c.txt" << "b.jpg";
388 const bool ok1
= (itemsInModel() == version1
);
389 const bool ok2
= (itemsInModel() == version2
);
394 void KFileItemModelTest::testResortAfterChangingName()
396 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
397 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
398 QVERIFY(itemsMovedSpy
.isValid());
400 // We sort by size in a directory where all files have the same size.
401 // Therefore, the files are sorted by their names.
402 m_model
->setSortRole("size");
404 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
406 m_model
->loadDirectory(m_testDir
->url());
407 QVERIFY(itemsInsertedSpy
.wait());
408 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
410 // We rename a.txt to d.txt. Even though the size has not changed at all,
411 // the model must re-sort the items.
412 QHash
<QByteArray
, QVariant
> data
;
413 data
.insert("text", "d.txt");
414 m_model
->setData(0, data
);
416 QVERIFY(itemsMovedSpy
.wait());
417 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
419 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
420 const KFileItem fileItemD
= m_model
->fileItem(2);
421 KFileItem fileItemA
= fileItemD
;
422 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
423 urlA
.setPath(urlA
.path() + "a.txt");
424 fileItemA
.setUrl(urlA
);
426 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
428 QVERIFY(itemsMovedSpy
.wait());
429 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
432 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
434 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
436 // KFileItemModel prevents that inserting a punch of items sequentially
437 // results in an itemsInserted()-signal for each item. Instead internally
438 // a timeout is given that collects such operations and results in only
439 // one itemsInserted()-signal. However in this test we want to stress
440 // KFileItemModel to do a lot of insert operation and hence decrease
441 // the timeout to 1 millisecond.
442 m_testDir
->createFile("1");
443 m_model
->loadDirectory(m_testDir
->url());
444 QVERIFY(itemsInsertedSpy
.wait());
445 QCOMPARE(m_model
->count(), 1);
447 // Insert 10 items for 20 times. After each insert operation the model consistency
449 QSet
<int> insertedItems
;
450 for (int i
= 0; i
< 20; ++i
) {
451 itemsInsertedSpy
.clear();
453 for (int j
= 0; j
< 10; ++j
) {
454 int itemName
= QRandomGenerator::global()->generate();
455 while (insertedItems
.contains(itemName
)) {
456 itemName
= QRandomGenerator::global()->generate();
458 insertedItems
.insert(itemName
);
460 m_testDir
->createFile(QString::number(itemName
));
463 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
464 if (itemsInsertedSpy
.isEmpty()) {
465 QVERIFY(itemsInsertedSpy
.wait());
468 QVERIFY(m_model
->isConsistent());
471 QCOMPARE(m_model
->count(), 201);
474 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
476 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
478 m_testDir
->createFiles({"B", "E", "G"});
480 // Due to inserting the 3 items one item-range with index == 0 and
481 // count == 3 must be given
482 m_model
->loadDirectory(m_testDir
->url());
483 QVERIFY(itemsInsertedSpy
.wait());
485 QCOMPARE(itemsInsertedSpy
.count(), 1);
486 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
487 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
488 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
490 // The indexes of the item-ranges must always be related to the model before
491 // the items have been inserted. Having:
494 // and inserting A, C, D, F the resulting model will be:
497 // and the item-ranges must be:
498 // index: 0, count: 1 for A
499 // index: 1, count: 2 for B, C
500 // index: 2, count: 1 for G
502 m_testDir
->createFiles({"A", "C", "D", "F"});
504 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
505 QVERIFY(itemsInsertedSpy
.wait());
507 QCOMPARE(itemsInsertedSpy
.count(), 1);
508 arguments
= itemsInsertedSpy
.takeFirst();
509 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
510 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
513 void KFileItemModelTest::testExpandItems()
515 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
516 QVERIFY(itemsInsertedSpy
.isValid());
517 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
518 QVERIFY(itemsRemovedSpy
.isValid());
519 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
520 QVERIFY(loadingCompletedSpy
.isValid());
522 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
523 // Besides testing the basic item expansion functionality, the test makes sure that
524 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
525 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
526 // first three characters.
527 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
528 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
529 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
530 m_model
->setRoles(modelRoles
);
532 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
534 // Store the URLs of all folders in a set.
535 QSet
<QUrl
> allFolders
;
536 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a")
537 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
538 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
540 m_model
->loadDirectory(m_testDir
->url());
541 QVERIFY(itemsInsertedSpy
.wait());
543 // So far, the model contains only "a/"
544 QCOMPARE(m_model
->count(), 1);
545 QVERIFY(m_model
->isExpandable(0));
546 QVERIFY(!m_model
->isExpanded(0));
547 QVERIFY(m_model
->expandedDirectories().empty());
549 QCOMPARE(itemsInsertedSpy
.count(), 1);
550 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
551 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
553 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
554 m_model
->setExpanded(0, true);
555 QVERIFY(m_model
->isExpanded(0));
556 QVERIFY(itemsInsertedSpy
.wait());
557 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
558 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
560 QCOMPARE(itemsInsertedSpy
.count(), 1);
561 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
562 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
564 QVERIFY(m_model
->isExpandable(1));
565 QVERIFY(!m_model
->isExpanded(1));
566 QVERIFY(m_model
->isExpandable(2));
567 QVERIFY(!m_model
->isExpanded(2));
569 // Expand the folder "a/a/" -> "a/a/1" becomes visible
570 m_model
->setExpanded(1, true);
571 QVERIFY(m_model
->isExpanded(1));
572 QVERIFY(itemsInsertedSpy
.wait());
573 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
574 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a")
575 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
577 QCOMPARE(itemsInsertedSpy
.count(), 1);
578 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
579 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
581 QVERIFY(!m_model
->isExpandable(2));
582 QVERIFY(!m_model
->isExpanded(2));
584 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
585 m_model
->setExpanded(3, true);
586 QVERIFY(m_model
->isExpanded(3));
587 QVERIFY(itemsInsertedSpy
.wait());
588 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
589 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
591 QCOMPARE(itemsInsertedSpy
.count(), 1);
592 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
593 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
595 QVERIFY(!m_model
->isExpandable(4));
596 QVERIFY(!m_model
->isExpanded(4));
598 // Collapse the top-level folder -> all other items should disappear
599 m_model
->setExpanded(0, false);
600 QVERIFY(!m_model
->isExpanded(0));
601 QCOMPARE(m_model
->count(), 1);
602 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
604 QCOMPARE(itemsRemovedSpy
.count(), 1);
605 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
606 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
607 QVERIFY(m_model
->isConsistent());
609 // Clear the model, reload the folder and try to restore the expanded folders.
611 QCOMPARE(m_model
->count(), 0);
612 QVERIFY(m_model
->expandedDirectories().empty());
614 m_model
->loadDirectory(m_testDir
->url());
615 m_model
->restoreExpandedDirectories(allFolders
);
616 QVERIFY(loadingCompletedSpy
.wait());
617 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
618 QVERIFY(m_model
->isExpanded(0));
619 QVERIFY(m_model
->isExpanded(1));
620 QVERIFY(!m_model
->isExpanded(2));
621 QVERIFY(m_model
->isExpanded(3));
622 QVERIFY(!m_model
->isExpanded(4));
623 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
624 QVERIFY(m_model
->isConsistent());
626 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
627 // This is how DolphinView restores the expanded folders when navigating in history.
628 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
629 QVERIFY(loadingCompletedSpy
.wait());
630 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
631 m_model
->restoreExpandedDirectories(allFolders
);
632 m_model
->loadDirectory(m_testDir
->url());
633 QVERIFY(loadingCompletedSpy
.wait());
634 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
635 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
637 // Remove all expanded items by changing the roles
638 itemsRemovedSpy
.clear();
639 m_model
->setRoles(originalModelRoles
);
640 QVERIFY(!m_model
->isExpanded(0));
641 QCOMPARE(m_model
->count(), 1);
642 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
644 QCOMPARE(itemsRemovedSpy
.count(), 1);
645 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
646 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
647 QVERIFY(m_model
->isConsistent());
650 void KFileItemModelTest::testExpandParentItems()
652 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
653 QSignalSpy
loadingCompletedSpy(m_model
, &KFileItemModel::directoryLoadingCompleted
);
654 QVERIFY(loadingCompletedSpy
.isValid());
656 // Create a tree structure of folders:
664 QSet
<QByteArray
> modelRoles
= m_model
->roles();
665 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
666 m_model
->setRoles(modelRoles
);
668 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
670 m_model
->loadDirectory(m_testDir
->url());
671 QVERIFY(itemsInsertedSpy
.wait());
673 // So far, the model contains only "a 1/" and "a2/".
674 QCOMPARE(m_model
->count(), 2);
675 QVERIFY(m_model
->expandedDirectories().empty());
677 // Expand the parents of "a2/b2/c2".
678 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
679 QVERIFY(loadingCompletedSpy
.wait());
681 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
682 // It's important that only the parents of "a1/b1/c1" are expanded.
683 QCOMPARE(m_model
->count(), 4);
684 QVERIFY(!m_model
->isExpanded(0));
685 QVERIFY(m_model
->isExpanded(1));
686 QVERIFY(m_model
->isExpanded(2));
687 QVERIFY(!m_model
->isExpanded(3));
689 // Expand the parents of "a 1/b1".
690 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
691 QVERIFY(loadingCompletedSpy
.wait());
693 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
694 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
695 QCOMPARE(m_model
->count(), 5);
696 QVERIFY(m_model
->isExpanded(0));
697 QVERIFY(!m_model
->isExpanded(1));
698 QVERIFY(m_model
->isExpanded(2));
699 QVERIFY(m_model
->isExpanded(3));
700 QVERIFY(!m_model
->isExpanded(4));
701 QVERIFY(m_model
->isConsistent());
704 m_model
->setExpanded(1, true);
705 QVERIFY(loadingCompletedSpy
.wait());
706 QCOMPARE(m_model
->count(), 6);
707 QVERIFY(m_model
->isExpanded(0));
708 QVERIFY(m_model
->isExpanded(1));
709 QVERIFY(!m_model
->isExpanded(2));
710 QVERIFY(m_model
->isExpanded(3));
711 QVERIFY(m_model
->isExpanded(4));
712 QVERIFY(!m_model
->isExpanded(5));
713 QVERIFY(m_model
->isConsistent());
715 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
716 m_model
->setExpanded(1, false);
717 QCOMPARE(m_model
->count(), 5);
718 QVERIFY(m_model
->isExpanded(0));
719 QVERIFY(!m_model
->isExpanded(1));
720 QVERIFY(m_model
->isExpanded(2));
721 QVERIFY(m_model
->isExpanded(3));
722 QVERIFY(!m_model
->isExpanded(4));
723 QVERIFY(m_model
->isConsistent());
727 * Renaming an expanded folder by prepending its name with a dot makes it
728 * hidden. Verify that this does not cause an inconsistent model state and
729 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
731 void KFileItemModelTest::testMakeExpandedItemHidden()
733 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
734 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
736 QSet
<QByteArray
> modelRoles
= m_model
->roles();
737 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
738 m_model
->setRoles(modelRoles
);
740 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
742 m_model
->loadDirectory(m_testDir
->url());
743 QVERIFY(itemsInsertedSpy
.wait());
745 // So far, the model contains only "1a/" and "1b".
746 QCOMPARE(m_model
->count(), 2);
747 m_model
->setExpanded(0, true);
748 QVERIFY(itemsInsertedSpy
.wait());
750 // Now "1a/2a" and "1a/2b" have appeared.
751 QCOMPARE(m_model
->count(), 4);
752 m_model
->setExpanded(1, true);
753 QVERIFY(itemsInsertedSpy
.wait());
754 QCOMPARE(m_model
->count(), 6);
756 // Rename "1a/2" and make it hidden.
757 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
758 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
760 KIO::SimpleJob
* job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
761 bool ok
= job
->exec();
763 QVERIFY(itemsRemovedSpy
.wait());
765 // "1a/2" and its subfolders have disappeared now.
766 QVERIFY(m_model
->isConsistent());
767 QCOMPARE(m_model
->count(), 3);
769 m_model
->setExpanded(0, false);
770 QCOMPARE(m_model
->count(), 2);
774 void KFileItemModelTest::testRemoveFilteredExpandedItems()
776 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
778 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
779 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
780 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
781 m_model
->setRoles(modelRoles
);
783 m_testDir
->createFiles({"folder/child", "file"});
785 m_model
->loadDirectory(m_testDir
->url());
786 QVERIFY(itemsInsertedSpy
.wait());
788 // So far, the model contains only "folder/" and "file".
789 QCOMPARE(m_model
->count(), 2);
790 QVERIFY(m_model
->isExpandable(0));
791 QVERIFY(!m_model
->isExpandable(1));
792 QVERIFY(!m_model
->isExpanded(0));
793 QVERIFY(!m_model
->isExpanded(1));
794 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
796 // Expand "folder" -> "folder/child" becomes visible.
797 m_model
->setExpanded(0, true);
798 QVERIFY(m_model
->isExpanded(0));
799 QVERIFY(itemsInsertedSpy
.wait());
800 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
802 // Add a name filter.
803 m_model
->setNameFilter("f");
804 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
806 m_model
->setNameFilter("fo");
807 QCOMPARE(itemsInModel(), QStringList() << "folder");
809 // Remove all expanded items by changing the roles
810 m_model
->setRoles(originalModelRoles
);
811 QVERIFY(!m_model
->isExpanded(0));
812 QCOMPARE(itemsInModel(), QStringList() << "folder");
814 // Remove the name filter and verify that "folder/child" does not reappear.
815 m_model
->setNameFilter(QString());
816 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
819 void KFileItemModelTest::testSorting()
821 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
822 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
823 QVERIFY(itemsMovedSpy
.isValid());
825 // Create some files with different sizes and modification times to check the different sorting options
826 QDateTime now
= QDateTime::currentDateTime();
828 QSet
<QByteArray
> roles
;
829 roles
.insert("text");
830 roles
.insert("isExpanded");
831 roles
.insert("isExpandable");
832 roles
.insert("expandedParentsCount");
833 m_model
->setRoles(roles
);
835 m_testDir
->createDir("c/c-2");
836 m_testDir
->createFile("c/c-2/c-3");
837 m_testDir
->createFile("c/c-1");
839 m_testDir
->createFile("a", "A file", now
.addDays(-3));
840 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
841 m_testDir
->createDir("c", now
.addDays(-2));
842 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
843 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
844 m_testDir
->createFile(".f");
846 m_model
->loadDirectory(m_testDir
->url());
847 QVERIFY(itemsInsertedSpy
.wait());
849 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
850 m_model
->setExpanded(index
, true);
851 QVERIFY(itemsInsertedSpy
.wait());
853 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
854 m_model
->setExpanded(index
, true);
855 QVERIFY(itemsInsertedSpy
.wait());
857 // Default: Sort by Name, ascending
858 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
859 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
860 QVERIFY(m_model
->sortDirectoriesFirst());
861 QVERIFY(!m_model
->showHiddenFiles());
862 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
864 // Sort by Name, ascending, 'Sort Folders First' disabled
865 m_model
->setSortDirectoriesFirst(false);
866 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
867 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
868 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
869 QCOMPARE(itemsMovedSpy
.count(), 1);
870 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
871 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
873 // Sort by Name, descending
874 m_model
->setSortDirectoriesFirst(true);
875 m_model
->setSortOrder(Qt::DescendingOrder
);
876 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
877 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
878 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
879 QCOMPARE(itemsMovedSpy
.count(), 2);
880 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
881 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
882 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
883 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
885 // Sort by Date, descending
886 m_model
->setSortDirectoriesFirst(true);
887 m_model
->setSortRole("modificationtime");
888 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
889 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
890 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
891 QCOMPARE(itemsMovedSpy
.count(), 1);
892 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
893 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
895 // Sort by Date, ascending
896 m_model
->setSortOrder(Qt::AscendingOrder
);
897 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
898 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
899 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
900 QCOMPARE(itemsMovedSpy
.count(), 1);
901 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
902 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
904 // Sort by Date, ascending, 'Sort Folders First' disabled
905 m_model
->setSortDirectoriesFirst(false);
906 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
907 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
908 QVERIFY(!m_model
->sortDirectoriesFirst());
909 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
910 QCOMPARE(itemsMovedSpy
.count(), 1);
911 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
912 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
914 // Sort by Name, ascending, 'Sort Folders First' disabled
915 m_model
->setSortRole("text");
916 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
917 QVERIFY(!m_model
->sortDirectoriesFirst());
918 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
919 QCOMPARE(itemsMovedSpy
.count(), 1);
920 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
921 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
923 // Sort by Size, ascending, 'Sort Folders First' disabled
924 m_model
->setSortRole("size");
925 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
926 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
927 QVERIFY(!m_model
->sortDirectoriesFirst());
928 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
929 QCOMPARE(itemsMovedSpy
.count(), 1);
930 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
931 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
933 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
934 m_model
->setSortDirectoriesFirst(true);
935 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
936 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
937 QVERIFY(m_model
->sortDirectoriesFirst());
938 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
939 QCOMPARE(itemsMovedSpy
.count(), 0);
941 // Sort by Size, descending, 'Sort Folders First' enabled
942 m_model
->setSortOrder(Qt::DescendingOrder
);
943 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
944 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
945 QVERIFY(m_model
->sortDirectoriesFirst());
946 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
947 QCOMPARE(itemsMovedSpy
.count(), 1);
948 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
949 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
951 // TODO: Sort by other roles; show/hide hidden files
954 void KFileItemModelTest::testIndexForKeyboardSearch()
956 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
958 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
960 m_model
->loadDirectory(m_testDir
->url());
961 QVERIFY(itemsInsertedSpy
.wait());
963 // Search from index 0
964 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
965 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
966 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
967 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
968 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
969 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
970 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
971 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
972 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
973 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
974 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
976 // Start a search somewhere in the middle
977 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
978 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
979 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
980 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
982 // Test searches that go past the last item back to index 0
983 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
984 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
985 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
986 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
988 // Test searches that yield no result
989 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
990 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
991 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
992 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
993 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
995 // Test upper case searches (note that search is case insensitive)
996 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
997 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
998 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
999 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
1001 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1004 void KFileItemModelTest::testNameFilter()
1006 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1008 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1010 m_model
->loadDirectory(m_testDir
->url());
1011 QVERIFY(itemsInsertedSpy
.wait());
1013 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1014 QCOMPARE(m_model
->count(), 3);
1016 m_model
->setNameFilter("A2"); // Shows only A2
1017 QCOMPARE(m_model
->count(), 1);
1019 m_model
->setNameFilter("A2"); // Shows only A1
1020 QCOMPARE(m_model
->count(), 1);
1022 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1023 QCOMPARE(m_model
->count(), 2);
1025 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1026 QCOMPARE(m_model
->count(), 2);
1028 m_model
->setNameFilter(QString()); // Shows again all items
1029 QCOMPARE(m_model
->count(), 5);
1033 * Verifies that we do not crash when adding a KFileItem with an empty path.
1034 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1035 * tried to always read the first character of the path, even if the path is empty.
1037 void KFileItemModelTest::testEmptyPath()
1039 QSet
<QByteArray
> roles
;
1040 roles
.insert("text");
1041 roles
.insert("isExpanded");
1042 roles
.insert("isExpandable");
1043 roles
.insert("expandedParentsCount");
1044 m_model
->setRoles(roles
);
1046 const QUrl emptyUrl
;
1047 QVERIFY(emptyUrl
.path().isEmpty());
1049 const QUrl
url("file:///test/");
1051 KFileItemList items
;
1052 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1053 m_model
->slotItemsAdded(emptyUrl
, items
);
1054 m_model
->slotCompleted();
1058 * Verifies that the 'isExpanded' state of folders does not change when the
1059 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1061 void KFileItemModelTest::testRefreshExpandedItem()
1063 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1064 QSignalSpy
itemsChangedSpy(m_model
, &KFileItemModel::itemsChanged
);
1065 QVERIFY(itemsChangedSpy
.isValid());
1067 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1068 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1069 m_model
->setRoles(modelRoles
);
1071 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1073 m_model
->loadDirectory(m_testDir
->url());
1074 QVERIFY(itemsInsertedSpy
.wait());
1075 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1077 m_model
->setExpanded(0, true);
1078 QVERIFY(itemsInsertedSpy
.wait());
1079 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1080 QVERIFY(m_model
->isExpanded(0));
1082 const KFileItem item
= m_model
->fileItem(0);
1083 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1084 QVERIFY(!itemsChangedSpy
.isEmpty());
1086 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1087 QVERIFY(m_model
->isExpanded(0));
1091 * Verify that removing hidden files and folders from the model does not
1092 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1094 void KFileItemModelTest::testRemoveHiddenItems()
1096 m_testDir
->createDir(".a");
1097 m_testDir
->createDir(".b");
1098 m_testDir
->createDir("c");
1099 m_testDir
->createDir("d");
1100 m_testDir
->createFiles({".f", ".g", "h", "i"});
1102 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1103 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1105 m_model
->setShowHiddenFiles(true);
1106 m_model
->loadDirectory(m_testDir
->url());
1107 QVERIFY(itemsInsertedSpy
.wait());
1108 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1109 QCOMPARE(itemsInsertedSpy
.count(), 1);
1110 QCOMPARE(itemsRemovedSpy
.count(), 0);
1111 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1112 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1114 m_model
->setShowHiddenFiles(false);
1115 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1116 QCOMPARE(itemsInsertedSpy
.count(), 0);
1117 QCOMPARE(itemsRemovedSpy
.count(), 1);
1118 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1119 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1121 m_model
->setShowHiddenFiles(true);
1122 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1123 QCOMPARE(itemsInsertedSpy
.count(), 1);
1124 QCOMPARE(itemsRemovedSpy
.count(), 0);
1125 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1126 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1129 QCOMPARE(itemsInModel(), QStringList());
1130 QCOMPARE(itemsInsertedSpy
.count(), 0);
1131 QCOMPARE(itemsRemovedSpy
.count(), 1);
1132 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1133 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1135 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1136 // Verify that this does not make the model crash.
1137 m_model
->setShowHiddenFiles(false);
1141 * Verify that filtered items are removed when their parent is collapsed.
1143 void KFileItemModelTest::collapseParentOfHiddenItems()
1145 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1146 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1148 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1149 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1150 m_model
->setRoles(modelRoles
);
1152 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1154 m_model
->loadDirectory(m_testDir
->url());
1155 QVERIFY(itemsInsertedSpy
.wait());
1156 QCOMPARE(m_model
->count(), 1); // Only "a/"
1159 m_model
->setExpanded(0, true);
1160 QVERIFY(itemsInsertedSpy
.wait());
1161 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1164 m_model
->setExpanded(1, true);
1165 QVERIFY(itemsInsertedSpy
.wait());
1166 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1169 m_model
->setExpanded(2, true);
1170 QVERIFY(itemsInsertedSpy
.wait());
1171 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"
1173 // Set a name filter that matches nothing -> only the expanded folders remain.
1174 m_model
->setNameFilter("xyz");
1175 QCOMPARE(itemsRemovedSpy
.count(), 1);
1176 QCOMPARE(m_model
->count(), 3);
1177 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1179 // Collapse the folder "a/".
1180 m_model
->setExpanded(0, false);
1181 QCOMPARE(itemsRemovedSpy
.count(), 2);
1182 QCOMPARE(m_model
->count(), 1);
1183 QCOMPARE(itemsInModel(), QStringList() << "a");
1185 // Remove the filter -> no files should appear (and we should not get a crash).
1186 m_model
->setNameFilter(QString());
1187 QCOMPARE(m_model
->count(), 1);
1191 * Verify that filtered items are removed when their parent is deleted.
1193 void KFileItemModelTest::removeParentOfHiddenItems()
1195 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1196 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1198 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1199 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1200 m_model
->setRoles(modelRoles
);
1202 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1204 m_model
->loadDirectory(m_testDir
->url());
1205 QVERIFY(itemsInsertedSpy
.wait());
1206 QCOMPARE(m_model
->count(), 1); // Only "a/"
1209 m_model
->setExpanded(0, true);
1210 QVERIFY(itemsInsertedSpy
.wait());
1211 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1214 m_model
->setExpanded(1, true);
1215 QVERIFY(itemsInsertedSpy
.wait());
1216 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1219 m_model
->setExpanded(2, true);
1220 QVERIFY(itemsInsertedSpy
.wait());
1221 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"
1223 // Set a name filter that matches nothing -> only the expanded folders remain.
1224 m_model
->setNameFilter("xyz");
1225 QCOMPARE(itemsRemovedSpy
.count(), 1);
1226 QCOMPARE(m_model
->count(), 3);
1227 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1229 // Simulate the deletion of the directory "a/b/".
1230 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1231 QCOMPARE(itemsRemovedSpy
.count(), 2);
1232 QCOMPARE(m_model
->count(), 1);
1233 QCOMPARE(itemsInModel(), QStringList() << "a");
1235 // Remove the filter -> only the file "a/1" should appear.
1236 m_model
->setNameFilter(QString());
1237 QCOMPARE(m_model
->count(), 2);
1238 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1242 * Create a tree structure where parent-child relationships can not be
1243 * determined by parsing the URLs, and verify that KFileItemModel
1244 * handles them correctly.
1246 void KFileItemModelTest::testGeneralParentChildRelationships()
1248 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1249 QSignalSpy
itemsRemovedSpy(m_model
, &KFileItemModel::itemsRemoved
);
1251 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1252 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1253 m_model
->setRoles(modelRoles
);
1255 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1257 m_model
->loadDirectory(m_testDir
->url());
1258 QVERIFY(itemsInsertedSpy
.wait());
1259 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1261 // Expand all folders.
1262 m_model
->setExpanded(0, true);
1263 QVERIFY(itemsInsertedSpy
.wait());
1264 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1266 m_model
->setExpanded(1, true);
1267 QVERIFY(itemsInsertedSpy
.wait());
1268 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1270 m_model
->setExpanded(3, true);
1271 QVERIFY(itemsInsertedSpy
.wait());
1272 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1274 m_model
->setExpanded(4, true);
1275 QVERIFY(itemsInsertedSpy
.wait());
1276 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1278 // Add some more children and grand-children.
1279 const QUrl parent1
= m_model
->fileItem(0).url();
1280 const QUrl parent2
= m_model
->fileItem(3).url();
1281 const QUrl realChild1
= m_model
->fileItem(1).url();
1282 const QUrl realChild2
= m_model
->fileItem(4).url();
1284 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1285 m_model
->slotCompleted();
1286 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1288 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1289 m_model
->slotCompleted();
1290 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1292 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1293 m_model
->slotCompleted();
1294 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1296 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1297 m_model
->slotCompleted();
1298 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1300 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1301 m_model
->slotCompleted();
1302 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1304 // Set a name filter that matches nothing -> only expanded folders remain.
1305 m_model
->setNameFilter("xyz");
1306 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1307 QCOMPARE(itemsRemovedSpy
.count(), 1);
1308 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1309 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1310 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1312 // Collapse "parent1".
1313 m_model
->setExpanded(0, false);
1314 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1315 QCOMPARE(itemsRemovedSpy
.count(), 1);
1316 arguments
= itemsRemovedSpy
.takeFirst();
1317 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1318 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1320 // Remove "parent2".
1321 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1322 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1323 QCOMPARE(itemsRemovedSpy
.count(), 1);
1324 arguments
= itemsRemovedSpy
.takeFirst();
1325 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1326 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1328 // Clear filter, verify that no items reappear.
1329 m_model
->setNameFilter(QString());
1330 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1333 void KFileItemModelTest::testNameRoleGroups()
1335 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1336 QSignalSpy
itemsMovedSpy(m_model
, &KFileItemModel::itemsMoved
);
1337 QVERIFY(itemsMovedSpy
.isValid());
1338 QSignalSpy
groupsChangedSpy(m_model
, &KFileItemModel::groupsChanged
);
1339 QVERIFY(groupsChangedSpy
.isValid());
1341 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1343 m_model
->setGroupedSorting(true);
1344 m_model
->loadDirectory(m_testDir
->url());
1345 QVERIFY(itemsInsertedSpy
.wait());
1346 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1348 QList
<QPair
<int, QVariant
> > expectedGroups
;
1349 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1350 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1351 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1352 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1353 QCOMPARE(m_model
->groups(), expectedGroups
);
1355 // Rename d.txt to a.txt.
1356 QHash
<QByteArray
, QVariant
> data
;
1357 data
.insert("text", "a.txt");
1358 m_model
->setData(2, data
);
1359 QVERIFY(itemsMovedSpy
.wait());
1360 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1362 expectedGroups
.clear();
1363 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1364 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1365 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1366 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1367 QCOMPARE(m_model
->groups(), expectedGroups
);
1369 // Rename c.txt to d.txt.
1370 data
.insert("text", "d.txt");
1371 m_model
->setData(2, data
);
1372 QVERIFY(groupsChangedSpy
.wait());
1373 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1375 expectedGroups
.clear();
1376 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1377 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1378 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1379 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1380 QCOMPARE(m_model
->groups(), expectedGroups
);
1382 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1383 const KFileItem fileItemD
= m_model
->fileItem(2);
1384 KFileItem fileItemC
= fileItemD
;
1385 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1386 urlC
.setPath(urlC
.path() + "c.txt");
1387 fileItemC
.setUrl(urlC
);
1389 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1390 QVERIFY(groupsChangedSpy
.wait());
1391 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1393 expectedGroups
.clear();
1394 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1395 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1396 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1397 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1398 QCOMPARE(m_model
->groups(), expectedGroups
);
1401 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1403 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1405 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1406 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1407 m_model
->setRoles(modelRoles
);
1409 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1411 m_model
->setGroupedSorting(true);
1412 m_model
->loadDirectory(m_testDir
->url());
1413 QVERIFY(itemsInsertedSpy
.wait());
1414 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1416 QList
<QPair
<int, QVariant
> > expectedGroups
;
1417 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1418 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1419 QCOMPARE(m_model
->groups(), expectedGroups
);
1421 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1422 expectedGroups
.clear();
1423 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1424 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1426 m_model
->setExpanded(0, true);
1427 QVERIFY(m_model
->isExpanded(0));
1428 QVERIFY(itemsInsertedSpy
.wait());
1429 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1430 QCOMPARE(m_model
->groups(), expectedGroups
);
1432 m_model
->setExpanded(3, true);
1433 QVERIFY(m_model
->isExpanded(3));
1434 QVERIFY(itemsInsertedSpy
.wait());
1435 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1436 QCOMPARE(m_model
->groups(), expectedGroups
);
1439 void KFileItemModelTest::testInconsistentModel()
1441 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1443 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1444 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1445 m_model
->setRoles(modelRoles
);
1447 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1449 m_model
->loadDirectory(m_testDir
->url());
1450 QVERIFY(itemsInsertedSpy
.wait());
1451 QCOMPARE(itemsInModel(), QStringList() << "a");
1453 // Expand "a/" and "a/b/".
1454 m_model
->setExpanded(0, true);
1455 QVERIFY(m_model
->isExpanded(0));
1456 QVERIFY(itemsInsertedSpy
.wait());
1457 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1459 m_model
->setExpanded(1, true);
1460 QVERIFY(m_model
->isExpanded(1));
1461 QVERIFY(itemsInsertedSpy
.wait());
1462 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1464 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1465 // Such a thing can in principle happen when performing a search, and there
1467 // (a) match the search string, and
1468 // (b) are children of a folder that matches the search string and is expanded.
1470 // Note that the first item in the list of added items must be new (i.e., not
1471 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1472 // it receives items that are in the model already and ignore them.
1473 QUrl
url(m_model
->directory().url() + "/a2");
1474 KFileItem
newItem(url
);
1476 KFileItemList items
;
1477 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1478 m_model
->slotItemsAdded(m_model
->directory(), items
);
1479 m_model
->slotCompleted();
1480 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1482 m_model
->setExpanded(0, false);
1484 // Test that the right items have been removed, see
1485 // https://bugs.kde.org/show_bug.cgi?id=324371
1486 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1488 // Test that resorting does not cause a crash, see
1489 // https://bugs.kde.org/show_bug.cgi?id=325359
1490 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1491 m_model
->resortAllItems();
1495 void KFileItemModelTest::testChangeRolesForFilteredItems()
1497 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1499 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1500 modelRoles
<< "owner";
1501 m_model
->setRoles(modelRoles
);
1503 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1505 m_model
->loadDirectory(m_testDir
->url());
1506 QVERIFY(itemsInsertedSpy
.wait());
1507 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1509 for (int index
= 0; index
< m_model
->count(); ++index
) {
1510 // All items should have the "text" and "owner" roles, but not "group".
1511 QVERIFY(m_model
->data(index
).contains("text"));
1512 QVERIFY(m_model
->data(index
).contains("owner"));
1513 QVERIFY(!m_model
->data(index
).contains("group"));
1516 // Add a filter, such that only "aaa.txt" remains in the model.
1517 m_model
->setNameFilter("aaa");
1518 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1520 // Add the "group" role.
1521 modelRoles
<< "group";
1522 m_model
->setRoles(modelRoles
);
1524 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1525 m_model
->setNameFilter("aa");
1526 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1528 for (int index
= 0; index
< m_model
->count(); ++index
) {
1529 // All items should have the "text", "owner", and "group" roles.
1530 QVERIFY(m_model
->data(index
).contains("text"));
1531 QVERIFY(m_model
->data(index
).contains("owner"));
1532 QVERIFY(m_model
->data(index
).contains("group"));
1535 // Remove the "owner" role.
1536 modelRoles
.remove("owner");
1537 m_model
->setRoles(modelRoles
);
1539 // Clear the filter, and verify that all items have the expected roles
1540 m_model
->setNameFilter(QString());
1541 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1543 for (int index
= 0; index
< m_model
->count(); ++index
) {
1544 // All items should have the "text" and "group" roles, but now "owner".
1545 QVERIFY(m_model
->data(index
).contains("text"));
1546 QVERIFY(!m_model
->data(index
).contains("owner"));
1547 QVERIFY(m_model
->data(index
).contains("group"));
1551 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1553 KFileItemList items
;
1555 KIO::UDSEntry entry
[3];
1557 entry
[0].fastInsert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1558 entry
[0].fastInsert(KIO::UDSEntry::UDS_USER
, "user-b");
1560 entry
[1].fastInsert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1561 entry
[1].fastInsert(KIO::UDSEntry::UDS_USER
, "user-c");
1563 entry
[2].fastInsert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1564 entry
[2].fastInsert(KIO::UDSEntry::UDS_USER
, "user-a");
1566 for (int i
= 0; i
< 3; ++i
) {
1567 entry
[i
].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1568 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1569 entry
[i
].fastInsert(KIO::UDSEntry::UDS_SIZE
, 0);
1570 entry
[i
].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1571 entry
[i
].fastInsert(KIO::UDSEntry::UDS_GROUP
, "group");
1572 entry
[i
].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1573 items
.append(KFileItem(entry
[i
], m_testDir
->url(), false, true));
1576 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1577 m_model
->slotCompleted();
1579 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1582 m_model
->setNameFilter("a");
1583 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1586 m_model
->setSortRole("owner");
1588 // Clear the filter, and verify that the items are sorted correctly.
1589 m_model
->setNameFilter(QString());
1590 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1593 void KFileItemModelTest::testRefreshFilteredItems()
1595 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1597 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1599 m_model
->loadDirectory(m_testDir
->url());
1600 QVERIFY(itemsInsertedSpy
.wait());
1601 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1603 const KFileItem fileItemC
= m_model
->fileItem(2);
1605 // Show only the .txt files.
1606 m_model
->setNameFilter(".txt");
1607 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1609 // Rename one of the .jpg files.
1610 KFileItem fileItemE
= fileItemC
;
1611 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1612 urlE
.setPath(urlE
.path() + "/e.jpg");
1613 fileItemE
.setUrl(urlE
);
1615 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1617 // Show all files again, and verify that the model has updated the file name.
1618 m_model
->setNameFilter(QString());
1619 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1622 void KFileItemModelTest::testCreateMimeData()
1624 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1626 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1627 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1628 m_model
->setRoles(modelRoles
);
1630 m_testDir
->createFile("a/1");
1632 m_model
->loadDirectory(m_testDir
->url());
1633 QVERIFY(itemsInsertedSpy
.wait());
1634 QCOMPARE(itemsInModel(), QStringList() << "a");
1637 m_model
->setExpanded(0, true);
1638 QVERIFY(itemsInsertedSpy
.wait());
1639 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1641 // Verify that creating the MIME data for a child of an expanded folder does
1642 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1644 selection
.insert(1);
1645 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1649 void KFileItemModelTest::testCollapseFolderWhileLoading()
1651 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1653 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1654 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1655 m_model
->setRoles(modelRoles
);
1657 m_testDir
->createFile("a2/b/c1.txt");
1659 m_model
->loadDirectory(m_testDir
->url());
1660 QVERIFY(itemsInsertedSpy
.wait());
1661 QCOMPARE(itemsInModel(), QStringList() << "a2");
1664 m_model
->setExpanded(0, true);
1665 QVERIFY(m_model
->isExpanded(0));
1666 QVERIFY(itemsInsertedSpy
.wait());
1667 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1670 m_model
->setExpanded(1, true);
1671 QVERIFY(m_model
->isExpanded(1));
1672 QVERIFY(itemsInsertedSpy
.wait());
1673 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1675 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1676 // signal is not emitted yet.
1677 const KFileItem fileItemC1
= m_model
->fileItem(2);
1678 KFileItem fileItemC2
= fileItemC1
;
1679 QUrl urlC2
= fileItemC2
.url();
1680 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1681 urlC2
.setPath(urlC2
.path() + "c2.txt");
1682 fileItemC2
.setUrl(urlC2
);
1684 const QUrl urlB
= m_model
->fileItem(1).url();
1685 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1686 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1688 // Collapse "a2/". This should also remove all its (indirect) children from
1689 // the model and from the model's m_pendingItemsToInsert member.
1690 m_model
->setExpanded(0, false);
1691 QCOMPARE(itemsInModel(), QStringList() << "a2");
1693 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1694 // is still in m_pendingItemsToInsert, then we might get a crash, see
1695 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1696 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1697 // without parent in the model.
1698 m_model
->slotCompleted();
1699 QCOMPARE(itemsInModel(), QStringList() << "a2");
1701 // Expand "a2/" again.
1702 m_model
->setExpanded(0, true);
1703 QVERIFY(m_model
->isExpanded(0));
1704 QVERIFY(itemsInsertedSpy
.wait());
1705 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1707 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1708 // completed() signal is not emitted yet.
1709 const KFileItem fileItemA2
= m_model
->fileItem(0);
1710 KFileItem fileItemA1
= fileItemA2
;
1711 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1712 urlA1
.setPath(urlA1
.path() + "a1");
1713 fileItemA1
.setUrl(urlA1
);
1715 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1716 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1718 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1719 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1720 // confuse the code which collapses the folder.
1721 m_model
->setExpanded(0, false);
1722 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1723 QVERIFY(!m_model
->isExpanded(0));
1724 QVERIFY(!m_model
->isExpanded(1));
1727 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1729 QSignalSpy
itemsInsertedSpy(m_model
, &KFileItemModel::itemsInserted
);
1731 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1733 m_model
->loadDirectory(m_testDir
->url());
1734 QVERIFY(itemsInsertedSpy
.wait());
1735 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1737 const KFileItem fileItemB
= m_model
->fileItem(1);
1739 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1741 list
<< fileItemB
<< fileItemB
;
1742 m_model
->slotItemsDeleted(list
);
1744 QVERIFY(m_model
->isConsistent());
1745 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1748 QStringList
KFileItemModelTest::itemsInModel() const
1751 for (int i
= 0; i
< m_model
->count(); i
++) {
1752 items
<< m_model
->fileItem(i
).text();
1757 QTEST_MAIN(KFileItemModelTest
)
1759 #include "kfileitemmodeltest.moc"