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();
90 void testNameRoleGroupsWithExpandedItems();
93 QStringList
itemsInModel() const;
96 KFileItemModel
* m_model
;
100 void KFileItemModelTest::init()
102 // The item-model tests result in a huge number of debugging
103 // output from kdelibs. Only show critical and fatal messages.
104 qInstallMsgHandler(myMessageOutput
);
106 qRegisterMetaType
<KItemRange
>("KItemRange");
107 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
108 qRegisterMetaType
<KFileItemList
>("KFileItemList");
110 m_testDir
= new TestDir();
111 m_model
= new KFileItemModel();
112 m_model
->m_dirLister
->setAutoUpdate(false);
114 // Reduce the timer interval to make the test run faster.
115 m_model
->m_resortAllItemsTimer
->setInterval(0);
118 void KFileItemModelTest::cleanup()
127 void KFileItemModelTest::testDefaultRoles()
129 const QSet
<QByteArray
> roles
= m_model
->roles();
130 QCOMPARE(roles
.count(), 3);
131 QVERIFY(roles
.contains("text"));
132 QVERIFY(roles
.contains("isDir"));
133 QVERIFY(roles
.contains("isLink"));
136 void KFileItemModelTest::testDefaultSortRole()
138 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
141 files
<< "c.txt" << "a.txt" << "b.txt";
143 m_testDir
->createFiles(files
);
145 m_model
->loadDirectory(m_testDir
->url());
146 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
148 QCOMPARE(m_model
->count(), 3);
149 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
150 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
151 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
154 void KFileItemModelTest::testDefaultGroupedSorting()
156 QCOMPARE(m_model
->groupedSorting(), false);
159 void KFileItemModelTest::testNewItems()
162 files
<< "a.txt" << "b.txt" << "c.txt";
163 m_testDir
->createFiles(files
);
165 m_model
->loadDirectory(m_testDir
->url());
166 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
168 QCOMPARE(m_model
->count(), 3);
170 QVERIFY(m_model
->isConsistent());
173 void KFileItemModelTest::testRemoveItems()
175 m_testDir
->createFile("a.txt");
176 m_testDir
->createFile("b.txt");
177 m_model
->loadDirectory(m_testDir
->url());
178 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
179 QCOMPARE(m_model
->count(), 2);
180 QVERIFY(m_model
->isConsistent());
182 m_testDir
->removeFile("a.txt");
183 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
184 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
185 QCOMPARE(m_model
->count(), 1);
186 QVERIFY(m_model
->isConsistent());
189 void KFileItemModelTest::testDirLoadingCompleted()
191 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
192 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
193 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
195 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
197 m_model
->loadDirectory(m_testDir
->url());
198 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
199 QCOMPARE(loadingCompletedSpy
.count(), 1);
200 QCOMPARE(itemsInsertedSpy
.count(), 1);
201 QCOMPARE(itemsRemovedSpy
.count(), 0);
202 QCOMPARE(m_model
->count(), 3);
204 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
205 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
206 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
207 QCOMPARE(loadingCompletedSpy
.count(), 2);
208 QCOMPARE(itemsInsertedSpy
.count(), 2);
209 QCOMPARE(itemsRemovedSpy
.count(), 0);
210 QCOMPARE(m_model
->count(), 5);
212 m_testDir
->removeFile("a.txt");
213 m_testDir
->createFile("f.txt");
214 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
215 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
216 QCOMPARE(loadingCompletedSpy
.count(), 3);
217 QCOMPARE(itemsInsertedSpy
.count(), 3);
218 QCOMPARE(itemsRemovedSpy
.count(), 1);
219 QCOMPARE(m_model
->count(), 5);
221 m_testDir
->removeFile("b.txt");
222 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
223 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
224 QCOMPARE(loadingCompletedSpy
.count(), 4);
225 QCOMPARE(itemsInsertedSpy
.count(), 3);
226 QCOMPARE(itemsRemovedSpy
.count(), 2);
227 QCOMPARE(m_model
->count(), 4);
229 QVERIFY(m_model
->isConsistent());
232 void KFileItemModelTest::testSetData()
234 m_testDir
->createFile("a.txt");
236 m_model
->loadDirectory(m_testDir
->url());
237 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
239 QHash
<QByteArray
, QVariant
> values
;
240 values
.insert("customRole1", "Test1");
241 values
.insert("customRole2", "Test2");
243 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
244 m_model
->setData(0, values
);
245 QCOMPARE(itemsChangedSpy
.count(), 1);
247 values
= m_model
->data(0);
248 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
249 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
250 QVERIFY(m_model
->isConsistent());
253 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
255 QTest::addColumn
<int>("changedIndex");
256 QTest::addColumn
<int>("changedRating");
257 QTest::addColumn
<bool>("expectMoveSignal");
258 QTest::addColumn
<int>("ratingIndex0");
259 QTest::addColumn
<int>("ratingIndex1");
260 QTest::addColumn
<int>("ratingIndex2");
263 // Index 0 = rating 2
264 // Index 1 = rating 4
265 // Index 2 = rating 6
267 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
268 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
269 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
271 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
272 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
273 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
276 void KFileItemModelTest::testSetDataWithModifiedSortRole()
278 QFETCH(int, changedIndex
);
279 QFETCH(int, changedRating
);
280 QFETCH(bool, expectMoveSignal
);
281 QFETCH(int, ratingIndex0
);
282 QFETCH(int, ratingIndex1
);
283 QFETCH(int, ratingIndex2
);
285 // Changing the value of a sort-role must result in
286 // a reordering of the items.
287 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
288 m_model
->setSortRole("rating");
289 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
292 files
<< "a.txt" << "b.txt" << "c.txt";
293 m_testDir
->createFiles(files
);
295 m_model
->loadDirectory(m_testDir
->url());
296 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
298 // Fill the "rating" role of each file:
303 QHash
<QByteArray
, QVariant
> ratingA
;
304 ratingA
.insert("rating", 2);
305 m_model
->setData(0, ratingA
);
307 QHash
<QByteArray
, QVariant
> ratingB
;
308 ratingB
.insert("rating", 4);
309 m_model
->setData(1, ratingB
);
311 QHash
<QByteArray
, QVariant
> ratingC
;
312 ratingC
.insert("rating", 6);
313 m_model
->setData(2, ratingC
);
315 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
316 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
317 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
319 // Now change the rating from a.txt. This usually results
320 // in reordering of the items.
321 QHash
<QByteArray
, QVariant
> rating
;
322 rating
.insert("rating", changedRating
);
323 m_model
->setData(changedIndex
, rating
);
325 if (expectMoveSignal
) {
326 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
329 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
330 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
331 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
332 QVERIFY(m_model
->isConsistent());
335 void KFileItemModelTest::testChangeSortRole()
337 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
340 files
<< "a.txt" << "b.jpg" << "c.txt";
341 m_testDir
->createFiles(files
);
343 m_model
->loadDirectory(m_testDir
->url());
344 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
345 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
347 // Simulate that KFileItemModelRolesUpdater determines the mime type.
348 // Resorting the files by 'type' will only work immediately if their
349 // mime types are known.
350 for (int index
= 0; index
< m_model
->count(); ++index
) {
351 m_model
->fileItem(index
).determineMimeType();
354 // Now: sort by type.
355 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
356 m_model
->setSortRole("type");
357 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
358 QVERIFY(!spyItemsMoved
.isEmpty());
360 // The actual order of the files might depend on the translation of the
361 // result of KFileItem::mimeComment() in the user's language.
362 QStringList version1
;
363 version1
<< "b.jpg" << "a.txt" << "c.txt";
365 QStringList version2
;
366 version2
<< "a.txt" << "c.txt" << "b.jpg";
368 const bool ok1
= (itemsInModel() == version1
);
369 const bool ok2
= (itemsInModel() == version2
);
374 void KFileItemModelTest::testResortAfterChangingName()
376 // We sort by size in a directory where all files have the same size.
377 // Therefore, the files are sorted by their names.
378 m_model
->setSortRole("size");
381 files
<< "a.txt" << "b.txt" << "c.txt";
382 m_testDir
->createFiles(files
);
384 m_model
->loadDirectory(m_testDir
->url());
385 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
386 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
388 // We rename a.txt to d.txt. Even though the size has not changed at all,
389 // the model must re-sort the items.
390 QHash
<QByteArray
, QVariant
> data
;
391 data
.insert("text", "d.txt");
392 m_model
->setData(0, data
);
394 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
395 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
397 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
398 const KFileItem fileItemD
= m_model
->fileItem(2);
399 KFileItem fileItemA
= fileItemD
;
400 KUrl urlA
= fileItemA
.url();
401 urlA
.setFileName("a.txt");
402 fileItemA
.setUrl(urlA
);
404 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
406 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
407 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
410 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
412 //QSKIP("Temporary disabled", SkipSingle);
414 // KFileItemModel prevents that inserting a punch of items sequentially
415 // results in an itemsInserted()-signal for each item. Instead internally
416 // a timeout is given that collects such operations and results in only
417 // one itemsInserted()-signal. However in this test we want to stress
418 // KFileItemModel to do a lot of insert operation and hence decrease
419 // the timeout to 1 millisecond.
420 m_testDir
->createFile("1");
421 m_model
->loadDirectory(m_testDir
->url());
422 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
423 QCOMPARE(m_model
->count(), 1);
425 // Insert 10 items for 20 times. After each insert operation the model consistency
427 QSet
<int> insertedItems
;
428 for (int i
= 0; i
< 20; ++i
) {
429 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
431 for (int j
= 0; j
< 10; ++j
) {
432 int itemName
= qrand();
433 while (insertedItems
.contains(itemName
)) {
436 insertedItems
.insert(itemName
);
438 m_testDir
->createFile(QString::number(itemName
));
441 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
442 if (spy
.count() == 0) {
443 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
446 QVERIFY(m_model
->isConsistent());
449 QCOMPARE(m_model
->count(), 201);
452 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
455 files
<< "B" << "E" << "G";
456 m_testDir
->createFiles(files
);
458 // Due to inserting the 3 items one item-range with index == 0 and
459 // count == 3 must be given
460 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
461 m_model
->loadDirectory(m_testDir
->url());
462 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
464 QCOMPARE(spy1
.count(), 1);
465 QList
<QVariant
> arguments
= spy1
.takeFirst();
466 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
467 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
469 // The indexes of the item-ranges must always be related to the model before
470 // the items have been inserted. Having:
473 // and inserting A, C, D, F the resulting model will be:
476 // and the item-ranges must be:
477 // index: 0, count: 1 for A
478 // index: 1, count: 2 for B, C
479 // index: 2, count: 1 for G
482 files
<< "A" << "C" << "D" << "F";
483 m_testDir
->createFiles(files
);
485 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
486 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
487 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
489 QCOMPARE(spy2
.count(), 1);
490 arguments
= spy2
.takeFirst();
491 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
492 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
495 void KFileItemModelTest::testExpandItems()
497 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
498 // Besides testing the basic item expansion functionality, the test makes sure that
499 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
500 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
501 // first three characters.
502 QSet
<QByteArray
> modelRoles
= m_model
->roles();
503 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
504 m_model
->setRoles(modelRoles
);
507 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
508 m_testDir
->createFiles(files
);
510 // Store the URLs of all folders in a set.
511 QSet
<KUrl
> allFolders
;
512 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
514 m_model
->loadDirectory(m_testDir
->url());
515 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
517 // So far, the model contains only "a/"
518 QCOMPARE(m_model
->count(), 1);
519 QVERIFY(m_model
->isExpandable(0));
520 QVERIFY(!m_model
->isExpanded(0));
521 QVERIFY(m_model
->expandedDirectories().empty());
523 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
525 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
526 m_model
->setExpanded(0, true);
527 QVERIFY(m_model
->isExpanded(0));
528 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
529 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
530 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
532 QCOMPARE(spyInserted
.count(), 1);
533 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
534 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
536 QVERIFY(m_model
->isExpandable(1));
537 QVERIFY(!m_model
->isExpanded(1));
538 QVERIFY(m_model
->isExpandable(2));
539 QVERIFY(!m_model
->isExpanded(2));
541 // Expand the folder "a/a/" -> "a/a/1" becomes visible
542 m_model
->setExpanded(1, true);
543 QVERIFY(m_model
->isExpanded(1));
544 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
545 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
546 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
548 QCOMPARE(spyInserted
.count(), 1);
549 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
550 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
552 QVERIFY(!m_model
->isExpandable(2));
553 QVERIFY(!m_model
->isExpanded(2));
555 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
556 m_model
->setExpanded(3, true);
557 QVERIFY(m_model
->isExpanded(3));
558 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
559 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
560 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
562 QCOMPARE(spyInserted
.count(), 1);
563 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
564 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
566 QVERIFY(!m_model
->isExpandable(4));
567 QVERIFY(!m_model
->isExpanded(4));
569 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
571 // Collapse the top-level folder -> all other items should disappear
572 m_model
->setExpanded(0, false);
573 QVERIFY(!m_model
->isExpanded(0));
574 QCOMPARE(m_model
->count(), 1);
575 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
577 QCOMPARE(spyRemoved
.count(), 1);
578 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
579 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
580 QVERIFY(m_model
->isConsistent());
582 // Clear the model, reload the folder and try to restore the expanded folders.
584 QCOMPARE(m_model
->count(), 0);
585 QVERIFY(m_model
->expandedDirectories().empty());
587 m_model
->loadDirectory(m_testDir
->url());
588 m_model
->restoreExpandedDirectories(allFolders
);
589 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
590 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
591 QVERIFY(m_model
->isExpanded(0));
592 QVERIFY(m_model
->isExpanded(1));
593 QVERIFY(!m_model
->isExpanded(2));
594 QVERIFY(m_model
->isExpanded(3));
595 QVERIFY(!m_model
->isExpanded(4));
596 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
597 QVERIFY(m_model
->isConsistent());
599 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
600 // This is how DolphinView restores the expanded folders when navigating in history.
601 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
602 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
603 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
604 m_model
->restoreExpandedDirectories(allFolders
);
605 m_model
->loadDirectory(m_testDir
->url());
606 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
607 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
608 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
611 void KFileItemModelTest::testExpandParentItems()
613 // Create a tree structure of folders:
621 QSet
<QByteArray
> modelRoles
= m_model
->roles();
622 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
623 m_model
->setRoles(modelRoles
);
626 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
627 m_testDir
->createFiles(files
);
629 m_model
->loadDirectory(m_testDir
->url());
630 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
632 // So far, the model contains only "a 1/" and "a2/".
633 QCOMPARE(m_model
->count(), 2);
634 QVERIFY(m_model
->expandedDirectories().empty());
636 // Expand the parents of "a2/b2/c2".
637 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
638 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
640 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
641 // It's important that only the parents of "a1/b1/c1" are expanded.
642 QCOMPARE(m_model
->count(), 4);
643 QVERIFY(!m_model
->isExpanded(0));
644 QVERIFY(m_model
->isExpanded(1));
645 QVERIFY(m_model
->isExpanded(2));
646 QVERIFY(!m_model
->isExpanded(3));
648 // Expand the parents of "a 1/b1".
649 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
650 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
652 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
653 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
654 QCOMPARE(m_model
->count(), 5);
655 QVERIFY(m_model
->isExpanded(0));
656 QVERIFY(!m_model
->isExpanded(1));
657 QVERIFY(m_model
->isExpanded(2));
658 QVERIFY(m_model
->isExpanded(3));
659 QVERIFY(!m_model
->isExpanded(4));
660 QVERIFY(m_model
->isConsistent());
664 * Renaming an expanded folder by prepending its name with a dot makes it
665 * hidden. Verify that this does not cause an inconsistent model state and
666 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
668 void KFileItemModelTest::testMakeExpandedItemHidden()
670 QSet
<QByteArray
> modelRoles
= m_model
->roles();
671 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
672 m_model
->setRoles(modelRoles
);
675 m_testDir
->createFile("1a/2a/3a");
676 m_testDir
->createFile("1a/2a/3b");
677 m_testDir
->createFile("1a/2b");
678 m_testDir
->createFile("1b");
680 m_model
->loadDirectory(m_testDir
->url());
681 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
683 // So far, the model contains only "1a/" and "1b".
684 QCOMPARE(m_model
->count(), 2);
685 m_model
->setExpanded(0, true);
686 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
688 // Now "1a/2a" and "1a/2b" have appeared.
689 QCOMPARE(m_model
->count(), 4);
690 m_model
->setExpanded(1, true);
691 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
692 QCOMPARE(m_model
->count(), 6);
694 // Rename "1a/2" and make it hidden.
695 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
696 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
698 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
699 bool ok
= job
->exec();
701 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
703 // "1a/2" and its subfolders have disappeared now.
704 QVERIFY(m_model
->isConsistent());
705 QCOMPARE(m_model
->count(), 3);
707 m_model
->setExpanded(0, false);
708 QCOMPARE(m_model
->count(), 2);
712 void KFileItemModelTest::testRemoveFilteredExpandedItems()
714 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
715 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
716 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
717 m_model
->setRoles(modelRoles
);
720 files
<< "folder/child" << "file"; // missing folders are created automatically
721 m_testDir
->createFiles(files
);
723 m_model
->loadDirectory(m_testDir
->url());
724 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
726 // So far, the model contains only "folder/" and "file".
727 QCOMPARE(m_model
->count(), 2);
728 QVERIFY(m_model
->isExpandable(0));
729 QVERIFY(!m_model
->isExpandable(1));
730 QVERIFY(!m_model
->isExpanded(0));
731 QVERIFY(!m_model
->isExpanded(1));
732 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
734 // Expand "folder" -> "folder/child" becomes visible.
735 m_model
->setExpanded(0, true);
736 QVERIFY(m_model
->isExpanded(0));
737 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
738 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
740 // Add a name filter.
741 m_model
->setNameFilter("f");
742 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
744 m_model
->setNameFilter("fo");
745 QCOMPARE(itemsInModel(), QStringList() << "folder");
747 // Remove all expanded items by changing the roles
748 m_model
->setRoles(originalModelRoles
);
749 QVERIFY(!m_model
->isExpanded(0));
750 QCOMPARE(itemsInModel(), QStringList() << "folder");
752 // Remove the name filter and verify that "folder/child" does not reappear.
753 m_model
->setNameFilter(QString());
754 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
757 void KFileItemModelTest::testSorting()
759 // Create some files with different sizes and modification times to check the different sorting options
760 QDateTime now
= QDateTime::currentDateTime();
762 QSet
<QByteArray
> roles
;
763 roles
.insert("text");
764 roles
.insert("isExpanded");
765 roles
.insert("isExpandable");
766 roles
.insert("expandedParentsCount");
767 m_model
->setRoles(roles
);
769 m_testDir
->createDir("c/c-2");
770 m_testDir
->createFile("c/c-2/c-3");
771 m_testDir
->createFile("c/c-1");
773 m_testDir
->createFile("a", "A file", now
.addDays(-3));
774 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
775 m_testDir
->createDir("c", now
.addDays(-2));
776 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
777 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
778 m_testDir
->createFile(".f");
780 m_model
->loadDirectory(m_testDir
->url());
781 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
783 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
784 m_model
->setExpanded(index
, true);
785 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
787 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
788 m_model
->setExpanded(index
, true);
789 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
791 // Default: Sort by Name, ascending
792 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
793 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
794 QVERIFY(m_model
->sortDirectoriesFirst());
795 QVERIFY(!m_model
->showHiddenFiles());
796 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
798 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
800 // Sort by Name, ascending, 'Sort Folders First' disabled
801 m_model
->setSortDirectoriesFirst(false);
802 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
803 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
804 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
805 QCOMPARE(spyItemsMoved
.count(), 1);
806 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
808 // Sort by Name, descending
809 m_model
->setSortDirectoriesFirst(true);
810 m_model
->setSortOrder(Qt::DescendingOrder
);
811 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
812 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
813 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
814 QCOMPARE(spyItemsMoved
.count(), 2);
815 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
816 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
818 // Sort by Date, descending
819 m_model
->setSortDirectoriesFirst(true);
820 m_model
->setSortRole("date");
821 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
822 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
823 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
824 QCOMPARE(spyItemsMoved
.count(), 1);
825 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
827 // Sort by Date, ascending
828 m_model
->setSortOrder(Qt::AscendingOrder
);
829 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
830 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
831 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
832 QCOMPARE(spyItemsMoved
.count(), 1);
833 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
835 // Sort by Date, ascending, 'Sort Folders First' disabled
836 m_model
->setSortDirectoriesFirst(false);
837 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
838 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
839 QVERIFY(!m_model
->sortDirectoriesFirst());
840 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
841 QCOMPARE(spyItemsMoved
.count(), 1);
842 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
844 // Sort by Name, ascending, 'Sort Folders First' disabled
845 m_model
->setSortRole("text");
846 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
847 QVERIFY(!m_model
->sortDirectoriesFirst());
848 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
849 QCOMPARE(spyItemsMoved
.count(), 1);
850 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
852 // Sort by Size, ascending, 'Sort Folders First' disabled
853 m_model
->setSortRole("size");
854 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
855 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
856 QVERIFY(!m_model
->sortDirectoriesFirst());
857 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
858 QCOMPARE(spyItemsMoved
.count(), 1);
859 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
861 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
862 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
863 "another signal", SkipSingle
);
864 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
866 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
867 m_model
->setSortDirectoriesFirst(true);
868 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
869 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
870 QVERIFY(m_model
->sortDirectoriesFirst());
871 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
872 QCOMPARE(spyItemsMoved
.count(), 0);
874 // Sort by Size, descending, 'Sort Folders First' enabled
875 m_model
->setSortOrder(Qt::DescendingOrder
);
876 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
877 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
878 QVERIFY(m_model
->sortDirectoriesFirst());
879 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
880 QCOMPARE(spyItemsMoved
.count(), 1);
881 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
883 // TODO: Sort by other roles; show/hide hidden files
886 void KFileItemModelTest::testIndexForKeyboardSearch()
889 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
890 m_testDir
->createFiles(files
);
892 m_model
->loadDirectory(m_testDir
->url());
893 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
895 // Search from index 0
896 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
897 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
898 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
899 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
900 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
901 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
902 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
903 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
904 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
905 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
906 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
908 // Start a search somewhere in the middle
909 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
910 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
911 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
912 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
914 // Test searches that go past the last item back to index 0
915 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
916 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
917 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
918 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
920 // Test searches that yield no result
921 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
922 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
923 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
924 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
925 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
927 // Test upper case searches (note that search is case insensitive)
928 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
929 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
930 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
931 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
933 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
936 void KFileItemModelTest::testNameFilter()
939 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
940 m_testDir
->createFiles(files
);
942 m_model
->loadDirectory(m_testDir
->url());
943 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
945 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
946 QCOMPARE(m_model
->count(), 3);
948 m_model
->setNameFilter("A2"); // Shows only A2
949 QCOMPARE(m_model
->count(), 1);
951 m_model
->setNameFilter("A2"); // Shows only A1
952 QCOMPARE(m_model
->count(), 1);
954 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
955 QCOMPARE(m_model
->count(), 2);
957 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
958 QCOMPARE(m_model
->count(), 2);
960 m_model
->setNameFilter(QString()); // Shows again all items
961 QCOMPARE(m_model
->count(), 5);
965 * Verifies that we do not crash when adding a KFileItem with an empty path.
966 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
967 * tried to always read the first character of the path, even if the path is empty.
969 void KFileItemModelTest::testEmptyPath()
971 QSet
<QByteArray
> roles
;
972 roles
.insert("text");
973 roles
.insert("isExpanded");
974 roles
.insert("isExpandable");
975 roles
.insert("expandedParentsCount");
976 m_model
->setRoles(roles
);
979 QVERIFY(emptyUrl
.path().isEmpty());
981 const KUrl
url("file:///test/");
984 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
985 m_model
->slotItemsAdded(emptyUrl
, items
);
986 m_model
->slotCompleted();
990 * Verifies that the 'isExpanded' state of folders does not change when the
991 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
993 void KFileItemModelTest::testRefreshExpandedItem()
995 QSet
<QByteArray
> modelRoles
= m_model
->roles();
996 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
997 m_model
->setRoles(modelRoles
);
1000 files
<< "a/1" << "a/2" << "3" << "4";
1001 m_testDir
->createFiles(files
);
1003 m_model
->loadDirectory(m_testDir
->url());
1004 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1005 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1007 m_model
->setExpanded(0, true);
1008 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1009 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1010 QVERIFY(m_model
->isExpanded(0));
1012 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1014 const KFileItem item
= m_model
->fileItem(0);
1015 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1016 QVERIFY(!spyItemsChanged
.isEmpty());
1018 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1019 QVERIFY(m_model
->isExpanded(0));
1023 * Verify that removing hidden files and folders from the model does not
1024 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1026 void KFileItemModelTest::testRemoveHiddenItems()
1028 m_testDir
->createDir(".a");
1029 m_testDir
->createDir(".b");
1030 m_testDir
->createDir("c");
1031 m_testDir
->createDir("d");
1032 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1034 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1035 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1037 m_model
->setShowHiddenFiles(true);
1038 m_model
->loadDirectory(m_testDir
->url());
1039 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1040 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1041 QCOMPARE(spyItemsInserted
.count(), 1);
1042 QCOMPARE(spyItemsRemoved
.count(), 0);
1043 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1044 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1046 m_model
->setShowHiddenFiles(false);
1047 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1048 QCOMPARE(spyItemsInserted
.count(), 0);
1049 QCOMPARE(spyItemsRemoved
.count(), 1);
1050 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1051 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1053 m_model
->setShowHiddenFiles(true);
1054 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1055 QCOMPARE(spyItemsInserted
.count(), 1);
1056 QCOMPARE(spyItemsRemoved
.count(), 0);
1057 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1058 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1061 QCOMPARE(itemsInModel(), QStringList());
1062 QCOMPARE(spyItemsInserted
.count(), 0);
1063 QCOMPARE(spyItemsRemoved
.count(), 1);
1064 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1065 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1067 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1068 // Verify that this does not make the model crash.
1069 m_model
->setShowHiddenFiles(false);
1073 * Verify that filtered items are removed when their parent is collapsed.
1075 void KFileItemModelTest::collapseParentOfHiddenItems()
1077 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1078 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1079 m_model
->setRoles(modelRoles
);
1082 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1083 m_testDir
->createFiles(files
);
1085 m_model
->loadDirectory(m_testDir
->url());
1086 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1087 QCOMPARE(m_model
->count(), 1); // Only "a/"
1090 m_model
->setExpanded(0, true);
1091 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1092 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1095 m_model
->setExpanded(1, true);
1096 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1097 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1100 m_model
->setExpanded(2, true);
1101 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1102 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"
1104 // Set a name filter that matches nothing -> only the expanded folders remain.
1105 m_model
->setNameFilter("xyz");
1106 QCOMPARE(m_model
->count(), 3);
1107 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1109 // Collapse the folder "a/".
1110 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1111 m_model
->setExpanded(0, false);
1112 QCOMPARE(spyItemsRemoved
.count(), 1);
1113 QCOMPARE(m_model
->count(), 1);
1114 QCOMPARE(itemsInModel(), QStringList() << "a");
1116 // Remove the filter -> no files should appear (and we should not get a crash).
1117 m_model
->setNameFilter(QString());
1118 QCOMPARE(m_model
->count(), 1);
1122 * Verify that filtered items are removed when their parent is deleted.
1124 void KFileItemModelTest::removeParentOfHiddenItems()
1126 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1127 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1128 m_model
->setRoles(modelRoles
);
1131 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1132 m_testDir
->createFiles(files
);
1134 m_model
->loadDirectory(m_testDir
->url());
1135 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1136 QCOMPARE(m_model
->count(), 1); // Only "a/"
1139 m_model
->setExpanded(0, true);
1140 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1141 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1144 m_model
->setExpanded(1, true);
1145 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1146 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1149 m_model
->setExpanded(2, true);
1150 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1151 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"
1153 // Set a name filter that matches nothing -> only the expanded folders remain.
1154 m_model
->setNameFilter("xyz");
1155 QCOMPARE(m_model
->count(), 3);
1156 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1158 // Simulate the deletion of the directory "a/b/".
1159 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1160 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1161 QCOMPARE(spyItemsRemoved
.count(), 1);
1162 QCOMPARE(m_model
->count(), 1);
1163 QCOMPARE(itemsInModel(), QStringList() << "a");
1165 // Remove the filter -> only the file "a/1" should appear.
1166 m_model
->setNameFilter(QString());
1167 QCOMPARE(m_model
->count(), 2);
1168 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1172 * Create a tree structure where parent-child relationships can not be
1173 * determined by parsing the URLs, and verify that KFileItemModel
1174 * handles them correctly.
1176 void KFileItemModelTest::testGeneralParentChildRelationships()
1178 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1179 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1180 m_model
->setRoles(modelRoles
);
1183 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1184 m_testDir
->createFiles(files
);
1186 m_model
->loadDirectory(m_testDir
->url());
1187 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1188 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1190 // Expand all folders.
1191 m_model
->setExpanded(0, true);
1192 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1193 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1195 m_model
->setExpanded(1, true);
1196 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1197 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1199 m_model
->setExpanded(3, true);
1200 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1201 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1203 m_model
->setExpanded(4, true);
1204 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1205 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1207 // Add some more children and grand-children.
1208 const KUrl parent1
= m_model
->fileItem(0).url();
1209 const KUrl parent2
= m_model
->fileItem(3).url();
1210 const KUrl realChild1
= m_model
->fileItem(1).url();
1211 const KUrl realChild2
= m_model
->fileItem(4).url();
1213 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1214 m_model
->slotCompleted();
1215 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1217 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1218 m_model
->slotCompleted();
1219 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1221 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1222 m_model
->slotCompleted();
1223 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1225 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1226 m_model
->slotCompleted();
1227 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1229 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1230 m_model
->slotCompleted();
1231 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1233 // Set a name filter that matches nothing -> only expanded folders remain.
1234 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1235 m_model
->setNameFilter("xyz");
1236 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1237 QCOMPARE(itemsRemovedSpy
.count(), 1);
1238 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1239 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1240 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1242 // Collapse "parent1".
1243 m_model
->setExpanded(0, false);
1244 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1245 QCOMPARE(itemsRemovedSpy
.count(), 1);
1246 arguments
= itemsRemovedSpy
.takeFirst();
1247 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1248 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1250 // Remove "parent2".
1251 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1252 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1253 QCOMPARE(itemsRemovedSpy
.count(), 1);
1254 arguments
= itemsRemovedSpy
.takeFirst();
1255 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1256 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1258 // Clear filter, verify that no items reappear.
1259 m_model
->setNameFilter(QString());
1260 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1263 void KFileItemModelTest::testNameRoleGroups()
1266 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1268 m_testDir
->createFiles(files
);
1270 m_model
->setGroupedSorting(true);
1271 m_model
->loadDirectory(m_testDir
->url());
1272 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1273 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1275 QList
<QPair
<int, QVariant
> > expectedGroups
;
1276 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1277 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1278 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1279 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1280 QCOMPARE(m_model
->groups(), expectedGroups
);
1282 // Rename d.txt to a.txt.
1283 QHash
<QByteArray
, QVariant
> data
;
1284 data
.insert("text", "a.txt");
1285 m_model
->setData(2, data
);
1286 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1287 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1289 expectedGroups
.clear();
1290 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1291 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1292 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1293 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1294 QCOMPARE(m_model
->groups(), expectedGroups
);
1296 // Rename c.txt to d.txt.
1297 data
.insert("text", "d.txt");
1298 m_model
->setData(2, data
);
1299 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1300 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1302 expectedGroups
.clear();
1303 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1304 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1305 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1306 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1307 QCOMPARE(m_model
->groups(), expectedGroups
);
1309 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1310 const KFileItem fileItemD
= m_model
->fileItem(2);
1311 KFileItem fileItemC
= fileItemD
;
1312 KUrl urlC
= fileItemC
.url();
1313 urlC
.setFileName("c.txt");
1314 fileItemC
.setUrl(urlC
);
1316 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1317 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1318 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1320 expectedGroups
.clear();
1321 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1322 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1323 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1324 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1325 QCOMPARE(m_model
->groups(), expectedGroups
);
1328 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1330 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1331 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1332 m_model
->setRoles(modelRoles
);
1335 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1337 m_testDir
->createFiles(files
);
1339 m_model
->setGroupedSorting(true);
1340 m_model
->loadDirectory(m_testDir
->url());
1341 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1342 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1344 QList
<QPair
<int, QVariant
> > expectedGroups
;
1345 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1346 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1347 QCOMPARE(m_model
->groups(), expectedGroups
);
1349 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1350 expectedGroups
.clear();
1351 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1352 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1354 m_model
->setExpanded(0, true);
1355 QVERIFY(m_model
->isExpanded(0));
1356 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1357 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1358 QCOMPARE(m_model
->groups(), expectedGroups
);
1360 m_model
->setExpanded(3, true);
1361 QVERIFY(m_model
->isExpanded(3));
1362 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1363 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1364 QCOMPARE(m_model
->groups(), expectedGroups
);
1367 QStringList
KFileItemModelTest::itemsInModel() const
1370 for (int i
= 0; i
< m_model
->count(); i
++) {
1371 items
<< m_model
->fileItem(i
).text();
1376 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1378 #include "kfileitemmodeltest.moc"