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>
24 #include "kitemviews/kfileitemmodel.h"
25 #include "kitemviews/private/kfileitemmodeldirlister.h"
28 void myMessageOutput(QtMsgType type
, const char* msg
)
36 fprintf(stderr
, "Critical: %s\n", msg
);
39 fprintf(stderr
, "Fatal: %s\n", msg
);
47 const int DefaultTimeout
= 5000;
50 Q_DECLARE_METATYPE(KItemRangeList
)
51 Q_DECLARE_METATYPE(QList
<int>)
53 class KFileItemModelTest
: public QObject
61 void testDefaultRoles();
62 void testDefaultSortRole();
63 void testDefaultGroupedSorting();
65 void testRemoveItems();
66 void testDirLoadingCompleted();
68 void testSetDataWithModifiedSortRole_data();
69 void testSetDataWithModifiedSortRole();
70 void testModelConsistencyWhenInsertingItems();
71 void testItemRangeConsistencyWhenInsertingItems();
72 void testExpandItems();
73 void testExpandParentItems();
75 void testIndexForKeyboardSearch();
76 void testNameFilter();
78 void testRemoveHiddenItems();
81 QStringList
itemsInModel() const;
84 KFileItemModel
* m_model
;
88 void KFileItemModelTest::init()
90 // The item-model tests result in a huge number of debugging
91 // output from kdelibs. Only show critical and fatal messages.
92 qInstallMsgHandler(myMessageOutput
);
94 qRegisterMetaType
<KItemRange
>("KItemRange");
95 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
96 qRegisterMetaType
<KFileItemList
>("KFileItemList");
98 m_testDir
= new TestDir();
99 m_model
= new KFileItemModel();
100 m_model
->m_dirLister
->setAutoUpdate(false);
103 void KFileItemModelTest::cleanup()
112 void KFileItemModelTest::testDefaultRoles()
114 const QSet
<QByteArray
> roles
= m_model
->roles();
115 QCOMPARE(roles
.count(), 3);
116 QVERIFY(roles
.contains("text"));
117 QVERIFY(roles
.contains("isDir"));
118 QVERIFY(roles
.contains("isLink"));
121 void KFileItemModelTest::testDefaultSortRole()
123 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
126 files
<< "c.txt" << "a.txt" << "b.txt";
128 m_testDir
->createFiles(files
);
130 m_model
->loadDirectory(m_testDir
->url());
131 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
133 QCOMPARE(m_model
->count(), 3);
134 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
135 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
136 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
139 void KFileItemModelTest::testDefaultGroupedSorting()
141 QCOMPARE(m_model
->groupedSorting(), false);
144 void KFileItemModelTest::testNewItems()
147 files
<< "a.txt" << "b.txt" << "c.txt";
148 m_testDir
->createFiles(files
);
150 m_model
->loadDirectory(m_testDir
->url());
151 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
153 QCOMPARE(m_model
->count(), 3);
155 QVERIFY(m_model
->isConsistent());
158 void KFileItemModelTest::testRemoveItems()
160 m_testDir
->createFile("a.txt");
161 m_testDir
->createFile("b.txt");
162 m_model
->loadDirectory(m_testDir
->url());
163 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
164 QCOMPARE(m_model
->count(), 2);
165 QVERIFY(m_model
->isConsistent());
167 m_testDir
->removeFile("a.txt");
168 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
169 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
170 QCOMPARE(m_model
->count(), 1);
171 QVERIFY(m_model
->isConsistent());
174 void KFileItemModelTest::testDirLoadingCompleted()
176 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
177 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
178 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
180 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
182 m_model
->loadDirectory(m_testDir
->url());
183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
184 QCOMPARE(loadingCompletedSpy
.count(), 1);
185 QCOMPARE(itemsInsertedSpy
.count(), 1);
186 QCOMPARE(itemsRemovedSpy
.count(), 0);
187 QCOMPARE(m_model
->count(), 3);
189 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
190 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
191 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
192 QCOMPARE(loadingCompletedSpy
.count(), 2);
193 QCOMPARE(itemsInsertedSpy
.count(), 2);
194 QCOMPARE(itemsRemovedSpy
.count(), 0);
195 QCOMPARE(m_model
->count(), 5);
197 m_testDir
->removeFile("a.txt");
198 m_testDir
->createFile("f.txt");
199 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
200 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
201 QCOMPARE(loadingCompletedSpy
.count(), 3);
202 QCOMPARE(itemsInsertedSpy
.count(), 3);
203 QCOMPARE(itemsRemovedSpy
.count(), 1);
204 QCOMPARE(m_model
->count(), 5);
206 m_testDir
->removeFile("b.txt");
207 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
208 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
209 QCOMPARE(loadingCompletedSpy
.count(), 4);
210 QCOMPARE(itemsInsertedSpy
.count(), 3);
211 QCOMPARE(itemsRemovedSpy
.count(), 2);
212 QCOMPARE(m_model
->count(), 4);
214 QVERIFY(m_model
->isConsistent());
217 void KFileItemModelTest::testSetData()
219 m_testDir
->createFile("a.txt");
221 m_model
->loadDirectory(m_testDir
->url());
222 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
224 QHash
<QByteArray
, QVariant
> values
;
225 values
.insert("customRole1", "Test1");
226 values
.insert("customRole2", "Test2");
228 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
229 m_model
->setData(0, values
);
230 QCOMPARE(itemsChangedSpy
.count(), 1);
232 values
= m_model
->data(0);
233 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
234 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
235 QVERIFY(m_model
->isConsistent());
238 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
240 QTest::addColumn
<int>("changedIndex");
241 QTest::addColumn
<int>("changedRating");
242 QTest::addColumn
<bool>("expectMoveSignal");
243 QTest::addColumn
<int>("ratingIndex0");
244 QTest::addColumn
<int>("ratingIndex1");
245 QTest::addColumn
<int>("ratingIndex2");
248 // Index 0 = rating 2
249 // Index 1 = rating 4
250 // Index 2 = rating 6
252 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
253 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
254 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
256 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
257 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
258 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
261 void KFileItemModelTest::testSetDataWithModifiedSortRole()
263 QFETCH(int, changedIndex
);
264 QFETCH(int, changedRating
);
265 QFETCH(bool, expectMoveSignal
);
266 QFETCH(int, ratingIndex0
);
267 QFETCH(int, ratingIndex1
);
268 QFETCH(int, ratingIndex2
);
270 // Changing the value of a sort-role must result in
271 // a reordering of the items.
272 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
275 files
<< "a.txt" << "b.txt" << "c.txt";
276 m_testDir
->createFiles(files
);
278 m_model
->loadDirectory(m_testDir
->url());
279 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
281 // Fill the "rating" role of each file:
286 QHash
<QByteArray
, QVariant
> ratingA
;
287 ratingA
.insert("rating", 2);
288 m_model
->setData(0, ratingA
);
290 QHash
<QByteArray
, QVariant
> ratingB
;
291 ratingB
.insert("rating", 4);
292 m_model
->setData(1, ratingB
);
294 QHash
<QByteArray
, QVariant
> ratingC
;
295 ratingC
.insert("rating", 6);
296 m_model
->setData(2, ratingC
);
298 m_model
->setSortRole("rating");
299 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
300 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
301 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
303 // Now change the rating from a.txt. This usually results
304 // in reordering of the items.
305 QHash
<QByteArray
, QVariant
> rating
;
306 rating
.insert("rating", changedRating
);
307 m_model
->setData(changedIndex
, rating
);
309 if (expectMoveSignal
) {
310 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
313 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
314 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
315 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
316 QVERIFY(m_model
->isConsistent());
319 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
321 //QSKIP("Temporary disabled", SkipSingle);
323 // KFileItemModel prevents that inserting a punch of items sequentially
324 // results in an itemsInserted()-signal for each item. Instead internally
325 // a timeout is given that collects such operations and results in only
326 // one itemsInserted()-signal. However in this test we want to stress
327 // KFileItemModel to do a lot of insert operation and hence decrease
328 // the timeout to 1 millisecond.
329 m_testDir
->createFile("1");
330 m_model
->loadDirectory(m_testDir
->url());
331 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
332 QCOMPARE(m_model
->count(), 1);
334 // Insert 10 items for 20 times. After each insert operation the model consistency
336 QSet
<int> insertedItems
;
337 for (int i
= 0; i
< 20; ++i
) {
338 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
340 for (int j
= 0; j
< 10; ++j
) {
341 int itemName
= qrand();
342 while (insertedItems
.contains(itemName
)) {
345 insertedItems
.insert(itemName
);
347 m_testDir
->createFile(QString::number(itemName
));
350 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
351 if (spy
.count() == 0) {
352 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
355 QVERIFY(m_model
->isConsistent());
358 QCOMPARE(m_model
->count(), 201);
361 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
364 files
<< "B" << "E" << "G";
365 m_testDir
->createFiles(files
);
367 // Due to inserting the 3 items one item-range with index == 0 and
368 // count == 3 must be given
369 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
370 m_model
->loadDirectory(m_testDir
->url());
371 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
373 QCOMPARE(spy1
.count(), 1);
374 QList
<QVariant
> arguments
= spy1
.takeFirst();
375 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
376 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
378 // The indexes of the item-ranges must always be related to the model before
379 // the items have been inserted. Having:
382 // and inserting A, C, D, F the resulting model will be:
385 // and the item-ranges must be:
386 // index: 0, count: 1 for A
387 // index: 1, count: 2 for B, C
388 // index: 2, count: 1 for G
391 files
<< "A" << "C" << "D" << "F";
392 m_testDir
->createFiles(files
);
394 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
395 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
396 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
398 QCOMPARE(spy2
.count(), 1);
399 arguments
= spy2
.takeFirst();
400 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
401 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
404 void KFileItemModelTest::testExpandItems()
406 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
407 // Besides testing the basic item expansion functionality, the test makes sure that
408 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
409 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
410 // first three characters.
411 QSet
<QByteArray
> modelRoles
= m_model
->roles();
412 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
413 m_model
->setRoles(modelRoles
);
416 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
417 m_testDir
->createFiles(files
);
419 // Store the URLs of all folders in a set.
420 QSet
<KUrl
> allFolders
;
421 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
423 m_model
->loadDirectory(m_testDir
->url());
424 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
426 // So far, the model contains only "a/"
427 QCOMPARE(m_model
->count(), 1);
428 QVERIFY(m_model
->isExpandable(0));
429 QVERIFY(!m_model
->isExpanded(0));
430 QVERIFY(m_model
->expandedDirectories().empty());
432 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
434 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
435 m_model
->setExpanded(0, true);
436 QVERIFY(m_model
->isExpanded(0));
437 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
438 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
439 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
441 QCOMPARE(spyInserted
.count(), 1);
442 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
443 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
445 QVERIFY(m_model
->isExpandable(1));
446 QVERIFY(!m_model
->isExpanded(1));
447 QVERIFY(m_model
->isExpandable(2));
448 QVERIFY(!m_model
->isExpanded(2));
450 // Expand the folder "a/a/" -> "a/a/1" becomes visible
451 m_model
->setExpanded(1, true);
452 QVERIFY(m_model
->isExpanded(1));
453 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
454 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
455 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
457 QCOMPARE(spyInserted
.count(), 1);
458 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
459 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
461 QVERIFY(!m_model
->isExpandable(2));
462 QVERIFY(!m_model
->isExpanded(2));
464 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
465 m_model
->setExpanded(3, true);
466 QVERIFY(m_model
->isExpanded(3));
467 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
468 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
469 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
471 QCOMPARE(spyInserted
.count(), 1);
472 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
473 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
475 QVERIFY(!m_model
->isExpandable(4));
476 QVERIFY(!m_model
->isExpanded(4));
478 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
480 // Collapse the top-level folder -> all other items should disappear
481 m_model
->setExpanded(0, false);
482 QVERIFY(!m_model
->isExpanded(0));
483 QCOMPARE(m_model
->count(), 1);
484 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
486 QCOMPARE(spyRemoved
.count(), 1);
487 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
488 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
489 QVERIFY(m_model
->isConsistent());
491 // Clear the model, reload the folder and try to restore the expanded folders.
493 QCOMPARE(m_model
->count(), 0);
494 QVERIFY(m_model
->expandedDirectories().empty());
496 m_model
->loadDirectory(m_testDir
->url());
497 m_model
->restoreExpandedDirectories(allFolders
);
498 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
499 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
500 QVERIFY(m_model
->isExpanded(0));
501 QVERIFY(m_model
->isExpanded(1));
502 QVERIFY(!m_model
->isExpanded(2));
503 QVERIFY(m_model
->isExpanded(3));
504 QVERIFY(!m_model
->isExpanded(4));
505 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
506 QVERIFY(m_model
->isConsistent());
508 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
509 // This is how DolphinView restores the expanded folders when navigating in history.
510 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
511 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
512 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
513 m_model
->restoreExpandedDirectories(allFolders
);
514 m_model
->loadDirectory(m_testDir
->url());
515 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
516 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
517 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
520 void KFileItemModelTest::testExpandParentItems()
522 // Create a tree structure of folders:
530 QSet
<QByteArray
> modelRoles
= m_model
->roles();
531 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
532 m_model
->setRoles(modelRoles
);
535 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
536 m_testDir
->createFiles(files
);
538 m_model
->loadDirectory(m_testDir
->url());
539 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
541 // So far, the model contains only "a 1/" and "a2/".
542 QCOMPARE(m_model
->count(), 2);
543 QVERIFY(m_model
->expandedDirectories().empty());
545 // Expand the parents of "a2/b2/c2".
546 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
547 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
549 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
550 // It's important that only the parents of "a1/b1/c1" are expanded.
551 QCOMPARE(m_model
->count(), 4);
552 QVERIFY(!m_model
->isExpanded(0));
553 QVERIFY(m_model
->isExpanded(1));
554 QVERIFY(m_model
->isExpanded(2));
555 QVERIFY(!m_model
->isExpanded(3));
557 // Expand the parents of "a 1/b1".
558 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
559 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
561 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
562 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
563 QCOMPARE(m_model
->count(), 5);
564 QVERIFY(m_model
->isExpanded(0));
565 QVERIFY(!m_model
->isExpanded(1));
566 QVERIFY(m_model
->isExpanded(2));
567 QVERIFY(m_model
->isExpanded(3));
568 QVERIFY(!m_model
->isExpanded(4));
569 QVERIFY(m_model
->isConsistent());
572 void KFileItemModelTest::testSorting()
574 // Create some files with different sizes and modification times to check the different sorting options
575 QDateTime now
= QDateTime::currentDateTime();
577 QSet
<QByteArray
> roles
;
578 roles
.insert("text");
579 roles
.insert("isExpanded");
580 roles
.insert("isExpandable");
581 roles
.insert("expandedParentsCount");
582 m_model
->setRoles(roles
);
584 m_testDir
->createDir("c/c-2");
585 m_testDir
->createFile("c/c-2/c-3");
586 m_testDir
->createFile("c/c-1");
588 m_testDir
->createFile("a", "A file", now
.addDays(-3));
589 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
590 m_testDir
->createDir("c", now
.addDays(-2));
591 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
592 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
593 m_testDir
->createFile(".f");
595 m_model
->loadDirectory(m_testDir
->url());
596 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
598 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
599 m_model
->setExpanded(index
, true);
600 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
602 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
603 m_model
->setExpanded(index
, true);
604 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
606 // Default: Sort by Name, ascending
607 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
608 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
609 QVERIFY(m_model
->sortDirectoriesFirst());
610 QVERIFY(!m_model
->showHiddenFiles());
611 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
613 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
615 // Sort by Name, ascending, 'Sort Folders First' disabled
616 m_model
->setSortDirectoriesFirst(false);
617 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
618 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
619 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
620 QCOMPARE(spyItemsMoved
.count(), 1);
621 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
623 // Sort by Name, descending
624 m_model
->setSortDirectoriesFirst(true);
625 m_model
->setSortOrder(Qt::DescendingOrder
);
626 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
627 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
628 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
629 QCOMPARE(spyItemsMoved
.count(), 2);
630 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
631 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
633 // Sort by Date, descending
634 m_model
->setSortDirectoriesFirst(true);
635 m_model
->setSortRole("date");
636 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
637 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
638 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
639 QCOMPARE(spyItemsMoved
.count(), 1);
640 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
642 // Sort by Date, ascending
643 m_model
->setSortOrder(Qt::AscendingOrder
);
644 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
645 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
646 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
647 QCOMPARE(spyItemsMoved
.count(), 1);
648 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
650 // Sort by Date, ascending, 'Sort Folders First' disabled
651 m_model
->setSortDirectoriesFirst(false);
652 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
653 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
654 QVERIFY(!m_model
->sortDirectoriesFirst());
655 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
656 QCOMPARE(spyItemsMoved
.count(), 1);
657 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
659 // Sort by Name, ascending, 'Sort Folders First' disabled
660 m_model
->setSortRole("text");
661 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
662 QVERIFY(!m_model
->sortDirectoriesFirst());
663 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
664 QCOMPARE(spyItemsMoved
.count(), 1);
665 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
667 // Sort by Size, ascending, 'Sort Folders First' disabled
668 m_model
->setSortRole("size");
669 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
670 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
671 QVERIFY(!m_model
->sortDirectoriesFirst());
672 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
673 QCOMPARE(spyItemsMoved
.count(), 1);
674 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
676 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
677 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
678 "another signal", SkipSingle
);
679 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
681 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
682 m_model
->setSortDirectoriesFirst(true);
683 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
684 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
685 QVERIFY(m_model
->sortDirectoriesFirst());
686 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
687 QCOMPARE(spyItemsMoved
.count(), 0);
689 // Sort by Size, descending, 'Sort Folders First' enabled
690 m_model
->setSortOrder(Qt::DescendingOrder
);
691 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
692 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
693 QVERIFY(m_model
->sortDirectoriesFirst());
694 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
695 QCOMPARE(spyItemsMoved
.count(), 1);
696 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
698 // TODO: Sort by other roles; show/hide hidden files
701 void KFileItemModelTest::testIndexForKeyboardSearch()
704 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
705 m_testDir
->createFiles(files
);
707 m_model
->loadDirectory(m_testDir
->url());
708 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
710 // Search from index 0
711 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
712 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
713 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
714 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
715 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
716 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
717 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
718 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
719 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
720 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
721 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
723 // Start a search somewhere in the middle
724 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
725 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
726 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
727 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
729 // Test searches that go past the last item back to index 0
730 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
731 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
732 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
733 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
735 // Test searches that yield no result
736 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
737 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
738 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
739 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
740 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
742 // Test upper case searches (note that search is case insensitive)
743 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
744 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
745 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
746 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
748 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
751 void KFileItemModelTest::testNameFilter()
754 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
755 m_testDir
->createFiles(files
);
757 m_model
->loadDirectory(m_testDir
->url());
758 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
760 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
761 QCOMPARE(m_model
->count(), 3);
763 m_model
->setNameFilter("A2"); // Shows only A2
764 QCOMPARE(m_model
->count(), 1);
766 m_model
->setNameFilter("A2"); // Shows only A1
767 QCOMPARE(m_model
->count(), 1);
769 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
770 QCOMPARE(m_model
->count(), 2);
772 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
773 QCOMPARE(m_model
->count(), 2);
775 m_model
->setNameFilter(QString()); // Shows again all items
776 QCOMPARE(m_model
->count(), 5);
780 * Verifies that we do not crash when adding a KFileItem with an empty path.
781 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
782 * tried to always read the first character of the path, even if the path is empty.
784 void KFileItemModelTest::testEmptyPath()
786 QSet
<QByteArray
> roles
;
787 roles
.insert("text");
788 roles
.insert("isExpanded");
789 roles
.insert("isExpandable");
790 roles
.insert("expandedParentsCount");
791 m_model
->setRoles(roles
);
794 QVERIFY(emptyUrl
.path().isEmpty());
796 const KUrl
url("file:///test/");
799 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
800 m_model
->slotNewItems(items
);
801 m_model
->slotCompleted();
805 * Verify that removing hidden files and folders from the model does not
806 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
808 void KFileItemModelTest::testRemoveHiddenItems()
810 m_testDir
->createDir(".a");
811 m_testDir
->createDir(".b");
812 m_testDir
->createDir("c");
813 m_testDir
->createDir("d");
814 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
816 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
817 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
819 m_model
->setShowHiddenFiles(true);
820 m_model
->loadDirectory(m_testDir
->url());
821 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
822 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
823 QCOMPARE(spyItemsInserted
.count(), 1);
824 QCOMPARE(spyItemsRemoved
.count(), 0);
825 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
826 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
828 m_model
->setShowHiddenFiles(false);
829 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
830 QCOMPARE(spyItemsInserted
.count(), 0);
831 QCOMPARE(spyItemsRemoved
.count(), 1);
832 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
833 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
835 m_model
->setShowHiddenFiles(true);
836 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
837 QCOMPARE(spyItemsInserted
.count(), 1);
838 QCOMPARE(spyItemsRemoved
.count(), 0);
839 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
840 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
843 QCOMPARE(itemsInModel(), QStringList());
844 QCOMPARE(spyItemsInserted
.count(), 0);
845 QCOMPARE(spyItemsRemoved
.count(), 1);
846 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
847 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
849 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
850 // Verify that this does not make the model crash.
851 m_model
->setShowHiddenFiles(false);
854 QStringList
KFileItemModelTest::itemsInModel() const
857 for (int i
= 0; i
< m_model
->count(); i
++) {
858 items
<< m_model
->data(i
).value("text").toString();
863 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
865 #include "kfileitemmodeltest.moc"