1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include <qtest_kde.h>
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
30 void myMessageOutput(QtMsgType type
, const char* msg
)
38 fprintf(stderr
, "Critical: %s\n", msg
);
41 fprintf(stderr
, "Fatal: %s\n", msg
);
49 const int DefaultTimeout
= 5000;
52 Q_DECLARE_METATYPE(KItemRangeList
)
53 Q_DECLARE_METATYPE(QList
<int>)
55 class KFileItemModelTest
: public QObject
63 void testDefaultRoles();
64 void testDefaultSortRole();
65 void testDefaultGroupedSorting();
67 void testRemoveItems();
68 void testDirLoadingCompleted();
70 void testSetDataWithModifiedSortRole_data();
71 void testSetDataWithModifiedSortRole();
72 void testModelConsistencyWhenInsertingItems();
73 void testItemRangeConsistencyWhenInsertingItems();
74 void testExpandItems();
75 void testExpandParentItems();
76 void testMakeExpandedItemHidden();
78 void testIndexForKeyboardSearch();
79 void testNameFilter();
81 void testRemoveHiddenItems();
84 QStringList
itemsInModel() const;
87 KFileItemModel
* m_model
;
91 void KFileItemModelTest::init()
93 // The item-model tests result in a huge number of debugging
94 // output from kdelibs. Only show critical and fatal messages.
95 qInstallMsgHandler(myMessageOutput
);
97 qRegisterMetaType
<KItemRange
>("KItemRange");
98 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
99 qRegisterMetaType
<KFileItemList
>("KFileItemList");
101 m_testDir
= new TestDir();
102 m_model
= new KFileItemModel();
103 m_model
->m_dirLister
->setAutoUpdate(false);
106 void KFileItemModelTest::cleanup()
115 void KFileItemModelTest::testDefaultRoles()
117 const QSet
<QByteArray
> roles
= m_model
->roles();
118 QCOMPARE(roles
.count(), 3);
119 QVERIFY(roles
.contains("text"));
120 QVERIFY(roles
.contains("isDir"));
121 QVERIFY(roles
.contains("isLink"));
124 void KFileItemModelTest::testDefaultSortRole()
126 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
129 files
<< "c.txt" << "a.txt" << "b.txt";
131 m_testDir
->createFiles(files
);
133 m_model
->loadDirectory(m_testDir
->url());
134 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
136 QCOMPARE(m_model
->count(), 3);
137 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
138 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
139 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
142 void KFileItemModelTest::testDefaultGroupedSorting()
144 QCOMPARE(m_model
->groupedSorting(), false);
147 void KFileItemModelTest::testNewItems()
150 files
<< "a.txt" << "b.txt" << "c.txt";
151 m_testDir
->createFiles(files
);
153 m_model
->loadDirectory(m_testDir
->url());
154 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
156 QCOMPARE(m_model
->count(), 3);
158 QVERIFY(m_model
->isConsistent());
161 void KFileItemModelTest::testRemoveItems()
163 m_testDir
->createFile("a.txt");
164 m_testDir
->createFile("b.txt");
165 m_model
->loadDirectory(m_testDir
->url());
166 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
167 QCOMPARE(m_model
->count(), 2);
168 QVERIFY(m_model
->isConsistent());
170 m_testDir
->removeFile("a.txt");
171 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
172 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
173 QCOMPARE(m_model
->count(), 1);
174 QVERIFY(m_model
->isConsistent());
177 void KFileItemModelTest::testDirLoadingCompleted()
179 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
180 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
181 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
183 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
185 m_model
->loadDirectory(m_testDir
->url());
186 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
187 QCOMPARE(loadingCompletedSpy
.count(), 1);
188 QCOMPARE(itemsInsertedSpy
.count(), 1);
189 QCOMPARE(itemsRemovedSpy
.count(), 0);
190 QCOMPARE(m_model
->count(), 3);
192 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
193 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
194 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
195 QCOMPARE(loadingCompletedSpy
.count(), 2);
196 QCOMPARE(itemsInsertedSpy
.count(), 2);
197 QCOMPARE(itemsRemovedSpy
.count(), 0);
198 QCOMPARE(m_model
->count(), 5);
200 m_testDir
->removeFile("a.txt");
201 m_testDir
->createFile("f.txt");
202 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
203 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
204 QCOMPARE(loadingCompletedSpy
.count(), 3);
205 QCOMPARE(itemsInsertedSpy
.count(), 3);
206 QCOMPARE(itemsRemovedSpy
.count(), 1);
207 QCOMPARE(m_model
->count(), 5);
209 m_testDir
->removeFile("b.txt");
210 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
211 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
212 QCOMPARE(loadingCompletedSpy
.count(), 4);
213 QCOMPARE(itemsInsertedSpy
.count(), 3);
214 QCOMPARE(itemsRemovedSpy
.count(), 2);
215 QCOMPARE(m_model
->count(), 4);
217 QVERIFY(m_model
->isConsistent());
220 void KFileItemModelTest::testSetData()
222 m_testDir
->createFile("a.txt");
224 m_model
->loadDirectory(m_testDir
->url());
225 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
227 QHash
<QByteArray
, QVariant
> values
;
228 values
.insert("customRole1", "Test1");
229 values
.insert("customRole2", "Test2");
231 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
232 m_model
->setData(0, values
);
233 QCOMPARE(itemsChangedSpy
.count(), 1);
235 values
= m_model
->data(0);
236 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
237 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
238 QVERIFY(m_model
->isConsistent());
241 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
243 QTest::addColumn
<int>("changedIndex");
244 QTest::addColumn
<int>("changedRating");
245 QTest::addColumn
<bool>("expectMoveSignal");
246 QTest::addColumn
<int>("ratingIndex0");
247 QTest::addColumn
<int>("ratingIndex1");
248 QTest::addColumn
<int>("ratingIndex2");
251 // Index 0 = rating 2
252 // Index 1 = rating 4
253 // Index 2 = rating 6
255 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
256 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
257 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
259 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
260 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
261 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
264 void KFileItemModelTest::testSetDataWithModifiedSortRole()
266 QFETCH(int, changedIndex
);
267 QFETCH(int, changedRating
);
268 QFETCH(bool, expectMoveSignal
);
269 QFETCH(int, ratingIndex0
);
270 QFETCH(int, ratingIndex1
);
271 QFETCH(int, ratingIndex2
);
273 // Changing the value of a sort-role must result in
274 // a reordering of the items.
275 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
278 files
<< "a.txt" << "b.txt" << "c.txt";
279 m_testDir
->createFiles(files
);
281 m_model
->loadDirectory(m_testDir
->url());
282 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
284 // Fill the "rating" role of each file:
289 QHash
<QByteArray
, QVariant
> ratingA
;
290 ratingA
.insert("rating", 2);
291 m_model
->setData(0, ratingA
);
293 QHash
<QByteArray
, QVariant
> ratingB
;
294 ratingB
.insert("rating", 4);
295 m_model
->setData(1, ratingB
);
297 QHash
<QByteArray
, QVariant
> ratingC
;
298 ratingC
.insert("rating", 6);
299 m_model
->setData(2, ratingC
);
301 m_model
->setSortRole("rating");
302 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
303 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
304 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
306 // Now change the rating from a.txt. This usually results
307 // in reordering of the items.
308 QHash
<QByteArray
, QVariant
> rating
;
309 rating
.insert("rating", changedRating
);
310 m_model
->setData(changedIndex
, rating
);
312 if (expectMoveSignal
) {
313 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
316 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
317 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
318 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
319 QVERIFY(m_model
->isConsistent());
322 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
324 //QSKIP("Temporary disabled", SkipSingle);
326 // KFileItemModel prevents that inserting a punch of items sequentially
327 // results in an itemsInserted()-signal for each item. Instead internally
328 // a timeout is given that collects such operations and results in only
329 // one itemsInserted()-signal. However in this test we want to stress
330 // KFileItemModel to do a lot of insert operation and hence decrease
331 // the timeout to 1 millisecond.
332 m_testDir
->createFile("1");
333 m_model
->loadDirectory(m_testDir
->url());
334 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
335 QCOMPARE(m_model
->count(), 1);
337 // Insert 10 items for 20 times. After each insert operation the model consistency
339 QSet
<int> insertedItems
;
340 for (int i
= 0; i
< 20; ++i
) {
341 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
343 for (int j
= 0; j
< 10; ++j
) {
344 int itemName
= qrand();
345 while (insertedItems
.contains(itemName
)) {
348 insertedItems
.insert(itemName
);
350 m_testDir
->createFile(QString::number(itemName
));
353 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
354 if (spy
.count() == 0) {
355 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
358 QVERIFY(m_model
->isConsistent());
361 QCOMPARE(m_model
->count(), 201);
364 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
367 files
<< "B" << "E" << "G";
368 m_testDir
->createFiles(files
);
370 // Due to inserting the 3 items one item-range with index == 0 and
371 // count == 3 must be given
372 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
373 m_model
->loadDirectory(m_testDir
->url());
374 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
376 QCOMPARE(spy1
.count(), 1);
377 QList
<QVariant
> arguments
= spy1
.takeFirst();
378 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
379 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
381 // The indexes of the item-ranges must always be related to the model before
382 // the items have been inserted. Having:
385 // and inserting A, C, D, F the resulting model will be:
388 // and the item-ranges must be:
389 // index: 0, count: 1 for A
390 // index: 1, count: 2 for B, C
391 // index: 2, count: 1 for G
394 files
<< "A" << "C" << "D" << "F";
395 m_testDir
->createFiles(files
);
397 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
398 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
399 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
401 QCOMPARE(spy2
.count(), 1);
402 arguments
= spy2
.takeFirst();
403 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
404 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
407 void KFileItemModelTest::testExpandItems()
409 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
410 // Besides testing the basic item expansion functionality, the test makes sure that
411 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
412 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
413 // first three characters.
414 QSet
<QByteArray
> modelRoles
= m_model
->roles();
415 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
416 m_model
->setRoles(modelRoles
);
419 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
420 m_testDir
->createFiles(files
);
422 // Store the URLs of all folders in a set.
423 QSet
<KUrl
> allFolders
;
424 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
426 m_model
->loadDirectory(m_testDir
->url());
427 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
429 // So far, the model contains only "a/"
430 QCOMPARE(m_model
->count(), 1);
431 QVERIFY(m_model
->isExpandable(0));
432 QVERIFY(!m_model
->isExpanded(0));
433 QVERIFY(m_model
->expandedDirectories().empty());
435 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
437 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
438 m_model
->setExpanded(0, true);
439 QVERIFY(m_model
->isExpanded(0));
440 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
441 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
442 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
444 QCOMPARE(spyInserted
.count(), 1);
445 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
446 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
448 QVERIFY(m_model
->isExpandable(1));
449 QVERIFY(!m_model
->isExpanded(1));
450 QVERIFY(m_model
->isExpandable(2));
451 QVERIFY(!m_model
->isExpanded(2));
453 // Expand the folder "a/a/" -> "a/a/1" becomes visible
454 m_model
->setExpanded(1, true);
455 QVERIFY(m_model
->isExpanded(1));
456 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
457 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
458 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
460 QCOMPARE(spyInserted
.count(), 1);
461 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
462 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
464 QVERIFY(!m_model
->isExpandable(2));
465 QVERIFY(!m_model
->isExpanded(2));
467 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
468 m_model
->setExpanded(3, true);
469 QVERIFY(m_model
->isExpanded(3));
470 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
471 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
472 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
474 QCOMPARE(spyInserted
.count(), 1);
475 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
476 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
478 QVERIFY(!m_model
->isExpandable(4));
479 QVERIFY(!m_model
->isExpanded(4));
481 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
483 // Collapse the top-level folder -> all other items should disappear
484 m_model
->setExpanded(0, false);
485 QVERIFY(!m_model
->isExpanded(0));
486 QCOMPARE(m_model
->count(), 1);
487 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
489 QCOMPARE(spyRemoved
.count(), 1);
490 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
491 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
492 QVERIFY(m_model
->isConsistent());
494 // Clear the model, reload the folder and try to restore the expanded folders.
496 QCOMPARE(m_model
->count(), 0);
497 QVERIFY(m_model
->expandedDirectories().empty());
499 m_model
->loadDirectory(m_testDir
->url());
500 m_model
->restoreExpandedDirectories(allFolders
);
501 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
502 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
503 QVERIFY(m_model
->isExpanded(0));
504 QVERIFY(m_model
->isExpanded(1));
505 QVERIFY(!m_model
->isExpanded(2));
506 QVERIFY(m_model
->isExpanded(3));
507 QVERIFY(!m_model
->isExpanded(4));
508 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
509 QVERIFY(m_model
->isConsistent());
511 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
512 // This is how DolphinView restores the expanded folders when navigating in history.
513 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
514 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
515 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
516 m_model
->restoreExpandedDirectories(allFolders
);
517 m_model
->loadDirectory(m_testDir
->url());
518 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
519 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
520 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
523 void KFileItemModelTest::testExpandParentItems()
525 // Create a tree structure of folders:
533 QSet
<QByteArray
> modelRoles
= m_model
->roles();
534 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
535 m_model
->setRoles(modelRoles
);
538 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
539 m_testDir
->createFiles(files
);
541 m_model
->loadDirectory(m_testDir
->url());
542 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
544 // So far, the model contains only "a 1/" and "a2/".
545 QCOMPARE(m_model
->count(), 2);
546 QVERIFY(m_model
->expandedDirectories().empty());
548 // Expand the parents of "a2/b2/c2".
549 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
550 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
552 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
553 // It's important that only the parents of "a1/b1/c1" are expanded.
554 QCOMPARE(m_model
->count(), 4);
555 QVERIFY(!m_model
->isExpanded(0));
556 QVERIFY(m_model
->isExpanded(1));
557 QVERIFY(m_model
->isExpanded(2));
558 QVERIFY(!m_model
->isExpanded(3));
560 // Expand the parents of "a 1/b1".
561 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
562 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
564 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
565 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
566 QCOMPARE(m_model
->count(), 5);
567 QVERIFY(m_model
->isExpanded(0));
568 QVERIFY(!m_model
->isExpanded(1));
569 QVERIFY(m_model
->isExpanded(2));
570 QVERIFY(m_model
->isExpanded(3));
571 QVERIFY(!m_model
->isExpanded(4));
572 QVERIFY(m_model
->isConsistent());
576 * Renaming an expanded folder by prepending its name with a dot makes it
577 * hidden. Verify that this does not cause an inconsistent model state and
578 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
580 void KFileItemModelTest::testMakeExpandedItemHidden()
582 QSet
<QByteArray
> modelRoles
= m_model
->roles();
583 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
584 m_model
->setRoles(modelRoles
);
587 m_testDir
->createFile("1a/2a/3a");
588 m_testDir
->createFile("1a/2a/3b");
589 m_testDir
->createFile("1a/2b");
590 m_testDir
->createFile("1b");
592 m_model
->loadDirectory(m_testDir
->url());
593 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
595 // So far, the model contains only "1a/" and "1b".
596 QCOMPARE(m_model
->count(), 2);
597 m_model
->setExpanded(0, true);
598 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
600 // Now "1a/2a" and "1a/2b" have appeared.
601 QCOMPARE(m_model
->count(), 4);
602 m_model
->setExpanded(1, true);
603 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
604 QCOMPARE(m_model
->count(), 6);
606 // Rename "1a/2" and make it hidden.
607 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
608 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
610 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
611 bool ok
= job
->exec();
613 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
615 // "1a/2" and its subfolders have disappeared now.
616 QVERIFY(m_model
->isConsistent());
617 QCOMPARE(m_model
->count(), 3);
619 m_model
->setExpanded(0, false);
620 QCOMPARE(m_model
->count(), 2);
624 void KFileItemModelTest::testSorting()
626 // Create some files with different sizes and modification times to check the different sorting options
627 QDateTime now
= QDateTime::currentDateTime();
629 QSet
<QByteArray
> roles
;
630 roles
.insert("text");
631 roles
.insert("isExpanded");
632 roles
.insert("isExpandable");
633 roles
.insert("expandedParentsCount");
634 m_model
->setRoles(roles
);
636 m_testDir
->createDir("c/c-2");
637 m_testDir
->createFile("c/c-2/c-3");
638 m_testDir
->createFile("c/c-1");
640 m_testDir
->createFile("a", "A file", now
.addDays(-3));
641 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
642 m_testDir
->createDir("c", now
.addDays(-2));
643 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
644 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
645 m_testDir
->createFile(".f");
647 m_model
->loadDirectory(m_testDir
->url());
648 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
650 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
651 m_model
->setExpanded(index
, true);
652 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
654 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
655 m_model
->setExpanded(index
, true);
656 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
658 // Default: Sort by Name, ascending
659 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
660 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
661 QVERIFY(m_model
->sortDirectoriesFirst());
662 QVERIFY(!m_model
->showHiddenFiles());
663 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
665 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
667 // Sort by Name, ascending, 'Sort Folders First' disabled
668 m_model
->setSortDirectoriesFirst(false);
669 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
670 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
671 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
672 QCOMPARE(spyItemsMoved
.count(), 1);
673 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
675 // Sort by Name, descending
676 m_model
->setSortDirectoriesFirst(true);
677 m_model
->setSortOrder(Qt::DescendingOrder
);
678 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
679 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
680 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
681 QCOMPARE(spyItemsMoved
.count(), 2);
682 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
683 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
685 // Sort by Date, descending
686 m_model
->setSortDirectoriesFirst(true);
687 m_model
->setSortRole("date");
688 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
689 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
690 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
691 QCOMPARE(spyItemsMoved
.count(), 1);
692 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
694 // Sort by Date, ascending
695 m_model
->setSortOrder(Qt::AscendingOrder
);
696 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
697 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
698 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
699 QCOMPARE(spyItemsMoved
.count(), 1);
700 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
702 // Sort by Date, ascending, 'Sort Folders First' disabled
703 m_model
->setSortDirectoriesFirst(false);
704 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
705 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
706 QVERIFY(!m_model
->sortDirectoriesFirst());
707 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
708 QCOMPARE(spyItemsMoved
.count(), 1);
709 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
711 // Sort by Name, ascending, 'Sort Folders First' disabled
712 m_model
->setSortRole("text");
713 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
714 QVERIFY(!m_model
->sortDirectoriesFirst());
715 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
716 QCOMPARE(spyItemsMoved
.count(), 1);
717 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
719 // Sort by Size, ascending, 'Sort Folders First' disabled
720 m_model
->setSortRole("size");
721 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
722 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
723 QVERIFY(!m_model
->sortDirectoriesFirst());
724 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
725 QCOMPARE(spyItemsMoved
.count(), 1);
726 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
728 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
729 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
730 "another signal", SkipSingle
);
731 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
733 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
734 m_model
->setSortDirectoriesFirst(true);
735 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
736 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
737 QVERIFY(m_model
->sortDirectoriesFirst());
738 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
739 QCOMPARE(spyItemsMoved
.count(), 0);
741 // Sort by Size, descending, 'Sort Folders First' enabled
742 m_model
->setSortOrder(Qt::DescendingOrder
);
743 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
744 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
745 QVERIFY(m_model
->sortDirectoriesFirst());
746 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
747 QCOMPARE(spyItemsMoved
.count(), 1);
748 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
750 // TODO: Sort by other roles; show/hide hidden files
753 void KFileItemModelTest::testIndexForKeyboardSearch()
756 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
757 m_testDir
->createFiles(files
);
759 m_model
->loadDirectory(m_testDir
->url());
760 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
762 // Search from index 0
763 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
764 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
765 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
766 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
767 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
768 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
769 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
770 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
771 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
772 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
773 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
775 // Start a search somewhere in the middle
776 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
777 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
778 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
779 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
781 // Test searches that go past the last item back to index 0
782 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
783 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
784 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
785 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
787 // Test searches that yield no result
788 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
789 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
790 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
791 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
792 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
794 // Test upper case searches (note that search is case insensitive)
795 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
796 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
797 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
798 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
800 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
803 void KFileItemModelTest::testNameFilter()
806 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
807 m_testDir
->createFiles(files
);
809 m_model
->loadDirectory(m_testDir
->url());
810 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
812 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
813 QCOMPARE(m_model
->count(), 3);
815 m_model
->setNameFilter("A2"); // Shows only A2
816 QCOMPARE(m_model
->count(), 1);
818 m_model
->setNameFilter("A2"); // Shows only A1
819 QCOMPARE(m_model
->count(), 1);
821 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
822 QCOMPARE(m_model
->count(), 2);
824 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
825 QCOMPARE(m_model
->count(), 2);
827 m_model
->setNameFilter(QString()); // Shows again all items
828 QCOMPARE(m_model
->count(), 5);
832 * Verifies that we do not crash when adding a KFileItem with an empty path.
833 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
834 * tried to always read the first character of the path, even if the path is empty.
836 void KFileItemModelTest::testEmptyPath()
838 QSet
<QByteArray
> roles
;
839 roles
.insert("text");
840 roles
.insert("isExpanded");
841 roles
.insert("isExpandable");
842 roles
.insert("expandedParentsCount");
843 m_model
->setRoles(roles
);
846 QVERIFY(emptyUrl
.path().isEmpty());
848 const KUrl
url("file:///test/");
851 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
852 m_model
->slotNewItems(items
);
853 m_model
->slotCompleted();
857 * Verify that removing hidden files and folders from the model does not
858 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
860 void KFileItemModelTest::testRemoveHiddenItems()
862 m_testDir
->createDir(".a");
863 m_testDir
->createDir(".b");
864 m_testDir
->createDir("c");
865 m_testDir
->createDir("d");
866 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
868 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
869 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
871 m_model
->setShowHiddenFiles(true);
872 m_model
->loadDirectory(m_testDir
->url());
873 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
874 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
875 QCOMPARE(spyItemsInserted
.count(), 1);
876 QCOMPARE(spyItemsRemoved
.count(), 0);
877 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
878 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
880 m_model
->setShowHiddenFiles(false);
881 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
882 QCOMPARE(spyItemsInserted
.count(), 0);
883 QCOMPARE(spyItemsRemoved
.count(), 1);
884 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
885 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
887 m_model
->setShowHiddenFiles(true);
888 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
889 QCOMPARE(spyItemsInserted
.count(), 1);
890 QCOMPARE(spyItemsRemoved
.count(), 0);
891 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
892 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
895 QCOMPARE(itemsInModel(), QStringList());
896 QCOMPARE(spyItemsInserted
.count(), 0);
897 QCOMPARE(spyItemsRemoved
.count(), 1);
898 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
899 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
901 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
902 // Verify that this does not make the model crash.
903 m_model
->setShowHiddenFiles(false);
906 QStringList
KFileItemModelTest::itemsInModel() const
909 for (int i
= 0; i
< m_model
->count(); i
++) {
910 items
<< m_model
->data(i
).value("text").toString();
915 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
917 #include "kfileitemmodeltest.moc"