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>
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
30 void myMessageOutput(QtMsgType type
, const char* msg
)
38 fprintf(stderr
, "Critical: %s\n", msg
);
41 fprintf(stderr
, "Fatal: %s\n", msg
);
49 const int DefaultTimeout
= 5000;
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();
92 QStringList
itemsInModel() const;
95 KFileItemModel
* m_model
;
99 void KFileItemModelTest::init()
101 // The item-model tests result in a huge number of debugging
102 // output from kdelibs. Only show critical and fatal messages.
103 qInstallMsgHandler(myMessageOutput
);
105 qRegisterMetaType
<KItemRange
>("KItemRange");
106 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
107 qRegisterMetaType
<KFileItemList
>("KFileItemList");
109 m_testDir
= new TestDir();
110 m_model
= new KFileItemModel();
111 m_model
->m_dirLister
->setAutoUpdate(false);
113 // Reduce the timer interval to make the test run faster.
114 m_model
->m_resortAllItemsTimer
->setInterval(0);
117 void KFileItemModelTest::cleanup()
126 void KFileItemModelTest::testDefaultRoles()
128 const QSet
<QByteArray
> roles
= m_model
->roles();
129 QCOMPARE(roles
.count(), 3);
130 QVERIFY(roles
.contains("text"));
131 QVERIFY(roles
.contains("isDir"));
132 QVERIFY(roles
.contains("isLink"));
135 void KFileItemModelTest::testDefaultSortRole()
137 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
140 files
<< "c.txt" << "a.txt" << "b.txt";
142 m_testDir
->createFiles(files
);
144 m_model
->loadDirectory(m_testDir
->url());
145 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
147 QCOMPARE(m_model
->count(), 3);
148 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
149 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
150 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
153 void KFileItemModelTest::testDefaultGroupedSorting()
155 QCOMPARE(m_model
->groupedSorting(), false);
158 void KFileItemModelTest::testNewItems()
161 files
<< "a.txt" << "b.txt" << "c.txt";
162 m_testDir
->createFiles(files
);
164 m_model
->loadDirectory(m_testDir
->url());
165 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
167 QCOMPARE(m_model
->count(), 3);
169 QVERIFY(m_model
->isConsistent());
172 void KFileItemModelTest::testRemoveItems()
174 m_testDir
->createFile("a.txt");
175 m_testDir
->createFile("b.txt");
176 m_model
->loadDirectory(m_testDir
->url());
177 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
178 QCOMPARE(m_model
->count(), 2);
179 QVERIFY(m_model
->isConsistent());
181 m_testDir
->removeFile("a.txt");
182 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
184 QCOMPARE(m_model
->count(), 1);
185 QVERIFY(m_model
->isConsistent());
188 void KFileItemModelTest::testDirLoadingCompleted()
190 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
191 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
192 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
194 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
196 m_model
->loadDirectory(m_testDir
->url());
197 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
198 QCOMPARE(loadingCompletedSpy
.count(), 1);
199 QCOMPARE(itemsInsertedSpy
.count(), 1);
200 QCOMPARE(itemsRemovedSpy
.count(), 0);
201 QCOMPARE(m_model
->count(), 3);
203 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
204 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
205 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
206 QCOMPARE(loadingCompletedSpy
.count(), 2);
207 QCOMPARE(itemsInsertedSpy
.count(), 2);
208 QCOMPARE(itemsRemovedSpy
.count(), 0);
209 QCOMPARE(m_model
->count(), 5);
211 m_testDir
->removeFile("a.txt");
212 m_testDir
->createFile("f.txt");
213 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
214 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
215 QCOMPARE(loadingCompletedSpy
.count(), 3);
216 QCOMPARE(itemsInsertedSpy
.count(), 3);
217 QCOMPARE(itemsRemovedSpy
.count(), 1);
218 QCOMPARE(m_model
->count(), 5);
220 m_testDir
->removeFile("b.txt");
221 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
222 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
223 QCOMPARE(loadingCompletedSpy
.count(), 4);
224 QCOMPARE(itemsInsertedSpy
.count(), 3);
225 QCOMPARE(itemsRemovedSpy
.count(), 2);
226 QCOMPARE(m_model
->count(), 4);
228 QVERIFY(m_model
->isConsistent());
231 void KFileItemModelTest::testSetData()
233 m_testDir
->createFile("a.txt");
235 m_model
->loadDirectory(m_testDir
->url());
236 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
238 QHash
<QByteArray
, QVariant
> values
;
239 values
.insert("customRole1", "Test1");
240 values
.insert("customRole2", "Test2");
242 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
243 m_model
->setData(0, values
);
244 QCOMPARE(itemsChangedSpy
.count(), 1);
246 values
= m_model
->data(0);
247 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
248 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
249 QVERIFY(m_model
->isConsistent());
252 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
254 QTest::addColumn
<int>("changedIndex");
255 QTest::addColumn
<int>("changedRating");
256 QTest::addColumn
<bool>("expectMoveSignal");
257 QTest::addColumn
<int>("ratingIndex0");
258 QTest::addColumn
<int>("ratingIndex1");
259 QTest::addColumn
<int>("ratingIndex2");
262 // Index 0 = rating 2
263 // Index 1 = rating 4
264 // Index 2 = rating 6
266 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
267 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
268 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
270 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
271 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
272 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
275 void KFileItemModelTest::testSetDataWithModifiedSortRole()
277 QFETCH(int, changedIndex
);
278 QFETCH(int, changedRating
);
279 QFETCH(bool, expectMoveSignal
);
280 QFETCH(int, ratingIndex0
);
281 QFETCH(int, ratingIndex1
);
282 QFETCH(int, ratingIndex2
);
284 // Changing the value of a sort-role must result in
285 // a reordering of the items.
286 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
287 m_model
->setSortRole("rating");
288 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
291 files
<< "a.txt" << "b.txt" << "c.txt";
292 m_testDir
->createFiles(files
);
294 m_model
->loadDirectory(m_testDir
->url());
295 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
297 // Fill the "rating" role of each file:
302 QHash
<QByteArray
, QVariant
> ratingA
;
303 ratingA
.insert("rating", 2);
304 m_model
->setData(0, ratingA
);
306 QHash
<QByteArray
, QVariant
> ratingB
;
307 ratingB
.insert("rating", 4);
308 m_model
->setData(1, ratingB
);
310 QHash
<QByteArray
, QVariant
> ratingC
;
311 ratingC
.insert("rating", 6);
312 m_model
->setData(2, ratingC
);
314 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
315 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
316 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
318 // Now change the rating from a.txt. This usually results
319 // in reordering of the items.
320 QHash
<QByteArray
, QVariant
> rating
;
321 rating
.insert("rating", changedRating
);
322 m_model
->setData(changedIndex
, rating
);
324 if (expectMoveSignal
) {
325 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
328 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
329 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
330 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
331 QVERIFY(m_model
->isConsistent());
334 void KFileItemModelTest::testChangeSortRole()
336 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
339 files
<< "a.txt" << "b.jpg" << "c.txt";
340 m_testDir
->createFiles(files
);
342 m_model
->loadDirectory(m_testDir
->url());
343 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
344 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
346 // Simulate that KFileItemModelRolesUpdater determines the mime type.
347 // Resorting the files by 'type' will only work immediately if their
348 // mime types are known.
349 for (int index
= 0; index
< m_model
->count(); ++index
) {
350 m_model
->fileItem(index
).determineMimeType();
353 // Now: sort by type.
354 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
355 m_model
->setSortRole("type");
356 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
357 QVERIFY(!spyItemsMoved
.isEmpty());
359 // The actual order of the files might depend on the translation of the
360 // result of KFileItem::mimeComment() in the user's language.
361 QStringList version1
;
362 version1
<< "b.jpg" << "a.txt" << "c.txt";
364 QStringList version2
;
365 version2
<< "a.txt" << "c.txt" << "b.jpg";
367 const bool ok1
= (itemsInModel() == version1
);
368 const bool ok2
= (itemsInModel() == version2
);
373 void KFileItemModelTest::testResortAfterChangingName()
375 // We sort by size in a directory where all files have the same size.
376 // Therefore, the files are sorted by their names.
377 m_model
->setSortRole("size");
380 files
<< "a.txt" << "b.txt" << "c.txt";
381 m_testDir
->createFiles(files
);
383 m_model
->loadDirectory(m_testDir
->url());
384 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
385 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
387 // We rename a.txt to d.txt. Even though the size has not changed at all,
388 // the model must re-sort the items.
389 QHash
<QByteArray
, QVariant
> data
;
390 data
.insert("text", "d.txt");
391 m_model
->setData(0, data
);
393 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
394 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
396 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
397 const KFileItem fileItemD
= m_model
->fileItem(2);
398 KFileItem fileItemA
= fileItemD
;
399 KUrl urlA
= fileItemA
.url();
400 urlA
.setFileName("a.txt");
401 fileItemA
.setUrl(urlA
);
403 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
405 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
406 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
409 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
411 //QSKIP("Temporary disabled", SkipSingle);
413 // KFileItemModel prevents that inserting a punch of items sequentially
414 // results in an itemsInserted()-signal for each item. Instead internally
415 // a timeout is given that collects such operations and results in only
416 // one itemsInserted()-signal. However in this test we want to stress
417 // KFileItemModel to do a lot of insert operation and hence decrease
418 // the timeout to 1 millisecond.
419 m_testDir
->createFile("1");
420 m_model
->loadDirectory(m_testDir
->url());
421 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
422 QCOMPARE(m_model
->count(), 1);
424 // Insert 10 items for 20 times. After each insert operation the model consistency
426 QSet
<int> insertedItems
;
427 for (int i
= 0; i
< 20; ++i
) {
428 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
430 for (int j
= 0; j
< 10; ++j
) {
431 int itemName
= qrand();
432 while (insertedItems
.contains(itemName
)) {
435 insertedItems
.insert(itemName
);
437 m_testDir
->createFile(QString::number(itemName
));
440 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
441 if (spy
.count() == 0) {
442 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
445 QVERIFY(m_model
->isConsistent());
448 QCOMPARE(m_model
->count(), 201);
451 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
454 files
<< "B" << "E" << "G";
455 m_testDir
->createFiles(files
);
457 // Due to inserting the 3 items one item-range with index == 0 and
458 // count == 3 must be given
459 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
460 m_model
->loadDirectory(m_testDir
->url());
461 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
463 QCOMPARE(spy1
.count(), 1);
464 QList
<QVariant
> arguments
= spy1
.takeFirst();
465 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
466 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
468 // The indexes of the item-ranges must always be related to the model before
469 // the items have been inserted. Having:
472 // and inserting A, C, D, F the resulting model will be:
475 // and the item-ranges must be:
476 // index: 0, count: 1 for A
477 // index: 1, count: 2 for B, C
478 // index: 2, count: 1 for G
481 files
<< "A" << "C" << "D" << "F";
482 m_testDir
->createFiles(files
);
484 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
485 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
486 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
488 QCOMPARE(spy2
.count(), 1);
489 arguments
= spy2
.takeFirst();
490 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
491 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
494 void KFileItemModelTest::testExpandItems()
496 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
497 // Besides testing the basic item expansion functionality, the test makes sure that
498 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
499 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
500 // first three characters.
501 QSet
<QByteArray
> modelRoles
= m_model
->roles();
502 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
503 m_model
->setRoles(modelRoles
);
506 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
507 m_testDir
->createFiles(files
);
509 // Store the URLs of all folders in a set.
510 QSet
<KUrl
> allFolders
;
511 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
513 m_model
->loadDirectory(m_testDir
->url());
514 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
516 // So far, the model contains only "a/"
517 QCOMPARE(m_model
->count(), 1);
518 QVERIFY(m_model
->isExpandable(0));
519 QVERIFY(!m_model
->isExpanded(0));
520 QVERIFY(m_model
->expandedDirectories().empty());
522 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
524 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
525 m_model
->setExpanded(0, true);
526 QVERIFY(m_model
->isExpanded(0));
527 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
528 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
529 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
531 QCOMPARE(spyInserted
.count(), 1);
532 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
533 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
535 QVERIFY(m_model
->isExpandable(1));
536 QVERIFY(!m_model
->isExpanded(1));
537 QVERIFY(m_model
->isExpandable(2));
538 QVERIFY(!m_model
->isExpanded(2));
540 // Expand the folder "a/a/" -> "a/a/1" becomes visible
541 m_model
->setExpanded(1, true);
542 QVERIFY(m_model
->isExpanded(1));
543 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
544 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
545 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
547 QCOMPARE(spyInserted
.count(), 1);
548 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
549 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
551 QVERIFY(!m_model
->isExpandable(2));
552 QVERIFY(!m_model
->isExpanded(2));
554 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
555 m_model
->setExpanded(3, true);
556 QVERIFY(m_model
->isExpanded(3));
557 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
558 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
559 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
561 QCOMPARE(spyInserted
.count(), 1);
562 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
563 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
565 QVERIFY(!m_model
->isExpandable(4));
566 QVERIFY(!m_model
->isExpanded(4));
568 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
570 // Collapse the top-level folder -> all other items should disappear
571 m_model
->setExpanded(0, false);
572 QVERIFY(!m_model
->isExpanded(0));
573 QCOMPARE(m_model
->count(), 1);
574 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
576 QCOMPARE(spyRemoved
.count(), 1);
577 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
578 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
579 QVERIFY(m_model
->isConsistent());
581 // Clear the model, reload the folder and try to restore the expanded folders.
583 QCOMPARE(m_model
->count(), 0);
584 QVERIFY(m_model
->expandedDirectories().empty());
586 m_model
->loadDirectory(m_testDir
->url());
587 m_model
->restoreExpandedDirectories(allFolders
);
588 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
589 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
590 QVERIFY(m_model
->isExpanded(0));
591 QVERIFY(m_model
->isExpanded(1));
592 QVERIFY(!m_model
->isExpanded(2));
593 QVERIFY(m_model
->isExpanded(3));
594 QVERIFY(!m_model
->isExpanded(4));
595 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
596 QVERIFY(m_model
->isConsistent());
598 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
599 // This is how DolphinView restores the expanded folders when navigating in history.
600 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
601 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
602 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
603 m_model
->restoreExpandedDirectories(allFolders
);
604 m_model
->loadDirectory(m_testDir
->url());
605 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
606 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
607 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
610 void KFileItemModelTest::testExpandParentItems()
612 // Create a tree structure of folders:
620 QSet
<QByteArray
> modelRoles
= m_model
->roles();
621 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
622 m_model
->setRoles(modelRoles
);
625 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
626 m_testDir
->createFiles(files
);
628 m_model
->loadDirectory(m_testDir
->url());
629 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
631 // So far, the model contains only "a 1/" and "a2/".
632 QCOMPARE(m_model
->count(), 2);
633 QVERIFY(m_model
->expandedDirectories().empty());
635 // Expand the parents of "a2/b2/c2".
636 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
637 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
639 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
640 // It's important that only the parents of "a1/b1/c1" are expanded.
641 QCOMPARE(m_model
->count(), 4);
642 QVERIFY(!m_model
->isExpanded(0));
643 QVERIFY(m_model
->isExpanded(1));
644 QVERIFY(m_model
->isExpanded(2));
645 QVERIFY(!m_model
->isExpanded(3));
647 // Expand the parents of "a 1/b1".
648 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
649 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
651 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
652 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
653 QCOMPARE(m_model
->count(), 5);
654 QVERIFY(m_model
->isExpanded(0));
655 QVERIFY(!m_model
->isExpanded(1));
656 QVERIFY(m_model
->isExpanded(2));
657 QVERIFY(m_model
->isExpanded(3));
658 QVERIFY(!m_model
->isExpanded(4));
659 QVERIFY(m_model
->isConsistent());
663 * Renaming an expanded folder by prepending its name with a dot makes it
664 * hidden. Verify that this does not cause an inconsistent model state and
665 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
667 void KFileItemModelTest::testMakeExpandedItemHidden()
669 QSet
<QByteArray
> modelRoles
= m_model
->roles();
670 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
671 m_model
->setRoles(modelRoles
);
674 m_testDir
->createFile("1a/2a/3a");
675 m_testDir
->createFile("1a/2a/3b");
676 m_testDir
->createFile("1a/2b");
677 m_testDir
->createFile("1b");
679 m_model
->loadDirectory(m_testDir
->url());
680 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
682 // So far, the model contains only "1a/" and "1b".
683 QCOMPARE(m_model
->count(), 2);
684 m_model
->setExpanded(0, true);
685 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
687 // Now "1a/2a" and "1a/2b" have appeared.
688 QCOMPARE(m_model
->count(), 4);
689 m_model
->setExpanded(1, true);
690 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
691 QCOMPARE(m_model
->count(), 6);
693 // Rename "1a/2" and make it hidden.
694 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
695 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
697 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
698 bool ok
= job
->exec();
700 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
702 // "1a/2" and its subfolders have disappeared now.
703 QVERIFY(m_model
->isConsistent());
704 QCOMPARE(m_model
->count(), 3);
706 m_model
->setExpanded(0, false);
707 QCOMPARE(m_model
->count(), 2);
711 void KFileItemModelTest::testRemoveFilteredExpandedItems()
713 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
714 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
715 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
716 m_model
->setRoles(modelRoles
);
719 files
<< "folder/child" << "file"; // missing folders are created automatically
720 m_testDir
->createFiles(files
);
722 m_model
->loadDirectory(m_testDir
->url());
723 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
725 // So far, the model contains only "folder/" and "file".
726 QCOMPARE(m_model
->count(), 2);
727 QVERIFY(m_model
->isExpandable(0));
728 QVERIFY(!m_model
->isExpandable(1));
729 QVERIFY(!m_model
->isExpanded(0));
730 QVERIFY(!m_model
->isExpanded(1));
731 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
733 // Expand "folder" -> "folder/child" becomes visible.
734 m_model
->setExpanded(0, true);
735 QVERIFY(m_model
->isExpanded(0));
736 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
737 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
739 // Add a name filter.
740 m_model
->setNameFilter("f");
741 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
743 m_model
->setNameFilter("fo");
744 QCOMPARE(itemsInModel(), QStringList() << "folder");
746 // Remove all expanded items by changing the roles
747 m_model
->setRoles(originalModelRoles
);
748 QVERIFY(!m_model
->isExpanded(0));
749 QCOMPARE(itemsInModel(), QStringList() << "folder");
751 // Remove the name filter and verify that "folder/child" does not reappear.
752 m_model
->setNameFilter(QString());
753 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
756 void KFileItemModelTest::testSorting()
758 // Create some files with different sizes and modification times to check the different sorting options
759 QDateTime now
= QDateTime::currentDateTime();
761 QSet
<QByteArray
> roles
;
762 roles
.insert("text");
763 roles
.insert("isExpanded");
764 roles
.insert("isExpandable");
765 roles
.insert("expandedParentsCount");
766 m_model
->setRoles(roles
);
768 m_testDir
->createDir("c/c-2");
769 m_testDir
->createFile("c/c-2/c-3");
770 m_testDir
->createFile("c/c-1");
772 m_testDir
->createFile("a", "A file", now
.addDays(-3));
773 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
774 m_testDir
->createDir("c", now
.addDays(-2));
775 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
776 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
777 m_testDir
->createFile(".f");
779 m_model
->loadDirectory(m_testDir
->url());
780 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
782 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
783 m_model
->setExpanded(index
, true);
784 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
786 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
787 m_model
->setExpanded(index
, true);
788 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
790 // Default: Sort by Name, ascending
791 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
792 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
793 QVERIFY(m_model
->sortDirectoriesFirst());
794 QVERIFY(!m_model
->showHiddenFiles());
795 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
797 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
799 // Sort by Name, ascending, 'Sort Folders First' disabled
800 m_model
->setSortDirectoriesFirst(false);
801 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
802 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
803 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
804 QCOMPARE(spyItemsMoved
.count(), 1);
805 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
807 // Sort by Name, descending
808 m_model
->setSortDirectoriesFirst(true);
809 m_model
->setSortOrder(Qt::DescendingOrder
);
810 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
811 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
812 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
813 QCOMPARE(spyItemsMoved
.count(), 2);
814 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
815 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
817 // Sort by Date, descending
818 m_model
->setSortDirectoriesFirst(true);
819 m_model
->setSortRole("date");
820 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
821 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
822 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
823 QCOMPARE(spyItemsMoved
.count(), 1);
824 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
826 // Sort by Date, ascending
827 m_model
->setSortOrder(Qt::AscendingOrder
);
828 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
829 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
830 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
831 QCOMPARE(spyItemsMoved
.count(), 1);
832 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
834 // Sort by Date, ascending, 'Sort Folders First' disabled
835 m_model
->setSortDirectoriesFirst(false);
836 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
837 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
838 QVERIFY(!m_model
->sortDirectoriesFirst());
839 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
840 QCOMPARE(spyItemsMoved
.count(), 1);
841 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
843 // Sort by Name, ascending, 'Sort Folders First' disabled
844 m_model
->setSortRole("text");
845 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
846 QVERIFY(!m_model
->sortDirectoriesFirst());
847 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
848 QCOMPARE(spyItemsMoved
.count(), 1);
849 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
851 // Sort by Size, ascending, 'Sort Folders First' disabled
852 m_model
->setSortRole("size");
853 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
854 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
855 QVERIFY(!m_model
->sortDirectoriesFirst());
856 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
857 QCOMPARE(spyItemsMoved
.count(), 1);
858 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
860 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
861 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
862 "another signal", SkipSingle
);
863 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
865 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
866 m_model
->setSortDirectoriesFirst(true);
867 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
868 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
869 QVERIFY(m_model
->sortDirectoriesFirst());
870 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
871 QCOMPARE(spyItemsMoved
.count(), 0);
873 // Sort by Size, descending, 'Sort Folders First' enabled
874 m_model
->setSortOrder(Qt::DescendingOrder
);
875 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
876 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
877 QVERIFY(m_model
->sortDirectoriesFirst());
878 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
879 QCOMPARE(spyItemsMoved
.count(), 1);
880 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
882 // TODO: Sort by other roles; show/hide hidden files
885 void KFileItemModelTest::testIndexForKeyboardSearch()
888 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
889 m_testDir
->createFiles(files
);
891 m_model
->loadDirectory(m_testDir
->url());
892 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
894 // Search from index 0
895 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
896 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
897 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
898 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
899 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
900 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
901 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
902 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
903 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
904 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
905 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
907 // Start a search somewhere in the middle
908 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
909 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
910 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
911 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
913 // Test searches that go past the last item back to index 0
914 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
915 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
916 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
917 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
919 // Test searches that yield no result
920 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
921 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
922 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
923 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
924 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
926 // Test upper case searches (note that search is case insensitive)
927 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
928 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
929 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
930 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
932 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
935 void KFileItemModelTest::testNameFilter()
938 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
939 m_testDir
->createFiles(files
);
941 m_model
->loadDirectory(m_testDir
->url());
942 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
944 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
945 QCOMPARE(m_model
->count(), 3);
947 m_model
->setNameFilter("A2"); // Shows only A2
948 QCOMPARE(m_model
->count(), 1);
950 m_model
->setNameFilter("A2"); // Shows only A1
951 QCOMPARE(m_model
->count(), 1);
953 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
954 QCOMPARE(m_model
->count(), 2);
956 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
957 QCOMPARE(m_model
->count(), 2);
959 m_model
->setNameFilter(QString()); // Shows again all items
960 QCOMPARE(m_model
->count(), 5);
964 * Verifies that we do not crash when adding a KFileItem with an empty path.
965 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
966 * tried to always read the first character of the path, even if the path is empty.
968 void KFileItemModelTest::testEmptyPath()
970 QSet
<QByteArray
> roles
;
971 roles
.insert("text");
972 roles
.insert("isExpanded");
973 roles
.insert("isExpandable");
974 roles
.insert("expandedParentsCount");
975 m_model
->setRoles(roles
);
978 QVERIFY(emptyUrl
.path().isEmpty());
980 const KUrl
url("file:///test/");
983 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
984 m_model
->slotItemsAdded(emptyUrl
, items
);
985 m_model
->slotCompleted();
989 * Verifies that the 'isExpanded' state of folders does not change when the
990 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
992 void KFileItemModelTest::testRefreshExpandedItem()
994 QSet
<QByteArray
> modelRoles
= m_model
->roles();
995 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
996 m_model
->setRoles(modelRoles
);
999 files
<< "a/1" << "a/2" << "3" << "4";
1000 m_testDir
->createFiles(files
);
1002 m_model
->loadDirectory(m_testDir
->url());
1003 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1004 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1006 m_model
->setExpanded(0, true);
1007 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1008 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1009 QVERIFY(m_model
->isExpanded(0));
1011 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1013 const KFileItem item
= m_model
->fileItem(0);
1014 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1015 QVERIFY(!spyItemsChanged
.isEmpty());
1017 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1018 QVERIFY(m_model
->isExpanded(0));
1022 * Verify that removing hidden files and folders from the model does not
1023 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1025 void KFileItemModelTest::testRemoveHiddenItems()
1027 m_testDir
->createDir(".a");
1028 m_testDir
->createDir(".b");
1029 m_testDir
->createDir("c");
1030 m_testDir
->createDir("d");
1031 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1033 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1034 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1036 m_model
->setShowHiddenFiles(true);
1037 m_model
->loadDirectory(m_testDir
->url());
1038 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1039 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1040 QCOMPARE(spyItemsInserted
.count(), 1);
1041 QCOMPARE(spyItemsRemoved
.count(), 0);
1042 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1043 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1045 m_model
->setShowHiddenFiles(false);
1046 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1047 QCOMPARE(spyItemsInserted
.count(), 0);
1048 QCOMPARE(spyItemsRemoved
.count(), 1);
1049 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1050 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1052 m_model
->setShowHiddenFiles(true);
1053 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1054 QCOMPARE(spyItemsInserted
.count(), 1);
1055 QCOMPARE(spyItemsRemoved
.count(), 0);
1056 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1057 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1060 QCOMPARE(itemsInModel(), QStringList());
1061 QCOMPARE(spyItemsInserted
.count(), 0);
1062 QCOMPARE(spyItemsRemoved
.count(), 1);
1063 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1064 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1066 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1067 // Verify that this does not make the model crash.
1068 m_model
->setShowHiddenFiles(false);
1072 * Verify that filtered items are removed when their parent is collapsed.
1074 void KFileItemModelTest::collapseParentOfHiddenItems()
1076 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1077 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1078 m_model
->setRoles(modelRoles
);
1081 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1082 m_testDir
->createFiles(files
);
1084 m_model
->loadDirectory(m_testDir
->url());
1085 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1086 QCOMPARE(m_model
->count(), 1); // Only "a/"
1089 m_model
->setExpanded(0, true);
1090 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1091 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1094 m_model
->setExpanded(1, true);
1095 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1096 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1099 m_model
->setExpanded(2, true);
1100 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1101 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"
1103 // Set a name filter that matches nothing -> only the expanded folders remain.
1104 m_model
->setNameFilter("xyz");
1105 QCOMPARE(m_model
->count(), 3);
1106 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1108 // Collapse the folder "a/".
1109 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1110 m_model
->setExpanded(0, false);
1111 QCOMPARE(spyItemsRemoved
.count(), 1);
1112 QCOMPARE(m_model
->count(), 1);
1113 QCOMPARE(itemsInModel(), QStringList() << "a");
1115 // Remove the filter -> no files should appear (and we should not get a crash).
1116 m_model
->setNameFilter(QString());
1117 QCOMPARE(m_model
->count(), 1);
1121 * Verify that filtered items are removed when their parent is deleted.
1123 void KFileItemModelTest::removeParentOfHiddenItems()
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 // Simulate the deletion of the directory "a/b/".
1158 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1159 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1160 QCOMPARE(spyItemsRemoved
.count(), 1);
1161 QCOMPARE(m_model
->count(), 1);
1162 QCOMPARE(itemsInModel(), QStringList() << "a");
1164 // Remove the filter -> only the file "a/1" should appear.
1165 m_model
->setNameFilter(QString());
1166 QCOMPARE(m_model
->count(), 2);
1167 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1171 * Create a tree structure where parent-child relationships can not be
1172 * determined by parsing the URLs, and verify that KFileItemModel
1173 * handles them correctly.
1175 void KFileItemModelTest::testGeneralParentChildRelationships()
1177 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1178 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1179 m_model
->setRoles(modelRoles
);
1182 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1183 m_testDir
->createFiles(files
);
1185 m_model
->loadDirectory(m_testDir
->url());
1186 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1187 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1189 // Expand all folders.
1190 m_model
->setExpanded(0, true);
1191 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1192 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1194 m_model
->setExpanded(1, true);
1195 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1196 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1198 m_model
->setExpanded(3, true);
1199 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1200 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1202 m_model
->setExpanded(4, true);
1203 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1204 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1206 // Add some more children and grand-children.
1207 const KUrl parent1
= m_model
->fileItem(0).url();
1208 const KUrl parent2
= m_model
->fileItem(3).url();
1209 const KUrl realChild1
= m_model
->fileItem(1).url();
1210 const KUrl realChild2
= m_model
->fileItem(4).url();
1212 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1213 m_model
->slotCompleted();
1214 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1216 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1217 m_model
->slotCompleted();
1218 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1220 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1221 m_model
->slotCompleted();
1222 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1224 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1225 m_model
->slotCompleted();
1226 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1228 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1229 m_model
->slotCompleted();
1230 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1232 // Set a name filter that matches nothing -> only expanded folders remain.
1233 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1234 m_model
->setNameFilter("xyz");
1235 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1236 QCOMPARE(itemsRemovedSpy
.count(), 1);
1237 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1238 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1239 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1241 // Collapse "parent1".
1242 m_model
->setExpanded(0, false);
1243 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1244 QCOMPARE(itemsRemovedSpy
.count(), 1);
1245 arguments
= itemsRemovedSpy
.takeFirst();
1246 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1247 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1249 // Remove "parent2".
1250 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1251 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1252 QCOMPARE(itemsRemovedSpy
.count(), 1);
1253 arguments
= itemsRemovedSpy
.takeFirst();
1254 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1255 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1257 // Clear filter, verify that no items reappear.
1258 m_model
->setNameFilter(QString());
1259 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1262 void KFileItemModelTest::testNameRoleGroups()
1265 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1267 m_testDir
->createFiles(files
);
1269 m_model
->setGroupedSorting(true);
1270 m_model
->loadDirectory(m_testDir
->url());
1271 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1272 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1274 QList
<QPair
<int, QVariant
> > expectedGroups
;
1275 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1276 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1277 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1278 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1279 QCOMPARE(m_model
->groups(), expectedGroups
);
1281 // Rename d.txt to a.txt.
1282 QHash
<QByteArray
, QVariant
> data
;
1283 data
.insert("text", "a.txt");
1284 m_model
->setData(2, data
);
1285 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1286 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1288 expectedGroups
.clear();
1289 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1290 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1291 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1292 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1293 QCOMPARE(m_model
->groups(), expectedGroups
);
1295 // Rename c.txt to d.txt.
1296 data
.insert("text", "d.txt");
1297 m_model
->setData(2, data
);
1298 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1299 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1301 expectedGroups
.clear();
1302 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1303 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1304 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1305 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1306 QCOMPARE(m_model
->groups(), expectedGroups
);
1308 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1309 const KFileItem fileItemD
= m_model
->fileItem(2);
1310 KFileItem fileItemC
= fileItemD
;
1311 KUrl urlC
= fileItemC
.url();
1312 urlC
.setFileName("c.txt");
1313 fileItemC
.setUrl(urlC
);
1315 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1316 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1317 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1319 expectedGroups
.clear();
1320 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1321 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1322 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1323 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1324 QCOMPARE(m_model
->groups(), expectedGroups
);
1327 QStringList
KFileItemModelTest::itemsInModel() const
1330 for (int i
= 0; i
< m_model
->count(); i
++) {
1331 items
<< m_model
->data(i
).value("text").toString();
1336 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1338 #include "kfileitemmodeltest.moc"