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(KItemRangeList
)
53 Q_DECLARE_METATYPE(QList
<int>)
55 class KFileItemModelTest
: public QObject
63 void testDefaultRoles();
64 void testDefaultSortRole();
65 void testDefaultGroupedSorting();
67 void testRemoveItems();
68 void testDirLoadingCompleted();
70 void testSetDataWithModifiedSortRole_data();
71 void testSetDataWithModifiedSortRole();
72 void testChangeSortRole();
73 void testModelConsistencyWhenInsertingItems();
74 void testItemRangeConsistencyWhenInsertingItems();
75 void testExpandItems();
76 void testExpandParentItems();
77 void testMakeExpandedItemHidden();
79 void testIndexForKeyboardSearch();
80 void testNameFilter();
82 void testRefreshExpandedItem();
83 void testRemoveHiddenItems();
84 void collapseParentOfHiddenItems();
85 void removeParentOfHiddenItems();
86 void testGeneralParentChildRelationships();
89 QStringList
itemsInModel() const;
92 KFileItemModel
* m_model
;
96 void KFileItemModelTest::init()
98 // The item-model tests result in a huge number of debugging
99 // output from kdelibs. Only show critical and fatal messages.
100 qInstallMsgHandler(myMessageOutput
);
102 qRegisterMetaType
<KItemRange
>("KItemRange");
103 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
104 qRegisterMetaType
<KFileItemList
>("KFileItemList");
106 m_testDir
= new TestDir();
107 m_model
= new KFileItemModel();
108 m_model
->m_dirLister
->setAutoUpdate(false);
110 // Reduce the timer interval to make the test run faster.
111 m_model
->m_resortAllItemsTimer
->setInterval(0);
114 void KFileItemModelTest::cleanup()
123 void KFileItemModelTest::testDefaultRoles()
125 const QSet
<QByteArray
> roles
= m_model
->roles();
126 QCOMPARE(roles
.count(), 3);
127 QVERIFY(roles
.contains("text"));
128 QVERIFY(roles
.contains("isDir"));
129 QVERIFY(roles
.contains("isLink"));
132 void KFileItemModelTest::testDefaultSortRole()
134 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
137 files
<< "c.txt" << "a.txt" << "b.txt";
139 m_testDir
->createFiles(files
);
141 m_model
->loadDirectory(m_testDir
->url());
142 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
144 QCOMPARE(m_model
->count(), 3);
145 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
146 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
147 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
150 void KFileItemModelTest::testDefaultGroupedSorting()
152 QCOMPARE(m_model
->groupedSorting(), false);
155 void KFileItemModelTest::testNewItems()
158 files
<< "a.txt" << "b.txt" << "c.txt";
159 m_testDir
->createFiles(files
);
161 m_model
->loadDirectory(m_testDir
->url());
162 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
164 QCOMPARE(m_model
->count(), 3);
166 QVERIFY(m_model
->isConsistent());
169 void KFileItemModelTest::testRemoveItems()
171 m_testDir
->createFile("a.txt");
172 m_testDir
->createFile("b.txt");
173 m_model
->loadDirectory(m_testDir
->url());
174 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
175 QCOMPARE(m_model
->count(), 2);
176 QVERIFY(m_model
->isConsistent());
178 m_testDir
->removeFile("a.txt");
179 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
180 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
181 QCOMPARE(m_model
->count(), 1);
182 QVERIFY(m_model
->isConsistent());
185 void KFileItemModelTest::testDirLoadingCompleted()
187 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
188 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
189 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
191 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
193 m_model
->loadDirectory(m_testDir
->url());
194 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
195 QCOMPARE(loadingCompletedSpy
.count(), 1);
196 QCOMPARE(itemsInsertedSpy
.count(), 1);
197 QCOMPARE(itemsRemovedSpy
.count(), 0);
198 QCOMPARE(m_model
->count(), 3);
200 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
201 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
202 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
203 QCOMPARE(loadingCompletedSpy
.count(), 2);
204 QCOMPARE(itemsInsertedSpy
.count(), 2);
205 QCOMPARE(itemsRemovedSpy
.count(), 0);
206 QCOMPARE(m_model
->count(), 5);
208 m_testDir
->removeFile("a.txt");
209 m_testDir
->createFile("f.txt");
210 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
211 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
212 QCOMPARE(loadingCompletedSpy
.count(), 3);
213 QCOMPARE(itemsInsertedSpy
.count(), 3);
214 QCOMPARE(itemsRemovedSpy
.count(), 1);
215 QCOMPARE(m_model
->count(), 5);
217 m_testDir
->removeFile("b.txt");
218 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
219 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
220 QCOMPARE(loadingCompletedSpy
.count(), 4);
221 QCOMPARE(itemsInsertedSpy
.count(), 3);
222 QCOMPARE(itemsRemovedSpy
.count(), 2);
223 QCOMPARE(m_model
->count(), 4);
225 QVERIFY(m_model
->isConsistent());
228 void KFileItemModelTest::testSetData()
230 m_testDir
->createFile("a.txt");
232 m_model
->loadDirectory(m_testDir
->url());
233 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
235 QHash
<QByteArray
, QVariant
> values
;
236 values
.insert("customRole1", "Test1");
237 values
.insert("customRole2", "Test2");
239 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
240 m_model
->setData(0, values
);
241 QCOMPARE(itemsChangedSpy
.count(), 1);
243 values
= m_model
->data(0);
244 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
245 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
246 QVERIFY(m_model
->isConsistent());
249 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
251 QTest::addColumn
<int>("changedIndex");
252 QTest::addColumn
<int>("changedRating");
253 QTest::addColumn
<bool>("expectMoveSignal");
254 QTest::addColumn
<int>("ratingIndex0");
255 QTest::addColumn
<int>("ratingIndex1");
256 QTest::addColumn
<int>("ratingIndex2");
259 // Index 0 = rating 2
260 // Index 1 = rating 4
261 // Index 2 = rating 6
263 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
264 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
265 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
267 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
268 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
269 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
272 void KFileItemModelTest::testSetDataWithModifiedSortRole()
274 QFETCH(int, changedIndex
);
275 QFETCH(int, changedRating
);
276 QFETCH(bool, expectMoveSignal
);
277 QFETCH(int, ratingIndex0
);
278 QFETCH(int, ratingIndex1
);
279 QFETCH(int, ratingIndex2
);
281 // Changing the value of a sort-role must result in
282 // a reordering of the items.
283 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
284 m_model
->setSortRole("rating");
285 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
288 files
<< "a.txt" << "b.txt" << "c.txt";
289 m_testDir
->createFiles(files
);
291 m_model
->loadDirectory(m_testDir
->url());
292 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
294 // Fill the "rating" role of each file:
299 QHash
<QByteArray
, QVariant
> ratingA
;
300 ratingA
.insert("rating", 2);
301 m_model
->setData(0, ratingA
);
303 QHash
<QByteArray
, QVariant
> ratingB
;
304 ratingB
.insert("rating", 4);
305 m_model
->setData(1, ratingB
);
307 QHash
<QByteArray
, QVariant
> ratingC
;
308 ratingC
.insert("rating", 6);
309 m_model
->setData(2, ratingC
);
311 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
312 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
313 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
315 // Now change the rating from a.txt. This usually results
316 // in reordering of the items.
317 QHash
<QByteArray
, QVariant
> rating
;
318 rating
.insert("rating", changedRating
);
319 m_model
->setData(changedIndex
, rating
);
321 if (expectMoveSignal
) {
322 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
325 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
326 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
327 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
328 QVERIFY(m_model
->isConsistent());
331 void KFileItemModelTest::testChangeSortRole()
333 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
336 files
<< "a.txt" << "b.jpg" << "c.txt";
337 m_testDir
->createFiles(files
);
339 m_model
->loadDirectory(m_testDir
->url());
340 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
341 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
343 // Simulate that KFileItemModelRolesUpdater determines the mime type.
344 // Resorting the files by 'type' will only work immediately if their
345 // mime types are known.
346 for (int index
= 0; index
< m_model
->count(); ++index
) {
347 m_model
->fileItem(index
).determineMimeType();
350 // Now: sort by type.
351 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
352 m_model
->setSortRole("type");
353 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
354 QVERIFY(!spyItemsMoved
.isEmpty());
356 // The actual order of the files might depend on the translation of the
357 // result of KFileItem::mimeComment() in the user's language.
358 QStringList version1
;
359 version1
<< "b.jpg" << "a.txt" << "c.txt";
361 QStringList version2
;
362 version2
<< "a.txt" << "c.txt" << "b.jpg";
364 const bool ok1
= (itemsInModel() == version1
);
365 const bool ok2
= (itemsInModel() == version2
);
370 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
372 //QSKIP("Temporary disabled", SkipSingle);
374 // KFileItemModel prevents that inserting a punch of items sequentially
375 // results in an itemsInserted()-signal for each item. Instead internally
376 // a timeout is given that collects such operations and results in only
377 // one itemsInserted()-signal. However in this test we want to stress
378 // KFileItemModel to do a lot of insert operation and hence decrease
379 // the timeout to 1 millisecond.
380 m_testDir
->createFile("1");
381 m_model
->loadDirectory(m_testDir
->url());
382 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
383 QCOMPARE(m_model
->count(), 1);
385 // Insert 10 items for 20 times. After each insert operation the model consistency
387 QSet
<int> insertedItems
;
388 for (int i
= 0; i
< 20; ++i
) {
389 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
391 for (int j
= 0; j
< 10; ++j
) {
392 int itemName
= qrand();
393 while (insertedItems
.contains(itemName
)) {
396 insertedItems
.insert(itemName
);
398 m_testDir
->createFile(QString::number(itemName
));
401 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
402 if (spy
.count() == 0) {
403 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
406 QVERIFY(m_model
->isConsistent());
409 QCOMPARE(m_model
->count(), 201);
412 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
415 files
<< "B" << "E" << "G";
416 m_testDir
->createFiles(files
);
418 // Due to inserting the 3 items one item-range with index == 0 and
419 // count == 3 must be given
420 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
421 m_model
->loadDirectory(m_testDir
->url());
422 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
424 QCOMPARE(spy1
.count(), 1);
425 QList
<QVariant
> arguments
= spy1
.takeFirst();
426 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
427 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
429 // The indexes of the item-ranges must always be related to the model before
430 // the items have been inserted. Having:
433 // and inserting A, C, D, F the resulting model will be:
436 // and the item-ranges must be:
437 // index: 0, count: 1 for A
438 // index: 1, count: 2 for B, C
439 // index: 2, count: 1 for G
442 files
<< "A" << "C" << "D" << "F";
443 m_testDir
->createFiles(files
);
445 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
446 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
447 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
449 QCOMPARE(spy2
.count(), 1);
450 arguments
= spy2
.takeFirst();
451 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
452 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
455 void KFileItemModelTest::testExpandItems()
457 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
458 // Besides testing the basic item expansion functionality, the test makes sure that
459 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
460 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
461 // first three characters.
462 QSet
<QByteArray
> modelRoles
= m_model
->roles();
463 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
464 m_model
->setRoles(modelRoles
);
467 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
468 m_testDir
->createFiles(files
);
470 // Store the URLs of all folders in a set.
471 QSet
<KUrl
> allFolders
;
472 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
474 m_model
->loadDirectory(m_testDir
->url());
475 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
477 // So far, the model contains only "a/"
478 QCOMPARE(m_model
->count(), 1);
479 QVERIFY(m_model
->isExpandable(0));
480 QVERIFY(!m_model
->isExpanded(0));
481 QVERIFY(m_model
->expandedDirectories().empty());
483 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
485 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
486 m_model
->setExpanded(0, true);
487 QVERIFY(m_model
->isExpanded(0));
488 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
489 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
490 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
492 QCOMPARE(spyInserted
.count(), 1);
493 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
494 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
496 QVERIFY(m_model
->isExpandable(1));
497 QVERIFY(!m_model
->isExpanded(1));
498 QVERIFY(m_model
->isExpandable(2));
499 QVERIFY(!m_model
->isExpanded(2));
501 // Expand the folder "a/a/" -> "a/a/1" becomes visible
502 m_model
->setExpanded(1, true);
503 QVERIFY(m_model
->isExpanded(1));
504 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
505 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
506 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
508 QCOMPARE(spyInserted
.count(), 1);
509 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
510 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
512 QVERIFY(!m_model
->isExpandable(2));
513 QVERIFY(!m_model
->isExpanded(2));
515 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
516 m_model
->setExpanded(3, true);
517 QVERIFY(m_model
->isExpanded(3));
518 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
519 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
520 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
522 QCOMPARE(spyInserted
.count(), 1);
523 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
524 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
526 QVERIFY(!m_model
->isExpandable(4));
527 QVERIFY(!m_model
->isExpanded(4));
529 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
531 // Collapse the top-level folder -> all other items should disappear
532 m_model
->setExpanded(0, false);
533 QVERIFY(!m_model
->isExpanded(0));
534 QCOMPARE(m_model
->count(), 1);
535 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
537 QCOMPARE(spyRemoved
.count(), 1);
538 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
539 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
540 QVERIFY(m_model
->isConsistent());
542 // Clear the model, reload the folder and try to restore the expanded folders.
544 QCOMPARE(m_model
->count(), 0);
545 QVERIFY(m_model
->expandedDirectories().empty());
547 m_model
->loadDirectory(m_testDir
->url());
548 m_model
->restoreExpandedDirectories(allFolders
);
549 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
550 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
551 QVERIFY(m_model
->isExpanded(0));
552 QVERIFY(m_model
->isExpanded(1));
553 QVERIFY(!m_model
->isExpanded(2));
554 QVERIFY(m_model
->isExpanded(3));
555 QVERIFY(!m_model
->isExpanded(4));
556 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
557 QVERIFY(m_model
->isConsistent());
559 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
560 // This is how DolphinView restores the expanded folders when navigating in history.
561 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
562 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
563 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
564 m_model
->restoreExpandedDirectories(allFolders
);
565 m_model
->loadDirectory(m_testDir
->url());
566 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
567 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
568 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
571 void KFileItemModelTest::testExpandParentItems()
573 // Create a tree structure of folders:
581 QSet
<QByteArray
> modelRoles
= m_model
->roles();
582 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
583 m_model
->setRoles(modelRoles
);
586 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
587 m_testDir
->createFiles(files
);
589 m_model
->loadDirectory(m_testDir
->url());
590 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
592 // So far, the model contains only "a 1/" and "a2/".
593 QCOMPARE(m_model
->count(), 2);
594 QVERIFY(m_model
->expandedDirectories().empty());
596 // Expand the parents of "a2/b2/c2".
597 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
598 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
600 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
601 // It's important that only the parents of "a1/b1/c1" are expanded.
602 QCOMPARE(m_model
->count(), 4);
603 QVERIFY(!m_model
->isExpanded(0));
604 QVERIFY(m_model
->isExpanded(1));
605 QVERIFY(m_model
->isExpanded(2));
606 QVERIFY(!m_model
->isExpanded(3));
608 // Expand the parents of "a 1/b1".
609 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
610 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
612 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
613 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
614 QCOMPARE(m_model
->count(), 5);
615 QVERIFY(m_model
->isExpanded(0));
616 QVERIFY(!m_model
->isExpanded(1));
617 QVERIFY(m_model
->isExpanded(2));
618 QVERIFY(m_model
->isExpanded(3));
619 QVERIFY(!m_model
->isExpanded(4));
620 QVERIFY(m_model
->isConsistent());
624 * Renaming an expanded folder by prepending its name with a dot makes it
625 * hidden. Verify that this does not cause an inconsistent model state and
626 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
628 void KFileItemModelTest::testMakeExpandedItemHidden()
630 QSet
<QByteArray
> modelRoles
= m_model
->roles();
631 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
632 m_model
->setRoles(modelRoles
);
635 m_testDir
->createFile("1a/2a/3a");
636 m_testDir
->createFile("1a/2a/3b");
637 m_testDir
->createFile("1a/2b");
638 m_testDir
->createFile("1b");
640 m_model
->loadDirectory(m_testDir
->url());
641 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
643 // So far, the model contains only "1a/" and "1b".
644 QCOMPARE(m_model
->count(), 2);
645 m_model
->setExpanded(0, true);
646 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
648 // Now "1a/2a" and "1a/2b" have appeared.
649 QCOMPARE(m_model
->count(), 4);
650 m_model
->setExpanded(1, true);
651 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
652 QCOMPARE(m_model
->count(), 6);
654 // Rename "1a/2" and make it hidden.
655 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
656 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
658 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
659 bool ok
= job
->exec();
661 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
663 // "1a/2" and its subfolders have disappeared now.
664 QVERIFY(m_model
->isConsistent());
665 QCOMPARE(m_model
->count(), 3);
667 m_model
->setExpanded(0, false);
668 QCOMPARE(m_model
->count(), 2);
672 void KFileItemModelTest::testSorting()
674 // Create some files with different sizes and modification times to check the different sorting options
675 QDateTime now
= QDateTime::currentDateTime();
677 QSet
<QByteArray
> roles
;
678 roles
.insert("text");
679 roles
.insert("isExpanded");
680 roles
.insert("isExpandable");
681 roles
.insert("expandedParentsCount");
682 m_model
->setRoles(roles
);
684 m_testDir
->createDir("c/c-2");
685 m_testDir
->createFile("c/c-2/c-3");
686 m_testDir
->createFile("c/c-1");
688 m_testDir
->createFile("a", "A file", now
.addDays(-3));
689 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
690 m_testDir
->createDir("c", now
.addDays(-2));
691 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
692 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
693 m_testDir
->createFile(".f");
695 m_model
->loadDirectory(m_testDir
->url());
696 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
698 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
699 m_model
->setExpanded(index
, true);
700 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
702 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
703 m_model
->setExpanded(index
, true);
704 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
706 // Default: Sort by Name, ascending
707 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
708 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
709 QVERIFY(m_model
->sortDirectoriesFirst());
710 QVERIFY(!m_model
->showHiddenFiles());
711 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
713 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
715 // Sort by Name, ascending, 'Sort Folders First' disabled
716 m_model
->setSortDirectoriesFirst(false);
717 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
718 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
719 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
720 QCOMPARE(spyItemsMoved
.count(), 1);
721 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
723 // Sort by Name, descending
724 m_model
->setSortDirectoriesFirst(true);
725 m_model
->setSortOrder(Qt::DescendingOrder
);
726 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
727 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
728 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
729 QCOMPARE(spyItemsMoved
.count(), 2);
730 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
731 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
733 // Sort by Date, descending
734 m_model
->setSortDirectoriesFirst(true);
735 m_model
->setSortRole("date");
736 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
737 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
738 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
739 QCOMPARE(spyItemsMoved
.count(), 1);
740 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
742 // Sort by Date, ascending
743 m_model
->setSortOrder(Qt::AscendingOrder
);
744 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
745 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
746 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
747 QCOMPARE(spyItemsMoved
.count(), 1);
748 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
750 // Sort by Date, ascending, 'Sort Folders First' disabled
751 m_model
->setSortDirectoriesFirst(false);
752 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
753 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
754 QVERIFY(!m_model
->sortDirectoriesFirst());
755 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
756 QCOMPARE(spyItemsMoved
.count(), 1);
757 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
759 // Sort by Name, ascending, 'Sort Folders First' disabled
760 m_model
->setSortRole("text");
761 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
762 QVERIFY(!m_model
->sortDirectoriesFirst());
763 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
764 QCOMPARE(spyItemsMoved
.count(), 1);
765 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
767 // Sort by Size, ascending, 'Sort Folders First' disabled
768 m_model
->setSortRole("size");
769 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
770 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
771 QVERIFY(!m_model
->sortDirectoriesFirst());
772 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
773 QCOMPARE(spyItemsMoved
.count(), 1);
774 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
776 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
777 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
778 "another signal", SkipSingle
);
779 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
781 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
782 m_model
->setSortDirectoriesFirst(true);
783 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
784 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
785 QVERIFY(m_model
->sortDirectoriesFirst());
786 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
787 QCOMPARE(spyItemsMoved
.count(), 0);
789 // Sort by Size, descending, 'Sort Folders First' enabled
790 m_model
->setSortOrder(Qt::DescendingOrder
);
791 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
792 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
793 QVERIFY(m_model
->sortDirectoriesFirst());
794 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
795 QCOMPARE(spyItemsMoved
.count(), 1);
796 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
798 // TODO: Sort by other roles; show/hide hidden files
801 void KFileItemModelTest::testIndexForKeyboardSearch()
804 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
805 m_testDir
->createFiles(files
);
807 m_model
->loadDirectory(m_testDir
->url());
808 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
810 // Search from index 0
811 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
812 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
813 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
814 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
815 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
816 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
817 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
818 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
819 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
820 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
821 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
823 // Start a search somewhere in the middle
824 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
825 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
826 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
827 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
829 // Test searches that go past the last item back to index 0
830 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
831 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
832 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
833 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
835 // Test searches that yield no result
836 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
837 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
838 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
839 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
840 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
842 // Test upper case searches (note that search is case insensitive)
843 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
844 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
845 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
846 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
848 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
851 void KFileItemModelTest::testNameFilter()
854 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
855 m_testDir
->createFiles(files
);
857 m_model
->loadDirectory(m_testDir
->url());
858 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
860 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
861 QCOMPARE(m_model
->count(), 3);
863 m_model
->setNameFilter("A2"); // Shows only A2
864 QCOMPARE(m_model
->count(), 1);
866 m_model
->setNameFilter("A2"); // Shows only A1
867 QCOMPARE(m_model
->count(), 1);
869 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
870 QCOMPARE(m_model
->count(), 2);
872 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
873 QCOMPARE(m_model
->count(), 2);
875 m_model
->setNameFilter(QString()); // Shows again all items
876 QCOMPARE(m_model
->count(), 5);
880 * Verifies that we do not crash when adding a KFileItem with an empty path.
881 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
882 * tried to always read the first character of the path, even if the path is empty.
884 void KFileItemModelTest::testEmptyPath()
886 QSet
<QByteArray
> roles
;
887 roles
.insert("text");
888 roles
.insert("isExpanded");
889 roles
.insert("isExpandable");
890 roles
.insert("expandedParentsCount");
891 m_model
->setRoles(roles
);
894 QVERIFY(emptyUrl
.path().isEmpty());
896 const KUrl
url("file:///test/");
899 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
900 m_model
->slotItemsAdded(emptyUrl
, items
);
901 m_model
->slotCompleted();
905 * Verifies that the 'isExpanded' state of folders does not change when the
906 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
908 void KFileItemModelTest::testRefreshExpandedItem()
910 QSet
<QByteArray
> modelRoles
= m_model
->roles();
911 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
912 m_model
->setRoles(modelRoles
);
915 files
<< "a/1" << "a/2" << "3" << "4";
916 m_testDir
->createFiles(files
);
918 m_model
->loadDirectory(m_testDir
->url());
919 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
920 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
922 m_model
->setExpanded(0, true);
923 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
924 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
925 QVERIFY(m_model
->isExpanded(0));
927 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
929 const KFileItem item
= m_model
->fileItem(0);
930 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
931 QVERIFY(!spyItemsChanged
.isEmpty());
933 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
934 QVERIFY(m_model
->isExpanded(0));
938 * Verify that removing hidden files and folders from the model does not
939 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
941 void KFileItemModelTest::testRemoveHiddenItems()
943 m_testDir
->createDir(".a");
944 m_testDir
->createDir(".b");
945 m_testDir
->createDir("c");
946 m_testDir
->createDir("d");
947 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
949 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
950 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
952 m_model
->setShowHiddenFiles(true);
953 m_model
->loadDirectory(m_testDir
->url());
954 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
955 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
956 QCOMPARE(spyItemsInserted
.count(), 1);
957 QCOMPARE(spyItemsRemoved
.count(), 0);
958 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
959 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
961 m_model
->setShowHiddenFiles(false);
962 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
963 QCOMPARE(spyItemsInserted
.count(), 0);
964 QCOMPARE(spyItemsRemoved
.count(), 1);
965 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
966 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
968 m_model
->setShowHiddenFiles(true);
969 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
970 QCOMPARE(spyItemsInserted
.count(), 1);
971 QCOMPARE(spyItemsRemoved
.count(), 0);
972 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
973 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
976 QCOMPARE(itemsInModel(), QStringList());
977 QCOMPARE(spyItemsInserted
.count(), 0);
978 QCOMPARE(spyItemsRemoved
.count(), 1);
979 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
980 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
982 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
983 // Verify that this does not make the model crash.
984 m_model
->setShowHiddenFiles(false);
988 * Verify that filtered items are removed when their parent is collapsed.
990 void KFileItemModelTest::collapseParentOfHiddenItems()
992 QSet
<QByteArray
> modelRoles
= m_model
->roles();
993 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
994 m_model
->setRoles(modelRoles
);
997 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
998 m_testDir
->createFiles(files
);
1000 m_model
->loadDirectory(m_testDir
->url());
1001 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1002 QCOMPARE(m_model
->count(), 1); // Only "a/"
1005 m_model
->setExpanded(0, true);
1006 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1007 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1010 m_model
->setExpanded(1, true);
1011 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1012 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1015 m_model
->setExpanded(2, true);
1016 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1017 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"
1019 // Set a name filter that matches nothing -> only the expanded folders remain.
1020 m_model
->setNameFilter("xyz");
1021 QCOMPARE(m_model
->count(), 3);
1022 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1024 // Collapse the folder "a/".
1025 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1026 m_model
->setExpanded(0, false);
1027 QCOMPARE(spyItemsRemoved
.count(), 1);
1028 QCOMPARE(m_model
->count(), 1);
1029 QCOMPARE(itemsInModel(), QStringList() << "a");
1031 // Remove the filter -> no files should appear (and we should not get a crash).
1032 m_model
->setNameFilter(QString());
1033 QCOMPARE(m_model
->count(), 1);
1037 * Verify that filtered items are removed when their parent is deleted.
1039 void KFileItemModelTest::removeParentOfHiddenItems()
1041 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1042 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1043 m_model
->setRoles(modelRoles
);
1046 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1047 m_testDir
->createFiles(files
);
1049 m_model
->loadDirectory(m_testDir
->url());
1050 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1051 QCOMPARE(m_model
->count(), 1); // Only "a/"
1054 m_model
->setExpanded(0, true);
1055 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1056 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1059 m_model
->setExpanded(1, true);
1060 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1061 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1064 m_model
->setExpanded(2, true);
1065 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1066 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"
1068 // Set a name filter that matches nothing -> only the expanded folders remain.
1069 m_model
->setNameFilter("xyz");
1070 QCOMPARE(m_model
->count(), 3);
1071 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1073 // Simulate the deletion of the directory "a/b/".
1074 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1075 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1076 QCOMPARE(spyItemsRemoved
.count(), 1);
1077 QCOMPARE(m_model
->count(), 1);
1078 QCOMPARE(itemsInModel(), QStringList() << "a");
1080 // Remove the filter -> only the file "a/1" should appear.
1081 m_model
->setNameFilter(QString());
1082 QCOMPARE(m_model
->count(), 2);
1083 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1087 * Create a tree structure where parent-child relationships can not be
1088 * determined by parsing the URLs, and verify that KFileItemModel
1089 * handles them correctly.
1091 void KFileItemModelTest::testGeneralParentChildRelationships()
1093 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1094 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1095 m_model
->setRoles(modelRoles
);
1098 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1099 m_testDir
->createFiles(files
);
1101 m_model
->loadDirectory(m_testDir
->url());
1102 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1103 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1105 // Expand all folders.
1106 m_model
->setExpanded(0, true);
1107 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1108 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1110 m_model
->setExpanded(1, true);
1111 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1112 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1114 m_model
->setExpanded(3, true);
1115 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1116 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1118 m_model
->setExpanded(4, true);
1119 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1120 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1122 // Add some more children and grand-children.
1123 const KUrl parent1
= m_model
->fileItem(0).url();
1124 const KUrl parent2
= m_model
->fileItem(3).url();
1125 const KUrl realChild1
= m_model
->fileItem(1).url();
1126 const KUrl realChild2
= m_model
->fileItem(4).url();
1128 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1129 m_model
->slotCompleted();
1130 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1132 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1133 m_model
->slotCompleted();
1134 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1136 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1137 m_model
->slotCompleted();
1138 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1140 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1141 m_model
->slotCompleted();
1142 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1144 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1145 m_model
->slotCompleted();
1146 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1148 // Set a name filter that matches nothing -> only expanded folders remain.
1149 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1150 m_model
->setNameFilter("xyz");
1151 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1152 QCOMPARE(itemsRemovedSpy
.count(), 1);
1153 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1154 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1155 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1157 // Collapse "parent1".
1158 m_model
->setExpanded(0, false);
1159 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1160 QCOMPARE(itemsRemovedSpy
.count(), 1);
1161 arguments
= itemsRemovedSpy
.takeFirst();
1162 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1163 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1165 // Remove "parent2".
1166 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1167 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1168 QCOMPARE(itemsRemovedSpy
.count(), 1);
1169 arguments
= itemsRemovedSpy
.takeFirst();
1170 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1171 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1173 // Clear filter, verify that no items reappear.
1174 m_model
->setNameFilter(QString());
1175 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1178 QStringList
KFileItemModelTest::itemsInModel() const
1181 for (int i
= 0; i
< m_model
->count(); i
++) {
1182 items
<< m_model
->data(i
).value("text").toString();
1187 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1189 #include "kfileitemmodeltest.moc"