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();
73 void testExpansionLevelsCompare_data();
74 void testExpansionLevelsCompare();
76 void testIndexForKeyboardSearch();
78 void testNameFilter();
81 bool isModelConsistent() const;
82 QStringList
itemsInModel() const;
85 KFileItemModel
* m_model
;
86 KDirLister
* m_dirLister
;
90 void KFileItemModelTest::init()
92 // The item-model tests result in a huge number of debugging
93 // output from kdelibs. Only show critical and fatal messages.
94 qInstallMsgHandler(myMessageOutput
);
96 qRegisterMetaType
<KItemRange
>("KItemRange");
97 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
98 qRegisterMetaType
<KFileItemList
>("KFileItemList");
100 m_testDir
= new TestDir();
101 m_dirLister
= new KDirLister();
102 m_model
= new KFileItemModel(m_dirLister
);
105 void KFileItemModelTest::cleanup()
117 void KFileItemModelTest::testDefaultRoles()
119 const QSet
<QByteArray
> roles
= m_model
->roles();
120 QCOMPARE(roles
.count(), 2);
121 QVERIFY(roles
.contains("name"));
122 QVERIFY(roles
.contains("isDir"));
125 void KFileItemModelTest::testDefaultSortRole()
127 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
130 files
<< "c.txt" << "a.txt" << "b.txt";
132 m_testDir
->createFiles(files
);
134 m_dirLister
->openUrl(m_testDir
->url());
135 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
137 QCOMPARE(m_model
->count(), 3);
138 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
139 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
140 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
143 void KFileItemModelTest::testDefaultGroupedSorting()
145 QCOMPARE(m_model
->groupedSorting(), false);
148 void KFileItemModelTest::testNewItems()
151 files
<< "a.txt" << "b.txt" << "c.txt";
152 m_testDir
->createFiles(files
);
154 m_dirLister
->openUrl(m_testDir
->url());
155 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
157 QCOMPARE(m_model
->count(), 3);
159 QVERIFY(isModelConsistent());
162 void KFileItemModelTest::testRemoveItems()
164 m_testDir
->createFile("a.txt");
165 m_dirLister
->openUrl(m_testDir
->url());
166 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
167 QCOMPARE(m_model
->count(), 1);
169 m_testDir
->removeFile("a.txt");
170 m_dirLister
->updateDirectory(m_testDir
->url());
171 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
172 QCOMPARE(m_model
->count(), 0);
175 void KFileItemModelTest::testSetData()
177 m_testDir
->createFile("a.txt");
179 m_dirLister
->openUrl(m_testDir
->url());
180 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
182 QHash
<QByteArray
, QVariant
> values
;
183 values
.insert("customRole1", "Test1");
184 values
.insert("customRole2", "Test2");
186 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
187 m_model
->setData(0, values
);
188 QCOMPARE(itemsChangedSpy
.count(), 1);
190 values
= m_model
->data(0);
191 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
192 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
193 QVERIFY(isModelConsistent());
196 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
198 QTest::addColumn
<int>("changedIndex");
199 QTest::addColumn
<int>("changedRating");
200 QTest::addColumn
<bool>("expectMoveSignal");
201 QTest::addColumn
<int>("ratingIndex0");
202 QTest::addColumn
<int>("ratingIndex1");
203 QTest::addColumn
<int>("ratingIndex2");
206 // Index 0 = rating 2
207 // Index 1 = rating 4
208 // Index 2 = rating 6
210 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
211 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
212 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
214 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
215 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
216 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
219 void KFileItemModelTest::testSetDataWithModifiedSortRole()
221 QFETCH(int, changedIndex
);
222 QFETCH(int, changedRating
);
223 QFETCH(bool, expectMoveSignal
);
224 QFETCH(int, ratingIndex0
);
225 QFETCH(int, ratingIndex1
);
226 QFETCH(int, ratingIndex2
);
228 // Changing the value of a sort-role must result in
229 // a reordering of the items.
230 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
233 files
<< "a.txt" << "b.txt" << "c.txt";
234 m_testDir
->createFiles(files
);
236 m_dirLister
->openUrl(m_testDir
->url());
237 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
239 // Fill the "rating" role of each file:
244 QHash
<QByteArray
, QVariant
> ratingA
;
245 ratingA
.insert("rating", 2);
246 m_model
->setData(0, ratingA
);
248 QHash
<QByteArray
, QVariant
> ratingB
;
249 ratingB
.insert("rating", 4);
250 m_model
->setData(1, ratingB
);
252 QHash
<QByteArray
, QVariant
> ratingC
;
253 ratingC
.insert("rating", 6);
254 m_model
->setData(2, ratingC
);
256 m_model
->setSortRole("rating");
257 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
258 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
259 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
261 // Now change the rating from a.txt. This usually results
262 // in reordering of the items.
263 QHash
<QByteArray
, QVariant
> rating
;
264 rating
.insert("rating", changedRating
);
265 m_model
->setData(changedIndex
, rating
);
267 if (expectMoveSignal
) {
268 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
271 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
272 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
273 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
274 QVERIFY(isModelConsistent());
277 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
279 //QSKIP("Temporary disabled", SkipSingle);
281 // KFileItemModel prevents that inserting a punch of items sequentially
282 // results in an itemsInserted()-signal for each item. Instead internally
283 // a timeout is given that collects such operations and results in only
284 // one itemsInserted()-signal. However in this test we want to stress
285 // KFileItemModel to do a lot of insert operation and hence decrease
286 // the timeout to 1 millisecond.
287 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
289 m_testDir
->createFile("1");
290 m_dirLister
->openUrl(m_testDir
->url());
291 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
292 QCOMPARE(m_model
->count(), 1);
294 // Insert 10 items for 20 times. After each insert operation the model consistency
296 QSet
<int> insertedItems
;
297 for (int i
= 0; i
< 20; ++i
) {
298 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
300 for (int j
= 0; j
< 10; ++j
) {
301 int itemName
= qrand();
302 while (insertedItems
.contains(itemName
)) {
305 insertedItems
.insert(itemName
);
307 m_testDir
->createFile(QString::number(itemName
));
310 m_dirLister
->updateDirectory(m_testDir
->url());
311 if (spy
.count() == 0) {
312 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
315 QVERIFY(isModelConsistent());
318 QCOMPARE(m_model
->count(), 201);
321 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
324 files
<< "B" << "E" << "G";
325 m_testDir
->createFiles(files
);
327 // Due to inserting the 3 items one item-range with index == 0 and
328 // count == 3 must be given
329 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
330 m_dirLister
->openUrl(m_testDir
->url());
331 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
333 QCOMPARE(spy1
.count(), 1);
334 QList
<QVariant
> arguments
= spy1
.takeFirst();
335 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
336 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
338 // The indexes of the item-ranges must always be related to the model before
339 // the items have been inserted. Having:
342 // and inserting A, C, D, F the resulting model will be:
345 // and the item-ranges must be:
346 // index: 0, count: 1 for A
347 // index: 1, count: 2 for B, C
348 // index: 2, count: 1 for G
351 files
<< "A" << "C" << "D" << "F";
352 m_testDir
->createFiles(files
);
354 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
355 m_dirLister
->updateDirectory(m_testDir
->url());
356 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
358 QCOMPARE(spy2
.count(), 1);
359 arguments
= spy2
.takeFirst();
360 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
361 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
364 void KFileItemModelTest::testExpandItems()
366 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
367 // Besides testing the basic item expansion functionality, the test makes sure that
368 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
369 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
370 // first three characters.
371 QSet
<QByteArray
> modelRoles
= m_model
->roles();
372 modelRoles
<< "isExpanded" << "expansionLevel";
373 m_model
->setRoles(modelRoles
);
376 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
377 m_testDir
->createFiles(files
);
379 // Store the URLs of all folders in a set.
380 QSet
<KUrl
> allFolders
;
381 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
383 m_dirLister
->openUrl(m_testDir
->url());
384 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
386 // So far, the model contains only "a/"
387 QCOMPARE(m_model
->count(), 1);
388 QVERIFY(m_model
->isExpandable(0));
389 QVERIFY(!m_model
->isExpanded(0));
390 QVERIFY(m_model
->expandedUrls().empty());
392 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
394 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
395 m_model
->setExpanded(0, true);
396 QVERIFY(m_model
->isExpanded(0));
397 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
398 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
399 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
401 QCOMPARE(spyInserted
.count(), 1);
402 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
403 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
405 QVERIFY(m_model
->isExpandable(1));
406 QVERIFY(!m_model
->isExpanded(1));
407 QVERIFY(m_model
->isExpandable(2));
408 QVERIFY(!m_model
->isExpanded(2));
410 // Expand the folder "a/a/" -> "a/a/1" becomes visible
411 m_model
->setExpanded(1, true);
412 QVERIFY(m_model
->isExpanded(1));
413 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
414 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
415 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
417 QCOMPARE(spyInserted
.count(), 1);
418 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
419 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
421 QVERIFY(!m_model
->isExpandable(2));
422 QVERIFY(!m_model
->isExpanded(2));
424 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
425 m_model
->setExpanded(3, true);
426 QVERIFY(m_model
->isExpanded(3));
427 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
428 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
429 QCOMPARE(m_model
->expandedUrls(), allFolders
);
431 QCOMPARE(spyInserted
.count(), 1);
432 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
433 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
435 QVERIFY(!m_model
->isExpandable(4));
436 QVERIFY(!m_model
->isExpanded(4));
438 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
440 // Collapse the top-level folder -> all other items should disappear
441 m_model
->setExpanded(0, false);
442 QVERIFY(!m_model
->isExpanded(0));
443 QCOMPARE(m_model
->count(), 1);
444 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
446 QCOMPARE(spyRemoved
.count(), 1);
447 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
448 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
450 // Clear the model, reload the folder and try to restore the expanded folders.
452 QCOMPARE(m_model
->count(), 0);
453 QVERIFY(m_model
->expandedUrls().empty());
455 m_dirLister
->openUrl(m_testDir
->url());
456 m_model
->restoreExpandedUrls(allFolders
);
457 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
458 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
459 QVERIFY(m_model
->isExpanded(0));
460 QVERIFY(m_model
->isExpanded(1));
461 QVERIFY(!m_model
->isExpanded(2));
462 QVERIFY(m_model
->isExpanded(3));
463 QVERIFY(!m_model
->isExpanded(4));
464 QCOMPARE(m_model
->expandedUrls(), allFolders
);
466 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
467 // This is how DolphinView restores the expanded folders when navigating in history.
468 m_dirLister
->openUrl(KUrl(m_testDir
->name() + "a/a/"));
469 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
470 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
471 m_model
->restoreExpandedUrls(allFolders
);
472 m_dirLister
->openUrl(m_testDir
->url());
473 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
474 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
475 QCOMPARE(m_model
->expandedUrls(), allFolders
);
478 void KFileItemModelTest::testSorting()
480 // Create some files with different sizes and modification times to check the different sorting options
481 QDateTime now
= QDateTime::currentDateTime();
483 m_testDir
->createFile("a", "A file", now
.addDays(-3));
484 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
485 m_testDir
->createDir("c", now
.addDays(-2));
486 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
487 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
488 m_testDir
->createFile(".f");
490 m_dirLister
->openUrl(m_testDir
->url());
491 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
493 // Default: Sort by Name, ascending
494 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
495 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
496 QVERIFY(m_model
->sortFoldersFirst());
497 //QVERIFY(!m_model->showHiddenFiles());
498 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
500 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
502 // Sort by Name, descending
503 m_model
->setSortOrder(Qt::DescendingOrder
);
504 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
505 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
506 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
507 QCOMPARE(spyItemsMoved
.count(), 1);
508 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
510 // Sort by Date, descending
511 m_model
->setSortRole("date");
512 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
513 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
514 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
515 QCOMPARE(spyItemsMoved
.count(), 1);
516 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
518 // Sort by Date, ascending
519 m_model
->setSortOrder(Qt::AscendingOrder
);
520 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
521 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
522 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
523 QCOMPARE(spyItemsMoved
.count(), 1);
524 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
526 // Sort by Date, ascending, 'Sort Folders First' disabled
527 m_model
->setSortFoldersFirst(false);
528 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
529 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
530 QVERIFY(!m_model
->sortFoldersFirst());
531 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
532 QCOMPARE(spyItemsMoved
.count(), 1);
533 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
535 // Sort by Name, ascending, 'Sort Folders First' disabled
536 m_model
->setSortRole("name");
537 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
538 QVERIFY(!m_model
->sortFoldersFirst());
539 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
540 QCOMPARE(spyItemsMoved
.count(), 1);
541 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
543 // Sort by Size, ascending, 'Sort Folders First' disabled
544 m_model
->setSortRole("size");
545 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
546 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
547 QVERIFY(!m_model
->sortFoldersFirst());
548 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
549 QCOMPARE(spyItemsMoved
.count(), 1);
550 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
552 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
553 m_model
->setSortFoldersFirst(true);
554 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
555 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
556 QVERIFY(m_model
->sortFoldersFirst());
557 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
558 QCOMPARE(spyItemsMoved
.count(), 0);
560 // Sort by Size, descending, 'Sort Folders First' enabled
561 m_model
->setSortOrder(Qt::DescendingOrder
);
562 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
563 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
564 QVERIFY(m_model
->sortFoldersFirst());
565 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
566 QCOMPARE(spyItemsMoved
.count(), 1);
567 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
569 // TODO: Sort by other roles; show/hide hidden files
572 void KFileItemModelTest::testExpansionLevelsCompare_data()
574 QTest::addColumn
<QString
>("urlA");
575 QTest::addColumn
<QString
>("urlB");
576 QTest::addColumn
<int>("result");
578 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
579 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
580 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
581 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
582 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
583 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
584 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
587 void KFileItemModelTest::testExpansionLevelsCompare()
589 QFETCH(QString
, urlA
);
590 QFETCH(QString
, urlB
);
593 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
594 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
595 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
598 void KFileItemModelTest::testIndexForKeyboardSearch()
601 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
602 m_testDir
->createFiles(files
);
604 m_dirLister
->openUrl(m_testDir
->url());
605 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
607 // Search from index 0
608 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
609 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
610 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
611 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
612 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
613 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
614 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
615 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
616 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
617 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
618 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
620 // Start a search somewhere in the middle
621 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
622 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
623 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
624 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
626 // Test searches that go past the last item back to index 0
627 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
628 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
629 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
630 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
632 // Test searches that yield no result
633 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
634 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
635 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
636 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
637 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
639 // Test upper case searches (note that search is case insensitive)
640 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
641 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
642 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
643 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
645 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
648 void KFileItemModelTest::testNameFilter()
651 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
652 m_testDir
->createFiles(files
);
654 m_dirLister
->openUrl(m_testDir
->url());
655 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
657 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
658 QCOMPARE(m_model
->count(), 3);
660 m_model
->setNameFilter("A2"); // Shows only A2
661 QCOMPARE(m_model
->count(), 1);
663 m_model
->setNameFilter("A2"); // Shows only A1
664 QCOMPARE(m_model
->count(), 1);
666 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
667 QCOMPARE(m_model
->count(), 2);
669 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
670 QCOMPARE(m_model
->count(), 2);
672 m_model
->setNameFilter(QString()); // Shows again all items
673 QCOMPARE(m_model
->count(), 5);
676 bool KFileItemModelTest::isModelConsistent() const
678 for (int i
= 0; i
< m_model
->count(); ++i
) {
679 const KFileItem item
= m_model
->fileItem(i
);
681 qWarning() << "Item" << i
<< "is null";
685 const int itemIndex
= m_model
->index(item
);
686 if (itemIndex
!= i
) {
687 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
695 QStringList
KFileItemModelTest::itemsInModel() const
699 for (int i
= 0; i
< m_model
->count(); i
++) {
700 items
<< m_model
->data(i
).value("name").toString();
706 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
708 #include "kfileitemmodeltest.moc"