1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include <qtest_kde.h>
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
30 void myMessageOutput(QtMsgType type
, const char* msg
)
38 fprintf(stderr
, "Critical: %s\n", msg
);
41 fprintf(stderr
, "Fatal: %s\n", msg
);
49 const int DefaultTimeout
= 5000;
52 Q_DECLARE_METATYPE(KItemRange
)
53 Q_DECLARE_METATYPE(KItemRangeList
)
54 Q_DECLARE_METATYPE(QList
<int>)
56 class KFileItemModelTest
: public QObject
64 void testDefaultRoles();
65 void testDefaultSortRole();
66 void testDefaultGroupedSorting();
68 void testRemoveItems();
69 void testDirLoadingCompleted();
71 void testSetDataWithModifiedSortRole_data();
72 void testSetDataWithModifiedSortRole();
73 void testChangeSortRole();
74 void testResortAfterChangingName();
75 void testModelConsistencyWhenInsertingItems();
76 void testItemRangeConsistencyWhenInsertingItems();
77 void testExpandItems();
78 void testExpandParentItems();
79 void testMakeExpandedItemHidden();
80 void testRemoveFilteredExpandedItems();
82 void testIndexForKeyboardSearch();
83 void testNameFilter();
85 void testRefreshExpandedItem();
86 void testRemoveHiddenItems();
87 void collapseParentOfHiddenItems();
88 void removeParentOfHiddenItems();
89 void testGeneralParentChildRelationships();
90 void testNameRoleGroups();
91 void testNameRoleGroupsWithExpandedItems();
92 void testInconsistentModel();
93 void testChangeRolesForFilteredItems();
94 void testChangeSortRoleWhileFiltering();
97 QStringList
itemsInModel() const;
100 KFileItemModel
* m_model
;
104 void KFileItemModelTest::init()
106 // The item-model tests result in a huge number of debugging
107 // output from kdelibs. Only show critical and fatal messages.
108 qInstallMsgHandler(myMessageOutput
);
110 qRegisterMetaType
<KItemRange
>("KItemRange");
111 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
112 qRegisterMetaType
<KFileItemList
>("KFileItemList");
114 m_testDir
= new TestDir();
115 m_model
= new KFileItemModel();
116 m_model
->m_dirLister
->setAutoUpdate(false);
118 // Reduce the timer interval to make the test run faster.
119 m_model
->m_resortAllItemsTimer
->setInterval(0);
122 void KFileItemModelTest::cleanup()
131 void KFileItemModelTest::testDefaultRoles()
133 const QSet
<QByteArray
> roles
= m_model
->roles();
134 QCOMPARE(roles
.count(), 3);
135 QVERIFY(roles
.contains("text"));
136 QVERIFY(roles
.contains("isDir"));
137 QVERIFY(roles
.contains("isLink"));
140 void KFileItemModelTest::testDefaultSortRole()
142 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
145 files
<< "c.txt" << "a.txt" << "b.txt";
147 m_testDir
->createFiles(files
);
149 m_model
->loadDirectory(m_testDir
->url());
150 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
152 QCOMPARE(m_model
->count(), 3);
153 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
154 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
155 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
158 void KFileItemModelTest::testDefaultGroupedSorting()
160 QCOMPARE(m_model
->groupedSorting(), false);
163 void KFileItemModelTest::testNewItems()
166 files
<< "a.txt" << "b.txt" << "c.txt";
167 m_testDir
->createFiles(files
);
169 m_model
->loadDirectory(m_testDir
->url());
170 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
172 QCOMPARE(m_model
->count(), 3);
174 QVERIFY(m_model
->isConsistent());
177 void KFileItemModelTest::testRemoveItems()
179 m_testDir
->createFile("a.txt");
180 m_testDir
->createFile("b.txt");
181 m_model
->loadDirectory(m_testDir
->url());
182 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
183 QCOMPARE(m_model
->count(), 2);
184 QVERIFY(m_model
->isConsistent());
186 m_testDir
->removeFile("a.txt");
187 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
188 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
189 QCOMPARE(m_model
->count(), 1);
190 QVERIFY(m_model
->isConsistent());
193 void KFileItemModelTest::testDirLoadingCompleted()
195 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
196 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
197 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
199 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
201 m_model
->loadDirectory(m_testDir
->url());
202 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
203 QCOMPARE(loadingCompletedSpy
.count(), 1);
204 QCOMPARE(itemsInsertedSpy
.count(), 1);
205 QCOMPARE(itemsRemovedSpy
.count(), 0);
206 QCOMPARE(m_model
->count(), 3);
208 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
209 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
210 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
211 QCOMPARE(loadingCompletedSpy
.count(), 2);
212 QCOMPARE(itemsInsertedSpy
.count(), 2);
213 QCOMPARE(itemsRemovedSpy
.count(), 0);
214 QCOMPARE(m_model
->count(), 5);
216 m_testDir
->removeFile("a.txt");
217 m_testDir
->createFile("f.txt");
218 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
219 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
220 QCOMPARE(loadingCompletedSpy
.count(), 3);
221 QCOMPARE(itemsInsertedSpy
.count(), 3);
222 QCOMPARE(itemsRemovedSpy
.count(), 1);
223 QCOMPARE(m_model
->count(), 5);
225 m_testDir
->removeFile("b.txt");
226 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
227 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
228 QCOMPARE(loadingCompletedSpy
.count(), 4);
229 QCOMPARE(itemsInsertedSpy
.count(), 3);
230 QCOMPARE(itemsRemovedSpy
.count(), 2);
231 QCOMPARE(m_model
->count(), 4);
233 QVERIFY(m_model
->isConsistent());
236 void KFileItemModelTest::testSetData()
238 m_testDir
->createFile("a.txt");
240 m_model
->loadDirectory(m_testDir
->url());
241 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
243 QHash
<QByteArray
, QVariant
> values
;
244 values
.insert("customRole1", "Test1");
245 values
.insert("customRole2", "Test2");
247 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
248 m_model
->setData(0, values
);
249 QCOMPARE(itemsChangedSpy
.count(), 1);
251 values
= m_model
->data(0);
252 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
253 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
254 QVERIFY(m_model
->isConsistent());
257 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
259 QTest::addColumn
<int>("changedIndex");
260 QTest::addColumn
<int>("changedRating");
261 QTest::addColumn
<bool>("expectMoveSignal");
262 QTest::addColumn
<int>("ratingIndex0");
263 QTest::addColumn
<int>("ratingIndex1");
264 QTest::addColumn
<int>("ratingIndex2");
267 // Index 0 = rating 2
268 // Index 1 = rating 4
269 // Index 2 = rating 6
271 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
272 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
273 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
275 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
276 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
277 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
280 void KFileItemModelTest::testSetDataWithModifiedSortRole()
282 QFETCH(int, changedIndex
);
283 QFETCH(int, changedRating
);
284 QFETCH(bool, expectMoveSignal
);
285 QFETCH(int, ratingIndex0
);
286 QFETCH(int, ratingIndex1
);
287 QFETCH(int, ratingIndex2
);
289 // Changing the value of a sort-role must result in
290 // a reordering of the items.
291 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
292 m_model
->setSortRole("rating");
293 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
296 files
<< "a.txt" << "b.txt" << "c.txt";
297 m_testDir
->createFiles(files
);
299 m_model
->loadDirectory(m_testDir
->url());
300 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
302 // Fill the "rating" role of each file:
307 QHash
<QByteArray
, QVariant
> ratingA
;
308 ratingA
.insert("rating", 2);
309 m_model
->setData(0, ratingA
);
311 QHash
<QByteArray
, QVariant
> ratingB
;
312 ratingB
.insert("rating", 4);
313 m_model
->setData(1, ratingB
);
315 QHash
<QByteArray
, QVariant
> ratingC
;
316 ratingC
.insert("rating", 6);
317 m_model
->setData(2, ratingC
);
319 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
320 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
321 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
323 // Now change the rating from a.txt. This usually results
324 // in reordering of the items.
325 QHash
<QByteArray
, QVariant
> rating
;
326 rating
.insert("rating", changedRating
);
327 m_model
->setData(changedIndex
, rating
);
329 if (expectMoveSignal
) {
330 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
333 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
334 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
335 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
336 QVERIFY(m_model
->isConsistent());
339 void KFileItemModelTest::testChangeSortRole()
341 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
344 files
<< "a.txt" << "b.jpg" << "c.txt";
345 m_testDir
->createFiles(files
);
347 m_model
->loadDirectory(m_testDir
->url());
348 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
349 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
351 // Simulate that KFileItemModelRolesUpdater determines the mime type.
352 // Resorting the files by 'type' will only work immediately if their
353 // mime types are known.
354 for (int index
= 0; index
< m_model
->count(); ++index
) {
355 m_model
->fileItem(index
).determineMimeType();
358 // Now: sort by type.
359 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
360 m_model
->setSortRole("type");
361 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
362 QVERIFY(!spyItemsMoved
.isEmpty());
364 // The actual order of the files might depend on the translation of the
365 // result of KFileItem::mimeComment() in the user's language.
366 QStringList version1
;
367 version1
<< "b.jpg" << "a.txt" << "c.txt";
369 QStringList version2
;
370 version2
<< "a.txt" << "c.txt" << "b.jpg";
372 const bool ok1
= (itemsInModel() == version1
);
373 const bool ok2
= (itemsInModel() == version2
);
378 void KFileItemModelTest::testResortAfterChangingName()
380 // We sort by size in a directory where all files have the same size.
381 // Therefore, the files are sorted by their names.
382 m_model
->setSortRole("size");
385 files
<< "a.txt" << "b.txt" << "c.txt";
386 m_testDir
->createFiles(files
);
388 m_model
->loadDirectory(m_testDir
->url());
389 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
390 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
392 // We rename a.txt to d.txt. Even though the size has not changed at all,
393 // the model must re-sort the items.
394 QHash
<QByteArray
, QVariant
> data
;
395 data
.insert("text", "d.txt");
396 m_model
->setData(0, data
);
398 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
399 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
401 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
402 const KFileItem fileItemD
= m_model
->fileItem(2);
403 KFileItem fileItemA
= fileItemD
;
404 KUrl urlA
= fileItemA
.url();
405 urlA
.setFileName("a.txt");
406 fileItemA
.setUrl(urlA
);
408 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
410 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
411 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
414 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
416 //QSKIP("Temporary disabled", SkipSingle);
418 // KFileItemModel prevents that inserting a punch of items sequentially
419 // results in an itemsInserted()-signal for each item. Instead internally
420 // a timeout is given that collects such operations and results in only
421 // one itemsInserted()-signal. However in this test we want to stress
422 // KFileItemModel to do a lot of insert operation and hence decrease
423 // the timeout to 1 millisecond.
424 m_testDir
->createFile("1");
425 m_model
->loadDirectory(m_testDir
->url());
426 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
427 QCOMPARE(m_model
->count(), 1);
429 // Insert 10 items for 20 times. After each insert operation the model consistency
431 QSet
<int> insertedItems
;
432 for (int i
= 0; i
< 20; ++i
) {
433 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
435 for (int j
= 0; j
< 10; ++j
) {
436 int itemName
= qrand();
437 while (insertedItems
.contains(itemName
)) {
440 insertedItems
.insert(itemName
);
442 m_testDir
->createFile(QString::number(itemName
));
445 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
446 if (spy
.count() == 0) {
447 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
450 QVERIFY(m_model
->isConsistent());
453 QCOMPARE(m_model
->count(), 201);
456 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
459 files
<< "B" << "E" << "G";
460 m_testDir
->createFiles(files
);
462 // Due to inserting the 3 items one item-range with index == 0 and
463 // count == 3 must be given
464 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
465 m_model
->loadDirectory(m_testDir
->url());
466 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
468 QCOMPARE(spy1
.count(), 1);
469 QList
<QVariant
> arguments
= spy1
.takeFirst();
470 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
471 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
473 // The indexes of the item-ranges must always be related to the model before
474 // the items have been inserted. Having:
477 // and inserting A, C, D, F the resulting model will be:
480 // and the item-ranges must be:
481 // index: 0, count: 1 for A
482 // index: 1, count: 2 for B, C
483 // index: 2, count: 1 for G
486 files
<< "A" << "C" << "D" << "F";
487 m_testDir
->createFiles(files
);
489 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
490 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
491 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
493 QCOMPARE(spy2
.count(), 1);
494 arguments
= spy2
.takeFirst();
495 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
496 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
499 void KFileItemModelTest::testExpandItems()
501 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
502 // Besides testing the basic item expansion functionality, the test makes sure that
503 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
504 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
505 // first three characters.
506 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
507 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
508 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
509 m_model
->setRoles(modelRoles
);
512 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
513 m_testDir
->createFiles(files
);
515 // Store the URLs of all folders in a set.
516 QSet
<KUrl
> allFolders
;
517 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
519 m_model
->loadDirectory(m_testDir
->url());
520 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
522 // So far, the model contains only "a/"
523 QCOMPARE(m_model
->count(), 1);
524 QVERIFY(m_model
->isExpandable(0));
525 QVERIFY(!m_model
->isExpanded(0));
526 QVERIFY(m_model
->expandedDirectories().empty());
528 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
530 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
531 m_model
->setExpanded(0, true);
532 QVERIFY(m_model
->isExpanded(0));
533 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
534 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
535 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
537 QCOMPARE(spyInserted
.count(), 1);
538 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
539 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
541 QVERIFY(m_model
->isExpandable(1));
542 QVERIFY(!m_model
->isExpanded(1));
543 QVERIFY(m_model
->isExpandable(2));
544 QVERIFY(!m_model
->isExpanded(2));
546 // Expand the folder "a/a/" -> "a/a/1" becomes visible
547 m_model
->setExpanded(1, true);
548 QVERIFY(m_model
->isExpanded(1));
549 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
550 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
551 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
553 QCOMPARE(spyInserted
.count(), 1);
554 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
555 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
557 QVERIFY(!m_model
->isExpandable(2));
558 QVERIFY(!m_model
->isExpanded(2));
560 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
561 m_model
->setExpanded(3, true);
562 QVERIFY(m_model
->isExpanded(3));
563 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
564 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
565 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
567 QCOMPARE(spyInserted
.count(), 1);
568 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
569 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
571 QVERIFY(!m_model
->isExpandable(4));
572 QVERIFY(!m_model
->isExpanded(4));
574 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
576 // Collapse the top-level folder -> all other items should disappear
577 m_model
->setExpanded(0, false);
578 QVERIFY(!m_model
->isExpanded(0));
579 QCOMPARE(m_model
->count(), 1);
580 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
582 QCOMPARE(spyRemoved
.count(), 1);
583 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
584 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
585 QVERIFY(m_model
->isConsistent());
587 // Clear the model, reload the folder and try to restore the expanded folders.
589 QCOMPARE(m_model
->count(), 0);
590 QVERIFY(m_model
->expandedDirectories().empty());
592 m_model
->loadDirectory(m_testDir
->url());
593 m_model
->restoreExpandedDirectories(allFolders
);
594 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
595 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
596 QVERIFY(m_model
->isExpanded(0));
597 QVERIFY(m_model
->isExpanded(1));
598 QVERIFY(!m_model
->isExpanded(2));
599 QVERIFY(m_model
->isExpanded(3));
600 QVERIFY(!m_model
->isExpanded(4));
601 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
602 QVERIFY(m_model
->isConsistent());
604 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
605 // This is how DolphinView restores the expanded folders when navigating in history.
606 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
607 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
608 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
609 m_model
->restoreExpandedDirectories(allFolders
);
610 m_model
->loadDirectory(m_testDir
->url());
611 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
612 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
613 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
615 // Remove all expanded items by changing the roles
617 m_model
->setRoles(originalModelRoles
);
618 QVERIFY(!m_model
->isExpanded(0));
619 QCOMPARE(m_model
->count(), 1);
620 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
622 QCOMPARE(spyRemoved
.count(), 1);
623 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
624 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
625 QVERIFY(m_model
->isConsistent());
628 void KFileItemModelTest::testExpandParentItems()
630 // Create a tree structure of folders:
638 QSet
<QByteArray
> modelRoles
= m_model
->roles();
639 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
640 m_model
->setRoles(modelRoles
);
643 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
644 m_testDir
->createFiles(files
);
646 m_model
->loadDirectory(m_testDir
->url());
647 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
649 // So far, the model contains only "a 1/" and "a2/".
650 QCOMPARE(m_model
->count(), 2);
651 QVERIFY(m_model
->expandedDirectories().empty());
653 // Expand the parents of "a2/b2/c2".
654 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
655 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
657 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
658 // It's important that only the parents of "a1/b1/c1" are expanded.
659 QCOMPARE(m_model
->count(), 4);
660 QVERIFY(!m_model
->isExpanded(0));
661 QVERIFY(m_model
->isExpanded(1));
662 QVERIFY(m_model
->isExpanded(2));
663 QVERIFY(!m_model
->isExpanded(3));
665 // Expand the parents of "a 1/b1".
666 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
667 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
669 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
670 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
671 QCOMPARE(m_model
->count(), 5);
672 QVERIFY(m_model
->isExpanded(0));
673 QVERIFY(!m_model
->isExpanded(1));
674 QVERIFY(m_model
->isExpanded(2));
675 QVERIFY(m_model
->isExpanded(3));
676 QVERIFY(!m_model
->isExpanded(4));
677 QVERIFY(m_model
->isConsistent());
680 m_model
->setExpanded(1, true);
681 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
682 QCOMPARE(m_model
->count(), 6);
683 QVERIFY(m_model
->isExpanded(0));
684 QVERIFY(m_model
->isExpanded(1));
685 QVERIFY(!m_model
->isExpanded(2));
686 QVERIFY(m_model
->isExpanded(3));
687 QVERIFY(m_model
->isExpanded(4));
688 QVERIFY(!m_model
->isExpanded(5));
689 QVERIFY(m_model
->isConsistent());
691 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
692 m_model
->setExpanded(1, false);
693 QCOMPARE(m_model
->count(), 5);
694 QVERIFY(m_model
->isExpanded(0));
695 QVERIFY(!m_model
->isExpanded(1));
696 QVERIFY(m_model
->isExpanded(2));
697 QVERIFY(m_model
->isExpanded(3));
698 QVERIFY(!m_model
->isExpanded(4));
699 QVERIFY(m_model
->isConsistent());
703 * Renaming an expanded folder by prepending its name with a dot makes it
704 * hidden. Verify that this does not cause an inconsistent model state and
705 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
707 void KFileItemModelTest::testMakeExpandedItemHidden()
709 QSet
<QByteArray
> modelRoles
= m_model
->roles();
710 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
711 m_model
->setRoles(modelRoles
);
714 m_testDir
->createFile("1a/2a/3a");
715 m_testDir
->createFile("1a/2a/3b");
716 m_testDir
->createFile("1a/2b");
717 m_testDir
->createFile("1b");
719 m_model
->loadDirectory(m_testDir
->url());
720 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
722 // So far, the model contains only "1a/" and "1b".
723 QCOMPARE(m_model
->count(), 2);
724 m_model
->setExpanded(0, true);
725 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
727 // Now "1a/2a" and "1a/2b" have appeared.
728 QCOMPARE(m_model
->count(), 4);
729 m_model
->setExpanded(1, true);
730 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
731 QCOMPARE(m_model
->count(), 6);
733 // Rename "1a/2" and make it hidden.
734 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
735 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
737 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
738 bool ok
= job
->exec();
740 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
742 // "1a/2" and its subfolders have disappeared now.
743 QVERIFY(m_model
->isConsistent());
744 QCOMPARE(m_model
->count(), 3);
746 m_model
->setExpanded(0, false);
747 QCOMPARE(m_model
->count(), 2);
751 void KFileItemModelTest::testRemoveFilteredExpandedItems()
753 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
754 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
755 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
756 m_model
->setRoles(modelRoles
);
759 files
<< "folder/child" << "file"; // missing folders are created automatically
760 m_testDir
->createFiles(files
);
762 m_model
->loadDirectory(m_testDir
->url());
763 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
765 // So far, the model contains only "folder/" and "file".
766 QCOMPARE(m_model
->count(), 2);
767 QVERIFY(m_model
->isExpandable(0));
768 QVERIFY(!m_model
->isExpandable(1));
769 QVERIFY(!m_model
->isExpanded(0));
770 QVERIFY(!m_model
->isExpanded(1));
771 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
773 // Expand "folder" -> "folder/child" becomes visible.
774 m_model
->setExpanded(0, true);
775 QVERIFY(m_model
->isExpanded(0));
776 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
777 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
779 // Add a name filter.
780 m_model
->setNameFilter("f");
781 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
783 m_model
->setNameFilter("fo");
784 QCOMPARE(itemsInModel(), QStringList() << "folder");
786 // Remove all expanded items by changing the roles
787 m_model
->setRoles(originalModelRoles
);
788 QVERIFY(!m_model
->isExpanded(0));
789 QCOMPARE(itemsInModel(), QStringList() << "folder");
791 // Remove the name filter and verify that "folder/child" does not reappear.
792 m_model
->setNameFilter(QString());
793 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
796 void KFileItemModelTest::testSorting()
798 // Create some files with different sizes and modification times to check the different sorting options
799 QDateTime now
= QDateTime::currentDateTime();
801 QSet
<QByteArray
> roles
;
802 roles
.insert("text");
803 roles
.insert("isExpanded");
804 roles
.insert("isExpandable");
805 roles
.insert("expandedParentsCount");
806 m_model
->setRoles(roles
);
808 m_testDir
->createDir("c/c-2");
809 m_testDir
->createFile("c/c-2/c-3");
810 m_testDir
->createFile("c/c-1");
812 m_testDir
->createFile("a", "A file", now
.addDays(-3));
813 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
814 m_testDir
->createDir("c", now
.addDays(-2));
815 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
816 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
817 m_testDir
->createFile(".f");
819 m_model
->loadDirectory(m_testDir
->url());
820 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
822 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
823 m_model
->setExpanded(index
, true);
824 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
826 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
827 m_model
->setExpanded(index
, true);
828 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
830 // Default: Sort by Name, ascending
831 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
832 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
833 QVERIFY(m_model
->sortDirectoriesFirst());
834 QVERIFY(!m_model
->showHiddenFiles());
835 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
837 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
839 // Sort by Name, ascending, 'Sort Folders First' disabled
840 m_model
->setSortDirectoriesFirst(false);
841 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
842 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
843 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
844 QCOMPARE(spyItemsMoved
.count(), 1);
845 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
846 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
848 // Sort by Name, descending
849 m_model
->setSortDirectoriesFirst(true);
850 m_model
->setSortOrder(Qt::DescendingOrder
);
851 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
852 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
853 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
854 QCOMPARE(spyItemsMoved
.count(), 2);
855 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
856 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
857 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
858 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
860 // Sort by Date, descending
861 m_model
->setSortDirectoriesFirst(true);
862 m_model
->setSortRole("date");
863 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
864 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
865 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
866 QCOMPARE(spyItemsMoved
.count(), 1);
867 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
868 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
870 // Sort by Date, ascending
871 m_model
->setSortOrder(Qt::AscendingOrder
);
872 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
873 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
874 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
875 QCOMPARE(spyItemsMoved
.count(), 1);
876 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
877 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
879 // Sort by Date, ascending, 'Sort Folders First' disabled
880 m_model
->setSortDirectoriesFirst(false);
881 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
882 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
883 QVERIFY(!m_model
->sortDirectoriesFirst());
884 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
885 QCOMPARE(spyItemsMoved
.count(), 1);
886 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
887 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
889 // Sort by Name, ascending, 'Sort Folders First' disabled
890 m_model
->setSortRole("text");
891 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
892 QVERIFY(!m_model
->sortDirectoriesFirst());
893 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
894 QCOMPARE(spyItemsMoved
.count(), 1);
895 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
896 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
898 // Sort by Size, ascending, 'Sort Folders First' disabled
899 m_model
->setSortRole("size");
900 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
901 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
902 QVERIFY(!m_model
->sortDirectoriesFirst());
903 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
904 QCOMPARE(spyItemsMoved
.count(), 1);
905 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
906 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
908 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
909 m_model
->setSortDirectoriesFirst(true);
910 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
911 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
912 QVERIFY(m_model
->sortDirectoriesFirst());
913 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
914 QCOMPARE(spyItemsMoved
.count(), 0);
916 // Sort by Size, descending, 'Sort Folders First' enabled
917 m_model
->setSortOrder(Qt::DescendingOrder
);
918 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
919 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
920 QVERIFY(m_model
->sortDirectoriesFirst());
921 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
922 QCOMPARE(spyItemsMoved
.count(), 1);
923 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
924 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
926 // TODO: Sort by other roles; show/hide hidden files
929 void KFileItemModelTest::testIndexForKeyboardSearch()
932 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
933 m_testDir
->createFiles(files
);
935 m_model
->loadDirectory(m_testDir
->url());
936 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
938 // Search from index 0
939 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
940 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
941 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
942 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
943 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
944 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
945 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
946 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
947 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
948 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
949 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
951 // Start a search somewhere in the middle
952 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
953 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
954 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
955 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
957 // Test searches that go past the last item back to index 0
958 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
959 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
960 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
961 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
963 // Test searches that yield no result
964 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
965 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
966 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
967 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
968 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
970 // Test upper case searches (note that search is case insensitive)
971 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
972 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
973 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
974 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
976 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
979 void KFileItemModelTest::testNameFilter()
982 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
983 m_testDir
->createFiles(files
);
985 m_model
->loadDirectory(m_testDir
->url());
986 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
988 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
989 QCOMPARE(m_model
->count(), 3);
991 m_model
->setNameFilter("A2"); // Shows only A2
992 QCOMPARE(m_model
->count(), 1);
994 m_model
->setNameFilter("A2"); // Shows only A1
995 QCOMPARE(m_model
->count(), 1);
997 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
998 QCOMPARE(m_model
->count(), 2);
1000 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1001 QCOMPARE(m_model
->count(), 2);
1003 m_model
->setNameFilter(QString()); // Shows again all items
1004 QCOMPARE(m_model
->count(), 5);
1008 * Verifies that we do not crash when adding a KFileItem with an empty path.
1009 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1010 * tried to always read the first character of the path, even if the path is empty.
1012 void KFileItemModelTest::testEmptyPath()
1014 QSet
<QByteArray
> roles
;
1015 roles
.insert("text");
1016 roles
.insert("isExpanded");
1017 roles
.insert("isExpandable");
1018 roles
.insert("expandedParentsCount");
1019 m_model
->setRoles(roles
);
1021 const KUrl emptyUrl
;
1022 QVERIFY(emptyUrl
.path().isEmpty());
1024 const KUrl
url("file:///test/");
1026 KFileItemList items
;
1027 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1028 m_model
->slotItemsAdded(emptyUrl
, items
);
1029 m_model
->slotCompleted();
1033 * Verifies that the 'isExpanded' state of folders does not change when the
1034 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1036 void KFileItemModelTest::testRefreshExpandedItem()
1038 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1039 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1040 m_model
->setRoles(modelRoles
);
1043 files
<< "a/1" << "a/2" << "3" << "4";
1044 m_testDir
->createFiles(files
);
1046 m_model
->loadDirectory(m_testDir
->url());
1047 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1048 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1050 m_model
->setExpanded(0, true);
1051 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1052 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1053 QVERIFY(m_model
->isExpanded(0));
1055 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1057 const KFileItem item
= m_model
->fileItem(0);
1058 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1059 QVERIFY(!spyItemsChanged
.isEmpty());
1061 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1062 QVERIFY(m_model
->isExpanded(0));
1066 * Verify that removing hidden files and folders from the model does not
1067 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1069 void KFileItemModelTest::testRemoveHiddenItems()
1071 m_testDir
->createDir(".a");
1072 m_testDir
->createDir(".b");
1073 m_testDir
->createDir("c");
1074 m_testDir
->createDir("d");
1075 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1077 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1078 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1080 m_model
->setShowHiddenFiles(true);
1081 m_model
->loadDirectory(m_testDir
->url());
1082 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1083 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1084 QCOMPARE(spyItemsInserted
.count(), 1);
1085 QCOMPARE(spyItemsRemoved
.count(), 0);
1086 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1087 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1089 m_model
->setShowHiddenFiles(false);
1090 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1091 QCOMPARE(spyItemsInserted
.count(), 0);
1092 QCOMPARE(spyItemsRemoved
.count(), 1);
1093 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1094 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1096 m_model
->setShowHiddenFiles(true);
1097 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1098 QCOMPARE(spyItemsInserted
.count(), 1);
1099 QCOMPARE(spyItemsRemoved
.count(), 0);
1100 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1101 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1104 QCOMPARE(itemsInModel(), QStringList());
1105 QCOMPARE(spyItemsInserted
.count(), 0);
1106 QCOMPARE(spyItemsRemoved
.count(), 1);
1107 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1108 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1110 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1111 // Verify that this does not make the model crash.
1112 m_model
->setShowHiddenFiles(false);
1116 * Verify that filtered items are removed when their parent is collapsed.
1118 void KFileItemModelTest::collapseParentOfHiddenItems()
1120 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1121 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1122 m_model
->setRoles(modelRoles
);
1125 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1126 m_testDir
->createFiles(files
);
1128 m_model
->loadDirectory(m_testDir
->url());
1129 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1130 QCOMPARE(m_model
->count(), 1); // Only "a/"
1133 m_model
->setExpanded(0, true);
1134 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1135 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1138 m_model
->setExpanded(1, true);
1139 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1140 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1143 m_model
->setExpanded(2, true);
1144 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1145 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"
1147 // Set a name filter that matches nothing -> only the expanded folders remain.
1148 m_model
->setNameFilter("xyz");
1149 QCOMPARE(m_model
->count(), 3);
1150 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1152 // Collapse the folder "a/".
1153 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1154 m_model
->setExpanded(0, false);
1155 QCOMPARE(spyItemsRemoved
.count(), 1);
1156 QCOMPARE(m_model
->count(), 1);
1157 QCOMPARE(itemsInModel(), QStringList() << "a");
1159 // Remove the filter -> no files should appear (and we should not get a crash).
1160 m_model
->setNameFilter(QString());
1161 QCOMPARE(m_model
->count(), 1);
1165 * Verify that filtered items are removed when their parent is deleted.
1167 void KFileItemModelTest::removeParentOfHiddenItems()
1169 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1170 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1171 m_model
->setRoles(modelRoles
);
1174 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1175 m_testDir
->createFiles(files
);
1177 m_model
->loadDirectory(m_testDir
->url());
1178 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1179 QCOMPARE(m_model
->count(), 1); // Only "a/"
1182 m_model
->setExpanded(0, true);
1183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1184 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1187 m_model
->setExpanded(1, true);
1188 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1189 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1192 m_model
->setExpanded(2, true);
1193 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1194 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"
1196 // Set a name filter that matches nothing -> only the expanded folders remain.
1197 m_model
->setNameFilter("xyz");
1198 QCOMPARE(m_model
->count(), 3);
1199 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1201 // Simulate the deletion of the directory "a/b/".
1202 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1203 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1204 QCOMPARE(spyItemsRemoved
.count(), 1);
1205 QCOMPARE(m_model
->count(), 1);
1206 QCOMPARE(itemsInModel(), QStringList() << "a");
1208 // Remove the filter -> only the file "a/1" should appear.
1209 m_model
->setNameFilter(QString());
1210 QCOMPARE(m_model
->count(), 2);
1211 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1215 * Create a tree structure where parent-child relationships can not be
1216 * determined by parsing the URLs, and verify that KFileItemModel
1217 * handles them correctly.
1219 void KFileItemModelTest::testGeneralParentChildRelationships()
1221 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1222 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1223 m_model
->setRoles(modelRoles
);
1226 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1227 m_testDir
->createFiles(files
);
1229 m_model
->loadDirectory(m_testDir
->url());
1230 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1231 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1233 // Expand all folders.
1234 m_model
->setExpanded(0, true);
1235 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1236 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1238 m_model
->setExpanded(1, true);
1239 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1240 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1242 m_model
->setExpanded(3, true);
1243 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1244 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1246 m_model
->setExpanded(4, true);
1247 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1248 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1250 // Add some more children and grand-children.
1251 const KUrl parent1
= m_model
->fileItem(0).url();
1252 const KUrl parent2
= m_model
->fileItem(3).url();
1253 const KUrl realChild1
= m_model
->fileItem(1).url();
1254 const KUrl realChild2
= m_model
->fileItem(4).url();
1256 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1257 m_model
->slotCompleted();
1258 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1260 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1261 m_model
->slotCompleted();
1262 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1264 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1265 m_model
->slotCompleted();
1266 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1268 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1269 m_model
->slotCompleted();
1270 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1272 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1273 m_model
->slotCompleted();
1274 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1276 // Set a name filter that matches nothing -> only expanded folders remain.
1277 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1278 m_model
->setNameFilter("xyz");
1279 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1280 QCOMPARE(itemsRemovedSpy
.count(), 1);
1281 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1282 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1283 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1285 // Collapse "parent1".
1286 m_model
->setExpanded(0, false);
1287 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1288 QCOMPARE(itemsRemovedSpy
.count(), 1);
1289 arguments
= itemsRemovedSpy
.takeFirst();
1290 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1291 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1293 // Remove "parent2".
1294 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1295 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1296 QCOMPARE(itemsRemovedSpy
.count(), 1);
1297 arguments
= itemsRemovedSpy
.takeFirst();
1298 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1299 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1301 // Clear filter, verify that no items reappear.
1302 m_model
->setNameFilter(QString());
1303 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1306 void KFileItemModelTest::testNameRoleGroups()
1309 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1311 m_testDir
->createFiles(files
);
1313 m_model
->setGroupedSorting(true);
1314 m_model
->loadDirectory(m_testDir
->url());
1315 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1316 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1318 QList
<QPair
<int, QVariant
> > expectedGroups
;
1319 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1320 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1321 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1322 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1323 QCOMPARE(m_model
->groups(), expectedGroups
);
1325 // Rename d.txt to a.txt.
1326 QHash
<QByteArray
, QVariant
> data
;
1327 data
.insert("text", "a.txt");
1328 m_model
->setData(2, data
);
1329 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1330 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1332 expectedGroups
.clear();
1333 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1334 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1335 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1336 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1337 QCOMPARE(m_model
->groups(), expectedGroups
);
1339 // Rename c.txt to d.txt.
1340 data
.insert("text", "d.txt");
1341 m_model
->setData(2, data
);
1342 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1343 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1345 expectedGroups
.clear();
1346 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1347 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1348 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1349 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1350 QCOMPARE(m_model
->groups(), expectedGroups
);
1352 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1353 const KFileItem fileItemD
= m_model
->fileItem(2);
1354 KFileItem fileItemC
= fileItemD
;
1355 KUrl urlC
= fileItemC
.url();
1356 urlC
.setFileName("c.txt");
1357 fileItemC
.setUrl(urlC
);
1359 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1360 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1361 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1363 expectedGroups
.clear();
1364 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1365 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1366 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1367 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1368 QCOMPARE(m_model
->groups(), expectedGroups
);
1371 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1373 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1374 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1375 m_model
->setRoles(modelRoles
);
1378 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1380 m_testDir
->createFiles(files
);
1382 m_model
->setGroupedSorting(true);
1383 m_model
->loadDirectory(m_testDir
->url());
1384 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1385 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1387 QList
<QPair
<int, QVariant
> > expectedGroups
;
1388 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1389 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1390 QCOMPARE(m_model
->groups(), expectedGroups
);
1392 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1393 expectedGroups
.clear();
1394 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1395 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1397 m_model
->setExpanded(0, true);
1398 QVERIFY(m_model
->isExpanded(0));
1399 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1400 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1401 QCOMPARE(m_model
->groups(), expectedGroups
);
1403 m_model
->setExpanded(3, true);
1404 QVERIFY(m_model
->isExpanded(3));
1405 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1406 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1407 QCOMPARE(m_model
->groups(), expectedGroups
);
1410 void KFileItemModelTest::testInconsistentModel()
1412 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1413 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1414 m_model
->setRoles(modelRoles
);
1417 files
<< "a/b/c1.txt" << "a/b/c2.txt";
1419 m_testDir
->createFiles(files
);
1421 m_model
->loadDirectory(m_testDir
->url());
1422 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1423 QCOMPARE(itemsInModel(), QStringList() << "a");
1425 // Expand "a/" and "a/b/".
1426 m_model
->setExpanded(0, true);
1427 QVERIFY(m_model
->isExpanded(0));
1428 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1429 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1431 m_model
->setExpanded(1, true);
1432 QVERIFY(m_model
->isExpanded(1));
1433 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1434 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1436 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1437 // Such a thing can in principle happen when performing a search, and there
1439 // (a) match the search string, and
1440 // (b) are children of a folder that matches the search string and is expanded.
1442 // Note that the first item in the list of added items must be new (i.e., not
1443 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1444 // it receives items that are in the model already and ignore them.
1445 KUrl
url(m_model
->directory().url() + "/a2");
1446 KFileItem
newItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
1448 KFileItemList items
;
1449 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1450 m_model
->slotItemsAdded(m_model
->directory(), items
);
1451 m_model
->slotCompleted();
1452 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1454 m_model
->setExpanded(0, false);
1456 // Test that the right items have been removed, see
1457 // https://bugs.kde.org/show_bug.cgi?id=324371
1458 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1460 // Test that resorting does not cause a crash, see
1461 // https://bugs.kde.org/show_bug.cgi?id=325359
1462 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1463 m_model
->resortAllItems();
1467 void KFileItemModelTest::testChangeRolesForFilteredItems()
1469 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1470 modelRoles
<< "owner";
1471 m_model
->setRoles(modelRoles
);
1474 files
<< "a.txt" << "aa.txt" << "aaa.txt";
1475 m_testDir
->createFiles(files
);
1477 m_model
->loadDirectory(m_testDir
->url());
1478 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1479 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1481 for (int index
= 0; index
< m_model
->count(); ++index
) {
1482 // All items should have the "text" and "owner" roles, but not "group".
1483 QVERIFY(m_model
->data(index
).contains("text"));
1484 QVERIFY(m_model
->data(index
).contains("owner"));
1485 QVERIFY(!m_model
->data(index
).contains("group"));
1488 // Add a filter, such that only "aaa.txt" remains in the model.
1489 m_model
->setNameFilter("aaa");
1490 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1492 // Add the "group" role.
1493 modelRoles
<< "group";
1494 m_model
->setRoles(modelRoles
);
1496 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1497 m_model
->setNameFilter("aa");
1498 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1500 for (int index
= 0; index
< m_model
->count(); ++index
) {
1501 // All items should have the "text", "owner", and "group" roles.
1502 QVERIFY(m_model
->data(index
).contains("text"));
1503 QVERIFY(m_model
->data(index
).contains("owner"));
1504 QVERIFY(m_model
->data(index
).contains("group"));
1507 // Remove the "owner" role.
1508 modelRoles
.remove("owner");
1509 m_model
->setRoles(modelRoles
);
1511 // Clear the filter, and verify that all items have the expected roles
1512 m_model
->setNameFilter(QString());
1513 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1515 for (int index
= 0; index
< m_model
->count(); ++index
) {
1516 // All items should have the "text" and "group" roles, but now "owner".
1517 QVERIFY(m_model
->data(index
).contains("text"));
1518 QVERIFY(!m_model
->data(index
).contains("owner"));
1519 QVERIFY(m_model
->data(index
).contains("group"));
1523 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1525 KFileItemList items
;
1527 KIO::UDSEntry entry
;
1528 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1529 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1530 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1531 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1532 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1533 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1535 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1536 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1537 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1539 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1540 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1541 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1543 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1544 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1545 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1547 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1548 m_model
->slotCompleted();
1550 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1553 m_model
->setNameFilter("a");
1554 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1557 m_model
->setSortRole("owner");
1559 // Clear the filter, and verify that the items are sorted correctly.
1560 m_model
->setNameFilter(QString());
1561 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1564 QStringList
KFileItemModelTest::itemsInModel() const
1567 for (int i
= 0; i
< m_model
->count(); i
++) {
1568 items
<< m_model
->fileItem(i
).text();
1573 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1575 #include "kfileitemmodeltest.moc"