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();
95 void testRefreshFilteredItems();
96 void testCreateMimeData();
99 QStringList
itemsInModel() const;
102 KFileItemModel
* m_model
;
106 void KFileItemModelTest::init()
108 // The item-model tests result in a huge number of debugging
109 // output from kdelibs. Only show critical and fatal messages.
110 qInstallMsgHandler(myMessageOutput
);
112 qRegisterMetaType
<KItemRange
>("KItemRange");
113 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
114 qRegisterMetaType
<KFileItemList
>("KFileItemList");
116 m_testDir
= new TestDir();
117 m_model
= new KFileItemModel();
118 m_model
->m_dirLister
->setAutoUpdate(false);
120 // Reduce the timer interval to make the test run faster.
121 m_model
->m_resortAllItemsTimer
->setInterval(0);
124 void KFileItemModelTest::cleanup()
133 void KFileItemModelTest::testDefaultRoles()
135 const QSet
<QByteArray
> roles
= m_model
->roles();
136 QCOMPARE(roles
.count(), 3);
137 QVERIFY(roles
.contains("text"));
138 QVERIFY(roles
.contains("isDir"));
139 QVERIFY(roles
.contains("isLink"));
142 void KFileItemModelTest::testDefaultSortRole()
144 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
147 files
<< "c.txt" << "a.txt" << "b.txt";
149 m_testDir
->createFiles(files
);
151 m_model
->loadDirectory(m_testDir
->url());
152 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
154 QCOMPARE(m_model
->count(), 3);
155 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
156 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
157 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
160 void KFileItemModelTest::testDefaultGroupedSorting()
162 QCOMPARE(m_model
->groupedSorting(), false);
165 void KFileItemModelTest::testNewItems()
168 files
<< "a.txt" << "b.txt" << "c.txt";
169 m_testDir
->createFiles(files
);
171 m_model
->loadDirectory(m_testDir
->url());
172 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
174 QCOMPARE(m_model
->count(), 3);
176 QVERIFY(m_model
->isConsistent());
179 void KFileItemModelTest::testRemoveItems()
181 m_testDir
->createFile("a.txt");
182 m_testDir
->createFile("b.txt");
183 m_model
->loadDirectory(m_testDir
->url());
184 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
185 QCOMPARE(m_model
->count(), 2);
186 QVERIFY(m_model
->isConsistent());
188 m_testDir
->removeFile("a.txt");
189 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
190 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
191 QCOMPARE(m_model
->count(), 1);
192 QVERIFY(m_model
->isConsistent());
195 void KFileItemModelTest::testDirLoadingCompleted()
197 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
198 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
199 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
201 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
203 m_model
->loadDirectory(m_testDir
->url());
204 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
205 QCOMPARE(loadingCompletedSpy
.count(), 1);
206 QCOMPARE(itemsInsertedSpy
.count(), 1);
207 QCOMPARE(itemsRemovedSpy
.count(), 0);
208 QCOMPARE(m_model
->count(), 3);
210 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
211 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
212 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
213 QCOMPARE(loadingCompletedSpy
.count(), 2);
214 QCOMPARE(itemsInsertedSpy
.count(), 2);
215 QCOMPARE(itemsRemovedSpy
.count(), 0);
216 QCOMPARE(m_model
->count(), 5);
218 m_testDir
->removeFile("a.txt");
219 m_testDir
->createFile("f.txt");
220 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
221 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
222 QCOMPARE(loadingCompletedSpy
.count(), 3);
223 QCOMPARE(itemsInsertedSpy
.count(), 3);
224 QCOMPARE(itemsRemovedSpy
.count(), 1);
225 QCOMPARE(m_model
->count(), 5);
227 m_testDir
->removeFile("b.txt");
228 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
229 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
230 QCOMPARE(loadingCompletedSpy
.count(), 4);
231 QCOMPARE(itemsInsertedSpy
.count(), 3);
232 QCOMPARE(itemsRemovedSpy
.count(), 2);
233 QCOMPARE(m_model
->count(), 4);
235 QVERIFY(m_model
->isConsistent());
238 void KFileItemModelTest::testSetData()
240 m_testDir
->createFile("a.txt");
242 m_model
->loadDirectory(m_testDir
->url());
243 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
245 QHash
<QByteArray
, QVariant
> values
;
246 values
.insert("customRole1", "Test1");
247 values
.insert("customRole2", "Test2");
249 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
250 m_model
->setData(0, values
);
251 QCOMPARE(itemsChangedSpy
.count(), 1);
253 values
= m_model
->data(0);
254 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
255 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
256 QVERIFY(m_model
->isConsistent());
259 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
261 QTest::addColumn
<int>("changedIndex");
262 QTest::addColumn
<int>("changedRating");
263 QTest::addColumn
<bool>("expectMoveSignal");
264 QTest::addColumn
<int>("ratingIndex0");
265 QTest::addColumn
<int>("ratingIndex1");
266 QTest::addColumn
<int>("ratingIndex2");
269 // Index 0 = rating 2
270 // Index 1 = rating 4
271 // Index 2 = rating 6
273 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
274 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
275 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
277 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
278 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
279 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
282 void KFileItemModelTest::testSetDataWithModifiedSortRole()
284 QFETCH(int, changedIndex
);
285 QFETCH(int, changedRating
);
286 QFETCH(bool, expectMoveSignal
);
287 QFETCH(int, ratingIndex0
);
288 QFETCH(int, ratingIndex1
);
289 QFETCH(int, ratingIndex2
);
291 // Changing the value of a sort-role must result in
292 // a reordering of the items.
293 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
294 m_model
->setSortRole("rating");
295 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
298 files
<< "a.txt" << "b.txt" << "c.txt";
299 m_testDir
->createFiles(files
);
301 m_model
->loadDirectory(m_testDir
->url());
302 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
304 // Fill the "rating" role of each file:
309 QHash
<QByteArray
, QVariant
> ratingA
;
310 ratingA
.insert("rating", 2);
311 m_model
->setData(0, ratingA
);
313 QHash
<QByteArray
, QVariant
> ratingB
;
314 ratingB
.insert("rating", 4);
315 m_model
->setData(1, ratingB
);
317 QHash
<QByteArray
, QVariant
> ratingC
;
318 ratingC
.insert("rating", 6);
319 m_model
->setData(2, ratingC
);
321 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
322 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
323 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
325 // Now change the rating from a.txt. This usually results
326 // in reordering of the items.
327 QHash
<QByteArray
, QVariant
> rating
;
328 rating
.insert("rating", changedRating
);
329 m_model
->setData(changedIndex
, rating
);
331 if (expectMoveSignal
) {
332 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
335 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
336 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
337 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
338 QVERIFY(m_model
->isConsistent());
341 void KFileItemModelTest::testChangeSortRole()
343 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
346 files
<< "a.txt" << "b.jpg" << "c.txt";
347 m_testDir
->createFiles(files
);
349 m_model
->loadDirectory(m_testDir
->url());
350 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
351 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
353 // Simulate that KFileItemModelRolesUpdater determines the mime type.
354 // Resorting the files by 'type' will only work immediately if their
355 // mime types are known.
356 for (int index
= 0; index
< m_model
->count(); ++index
) {
357 m_model
->fileItem(index
).determineMimeType();
360 // Now: sort by type.
361 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
362 m_model
->setSortRole("type");
363 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
364 QVERIFY(!spyItemsMoved
.isEmpty());
366 // The actual order of the files might depend on the translation of the
367 // result of KFileItem::mimeComment() in the user's language.
368 QStringList version1
;
369 version1
<< "b.jpg" << "a.txt" << "c.txt";
371 QStringList version2
;
372 version2
<< "a.txt" << "c.txt" << "b.jpg";
374 const bool ok1
= (itemsInModel() == version1
);
375 const bool ok2
= (itemsInModel() == version2
);
380 void KFileItemModelTest::testResortAfterChangingName()
382 // We sort by size in a directory where all files have the same size.
383 // Therefore, the files are sorted by their names.
384 m_model
->setSortRole("size");
387 files
<< "a.txt" << "b.txt" << "c.txt";
388 m_testDir
->createFiles(files
);
390 m_model
->loadDirectory(m_testDir
->url());
391 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
392 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
394 // We rename a.txt to d.txt. Even though the size has not changed at all,
395 // the model must re-sort the items.
396 QHash
<QByteArray
, QVariant
> data
;
397 data
.insert("text", "d.txt");
398 m_model
->setData(0, data
);
400 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
401 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
403 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
404 const KFileItem fileItemD
= m_model
->fileItem(2);
405 KFileItem fileItemA
= fileItemD
;
406 KUrl urlA
= fileItemA
.url();
407 urlA
.setFileName("a.txt");
408 fileItemA
.setUrl(urlA
);
410 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
412 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
413 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
416 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
418 //QSKIP("Temporary disabled", SkipSingle);
420 // KFileItemModel prevents that inserting a punch of items sequentially
421 // results in an itemsInserted()-signal for each item. Instead internally
422 // a timeout is given that collects such operations and results in only
423 // one itemsInserted()-signal. However in this test we want to stress
424 // KFileItemModel to do a lot of insert operation and hence decrease
425 // the timeout to 1 millisecond.
426 m_testDir
->createFile("1");
427 m_model
->loadDirectory(m_testDir
->url());
428 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
429 QCOMPARE(m_model
->count(), 1);
431 // Insert 10 items for 20 times. After each insert operation the model consistency
433 QSet
<int> insertedItems
;
434 for (int i
= 0; i
< 20; ++i
) {
435 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
437 for (int j
= 0; j
< 10; ++j
) {
438 int itemName
= qrand();
439 while (insertedItems
.contains(itemName
)) {
442 insertedItems
.insert(itemName
);
444 m_testDir
->createFile(QString::number(itemName
));
447 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
448 if (spy
.count() == 0) {
449 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
452 QVERIFY(m_model
->isConsistent());
455 QCOMPARE(m_model
->count(), 201);
458 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
461 files
<< "B" << "E" << "G";
462 m_testDir
->createFiles(files
);
464 // Due to inserting the 3 items one item-range with index == 0 and
465 // count == 3 must be given
466 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
467 m_model
->loadDirectory(m_testDir
->url());
468 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
470 QCOMPARE(spy1
.count(), 1);
471 QList
<QVariant
> arguments
= spy1
.takeFirst();
472 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
473 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
475 // The indexes of the item-ranges must always be related to the model before
476 // the items have been inserted. Having:
479 // and inserting A, C, D, F the resulting model will be:
482 // and the item-ranges must be:
483 // index: 0, count: 1 for A
484 // index: 1, count: 2 for B, C
485 // index: 2, count: 1 for G
488 files
<< "A" << "C" << "D" << "F";
489 m_testDir
->createFiles(files
);
491 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
492 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
493 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
495 QCOMPARE(spy2
.count(), 1);
496 arguments
= spy2
.takeFirst();
497 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
498 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
501 void KFileItemModelTest::testExpandItems()
503 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
504 // Besides testing the basic item expansion functionality, the test makes sure that
505 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
506 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
507 // first three characters.
508 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
509 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
510 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
511 m_model
->setRoles(modelRoles
);
514 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
515 m_testDir
->createFiles(files
);
517 // Store the URLs of all folders in a set.
518 QSet
<KUrl
> allFolders
;
519 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
521 m_model
->loadDirectory(m_testDir
->url());
522 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
524 // So far, the model contains only "a/"
525 QCOMPARE(m_model
->count(), 1);
526 QVERIFY(m_model
->isExpandable(0));
527 QVERIFY(!m_model
->isExpanded(0));
528 QVERIFY(m_model
->expandedDirectories().empty());
530 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
532 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
533 m_model
->setExpanded(0, true);
534 QVERIFY(m_model
->isExpanded(0));
535 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
536 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
537 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
539 QCOMPARE(spyInserted
.count(), 1);
540 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
541 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
543 QVERIFY(m_model
->isExpandable(1));
544 QVERIFY(!m_model
->isExpanded(1));
545 QVERIFY(m_model
->isExpandable(2));
546 QVERIFY(!m_model
->isExpanded(2));
548 // Expand the folder "a/a/" -> "a/a/1" becomes visible
549 m_model
->setExpanded(1, true);
550 QVERIFY(m_model
->isExpanded(1));
551 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
552 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
553 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
555 QCOMPARE(spyInserted
.count(), 1);
556 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
557 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
559 QVERIFY(!m_model
->isExpandable(2));
560 QVERIFY(!m_model
->isExpanded(2));
562 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
563 m_model
->setExpanded(3, true);
564 QVERIFY(m_model
->isExpanded(3));
565 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
566 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
567 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
569 QCOMPARE(spyInserted
.count(), 1);
570 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
571 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
573 QVERIFY(!m_model
->isExpandable(4));
574 QVERIFY(!m_model
->isExpanded(4));
576 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
578 // Collapse the top-level folder -> all other items should disappear
579 m_model
->setExpanded(0, false);
580 QVERIFY(!m_model
->isExpanded(0));
581 QCOMPARE(m_model
->count(), 1);
582 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
584 QCOMPARE(spyRemoved
.count(), 1);
585 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
586 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
587 QVERIFY(m_model
->isConsistent());
589 // Clear the model, reload the folder and try to restore the expanded folders.
591 QCOMPARE(m_model
->count(), 0);
592 QVERIFY(m_model
->expandedDirectories().empty());
594 m_model
->loadDirectory(m_testDir
->url());
595 m_model
->restoreExpandedDirectories(allFolders
);
596 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
597 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
598 QVERIFY(m_model
->isExpanded(0));
599 QVERIFY(m_model
->isExpanded(1));
600 QVERIFY(!m_model
->isExpanded(2));
601 QVERIFY(m_model
->isExpanded(3));
602 QVERIFY(!m_model
->isExpanded(4));
603 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
604 QVERIFY(m_model
->isConsistent());
606 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
607 // This is how DolphinView restores the expanded folders when navigating in history.
608 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
609 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
610 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
611 m_model
->restoreExpandedDirectories(allFolders
);
612 m_model
->loadDirectory(m_testDir
->url());
613 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
614 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
615 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
617 // Remove all expanded items by changing the roles
619 m_model
->setRoles(originalModelRoles
);
620 QVERIFY(!m_model
->isExpanded(0));
621 QCOMPARE(m_model
->count(), 1);
622 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
624 QCOMPARE(spyRemoved
.count(), 1);
625 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
626 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
627 QVERIFY(m_model
->isConsistent());
630 void KFileItemModelTest::testExpandParentItems()
632 // Create a tree structure of folders:
640 QSet
<QByteArray
> modelRoles
= m_model
->roles();
641 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
642 m_model
->setRoles(modelRoles
);
645 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
646 m_testDir
->createFiles(files
);
648 m_model
->loadDirectory(m_testDir
->url());
649 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
651 // So far, the model contains only "a 1/" and "a2/".
652 QCOMPARE(m_model
->count(), 2);
653 QVERIFY(m_model
->expandedDirectories().empty());
655 // Expand the parents of "a2/b2/c2".
656 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
657 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
659 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
660 // It's important that only the parents of "a1/b1/c1" are expanded.
661 QCOMPARE(m_model
->count(), 4);
662 QVERIFY(!m_model
->isExpanded(0));
663 QVERIFY(m_model
->isExpanded(1));
664 QVERIFY(m_model
->isExpanded(2));
665 QVERIFY(!m_model
->isExpanded(3));
667 // Expand the parents of "a 1/b1".
668 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
669 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
671 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
672 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
673 QCOMPARE(m_model
->count(), 5);
674 QVERIFY(m_model
->isExpanded(0));
675 QVERIFY(!m_model
->isExpanded(1));
676 QVERIFY(m_model
->isExpanded(2));
677 QVERIFY(m_model
->isExpanded(3));
678 QVERIFY(!m_model
->isExpanded(4));
679 QVERIFY(m_model
->isConsistent());
682 m_model
->setExpanded(1, true);
683 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
684 QCOMPARE(m_model
->count(), 6);
685 QVERIFY(m_model
->isExpanded(0));
686 QVERIFY(m_model
->isExpanded(1));
687 QVERIFY(!m_model
->isExpanded(2));
688 QVERIFY(m_model
->isExpanded(3));
689 QVERIFY(m_model
->isExpanded(4));
690 QVERIFY(!m_model
->isExpanded(5));
691 QVERIFY(m_model
->isConsistent());
693 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
694 m_model
->setExpanded(1, false);
695 QCOMPARE(m_model
->count(), 5);
696 QVERIFY(m_model
->isExpanded(0));
697 QVERIFY(!m_model
->isExpanded(1));
698 QVERIFY(m_model
->isExpanded(2));
699 QVERIFY(m_model
->isExpanded(3));
700 QVERIFY(!m_model
->isExpanded(4));
701 QVERIFY(m_model
->isConsistent());
705 * Renaming an expanded folder by prepending its name with a dot makes it
706 * hidden. Verify that this does not cause an inconsistent model state and
707 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
709 void KFileItemModelTest::testMakeExpandedItemHidden()
711 QSet
<QByteArray
> modelRoles
= m_model
->roles();
712 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
713 m_model
->setRoles(modelRoles
);
716 m_testDir
->createFile("1a/2a/3a");
717 m_testDir
->createFile("1a/2a/3b");
718 m_testDir
->createFile("1a/2b");
719 m_testDir
->createFile("1b");
721 m_model
->loadDirectory(m_testDir
->url());
722 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
724 // So far, the model contains only "1a/" and "1b".
725 QCOMPARE(m_model
->count(), 2);
726 m_model
->setExpanded(0, true);
727 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
729 // Now "1a/2a" and "1a/2b" have appeared.
730 QCOMPARE(m_model
->count(), 4);
731 m_model
->setExpanded(1, true);
732 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
733 QCOMPARE(m_model
->count(), 6);
735 // Rename "1a/2" and make it hidden.
736 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
737 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
739 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
740 bool ok
= job
->exec();
742 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
744 // "1a/2" and its subfolders have disappeared now.
745 QVERIFY(m_model
->isConsistent());
746 QCOMPARE(m_model
->count(), 3);
748 m_model
->setExpanded(0, false);
749 QCOMPARE(m_model
->count(), 2);
753 void KFileItemModelTest::testRemoveFilteredExpandedItems()
755 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
756 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
757 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
758 m_model
->setRoles(modelRoles
);
761 files
<< "folder/child" << "file"; // missing folders are created automatically
762 m_testDir
->createFiles(files
);
764 m_model
->loadDirectory(m_testDir
->url());
765 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
767 // So far, the model contains only "folder/" and "file".
768 QCOMPARE(m_model
->count(), 2);
769 QVERIFY(m_model
->isExpandable(0));
770 QVERIFY(!m_model
->isExpandable(1));
771 QVERIFY(!m_model
->isExpanded(0));
772 QVERIFY(!m_model
->isExpanded(1));
773 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
775 // Expand "folder" -> "folder/child" becomes visible.
776 m_model
->setExpanded(0, true);
777 QVERIFY(m_model
->isExpanded(0));
778 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
779 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
781 // Add a name filter.
782 m_model
->setNameFilter("f");
783 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
785 m_model
->setNameFilter("fo");
786 QCOMPARE(itemsInModel(), QStringList() << "folder");
788 // Remove all expanded items by changing the roles
789 m_model
->setRoles(originalModelRoles
);
790 QVERIFY(!m_model
->isExpanded(0));
791 QCOMPARE(itemsInModel(), QStringList() << "folder");
793 // Remove the name filter and verify that "folder/child" does not reappear.
794 m_model
->setNameFilter(QString());
795 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
798 void KFileItemModelTest::testSorting()
800 // Create some files with different sizes and modification times to check the different sorting options
801 QDateTime now
= QDateTime::currentDateTime();
803 QSet
<QByteArray
> roles
;
804 roles
.insert("text");
805 roles
.insert("isExpanded");
806 roles
.insert("isExpandable");
807 roles
.insert("expandedParentsCount");
808 m_model
->setRoles(roles
);
810 m_testDir
->createDir("c/c-2");
811 m_testDir
->createFile("c/c-2/c-3");
812 m_testDir
->createFile("c/c-1");
814 m_testDir
->createFile("a", "A file", now
.addDays(-3));
815 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
816 m_testDir
->createDir("c", now
.addDays(-2));
817 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
818 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
819 m_testDir
->createFile(".f");
821 m_model
->loadDirectory(m_testDir
->url());
822 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
824 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
825 m_model
->setExpanded(index
, true);
826 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
828 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
829 m_model
->setExpanded(index
, true);
830 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
832 // Default: Sort by Name, ascending
833 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
834 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
835 QVERIFY(m_model
->sortDirectoriesFirst());
836 QVERIFY(!m_model
->showHiddenFiles());
837 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
839 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
841 // Sort by Name, ascending, 'Sort Folders First' disabled
842 m_model
->setSortDirectoriesFirst(false);
843 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
844 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
845 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
846 QCOMPARE(spyItemsMoved
.count(), 1);
847 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
848 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
850 // Sort by Name, descending
851 m_model
->setSortDirectoriesFirst(true);
852 m_model
->setSortOrder(Qt::DescendingOrder
);
853 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
854 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
855 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
856 QCOMPARE(spyItemsMoved
.count(), 2);
857 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
858 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
859 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
860 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
862 // Sort by Date, descending
863 m_model
->setSortDirectoriesFirst(true);
864 m_model
->setSortRole("date");
865 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
866 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
867 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
868 QCOMPARE(spyItemsMoved
.count(), 1);
869 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
870 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
872 // Sort by Date, ascending
873 m_model
->setSortOrder(Qt::AscendingOrder
);
874 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
875 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
876 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
877 QCOMPARE(spyItemsMoved
.count(), 1);
878 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
879 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
881 // Sort by Date, ascending, 'Sort Folders First' disabled
882 m_model
->setSortDirectoriesFirst(false);
883 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
884 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
885 QVERIFY(!m_model
->sortDirectoriesFirst());
886 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
887 QCOMPARE(spyItemsMoved
.count(), 1);
888 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
889 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
891 // Sort by Name, ascending, 'Sort Folders First' disabled
892 m_model
->setSortRole("text");
893 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
894 QVERIFY(!m_model
->sortDirectoriesFirst());
895 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
896 QCOMPARE(spyItemsMoved
.count(), 1);
897 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
898 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
900 // Sort by Size, ascending, 'Sort Folders First' disabled
901 m_model
->setSortRole("size");
902 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
903 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
904 QVERIFY(!m_model
->sortDirectoriesFirst());
905 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
906 QCOMPARE(spyItemsMoved
.count(), 1);
907 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
908 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
910 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
911 m_model
->setSortDirectoriesFirst(true);
912 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
913 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
914 QVERIFY(m_model
->sortDirectoriesFirst());
915 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
916 QCOMPARE(spyItemsMoved
.count(), 0);
918 // Sort by Size, descending, 'Sort Folders First' enabled
919 m_model
->setSortOrder(Qt::DescendingOrder
);
920 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
921 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
922 QVERIFY(m_model
->sortDirectoriesFirst());
923 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
924 QCOMPARE(spyItemsMoved
.count(), 1);
925 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
926 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
928 // TODO: Sort by other roles; show/hide hidden files
931 void KFileItemModelTest::testIndexForKeyboardSearch()
934 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
935 m_testDir
->createFiles(files
);
937 m_model
->loadDirectory(m_testDir
->url());
938 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
940 // Search from index 0
941 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
942 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
943 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
944 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
945 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
946 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
947 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
948 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
949 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
950 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
951 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
953 // Start a search somewhere in the middle
954 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
955 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
956 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
957 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
959 // Test searches that go past the last item back to index 0
960 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
961 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
962 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
963 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
965 // Test searches that yield no result
966 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
967 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
968 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
969 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
970 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
972 // Test upper case searches (note that search is case insensitive)
973 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
974 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
975 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
976 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
978 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
981 void KFileItemModelTest::testNameFilter()
984 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
985 m_testDir
->createFiles(files
);
987 m_model
->loadDirectory(m_testDir
->url());
988 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
990 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
991 QCOMPARE(m_model
->count(), 3);
993 m_model
->setNameFilter("A2"); // Shows only A2
994 QCOMPARE(m_model
->count(), 1);
996 m_model
->setNameFilter("A2"); // Shows only A1
997 QCOMPARE(m_model
->count(), 1);
999 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1000 QCOMPARE(m_model
->count(), 2);
1002 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1003 QCOMPARE(m_model
->count(), 2);
1005 m_model
->setNameFilter(QString()); // Shows again all items
1006 QCOMPARE(m_model
->count(), 5);
1010 * Verifies that we do not crash when adding a KFileItem with an empty path.
1011 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1012 * tried to always read the first character of the path, even if the path is empty.
1014 void KFileItemModelTest::testEmptyPath()
1016 QSet
<QByteArray
> roles
;
1017 roles
.insert("text");
1018 roles
.insert("isExpanded");
1019 roles
.insert("isExpandable");
1020 roles
.insert("expandedParentsCount");
1021 m_model
->setRoles(roles
);
1023 const KUrl emptyUrl
;
1024 QVERIFY(emptyUrl
.path().isEmpty());
1026 const KUrl
url("file:///test/");
1028 KFileItemList items
;
1029 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1030 m_model
->slotItemsAdded(emptyUrl
, items
);
1031 m_model
->slotCompleted();
1035 * Verifies that the 'isExpanded' state of folders does not change when the
1036 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1038 void KFileItemModelTest::testRefreshExpandedItem()
1040 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1041 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1042 m_model
->setRoles(modelRoles
);
1045 files
<< "a/1" << "a/2" << "3" << "4";
1046 m_testDir
->createFiles(files
);
1048 m_model
->loadDirectory(m_testDir
->url());
1049 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1050 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1052 m_model
->setExpanded(0, true);
1053 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1054 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1055 QVERIFY(m_model
->isExpanded(0));
1057 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1059 const KFileItem item
= m_model
->fileItem(0);
1060 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1061 QVERIFY(!spyItemsChanged
.isEmpty());
1063 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1064 QVERIFY(m_model
->isExpanded(0));
1068 * Verify that removing hidden files and folders from the model does not
1069 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1071 void KFileItemModelTest::testRemoveHiddenItems()
1073 m_testDir
->createDir(".a");
1074 m_testDir
->createDir(".b");
1075 m_testDir
->createDir("c");
1076 m_testDir
->createDir("d");
1077 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1079 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1080 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1082 m_model
->setShowHiddenFiles(true);
1083 m_model
->loadDirectory(m_testDir
->url());
1084 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1085 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1086 QCOMPARE(spyItemsInserted
.count(), 1);
1087 QCOMPARE(spyItemsRemoved
.count(), 0);
1088 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1089 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1091 m_model
->setShowHiddenFiles(false);
1092 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1093 QCOMPARE(spyItemsInserted
.count(), 0);
1094 QCOMPARE(spyItemsRemoved
.count(), 1);
1095 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1096 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1098 m_model
->setShowHiddenFiles(true);
1099 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1100 QCOMPARE(spyItemsInserted
.count(), 1);
1101 QCOMPARE(spyItemsRemoved
.count(), 0);
1102 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1103 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1106 QCOMPARE(itemsInModel(), QStringList());
1107 QCOMPARE(spyItemsInserted
.count(), 0);
1108 QCOMPARE(spyItemsRemoved
.count(), 1);
1109 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1110 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1112 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1113 // Verify that this does not make the model crash.
1114 m_model
->setShowHiddenFiles(false);
1118 * Verify that filtered items are removed when their parent is collapsed.
1120 void KFileItemModelTest::collapseParentOfHiddenItems()
1122 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1123 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1124 m_model
->setRoles(modelRoles
);
1127 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1128 m_testDir
->createFiles(files
);
1130 m_model
->loadDirectory(m_testDir
->url());
1131 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1132 QCOMPARE(m_model
->count(), 1); // Only "a/"
1135 m_model
->setExpanded(0, true);
1136 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1137 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1140 m_model
->setExpanded(1, true);
1141 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1142 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1145 m_model
->setExpanded(2, true);
1146 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1147 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"
1149 // Set a name filter that matches nothing -> only the expanded folders remain.
1150 m_model
->setNameFilter("xyz");
1151 QCOMPARE(m_model
->count(), 3);
1152 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1154 // Collapse the folder "a/".
1155 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1156 m_model
->setExpanded(0, false);
1157 QCOMPARE(spyItemsRemoved
.count(), 1);
1158 QCOMPARE(m_model
->count(), 1);
1159 QCOMPARE(itemsInModel(), QStringList() << "a");
1161 // Remove the filter -> no files should appear (and we should not get a crash).
1162 m_model
->setNameFilter(QString());
1163 QCOMPARE(m_model
->count(), 1);
1167 * Verify that filtered items are removed when their parent is deleted.
1169 void KFileItemModelTest::removeParentOfHiddenItems()
1171 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1172 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1173 m_model
->setRoles(modelRoles
);
1176 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1177 m_testDir
->createFiles(files
);
1179 m_model
->loadDirectory(m_testDir
->url());
1180 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1181 QCOMPARE(m_model
->count(), 1); // Only "a/"
1184 m_model
->setExpanded(0, true);
1185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1186 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1189 m_model
->setExpanded(1, true);
1190 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1191 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1194 m_model
->setExpanded(2, true);
1195 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1196 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"
1198 // Set a name filter that matches nothing -> only the expanded folders remain.
1199 m_model
->setNameFilter("xyz");
1200 QCOMPARE(m_model
->count(), 3);
1201 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1203 // Simulate the deletion of the directory "a/b/".
1204 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1205 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1206 QCOMPARE(spyItemsRemoved
.count(), 1);
1207 QCOMPARE(m_model
->count(), 1);
1208 QCOMPARE(itemsInModel(), QStringList() << "a");
1210 // Remove the filter -> only the file "a/1" should appear.
1211 m_model
->setNameFilter(QString());
1212 QCOMPARE(m_model
->count(), 2);
1213 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1217 * Create a tree structure where parent-child relationships can not be
1218 * determined by parsing the URLs, and verify that KFileItemModel
1219 * handles them correctly.
1221 void KFileItemModelTest::testGeneralParentChildRelationships()
1223 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1224 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1225 m_model
->setRoles(modelRoles
);
1228 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1229 m_testDir
->createFiles(files
);
1231 m_model
->loadDirectory(m_testDir
->url());
1232 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1233 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1235 // Expand all folders.
1236 m_model
->setExpanded(0, true);
1237 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1238 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1240 m_model
->setExpanded(1, true);
1241 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1242 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1244 m_model
->setExpanded(3, true);
1245 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1246 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1248 m_model
->setExpanded(4, true);
1249 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1250 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1252 // Add some more children and grand-children.
1253 const KUrl parent1
= m_model
->fileItem(0).url();
1254 const KUrl parent2
= m_model
->fileItem(3).url();
1255 const KUrl realChild1
= m_model
->fileItem(1).url();
1256 const KUrl realChild2
= m_model
->fileItem(4).url();
1258 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1259 m_model
->slotCompleted();
1260 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1262 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1263 m_model
->slotCompleted();
1264 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1266 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1267 m_model
->slotCompleted();
1268 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1270 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1271 m_model
->slotCompleted();
1272 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1274 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1275 m_model
->slotCompleted();
1276 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1278 // Set a name filter that matches nothing -> only expanded folders remain.
1279 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1280 m_model
->setNameFilter("xyz");
1281 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1282 QCOMPARE(itemsRemovedSpy
.count(), 1);
1283 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1284 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1285 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1287 // Collapse "parent1".
1288 m_model
->setExpanded(0, false);
1289 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1290 QCOMPARE(itemsRemovedSpy
.count(), 1);
1291 arguments
= itemsRemovedSpy
.takeFirst();
1292 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1293 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1295 // Remove "parent2".
1296 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1297 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1298 QCOMPARE(itemsRemovedSpy
.count(), 1);
1299 arguments
= itemsRemovedSpy
.takeFirst();
1300 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1301 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1303 // Clear filter, verify that no items reappear.
1304 m_model
->setNameFilter(QString());
1305 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1308 void KFileItemModelTest::testNameRoleGroups()
1311 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1313 m_testDir
->createFiles(files
);
1315 m_model
->setGroupedSorting(true);
1316 m_model
->loadDirectory(m_testDir
->url());
1317 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1318 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1320 QList
<QPair
<int, QVariant
> > expectedGroups
;
1321 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1322 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1323 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1324 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1325 QCOMPARE(m_model
->groups(), expectedGroups
);
1327 // Rename d.txt to a.txt.
1328 QHash
<QByteArray
, QVariant
> data
;
1329 data
.insert("text", "a.txt");
1330 m_model
->setData(2, data
);
1331 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1332 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1334 expectedGroups
.clear();
1335 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1336 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1337 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1338 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1339 QCOMPARE(m_model
->groups(), expectedGroups
);
1341 // Rename c.txt to d.txt.
1342 data
.insert("text", "d.txt");
1343 m_model
->setData(2, data
);
1344 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1345 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1347 expectedGroups
.clear();
1348 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1349 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1350 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1351 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1352 QCOMPARE(m_model
->groups(), expectedGroups
);
1354 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1355 const KFileItem fileItemD
= m_model
->fileItem(2);
1356 KFileItem fileItemC
= fileItemD
;
1357 KUrl urlC
= fileItemC
.url();
1358 urlC
.setFileName("c.txt");
1359 fileItemC
.setUrl(urlC
);
1361 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1362 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1363 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1365 expectedGroups
.clear();
1366 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1367 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1368 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1369 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1370 QCOMPARE(m_model
->groups(), expectedGroups
);
1373 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1375 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1376 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1377 m_model
->setRoles(modelRoles
);
1380 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1382 m_testDir
->createFiles(files
);
1384 m_model
->setGroupedSorting(true);
1385 m_model
->loadDirectory(m_testDir
->url());
1386 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1387 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1389 QList
<QPair
<int, QVariant
> > expectedGroups
;
1390 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1391 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1392 QCOMPARE(m_model
->groups(), expectedGroups
);
1394 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1395 expectedGroups
.clear();
1396 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1397 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1399 m_model
->setExpanded(0, true);
1400 QVERIFY(m_model
->isExpanded(0));
1401 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1402 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1403 QCOMPARE(m_model
->groups(), expectedGroups
);
1405 m_model
->setExpanded(3, true);
1406 QVERIFY(m_model
->isExpanded(3));
1407 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1408 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1409 QCOMPARE(m_model
->groups(), expectedGroups
);
1412 void KFileItemModelTest::testInconsistentModel()
1414 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1415 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1416 m_model
->setRoles(modelRoles
);
1419 files
<< "a/b/c1.txt" << "a/b/c2.txt";
1421 m_testDir
->createFiles(files
);
1423 m_model
->loadDirectory(m_testDir
->url());
1424 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1425 QCOMPARE(itemsInModel(), QStringList() << "a");
1427 // Expand "a/" and "a/b/".
1428 m_model
->setExpanded(0, true);
1429 QVERIFY(m_model
->isExpanded(0));
1430 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1431 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1433 m_model
->setExpanded(1, true);
1434 QVERIFY(m_model
->isExpanded(1));
1435 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1436 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1438 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1439 // Such a thing can in principle happen when performing a search, and there
1441 // (a) match the search string, and
1442 // (b) are children of a folder that matches the search string and is expanded.
1444 // Note that the first item in the list of added items must be new (i.e., not
1445 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1446 // it receives items that are in the model already and ignore them.
1447 KUrl
url(m_model
->directory().url() + "/a2");
1448 KFileItem
newItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
1450 KFileItemList items
;
1451 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1452 m_model
->slotItemsAdded(m_model
->directory(), items
);
1453 m_model
->slotCompleted();
1454 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1456 m_model
->setExpanded(0, false);
1458 // Test that the right items have been removed, see
1459 // https://bugs.kde.org/show_bug.cgi?id=324371
1460 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1462 // Test that resorting does not cause a crash, see
1463 // https://bugs.kde.org/show_bug.cgi?id=325359
1464 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1465 m_model
->resortAllItems();
1469 void KFileItemModelTest::testChangeRolesForFilteredItems()
1471 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1472 modelRoles
<< "owner";
1473 m_model
->setRoles(modelRoles
);
1476 files
<< "a.txt" << "aa.txt" << "aaa.txt";
1477 m_testDir
->createFiles(files
);
1479 m_model
->loadDirectory(m_testDir
->url());
1480 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1481 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1483 for (int index
= 0; index
< m_model
->count(); ++index
) {
1484 // All items should have the "text" and "owner" roles, but not "group".
1485 QVERIFY(m_model
->data(index
).contains("text"));
1486 QVERIFY(m_model
->data(index
).contains("owner"));
1487 QVERIFY(!m_model
->data(index
).contains("group"));
1490 // Add a filter, such that only "aaa.txt" remains in the model.
1491 m_model
->setNameFilter("aaa");
1492 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1494 // Add the "group" role.
1495 modelRoles
<< "group";
1496 m_model
->setRoles(modelRoles
);
1498 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1499 m_model
->setNameFilter("aa");
1500 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1502 for (int index
= 0; index
< m_model
->count(); ++index
) {
1503 // All items should have the "text", "owner", and "group" roles.
1504 QVERIFY(m_model
->data(index
).contains("text"));
1505 QVERIFY(m_model
->data(index
).contains("owner"));
1506 QVERIFY(m_model
->data(index
).contains("group"));
1509 // Remove the "owner" role.
1510 modelRoles
.remove("owner");
1511 m_model
->setRoles(modelRoles
);
1513 // Clear the filter, and verify that all items have the expected roles
1514 m_model
->setNameFilter(QString());
1515 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1517 for (int index
= 0; index
< m_model
->count(); ++index
) {
1518 // All items should have the "text" and "group" roles, but now "owner".
1519 QVERIFY(m_model
->data(index
).contains("text"));
1520 QVERIFY(!m_model
->data(index
).contains("owner"));
1521 QVERIFY(m_model
->data(index
).contains("group"));
1525 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1527 KFileItemList items
;
1529 KIO::UDSEntry entry
;
1530 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1531 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1532 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1533 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1534 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1535 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1537 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1538 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1539 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1541 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1542 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1543 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1545 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1546 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1547 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1549 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1550 m_model
->slotCompleted();
1552 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1555 m_model
->setNameFilter("a");
1556 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1559 m_model
->setSortRole("owner");
1561 // Clear the filter, and verify that the items are sorted correctly.
1562 m_model
->setNameFilter(QString());
1563 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1566 void KFileItemModelTest::testRefreshFilteredItems()
1569 files
<< "a.txt" << "b.txt" << "c.jpg" << "d.jpg";
1570 m_testDir
->createFiles(files
);
1572 m_model
->loadDirectory(m_testDir
->url());
1573 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1574 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1576 const KFileItem fileItemC
= m_model
->fileItem(2);
1578 // Show only the .txt files.
1579 m_model
->setNameFilter(".txt");
1580 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1582 // Rename one of the .jpg files.
1583 KFileItem fileItemE
= fileItemC
;
1584 KUrl urlE
= fileItemE
.url();
1585 urlE
.setFileName("e.jpg");
1586 fileItemE
.setUrl(urlE
);
1588 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemC
, fileItemE
));
1590 // Show all files again, and verify that the model has updated the file name.
1591 m_model
->setNameFilter(QString());
1592 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1595 void KFileItemModelTest::testCreateMimeData()
1597 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1598 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1599 m_model
->setRoles(modelRoles
);
1603 m_testDir
->createFiles(files
);
1605 m_model
->loadDirectory(m_testDir
->url());
1606 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1607 QCOMPARE(itemsInModel(), QStringList() << "a");
1610 m_model
->setExpanded(0, true);
1611 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1612 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1614 // Verify that creating the MIME data for a child of an expanded folder does
1615 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1617 selection
.insert(1);
1618 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1622 QStringList
KFileItemModelTest::itemsInModel() const
1625 for (int i
= 0; i
< m_model
->count(); i
++) {
1626 items
<< m_model
->fileItem(i
).text();
1631 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1633 #include "kfileitemmodeltest.moc"