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(), 3);
139 QVERIFY(roles
.contains("text"));
140 QVERIFY(roles
.contains("isDir"));
141 QVERIFY(roles
.contains("isLink"));
144 void KFileItemModelTest::testDefaultSortRole()
146 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
147 QVERIFY(itemsInsertedSpy
.isValid());
149 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
151 m_testDir
->createFiles({"c.txt", "a.txt", "b.txt"});
153 m_model
->loadDirectory(m_testDir
->url());
154 QVERIFY(itemsInsertedSpy
.wait());
156 QCOMPARE(m_model
->count(), 3);
157 QCOMPARE(m_model
->data(0).value("text").toString(), QString("a.txt"));
158 QCOMPARE(m_model
->data(1).value("text").toString(), QString("b.txt"));
159 QCOMPARE(m_model
->data(2).value("text").toString(), QString("c.txt"));
162 void KFileItemModelTest::testDefaultGroupedSorting()
164 QCOMPARE(m_model
->groupedSorting(), false);
167 void KFileItemModelTest::testNewItems()
169 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
171 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
173 m_model
->loadDirectory(m_testDir
->url());
174 QVERIFY(itemsInsertedSpy
.wait());
176 QCOMPARE(m_model
->count(), 3);
178 QVERIFY(m_model
->isConsistent());
181 void KFileItemModelTest::testRemoveItems()
183 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
184 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
186 m_testDir
->createFiles({"a.txt", "b.txt"});
187 m_model
->loadDirectory(m_testDir
->url());
188 QVERIFY(itemsInsertedSpy
.wait());
189 QCOMPARE(m_model
->count(), 2);
190 QVERIFY(m_model
->isConsistent());
192 m_testDir
->removeFile("a.txt");
193 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
194 QVERIFY(itemsRemovedSpy
.wait());
195 QCOMPARE(m_model
->count(), 1);
196 QVERIFY(m_model
->isConsistent());
199 void KFileItemModelTest::testDirLoadingCompleted()
201 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
202 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
203 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
205 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
207 m_model
->loadDirectory(m_testDir
->url());
208 QVERIFY(loadingCompletedSpy
.wait());
209 QCOMPARE(loadingCompletedSpy
.count(), 1);
210 QCOMPARE(itemsInsertedSpy
.count(), 1);
211 QCOMPARE(itemsRemovedSpy
.count(), 0);
212 QCOMPARE(m_model
->count(), 3);
214 m_testDir
->createFiles({"d.txt", "e.txt"});
215 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
216 QVERIFY(loadingCompletedSpy
.wait());
217 QCOMPARE(loadingCompletedSpy
.count(), 2);
218 QCOMPARE(itemsInsertedSpy
.count(), 2);
219 QCOMPARE(itemsRemovedSpy
.count(), 0);
220 QCOMPARE(m_model
->count(), 5);
222 m_testDir
->removeFile("a.txt");
223 m_testDir
->createFile("f.txt");
224 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
225 QVERIFY(loadingCompletedSpy
.wait());
226 QCOMPARE(loadingCompletedSpy
.count(), 3);
227 QCOMPARE(itemsInsertedSpy
.count(), 3);
228 QCOMPARE(itemsRemovedSpy
.count(), 1);
229 QCOMPARE(m_model
->count(), 5);
231 m_testDir
->removeFile("b.txt");
232 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
233 QVERIFY(itemsRemovedSpy
.wait());
234 QCOMPARE(loadingCompletedSpy
.count(), 4);
235 QCOMPARE(itemsInsertedSpy
.count(), 3);
236 QCOMPARE(itemsRemovedSpy
.count(), 2);
237 QCOMPARE(m_model
->count(), 4);
239 QVERIFY(m_model
->isConsistent());
242 void KFileItemModelTest::testSetData()
244 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
245 QVERIFY(itemsInsertedSpy
.isValid());
246 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
, QSet
<QByteArray
>)));
247 QVERIFY(itemsChangedSpy
.isValid());
249 m_testDir
->createFile("a.txt");
251 m_model
->loadDirectory(m_testDir
->url());
252 QVERIFY(itemsInsertedSpy
.wait());
254 QHash
<QByteArray
, QVariant
> values
;
255 values
.insert("customRole1", "Test1");
256 values
.insert("customRole2", "Test2");
258 m_model
->setData(0, values
);
259 QCOMPARE(itemsChangedSpy
.count(), 1);
261 values
= m_model
->data(0);
262 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
263 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
264 QVERIFY(m_model
->isConsistent());
267 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
269 QTest::addColumn
<int>("changedIndex");
270 QTest::addColumn
<int>("changedRating");
271 QTest::addColumn
<bool>("expectMoveSignal");
272 QTest::addColumn
<int>("ratingIndex0");
273 QTest::addColumn
<int>("ratingIndex1");
274 QTest::addColumn
<int>("ratingIndex2");
277 // Index 0 = rating 2
278 // Index 1 = rating 4
279 // Index 2 = rating 6
281 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
282 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
283 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
285 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
286 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
287 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
290 void KFileItemModelTest::testSetDataWithModifiedSortRole()
292 QFETCH(int, changedIndex
);
293 QFETCH(int, changedRating
);
294 QFETCH(bool, expectMoveSignal
);
295 QFETCH(int, ratingIndex0
);
296 QFETCH(int, ratingIndex1
);
297 QFETCH(int, ratingIndex2
);
299 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
300 QVERIFY(itemsInsertedSpy
.isValid());
301 QSignalSpy
itemsMovedSpy(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
302 QVERIFY(itemsMovedSpy
.isValid());
304 // Changing the value of a sort-role must result in
305 // a reordering of the items.
306 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
307 m_model
->setSortRole("rating");
308 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
310 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
312 m_model
->loadDirectory(m_testDir
->url());
313 QVERIFY(itemsInsertedSpy
.wait());
315 // Fill the "rating" role of each file:
320 QHash
<QByteArray
, QVariant
> ratingA
;
321 ratingA
.insert("rating", 2);
322 m_model
->setData(0, ratingA
);
324 QHash
<QByteArray
, QVariant
> ratingB
;
325 ratingB
.insert("rating", 4);
326 m_model
->setData(1, ratingB
);
328 QHash
<QByteArray
, QVariant
> ratingC
;
329 ratingC
.insert("rating", 6);
330 m_model
->setData(2, ratingC
);
332 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
333 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
334 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
336 // Now change the rating from a.txt. This usually results
337 // in reordering of the items.
338 QHash
<QByteArray
, QVariant
> rating
;
339 rating
.insert("rating", changedRating
);
340 m_model
->setData(changedIndex
, rating
);
342 if (expectMoveSignal
) {
343 QVERIFY(itemsMovedSpy
.wait());
346 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
347 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
348 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
349 QVERIFY(m_model
->isConsistent());
352 void KFileItemModelTest::testChangeSortRole()
354 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
355 QSignalSpy
itemsMovedSpy(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
356 QVERIFY(itemsMovedSpy
.isValid());
358 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
360 m_testDir
->createFiles({"a.txt", "b.jpg", "c.txt"});
362 m_model
->loadDirectory(m_testDir
->url());
363 QVERIFY(itemsInsertedSpy
.wait());
364 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
366 // Simulate that KFileItemModelRolesUpdater determines the mime type.
367 // Resorting the files by 'type' will only work immediately if their
368 // mime types are known.
369 for (int index
= 0; index
< m_model
->count(); ++index
) {
370 m_model
->fileItem(index
).determineMimeType();
373 // Now: sort by type.
374 m_model
->setSortRole("type");
375 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
376 QVERIFY(!itemsMovedSpy
.isEmpty());
378 // The actual order of the files might depend on the translation of the
379 // result of KFileItem::mimeComment() in the user's language.
380 QStringList version1
;
381 version1
<< "b.jpg" << "a.txt" << "c.txt";
383 QStringList version2
;
384 version2
<< "a.txt" << "c.txt" << "b.jpg";
386 const bool ok1
= (itemsInModel() == version1
);
387 const bool ok2
= (itemsInModel() == version2
);
392 void KFileItemModelTest::testResortAfterChangingName()
394 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
395 QSignalSpy
itemsMovedSpy(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
396 QVERIFY(itemsMovedSpy
.isValid());
398 // We sort by size in a directory where all files have the same size.
399 // Therefore, the files are sorted by their names.
400 m_model
->setSortRole("size");
402 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
404 m_model
->loadDirectory(m_testDir
->url());
405 QVERIFY(itemsInsertedSpy
.wait());
406 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
408 // We rename a.txt to d.txt. Even though the size has not changed at all,
409 // the model must re-sort the items.
410 QHash
<QByteArray
, QVariant
> data
;
411 data
.insert("text", "d.txt");
412 m_model
->setData(0, data
);
414 QVERIFY(itemsMovedSpy
.wait());
415 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
417 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
418 const KFileItem fileItemD
= m_model
->fileItem(2);
419 KFileItem fileItemA
= fileItemD
;
420 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
421 urlA
.setPath(urlA
.path() + "a.txt");
422 fileItemA
.setUrl(urlA
);
424 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
426 QVERIFY(itemsMovedSpy
.wait());
427 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
430 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
432 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
434 // KFileItemModel prevents that inserting a punch of items sequentially
435 // results in an itemsInserted()-signal for each item. Instead internally
436 // a timeout is given that collects such operations and results in only
437 // one itemsInserted()-signal. However in this test we want to stress
438 // KFileItemModel to do a lot of insert operation and hence decrease
439 // the timeout to 1 millisecond.
440 m_testDir
->createFile("1");
441 m_model
->loadDirectory(m_testDir
->url());
442 QVERIFY(itemsInsertedSpy
.wait());
443 QCOMPARE(m_model
->count(), 1);
445 // Insert 10 items for 20 times. After each insert operation the model consistency
447 QSet
<int> insertedItems
;
448 for (int i
= 0; i
< 20; ++i
) {
449 itemsInsertedSpy
.clear();
451 for (int j
= 0; j
< 10; ++j
) {
452 int itemName
= qrand();
453 while (insertedItems
.contains(itemName
)) {
456 insertedItems
.insert(itemName
);
458 m_testDir
->createFile(QString::number(itemName
));
461 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
462 if (itemsInsertedSpy
.count() == 0) {
463 QVERIFY(itemsInsertedSpy
.wait());
466 QVERIFY(m_model
->isConsistent());
469 QCOMPARE(m_model
->count(), 201);
472 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
474 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
476 m_testDir
->createFiles({"B", "E", "G"});
478 // Due to inserting the 3 items one item-range with index == 0 and
479 // count == 3 must be given
480 m_model
->loadDirectory(m_testDir
->url());
481 QVERIFY(itemsInsertedSpy
.wait());
483 QCOMPARE(itemsInsertedSpy
.count(), 1);
484 QList
<QVariant
> arguments
= itemsInsertedSpy
.takeFirst();
485 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
486 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
488 // The indexes of the item-ranges must always be related to the model before
489 // the items have been inserted. Having:
492 // and inserting A, C, D, F the resulting model will be:
495 // and the item-ranges must be:
496 // index: 0, count: 1 for A
497 // index: 1, count: 2 for B, C
498 // index: 2, count: 1 for G
500 m_testDir
->createFiles({"A", "C", "D", "F"});
502 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
503 QVERIFY(itemsInsertedSpy
.wait());
505 QCOMPARE(itemsInsertedSpy
.count(), 1);
506 arguments
= itemsInsertedSpy
.takeFirst();
507 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
508 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
511 void KFileItemModelTest::testExpandItems()
513 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
514 QVERIFY(itemsInsertedSpy
.isValid());
515 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
516 QVERIFY(itemsRemovedSpy
.isValid());
517 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
518 QVERIFY(loadingCompletedSpy
.isValid());
520 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
521 // Besides testing the basic item expansion functionality, the test makes sure that
522 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
523 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
524 // first three characters.
525 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
526 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
527 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
528 m_model
->setRoles(modelRoles
);
530 m_testDir
->createFiles({"a/a/1", "a/a-1/1"});
532 // Store the URLs of all folders in a set.
533 QSet
<QUrl
> allFolders
;
534 allFolders
<< QUrl::fromLocalFile(m_testDir
->path() + "/a")
535 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a")
536 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a-1");
538 m_model
->loadDirectory(m_testDir
->url());
539 QVERIFY(itemsInsertedSpy
.wait());
541 // So far, the model contains only "a/"
542 QCOMPARE(m_model
->count(), 1);
543 QVERIFY(m_model
->isExpandable(0));
544 QVERIFY(!m_model
->isExpanded(0));
545 QVERIFY(m_model
->expandedDirectories().empty());
547 QCOMPARE(itemsInsertedSpy
.count(), 1);
548 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
549 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
551 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
552 m_model
->setExpanded(0, true);
553 QVERIFY(m_model
->isExpanded(0));
554 QVERIFY(itemsInsertedSpy
.wait());
555 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
556 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a"));
558 QCOMPARE(itemsInsertedSpy
.count(), 1);
559 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
560 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
562 QVERIFY(m_model
->isExpandable(1));
563 QVERIFY(!m_model
->isExpanded(1));
564 QVERIFY(m_model
->isExpandable(2));
565 QVERIFY(!m_model
->isExpanded(2));
567 // Expand the folder "a/a/" -> "a/a/1" becomes visible
568 m_model
->setExpanded(1, true);
569 QVERIFY(m_model
->isExpanded(1));
570 QVERIFY(itemsInsertedSpy
.wait());
571 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
572 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->path() + "/a")
573 << QUrl::fromLocalFile(m_testDir
->path() + "/a/a"));
575 QCOMPARE(itemsInsertedSpy
.count(), 1);
576 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
577 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
579 QVERIFY(!m_model
->isExpandable(2));
580 QVERIFY(!m_model
->isExpanded(2));
582 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
583 m_model
->setExpanded(3, true);
584 QVERIFY(m_model
->isExpanded(3));
585 QVERIFY(itemsInsertedSpy
.wait());
586 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
587 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
589 QCOMPARE(itemsInsertedSpy
.count(), 1);
590 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
591 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
593 QVERIFY(!m_model
->isExpandable(4));
594 QVERIFY(!m_model
->isExpanded(4));
596 // Collapse the top-level folder -> all other items should disappear
597 m_model
->setExpanded(0, false);
598 QVERIFY(!m_model
->isExpanded(0));
599 QCOMPARE(m_model
->count(), 1);
600 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a"))); // TODO: Make sure that child URLs are also removed
602 QCOMPARE(itemsRemovedSpy
.count(), 1);
603 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
604 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
605 QVERIFY(m_model
->isConsistent());
607 // Clear the model, reload the folder and try to restore the expanded folders.
609 QCOMPARE(m_model
->count(), 0);
610 QVERIFY(m_model
->expandedDirectories().empty());
612 m_model
->loadDirectory(m_testDir
->url());
613 m_model
->restoreExpandedDirectories(allFolders
);
614 QVERIFY(loadingCompletedSpy
.wait());
615 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
616 QVERIFY(m_model
->isExpanded(0));
617 QVERIFY(m_model
->isExpanded(1));
618 QVERIFY(!m_model
->isExpanded(2));
619 QVERIFY(m_model
->isExpanded(3));
620 QVERIFY(!m_model
->isExpanded(4));
621 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
622 QVERIFY(m_model
->isConsistent());
624 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
625 // This is how DolphinView restores the expanded folders when navigating in history.
626 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->path() + "/a/a/"));
627 QVERIFY(loadingCompletedSpy
.wait());
628 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
629 m_model
->restoreExpandedDirectories(allFolders
);
630 m_model
->loadDirectory(m_testDir
->url());
631 QVERIFY(loadingCompletedSpy
.wait());
632 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
633 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
635 // Remove all expanded items by changing the roles
636 itemsRemovedSpy
.clear();
637 m_model
->setRoles(originalModelRoles
);
638 QVERIFY(!m_model
->isExpanded(0));
639 QCOMPARE(m_model
->count(), 1);
640 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->path() + "/a")));
642 QCOMPARE(itemsRemovedSpy
.count(), 1);
643 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
644 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
645 QVERIFY(m_model
->isConsistent());
648 void KFileItemModelTest::testExpandParentItems()
650 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
651 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
652 QVERIFY(loadingCompletedSpy
.isValid());
654 // Create a tree structure of folders:
662 QSet
<QByteArray
> modelRoles
= m_model
->roles();
663 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
664 m_model
->setRoles(modelRoles
);
666 m_testDir
->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
668 m_model
->loadDirectory(m_testDir
->url());
669 QVERIFY(itemsInsertedSpy
.wait());
671 // So far, the model contains only "a 1/" and "a2/".
672 QCOMPARE(m_model
->count(), 2);
673 QVERIFY(m_model
->expandedDirectories().empty());
675 // Expand the parents of "a2/b2/c2".
676 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a2/b2/c2"));
677 QVERIFY(loadingCompletedSpy
.wait());
679 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
680 // It's important that only the parents of "a1/b1/c1" are expanded.
681 QCOMPARE(m_model
->count(), 4);
682 QVERIFY(!m_model
->isExpanded(0));
683 QVERIFY(m_model
->isExpanded(1));
684 QVERIFY(m_model
->isExpanded(2));
685 QVERIFY(!m_model
->isExpanded(3));
687 // Expand the parents of "a 1/b1".
688 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->path() + "a 1/b1"));
689 QVERIFY(loadingCompletedSpy
.wait());
691 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
692 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
693 QCOMPARE(m_model
->count(), 5);
694 QVERIFY(m_model
->isExpanded(0));
695 QVERIFY(!m_model
->isExpanded(1));
696 QVERIFY(m_model
->isExpanded(2));
697 QVERIFY(m_model
->isExpanded(3));
698 QVERIFY(!m_model
->isExpanded(4));
699 QVERIFY(m_model
->isConsistent());
702 m_model
->setExpanded(1, true);
703 QVERIFY(loadingCompletedSpy
.wait());
704 QCOMPARE(m_model
->count(), 6);
705 QVERIFY(m_model
->isExpanded(0));
706 QVERIFY(m_model
->isExpanded(1));
707 QVERIFY(!m_model
->isExpanded(2));
708 QVERIFY(m_model
->isExpanded(3));
709 QVERIFY(m_model
->isExpanded(4));
710 QVERIFY(!m_model
->isExpanded(5));
711 QVERIFY(m_model
->isConsistent());
713 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
714 m_model
->setExpanded(1, false);
715 QCOMPARE(m_model
->count(), 5);
716 QVERIFY(m_model
->isExpanded(0));
717 QVERIFY(!m_model
->isExpanded(1));
718 QVERIFY(m_model
->isExpanded(2));
719 QVERIFY(m_model
->isExpanded(3));
720 QVERIFY(!m_model
->isExpanded(4));
721 QVERIFY(m_model
->isConsistent());
725 * Renaming an expanded folder by prepending its name with a dot makes it
726 * hidden. Verify that this does not cause an inconsistent model state and
727 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
729 void KFileItemModelTest::testMakeExpandedItemHidden()
731 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
732 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
734 QSet
<QByteArray
> modelRoles
= m_model
->roles();
735 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
736 m_model
->setRoles(modelRoles
);
738 m_testDir
->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
740 m_model
->loadDirectory(m_testDir
->url());
741 QVERIFY(itemsInsertedSpy
.wait());
743 // So far, the model contains only "1a/" and "1b".
744 QCOMPARE(m_model
->count(), 2);
745 m_model
->setExpanded(0, true);
746 QVERIFY(itemsInsertedSpy
.wait());
748 // Now "1a/2a" and "1a/2b" have appeared.
749 QCOMPARE(m_model
->count(), 4);
750 m_model
->setExpanded(1, true);
751 QVERIFY(itemsInsertedSpy
.wait());
752 QCOMPARE(m_model
->count(), 6);
754 // Rename "1a/2" and make it hidden.
755 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
756 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
758 KIO::SimpleJob
* job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
759 bool ok
= job
->exec();
761 QVERIFY(itemsRemovedSpy
.wait());
763 // "1a/2" and its subfolders have disappeared now.
764 QVERIFY(m_model
->isConsistent());
765 QCOMPARE(m_model
->count(), 3);
767 m_model
->setExpanded(0, false);
768 QCOMPARE(m_model
->count(), 2);
772 void KFileItemModelTest::testRemoveFilteredExpandedItems()
774 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
776 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
777 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
778 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
779 m_model
->setRoles(modelRoles
);
781 m_testDir
->createFiles({"folder/child", "file"});
783 m_model
->loadDirectory(m_testDir
->url());
784 QVERIFY(itemsInsertedSpy
.wait());
786 // So far, the model contains only "folder/" and "file".
787 QCOMPARE(m_model
->count(), 2);
788 QVERIFY(m_model
->isExpandable(0));
789 QVERIFY(!m_model
->isExpandable(1));
790 QVERIFY(!m_model
->isExpanded(0));
791 QVERIFY(!m_model
->isExpanded(1));
792 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
794 // Expand "folder" -> "folder/child" becomes visible.
795 m_model
->setExpanded(0, true);
796 QVERIFY(m_model
->isExpanded(0));
797 QVERIFY(itemsInsertedSpy
.wait());
798 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
800 // Add a name filter.
801 m_model
->setNameFilter("f");
802 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
804 m_model
->setNameFilter("fo");
805 QCOMPARE(itemsInModel(), QStringList() << "folder");
807 // Remove all expanded items by changing the roles
808 m_model
->setRoles(originalModelRoles
);
809 QVERIFY(!m_model
->isExpanded(0));
810 QCOMPARE(itemsInModel(), QStringList() << "folder");
812 // Remove the name filter and verify that "folder/child" does not reappear.
813 m_model
->setNameFilter(QString());
814 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
817 void KFileItemModelTest::testSorting()
819 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
820 QSignalSpy
itemsMovedSpy(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
821 QVERIFY(itemsMovedSpy
.isValid());
823 // Create some files with different sizes and modification times to check the different sorting options
824 QDateTime now
= QDateTime::currentDateTime();
826 QSet
<QByteArray
> roles
;
827 roles
.insert("text");
828 roles
.insert("isExpanded");
829 roles
.insert("isExpandable");
830 roles
.insert("expandedParentsCount");
831 m_model
->setRoles(roles
);
833 m_testDir
->createDir("c/c-2");
834 m_testDir
->createFile("c/c-2/c-3");
835 m_testDir
->createFile("c/c-1");
837 m_testDir
->createFile("a", "A file", now
.addDays(-3));
838 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
839 m_testDir
->createDir("c", now
.addDays(-2));
840 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
841 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
842 m_testDir
->createFile(".f");
844 m_model
->loadDirectory(m_testDir
->url());
845 QVERIFY(itemsInsertedSpy
.wait());
847 int index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c"));
848 m_model
->setExpanded(index
, true);
849 QVERIFY(itemsInsertedSpy
.wait());
851 index
= m_model
->index(QUrl(m_testDir
->url().url() + "/c/c-2"));
852 m_model
->setExpanded(index
, true);
853 QVERIFY(itemsInsertedSpy
.wait());
855 // Default: Sort by Name, ascending
856 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
857 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
858 QVERIFY(m_model
->sortDirectoriesFirst());
859 QVERIFY(!m_model
->showHiddenFiles());
860 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
862 // Sort by Name, ascending, 'Sort Folders First' disabled
863 m_model
->setSortDirectoriesFirst(false);
864 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
865 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
866 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
867 QCOMPARE(itemsMovedSpy
.count(), 1);
868 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
869 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
871 // Sort by Name, descending
872 m_model
->setSortDirectoriesFirst(true);
873 m_model
->setSortOrder(Qt::DescendingOrder
);
874 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
875 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
876 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
877 QCOMPARE(itemsMovedSpy
.count(), 2);
878 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
879 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
880 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
881 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
883 // Sort by Date, descending
884 m_model
->setSortDirectoriesFirst(true);
885 m_model
->setSortRole("modificationtime");
886 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
887 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
888 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
889 QCOMPARE(itemsMovedSpy
.count(), 1);
890 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
891 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
893 // Sort by Date, ascending
894 m_model
->setSortOrder(Qt::AscendingOrder
);
895 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
896 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
897 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
898 QCOMPARE(itemsMovedSpy
.count(), 1);
899 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
900 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
902 // Sort by Date, ascending, 'Sort Folders First' disabled
903 m_model
->setSortDirectoriesFirst(false);
904 QCOMPARE(m_model
->sortRole(), QByteArray("modificationtime"));
905 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
906 QVERIFY(!m_model
->sortDirectoriesFirst());
907 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
908 QCOMPARE(itemsMovedSpy
.count(), 1);
909 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
910 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
912 // Sort by Name, ascending, 'Sort Folders First' disabled
913 m_model
->setSortRole("text");
914 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
915 QVERIFY(!m_model
->sortDirectoriesFirst());
916 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
917 QCOMPARE(itemsMovedSpy
.count(), 1);
918 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
919 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
921 // Sort by Size, ascending, 'Sort Folders First' disabled
922 m_model
->setSortRole("size");
923 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
924 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
925 QVERIFY(!m_model
->sortDirectoriesFirst());
926 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
927 QCOMPARE(itemsMovedSpy
.count(), 1);
928 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
929 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
931 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
932 m_model
->setSortDirectoriesFirst(true);
933 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
934 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
935 QVERIFY(m_model
->sortDirectoriesFirst());
936 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
937 QCOMPARE(itemsMovedSpy
.count(), 0);
939 // Sort by Size, descending, 'Sort Folders First' enabled
940 m_model
->setSortOrder(Qt::DescendingOrder
);
941 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
942 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
943 QVERIFY(m_model
->sortDirectoriesFirst());
944 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
945 QCOMPARE(itemsMovedSpy
.count(), 1);
946 QCOMPARE(itemsMovedSpy
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
947 QCOMPARE(itemsMovedSpy
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
949 // TODO: Sort by other roles; show/hide hidden files
952 void KFileItemModelTest::testIndexForKeyboardSearch()
954 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
956 m_testDir
->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
958 m_model
->loadDirectory(m_testDir
->url());
959 QVERIFY(itemsInsertedSpy
.wait());
961 // Search from index 0
962 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
963 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
964 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
965 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
966 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
967 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
968 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
969 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
970 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
971 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
972 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
974 // Start a search somewhere in the middle
975 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
976 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
977 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
978 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
980 // Test searches that go past the last item back to index 0
981 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
982 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
983 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
984 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
986 // Test searches that yield no result
987 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
988 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
989 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
990 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
991 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
993 // Test upper case searches (note that search is case insensitive)
994 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
995 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
996 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
997 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
999 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1002 void KFileItemModelTest::testNameFilter()
1004 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1006 m_testDir
->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1008 m_model
->loadDirectory(m_testDir
->url());
1009 QVERIFY(itemsInsertedSpy
.wait());
1011 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
1012 QCOMPARE(m_model
->count(), 3);
1014 m_model
->setNameFilter("A2"); // Shows only A2
1015 QCOMPARE(m_model
->count(), 1);
1017 m_model
->setNameFilter("A2"); // Shows only A1
1018 QCOMPARE(m_model
->count(), 1);
1020 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1021 QCOMPARE(m_model
->count(), 2);
1023 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1024 QCOMPARE(m_model
->count(), 2);
1026 m_model
->setNameFilter(QString()); // Shows again all items
1027 QCOMPARE(m_model
->count(), 5);
1031 * Verifies that we do not crash when adding a KFileItem with an empty path.
1032 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1033 * tried to always read the first character of the path, even if the path is empty.
1035 void KFileItemModelTest::testEmptyPath()
1037 QSet
<QByteArray
> roles
;
1038 roles
.insert("text");
1039 roles
.insert("isExpanded");
1040 roles
.insert("isExpandable");
1041 roles
.insert("expandedParentsCount");
1042 m_model
->setRoles(roles
);
1044 const QUrl emptyUrl
;
1045 QVERIFY(emptyUrl
.path().isEmpty());
1047 const QUrl
url("file:///test/");
1049 KFileItemList items
;
1050 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1051 m_model
->slotItemsAdded(emptyUrl
, items
);
1052 m_model
->slotCompleted();
1056 * Verifies that the 'isExpanded' state of folders does not change when the
1057 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1059 void KFileItemModelTest::testRefreshExpandedItem()
1061 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1062 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
, QSet
<QByteArray
>)));
1063 QVERIFY(itemsChangedSpy
.isValid());
1065 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1066 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1067 m_model
->setRoles(modelRoles
);
1069 m_testDir
->createFiles({"a/1", "a/2", "3", "4"});
1071 m_model
->loadDirectory(m_testDir
->url());
1072 QVERIFY(itemsInsertedSpy
.wait());
1073 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1075 m_model
->setExpanded(0, true);
1076 QVERIFY(itemsInsertedSpy
.wait());
1077 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1078 QVERIFY(m_model
->isExpanded(0));
1080 const KFileItem item
= m_model
->fileItem(0);
1081 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1082 QVERIFY(!itemsChangedSpy
.isEmpty());
1084 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1085 QVERIFY(m_model
->isExpanded(0));
1089 * Verify that removing hidden files and folders from the model does not
1090 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1092 void KFileItemModelTest::testRemoveHiddenItems()
1094 m_testDir
->createDir(".a");
1095 m_testDir
->createDir(".b");
1096 m_testDir
->createDir("c");
1097 m_testDir
->createDir("d");
1098 m_testDir
->createFiles({".f", ".g", "h", "i"});
1100 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1101 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1103 m_model
->setShowHiddenFiles(true);
1104 m_model
->loadDirectory(m_testDir
->url());
1105 QVERIFY(itemsInsertedSpy
.wait());
1106 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1107 QCOMPARE(itemsInsertedSpy
.count(), 1);
1108 QCOMPARE(itemsRemovedSpy
.count(), 0);
1109 KItemRangeList itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1110 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1112 m_model
->setShowHiddenFiles(false);
1113 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1114 QCOMPARE(itemsInsertedSpy
.count(), 0);
1115 QCOMPARE(itemsRemovedSpy
.count(), 1);
1116 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1117 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1119 m_model
->setShowHiddenFiles(true);
1120 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1121 QCOMPARE(itemsInsertedSpy
.count(), 1);
1122 QCOMPARE(itemsRemovedSpy
.count(), 0);
1123 itemRangeList
= itemsInsertedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1124 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1127 QCOMPARE(itemsInModel(), QStringList());
1128 QCOMPARE(itemsInsertedSpy
.count(), 0);
1129 QCOMPARE(itemsRemovedSpy
.count(), 1);
1130 itemRangeList
= itemsRemovedSpy
.takeFirst().at(0).value
<KItemRangeList
>();
1131 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1133 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1134 // Verify that this does not make the model crash.
1135 m_model
->setShowHiddenFiles(false);
1139 * Verify that filtered items are removed when their parent is collapsed.
1141 void KFileItemModelTest::collapseParentOfHiddenItems()
1143 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1144 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1146 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1147 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1148 m_model
->setRoles(modelRoles
);
1150 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1152 m_model
->loadDirectory(m_testDir
->url());
1153 QVERIFY(itemsInsertedSpy
.wait());
1154 QCOMPARE(m_model
->count(), 1); // Only "a/"
1157 m_model
->setExpanded(0, true);
1158 QVERIFY(itemsInsertedSpy
.wait());
1159 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1162 m_model
->setExpanded(1, true);
1163 QVERIFY(itemsInsertedSpy
.wait());
1164 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1167 m_model
->setExpanded(2, true);
1168 QVERIFY(itemsInsertedSpy
.wait());
1169 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"
1171 // Set a name filter that matches nothing -> only the expanded folders remain.
1172 m_model
->setNameFilter("xyz");
1173 QCOMPARE(itemsRemovedSpy
.count(), 1);
1174 QCOMPARE(m_model
->count(), 3);
1175 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1177 // Collapse the folder "a/".
1178 m_model
->setExpanded(0, false);
1179 QCOMPARE(itemsRemovedSpy
.count(), 2);
1180 QCOMPARE(m_model
->count(), 1);
1181 QCOMPARE(itemsInModel(), QStringList() << "a");
1183 // Remove the filter -> no files should appear (and we should not get a crash).
1184 m_model
->setNameFilter(QString());
1185 QCOMPARE(m_model
->count(), 1);
1189 * Verify that filtered items are removed when their parent is deleted.
1191 void KFileItemModelTest::removeParentOfHiddenItems()
1193 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1194 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1196 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1197 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1198 m_model
->setRoles(modelRoles
);
1200 m_testDir
->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1202 m_model
->loadDirectory(m_testDir
->url());
1203 QVERIFY(itemsInsertedSpy
.wait());
1204 QCOMPARE(m_model
->count(), 1); // Only "a/"
1207 m_model
->setExpanded(0, true);
1208 QVERIFY(itemsInsertedSpy
.wait());
1209 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1212 m_model
->setExpanded(1, true);
1213 QVERIFY(itemsInsertedSpy
.wait());
1214 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1217 m_model
->setExpanded(2, true);
1218 QVERIFY(itemsInsertedSpy
.wait());
1219 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"
1221 // Set a name filter that matches nothing -> only the expanded folders remain.
1222 m_model
->setNameFilter("xyz");
1223 QCOMPARE(itemsRemovedSpy
.count(), 1);
1224 QCOMPARE(m_model
->count(), 3);
1225 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1227 // Simulate the deletion of the directory "a/b/".
1228 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1229 QCOMPARE(itemsRemovedSpy
.count(), 2);
1230 QCOMPARE(m_model
->count(), 1);
1231 QCOMPARE(itemsInModel(), QStringList() << "a");
1233 // Remove the filter -> only the file "a/1" should appear.
1234 m_model
->setNameFilter(QString());
1235 QCOMPARE(m_model
->count(), 2);
1236 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1240 * Create a tree structure where parent-child relationships can not be
1241 * determined by parsing the URLs, and verify that KFileItemModel
1242 * handles them correctly.
1244 void KFileItemModelTest::testGeneralParentChildRelationships()
1246 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1247 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1249 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1250 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1251 m_model
->setRoles(modelRoles
);
1253 m_testDir
->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1255 m_model
->loadDirectory(m_testDir
->url());
1256 QVERIFY(itemsInsertedSpy
.wait());
1257 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1259 // Expand all folders.
1260 m_model
->setExpanded(0, true);
1261 QVERIFY(itemsInsertedSpy
.wait());
1262 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1264 m_model
->setExpanded(1, true);
1265 QVERIFY(itemsInsertedSpy
.wait());
1266 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1268 m_model
->setExpanded(3, true);
1269 QVERIFY(itemsInsertedSpy
.wait());
1270 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1272 m_model
->setExpanded(4, true);
1273 QVERIFY(itemsInsertedSpy
.wait());
1274 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1276 // Add some more children and grand-children.
1277 const QUrl parent1
= m_model
->fileItem(0).url();
1278 const QUrl parent2
= m_model
->fileItem(3).url();
1279 const QUrl realChild1
= m_model
->fileItem(1).url();
1280 const QUrl realChild2
= m_model
->fileItem(4).url();
1282 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1283 m_model
->slotCompleted();
1284 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1286 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1287 m_model
->slotCompleted();
1288 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1290 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1291 m_model
->slotCompleted();
1292 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1294 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1295 m_model
->slotCompleted();
1296 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1298 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1299 m_model
->slotCompleted();
1300 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1302 // Set a name filter that matches nothing -> only expanded folders remain.
1303 m_model
->setNameFilter("xyz");
1304 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1305 QCOMPARE(itemsRemovedSpy
.count(), 1);
1306 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1307 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1308 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1310 // Collapse "parent1".
1311 m_model
->setExpanded(0, false);
1312 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1313 QCOMPARE(itemsRemovedSpy
.count(), 1);
1314 arguments
= itemsRemovedSpy
.takeFirst();
1315 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1316 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1318 // Remove "parent2".
1319 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1320 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1321 QCOMPARE(itemsRemovedSpy
.count(), 1);
1322 arguments
= itemsRemovedSpy
.takeFirst();
1323 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1324 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1326 // Clear filter, verify that no items reappear.
1327 m_model
->setNameFilter(QString());
1328 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1331 void KFileItemModelTest::testNameRoleGroups()
1333 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1334 QSignalSpy
itemsMovedSpy(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
1335 QVERIFY(itemsMovedSpy
.isValid());
1336 QSignalSpy
groupsChangedSpy(m_model
, SIGNAL(groupsChanged()));
1337 QVERIFY(groupsChangedSpy
.isValid());
1339 m_testDir
->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1341 m_model
->setGroupedSorting(true);
1342 m_model
->loadDirectory(m_testDir
->url());
1343 QVERIFY(itemsInsertedSpy
.wait());
1344 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1346 QList
<QPair
<int, QVariant
> > expectedGroups
;
1347 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1348 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1349 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1350 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1351 QCOMPARE(m_model
->groups(), expectedGroups
);
1353 // Rename d.txt to a.txt.
1354 QHash
<QByteArray
, QVariant
> data
;
1355 data
.insert("text", "a.txt");
1356 m_model
->setData(2, data
);
1357 QVERIFY(itemsMovedSpy
.wait());
1358 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1360 expectedGroups
.clear();
1361 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1362 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1363 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1364 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1365 QCOMPARE(m_model
->groups(), expectedGroups
);
1367 // Rename c.txt to d.txt.
1368 data
.insert("text", "d.txt");
1369 m_model
->setData(2, data
);
1370 QVERIFY(groupsChangedSpy
.wait());
1371 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1373 expectedGroups
.clear();
1374 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1375 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1376 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1377 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1378 QCOMPARE(m_model
->groups(), expectedGroups
);
1380 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1381 const KFileItem fileItemD
= m_model
->fileItem(2);
1382 KFileItem fileItemC
= fileItemD
;
1383 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1384 urlC
.setPath(urlC
.path() + "c.txt");
1385 fileItemC
.setUrl(urlC
);
1387 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1388 QVERIFY(groupsChangedSpy
.wait());
1389 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1391 expectedGroups
.clear();
1392 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1393 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1394 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1395 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1396 QCOMPARE(m_model
->groups(), expectedGroups
);
1399 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1401 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1403 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1404 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1405 m_model
->setRoles(modelRoles
);
1407 m_testDir
->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1409 m_model
->setGroupedSorting(true);
1410 m_model
->loadDirectory(m_testDir
->url());
1411 QVERIFY(itemsInsertedSpy
.wait());
1412 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1414 QList
<QPair
<int, QVariant
> > expectedGroups
;
1415 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1416 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1417 QCOMPARE(m_model
->groups(), expectedGroups
);
1419 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1420 expectedGroups
.clear();
1421 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1422 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1424 m_model
->setExpanded(0, true);
1425 QVERIFY(m_model
->isExpanded(0));
1426 QVERIFY(itemsInsertedSpy
.wait());
1427 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1428 QCOMPARE(m_model
->groups(), expectedGroups
);
1430 m_model
->setExpanded(3, true);
1431 QVERIFY(m_model
->isExpanded(3));
1432 QVERIFY(itemsInsertedSpy
.wait());
1433 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1434 QCOMPARE(m_model
->groups(), expectedGroups
);
1437 void KFileItemModelTest::testInconsistentModel()
1439 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1441 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1442 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1443 m_model
->setRoles(modelRoles
);
1445 m_testDir
->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1447 m_model
->loadDirectory(m_testDir
->url());
1448 QVERIFY(itemsInsertedSpy
.wait());
1449 QCOMPARE(itemsInModel(), QStringList() << "a");
1451 // Expand "a/" and "a/b/".
1452 m_model
->setExpanded(0, true);
1453 QVERIFY(m_model
->isExpanded(0));
1454 QVERIFY(itemsInsertedSpy
.wait());
1455 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1457 m_model
->setExpanded(1, true);
1458 QVERIFY(m_model
->isExpanded(1));
1459 QVERIFY(itemsInsertedSpy
.wait());
1460 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1462 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1463 // Such a thing can in principle happen when performing a search, and there
1465 // (a) match the search string, and
1466 // (b) are children of a folder that matches the search string and is expanded.
1468 // Note that the first item in the list of added items must be new (i.e., not
1469 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1470 // it receives items that are in the model already and ignore them.
1471 QUrl
url(m_model
->directory().url() + "/a2");
1472 KFileItem
newItem(url
);
1474 KFileItemList items
;
1475 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1476 m_model
->slotItemsAdded(m_model
->directory(), items
);
1477 m_model
->slotCompleted();
1478 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1480 m_model
->setExpanded(0, false);
1482 // Test that the right items have been removed, see
1483 // https://bugs.kde.org/show_bug.cgi?id=324371
1484 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1486 // Test that resorting does not cause a crash, see
1487 // https://bugs.kde.org/show_bug.cgi?id=325359
1488 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1489 m_model
->resortAllItems();
1493 void KFileItemModelTest::testChangeRolesForFilteredItems()
1495 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1497 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1498 modelRoles
<< "owner";
1499 m_model
->setRoles(modelRoles
);
1501 m_testDir
->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1503 m_model
->loadDirectory(m_testDir
->url());
1504 QVERIFY(itemsInsertedSpy
.wait());
1505 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1507 for (int index
= 0; index
< m_model
->count(); ++index
) {
1508 // All items should have the "text" and "owner" roles, but not "group".
1509 QVERIFY(m_model
->data(index
).contains("text"));
1510 QVERIFY(m_model
->data(index
).contains("owner"));
1511 QVERIFY(!m_model
->data(index
).contains("group"));
1514 // Add a filter, such that only "aaa.txt" remains in the model.
1515 m_model
->setNameFilter("aaa");
1516 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1518 // Add the "group" role.
1519 modelRoles
<< "group";
1520 m_model
->setRoles(modelRoles
);
1522 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1523 m_model
->setNameFilter("aa");
1524 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1526 for (int index
= 0; index
< m_model
->count(); ++index
) {
1527 // All items should have the "text", "owner", and "group" roles.
1528 QVERIFY(m_model
->data(index
).contains("text"));
1529 QVERIFY(m_model
->data(index
).contains("owner"));
1530 QVERIFY(m_model
->data(index
).contains("group"));
1533 // Remove the "owner" role.
1534 modelRoles
.remove("owner");
1535 m_model
->setRoles(modelRoles
);
1537 // Clear the filter, and verify that all items have the expected roles
1538 m_model
->setNameFilter(QString());
1539 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1541 for (int index
= 0; index
< m_model
->count(); ++index
) {
1542 // All items should have the "text" and "group" roles, but now "owner".
1543 QVERIFY(m_model
->data(index
).contains("text"));
1544 QVERIFY(!m_model
->data(index
).contains("owner"));
1545 QVERIFY(m_model
->data(index
).contains("group"));
1549 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1551 KFileItemList items
;
1553 KIO::UDSEntry entry
;
1554 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1555 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1556 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1557 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1558 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1559 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1561 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1562 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1563 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1565 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1566 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1567 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1569 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1570 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1571 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1573 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1574 m_model
->slotCompleted();
1576 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1579 m_model
->setNameFilter("a");
1580 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1583 m_model
->setSortRole("owner");
1585 // Clear the filter, and verify that the items are sorted correctly.
1586 m_model
->setNameFilter(QString());
1587 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1590 void KFileItemModelTest::testRefreshFilteredItems()
1592 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1594 m_testDir
->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1596 m_model
->loadDirectory(m_testDir
->url());
1597 QVERIFY(itemsInsertedSpy
.wait());
1598 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1600 const KFileItem fileItemC
= m_model
->fileItem(2);
1602 // Show only the .txt files.
1603 m_model
->setNameFilter(".txt");
1604 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1606 // Rename one of the .jpg files.
1607 KFileItem fileItemE
= fileItemC
;
1608 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1609 urlE
.setPath(urlE
.path() + "/e.jpg");
1610 fileItemE
.setUrl(urlE
);
1612 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1614 // Show all files again, and verify that the model has updated the file name.
1615 m_model
->setNameFilter(QString());
1616 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1619 void KFileItemModelTest::testCreateMimeData()
1621 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1623 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1624 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1625 m_model
->setRoles(modelRoles
);
1627 m_testDir
->createFile("a/1");
1629 m_model
->loadDirectory(m_testDir
->url());
1630 QVERIFY(itemsInsertedSpy
.wait());
1631 QCOMPARE(itemsInModel(), QStringList() << "a");
1634 m_model
->setExpanded(0, true);
1635 QVERIFY(itemsInsertedSpy
.wait());
1636 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1638 // Verify that creating the MIME data for a child of an expanded folder does
1639 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1641 selection
.insert(1);
1642 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1646 void KFileItemModelTest::testCollapseFolderWhileLoading()
1648 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1650 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1651 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1652 m_model
->setRoles(modelRoles
);
1654 m_testDir
->createFile("a2/b/c1.txt");
1656 m_model
->loadDirectory(m_testDir
->url());
1657 QVERIFY(itemsInsertedSpy
.wait());
1658 QCOMPARE(itemsInModel(), QStringList() << "a2");
1661 m_model
->setExpanded(0, true);
1662 QVERIFY(m_model
->isExpanded(0));
1663 QVERIFY(itemsInsertedSpy
.wait());
1664 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1667 m_model
->setExpanded(1, true);
1668 QVERIFY(m_model
->isExpanded(1));
1669 QVERIFY(itemsInsertedSpy
.wait());
1670 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1672 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1673 // signal is not emitted yet.
1674 const KFileItem fileItemC1
= m_model
->fileItem(2);
1675 KFileItem fileItemC2
= fileItemC1
;
1676 QUrl urlC2
= fileItemC2
.url();
1677 urlC2
= urlC2
.adjusted(QUrl::RemoveFilename
);
1678 urlC2
.setPath(urlC2
.path() + "c2.txt");
1679 fileItemC2
.setUrl(urlC2
);
1681 const QUrl urlB
= m_model
->fileItem(1).url();
1682 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1683 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1685 // Collapse "a2/". This should also remove all its (indirect) children from
1686 // the model and from the model's m_pendingItemsToInsert member.
1687 m_model
->setExpanded(0, false);
1688 QCOMPARE(itemsInModel(), QStringList() << "a2");
1690 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1691 // is still in m_pendingItemsToInsert, then we might get a crash, see
1692 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1693 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1694 // without parent in the model.
1695 m_model
->slotCompleted();
1696 QCOMPARE(itemsInModel(), QStringList() << "a2");
1698 // Expand "a2/" again.
1699 m_model
->setExpanded(0, true);
1700 QVERIFY(m_model
->isExpanded(0));
1701 QVERIFY(itemsInsertedSpy
.wait());
1702 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1704 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1705 // completed() signal is not emitted yet.
1706 const KFileItem fileItemA2
= m_model
->fileItem(0);
1707 KFileItem fileItemA1
= fileItemA2
;
1708 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1709 urlA1
.setPath(urlA1
.path() + "a1");
1710 fileItemA1
.setUrl(urlA1
);
1712 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1713 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1715 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1716 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1717 // confuse the code which collapses the folder.
1718 m_model
->setExpanded(0, false);
1719 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1720 QVERIFY(!m_model
->isExpanded(0));
1721 QVERIFY(!m_model
->isExpanded(1));
1724 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1726 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1728 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
1730 m_model
->loadDirectory(m_testDir
->url());
1731 QVERIFY(itemsInsertedSpy
.wait());
1732 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1734 const KFileItem fileItemB
= m_model
->fileItem(1);
1736 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1738 list
<< fileItemB
<< fileItemB
;
1739 m_model
->slotItemsDeleted(list
);
1741 QVERIFY(m_model
->isConsistent());
1742 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1745 QStringList
KFileItemModelTest::itemsInModel() const
1748 for (int i
= 0; i
< m_model
->count(); i
++) {
1749 items
<< m_model
->fileItem(i
).text();
1754 QTEST_MAIN(KFileItemModelTest
)
1756 #include "kfileitemmodeltest.moc"