1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include <qtest_kde.h>
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
30 void myMessageOutput(QtMsgType type
, const char* msg
)
38 fprintf(stderr
, "Critical: %s\n", msg
);
41 fprintf(stderr
, "Fatal: %s\n", msg
);
49 const int DefaultTimeout
= 5000;
52 Q_DECLARE_METATYPE(KItemRange
)
53 Q_DECLARE_METATYPE(KItemRangeList
)
54 Q_DECLARE_METATYPE(QList
<int>)
56 class KFileItemModelTest
: public QObject
64 void testDefaultRoles();
65 void testDefaultSortRole();
66 void testDefaultGroupedSorting();
68 void testRemoveItems();
69 void testDirLoadingCompleted();
71 void testSetDataWithModifiedSortRole_data();
72 void testSetDataWithModifiedSortRole();
73 void testChangeSortRole();
74 void testResortAfterChangingName();
75 void testModelConsistencyWhenInsertingItems();
76 void testItemRangeConsistencyWhenInsertingItems();
77 void testExpandItems();
78 void testExpandParentItems();
79 void testMakeExpandedItemHidden();
81 void testIndexForKeyboardSearch();
82 void testNameFilter();
84 void testRefreshExpandedItem();
85 void testRemoveHiddenItems();
86 void collapseParentOfHiddenItems();
87 void removeParentOfHiddenItems();
88 void testGeneralParentChildRelationships();
89 void testNameRoleGroups();
92 QStringList
itemsInModel() const;
95 KFileItemModel
* m_model
;
99 void KFileItemModelTest::init()
101 // The item-model tests result in a huge number of debugging
102 // output from kdelibs. Only show critical and fatal messages.
103 qInstallMsgHandler(myMessageOutput
);
105 qRegisterMetaType
<KItemRange
>("KItemRange");
106 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
107 qRegisterMetaType
<KFileItemList
>("KFileItemList");
109 m_testDir
= new TestDir();
110 m_model
= new KFileItemModel();
111 m_model
->m_dirLister
->setAutoUpdate(false);
113 // Reduce the timer interval to make the test run faster.
114 m_model
->m_resortAllItemsTimer
->setInterval(0);
117 void KFileItemModelTest::cleanup()
126 void KFileItemModelTest::testDefaultRoles()
128 const QSet
<QByteArray
> roles
= m_model
->roles();
129 QCOMPARE(roles
.count(), 3);
130 QVERIFY(roles
.contains("text"));
131 QVERIFY(roles
.contains("isDir"));
132 QVERIFY(roles
.contains("isLink"));
135 void KFileItemModelTest::testDefaultSortRole()
137 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
140 files
<< "c.txt" << "a.txt" << "b.txt";
142 m_testDir
->createFiles(files
);
144 m_model
->loadDirectory(m_testDir
->url());
145 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
147 QCOMPARE(m_model
->count(), 3);
148 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
149 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
150 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
153 void KFileItemModelTest::testDefaultGroupedSorting()
155 QCOMPARE(m_model
->groupedSorting(), false);
158 void KFileItemModelTest::testNewItems()
161 files
<< "a.txt" << "b.txt" << "c.txt";
162 m_testDir
->createFiles(files
);
164 m_model
->loadDirectory(m_testDir
->url());
165 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
167 QCOMPARE(m_model
->count(), 3);
169 QVERIFY(m_model
->isConsistent());
172 void KFileItemModelTest::testRemoveItems()
174 m_testDir
->createFile("a.txt");
175 m_testDir
->createFile("b.txt");
176 m_model
->loadDirectory(m_testDir
->url());
177 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
178 QCOMPARE(m_model
->count(), 2);
179 QVERIFY(m_model
->isConsistent());
181 m_testDir
->removeFile("a.txt");
182 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
184 QCOMPARE(m_model
->count(), 1);
185 QVERIFY(m_model
->isConsistent());
188 void KFileItemModelTest::testDirLoadingCompleted()
190 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
191 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
192 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
194 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
196 m_model
->loadDirectory(m_testDir
->url());
197 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
198 QCOMPARE(loadingCompletedSpy
.count(), 1);
199 QCOMPARE(itemsInsertedSpy
.count(), 1);
200 QCOMPARE(itemsRemovedSpy
.count(), 0);
201 QCOMPARE(m_model
->count(), 3);
203 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
204 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
205 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
206 QCOMPARE(loadingCompletedSpy
.count(), 2);
207 QCOMPARE(itemsInsertedSpy
.count(), 2);
208 QCOMPARE(itemsRemovedSpy
.count(), 0);
209 QCOMPARE(m_model
->count(), 5);
211 m_testDir
->removeFile("a.txt");
212 m_testDir
->createFile("f.txt");
213 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
214 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
215 QCOMPARE(loadingCompletedSpy
.count(), 3);
216 QCOMPARE(itemsInsertedSpy
.count(), 3);
217 QCOMPARE(itemsRemovedSpy
.count(), 1);
218 QCOMPARE(m_model
->count(), 5);
220 m_testDir
->removeFile("b.txt");
221 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
222 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
223 QCOMPARE(loadingCompletedSpy
.count(), 4);
224 QCOMPARE(itemsInsertedSpy
.count(), 3);
225 QCOMPARE(itemsRemovedSpy
.count(), 2);
226 QCOMPARE(m_model
->count(), 4);
228 QVERIFY(m_model
->isConsistent());
231 void KFileItemModelTest::testSetData()
233 m_testDir
->createFile("a.txt");
235 m_model
->loadDirectory(m_testDir
->url());
236 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
238 QHash
<QByteArray
, QVariant
> values
;
239 values
.insert("customRole1", "Test1");
240 values
.insert("customRole2", "Test2");
242 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
243 m_model
->setData(0, values
);
244 QCOMPARE(itemsChangedSpy
.count(), 1);
246 values
= m_model
->data(0);
247 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
248 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
249 QVERIFY(m_model
->isConsistent());
252 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
254 QTest::addColumn
<int>("changedIndex");
255 QTest::addColumn
<int>("changedRating");
256 QTest::addColumn
<bool>("expectMoveSignal");
257 QTest::addColumn
<int>("ratingIndex0");
258 QTest::addColumn
<int>("ratingIndex1");
259 QTest::addColumn
<int>("ratingIndex2");
262 // Index 0 = rating 2
263 // Index 1 = rating 4
264 // Index 2 = rating 6
266 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
267 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
268 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
270 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
271 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
272 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
275 void KFileItemModelTest::testSetDataWithModifiedSortRole()
277 QFETCH(int, changedIndex
);
278 QFETCH(int, changedRating
);
279 QFETCH(bool, expectMoveSignal
);
280 QFETCH(int, ratingIndex0
);
281 QFETCH(int, ratingIndex1
);
282 QFETCH(int, ratingIndex2
);
284 // Changing the value of a sort-role must result in
285 // a reordering of the items.
286 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
287 m_model
->setSortRole("rating");
288 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
291 files
<< "a.txt" << "b.txt" << "c.txt";
292 m_testDir
->createFiles(files
);
294 m_model
->loadDirectory(m_testDir
->url());
295 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
297 // Fill the "rating" role of each file:
302 QHash
<QByteArray
, QVariant
> ratingA
;
303 ratingA
.insert("rating", 2);
304 m_model
->setData(0, ratingA
);
306 QHash
<QByteArray
, QVariant
> ratingB
;
307 ratingB
.insert("rating", 4);
308 m_model
->setData(1, ratingB
);
310 QHash
<QByteArray
, QVariant
> ratingC
;
311 ratingC
.insert("rating", 6);
312 m_model
->setData(2, ratingC
);
314 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
315 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
316 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
318 // Now change the rating from a.txt. This usually results
319 // in reordering of the items.
320 QHash
<QByteArray
, QVariant
> rating
;
321 rating
.insert("rating", changedRating
);
322 m_model
->setData(changedIndex
, rating
);
324 if (expectMoveSignal
) {
325 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
328 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
329 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
330 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
331 QVERIFY(m_model
->isConsistent());
334 void KFileItemModelTest::testChangeSortRole()
336 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
339 files
<< "a.txt" << "b.jpg" << "c.txt";
340 m_testDir
->createFiles(files
);
342 m_model
->loadDirectory(m_testDir
->url());
343 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
344 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
346 // Simulate that KFileItemModelRolesUpdater determines the mime type.
347 // Resorting the files by 'type' will only work immediately if their
348 // mime types are known.
349 for (int index
= 0; index
< m_model
->count(); ++index
) {
350 m_model
->fileItem(index
).determineMimeType();
353 // Now: sort by type.
354 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
355 m_model
->setSortRole("type");
356 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
357 QVERIFY(!spyItemsMoved
.isEmpty());
359 // The actual order of the files might depend on the translation of the
360 // result of KFileItem::mimeComment() in the user's language.
361 QStringList version1
;
362 version1
<< "b.jpg" << "a.txt" << "c.txt";
364 QStringList version2
;
365 version2
<< "a.txt" << "c.txt" << "b.jpg";
367 const bool ok1
= (itemsInModel() == version1
);
368 const bool ok2
= (itemsInModel() == version2
);
373 void KFileItemModelTest::testResortAfterChangingName()
375 // We sort by size in a directory where all files have the same size.
376 // Therefore, the files are sorted by their names.
377 m_model
->setSortRole("size");
380 files
<< "a.txt" << "b.txt" << "c.txt";
381 m_testDir
->createFiles(files
);
383 m_model
->loadDirectory(m_testDir
->url());
384 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
385 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
387 // We rename a.txt to d.txt. Even though the size has not changed at all,
388 // the model must re-sort the items.
389 QHash
<QByteArray
, QVariant
> data
;
390 data
.insert("text", "d.txt");
391 m_model
->setData(0, data
);
393 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
394 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
397 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
399 //QSKIP("Temporary disabled", SkipSingle);
401 // KFileItemModel prevents that inserting a punch of items sequentially
402 // results in an itemsInserted()-signal for each item. Instead internally
403 // a timeout is given that collects such operations and results in only
404 // one itemsInserted()-signal. However in this test we want to stress
405 // KFileItemModel to do a lot of insert operation and hence decrease
406 // the timeout to 1 millisecond.
407 m_testDir
->createFile("1");
408 m_model
->loadDirectory(m_testDir
->url());
409 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
410 QCOMPARE(m_model
->count(), 1);
412 // Insert 10 items for 20 times. After each insert operation the model consistency
414 QSet
<int> insertedItems
;
415 for (int i
= 0; i
< 20; ++i
) {
416 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
418 for (int j
= 0; j
< 10; ++j
) {
419 int itemName
= qrand();
420 while (insertedItems
.contains(itemName
)) {
423 insertedItems
.insert(itemName
);
425 m_testDir
->createFile(QString::number(itemName
));
428 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
429 if (spy
.count() == 0) {
430 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
433 QVERIFY(m_model
->isConsistent());
436 QCOMPARE(m_model
->count(), 201);
439 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
442 files
<< "B" << "E" << "G";
443 m_testDir
->createFiles(files
);
445 // Due to inserting the 3 items one item-range with index == 0 and
446 // count == 3 must be given
447 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
448 m_model
->loadDirectory(m_testDir
->url());
449 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
451 QCOMPARE(spy1
.count(), 1);
452 QList
<QVariant
> arguments
= spy1
.takeFirst();
453 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
454 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
456 // The indexes of the item-ranges must always be related to the model before
457 // the items have been inserted. Having:
460 // and inserting A, C, D, F the resulting model will be:
463 // and the item-ranges must be:
464 // index: 0, count: 1 for A
465 // index: 1, count: 2 for B, C
466 // index: 2, count: 1 for G
469 files
<< "A" << "C" << "D" << "F";
470 m_testDir
->createFiles(files
);
472 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
473 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
474 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
476 QCOMPARE(spy2
.count(), 1);
477 arguments
= spy2
.takeFirst();
478 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
479 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
482 void KFileItemModelTest::testExpandItems()
484 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
485 // Besides testing the basic item expansion functionality, the test makes sure that
486 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
487 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
488 // first three characters.
489 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
490 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
491 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
492 m_model
->setRoles(modelRoles
);
495 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
496 m_testDir
->createFiles(files
);
498 // Store the URLs of all folders in a set.
499 QSet
<KUrl
> allFolders
;
500 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
502 m_model
->loadDirectory(m_testDir
->url());
503 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
505 // So far, the model contains only "a/"
506 QCOMPARE(m_model
->count(), 1);
507 QVERIFY(m_model
->isExpandable(0));
508 QVERIFY(!m_model
->isExpanded(0));
509 QVERIFY(m_model
->expandedDirectories().empty());
511 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
513 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
514 m_model
->setExpanded(0, true);
515 QVERIFY(m_model
->isExpanded(0));
516 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
517 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
518 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
520 QCOMPARE(spyInserted
.count(), 1);
521 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
522 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
524 QVERIFY(m_model
->isExpandable(1));
525 QVERIFY(!m_model
->isExpanded(1));
526 QVERIFY(m_model
->isExpandable(2));
527 QVERIFY(!m_model
->isExpanded(2));
529 // Expand the folder "a/a/" -> "a/a/1" becomes visible
530 m_model
->setExpanded(1, true);
531 QVERIFY(m_model
->isExpanded(1));
532 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
533 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
534 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
536 QCOMPARE(spyInserted
.count(), 1);
537 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
538 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
540 QVERIFY(!m_model
->isExpandable(2));
541 QVERIFY(!m_model
->isExpanded(2));
543 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
544 m_model
->setExpanded(3, true);
545 QVERIFY(m_model
->isExpanded(3));
546 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
547 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
548 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
550 QCOMPARE(spyInserted
.count(), 1);
551 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
552 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
554 QVERIFY(!m_model
->isExpandable(4));
555 QVERIFY(!m_model
->isExpanded(4));
557 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
559 // Collapse the top-level folder -> all other items should disappear
560 m_model
->setExpanded(0, false);
561 QVERIFY(!m_model
->isExpanded(0));
562 QCOMPARE(m_model
->count(), 1);
563 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
565 QCOMPARE(spyRemoved
.count(), 1);
566 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
567 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
568 QVERIFY(m_model
->isConsistent());
570 // Clear the model, reload the folder and try to restore the expanded folders.
572 QCOMPARE(m_model
->count(), 0);
573 QVERIFY(m_model
->expandedDirectories().empty());
575 m_model
->loadDirectory(m_testDir
->url());
576 m_model
->restoreExpandedDirectories(allFolders
);
577 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
578 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
579 QVERIFY(m_model
->isExpanded(0));
580 QVERIFY(m_model
->isExpanded(1));
581 QVERIFY(!m_model
->isExpanded(2));
582 QVERIFY(m_model
->isExpanded(3));
583 QVERIFY(!m_model
->isExpanded(4));
584 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
585 QVERIFY(m_model
->isConsistent());
587 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
588 // This is how DolphinView restores the expanded folders when navigating in history.
589 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
590 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
591 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
592 m_model
->restoreExpandedDirectories(allFolders
);
593 m_model
->loadDirectory(m_testDir
->url());
594 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
595 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
596 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
598 // Remove all expanded items by changing the roles
600 m_model
->setRoles(originalModelRoles
);
601 QVERIFY(!m_model
->isExpanded(0));
602 QCOMPARE(m_model
->count(), 1);
603 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
605 QCOMPARE(spyRemoved
.count(), 1);
606 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
607 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
608 QVERIFY(m_model
->isConsistent());
611 void KFileItemModelTest::testExpandParentItems()
613 // Create a tree structure of folders:
621 QSet
<QByteArray
> modelRoles
= m_model
->roles();
622 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
623 m_model
->setRoles(modelRoles
);
626 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
627 m_testDir
->createFiles(files
);
629 m_model
->loadDirectory(m_testDir
->url());
630 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
632 // So far, the model contains only "a 1/" and "a2/".
633 QCOMPARE(m_model
->count(), 2);
634 QVERIFY(m_model
->expandedDirectories().empty());
636 // Expand the parents of "a2/b2/c2".
637 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
638 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
640 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
641 // It's important that only the parents of "a1/b1/c1" are expanded.
642 QCOMPARE(m_model
->count(), 4);
643 QVERIFY(!m_model
->isExpanded(0));
644 QVERIFY(m_model
->isExpanded(1));
645 QVERIFY(m_model
->isExpanded(2));
646 QVERIFY(!m_model
->isExpanded(3));
648 // Expand the parents of "a 1/b1".
649 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
650 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
652 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
653 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
654 QCOMPARE(m_model
->count(), 5);
655 QVERIFY(m_model
->isExpanded(0));
656 QVERIFY(!m_model
->isExpanded(1));
657 QVERIFY(m_model
->isExpanded(2));
658 QVERIFY(m_model
->isExpanded(3));
659 QVERIFY(!m_model
->isExpanded(4));
660 QVERIFY(m_model
->isConsistent());
663 m_model
->setExpanded(1, true);
664 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
665 QCOMPARE(m_model
->count(), 6);
666 QVERIFY(m_model
->isExpanded(0));
667 QVERIFY(m_model
->isExpanded(1));
668 QVERIFY(!m_model
->isExpanded(2));
669 QVERIFY(m_model
->isExpanded(3));
670 QVERIFY(m_model
->isExpanded(4));
671 QVERIFY(!m_model
->isExpanded(5));
672 QVERIFY(m_model
->isConsistent());
674 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
675 m_model
->setExpanded(1, false);
676 QCOMPARE(m_model
->count(), 5);
677 QVERIFY(m_model
->isExpanded(0));
678 QVERIFY(!m_model
->isExpanded(1));
679 QVERIFY(m_model
->isExpanded(2));
680 QVERIFY(m_model
->isExpanded(3));
681 QVERIFY(!m_model
->isExpanded(4));
682 QVERIFY(m_model
->isConsistent());
686 * Renaming an expanded folder by prepending its name with a dot makes it
687 * hidden. Verify that this does not cause an inconsistent model state and
688 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
690 void KFileItemModelTest::testMakeExpandedItemHidden()
692 QSet
<QByteArray
> modelRoles
= m_model
->roles();
693 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
694 m_model
->setRoles(modelRoles
);
697 m_testDir
->createFile("1a/2a/3a");
698 m_testDir
->createFile("1a/2a/3b");
699 m_testDir
->createFile("1a/2b");
700 m_testDir
->createFile("1b");
702 m_model
->loadDirectory(m_testDir
->url());
703 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
705 // So far, the model contains only "1a/" and "1b".
706 QCOMPARE(m_model
->count(), 2);
707 m_model
->setExpanded(0, true);
708 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
710 // Now "1a/2a" and "1a/2b" have appeared.
711 QCOMPARE(m_model
->count(), 4);
712 m_model
->setExpanded(1, true);
713 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
714 QCOMPARE(m_model
->count(), 6);
716 // Rename "1a/2" and make it hidden.
717 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
718 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
720 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
721 bool ok
= job
->exec();
723 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
725 // "1a/2" and its subfolders have disappeared now.
726 QVERIFY(m_model
->isConsistent());
727 QCOMPARE(m_model
->count(), 3);
729 m_model
->setExpanded(0, false);
730 QCOMPARE(m_model
->count(), 2);
734 void KFileItemModelTest::testSorting()
736 // Create some files with different sizes and modification times to check the different sorting options
737 QDateTime now
= QDateTime::currentDateTime();
739 QSet
<QByteArray
> roles
;
740 roles
.insert("text");
741 roles
.insert("isExpanded");
742 roles
.insert("isExpandable");
743 roles
.insert("expandedParentsCount");
744 m_model
->setRoles(roles
);
746 m_testDir
->createDir("c/c-2");
747 m_testDir
->createFile("c/c-2/c-3");
748 m_testDir
->createFile("c/c-1");
750 m_testDir
->createFile("a", "A file", now
.addDays(-3));
751 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
752 m_testDir
->createDir("c", now
.addDays(-2));
753 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
754 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
755 m_testDir
->createFile(".f");
757 m_model
->loadDirectory(m_testDir
->url());
758 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
760 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
761 m_model
->setExpanded(index
, true);
762 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
764 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
765 m_model
->setExpanded(index
, true);
766 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
768 // Default: Sort by Name, ascending
769 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
770 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
771 QVERIFY(m_model
->sortDirectoriesFirst());
772 QVERIFY(!m_model
->showHiddenFiles());
773 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
775 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
777 // Sort by Name, ascending, 'Sort Folders First' disabled
778 m_model
->setSortDirectoriesFirst(false);
779 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
780 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
781 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
782 QCOMPARE(spyItemsMoved
.count(), 1);
783 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
784 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
786 // Sort by Name, descending
787 m_model
->setSortDirectoriesFirst(true);
788 m_model
->setSortOrder(Qt::DescendingOrder
);
789 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
790 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
791 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
792 QCOMPARE(spyItemsMoved
.count(), 2);
793 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
794 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
795 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
796 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
798 // Sort by Date, descending
799 m_model
->setSortDirectoriesFirst(true);
800 m_model
->setSortRole("date");
801 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
802 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
803 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
804 QCOMPARE(spyItemsMoved
.count(), 1);
805 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
806 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
808 // Sort by Date, ascending
809 m_model
->setSortOrder(Qt::AscendingOrder
);
810 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
811 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
812 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
813 QCOMPARE(spyItemsMoved
.count(), 1);
814 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
815 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
817 // Sort by Date, ascending, 'Sort Folders First' disabled
818 m_model
->setSortDirectoriesFirst(false);
819 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
820 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
821 QVERIFY(!m_model
->sortDirectoriesFirst());
822 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
823 QCOMPARE(spyItemsMoved
.count(), 1);
824 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
825 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
827 // Sort by Name, ascending, 'Sort Folders First' disabled
828 m_model
->setSortRole("text");
829 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
830 QVERIFY(!m_model
->sortDirectoriesFirst());
831 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
832 QCOMPARE(spyItemsMoved
.count(), 1);
833 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
834 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
836 // Sort by Size, ascending, 'Sort Folders First' disabled
837 m_model
->setSortRole("size");
838 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
839 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
840 QVERIFY(!m_model
->sortDirectoriesFirst());
841 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
842 QCOMPARE(spyItemsMoved
.count(), 1);
843 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
844 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
846 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
847 m_model
->setSortDirectoriesFirst(true);
848 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
849 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
850 QVERIFY(m_model
->sortDirectoriesFirst());
851 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
852 QCOMPARE(spyItemsMoved
.count(), 0);
854 // Sort by Size, descending, 'Sort Folders First' enabled
855 m_model
->setSortOrder(Qt::DescendingOrder
);
856 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
857 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
858 QVERIFY(m_model
->sortDirectoriesFirst());
859 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
860 QCOMPARE(spyItemsMoved
.count(), 1);
861 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
862 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
864 // TODO: Sort by other roles; show/hide hidden files
867 void KFileItemModelTest::testIndexForKeyboardSearch()
870 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
871 m_testDir
->createFiles(files
);
873 m_model
->loadDirectory(m_testDir
->url());
874 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
876 // Search from index 0
877 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
878 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
879 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
880 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
881 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
882 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
883 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
884 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
885 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
886 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
887 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
889 // Start a search somewhere in the middle
890 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
891 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
892 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
893 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
895 // Test searches that go past the last item back to index 0
896 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
897 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
898 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
899 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
901 // Test searches that yield no result
902 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
903 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
904 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
905 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
906 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
908 // Test upper case searches (note that search is case insensitive)
909 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
910 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
911 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
912 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
914 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
917 void KFileItemModelTest::testNameFilter()
920 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
921 m_testDir
->createFiles(files
);
923 m_model
->loadDirectory(m_testDir
->url());
924 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
926 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
927 QCOMPARE(m_model
->count(), 3);
929 m_model
->setNameFilter("A2"); // Shows only A2
930 QCOMPARE(m_model
->count(), 1);
932 m_model
->setNameFilter("A2"); // Shows only A1
933 QCOMPARE(m_model
->count(), 1);
935 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
936 QCOMPARE(m_model
->count(), 2);
938 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
939 QCOMPARE(m_model
->count(), 2);
941 m_model
->setNameFilter(QString()); // Shows again all items
942 QCOMPARE(m_model
->count(), 5);
946 * Verifies that we do not crash when adding a KFileItem with an empty path.
947 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
948 * tried to always read the first character of the path, even if the path is empty.
950 void KFileItemModelTest::testEmptyPath()
952 QSet
<QByteArray
> roles
;
953 roles
.insert("text");
954 roles
.insert("isExpanded");
955 roles
.insert("isExpandable");
956 roles
.insert("expandedParentsCount");
957 m_model
->setRoles(roles
);
960 QVERIFY(emptyUrl
.path().isEmpty());
962 const KUrl
url("file:///test/");
965 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
966 m_model
->slotItemsAdded(emptyUrl
, items
);
967 m_model
->slotCompleted();
971 * Verifies that the 'isExpanded' state of folders does not change when the
972 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
974 void KFileItemModelTest::testRefreshExpandedItem()
976 QSet
<QByteArray
> modelRoles
= m_model
->roles();
977 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
978 m_model
->setRoles(modelRoles
);
981 files
<< "a/1" << "a/2" << "3" << "4";
982 m_testDir
->createFiles(files
);
984 m_model
->loadDirectory(m_testDir
->url());
985 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
986 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
988 m_model
->setExpanded(0, true);
989 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
990 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
991 QVERIFY(m_model
->isExpanded(0));
993 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
995 const KFileItem item
= m_model
->fileItem(0);
996 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
997 QVERIFY(!spyItemsChanged
.isEmpty());
999 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1000 QVERIFY(m_model
->isExpanded(0));
1004 * Verify that removing hidden files and folders from the model does not
1005 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1007 void KFileItemModelTest::testRemoveHiddenItems()
1009 m_testDir
->createDir(".a");
1010 m_testDir
->createDir(".b");
1011 m_testDir
->createDir("c");
1012 m_testDir
->createDir("d");
1013 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1015 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1016 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1018 m_model
->setShowHiddenFiles(true);
1019 m_model
->loadDirectory(m_testDir
->url());
1020 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1021 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1022 QCOMPARE(spyItemsInserted
.count(), 1);
1023 QCOMPARE(spyItemsRemoved
.count(), 0);
1024 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1025 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1027 m_model
->setShowHiddenFiles(false);
1028 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1029 QCOMPARE(spyItemsInserted
.count(), 0);
1030 QCOMPARE(spyItemsRemoved
.count(), 1);
1031 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1032 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1034 m_model
->setShowHiddenFiles(true);
1035 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1036 QCOMPARE(spyItemsInserted
.count(), 1);
1037 QCOMPARE(spyItemsRemoved
.count(), 0);
1038 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1039 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1042 QCOMPARE(itemsInModel(), QStringList());
1043 QCOMPARE(spyItemsInserted
.count(), 0);
1044 QCOMPARE(spyItemsRemoved
.count(), 1);
1045 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1046 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1048 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1049 // Verify that this does not make the model crash.
1050 m_model
->setShowHiddenFiles(false);
1054 * Verify that filtered items are removed when their parent is collapsed.
1056 void KFileItemModelTest::collapseParentOfHiddenItems()
1058 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1059 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1060 m_model
->setRoles(modelRoles
);
1063 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1064 m_testDir
->createFiles(files
);
1066 m_model
->loadDirectory(m_testDir
->url());
1067 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1068 QCOMPARE(m_model
->count(), 1); // Only "a/"
1071 m_model
->setExpanded(0, true);
1072 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1073 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1076 m_model
->setExpanded(1, true);
1077 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1078 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1081 m_model
->setExpanded(2, true);
1082 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1083 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"
1085 // Set a name filter that matches nothing -> only the expanded folders remain.
1086 m_model
->setNameFilter("xyz");
1087 QCOMPARE(m_model
->count(), 3);
1088 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1090 // Collapse the folder "a/".
1091 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1092 m_model
->setExpanded(0, false);
1093 QCOMPARE(spyItemsRemoved
.count(), 1);
1094 QCOMPARE(m_model
->count(), 1);
1095 QCOMPARE(itemsInModel(), QStringList() << "a");
1097 // Remove the filter -> no files should appear (and we should not get a crash).
1098 m_model
->setNameFilter(QString());
1099 QCOMPARE(m_model
->count(), 1);
1103 * Verify that filtered items are removed when their parent is deleted.
1105 void KFileItemModelTest::removeParentOfHiddenItems()
1107 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1108 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1109 m_model
->setRoles(modelRoles
);
1112 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1113 m_testDir
->createFiles(files
);
1115 m_model
->loadDirectory(m_testDir
->url());
1116 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1117 QCOMPARE(m_model
->count(), 1); // Only "a/"
1120 m_model
->setExpanded(0, true);
1121 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1122 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1125 m_model
->setExpanded(1, true);
1126 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1127 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1130 m_model
->setExpanded(2, true);
1131 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1132 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"
1134 // Set a name filter that matches nothing -> only the expanded folders remain.
1135 m_model
->setNameFilter("xyz");
1136 QCOMPARE(m_model
->count(), 3);
1137 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1139 // Simulate the deletion of the directory "a/b/".
1140 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1141 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1142 QCOMPARE(spyItemsRemoved
.count(), 1);
1143 QCOMPARE(m_model
->count(), 1);
1144 QCOMPARE(itemsInModel(), QStringList() << "a");
1146 // Remove the filter -> only the file "a/1" should appear.
1147 m_model
->setNameFilter(QString());
1148 QCOMPARE(m_model
->count(), 2);
1149 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1153 * Create a tree structure where parent-child relationships can not be
1154 * determined by parsing the URLs, and verify that KFileItemModel
1155 * handles them correctly.
1157 void KFileItemModelTest::testGeneralParentChildRelationships()
1159 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1160 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1161 m_model
->setRoles(modelRoles
);
1164 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1165 m_testDir
->createFiles(files
);
1167 m_model
->loadDirectory(m_testDir
->url());
1168 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1169 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1171 // Expand all folders.
1172 m_model
->setExpanded(0, true);
1173 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1174 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1176 m_model
->setExpanded(1, true);
1177 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1178 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1180 m_model
->setExpanded(3, true);
1181 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1182 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1184 m_model
->setExpanded(4, true);
1185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1186 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1188 // Add some more children and grand-children.
1189 const KUrl parent1
= m_model
->fileItem(0).url();
1190 const KUrl parent2
= m_model
->fileItem(3).url();
1191 const KUrl realChild1
= m_model
->fileItem(1).url();
1192 const KUrl realChild2
= m_model
->fileItem(4).url();
1194 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1195 m_model
->slotCompleted();
1196 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1198 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1199 m_model
->slotCompleted();
1200 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1202 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1203 m_model
->slotCompleted();
1204 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1206 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1207 m_model
->slotCompleted();
1208 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1210 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1211 m_model
->slotCompleted();
1212 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1214 // Set a name filter that matches nothing -> only expanded folders remain.
1215 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1216 m_model
->setNameFilter("xyz");
1217 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1218 QCOMPARE(itemsRemovedSpy
.count(), 1);
1219 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1220 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1221 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1223 // Collapse "parent1".
1224 m_model
->setExpanded(0, false);
1225 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1226 QCOMPARE(itemsRemovedSpy
.count(), 1);
1227 arguments
= itemsRemovedSpy
.takeFirst();
1228 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1229 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1231 // Remove "parent2".
1232 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1233 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1234 QCOMPARE(itemsRemovedSpy
.count(), 1);
1235 arguments
= itemsRemovedSpy
.takeFirst();
1236 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1237 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1239 // Clear filter, verify that no items reappear.
1240 m_model
->setNameFilter(QString());
1241 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1244 void KFileItemModelTest::testNameRoleGroups()
1247 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1249 m_testDir
->createFiles(files
);
1251 m_model
->setGroupedSorting(true);
1252 m_model
->loadDirectory(m_testDir
->url());
1253 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1254 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1256 QList
<QPair
<int, QVariant
> > expectedGroups
;
1257 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1258 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1259 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1260 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1261 QCOMPARE(m_model
->groups(), expectedGroups
);
1263 // Rename d.txt to a.txt.
1264 QHash
<QByteArray
, QVariant
> data
;
1265 data
.insert("text", "a.txt");
1266 m_model
->setData(2, data
);
1267 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1268 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1270 expectedGroups
.clear();
1271 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1272 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1273 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1274 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1275 QCOMPARE(m_model
->groups(), expectedGroups
);
1277 // Rename c.txt to d.txt.
1278 data
.insert("text", "d.txt");
1279 m_model
->setData(2, data
);
1280 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1281 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1283 expectedGroups
.clear();
1284 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1285 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1286 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1287 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1288 QCOMPARE(m_model
->groups(), expectedGroups
);
1290 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1291 const KFileItem fileItemD
= m_model
->fileItem(2);
1292 KFileItem fileItemC
= fileItemD
;
1293 KUrl urlC
= fileItemC
.url();
1294 urlC
.setFileName("c.txt");
1295 fileItemC
.setUrl(urlC
);
1297 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1298 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1299 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1301 expectedGroups
.clear();
1302 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1303 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1304 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1305 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1306 QCOMPARE(m_model
->groups(), expectedGroups
);
1309 QStringList
KFileItemModelTest::itemsInModel() const
1312 for (int i
= 0; i
< m_model
->count(); i
++) {
1313 items
<< m_model
->data(i
).value("text").toString();
1318 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1320 #include "kfileitemmodeltest.moc"