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
<int>("ratingIndex0");
199 QTest::addColumn
<int>("ratingIndex1");
200 QTest::addColumn
<int>("ratingIndex2");
203 // Index 0 = rating 2
204 // Index 1 = rating 4
205 // Index 2 = rating 6
207 QTest::newRow("Index 0: Rating 3") << 0 << 3 << 3 << 4 << 6;
208 QTest::newRow("Index 0: Rating 5") << 0 << 5 << 4 << 5 << 6;
209 QTest::newRow("Index 0: Rating 8") << 0 << 8 << 4 << 6 << 8;
211 QTest::newRow("Index 2: Rating 1") << 2 << 1 << 1 << 2 << 4;
212 QTest::newRow("Index 2: Rating 3") << 2 << 3 << 2 << 3 << 4;
213 QTest::newRow("Index 2: Rating 5") << 2 << 5 << 2 << 4 << 5;
216 void KFileItemModelTest::testSetDataWithModifiedSortRole()
218 QFETCH(int, changedIndex
);
219 QFETCH(int, changedRating
);
220 QFETCH(int, ratingIndex0
);
221 QFETCH(int, ratingIndex1
);
222 QFETCH(int, ratingIndex2
);
224 // Changing the value of a sort-role must result in
225 // a reordering of the items.
226 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
229 files
<< "a.txt" << "b.txt" << "c.txt";
230 m_testDir
->createFiles(files
);
232 m_dirLister
->openUrl(m_testDir
->url());
233 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
235 // Fill the "rating" role of each file:
240 QHash
<QByteArray
, QVariant
> ratingA
;
241 ratingA
.insert("rating", 2);
242 m_model
->setData(0, ratingA
);
244 QHash
<QByteArray
, QVariant
> ratingB
;
245 ratingB
.insert("rating", 4);
246 m_model
->setData(1, ratingB
);
248 QHash
<QByteArray
, QVariant
> ratingC
;
249 ratingC
.insert("rating", 6);
250 m_model
->setData(2, ratingC
);
252 m_model
->setSortRole("rating");
253 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
254 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
255 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
257 // Now change the rating from a.txt. This usually results
258 // in reordering of the items.
259 QHash
<QByteArray
, QVariant
> rating
;
260 rating
.insert("rating", changedRating
);
261 m_model
->setData(changedIndex
, rating
);
263 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
264 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
265 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
266 QVERIFY(isModelConsistent());
269 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
271 //QSKIP("Temporary disabled", SkipSingle);
273 // KFileItemModel prevents that inserting a punch of items sequentially
274 // results in an itemsInserted()-signal for each item. Instead internally
275 // a timeout is given that collects such operations and results in only
276 // one itemsInserted()-signal. However in this test we want to stress
277 // KFileItemModel to do a lot of insert operation and hence decrease
278 // the timeout to 1 millisecond.
279 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
281 m_testDir
->createFile("1");
282 m_dirLister
->openUrl(m_testDir
->url());
283 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
284 QCOMPARE(m_model
->count(), 1);
286 // Insert 10 items for 20 times. After each insert operation the model consistency
288 QSet
<int> insertedItems
;
289 for (int i
= 0; i
< 20; ++i
) {
290 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
292 for (int j
= 0; j
< 10; ++j
) {
293 int itemName
= qrand();
294 while (insertedItems
.contains(itemName
)) {
297 insertedItems
.insert(itemName
);
299 m_testDir
->createFile(QString::number(itemName
));
302 m_dirLister
->updateDirectory(m_testDir
->url());
303 if (spy
.count() == 0) {
304 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
307 QVERIFY(isModelConsistent());
310 QCOMPARE(m_model
->count(), 201);
313 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
316 files
<< "B" << "E" << "G";
317 m_testDir
->createFiles(files
);
319 // Due to inserting the 3 items one item-range with index == 0 and
320 // count == 3 must be given
321 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
322 m_dirLister
->openUrl(m_testDir
->url());
323 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
325 QCOMPARE(spy1
.count(), 1);
326 QList
<QVariant
> arguments
= spy1
.takeFirst();
327 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
328 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
330 // The indexes of the item-ranges must always be related to the model before
331 // the items have been inserted. Having:
334 // and inserting A, C, D, F the resulting model will be:
337 // and the item-ranges must be:
338 // index: 0, count: 1 for A
339 // index: 1, count: 2 for B, C
340 // index: 2, count: 1 for G
343 files
<< "A" << "C" << "D" << "F";
344 m_testDir
->createFiles(files
);
346 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
347 m_dirLister
->updateDirectory(m_testDir
->url());
348 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
350 QCOMPARE(spy2
.count(), 1);
351 arguments
= spy2
.takeFirst();
352 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
353 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
356 void KFileItemModelTest::testExpandItems()
358 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
359 // Besides testing the basic item expansion functionality, the test makes sure that
360 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
361 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
362 // first three characters.
363 QSet
<QByteArray
> modelRoles
= m_model
->roles();
364 modelRoles
<< "isExpanded" << "expansionLevel";
365 m_model
->setRoles(modelRoles
);
368 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
369 m_testDir
->createFiles(files
);
371 // Store the URLs of all folders in a set.
372 QSet
<KUrl
> allFolders
;
373 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
375 m_dirLister
->openUrl(m_testDir
->url());
376 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
378 // So far, the model contains only "a/"
379 QCOMPARE(m_model
->count(), 1);
380 QVERIFY(m_model
->isExpandable(0));
381 QVERIFY(!m_model
->isExpanded(0));
382 QVERIFY(m_model
->expandedUrls().empty());
384 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
386 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
387 m_model
->setExpanded(0, true);
388 QVERIFY(m_model
->isExpanded(0));
389 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
390 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
391 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
393 QCOMPARE(spyInserted
.count(), 1);
394 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
395 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
397 QVERIFY(m_model
->isExpandable(1));
398 QVERIFY(!m_model
->isExpanded(1));
399 QVERIFY(m_model
->isExpandable(2));
400 QVERIFY(!m_model
->isExpanded(2));
402 // Expand the folder "a/a/" -> "a/a/1" becomes visible
403 m_model
->setExpanded(1, true);
404 QVERIFY(m_model
->isExpanded(1));
405 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
406 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
407 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
409 QCOMPARE(spyInserted
.count(), 1);
410 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
411 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
413 QVERIFY(!m_model
->isExpandable(2));
414 QVERIFY(!m_model
->isExpanded(2));
416 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
417 m_model
->setExpanded(3, true);
418 QVERIFY(m_model
->isExpanded(3));
419 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
420 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
421 QCOMPARE(m_model
->expandedUrls(), allFolders
);
423 QCOMPARE(spyInserted
.count(), 1);
424 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
425 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
427 QVERIFY(!m_model
->isExpandable(4));
428 QVERIFY(!m_model
->isExpanded(4));
430 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
432 // Collapse the top-level folder -> all other items should disappear
433 m_model
->setExpanded(0, false);
434 QVERIFY(!m_model
->isExpanded(0));
435 QCOMPARE(m_model
->count(), 1);
436 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
438 QCOMPARE(spyRemoved
.count(), 1);
439 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
440 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
442 // Clear the model, reload the folder and try to restore the expanded folders.
444 QCOMPARE(m_model
->count(), 0);
445 QVERIFY(m_model
->expandedUrls().empty());
447 m_dirLister
->openUrl(m_testDir
->url());
448 m_model
->restoreExpandedUrls(allFolders
);
449 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
450 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
451 QVERIFY(m_model
->isExpanded(0));
452 QVERIFY(m_model
->isExpanded(1));
453 QVERIFY(!m_model
->isExpanded(2));
454 QVERIFY(m_model
->isExpanded(3));
455 QVERIFY(!m_model
->isExpanded(4));
456 QCOMPARE(m_model
->expandedUrls(), allFolders
);
459 void KFileItemModelTest::testSorting()
461 // Create some files with different sizes and modification times to check the different sorting options
462 QDateTime now
= QDateTime::currentDateTime();
464 m_testDir
->createFile("a", "A file", now
.addDays(-3));
465 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
466 m_testDir
->createDir("c", now
.addDays(-2));
467 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
468 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
469 m_testDir
->createFile(".f");
471 m_dirLister
->openUrl(m_testDir
->url());
472 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
474 // Default: Sort by Name, ascending
475 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
476 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
477 QVERIFY(m_model
->sortFoldersFirst());
478 //QVERIFY(!m_model->showHiddenFiles());
479 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
481 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
483 // Sort by Name, descending
484 m_model
->setSortOrder(Qt::DescendingOrder
);
485 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
486 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
487 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
488 QCOMPARE(spyItemsMoved
.count(), 1);
489 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
491 // Sort by Date, descending
492 m_model
->setSortRole("date");
493 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
494 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
495 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
496 QCOMPARE(spyItemsMoved
.count(), 1);
497 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
499 // Sort by Date, ascending
500 m_model
->setSortOrder(Qt::AscendingOrder
);
501 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
502 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
503 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
504 QCOMPARE(spyItemsMoved
.count(), 1);
505 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
507 // Sort by Date, ascending, 'Sort Folders First' disabled
508 m_model
->setSortFoldersFirst(false);
509 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
510 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
511 QVERIFY(!m_model
->sortFoldersFirst());
512 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
513 QCOMPARE(spyItemsMoved
.count(), 1);
514 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
516 // Sort by Name, ascending, 'Sort Folders First' disabled
517 m_model
->setSortRole("name");
518 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
519 QVERIFY(!m_model
->sortFoldersFirst());
520 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
521 QCOMPARE(spyItemsMoved
.count(), 1);
522 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
524 // Sort by Size, ascending, 'Sort Folders First' disabled
525 m_model
->setSortRole("size");
526 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
527 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
528 QVERIFY(!m_model
->sortFoldersFirst());
529 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
530 QCOMPARE(spyItemsMoved
.count(), 1);
531 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
533 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
534 m_model
->setSortFoldersFirst(true);
535 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
536 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
537 QVERIFY(m_model
->sortFoldersFirst());
538 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
539 QCOMPARE(spyItemsMoved
.count(), 0);
541 // Sort by Size, descending, 'Sort Folders First' enabled
542 m_model
->setSortOrder(Qt::DescendingOrder
);
543 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
544 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
545 QVERIFY(m_model
->sortFoldersFirst());
546 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
547 QCOMPARE(spyItemsMoved
.count(), 1);
548 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
550 // TODO: Sort by other roles; show/hide hidden files
553 void KFileItemModelTest::testExpansionLevelsCompare_data()
555 QTest::addColumn
<QString
>("urlA");
556 QTest::addColumn
<QString
>("urlB");
557 QTest::addColumn
<int>("result");
559 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
560 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
561 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
562 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
563 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
564 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
565 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
568 void KFileItemModelTest::testExpansionLevelsCompare()
570 QFETCH(QString
, urlA
);
571 QFETCH(QString
, urlB
);
574 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
575 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
576 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
579 void KFileItemModelTest::testIndexForKeyboardSearch()
582 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
583 m_testDir
->createFiles(files
);
585 m_dirLister
->openUrl(m_testDir
->url());
586 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
588 // Search from index 0
589 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
590 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
591 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
592 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
593 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
594 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
595 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
596 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
597 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
598 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
599 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
601 // Start a search somewhere in the middle
602 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
603 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
604 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
605 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
607 // Test searches that go past the last item back to index 0
608 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
609 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
610 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
611 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
613 // Test searches that yield no result
614 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
615 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
616 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
617 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
618 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
620 // Test upper case searches (note that search is case insensitive)
621 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
622 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
623 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
624 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
626 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
629 bool KFileItemModelTest::isModelConsistent() const
631 for (int i
= 0; i
< m_model
->count(); ++i
) {
632 const KFileItem item
= m_model
->fileItem(i
);
634 qWarning() << "Item" << i
<< "is null";
638 const int itemIndex
= m_model
->index(item
);
639 if (itemIndex
!= i
) {
640 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
648 QStringList
KFileItemModelTest::itemsInModel() const
652 for (int i
= 0; i
< m_model
->count(); i
++) {
653 items
<< m_model
->data(i
).value("name").toString();
659 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
661 #include "kfileitemmodeltest.moc"