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();
79 void removeParentOfHiddenItems();
82 bool isModelConsistent() const;
83 QStringList
itemsInModel() const;
86 KFileItemModel
* m_model
;
90 void KFileItemModelTest::init()
92 // The item-model tests result in a huge number of debugging
93 // output from kdelibs. Only show critical and fatal messages.
94 qInstallMsgHandler(myMessageOutput
);
96 qRegisterMetaType
<KItemRange
>("KItemRange");
97 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
98 qRegisterMetaType
<KFileItemList
>("KFileItemList");
100 m_testDir
= new TestDir();
101 m_model
= new KFileItemModel();
102 m_model
->m_dirLister
->setAutoUpdate(false);
105 void KFileItemModelTest::cleanup()
114 void KFileItemModelTest::testDefaultRoles()
116 const QSet
<QByteArray
> roles
= m_model
->roles();
117 QCOMPARE(roles
.count(), 3);
118 QVERIFY(roles
.contains("text"));
119 QVERIFY(roles
.contains("isDir"));
120 QVERIFY(roles
.contains("isLink"));
123 void KFileItemModelTest::testDefaultSortRole()
125 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
128 files
<< "c.txt" << "a.txt" << "b.txt";
130 m_testDir
->createFiles(files
);
132 m_model
->loadDirectory(m_testDir
->url());
133 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
135 QCOMPARE(m_model
->count(), 3);
136 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
137 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
138 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
141 void KFileItemModelTest::testDefaultGroupedSorting()
143 QCOMPARE(m_model
->groupedSorting(), false);
146 void KFileItemModelTest::testNewItems()
149 files
<< "a.txt" << "b.txt" << "c.txt";
150 m_testDir
->createFiles(files
);
152 m_model
->loadDirectory(m_testDir
->url());
153 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
155 QCOMPARE(m_model
->count(), 3);
157 QVERIFY(isModelConsistent());
160 void KFileItemModelTest::testRemoveItems()
162 m_testDir
->createFile("a.txt");
163 m_testDir
->createFile("b.txt");
164 m_model
->loadDirectory(m_testDir
->url());
165 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
166 QCOMPARE(m_model
->count(), 2);
167 QVERIFY(isModelConsistent());
169 m_testDir
->removeFile("a.txt");
170 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
171 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
172 QCOMPARE(m_model
->count(), 1);
173 QVERIFY(isModelConsistent());
176 void KFileItemModelTest::testDirLoadingCompleted()
178 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
179 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
180 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
182 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
184 m_model
->loadDirectory(m_testDir
->url());
185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
186 QCOMPARE(loadingCompletedSpy
.count(), 1);
187 QCOMPARE(itemsInsertedSpy
.count(), 1);
188 QCOMPARE(itemsRemovedSpy
.count(), 0);
189 QCOMPARE(m_model
->count(), 3);
191 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
192 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
193 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
194 QCOMPARE(loadingCompletedSpy
.count(), 2);
195 QCOMPARE(itemsInsertedSpy
.count(), 2);
196 QCOMPARE(itemsRemovedSpy
.count(), 0);
197 QCOMPARE(m_model
->count(), 5);
199 m_testDir
->removeFile("a.txt");
200 m_testDir
->createFile("f.txt");
201 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
202 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
203 QCOMPARE(loadingCompletedSpy
.count(), 3);
204 QCOMPARE(itemsInsertedSpy
.count(), 3);
205 QCOMPARE(itemsRemovedSpy
.count(), 1);
206 QCOMPARE(m_model
->count(), 5);
208 m_testDir
->removeFile("b.txt");
209 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
210 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
211 QCOMPARE(loadingCompletedSpy
.count(), 4);
212 QCOMPARE(itemsInsertedSpy
.count(), 3);
213 QCOMPARE(itemsRemovedSpy
.count(), 2);
214 QCOMPARE(m_model
->count(), 4);
216 QVERIFY(isModelConsistent());
219 void KFileItemModelTest::testSetData()
221 m_testDir
->createFile("a.txt");
223 m_model
->loadDirectory(m_testDir
->url());
224 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
226 QHash
<QByteArray
, QVariant
> values
;
227 values
.insert("customRole1", "Test1");
228 values
.insert("customRole2", "Test2");
230 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
231 m_model
->setData(0, values
);
232 QCOMPARE(itemsChangedSpy
.count(), 1);
234 values
= m_model
->data(0);
235 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
236 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
237 QVERIFY(isModelConsistent());
240 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
242 QTest::addColumn
<int>("changedIndex");
243 QTest::addColumn
<int>("changedRating");
244 QTest::addColumn
<bool>("expectMoveSignal");
245 QTest::addColumn
<int>("ratingIndex0");
246 QTest::addColumn
<int>("ratingIndex1");
247 QTest::addColumn
<int>("ratingIndex2");
250 // Index 0 = rating 2
251 // Index 1 = rating 4
252 // Index 2 = rating 6
254 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
255 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
256 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
258 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
259 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
260 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
263 void KFileItemModelTest::testSetDataWithModifiedSortRole()
265 QFETCH(int, changedIndex
);
266 QFETCH(int, changedRating
);
267 QFETCH(bool, expectMoveSignal
);
268 QFETCH(int, ratingIndex0
);
269 QFETCH(int, ratingIndex1
);
270 QFETCH(int, ratingIndex2
);
272 // Changing the value of a sort-role must result in
273 // a reordering of the items.
274 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
277 files
<< "a.txt" << "b.txt" << "c.txt";
278 m_testDir
->createFiles(files
);
280 m_model
->loadDirectory(m_testDir
->url());
281 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
283 // Fill the "rating" role of each file:
288 QHash
<QByteArray
, QVariant
> ratingA
;
289 ratingA
.insert("rating", 2);
290 m_model
->setData(0, ratingA
);
292 QHash
<QByteArray
, QVariant
> ratingB
;
293 ratingB
.insert("rating", 4);
294 m_model
->setData(1, ratingB
);
296 QHash
<QByteArray
, QVariant
> ratingC
;
297 ratingC
.insert("rating", 6);
298 m_model
->setData(2, ratingC
);
300 m_model
->setSortRole("rating");
301 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
302 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
303 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
305 // Now change the rating from a.txt. This usually results
306 // in reordering of the items.
307 QHash
<QByteArray
, QVariant
> rating
;
308 rating
.insert("rating", changedRating
);
309 m_model
->setData(changedIndex
, rating
);
311 if (expectMoveSignal
) {
312 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
315 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
316 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
317 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
318 QVERIFY(isModelConsistent());
321 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
323 //QSKIP("Temporary disabled", SkipSingle);
325 // KFileItemModel prevents that inserting a punch of items sequentially
326 // results in an itemsInserted()-signal for each item. Instead internally
327 // a timeout is given that collects such operations and results in only
328 // one itemsInserted()-signal. However in this test we want to stress
329 // KFileItemModel to do a lot of insert operation and hence decrease
330 // the timeout to 1 millisecond.
331 m_testDir
->createFile("1");
332 m_model
->loadDirectory(m_testDir
->url());
333 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
334 QCOMPARE(m_model
->count(), 1);
336 // Insert 10 items for 20 times. After each insert operation the model consistency
338 QSet
<int> insertedItems
;
339 for (int i
= 0; i
< 20; ++i
) {
340 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
342 for (int j
= 0; j
< 10; ++j
) {
343 int itemName
= qrand();
344 while (insertedItems
.contains(itemName
)) {
347 insertedItems
.insert(itemName
);
349 m_testDir
->createFile(QString::number(itemName
));
352 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
353 if (spy
.count() == 0) {
354 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
357 QVERIFY(isModelConsistent());
360 QCOMPARE(m_model
->count(), 201);
363 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
366 files
<< "B" << "E" << "G";
367 m_testDir
->createFiles(files
);
369 // Due to inserting the 3 items one item-range with index == 0 and
370 // count == 3 must be given
371 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
372 m_model
->loadDirectory(m_testDir
->url());
373 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
375 QCOMPARE(spy1
.count(), 1);
376 QList
<QVariant
> arguments
= spy1
.takeFirst();
377 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
378 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
380 // The indexes of the item-ranges must always be related to the model before
381 // the items have been inserted. Having:
384 // and inserting A, C, D, F the resulting model will be:
387 // and the item-ranges must be:
388 // index: 0, count: 1 for A
389 // index: 1, count: 2 for B, C
390 // index: 2, count: 1 for G
393 files
<< "A" << "C" << "D" << "F";
394 m_testDir
->createFiles(files
);
396 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
397 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
398 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
400 QCOMPARE(spy2
.count(), 1);
401 arguments
= spy2
.takeFirst();
402 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
403 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
406 void KFileItemModelTest::testExpandItems()
408 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
409 // Besides testing the basic item expansion functionality, the test makes sure that
410 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
411 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
412 // first three characters.
413 QSet
<QByteArray
> modelRoles
= m_model
->roles();
414 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
415 m_model
->setRoles(modelRoles
);
418 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
419 m_testDir
->createFiles(files
);
421 // Store the URLs of all folders in a set.
422 QSet
<KUrl
> allFolders
;
423 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
425 m_model
->loadDirectory(m_testDir
->url());
426 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
428 // So far, the model contains only "a/"
429 QCOMPARE(m_model
->count(), 1);
430 QVERIFY(m_model
->isExpandable(0));
431 QVERIFY(!m_model
->isExpanded(0));
432 QVERIFY(m_model
->expandedDirectories().empty());
434 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
436 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
437 m_model
->setExpanded(0, true);
438 QVERIFY(m_model
->isExpanded(0));
439 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
440 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
441 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
443 QCOMPARE(spyInserted
.count(), 1);
444 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
445 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
447 QVERIFY(m_model
->isExpandable(1));
448 QVERIFY(!m_model
->isExpanded(1));
449 QVERIFY(m_model
->isExpandable(2));
450 QVERIFY(!m_model
->isExpanded(2));
452 // Expand the folder "a/a/" -> "a/a/1" becomes visible
453 m_model
->setExpanded(1, true);
454 QVERIFY(m_model
->isExpanded(1));
455 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
456 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
457 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
459 QCOMPARE(spyInserted
.count(), 1);
460 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
461 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
463 QVERIFY(!m_model
->isExpandable(2));
464 QVERIFY(!m_model
->isExpanded(2));
466 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
467 m_model
->setExpanded(3, true);
468 QVERIFY(m_model
->isExpanded(3));
469 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
470 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
471 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
473 QCOMPARE(spyInserted
.count(), 1);
474 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
475 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
477 QVERIFY(!m_model
->isExpandable(4));
478 QVERIFY(!m_model
->isExpanded(4));
480 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
482 // Collapse the top-level folder -> all other items should disappear
483 m_model
->setExpanded(0, false);
484 QVERIFY(!m_model
->isExpanded(0));
485 QCOMPARE(m_model
->count(), 1);
486 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
488 QCOMPARE(spyRemoved
.count(), 1);
489 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
490 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
492 // Clear the model, reload the folder and try to restore the expanded folders.
494 QCOMPARE(m_model
->count(), 0);
495 QVERIFY(m_model
->expandedDirectories().empty());
497 m_model
->loadDirectory(m_testDir
->url());
498 m_model
->restoreExpandedDirectories(allFolders
);
499 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
500 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
501 QVERIFY(m_model
->isExpanded(0));
502 QVERIFY(m_model
->isExpanded(1));
503 QVERIFY(!m_model
->isExpanded(2));
504 QVERIFY(m_model
->isExpanded(3));
505 QVERIFY(!m_model
->isExpanded(4));
506 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
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));
571 void KFileItemModelTest::testSorting()
573 // Create some files with different sizes and modification times to check the different sorting options
574 QDateTime now
= QDateTime::currentDateTime();
576 QSet
<QByteArray
> roles
;
577 roles
.insert("text");
578 roles
.insert("isExpanded");
579 roles
.insert("isExpandable");
580 roles
.insert("expandedParentsCount");
581 m_model
->setRoles(roles
);
583 m_testDir
->createDir("c/c-2");
584 m_testDir
->createFile("c/c-2/c-3");
585 m_testDir
->createFile("c/c-1");
587 m_testDir
->createFile("a", "A file", now
.addDays(-3));
588 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
589 m_testDir
->createDir("c", now
.addDays(-2));
590 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
591 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
592 m_testDir
->createFile(".f");
594 m_model
->loadDirectory(m_testDir
->url());
595 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
597 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
598 m_model
->setExpanded(index
, true);
599 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
601 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
602 m_model
->setExpanded(index
, true);
603 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
605 // Default: Sort by Name, ascending
606 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
607 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
608 QVERIFY(m_model
->sortDirectoriesFirst());
609 QVERIFY(!m_model
->showHiddenFiles());
610 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
612 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
614 // Sort by Name, ascending, 'Sort Folders First' disabled
615 m_model
->setSortDirectoriesFirst(false);
616 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
617 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
618 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
619 QCOMPARE(spyItemsMoved
.count(), 1);
620 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
622 // Sort by Name, descending
623 m_model
->setSortDirectoriesFirst(true);
624 m_model
->setSortOrder(Qt::DescendingOrder
);
625 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
626 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
627 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
628 QCOMPARE(spyItemsMoved
.count(), 2);
629 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
630 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
632 // Sort by Date, descending
633 m_model
->setSortDirectoriesFirst(true);
634 m_model
->setSortRole("date");
635 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
636 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
637 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
638 QCOMPARE(spyItemsMoved
.count(), 1);
639 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
641 // Sort by Date, ascending
642 m_model
->setSortOrder(Qt::AscendingOrder
);
643 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
644 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
645 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
646 QCOMPARE(spyItemsMoved
.count(), 1);
647 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
649 // Sort by Date, ascending, 'Sort Folders First' disabled
650 m_model
->setSortDirectoriesFirst(false);
651 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
652 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
653 QVERIFY(!m_model
->sortDirectoriesFirst());
654 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
655 QCOMPARE(spyItemsMoved
.count(), 1);
656 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
658 // Sort by Name, ascending, 'Sort Folders First' disabled
659 m_model
->setSortRole("text");
660 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
661 QVERIFY(!m_model
->sortDirectoriesFirst());
662 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
663 QCOMPARE(spyItemsMoved
.count(), 1);
664 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
666 // Sort by Size, ascending, 'Sort Folders First' disabled
667 m_model
->setSortRole("size");
668 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
669 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
670 QVERIFY(!m_model
->sortDirectoriesFirst());
671 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
672 QCOMPARE(spyItemsMoved
.count(), 1);
673 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
675 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
676 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
677 "another signal", SkipSingle
);
678 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
680 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
681 m_model
->setSortDirectoriesFirst(true);
682 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
683 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
684 QVERIFY(m_model
->sortDirectoriesFirst());
685 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
686 QCOMPARE(spyItemsMoved
.count(), 0);
688 // Sort by Size, descending, 'Sort Folders First' enabled
689 m_model
->setSortOrder(Qt::DescendingOrder
);
690 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
691 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
692 QVERIFY(m_model
->sortDirectoriesFirst());
693 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
694 QCOMPARE(spyItemsMoved
.count(), 1);
695 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
697 // TODO: Sort by other roles; show/hide hidden files
700 void KFileItemModelTest::testIndexForKeyboardSearch()
703 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
704 m_testDir
->createFiles(files
);
706 m_model
->loadDirectory(m_testDir
->url());
707 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
709 // Search from index 0
710 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
711 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
712 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
713 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
714 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
715 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
716 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
717 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
718 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
719 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
720 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
722 // Start a search somewhere in the middle
723 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
724 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
725 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
726 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
728 // Test searches that go past the last item back to index 0
729 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
730 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
731 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
732 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
734 // Test searches that yield no result
735 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
736 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
737 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
738 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
739 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
741 // Test upper case searches (note that search is case insensitive)
742 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
743 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
744 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
745 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
747 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
750 void KFileItemModelTest::testNameFilter()
753 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
754 m_testDir
->createFiles(files
);
756 m_model
->loadDirectory(m_testDir
->url());
757 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
759 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
760 QCOMPARE(m_model
->count(), 3);
762 m_model
->setNameFilter("A2"); // Shows only A2
763 QCOMPARE(m_model
->count(), 1);
765 m_model
->setNameFilter("A2"); // Shows only A1
766 QCOMPARE(m_model
->count(), 1);
768 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
769 QCOMPARE(m_model
->count(), 2);
771 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
772 QCOMPARE(m_model
->count(), 2);
774 m_model
->setNameFilter(QString()); // Shows again all items
775 QCOMPARE(m_model
->count(), 5);
779 * Verifies that we do not crash when adding a KFileItem with an empty path.
780 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
781 * tried to always read the first character of the path, even if the path is empty.
783 void KFileItemModelTest::testEmptyPath()
785 QSet
<QByteArray
> roles
;
786 roles
.insert("text");
787 roles
.insert("isExpanded");
788 roles
.insert("isExpandable");
789 roles
.insert("expandedParentsCount");
790 m_model
->setRoles(roles
);
793 QVERIFY(emptyUrl
.path().isEmpty());
795 const KUrl
url("file:///test/");
798 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
799 m_model
->slotNewItems(items
);
800 m_model
->slotCompleted();
804 * Verify that removing hidden files and folders from the model does not
805 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
807 void KFileItemModelTest::testRemoveHiddenItems()
809 m_testDir
->createDir(".a");
810 m_testDir
->createDir(".b");
811 m_testDir
->createDir("c");
812 m_testDir
->createDir("d");
813 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
815 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
816 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
818 m_model
->setShowHiddenFiles(true);
819 m_model
->loadDirectory(m_testDir
->url());
820 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
821 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
822 QCOMPARE(spyItemsInserted
.count(), 1);
823 QCOMPARE(spyItemsRemoved
.count(), 0);
824 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
825 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
827 m_model
->setShowHiddenFiles(false);
828 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
829 QCOMPARE(spyItemsInserted
.count(), 0);
830 QCOMPARE(spyItemsRemoved
.count(), 1);
831 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
832 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
834 m_model
->setShowHiddenFiles(true);
835 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
836 QCOMPARE(spyItemsInserted
.count(), 1);
837 QCOMPARE(spyItemsRemoved
.count(), 0);
838 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
839 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
842 QCOMPARE(itemsInModel(), QStringList());
843 QCOMPARE(spyItemsInserted
.count(), 0);
844 QCOMPARE(spyItemsRemoved
.count(), 1);
845 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
846 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
848 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
849 // Verify that this does not make the model crash.
850 m_model
->setShowHiddenFiles(false);
854 * Verify that filtered items are removed when their parent is deleted.
856 void KFileItemModelTest::removeParentOfHiddenItems()
858 QSet
<QByteArray
> modelRoles
= m_model
->roles();
859 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
860 m_model
->setRoles(modelRoles
);
863 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
864 m_testDir
->createFiles(files
);
866 m_model
->loadDirectory(m_testDir
->url());
867 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
868 QCOMPARE(m_model
->count(), 1); // Only "a/"
871 m_model
->setExpanded(0, true);
872 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
873 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
876 m_model
->setExpanded(1, true);
877 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
878 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
881 m_model
->setExpanded(2, true);
882 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
883 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"
885 // Set a name filter that matches nothing -> only the expanded folders remain.
886 m_model
->setNameFilter("xyz");
887 QCOMPARE(m_model
->count(), 3);
888 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
890 // Simulate the deletion of the directory "a/b/".
891 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
892 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
893 QCOMPARE(spyItemsRemoved
.count(), 1);
894 QCOMPARE(m_model
->count(), 1);
895 QCOMPARE(itemsInModel(), QStringList() << "a");
897 // Remove the filter -> only the file "a/1" should appear.
898 m_model
->setNameFilter(QString());
899 QCOMPARE(m_model
->count(), 2);
900 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
903 bool KFileItemModelTest::isModelConsistent() const
905 if (m_model
->m_items
.count() != m_model
->m_itemData
.count()) {
909 for (int i
= 0; i
< m_model
->count(); ++i
) {
910 const KFileItem item
= m_model
->fileItem(i
);
912 qWarning() << "Item" << i
<< "is null";
916 const int itemIndex
= m_model
->index(item
);
917 if (itemIndex
!= i
) {
918 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
926 QStringList
KFileItemModelTest::itemsInModel() const
929 for (int i
= 0; i
< m_model
->count(); i
++) {
930 items
<< m_model
->data(i
).value("text").toString();
935 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
937 #include "kfileitemmodeltest.moc"