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"
27 void myMessageOutput(QtMsgType type
, const char* msg
)
35 fprintf(stderr
, "Critical: %s\n", msg
);
38 fprintf(stderr
, "Fatal: %s\n", msg
);
46 const int DefaultTimeout
= 5000;
49 Q_DECLARE_METATYPE(KItemRangeList
)
50 Q_DECLARE_METATYPE(QList
<int>)
52 class KFileItemModelTest
: public QObject
60 void testDefaultRoles();
61 void testDefaultSortRole();
62 void testDefaultGroupedSorting();
64 void testRemoveItems();
66 void testSetDataWithModifiedSortRole_data();
67 void testSetDataWithModifiedSortRole();
68 void testModelConsistencyWhenInsertingItems();
69 void testItemRangeConsistencyWhenInsertingItems();
70 void testExpandItems();
71 void testExpandParentItems();
74 void testExpansionLevelsCompare_data();
75 void testExpansionLevelsCompare();
77 void testIndexForKeyboardSearch();
79 void testNameFilter();
82 bool isModelConsistent() const;
83 QStringList
itemsInModel() const;
86 KFileItemModel
* m_model
;
87 KDirLister
* m_dirLister
;
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_dirLister
= new KDirLister();
103 m_model
= new KFileItemModel(m_dirLister
);
106 void KFileItemModelTest::cleanup()
118 void KFileItemModelTest::testDefaultRoles()
120 const QSet
<QByteArray
> roles
= m_model
->roles();
121 QCOMPARE(roles
.count(), 2);
122 QVERIFY(roles
.contains("name"));
123 QVERIFY(roles
.contains("isDir"));
126 void KFileItemModelTest::testDefaultSortRole()
128 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
131 files
<< "c.txt" << "a.txt" << "b.txt";
133 m_testDir
->createFiles(files
);
135 m_dirLister
->openUrl(m_testDir
->url());
136 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
138 QCOMPARE(m_model
->count(), 3);
139 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
140 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
141 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
144 void KFileItemModelTest::testDefaultGroupedSorting()
146 QCOMPARE(m_model
->groupedSorting(), false);
149 void KFileItemModelTest::testNewItems()
152 files
<< "a.txt" << "b.txt" << "c.txt";
153 m_testDir
->createFiles(files
);
155 m_dirLister
->openUrl(m_testDir
->url());
156 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
158 QCOMPARE(m_model
->count(), 3);
160 QVERIFY(isModelConsistent());
163 void KFileItemModelTest::testRemoveItems()
165 m_testDir
->createFile("a.txt");
166 m_testDir
->createFile("b.txt");
167 m_dirLister
->openUrl(m_testDir
->url());
168 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
169 QCOMPARE(m_model
->count(), 2);
170 QVERIFY(isModelConsistent());
172 m_testDir
->removeFile("a.txt");
173 m_dirLister
->updateDirectory(m_testDir
->url());
174 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
175 QCOMPARE(m_model
->count(), 1);
176 QVERIFY(isModelConsistent());
179 void KFileItemModelTest::testSetData()
181 m_testDir
->createFile("a.txt");
183 m_dirLister
->openUrl(m_testDir
->url());
184 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
186 QHash
<QByteArray
, QVariant
> values
;
187 values
.insert("customRole1", "Test1");
188 values
.insert("customRole2", "Test2");
190 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
191 m_model
->setData(0, values
);
192 QCOMPARE(itemsChangedSpy
.count(), 1);
194 values
= m_model
->data(0);
195 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
196 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
197 QVERIFY(isModelConsistent());
200 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
202 QTest::addColumn
<int>("changedIndex");
203 QTest::addColumn
<int>("changedRating");
204 QTest::addColumn
<bool>("expectMoveSignal");
205 QTest::addColumn
<int>("ratingIndex0");
206 QTest::addColumn
<int>("ratingIndex1");
207 QTest::addColumn
<int>("ratingIndex2");
210 // Index 0 = rating 2
211 // Index 1 = rating 4
212 // Index 2 = rating 6
214 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
215 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
216 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
218 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
219 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
220 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
223 void KFileItemModelTest::testSetDataWithModifiedSortRole()
225 QFETCH(int, changedIndex
);
226 QFETCH(int, changedRating
);
227 QFETCH(bool, expectMoveSignal
);
228 QFETCH(int, ratingIndex0
);
229 QFETCH(int, ratingIndex1
);
230 QFETCH(int, ratingIndex2
);
232 // Changing the value of a sort-role must result in
233 // a reordering of the items.
234 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
237 files
<< "a.txt" << "b.txt" << "c.txt";
238 m_testDir
->createFiles(files
);
240 m_dirLister
->openUrl(m_testDir
->url());
241 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
243 // Fill the "rating" role of each file:
248 QHash
<QByteArray
, QVariant
> ratingA
;
249 ratingA
.insert("rating", 2);
250 m_model
->setData(0, ratingA
);
252 QHash
<QByteArray
, QVariant
> ratingB
;
253 ratingB
.insert("rating", 4);
254 m_model
->setData(1, ratingB
);
256 QHash
<QByteArray
, QVariant
> ratingC
;
257 ratingC
.insert("rating", 6);
258 m_model
->setData(2, ratingC
);
260 m_model
->setSortRole("rating");
261 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
262 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
263 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
265 // Now change the rating from a.txt. This usually results
266 // in reordering of the items.
267 QHash
<QByteArray
, QVariant
> rating
;
268 rating
.insert("rating", changedRating
);
269 m_model
->setData(changedIndex
, rating
);
271 if (expectMoveSignal
) {
272 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
275 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
276 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
277 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
278 QVERIFY(isModelConsistent());
281 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
283 //QSKIP("Temporary disabled", SkipSingle);
285 // KFileItemModel prevents that inserting a punch of items sequentially
286 // results in an itemsInserted()-signal for each item. Instead internally
287 // a timeout is given that collects such operations and results in only
288 // one itemsInserted()-signal. However in this test we want to stress
289 // KFileItemModel to do a lot of insert operation and hence decrease
290 // the timeout to 1 millisecond.
291 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
293 m_testDir
->createFile("1");
294 m_dirLister
->openUrl(m_testDir
->url());
295 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
296 QCOMPARE(m_model
->count(), 1);
298 // Insert 10 items for 20 times. After each insert operation the model consistency
300 QSet
<int> insertedItems
;
301 for (int i
= 0; i
< 20; ++i
) {
302 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
304 for (int j
= 0; j
< 10; ++j
) {
305 int itemName
= qrand();
306 while (insertedItems
.contains(itemName
)) {
309 insertedItems
.insert(itemName
);
311 m_testDir
->createFile(QString::number(itemName
));
314 m_dirLister
->updateDirectory(m_testDir
->url());
315 if (spy
.count() == 0) {
316 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
319 QVERIFY(isModelConsistent());
322 QCOMPARE(m_model
->count(), 201);
325 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
328 files
<< "B" << "E" << "G";
329 m_testDir
->createFiles(files
);
331 // Due to inserting the 3 items one item-range with index == 0 and
332 // count == 3 must be given
333 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
334 m_dirLister
->openUrl(m_testDir
->url());
335 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
337 QCOMPARE(spy1
.count(), 1);
338 QList
<QVariant
> arguments
= spy1
.takeFirst();
339 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
340 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
342 // The indexes of the item-ranges must always be related to the model before
343 // the items have been inserted. Having:
346 // and inserting A, C, D, F the resulting model will be:
349 // and the item-ranges must be:
350 // index: 0, count: 1 for A
351 // index: 1, count: 2 for B, C
352 // index: 2, count: 1 for G
355 files
<< "A" << "C" << "D" << "F";
356 m_testDir
->createFiles(files
);
358 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
359 m_dirLister
->updateDirectory(m_testDir
->url());
360 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
362 QCOMPARE(spy2
.count(), 1);
363 arguments
= spy2
.takeFirst();
364 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
365 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
368 void KFileItemModelTest::testExpandItems()
370 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
371 // Besides testing the basic item expansion functionality, the test makes sure that
372 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
373 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
374 // first three characters.
375 QSet
<QByteArray
> modelRoles
= m_model
->roles();
376 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
377 m_model
->setRoles(modelRoles
);
380 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
381 m_testDir
->createFiles(files
);
383 // Store the URLs of all folders in a set.
384 QSet
<KUrl
> allFolders
;
385 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
387 m_dirLister
->openUrl(m_testDir
->url());
388 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
390 // So far, the model contains only "a/"
391 QCOMPARE(m_model
->count(), 1);
392 QVERIFY(m_model
->isExpandable(0));
393 QVERIFY(!m_model
->isExpanded(0));
394 QVERIFY(m_model
->expandedUrls().empty());
396 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
398 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
399 m_model
->setExpanded(0, true);
400 QVERIFY(m_model
->isExpanded(0));
401 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
402 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
403 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
405 QCOMPARE(spyInserted
.count(), 1);
406 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
407 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
409 QVERIFY(m_model
->isExpandable(1));
410 QVERIFY(!m_model
->isExpanded(1));
411 QVERIFY(m_model
->isExpandable(2));
412 QVERIFY(!m_model
->isExpanded(2));
414 // Expand the folder "a/a/" -> "a/a/1" becomes visible
415 m_model
->setExpanded(1, true);
416 QVERIFY(m_model
->isExpanded(1));
417 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
418 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
419 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
421 QCOMPARE(spyInserted
.count(), 1);
422 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
423 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
425 QVERIFY(!m_model
->isExpandable(2));
426 QVERIFY(!m_model
->isExpanded(2));
428 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
429 m_model
->setExpanded(3, true);
430 QVERIFY(m_model
->isExpanded(3));
431 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
432 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
433 QCOMPARE(m_model
->expandedUrls(), allFolders
);
435 QCOMPARE(spyInserted
.count(), 1);
436 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
437 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
439 QVERIFY(!m_model
->isExpandable(4));
440 QVERIFY(!m_model
->isExpanded(4));
442 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
444 // Collapse the top-level folder -> all other items should disappear
445 m_model
->setExpanded(0, false);
446 QVERIFY(!m_model
->isExpanded(0));
447 QCOMPARE(m_model
->count(), 1);
448 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
450 QCOMPARE(spyRemoved
.count(), 1);
451 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
452 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
454 // Clear the model, reload the folder and try to restore the expanded folders.
456 QCOMPARE(m_model
->count(), 0);
457 QVERIFY(m_model
->expandedUrls().empty());
459 m_dirLister
->openUrl(m_testDir
->url());
460 m_model
->restoreExpandedUrls(allFolders
);
461 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
462 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
463 QVERIFY(m_model
->isExpanded(0));
464 QVERIFY(m_model
->isExpanded(1));
465 QVERIFY(!m_model
->isExpanded(2));
466 QVERIFY(m_model
->isExpanded(3));
467 QVERIFY(!m_model
->isExpanded(4));
468 QCOMPARE(m_model
->expandedUrls(), allFolders
);
470 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
471 // This is how DolphinView restores the expanded folders when navigating in history.
472 m_dirLister
->openUrl(KUrl(m_testDir
->name() + "a/a/"));
473 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
474 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
475 m_model
->restoreExpandedUrls(allFolders
);
476 m_dirLister
->openUrl(m_testDir
->url());
477 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
478 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
479 QCOMPARE(m_model
->expandedUrls(), allFolders
);
482 void KFileItemModelTest::testExpandParentItems()
484 // Create a tree structure of folders:
492 QSet
<QByteArray
> modelRoles
= m_model
->roles();
493 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
494 m_model
->setRoles(modelRoles
);
497 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
498 m_testDir
->createFiles(files
);
500 m_dirLister
->openUrl(m_testDir
->url());
501 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
503 // So far, the model contains only "a 1/" and "a2/".
504 QCOMPARE(m_model
->count(), 2);
505 QVERIFY(m_model
->expandedUrls().empty());
507 // Expand the parents of "a2/b2/c2".
508 m_model
->expandParentItems(KUrl(m_testDir
->name() + "a2/b2/c2"));
509 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
511 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
512 // It's important that only the parents of "a1/b1/c1" are expanded.
513 QCOMPARE(m_model
->count(), 4);
514 QVERIFY(!m_model
->isExpanded(0));
515 QVERIFY(m_model
->isExpanded(1));
516 QVERIFY(m_model
->isExpanded(2));
517 QVERIFY(!m_model
->isExpanded(3));
519 // Expand the parents of "a 1/b1".
520 m_model
->expandParentItems(KUrl(m_testDir
->name() + "a 1/b1"));
521 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
523 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
524 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
525 QCOMPARE(m_model
->count(), 5);
526 QVERIFY(m_model
->isExpanded(0));
527 QVERIFY(!m_model
->isExpanded(1));
528 QVERIFY(m_model
->isExpanded(2));
529 QVERIFY(m_model
->isExpanded(3));
530 QVERIFY(!m_model
->isExpanded(4));
533 void KFileItemModelTest::testSorting()
535 // Create some files with different sizes and modification times to check the different sorting options
536 QDateTime now
= QDateTime::currentDateTime();
538 m_testDir
->createFile("a", "A file", now
.addDays(-3));
539 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
540 m_testDir
->createDir("c", now
.addDays(-2));
541 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
542 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
543 m_testDir
->createFile(".f");
545 m_dirLister
->openUrl(m_testDir
->url());
546 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
548 // Default: Sort by Name, ascending
549 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
550 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
551 QVERIFY(m_model
->sortFoldersFirst());
552 //QVERIFY(!m_model->showHiddenFiles());
553 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
555 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
557 // Sort by Name, descending
558 m_model
->setSortOrder(Qt::DescendingOrder
);
559 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
560 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
561 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
562 QCOMPARE(spyItemsMoved
.count(), 1);
563 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
565 // Sort by Date, descending
566 m_model
->setSortRole("date");
567 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
568 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
569 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
570 QCOMPARE(spyItemsMoved
.count(), 1);
571 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
573 // Sort by Date, ascending
574 m_model
->setSortOrder(Qt::AscendingOrder
);
575 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
576 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
577 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
578 QCOMPARE(spyItemsMoved
.count(), 1);
579 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
581 // Sort by Date, ascending, 'Sort Folders First' disabled
582 m_model
->setSortFoldersFirst(false);
583 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
584 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
585 QVERIFY(!m_model
->sortFoldersFirst());
586 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
587 QCOMPARE(spyItemsMoved
.count(), 1);
588 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
590 // Sort by Name, ascending, 'Sort Folders First' disabled
591 m_model
->setSortRole("name");
592 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
593 QVERIFY(!m_model
->sortFoldersFirst());
594 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
595 QCOMPARE(spyItemsMoved
.count(), 1);
596 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
598 // Sort by Size, ascending, 'Sort Folders First' disabled
599 m_model
->setSortRole("size");
600 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
601 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
602 QVERIFY(!m_model
->sortFoldersFirst());
603 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
604 QCOMPARE(spyItemsMoved
.count(), 1);
605 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
607 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
608 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
609 "another signal", SkipSingle
);
610 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
612 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
613 m_model
->setSortFoldersFirst(true);
614 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
615 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
616 QVERIFY(m_model
->sortFoldersFirst());
617 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
618 QCOMPARE(spyItemsMoved
.count(), 0);
620 // Sort by Size, descending, 'Sort Folders First' enabled
621 m_model
->setSortOrder(Qt::DescendingOrder
);
622 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
623 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
624 QVERIFY(m_model
->sortFoldersFirst());
625 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
626 QCOMPARE(spyItemsMoved
.count(), 1);
627 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
629 // TODO: Sort by other roles; show/hide hidden files
632 void KFileItemModelTest::testExpansionLevelsCompare_data()
634 QTest::addColumn
<QString
>("urlA");
635 QTest::addColumn
<QString
>("urlB");
636 QTest::addColumn
<int>("result");
638 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
639 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
640 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
641 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
642 QTest::newRow("Same level: /a-1/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
643 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
644 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
647 void KFileItemModelTest::testExpansionLevelsCompare()
649 QSKIP("Temporary deactivated as KFileItemModel::ItemData has been extended "
650 "by a 'parent' member that is required for a correct comparison. For a "
651 "successful test the item-data of all parents must be available.", SkipAll
);
653 QFETCH(QString
, urlA
);
654 QFETCH(QString
, urlB
);
657 const KFileItem
itemA(KUrl(urlA
), QString(), mode_t(-1));
658 const KFileItem
itemB(KUrl(urlB
), QString(), mode_t(-1));
660 KFileItemModel::ItemData a
;
664 KFileItemModel::ItemData b
;
668 QCOMPARE(m_model
->expandedParentsCountCompare(&a
, &b
), result
);
671 void KFileItemModelTest::testIndexForKeyboardSearch()
674 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
675 m_testDir
->createFiles(files
);
677 m_dirLister
->openUrl(m_testDir
->url());
678 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
680 // Search from index 0
681 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
682 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
683 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
684 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
685 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
686 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
687 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
688 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
689 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
690 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
691 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
693 // Start a search somewhere in the middle
694 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
695 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
696 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
697 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
699 // Test searches that go past the last item back to index 0
700 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
701 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
702 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
703 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
705 // Test searches that yield no result
706 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
707 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
708 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
709 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
710 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
712 // Test upper case searches (note that search is case insensitive)
713 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
714 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
715 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
716 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
718 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
721 void KFileItemModelTest::testNameFilter()
724 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
725 m_testDir
->createFiles(files
);
727 m_dirLister
->openUrl(m_testDir
->url());
728 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
730 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
731 QCOMPARE(m_model
->count(), 3);
733 m_model
->setNameFilter("A2"); // Shows only A2
734 QCOMPARE(m_model
->count(), 1);
736 m_model
->setNameFilter("A2"); // Shows only A1
737 QCOMPARE(m_model
->count(), 1);
739 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
740 QCOMPARE(m_model
->count(), 2);
742 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
743 QCOMPARE(m_model
->count(), 2);
745 m_model
->setNameFilter(QString()); // Shows again all items
746 QCOMPARE(m_model
->count(), 5);
749 bool KFileItemModelTest::isModelConsistent() const
751 if (m_model
->m_items
.count() != m_model
->m_itemData
.count()) {
755 for (int i
= 0; i
< m_model
->count(); ++i
) {
756 const KFileItem item
= m_model
->fileItem(i
);
758 qWarning() << "Item" << i
<< "is null";
762 const int itemIndex
= m_model
->index(item
);
763 if (itemIndex
!= i
) {
764 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
772 QStringList
KFileItemModelTest::itemsInModel() const
776 for (int i
= 0; i
< m_model
->count(); i
++) {
777 items
<< m_model
->data(i
).value("name").toString();
783 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
785 #include "kfileitemmodeltest.moc"