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();
79 bool isModelConsistent() const;
80 QStringList
itemsInModel() const;
83 KFileItemModel
* m_model
;
84 KDirLister
* m_dirLister
;
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_dirLister
= new KDirLister();
100 m_model
= new KFileItemModel(m_dirLister
);
103 void KFileItemModelTest::cleanup()
115 void KFileItemModelTest::testDefaultRoles()
117 const QSet
<QByteArray
> roles
= m_model
->roles();
118 QCOMPARE(roles
.count(), 2);
119 QVERIFY(roles
.contains("name"));
120 QVERIFY(roles
.contains("isDir"));
123 void KFileItemModelTest::testDefaultSortRole()
125 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
128 files
<< "c.txt" << "a.txt" << "b.txt";
130 m_testDir
->createFiles(files
);
132 m_dirLister
->openUrl(m_testDir
->url());
133 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
135 QCOMPARE(m_model
->count(), 3);
136 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
137 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
138 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
141 void KFileItemModelTest::testDefaultGroupedSorting()
143 QCOMPARE(m_model
->groupedSorting(), false);
146 void KFileItemModelTest::testNewItems()
149 files
<< "a.txt" << "b.txt" << "c.txt";
150 m_testDir
->createFiles(files
);
152 m_dirLister
->openUrl(m_testDir
->url());
153 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
155 QCOMPARE(m_model
->count(), 3);
157 QVERIFY(isModelConsistent());
160 void KFileItemModelTest::testRemoveItems()
162 m_testDir
->createFile("a.txt");
163 m_dirLister
->openUrl(m_testDir
->url());
164 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
165 QCOMPARE(m_model
->count(), 1);
167 m_testDir
->removeFile("a.txt");
168 m_dirLister
->updateDirectory(m_testDir
->url());
169 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
170 QCOMPARE(m_model
->count(), 0);
173 void KFileItemModelTest::testSetData()
175 m_testDir
->createFile("a.txt");
177 m_dirLister
->openUrl(m_testDir
->url());
178 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
180 QHash
<QByteArray
, QVariant
> values
;
181 values
.insert("customRole1", "Test1");
182 values
.insert("customRole2", "Test2");
184 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
185 m_model
->setData(0, values
);
186 QCOMPARE(itemsChangedSpy
.count(), 1);
188 values
= m_model
->data(0);
189 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
190 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
191 QVERIFY(isModelConsistent());
194 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
196 QTest::addColumn
<int>("changedIndex");
197 QTest::addColumn
<int>("changedRating");
198 QTest::addColumn
<bool>("expectMoveSignal");
199 QTest::addColumn
<int>("ratingIndex0");
200 QTest::addColumn
<int>("ratingIndex1");
201 QTest::addColumn
<int>("ratingIndex2");
204 // Index 0 = rating 2
205 // Index 1 = rating 4
206 // Index 2 = rating 6
208 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
209 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
210 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
212 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
213 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
214 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
217 void KFileItemModelTest::testSetDataWithModifiedSortRole()
219 QFETCH(int, changedIndex
);
220 QFETCH(int, changedRating
);
221 QFETCH(bool, expectMoveSignal
);
222 QFETCH(int, ratingIndex0
);
223 QFETCH(int, ratingIndex1
);
224 QFETCH(int, ratingIndex2
);
226 // Changing the value of a sort-role must result in
227 // a reordering of the items.
228 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
231 files
<< "a.txt" << "b.txt" << "c.txt";
232 m_testDir
->createFiles(files
);
234 m_dirLister
->openUrl(m_testDir
->url());
235 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
237 // Fill the "rating" role of each file:
242 QHash
<QByteArray
, QVariant
> ratingA
;
243 ratingA
.insert("rating", 2);
244 m_model
->setData(0, ratingA
);
246 QHash
<QByteArray
, QVariant
> ratingB
;
247 ratingB
.insert("rating", 4);
248 m_model
->setData(1, ratingB
);
250 QHash
<QByteArray
, QVariant
> ratingC
;
251 ratingC
.insert("rating", 6);
252 m_model
->setData(2, ratingC
);
254 m_model
->setSortRole("rating");
255 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
256 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
257 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
259 // Now change the rating from a.txt. This usually results
260 // in reordering of the items.
261 QHash
<QByteArray
, QVariant
> rating
;
262 rating
.insert("rating", changedRating
);
263 m_model
->setData(changedIndex
, rating
);
265 if (expectMoveSignal
) {
266 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
269 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
270 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
271 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
272 QVERIFY(isModelConsistent());
275 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
277 //QSKIP("Temporary disabled", SkipSingle);
279 // KFileItemModel prevents that inserting a punch of items sequentially
280 // results in an itemsInserted()-signal for each item. Instead internally
281 // a timeout is given that collects such operations and results in only
282 // one itemsInserted()-signal. However in this test we want to stress
283 // KFileItemModel to do a lot of insert operation and hence decrease
284 // the timeout to 1 millisecond.
285 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
287 m_testDir
->createFile("1");
288 m_dirLister
->openUrl(m_testDir
->url());
289 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
290 QCOMPARE(m_model
->count(), 1);
292 // Insert 10 items for 20 times. After each insert operation the model consistency
294 QSet
<int> insertedItems
;
295 for (int i
= 0; i
< 20; ++i
) {
296 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
298 for (int j
= 0; j
< 10; ++j
) {
299 int itemName
= qrand();
300 while (insertedItems
.contains(itemName
)) {
303 insertedItems
.insert(itemName
);
305 m_testDir
->createFile(QString::number(itemName
));
308 m_dirLister
->updateDirectory(m_testDir
->url());
309 if (spy
.count() == 0) {
310 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
313 QVERIFY(isModelConsistent());
316 QCOMPARE(m_model
->count(), 201);
319 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
322 files
<< "B" << "E" << "G";
323 m_testDir
->createFiles(files
);
325 // Due to inserting the 3 items one item-range with index == 0 and
326 // count == 3 must be given
327 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
328 m_dirLister
->openUrl(m_testDir
->url());
329 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
331 QCOMPARE(spy1
.count(), 1);
332 QList
<QVariant
> arguments
= spy1
.takeFirst();
333 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
334 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
336 // The indexes of the item-ranges must always be related to the model before
337 // the items have been inserted. Having:
340 // and inserting A, C, D, F the resulting model will be:
343 // and the item-ranges must be:
344 // index: 0, count: 1 for A
345 // index: 1, count: 2 for B, C
346 // index: 2, count: 1 for G
349 files
<< "A" << "C" << "D" << "F";
350 m_testDir
->createFiles(files
);
352 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
353 m_dirLister
->updateDirectory(m_testDir
->url());
354 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
356 QCOMPARE(spy2
.count(), 1);
357 arguments
= spy2
.takeFirst();
358 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
359 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
362 void KFileItemModelTest::testExpandItems()
364 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
365 // Besides testing the basic item expansion functionality, the test makes sure that
366 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
367 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
368 // first three characters.
369 QSet
<QByteArray
> modelRoles
= m_model
->roles();
370 modelRoles
<< "isExpanded" << "expansionLevel";
371 m_model
->setRoles(modelRoles
);
374 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
375 m_testDir
->createFiles(files
);
377 // Store the URLs of all folders in a set.
378 QSet
<KUrl
> allFolders
;
379 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
381 m_dirLister
->openUrl(m_testDir
->url());
382 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
384 // So far, the model contains only "a/"
385 QCOMPARE(m_model
->count(), 1);
386 QVERIFY(m_model
->isExpandable(0));
387 QVERIFY(!m_model
->isExpanded(0));
388 QVERIFY(m_model
->expandedUrls().empty());
390 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
392 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
393 m_model
->setExpanded(0, true);
394 QVERIFY(m_model
->isExpanded(0));
395 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
396 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
397 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
399 QCOMPARE(spyInserted
.count(), 1);
400 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
401 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
403 QVERIFY(m_model
->isExpandable(1));
404 QVERIFY(!m_model
->isExpanded(1));
405 QVERIFY(m_model
->isExpandable(2));
406 QVERIFY(!m_model
->isExpanded(2));
408 // Expand the folder "a/a/" -> "a/a/1" becomes visible
409 m_model
->setExpanded(1, true);
410 QVERIFY(m_model
->isExpanded(1));
411 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
412 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
413 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
415 QCOMPARE(spyInserted
.count(), 1);
416 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
417 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
419 QVERIFY(!m_model
->isExpandable(2));
420 QVERIFY(!m_model
->isExpanded(2));
422 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
423 m_model
->setExpanded(3, true);
424 QVERIFY(m_model
->isExpanded(3));
425 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
426 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
427 QCOMPARE(m_model
->expandedUrls(), allFolders
);
429 QCOMPARE(spyInserted
.count(), 1);
430 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
431 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
433 QVERIFY(!m_model
->isExpandable(4));
434 QVERIFY(!m_model
->isExpanded(4));
436 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
438 // Collapse the top-level folder -> all other items should disappear
439 m_model
->setExpanded(0, false);
440 QVERIFY(!m_model
->isExpanded(0));
441 QCOMPARE(m_model
->count(), 1);
442 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
444 QCOMPARE(spyRemoved
.count(), 1);
445 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
446 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
448 // Clear the model, reload the folder and try to restore the expanded folders.
450 QCOMPARE(m_model
->count(), 0);
451 QVERIFY(m_model
->expandedUrls().empty());
453 m_dirLister
->openUrl(m_testDir
->url());
454 m_model
->restoreExpandedUrls(allFolders
);
455 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
456 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
457 QVERIFY(m_model
->isExpanded(0));
458 QVERIFY(m_model
->isExpanded(1));
459 QVERIFY(!m_model
->isExpanded(2));
460 QVERIFY(m_model
->isExpanded(3));
461 QVERIFY(!m_model
->isExpanded(4));
462 QCOMPARE(m_model
->expandedUrls(), allFolders
);
465 void KFileItemModelTest::testSorting()
467 // Create some files with different sizes and modification times to check the different sorting options
468 QDateTime now
= QDateTime::currentDateTime();
470 m_testDir
->createFile("a", "A file", now
.addDays(-3));
471 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
472 m_testDir
->createDir("c", now
.addDays(-2));
473 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
474 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
475 m_testDir
->createFile(".f");
477 m_dirLister
->openUrl(m_testDir
->url());
478 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
480 // Default: Sort by Name, ascending
481 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
482 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
483 QVERIFY(m_model
->sortFoldersFirst());
484 //QVERIFY(!m_model->showHiddenFiles());
485 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
487 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
489 // Sort by Name, descending
490 m_model
->setSortOrder(Qt::DescendingOrder
);
491 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
492 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
493 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
494 QCOMPARE(spyItemsMoved
.count(), 1);
495 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
497 // Sort by Date, descending
498 m_model
->setSortRole("date");
499 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
500 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
501 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
502 QCOMPARE(spyItemsMoved
.count(), 1);
503 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
505 // Sort by Date, ascending
506 m_model
->setSortOrder(Qt::AscendingOrder
);
507 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
508 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
509 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
510 QCOMPARE(spyItemsMoved
.count(), 1);
511 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
513 // Sort by Date, ascending, 'Sort Folders First' disabled
514 m_model
->setSortFoldersFirst(false);
515 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
516 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
517 QVERIFY(!m_model
->sortFoldersFirst());
518 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
519 QCOMPARE(spyItemsMoved
.count(), 1);
520 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
522 // Sort by Name, ascending, 'Sort Folders First' disabled
523 m_model
->setSortRole("name");
524 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
525 QVERIFY(!m_model
->sortFoldersFirst());
526 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
527 QCOMPARE(spyItemsMoved
.count(), 1);
528 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
530 // Sort by Size, ascending, 'Sort Folders First' disabled
531 m_model
->setSortRole("size");
532 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
533 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
534 QVERIFY(!m_model
->sortFoldersFirst());
535 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
536 QCOMPARE(spyItemsMoved
.count(), 1);
537 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
539 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
540 m_model
->setSortFoldersFirst(true);
541 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
542 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
543 QVERIFY(m_model
->sortFoldersFirst());
544 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
545 QCOMPARE(spyItemsMoved
.count(), 0);
547 // Sort by Size, descending, 'Sort Folders First' enabled
548 m_model
->setSortOrder(Qt::DescendingOrder
);
549 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
550 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
551 QVERIFY(m_model
->sortFoldersFirst());
552 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
553 QCOMPARE(spyItemsMoved
.count(), 1);
554 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
556 // TODO: Sort by other roles; show/hide hidden files
559 void KFileItemModelTest::testExpansionLevelsCompare_data()
561 QTest::addColumn
<QString
>("urlA");
562 QTest::addColumn
<QString
>("urlB");
563 QTest::addColumn
<int>("result");
565 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
566 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
567 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
568 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
569 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
570 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
571 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
574 void KFileItemModelTest::testExpansionLevelsCompare()
576 QFETCH(QString
, urlA
);
577 QFETCH(QString
, urlB
);
580 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
581 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
582 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
585 void KFileItemModelTest::testIndexForKeyboardSearch()
588 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
589 m_testDir
->createFiles(files
);
591 m_dirLister
->openUrl(m_testDir
->url());
592 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
594 // Search from index 0
595 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
596 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
597 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
598 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
599 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
600 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
601 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
602 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
603 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
604 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
605 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
607 // Start a search somewhere in the middle
608 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
609 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
610 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
611 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
613 // Test searches that go past the last item back to index 0
614 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
615 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
616 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
617 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
619 // Test searches that yield no result
620 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
621 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
622 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
623 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
624 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
626 // Test upper case searches (note that search is case insensitive)
627 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
628 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
629 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
630 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
632 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
635 bool KFileItemModelTest::isModelConsistent() const
637 for (int i
= 0; i
< m_model
->count(); ++i
) {
638 const KFileItem item
= m_model
->fileItem(i
);
640 qWarning() << "Item" << i
<< "is null";
644 const int itemIndex
= m_model
->index(item
);
645 if (itemIndex
!= i
) {
646 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
654 QStringList
KFileItemModelTest::itemsInModel() const
658 for (int i
= 0; i
< m_model
->count(); i
++) {
659 items
<< m_model
->data(i
).value("name").toString();
665 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
667 #include "kfileitemmodeltest.moc"