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_testDir
->createFile("b.txt");
166 m_dirLister
->openUrl(m_testDir
->url());
167 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
168 QCOMPARE(m_model
->count(), 2);
169 QVERIFY(isModelConsistent());
171 m_testDir
->removeFile("a.txt");
172 m_dirLister
->updateDirectory(m_testDir
->url());
173 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
174 QCOMPARE(m_model
->count(), 1);
175 QVERIFY(isModelConsistent());
178 void KFileItemModelTest::testSetData()
180 m_testDir
->createFile("a.txt");
182 m_dirLister
->openUrl(m_testDir
->url());
183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
185 QHash
<QByteArray
, QVariant
> values
;
186 values
.insert("customRole1", "Test1");
187 values
.insert("customRole2", "Test2");
189 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
190 m_model
->setData(0, values
);
191 QCOMPARE(itemsChangedSpy
.count(), 1);
193 values
= m_model
->data(0);
194 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
195 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
196 QVERIFY(isModelConsistent());
199 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
201 QTest::addColumn
<int>("changedIndex");
202 QTest::addColumn
<int>("changedRating");
203 QTest::addColumn
<bool>("expectMoveSignal");
204 QTest::addColumn
<int>("ratingIndex0");
205 QTest::addColumn
<int>("ratingIndex1");
206 QTest::addColumn
<int>("ratingIndex2");
209 // Index 0 = rating 2
210 // Index 1 = rating 4
211 // Index 2 = rating 6
213 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
214 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
215 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
217 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
218 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
219 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
222 void KFileItemModelTest::testSetDataWithModifiedSortRole()
224 QFETCH(int, changedIndex
);
225 QFETCH(int, changedRating
);
226 QFETCH(bool, expectMoveSignal
);
227 QFETCH(int, ratingIndex0
);
228 QFETCH(int, ratingIndex1
);
229 QFETCH(int, ratingIndex2
);
231 // Changing the value of a sort-role must result in
232 // a reordering of the items.
233 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
236 files
<< "a.txt" << "b.txt" << "c.txt";
237 m_testDir
->createFiles(files
);
239 m_dirLister
->openUrl(m_testDir
->url());
240 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
242 // Fill the "rating" role of each file:
247 QHash
<QByteArray
, QVariant
> ratingA
;
248 ratingA
.insert("rating", 2);
249 m_model
->setData(0, ratingA
);
251 QHash
<QByteArray
, QVariant
> ratingB
;
252 ratingB
.insert("rating", 4);
253 m_model
->setData(1, ratingB
);
255 QHash
<QByteArray
, QVariant
> ratingC
;
256 ratingC
.insert("rating", 6);
257 m_model
->setData(2, ratingC
);
259 m_model
->setSortRole("rating");
260 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
261 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
262 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
264 // Now change the rating from a.txt. This usually results
265 // in reordering of the items.
266 QHash
<QByteArray
, QVariant
> rating
;
267 rating
.insert("rating", changedRating
);
268 m_model
->setData(changedIndex
, rating
);
270 if (expectMoveSignal
) {
271 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
274 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
275 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
276 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
277 QVERIFY(isModelConsistent());
280 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
282 //QSKIP("Temporary disabled", SkipSingle);
284 // KFileItemModel prevents that inserting a punch of items sequentially
285 // results in an itemsInserted()-signal for each item. Instead internally
286 // a timeout is given that collects such operations and results in only
287 // one itemsInserted()-signal. However in this test we want to stress
288 // KFileItemModel to do a lot of insert operation and hence decrease
289 // the timeout to 1 millisecond.
290 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
292 m_testDir
->createFile("1");
293 m_dirLister
->openUrl(m_testDir
->url());
294 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
295 QCOMPARE(m_model
->count(), 1);
297 // Insert 10 items for 20 times. After each insert operation the model consistency
299 QSet
<int> insertedItems
;
300 for (int i
= 0; i
< 20; ++i
) {
301 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
303 for (int j
= 0; j
< 10; ++j
) {
304 int itemName
= qrand();
305 while (insertedItems
.contains(itemName
)) {
308 insertedItems
.insert(itemName
);
310 m_testDir
->createFile(QString::number(itemName
));
313 m_dirLister
->updateDirectory(m_testDir
->url());
314 if (spy
.count() == 0) {
315 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
318 QVERIFY(isModelConsistent());
321 QCOMPARE(m_model
->count(), 201);
324 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
327 files
<< "B" << "E" << "G";
328 m_testDir
->createFiles(files
);
330 // Due to inserting the 3 items one item-range with index == 0 and
331 // count == 3 must be given
332 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
333 m_dirLister
->openUrl(m_testDir
->url());
334 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
336 QCOMPARE(spy1
.count(), 1);
337 QList
<QVariant
> arguments
= spy1
.takeFirst();
338 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
339 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
341 // The indexes of the item-ranges must always be related to the model before
342 // the items have been inserted. Having:
345 // and inserting A, C, D, F the resulting model will be:
348 // and the item-ranges must be:
349 // index: 0, count: 1 for A
350 // index: 1, count: 2 for B, C
351 // index: 2, count: 1 for G
354 files
<< "A" << "C" << "D" << "F";
355 m_testDir
->createFiles(files
);
357 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
358 m_dirLister
->updateDirectory(m_testDir
->url());
359 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
361 QCOMPARE(spy2
.count(), 1);
362 arguments
= spy2
.takeFirst();
363 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
364 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
367 void KFileItemModelTest::testExpandItems()
369 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
370 // Besides testing the basic item expansion functionality, the test makes sure that
371 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
372 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
373 // first three characters.
374 QSet
<QByteArray
> modelRoles
= m_model
->roles();
375 modelRoles
<< "isExpanded" << "expansionLevel";
376 m_model
->setRoles(modelRoles
);
379 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
380 m_testDir
->createFiles(files
);
382 // Store the URLs of all folders in a set.
383 QSet
<KUrl
> allFolders
;
384 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
386 m_dirLister
->openUrl(m_testDir
->url());
387 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
389 // So far, the model contains only "a/"
390 QCOMPARE(m_model
->count(), 1);
391 QVERIFY(m_model
->isExpandable(0));
392 QVERIFY(!m_model
->isExpanded(0));
393 QVERIFY(m_model
->expandedUrls().empty());
395 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
397 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
398 m_model
->setExpanded(0, true);
399 QVERIFY(m_model
->isExpanded(0));
400 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
401 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
402 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
404 QCOMPARE(spyInserted
.count(), 1);
405 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
406 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
408 QVERIFY(m_model
->isExpandable(1));
409 QVERIFY(!m_model
->isExpanded(1));
410 QVERIFY(m_model
->isExpandable(2));
411 QVERIFY(!m_model
->isExpanded(2));
413 // Expand the folder "a/a/" -> "a/a/1" becomes visible
414 m_model
->setExpanded(1, true);
415 QVERIFY(m_model
->isExpanded(1));
416 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
417 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
418 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
420 QCOMPARE(spyInserted
.count(), 1);
421 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
422 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
424 QVERIFY(!m_model
->isExpandable(2));
425 QVERIFY(!m_model
->isExpanded(2));
427 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
428 m_model
->setExpanded(3, true);
429 QVERIFY(m_model
->isExpanded(3));
430 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
431 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
432 QCOMPARE(m_model
->expandedUrls(), allFolders
);
434 QCOMPARE(spyInserted
.count(), 1);
435 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
436 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
438 QVERIFY(!m_model
->isExpandable(4));
439 QVERIFY(!m_model
->isExpanded(4));
441 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
443 // Collapse the top-level folder -> all other items should disappear
444 m_model
->setExpanded(0, false);
445 QVERIFY(!m_model
->isExpanded(0));
446 QCOMPARE(m_model
->count(), 1);
447 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
449 QCOMPARE(spyRemoved
.count(), 1);
450 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
451 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
453 // Clear the model, reload the folder and try to restore the expanded folders.
455 QCOMPARE(m_model
->count(), 0);
456 QVERIFY(m_model
->expandedUrls().empty());
458 m_dirLister
->openUrl(m_testDir
->url());
459 m_model
->restoreExpandedUrls(allFolders
);
460 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
461 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
462 QVERIFY(m_model
->isExpanded(0));
463 QVERIFY(m_model
->isExpanded(1));
464 QVERIFY(!m_model
->isExpanded(2));
465 QVERIFY(m_model
->isExpanded(3));
466 QVERIFY(!m_model
->isExpanded(4));
467 QCOMPARE(m_model
->expandedUrls(), allFolders
);
469 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
470 // This is how DolphinView restores the expanded folders when navigating in history.
471 m_dirLister
->openUrl(KUrl(m_testDir
->name() + "a/a/"));
472 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
473 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
474 m_model
->restoreExpandedUrls(allFolders
);
475 m_dirLister
->openUrl(m_testDir
->url());
476 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
477 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
478 QCOMPARE(m_model
->expandedUrls(), allFolders
);
481 void KFileItemModelTest::testSorting()
483 // Create some files with different sizes and modification times to check the different sorting options
484 QDateTime now
= QDateTime::currentDateTime();
486 m_testDir
->createFile("a", "A file", now
.addDays(-3));
487 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
488 m_testDir
->createDir("c", now
.addDays(-2));
489 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
490 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
491 m_testDir
->createFile(".f");
493 m_dirLister
->openUrl(m_testDir
->url());
494 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
496 // Default: Sort by Name, ascending
497 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
498 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
499 QVERIFY(m_model
->sortFoldersFirst());
500 //QVERIFY(!m_model->showHiddenFiles());
501 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
503 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
505 // Sort by Name, descending
506 m_model
->setSortOrder(Qt::DescendingOrder
);
507 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
508 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
509 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
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, descending
514 m_model
->setSortRole("date");
515 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
516 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
517 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
518 QCOMPARE(spyItemsMoved
.count(), 1);
519 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
521 // Sort by Date, ascending
522 m_model
->setSortOrder(Qt::AscendingOrder
);
523 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
524 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
525 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
526 QCOMPARE(spyItemsMoved
.count(), 1);
527 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
529 // Sort by Date, ascending, 'Sort Folders First' disabled
530 m_model
->setSortFoldersFirst(false);
531 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
532 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
533 QVERIFY(!m_model
->sortFoldersFirst());
534 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
535 QCOMPARE(spyItemsMoved
.count(), 1);
536 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
538 // Sort by Name, ascending, 'Sort Folders First' disabled
539 m_model
->setSortRole("name");
540 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
541 QVERIFY(!m_model
->sortFoldersFirst());
542 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
543 QCOMPARE(spyItemsMoved
.count(), 1);
544 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
546 // Sort by Size, ascending, 'Sort Folders First' disabled
547 m_model
->setSortRole("size");
548 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
549 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
550 QVERIFY(!m_model
->sortFoldersFirst());
551 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
552 QCOMPARE(spyItemsMoved
.count(), 1);
553 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
555 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
556 m_model
->setSortFoldersFirst(true);
557 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
558 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
559 QVERIFY(m_model
->sortFoldersFirst());
560 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
561 QCOMPARE(spyItemsMoved
.count(), 0);
563 // Sort by Size, descending, 'Sort Folders First' enabled
564 m_model
->setSortOrder(Qt::DescendingOrder
);
565 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
566 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
567 QVERIFY(m_model
->sortFoldersFirst());
568 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
569 QCOMPARE(spyItemsMoved
.count(), 1);
570 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
572 // TODO: Sort by other roles; show/hide hidden files
575 void KFileItemModelTest::testExpansionLevelsCompare_data()
577 QTest::addColumn
<QString
>("urlA");
578 QTest::addColumn
<QString
>("urlB");
579 QTest::addColumn
<int>("result");
581 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
582 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
583 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
584 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
585 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
586 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
587 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
590 void KFileItemModelTest::testExpansionLevelsCompare()
592 QFETCH(QString
, urlA
);
593 QFETCH(QString
, urlB
);
596 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
597 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
598 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
601 void KFileItemModelTest::testIndexForKeyboardSearch()
604 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
605 m_testDir
->createFiles(files
);
607 m_dirLister
->openUrl(m_testDir
->url());
608 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
610 // Search from index 0
611 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
612 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
613 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
614 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
615 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
616 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
617 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
618 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
619 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
620 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
621 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
623 // Start a search somewhere in the middle
624 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
625 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
626 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
627 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
629 // Test searches that go past the last item back to index 0
630 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
631 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
632 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
633 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
635 // Test searches that yield no result
636 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
637 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
638 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
639 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
640 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
642 // Test upper case searches (note that search is case insensitive)
643 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
644 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
645 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
646 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
648 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
651 void KFileItemModelTest::testNameFilter()
654 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
655 m_testDir
->createFiles(files
);
657 m_dirLister
->openUrl(m_testDir
->url());
658 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
660 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
661 QCOMPARE(m_model
->count(), 3);
663 m_model
->setNameFilter("A2"); // Shows only A2
664 QCOMPARE(m_model
->count(), 1);
666 m_model
->setNameFilter("A2"); // Shows only A1
667 QCOMPARE(m_model
->count(), 1);
669 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
670 QCOMPARE(m_model
->count(), 2);
672 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
673 QCOMPARE(m_model
->count(), 2);
675 m_model
->setNameFilter(QString()); // Shows again all items
676 QCOMPARE(m_model
->count(), 5);
679 bool KFileItemModelTest::isModelConsistent() const
681 if (m_model
->m_items
.count() != m_model
->m_itemData
.count()) {
685 for (int i
= 0; i
< m_model
->count(); ++i
) {
686 const KFileItem item
= m_model
->fileItem(i
);
688 qWarning() << "Item" << i
<< "is null";
692 const int itemIndex
= m_model
->index(item
);
693 if (itemIndex
!= i
) {
694 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
702 QStringList
KFileItemModelTest::itemsInModel() const
706 for (int i
= 0; i
< m_model
->count(); i
++) {
707 items
<< m_model
->data(i
).value("name").toString();
713 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
715 #include "kfileitemmodeltest.moc"