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 <qtest_kde.h>
25 #include "kitemviews/kfileitemmodel.h"
26 #include "kitemviews/private/kfileitemmodeldirlister.h"
29 void myMessageOutput(QtMsgType type
, const char* msg
)
37 fprintf(stderr
, "Critical: %s\n", msg
);
40 fprintf(stderr
, "Fatal: %s\n", msg
);
48 const int DefaultTimeout
= 5000;
51 Q_DECLARE_METATYPE(KItemRange
)
52 Q_DECLARE_METATYPE(KItemRangeList
)
53 Q_DECLARE_METATYPE(QList
<int>)
55 class KFileItemModelTest
: public QObject
63 void testDefaultRoles();
64 void testDefaultSortRole();
65 void testDefaultGroupedSorting();
67 void testRemoveItems();
68 void testDirLoadingCompleted();
70 void testSetDataWithModifiedSortRole_data();
71 void testSetDataWithModifiedSortRole();
72 void testChangeSortRole();
73 void testResortAfterChangingName();
74 void testModelConsistencyWhenInsertingItems();
75 void testItemRangeConsistencyWhenInsertingItems();
76 void testExpandItems();
77 void testExpandParentItems();
78 void testMakeExpandedItemHidden();
79 void testRemoveFilteredExpandedItems();
81 void testIndexForKeyboardSearch();
82 void testNameFilter();
84 void testRefreshExpandedItem();
85 void testRemoveHiddenItems();
86 void collapseParentOfHiddenItems();
87 void removeParentOfHiddenItems();
88 void testGeneralParentChildRelationships();
89 void testNameRoleGroups();
90 void testNameRoleGroupsWithExpandedItems();
91 void testInconsistentModel();
92 void testChangeRolesForFilteredItems();
93 void testChangeSortRoleWhileFiltering();
94 void testRefreshFilteredItems();
95 void testCollapseFolderWhileLoading();
96 void testCreateMimeData();
97 void testDeleteFileMoreThanOnce();
100 QStringList
itemsInModel() const;
103 KFileItemModel
* m_model
;
107 void KFileItemModelTest::init()
109 // The item-model tests result in a huge number of debugging
110 // output from kdelibs. Only show critical and fatal messages.
111 qInstallMsgHandler(myMessageOutput
);
113 qRegisterMetaType
<KItemRange
>("KItemRange");
114 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
115 qRegisterMetaType
<KFileItemList
>("KFileItemList");
117 m_testDir
= new TestDir();
118 m_model
= new KFileItemModel();
119 m_model
->m_dirLister
->setAutoUpdate(false);
121 // Reduce the timer interval to make the test run faster.
122 m_model
->m_resortAllItemsTimer
->setInterval(0);
125 void KFileItemModelTest::cleanup()
134 void KFileItemModelTest::testDefaultRoles()
136 const QSet
<QByteArray
> roles
= m_model
->roles();
137 QCOMPARE(roles
.count(), 3);
138 QVERIFY(roles
.contains("text"));
139 QVERIFY(roles
.contains("isDir"));
140 QVERIFY(roles
.contains("isLink"));
143 void KFileItemModelTest::testDefaultSortRole()
145 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
148 files
<< "c.txt" << "a.txt" << "b.txt";
150 m_testDir
->createFiles(files
);
152 m_model
->loadDirectory(m_testDir
->url());
153 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
155 QCOMPARE(m_model
->count(), 3);
156 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
157 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
158 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
161 void KFileItemModelTest::testDefaultGroupedSorting()
163 QCOMPARE(m_model
->groupedSorting(), false);
166 void KFileItemModelTest::testNewItems()
169 files
<< "a.txt" << "b.txt" << "c.txt";
170 m_testDir
->createFiles(files
);
172 m_model
->loadDirectory(m_testDir
->url());
173 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
175 QCOMPARE(m_model
->count(), 3);
177 QVERIFY(m_model
->isConsistent());
180 void KFileItemModelTest::testRemoveItems()
182 m_testDir
->createFile("a.txt");
183 m_testDir
->createFile("b.txt");
184 m_model
->loadDirectory(m_testDir
->url());
185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
186 QCOMPARE(m_model
->count(), 2);
187 QVERIFY(m_model
->isConsistent());
189 m_testDir
->removeFile("a.txt");
190 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
191 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
192 QCOMPARE(m_model
->count(), 1);
193 QVERIFY(m_model
->isConsistent());
196 void KFileItemModelTest::testDirLoadingCompleted()
198 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
199 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
200 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
202 m_testDir
->createFiles({"a.txt", "b.txt", "c.txt"});
204 m_model
->loadDirectory(m_testDir
->url());
205 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
206 QCOMPARE(loadingCompletedSpy
.count(), 1);
207 QCOMPARE(itemsInsertedSpy
.count(), 1);
208 QCOMPARE(itemsRemovedSpy
.count(), 0);
209 QCOMPARE(m_model
->count(), 3);
211 m_testDir
->createFiles({"d.txt", "e.txt"});
212 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
213 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
214 QCOMPARE(loadingCompletedSpy
.count(), 2);
215 QCOMPARE(itemsInsertedSpy
.count(), 2);
216 QCOMPARE(itemsRemovedSpy
.count(), 0);
217 QCOMPARE(m_model
->count(), 5);
219 m_testDir
->removeFile("a.txt");
220 m_testDir
->createFile("f.txt");
221 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
222 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
223 QCOMPARE(loadingCompletedSpy
.count(), 3);
224 QCOMPARE(itemsInsertedSpy
.count(), 3);
225 QCOMPARE(itemsRemovedSpy
.count(), 1);
226 QCOMPARE(m_model
->count(), 5);
228 m_testDir
->removeFile("b.txt");
229 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
230 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
231 QCOMPARE(loadingCompletedSpy
.count(), 4);
232 QCOMPARE(itemsInsertedSpy
.count(), 3);
233 QCOMPARE(itemsRemovedSpy
.count(), 2);
234 QCOMPARE(m_model
->count(), 4);
236 QVERIFY(m_model
->isConsistent());
239 void KFileItemModelTest::testSetData()
241 m_testDir
->createFile("a.txt");
243 m_model
->loadDirectory(m_testDir
->url());
244 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
246 QHash
<QByteArray
, QVariant
> values
;
247 values
.insert("customRole1", "Test1");
248 values
.insert("customRole2", "Test2");
250 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
251 m_model
->setData(0, values
);
252 QCOMPARE(itemsChangedSpy
.count(), 1);
254 values
= m_model
->data(0);
255 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
256 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
257 QVERIFY(m_model
->isConsistent());
260 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
262 QTest::addColumn
<int>("changedIndex");
263 QTest::addColumn
<int>("changedRating");
264 QTest::addColumn
<bool>("expectMoveSignal");
265 QTest::addColumn
<int>("ratingIndex0");
266 QTest::addColumn
<int>("ratingIndex1");
267 QTest::addColumn
<int>("ratingIndex2");
270 // Index 0 = rating 2
271 // Index 1 = rating 4
272 // Index 2 = rating 6
274 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
275 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
276 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
278 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
279 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
280 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
283 void KFileItemModelTest::testSetDataWithModifiedSortRole()
285 QFETCH(int, changedIndex
);
286 QFETCH(int, changedRating
);
287 QFETCH(bool, expectMoveSignal
);
288 QFETCH(int, ratingIndex0
);
289 QFETCH(int, ratingIndex1
);
290 QFETCH(int, ratingIndex2
);
292 // Changing the value of a sort-role must result in
293 // a reordering of the items.
294 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
295 m_model
->setSortRole("rating");
296 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
299 files
<< "a.txt" << "b.txt" << "c.txt";
300 m_testDir
->createFiles(files
);
302 m_model
->loadDirectory(m_testDir
->url());
303 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
305 // Fill the "rating" role of each file:
310 QHash
<QByteArray
, QVariant
> ratingA
;
311 ratingA
.insert("rating", 2);
312 m_model
->setData(0, ratingA
);
314 QHash
<QByteArray
, QVariant
> ratingB
;
315 ratingB
.insert("rating", 4);
316 m_model
->setData(1, ratingB
);
318 QHash
<QByteArray
, QVariant
> ratingC
;
319 ratingC
.insert("rating", 6);
320 m_model
->setData(2, ratingC
);
322 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
323 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
324 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
326 // Now change the rating from a.txt. This usually results
327 // in reordering of the items.
328 QHash
<QByteArray
, QVariant
> rating
;
329 rating
.insert("rating", changedRating
);
330 m_model
->setData(changedIndex
, rating
);
332 if (expectMoveSignal
) {
333 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
336 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
337 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
338 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
339 QVERIFY(m_model
->isConsistent());
342 void KFileItemModelTest::testChangeSortRole()
344 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
347 files
<< "a.txt" << "b.jpg" << "c.txt";
348 m_testDir
->createFiles(files
);
350 m_model
->loadDirectory(m_testDir
->url());
351 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
352 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
354 // Simulate that KFileItemModelRolesUpdater determines the mime type.
355 // Resorting the files by 'type' will only work immediately if their
356 // mime types are known.
357 for (int index
= 0; index
< m_model
->count(); ++index
) {
358 m_model
->fileItem(index
).determineMimeType();
361 // Now: sort by type.
362 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
363 m_model
->setSortRole("type");
364 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
365 QVERIFY(!spyItemsMoved
.isEmpty());
367 // The actual order of the files might depend on the translation of the
368 // result of KFileItem::mimeComment() in the user's language.
369 QStringList version1
;
370 version1
<< "b.jpg" << "a.txt" << "c.txt";
372 QStringList version2
;
373 version2
<< "a.txt" << "c.txt" << "b.jpg";
375 const bool ok1
= (itemsInModel() == version1
);
376 const bool ok2
= (itemsInModel() == version2
);
381 void KFileItemModelTest::testResortAfterChangingName()
383 // We sort by size in a directory where all files have the same size.
384 // Therefore, the files are sorted by their names.
385 m_model
->setSortRole("size");
388 files
<< "a.txt" << "b.txt" << "c.txt";
389 m_testDir
->createFiles(files
);
391 m_model
->loadDirectory(m_testDir
->url());
392 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
393 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
395 // We rename a.txt to d.txt. Even though the size has not changed at all,
396 // the model must re-sort the items.
397 QHash
<QByteArray
, QVariant
> data
;
398 data
.insert("text", "d.txt");
399 m_model
->setData(0, data
);
401 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
402 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
404 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
405 const KFileItem fileItemD
= m_model
->fileItem(2);
406 KFileItem fileItemA
= fileItemD
;
407 QUrl urlA
= fileItemA
.url().adjusted(QUrl::RemoveFilename
);
408 urlA
.setPath(urlA
.path() + "a.txt");
409 fileItemA
.setUrl(urlA
);
411 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemA
)});
413 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
414 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
417 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
419 //QSKIP("Temporary disabled", SkipSingle);
421 // KFileItemModel prevents that inserting a punch of items sequentially
422 // results in an itemsInserted()-signal for each item. Instead internally
423 // a timeout is given that collects such operations and results in only
424 // one itemsInserted()-signal. However in this test we want to stress
425 // KFileItemModel to do a lot of insert operation and hence decrease
426 // the timeout to 1 millisecond.
427 m_testDir
->createFile("1");
428 m_model
->loadDirectory(m_testDir
->url());
429 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
430 QCOMPARE(m_model
->count(), 1);
432 // Insert 10 items for 20 times. After each insert operation the model consistency
434 QSet
<int> insertedItems
;
435 for (int i
= 0; i
< 20; ++i
) {
436 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
438 for (int j
= 0; j
< 10; ++j
) {
439 int itemName
= qrand();
440 while (insertedItems
.contains(itemName
)) {
443 insertedItems
.insert(itemName
);
445 m_testDir
->createFile(QString::number(itemName
));
448 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
449 if (spy
.count() == 0) {
450 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
453 QVERIFY(m_model
->isConsistent());
456 QCOMPARE(m_model
->count(), 201);
459 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
462 files
<< "B" << "E" << "G";
463 m_testDir
->createFiles(files
);
465 // Due to inserting the 3 items one item-range with index == 0 and
466 // count == 3 must be given
467 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
468 m_model
->loadDirectory(m_testDir
->url());
469 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
471 QCOMPARE(spy1
.count(), 1);
472 QList
<QVariant
> arguments
= spy1
.takeFirst();
473 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
474 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
476 // The indexes of the item-ranges must always be related to the model before
477 // the items have been inserted. Having:
480 // and inserting A, C, D, F the resulting model will be:
483 // and the item-ranges must be:
484 // index: 0, count: 1 for A
485 // index: 1, count: 2 for B, C
486 // index: 2, count: 1 for G
489 files
<< "A" << "C" << "D" << "F";
490 m_testDir
->createFiles(files
);
492 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
493 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
494 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
496 QCOMPARE(spy2
.count(), 1);
497 arguments
= spy2
.takeFirst();
498 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
499 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
502 void KFileItemModelTest::testExpandItems()
504 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
505 // Besides testing the basic item expansion functionality, the test makes sure that
506 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
507 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
508 // first three characters.
509 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
510 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
511 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
512 m_model
->setRoles(modelRoles
);
515 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
516 m_testDir
->createFiles(files
);
518 // Store the URLs of all folders in a set.
519 QSet
<QUrl
> allFolders
;
520 allFolders
<< QUrl::fromLocalFile(m_testDir
->name() + 'a')
521 << QUrl::fromLocalFile(m_testDir
->name() + "a/a")
522 << QUrl::fromLocalFile(m_testDir
->name() + "a/a-1");
524 m_model
->loadDirectory(m_testDir
->url());
525 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
527 // So far, the model contains only "a/"
528 QCOMPARE(m_model
->count(), 1);
529 QVERIFY(m_model
->isExpandable(0));
530 QVERIFY(!m_model
->isExpanded(0));
531 QVERIFY(m_model
->expandedDirectories().empty());
533 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
535 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
536 m_model
->setExpanded(0, true);
537 QVERIFY(m_model
->isExpanded(0));
538 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
539 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
540 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->name() + 'a'));
542 QCOMPARE(spyInserted
.count(), 1);
543 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
544 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
546 QVERIFY(m_model
->isExpandable(1));
547 QVERIFY(!m_model
->isExpanded(1));
548 QVERIFY(m_model
->isExpandable(2));
549 QVERIFY(!m_model
->isExpanded(2));
551 // Expand the folder "a/a/" -> "a/a/1" becomes visible
552 m_model
->setExpanded(1, true);
553 QVERIFY(m_model
->isExpanded(1));
554 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
555 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
556 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl::fromLocalFile(m_testDir
->name() + 'a') << QUrl::fromLocalFile(m_testDir
->name() + "a/a"));
558 QCOMPARE(spyInserted
.count(), 1);
559 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
560 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
562 QVERIFY(!m_model
->isExpandable(2));
563 QVERIFY(!m_model
->isExpanded(2));
565 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
566 m_model
->setExpanded(3, true);
567 QVERIFY(m_model
->isExpanded(3));
568 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
569 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
570 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
572 QCOMPARE(spyInserted
.count(), 1);
573 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
574 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
576 QVERIFY(!m_model
->isExpandable(4));
577 QVERIFY(!m_model
->isExpanded(4));
579 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
581 // Collapse the top-level folder -> all other items should disappear
582 m_model
->setExpanded(0, false);
583 QVERIFY(!m_model
->isExpanded(0));
584 QCOMPARE(m_model
->count(), 1);
585 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
587 QCOMPARE(spyRemoved
.count(), 1);
588 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
589 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
590 QVERIFY(m_model
->isConsistent());
592 // Clear the model, reload the folder and try to restore the expanded folders.
594 QCOMPARE(m_model
->count(), 0);
595 QVERIFY(m_model
->expandedDirectories().empty());
597 m_model
->loadDirectory(m_testDir
->url());
598 m_model
->restoreExpandedDirectories(allFolders
);
599 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
600 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
601 QVERIFY(m_model
->isExpanded(0));
602 QVERIFY(m_model
->isExpanded(1));
603 QVERIFY(!m_model
->isExpanded(2));
604 QVERIFY(m_model
->isExpanded(3));
605 QVERIFY(!m_model
->isExpanded(4));
606 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
607 QVERIFY(m_model
->isConsistent());
609 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
610 // This is how DolphinView restores the expanded folders when navigating in history.
611 m_model
->loadDirectory(QUrl::fromLocalFile(m_testDir
->name() + "a/a/"));
612 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
613 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
614 m_model
->restoreExpandedDirectories(allFolders
);
615 m_model
->loadDirectory(m_testDir
->url());
616 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
617 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
618 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
620 // Remove all expanded items by changing the roles
622 m_model
->setRoles(originalModelRoles
);
623 QVERIFY(!m_model
->isExpanded(0));
624 QCOMPARE(m_model
->count(), 1);
625 QVERIFY(!m_model
->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir
->name() + 'a')));
627 QCOMPARE(spyRemoved
.count(), 1);
628 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
629 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
630 QVERIFY(m_model
->isConsistent());
633 void KFileItemModelTest::testExpandParentItems()
635 // Create a tree structure of folders:
643 QSet
<QByteArray
> modelRoles
= m_model
->roles();
644 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
645 m_model
->setRoles(modelRoles
);
648 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
649 m_testDir
->createFiles(files
);
651 m_model
->loadDirectory(m_testDir
->url());
652 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
654 // So far, the model contains only "a 1/" and "a2/".
655 QCOMPARE(m_model
->count(), 2);
656 QVERIFY(m_model
->expandedDirectories().empty());
658 // Expand the parents of "a2/b2/c2".
659 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->name() + "a2/b2/c2"));
660 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
662 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
663 // It's important that only the parents of "a1/b1/c1" are expanded.
664 QCOMPARE(m_model
->count(), 4);
665 QVERIFY(!m_model
->isExpanded(0));
666 QVERIFY(m_model
->isExpanded(1));
667 QVERIFY(m_model
->isExpanded(2));
668 QVERIFY(!m_model
->isExpanded(3));
670 // Expand the parents of "a 1/b1".
671 m_model
->expandParentDirectories(QUrl::fromLocalFile(m_testDir
->name() + "a 1/b1"));
672 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
674 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
675 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
676 QCOMPARE(m_model
->count(), 5);
677 QVERIFY(m_model
->isExpanded(0));
678 QVERIFY(!m_model
->isExpanded(1));
679 QVERIFY(m_model
->isExpanded(2));
680 QVERIFY(m_model
->isExpanded(3));
681 QVERIFY(!m_model
->isExpanded(4));
682 QVERIFY(m_model
->isConsistent());
685 m_model
->setExpanded(1, true);
686 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
687 QCOMPARE(m_model
->count(), 6);
688 QVERIFY(m_model
->isExpanded(0));
689 QVERIFY(m_model
->isExpanded(1));
690 QVERIFY(!m_model
->isExpanded(2));
691 QVERIFY(m_model
->isExpanded(3));
692 QVERIFY(m_model
->isExpanded(4));
693 QVERIFY(!m_model
->isExpanded(5));
694 QVERIFY(m_model
->isConsistent());
696 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
697 m_model
->setExpanded(1, false);
698 QCOMPARE(m_model
->count(), 5);
699 QVERIFY(m_model
->isExpanded(0));
700 QVERIFY(!m_model
->isExpanded(1));
701 QVERIFY(m_model
->isExpanded(2));
702 QVERIFY(m_model
->isExpanded(3));
703 QVERIFY(!m_model
->isExpanded(4));
704 QVERIFY(m_model
->isConsistent());
708 * Renaming an expanded folder by prepending its name with a dot makes it
709 * hidden. Verify that this does not cause an inconsistent model state and
710 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
712 void KFileItemModelTest::testMakeExpandedItemHidden()
714 QSet
<QByteArray
> modelRoles
= m_model
->roles();
715 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
716 m_model
->setRoles(modelRoles
);
719 m_testDir
->createFile("1a/2a/3a");
720 m_testDir
->createFile("1a/2a/3b");
721 m_testDir
->createFile("1a/2b");
722 m_testDir
->createFile("1b");
724 m_model
->loadDirectory(m_testDir
->url());
725 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
727 // So far, the model contains only "1a/" and "1b".
728 QCOMPARE(m_model
->count(), 2);
729 m_model
->setExpanded(0, true);
730 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
732 // Now "1a/2a" and "1a/2b" have appeared.
733 QCOMPARE(m_model
->count(), 4);
734 m_model
->setExpanded(1, true);
735 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
736 QCOMPARE(m_model
->count(), 6);
738 // Rename "1a/2" and make it hidden.
739 const QUrl oldUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/2a");
740 const QUrl newUrl
= QUrl::fromLocalFile(m_model
->fileItem(0).url().path() + "/.2a");
742 KIO::SimpleJob
* job
= KIO::rename(oldUrl
, newUrl
, KIO::HideProgressInfo
);
743 bool ok
= job
->exec();
745 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
747 // "1a/2" and its subfolders have disappeared now.
748 QVERIFY(m_model
->isConsistent());
749 QCOMPARE(m_model
->count(), 3);
751 m_model
->setExpanded(0, false);
752 QCOMPARE(m_model
->count(), 2);
756 void KFileItemModelTest::testRemoveFilteredExpandedItems()
758 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
759 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
760 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
761 m_model
->setRoles(modelRoles
);
764 files
<< "folder/child" << "file"; // missing folders are created automatically
765 m_testDir
->createFiles(files
);
767 m_model
->loadDirectory(m_testDir
->url());
768 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
770 // So far, the model contains only "folder/" and "file".
771 QCOMPARE(m_model
->count(), 2);
772 QVERIFY(m_model
->isExpandable(0));
773 QVERIFY(!m_model
->isExpandable(1));
774 QVERIFY(!m_model
->isExpanded(0));
775 QVERIFY(!m_model
->isExpanded(1));
776 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
778 // Expand "folder" -> "folder/child" becomes visible.
779 m_model
->setExpanded(0, true);
780 QVERIFY(m_model
->isExpanded(0));
781 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
782 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
784 // Add a name filter.
785 m_model
->setNameFilter("f");
786 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
788 m_model
->setNameFilter("fo");
789 QCOMPARE(itemsInModel(), QStringList() << "folder");
791 // Remove all expanded items by changing the roles
792 m_model
->setRoles(originalModelRoles
);
793 QVERIFY(!m_model
->isExpanded(0));
794 QCOMPARE(itemsInModel(), QStringList() << "folder");
796 // Remove the name filter and verify that "folder/child" does not reappear.
797 m_model
->setNameFilter(QString());
798 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
801 void KFileItemModelTest::testSorting()
803 // Create some files with different sizes and modification times to check the different sorting options
804 QDateTime now
= QDateTime::currentDateTime();
806 QSet
<QByteArray
> roles
;
807 roles
.insert("text");
808 roles
.insert("isExpanded");
809 roles
.insert("isExpandable");
810 roles
.insert("expandedParentsCount");
811 m_model
->setRoles(roles
);
813 m_testDir
->createDir("c/c-2");
814 m_testDir
->createFile("c/c-2/c-3");
815 m_testDir
->createFile("c/c-1");
817 m_testDir
->createFile("a", "A file", now
.addDays(-3));
818 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
819 m_testDir
->createDir("c", now
.addDays(-2));
820 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
821 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
822 m_testDir
->createFile(".f");
824 m_model
->loadDirectory(m_testDir
->url());
825 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
827 int index
= m_model
->index(QUrl(m_testDir
->url().url() + 'c'));
828 m_model
->setExpanded(index
, true);
829 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
831 index
= m_model
->index(QUrl(m_testDir
->url().url() + "c/c-2"));
832 m_model
->setExpanded(index
, true);
833 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
835 // Default: Sort by Name, ascending
836 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
837 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
838 QVERIFY(m_model
->sortDirectoriesFirst());
839 QVERIFY(!m_model
->showHiddenFiles());
840 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
842 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
844 // Sort by Name, ascending, 'Sort Folders First' disabled
845 m_model
->setSortDirectoriesFirst(false);
846 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
847 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
848 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
849 QCOMPARE(spyItemsMoved
.count(), 1);
850 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
851 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
853 // Sort by Name, descending
854 m_model
->setSortDirectoriesFirst(true);
855 m_model
->setSortOrder(Qt::DescendingOrder
);
856 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
857 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
858 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
859 QCOMPARE(spyItemsMoved
.count(), 2);
860 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
861 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
862 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
863 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
865 // Sort by Date, descending
866 m_model
->setSortDirectoriesFirst(true);
867 m_model
->setSortRole("date");
868 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
869 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
870 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
871 QCOMPARE(spyItemsMoved
.count(), 1);
872 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
873 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
875 // Sort by Date, ascending
876 m_model
->setSortOrder(Qt::AscendingOrder
);
877 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
878 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
879 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
880 QCOMPARE(spyItemsMoved
.count(), 1);
881 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
882 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
884 // Sort by Date, ascending, 'Sort Folders First' disabled
885 m_model
->setSortDirectoriesFirst(false);
886 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
887 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
888 QVERIFY(!m_model
->sortDirectoriesFirst());
889 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
890 QCOMPARE(spyItemsMoved
.count(), 1);
891 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
892 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
894 // Sort by Name, ascending, 'Sort Folders First' disabled
895 m_model
->setSortRole("text");
896 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
897 QVERIFY(!m_model
->sortDirectoriesFirst());
898 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
899 QCOMPARE(spyItemsMoved
.count(), 1);
900 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
901 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
903 // Sort by Size, ascending, 'Sort Folders First' disabled
904 m_model
->setSortRole("size");
905 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
906 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
907 QVERIFY(!m_model
->sortDirectoriesFirst());
908 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
909 QCOMPARE(spyItemsMoved
.count(), 1);
910 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
911 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
913 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
914 m_model
->setSortDirectoriesFirst(true);
915 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
916 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
917 QVERIFY(m_model
->sortDirectoriesFirst());
918 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
919 QCOMPARE(spyItemsMoved
.count(), 0);
921 // Sort by Size, descending, 'Sort Folders First' enabled
922 m_model
->setSortOrder(Qt::DescendingOrder
);
923 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
924 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
925 QVERIFY(m_model
->sortDirectoriesFirst());
926 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
927 QCOMPARE(spyItemsMoved
.count(), 1);
928 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
929 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
931 // TODO: Sort by other roles; show/hide hidden files
934 void KFileItemModelTest::testIndexForKeyboardSearch()
937 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
938 m_testDir
->createFiles(files
);
940 m_model
->loadDirectory(m_testDir
->url());
941 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
943 // Search from index 0
944 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
945 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
946 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
947 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
948 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
949 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
950 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
951 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
952 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
953 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
954 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
956 // Start a search somewhere in the middle
957 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
958 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
959 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
960 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
962 // Test searches that go past the last item back to index 0
963 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
964 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
965 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
966 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
968 // Test searches that yield no result
969 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
970 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
971 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
972 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
973 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
975 // Test upper case searches (note that search is case insensitive)
976 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
977 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
978 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
979 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
981 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
984 void KFileItemModelTest::testNameFilter()
987 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
988 m_testDir
->createFiles(files
);
990 m_model
->loadDirectory(m_testDir
->url());
991 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
993 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
994 QCOMPARE(m_model
->count(), 3);
996 m_model
->setNameFilter("A2"); // Shows only A2
997 QCOMPARE(m_model
->count(), 1);
999 m_model
->setNameFilter("A2"); // Shows only A1
1000 QCOMPARE(m_model
->count(), 1);
1002 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1003 QCOMPARE(m_model
->count(), 2);
1005 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1006 QCOMPARE(m_model
->count(), 2);
1008 m_model
->setNameFilter(QString()); // Shows again all items
1009 QCOMPARE(m_model
->count(), 5);
1013 * Verifies that we do not crash when adding a KFileItem with an empty path.
1014 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1015 * tried to always read the first character of the path, even if the path is empty.
1017 void KFileItemModelTest::testEmptyPath()
1019 QSet
<QByteArray
> roles
;
1020 roles
.insert("text");
1021 roles
.insert("isExpanded");
1022 roles
.insert("isExpandable");
1023 roles
.insert("expandedParentsCount");
1024 m_model
->setRoles(roles
);
1026 const QUrl emptyUrl
;
1027 QVERIFY(emptyUrl
.path().isEmpty());
1029 const QUrl
url("file:///test/");
1031 KFileItemList items
;
1032 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1033 m_model
->slotItemsAdded(emptyUrl
, items
);
1034 m_model
->slotCompleted();
1038 * Verifies that the 'isExpanded' state of folders does not change when the
1039 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1041 void KFileItemModelTest::testRefreshExpandedItem()
1043 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1044 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1045 m_model
->setRoles(modelRoles
);
1048 files
<< "a/1" << "a/2" << "3" << "4";
1049 m_testDir
->createFiles(files
);
1051 m_model
->loadDirectory(m_testDir
->url());
1052 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1053 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1055 m_model
->setExpanded(0, true);
1056 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1057 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1058 QVERIFY(m_model
->isExpanded(0));
1060 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1062 const KFileItem item
= m_model
->fileItem(0);
1063 m_model
->slotRefreshItems({qMakePair(item
, item
)});
1064 QVERIFY(!spyItemsChanged
.isEmpty());
1066 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1067 QVERIFY(m_model
->isExpanded(0));
1071 * Verify that removing hidden files and folders from the model does not
1072 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1074 void KFileItemModelTest::testRemoveHiddenItems()
1076 m_testDir
->createDir(".a");
1077 m_testDir
->createDir(".b");
1078 m_testDir
->createDir("c");
1079 m_testDir
->createDir("d");
1080 m_testDir
->createFiles({".f", ".g", "h", "i"});
1082 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1083 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1085 m_model
->setShowHiddenFiles(true);
1086 m_model
->loadDirectory(m_testDir
->url());
1087 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1088 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1089 QCOMPARE(spyItemsInserted
.count(), 1);
1090 QCOMPARE(spyItemsRemoved
.count(), 0);
1091 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1092 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1094 m_model
->setShowHiddenFiles(false);
1095 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1096 QCOMPARE(spyItemsInserted
.count(), 0);
1097 QCOMPARE(spyItemsRemoved
.count(), 1);
1098 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1099 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1101 m_model
->setShowHiddenFiles(true);
1102 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1103 QCOMPARE(spyItemsInserted
.count(), 1);
1104 QCOMPARE(spyItemsRemoved
.count(), 0);
1105 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1106 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1109 QCOMPARE(itemsInModel(), QStringList());
1110 QCOMPARE(spyItemsInserted
.count(), 0);
1111 QCOMPARE(spyItemsRemoved
.count(), 1);
1112 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1113 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1115 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1116 // Verify that this does not make the model crash.
1117 m_model
->setShowHiddenFiles(false);
1121 * Verify that filtered items are removed when their parent is collapsed.
1123 void KFileItemModelTest::collapseParentOfHiddenItems()
1125 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1126 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1127 m_model
->setRoles(modelRoles
);
1130 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1131 m_testDir
->createFiles(files
);
1133 m_model
->loadDirectory(m_testDir
->url());
1134 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1135 QCOMPARE(m_model
->count(), 1); // Only "a/"
1138 m_model
->setExpanded(0, true);
1139 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1140 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1143 m_model
->setExpanded(1, true);
1144 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1145 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1148 m_model
->setExpanded(2, true);
1149 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1150 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"
1152 // Set a name filter that matches nothing -> only the expanded folders remain.
1153 m_model
->setNameFilter("xyz");
1154 QCOMPARE(m_model
->count(), 3);
1155 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1157 // Collapse the folder "a/".
1158 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1159 m_model
->setExpanded(0, false);
1160 QCOMPARE(spyItemsRemoved
.count(), 1);
1161 QCOMPARE(m_model
->count(), 1);
1162 QCOMPARE(itemsInModel(), QStringList() << "a");
1164 // Remove the filter -> no files should appear (and we should not get a crash).
1165 m_model
->setNameFilter(QString());
1166 QCOMPARE(m_model
->count(), 1);
1170 * Verify that filtered items are removed when their parent is deleted.
1172 void KFileItemModelTest::removeParentOfHiddenItems()
1174 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1175 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1176 m_model
->setRoles(modelRoles
);
1179 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1180 m_testDir
->createFiles(files
);
1182 m_model
->loadDirectory(m_testDir
->url());
1183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1184 QCOMPARE(m_model
->count(), 1); // Only "a/"
1187 m_model
->setExpanded(0, true);
1188 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1189 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1192 m_model
->setExpanded(1, true);
1193 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1194 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1197 m_model
->setExpanded(2, true);
1198 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1199 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"
1201 // Set a name filter that matches nothing -> only the expanded folders remain.
1202 m_model
->setNameFilter("xyz");
1203 QCOMPARE(m_model
->count(), 3);
1204 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1206 // Simulate the deletion of the directory "a/b/".
1207 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1208 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1209 QCOMPARE(spyItemsRemoved
.count(), 1);
1210 QCOMPARE(m_model
->count(), 1);
1211 QCOMPARE(itemsInModel(), QStringList() << "a");
1213 // Remove the filter -> only the file "a/1" should appear.
1214 m_model
->setNameFilter(QString());
1215 QCOMPARE(m_model
->count(), 2);
1216 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1220 * Create a tree structure where parent-child relationships can not be
1221 * determined by parsing the URLs, and verify that KFileItemModel
1222 * handles them correctly.
1224 void KFileItemModelTest::testGeneralParentChildRelationships()
1226 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1227 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1228 m_model
->setRoles(modelRoles
);
1231 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1232 m_testDir
->createFiles(files
);
1234 m_model
->loadDirectory(m_testDir
->url());
1235 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1236 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1238 // Expand all folders.
1239 m_model
->setExpanded(0, true);
1240 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1241 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1243 m_model
->setExpanded(1, true);
1244 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1245 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1247 m_model
->setExpanded(3, true);
1248 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1249 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1251 m_model
->setExpanded(4, true);
1252 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1253 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1255 // Add some more children and grand-children.
1256 const QUrl parent1
= m_model
->fileItem(0).url();
1257 const QUrl parent2
= m_model
->fileItem(3).url();
1258 const QUrl realChild1
= m_model
->fileItem(1).url();
1259 const QUrl realChild2
= m_model
->fileItem(4).url();
1261 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1262 m_model
->slotCompleted();
1263 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1265 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1266 m_model
->slotCompleted();
1267 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1269 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1270 m_model
->slotCompleted();
1271 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1273 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1274 m_model
->slotCompleted();
1275 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1277 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1278 m_model
->slotCompleted();
1279 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1281 // Set a name filter that matches nothing -> only expanded folders remain.
1282 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1283 m_model
->setNameFilter("xyz");
1284 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1285 QCOMPARE(itemsRemovedSpy
.count(), 1);
1286 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1287 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1288 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1290 // Collapse "parent1".
1291 m_model
->setExpanded(0, false);
1292 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1293 QCOMPARE(itemsRemovedSpy
.count(), 1);
1294 arguments
= itemsRemovedSpy
.takeFirst();
1295 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1296 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1298 // Remove "parent2".
1299 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1300 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1301 QCOMPARE(itemsRemovedSpy
.count(), 1);
1302 arguments
= itemsRemovedSpy
.takeFirst();
1303 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1304 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1306 // Clear filter, verify that no items reappear.
1307 m_model
->setNameFilter(QString());
1308 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1311 void KFileItemModelTest::testNameRoleGroups()
1314 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1316 m_testDir
->createFiles(files
);
1318 m_model
->setGroupedSorting(true);
1319 m_model
->loadDirectory(m_testDir
->url());
1320 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1321 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1323 QList
<QPair
<int, QVariant
> > expectedGroups
;
1324 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1325 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1326 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1327 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1328 QCOMPARE(m_model
->groups(), expectedGroups
);
1330 // Rename d.txt to a.txt.
1331 QHash
<QByteArray
, QVariant
> data
;
1332 data
.insert("text", "a.txt");
1333 m_model
->setData(2, data
);
1334 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1335 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1337 expectedGroups
.clear();
1338 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1339 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1340 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1341 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1342 QCOMPARE(m_model
->groups(), expectedGroups
);
1344 // Rename c.txt to d.txt.
1345 data
.insert("text", "d.txt");
1346 m_model
->setData(2, data
);
1347 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1348 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1350 expectedGroups
.clear();
1351 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1352 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1353 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1354 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1355 QCOMPARE(m_model
->groups(), expectedGroups
);
1357 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1358 const KFileItem fileItemD
= m_model
->fileItem(2);
1359 KFileItem fileItemC
= fileItemD
;
1360 QUrl urlC
= fileItemC
.url().adjusted(QUrl::RemoveFilename
);
1361 urlC
.setPath(urlC
.path() + "c.txt");
1362 fileItemC
.setUrl(urlC
);
1364 m_model
->slotRefreshItems({qMakePair(fileItemD
, fileItemC
)});
1365 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1366 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1368 expectedGroups
.clear();
1369 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1370 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1371 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1372 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1373 QCOMPARE(m_model
->groups(), expectedGroups
);
1376 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1378 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1379 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1380 m_model
->setRoles(modelRoles
);
1383 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1385 m_testDir
->createFiles(files
);
1387 m_model
->setGroupedSorting(true);
1388 m_model
->loadDirectory(m_testDir
->url());
1389 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1390 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1392 QList
<QPair
<int, QVariant
> > expectedGroups
;
1393 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1394 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1395 QCOMPARE(m_model
->groups(), expectedGroups
);
1397 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1398 expectedGroups
.clear();
1399 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1400 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1402 m_model
->setExpanded(0, true);
1403 QVERIFY(m_model
->isExpanded(0));
1404 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1405 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1406 QCOMPARE(m_model
->groups(), expectedGroups
);
1408 m_model
->setExpanded(3, true);
1409 QVERIFY(m_model
->isExpanded(3));
1410 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1411 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1412 QCOMPARE(m_model
->groups(), expectedGroups
);
1415 void KFileItemModelTest::testInconsistentModel()
1417 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1418 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1419 m_model
->setRoles(modelRoles
);
1422 files
<< "a/b/c1.txt" << "a/b/c2.txt";
1424 m_testDir
->createFiles(files
);
1426 m_model
->loadDirectory(m_testDir
->url());
1427 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1428 QCOMPARE(itemsInModel(), QStringList() << "a");
1430 // Expand "a/" and "a/b/".
1431 m_model
->setExpanded(0, true);
1432 QVERIFY(m_model
->isExpanded(0));
1433 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1434 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1436 m_model
->setExpanded(1, true);
1437 QVERIFY(m_model
->isExpanded(1));
1438 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1439 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1441 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1442 // Such a thing can in principle happen when performing a search, and there
1444 // (a) match the search string, and
1445 // (b) are children of a folder that matches the search string and is expanded.
1447 // Note that the first item in the list of added items must be new (i.e., not
1448 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1449 // it receives items that are in the model already and ignore them.
1450 QUrl
url(m_model
->directory().url() + "/a2");
1451 KFileItem
newItem(url
);
1453 KFileItemList items
;
1454 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1455 m_model
->slotItemsAdded(m_model
->directory(), items
);
1456 m_model
->slotCompleted();
1457 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1459 m_model
->setExpanded(0, false);
1461 // Test that the right items have been removed, see
1462 // https://bugs.kde.org/show_bug.cgi?id=324371
1463 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1465 // Test that resorting does not cause a crash, see
1466 // https://bugs.kde.org/show_bug.cgi?id=325359
1467 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1468 m_model
->resortAllItems();
1472 void KFileItemModelTest::testChangeRolesForFilteredItems()
1474 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1475 modelRoles
<< "owner";
1476 m_model
->setRoles(modelRoles
);
1479 files
<< "a.txt" << "aa.txt" << "aaa.txt";
1480 m_testDir
->createFiles(files
);
1482 m_model
->loadDirectory(m_testDir
->url());
1483 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1484 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1486 for (int index
= 0; index
< m_model
->count(); ++index
) {
1487 // All items should have the "text" and "owner" roles, but not "group".
1488 QVERIFY(m_model
->data(index
).contains("text"));
1489 QVERIFY(m_model
->data(index
).contains("owner"));
1490 QVERIFY(!m_model
->data(index
).contains("group"));
1493 // Add a filter, such that only "aaa.txt" remains in the model.
1494 m_model
->setNameFilter("aaa");
1495 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1497 // Add the "group" role.
1498 modelRoles
<< "group";
1499 m_model
->setRoles(modelRoles
);
1501 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1502 m_model
->setNameFilter("aa");
1503 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1505 for (int index
= 0; index
< m_model
->count(); ++index
) {
1506 // All items should have the "text", "owner", and "group" roles.
1507 QVERIFY(m_model
->data(index
).contains("text"));
1508 QVERIFY(m_model
->data(index
).contains("owner"));
1509 QVERIFY(m_model
->data(index
).contains("group"));
1512 // Remove the "owner" role.
1513 modelRoles
.remove("owner");
1514 m_model
->setRoles(modelRoles
);
1516 // Clear the filter, and verify that all items have the expected roles
1517 m_model
->setNameFilter(QString());
1518 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1520 for (int index
= 0; index
< m_model
->count(); ++index
) {
1521 // All items should have the "text" and "group" roles, but now "owner".
1522 QVERIFY(m_model
->data(index
).contains("text"));
1523 QVERIFY(!m_model
->data(index
).contains("owner"));
1524 QVERIFY(m_model
->data(index
).contains("group"));
1528 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1530 KFileItemList items
;
1532 KIO::UDSEntry entry
;
1533 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1534 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1535 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1536 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1537 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1538 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1540 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1541 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1542 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1544 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1545 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1546 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1548 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1549 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1550 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1552 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1553 m_model
->slotCompleted();
1555 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1558 m_model
->setNameFilter("a");
1559 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1562 m_model
->setSortRole("owner");
1564 // Clear the filter, and verify that the items are sorted correctly.
1565 m_model
->setNameFilter(QString());
1566 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1569 void KFileItemModelTest::testRefreshFilteredItems()
1572 files
<< "a.txt" << "b.txt" << "c.jpg" << "d.jpg";
1573 m_testDir
->createFiles(files
);
1575 m_model
->loadDirectory(m_testDir
->url());
1576 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1577 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1579 const KFileItem fileItemC
= m_model
->fileItem(2);
1581 // Show only the .txt files.
1582 m_model
->setNameFilter(".txt");
1583 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1585 // Rename one of the .jpg files.
1586 KFileItem fileItemE
= fileItemC
;
1587 QUrl urlE
= fileItemE
.url().adjusted(QUrl::RemoveFilename
);
1588 urlE
.setPath(urlE
.path() + "e.jpg");
1589 fileItemE
.setUrl(urlE
);
1591 m_model
->slotRefreshItems({qMakePair(fileItemC
, fileItemE
)});
1593 // Show all files again, and verify that the model has updated the file name.
1594 m_model
->setNameFilter(QString());
1595 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1598 void KFileItemModelTest::testCreateMimeData()
1600 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1601 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1602 m_model
->setRoles(modelRoles
);
1606 m_testDir
->createFiles(files
);
1608 m_model
->loadDirectory(m_testDir
->url());
1609 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1610 QCOMPARE(itemsInModel(), QStringList() << "a");
1613 m_model
->setExpanded(0, true);
1614 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1615 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1617 // Verify that creating the MIME data for a child of an expanded folder does
1618 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1620 selection
.insert(1);
1621 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1625 void KFileItemModelTest::testCollapseFolderWhileLoading()
1627 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1628 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1629 m_model
->setRoles(modelRoles
);
1632 files
<< "a2/b/c1.txt";
1633 m_testDir
->createFiles(files
);
1635 m_model
->loadDirectory(m_testDir
->url());
1636 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1637 QCOMPARE(itemsInModel(), QStringList() << "a2");
1640 m_model
->setExpanded(0, true);
1641 QVERIFY(m_model
->isExpanded(0));
1642 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1643 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1646 m_model
->setExpanded(1, true);
1647 QVERIFY(m_model
->isExpanded(1));
1648 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1649 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1651 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1652 // signal is not emitted yet.
1653 const KFileItem fileItemC1
= m_model
->fileItem(2);
1654 KFileItem fileItemC2
= fileItemC1
;
1655 QUrl urlC2
= fileItemC2
.url();
1656 urlC2
.adjusted(QUrl::RemoveFilename
);
1657 urlC2
.setPath(urlC2
.path() + "c2.txt");
1658 fileItemC2
.setUrl(urlC2
);
1660 const QUrl urlB
= m_model
->fileItem(1).url();
1661 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1662 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1664 // Collapse "a2/". This should also remove all its (indirect) children from
1665 // the model and from the model's m_pendingItemsToInsert member.
1666 m_model
->setExpanded(0, false);
1667 QCOMPARE(itemsInModel(), QStringList() << "a2");
1669 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1670 // is still in m_pendingItemsToInsert, then we might get a crash, see
1671 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1672 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1673 // without parent in the model.
1674 m_model
->slotCompleted();
1675 QCOMPARE(itemsInModel(), QStringList() << "a2");
1677 // Expand "a2/" again.
1678 m_model
->setExpanded(0, true);
1679 QVERIFY(m_model
->isExpanded(0));
1680 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1681 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1683 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1684 // completed() signal is not emitted yet.
1685 const KFileItem fileItemA2
= m_model
->fileItem(0);
1686 KFileItem fileItemA1
= fileItemA2
;
1687 QUrl urlA1
= fileItemA1
.url().adjusted(QUrl::RemoveFilename
);
1688 urlA1
.setPath(urlA1
.path() + "a1");
1689 fileItemA1
.setUrl(urlA1
);
1691 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1692 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1694 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1695 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1696 // confuse the code which collapses the folder.
1697 m_model
->setExpanded(0, false);
1698 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1699 QVERIFY(!m_model
->isExpanded(0));
1700 QVERIFY(!m_model
->isExpanded(1));
1703 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1706 files
<< "a.txt" << "b.txt" << "c.txt" << "d.txt";
1707 m_testDir
->createFiles(files
);
1709 m_model
->loadDirectory(m_testDir
->url());
1710 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1711 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1713 const KFileItem fileItemB
= m_model
->fileItem(1);
1715 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1717 list
<< fileItemB
<< fileItemB
;
1718 m_model
->slotItemsDeleted(list
);
1720 QVERIFY(m_model
->isConsistent());
1721 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1724 QStringList
KFileItemModelTest::itemsInModel() const
1727 for (int i
= 0; i
< m_model
->count(); i
++) {
1728 items
<< m_model
->fileItem(i
).text();
1733 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1735 #include "kfileitemmodeltest.moc"