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