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 removeParentOfHiddenItems();
85 QStringList
itemsInModel() const;
88 KFileItemModel
* m_model
;
92 void KFileItemModelTest::init()
94 // The item-model tests result in a huge number of debugging
95 // output from kdelibs. Only show critical and fatal messages.
96 qInstallMsgHandler(myMessageOutput
);
98 qRegisterMetaType
<KItemRange
>("KItemRange");
99 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
100 qRegisterMetaType
<KFileItemList
>("KFileItemList");
102 m_testDir
= new TestDir();
103 m_model
= new KFileItemModel();
104 m_model
->m_dirLister
->setAutoUpdate(false);
107 void KFileItemModelTest::cleanup()
116 void KFileItemModelTest::testDefaultRoles()
118 const QSet
<QByteArray
> roles
= m_model
->roles();
119 QCOMPARE(roles
.count(), 3);
120 QVERIFY(roles
.contains("text"));
121 QVERIFY(roles
.contains("isDir"));
122 QVERIFY(roles
.contains("isLink"));
125 void KFileItemModelTest::testDefaultSortRole()
127 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
130 files
<< "c.txt" << "a.txt" << "b.txt";
132 m_testDir
->createFiles(files
);
134 m_model
->loadDirectory(m_testDir
->url());
135 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
137 QCOMPARE(m_model
->count(), 3);
138 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
139 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
140 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
143 void KFileItemModelTest::testDefaultGroupedSorting()
145 QCOMPARE(m_model
->groupedSorting(), false);
148 void KFileItemModelTest::testNewItems()
151 files
<< "a.txt" << "b.txt" << "c.txt";
152 m_testDir
->createFiles(files
);
154 m_model
->loadDirectory(m_testDir
->url());
155 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
157 QCOMPARE(m_model
->count(), 3);
159 QVERIFY(m_model
->isConsistent());
162 void KFileItemModelTest::testRemoveItems()
164 m_testDir
->createFile("a.txt");
165 m_testDir
->createFile("b.txt");
166 m_model
->loadDirectory(m_testDir
->url());
167 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
168 QCOMPARE(m_model
->count(), 2);
169 QVERIFY(m_model
->isConsistent());
171 m_testDir
->removeFile("a.txt");
172 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
173 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
174 QCOMPARE(m_model
->count(), 1);
175 QVERIFY(m_model
->isConsistent());
178 void KFileItemModelTest::testDirLoadingCompleted()
180 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
181 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
182 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
184 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
186 m_model
->loadDirectory(m_testDir
->url());
187 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
188 QCOMPARE(loadingCompletedSpy
.count(), 1);
189 QCOMPARE(itemsInsertedSpy
.count(), 1);
190 QCOMPARE(itemsRemovedSpy
.count(), 0);
191 QCOMPARE(m_model
->count(), 3);
193 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
194 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
195 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
196 QCOMPARE(loadingCompletedSpy
.count(), 2);
197 QCOMPARE(itemsInsertedSpy
.count(), 2);
198 QCOMPARE(itemsRemovedSpy
.count(), 0);
199 QCOMPARE(m_model
->count(), 5);
201 m_testDir
->removeFile("a.txt");
202 m_testDir
->createFile("f.txt");
203 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
204 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
205 QCOMPARE(loadingCompletedSpy
.count(), 3);
206 QCOMPARE(itemsInsertedSpy
.count(), 3);
207 QCOMPARE(itemsRemovedSpy
.count(), 1);
208 QCOMPARE(m_model
->count(), 5);
210 m_testDir
->removeFile("b.txt");
211 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
212 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
213 QCOMPARE(loadingCompletedSpy
.count(), 4);
214 QCOMPARE(itemsInsertedSpy
.count(), 3);
215 QCOMPARE(itemsRemovedSpy
.count(), 2);
216 QCOMPARE(m_model
->count(), 4);
218 QVERIFY(m_model
->isConsistent());
221 void KFileItemModelTest::testSetData()
223 m_testDir
->createFile("a.txt");
225 m_model
->loadDirectory(m_testDir
->url());
226 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
228 QHash
<QByteArray
, QVariant
> values
;
229 values
.insert("customRole1", "Test1");
230 values
.insert("customRole2", "Test2");
232 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
233 m_model
->setData(0, values
);
234 QCOMPARE(itemsChangedSpy
.count(), 1);
236 values
= m_model
->data(0);
237 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
238 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
239 QVERIFY(m_model
->isConsistent());
242 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
244 QTest::addColumn
<int>("changedIndex");
245 QTest::addColumn
<int>("changedRating");
246 QTest::addColumn
<bool>("expectMoveSignal");
247 QTest::addColumn
<int>("ratingIndex0");
248 QTest::addColumn
<int>("ratingIndex1");
249 QTest::addColumn
<int>("ratingIndex2");
252 // Index 0 = rating 2
253 // Index 1 = rating 4
254 // Index 2 = rating 6
256 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
257 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
258 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
260 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
261 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
262 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
265 void KFileItemModelTest::testSetDataWithModifiedSortRole()
267 QFETCH(int, changedIndex
);
268 QFETCH(int, changedRating
);
269 QFETCH(bool, expectMoveSignal
);
270 QFETCH(int, ratingIndex0
);
271 QFETCH(int, ratingIndex1
);
272 QFETCH(int, ratingIndex2
);
274 // Changing the value of a sort-role must result in
275 // a reordering of the items.
276 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
279 files
<< "a.txt" << "b.txt" << "c.txt";
280 m_testDir
->createFiles(files
);
282 m_model
->loadDirectory(m_testDir
->url());
283 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
285 // Fill the "rating" role of each file:
290 QHash
<QByteArray
, QVariant
> ratingA
;
291 ratingA
.insert("rating", 2);
292 m_model
->setData(0, ratingA
);
294 QHash
<QByteArray
, QVariant
> ratingB
;
295 ratingB
.insert("rating", 4);
296 m_model
->setData(1, ratingB
);
298 QHash
<QByteArray
, QVariant
> ratingC
;
299 ratingC
.insert("rating", 6);
300 m_model
->setData(2, ratingC
);
302 m_model
->setSortRole("rating");
303 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
304 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
305 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
307 // Now change the rating from a.txt. This usually results
308 // in reordering of the items.
309 QHash
<QByteArray
, QVariant
> rating
;
310 rating
.insert("rating", changedRating
);
311 m_model
->setData(changedIndex
, rating
);
313 if (expectMoveSignal
) {
314 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
317 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
318 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
319 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
320 QVERIFY(m_model
->isConsistent());
323 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
325 //QSKIP("Temporary disabled", SkipSingle);
327 // KFileItemModel prevents that inserting a punch of items sequentially
328 // results in an itemsInserted()-signal for each item. Instead internally
329 // a timeout is given that collects such operations and results in only
330 // one itemsInserted()-signal. However in this test we want to stress
331 // KFileItemModel to do a lot of insert operation and hence decrease
332 // the timeout to 1 millisecond.
333 m_testDir
->createFile("1");
334 m_model
->loadDirectory(m_testDir
->url());
335 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
336 QCOMPARE(m_model
->count(), 1);
338 // Insert 10 items for 20 times. After each insert operation the model consistency
340 QSet
<int> insertedItems
;
341 for (int i
= 0; i
< 20; ++i
) {
342 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
344 for (int j
= 0; j
< 10; ++j
) {
345 int itemName
= qrand();
346 while (insertedItems
.contains(itemName
)) {
349 insertedItems
.insert(itemName
);
351 m_testDir
->createFile(QString::number(itemName
));
354 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
355 if (spy
.count() == 0) {
356 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
359 QVERIFY(m_model
->isConsistent());
362 QCOMPARE(m_model
->count(), 201);
365 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
368 files
<< "B" << "E" << "G";
369 m_testDir
->createFiles(files
);
371 // Due to inserting the 3 items one item-range with index == 0 and
372 // count == 3 must be given
373 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
374 m_model
->loadDirectory(m_testDir
->url());
375 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
377 QCOMPARE(spy1
.count(), 1);
378 QList
<QVariant
> arguments
= spy1
.takeFirst();
379 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
380 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
382 // The indexes of the item-ranges must always be related to the model before
383 // the items have been inserted. Having:
386 // and inserting A, C, D, F the resulting model will be:
389 // and the item-ranges must be:
390 // index: 0, count: 1 for A
391 // index: 1, count: 2 for B, C
392 // index: 2, count: 1 for G
395 files
<< "A" << "C" << "D" << "F";
396 m_testDir
->createFiles(files
);
398 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
399 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
400 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
402 QCOMPARE(spy2
.count(), 1);
403 arguments
= spy2
.takeFirst();
404 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
405 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
408 void KFileItemModelTest::testExpandItems()
410 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
411 // Besides testing the basic item expansion functionality, the test makes sure that
412 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
413 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
414 // first three characters.
415 QSet
<QByteArray
> modelRoles
= m_model
->roles();
416 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
417 m_model
->setRoles(modelRoles
);
420 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
421 m_testDir
->createFiles(files
);
423 // Store the URLs of all folders in a set.
424 QSet
<KUrl
> allFolders
;
425 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
427 m_model
->loadDirectory(m_testDir
->url());
428 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
430 // So far, the model contains only "a/"
431 QCOMPARE(m_model
->count(), 1);
432 QVERIFY(m_model
->isExpandable(0));
433 QVERIFY(!m_model
->isExpanded(0));
434 QVERIFY(m_model
->expandedDirectories().empty());
436 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
438 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
439 m_model
->setExpanded(0, true);
440 QVERIFY(m_model
->isExpanded(0));
441 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
442 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
443 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
445 QCOMPARE(spyInserted
.count(), 1);
446 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
447 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
449 QVERIFY(m_model
->isExpandable(1));
450 QVERIFY(!m_model
->isExpanded(1));
451 QVERIFY(m_model
->isExpandable(2));
452 QVERIFY(!m_model
->isExpanded(2));
454 // Expand the folder "a/a/" -> "a/a/1" becomes visible
455 m_model
->setExpanded(1, true);
456 QVERIFY(m_model
->isExpanded(1));
457 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
458 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
459 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
461 QCOMPARE(spyInserted
.count(), 1);
462 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
463 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
465 QVERIFY(!m_model
->isExpandable(2));
466 QVERIFY(!m_model
->isExpanded(2));
468 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
469 m_model
->setExpanded(3, true);
470 QVERIFY(m_model
->isExpanded(3));
471 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
472 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
473 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
475 QCOMPARE(spyInserted
.count(), 1);
476 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
477 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
479 QVERIFY(!m_model
->isExpandable(4));
480 QVERIFY(!m_model
->isExpanded(4));
482 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
484 // Collapse the top-level folder -> all other items should disappear
485 m_model
->setExpanded(0, false);
486 QVERIFY(!m_model
->isExpanded(0));
487 QCOMPARE(m_model
->count(), 1);
488 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
490 QCOMPARE(spyRemoved
.count(), 1);
491 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
492 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
493 QVERIFY(m_model
->isConsistent());
495 // Clear the model, reload the folder and try to restore the expanded folders.
497 QCOMPARE(m_model
->count(), 0);
498 QVERIFY(m_model
->expandedDirectories().empty());
500 m_model
->loadDirectory(m_testDir
->url());
501 m_model
->restoreExpandedDirectories(allFolders
);
502 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
503 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
504 QVERIFY(m_model
->isExpanded(0));
505 QVERIFY(m_model
->isExpanded(1));
506 QVERIFY(!m_model
->isExpanded(2));
507 QVERIFY(m_model
->isExpanded(3));
508 QVERIFY(!m_model
->isExpanded(4));
509 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
510 QVERIFY(m_model
->isConsistent());
512 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
513 // This is how DolphinView restores the expanded folders when navigating in history.
514 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
515 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
516 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
517 m_model
->restoreExpandedDirectories(allFolders
);
518 m_model
->loadDirectory(m_testDir
->url());
519 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
520 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
521 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
524 void KFileItemModelTest::testExpandParentItems()
526 // Create a tree structure of folders:
534 QSet
<QByteArray
> modelRoles
= m_model
->roles();
535 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
536 m_model
->setRoles(modelRoles
);
539 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
540 m_testDir
->createFiles(files
);
542 m_model
->loadDirectory(m_testDir
->url());
543 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
545 // So far, the model contains only "a 1/" and "a2/".
546 QCOMPARE(m_model
->count(), 2);
547 QVERIFY(m_model
->expandedDirectories().empty());
549 // Expand the parents of "a2/b2/c2".
550 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
551 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
553 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
554 // It's important that only the parents of "a1/b1/c1" are expanded.
555 QCOMPARE(m_model
->count(), 4);
556 QVERIFY(!m_model
->isExpanded(0));
557 QVERIFY(m_model
->isExpanded(1));
558 QVERIFY(m_model
->isExpanded(2));
559 QVERIFY(!m_model
->isExpanded(3));
561 // Expand the parents of "a 1/b1".
562 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
563 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
565 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
566 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
567 QCOMPARE(m_model
->count(), 5);
568 QVERIFY(m_model
->isExpanded(0));
569 QVERIFY(!m_model
->isExpanded(1));
570 QVERIFY(m_model
->isExpanded(2));
571 QVERIFY(m_model
->isExpanded(3));
572 QVERIFY(!m_model
->isExpanded(4));
573 QVERIFY(m_model
->isConsistent());
577 * Renaming an expanded folder by prepending its name with a dot makes it
578 * hidden. Verify that this does not cause an inconsistent model state and
579 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
581 void KFileItemModelTest::testMakeExpandedItemHidden()
583 QSet
<QByteArray
> modelRoles
= m_model
->roles();
584 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
585 m_model
->setRoles(modelRoles
);
588 m_testDir
->createFile("1a/2a/3a");
589 m_testDir
->createFile("1a/2a/3b");
590 m_testDir
->createFile("1a/2b");
591 m_testDir
->createFile("1b");
593 m_model
->loadDirectory(m_testDir
->url());
594 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
596 // So far, the model contains only "1a/" and "1b".
597 QCOMPARE(m_model
->count(), 2);
598 m_model
->setExpanded(0, true);
599 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
601 // Now "1a/2a" and "1a/2b" have appeared.
602 QCOMPARE(m_model
->count(), 4);
603 m_model
->setExpanded(1, true);
604 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
605 QCOMPARE(m_model
->count(), 6);
607 // Rename "1a/2" and make it hidden.
608 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
609 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
611 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
612 bool ok
= job
->exec();
614 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
616 // "1a/2" and its subfolders have disappeared now.
617 QVERIFY(m_model
->isConsistent());
618 QCOMPARE(m_model
->count(), 3);
620 m_model
->setExpanded(0, false);
621 QCOMPARE(m_model
->count(), 2);
625 void KFileItemModelTest::testSorting()
627 // Create some files with different sizes and modification times to check the different sorting options
628 QDateTime now
= QDateTime::currentDateTime();
630 QSet
<QByteArray
> roles
;
631 roles
.insert("text");
632 roles
.insert("isExpanded");
633 roles
.insert("isExpandable");
634 roles
.insert("expandedParentsCount");
635 m_model
->setRoles(roles
);
637 m_testDir
->createDir("c/c-2");
638 m_testDir
->createFile("c/c-2/c-3");
639 m_testDir
->createFile("c/c-1");
641 m_testDir
->createFile("a", "A file", now
.addDays(-3));
642 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
643 m_testDir
->createDir("c", now
.addDays(-2));
644 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
645 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
646 m_testDir
->createFile(".f");
648 m_model
->loadDirectory(m_testDir
->url());
649 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
651 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
652 m_model
->setExpanded(index
, true);
653 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
655 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
656 m_model
->setExpanded(index
, true);
657 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
659 // Default: Sort by Name, ascending
660 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
661 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
662 QVERIFY(m_model
->sortDirectoriesFirst());
663 QVERIFY(!m_model
->showHiddenFiles());
664 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
666 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
668 // Sort by Name, ascending, 'Sort Folders First' disabled
669 m_model
->setSortDirectoriesFirst(false);
670 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
671 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
672 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
673 QCOMPARE(spyItemsMoved
.count(), 1);
674 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
676 // Sort by Name, descending
677 m_model
->setSortDirectoriesFirst(true);
678 m_model
->setSortOrder(Qt::DescendingOrder
);
679 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
680 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
681 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
682 QCOMPARE(spyItemsMoved
.count(), 2);
683 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
684 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
686 // Sort by Date, descending
687 m_model
->setSortDirectoriesFirst(true);
688 m_model
->setSortRole("date");
689 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
690 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
691 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
692 QCOMPARE(spyItemsMoved
.count(), 1);
693 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
695 // Sort by Date, ascending
696 m_model
->setSortOrder(Qt::AscendingOrder
);
697 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
698 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
699 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
700 QCOMPARE(spyItemsMoved
.count(), 1);
701 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
703 // Sort by Date, ascending, 'Sort Folders First' disabled
704 m_model
->setSortDirectoriesFirst(false);
705 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
706 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
707 QVERIFY(!m_model
->sortDirectoriesFirst());
708 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
709 QCOMPARE(spyItemsMoved
.count(), 1);
710 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
712 // Sort by Name, ascending, 'Sort Folders First' disabled
713 m_model
->setSortRole("text");
714 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
715 QVERIFY(!m_model
->sortDirectoriesFirst());
716 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
717 QCOMPARE(spyItemsMoved
.count(), 1);
718 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
720 // Sort by Size, ascending, 'Sort Folders First' disabled
721 m_model
->setSortRole("size");
722 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
723 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
724 QVERIFY(!m_model
->sortDirectoriesFirst());
725 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
726 QCOMPARE(spyItemsMoved
.count(), 1);
727 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
729 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
730 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
731 "another signal", SkipSingle
);
732 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
734 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
735 m_model
->setSortDirectoriesFirst(true);
736 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
737 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
738 QVERIFY(m_model
->sortDirectoriesFirst());
739 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
740 QCOMPARE(spyItemsMoved
.count(), 0);
742 // Sort by Size, descending, 'Sort Folders First' enabled
743 m_model
->setSortOrder(Qt::DescendingOrder
);
744 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
745 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
746 QVERIFY(m_model
->sortDirectoriesFirst());
747 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
748 QCOMPARE(spyItemsMoved
.count(), 1);
749 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
751 // TODO: Sort by other roles; show/hide hidden files
754 void KFileItemModelTest::testIndexForKeyboardSearch()
757 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
758 m_testDir
->createFiles(files
);
760 m_model
->loadDirectory(m_testDir
->url());
761 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
763 // Search from index 0
764 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
765 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
766 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
767 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
768 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
769 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
770 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
771 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
772 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
773 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
774 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
776 // Start a search somewhere in the middle
777 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
778 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
779 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
780 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
782 // Test searches that go past the last item back to index 0
783 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
784 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
785 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
786 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
788 // Test searches that yield no result
789 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
790 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
791 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
792 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
793 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
795 // Test upper case searches (note that search is case insensitive)
796 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
797 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
798 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
799 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
801 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
804 void KFileItemModelTest::testNameFilter()
807 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
808 m_testDir
->createFiles(files
);
810 m_model
->loadDirectory(m_testDir
->url());
811 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
813 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
814 QCOMPARE(m_model
->count(), 3);
816 m_model
->setNameFilter("A2"); // Shows only A2
817 QCOMPARE(m_model
->count(), 1);
819 m_model
->setNameFilter("A2"); // Shows only A1
820 QCOMPARE(m_model
->count(), 1);
822 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
823 QCOMPARE(m_model
->count(), 2);
825 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
826 QCOMPARE(m_model
->count(), 2);
828 m_model
->setNameFilter(QString()); // Shows again all items
829 QCOMPARE(m_model
->count(), 5);
833 * Verifies that we do not crash when adding a KFileItem with an empty path.
834 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
835 * tried to always read the first character of the path, even if the path is empty.
837 void KFileItemModelTest::testEmptyPath()
839 QSet
<QByteArray
> roles
;
840 roles
.insert("text");
841 roles
.insert("isExpanded");
842 roles
.insert("isExpandable");
843 roles
.insert("expandedParentsCount");
844 m_model
->setRoles(roles
);
847 QVERIFY(emptyUrl
.path().isEmpty());
849 const KUrl
url("file:///test/");
852 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
853 m_model
->slotItemsAdded(emptyUrl
, items
);
854 m_model
->slotCompleted();
858 * Verify that removing hidden files and folders from the model does not
859 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
861 void KFileItemModelTest::testRemoveHiddenItems()
863 m_testDir
->createDir(".a");
864 m_testDir
->createDir(".b");
865 m_testDir
->createDir("c");
866 m_testDir
->createDir("d");
867 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
869 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
870 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
872 m_model
->setShowHiddenFiles(true);
873 m_model
->loadDirectory(m_testDir
->url());
874 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
875 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
876 QCOMPARE(spyItemsInserted
.count(), 1);
877 QCOMPARE(spyItemsRemoved
.count(), 0);
878 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
879 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
881 m_model
->setShowHiddenFiles(false);
882 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
883 QCOMPARE(spyItemsInserted
.count(), 0);
884 QCOMPARE(spyItemsRemoved
.count(), 1);
885 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
886 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
888 m_model
->setShowHiddenFiles(true);
889 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
890 QCOMPARE(spyItemsInserted
.count(), 1);
891 QCOMPARE(spyItemsRemoved
.count(), 0);
892 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
893 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
896 QCOMPARE(itemsInModel(), QStringList());
897 QCOMPARE(spyItemsInserted
.count(), 0);
898 QCOMPARE(spyItemsRemoved
.count(), 1);
899 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
900 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
902 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
903 // Verify that this does not make the model crash.
904 m_model
->setShowHiddenFiles(false);
908 * Verify that filtered items are removed when their parent is deleted.
910 void KFileItemModelTest::removeParentOfHiddenItems()
912 QSet
<QByteArray
> modelRoles
= m_model
->roles();
913 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
914 m_model
->setRoles(modelRoles
);
917 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
918 m_testDir
->createFiles(files
);
920 m_model
->loadDirectory(m_testDir
->url());
921 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
922 QCOMPARE(m_model
->count(), 1); // Only "a/"
925 m_model
->setExpanded(0, true);
926 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
927 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
930 m_model
->setExpanded(1, true);
931 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
932 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
935 m_model
->setExpanded(2, true);
936 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
937 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"
939 // Set a name filter that matches nothing -> only the expanded folders remain.
940 m_model
->setNameFilter("xyz");
941 QCOMPARE(m_model
->count(), 3);
942 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
944 // Simulate the deletion of the directory "a/b/".
945 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
946 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
947 QCOMPARE(spyItemsRemoved
.count(), 1);
948 QCOMPARE(m_model
->count(), 1);
949 QCOMPARE(itemsInModel(), QStringList() << "a");
951 // Remove the filter -> only the file "a/1" should appear.
952 m_model
->setNameFilter(QString());
953 QCOMPARE(m_model
->count(), 2);
954 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
957 QStringList
KFileItemModelTest::itemsInModel() const
960 for (int i
= 0; i
< m_model
->count(); i
++) {
961 items
<< m_model
->data(i
).value("text").toString();
966 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
968 #include "kfileitemmodeltest.moc"