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();
80 void testIndexForKeyboardSearch();
81 void testNameFilter();
83 void testRefreshExpandedItem();
84 void testRemoveHiddenItems();
85 void collapseParentOfHiddenItems();
86 void removeParentOfHiddenItems();
87 void testGeneralParentChildRelationships();
88 void testNameRoleGroups();
91 QStringList
itemsInModel() const;
94 KFileItemModel
* m_model
;
98 void KFileItemModelTest::init()
100 // The item-model tests result in a huge number of debugging
101 // output from kdelibs. Only show critical and fatal messages.
102 qInstallMsgHandler(myMessageOutput
);
104 qRegisterMetaType
<KItemRange
>("KItemRange");
105 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
106 qRegisterMetaType
<KFileItemList
>("KFileItemList");
108 m_testDir
= new TestDir();
109 m_model
= new KFileItemModel();
110 m_model
->m_dirLister
->setAutoUpdate(false);
112 // Reduce the timer interval to make the test run faster.
113 m_model
->m_resortAllItemsTimer
->setInterval(0);
116 void KFileItemModelTest::cleanup()
125 void KFileItemModelTest::testDefaultRoles()
127 const QSet
<QByteArray
> roles
= m_model
->roles();
128 QCOMPARE(roles
.count(), 3);
129 QVERIFY(roles
.contains("text"));
130 QVERIFY(roles
.contains("isDir"));
131 QVERIFY(roles
.contains("isLink"));
134 void KFileItemModelTest::testDefaultSortRole()
136 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
139 files
<< "c.txt" << "a.txt" << "b.txt";
141 m_testDir
->createFiles(files
);
143 m_model
->loadDirectory(m_testDir
->url());
144 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
146 QCOMPARE(m_model
->count(), 3);
147 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
148 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
149 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
152 void KFileItemModelTest::testDefaultGroupedSorting()
154 QCOMPARE(m_model
->groupedSorting(), false);
157 void KFileItemModelTest::testNewItems()
160 files
<< "a.txt" << "b.txt" << "c.txt";
161 m_testDir
->createFiles(files
);
163 m_model
->loadDirectory(m_testDir
->url());
164 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
166 QCOMPARE(m_model
->count(), 3);
168 QVERIFY(m_model
->isConsistent());
171 void KFileItemModelTest::testRemoveItems()
173 m_testDir
->createFile("a.txt");
174 m_testDir
->createFile("b.txt");
175 m_model
->loadDirectory(m_testDir
->url());
176 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
177 QCOMPARE(m_model
->count(), 2);
178 QVERIFY(m_model
->isConsistent());
180 m_testDir
->removeFile("a.txt");
181 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
182 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
183 QCOMPARE(m_model
->count(), 1);
184 QVERIFY(m_model
->isConsistent());
187 void KFileItemModelTest::testDirLoadingCompleted()
189 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
190 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
191 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
193 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
195 m_model
->loadDirectory(m_testDir
->url());
196 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
197 QCOMPARE(loadingCompletedSpy
.count(), 1);
198 QCOMPARE(itemsInsertedSpy
.count(), 1);
199 QCOMPARE(itemsRemovedSpy
.count(), 0);
200 QCOMPARE(m_model
->count(), 3);
202 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
203 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
204 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
205 QCOMPARE(loadingCompletedSpy
.count(), 2);
206 QCOMPARE(itemsInsertedSpy
.count(), 2);
207 QCOMPARE(itemsRemovedSpy
.count(), 0);
208 QCOMPARE(m_model
->count(), 5);
210 m_testDir
->removeFile("a.txt");
211 m_testDir
->createFile("f.txt");
212 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
213 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
214 QCOMPARE(loadingCompletedSpy
.count(), 3);
215 QCOMPARE(itemsInsertedSpy
.count(), 3);
216 QCOMPARE(itemsRemovedSpy
.count(), 1);
217 QCOMPARE(m_model
->count(), 5);
219 m_testDir
->removeFile("b.txt");
220 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
221 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
222 QCOMPARE(loadingCompletedSpy
.count(), 4);
223 QCOMPARE(itemsInsertedSpy
.count(), 3);
224 QCOMPARE(itemsRemovedSpy
.count(), 2);
225 QCOMPARE(m_model
->count(), 4);
227 QVERIFY(m_model
->isConsistent());
230 void KFileItemModelTest::testSetData()
232 m_testDir
->createFile("a.txt");
234 m_model
->loadDirectory(m_testDir
->url());
235 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
237 QHash
<QByteArray
, QVariant
> values
;
238 values
.insert("customRole1", "Test1");
239 values
.insert("customRole2", "Test2");
241 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
242 m_model
->setData(0, values
);
243 QCOMPARE(itemsChangedSpy
.count(), 1);
245 values
= m_model
->data(0);
246 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
247 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
248 QVERIFY(m_model
->isConsistent());
251 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
253 QTest::addColumn
<int>("changedIndex");
254 QTest::addColumn
<int>("changedRating");
255 QTest::addColumn
<bool>("expectMoveSignal");
256 QTest::addColumn
<int>("ratingIndex0");
257 QTest::addColumn
<int>("ratingIndex1");
258 QTest::addColumn
<int>("ratingIndex2");
261 // Index 0 = rating 2
262 // Index 1 = rating 4
263 // Index 2 = rating 6
265 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
266 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
267 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
269 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
270 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
271 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
274 void KFileItemModelTest::testSetDataWithModifiedSortRole()
276 QFETCH(int, changedIndex
);
277 QFETCH(int, changedRating
);
278 QFETCH(bool, expectMoveSignal
);
279 QFETCH(int, ratingIndex0
);
280 QFETCH(int, ratingIndex1
);
281 QFETCH(int, ratingIndex2
);
283 // Changing the value of a sort-role must result in
284 // a reordering of the items.
285 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
286 m_model
->setSortRole("rating");
287 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
290 files
<< "a.txt" << "b.txt" << "c.txt";
291 m_testDir
->createFiles(files
);
293 m_model
->loadDirectory(m_testDir
->url());
294 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
296 // Fill the "rating" role of each file:
301 QHash
<QByteArray
, QVariant
> ratingA
;
302 ratingA
.insert("rating", 2);
303 m_model
->setData(0, ratingA
);
305 QHash
<QByteArray
, QVariant
> ratingB
;
306 ratingB
.insert("rating", 4);
307 m_model
->setData(1, ratingB
);
309 QHash
<QByteArray
, QVariant
> ratingC
;
310 ratingC
.insert("rating", 6);
311 m_model
->setData(2, ratingC
);
313 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
314 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
315 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
317 // Now change the rating from a.txt. This usually results
318 // in reordering of the items.
319 QHash
<QByteArray
, QVariant
> rating
;
320 rating
.insert("rating", changedRating
);
321 m_model
->setData(changedIndex
, rating
);
323 if (expectMoveSignal
) {
324 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
327 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
328 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
329 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
330 QVERIFY(m_model
->isConsistent());
333 void KFileItemModelTest::testChangeSortRole()
335 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
338 files
<< "a.txt" << "b.jpg" << "c.txt";
339 m_testDir
->createFiles(files
);
341 m_model
->loadDirectory(m_testDir
->url());
342 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
343 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
345 // Simulate that KFileItemModelRolesUpdater determines the mime type.
346 // Resorting the files by 'type' will only work immediately if their
347 // mime types are known.
348 for (int index
= 0; index
< m_model
->count(); ++index
) {
349 m_model
->fileItem(index
).determineMimeType();
352 // Now: sort by type.
353 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
354 m_model
->setSortRole("type");
355 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
356 QVERIFY(!spyItemsMoved
.isEmpty());
358 // The actual order of the files might depend on the translation of the
359 // result of KFileItem::mimeComment() in the user's language.
360 QStringList version1
;
361 version1
<< "b.jpg" << "a.txt" << "c.txt";
363 QStringList version2
;
364 version2
<< "a.txt" << "c.txt" << "b.jpg";
366 const bool ok1
= (itemsInModel() == version1
);
367 const bool ok2
= (itemsInModel() == version2
);
372 void KFileItemModelTest::testResortAfterChangingName()
374 // We sort by size in a directory where all files have the same size.
375 // Therefore, the files are sorted by their names.
376 m_model
->setSortRole("size");
379 files
<< "a.txt" << "b.txt" << "c.txt";
380 m_testDir
->createFiles(files
);
382 m_model
->loadDirectory(m_testDir
->url());
383 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
384 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
386 // We rename a.txt to d.txt. Even though the size has not changed at all,
387 // the model must re-sort the items.
388 QHash
<QByteArray
, QVariant
> data
;
389 data
.insert("text", "d.txt");
390 m_model
->setData(0, data
);
392 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
393 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
396 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
398 //QSKIP("Temporary disabled", SkipSingle);
400 // KFileItemModel prevents that inserting a punch of items sequentially
401 // results in an itemsInserted()-signal for each item. Instead internally
402 // a timeout is given that collects such operations and results in only
403 // one itemsInserted()-signal. However in this test we want to stress
404 // KFileItemModel to do a lot of insert operation and hence decrease
405 // the timeout to 1 millisecond.
406 m_testDir
->createFile("1");
407 m_model
->loadDirectory(m_testDir
->url());
408 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
409 QCOMPARE(m_model
->count(), 1);
411 // Insert 10 items for 20 times. After each insert operation the model consistency
413 QSet
<int> insertedItems
;
414 for (int i
= 0; i
< 20; ++i
) {
415 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
417 for (int j
= 0; j
< 10; ++j
) {
418 int itemName
= qrand();
419 while (insertedItems
.contains(itemName
)) {
422 insertedItems
.insert(itemName
);
424 m_testDir
->createFile(QString::number(itemName
));
427 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
428 if (spy
.count() == 0) {
429 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
432 QVERIFY(m_model
->isConsistent());
435 QCOMPARE(m_model
->count(), 201);
438 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
441 files
<< "B" << "E" << "G";
442 m_testDir
->createFiles(files
);
444 // Due to inserting the 3 items one item-range with index == 0 and
445 // count == 3 must be given
446 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
447 m_model
->loadDirectory(m_testDir
->url());
448 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
450 QCOMPARE(spy1
.count(), 1);
451 QList
<QVariant
> arguments
= spy1
.takeFirst();
452 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
453 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
455 // The indexes of the item-ranges must always be related to the model before
456 // the items have been inserted. Having:
459 // and inserting A, C, D, F the resulting model will be:
462 // and the item-ranges must be:
463 // index: 0, count: 1 for A
464 // index: 1, count: 2 for B, C
465 // index: 2, count: 1 for G
468 files
<< "A" << "C" << "D" << "F";
469 m_testDir
->createFiles(files
);
471 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
472 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
473 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
475 QCOMPARE(spy2
.count(), 1);
476 arguments
= spy2
.takeFirst();
477 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
478 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
481 void KFileItemModelTest::testExpandItems()
483 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
484 // Besides testing the basic item expansion functionality, the test makes sure that
485 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
486 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
487 // first three characters.
488 QSet
<QByteArray
> modelRoles
= m_model
->roles();
489 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
490 m_model
->setRoles(modelRoles
);
493 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
494 m_testDir
->createFiles(files
);
496 // Store the URLs of all folders in a set.
497 QSet
<KUrl
> allFolders
;
498 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
500 m_model
->loadDirectory(m_testDir
->url());
501 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
503 // So far, the model contains only "a/"
504 QCOMPARE(m_model
->count(), 1);
505 QVERIFY(m_model
->isExpandable(0));
506 QVERIFY(!m_model
->isExpanded(0));
507 QVERIFY(m_model
->expandedDirectories().empty());
509 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
511 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
512 m_model
->setExpanded(0, true);
513 QVERIFY(m_model
->isExpanded(0));
514 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
515 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
516 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
518 QCOMPARE(spyInserted
.count(), 1);
519 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
520 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
522 QVERIFY(m_model
->isExpandable(1));
523 QVERIFY(!m_model
->isExpanded(1));
524 QVERIFY(m_model
->isExpandable(2));
525 QVERIFY(!m_model
->isExpanded(2));
527 // Expand the folder "a/a/" -> "a/a/1" becomes visible
528 m_model
->setExpanded(1, true);
529 QVERIFY(m_model
->isExpanded(1));
530 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
531 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
532 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
534 QCOMPARE(spyInserted
.count(), 1);
535 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
536 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
538 QVERIFY(!m_model
->isExpandable(2));
539 QVERIFY(!m_model
->isExpanded(2));
541 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
542 m_model
->setExpanded(3, true);
543 QVERIFY(m_model
->isExpanded(3));
544 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
545 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
546 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
548 QCOMPARE(spyInserted
.count(), 1);
549 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
550 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
552 QVERIFY(!m_model
->isExpandable(4));
553 QVERIFY(!m_model
->isExpanded(4));
555 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
557 // Collapse the top-level folder -> all other items should disappear
558 m_model
->setExpanded(0, false);
559 QVERIFY(!m_model
->isExpanded(0));
560 QCOMPARE(m_model
->count(), 1);
561 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
563 QCOMPARE(spyRemoved
.count(), 1);
564 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
565 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
566 QVERIFY(m_model
->isConsistent());
568 // Clear the model, reload the folder and try to restore the expanded folders.
570 QCOMPARE(m_model
->count(), 0);
571 QVERIFY(m_model
->expandedDirectories().empty());
573 m_model
->loadDirectory(m_testDir
->url());
574 m_model
->restoreExpandedDirectories(allFolders
);
575 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
576 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
577 QVERIFY(m_model
->isExpanded(0));
578 QVERIFY(m_model
->isExpanded(1));
579 QVERIFY(!m_model
->isExpanded(2));
580 QVERIFY(m_model
->isExpanded(3));
581 QVERIFY(!m_model
->isExpanded(4));
582 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
583 QVERIFY(m_model
->isConsistent());
585 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
586 // This is how DolphinView restores the expanded folders when navigating in history.
587 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
588 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
589 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
590 m_model
->restoreExpandedDirectories(allFolders
);
591 m_model
->loadDirectory(m_testDir
->url());
592 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
593 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
594 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
597 void KFileItemModelTest::testExpandParentItems()
599 // Create a tree structure of folders:
607 QSet
<QByteArray
> modelRoles
= m_model
->roles();
608 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
609 m_model
->setRoles(modelRoles
);
612 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
613 m_testDir
->createFiles(files
);
615 m_model
->loadDirectory(m_testDir
->url());
616 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
618 // So far, the model contains only "a 1/" and "a2/".
619 QCOMPARE(m_model
->count(), 2);
620 QVERIFY(m_model
->expandedDirectories().empty());
622 // Expand the parents of "a2/b2/c2".
623 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
624 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
626 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
627 // It's important that only the parents of "a1/b1/c1" are expanded.
628 QCOMPARE(m_model
->count(), 4);
629 QVERIFY(!m_model
->isExpanded(0));
630 QVERIFY(m_model
->isExpanded(1));
631 QVERIFY(m_model
->isExpanded(2));
632 QVERIFY(!m_model
->isExpanded(3));
634 // Expand the parents of "a 1/b1".
635 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
636 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
638 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
639 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
640 QCOMPARE(m_model
->count(), 5);
641 QVERIFY(m_model
->isExpanded(0));
642 QVERIFY(!m_model
->isExpanded(1));
643 QVERIFY(m_model
->isExpanded(2));
644 QVERIFY(m_model
->isExpanded(3));
645 QVERIFY(!m_model
->isExpanded(4));
646 QVERIFY(m_model
->isConsistent());
650 * Renaming an expanded folder by prepending its name with a dot makes it
651 * hidden. Verify that this does not cause an inconsistent model state and
652 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
654 void KFileItemModelTest::testMakeExpandedItemHidden()
656 QSet
<QByteArray
> modelRoles
= m_model
->roles();
657 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
658 m_model
->setRoles(modelRoles
);
661 m_testDir
->createFile("1a/2a/3a");
662 m_testDir
->createFile("1a/2a/3b");
663 m_testDir
->createFile("1a/2b");
664 m_testDir
->createFile("1b");
666 m_model
->loadDirectory(m_testDir
->url());
667 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
669 // So far, the model contains only "1a/" and "1b".
670 QCOMPARE(m_model
->count(), 2);
671 m_model
->setExpanded(0, true);
672 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
674 // Now "1a/2a" and "1a/2b" have appeared.
675 QCOMPARE(m_model
->count(), 4);
676 m_model
->setExpanded(1, true);
677 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
678 QCOMPARE(m_model
->count(), 6);
680 // Rename "1a/2" and make it hidden.
681 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
682 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
684 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
685 bool ok
= job
->exec();
687 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
689 // "1a/2" and its subfolders have disappeared now.
690 QVERIFY(m_model
->isConsistent());
691 QCOMPARE(m_model
->count(), 3);
693 m_model
->setExpanded(0, false);
694 QCOMPARE(m_model
->count(), 2);
698 void KFileItemModelTest::testSorting()
700 // Create some files with different sizes and modification times to check the different sorting options
701 QDateTime now
= QDateTime::currentDateTime();
703 QSet
<QByteArray
> roles
;
704 roles
.insert("text");
705 roles
.insert("isExpanded");
706 roles
.insert("isExpandable");
707 roles
.insert("expandedParentsCount");
708 m_model
->setRoles(roles
);
710 m_testDir
->createDir("c/c-2");
711 m_testDir
->createFile("c/c-2/c-3");
712 m_testDir
->createFile("c/c-1");
714 m_testDir
->createFile("a", "A file", now
.addDays(-3));
715 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
716 m_testDir
->createDir("c", now
.addDays(-2));
717 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
718 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
719 m_testDir
->createFile(".f");
721 m_model
->loadDirectory(m_testDir
->url());
722 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
724 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
725 m_model
->setExpanded(index
, true);
726 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
728 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
729 m_model
->setExpanded(index
, true);
730 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
732 // Default: Sort by Name, ascending
733 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
734 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
735 QVERIFY(m_model
->sortDirectoriesFirst());
736 QVERIFY(!m_model
->showHiddenFiles());
737 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
739 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
741 // Sort by Name, ascending, 'Sort Folders First' disabled
742 m_model
->setSortDirectoriesFirst(false);
743 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
744 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
745 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
746 QCOMPARE(spyItemsMoved
.count(), 1);
747 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
749 // Sort by Name, descending
750 m_model
->setSortDirectoriesFirst(true);
751 m_model
->setSortOrder(Qt::DescendingOrder
);
752 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
753 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
754 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
755 QCOMPARE(spyItemsMoved
.count(), 2);
756 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
757 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
759 // Sort by Date, descending
760 m_model
->setSortDirectoriesFirst(true);
761 m_model
->setSortRole("date");
762 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
763 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
764 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
765 QCOMPARE(spyItemsMoved
.count(), 1);
766 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
768 // Sort by Date, ascending
769 m_model
->setSortOrder(Qt::AscendingOrder
);
770 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
771 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
772 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
773 QCOMPARE(spyItemsMoved
.count(), 1);
774 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
776 // Sort by Date, ascending, 'Sort Folders First' disabled
777 m_model
->setSortDirectoriesFirst(false);
778 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
779 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
780 QVERIFY(!m_model
->sortDirectoriesFirst());
781 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
782 QCOMPARE(spyItemsMoved
.count(), 1);
783 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
785 // Sort by Name, ascending, 'Sort Folders First' disabled
786 m_model
->setSortRole("text");
787 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
788 QVERIFY(!m_model
->sortDirectoriesFirst());
789 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
790 QCOMPARE(spyItemsMoved
.count(), 1);
791 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
793 // Sort by Size, ascending, 'Sort Folders First' disabled
794 m_model
->setSortRole("size");
795 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
796 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
797 QVERIFY(!m_model
->sortDirectoriesFirst());
798 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
799 QCOMPARE(spyItemsMoved
.count(), 1);
800 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
802 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
803 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
804 "another signal", SkipSingle
);
805 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
807 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
808 m_model
->setSortDirectoriesFirst(true);
809 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
810 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
811 QVERIFY(m_model
->sortDirectoriesFirst());
812 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
813 QCOMPARE(spyItemsMoved
.count(), 0);
815 // Sort by Size, descending, 'Sort Folders First' enabled
816 m_model
->setSortOrder(Qt::DescendingOrder
);
817 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
818 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
819 QVERIFY(m_model
->sortDirectoriesFirst());
820 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
821 QCOMPARE(spyItemsMoved
.count(), 1);
822 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
824 // TODO: Sort by other roles; show/hide hidden files
827 void KFileItemModelTest::testIndexForKeyboardSearch()
830 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
831 m_testDir
->createFiles(files
);
833 m_model
->loadDirectory(m_testDir
->url());
834 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
836 // Search from index 0
837 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
838 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
839 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
840 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
841 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
842 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
843 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
844 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
845 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
846 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
847 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
849 // Start a search somewhere in the middle
850 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
851 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
852 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
853 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
855 // Test searches that go past the last item back to index 0
856 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
857 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
858 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
859 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
861 // Test searches that yield no result
862 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
863 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
864 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
865 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
866 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
868 // Test upper case searches (note that search is case insensitive)
869 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
870 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
871 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
872 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
874 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
877 void KFileItemModelTest::testNameFilter()
880 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
881 m_testDir
->createFiles(files
);
883 m_model
->loadDirectory(m_testDir
->url());
884 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
886 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
887 QCOMPARE(m_model
->count(), 3);
889 m_model
->setNameFilter("A2"); // Shows only A2
890 QCOMPARE(m_model
->count(), 1);
892 m_model
->setNameFilter("A2"); // Shows only A1
893 QCOMPARE(m_model
->count(), 1);
895 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
896 QCOMPARE(m_model
->count(), 2);
898 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
899 QCOMPARE(m_model
->count(), 2);
901 m_model
->setNameFilter(QString()); // Shows again all items
902 QCOMPARE(m_model
->count(), 5);
906 * Verifies that we do not crash when adding a KFileItem with an empty path.
907 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
908 * tried to always read the first character of the path, even if the path is empty.
910 void KFileItemModelTest::testEmptyPath()
912 QSet
<QByteArray
> roles
;
913 roles
.insert("text");
914 roles
.insert("isExpanded");
915 roles
.insert("isExpandable");
916 roles
.insert("expandedParentsCount");
917 m_model
->setRoles(roles
);
920 QVERIFY(emptyUrl
.path().isEmpty());
922 const KUrl
url("file:///test/");
925 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
926 m_model
->slotItemsAdded(emptyUrl
, items
);
927 m_model
->slotCompleted();
931 * Verifies that the 'isExpanded' state of folders does not change when the
932 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
934 void KFileItemModelTest::testRefreshExpandedItem()
936 QSet
<QByteArray
> modelRoles
= m_model
->roles();
937 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
938 m_model
->setRoles(modelRoles
);
941 files
<< "a/1" << "a/2" << "3" << "4";
942 m_testDir
->createFiles(files
);
944 m_model
->loadDirectory(m_testDir
->url());
945 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
946 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
948 m_model
->setExpanded(0, true);
949 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
950 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
951 QVERIFY(m_model
->isExpanded(0));
953 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
955 const KFileItem item
= m_model
->fileItem(0);
956 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
957 QVERIFY(!spyItemsChanged
.isEmpty());
959 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
960 QVERIFY(m_model
->isExpanded(0));
964 * Verify that removing hidden files and folders from the model does not
965 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
967 void KFileItemModelTest::testRemoveHiddenItems()
969 m_testDir
->createDir(".a");
970 m_testDir
->createDir(".b");
971 m_testDir
->createDir("c");
972 m_testDir
->createDir("d");
973 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
975 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
976 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
978 m_model
->setShowHiddenFiles(true);
979 m_model
->loadDirectory(m_testDir
->url());
980 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
981 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
982 QCOMPARE(spyItemsInserted
.count(), 1);
983 QCOMPARE(spyItemsRemoved
.count(), 0);
984 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
985 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
987 m_model
->setShowHiddenFiles(false);
988 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
989 QCOMPARE(spyItemsInserted
.count(), 0);
990 QCOMPARE(spyItemsRemoved
.count(), 1);
991 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
992 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
994 m_model
->setShowHiddenFiles(true);
995 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
996 QCOMPARE(spyItemsInserted
.count(), 1);
997 QCOMPARE(spyItemsRemoved
.count(), 0);
998 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
999 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1002 QCOMPARE(itemsInModel(), QStringList());
1003 QCOMPARE(spyItemsInserted
.count(), 0);
1004 QCOMPARE(spyItemsRemoved
.count(), 1);
1005 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1006 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1008 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1009 // Verify that this does not make the model crash.
1010 m_model
->setShowHiddenFiles(false);
1014 * Verify that filtered items are removed when their parent is collapsed.
1016 void KFileItemModelTest::collapseParentOfHiddenItems()
1018 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1019 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1020 m_model
->setRoles(modelRoles
);
1023 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1024 m_testDir
->createFiles(files
);
1026 m_model
->loadDirectory(m_testDir
->url());
1027 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1028 QCOMPARE(m_model
->count(), 1); // Only "a/"
1031 m_model
->setExpanded(0, true);
1032 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1033 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1036 m_model
->setExpanded(1, true);
1037 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1038 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1041 m_model
->setExpanded(2, true);
1042 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1043 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"
1045 // Set a name filter that matches nothing -> only the expanded folders remain.
1046 m_model
->setNameFilter("xyz");
1047 QCOMPARE(m_model
->count(), 3);
1048 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1050 // Collapse the folder "a/".
1051 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1052 m_model
->setExpanded(0, false);
1053 QCOMPARE(spyItemsRemoved
.count(), 1);
1054 QCOMPARE(m_model
->count(), 1);
1055 QCOMPARE(itemsInModel(), QStringList() << "a");
1057 // Remove the filter -> no files should appear (and we should not get a crash).
1058 m_model
->setNameFilter(QString());
1059 QCOMPARE(m_model
->count(), 1);
1063 * Verify that filtered items are removed when their parent is deleted.
1065 void KFileItemModelTest::removeParentOfHiddenItems()
1067 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1068 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1069 m_model
->setRoles(modelRoles
);
1072 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1073 m_testDir
->createFiles(files
);
1075 m_model
->loadDirectory(m_testDir
->url());
1076 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1077 QCOMPARE(m_model
->count(), 1); // Only "a/"
1080 m_model
->setExpanded(0, true);
1081 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1082 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1085 m_model
->setExpanded(1, true);
1086 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1087 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1090 m_model
->setExpanded(2, true);
1091 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1092 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"
1094 // Set a name filter that matches nothing -> only the expanded folders remain.
1095 m_model
->setNameFilter("xyz");
1096 QCOMPARE(m_model
->count(), 3);
1097 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1099 // Simulate the deletion of the directory "a/b/".
1100 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1101 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1102 QCOMPARE(spyItemsRemoved
.count(), 1);
1103 QCOMPARE(m_model
->count(), 1);
1104 QCOMPARE(itemsInModel(), QStringList() << "a");
1106 // Remove the filter -> only the file "a/1" should appear.
1107 m_model
->setNameFilter(QString());
1108 QCOMPARE(m_model
->count(), 2);
1109 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1113 * Create a tree structure where parent-child relationships can not be
1114 * determined by parsing the URLs, and verify that KFileItemModel
1115 * handles them correctly.
1117 void KFileItemModelTest::testGeneralParentChildRelationships()
1119 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1120 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1121 m_model
->setRoles(modelRoles
);
1124 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1125 m_testDir
->createFiles(files
);
1127 m_model
->loadDirectory(m_testDir
->url());
1128 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1129 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1131 // Expand all folders.
1132 m_model
->setExpanded(0, true);
1133 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1134 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1136 m_model
->setExpanded(1, true);
1137 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1138 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1140 m_model
->setExpanded(3, true);
1141 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1142 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1144 m_model
->setExpanded(4, true);
1145 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1146 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1148 // Add some more children and grand-children.
1149 const KUrl parent1
= m_model
->fileItem(0).url();
1150 const KUrl parent2
= m_model
->fileItem(3).url();
1151 const KUrl realChild1
= m_model
->fileItem(1).url();
1152 const KUrl realChild2
= m_model
->fileItem(4).url();
1154 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1155 m_model
->slotCompleted();
1156 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1158 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1159 m_model
->slotCompleted();
1160 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1162 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1163 m_model
->slotCompleted();
1164 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1166 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1167 m_model
->slotCompleted();
1168 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1170 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1171 m_model
->slotCompleted();
1172 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1174 // Set a name filter that matches nothing -> only expanded folders remain.
1175 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1176 m_model
->setNameFilter("xyz");
1177 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1178 QCOMPARE(itemsRemovedSpy
.count(), 1);
1179 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1180 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1181 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1183 // Collapse "parent1".
1184 m_model
->setExpanded(0, false);
1185 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1186 QCOMPARE(itemsRemovedSpy
.count(), 1);
1187 arguments
= itemsRemovedSpy
.takeFirst();
1188 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1189 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1191 // Remove "parent2".
1192 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1193 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1194 QCOMPARE(itemsRemovedSpy
.count(), 1);
1195 arguments
= itemsRemovedSpy
.takeFirst();
1196 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1197 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1199 // Clear filter, verify that no items reappear.
1200 m_model
->setNameFilter(QString());
1201 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1204 void KFileItemModelTest::testNameRoleGroups()
1207 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1209 m_testDir
->createFiles(files
);
1211 m_model
->setGroupedSorting(true);
1212 m_model
->loadDirectory(m_testDir
->url());
1213 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1214 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1216 QList
<QPair
<int, QVariant
> > expectedGroups
;
1217 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1218 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1219 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1220 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1221 QCOMPARE(m_model
->groups(), expectedGroups
);
1223 // Rename d.txt to a.txt.
1224 QHash
<QByteArray
, QVariant
> data
;
1225 data
.insert("text", "a.txt");
1226 m_model
->setData(2, data
);
1227 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1228 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1230 expectedGroups
.clear();
1231 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1232 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1233 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1234 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1235 QCOMPARE(m_model
->groups(), expectedGroups
);
1237 // Rename c.txt to d.txt.
1238 data
.insert("text", "d.txt");
1239 m_model
->setData(2, data
);
1240 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1241 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1243 expectedGroups
.clear();
1244 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1245 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1246 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1247 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1248 QCOMPARE(m_model
->groups(), expectedGroups
);
1251 QStringList
KFileItemModelTest::itemsInModel() const
1254 for (int i
= 0; i
< m_model
->count(); i
++) {
1255 items
<< m_model
->data(i
).value("text").toString();
1260 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1262 #include "kfileitemmodeltest.moc"