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 testModelConsistencyWhenInsertingItems();
73 void testItemRangeConsistencyWhenInsertingItems();
74 void testExpandItems();
75 void testExpandParentItems();
76 void testMakeExpandedItemHidden();
78 void testIndexForKeyboardSearch();
79 void testNameFilter();
81 void testRefreshExpandedItem();
82 void testRemoveHiddenItems();
83 void collapseParentOfHiddenItems();
84 void removeParentOfHiddenItems();
85 void testGeneralParentChildRelationships();
88 QStringList
itemsInModel() const;
91 KFileItemModel
* m_model
;
95 void KFileItemModelTest::init()
97 // The item-model tests result in a huge number of debugging
98 // output from kdelibs. Only show critical and fatal messages.
99 qInstallMsgHandler(myMessageOutput
);
101 qRegisterMetaType
<KItemRange
>("KItemRange");
102 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
103 qRegisterMetaType
<KFileItemList
>("KFileItemList");
105 m_testDir
= new TestDir();
106 m_model
= new KFileItemModel();
107 m_model
->m_dirLister
->setAutoUpdate(false);
110 void KFileItemModelTest::cleanup()
119 void KFileItemModelTest::testDefaultRoles()
121 const QSet
<QByteArray
> roles
= m_model
->roles();
122 QCOMPARE(roles
.count(), 3);
123 QVERIFY(roles
.contains("text"));
124 QVERIFY(roles
.contains("isDir"));
125 QVERIFY(roles
.contains("isLink"));
128 void KFileItemModelTest::testDefaultSortRole()
130 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
133 files
<< "c.txt" << "a.txt" << "b.txt";
135 m_testDir
->createFiles(files
);
137 m_model
->loadDirectory(m_testDir
->url());
138 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
140 QCOMPARE(m_model
->count(), 3);
141 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
142 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
143 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
146 void KFileItemModelTest::testDefaultGroupedSorting()
148 QCOMPARE(m_model
->groupedSorting(), false);
151 void KFileItemModelTest::testNewItems()
154 files
<< "a.txt" << "b.txt" << "c.txt";
155 m_testDir
->createFiles(files
);
157 m_model
->loadDirectory(m_testDir
->url());
158 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
160 QCOMPARE(m_model
->count(), 3);
162 QVERIFY(m_model
->isConsistent());
165 void KFileItemModelTest::testRemoveItems()
167 m_testDir
->createFile("a.txt");
168 m_testDir
->createFile("b.txt");
169 m_model
->loadDirectory(m_testDir
->url());
170 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
171 QCOMPARE(m_model
->count(), 2);
172 QVERIFY(m_model
->isConsistent());
174 m_testDir
->removeFile("a.txt");
175 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
176 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
177 QCOMPARE(m_model
->count(), 1);
178 QVERIFY(m_model
->isConsistent());
181 void KFileItemModelTest::testDirLoadingCompleted()
183 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
184 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
185 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
187 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
189 m_model
->loadDirectory(m_testDir
->url());
190 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
191 QCOMPARE(loadingCompletedSpy
.count(), 1);
192 QCOMPARE(itemsInsertedSpy
.count(), 1);
193 QCOMPARE(itemsRemovedSpy
.count(), 0);
194 QCOMPARE(m_model
->count(), 3);
196 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
197 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
198 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
199 QCOMPARE(loadingCompletedSpy
.count(), 2);
200 QCOMPARE(itemsInsertedSpy
.count(), 2);
201 QCOMPARE(itemsRemovedSpy
.count(), 0);
202 QCOMPARE(m_model
->count(), 5);
204 m_testDir
->removeFile("a.txt");
205 m_testDir
->createFile("f.txt");
206 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
207 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
208 QCOMPARE(loadingCompletedSpy
.count(), 3);
209 QCOMPARE(itemsInsertedSpy
.count(), 3);
210 QCOMPARE(itemsRemovedSpy
.count(), 1);
211 QCOMPARE(m_model
->count(), 5);
213 m_testDir
->removeFile("b.txt");
214 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
215 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
216 QCOMPARE(loadingCompletedSpy
.count(), 4);
217 QCOMPARE(itemsInsertedSpy
.count(), 3);
218 QCOMPARE(itemsRemovedSpy
.count(), 2);
219 QCOMPARE(m_model
->count(), 4);
221 QVERIFY(m_model
->isConsistent());
224 void KFileItemModelTest::testSetData()
226 m_testDir
->createFile("a.txt");
228 m_model
->loadDirectory(m_testDir
->url());
229 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
231 QHash
<QByteArray
, QVariant
> values
;
232 values
.insert("customRole1", "Test1");
233 values
.insert("customRole2", "Test2");
235 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
236 m_model
->setData(0, values
);
237 QCOMPARE(itemsChangedSpy
.count(), 1);
239 values
= m_model
->data(0);
240 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
241 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
242 QVERIFY(m_model
->isConsistent());
245 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
247 QTest::addColumn
<int>("changedIndex");
248 QTest::addColumn
<int>("changedRating");
249 QTest::addColumn
<bool>("expectMoveSignal");
250 QTest::addColumn
<int>("ratingIndex0");
251 QTest::addColumn
<int>("ratingIndex1");
252 QTest::addColumn
<int>("ratingIndex2");
255 // Index 0 = rating 2
256 // Index 1 = rating 4
257 // Index 2 = rating 6
259 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
260 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
261 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
263 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
264 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
265 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
268 void KFileItemModelTest::testSetDataWithModifiedSortRole()
270 QFETCH(int, changedIndex
);
271 QFETCH(int, changedRating
);
272 QFETCH(bool, expectMoveSignal
);
273 QFETCH(int, ratingIndex0
);
274 QFETCH(int, ratingIndex1
);
275 QFETCH(int, ratingIndex2
);
277 // Changing the value of a sort-role must result in
278 // a reordering of the items.
279 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
282 files
<< "a.txt" << "b.txt" << "c.txt";
283 m_testDir
->createFiles(files
);
285 m_model
->loadDirectory(m_testDir
->url());
286 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
288 // Fill the "rating" role of each file:
293 QHash
<QByteArray
, QVariant
> ratingA
;
294 ratingA
.insert("rating", 2);
295 m_model
->setData(0, ratingA
);
297 QHash
<QByteArray
, QVariant
> ratingB
;
298 ratingB
.insert("rating", 4);
299 m_model
->setData(1, ratingB
);
301 QHash
<QByteArray
, QVariant
> ratingC
;
302 ratingC
.insert("rating", 6);
303 m_model
->setData(2, ratingC
);
305 m_model
->setSortRole("rating");
306 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
307 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
308 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
310 // Now change the rating from a.txt. This usually results
311 // in reordering of the items.
312 QHash
<QByteArray
, QVariant
> rating
;
313 rating
.insert("rating", changedRating
);
314 m_model
->setData(changedIndex
, rating
);
316 if (expectMoveSignal
) {
317 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
320 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
321 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
322 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
323 QVERIFY(m_model
->isConsistent());
326 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
328 //QSKIP("Temporary disabled", SkipSingle);
330 // KFileItemModel prevents that inserting a punch of items sequentially
331 // results in an itemsInserted()-signal for each item. Instead internally
332 // a timeout is given that collects such operations and results in only
333 // one itemsInserted()-signal. However in this test we want to stress
334 // KFileItemModel to do a lot of insert operation and hence decrease
335 // the timeout to 1 millisecond.
336 m_testDir
->createFile("1");
337 m_model
->loadDirectory(m_testDir
->url());
338 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
339 QCOMPARE(m_model
->count(), 1);
341 // Insert 10 items for 20 times. After each insert operation the model consistency
343 QSet
<int> insertedItems
;
344 for (int i
= 0; i
< 20; ++i
) {
345 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
347 for (int j
= 0; j
< 10; ++j
) {
348 int itemName
= qrand();
349 while (insertedItems
.contains(itemName
)) {
352 insertedItems
.insert(itemName
);
354 m_testDir
->createFile(QString::number(itemName
));
357 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
358 if (spy
.count() == 0) {
359 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
362 QVERIFY(m_model
->isConsistent());
365 QCOMPARE(m_model
->count(), 201);
368 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
371 files
<< "B" << "E" << "G";
372 m_testDir
->createFiles(files
);
374 // Due to inserting the 3 items one item-range with index == 0 and
375 // count == 3 must be given
376 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
377 m_model
->loadDirectory(m_testDir
->url());
378 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
380 QCOMPARE(spy1
.count(), 1);
381 QList
<QVariant
> arguments
= spy1
.takeFirst();
382 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
383 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
385 // The indexes of the item-ranges must always be related to the model before
386 // the items have been inserted. Having:
389 // and inserting A, C, D, F the resulting model will be:
392 // and the item-ranges must be:
393 // index: 0, count: 1 for A
394 // index: 1, count: 2 for B, C
395 // index: 2, count: 1 for G
398 files
<< "A" << "C" << "D" << "F";
399 m_testDir
->createFiles(files
);
401 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
402 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
403 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
405 QCOMPARE(spy2
.count(), 1);
406 arguments
= spy2
.takeFirst();
407 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
408 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
411 void KFileItemModelTest::testExpandItems()
413 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
414 // Besides testing the basic item expansion functionality, the test makes sure that
415 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
416 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
417 // first three characters.
418 QSet
<QByteArray
> modelRoles
= m_model
->roles();
419 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
420 m_model
->setRoles(modelRoles
);
423 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
424 m_testDir
->createFiles(files
);
426 // Store the URLs of all folders in a set.
427 QSet
<KUrl
> allFolders
;
428 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
430 m_model
->loadDirectory(m_testDir
->url());
431 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
433 // So far, the model contains only "a/"
434 QCOMPARE(m_model
->count(), 1);
435 QVERIFY(m_model
->isExpandable(0));
436 QVERIFY(!m_model
->isExpanded(0));
437 QVERIFY(m_model
->expandedDirectories().empty());
439 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
441 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
442 m_model
->setExpanded(0, true);
443 QVERIFY(m_model
->isExpanded(0));
444 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
445 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
446 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
448 QCOMPARE(spyInserted
.count(), 1);
449 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
450 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
452 QVERIFY(m_model
->isExpandable(1));
453 QVERIFY(!m_model
->isExpanded(1));
454 QVERIFY(m_model
->isExpandable(2));
455 QVERIFY(!m_model
->isExpanded(2));
457 // Expand the folder "a/a/" -> "a/a/1" becomes visible
458 m_model
->setExpanded(1, true);
459 QVERIFY(m_model
->isExpanded(1));
460 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
461 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
462 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
464 QCOMPARE(spyInserted
.count(), 1);
465 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
466 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
468 QVERIFY(!m_model
->isExpandable(2));
469 QVERIFY(!m_model
->isExpanded(2));
471 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
472 m_model
->setExpanded(3, true);
473 QVERIFY(m_model
->isExpanded(3));
474 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
475 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
476 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
478 QCOMPARE(spyInserted
.count(), 1);
479 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
480 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
482 QVERIFY(!m_model
->isExpandable(4));
483 QVERIFY(!m_model
->isExpanded(4));
485 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
487 // Collapse the top-level folder -> all other items should disappear
488 m_model
->setExpanded(0, false);
489 QVERIFY(!m_model
->isExpanded(0));
490 QCOMPARE(m_model
->count(), 1);
491 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
493 QCOMPARE(spyRemoved
.count(), 1);
494 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
495 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
496 QVERIFY(m_model
->isConsistent());
498 // Clear the model, reload the folder and try to restore the expanded folders.
500 QCOMPARE(m_model
->count(), 0);
501 QVERIFY(m_model
->expandedDirectories().empty());
503 m_model
->loadDirectory(m_testDir
->url());
504 m_model
->restoreExpandedDirectories(allFolders
);
505 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
506 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
507 QVERIFY(m_model
->isExpanded(0));
508 QVERIFY(m_model
->isExpanded(1));
509 QVERIFY(!m_model
->isExpanded(2));
510 QVERIFY(m_model
->isExpanded(3));
511 QVERIFY(!m_model
->isExpanded(4));
512 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
513 QVERIFY(m_model
->isConsistent());
515 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
516 // This is how DolphinView restores the expanded folders when navigating in history.
517 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
518 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
519 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
520 m_model
->restoreExpandedDirectories(allFolders
);
521 m_model
->loadDirectory(m_testDir
->url());
522 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
523 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
524 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
527 void KFileItemModelTest::testExpandParentItems()
529 // Create a tree structure of folders:
537 QSet
<QByteArray
> modelRoles
= m_model
->roles();
538 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
539 m_model
->setRoles(modelRoles
);
542 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
543 m_testDir
->createFiles(files
);
545 m_model
->loadDirectory(m_testDir
->url());
546 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
548 // So far, the model contains only "a 1/" and "a2/".
549 QCOMPARE(m_model
->count(), 2);
550 QVERIFY(m_model
->expandedDirectories().empty());
552 // Expand the parents of "a2/b2/c2".
553 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
554 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
556 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
557 // It's important that only the parents of "a1/b1/c1" are expanded.
558 QCOMPARE(m_model
->count(), 4);
559 QVERIFY(!m_model
->isExpanded(0));
560 QVERIFY(m_model
->isExpanded(1));
561 QVERIFY(m_model
->isExpanded(2));
562 QVERIFY(!m_model
->isExpanded(3));
564 // Expand the parents of "a 1/b1".
565 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
566 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
568 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
569 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
570 QCOMPARE(m_model
->count(), 5);
571 QVERIFY(m_model
->isExpanded(0));
572 QVERIFY(!m_model
->isExpanded(1));
573 QVERIFY(m_model
->isExpanded(2));
574 QVERIFY(m_model
->isExpanded(3));
575 QVERIFY(!m_model
->isExpanded(4));
576 QVERIFY(m_model
->isConsistent());
580 * Renaming an expanded folder by prepending its name with a dot makes it
581 * hidden. Verify that this does not cause an inconsistent model state and
582 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
584 void KFileItemModelTest::testMakeExpandedItemHidden()
586 QSet
<QByteArray
> modelRoles
= m_model
->roles();
587 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
588 m_model
->setRoles(modelRoles
);
591 m_testDir
->createFile("1a/2a/3a");
592 m_testDir
->createFile("1a/2a/3b");
593 m_testDir
->createFile("1a/2b");
594 m_testDir
->createFile("1b");
596 m_model
->loadDirectory(m_testDir
->url());
597 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
599 // So far, the model contains only "1a/" and "1b".
600 QCOMPARE(m_model
->count(), 2);
601 m_model
->setExpanded(0, true);
602 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
604 // Now "1a/2a" and "1a/2b" have appeared.
605 QCOMPARE(m_model
->count(), 4);
606 m_model
->setExpanded(1, true);
607 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
608 QCOMPARE(m_model
->count(), 6);
610 // Rename "1a/2" and make it hidden.
611 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
612 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
614 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
615 bool ok
= job
->exec();
617 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
619 // "1a/2" and its subfolders have disappeared now.
620 QVERIFY(m_model
->isConsistent());
621 QCOMPARE(m_model
->count(), 3);
623 m_model
->setExpanded(0, false);
624 QCOMPARE(m_model
->count(), 2);
628 void KFileItemModelTest::testSorting()
630 // Create some files with different sizes and modification times to check the different sorting options
631 QDateTime now
= QDateTime::currentDateTime();
633 QSet
<QByteArray
> roles
;
634 roles
.insert("text");
635 roles
.insert("isExpanded");
636 roles
.insert("isExpandable");
637 roles
.insert("expandedParentsCount");
638 m_model
->setRoles(roles
);
640 m_testDir
->createDir("c/c-2");
641 m_testDir
->createFile("c/c-2/c-3");
642 m_testDir
->createFile("c/c-1");
644 m_testDir
->createFile("a", "A file", now
.addDays(-3));
645 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
646 m_testDir
->createDir("c", now
.addDays(-2));
647 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
648 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
649 m_testDir
->createFile(".f");
651 m_model
->loadDirectory(m_testDir
->url());
652 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
654 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
655 m_model
->setExpanded(index
, true);
656 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
658 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
659 m_model
->setExpanded(index
, true);
660 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
662 // Default: Sort by Name, ascending
663 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
664 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
665 QVERIFY(m_model
->sortDirectoriesFirst());
666 QVERIFY(!m_model
->showHiddenFiles());
667 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
669 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
671 // Sort by Name, ascending, 'Sort Folders First' disabled
672 m_model
->setSortDirectoriesFirst(false);
673 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
674 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
675 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
676 QCOMPARE(spyItemsMoved
.count(), 1);
677 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
679 // Sort by Name, descending
680 m_model
->setSortDirectoriesFirst(true);
681 m_model
->setSortOrder(Qt::DescendingOrder
);
682 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
683 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
684 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
685 QCOMPARE(spyItemsMoved
.count(), 2);
686 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
687 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
689 // Sort by Date, descending
690 m_model
->setSortDirectoriesFirst(true);
691 m_model
->setSortRole("date");
692 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
693 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
694 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
695 QCOMPARE(spyItemsMoved
.count(), 1);
696 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
698 // Sort by Date, ascending
699 m_model
->setSortOrder(Qt::AscendingOrder
);
700 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
701 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
702 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
703 QCOMPARE(spyItemsMoved
.count(), 1);
704 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
706 // Sort by Date, ascending, 'Sort Folders First' disabled
707 m_model
->setSortDirectoriesFirst(false);
708 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
709 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
710 QVERIFY(!m_model
->sortDirectoriesFirst());
711 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
712 QCOMPARE(spyItemsMoved
.count(), 1);
713 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
715 // Sort by Name, ascending, 'Sort Folders First' disabled
716 m_model
->setSortRole("text");
717 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
718 QVERIFY(!m_model
->sortDirectoriesFirst());
719 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
720 QCOMPARE(spyItemsMoved
.count(), 1);
721 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
723 // Sort by Size, ascending, 'Sort Folders First' disabled
724 m_model
->setSortRole("size");
725 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
726 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
727 QVERIFY(!m_model
->sortDirectoriesFirst());
728 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
729 QCOMPARE(spyItemsMoved
.count(), 1);
730 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
732 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
733 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
734 "another signal", SkipSingle
);
735 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
737 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
738 m_model
->setSortDirectoriesFirst(true);
739 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
740 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
741 QVERIFY(m_model
->sortDirectoriesFirst());
742 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
743 QCOMPARE(spyItemsMoved
.count(), 0);
745 // Sort by Size, descending, 'Sort Folders First' enabled
746 m_model
->setSortOrder(Qt::DescendingOrder
);
747 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
748 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
749 QVERIFY(m_model
->sortDirectoriesFirst());
750 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
751 QCOMPARE(spyItemsMoved
.count(), 1);
752 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
754 // TODO: Sort by other roles; show/hide hidden files
757 void KFileItemModelTest::testIndexForKeyboardSearch()
760 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
761 m_testDir
->createFiles(files
);
763 m_model
->loadDirectory(m_testDir
->url());
764 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
766 // Search from index 0
767 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
768 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
769 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
770 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
771 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
772 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
773 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
774 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
775 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
776 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
777 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
779 // Start a search somewhere in the middle
780 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
781 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
782 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
783 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
785 // Test searches that go past the last item back to index 0
786 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
787 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
788 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
789 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
791 // Test searches that yield no result
792 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
793 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
794 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
795 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
796 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
798 // Test upper case searches (note that search is case insensitive)
799 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
800 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
801 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
802 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
804 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
807 void KFileItemModelTest::testNameFilter()
810 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
811 m_testDir
->createFiles(files
);
813 m_model
->loadDirectory(m_testDir
->url());
814 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
816 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
817 QCOMPARE(m_model
->count(), 3);
819 m_model
->setNameFilter("A2"); // Shows only A2
820 QCOMPARE(m_model
->count(), 1);
822 m_model
->setNameFilter("A2"); // Shows only A1
823 QCOMPARE(m_model
->count(), 1);
825 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
826 QCOMPARE(m_model
->count(), 2);
828 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
829 QCOMPARE(m_model
->count(), 2);
831 m_model
->setNameFilter(QString()); // Shows again all items
832 QCOMPARE(m_model
->count(), 5);
836 * Verifies that we do not crash when adding a KFileItem with an empty path.
837 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
838 * tried to always read the first character of the path, even if the path is empty.
840 void KFileItemModelTest::testEmptyPath()
842 QSet
<QByteArray
> roles
;
843 roles
.insert("text");
844 roles
.insert("isExpanded");
845 roles
.insert("isExpandable");
846 roles
.insert("expandedParentsCount");
847 m_model
->setRoles(roles
);
850 QVERIFY(emptyUrl
.path().isEmpty());
852 const KUrl
url("file:///test/");
855 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
856 m_model
->slotItemsAdded(emptyUrl
, items
);
857 m_model
->slotCompleted();
861 * Verifies that the 'isExpanded' state of folders does not change when the
862 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
864 void KFileItemModelTest::testRefreshExpandedItem()
866 QSet
<QByteArray
> modelRoles
= m_model
->roles();
867 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
868 m_model
->setRoles(modelRoles
);
871 files
<< "a/1" << "a/2" << "3" << "4";
872 m_testDir
->createFiles(files
);
874 m_model
->loadDirectory(m_testDir
->url());
875 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
876 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
878 m_model
->setExpanded(0, true);
879 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
880 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
881 QVERIFY(m_model
->isExpanded(0));
883 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
885 const KFileItem item
= m_model
->fileItem(0);
886 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
887 QVERIFY(!spyItemsChanged
.isEmpty());
889 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
890 QVERIFY(m_model
->isExpanded(0));
894 * Verify that removing hidden files and folders from the model does not
895 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
897 void KFileItemModelTest::testRemoveHiddenItems()
899 m_testDir
->createDir(".a");
900 m_testDir
->createDir(".b");
901 m_testDir
->createDir("c");
902 m_testDir
->createDir("d");
903 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
905 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
906 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
908 m_model
->setShowHiddenFiles(true);
909 m_model
->loadDirectory(m_testDir
->url());
910 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
911 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
912 QCOMPARE(spyItemsInserted
.count(), 1);
913 QCOMPARE(spyItemsRemoved
.count(), 0);
914 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
915 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
917 m_model
->setShowHiddenFiles(false);
918 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
919 QCOMPARE(spyItemsInserted
.count(), 0);
920 QCOMPARE(spyItemsRemoved
.count(), 1);
921 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
922 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
924 m_model
->setShowHiddenFiles(true);
925 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
926 QCOMPARE(spyItemsInserted
.count(), 1);
927 QCOMPARE(spyItemsRemoved
.count(), 0);
928 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
929 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
932 QCOMPARE(itemsInModel(), QStringList());
933 QCOMPARE(spyItemsInserted
.count(), 0);
934 QCOMPARE(spyItemsRemoved
.count(), 1);
935 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
936 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
938 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
939 // Verify that this does not make the model crash.
940 m_model
->setShowHiddenFiles(false);
944 * Verify that filtered items are removed when their parent is collapsed.
946 void KFileItemModelTest::collapseParentOfHiddenItems()
948 QSet
<QByteArray
> modelRoles
= m_model
->roles();
949 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
950 m_model
->setRoles(modelRoles
);
953 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
954 m_testDir
->createFiles(files
);
956 m_model
->loadDirectory(m_testDir
->url());
957 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
958 QCOMPARE(m_model
->count(), 1); // Only "a/"
961 m_model
->setExpanded(0, true);
962 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
963 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
966 m_model
->setExpanded(1, true);
967 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
968 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
971 m_model
->setExpanded(2, true);
972 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
973 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"
975 // Set a name filter that matches nothing -> only the expanded folders remain.
976 m_model
->setNameFilter("xyz");
977 QCOMPARE(m_model
->count(), 3);
978 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
980 // Collapse the folder "a/".
981 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
982 m_model
->setExpanded(0, false);
983 QCOMPARE(spyItemsRemoved
.count(), 1);
984 QCOMPARE(m_model
->count(), 1);
985 QCOMPARE(itemsInModel(), QStringList() << "a");
987 // Remove the filter -> no files should appear (and we should not get a crash).
988 m_model
->setNameFilter(QString());
989 QCOMPARE(m_model
->count(), 1);
993 * Verify that filtered items are removed when their parent is deleted.
995 void KFileItemModelTest::removeParentOfHiddenItems()
997 QSet
<QByteArray
> modelRoles
= m_model
->roles();
998 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
999 m_model
->setRoles(modelRoles
);
1002 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1003 m_testDir
->createFiles(files
);
1005 m_model
->loadDirectory(m_testDir
->url());
1006 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1007 QCOMPARE(m_model
->count(), 1); // Only "a/"
1010 m_model
->setExpanded(0, true);
1011 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1012 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1015 m_model
->setExpanded(1, true);
1016 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1017 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1020 m_model
->setExpanded(2, true);
1021 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1022 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"
1024 // Set a name filter that matches nothing -> only the expanded folders remain.
1025 m_model
->setNameFilter("xyz");
1026 QCOMPARE(m_model
->count(), 3);
1027 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1029 // Simulate the deletion of the directory "a/b/".
1030 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1031 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1032 QCOMPARE(spyItemsRemoved
.count(), 1);
1033 QCOMPARE(m_model
->count(), 1);
1034 QCOMPARE(itemsInModel(), QStringList() << "a");
1036 // Remove the filter -> only the file "a/1" should appear.
1037 m_model
->setNameFilter(QString());
1038 QCOMPARE(m_model
->count(), 2);
1039 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1043 * Create a tree structure where parent-child relationships can not be
1044 * determined by parsing the URLs, and verify that KFileItemModel
1045 * handles them correctly.
1047 void KFileItemModelTest::testGeneralParentChildRelationships()
1049 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1050 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1051 m_model
->setRoles(modelRoles
);
1054 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1055 m_testDir
->createFiles(files
);
1057 m_model
->loadDirectory(m_testDir
->url());
1058 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1059 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1061 // Expand all folders.
1062 m_model
->setExpanded(0, true);
1063 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1064 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1066 m_model
->setExpanded(1, true);
1067 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1068 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1070 m_model
->setExpanded(3, true);
1071 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1072 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1074 m_model
->setExpanded(4, true);
1075 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1076 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1078 // Add some more children and grand-children.
1079 const KUrl parent1
= m_model
->fileItem(0).url();
1080 const KUrl parent2
= m_model
->fileItem(3).url();
1081 const KUrl realChild1
= m_model
->fileItem(1).url();
1082 const KUrl realChild2
= m_model
->fileItem(4).url();
1084 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1085 m_model
->slotCompleted();
1086 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1088 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1089 m_model
->slotCompleted();
1090 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1092 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1093 m_model
->slotCompleted();
1094 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1096 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1097 m_model
->slotCompleted();
1098 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1100 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1101 m_model
->slotCompleted();
1102 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1104 // Set a name filter that matches nothing -> only expanded folders remain.
1105 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1106 m_model
->setNameFilter("xyz");
1107 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1108 QCOMPARE(itemsRemovedSpy
.count(), 1);
1109 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1110 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1111 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1113 // Collapse "parent1".
1114 m_model
->setExpanded(0, false);
1115 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1116 QCOMPARE(itemsRemovedSpy
.count(), 1);
1117 arguments
= itemsRemovedSpy
.takeFirst();
1118 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1119 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1121 // Remove "parent2".
1122 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1123 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1124 QCOMPARE(itemsRemovedSpy
.count(), 1);
1125 arguments
= itemsRemovedSpy
.takeFirst();
1126 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1127 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1129 // Clear filter, verify that no items reappear.
1130 m_model
->setNameFilter(QString());
1131 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1134 QStringList
KFileItemModelTest::itemsInModel() const
1137 for (int i
= 0; i
< m_model
->count(); i
++) {
1138 items
<< m_model
->data(i
).value("text").toString();
1143 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1145 #include "kfileitemmodeltest.moc"