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>
25 #include "kitemviews/kfileitemmodel.h"
26 #include "kitemviews/private/kfileitemmodeldirlister.h"
29 void myMessageOutput(QtMsgType type
, const char* msg
)
37 fprintf(stderr
, "Critical: %s\n", msg
);
40 fprintf(stderr
, "Fatal: %s\n", msg
);
48 const int DefaultTimeout
= 5000;
51 Q_DECLARE_METATYPE(KItemRange
)
52 Q_DECLARE_METATYPE(KItemRangeList
)
53 Q_DECLARE_METATYPE(QList
<int>)
55 class KFileItemModelTest
: public QObject
63 void testDefaultRoles();
64 void testDefaultSortRole();
65 void testDefaultGroupedSorting();
67 void testRemoveItems();
68 void testDirLoadingCompleted();
70 void testSetDataWithModifiedSortRole_data();
71 void testSetDataWithModifiedSortRole();
72 void testChangeSortRole();
73 void testResortAfterChangingName();
74 void testModelConsistencyWhenInsertingItems();
75 void testItemRangeConsistencyWhenInsertingItems();
76 void testExpandItems();
77 void testExpandParentItems();
78 void testMakeExpandedItemHidden();
79 void testRemoveFilteredExpandedItems();
81 void testIndexForKeyboardSearch();
82 void testNameFilter();
84 void testRefreshExpandedItem();
85 void testRemoveHiddenItems();
86 void collapseParentOfHiddenItems();
87 void removeParentOfHiddenItems();
88 void testGeneralParentChildRelationships();
89 void testNameRoleGroups();
90 void testNameRoleGroupsWithExpandedItems();
91 void testInconsistentModel();
92 void testChangeRolesForFilteredItems();
93 void testChangeSortRoleWhileFiltering();
94 void testRefreshFilteredItems();
95 void testCollapseFolderWhileLoading();
96 void testCreateMimeData();
97 void testDeleteFileMoreThanOnce();
100 QStringList
itemsInModel() const;
103 KFileItemModel
* m_model
;
107 void KFileItemModelTest::init()
109 // The item-model tests result in a huge number of debugging
110 // output from kdelibs. Only show critical and fatal messages.
111 qInstallMsgHandler(myMessageOutput
);
113 qRegisterMetaType
<KItemRange
>("KItemRange");
114 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
115 qRegisterMetaType
<KFileItemList
>("KFileItemList");
117 m_testDir
= new TestDir();
118 m_model
= new KFileItemModel();
119 m_model
->m_dirLister
->setAutoUpdate(false);
121 // Reduce the timer interval to make the test run faster.
122 m_model
->m_resortAllItemsTimer
->setInterval(0);
125 void KFileItemModelTest::cleanup()
134 void KFileItemModelTest::testDefaultRoles()
136 const QSet
<QByteArray
> roles
= m_model
->roles();
137 QCOMPARE(roles
.count(), 3);
138 QVERIFY(roles
.contains("text"));
139 QVERIFY(roles
.contains("isDir"));
140 QVERIFY(roles
.contains("isLink"));
143 void KFileItemModelTest::testDefaultSortRole()
145 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
148 files
<< "c.txt" << "a.txt" << "b.txt";
150 m_testDir
->createFiles(files
);
152 m_model
->loadDirectory(m_testDir
->url());
153 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
155 QCOMPARE(m_model
->count(), 3);
156 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
157 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
158 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
161 void KFileItemModelTest::testDefaultGroupedSorting()
163 QCOMPARE(m_model
->groupedSorting(), false);
166 void KFileItemModelTest::testNewItems()
169 files
<< "a.txt" << "b.txt" << "c.txt";
170 m_testDir
->createFiles(files
);
172 m_model
->loadDirectory(m_testDir
->url());
173 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
175 QCOMPARE(m_model
->count(), 3);
177 QVERIFY(m_model
->isConsistent());
180 void KFileItemModelTest::testRemoveItems()
182 m_testDir
->createFile("a.txt");
183 m_testDir
->createFile("b.txt");
184 m_model
->loadDirectory(m_testDir
->url());
185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
186 QCOMPARE(m_model
->count(), 2);
187 QVERIFY(m_model
->isConsistent());
189 m_testDir
->removeFile("a.txt");
190 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
191 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
192 QCOMPARE(m_model
->count(), 1);
193 QVERIFY(m_model
->isConsistent());
196 void KFileItemModelTest::testDirLoadingCompleted()
198 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
199 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
200 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
202 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
204 m_model
->loadDirectory(m_testDir
->url());
205 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
206 QCOMPARE(loadingCompletedSpy
.count(), 1);
207 QCOMPARE(itemsInsertedSpy
.count(), 1);
208 QCOMPARE(itemsRemovedSpy
.count(), 0);
209 QCOMPARE(m_model
->count(), 3);
211 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
212 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
213 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
214 QCOMPARE(loadingCompletedSpy
.count(), 2);
215 QCOMPARE(itemsInsertedSpy
.count(), 2);
216 QCOMPARE(itemsRemovedSpy
.count(), 0);
217 QCOMPARE(m_model
->count(), 5);
219 m_testDir
->removeFile("a.txt");
220 m_testDir
->createFile("f.txt");
221 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
222 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
223 QCOMPARE(loadingCompletedSpy
.count(), 3);
224 QCOMPARE(itemsInsertedSpy
.count(), 3);
225 QCOMPARE(itemsRemovedSpy
.count(), 1);
226 QCOMPARE(m_model
->count(), 5);
228 m_testDir
->removeFile("b.txt");
229 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
230 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
231 QCOMPARE(loadingCompletedSpy
.count(), 4);
232 QCOMPARE(itemsInsertedSpy
.count(), 3);
233 QCOMPARE(itemsRemovedSpy
.count(), 2);
234 QCOMPARE(m_model
->count(), 4);
236 QVERIFY(m_model
->isConsistent());
239 void KFileItemModelTest::testSetData()
241 m_testDir
->createFile("a.txt");
243 m_model
->loadDirectory(m_testDir
->url());
244 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
246 QHash
<QByteArray
, QVariant
> values
;
247 values
.insert("customRole1", "Test1");
248 values
.insert("customRole2", "Test2");
250 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
251 m_model
->setData(0, values
);
252 QCOMPARE(itemsChangedSpy
.count(), 1);
254 values
= m_model
->data(0);
255 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
256 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
257 QVERIFY(m_model
->isConsistent());
260 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
262 QTest::addColumn
<int>("changedIndex");
263 QTest::addColumn
<int>("changedRating");
264 QTest::addColumn
<bool>("expectMoveSignal");
265 QTest::addColumn
<int>("ratingIndex0");
266 QTest::addColumn
<int>("ratingIndex1");
267 QTest::addColumn
<int>("ratingIndex2");
270 // Index 0 = rating 2
271 // Index 1 = rating 4
272 // Index 2 = rating 6
274 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
275 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
276 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
278 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
279 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
280 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
283 void KFileItemModelTest::testSetDataWithModifiedSortRole()
285 QFETCH(int, changedIndex
);
286 QFETCH(int, changedRating
);
287 QFETCH(bool, expectMoveSignal
);
288 QFETCH(int, ratingIndex0
);
289 QFETCH(int, ratingIndex1
);
290 QFETCH(int, ratingIndex2
);
292 // Changing the value of a sort-role must result in
293 // a reordering of the items.
294 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
295 m_model
->setSortRole("rating");
296 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
299 files
<< "a.txt" << "b.txt" << "c.txt";
300 m_testDir
->createFiles(files
);
302 m_model
->loadDirectory(m_testDir
->url());
303 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
305 // Fill the "rating" role of each file:
310 QHash
<QByteArray
, QVariant
> ratingA
;
311 ratingA
.insert("rating", 2);
312 m_model
->setData(0, ratingA
);
314 QHash
<QByteArray
, QVariant
> ratingB
;
315 ratingB
.insert("rating", 4);
316 m_model
->setData(1, ratingB
);
318 QHash
<QByteArray
, QVariant
> ratingC
;
319 ratingC
.insert("rating", 6);
320 m_model
->setData(2, ratingC
);
322 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
323 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
324 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
326 // Now change the rating from a.txt. This usually results
327 // in reordering of the items.
328 QHash
<QByteArray
, QVariant
> rating
;
329 rating
.insert("rating", changedRating
);
330 m_model
->setData(changedIndex
, rating
);
332 if (expectMoveSignal
) {
333 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
336 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
337 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
338 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
339 QVERIFY(m_model
->isConsistent());
342 void KFileItemModelTest::testChangeSortRole()
344 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
347 files
<< "a.txt" << "b.jpg" << "c.txt";
348 m_testDir
->createFiles(files
);
350 m_model
->loadDirectory(m_testDir
->url());
351 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
352 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
354 // Simulate that KFileItemModelRolesUpdater determines the mime type.
355 // Resorting the files by 'type' will only work immediately if their
356 // mime types are known.
357 for (int index
= 0; index
< m_model
->count(); ++index
) {
358 m_model
->fileItem(index
).determineMimeType();
361 // Now: sort by type.
362 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
363 m_model
->setSortRole("type");
364 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
365 QVERIFY(!spyItemsMoved
.isEmpty());
367 // The actual order of the files might depend on the translation of the
368 // result of KFileItem::mimeComment() in the user's language.
369 QStringList version1
;
370 version1
<< "b.jpg" << "a.txt" << "c.txt";
372 QStringList version2
;
373 version2
<< "a.txt" << "c.txt" << "b.jpg";
375 const bool ok1
= (itemsInModel() == version1
);
376 const bool ok2
= (itemsInModel() == version2
);
381 void KFileItemModelTest::testResortAfterChangingName()
383 // We sort by size in a directory where all files have the same size.
384 // Therefore, the files are sorted by their names.
385 m_model
->setSortRole("size");
388 files
<< "a.txt" << "b.txt" << "c.txt";
389 m_testDir
->createFiles(files
);
391 m_model
->loadDirectory(m_testDir
->url());
392 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
393 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
395 // We rename a.txt to d.txt. Even though the size has not changed at all,
396 // the model must re-sort the items.
397 QHash
<QByteArray
, QVariant
> data
;
398 data
.insert("text", "d.txt");
399 m_model
->setData(0, data
);
401 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
402 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
404 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
405 const KFileItem fileItemD
= m_model
->fileItem(2);
406 KFileItem fileItemA
= fileItemD
;
407 QUrl urlA
= fileItemA
.url();
408 urlA
.adjusted(QUrl::RemoveFilename
);
409 urlA
.setPath(urlA
.path() + "a.txt");
410 fileItemA
.setUrl(urlA
);
412 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
414 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
415 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
418 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
420 //QSKIP("Temporary disabled", SkipSingle);
422 // KFileItemModel prevents that inserting a punch of items sequentially
423 // results in an itemsInserted()-signal for each item. Instead internally
424 // a timeout is given that collects such operations and results in only
425 // one itemsInserted()-signal. However in this test we want to stress
426 // KFileItemModel to do a lot of insert operation and hence decrease
427 // the timeout to 1 millisecond.
428 m_testDir
->createFile("1");
429 m_model
->loadDirectory(m_testDir
->url());
430 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
431 QCOMPARE(m_model
->count(), 1);
433 // Insert 10 items for 20 times. After each insert operation the model consistency
435 QSet
<int> insertedItems
;
436 for (int i
= 0; i
< 20; ++i
) {
437 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
439 for (int j
= 0; j
< 10; ++j
) {
440 int itemName
= qrand();
441 while (insertedItems
.contains(itemName
)) {
444 insertedItems
.insert(itemName
);
446 m_testDir
->createFile(QString::number(itemName
));
449 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
450 if (spy
.count() == 0) {
451 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
454 QVERIFY(m_model
->isConsistent());
457 QCOMPARE(m_model
->count(), 201);
460 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
463 files
<< "B" << "E" << "G";
464 m_testDir
->createFiles(files
);
466 // Due to inserting the 3 items one item-range with index == 0 and
467 // count == 3 must be given
468 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
469 m_model
->loadDirectory(m_testDir
->url());
470 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
472 QCOMPARE(spy1
.count(), 1);
473 QList
<QVariant
> arguments
= spy1
.takeFirst();
474 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
475 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
477 // The indexes of the item-ranges must always be related to the model before
478 // the items have been inserted. Having:
481 // and inserting A, C, D, F the resulting model will be:
484 // and the item-ranges must be:
485 // index: 0, count: 1 for A
486 // index: 1, count: 2 for B, C
487 // index: 2, count: 1 for G
490 files
<< "A" << "C" << "D" << "F";
491 m_testDir
->createFiles(files
);
493 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
494 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
495 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
497 QCOMPARE(spy2
.count(), 1);
498 arguments
= spy2
.takeFirst();
499 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
500 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
503 void KFileItemModelTest::testExpandItems()
505 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
506 // Besides testing the basic item expansion functionality, the test makes sure that
507 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
508 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
509 // first three characters.
510 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
511 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
512 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
513 m_model
->setRoles(modelRoles
);
516 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
517 m_testDir
->createFiles(files
);
519 // Store the URLs of all folders in a set.
520 QSet
<QUrl
> allFolders
;
521 allFolders
<< QUrl::fromLocalFile(m_testDir
->name() + 'a')
522 << QUrl::fromLocalFile(m_testDir
->name() + "a/a")
523 << QUrl::fromLocalFile(m_testDir
->name() + "a/a-1");
525 m_model
->loadDirectory(m_testDir
->url());
526 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
528 // So far, the model contains only "a/"
529 QCOMPARE(m_model
->count(), 1);
530 QVERIFY(m_model
->isExpandable(0));
531 QVERIFY(!m_model
->isExpanded(0));
532 QVERIFY(m_model
->expandedDirectories().empty());
534 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
536 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
537 m_model
->setExpanded(0, true);
538 QVERIFY(m_model
->isExpanded(0));
539 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
540 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
541 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl(m_testDir
->name() + 'a'));
543 QCOMPARE(spyInserted
.count(), 1);
544 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
545 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
547 QVERIFY(m_model
->isExpandable(1));
548 QVERIFY(!m_model
->isExpanded(1));
549 QVERIFY(m_model
->isExpandable(2));
550 QVERIFY(!m_model
->isExpanded(2));
552 // Expand the folder "a/a/" -> "a/a/1" becomes visible
553 m_model
->setExpanded(1, true);
554 QVERIFY(m_model
->isExpanded(1));
555 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
556 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
557 QCOMPARE(m_model
->expandedDirectories(), QSet
<QUrl
>() << QUrl(m_testDir
->name() + 'a') << QUrl(m_testDir
->name() + "a/a"));
559 QCOMPARE(spyInserted
.count(), 1);
560 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
561 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
563 QVERIFY(!m_model
->isExpandable(2));
564 QVERIFY(!m_model
->isExpanded(2));
566 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
567 m_model
->setExpanded(3, true);
568 QVERIFY(m_model
->isExpanded(3));
569 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
570 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
571 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
573 QCOMPARE(spyInserted
.count(), 1);
574 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
575 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
577 QVERIFY(!m_model
->isExpandable(4));
578 QVERIFY(!m_model
->isExpanded(4));
580 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
582 // Collapse the top-level folder -> all other items should disappear
583 m_model
->setExpanded(0, false);
584 QVERIFY(!m_model
->isExpanded(0));
585 QCOMPARE(m_model
->count(), 1);
586 QVERIFY(!m_model
->expandedDirectories().contains(QUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
588 QCOMPARE(spyRemoved
.count(), 1);
589 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
590 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
591 QVERIFY(m_model
->isConsistent());
593 // Clear the model, reload the folder and try to restore the expanded folders.
595 QCOMPARE(m_model
->count(), 0);
596 QVERIFY(m_model
->expandedDirectories().empty());
598 m_model
->loadDirectory(m_testDir
->url());
599 m_model
->restoreExpandedDirectories(allFolders
);
600 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
601 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
602 QVERIFY(m_model
->isExpanded(0));
603 QVERIFY(m_model
->isExpanded(1));
604 QVERIFY(!m_model
->isExpanded(2));
605 QVERIFY(m_model
->isExpanded(3));
606 QVERIFY(!m_model
->isExpanded(4));
607 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
608 QVERIFY(m_model
->isConsistent());
610 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
611 // This is how DolphinView restores the expanded folders when navigating in history.
612 m_model
->loadDirectory(QUrl(m_testDir
->name() + "a/a/"));
613 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
614 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
615 m_model
->restoreExpandedDirectories(allFolders
);
616 m_model
->loadDirectory(m_testDir
->url());
617 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
618 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
619 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
621 // Remove all expanded items by changing the roles
623 m_model
->setRoles(originalModelRoles
);
624 QVERIFY(!m_model
->isExpanded(0));
625 QCOMPARE(m_model
->count(), 1);
626 QVERIFY(!m_model
->expandedDirectories().contains(QUrl(m_testDir
->name() + 'a')));
628 QCOMPARE(spyRemoved
.count(), 1);
629 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
630 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
631 QVERIFY(m_model
->isConsistent());
634 void KFileItemModelTest::testExpandParentItems()
636 // Create a tree structure of folders:
644 QSet
<QByteArray
> modelRoles
= m_model
->roles();
645 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
646 m_model
->setRoles(modelRoles
);
649 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
650 m_testDir
->createFiles(files
);
652 m_model
->loadDirectory(m_testDir
->url());
653 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
655 // So far, the model contains only "a 1/" and "a2/".
656 QCOMPARE(m_model
->count(), 2);
657 QVERIFY(m_model
->expandedDirectories().empty());
659 // Expand the parents of "a2/b2/c2".
660 m_model
->expandParentDirectories(QUrl(m_testDir
->name() + "a2/b2/c2"));
661 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
663 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
664 // It's important that only the parents of "a1/b1/c1" are expanded.
665 QCOMPARE(m_model
->count(), 4);
666 QVERIFY(!m_model
->isExpanded(0));
667 QVERIFY(m_model
->isExpanded(1));
668 QVERIFY(m_model
->isExpanded(2));
669 QVERIFY(!m_model
->isExpanded(3));
671 // Expand the parents of "a 1/b1".
672 m_model
->expandParentDirectories(QUrl(m_testDir
->name() + "a 1/b1"));
673 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
675 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
676 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
677 QCOMPARE(m_model
->count(), 5);
678 QVERIFY(m_model
->isExpanded(0));
679 QVERIFY(!m_model
->isExpanded(1));
680 QVERIFY(m_model
->isExpanded(2));
681 QVERIFY(m_model
->isExpanded(3));
682 QVERIFY(!m_model
->isExpanded(4));
683 QVERIFY(m_model
->isConsistent());
686 m_model
->setExpanded(1, true);
687 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
688 QCOMPARE(m_model
->count(), 6);
689 QVERIFY(m_model
->isExpanded(0));
690 QVERIFY(m_model
->isExpanded(1));
691 QVERIFY(!m_model
->isExpanded(2));
692 QVERIFY(m_model
->isExpanded(3));
693 QVERIFY(m_model
->isExpanded(4));
694 QVERIFY(!m_model
->isExpanded(5));
695 QVERIFY(m_model
->isConsistent());
697 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
698 m_model
->setExpanded(1, false);
699 QCOMPARE(m_model
->count(), 5);
700 QVERIFY(m_model
->isExpanded(0));
701 QVERIFY(!m_model
->isExpanded(1));
702 QVERIFY(m_model
->isExpanded(2));
703 QVERIFY(m_model
->isExpanded(3));
704 QVERIFY(!m_model
->isExpanded(4));
705 QVERIFY(m_model
->isConsistent());
709 * Renaming an expanded folder by prepending its name with a dot makes it
710 * hidden. Verify that this does not cause an inconsistent model state and
711 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
713 void KFileItemModelTest::testMakeExpandedItemHidden()
715 QSet
<QByteArray
> modelRoles
= m_model
->roles();
716 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
717 m_model
->setRoles(modelRoles
);
720 m_testDir
->createFile("1a/2a/3a");
721 m_testDir
->createFile("1a/2a/3b");
722 m_testDir
->createFile("1a/2b");
723 m_testDir
->createFile("1b");
725 m_model
->loadDirectory(m_testDir
->url());
726 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
728 // So far, the model contains only "1a/" and "1b".
729 QCOMPARE(m_model
->count(), 2);
730 m_model
->setExpanded(0, true);
731 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
733 // Now "1a/2a" and "1a/2b" have appeared.
734 QCOMPARE(m_model
->count(), 4);
735 m_model
->setExpanded(1, true);
736 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
737 QCOMPARE(m_model
->count(), 6);
739 // Rename "1a/2" and make it hidden.
740 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
741 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
743 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
744 bool ok
= job
->exec();
746 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
748 // "1a/2" and its subfolders have disappeared now.
749 QVERIFY(m_model
->isConsistent());
750 QCOMPARE(m_model
->count(), 3);
752 m_model
->setExpanded(0, false);
753 QCOMPARE(m_model
->count(), 2);
757 void KFileItemModelTest::testRemoveFilteredExpandedItems()
759 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
760 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
761 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
762 m_model
->setRoles(modelRoles
);
765 files
<< "folder/child" << "file"; // missing folders are created automatically
766 m_testDir
->createFiles(files
);
768 m_model
->loadDirectory(m_testDir
->url());
769 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
771 // So far, the model contains only "folder/" and "file".
772 QCOMPARE(m_model
->count(), 2);
773 QVERIFY(m_model
->isExpandable(0));
774 QVERIFY(!m_model
->isExpandable(1));
775 QVERIFY(!m_model
->isExpanded(0));
776 QVERIFY(!m_model
->isExpanded(1));
777 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
779 // Expand "folder" -> "folder/child" becomes visible.
780 m_model
->setExpanded(0, true);
781 QVERIFY(m_model
->isExpanded(0));
782 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
783 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
785 // Add a name filter.
786 m_model
->setNameFilter("f");
787 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
789 m_model
->setNameFilter("fo");
790 QCOMPARE(itemsInModel(), QStringList() << "folder");
792 // Remove all expanded items by changing the roles
793 m_model
->setRoles(originalModelRoles
);
794 QVERIFY(!m_model
->isExpanded(0));
795 QCOMPARE(itemsInModel(), QStringList() << "folder");
797 // Remove the name filter and verify that "folder/child" does not reappear.
798 m_model
->setNameFilter(QString());
799 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
802 void KFileItemModelTest::testSorting()
804 // Create some files with different sizes and modification times to check the different sorting options
805 QDateTime now
= QDateTime::currentDateTime();
807 QSet
<QByteArray
> roles
;
808 roles
.insert("text");
809 roles
.insert("isExpanded");
810 roles
.insert("isExpandable");
811 roles
.insert("expandedParentsCount");
812 m_model
->setRoles(roles
);
814 m_testDir
->createDir("c/c-2");
815 m_testDir
->createFile("c/c-2/c-3");
816 m_testDir
->createFile("c/c-1");
818 m_testDir
->createFile("a", "A file", now
.addDays(-3));
819 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
820 m_testDir
->createDir("c", now
.addDays(-2));
821 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
822 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
823 m_testDir
->createFile(".f");
825 m_model
->loadDirectory(m_testDir
->url());
826 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
828 int index
= m_model
->index(QUrl(m_testDir
->url().url() + 'c'));
829 m_model
->setExpanded(index
, true);
830 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
832 index
= m_model
->index(QUrl(m_testDir
->url().url() + "c/c-2"));
833 m_model
->setExpanded(index
, true);
834 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
836 // Default: Sort by Name, ascending
837 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
838 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
839 QVERIFY(m_model
->sortDirectoriesFirst());
840 QVERIFY(!m_model
->showHiddenFiles());
841 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
843 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
845 // Sort by Name, ascending, 'Sort Folders First' disabled
846 m_model
->setSortDirectoriesFirst(false);
847 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
848 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
849 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
850 QCOMPARE(spyItemsMoved
.count(), 1);
851 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
852 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
854 // Sort by Name, descending
855 m_model
->setSortDirectoriesFirst(true);
856 m_model
->setSortOrder(Qt::DescendingOrder
);
857 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
858 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
859 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
860 QCOMPARE(spyItemsMoved
.count(), 2);
861 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
862 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
863 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
864 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
866 // Sort by Date, descending
867 m_model
->setSortDirectoriesFirst(true);
868 m_model
->setSortRole("date");
869 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
870 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
871 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
872 QCOMPARE(spyItemsMoved
.count(), 1);
873 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
874 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
876 // Sort by Date, ascending
877 m_model
->setSortOrder(Qt::AscendingOrder
);
878 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
879 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
880 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
881 QCOMPARE(spyItemsMoved
.count(), 1);
882 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
883 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
885 // Sort by Date, ascending, 'Sort Folders First' disabled
886 m_model
->setSortDirectoriesFirst(false);
887 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
888 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
889 QVERIFY(!m_model
->sortDirectoriesFirst());
890 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
891 QCOMPARE(spyItemsMoved
.count(), 1);
892 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
893 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
895 // Sort by Name, ascending, 'Sort Folders First' disabled
896 m_model
->setSortRole("text");
897 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
898 QVERIFY(!m_model
->sortDirectoriesFirst());
899 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
900 QCOMPARE(spyItemsMoved
.count(), 1);
901 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
902 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
904 // Sort by Size, ascending, 'Sort Folders First' disabled
905 m_model
->setSortRole("size");
906 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
907 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
908 QVERIFY(!m_model
->sortDirectoriesFirst());
909 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
910 QCOMPARE(spyItemsMoved
.count(), 1);
911 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
912 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
914 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
915 m_model
->setSortDirectoriesFirst(true);
916 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
917 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
918 QVERIFY(m_model
->sortDirectoriesFirst());
919 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
920 QCOMPARE(spyItemsMoved
.count(), 0);
922 // Sort by Size, descending, 'Sort Folders First' enabled
923 m_model
->setSortOrder(Qt::DescendingOrder
);
924 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
925 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
926 QVERIFY(m_model
->sortDirectoriesFirst());
927 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
928 QCOMPARE(spyItemsMoved
.count(), 1);
929 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
930 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
932 // TODO: Sort by other roles; show/hide hidden files
935 void KFileItemModelTest::testIndexForKeyboardSearch()
938 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
939 m_testDir
->createFiles(files
);
941 m_model
->loadDirectory(m_testDir
->url());
942 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
944 // Search from index 0
945 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
946 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
947 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
948 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
949 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
950 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
951 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
952 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
953 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
954 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
955 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
957 // Start a search somewhere in the middle
958 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
959 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
960 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
961 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
963 // Test searches that go past the last item back to index 0
964 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
965 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
966 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
967 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
969 // Test searches that yield no result
970 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
971 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
972 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
973 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
974 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
976 // Test upper case searches (note that search is case insensitive)
977 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
978 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
979 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
980 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
982 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
985 void KFileItemModelTest::testNameFilter()
988 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
989 m_testDir
->createFiles(files
);
991 m_model
->loadDirectory(m_testDir
->url());
992 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
994 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
995 QCOMPARE(m_model
->count(), 3);
997 m_model
->setNameFilter("A2"); // Shows only A2
998 QCOMPARE(m_model
->count(), 1);
1000 m_model
->setNameFilter("A2"); // Shows only A1
1001 QCOMPARE(m_model
->count(), 1);
1003 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1004 QCOMPARE(m_model
->count(), 2);
1006 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1007 QCOMPARE(m_model
->count(), 2);
1009 m_model
->setNameFilter(QString()); // Shows again all items
1010 QCOMPARE(m_model
->count(), 5);
1014 * Verifies that we do not crash when adding a KFileItem with an empty path.
1015 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1016 * tried to always read the first character of the path, even if the path is empty.
1018 void KFileItemModelTest::testEmptyPath()
1020 QSet
<QByteArray
> roles
;
1021 roles
.insert("text");
1022 roles
.insert("isExpanded");
1023 roles
.insert("isExpandable");
1024 roles
.insert("expandedParentsCount");
1025 m_model
->setRoles(roles
);
1027 const QUrl emptyUrl
;
1028 QVERIFY(emptyUrl
.path().isEmpty());
1030 const QUrl
url("file:///test/");
1032 KFileItemList items
;
1033 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1034 m_model
->slotItemsAdded(emptyUrl
, items
);
1035 m_model
->slotCompleted();
1039 * Verifies that the 'isExpanded' state of folders does not change when the
1040 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1042 void KFileItemModelTest::testRefreshExpandedItem()
1044 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1045 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1046 m_model
->setRoles(modelRoles
);
1049 files
<< "a/1" << "a/2" << "3" << "4";
1050 m_testDir
->createFiles(files
);
1052 m_model
->loadDirectory(m_testDir
->url());
1053 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1054 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1056 m_model
->setExpanded(0, true);
1057 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1058 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1059 QVERIFY(m_model
->isExpanded(0));
1061 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1063 const KFileItem item
= m_model
->fileItem(0);
1064 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1065 QVERIFY(!spyItemsChanged
.isEmpty());
1067 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1068 QVERIFY(m_model
->isExpanded(0));
1072 * Verify that removing hidden files and folders from the model does not
1073 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1075 void KFileItemModelTest::testRemoveHiddenItems()
1077 m_testDir
->createDir(".a");
1078 m_testDir
->createDir(".b");
1079 m_testDir
->createDir("c");
1080 m_testDir
->createDir("d");
1081 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1083 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1084 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1086 m_model
->setShowHiddenFiles(true);
1087 m_model
->loadDirectory(m_testDir
->url());
1088 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1089 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1090 QCOMPARE(spyItemsInserted
.count(), 1);
1091 QCOMPARE(spyItemsRemoved
.count(), 0);
1092 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1093 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1095 m_model
->setShowHiddenFiles(false);
1096 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1097 QCOMPARE(spyItemsInserted
.count(), 0);
1098 QCOMPARE(spyItemsRemoved
.count(), 1);
1099 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1100 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1102 m_model
->setShowHiddenFiles(true);
1103 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1104 QCOMPARE(spyItemsInserted
.count(), 1);
1105 QCOMPARE(spyItemsRemoved
.count(), 0);
1106 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1107 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1110 QCOMPARE(itemsInModel(), QStringList());
1111 QCOMPARE(spyItemsInserted
.count(), 0);
1112 QCOMPARE(spyItemsRemoved
.count(), 1);
1113 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1114 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1116 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1117 // Verify that this does not make the model crash.
1118 m_model
->setShowHiddenFiles(false);
1122 * Verify that filtered items are removed when their parent is collapsed.
1124 void KFileItemModelTest::collapseParentOfHiddenItems()
1126 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1127 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1128 m_model
->setRoles(modelRoles
);
1131 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1132 m_testDir
->createFiles(files
);
1134 m_model
->loadDirectory(m_testDir
->url());
1135 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1136 QCOMPARE(m_model
->count(), 1); // Only "a/"
1139 m_model
->setExpanded(0, true);
1140 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1141 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1144 m_model
->setExpanded(1, true);
1145 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1146 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1149 m_model
->setExpanded(2, true);
1150 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1151 QCOMPARE(m_model
->count(), 7); // 7 items: "a/", "a/b/", "a/b/c", "a/b/c/d/", "a/b/c/1", "a/b/1", "a/1"
1153 // Set a name filter that matches nothing -> only the expanded folders remain.
1154 m_model
->setNameFilter("xyz");
1155 QCOMPARE(m_model
->count(), 3);
1156 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1158 // Collapse the folder "a/".
1159 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1160 m_model
->setExpanded(0, false);
1161 QCOMPARE(spyItemsRemoved
.count(), 1);
1162 QCOMPARE(m_model
->count(), 1);
1163 QCOMPARE(itemsInModel(), QStringList() << "a");
1165 // Remove the filter -> no files should appear (and we should not get a crash).
1166 m_model
->setNameFilter(QString());
1167 QCOMPARE(m_model
->count(), 1);
1171 * Verify that filtered items are removed when their parent is deleted.
1173 void KFileItemModelTest::removeParentOfHiddenItems()
1175 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1176 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1177 m_model
->setRoles(modelRoles
);
1180 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1181 m_testDir
->createFiles(files
);
1183 m_model
->loadDirectory(m_testDir
->url());
1184 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1185 QCOMPARE(m_model
->count(), 1); // Only "a/"
1188 m_model
->setExpanded(0, true);
1189 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1190 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1193 m_model
->setExpanded(1, true);
1194 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1195 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1198 m_model
->setExpanded(2, true);
1199 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1200 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"
1202 // Set a name filter that matches nothing -> only the expanded folders remain.
1203 m_model
->setNameFilter("xyz");
1204 QCOMPARE(m_model
->count(), 3);
1205 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1207 // Simulate the deletion of the directory "a/b/".
1208 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1209 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1210 QCOMPARE(spyItemsRemoved
.count(), 1);
1211 QCOMPARE(m_model
->count(), 1);
1212 QCOMPARE(itemsInModel(), QStringList() << "a");
1214 // Remove the filter -> only the file "a/1" should appear.
1215 m_model
->setNameFilter(QString());
1216 QCOMPARE(m_model
->count(), 2);
1217 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1221 * Create a tree structure where parent-child relationships can not be
1222 * determined by parsing the URLs, and verify that KFileItemModel
1223 * handles them correctly.
1225 void KFileItemModelTest::testGeneralParentChildRelationships()
1227 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1228 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1229 m_model
->setRoles(modelRoles
);
1232 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1233 m_testDir
->createFiles(files
);
1235 m_model
->loadDirectory(m_testDir
->url());
1236 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1237 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1239 // Expand all folders.
1240 m_model
->setExpanded(0, true);
1241 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1242 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1244 m_model
->setExpanded(1, true);
1245 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1246 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1248 m_model
->setExpanded(3, true);
1249 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1250 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1252 m_model
->setExpanded(4, true);
1253 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1254 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1256 // Add some more children and grand-children.
1257 const QUrl parent1
= m_model
->fileItem(0).url();
1258 const QUrl parent2
= m_model
->fileItem(3).url();
1259 const QUrl realChild1
= m_model
->fileItem(1).url();
1260 const QUrl realChild2
= m_model
->fileItem(4).url();
1262 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown
));
1263 m_model
->slotCompleted();
1264 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1266 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown
));
1267 m_model
->slotCompleted();
1268 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1270 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1271 m_model
->slotCompleted();
1272 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1274 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown
));
1275 m_model
->slotCompleted();
1276 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1278 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown
));
1279 m_model
->slotCompleted();
1280 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1282 // Set a name filter that matches nothing -> only expanded folders remain.
1283 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1284 m_model
->setNameFilter("xyz");
1285 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1286 QCOMPARE(itemsRemovedSpy
.count(), 1);
1287 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1288 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1289 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1291 // Collapse "parent1".
1292 m_model
->setExpanded(0, false);
1293 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1294 QCOMPARE(itemsRemovedSpy
.count(), 1);
1295 arguments
= itemsRemovedSpy
.takeFirst();
1296 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1297 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1299 // Remove "parent2".
1300 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1301 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1302 QCOMPARE(itemsRemovedSpy
.count(), 1);
1303 arguments
= itemsRemovedSpy
.takeFirst();
1304 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1305 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1307 // Clear filter, verify that no items reappear.
1308 m_model
->setNameFilter(QString());
1309 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1312 void KFileItemModelTest::testNameRoleGroups()
1315 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1317 m_testDir
->createFiles(files
);
1319 m_model
->setGroupedSorting(true);
1320 m_model
->loadDirectory(m_testDir
->url());
1321 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1322 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1324 QList
<QPair
<int, QVariant
> > expectedGroups
;
1325 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1326 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1327 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1328 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1329 QCOMPARE(m_model
->groups(), expectedGroups
);
1331 // Rename d.txt to a.txt.
1332 QHash
<QByteArray
, QVariant
> data
;
1333 data
.insert("text", "a.txt");
1334 m_model
->setData(2, data
);
1335 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1336 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1338 expectedGroups
.clear();
1339 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1340 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1341 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1342 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1343 QCOMPARE(m_model
->groups(), expectedGroups
);
1345 // Rename c.txt to d.txt.
1346 data
.insert("text", "d.txt");
1347 m_model
->setData(2, data
);
1348 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1349 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1351 expectedGroups
.clear();
1352 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1353 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1354 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1355 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1356 QCOMPARE(m_model
->groups(), expectedGroups
);
1358 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1359 const KFileItem fileItemD
= m_model
->fileItem(2);
1360 KFileItem fileItemC
= fileItemD
;
1361 QUrl urlC
= fileItemC
.url();
1362 urlC
.adjusted(QUrl::RemoveFilename
);
1363 urlC
.setPath(urlC
.path() + "c.txt");
1364 fileItemC
.setUrl(urlC
);
1366 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1367 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1368 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1370 expectedGroups
.clear();
1371 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1372 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1373 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1374 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1375 QCOMPARE(m_model
->groups(), expectedGroups
);
1378 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1380 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1381 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1382 m_model
->setRoles(modelRoles
);
1385 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1387 m_testDir
->createFiles(files
);
1389 m_model
->setGroupedSorting(true);
1390 m_model
->loadDirectory(m_testDir
->url());
1391 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1392 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1394 QList
<QPair
<int, QVariant
> > expectedGroups
;
1395 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1396 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1397 QCOMPARE(m_model
->groups(), expectedGroups
);
1399 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1400 expectedGroups
.clear();
1401 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1402 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1404 m_model
->setExpanded(0, true);
1405 QVERIFY(m_model
->isExpanded(0));
1406 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1407 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1408 QCOMPARE(m_model
->groups(), expectedGroups
);
1410 m_model
->setExpanded(3, true);
1411 QVERIFY(m_model
->isExpanded(3));
1412 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1413 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1414 QCOMPARE(m_model
->groups(), expectedGroups
);
1417 void KFileItemModelTest::testInconsistentModel()
1419 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1420 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1421 m_model
->setRoles(modelRoles
);
1424 files
<< "a/b/c1.txt" << "a/b/c2.txt";
1426 m_testDir
->createFiles(files
);
1428 m_model
->loadDirectory(m_testDir
->url());
1429 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1430 QCOMPARE(itemsInModel(), QStringList() << "a");
1432 // Expand "a/" and "a/b/".
1433 m_model
->setExpanded(0, true);
1434 QVERIFY(m_model
->isExpanded(0));
1435 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1436 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1438 m_model
->setExpanded(1, true);
1439 QVERIFY(m_model
->isExpanded(1));
1440 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1441 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1443 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1444 // Such a thing can in principle happen when performing a search, and there
1446 // (a) match the search string, and
1447 // (b) are children of a folder that matches the search string and is expanded.
1449 // Note that the first item in the list of added items must be new (i.e., not
1450 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1451 // it receives items that are in the model already and ignore them.
1452 QUrl
url(m_model
->directory().url() + "/a2");
1453 KFileItem
newItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
1455 KFileItemList items
;
1456 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1457 m_model
->slotItemsAdded(m_model
->directory(), items
);
1458 m_model
->slotCompleted();
1459 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1461 m_model
->setExpanded(0, false);
1463 // Test that the right items have been removed, see
1464 // https://bugs.kde.org/show_bug.cgi?id=324371
1465 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1467 // Test that resorting does not cause a crash, see
1468 // https://bugs.kde.org/show_bug.cgi?id=325359
1469 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1470 m_model
->resortAllItems();
1474 void KFileItemModelTest::testChangeRolesForFilteredItems()
1476 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1477 modelRoles
<< "owner";
1478 m_model
->setRoles(modelRoles
);
1481 files
<< "a.txt" << "aa.txt" << "aaa.txt";
1482 m_testDir
->createFiles(files
);
1484 m_model
->loadDirectory(m_testDir
->url());
1485 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1486 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1488 for (int index
= 0; index
< m_model
->count(); ++index
) {
1489 // All items should have the "text" and "owner" roles, but not "group".
1490 QVERIFY(m_model
->data(index
).contains("text"));
1491 QVERIFY(m_model
->data(index
).contains("owner"));
1492 QVERIFY(!m_model
->data(index
).contains("group"));
1495 // Add a filter, such that only "aaa.txt" remains in the model.
1496 m_model
->setNameFilter("aaa");
1497 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1499 // Add the "group" role.
1500 modelRoles
<< "group";
1501 m_model
->setRoles(modelRoles
);
1503 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1504 m_model
->setNameFilter("aa");
1505 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1507 for (int index
= 0; index
< m_model
->count(); ++index
) {
1508 // All items should have the "text", "owner", and "group" roles.
1509 QVERIFY(m_model
->data(index
).contains("text"));
1510 QVERIFY(m_model
->data(index
).contains("owner"));
1511 QVERIFY(m_model
->data(index
).contains("group"));
1514 // Remove the "owner" role.
1515 modelRoles
.remove("owner");
1516 m_model
->setRoles(modelRoles
);
1518 // Clear the filter, and verify that all items have the expected roles
1519 m_model
->setNameFilter(QString());
1520 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1522 for (int index
= 0; index
< m_model
->count(); ++index
) {
1523 // All items should have the "text" and "group" roles, but now "owner".
1524 QVERIFY(m_model
->data(index
).contains("text"));
1525 QVERIFY(!m_model
->data(index
).contains("owner"));
1526 QVERIFY(m_model
->data(index
).contains("group"));
1530 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1532 KFileItemList items
;
1534 KIO::UDSEntry entry
;
1535 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1536 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1537 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1538 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1539 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1540 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1542 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1543 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1544 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1546 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1547 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1548 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1550 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1551 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1552 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1554 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1555 m_model
->slotCompleted();
1557 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1560 m_model
->setNameFilter("a");
1561 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1564 m_model
->setSortRole("owner");
1566 // Clear the filter, and verify that the items are sorted correctly.
1567 m_model
->setNameFilter(QString());
1568 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1571 void KFileItemModelTest::testRefreshFilteredItems()
1574 files
<< "a.txt" << "b.txt" << "c.jpg" << "d.jpg";
1575 m_testDir
->createFiles(files
);
1577 m_model
->loadDirectory(m_testDir
->url());
1578 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1579 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1581 const KFileItem fileItemC
= m_model
->fileItem(2);
1583 // Show only the .txt files.
1584 m_model
->setNameFilter(".txt");
1585 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1587 // Rename one of the .jpg files.
1588 KFileItem fileItemE
= fileItemC
;
1589 QUrl urlE
= fileItemE
.url();
1590 urlE
.adjusted(QUrl::RemoveFilename
);
1591 urlE
.setPath(urlE
.path() + "e.jpg");
1592 fileItemE
.setUrl(urlE
);
1594 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemC
, fileItemE
));
1596 // Show all files again, and verify that the model has updated the file name.
1597 m_model
->setNameFilter(QString());
1598 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1601 void KFileItemModelTest::testCreateMimeData()
1603 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1604 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1605 m_model
->setRoles(modelRoles
);
1609 m_testDir
->createFiles(files
);
1611 m_model
->loadDirectory(m_testDir
->url());
1612 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1613 QCOMPARE(itemsInModel(), QStringList() << "a");
1616 m_model
->setExpanded(0, true);
1617 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1618 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1620 // Verify that creating the MIME data for a child of an expanded folder does
1621 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1623 selection
.insert(1);
1624 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1628 void KFileItemModelTest::testCollapseFolderWhileLoading()
1630 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1631 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1632 m_model
->setRoles(modelRoles
);
1635 files
<< "a2/b/c1.txt";
1636 m_testDir
->createFiles(files
);
1638 m_model
->loadDirectory(m_testDir
->url());
1639 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1640 QCOMPARE(itemsInModel(), QStringList() << "a2");
1643 m_model
->setExpanded(0, true);
1644 QVERIFY(m_model
->isExpanded(0));
1645 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1646 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1649 m_model
->setExpanded(1, true);
1650 QVERIFY(m_model
->isExpanded(1));
1651 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1652 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1654 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1655 // signal is not emitted yet.
1656 const KFileItem fileItemC1
= m_model
->fileItem(2);
1657 KFileItem fileItemC2
= fileItemC1
;
1658 QUrl urlC2
= fileItemC2
.url();
1659 urlC2
.adjusted(QUrl::RemoveFilename
);
1660 urlC2
.setPath(urlC2
.path() + "c2.txt");
1661 fileItemC2
.setUrl(urlC2
);
1663 const QUrl urlB
= m_model
->fileItem(1).url();
1664 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1665 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1667 // Collapse "a2/". This should also remove all its (indirect) children from
1668 // the model and from the model's m_pendingItemsToInsert member.
1669 m_model
->setExpanded(0, false);
1670 QCOMPARE(itemsInModel(), QStringList() << "a2");
1672 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1673 // is still in m_pendingItemsToInsert, then we might get a crash, see
1674 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1675 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1676 // without parent in the model.
1677 m_model
->slotCompleted();
1678 QCOMPARE(itemsInModel(), QStringList() << "a2");
1680 // Expand "a2/" again.
1681 m_model
->setExpanded(0, true);
1682 QVERIFY(m_model
->isExpanded(0));
1683 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1684 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1686 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1687 // completed() signal is not emitted yet.
1688 const KFileItem fileItemA2
= m_model
->fileItem(0);
1689 KFileItem fileItemA1
= fileItemA2
;
1690 QUrl urlA1
= fileItemA1
.url();
1691 urlA1
.adjusted(QUrl::RemoveFilename
);
1692 urlA1
.setPath(urlA1
.path() + "a1");
1693 fileItemA1
.setUrl(urlA1
);
1695 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1696 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1698 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1699 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1700 // confuse the code which collapses the folder.
1701 m_model
->setExpanded(0, false);
1702 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1703 QVERIFY(!m_model
->isExpanded(0));
1704 QVERIFY(!m_model
->isExpanded(1));
1707 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1710 files
<< "a.txt" << "b.txt" << "c.txt" << "d.txt";
1711 m_testDir
->createFiles(files
);
1713 m_model
->loadDirectory(m_testDir
->url());
1714 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1715 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1717 const KFileItem fileItemB
= m_model
->fileItem(1);
1719 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1721 list
<< fileItemB
<< fileItemB
;
1722 m_model
->slotItemsDeleted(list
);
1724 QVERIFY(m_model
->isConsistent());
1725 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1728 QStringList
KFileItemModelTest::itemsInModel() const
1731 for (int i
= 0; i
< m_model
->count(); i
++) {
1732 items
<< m_model
->fileItem(i
).text();
1737 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1739 #include "kfileitemmodeltest.moc"