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(KItemRange
)
53 Q_DECLARE_METATYPE(KItemRangeList
)
54 Q_DECLARE_METATYPE(QList
<int>)
56 class KFileItemModelTest
: public QObject
64 void testDefaultRoles();
65 void testDefaultSortRole();
66 void testDefaultGroupedSorting();
68 void testRemoveItems();
69 void testDirLoadingCompleted();
71 void testSetDataWithModifiedSortRole_data();
72 void testSetDataWithModifiedSortRole();
73 void testChangeSortRole();
74 void testResortAfterChangingName();
75 void testModelConsistencyWhenInsertingItems();
76 void testItemRangeConsistencyWhenInsertingItems();
77 void testExpandItems();
78 void testExpandParentItems();
79 void testMakeExpandedItemHidden();
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
> originalModelRoles
= m_model
->roles();
502 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
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
);
610 // Remove all expanded items by changing the roles
612 m_model
->setRoles(originalModelRoles
);
613 QVERIFY(!m_model
->isExpanded(0));
614 QCOMPARE(m_model
->count(), 1);
615 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
617 QCOMPARE(spyRemoved
.count(), 1);
618 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
619 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
620 QVERIFY(m_model
->isConsistent());
623 void KFileItemModelTest::testExpandParentItems()
625 // Create a tree structure of folders:
633 QSet
<QByteArray
> modelRoles
= m_model
->roles();
634 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
635 m_model
->setRoles(modelRoles
);
638 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
639 m_testDir
->createFiles(files
);
641 m_model
->loadDirectory(m_testDir
->url());
642 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
644 // So far, the model contains only "a 1/" and "a2/".
645 QCOMPARE(m_model
->count(), 2);
646 QVERIFY(m_model
->expandedDirectories().empty());
648 // Expand the parents of "a2/b2/c2".
649 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
650 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
652 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
653 // It's important that only the parents of "a1/b1/c1" are expanded.
654 QCOMPARE(m_model
->count(), 4);
655 QVERIFY(!m_model
->isExpanded(0));
656 QVERIFY(m_model
->isExpanded(1));
657 QVERIFY(m_model
->isExpanded(2));
658 QVERIFY(!m_model
->isExpanded(3));
660 // Expand the parents of "a 1/b1".
661 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
662 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
664 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
665 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
666 QCOMPARE(m_model
->count(), 5);
667 QVERIFY(m_model
->isExpanded(0));
668 QVERIFY(!m_model
->isExpanded(1));
669 QVERIFY(m_model
->isExpanded(2));
670 QVERIFY(m_model
->isExpanded(3));
671 QVERIFY(!m_model
->isExpanded(4));
672 QVERIFY(m_model
->isConsistent());
675 m_model
->setExpanded(1, true);
676 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
677 QCOMPARE(m_model
->count(), 6);
678 QVERIFY(m_model
->isExpanded(0));
679 QVERIFY(m_model
->isExpanded(1));
680 QVERIFY(!m_model
->isExpanded(2));
681 QVERIFY(m_model
->isExpanded(3));
682 QVERIFY(m_model
->isExpanded(4));
683 QVERIFY(!m_model
->isExpanded(5));
684 QVERIFY(m_model
->isConsistent());
686 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
687 m_model
->setExpanded(1, false);
688 QCOMPARE(m_model
->count(), 5);
689 QVERIFY(m_model
->isExpanded(0));
690 QVERIFY(!m_model
->isExpanded(1));
691 QVERIFY(m_model
->isExpanded(2));
692 QVERIFY(m_model
->isExpanded(3));
693 QVERIFY(!m_model
->isExpanded(4));
694 QVERIFY(m_model
->isConsistent());
698 * Renaming an expanded folder by prepending its name with a dot makes it
699 * hidden. Verify that this does not cause an inconsistent model state and
700 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
702 void KFileItemModelTest::testMakeExpandedItemHidden()
704 QSet
<QByteArray
> modelRoles
= m_model
->roles();
705 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
706 m_model
->setRoles(modelRoles
);
709 m_testDir
->createFile("1a/2a/3a");
710 m_testDir
->createFile("1a/2a/3b");
711 m_testDir
->createFile("1a/2b");
712 m_testDir
->createFile("1b");
714 m_model
->loadDirectory(m_testDir
->url());
715 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
717 // So far, the model contains only "1a/" and "1b".
718 QCOMPARE(m_model
->count(), 2);
719 m_model
->setExpanded(0, true);
720 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
722 // Now "1a/2a" and "1a/2b" have appeared.
723 QCOMPARE(m_model
->count(), 4);
724 m_model
->setExpanded(1, true);
725 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
726 QCOMPARE(m_model
->count(), 6);
728 // Rename "1a/2" and make it hidden.
729 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
730 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
732 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
733 bool ok
= job
->exec();
735 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
737 // "1a/2" and its subfolders have disappeared now.
738 QVERIFY(m_model
->isConsistent());
739 QCOMPARE(m_model
->count(), 3);
741 m_model
->setExpanded(0, false);
742 QCOMPARE(m_model
->count(), 2);
746 void KFileItemModelTest::testSorting()
748 // Create some files with different sizes and modification times to check the different sorting options
749 QDateTime now
= QDateTime::currentDateTime();
751 QSet
<QByteArray
> roles
;
752 roles
.insert("text");
753 roles
.insert("isExpanded");
754 roles
.insert("isExpandable");
755 roles
.insert("expandedParentsCount");
756 m_model
->setRoles(roles
);
758 m_testDir
->createDir("c/c-2");
759 m_testDir
->createFile("c/c-2/c-3");
760 m_testDir
->createFile("c/c-1");
762 m_testDir
->createFile("a", "A file", now
.addDays(-3));
763 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
764 m_testDir
->createDir("c", now
.addDays(-2));
765 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
766 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
767 m_testDir
->createFile(".f");
769 m_model
->loadDirectory(m_testDir
->url());
770 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
772 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
773 m_model
->setExpanded(index
, true);
774 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
776 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
777 m_model
->setExpanded(index
, true);
778 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
780 // Default: Sort by Name, ascending
781 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
782 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
783 QVERIFY(m_model
->sortDirectoriesFirst());
784 QVERIFY(!m_model
->showHiddenFiles());
785 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
787 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
789 // Sort by Name, ascending, 'Sort Folders First' disabled
790 m_model
->setSortDirectoriesFirst(false);
791 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
792 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
793 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
794 QCOMPARE(spyItemsMoved
.count(), 1);
795 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
796 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
798 // Sort by Name, descending
799 m_model
->setSortDirectoriesFirst(true);
800 m_model
->setSortOrder(Qt::DescendingOrder
);
801 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
802 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
803 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
804 QCOMPARE(spyItemsMoved
.count(), 2);
805 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
806 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
807 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
808 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
810 // Sort by Date, descending
811 m_model
->setSortDirectoriesFirst(true);
812 m_model
->setSortRole("date");
813 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
814 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
815 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
816 QCOMPARE(spyItemsMoved
.count(), 1);
817 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
818 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
820 // Sort by Date, ascending
821 m_model
->setSortOrder(Qt::AscendingOrder
);
822 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
823 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
824 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
825 QCOMPARE(spyItemsMoved
.count(), 1);
826 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
827 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
829 // Sort by Date, ascending, 'Sort Folders First' disabled
830 m_model
->setSortDirectoriesFirst(false);
831 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
832 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
833 QVERIFY(!m_model
->sortDirectoriesFirst());
834 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
835 QCOMPARE(spyItemsMoved
.count(), 1);
836 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
837 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
839 // Sort by Name, ascending, 'Sort Folders First' disabled
840 m_model
->setSortRole("text");
841 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
842 QVERIFY(!m_model
->sortDirectoriesFirst());
843 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
844 QCOMPARE(spyItemsMoved
.count(), 1);
845 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
846 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
848 // Sort by Size, ascending, 'Sort Folders First' disabled
849 m_model
->setSortRole("size");
850 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
851 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
852 QVERIFY(!m_model
->sortDirectoriesFirst());
853 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
854 QCOMPARE(spyItemsMoved
.count(), 1);
855 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
856 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
858 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
859 m_model
->setSortDirectoriesFirst(true);
860 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
861 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
862 QVERIFY(m_model
->sortDirectoriesFirst());
863 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
864 QCOMPARE(spyItemsMoved
.count(), 0);
866 // Sort by Size, descending, 'Sort Folders First' enabled
867 m_model
->setSortOrder(Qt::DescendingOrder
);
868 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
869 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
870 QVERIFY(m_model
->sortDirectoriesFirst());
871 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
872 QCOMPARE(spyItemsMoved
.count(), 1);
873 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
874 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
876 // TODO: Sort by other roles; show/hide hidden files
879 void KFileItemModelTest::testIndexForKeyboardSearch()
882 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
883 m_testDir
->createFiles(files
);
885 m_model
->loadDirectory(m_testDir
->url());
886 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
888 // Search from index 0
889 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
890 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
891 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
892 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
893 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
894 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
895 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
896 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
897 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
898 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
899 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
901 // Start a search somewhere in the middle
902 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
903 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
904 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
905 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
907 // Test searches that go past the last item back to index 0
908 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
909 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
910 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
911 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
913 // Test searches that yield no result
914 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
915 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
916 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
917 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
918 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
920 // Test upper case searches (note that search is case insensitive)
921 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
922 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
923 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
924 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
926 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
929 void KFileItemModelTest::testNameFilter()
932 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
933 m_testDir
->createFiles(files
);
935 m_model
->loadDirectory(m_testDir
->url());
936 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
938 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
939 QCOMPARE(m_model
->count(), 3);
941 m_model
->setNameFilter("A2"); // Shows only A2
942 QCOMPARE(m_model
->count(), 1);
944 m_model
->setNameFilter("A2"); // Shows only A1
945 QCOMPARE(m_model
->count(), 1);
947 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
948 QCOMPARE(m_model
->count(), 2);
950 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
951 QCOMPARE(m_model
->count(), 2);
953 m_model
->setNameFilter(QString()); // Shows again all items
954 QCOMPARE(m_model
->count(), 5);
958 * Verifies that we do not crash when adding a KFileItem with an empty path.
959 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
960 * tried to always read the first character of the path, even if the path is empty.
962 void KFileItemModelTest::testEmptyPath()
964 QSet
<QByteArray
> roles
;
965 roles
.insert("text");
966 roles
.insert("isExpanded");
967 roles
.insert("isExpandable");
968 roles
.insert("expandedParentsCount");
969 m_model
->setRoles(roles
);
972 QVERIFY(emptyUrl
.path().isEmpty());
974 const KUrl
url("file:///test/");
977 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
978 m_model
->slotItemsAdded(emptyUrl
, items
);
979 m_model
->slotCompleted();
983 * Verifies that the 'isExpanded' state of folders does not change when the
984 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
986 void KFileItemModelTest::testRefreshExpandedItem()
988 QSet
<QByteArray
> modelRoles
= m_model
->roles();
989 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
990 m_model
->setRoles(modelRoles
);
993 files
<< "a/1" << "a/2" << "3" << "4";
994 m_testDir
->createFiles(files
);
996 m_model
->loadDirectory(m_testDir
->url());
997 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
998 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1000 m_model
->setExpanded(0, true);
1001 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1002 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1003 QVERIFY(m_model
->isExpanded(0));
1005 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1007 const KFileItem item
= m_model
->fileItem(0);
1008 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1009 QVERIFY(!spyItemsChanged
.isEmpty());
1011 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1012 QVERIFY(m_model
->isExpanded(0));
1016 * Verify that removing hidden files and folders from the model does not
1017 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1019 void KFileItemModelTest::testRemoveHiddenItems()
1021 m_testDir
->createDir(".a");
1022 m_testDir
->createDir(".b");
1023 m_testDir
->createDir("c");
1024 m_testDir
->createDir("d");
1025 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1027 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1028 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1030 m_model
->setShowHiddenFiles(true);
1031 m_model
->loadDirectory(m_testDir
->url());
1032 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1033 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1034 QCOMPARE(spyItemsInserted
.count(), 1);
1035 QCOMPARE(spyItemsRemoved
.count(), 0);
1036 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1037 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1039 m_model
->setShowHiddenFiles(false);
1040 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1041 QCOMPARE(spyItemsInserted
.count(), 0);
1042 QCOMPARE(spyItemsRemoved
.count(), 1);
1043 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1044 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1046 m_model
->setShowHiddenFiles(true);
1047 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1048 QCOMPARE(spyItemsInserted
.count(), 1);
1049 QCOMPARE(spyItemsRemoved
.count(), 0);
1050 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1051 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1054 QCOMPARE(itemsInModel(), QStringList());
1055 QCOMPARE(spyItemsInserted
.count(), 0);
1056 QCOMPARE(spyItemsRemoved
.count(), 1);
1057 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1058 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1060 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1061 // Verify that this does not make the model crash.
1062 m_model
->setShowHiddenFiles(false);
1066 * Verify that filtered items are removed when their parent is collapsed.
1068 void KFileItemModelTest::collapseParentOfHiddenItems()
1070 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1071 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1072 m_model
->setRoles(modelRoles
);
1075 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1076 m_testDir
->createFiles(files
);
1078 m_model
->loadDirectory(m_testDir
->url());
1079 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1080 QCOMPARE(m_model
->count(), 1); // Only "a/"
1083 m_model
->setExpanded(0, true);
1084 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1085 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1088 m_model
->setExpanded(1, true);
1089 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1090 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1093 m_model
->setExpanded(2, true);
1094 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1095 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"
1097 // Set a name filter that matches nothing -> only the expanded folders remain.
1098 m_model
->setNameFilter("xyz");
1099 QCOMPARE(m_model
->count(), 3);
1100 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1102 // Collapse the folder "a/".
1103 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1104 m_model
->setExpanded(0, false);
1105 QCOMPARE(spyItemsRemoved
.count(), 1);
1106 QCOMPARE(m_model
->count(), 1);
1107 QCOMPARE(itemsInModel(), QStringList() << "a");
1109 // Remove the filter -> no files should appear (and we should not get a crash).
1110 m_model
->setNameFilter(QString());
1111 QCOMPARE(m_model
->count(), 1);
1115 * Verify that filtered items are removed when their parent is deleted.
1117 void KFileItemModelTest::removeParentOfHiddenItems()
1119 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1120 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1121 m_model
->setRoles(modelRoles
);
1124 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1125 m_testDir
->createFiles(files
);
1127 m_model
->loadDirectory(m_testDir
->url());
1128 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1129 QCOMPARE(m_model
->count(), 1); // Only "a/"
1132 m_model
->setExpanded(0, true);
1133 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1134 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1137 m_model
->setExpanded(1, true);
1138 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1139 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1142 m_model
->setExpanded(2, true);
1143 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1144 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"
1146 // Set a name filter that matches nothing -> only the expanded folders remain.
1147 m_model
->setNameFilter("xyz");
1148 QCOMPARE(m_model
->count(), 3);
1149 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1151 // Simulate the deletion of the directory "a/b/".
1152 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1153 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1154 QCOMPARE(spyItemsRemoved
.count(), 1);
1155 QCOMPARE(m_model
->count(), 1);
1156 QCOMPARE(itemsInModel(), QStringList() << "a");
1158 // Remove the filter -> only the file "a/1" should appear.
1159 m_model
->setNameFilter(QString());
1160 QCOMPARE(m_model
->count(), 2);
1161 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1165 * Create a tree structure where parent-child relationships can not be
1166 * determined by parsing the URLs, and verify that KFileItemModel
1167 * handles them correctly.
1169 void KFileItemModelTest::testGeneralParentChildRelationships()
1171 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1172 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1173 m_model
->setRoles(modelRoles
);
1176 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1177 m_testDir
->createFiles(files
);
1179 m_model
->loadDirectory(m_testDir
->url());
1180 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1181 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1183 // Expand all folders.
1184 m_model
->setExpanded(0, true);
1185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1186 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1188 m_model
->setExpanded(1, true);
1189 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1190 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1192 m_model
->setExpanded(3, true);
1193 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1194 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1196 m_model
->setExpanded(4, true);
1197 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1198 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1200 // Add some more children and grand-children.
1201 const KUrl parent1
= m_model
->fileItem(0).url();
1202 const KUrl parent2
= m_model
->fileItem(3).url();
1203 const KUrl realChild1
= m_model
->fileItem(1).url();
1204 const KUrl realChild2
= m_model
->fileItem(4).url();
1206 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1207 m_model
->slotCompleted();
1208 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1210 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1211 m_model
->slotCompleted();
1212 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1214 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1215 m_model
->slotCompleted();
1216 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1218 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1219 m_model
->slotCompleted();
1220 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1222 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1223 m_model
->slotCompleted();
1224 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1226 // Set a name filter that matches nothing -> only expanded folders remain.
1227 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1228 m_model
->setNameFilter("xyz");
1229 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1230 QCOMPARE(itemsRemovedSpy
.count(), 1);
1231 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1232 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1233 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1235 // Collapse "parent1".
1236 m_model
->setExpanded(0, false);
1237 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1238 QCOMPARE(itemsRemovedSpy
.count(), 1);
1239 arguments
= itemsRemovedSpy
.takeFirst();
1240 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1241 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1243 // Remove "parent2".
1244 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1245 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1246 QCOMPARE(itemsRemovedSpy
.count(), 1);
1247 arguments
= itemsRemovedSpy
.takeFirst();
1248 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1249 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1251 // Clear filter, verify that no items reappear.
1252 m_model
->setNameFilter(QString());
1253 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1256 void KFileItemModelTest::testNameRoleGroups()
1259 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1261 m_testDir
->createFiles(files
);
1263 m_model
->setGroupedSorting(true);
1264 m_model
->loadDirectory(m_testDir
->url());
1265 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1266 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1268 QList
<QPair
<int, QVariant
> > expectedGroups
;
1269 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1270 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1271 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1272 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1273 QCOMPARE(m_model
->groups(), expectedGroups
);
1275 // Rename d.txt to a.txt.
1276 QHash
<QByteArray
, QVariant
> data
;
1277 data
.insert("text", "a.txt");
1278 m_model
->setData(2, data
);
1279 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1280 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1282 expectedGroups
.clear();
1283 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1284 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1285 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1286 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1287 QCOMPARE(m_model
->groups(), expectedGroups
);
1289 // Rename c.txt to d.txt.
1290 data
.insert("text", "d.txt");
1291 m_model
->setData(2, data
);
1292 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1293 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1295 expectedGroups
.clear();
1296 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1297 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1298 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1299 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1300 QCOMPARE(m_model
->groups(), expectedGroups
);
1302 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1303 const KFileItem fileItemD
= m_model
->fileItem(2);
1304 KFileItem fileItemC
= fileItemD
;
1305 KUrl urlC
= fileItemC
.url();
1306 urlC
.setFileName("c.txt");
1307 fileItemC
.setUrl(urlC
);
1309 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1310 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1311 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1313 expectedGroups
.clear();
1314 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1315 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1316 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1317 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1318 QCOMPARE(m_model
->groups(), expectedGroups
);
1321 QStringList
KFileItemModelTest::itemsInModel() const
1324 for (int i
= 0; i
< m_model
->count(); i
++) {
1325 items
<< m_model
->data(i
).value("text").toString();
1330 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1332 #include "kfileitemmodeltest.moc"