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 KUrl urlA
= fileItemA
.url();
408 urlA
.setFileName("a.txt");
409 fileItemA
.setUrl(urlA
);
411 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
413 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
414 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
417 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
419 //QSKIP("Temporary disabled", SkipSingle);
421 // KFileItemModel prevents that inserting a punch of items sequentially
422 // results in an itemsInserted()-signal for each item. Instead internally
423 // a timeout is given that collects such operations and results in only
424 // one itemsInserted()-signal. However in this test we want to stress
425 // KFileItemModel to do a lot of insert operation and hence decrease
426 // the timeout to 1 millisecond.
427 m_testDir
->createFile("1");
428 m_model
->loadDirectory(m_testDir
->url());
429 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
430 QCOMPARE(m_model
->count(), 1);
432 // Insert 10 items for 20 times. After each insert operation the model consistency
434 QSet
<int> insertedItems
;
435 for (int i
= 0; i
< 20; ++i
) {
436 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
438 for (int j
= 0; j
< 10; ++j
) {
439 int itemName
= qrand();
440 while (insertedItems
.contains(itemName
)) {
443 insertedItems
.insert(itemName
);
445 m_testDir
->createFile(QString::number(itemName
));
448 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
449 if (spy
.count() == 0) {
450 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
453 QVERIFY(m_model
->isConsistent());
456 QCOMPARE(m_model
->count(), 201);
459 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
462 files
<< "B" << "E" << "G";
463 m_testDir
->createFiles(files
);
465 // Due to inserting the 3 items one item-range with index == 0 and
466 // count == 3 must be given
467 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
468 m_model
->loadDirectory(m_testDir
->url());
469 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
471 QCOMPARE(spy1
.count(), 1);
472 QList
<QVariant
> arguments
= spy1
.takeFirst();
473 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
474 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
476 // The indexes of the item-ranges must always be related to the model before
477 // the items have been inserted. Having:
480 // and inserting A, C, D, F the resulting model will be:
483 // and the item-ranges must be:
484 // index: 0, count: 1 for A
485 // index: 1, count: 2 for B, C
486 // index: 2, count: 1 for G
489 files
<< "A" << "C" << "D" << "F";
490 m_testDir
->createFiles(files
);
492 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
493 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
494 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
496 QCOMPARE(spy2
.count(), 1);
497 arguments
= spy2
.takeFirst();
498 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
499 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
502 void KFileItemModelTest::testExpandItems()
504 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
505 // Besides testing the basic item expansion functionality, the test makes sure that
506 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
507 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
508 // first three characters.
509 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
510 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
511 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
512 m_model
->setRoles(modelRoles
);
515 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
516 m_testDir
->createFiles(files
);
518 // Store the URLs of all folders in a set.
519 QSet
<KUrl
> allFolders
;
520 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
522 m_model
->loadDirectory(m_testDir
->url());
523 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
525 // So far, the model contains only "a/"
526 QCOMPARE(m_model
->count(), 1);
527 QVERIFY(m_model
->isExpandable(0));
528 QVERIFY(!m_model
->isExpanded(0));
529 QVERIFY(m_model
->expandedDirectories().empty());
531 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
533 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
534 m_model
->setExpanded(0, true);
535 QVERIFY(m_model
->isExpanded(0));
536 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
537 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
538 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
540 QCOMPARE(spyInserted
.count(), 1);
541 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
542 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
544 QVERIFY(m_model
->isExpandable(1));
545 QVERIFY(!m_model
->isExpanded(1));
546 QVERIFY(m_model
->isExpandable(2));
547 QVERIFY(!m_model
->isExpanded(2));
549 // Expand the folder "a/a/" -> "a/a/1" becomes visible
550 m_model
->setExpanded(1, true);
551 QVERIFY(m_model
->isExpanded(1));
552 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
553 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
554 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
556 QCOMPARE(spyInserted
.count(), 1);
557 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
558 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
560 QVERIFY(!m_model
->isExpandable(2));
561 QVERIFY(!m_model
->isExpanded(2));
563 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
564 m_model
->setExpanded(3, true);
565 QVERIFY(m_model
->isExpanded(3));
566 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
567 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
568 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
570 QCOMPARE(spyInserted
.count(), 1);
571 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
572 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
574 QVERIFY(!m_model
->isExpandable(4));
575 QVERIFY(!m_model
->isExpanded(4));
577 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
579 // Collapse the top-level folder -> all other items should disappear
580 m_model
->setExpanded(0, false);
581 QVERIFY(!m_model
->isExpanded(0));
582 QCOMPARE(m_model
->count(), 1);
583 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
585 QCOMPARE(spyRemoved
.count(), 1);
586 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
587 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
588 QVERIFY(m_model
->isConsistent());
590 // Clear the model, reload the folder and try to restore the expanded folders.
592 QCOMPARE(m_model
->count(), 0);
593 QVERIFY(m_model
->expandedDirectories().empty());
595 m_model
->loadDirectory(m_testDir
->url());
596 m_model
->restoreExpandedDirectories(allFolders
);
597 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
598 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
599 QVERIFY(m_model
->isExpanded(0));
600 QVERIFY(m_model
->isExpanded(1));
601 QVERIFY(!m_model
->isExpanded(2));
602 QVERIFY(m_model
->isExpanded(3));
603 QVERIFY(!m_model
->isExpanded(4));
604 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
605 QVERIFY(m_model
->isConsistent());
607 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
608 // This is how DolphinView restores the expanded folders when navigating in history.
609 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
610 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
611 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
612 m_model
->restoreExpandedDirectories(allFolders
);
613 m_model
->loadDirectory(m_testDir
->url());
614 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
615 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
616 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
618 // Remove all expanded items by changing the roles
620 m_model
->setRoles(originalModelRoles
);
621 QVERIFY(!m_model
->isExpanded(0));
622 QCOMPARE(m_model
->count(), 1);
623 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
625 QCOMPARE(spyRemoved
.count(), 1);
626 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
627 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
628 QVERIFY(m_model
->isConsistent());
631 void KFileItemModelTest::testExpandParentItems()
633 // Create a tree structure of folders:
641 QSet
<QByteArray
> modelRoles
= m_model
->roles();
642 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
643 m_model
->setRoles(modelRoles
);
646 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
647 m_testDir
->createFiles(files
);
649 m_model
->loadDirectory(m_testDir
->url());
650 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
652 // So far, the model contains only "a 1/" and "a2/".
653 QCOMPARE(m_model
->count(), 2);
654 QVERIFY(m_model
->expandedDirectories().empty());
656 // Expand the parents of "a2/b2/c2".
657 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
658 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
660 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
661 // It's important that only the parents of "a1/b1/c1" are expanded.
662 QCOMPARE(m_model
->count(), 4);
663 QVERIFY(!m_model
->isExpanded(0));
664 QVERIFY(m_model
->isExpanded(1));
665 QVERIFY(m_model
->isExpanded(2));
666 QVERIFY(!m_model
->isExpanded(3));
668 // Expand the parents of "a 1/b1".
669 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
670 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
672 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
673 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
674 QCOMPARE(m_model
->count(), 5);
675 QVERIFY(m_model
->isExpanded(0));
676 QVERIFY(!m_model
->isExpanded(1));
677 QVERIFY(m_model
->isExpanded(2));
678 QVERIFY(m_model
->isExpanded(3));
679 QVERIFY(!m_model
->isExpanded(4));
680 QVERIFY(m_model
->isConsistent());
683 m_model
->setExpanded(1, true);
684 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
685 QCOMPARE(m_model
->count(), 6);
686 QVERIFY(m_model
->isExpanded(0));
687 QVERIFY(m_model
->isExpanded(1));
688 QVERIFY(!m_model
->isExpanded(2));
689 QVERIFY(m_model
->isExpanded(3));
690 QVERIFY(m_model
->isExpanded(4));
691 QVERIFY(!m_model
->isExpanded(5));
692 QVERIFY(m_model
->isConsistent());
694 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
695 m_model
->setExpanded(1, false);
696 QCOMPARE(m_model
->count(), 5);
697 QVERIFY(m_model
->isExpanded(0));
698 QVERIFY(!m_model
->isExpanded(1));
699 QVERIFY(m_model
->isExpanded(2));
700 QVERIFY(m_model
->isExpanded(3));
701 QVERIFY(!m_model
->isExpanded(4));
702 QVERIFY(m_model
->isConsistent());
706 * Renaming an expanded folder by prepending its name with a dot makes it
707 * hidden. Verify that this does not cause an inconsistent model state and
708 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
710 void KFileItemModelTest::testMakeExpandedItemHidden()
712 QSet
<QByteArray
> modelRoles
= m_model
->roles();
713 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
714 m_model
->setRoles(modelRoles
);
717 m_testDir
->createFile("1a/2a/3a");
718 m_testDir
->createFile("1a/2a/3b");
719 m_testDir
->createFile("1a/2b");
720 m_testDir
->createFile("1b");
722 m_model
->loadDirectory(m_testDir
->url());
723 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
725 // So far, the model contains only "1a/" and "1b".
726 QCOMPARE(m_model
->count(), 2);
727 m_model
->setExpanded(0, true);
728 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
730 // Now "1a/2a" and "1a/2b" have appeared.
731 QCOMPARE(m_model
->count(), 4);
732 m_model
->setExpanded(1, true);
733 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
734 QCOMPARE(m_model
->count(), 6);
736 // Rename "1a/2" and make it hidden.
737 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
738 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
740 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
741 bool ok
= job
->exec();
743 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
745 // "1a/2" and its subfolders have disappeared now.
746 QVERIFY(m_model
->isConsistent());
747 QCOMPARE(m_model
->count(), 3);
749 m_model
->setExpanded(0, false);
750 QCOMPARE(m_model
->count(), 2);
754 void KFileItemModelTest::testRemoveFilteredExpandedItems()
756 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
757 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
758 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
759 m_model
->setRoles(modelRoles
);
762 files
<< "folder/child" << "file"; // missing folders are created automatically
763 m_testDir
->createFiles(files
);
765 m_model
->loadDirectory(m_testDir
->url());
766 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
768 // So far, the model contains only "folder/" and "file".
769 QCOMPARE(m_model
->count(), 2);
770 QVERIFY(m_model
->isExpandable(0));
771 QVERIFY(!m_model
->isExpandable(1));
772 QVERIFY(!m_model
->isExpanded(0));
773 QVERIFY(!m_model
->isExpanded(1));
774 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
776 // Expand "folder" -> "folder/child" becomes visible.
777 m_model
->setExpanded(0, true);
778 QVERIFY(m_model
->isExpanded(0));
779 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
780 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
782 // Add a name filter.
783 m_model
->setNameFilter("f");
784 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
786 m_model
->setNameFilter("fo");
787 QCOMPARE(itemsInModel(), QStringList() << "folder");
789 // Remove all expanded items by changing the roles
790 m_model
->setRoles(originalModelRoles
);
791 QVERIFY(!m_model
->isExpanded(0));
792 QCOMPARE(itemsInModel(), QStringList() << "folder");
794 // Remove the name filter and verify that "folder/child" does not reappear.
795 m_model
->setNameFilter(QString());
796 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
799 void KFileItemModelTest::testSorting()
801 // Create some files with different sizes and modification times to check the different sorting options
802 QDateTime now
= QDateTime::currentDateTime();
804 QSet
<QByteArray
> roles
;
805 roles
.insert("text");
806 roles
.insert("isExpanded");
807 roles
.insert("isExpandable");
808 roles
.insert("expandedParentsCount");
809 m_model
->setRoles(roles
);
811 m_testDir
->createDir("c/c-2");
812 m_testDir
->createFile("c/c-2/c-3");
813 m_testDir
->createFile("c/c-1");
815 m_testDir
->createFile("a", "A file", now
.addDays(-3));
816 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
817 m_testDir
->createDir("c", now
.addDays(-2));
818 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
819 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
820 m_testDir
->createFile(".f");
822 m_model
->loadDirectory(m_testDir
->url());
823 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
825 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
826 m_model
->setExpanded(index
, true);
827 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
829 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
830 m_model
->setExpanded(index
, true);
831 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
833 // Default: Sort by Name, ascending
834 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
835 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
836 QVERIFY(m_model
->sortDirectoriesFirst());
837 QVERIFY(!m_model
->showHiddenFiles());
838 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
840 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
842 // Sort by Name, ascending, 'Sort Folders First' disabled
843 m_model
->setSortDirectoriesFirst(false);
844 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
845 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
846 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
847 QCOMPARE(spyItemsMoved
.count(), 1);
848 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
849 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
851 // Sort by Name, descending
852 m_model
->setSortDirectoriesFirst(true);
853 m_model
->setSortOrder(Qt::DescendingOrder
);
854 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
855 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
856 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
857 QCOMPARE(spyItemsMoved
.count(), 2);
858 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
859 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
860 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
861 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
863 // Sort by Date, descending
864 m_model
->setSortDirectoriesFirst(true);
865 m_model
->setSortRole("date");
866 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
867 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
868 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
869 QCOMPARE(spyItemsMoved
.count(), 1);
870 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
871 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
873 // Sort by Date, ascending
874 m_model
->setSortOrder(Qt::AscendingOrder
);
875 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
876 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
877 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
878 QCOMPARE(spyItemsMoved
.count(), 1);
879 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
880 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
882 // Sort by Date, ascending, 'Sort Folders First' disabled
883 m_model
->setSortDirectoriesFirst(false);
884 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
885 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
886 QVERIFY(!m_model
->sortDirectoriesFirst());
887 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
888 QCOMPARE(spyItemsMoved
.count(), 1);
889 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
890 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
892 // Sort by Name, ascending, 'Sort Folders First' disabled
893 m_model
->setSortRole("text");
894 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
895 QVERIFY(!m_model
->sortDirectoriesFirst());
896 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
897 QCOMPARE(spyItemsMoved
.count(), 1);
898 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
899 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
901 // Sort by Size, ascending, 'Sort Folders First' disabled
902 m_model
->setSortRole("size");
903 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
904 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
905 QVERIFY(!m_model
->sortDirectoriesFirst());
906 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
907 QCOMPARE(spyItemsMoved
.count(), 1);
908 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
909 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
911 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
912 m_model
->setSortDirectoriesFirst(true);
913 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
914 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
915 QVERIFY(m_model
->sortDirectoriesFirst());
916 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
917 QCOMPARE(spyItemsMoved
.count(), 0);
919 // Sort by Size, descending, 'Sort Folders First' enabled
920 m_model
->setSortOrder(Qt::DescendingOrder
);
921 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
922 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
923 QVERIFY(m_model
->sortDirectoriesFirst());
924 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
925 QCOMPARE(spyItemsMoved
.count(), 1);
926 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
927 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
929 // TODO: Sort by other roles; show/hide hidden files
932 void KFileItemModelTest::testIndexForKeyboardSearch()
935 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
936 m_testDir
->createFiles(files
);
938 m_model
->loadDirectory(m_testDir
->url());
939 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
941 // Search from index 0
942 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
943 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
944 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
945 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
946 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
947 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
948 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
949 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
950 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
951 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
952 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
954 // Start a search somewhere in the middle
955 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
956 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
957 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
958 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
960 // Test searches that go past the last item back to index 0
961 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
962 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
963 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
964 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
966 // Test searches that yield no result
967 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
968 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
969 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
970 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
971 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
973 // Test upper case searches (note that search is case insensitive)
974 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
975 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
976 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
977 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
979 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
982 void KFileItemModelTest::testNameFilter()
985 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
986 m_testDir
->createFiles(files
);
988 m_model
->loadDirectory(m_testDir
->url());
989 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
991 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
992 QCOMPARE(m_model
->count(), 3);
994 m_model
->setNameFilter("A2"); // Shows only A2
995 QCOMPARE(m_model
->count(), 1);
997 m_model
->setNameFilter("A2"); // Shows only A1
998 QCOMPARE(m_model
->count(), 1);
1000 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1001 QCOMPARE(m_model
->count(), 2);
1003 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1004 QCOMPARE(m_model
->count(), 2);
1006 m_model
->setNameFilter(QString()); // Shows again all items
1007 QCOMPARE(m_model
->count(), 5);
1011 * Verifies that we do not crash when adding a KFileItem with an empty path.
1012 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1013 * tried to always read the first character of the path, even if the path is empty.
1015 void KFileItemModelTest::testEmptyPath()
1017 QSet
<QByteArray
> roles
;
1018 roles
.insert("text");
1019 roles
.insert("isExpanded");
1020 roles
.insert("isExpandable");
1021 roles
.insert("expandedParentsCount");
1022 m_model
->setRoles(roles
);
1024 const KUrl emptyUrl
;
1025 QVERIFY(emptyUrl
.path().isEmpty());
1027 const KUrl
url("file:///test/");
1029 KFileItemList items
;
1030 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1031 m_model
->slotItemsAdded(emptyUrl
, items
);
1032 m_model
->slotCompleted();
1036 * Verifies that the 'isExpanded' state of folders does not change when the
1037 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1039 void KFileItemModelTest::testRefreshExpandedItem()
1041 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1042 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1043 m_model
->setRoles(modelRoles
);
1046 files
<< "a/1" << "a/2" << "3" << "4";
1047 m_testDir
->createFiles(files
);
1049 m_model
->loadDirectory(m_testDir
->url());
1050 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1051 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1053 m_model
->setExpanded(0, true);
1054 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1055 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1056 QVERIFY(m_model
->isExpanded(0));
1058 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1060 const KFileItem item
= m_model
->fileItem(0);
1061 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1062 QVERIFY(!spyItemsChanged
.isEmpty());
1064 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1065 QVERIFY(m_model
->isExpanded(0));
1069 * Verify that removing hidden files and folders from the model does not
1070 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1072 void KFileItemModelTest::testRemoveHiddenItems()
1074 m_testDir
->createDir(".a");
1075 m_testDir
->createDir(".b");
1076 m_testDir
->createDir("c");
1077 m_testDir
->createDir("d");
1078 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1080 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1081 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1083 m_model
->setShowHiddenFiles(true);
1084 m_model
->loadDirectory(m_testDir
->url());
1085 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1086 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1087 QCOMPARE(spyItemsInserted
.count(), 1);
1088 QCOMPARE(spyItemsRemoved
.count(), 0);
1089 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1090 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1092 m_model
->setShowHiddenFiles(false);
1093 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1094 QCOMPARE(spyItemsInserted
.count(), 0);
1095 QCOMPARE(spyItemsRemoved
.count(), 1);
1096 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1097 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1099 m_model
->setShowHiddenFiles(true);
1100 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1101 QCOMPARE(spyItemsInserted
.count(), 1);
1102 QCOMPARE(spyItemsRemoved
.count(), 0);
1103 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1104 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1107 QCOMPARE(itemsInModel(), QStringList());
1108 QCOMPARE(spyItemsInserted
.count(), 0);
1109 QCOMPARE(spyItemsRemoved
.count(), 1);
1110 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1111 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1113 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1114 // Verify that this does not make the model crash.
1115 m_model
->setShowHiddenFiles(false);
1119 * Verify that filtered items are removed when their parent is collapsed.
1121 void KFileItemModelTest::collapseParentOfHiddenItems()
1123 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1124 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1125 m_model
->setRoles(modelRoles
);
1128 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1129 m_testDir
->createFiles(files
);
1131 m_model
->loadDirectory(m_testDir
->url());
1132 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1133 QCOMPARE(m_model
->count(), 1); // Only "a/"
1136 m_model
->setExpanded(0, true);
1137 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1138 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1141 m_model
->setExpanded(1, true);
1142 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1143 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1146 m_model
->setExpanded(2, true);
1147 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1148 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"
1150 // Set a name filter that matches nothing -> only the expanded folders remain.
1151 m_model
->setNameFilter("xyz");
1152 QCOMPARE(m_model
->count(), 3);
1153 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1155 // Collapse the folder "a/".
1156 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1157 m_model
->setExpanded(0, false);
1158 QCOMPARE(spyItemsRemoved
.count(), 1);
1159 QCOMPARE(m_model
->count(), 1);
1160 QCOMPARE(itemsInModel(), QStringList() << "a");
1162 // Remove the filter -> no files should appear (and we should not get a crash).
1163 m_model
->setNameFilter(QString());
1164 QCOMPARE(m_model
->count(), 1);
1168 * Verify that filtered items are removed when their parent is deleted.
1170 void KFileItemModelTest::removeParentOfHiddenItems()
1172 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1173 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1174 m_model
->setRoles(modelRoles
);
1177 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1178 m_testDir
->createFiles(files
);
1180 m_model
->loadDirectory(m_testDir
->url());
1181 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1182 QCOMPARE(m_model
->count(), 1); // Only "a/"
1185 m_model
->setExpanded(0, true);
1186 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1187 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1190 m_model
->setExpanded(1, true);
1191 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1192 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1195 m_model
->setExpanded(2, true);
1196 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1197 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"
1199 // Set a name filter that matches nothing -> only the expanded folders remain.
1200 m_model
->setNameFilter("xyz");
1201 QCOMPARE(m_model
->count(), 3);
1202 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1204 // Simulate the deletion of the directory "a/b/".
1205 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1206 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1207 QCOMPARE(spyItemsRemoved
.count(), 1);
1208 QCOMPARE(m_model
->count(), 1);
1209 QCOMPARE(itemsInModel(), QStringList() << "a");
1211 // Remove the filter -> only the file "a/1" should appear.
1212 m_model
->setNameFilter(QString());
1213 QCOMPARE(m_model
->count(), 2);
1214 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1218 * Create a tree structure where parent-child relationships can not be
1219 * determined by parsing the URLs, and verify that KFileItemModel
1220 * handles them correctly.
1222 void KFileItemModelTest::testGeneralParentChildRelationships()
1224 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1225 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1226 m_model
->setRoles(modelRoles
);
1229 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1230 m_testDir
->createFiles(files
);
1232 m_model
->loadDirectory(m_testDir
->url());
1233 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1234 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1236 // Expand all folders.
1237 m_model
->setExpanded(0, true);
1238 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1239 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1241 m_model
->setExpanded(1, true);
1242 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1243 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1245 m_model
->setExpanded(3, true);
1246 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1247 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1249 m_model
->setExpanded(4, true);
1250 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1251 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1253 // Add some more children and grand-children.
1254 const KUrl parent1
= m_model
->fileItem(0).url();
1255 const KUrl parent2
= m_model
->fileItem(3).url();
1256 const KUrl realChild1
= m_model
->fileItem(1).url();
1257 const KUrl realChild2
= m_model
->fileItem(4).url();
1259 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1260 m_model
->slotCompleted();
1261 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1263 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1264 m_model
->slotCompleted();
1265 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1267 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1268 m_model
->slotCompleted();
1269 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1271 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1272 m_model
->slotCompleted();
1273 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1275 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1276 m_model
->slotCompleted();
1277 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1279 // Set a name filter that matches nothing -> only expanded folders remain.
1280 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1281 m_model
->setNameFilter("xyz");
1282 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1283 QCOMPARE(itemsRemovedSpy
.count(), 1);
1284 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1285 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1286 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1288 // Collapse "parent1".
1289 m_model
->setExpanded(0, false);
1290 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1291 QCOMPARE(itemsRemovedSpy
.count(), 1);
1292 arguments
= itemsRemovedSpy
.takeFirst();
1293 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1294 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1296 // Remove "parent2".
1297 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1298 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1299 QCOMPARE(itemsRemovedSpy
.count(), 1);
1300 arguments
= itemsRemovedSpy
.takeFirst();
1301 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1302 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1304 // Clear filter, verify that no items reappear.
1305 m_model
->setNameFilter(QString());
1306 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1309 void KFileItemModelTest::testNameRoleGroups()
1312 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1314 m_testDir
->createFiles(files
);
1316 m_model
->setGroupedSorting(true);
1317 m_model
->loadDirectory(m_testDir
->url());
1318 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1319 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1321 QList
<QPair
<int, QVariant
> > expectedGroups
;
1322 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1323 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1324 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1325 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1326 QCOMPARE(m_model
->groups(), expectedGroups
);
1328 // Rename d.txt to a.txt.
1329 QHash
<QByteArray
, QVariant
> data
;
1330 data
.insert("text", "a.txt");
1331 m_model
->setData(2, data
);
1332 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1333 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1335 expectedGroups
.clear();
1336 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1337 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1338 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1339 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1340 QCOMPARE(m_model
->groups(), expectedGroups
);
1342 // Rename c.txt to d.txt.
1343 data
.insert("text", "d.txt");
1344 m_model
->setData(2, data
);
1345 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1346 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1348 expectedGroups
.clear();
1349 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1350 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1351 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1352 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1353 QCOMPARE(m_model
->groups(), expectedGroups
);
1355 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1356 const KFileItem fileItemD
= m_model
->fileItem(2);
1357 KFileItem fileItemC
= fileItemD
;
1358 KUrl urlC
= fileItemC
.url();
1359 urlC
.setFileName("c.txt");
1360 fileItemC
.setUrl(urlC
);
1362 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1363 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1364 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1366 expectedGroups
.clear();
1367 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1368 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1369 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1370 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1371 QCOMPARE(m_model
->groups(), expectedGroups
);
1374 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1376 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1377 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1378 m_model
->setRoles(modelRoles
);
1381 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1383 m_testDir
->createFiles(files
);
1385 m_model
->setGroupedSorting(true);
1386 m_model
->loadDirectory(m_testDir
->url());
1387 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1388 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1390 QList
<QPair
<int, QVariant
> > expectedGroups
;
1391 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1392 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1393 QCOMPARE(m_model
->groups(), expectedGroups
);
1395 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1396 expectedGroups
.clear();
1397 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1398 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1400 m_model
->setExpanded(0, true);
1401 QVERIFY(m_model
->isExpanded(0));
1402 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1403 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1404 QCOMPARE(m_model
->groups(), expectedGroups
);
1406 m_model
->setExpanded(3, true);
1407 QVERIFY(m_model
->isExpanded(3));
1408 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1409 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1410 QCOMPARE(m_model
->groups(), expectedGroups
);
1413 void KFileItemModelTest::testInconsistentModel()
1415 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1416 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1417 m_model
->setRoles(modelRoles
);
1420 files
<< "a/b/c1.txt" << "a/b/c2.txt";
1422 m_testDir
->createFiles(files
);
1424 m_model
->loadDirectory(m_testDir
->url());
1425 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1426 QCOMPARE(itemsInModel(), QStringList() << "a");
1428 // Expand "a/" and "a/b/".
1429 m_model
->setExpanded(0, true);
1430 QVERIFY(m_model
->isExpanded(0));
1431 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1432 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1434 m_model
->setExpanded(1, true);
1435 QVERIFY(m_model
->isExpanded(1));
1436 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1437 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1439 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1440 // Such a thing can in principle happen when performing a search, and there
1442 // (a) match the search string, and
1443 // (b) are children of a folder that matches the search string and is expanded.
1445 // Note that the first item in the list of added items must be new (i.e., not
1446 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1447 // it receives items that are in the model already and ignore them.
1448 KUrl
url(m_model
->directory().url() + "/a2");
1449 KFileItem
newItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
1451 KFileItemList items
;
1452 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1453 m_model
->slotItemsAdded(m_model
->directory(), items
);
1454 m_model
->slotCompleted();
1455 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1457 m_model
->setExpanded(0, false);
1459 // Test that the right items have been removed, see
1460 // https://bugs.kde.org/show_bug.cgi?id=324371
1461 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1463 // Test that resorting does not cause a crash, see
1464 // https://bugs.kde.org/show_bug.cgi?id=325359
1465 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1466 m_model
->resortAllItems();
1470 void KFileItemModelTest::testChangeRolesForFilteredItems()
1472 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1473 modelRoles
<< "owner";
1474 m_model
->setRoles(modelRoles
);
1477 files
<< "a.txt" << "aa.txt" << "aaa.txt";
1478 m_testDir
->createFiles(files
);
1480 m_model
->loadDirectory(m_testDir
->url());
1481 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1482 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1484 for (int index
= 0; index
< m_model
->count(); ++index
) {
1485 // All items should have the "text" and "owner" roles, but not "group".
1486 QVERIFY(m_model
->data(index
).contains("text"));
1487 QVERIFY(m_model
->data(index
).contains("owner"));
1488 QVERIFY(!m_model
->data(index
).contains("group"));
1491 // Add a filter, such that only "aaa.txt" remains in the model.
1492 m_model
->setNameFilter("aaa");
1493 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1495 // Add the "group" role.
1496 modelRoles
<< "group";
1497 m_model
->setRoles(modelRoles
);
1499 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1500 m_model
->setNameFilter("aa");
1501 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1503 for (int index
= 0; index
< m_model
->count(); ++index
) {
1504 // All items should have the "text", "owner", and "group" roles.
1505 QVERIFY(m_model
->data(index
).contains("text"));
1506 QVERIFY(m_model
->data(index
).contains("owner"));
1507 QVERIFY(m_model
->data(index
).contains("group"));
1510 // Remove the "owner" role.
1511 modelRoles
.remove("owner");
1512 m_model
->setRoles(modelRoles
);
1514 // Clear the filter, and verify that all items have the expected roles
1515 m_model
->setNameFilter(QString());
1516 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1518 for (int index
= 0; index
< m_model
->count(); ++index
) {
1519 // All items should have the "text" and "group" roles, but now "owner".
1520 QVERIFY(m_model
->data(index
).contains("text"));
1521 QVERIFY(!m_model
->data(index
).contains("owner"));
1522 QVERIFY(m_model
->data(index
).contains("group"));
1526 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1528 KFileItemList items
;
1530 KIO::UDSEntry entry
;
1531 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1532 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1533 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1534 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1535 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1536 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1538 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1539 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1540 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1542 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1543 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1544 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1546 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1547 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1548 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1550 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1551 m_model
->slotCompleted();
1553 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1556 m_model
->setNameFilter("a");
1557 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1560 m_model
->setSortRole("owner");
1562 // Clear the filter, and verify that the items are sorted correctly.
1563 m_model
->setNameFilter(QString());
1564 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1567 void KFileItemModelTest::testRefreshFilteredItems()
1570 files
<< "a.txt" << "b.txt" << "c.jpg" << "d.jpg";
1571 m_testDir
->createFiles(files
);
1573 m_model
->loadDirectory(m_testDir
->url());
1574 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1575 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1577 const KFileItem fileItemC
= m_model
->fileItem(2);
1579 // Show only the .txt files.
1580 m_model
->setNameFilter(".txt");
1581 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1583 // Rename one of the .jpg files.
1584 KFileItem fileItemE
= fileItemC
;
1585 KUrl urlE
= fileItemE
.url();
1586 urlE
.setFileName("e.jpg");
1587 fileItemE
.setUrl(urlE
);
1589 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemC
, fileItemE
));
1591 // Show all files again, and verify that the model has updated the file name.
1592 m_model
->setNameFilter(QString());
1593 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1596 void KFileItemModelTest::testCreateMimeData()
1598 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1599 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1600 m_model
->setRoles(modelRoles
);
1604 m_testDir
->createFiles(files
);
1606 m_model
->loadDirectory(m_testDir
->url());
1607 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1608 QCOMPARE(itemsInModel(), QStringList() << "a");
1611 m_model
->setExpanded(0, true);
1612 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1613 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1615 // Verify that creating the MIME data for a child of an expanded folder does
1616 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1618 selection
.insert(1);
1619 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1623 void KFileItemModelTest::testCollapseFolderWhileLoading()
1625 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1626 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1627 m_model
->setRoles(modelRoles
);
1630 files
<< "a2/b/c1.txt";
1631 m_testDir
->createFiles(files
);
1633 m_model
->loadDirectory(m_testDir
->url());
1634 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1635 QCOMPARE(itemsInModel(), QStringList() << "a2");
1638 m_model
->setExpanded(0, true);
1639 QVERIFY(m_model
->isExpanded(0));
1640 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1641 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1644 m_model
->setExpanded(1, true);
1645 QVERIFY(m_model
->isExpanded(1));
1646 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1647 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1649 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1650 // signal is not emitted yet.
1651 const KFileItem fileItemC1
= m_model
->fileItem(2);
1652 KFileItem fileItemC2
= fileItemC1
;
1653 KUrl urlC2
= fileItemC2
.url();
1654 urlC2
.setFileName("c2.txt");
1655 fileItemC2
.setUrl(urlC2
);
1657 const KUrl urlB
= m_model
->fileItem(1).url();
1658 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1659 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1661 // Collapse "a2/". This should also remove all its (indirect) children from
1662 // the model and from the model's m_pendingItemsToInsert member.
1663 m_model
->setExpanded(0, false);
1664 QCOMPARE(itemsInModel(), QStringList() << "a2");
1666 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1667 // is still in m_pendingItemsToInsert, then we might get a crash, see
1668 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1669 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1670 // without parent in the model.
1671 m_model
->slotCompleted();
1672 QCOMPARE(itemsInModel(), QStringList() << "a2");
1674 // Expand "a2/" again.
1675 m_model
->setExpanded(0, true);
1676 QVERIFY(m_model
->isExpanded(0));
1677 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1678 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1680 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1681 // completed() signal is not emitted yet.
1682 const KFileItem fileItemA2
= m_model
->fileItem(0);
1683 KFileItem fileItemA1
= fileItemA2
;
1684 KUrl urlA1
= fileItemA1
.url();
1685 urlA1
.setFileName("a1");
1686 fileItemA1
.setUrl(urlA1
);
1688 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1689 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1691 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1692 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1693 // confuse the code which collapses the folder.
1694 m_model
->setExpanded(0, false);
1695 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1696 QVERIFY(!m_model
->isExpanded(0));
1697 QVERIFY(!m_model
->isExpanded(1));
1700 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1703 files
<< "a.txt" << "b.txt" << "c.txt" << "d.txt";
1704 m_testDir
->createFiles(files
);
1706 m_model
->loadDirectory(m_testDir
->url());
1707 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1708 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1710 const KFileItem fileItemB
= m_model
->fileItem(1);
1712 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1714 list
<< fileItemB
<< fileItemB
;
1715 m_model
->slotItemsDeleted(list
);
1717 QVERIFY(m_model
->isConsistent());
1718 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1721 QStringList
KFileItemModelTest::itemsInModel() const
1724 for (int i
= 0; i
< m_model
->count(); i
++) {
1725 items
<< m_model
->fileItem(i
).text();
1730 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1732 #include "kfileitemmodeltest.moc"