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 testCollapseFolderWhileLoading();
97 void testCreateMimeData();
98 void testDeleteFileMoreThanOnce();
101 QStringList
itemsInModel() const;
104 KFileItemModel
* m_model
;
108 void KFileItemModelTest::init()
110 // The item-model tests result in a huge number of debugging
111 // output from kdelibs. Only show critical and fatal messages.
112 qInstallMsgHandler(myMessageOutput
);
114 qRegisterMetaType
<KItemRange
>("KItemRange");
115 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
116 qRegisterMetaType
<KFileItemList
>("KFileItemList");
118 m_testDir
= new TestDir();
119 m_model
= new KFileItemModel();
120 m_model
->m_dirLister
->setAutoUpdate(false);
122 // Reduce the timer interval to make the test run faster.
123 m_model
->m_resortAllItemsTimer
->setInterval(0);
126 void KFileItemModelTest::cleanup()
135 void KFileItemModelTest::testDefaultRoles()
137 const QSet
<QByteArray
> roles
= m_model
->roles();
138 QCOMPARE(roles
.count(), 3);
139 QVERIFY(roles
.contains("text"));
140 QVERIFY(roles
.contains("isDir"));
141 QVERIFY(roles
.contains("isLink"));
144 void KFileItemModelTest::testDefaultSortRole()
146 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
149 files
<< "c.txt" << "a.txt" << "b.txt";
151 m_testDir
->createFiles(files
);
153 m_model
->loadDirectory(m_testDir
->url());
154 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
156 QCOMPARE(m_model
->count(), 3);
157 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
158 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
159 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
162 void KFileItemModelTest::testDefaultGroupedSorting()
164 QCOMPARE(m_model
->groupedSorting(), false);
167 void KFileItemModelTest::testNewItems()
170 files
<< "a.txt" << "b.txt" << "c.txt";
171 m_testDir
->createFiles(files
);
173 m_model
->loadDirectory(m_testDir
->url());
174 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
176 QCOMPARE(m_model
->count(), 3);
178 QVERIFY(m_model
->isConsistent());
181 void KFileItemModelTest::testRemoveItems()
183 m_testDir
->createFile("a.txt");
184 m_testDir
->createFile("b.txt");
185 m_model
->loadDirectory(m_testDir
->url());
186 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
187 QCOMPARE(m_model
->count(), 2);
188 QVERIFY(m_model
->isConsistent());
190 m_testDir
->removeFile("a.txt");
191 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
192 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
193 QCOMPARE(m_model
->count(), 1);
194 QVERIFY(m_model
->isConsistent());
197 void KFileItemModelTest::testDirLoadingCompleted()
199 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
200 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
201 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
203 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
205 m_model
->loadDirectory(m_testDir
->url());
206 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
207 QCOMPARE(loadingCompletedSpy
.count(), 1);
208 QCOMPARE(itemsInsertedSpy
.count(), 1);
209 QCOMPARE(itemsRemovedSpy
.count(), 0);
210 QCOMPARE(m_model
->count(), 3);
212 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
213 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
214 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
215 QCOMPARE(loadingCompletedSpy
.count(), 2);
216 QCOMPARE(itemsInsertedSpy
.count(), 2);
217 QCOMPARE(itemsRemovedSpy
.count(), 0);
218 QCOMPARE(m_model
->count(), 5);
220 m_testDir
->removeFile("a.txt");
221 m_testDir
->createFile("f.txt");
222 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
223 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
224 QCOMPARE(loadingCompletedSpy
.count(), 3);
225 QCOMPARE(itemsInsertedSpy
.count(), 3);
226 QCOMPARE(itemsRemovedSpy
.count(), 1);
227 QCOMPARE(m_model
->count(), 5);
229 m_testDir
->removeFile("b.txt");
230 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
231 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
232 QCOMPARE(loadingCompletedSpy
.count(), 4);
233 QCOMPARE(itemsInsertedSpy
.count(), 3);
234 QCOMPARE(itemsRemovedSpy
.count(), 2);
235 QCOMPARE(m_model
->count(), 4);
237 QVERIFY(m_model
->isConsistent());
240 void KFileItemModelTest::testSetData()
242 m_testDir
->createFile("a.txt");
244 m_model
->loadDirectory(m_testDir
->url());
245 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
247 QHash
<QByteArray
, QVariant
> values
;
248 values
.insert("customRole1", "Test1");
249 values
.insert("customRole2", "Test2");
251 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
252 m_model
->setData(0, values
);
253 QCOMPARE(itemsChangedSpy
.count(), 1);
255 values
= m_model
->data(0);
256 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
257 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
258 QVERIFY(m_model
->isConsistent());
261 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
263 QTest::addColumn
<int>("changedIndex");
264 QTest::addColumn
<int>("changedRating");
265 QTest::addColumn
<bool>("expectMoveSignal");
266 QTest::addColumn
<int>("ratingIndex0");
267 QTest::addColumn
<int>("ratingIndex1");
268 QTest::addColumn
<int>("ratingIndex2");
271 // Index 0 = rating 2
272 // Index 1 = rating 4
273 // Index 2 = rating 6
275 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
276 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
277 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
279 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
280 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
281 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
284 void KFileItemModelTest::testSetDataWithModifiedSortRole()
286 QFETCH(int, changedIndex
);
287 QFETCH(int, changedRating
);
288 QFETCH(bool, expectMoveSignal
);
289 QFETCH(int, ratingIndex0
);
290 QFETCH(int, ratingIndex1
);
291 QFETCH(int, ratingIndex2
);
293 // Changing the value of a sort-role must result in
294 // a reordering of the items.
295 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
296 m_model
->setSortRole("rating");
297 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
300 files
<< "a.txt" << "b.txt" << "c.txt";
301 m_testDir
->createFiles(files
);
303 m_model
->loadDirectory(m_testDir
->url());
304 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
306 // Fill the "rating" role of each file:
311 QHash
<QByteArray
, QVariant
> ratingA
;
312 ratingA
.insert("rating", 2);
313 m_model
->setData(0, ratingA
);
315 QHash
<QByteArray
, QVariant
> ratingB
;
316 ratingB
.insert("rating", 4);
317 m_model
->setData(1, ratingB
);
319 QHash
<QByteArray
, QVariant
> ratingC
;
320 ratingC
.insert("rating", 6);
321 m_model
->setData(2, ratingC
);
323 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
324 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
325 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
327 // Now change the rating from a.txt. This usually results
328 // in reordering of the items.
329 QHash
<QByteArray
, QVariant
> rating
;
330 rating
.insert("rating", changedRating
);
331 m_model
->setData(changedIndex
, rating
);
333 if (expectMoveSignal
) {
334 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
337 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
338 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
339 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
340 QVERIFY(m_model
->isConsistent());
343 void KFileItemModelTest::testChangeSortRole()
345 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
348 files
<< "a.txt" << "b.jpg" << "c.txt";
349 m_testDir
->createFiles(files
);
351 m_model
->loadDirectory(m_testDir
->url());
352 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
353 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
355 // Simulate that KFileItemModelRolesUpdater determines the mime type.
356 // Resorting the files by 'type' will only work immediately if their
357 // mime types are known.
358 for (int index
= 0; index
< m_model
->count(); ++index
) {
359 m_model
->fileItem(index
).determineMimeType();
362 // Now: sort by type.
363 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
364 m_model
->setSortRole("type");
365 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
366 QVERIFY(!spyItemsMoved
.isEmpty());
368 // The actual order of the files might depend on the translation of the
369 // result of KFileItem::mimeComment() in the user's language.
370 QStringList version1
;
371 version1
<< "b.jpg" << "a.txt" << "c.txt";
373 QStringList version2
;
374 version2
<< "a.txt" << "c.txt" << "b.jpg";
376 const bool ok1
= (itemsInModel() == version1
);
377 const bool ok2
= (itemsInModel() == version2
);
382 void KFileItemModelTest::testResortAfterChangingName()
384 // We sort by size in a directory where all files have the same size.
385 // Therefore, the files are sorted by their names.
386 m_model
->setSortRole("size");
389 files
<< "a.txt" << "b.txt" << "c.txt";
390 m_testDir
->createFiles(files
);
392 m_model
->loadDirectory(m_testDir
->url());
393 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
394 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
396 // We rename a.txt to d.txt. Even though the size has not changed at all,
397 // the model must re-sort the items.
398 QHash
<QByteArray
, QVariant
> data
;
399 data
.insert("text", "d.txt");
400 m_model
->setData(0, data
);
402 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
403 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
405 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
406 const KFileItem fileItemD
= m_model
->fileItem(2);
407 KFileItem fileItemA
= fileItemD
;
408 KUrl urlA
= fileItemA
.url();
409 urlA
.setFileName("a.txt");
410 fileItemA
.setUrl(urlA
);
412 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
414 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
415 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
418 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
420 //QSKIP("Temporary disabled", SkipSingle);
422 // KFileItemModel prevents that inserting a punch of items sequentially
423 // results in an itemsInserted()-signal for each item. Instead internally
424 // a timeout is given that collects such operations and results in only
425 // one itemsInserted()-signal. However in this test we want to stress
426 // KFileItemModel to do a lot of insert operation and hence decrease
427 // the timeout to 1 millisecond.
428 m_testDir
->createFile("1");
429 m_model
->loadDirectory(m_testDir
->url());
430 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
431 QCOMPARE(m_model
->count(), 1);
433 // Insert 10 items for 20 times. After each insert operation the model consistency
435 QSet
<int> insertedItems
;
436 for (int i
= 0; i
< 20; ++i
) {
437 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
439 for (int j
= 0; j
< 10; ++j
) {
440 int itemName
= qrand();
441 while (insertedItems
.contains(itemName
)) {
444 insertedItems
.insert(itemName
);
446 m_testDir
->createFile(QString::number(itemName
));
449 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
450 if (spy
.count() == 0) {
451 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
454 QVERIFY(m_model
->isConsistent());
457 QCOMPARE(m_model
->count(), 201);
460 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
463 files
<< "B" << "E" << "G";
464 m_testDir
->createFiles(files
);
466 // Due to inserting the 3 items one item-range with index == 0 and
467 // count == 3 must be given
468 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
469 m_model
->loadDirectory(m_testDir
->url());
470 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
472 QCOMPARE(spy1
.count(), 1);
473 QList
<QVariant
> arguments
= spy1
.takeFirst();
474 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
475 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
477 // The indexes of the item-ranges must always be related to the model before
478 // the items have been inserted. Having:
481 // and inserting A, C, D, F the resulting model will be:
484 // and the item-ranges must be:
485 // index: 0, count: 1 for A
486 // index: 1, count: 2 for B, C
487 // index: 2, count: 1 for G
490 files
<< "A" << "C" << "D" << "F";
491 m_testDir
->createFiles(files
);
493 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
494 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
495 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
497 QCOMPARE(spy2
.count(), 1);
498 arguments
= spy2
.takeFirst();
499 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
500 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
503 void KFileItemModelTest::testExpandItems()
505 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
506 // Besides testing the basic item expansion functionality, the test makes sure that
507 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
508 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
509 // first three characters.
510 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
511 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
512 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
513 m_model
->setRoles(modelRoles
);
516 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
517 m_testDir
->createFiles(files
);
519 // Store the URLs of all folders in a set.
520 QSet
<KUrl
> allFolders
;
521 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
523 m_model
->loadDirectory(m_testDir
->url());
524 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
526 // So far, the model contains only "a/"
527 QCOMPARE(m_model
->count(), 1);
528 QVERIFY(m_model
->isExpandable(0));
529 QVERIFY(!m_model
->isExpanded(0));
530 QVERIFY(m_model
->expandedDirectories().empty());
532 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
534 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
535 m_model
->setExpanded(0, true);
536 QVERIFY(m_model
->isExpanded(0));
537 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
538 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
539 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
541 QCOMPARE(spyInserted
.count(), 1);
542 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
543 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
545 QVERIFY(m_model
->isExpandable(1));
546 QVERIFY(!m_model
->isExpanded(1));
547 QVERIFY(m_model
->isExpandable(2));
548 QVERIFY(!m_model
->isExpanded(2));
550 // Expand the folder "a/a/" -> "a/a/1" becomes visible
551 m_model
->setExpanded(1, true);
552 QVERIFY(m_model
->isExpanded(1));
553 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
554 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
555 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
557 QCOMPARE(spyInserted
.count(), 1);
558 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
559 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
561 QVERIFY(!m_model
->isExpandable(2));
562 QVERIFY(!m_model
->isExpanded(2));
564 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
565 m_model
->setExpanded(3, true);
566 QVERIFY(m_model
->isExpanded(3));
567 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
568 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
569 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
571 QCOMPARE(spyInserted
.count(), 1);
572 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
573 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
575 QVERIFY(!m_model
->isExpandable(4));
576 QVERIFY(!m_model
->isExpanded(4));
578 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
580 // Collapse the top-level folder -> all other items should disappear
581 m_model
->setExpanded(0, false);
582 QVERIFY(!m_model
->isExpanded(0));
583 QCOMPARE(m_model
->count(), 1);
584 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
586 QCOMPARE(spyRemoved
.count(), 1);
587 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
588 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
589 QVERIFY(m_model
->isConsistent());
591 // Clear the model, reload the folder and try to restore the expanded folders.
593 QCOMPARE(m_model
->count(), 0);
594 QVERIFY(m_model
->expandedDirectories().empty());
596 m_model
->loadDirectory(m_testDir
->url());
597 m_model
->restoreExpandedDirectories(allFolders
);
598 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
599 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
600 QVERIFY(m_model
->isExpanded(0));
601 QVERIFY(m_model
->isExpanded(1));
602 QVERIFY(!m_model
->isExpanded(2));
603 QVERIFY(m_model
->isExpanded(3));
604 QVERIFY(!m_model
->isExpanded(4));
605 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
606 QVERIFY(m_model
->isConsistent());
608 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
609 // This is how DolphinView restores the expanded folders when navigating in history.
610 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
611 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
612 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
613 m_model
->restoreExpandedDirectories(allFolders
);
614 m_model
->loadDirectory(m_testDir
->url());
615 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
616 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
617 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
619 // Remove all expanded items by changing the roles
621 m_model
->setRoles(originalModelRoles
);
622 QVERIFY(!m_model
->isExpanded(0));
623 QCOMPARE(m_model
->count(), 1);
624 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
626 QCOMPARE(spyRemoved
.count(), 1);
627 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
628 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
629 QVERIFY(m_model
->isConsistent());
632 void KFileItemModelTest::testExpandParentItems()
634 // Create a tree structure of folders:
642 QSet
<QByteArray
> modelRoles
= m_model
->roles();
643 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
644 m_model
->setRoles(modelRoles
);
647 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
648 m_testDir
->createFiles(files
);
650 m_model
->loadDirectory(m_testDir
->url());
651 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
653 // So far, the model contains only "a 1/" and "a2/".
654 QCOMPARE(m_model
->count(), 2);
655 QVERIFY(m_model
->expandedDirectories().empty());
657 // Expand the parents of "a2/b2/c2".
658 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
659 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
661 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
662 // It's important that only the parents of "a1/b1/c1" are expanded.
663 QCOMPARE(m_model
->count(), 4);
664 QVERIFY(!m_model
->isExpanded(0));
665 QVERIFY(m_model
->isExpanded(1));
666 QVERIFY(m_model
->isExpanded(2));
667 QVERIFY(!m_model
->isExpanded(3));
669 // Expand the parents of "a 1/b1".
670 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
671 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
673 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
674 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
675 QCOMPARE(m_model
->count(), 5);
676 QVERIFY(m_model
->isExpanded(0));
677 QVERIFY(!m_model
->isExpanded(1));
678 QVERIFY(m_model
->isExpanded(2));
679 QVERIFY(m_model
->isExpanded(3));
680 QVERIFY(!m_model
->isExpanded(4));
681 QVERIFY(m_model
->isConsistent());
684 m_model
->setExpanded(1, true);
685 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
686 QCOMPARE(m_model
->count(), 6);
687 QVERIFY(m_model
->isExpanded(0));
688 QVERIFY(m_model
->isExpanded(1));
689 QVERIFY(!m_model
->isExpanded(2));
690 QVERIFY(m_model
->isExpanded(3));
691 QVERIFY(m_model
->isExpanded(4));
692 QVERIFY(!m_model
->isExpanded(5));
693 QVERIFY(m_model
->isConsistent());
695 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
696 m_model
->setExpanded(1, false);
697 QCOMPARE(m_model
->count(), 5);
698 QVERIFY(m_model
->isExpanded(0));
699 QVERIFY(!m_model
->isExpanded(1));
700 QVERIFY(m_model
->isExpanded(2));
701 QVERIFY(m_model
->isExpanded(3));
702 QVERIFY(!m_model
->isExpanded(4));
703 QVERIFY(m_model
->isConsistent());
707 * Renaming an expanded folder by prepending its name with a dot makes it
708 * hidden. Verify that this does not cause an inconsistent model state and
709 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
711 void KFileItemModelTest::testMakeExpandedItemHidden()
713 QSet
<QByteArray
> modelRoles
= m_model
->roles();
714 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
715 m_model
->setRoles(modelRoles
);
718 m_testDir
->createFile("1a/2a/3a");
719 m_testDir
->createFile("1a/2a/3b");
720 m_testDir
->createFile("1a/2b");
721 m_testDir
->createFile("1b");
723 m_model
->loadDirectory(m_testDir
->url());
724 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
726 // So far, the model contains only "1a/" and "1b".
727 QCOMPARE(m_model
->count(), 2);
728 m_model
->setExpanded(0, true);
729 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
731 // Now "1a/2a" and "1a/2b" have appeared.
732 QCOMPARE(m_model
->count(), 4);
733 m_model
->setExpanded(1, true);
734 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
735 QCOMPARE(m_model
->count(), 6);
737 // Rename "1a/2" and make it hidden.
738 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
739 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
741 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
742 bool ok
= job
->exec();
744 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
746 // "1a/2" and its subfolders have disappeared now.
747 QVERIFY(m_model
->isConsistent());
748 QCOMPARE(m_model
->count(), 3);
750 m_model
->setExpanded(0, false);
751 QCOMPARE(m_model
->count(), 2);
755 void KFileItemModelTest::testRemoveFilteredExpandedItems()
757 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
758 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
759 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
760 m_model
->setRoles(modelRoles
);
763 files
<< "folder/child" << "file"; // missing folders are created automatically
764 m_testDir
->createFiles(files
);
766 m_model
->loadDirectory(m_testDir
->url());
767 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
769 // So far, the model contains only "folder/" and "file".
770 QCOMPARE(m_model
->count(), 2);
771 QVERIFY(m_model
->isExpandable(0));
772 QVERIFY(!m_model
->isExpandable(1));
773 QVERIFY(!m_model
->isExpanded(0));
774 QVERIFY(!m_model
->isExpanded(1));
775 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
777 // Expand "folder" -> "folder/child" becomes visible.
778 m_model
->setExpanded(0, true);
779 QVERIFY(m_model
->isExpanded(0));
780 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
781 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
783 // Add a name filter.
784 m_model
->setNameFilter("f");
785 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
787 m_model
->setNameFilter("fo");
788 QCOMPARE(itemsInModel(), QStringList() << "folder");
790 // Remove all expanded items by changing the roles
791 m_model
->setRoles(originalModelRoles
);
792 QVERIFY(!m_model
->isExpanded(0));
793 QCOMPARE(itemsInModel(), QStringList() << "folder");
795 // Remove the name filter and verify that "folder/child" does not reappear.
796 m_model
->setNameFilter(QString());
797 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
800 void KFileItemModelTest::testSorting()
802 // Create some files with different sizes and modification times to check the different sorting options
803 QDateTime now
= QDateTime::currentDateTime();
805 QSet
<QByteArray
> roles
;
806 roles
.insert("text");
807 roles
.insert("isExpanded");
808 roles
.insert("isExpandable");
809 roles
.insert("expandedParentsCount");
810 m_model
->setRoles(roles
);
812 m_testDir
->createDir("c/c-2");
813 m_testDir
->createFile("c/c-2/c-3");
814 m_testDir
->createFile("c/c-1");
816 m_testDir
->createFile("a", "A file", now
.addDays(-3));
817 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
818 m_testDir
->createDir("c", now
.addDays(-2));
819 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
820 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
821 m_testDir
->createFile(".f");
823 m_model
->loadDirectory(m_testDir
->url());
824 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
826 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
827 m_model
->setExpanded(index
, true);
828 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
830 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
831 m_model
->setExpanded(index
, true);
832 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
834 // Default: Sort by Name, ascending
835 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
836 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
837 QVERIFY(m_model
->sortDirectoriesFirst());
838 QVERIFY(!m_model
->showHiddenFiles());
839 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
841 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
843 // Sort by Name, ascending, 'Sort Folders First' disabled
844 m_model
->setSortDirectoriesFirst(false);
845 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
846 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
847 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
848 QCOMPARE(spyItemsMoved
.count(), 1);
849 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
850 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
852 // Sort by Name, descending
853 m_model
->setSortDirectoriesFirst(true);
854 m_model
->setSortOrder(Qt::DescendingOrder
);
855 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
856 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
857 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
858 QCOMPARE(spyItemsMoved
.count(), 2);
859 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
860 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
861 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
862 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
864 // Sort by Date, descending
865 m_model
->setSortDirectoriesFirst(true);
866 m_model
->setSortRole("date");
867 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
868 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
869 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
870 QCOMPARE(spyItemsMoved
.count(), 1);
871 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
872 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
874 // Sort by Date, ascending
875 m_model
->setSortOrder(Qt::AscendingOrder
);
876 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
877 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
878 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
879 QCOMPARE(spyItemsMoved
.count(), 1);
880 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
881 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
883 // Sort by Date, ascending, 'Sort Folders First' disabled
884 m_model
->setSortDirectoriesFirst(false);
885 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
886 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
887 QVERIFY(!m_model
->sortDirectoriesFirst());
888 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
889 QCOMPARE(spyItemsMoved
.count(), 1);
890 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
891 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
893 // Sort by Name, ascending, 'Sort Folders First' disabled
894 m_model
->setSortRole("text");
895 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
896 QVERIFY(!m_model
->sortDirectoriesFirst());
897 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
898 QCOMPARE(spyItemsMoved
.count(), 1);
899 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
900 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
902 // Sort by Size, ascending, 'Sort Folders First' disabled
903 m_model
->setSortRole("size");
904 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
905 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
906 QVERIFY(!m_model
->sortDirectoriesFirst());
907 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
908 QCOMPARE(spyItemsMoved
.count(), 1);
909 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
910 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
912 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
913 m_model
->setSortDirectoriesFirst(true);
914 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
915 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
916 QVERIFY(m_model
->sortDirectoriesFirst());
917 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
918 QCOMPARE(spyItemsMoved
.count(), 0);
920 // Sort by Size, descending, 'Sort Folders First' enabled
921 m_model
->setSortOrder(Qt::DescendingOrder
);
922 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
923 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
924 QVERIFY(m_model
->sortDirectoriesFirst());
925 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
926 QCOMPARE(spyItemsMoved
.count(), 1);
927 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
928 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
930 // TODO: Sort by other roles; show/hide hidden files
933 void KFileItemModelTest::testIndexForKeyboardSearch()
936 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
937 m_testDir
->createFiles(files
);
939 m_model
->loadDirectory(m_testDir
->url());
940 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
942 // Search from index 0
943 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
944 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
945 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
946 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
947 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
948 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
949 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
950 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
951 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
952 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
953 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
955 // Start a search somewhere in the middle
956 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
957 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
958 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
959 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
961 // Test searches that go past the last item back to index 0
962 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
963 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
964 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
965 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
967 // Test searches that yield no result
968 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
969 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
970 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
971 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
972 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
974 // Test upper case searches (note that search is case insensitive)
975 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
976 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
977 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
978 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
980 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
983 void KFileItemModelTest::testNameFilter()
986 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
987 m_testDir
->createFiles(files
);
989 m_model
->loadDirectory(m_testDir
->url());
990 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
992 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
993 QCOMPARE(m_model
->count(), 3);
995 m_model
->setNameFilter("A2"); // Shows only A2
996 QCOMPARE(m_model
->count(), 1);
998 m_model
->setNameFilter("A2"); // Shows only A1
999 QCOMPARE(m_model
->count(), 1);
1001 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1002 QCOMPARE(m_model
->count(), 2);
1004 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1005 QCOMPARE(m_model
->count(), 2);
1007 m_model
->setNameFilter(QString()); // Shows again all items
1008 QCOMPARE(m_model
->count(), 5);
1012 * Verifies that we do not crash when adding a KFileItem with an empty path.
1013 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1014 * tried to always read the first character of the path, even if the path is empty.
1016 void KFileItemModelTest::testEmptyPath()
1018 QSet
<QByteArray
> roles
;
1019 roles
.insert("text");
1020 roles
.insert("isExpanded");
1021 roles
.insert("isExpandable");
1022 roles
.insert("expandedParentsCount");
1023 m_model
->setRoles(roles
);
1025 const KUrl emptyUrl
;
1026 QVERIFY(emptyUrl
.path().isEmpty());
1028 const KUrl
url("file:///test/");
1030 KFileItemList items
;
1031 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1032 m_model
->slotItemsAdded(emptyUrl
, items
);
1033 m_model
->slotCompleted();
1037 * Verifies that the 'isExpanded' state of folders does not change when the
1038 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1040 void KFileItemModelTest::testRefreshExpandedItem()
1042 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1043 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1044 m_model
->setRoles(modelRoles
);
1047 files
<< "a/1" << "a/2" << "3" << "4";
1048 m_testDir
->createFiles(files
);
1050 m_model
->loadDirectory(m_testDir
->url());
1051 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1052 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1054 m_model
->setExpanded(0, true);
1055 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1056 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1057 QVERIFY(m_model
->isExpanded(0));
1059 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1061 const KFileItem item
= m_model
->fileItem(0);
1062 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1063 QVERIFY(!spyItemsChanged
.isEmpty());
1065 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1066 QVERIFY(m_model
->isExpanded(0));
1070 * Verify that removing hidden files and folders from the model does not
1071 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1073 void KFileItemModelTest::testRemoveHiddenItems()
1075 m_testDir
->createDir(".a");
1076 m_testDir
->createDir(".b");
1077 m_testDir
->createDir("c");
1078 m_testDir
->createDir("d");
1079 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1081 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1082 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1084 m_model
->setShowHiddenFiles(true);
1085 m_model
->loadDirectory(m_testDir
->url());
1086 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1087 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1088 QCOMPARE(spyItemsInserted
.count(), 1);
1089 QCOMPARE(spyItemsRemoved
.count(), 0);
1090 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1091 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1093 m_model
->setShowHiddenFiles(false);
1094 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1095 QCOMPARE(spyItemsInserted
.count(), 0);
1096 QCOMPARE(spyItemsRemoved
.count(), 1);
1097 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1098 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1100 m_model
->setShowHiddenFiles(true);
1101 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1102 QCOMPARE(spyItemsInserted
.count(), 1);
1103 QCOMPARE(spyItemsRemoved
.count(), 0);
1104 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1105 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1108 QCOMPARE(itemsInModel(), QStringList());
1109 QCOMPARE(spyItemsInserted
.count(), 0);
1110 QCOMPARE(spyItemsRemoved
.count(), 1);
1111 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1112 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1114 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1115 // Verify that this does not make the model crash.
1116 m_model
->setShowHiddenFiles(false);
1120 * Verify that filtered items are removed when their parent is collapsed.
1122 void KFileItemModelTest::collapseParentOfHiddenItems()
1124 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1125 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1126 m_model
->setRoles(modelRoles
);
1129 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1130 m_testDir
->createFiles(files
);
1132 m_model
->loadDirectory(m_testDir
->url());
1133 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1134 QCOMPARE(m_model
->count(), 1); // Only "a/"
1137 m_model
->setExpanded(0, true);
1138 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1139 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1142 m_model
->setExpanded(1, true);
1143 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1144 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1147 m_model
->setExpanded(2, true);
1148 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1149 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"
1151 // Set a name filter that matches nothing -> only the expanded folders remain.
1152 m_model
->setNameFilter("xyz");
1153 QCOMPARE(m_model
->count(), 3);
1154 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1156 // Collapse the folder "a/".
1157 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1158 m_model
->setExpanded(0, false);
1159 QCOMPARE(spyItemsRemoved
.count(), 1);
1160 QCOMPARE(m_model
->count(), 1);
1161 QCOMPARE(itemsInModel(), QStringList() << "a");
1163 // Remove the filter -> no files should appear (and we should not get a crash).
1164 m_model
->setNameFilter(QString());
1165 QCOMPARE(m_model
->count(), 1);
1169 * Verify that filtered items are removed when their parent is deleted.
1171 void KFileItemModelTest::removeParentOfHiddenItems()
1173 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1174 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1175 m_model
->setRoles(modelRoles
);
1178 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1179 m_testDir
->createFiles(files
);
1181 m_model
->loadDirectory(m_testDir
->url());
1182 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1183 QCOMPARE(m_model
->count(), 1); // Only "a/"
1186 m_model
->setExpanded(0, true);
1187 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1188 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1191 m_model
->setExpanded(1, true);
1192 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1193 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1196 m_model
->setExpanded(2, true);
1197 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1198 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"
1200 // Set a name filter that matches nothing -> only the expanded folders remain.
1201 m_model
->setNameFilter("xyz");
1202 QCOMPARE(m_model
->count(), 3);
1203 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1205 // Simulate the deletion of the directory "a/b/".
1206 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1207 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1208 QCOMPARE(spyItemsRemoved
.count(), 1);
1209 QCOMPARE(m_model
->count(), 1);
1210 QCOMPARE(itemsInModel(), QStringList() << "a");
1212 // Remove the filter -> only the file "a/1" should appear.
1213 m_model
->setNameFilter(QString());
1214 QCOMPARE(m_model
->count(), 2);
1215 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1219 * Create a tree structure where parent-child relationships can not be
1220 * determined by parsing the URLs, and verify that KFileItemModel
1221 * handles them correctly.
1223 void KFileItemModelTest::testGeneralParentChildRelationships()
1225 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1226 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1227 m_model
->setRoles(modelRoles
);
1230 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1231 m_testDir
->createFiles(files
);
1233 m_model
->loadDirectory(m_testDir
->url());
1234 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1235 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1237 // Expand all folders.
1238 m_model
->setExpanded(0, true);
1239 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1240 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1242 m_model
->setExpanded(1, true);
1243 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1244 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1246 m_model
->setExpanded(3, true);
1247 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1248 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1250 m_model
->setExpanded(4, true);
1251 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1252 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1254 // Add some more children and grand-children.
1255 const KUrl parent1
= m_model
->fileItem(0).url();
1256 const KUrl parent2
= m_model
->fileItem(3).url();
1257 const KUrl realChild1
= m_model
->fileItem(1).url();
1258 const KUrl realChild2
= m_model
->fileItem(4).url();
1260 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1261 m_model
->slotCompleted();
1262 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1264 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1265 m_model
->slotCompleted();
1266 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "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(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1273 m_model
->slotCompleted();
1274 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1276 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1277 m_model
->slotCompleted();
1278 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1280 // Set a name filter that matches nothing -> only expanded folders remain.
1281 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1282 m_model
->setNameFilter("xyz");
1283 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1284 QCOMPARE(itemsRemovedSpy
.count(), 1);
1285 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1286 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1287 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1289 // Collapse "parent1".
1290 m_model
->setExpanded(0, false);
1291 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1292 QCOMPARE(itemsRemovedSpy
.count(), 1);
1293 arguments
= itemsRemovedSpy
.takeFirst();
1294 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1295 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1297 // Remove "parent2".
1298 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1299 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1300 QCOMPARE(itemsRemovedSpy
.count(), 1);
1301 arguments
= itemsRemovedSpy
.takeFirst();
1302 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1303 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1305 // Clear filter, verify that no items reappear.
1306 m_model
->setNameFilter(QString());
1307 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1310 void KFileItemModelTest::testNameRoleGroups()
1313 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1315 m_testDir
->createFiles(files
);
1317 m_model
->setGroupedSorting(true);
1318 m_model
->loadDirectory(m_testDir
->url());
1319 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1320 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1322 QList
<QPair
<int, QVariant
> > expectedGroups
;
1323 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1324 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1325 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1326 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1327 QCOMPARE(m_model
->groups(), expectedGroups
);
1329 // Rename d.txt to a.txt.
1330 QHash
<QByteArray
, QVariant
> data
;
1331 data
.insert("text", "a.txt");
1332 m_model
->setData(2, data
);
1333 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1334 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1336 expectedGroups
.clear();
1337 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1338 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1339 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1340 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1341 QCOMPARE(m_model
->groups(), expectedGroups
);
1343 // Rename c.txt to d.txt.
1344 data
.insert("text", "d.txt");
1345 m_model
->setData(2, data
);
1346 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1347 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1349 expectedGroups
.clear();
1350 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1351 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1352 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1353 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1354 QCOMPARE(m_model
->groups(), expectedGroups
);
1356 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1357 const KFileItem fileItemD
= m_model
->fileItem(2);
1358 KFileItem fileItemC
= fileItemD
;
1359 KUrl urlC
= fileItemC
.url();
1360 urlC
.setFileName("c.txt");
1361 fileItemC
.setUrl(urlC
);
1363 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1364 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1365 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1367 expectedGroups
.clear();
1368 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1369 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1370 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1371 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1372 QCOMPARE(m_model
->groups(), expectedGroups
);
1375 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1377 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1378 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1379 m_model
->setRoles(modelRoles
);
1382 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1384 m_testDir
->createFiles(files
);
1386 m_model
->setGroupedSorting(true);
1387 m_model
->loadDirectory(m_testDir
->url());
1388 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1389 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1391 QList
<QPair
<int, QVariant
> > expectedGroups
;
1392 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1393 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1394 QCOMPARE(m_model
->groups(), expectedGroups
);
1396 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1397 expectedGroups
.clear();
1398 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1399 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1401 m_model
->setExpanded(0, true);
1402 QVERIFY(m_model
->isExpanded(0));
1403 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1404 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1405 QCOMPARE(m_model
->groups(), expectedGroups
);
1407 m_model
->setExpanded(3, true);
1408 QVERIFY(m_model
->isExpanded(3));
1409 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1410 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1411 QCOMPARE(m_model
->groups(), expectedGroups
);
1414 void KFileItemModelTest::testInconsistentModel()
1416 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1417 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1418 m_model
->setRoles(modelRoles
);
1421 files
<< "a/b/c1.txt" << "a/b/c2.txt";
1423 m_testDir
->createFiles(files
);
1425 m_model
->loadDirectory(m_testDir
->url());
1426 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1427 QCOMPARE(itemsInModel(), QStringList() << "a");
1429 // Expand "a/" and "a/b/".
1430 m_model
->setExpanded(0, true);
1431 QVERIFY(m_model
->isExpanded(0));
1432 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1433 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1435 m_model
->setExpanded(1, true);
1436 QVERIFY(m_model
->isExpanded(1));
1437 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1438 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1440 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1441 // Such a thing can in principle happen when performing a search, and there
1443 // (a) match the search string, and
1444 // (b) are children of a folder that matches the search string and is expanded.
1446 // Note that the first item in the list of added items must be new (i.e., not
1447 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1448 // it receives items that are in the model already and ignore them.
1449 KUrl
url(m_model
->directory().url() + "/a2");
1450 KFileItem
newItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
1452 KFileItemList items
;
1453 items
<< newItem
<< m_model
->fileItem(2) << m_model
->fileItem(3);
1454 m_model
->slotItemsAdded(m_model
->directory(), items
);
1455 m_model
->slotCompleted();
1456 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1458 m_model
->setExpanded(0, false);
1460 // Test that the right items have been removed, see
1461 // https://bugs.kde.org/show_bug.cgi?id=324371
1462 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1464 // Test that resorting does not cause a crash, see
1465 // https://bugs.kde.org/show_bug.cgi?id=325359
1466 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1467 m_model
->resortAllItems();
1471 void KFileItemModelTest::testChangeRolesForFilteredItems()
1473 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1474 modelRoles
<< "owner";
1475 m_model
->setRoles(modelRoles
);
1478 files
<< "a.txt" << "aa.txt" << "aaa.txt";
1479 m_testDir
->createFiles(files
);
1481 m_model
->loadDirectory(m_testDir
->url());
1482 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1483 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1485 for (int index
= 0; index
< m_model
->count(); ++index
) {
1486 // All items should have the "text" and "owner" roles, but not "group".
1487 QVERIFY(m_model
->data(index
).contains("text"));
1488 QVERIFY(m_model
->data(index
).contains("owner"));
1489 QVERIFY(!m_model
->data(index
).contains("group"));
1492 // Add a filter, such that only "aaa.txt" remains in the model.
1493 m_model
->setNameFilter("aaa");
1494 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1496 // Add the "group" role.
1497 modelRoles
<< "group";
1498 m_model
->setRoles(modelRoles
);
1500 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1501 m_model
->setNameFilter("aa");
1502 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1504 for (int index
= 0; index
< m_model
->count(); ++index
) {
1505 // All items should have the "text", "owner", and "group" roles.
1506 QVERIFY(m_model
->data(index
).contains("text"));
1507 QVERIFY(m_model
->data(index
).contains("owner"));
1508 QVERIFY(m_model
->data(index
).contains("group"));
1511 // Remove the "owner" role.
1512 modelRoles
.remove("owner");
1513 m_model
->setRoles(modelRoles
);
1515 // Clear the filter, and verify that all items have the expected roles
1516 m_model
->setNameFilter(QString());
1517 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1519 for (int index
= 0; index
< m_model
->count(); ++index
) {
1520 // All items should have the "text" and "group" roles, but now "owner".
1521 QVERIFY(m_model
->data(index
).contains("text"));
1522 QVERIFY(!m_model
->data(index
).contains("owner"));
1523 QVERIFY(m_model
->data(index
).contains("group"));
1527 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1529 KFileItemList items
;
1531 KIO::UDSEntry entry
;
1532 entry
.insert(KIO::UDSEntry::UDS_FILE_TYPE
, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1533 entry
.insert(KIO::UDSEntry::UDS_ACCESS
, 07777);
1534 entry
.insert(KIO::UDSEntry::UDS_SIZE
, 0);
1535 entry
.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME
, 0);
1536 entry
.insert(KIO::UDSEntry::UDS_GROUP
, "group");
1537 entry
.insert(KIO::UDSEntry::UDS_ACCESS_TIME
, 0);
1539 entry
.insert(KIO::UDSEntry::UDS_NAME
, "a.txt");
1540 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-b");
1541 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1543 entry
.insert(KIO::UDSEntry::UDS_NAME
, "b.txt");
1544 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-c");
1545 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1547 entry
.insert(KIO::UDSEntry::UDS_NAME
, "c.txt");
1548 entry
.insert(KIO::UDSEntry::UDS_USER
, "user-a");
1549 items
.append(KFileItem(entry
, m_testDir
->url(), false, true));
1551 m_model
->slotItemsAdded(m_testDir
->url(), items
);
1552 m_model
->slotCompleted();
1554 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1557 m_model
->setNameFilter("a");
1558 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1561 m_model
->setSortRole("owner");
1563 // Clear the filter, and verify that the items are sorted correctly.
1564 m_model
->setNameFilter(QString());
1565 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1568 void KFileItemModelTest::testRefreshFilteredItems()
1571 files
<< "a.txt" << "b.txt" << "c.jpg" << "d.jpg";
1572 m_testDir
->createFiles(files
);
1574 m_model
->loadDirectory(m_testDir
->url());
1575 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1576 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1578 const KFileItem fileItemC
= m_model
->fileItem(2);
1580 // Show only the .txt files.
1581 m_model
->setNameFilter(".txt");
1582 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1584 // Rename one of the .jpg files.
1585 KFileItem fileItemE
= fileItemC
;
1586 KUrl urlE
= fileItemE
.url();
1587 urlE
.setFileName("e.jpg");
1588 fileItemE
.setUrl(urlE
);
1590 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemC
, fileItemE
));
1592 // Show all files again, and verify that the model has updated the file name.
1593 m_model
->setNameFilter(QString());
1594 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1597 void KFileItemModelTest::testCreateMimeData()
1599 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1600 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1601 m_model
->setRoles(modelRoles
);
1605 m_testDir
->createFiles(files
);
1607 m_model
->loadDirectory(m_testDir
->url());
1608 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1609 QCOMPARE(itemsInModel(), QStringList() << "a");
1612 m_model
->setExpanded(0, true);
1613 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1614 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1616 // Verify that creating the MIME data for a child of an expanded folder does
1617 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1619 selection
.insert(1);
1620 QMimeData
* mimeData
= m_model
->createMimeData(selection
);
1624 void KFileItemModelTest::testCollapseFolderWhileLoading()
1626 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1627 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1628 m_model
->setRoles(modelRoles
);
1631 files
<< "a2/b/c1.txt";
1632 m_testDir
->createFiles(files
);
1634 m_model
->loadDirectory(m_testDir
->url());
1635 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1636 QCOMPARE(itemsInModel(), QStringList() << "a2");
1639 m_model
->setExpanded(0, true);
1640 QVERIFY(m_model
->isExpanded(0));
1641 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1642 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1645 m_model
->setExpanded(1, true);
1646 QVERIFY(m_model
->isExpanded(1));
1647 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1648 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1650 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1651 // signal is not emitted yet.
1652 const KFileItem fileItemC1
= m_model
->fileItem(2);
1653 KFileItem fileItemC2
= fileItemC1
;
1654 KUrl urlC2
= fileItemC2
.url();
1655 urlC2
.setFileName("c2.txt");
1656 fileItemC2
.setUrl(urlC2
);
1658 const KUrl urlB
= m_model
->fileItem(1).url();
1659 m_model
->slotItemsAdded(urlB
, KFileItemList() << fileItemC2
);
1660 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1662 // Collapse "a2/". This should also remove all its (indirect) children from
1663 // the model and from the model's m_pendingItemsToInsert member.
1664 m_model
->setExpanded(0, false);
1665 QCOMPARE(itemsInModel(), QStringList() << "a2");
1667 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1668 // is still in m_pendingItemsToInsert, then we might get a crash, see
1669 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1670 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1671 // without parent in the model.
1672 m_model
->slotCompleted();
1673 QCOMPARE(itemsInModel(), QStringList() << "a2");
1675 // Expand "a2/" again.
1676 m_model
->setExpanded(0, true);
1677 QVERIFY(m_model
->isExpanded(0));
1678 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1679 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1681 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1682 // completed() signal is not emitted yet.
1683 const KFileItem fileItemA2
= m_model
->fileItem(0);
1684 KFileItem fileItemA1
= fileItemA2
;
1685 KUrl urlA1
= fileItemA1
.url();
1686 urlA1
.setFileName("a1");
1687 fileItemA1
.setUrl(urlA1
);
1689 m_model
->slotItemsAdded(m_model
->directory(), KFileItemList() << fileItemA1
);
1690 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1692 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1693 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1694 // confuse the code which collapses the folder.
1695 m_model
->setExpanded(0, false);
1696 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1697 QVERIFY(!m_model
->isExpanded(0));
1698 QVERIFY(!m_model
->isExpanded(1));
1701 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1704 files
<< "a.txt" << "b.txt" << "c.txt" << "d.txt";
1705 m_testDir
->createFiles(files
);
1707 m_model
->loadDirectory(m_testDir
->url());
1708 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1709 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1711 const KFileItem fileItemB
= m_model
->fileItem(1);
1713 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1715 list
<< fileItemB
<< fileItemB
;
1716 m_model
->slotItemsDeleted(list
);
1718 QVERIFY(m_model
->isConsistent());
1719 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1722 QStringList
KFileItemModelTest::itemsInModel() const
1725 for (int i
= 0; i
< m_model
->count(); i
++) {
1726 items
<< m_model
->fileItem(i
).text();
1731 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1733 #include "kfileitemmodeltest.moc"