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);
111 void KFileItemModelTest::cleanup()
120 void KFileItemModelTest::testDefaultRoles()
122 const QSet
<QByteArray
> roles
= m_model
->roles();
123 QCOMPARE(roles
.count(), 3);
124 QVERIFY(roles
.contains("text"));
125 QVERIFY(roles
.contains("isDir"));
126 QVERIFY(roles
.contains("isLink"));
129 void KFileItemModelTest::testDefaultSortRole()
131 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
134 files
<< "c.txt" << "a.txt" << "b.txt";
136 m_testDir
->createFiles(files
);
138 m_model
->loadDirectory(m_testDir
->url());
139 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
141 QCOMPARE(m_model
->count(), 3);
142 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
143 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
144 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
147 void KFileItemModelTest::testDefaultGroupedSorting()
149 QCOMPARE(m_model
->groupedSorting(), false);
152 void KFileItemModelTest::testNewItems()
155 files
<< "a.txt" << "b.txt" << "c.txt";
156 m_testDir
->createFiles(files
);
158 m_model
->loadDirectory(m_testDir
->url());
159 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
161 QCOMPARE(m_model
->count(), 3);
163 QVERIFY(m_model
->isConsistent());
166 void KFileItemModelTest::testRemoveItems()
168 m_testDir
->createFile("a.txt");
169 m_testDir
->createFile("b.txt");
170 m_model
->loadDirectory(m_testDir
->url());
171 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
172 QCOMPARE(m_model
->count(), 2);
173 QVERIFY(m_model
->isConsistent());
175 m_testDir
->removeFile("a.txt");
176 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
177 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
178 QCOMPARE(m_model
->count(), 1);
179 QVERIFY(m_model
->isConsistent());
182 void KFileItemModelTest::testDirLoadingCompleted()
184 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
185 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
186 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
188 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
190 m_model
->loadDirectory(m_testDir
->url());
191 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
192 QCOMPARE(loadingCompletedSpy
.count(), 1);
193 QCOMPARE(itemsInsertedSpy
.count(), 1);
194 QCOMPARE(itemsRemovedSpy
.count(), 0);
195 QCOMPARE(m_model
->count(), 3);
197 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
198 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
199 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
200 QCOMPARE(loadingCompletedSpy
.count(), 2);
201 QCOMPARE(itemsInsertedSpy
.count(), 2);
202 QCOMPARE(itemsRemovedSpy
.count(), 0);
203 QCOMPARE(m_model
->count(), 5);
205 m_testDir
->removeFile("a.txt");
206 m_testDir
->createFile("f.txt");
207 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
208 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
209 QCOMPARE(loadingCompletedSpy
.count(), 3);
210 QCOMPARE(itemsInsertedSpy
.count(), 3);
211 QCOMPARE(itemsRemovedSpy
.count(), 1);
212 QCOMPARE(m_model
->count(), 5);
214 m_testDir
->removeFile("b.txt");
215 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
216 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
217 QCOMPARE(loadingCompletedSpy
.count(), 4);
218 QCOMPARE(itemsInsertedSpy
.count(), 3);
219 QCOMPARE(itemsRemovedSpy
.count(), 2);
220 QCOMPARE(m_model
->count(), 4);
222 QVERIFY(m_model
->isConsistent());
225 void KFileItemModelTest::testSetData()
227 m_testDir
->createFile("a.txt");
229 m_model
->loadDirectory(m_testDir
->url());
230 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
232 QHash
<QByteArray
, QVariant
> values
;
233 values
.insert("customRole1", "Test1");
234 values
.insert("customRole2", "Test2");
236 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
237 m_model
->setData(0, values
);
238 QCOMPARE(itemsChangedSpy
.count(), 1);
240 values
= m_model
->data(0);
241 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
242 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
243 QVERIFY(m_model
->isConsistent());
246 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
248 QTest::addColumn
<int>("changedIndex");
249 QTest::addColumn
<int>("changedRating");
250 QTest::addColumn
<bool>("expectMoveSignal");
251 QTest::addColumn
<int>("ratingIndex0");
252 QTest::addColumn
<int>("ratingIndex1");
253 QTest::addColumn
<int>("ratingIndex2");
256 // Index 0 = rating 2
257 // Index 1 = rating 4
258 // Index 2 = rating 6
260 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
261 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
262 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
264 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
265 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
266 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
269 void KFileItemModelTest::testSetDataWithModifiedSortRole()
271 QFETCH(int, changedIndex
);
272 QFETCH(int, changedRating
);
273 QFETCH(bool, expectMoveSignal
);
274 QFETCH(int, ratingIndex0
);
275 QFETCH(int, ratingIndex1
);
276 QFETCH(int, ratingIndex2
);
278 // Changing the value of a sort-role must result in
279 // a reordering of the items.
280 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
281 m_model
->setSortRole("rating");
282 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
285 files
<< "a.txt" << "b.txt" << "c.txt";
286 m_testDir
->createFiles(files
);
288 m_model
->loadDirectory(m_testDir
->url());
289 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
291 // Fill the "rating" role of each file:
296 QHash
<QByteArray
, QVariant
> ratingA
;
297 ratingA
.insert("rating", 2);
298 m_model
->setData(0, ratingA
);
300 QHash
<QByteArray
, QVariant
> ratingB
;
301 ratingB
.insert("rating", 4);
302 m_model
->setData(1, ratingB
);
304 QHash
<QByteArray
, QVariant
> ratingC
;
305 ratingC
.insert("rating", 6);
306 m_model
->setData(2, ratingC
);
308 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
309 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
310 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
312 // Now change the rating from a.txt. This usually results
313 // in reordering of the items.
314 QHash
<QByteArray
, QVariant
> rating
;
315 rating
.insert("rating", changedRating
);
316 m_model
->setData(changedIndex
, rating
);
318 if (expectMoveSignal
) {
319 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
322 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
323 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
324 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
325 QVERIFY(m_model
->isConsistent());
328 void KFileItemModelTest::testChangeSortRole()
330 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
333 files
<< "a.txt" << "b.jpg" << "c.txt";
334 m_testDir
->createFiles(files
);
336 m_model
->loadDirectory(m_testDir
->url());
337 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
338 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
340 // Simulate that KFileItemModelRolesUpdater determines the mime type.
341 // Resorting the files by 'type' will only work immediately if their
342 // mime types are known.
343 for (int index
= 0; index
< m_model
->count(); ++index
) {
344 m_model
->fileItem(index
).determineMimeType();
347 // Now: sort by type.
348 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
349 m_model
->setSortRole("type");
350 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
351 QVERIFY(!spyItemsMoved
.isEmpty());
353 // The actual order of the files might depend on the translation of the
354 // result of KFileItem::mimeComment() in the user's language.
355 QStringList version1
;
356 version1
<< "b.jpg" << "a.txt" << "c.txt";
358 QStringList version2
;
359 version2
<< "a.txt" << "c.txt" << "b.jpg";
361 const bool ok1
= (itemsInModel() == version1
);
362 const bool ok2
= (itemsInModel() == version2
);
367 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
369 //QSKIP("Temporary disabled", SkipSingle);
371 // KFileItemModel prevents that inserting a punch of items sequentially
372 // results in an itemsInserted()-signal for each item. Instead internally
373 // a timeout is given that collects such operations and results in only
374 // one itemsInserted()-signal. However in this test we want to stress
375 // KFileItemModel to do a lot of insert operation and hence decrease
376 // the timeout to 1 millisecond.
377 m_testDir
->createFile("1");
378 m_model
->loadDirectory(m_testDir
->url());
379 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
380 QCOMPARE(m_model
->count(), 1);
382 // Insert 10 items for 20 times. After each insert operation the model consistency
384 QSet
<int> insertedItems
;
385 for (int i
= 0; i
< 20; ++i
) {
386 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
388 for (int j
= 0; j
< 10; ++j
) {
389 int itemName
= qrand();
390 while (insertedItems
.contains(itemName
)) {
393 insertedItems
.insert(itemName
);
395 m_testDir
->createFile(QString::number(itemName
));
398 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
399 if (spy
.count() == 0) {
400 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
403 QVERIFY(m_model
->isConsistent());
406 QCOMPARE(m_model
->count(), 201);
409 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
412 files
<< "B" << "E" << "G";
413 m_testDir
->createFiles(files
);
415 // Due to inserting the 3 items one item-range with index == 0 and
416 // count == 3 must be given
417 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
418 m_model
->loadDirectory(m_testDir
->url());
419 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
421 QCOMPARE(spy1
.count(), 1);
422 QList
<QVariant
> arguments
= spy1
.takeFirst();
423 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
424 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
426 // The indexes of the item-ranges must always be related to the model before
427 // the items have been inserted. Having:
430 // and inserting A, C, D, F the resulting model will be:
433 // and the item-ranges must be:
434 // index: 0, count: 1 for A
435 // index: 1, count: 2 for B, C
436 // index: 2, count: 1 for G
439 files
<< "A" << "C" << "D" << "F";
440 m_testDir
->createFiles(files
);
442 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
443 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
444 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
446 QCOMPARE(spy2
.count(), 1);
447 arguments
= spy2
.takeFirst();
448 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
449 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
452 void KFileItemModelTest::testExpandItems()
454 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
455 // Besides testing the basic item expansion functionality, the test makes sure that
456 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
457 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
458 // first three characters.
459 QSet
<QByteArray
> modelRoles
= m_model
->roles();
460 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
461 m_model
->setRoles(modelRoles
);
464 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
465 m_testDir
->createFiles(files
);
467 // Store the URLs of all folders in a set.
468 QSet
<KUrl
> allFolders
;
469 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
471 m_model
->loadDirectory(m_testDir
->url());
472 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
474 // So far, the model contains only "a/"
475 QCOMPARE(m_model
->count(), 1);
476 QVERIFY(m_model
->isExpandable(0));
477 QVERIFY(!m_model
->isExpanded(0));
478 QVERIFY(m_model
->expandedDirectories().empty());
480 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
482 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
483 m_model
->setExpanded(0, true);
484 QVERIFY(m_model
->isExpanded(0));
485 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
486 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
487 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
489 QCOMPARE(spyInserted
.count(), 1);
490 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
491 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
493 QVERIFY(m_model
->isExpandable(1));
494 QVERIFY(!m_model
->isExpanded(1));
495 QVERIFY(m_model
->isExpandable(2));
496 QVERIFY(!m_model
->isExpanded(2));
498 // Expand the folder "a/a/" -> "a/a/1" becomes visible
499 m_model
->setExpanded(1, true);
500 QVERIFY(m_model
->isExpanded(1));
501 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
502 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
503 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
505 QCOMPARE(spyInserted
.count(), 1);
506 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
507 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
509 QVERIFY(!m_model
->isExpandable(2));
510 QVERIFY(!m_model
->isExpanded(2));
512 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
513 m_model
->setExpanded(3, true);
514 QVERIFY(m_model
->isExpanded(3));
515 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
516 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
517 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
519 QCOMPARE(spyInserted
.count(), 1);
520 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
521 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
523 QVERIFY(!m_model
->isExpandable(4));
524 QVERIFY(!m_model
->isExpanded(4));
526 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
528 // Collapse the top-level folder -> all other items should disappear
529 m_model
->setExpanded(0, false);
530 QVERIFY(!m_model
->isExpanded(0));
531 QCOMPARE(m_model
->count(), 1);
532 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
534 QCOMPARE(spyRemoved
.count(), 1);
535 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
536 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
537 QVERIFY(m_model
->isConsistent());
539 // Clear the model, reload the folder and try to restore the expanded folders.
541 QCOMPARE(m_model
->count(), 0);
542 QVERIFY(m_model
->expandedDirectories().empty());
544 m_model
->loadDirectory(m_testDir
->url());
545 m_model
->restoreExpandedDirectories(allFolders
);
546 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
547 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
548 QVERIFY(m_model
->isExpanded(0));
549 QVERIFY(m_model
->isExpanded(1));
550 QVERIFY(!m_model
->isExpanded(2));
551 QVERIFY(m_model
->isExpanded(3));
552 QVERIFY(!m_model
->isExpanded(4));
553 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
554 QVERIFY(m_model
->isConsistent());
556 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
557 // This is how DolphinView restores the expanded folders when navigating in history.
558 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
559 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
560 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
561 m_model
->restoreExpandedDirectories(allFolders
);
562 m_model
->loadDirectory(m_testDir
->url());
563 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
564 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
565 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
568 void KFileItemModelTest::testExpandParentItems()
570 // Create a tree structure of folders:
578 QSet
<QByteArray
> modelRoles
= m_model
->roles();
579 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
580 m_model
->setRoles(modelRoles
);
583 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
584 m_testDir
->createFiles(files
);
586 m_model
->loadDirectory(m_testDir
->url());
587 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
589 // So far, the model contains only "a 1/" and "a2/".
590 QCOMPARE(m_model
->count(), 2);
591 QVERIFY(m_model
->expandedDirectories().empty());
593 // Expand the parents of "a2/b2/c2".
594 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
595 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
597 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
598 // It's important that only the parents of "a1/b1/c1" are expanded.
599 QCOMPARE(m_model
->count(), 4);
600 QVERIFY(!m_model
->isExpanded(0));
601 QVERIFY(m_model
->isExpanded(1));
602 QVERIFY(m_model
->isExpanded(2));
603 QVERIFY(!m_model
->isExpanded(3));
605 // Expand the parents of "a 1/b1".
606 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
607 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
609 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
610 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
611 QCOMPARE(m_model
->count(), 5);
612 QVERIFY(m_model
->isExpanded(0));
613 QVERIFY(!m_model
->isExpanded(1));
614 QVERIFY(m_model
->isExpanded(2));
615 QVERIFY(m_model
->isExpanded(3));
616 QVERIFY(!m_model
->isExpanded(4));
617 QVERIFY(m_model
->isConsistent());
621 * Renaming an expanded folder by prepending its name with a dot makes it
622 * hidden. Verify that this does not cause an inconsistent model state and
623 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
625 void KFileItemModelTest::testMakeExpandedItemHidden()
627 QSet
<QByteArray
> modelRoles
= m_model
->roles();
628 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
629 m_model
->setRoles(modelRoles
);
632 m_testDir
->createFile("1a/2a/3a");
633 m_testDir
->createFile("1a/2a/3b");
634 m_testDir
->createFile("1a/2b");
635 m_testDir
->createFile("1b");
637 m_model
->loadDirectory(m_testDir
->url());
638 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
640 // So far, the model contains only "1a/" and "1b".
641 QCOMPARE(m_model
->count(), 2);
642 m_model
->setExpanded(0, true);
643 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
645 // Now "1a/2a" and "1a/2b" have appeared.
646 QCOMPARE(m_model
->count(), 4);
647 m_model
->setExpanded(1, true);
648 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
649 QCOMPARE(m_model
->count(), 6);
651 // Rename "1a/2" and make it hidden.
652 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
653 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
655 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
656 bool ok
= job
->exec();
658 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
660 // "1a/2" and its subfolders have disappeared now.
661 QVERIFY(m_model
->isConsistent());
662 QCOMPARE(m_model
->count(), 3);
664 m_model
->setExpanded(0, false);
665 QCOMPARE(m_model
->count(), 2);
669 void KFileItemModelTest::testSorting()
671 // Create some files with different sizes and modification times to check the different sorting options
672 QDateTime now
= QDateTime::currentDateTime();
674 QSet
<QByteArray
> roles
;
675 roles
.insert("text");
676 roles
.insert("isExpanded");
677 roles
.insert("isExpandable");
678 roles
.insert("expandedParentsCount");
679 m_model
->setRoles(roles
);
681 m_testDir
->createDir("c/c-2");
682 m_testDir
->createFile("c/c-2/c-3");
683 m_testDir
->createFile("c/c-1");
685 m_testDir
->createFile("a", "A file", now
.addDays(-3));
686 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
687 m_testDir
->createDir("c", now
.addDays(-2));
688 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
689 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
690 m_testDir
->createFile(".f");
692 m_model
->loadDirectory(m_testDir
->url());
693 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
695 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
696 m_model
->setExpanded(index
, true);
697 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
699 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
700 m_model
->setExpanded(index
, true);
701 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
703 // Default: Sort by Name, ascending
704 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
705 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
706 QVERIFY(m_model
->sortDirectoriesFirst());
707 QVERIFY(!m_model
->showHiddenFiles());
708 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
710 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
712 // Sort by Name, ascending, 'Sort Folders First' disabled
713 m_model
->setSortDirectoriesFirst(false);
714 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
715 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
716 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
717 QCOMPARE(spyItemsMoved
.count(), 1);
718 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
720 // Sort by Name, descending
721 m_model
->setSortDirectoriesFirst(true);
722 m_model
->setSortOrder(Qt::DescendingOrder
);
723 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
724 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
725 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
726 QCOMPARE(spyItemsMoved
.count(), 2);
727 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
728 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
730 // Sort by Date, descending
731 m_model
->setSortDirectoriesFirst(true);
732 m_model
->setSortRole("date");
733 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
734 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
735 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
736 QCOMPARE(spyItemsMoved
.count(), 1);
737 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
739 // Sort by Date, ascending
740 m_model
->setSortOrder(Qt::AscendingOrder
);
741 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
742 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
743 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
744 QCOMPARE(spyItemsMoved
.count(), 1);
745 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
747 // Sort by Date, ascending, 'Sort Folders First' disabled
748 m_model
->setSortDirectoriesFirst(false);
749 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
750 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
751 QVERIFY(!m_model
->sortDirectoriesFirst());
752 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
753 QCOMPARE(spyItemsMoved
.count(), 1);
754 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
756 // Sort by Name, ascending, 'Sort Folders First' disabled
757 m_model
->setSortRole("text");
758 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
759 QVERIFY(!m_model
->sortDirectoriesFirst());
760 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
761 QCOMPARE(spyItemsMoved
.count(), 1);
762 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
764 // Sort by Size, ascending, 'Sort Folders First' disabled
765 m_model
->setSortRole("size");
766 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
767 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
768 QVERIFY(!m_model
->sortDirectoriesFirst());
769 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
770 QCOMPARE(spyItemsMoved
.count(), 1);
771 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
773 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
774 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
775 "another signal", SkipSingle
);
776 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
778 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
779 m_model
->setSortDirectoriesFirst(true);
780 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
781 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
782 QVERIFY(m_model
->sortDirectoriesFirst());
783 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
784 QCOMPARE(spyItemsMoved
.count(), 0);
786 // Sort by Size, descending, 'Sort Folders First' enabled
787 m_model
->setSortOrder(Qt::DescendingOrder
);
788 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
789 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
790 QVERIFY(m_model
->sortDirectoriesFirst());
791 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
792 QCOMPARE(spyItemsMoved
.count(), 1);
793 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
795 // TODO: Sort by other roles; show/hide hidden files
798 void KFileItemModelTest::testIndexForKeyboardSearch()
801 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
802 m_testDir
->createFiles(files
);
804 m_model
->loadDirectory(m_testDir
->url());
805 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
807 // Search from index 0
808 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
809 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
810 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
811 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
812 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
813 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
814 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
815 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
816 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
817 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
818 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
820 // Start a search somewhere in the middle
821 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
822 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
823 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
824 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
826 // Test searches that go past the last item back to index 0
827 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
828 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
829 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
830 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
832 // Test searches that yield no result
833 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
834 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
835 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
836 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
837 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
839 // Test upper case searches (note that search is case insensitive)
840 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
841 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
842 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
843 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
845 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
848 void KFileItemModelTest::testNameFilter()
851 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
852 m_testDir
->createFiles(files
);
854 m_model
->loadDirectory(m_testDir
->url());
855 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
857 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
858 QCOMPARE(m_model
->count(), 3);
860 m_model
->setNameFilter("A2"); // Shows only A2
861 QCOMPARE(m_model
->count(), 1);
863 m_model
->setNameFilter("A2"); // Shows only A1
864 QCOMPARE(m_model
->count(), 1);
866 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
867 QCOMPARE(m_model
->count(), 2);
869 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
870 QCOMPARE(m_model
->count(), 2);
872 m_model
->setNameFilter(QString()); // Shows again all items
873 QCOMPARE(m_model
->count(), 5);
877 * Verifies that we do not crash when adding a KFileItem with an empty path.
878 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
879 * tried to always read the first character of the path, even if the path is empty.
881 void KFileItemModelTest::testEmptyPath()
883 QSet
<QByteArray
> roles
;
884 roles
.insert("text");
885 roles
.insert("isExpanded");
886 roles
.insert("isExpandable");
887 roles
.insert("expandedParentsCount");
888 m_model
->setRoles(roles
);
891 QVERIFY(emptyUrl
.path().isEmpty());
893 const KUrl
url("file:///test/");
896 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
897 m_model
->slotItemsAdded(emptyUrl
, items
);
898 m_model
->slotCompleted();
902 * Verifies that the 'isExpanded' state of folders does not change when the
903 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
905 void KFileItemModelTest::testRefreshExpandedItem()
907 QSet
<QByteArray
> modelRoles
= m_model
->roles();
908 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
909 m_model
->setRoles(modelRoles
);
912 files
<< "a/1" << "a/2" << "3" << "4";
913 m_testDir
->createFiles(files
);
915 m_model
->loadDirectory(m_testDir
->url());
916 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
917 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
919 m_model
->setExpanded(0, true);
920 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
921 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
922 QVERIFY(m_model
->isExpanded(0));
924 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
926 const KFileItem item
= m_model
->fileItem(0);
927 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
928 QVERIFY(!spyItemsChanged
.isEmpty());
930 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
931 QVERIFY(m_model
->isExpanded(0));
935 * Verify that removing hidden files and folders from the model does not
936 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
938 void KFileItemModelTest::testRemoveHiddenItems()
940 m_testDir
->createDir(".a");
941 m_testDir
->createDir(".b");
942 m_testDir
->createDir("c");
943 m_testDir
->createDir("d");
944 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
946 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
947 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
949 m_model
->setShowHiddenFiles(true);
950 m_model
->loadDirectory(m_testDir
->url());
951 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
952 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
953 QCOMPARE(spyItemsInserted
.count(), 1);
954 QCOMPARE(spyItemsRemoved
.count(), 0);
955 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
956 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
958 m_model
->setShowHiddenFiles(false);
959 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
960 QCOMPARE(spyItemsInserted
.count(), 0);
961 QCOMPARE(spyItemsRemoved
.count(), 1);
962 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
963 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
965 m_model
->setShowHiddenFiles(true);
966 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
967 QCOMPARE(spyItemsInserted
.count(), 1);
968 QCOMPARE(spyItemsRemoved
.count(), 0);
969 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
970 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
973 QCOMPARE(itemsInModel(), QStringList());
974 QCOMPARE(spyItemsInserted
.count(), 0);
975 QCOMPARE(spyItemsRemoved
.count(), 1);
976 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
977 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
979 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
980 // Verify that this does not make the model crash.
981 m_model
->setShowHiddenFiles(false);
985 * Verify that filtered items are removed when their parent is collapsed.
987 void KFileItemModelTest::collapseParentOfHiddenItems()
989 QSet
<QByteArray
> modelRoles
= m_model
->roles();
990 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
991 m_model
->setRoles(modelRoles
);
994 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
995 m_testDir
->createFiles(files
);
997 m_model
->loadDirectory(m_testDir
->url());
998 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
999 QCOMPARE(m_model
->count(), 1); // Only "a/"
1002 m_model
->setExpanded(0, true);
1003 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1004 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1007 m_model
->setExpanded(1, true);
1008 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1009 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1012 m_model
->setExpanded(2, true);
1013 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1014 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"
1016 // Set a name filter that matches nothing -> only the expanded folders remain.
1017 m_model
->setNameFilter("xyz");
1018 QCOMPARE(m_model
->count(), 3);
1019 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1021 // Collapse the folder "a/".
1022 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1023 m_model
->setExpanded(0, false);
1024 QCOMPARE(spyItemsRemoved
.count(), 1);
1025 QCOMPARE(m_model
->count(), 1);
1026 QCOMPARE(itemsInModel(), QStringList() << "a");
1028 // Remove the filter -> no files should appear (and we should not get a crash).
1029 m_model
->setNameFilter(QString());
1030 QCOMPARE(m_model
->count(), 1);
1034 * Verify that filtered items are removed when their parent is deleted.
1036 void KFileItemModelTest::removeParentOfHiddenItems()
1038 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1039 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1040 m_model
->setRoles(modelRoles
);
1043 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1044 m_testDir
->createFiles(files
);
1046 m_model
->loadDirectory(m_testDir
->url());
1047 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1048 QCOMPARE(m_model
->count(), 1); // Only "a/"
1051 m_model
->setExpanded(0, true);
1052 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1053 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1056 m_model
->setExpanded(1, true);
1057 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1058 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1061 m_model
->setExpanded(2, true);
1062 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1063 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"
1065 // Set a name filter that matches nothing -> only the expanded folders remain.
1066 m_model
->setNameFilter("xyz");
1067 QCOMPARE(m_model
->count(), 3);
1068 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1070 // Simulate the deletion of the directory "a/b/".
1071 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1072 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1073 QCOMPARE(spyItemsRemoved
.count(), 1);
1074 QCOMPARE(m_model
->count(), 1);
1075 QCOMPARE(itemsInModel(), QStringList() << "a");
1077 // Remove the filter -> only the file "a/1" should appear.
1078 m_model
->setNameFilter(QString());
1079 QCOMPARE(m_model
->count(), 2);
1080 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1084 * Create a tree structure where parent-child relationships can not be
1085 * determined by parsing the URLs, and verify that KFileItemModel
1086 * handles them correctly.
1088 void KFileItemModelTest::testGeneralParentChildRelationships()
1090 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1091 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1092 m_model
->setRoles(modelRoles
);
1095 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1096 m_testDir
->createFiles(files
);
1098 m_model
->loadDirectory(m_testDir
->url());
1099 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1100 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1102 // Expand all folders.
1103 m_model
->setExpanded(0, true);
1104 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1105 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1107 m_model
->setExpanded(1, true);
1108 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1109 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1111 m_model
->setExpanded(3, true);
1112 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1113 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1115 m_model
->setExpanded(4, true);
1116 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1117 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1119 // Add some more children and grand-children.
1120 const KUrl parent1
= m_model
->fileItem(0).url();
1121 const KUrl parent2
= m_model
->fileItem(3).url();
1122 const KUrl realChild1
= m_model
->fileItem(1).url();
1123 const KUrl realChild2
= m_model
->fileItem(4).url();
1125 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1126 m_model
->slotCompleted();
1127 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1129 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1130 m_model
->slotCompleted();
1131 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1133 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1134 m_model
->slotCompleted();
1135 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1137 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1138 m_model
->slotCompleted();
1139 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1141 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1142 m_model
->slotCompleted();
1143 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1145 // Set a name filter that matches nothing -> only expanded folders remain.
1146 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1147 m_model
->setNameFilter("xyz");
1148 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1149 QCOMPARE(itemsRemovedSpy
.count(), 1);
1150 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1151 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1152 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1154 // Collapse "parent1".
1155 m_model
->setExpanded(0, false);
1156 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1157 QCOMPARE(itemsRemovedSpy
.count(), 1);
1158 arguments
= itemsRemovedSpy
.takeFirst();
1159 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1160 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1162 // Remove "parent2".
1163 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1164 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1165 QCOMPARE(itemsRemovedSpy
.count(), 1);
1166 arguments
= itemsRemovedSpy
.takeFirst();
1167 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1168 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1170 // Clear filter, verify that no items reappear.
1171 m_model
->setNameFilter(QString());
1172 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1175 QStringList
KFileItemModelTest::itemsInModel() const
1178 for (int i
= 0; i
< m_model
->count(); i
++) {
1179 items
<< m_model
->data(i
).value("text").toString();
1184 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1186 #include "kfileitemmodeltest.moc"