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");
395 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
396 const KFileItem fileItemD
= m_model
->fileItem(2);
397 KFileItem fileItemA
= fileItemD
;
398 KUrl urlA
= fileItemA
.url();
399 urlA
.setFileName("a.txt");
400 fileItemA
.setUrl(urlA
);
402 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
404 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
405 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
408 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
410 //QSKIP("Temporary disabled", SkipSingle);
412 // KFileItemModel prevents that inserting a punch of items sequentially
413 // results in an itemsInserted()-signal for each item. Instead internally
414 // a timeout is given that collects such operations and results in only
415 // one itemsInserted()-signal. However in this test we want to stress
416 // KFileItemModel to do a lot of insert operation and hence decrease
417 // the timeout to 1 millisecond.
418 m_testDir
->createFile("1");
419 m_model
->loadDirectory(m_testDir
->url());
420 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
421 QCOMPARE(m_model
->count(), 1);
423 // Insert 10 items for 20 times. After each insert operation the model consistency
425 QSet
<int> insertedItems
;
426 for (int i
= 0; i
< 20; ++i
) {
427 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
429 for (int j
= 0; j
< 10; ++j
) {
430 int itemName
= qrand();
431 while (insertedItems
.contains(itemName
)) {
434 insertedItems
.insert(itemName
);
436 m_testDir
->createFile(QString::number(itemName
));
439 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
440 if (spy
.count() == 0) {
441 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
444 QVERIFY(m_model
->isConsistent());
447 QCOMPARE(m_model
->count(), 201);
450 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
453 files
<< "B" << "E" << "G";
454 m_testDir
->createFiles(files
);
456 // Due to inserting the 3 items one item-range with index == 0 and
457 // count == 3 must be given
458 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
459 m_model
->loadDirectory(m_testDir
->url());
460 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
462 QCOMPARE(spy1
.count(), 1);
463 QList
<QVariant
> arguments
= spy1
.takeFirst();
464 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
465 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
467 // The indexes of the item-ranges must always be related to the model before
468 // the items have been inserted. Having:
471 // and inserting A, C, D, F the resulting model will be:
474 // and the item-ranges must be:
475 // index: 0, count: 1 for A
476 // index: 1, count: 2 for B, C
477 // index: 2, count: 1 for G
480 files
<< "A" << "C" << "D" << "F";
481 m_testDir
->createFiles(files
);
483 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
484 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
485 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
487 QCOMPARE(spy2
.count(), 1);
488 arguments
= spy2
.takeFirst();
489 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
490 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
493 void KFileItemModelTest::testExpandItems()
495 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
496 // Besides testing the basic item expansion functionality, the test makes sure that
497 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
498 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
499 // first three characters.
500 QSet
<QByteArray
> modelRoles
= m_model
->roles();
501 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
502 m_model
->setRoles(modelRoles
);
505 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
506 m_testDir
->createFiles(files
);
508 // Store the URLs of all folders in a set.
509 QSet
<KUrl
> allFolders
;
510 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
512 m_model
->loadDirectory(m_testDir
->url());
513 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
515 // So far, the model contains only "a/"
516 QCOMPARE(m_model
->count(), 1);
517 QVERIFY(m_model
->isExpandable(0));
518 QVERIFY(!m_model
->isExpanded(0));
519 QVERIFY(m_model
->expandedDirectories().empty());
521 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
523 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
524 m_model
->setExpanded(0, true);
525 QVERIFY(m_model
->isExpanded(0));
526 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
527 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
528 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
530 QCOMPARE(spyInserted
.count(), 1);
531 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
532 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
534 QVERIFY(m_model
->isExpandable(1));
535 QVERIFY(!m_model
->isExpanded(1));
536 QVERIFY(m_model
->isExpandable(2));
537 QVERIFY(!m_model
->isExpanded(2));
539 // Expand the folder "a/a/" -> "a/a/1" becomes visible
540 m_model
->setExpanded(1, true);
541 QVERIFY(m_model
->isExpanded(1));
542 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
543 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
544 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
546 QCOMPARE(spyInserted
.count(), 1);
547 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
548 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
550 QVERIFY(!m_model
->isExpandable(2));
551 QVERIFY(!m_model
->isExpanded(2));
553 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
554 m_model
->setExpanded(3, true);
555 QVERIFY(m_model
->isExpanded(3));
556 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
557 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
558 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
560 QCOMPARE(spyInserted
.count(), 1);
561 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
562 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
564 QVERIFY(!m_model
->isExpandable(4));
565 QVERIFY(!m_model
->isExpanded(4));
567 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
569 // Collapse the top-level folder -> all other items should disappear
570 m_model
->setExpanded(0, false);
571 QVERIFY(!m_model
->isExpanded(0));
572 QCOMPARE(m_model
->count(), 1);
573 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
575 QCOMPARE(spyRemoved
.count(), 1);
576 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
577 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
578 QVERIFY(m_model
->isConsistent());
580 // Clear the model, reload the folder and try to restore the expanded folders.
582 QCOMPARE(m_model
->count(), 0);
583 QVERIFY(m_model
->expandedDirectories().empty());
585 m_model
->loadDirectory(m_testDir
->url());
586 m_model
->restoreExpandedDirectories(allFolders
);
587 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
588 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
589 QVERIFY(m_model
->isExpanded(0));
590 QVERIFY(m_model
->isExpanded(1));
591 QVERIFY(!m_model
->isExpanded(2));
592 QVERIFY(m_model
->isExpanded(3));
593 QVERIFY(!m_model
->isExpanded(4));
594 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
595 QVERIFY(m_model
->isConsistent());
597 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
598 // This is how DolphinView restores the expanded folders when navigating in history.
599 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
600 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
601 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
602 m_model
->restoreExpandedDirectories(allFolders
);
603 m_model
->loadDirectory(m_testDir
->url());
604 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
605 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
606 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
609 void KFileItemModelTest::testExpandParentItems()
611 // Create a tree structure of folders:
619 QSet
<QByteArray
> modelRoles
= m_model
->roles();
620 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
621 m_model
->setRoles(modelRoles
);
624 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
625 m_testDir
->createFiles(files
);
627 m_model
->loadDirectory(m_testDir
->url());
628 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
630 // So far, the model contains only "a 1/" and "a2/".
631 QCOMPARE(m_model
->count(), 2);
632 QVERIFY(m_model
->expandedDirectories().empty());
634 // Expand the parents of "a2/b2/c2".
635 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
636 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
638 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
639 // It's important that only the parents of "a1/b1/c1" are expanded.
640 QCOMPARE(m_model
->count(), 4);
641 QVERIFY(!m_model
->isExpanded(0));
642 QVERIFY(m_model
->isExpanded(1));
643 QVERIFY(m_model
->isExpanded(2));
644 QVERIFY(!m_model
->isExpanded(3));
646 // Expand the parents of "a 1/b1".
647 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
648 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
650 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
651 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
652 QCOMPARE(m_model
->count(), 5);
653 QVERIFY(m_model
->isExpanded(0));
654 QVERIFY(!m_model
->isExpanded(1));
655 QVERIFY(m_model
->isExpanded(2));
656 QVERIFY(m_model
->isExpanded(3));
657 QVERIFY(!m_model
->isExpanded(4));
658 QVERIFY(m_model
->isConsistent());
662 * Renaming an expanded folder by prepending its name with a dot makes it
663 * hidden. Verify that this does not cause an inconsistent model state and
664 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
666 void KFileItemModelTest::testMakeExpandedItemHidden()
668 QSet
<QByteArray
> modelRoles
= m_model
->roles();
669 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
670 m_model
->setRoles(modelRoles
);
673 m_testDir
->createFile("1a/2a/3a");
674 m_testDir
->createFile("1a/2a/3b");
675 m_testDir
->createFile("1a/2b");
676 m_testDir
->createFile("1b");
678 m_model
->loadDirectory(m_testDir
->url());
679 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
681 // So far, the model contains only "1a/" and "1b".
682 QCOMPARE(m_model
->count(), 2);
683 m_model
->setExpanded(0, true);
684 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
686 // Now "1a/2a" and "1a/2b" have appeared.
687 QCOMPARE(m_model
->count(), 4);
688 m_model
->setExpanded(1, true);
689 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
690 QCOMPARE(m_model
->count(), 6);
692 // Rename "1a/2" and make it hidden.
693 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
694 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
696 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
697 bool ok
= job
->exec();
699 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
701 // "1a/2" and its subfolders have disappeared now.
702 QVERIFY(m_model
->isConsistent());
703 QCOMPARE(m_model
->count(), 3);
705 m_model
->setExpanded(0, false);
706 QCOMPARE(m_model
->count(), 2);
710 void KFileItemModelTest::testSorting()
712 // Create some files with different sizes and modification times to check the different sorting options
713 QDateTime now
= QDateTime::currentDateTime();
715 QSet
<QByteArray
> roles
;
716 roles
.insert("text");
717 roles
.insert("isExpanded");
718 roles
.insert("isExpandable");
719 roles
.insert("expandedParentsCount");
720 m_model
->setRoles(roles
);
722 m_testDir
->createDir("c/c-2");
723 m_testDir
->createFile("c/c-2/c-3");
724 m_testDir
->createFile("c/c-1");
726 m_testDir
->createFile("a", "A file", now
.addDays(-3));
727 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
728 m_testDir
->createDir("c", now
.addDays(-2));
729 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
730 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
731 m_testDir
->createFile(".f");
733 m_model
->loadDirectory(m_testDir
->url());
734 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
736 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
737 m_model
->setExpanded(index
, true);
738 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
740 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
741 m_model
->setExpanded(index
, true);
742 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
744 // Default: Sort by Name, ascending
745 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
746 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
747 QVERIFY(m_model
->sortDirectoriesFirst());
748 QVERIFY(!m_model
->showHiddenFiles());
749 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
751 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
753 // Sort by Name, ascending, 'Sort Folders First' disabled
754 m_model
->setSortDirectoriesFirst(false);
755 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
756 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
757 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
758 QCOMPARE(spyItemsMoved
.count(), 1);
759 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
761 // Sort by Name, descending
762 m_model
->setSortDirectoriesFirst(true);
763 m_model
->setSortOrder(Qt::DescendingOrder
);
764 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
765 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
766 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
767 QCOMPARE(spyItemsMoved
.count(), 2);
768 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
769 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
771 // Sort by Date, descending
772 m_model
->setSortDirectoriesFirst(true);
773 m_model
->setSortRole("date");
774 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
775 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
776 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
777 QCOMPARE(spyItemsMoved
.count(), 1);
778 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
780 // Sort by Date, ascending
781 m_model
->setSortOrder(Qt::AscendingOrder
);
782 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
783 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
784 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
785 QCOMPARE(spyItemsMoved
.count(), 1);
786 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
788 // Sort by Date, ascending, 'Sort Folders First' disabled
789 m_model
->setSortDirectoriesFirst(false);
790 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
791 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
792 QVERIFY(!m_model
->sortDirectoriesFirst());
793 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
794 QCOMPARE(spyItemsMoved
.count(), 1);
795 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
797 // Sort by Name, ascending, 'Sort Folders First' disabled
798 m_model
->setSortRole("text");
799 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
800 QVERIFY(!m_model
->sortDirectoriesFirst());
801 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
802 QCOMPARE(spyItemsMoved
.count(), 1);
803 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
805 // Sort by Size, ascending, 'Sort Folders First' disabled
806 m_model
->setSortRole("size");
807 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
808 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
809 QVERIFY(!m_model
->sortDirectoriesFirst());
810 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
811 QCOMPARE(spyItemsMoved
.count(), 1);
812 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
814 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
815 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
816 "another signal", SkipSingle
);
817 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
819 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
820 m_model
->setSortDirectoriesFirst(true);
821 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
822 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
823 QVERIFY(m_model
->sortDirectoriesFirst());
824 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
825 QCOMPARE(spyItemsMoved
.count(), 0);
827 // Sort by Size, descending, 'Sort Folders First' enabled
828 m_model
->setSortOrder(Qt::DescendingOrder
);
829 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
830 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
831 QVERIFY(m_model
->sortDirectoriesFirst());
832 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
833 QCOMPARE(spyItemsMoved
.count(), 1);
834 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
836 // TODO: Sort by other roles; show/hide hidden files
839 void KFileItemModelTest::testIndexForKeyboardSearch()
842 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
843 m_testDir
->createFiles(files
);
845 m_model
->loadDirectory(m_testDir
->url());
846 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
848 // Search from index 0
849 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
850 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
851 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
852 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
853 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
854 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
855 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
856 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
857 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
858 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
859 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
861 // Start a search somewhere in the middle
862 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
863 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
864 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
865 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
867 // Test searches that go past the last item back to index 0
868 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
869 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
870 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
871 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
873 // Test searches that yield no result
874 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
875 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
876 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
877 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
878 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
880 // Test upper case searches (note that search is case insensitive)
881 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
882 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
883 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
884 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
886 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
889 void KFileItemModelTest::testNameFilter()
892 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
893 m_testDir
->createFiles(files
);
895 m_model
->loadDirectory(m_testDir
->url());
896 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
898 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
899 QCOMPARE(m_model
->count(), 3);
901 m_model
->setNameFilter("A2"); // Shows only A2
902 QCOMPARE(m_model
->count(), 1);
904 m_model
->setNameFilter("A2"); // Shows only A1
905 QCOMPARE(m_model
->count(), 1);
907 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
908 QCOMPARE(m_model
->count(), 2);
910 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
911 QCOMPARE(m_model
->count(), 2);
913 m_model
->setNameFilter(QString()); // Shows again all items
914 QCOMPARE(m_model
->count(), 5);
918 * Verifies that we do not crash when adding a KFileItem with an empty path.
919 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
920 * tried to always read the first character of the path, even if the path is empty.
922 void KFileItemModelTest::testEmptyPath()
924 QSet
<QByteArray
> roles
;
925 roles
.insert("text");
926 roles
.insert("isExpanded");
927 roles
.insert("isExpandable");
928 roles
.insert("expandedParentsCount");
929 m_model
->setRoles(roles
);
932 QVERIFY(emptyUrl
.path().isEmpty());
934 const KUrl
url("file:///test/");
937 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
938 m_model
->slotItemsAdded(emptyUrl
, items
);
939 m_model
->slotCompleted();
943 * Verifies that the 'isExpanded' state of folders does not change when the
944 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
946 void KFileItemModelTest::testRefreshExpandedItem()
948 QSet
<QByteArray
> modelRoles
= m_model
->roles();
949 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
950 m_model
->setRoles(modelRoles
);
953 files
<< "a/1" << "a/2" << "3" << "4";
954 m_testDir
->createFiles(files
);
956 m_model
->loadDirectory(m_testDir
->url());
957 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
958 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
960 m_model
->setExpanded(0, true);
961 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
962 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
963 QVERIFY(m_model
->isExpanded(0));
965 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
967 const KFileItem item
= m_model
->fileItem(0);
968 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
969 QVERIFY(!spyItemsChanged
.isEmpty());
971 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
972 QVERIFY(m_model
->isExpanded(0));
976 * Verify that removing hidden files and folders from the model does not
977 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
979 void KFileItemModelTest::testRemoveHiddenItems()
981 m_testDir
->createDir(".a");
982 m_testDir
->createDir(".b");
983 m_testDir
->createDir("c");
984 m_testDir
->createDir("d");
985 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
987 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
988 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
990 m_model
->setShowHiddenFiles(true);
991 m_model
->loadDirectory(m_testDir
->url());
992 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
993 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
994 QCOMPARE(spyItemsInserted
.count(), 1);
995 QCOMPARE(spyItemsRemoved
.count(), 0);
996 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
997 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
999 m_model
->setShowHiddenFiles(false);
1000 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1001 QCOMPARE(spyItemsInserted
.count(), 0);
1002 QCOMPARE(spyItemsRemoved
.count(), 1);
1003 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1004 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1006 m_model
->setShowHiddenFiles(true);
1007 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1008 QCOMPARE(spyItemsInserted
.count(), 1);
1009 QCOMPARE(spyItemsRemoved
.count(), 0);
1010 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1011 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1014 QCOMPARE(itemsInModel(), QStringList());
1015 QCOMPARE(spyItemsInserted
.count(), 0);
1016 QCOMPARE(spyItemsRemoved
.count(), 1);
1017 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1018 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1020 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1021 // Verify that this does not make the model crash.
1022 m_model
->setShowHiddenFiles(false);
1026 * Verify that filtered items are removed when their parent is collapsed.
1028 void KFileItemModelTest::collapseParentOfHiddenItems()
1030 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1031 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1032 m_model
->setRoles(modelRoles
);
1035 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1036 m_testDir
->createFiles(files
);
1038 m_model
->loadDirectory(m_testDir
->url());
1039 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1040 QCOMPARE(m_model
->count(), 1); // Only "a/"
1043 m_model
->setExpanded(0, true);
1044 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1045 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1048 m_model
->setExpanded(1, true);
1049 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1050 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1053 m_model
->setExpanded(2, true);
1054 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1055 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"
1057 // Set a name filter that matches nothing -> only the expanded folders remain.
1058 m_model
->setNameFilter("xyz");
1059 QCOMPARE(m_model
->count(), 3);
1060 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1062 // Collapse the folder "a/".
1063 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1064 m_model
->setExpanded(0, false);
1065 QCOMPARE(spyItemsRemoved
.count(), 1);
1066 QCOMPARE(m_model
->count(), 1);
1067 QCOMPARE(itemsInModel(), QStringList() << "a");
1069 // Remove the filter -> no files should appear (and we should not get a crash).
1070 m_model
->setNameFilter(QString());
1071 QCOMPARE(m_model
->count(), 1);
1075 * Verify that filtered items are removed when their parent is deleted.
1077 void KFileItemModelTest::removeParentOfHiddenItems()
1079 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1080 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1081 m_model
->setRoles(modelRoles
);
1084 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1085 m_testDir
->createFiles(files
);
1087 m_model
->loadDirectory(m_testDir
->url());
1088 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1089 QCOMPARE(m_model
->count(), 1); // Only "a/"
1092 m_model
->setExpanded(0, true);
1093 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1094 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1097 m_model
->setExpanded(1, true);
1098 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1099 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1102 m_model
->setExpanded(2, true);
1103 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1104 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"
1106 // Set a name filter that matches nothing -> only the expanded folders remain.
1107 m_model
->setNameFilter("xyz");
1108 QCOMPARE(m_model
->count(), 3);
1109 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1111 // Simulate the deletion of the directory "a/b/".
1112 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1113 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1114 QCOMPARE(spyItemsRemoved
.count(), 1);
1115 QCOMPARE(m_model
->count(), 1);
1116 QCOMPARE(itemsInModel(), QStringList() << "a");
1118 // Remove the filter -> only the file "a/1" should appear.
1119 m_model
->setNameFilter(QString());
1120 QCOMPARE(m_model
->count(), 2);
1121 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1125 * Create a tree structure where parent-child relationships can not be
1126 * determined by parsing the URLs, and verify that KFileItemModel
1127 * handles them correctly.
1129 void KFileItemModelTest::testGeneralParentChildRelationships()
1131 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1132 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1133 m_model
->setRoles(modelRoles
);
1136 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1137 m_testDir
->createFiles(files
);
1139 m_model
->loadDirectory(m_testDir
->url());
1140 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1141 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1143 // Expand all folders.
1144 m_model
->setExpanded(0, true);
1145 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1146 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1148 m_model
->setExpanded(1, true);
1149 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1150 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1152 m_model
->setExpanded(3, true);
1153 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1154 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1156 m_model
->setExpanded(4, true);
1157 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1158 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1160 // Add some more children and grand-children.
1161 const KUrl parent1
= m_model
->fileItem(0).url();
1162 const KUrl parent2
= m_model
->fileItem(3).url();
1163 const KUrl realChild1
= m_model
->fileItem(1).url();
1164 const KUrl realChild2
= m_model
->fileItem(4).url();
1166 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1167 m_model
->slotCompleted();
1168 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1170 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1171 m_model
->slotCompleted();
1172 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1174 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1175 m_model
->slotCompleted();
1176 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1178 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1179 m_model
->slotCompleted();
1180 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1182 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1183 m_model
->slotCompleted();
1184 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1186 // Set a name filter that matches nothing -> only expanded folders remain.
1187 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1188 m_model
->setNameFilter("xyz");
1189 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1190 QCOMPARE(itemsRemovedSpy
.count(), 1);
1191 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1192 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1193 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1195 // Collapse "parent1".
1196 m_model
->setExpanded(0, false);
1197 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1198 QCOMPARE(itemsRemovedSpy
.count(), 1);
1199 arguments
= itemsRemovedSpy
.takeFirst();
1200 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1201 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1203 // Remove "parent2".
1204 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1205 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1206 QCOMPARE(itemsRemovedSpy
.count(), 1);
1207 arguments
= itemsRemovedSpy
.takeFirst();
1208 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1209 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1211 // Clear filter, verify that no items reappear.
1212 m_model
->setNameFilter(QString());
1213 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1216 void KFileItemModelTest::testNameRoleGroups()
1219 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1221 m_testDir
->createFiles(files
);
1223 m_model
->setGroupedSorting(true);
1224 m_model
->loadDirectory(m_testDir
->url());
1225 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1226 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1228 QList
<QPair
<int, QVariant
> > expectedGroups
;
1229 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1230 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1231 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1232 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1233 QCOMPARE(m_model
->groups(), expectedGroups
);
1235 // Rename d.txt to a.txt.
1236 QHash
<QByteArray
, QVariant
> data
;
1237 data
.insert("text", "a.txt");
1238 m_model
->setData(2, data
);
1239 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1240 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1242 expectedGroups
.clear();
1243 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1244 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1245 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1246 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1247 QCOMPARE(m_model
->groups(), expectedGroups
);
1249 // Rename c.txt to d.txt.
1250 data
.insert("text", "d.txt");
1251 m_model
->setData(2, data
);
1252 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1253 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1255 expectedGroups
.clear();
1256 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1257 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1258 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1259 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1260 QCOMPARE(m_model
->groups(), expectedGroups
);
1262 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1263 const KFileItem fileItemD
= m_model
->fileItem(2);
1264 KFileItem fileItemC
= fileItemD
;
1265 KUrl urlC
= fileItemC
.url();
1266 urlC
.setFileName("c.txt");
1267 fileItemC
.setUrl(urlC
);
1269 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1270 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1271 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1273 expectedGroups
.clear();
1274 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1275 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1276 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1277 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1278 QCOMPARE(m_model
->groups(), expectedGroups
);
1281 QStringList
KFileItemModelTest::itemsInModel() const
1284 for (int i
= 0; i
< m_model
->count(); i
++) {
1285 items
<< m_model
->data(i
).value("text").toString();
1290 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1292 #include "kfileitemmodeltest.moc"