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 bool isModelConsistent() const;
82 QStringList
itemsInModel() const;
85 KFileItemModel
* m_model
;
89 void KFileItemModelTest::init()
91 // The item-model tests result in a huge number of debugging
92 // output from kdelibs. Only show critical and fatal messages.
93 qInstallMsgHandler(myMessageOutput
);
95 qRegisterMetaType
<KItemRange
>("KItemRange");
96 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
97 qRegisterMetaType
<KFileItemList
>("KFileItemList");
99 m_testDir
= new TestDir();
100 m_model
= new KFileItemModel();
101 m_model
->m_dirLister
->setAutoUpdate(false);
104 void KFileItemModelTest::cleanup()
113 void KFileItemModelTest::testDefaultRoles()
115 const QSet
<QByteArray
> roles
= m_model
->roles();
116 QCOMPARE(roles
.count(), 3);
117 QVERIFY(roles
.contains("text"));
118 QVERIFY(roles
.contains("isDir"));
119 QVERIFY(roles
.contains("isLink"));
122 void KFileItemModelTest::testDefaultSortRole()
124 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
127 files
<< "c.txt" << "a.txt" << "b.txt";
129 m_testDir
->createFiles(files
);
131 m_model
->loadDirectory(m_testDir
->url());
132 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
134 QCOMPARE(m_model
->count(), 3);
135 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
136 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
137 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
140 void KFileItemModelTest::testDefaultGroupedSorting()
142 QCOMPARE(m_model
->groupedSorting(), false);
145 void KFileItemModelTest::testNewItems()
148 files
<< "a.txt" << "b.txt" << "c.txt";
149 m_testDir
->createFiles(files
);
151 m_model
->loadDirectory(m_testDir
->url());
152 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
154 QCOMPARE(m_model
->count(), 3);
156 QVERIFY(isModelConsistent());
159 void KFileItemModelTest::testRemoveItems()
161 m_testDir
->createFile("a.txt");
162 m_testDir
->createFile("b.txt");
163 m_model
->loadDirectory(m_testDir
->url());
164 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
165 QCOMPARE(m_model
->count(), 2);
166 QVERIFY(isModelConsistent());
168 m_testDir
->removeFile("a.txt");
169 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
170 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
171 QCOMPARE(m_model
->count(), 1);
172 QVERIFY(isModelConsistent());
175 void KFileItemModelTest::testDirLoadingCompleted()
177 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
178 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
179 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
181 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
183 m_model
->loadDirectory(m_testDir
->url());
184 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
185 QCOMPARE(loadingCompletedSpy
.count(), 1);
186 QCOMPARE(itemsInsertedSpy
.count(), 1);
187 QCOMPARE(itemsRemovedSpy
.count(), 0);
188 QCOMPARE(m_model
->count(), 3);
190 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
191 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
192 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
193 QCOMPARE(loadingCompletedSpy
.count(), 2);
194 QCOMPARE(itemsInsertedSpy
.count(), 2);
195 QCOMPARE(itemsRemovedSpy
.count(), 0);
196 QCOMPARE(m_model
->count(), 5);
198 m_testDir
->removeFile("a.txt");
199 m_testDir
->createFile("f.txt");
200 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
201 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
202 QCOMPARE(loadingCompletedSpy
.count(), 3);
203 QCOMPARE(itemsInsertedSpy
.count(), 3);
204 QCOMPARE(itemsRemovedSpy
.count(), 1);
205 QCOMPARE(m_model
->count(), 5);
207 m_testDir
->removeFile("b.txt");
208 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
209 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
210 QCOMPARE(loadingCompletedSpy
.count(), 4);
211 QCOMPARE(itemsInsertedSpy
.count(), 3);
212 QCOMPARE(itemsRemovedSpy
.count(), 2);
213 QCOMPARE(m_model
->count(), 4);
215 QVERIFY(isModelConsistent());
218 void KFileItemModelTest::testSetData()
220 m_testDir
->createFile("a.txt");
222 m_model
->loadDirectory(m_testDir
->url());
223 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
225 QHash
<QByteArray
, QVariant
> values
;
226 values
.insert("customRole1", "Test1");
227 values
.insert("customRole2", "Test2");
229 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
230 m_model
->setData(0, values
);
231 QCOMPARE(itemsChangedSpy
.count(), 1);
233 values
= m_model
->data(0);
234 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
235 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
236 QVERIFY(isModelConsistent());
239 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
241 QTest::addColumn
<int>("changedIndex");
242 QTest::addColumn
<int>("changedRating");
243 QTest::addColumn
<bool>("expectMoveSignal");
244 QTest::addColumn
<int>("ratingIndex0");
245 QTest::addColumn
<int>("ratingIndex1");
246 QTest::addColumn
<int>("ratingIndex2");
249 // Index 0 = rating 2
250 // Index 1 = rating 4
251 // Index 2 = rating 6
253 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
254 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
255 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
257 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
258 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
259 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
262 void KFileItemModelTest::testSetDataWithModifiedSortRole()
264 QFETCH(int, changedIndex
);
265 QFETCH(int, changedRating
);
266 QFETCH(bool, expectMoveSignal
);
267 QFETCH(int, ratingIndex0
);
268 QFETCH(int, ratingIndex1
);
269 QFETCH(int, ratingIndex2
);
271 // Changing the value of a sort-role must result in
272 // a reordering of the items.
273 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
276 files
<< "a.txt" << "b.txt" << "c.txt";
277 m_testDir
->createFiles(files
);
279 m_model
->loadDirectory(m_testDir
->url());
280 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
282 // Fill the "rating" role of each file:
287 QHash
<QByteArray
, QVariant
> ratingA
;
288 ratingA
.insert("rating", 2);
289 m_model
->setData(0, ratingA
);
291 QHash
<QByteArray
, QVariant
> ratingB
;
292 ratingB
.insert("rating", 4);
293 m_model
->setData(1, ratingB
);
295 QHash
<QByteArray
, QVariant
> ratingC
;
296 ratingC
.insert("rating", 6);
297 m_model
->setData(2, ratingC
);
299 m_model
->setSortRole("rating");
300 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
301 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
302 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
304 // Now change the rating from a.txt. This usually results
305 // in reordering of the items.
306 QHash
<QByteArray
, QVariant
> rating
;
307 rating
.insert("rating", changedRating
);
308 m_model
->setData(changedIndex
, rating
);
310 if (expectMoveSignal
) {
311 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
314 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
315 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
316 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
317 QVERIFY(isModelConsistent());
320 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
322 //QSKIP("Temporary disabled", SkipSingle);
324 // KFileItemModel prevents that inserting a punch of items sequentially
325 // results in an itemsInserted()-signal for each item. Instead internally
326 // a timeout is given that collects such operations and results in only
327 // one itemsInserted()-signal. However in this test we want to stress
328 // KFileItemModel to do a lot of insert operation and hence decrease
329 // the timeout to 1 millisecond.
330 m_testDir
->createFile("1");
331 m_model
->loadDirectory(m_testDir
->url());
332 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
333 QCOMPARE(m_model
->count(), 1);
335 // Insert 10 items for 20 times. After each insert operation the model consistency
337 QSet
<int> insertedItems
;
338 for (int i
= 0; i
< 20; ++i
) {
339 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
341 for (int j
= 0; j
< 10; ++j
) {
342 int itemName
= qrand();
343 while (insertedItems
.contains(itemName
)) {
346 insertedItems
.insert(itemName
);
348 m_testDir
->createFile(QString::number(itemName
));
351 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
352 if (spy
.count() == 0) {
353 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
356 QVERIFY(isModelConsistent());
359 QCOMPARE(m_model
->count(), 201);
362 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
365 files
<< "B" << "E" << "G";
366 m_testDir
->createFiles(files
);
368 // Due to inserting the 3 items one item-range with index == 0 and
369 // count == 3 must be given
370 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
371 m_model
->loadDirectory(m_testDir
->url());
372 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
374 QCOMPARE(spy1
.count(), 1);
375 QList
<QVariant
> arguments
= spy1
.takeFirst();
376 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
377 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
379 // The indexes of the item-ranges must always be related to the model before
380 // the items have been inserted. Having:
383 // and inserting A, C, D, F the resulting model will be:
386 // and the item-ranges must be:
387 // index: 0, count: 1 for A
388 // index: 1, count: 2 for B, C
389 // index: 2, count: 1 for G
392 files
<< "A" << "C" << "D" << "F";
393 m_testDir
->createFiles(files
);
395 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
396 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
397 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
399 QCOMPARE(spy2
.count(), 1);
400 arguments
= spy2
.takeFirst();
401 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
402 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
405 void KFileItemModelTest::testExpandItems()
407 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
408 // Besides testing the basic item expansion functionality, the test makes sure that
409 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
410 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
411 // first three characters.
412 QSet
<QByteArray
> modelRoles
= m_model
->roles();
413 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
414 m_model
->setRoles(modelRoles
);
417 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
418 m_testDir
->createFiles(files
);
420 // Store the URLs of all folders in a set.
421 QSet
<KUrl
> allFolders
;
422 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
424 m_model
->loadDirectory(m_testDir
->url());
425 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
427 // So far, the model contains only "a/"
428 QCOMPARE(m_model
->count(), 1);
429 QVERIFY(m_model
->isExpandable(0));
430 QVERIFY(!m_model
->isExpanded(0));
431 QVERIFY(m_model
->expandedDirectories().empty());
433 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
435 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
436 m_model
->setExpanded(0, true);
437 QVERIFY(m_model
->isExpanded(0));
438 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
439 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
440 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
442 QCOMPARE(spyInserted
.count(), 1);
443 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
444 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
446 QVERIFY(m_model
->isExpandable(1));
447 QVERIFY(!m_model
->isExpanded(1));
448 QVERIFY(m_model
->isExpandable(2));
449 QVERIFY(!m_model
->isExpanded(2));
451 // Expand the folder "a/a/" -> "a/a/1" becomes visible
452 m_model
->setExpanded(1, true);
453 QVERIFY(m_model
->isExpanded(1));
454 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
455 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
456 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
458 QCOMPARE(spyInserted
.count(), 1);
459 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
460 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
462 QVERIFY(!m_model
->isExpandable(2));
463 QVERIFY(!m_model
->isExpanded(2));
465 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
466 m_model
->setExpanded(3, true);
467 QVERIFY(m_model
->isExpanded(3));
468 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
469 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
470 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
472 QCOMPARE(spyInserted
.count(), 1);
473 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
474 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
476 QVERIFY(!m_model
->isExpandable(4));
477 QVERIFY(!m_model
->isExpanded(4));
479 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
481 // Collapse the top-level folder -> all other items should disappear
482 m_model
->setExpanded(0, false);
483 QVERIFY(!m_model
->isExpanded(0));
484 QCOMPARE(m_model
->count(), 1);
485 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
487 QCOMPARE(spyRemoved
.count(), 1);
488 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
489 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
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
);
507 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
508 // This is how DolphinView restores the expanded folders when navigating in history.
509 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
510 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
511 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
512 m_model
->restoreExpandedDirectories(allFolders
);
513 m_model
->loadDirectory(m_testDir
->url());
514 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
515 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
516 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
519 void KFileItemModelTest::testExpandParentItems()
521 // Create a tree structure of folders:
529 QSet
<QByteArray
> modelRoles
= m_model
->roles();
530 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
531 m_model
->setRoles(modelRoles
);
534 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
535 m_testDir
->createFiles(files
);
537 m_model
->loadDirectory(m_testDir
->url());
538 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
540 // So far, the model contains only "a 1/" and "a2/".
541 QCOMPARE(m_model
->count(), 2);
542 QVERIFY(m_model
->expandedDirectories().empty());
544 // Expand the parents of "a2/b2/c2".
545 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
546 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
548 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
549 // It's important that only the parents of "a1/b1/c1" are expanded.
550 QCOMPARE(m_model
->count(), 4);
551 QVERIFY(!m_model
->isExpanded(0));
552 QVERIFY(m_model
->isExpanded(1));
553 QVERIFY(m_model
->isExpanded(2));
554 QVERIFY(!m_model
->isExpanded(3));
556 // Expand the parents of "a 1/b1".
557 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
558 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
560 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
561 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
562 QCOMPARE(m_model
->count(), 5);
563 QVERIFY(m_model
->isExpanded(0));
564 QVERIFY(!m_model
->isExpanded(1));
565 QVERIFY(m_model
->isExpanded(2));
566 QVERIFY(m_model
->isExpanded(3));
567 QVERIFY(!m_model
->isExpanded(4));
570 void KFileItemModelTest::testSorting()
572 // Create some files with different sizes and modification times to check the different sorting options
573 QDateTime now
= QDateTime::currentDateTime();
575 QSet
<QByteArray
> roles
;
576 roles
.insert("text");
577 roles
.insert("isExpanded");
578 roles
.insert("isExpandable");
579 roles
.insert("expandedParentsCount");
580 m_model
->setRoles(roles
);
582 m_testDir
->createDir("c/c-2");
583 m_testDir
->createFile("c/c-2/c-3");
584 m_testDir
->createFile("c/c-1");
586 m_testDir
->createFile("a", "A file", now
.addDays(-3));
587 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
588 m_testDir
->createDir("c", now
.addDays(-2));
589 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
590 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
591 m_testDir
->createFile(".f");
593 m_model
->loadDirectory(m_testDir
->url());
594 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
596 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
597 m_model
->setExpanded(index
, true);
598 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
600 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
601 m_model
->setExpanded(index
, true);
602 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
604 // Default: Sort by Name, ascending
605 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
606 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
607 QVERIFY(m_model
->sortDirectoriesFirst());
608 QVERIFY(!m_model
->showHiddenFiles());
609 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
611 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
613 // Sort by Name, ascending, 'Sort Folders First' disabled
614 m_model
->setSortDirectoriesFirst(false);
615 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
616 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
617 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
618 QCOMPARE(spyItemsMoved
.count(), 1);
619 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
621 // Sort by Name, descending
622 m_model
->setSortDirectoriesFirst(true);
623 m_model
->setSortOrder(Qt::DescendingOrder
);
624 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
625 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
626 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
627 QCOMPARE(spyItemsMoved
.count(), 2);
628 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
629 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
631 // Sort by Date, descending
632 m_model
->setSortDirectoriesFirst(true);
633 m_model
->setSortRole("date");
634 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
635 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
636 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
637 QCOMPARE(spyItemsMoved
.count(), 1);
638 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
640 // Sort by Date, ascending
641 m_model
->setSortOrder(Qt::AscendingOrder
);
642 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
643 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
644 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
645 QCOMPARE(spyItemsMoved
.count(), 1);
646 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
648 // Sort by Date, ascending, 'Sort Folders First' disabled
649 m_model
->setSortDirectoriesFirst(false);
650 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
651 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
652 QVERIFY(!m_model
->sortDirectoriesFirst());
653 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
654 QCOMPARE(spyItemsMoved
.count(), 1);
655 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
657 // Sort by Name, ascending, 'Sort Folders First' disabled
658 m_model
->setSortRole("text");
659 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
660 QVERIFY(!m_model
->sortDirectoriesFirst());
661 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
662 QCOMPARE(spyItemsMoved
.count(), 1);
663 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
665 // Sort by Size, ascending, 'Sort Folders First' disabled
666 m_model
->setSortRole("size");
667 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
668 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
669 QVERIFY(!m_model
->sortDirectoriesFirst());
670 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
671 QCOMPARE(spyItemsMoved
.count(), 1);
672 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
674 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
675 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
676 "another signal", SkipSingle
);
677 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
679 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
680 m_model
->setSortDirectoriesFirst(true);
681 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
682 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
683 QVERIFY(m_model
->sortDirectoriesFirst());
684 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
685 QCOMPARE(spyItemsMoved
.count(), 0);
687 // Sort by Size, descending, 'Sort Folders First' enabled
688 m_model
->setSortOrder(Qt::DescendingOrder
);
689 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
690 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
691 QVERIFY(m_model
->sortDirectoriesFirst());
692 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
693 QCOMPARE(spyItemsMoved
.count(), 1);
694 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
696 // TODO: Sort by other roles; show/hide hidden files
699 void KFileItemModelTest::testIndexForKeyboardSearch()
702 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
703 m_testDir
->createFiles(files
);
705 m_model
->loadDirectory(m_testDir
->url());
706 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
708 // Search from index 0
709 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
710 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
711 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
712 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
713 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
714 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
715 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
716 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
717 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
718 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
719 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
721 // Start a search somewhere in the middle
722 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
723 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
724 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
725 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
727 // Test searches that go past the last item back to index 0
728 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
729 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
730 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
731 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
733 // Test searches that yield no result
734 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
735 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
736 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
737 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
738 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
740 // Test upper case searches (note that search is case insensitive)
741 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
742 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
743 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
744 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
746 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
749 void KFileItemModelTest::testNameFilter()
752 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
753 m_testDir
->createFiles(files
);
755 m_model
->loadDirectory(m_testDir
->url());
756 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
758 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
759 QCOMPARE(m_model
->count(), 3);
761 m_model
->setNameFilter("A2"); // Shows only A2
762 QCOMPARE(m_model
->count(), 1);
764 m_model
->setNameFilter("A2"); // Shows only A1
765 QCOMPARE(m_model
->count(), 1);
767 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
768 QCOMPARE(m_model
->count(), 2);
770 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
771 QCOMPARE(m_model
->count(), 2);
773 m_model
->setNameFilter(QString()); // Shows again all items
774 QCOMPARE(m_model
->count(), 5);
778 * Verifies that we do not crash when adding a KFileItem with an empty path.
779 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
780 * tried to always read the first character of the path, even if the path is empty.
782 void KFileItemModelTest::testEmptyPath()
784 QSet
<QByteArray
> roles
;
785 roles
.insert("text");
786 roles
.insert("isExpanded");
787 roles
.insert("isExpandable");
788 roles
.insert("expandedParentsCount");
789 m_model
->setRoles(roles
);
792 QVERIFY(emptyUrl
.path().isEmpty());
794 const KUrl
url("file:///test/");
797 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
798 m_model
->slotNewItems(items
);
799 m_model
->slotCompleted();
803 * Verify that removing hidden files and folders from the model does not
804 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
806 void KFileItemModelTest::testRemoveHiddenItems()
808 m_testDir
->createDir(".a");
809 m_testDir
->createDir(".b");
810 m_testDir
->createDir("c");
811 m_testDir
->createDir("d");
812 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
814 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
815 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
817 m_model
->setShowHiddenFiles(true);
818 m_model
->loadDirectory(m_testDir
->url());
819 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
820 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
821 QCOMPARE(spyItemsInserted
.count(), 1);
822 QCOMPARE(spyItemsRemoved
.count(), 0);
823 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
824 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
826 m_model
->setShowHiddenFiles(false);
827 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
828 QCOMPARE(spyItemsInserted
.count(), 0);
829 QCOMPARE(spyItemsRemoved
.count(), 1);
830 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
831 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
833 m_model
->setShowHiddenFiles(true);
834 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
835 QCOMPARE(spyItemsInserted
.count(), 1);
836 QCOMPARE(spyItemsRemoved
.count(), 0);
837 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
838 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
841 QCOMPARE(itemsInModel(), QStringList());
842 QCOMPARE(spyItemsInserted
.count(), 0);
843 QCOMPARE(spyItemsRemoved
.count(), 1);
844 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
845 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
847 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
848 // Verify that this does not make the model crash.
849 m_model
->setShowHiddenFiles(false);
852 bool KFileItemModelTest::isModelConsistent() const
854 if (m_model
->m_items
.count() != m_model
->m_itemData
.count()) {
858 for (int i
= 0; i
< m_model
->count(); ++i
) {
859 const KFileItem item
= m_model
->fileItem(i
);
861 qWarning() << "Item" << i
<< "is null";
865 const int itemIndex
= m_model
->index(item
);
866 if (itemIndex
!= i
) {
867 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
875 QStringList
KFileItemModelTest::itemsInModel() const
878 for (int i
= 0; i
< m_model
->count(); i
++) {
879 items
<< m_model
->data(i
).value("text").toString();
884 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
886 #include "kfileitemmodeltest.moc"