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 testRemoveHiddenItems();
82 void collapseParentOfHiddenItems();
83 void removeParentOfHiddenItems();
84 void testGeneralParentChildRelationships();
87 QStringList
itemsInModel() const;
90 KFileItemModel
* m_model
;
94 void KFileItemModelTest::init()
96 // The item-model tests result in a huge number of debugging
97 // output from kdelibs. Only show critical and fatal messages.
98 qInstallMsgHandler(myMessageOutput
);
100 qRegisterMetaType
<KItemRange
>("KItemRange");
101 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
102 qRegisterMetaType
<KFileItemList
>("KFileItemList");
104 m_testDir
= new TestDir();
105 m_model
= new KFileItemModel();
106 m_model
->m_dirLister
->setAutoUpdate(false);
109 void KFileItemModelTest::cleanup()
118 void KFileItemModelTest::testDefaultRoles()
120 const QSet
<QByteArray
> roles
= m_model
->roles();
121 QCOMPARE(roles
.count(), 3);
122 QVERIFY(roles
.contains("text"));
123 QVERIFY(roles
.contains("isDir"));
124 QVERIFY(roles
.contains("isLink"));
127 void KFileItemModelTest::testDefaultSortRole()
129 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
132 files
<< "c.txt" << "a.txt" << "b.txt";
134 m_testDir
->createFiles(files
);
136 m_model
->loadDirectory(m_testDir
->url());
137 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
139 QCOMPARE(m_model
->count(), 3);
140 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
141 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
142 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
145 void KFileItemModelTest::testDefaultGroupedSorting()
147 QCOMPARE(m_model
->groupedSorting(), false);
150 void KFileItemModelTest::testNewItems()
153 files
<< "a.txt" << "b.txt" << "c.txt";
154 m_testDir
->createFiles(files
);
156 m_model
->loadDirectory(m_testDir
->url());
157 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
159 QCOMPARE(m_model
->count(), 3);
161 QVERIFY(m_model
->isConsistent());
164 void KFileItemModelTest::testRemoveItems()
166 m_testDir
->createFile("a.txt");
167 m_testDir
->createFile("b.txt");
168 m_model
->loadDirectory(m_testDir
->url());
169 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
170 QCOMPARE(m_model
->count(), 2);
171 QVERIFY(m_model
->isConsistent());
173 m_testDir
->removeFile("a.txt");
174 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
175 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
176 QCOMPARE(m_model
->count(), 1);
177 QVERIFY(m_model
->isConsistent());
180 void KFileItemModelTest::testDirLoadingCompleted()
182 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
183 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
184 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
186 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
188 m_model
->loadDirectory(m_testDir
->url());
189 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
190 QCOMPARE(loadingCompletedSpy
.count(), 1);
191 QCOMPARE(itemsInsertedSpy
.count(), 1);
192 QCOMPARE(itemsRemovedSpy
.count(), 0);
193 QCOMPARE(m_model
->count(), 3);
195 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
196 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
197 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
198 QCOMPARE(loadingCompletedSpy
.count(), 2);
199 QCOMPARE(itemsInsertedSpy
.count(), 2);
200 QCOMPARE(itemsRemovedSpy
.count(), 0);
201 QCOMPARE(m_model
->count(), 5);
203 m_testDir
->removeFile("a.txt");
204 m_testDir
->createFile("f.txt");
205 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
206 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
207 QCOMPARE(loadingCompletedSpy
.count(), 3);
208 QCOMPARE(itemsInsertedSpy
.count(), 3);
209 QCOMPARE(itemsRemovedSpy
.count(), 1);
210 QCOMPARE(m_model
->count(), 5);
212 m_testDir
->removeFile("b.txt");
213 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
214 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
215 QCOMPARE(loadingCompletedSpy
.count(), 4);
216 QCOMPARE(itemsInsertedSpy
.count(), 3);
217 QCOMPARE(itemsRemovedSpy
.count(), 2);
218 QCOMPARE(m_model
->count(), 4);
220 QVERIFY(m_model
->isConsistent());
223 void KFileItemModelTest::testSetData()
225 m_testDir
->createFile("a.txt");
227 m_model
->loadDirectory(m_testDir
->url());
228 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
230 QHash
<QByteArray
, QVariant
> values
;
231 values
.insert("customRole1", "Test1");
232 values
.insert("customRole2", "Test2");
234 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
235 m_model
->setData(0, values
);
236 QCOMPARE(itemsChangedSpy
.count(), 1);
238 values
= m_model
->data(0);
239 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
240 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
241 QVERIFY(m_model
->isConsistent());
244 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
246 QTest::addColumn
<int>("changedIndex");
247 QTest::addColumn
<int>("changedRating");
248 QTest::addColumn
<bool>("expectMoveSignal");
249 QTest::addColumn
<int>("ratingIndex0");
250 QTest::addColumn
<int>("ratingIndex1");
251 QTest::addColumn
<int>("ratingIndex2");
254 // Index 0 = rating 2
255 // Index 1 = rating 4
256 // Index 2 = rating 6
258 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
259 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
260 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
262 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
263 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
264 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
267 void KFileItemModelTest::testSetDataWithModifiedSortRole()
269 QFETCH(int, changedIndex
);
270 QFETCH(int, changedRating
);
271 QFETCH(bool, expectMoveSignal
);
272 QFETCH(int, ratingIndex0
);
273 QFETCH(int, ratingIndex1
);
274 QFETCH(int, ratingIndex2
);
276 // Changing the value of a sort-role must result in
277 // a reordering of the items.
278 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
281 files
<< "a.txt" << "b.txt" << "c.txt";
282 m_testDir
->createFiles(files
);
284 m_model
->loadDirectory(m_testDir
->url());
285 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
287 // Fill the "rating" role of each file:
292 QHash
<QByteArray
, QVariant
> ratingA
;
293 ratingA
.insert("rating", 2);
294 m_model
->setData(0, ratingA
);
296 QHash
<QByteArray
, QVariant
> ratingB
;
297 ratingB
.insert("rating", 4);
298 m_model
->setData(1, ratingB
);
300 QHash
<QByteArray
, QVariant
> ratingC
;
301 ratingC
.insert("rating", 6);
302 m_model
->setData(2, ratingC
);
304 m_model
->setSortRole("rating");
305 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
306 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
307 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
309 // Now change the rating from a.txt. This usually results
310 // in reordering of the items.
311 QHash
<QByteArray
, QVariant
> rating
;
312 rating
.insert("rating", changedRating
);
313 m_model
->setData(changedIndex
, rating
);
315 if (expectMoveSignal
) {
316 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
319 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
320 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
321 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
322 QVERIFY(m_model
->isConsistent());
325 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
327 //QSKIP("Temporary disabled", SkipSingle);
329 // KFileItemModel prevents that inserting a punch of items sequentially
330 // results in an itemsInserted()-signal for each item. Instead internally
331 // a timeout is given that collects such operations and results in only
332 // one itemsInserted()-signal. However in this test we want to stress
333 // KFileItemModel to do a lot of insert operation and hence decrease
334 // the timeout to 1 millisecond.
335 m_testDir
->createFile("1");
336 m_model
->loadDirectory(m_testDir
->url());
337 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
338 QCOMPARE(m_model
->count(), 1);
340 // Insert 10 items for 20 times. After each insert operation the model consistency
342 QSet
<int> insertedItems
;
343 for (int i
= 0; i
< 20; ++i
) {
344 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
346 for (int j
= 0; j
< 10; ++j
) {
347 int itemName
= qrand();
348 while (insertedItems
.contains(itemName
)) {
351 insertedItems
.insert(itemName
);
353 m_testDir
->createFile(QString::number(itemName
));
356 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
357 if (spy
.count() == 0) {
358 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
361 QVERIFY(m_model
->isConsistent());
364 QCOMPARE(m_model
->count(), 201);
367 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
370 files
<< "B" << "E" << "G";
371 m_testDir
->createFiles(files
);
373 // Due to inserting the 3 items one item-range with index == 0 and
374 // count == 3 must be given
375 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
376 m_model
->loadDirectory(m_testDir
->url());
377 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
379 QCOMPARE(spy1
.count(), 1);
380 QList
<QVariant
> arguments
= spy1
.takeFirst();
381 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
382 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
384 // The indexes of the item-ranges must always be related to the model before
385 // the items have been inserted. Having:
388 // and inserting A, C, D, F the resulting model will be:
391 // and the item-ranges must be:
392 // index: 0, count: 1 for A
393 // index: 1, count: 2 for B, C
394 // index: 2, count: 1 for G
397 files
<< "A" << "C" << "D" << "F";
398 m_testDir
->createFiles(files
);
400 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
401 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
402 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
404 QCOMPARE(spy2
.count(), 1);
405 arguments
= spy2
.takeFirst();
406 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
407 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
410 void KFileItemModelTest::testExpandItems()
412 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
413 // Besides testing the basic item expansion functionality, the test makes sure that
414 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
415 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
416 // first three characters.
417 QSet
<QByteArray
> modelRoles
= m_model
->roles();
418 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
419 m_model
->setRoles(modelRoles
);
422 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
423 m_testDir
->createFiles(files
);
425 // Store the URLs of all folders in a set.
426 QSet
<KUrl
> allFolders
;
427 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
429 m_model
->loadDirectory(m_testDir
->url());
430 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
432 // So far, the model contains only "a/"
433 QCOMPARE(m_model
->count(), 1);
434 QVERIFY(m_model
->isExpandable(0));
435 QVERIFY(!m_model
->isExpanded(0));
436 QVERIFY(m_model
->expandedDirectories().empty());
438 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
440 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
441 m_model
->setExpanded(0, true);
442 QVERIFY(m_model
->isExpanded(0));
443 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
444 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
445 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
447 QCOMPARE(spyInserted
.count(), 1);
448 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
449 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
451 QVERIFY(m_model
->isExpandable(1));
452 QVERIFY(!m_model
->isExpanded(1));
453 QVERIFY(m_model
->isExpandable(2));
454 QVERIFY(!m_model
->isExpanded(2));
456 // Expand the folder "a/a/" -> "a/a/1" becomes visible
457 m_model
->setExpanded(1, true);
458 QVERIFY(m_model
->isExpanded(1));
459 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
460 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
461 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
463 QCOMPARE(spyInserted
.count(), 1);
464 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
465 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
467 QVERIFY(!m_model
->isExpandable(2));
468 QVERIFY(!m_model
->isExpanded(2));
470 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
471 m_model
->setExpanded(3, true);
472 QVERIFY(m_model
->isExpanded(3));
473 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
474 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
475 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
477 QCOMPARE(spyInserted
.count(), 1);
478 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
479 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
481 QVERIFY(!m_model
->isExpandable(4));
482 QVERIFY(!m_model
->isExpanded(4));
484 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
486 // Collapse the top-level folder -> all other items should disappear
487 m_model
->setExpanded(0, false);
488 QVERIFY(!m_model
->isExpanded(0));
489 QCOMPARE(m_model
->count(), 1);
490 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
492 QCOMPARE(spyRemoved
.count(), 1);
493 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
494 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
495 QVERIFY(m_model
->isConsistent());
497 // Clear the model, reload the folder and try to restore the expanded folders.
499 QCOMPARE(m_model
->count(), 0);
500 QVERIFY(m_model
->expandedDirectories().empty());
502 m_model
->loadDirectory(m_testDir
->url());
503 m_model
->restoreExpandedDirectories(allFolders
);
504 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
505 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
506 QVERIFY(m_model
->isExpanded(0));
507 QVERIFY(m_model
->isExpanded(1));
508 QVERIFY(!m_model
->isExpanded(2));
509 QVERIFY(m_model
->isExpanded(3));
510 QVERIFY(!m_model
->isExpanded(4));
511 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
512 QVERIFY(m_model
->isConsistent());
514 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
515 // This is how DolphinView restores the expanded folders when navigating in history.
516 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
517 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
518 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
519 m_model
->restoreExpandedDirectories(allFolders
);
520 m_model
->loadDirectory(m_testDir
->url());
521 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
522 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
523 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
526 void KFileItemModelTest::testExpandParentItems()
528 // Create a tree structure of folders:
536 QSet
<QByteArray
> modelRoles
= m_model
->roles();
537 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
538 m_model
->setRoles(modelRoles
);
541 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
542 m_testDir
->createFiles(files
);
544 m_model
->loadDirectory(m_testDir
->url());
545 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
547 // So far, the model contains only "a 1/" and "a2/".
548 QCOMPARE(m_model
->count(), 2);
549 QVERIFY(m_model
->expandedDirectories().empty());
551 // Expand the parents of "a2/b2/c2".
552 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
553 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
555 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
556 // It's important that only the parents of "a1/b1/c1" are expanded.
557 QCOMPARE(m_model
->count(), 4);
558 QVERIFY(!m_model
->isExpanded(0));
559 QVERIFY(m_model
->isExpanded(1));
560 QVERIFY(m_model
->isExpanded(2));
561 QVERIFY(!m_model
->isExpanded(3));
563 // Expand the parents of "a 1/b1".
564 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
565 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
567 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
568 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
569 QCOMPARE(m_model
->count(), 5);
570 QVERIFY(m_model
->isExpanded(0));
571 QVERIFY(!m_model
->isExpanded(1));
572 QVERIFY(m_model
->isExpanded(2));
573 QVERIFY(m_model
->isExpanded(3));
574 QVERIFY(!m_model
->isExpanded(4));
575 QVERIFY(m_model
->isConsistent());
579 * Renaming an expanded folder by prepending its name with a dot makes it
580 * hidden. Verify that this does not cause an inconsistent model state and
581 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
583 void KFileItemModelTest::testMakeExpandedItemHidden()
585 QSet
<QByteArray
> modelRoles
= m_model
->roles();
586 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
587 m_model
->setRoles(modelRoles
);
590 m_testDir
->createFile("1a/2a/3a");
591 m_testDir
->createFile("1a/2a/3b");
592 m_testDir
->createFile("1a/2b");
593 m_testDir
->createFile("1b");
595 m_model
->loadDirectory(m_testDir
->url());
596 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
598 // So far, the model contains only "1a/" and "1b".
599 QCOMPARE(m_model
->count(), 2);
600 m_model
->setExpanded(0, true);
601 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
603 // Now "1a/2a" and "1a/2b" have appeared.
604 QCOMPARE(m_model
->count(), 4);
605 m_model
->setExpanded(1, true);
606 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
607 QCOMPARE(m_model
->count(), 6);
609 // Rename "1a/2" and make it hidden.
610 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
611 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
613 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
614 bool ok
= job
->exec();
616 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
618 // "1a/2" and its subfolders have disappeared now.
619 QVERIFY(m_model
->isConsistent());
620 QCOMPARE(m_model
->count(), 3);
622 m_model
->setExpanded(0, false);
623 QCOMPARE(m_model
->count(), 2);
627 void KFileItemModelTest::testSorting()
629 // Create some files with different sizes and modification times to check the different sorting options
630 QDateTime now
= QDateTime::currentDateTime();
632 QSet
<QByteArray
> roles
;
633 roles
.insert("text");
634 roles
.insert("isExpanded");
635 roles
.insert("isExpandable");
636 roles
.insert("expandedParentsCount");
637 m_model
->setRoles(roles
);
639 m_testDir
->createDir("c/c-2");
640 m_testDir
->createFile("c/c-2/c-3");
641 m_testDir
->createFile("c/c-1");
643 m_testDir
->createFile("a", "A file", now
.addDays(-3));
644 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
645 m_testDir
->createDir("c", now
.addDays(-2));
646 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
647 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
648 m_testDir
->createFile(".f");
650 m_model
->loadDirectory(m_testDir
->url());
651 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
653 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
654 m_model
->setExpanded(index
, true);
655 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
657 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
658 m_model
->setExpanded(index
, true);
659 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
661 // Default: Sort by Name, ascending
662 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
663 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
664 QVERIFY(m_model
->sortDirectoriesFirst());
665 QVERIFY(!m_model
->showHiddenFiles());
666 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
668 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
670 // Sort by Name, ascending, 'Sort Folders First' disabled
671 m_model
->setSortDirectoriesFirst(false);
672 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
673 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
674 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
675 QCOMPARE(spyItemsMoved
.count(), 1);
676 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
678 // Sort by Name, descending
679 m_model
->setSortDirectoriesFirst(true);
680 m_model
->setSortOrder(Qt::DescendingOrder
);
681 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
682 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
683 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
684 QCOMPARE(spyItemsMoved
.count(), 2);
685 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
686 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
688 // Sort by Date, descending
689 m_model
->setSortDirectoriesFirst(true);
690 m_model
->setSortRole("date");
691 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
692 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
693 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
694 QCOMPARE(spyItemsMoved
.count(), 1);
695 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
697 // Sort by Date, ascending
698 m_model
->setSortOrder(Qt::AscendingOrder
);
699 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
700 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
701 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
702 QCOMPARE(spyItemsMoved
.count(), 1);
703 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
705 // Sort by Date, ascending, 'Sort Folders First' disabled
706 m_model
->setSortDirectoriesFirst(false);
707 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
708 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
709 QVERIFY(!m_model
->sortDirectoriesFirst());
710 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
711 QCOMPARE(spyItemsMoved
.count(), 1);
712 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
714 // Sort by Name, ascending, 'Sort Folders First' disabled
715 m_model
->setSortRole("text");
716 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
717 QVERIFY(!m_model
->sortDirectoriesFirst());
718 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
719 QCOMPARE(spyItemsMoved
.count(), 1);
720 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
722 // Sort by Size, ascending, 'Sort Folders First' disabled
723 m_model
->setSortRole("size");
724 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
725 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
726 QVERIFY(!m_model
->sortDirectoriesFirst());
727 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
728 QCOMPARE(spyItemsMoved
.count(), 1);
729 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
731 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
732 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
733 "another signal", SkipSingle
);
734 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
736 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
737 m_model
->setSortDirectoriesFirst(true);
738 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
739 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
740 QVERIFY(m_model
->sortDirectoriesFirst());
741 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
742 QCOMPARE(spyItemsMoved
.count(), 0);
744 // Sort by Size, descending, 'Sort Folders First' enabled
745 m_model
->setSortOrder(Qt::DescendingOrder
);
746 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
747 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
748 QVERIFY(m_model
->sortDirectoriesFirst());
749 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
750 QCOMPARE(spyItemsMoved
.count(), 1);
751 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
753 // TODO: Sort by other roles; show/hide hidden files
756 void KFileItemModelTest::testIndexForKeyboardSearch()
759 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
760 m_testDir
->createFiles(files
);
762 m_model
->loadDirectory(m_testDir
->url());
763 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
765 // Search from index 0
766 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
767 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
768 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
769 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
770 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
771 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
772 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
773 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
774 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
775 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
776 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
778 // Start a search somewhere in the middle
779 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
780 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
781 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
782 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
784 // Test searches that go past the last item back to index 0
785 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
786 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
787 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
788 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
790 // Test searches that yield no result
791 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
792 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
793 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
794 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
795 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
797 // Test upper case searches (note that search is case insensitive)
798 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
799 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
800 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
801 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
803 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
806 void KFileItemModelTest::testNameFilter()
809 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
810 m_testDir
->createFiles(files
);
812 m_model
->loadDirectory(m_testDir
->url());
813 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
815 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
816 QCOMPARE(m_model
->count(), 3);
818 m_model
->setNameFilter("A2"); // Shows only A2
819 QCOMPARE(m_model
->count(), 1);
821 m_model
->setNameFilter("A2"); // Shows only A1
822 QCOMPARE(m_model
->count(), 1);
824 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
825 QCOMPARE(m_model
->count(), 2);
827 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
828 QCOMPARE(m_model
->count(), 2);
830 m_model
->setNameFilter(QString()); // Shows again all items
831 QCOMPARE(m_model
->count(), 5);
835 * Verifies that we do not crash when adding a KFileItem with an empty path.
836 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
837 * tried to always read the first character of the path, even if the path is empty.
839 void KFileItemModelTest::testEmptyPath()
841 QSet
<QByteArray
> roles
;
842 roles
.insert("text");
843 roles
.insert("isExpanded");
844 roles
.insert("isExpandable");
845 roles
.insert("expandedParentsCount");
846 m_model
->setRoles(roles
);
849 QVERIFY(emptyUrl
.path().isEmpty());
851 const KUrl
url("file:///test/");
854 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
855 m_model
->slotItemsAdded(emptyUrl
, items
);
856 m_model
->slotCompleted();
860 * Verify that removing hidden files and folders from the model does not
861 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
863 void KFileItemModelTest::testRemoveHiddenItems()
865 m_testDir
->createDir(".a");
866 m_testDir
->createDir(".b");
867 m_testDir
->createDir("c");
868 m_testDir
->createDir("d");
869 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
871 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
872 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
874 m_model
->setShowHiddenFiles(true);
875 m_model
->loadDirectory(m_testDir
->url());
876 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
877 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
878 QCOMPARE(spyItemsInserted
.count(), 1);
879 QCOMPARE(spyItemsRemoved
.count(), 0);
880 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
881 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
883 m_model
->setShowHiddenFiles(false);
884 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
885 QCOMPARE(spyItemsInserted
.count(), 0);
886 QCOMPARE(spyItemsRemoved
.count(), 1);
887 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
888 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
890 m_model
->setShowHiddenFiles(true);
891 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
892 QCOMPARE(spyItemsInserted
.count(), 1);
893 QCOMPARE(spyItemsRemoved
.count(), 0);
894 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
895 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
898 QCOMPARE(itemsInModel(), QStringList());
899 QCOMPARE(spyItemsInserted
.count(), 0);
900 QCOMPARE(spyItemsRemoved
.count(), 1);
901 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
902 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
904 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
905 // Verify that this does not make the model crash.
906 m_model
->setShowHiddenFiles(false);
910 * Verify that filtered items are removed when their parent is collapsed.
912 void KFileItemModelTest::collapseParentOfHiddenItems()
914 QSet
<QByteArray
> modelRoles
= m_model
->roles();
915 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
916 m_model
->setRoles(modelRoles
);
919 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
920 m_testDir
->createFiles(files
);
922 m_model
->loadDirectory(m_testDir
->url());
923 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
924 QCOMPARE(m_model
->count(), 1); // Only "a/"
927 m_model
->setExpanded(0, true);
928 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
929 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
932 m_model
->setExpanded(1, true);
933 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
934 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
937 m_model
->setExpanded(2, true);
938 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
939 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"
941 // Set a name filter that matches nothing -> only the expanded folders remain.
942 m_model
->setNameFilter("xyz");
943 QCOMPARE(m_model
->count(), 3);
944 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
946 // Collapse the folder "a/".
947 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
948 m_model
->setExpanded(0, false);
949 QCOMPARE(spyItemsRemoved
.count(), 1);
950 QCOMPARE(m_model
->count(), 1);
951 QCOMPARE(itemsInModel(), QStringList() << "a");
953 // Remove the filter -> no files should appear (and we should not get a crash).
954 m_model
->setNameFilter(QString());
955 QCOMPARE(m_model
->count(), 1);
959 * Verify that filtered items are removed when their parent is deleted.
961 void KFileItemModelTest::removeParentOfHiddenItems()
963 QSet
<QByteArray
> modelRoles
= m_model
->roles();
964 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
965 m_model
->setRoles(modelRoles
);
968 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
969 m_testDir
->createFiles(files
);
971 m_model
->loadDirectory(m_testDir
->url());
972 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
973 QCOMPARE(m_model
->count(), 1); // Only "a/"
976 m_model
->setExpanded(0, true);
977 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
978 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
981 m_model
->setExpanded(1, true);
982 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
983 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
986 m_model
->setExpanded(2, true);
987 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
988 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"
990 // Set a name filter that matches nothing -> only the expanded folders remain.
991 m_model
->setNameFilter("xyz");
992 QCOMPARE(m_model
->count(), 3);
993 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
995 // Simulate the deletion of the directory "a/b/".
996 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
997 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
998 QCOMPARE(spyItemsRemoved
.count(), 1);
999 QCOMPARE(m_model
->count(), 1);
1000 QCOMPARE(itemsInModel(), QStringList() << "a");
1002 // Remove the filter -> only the file "a/1" should appear.
1003 m_model
->setNameFilter(QString());
1004 QCOMPARE(m_model
->count(), 2);
1005 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1009 * Create a tree structure where parent-child relationships can not be
1010 * determined by parsing the URLs, and verify that KFileItemModel
1011 * handles them correctly.
1013 void KFileItemModelTest::testGeneralParentChildRelationships()
1015 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1016 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1017 m_model
->setRoles(modelRoles
);
1020 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1021 m_testDir
->createFiles(files
);
1023 m_model
->loadDirectory(m_testDir
->url());
1024 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1025 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1027 // Expand all folders.
1028 m_model
->setExpanded(0, true);
1029 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1030 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1032 m_model
->setExpanded(1, true);
1033 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1034 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1036 m_model
->setExpanded(3, true);
1037 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1038 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1040 m_model
->setExpanded(4, true);
1041 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1042 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1044 // Add some more children and grand-children.
1045 const KUrl parent1
= m_model
->fileItem(0).url();
1046 const KUrl parent2
= m_model
->fileItem(3).url();
1047 const KUrl realChild1
= m_model
->fileItem(1).url();
1048 const KUrl realChild2
= m_model
->fileItem(4).url();
1050 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1051 m_model
->slotCompleted();
1052 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1054 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1055 m_model
->slotCompleted();
1056 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1058 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1059 m_model
->slotCompleted();
1060 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1062 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1063 m_model
->slotCompleted();
1064 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1066 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1067 m_model
->slotCompleted();
1068 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1070 // Set a name filter that matches nothing -> only expanded folders remain.
1071 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1072 m_model
->setNameFilter("xyz");
1073 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1074 QCOMPARE(itemsRemovedSpy
.count(), 1);
1075 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1076 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1077 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1079 // Collapse "parent1".
1080 m_model
->setExpanded(0, false);
1081 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1082 QCOMPARE(itemsRemovedSpy
.count(), 1);
1083 arguments
= itemsRemovedSpy
.takeFirst();
1084 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1085 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1087 // Remove "parent2".
1088 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1089 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1090 QCOMPARE(itemsRemovedSpy
.count(), 1);
1091 arguments
= itemsRemovedSpy
.takeFirst();
1092 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1093 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1095 // Clear filter, verify that no items reappear.
1096 m_model
->setNameFilter(QString());
1097 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1100 QStringList
KFileItemModelTest::itemsInModel() const
1103 for (int i
= 0; i
< m_model
->count(); i
++) {
1104 items
<< m_model
->data(i
).value("text").toString();
1109 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1111 #include "kfileitemmodeltest.moc"