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();
65 void testLoadingCompleted();
67 void testSetDataWithModifiedSortRole_data();
68 void testSetDataWithModifiedSortRole();
69 void testModelConsistencyWhenInsertingItems();
70 void testItemRangeConsistencyWhenInsertingItems();
71 void testExpandItems();
72 void testExpandParentItems();
75 void testExpansionLevelsCompare_data();
76 void testExpansionLevelsCompare();
78 void testIndexForKeyboardSearch();
80 void testNameFilter();
83 bool isModelConsistent() const;
84 QStringList
itemsInModel() const;
87 KFileItemModel
* m_model
;
88 KDirLister
* m_dirLister
;
92 void KFileItemModelTest::init()
94 // The item-model tests result in a huge number of debugging
95 // output from kdelibs. Only show critical and fatal messages.
96 qInstallMsgHandler(myMessageOutput
);
98 qRegisterMetaType
<KItemRange
>("KItemRange");
99 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
100 qRegisterMetaType
<KFileItemList
>("KFileItemList");
102 m_testDir
= new TestDir();
103 m_dirLister
= new KDirLister();
104 m_dirLister
->setAutoUpdate(false);
105 m_model
= new KFileItemModel(m_dirLister
);
108 void KFileItemModelTest::cleanup()
120 void KFileItemModelTest::testDefaultRoles()
122 const QSet
<QByteArray
> roles
= m_model
->roles();
123 QCOMPARE(roles
.count(), 2);
124 QVERIFY(roles
.contains("name"));
125 QVERIFY(roles
.contains("isDir"));
128 void KFileItemModelTest::testDefaultSortRole()
130 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
133 files
<< "c.txt" << "a.txt" << "b.txt";
135 m_testDir
->createFiles(files
);
137 m_dirLister
->openUrl(m_testDir
->url());
138 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
140 QCOMPARE(m_model
->count(), 3);
141 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
142 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
143 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
146 void KFileItemModelTest::testDefaultGroupedSorting()
148 QCOMPARE(m_model
->groupedSorting(), false);
151 void KFileItemModelTest::testNewItems()
154 files
<< "a.txt" << "b.txt" << "c.txt";
155 m_testDir
->createFiles(files
);
157 m_dirLister
->openUrl(m_testDir
->url());
158 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
160 QCOMPARE(m_model
->count(), 3);
162 QVERIFY(isModelConsistent());
165 void KFileItemModelTest::testRemoveItems()
167 m_testDir
->createFile("a.txt");
168 m_testDir
->createFile("b.txt");
169 m_dirLister
->openUrl(m_testDir
->url());
170 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
171 QCOMPARE(m_model
->count(), 2);
172 QVERIFY(isModelConsistent());
174 m_testDir
->removeFile("a.txt");
175 m_dirLister
->updateDirectory(m_testDir
->url());
176 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
177 QCOMPARE(m_model
->count(), 1);
178 QVERIFY(isModelConsistent());
181 void KFileItemModelTest::testLoadingCompleted()
183 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(loadingCompleted()));
184 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
185 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
187 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
189 m_dirLister
->openUrl(m_testDir
->url());
190 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
191 QCOMPARE(loadingCompletedSpy
.count(), 1);
192 QCOMPARE(itemsInsertedSpy
.count(), 1);
193 QCOMPARE(itemsRemovedSpy
.count(), 0);
194 QCOMPARE(m_model
->count(), 3);
196 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
197 m_dirLister
->updateDirectory(m_testDir
->url());
198 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
199 QCOMPARE(loadingCompletedSpy
.count(), 2);
200 QCOMPARE(itemsInsertedSpy
.count(), 2);
201 QCOMPARE(itemsRemovedSpy
.count(), 0);
202 QCOMPARE(m_model
->count(), 5);
204 m_testDir
->removeFile("a.txt");
205 m_testDir
->createFile("f.txt");
206 m_dirLister
->updateDirectory(m_testDir
->url());
207 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
208 QCOMPARE(loadingCompletedSpy
.count(), 3);
209 QCOMPARE(itemsInsertedSpy
.count(), 3);
210 QCOMPARE(itemsRemovedSpy
.count(), 1);
211 QCOMPARE(m_model
->count(), 5);
213 m_testDir
->removeFile("b.txt");
214 m_dirLister
->updateDirectory(m_testDir
->url());
215 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
216 QCOMPARE(loadingCompletedSpy
.count(), 4);
217 QCOMPARE(itemsInsertedSpy
.count(), 3);
218 QCOMPARE(itemsRemovedSpy
.count(), 2);
219 QCOMPARE(m_model
->count(), 4);
221 QVERIFY(isModelConsistent());
224 void KFileItemModelTest::testSetData()
226 m_testDir
->createFile("a.txt");
228 m_dirLister
->openUrl(m_testDir
->url());
229 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
231 QHash
<QByteArray
, QVariant
> values
;
232 values
.insert("customRole1", "Test1");
233 values
.insert("customRole2", "Test2");
235 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
236 m_model
->setData(0, values
);
237 QCOMPARE(itemsChangedSpy
.count(), 1);
239 values
= m_model
->data(0);
240 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
241 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
242 QVERIFY(isModelConsistent());
245 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
247 QTest::addColumn
<int>("changedIndex");
248 QTest::addColumn
<int>("changedRating");
249 QTest::addColumn
<bool>("expectMoveSignal");
250 QTest::addColumn
<int>("ratingIndex0");
251 QTest::addColumn
<int>("ratingIndex1");
252 QTest::addColumn
<int>("ratingIndex2");
255 // Index 0 = rating 2
256 // Index 1 = rating 4
257 // Index 2 = rating 6
259 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
260 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
261 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
263 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
264 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
265 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
268 void KFileItemModelTest::testSetDataWithModifiedSortRole()
270 QFETCH(int, changedIndex
);
271 QFETCH(int, changedRating
);
272 QFETCH(bool, expectMoveSignal
);
273 QFETCH(int, ratingIndex0
);
274 QFETCH(int, ratingIndex1
);
275 QFETCH(int, ratingIndex2
);
277 // Changing the value of a sort-role must result in
278 // a reordering of the items.
279 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
282 files
<< "a.txt" << "b.txt" << "c.txt";
283 m_testDir
->createFiles(files
);
285 m_dirLister
->openUrl(m_testDir
->url());
286 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
288 // Fill the "rating" role of each file:
293 QHash
<QByteArray
, QVariant
> ratingA
;
294 ratingA
.insert("rating", 2);
295 m_model
->setData(0, ratingA
);
297 QHash
<QByteArray
, QVariant
> ratingB
;
298 ratingB
.insert("rating", 4);
299 m_model
->setData(1, ratingB
);
301 QHash
<QByteArray
, QVariant
> ratingC
;
302 ratingC
.insert("rating", 6);
303 m_model
->setData(2, ratingC
);
305 m_model
->setSortRole("rating");
306 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
307 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
308 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
310 // Now change the rating from a.txt. This usually results
311 // in reordering of the items.
312 QHash
<QByteArray
, QVariant
> rating
;
313 rating
.insert("rating", changedRating
);
314 m_model
->setData(changedIndex
, rating
);
316 if (expectMoveSignal
) {
317 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
320 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
321 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
322 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
323 QVERIFY(isModelConsistent());
326 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
328 //QSKIP("Temporary disabled", SkipSingle);
330 // KFileItemModel prevents that inserting a punch of items sequentially
331 // results in an itemsInserted()-signal for each item. Instead internally
332 // a timeout is given that collects such operations and results in only
333 // one itemsInserted()-signal. However in this test we want to stress
334 // KFileItemModel to do a lot of insert operation and hence decrease
335 // the timeout to 1 millisecond.
336 m_testDir
->createFile("1");
337 m_dirLister
->openUrl(m_testDir
->url());
338 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
339 QCOMPARE(m_model
->count(), 1);
341 // Insert 10 items for 20 times. After each insert operation the model consistency
343 QSet
<int> insertedItems
;
344 for (int i
= 0; i
< 20; ++i
) {
345 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
347 for (int j
= 0; j
< 10; ++j
) {
348 int itemName
= qrand();
349 while (insertedItems
.contains(itemName
)) {
352 insertedItems
.insert(itemName
);
354 m_testDir
->createFile(QString::number(itemName
));
357 m_dirLister
->updateDirectory(m_testDir
->url());
358 if (spy
.count() == 0) {
359 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
362 QVERIFY(isModelConsistent());
365 QCOMPARE(m_model
->count(), 201);
368 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
371 files
<< "B" << "E" << "G";
372 m_testDir
->createFiles(files
);
374 // Due to inserting the 3 items one item-range with index == 0 and
375 // count == 3 must be given
376 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
377 m_dirLister
->openUrl(m_testDir
->url());
378 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
380 QCOMPARE(spy1
.count(), 1);
381 QList
<QVariant
> arguments
= spy1
.takeFirst();
382 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
383 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
385 // The indexes of the item-ranges must always be related to the model before
386 // the items have been inserted. Having:
389 // and inserting A, C, D, F the resulting model will be:
392 // and the item-ranges must be:
393 // index: 0, count: 1 for A
394 // index: 1, count: 2 for B, C
395 // index: 2, count: 1 for G
398 files
<< "A" << "C" << "D" << "F";
399 m_testDir
->createFiles(files
);
401 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
402 m_dirLister
->updateDirectory(m_testDir
->url());
403 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
405 QCOMPARE(spy2
.count(), 1);
406 arguments
= spy2
.takeFirst();
407 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
408 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
411 void KFileItemModelTest::testExpandItems()
413 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
414 // Besides testing the basic item expansion functionality, the test makes sure that
415 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
416 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
417 // first three characters.
418 QSet
<QByteArray
> modelRoles
= m_model
->roles();
419 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
420 m_model
->setRoles(modelRoles
);
423 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
424 m_testDir
->createFiles(files
);
426 // Store the URLs of all folders in a set.
427 QSet
<KUrl
> allFolders
;
428 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
430 m_dirLister
->openUrl(m_testDir
->url());
431 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
433 // So far, the model contains only "a/"
434 QCOMPARE(m_model
->count(), 1);
435 QVERIFY(m_model
->isExpandable(0));
436 QVERIFY(!m_model
->isExpanded(0));
437 QVERIFY(m_model
->expandedUrls().empty());
439 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
441 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
442 m_model
->setExpanded(0, true);
443 QVERIFY(m_model
->isExpanded(0));
444 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
445 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
446 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
448 QCOMPARE(spyInserted
.count(), 1);
449 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
450 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
452 QVERIFY(m_model
->isExpandable(1));
453 QVERIFY(!m_model
->isExpanded(1));
454 QVERIFY(m_model
->isExpandable(2));
455 QVERIFY(!m_model
->isExpanded(2));
457 // Expand the folder "a/a/" -> "a/a/1" becomes visible
458 m_model
->setExpanded(1, true);
459 QVERIFY(m_model
->isExpanded(1));
460 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
461 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
462 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
464 QCOMPARE(spyInserted
.count(), 1);
465 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
466 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
468 QVERIFY(!m_model
->isExpandable(2));
469 QVERIFY(!m_model
->isExpanded(2));
471 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
472 m_model
->setExpanded(3, true);
473 QVERIFY(m_model
->isExpanded(3));
474 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
475 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
476 QCOMPARE(m_model
->expandedUrls(), allFolders
);
478 QCOMPARE(spyInserted
.count(), 1);
479 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
480 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
482 QVERIFY(!m_model
->isExpandable(4));
483 QVERIFY(!m_model
->isExpanded(4));
485 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
487 // Collapse the top-level folder -> all other items should disappear
488 m_model
->setExpanded(0, false);
489 QVERIFY(!m_model
->isExpanded(0));
490 QCOMPARE(m_model
->count(), 1);
491 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
493 QCOMPARE(spyRemoved
.count(), 1);
494 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
495 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
497 // Clear the model, reload the folder and try to restore the expanded folders.
499 QCOMPARE(m_model
->count(), 0);
500 QVERIFY(m_model
->expandedUrls().empty());
502 m_dirLister
->openUrl(m_testDir
->url());
503 m_model
->restoreExpandedUrls(allFolders
);
504 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
505 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
506 QVERIFY(m_model
->isExpanded(0));
507 QVERIFY(m_model
->isExpanded(1));
508 QVERIFY(!m_model
->isExpanded(2));
509 QVERIFY(m_model
->isExpanded(3));
510 QVERIFY(!m_model
->isExpanded(4));
511 QCOMPARE(m_model
->expandedUrls(), allFolders
);
513 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
514 // This is how DolphinView restores the expanded folders when navigating in history.
515 m_dirLister
->openUrl(KUrl(m_testDir
->name() + "a/a/"));
516 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
517 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
518 m_model
->restoreExpandedUrls(allFolders
);
519 m_dirLister
->openUrl(m_testDir
->url());
520 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
521 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
522 QCOMPARE(m_model
->expandedUrls(), allFolders
);
525 void KFileItemModelTest::testExpandParentItems()
527 // Create a tree structure of folders:
535 QSet
<QByteArray
> modelRoles
= m_model
->roles();
536 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
537 m_model
->setRoles(modelRoles
);
540 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
541 m_testDir
->createFiles(files
);
543 m_dirLister
->openUrl(m_testDir
->url());
544 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
546 // So far, the model contains only "a 1/" and "a2/".
547 QCOMPARE(m_model
->count(), 2);
548 QVERIFY(m_model
->expandedUrls().empty());
550 // Expand the parents of "a2/b2/c2".
551 m_model
->expandParentItems(KUrl(m_testDir
->name() + "a2/b2/c2"));
552 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
554 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
555 // It's important that only the parents of "a1/b1/c1" are expanded.
556 QCOMPARE(m_model
->count(), 4);
557 QVERIFY(!m_model
->isExpanded(0));
558 QVERIFY(m_model
->isExpanded(1));
559 QVERIFY(m_model
->isExpanded(2));
560 QVERIFY(!m_model
->isExpanded(3));
562 // Expand the parents of "a 1/b1".
563 m_model
->expandParentItems(KUrl(m_testDir
->name() + "a 1/b1"));
564 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
566 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
567 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
568 QCOMPARE(m_model
->count(), 5);
569 QVERIFY(m_model
->isExpanded(0));
570 QVERIFY(!m_model
->isExpanded(1));
571 QVERIFY(m_model
->isExpanded(2));
572 QVERIFY(m_model
->isExpanded(3));
573 QVERIFY(!m_model
->isExpanded(4));
576 void KFileItemModelTest::testSorting()
578 // Create some files with different sizes and modification times to check the different sorting options
579 QDateTime now
= QDateTime::currentDateTime();
581 m_testDir
->createFile("a", "A file", now
.addDays(-3));
582 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
583 m_testDir
->createDir("c", now
.addDays(-2));
584 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
585 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
586 m_testDir
->createFile(".f");
588 m_dirLister
->openUrl(m_testDir
->url());
589 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
591 // Default: Sort by Name, ascending
592 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
593 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
594 QVERIFY(m_model
->sortFoldersFirst());
595 //QVERIFY(!m_model->showHiddenFiles());
596 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
598 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
600 // Sort by Name, descending
601 m_model
->setSortOrder(Qt::DescendingOrder
);
602 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
603 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
604 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
605 QCOMPARE(spyItemsMoved
.count(), 1);
606 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
608 // Sort by Date, descending
609 m_model
->setSortRole("date");
610 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
611 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
612 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
613 QCOMPARE(spyItemsMoved
.count(), 1);
614 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
616 // Sort by Date, ascending
617 m_model
->setSortOrder(Qt::AscendingOrder
);
618 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
619 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
620 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
621 QCOMPARE(spyItemsMoved
.count(), 1);
622 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
624 // Sort by Date, ascending, 'Sort Folders First' disabled
625 m_model
->setSortFoldersFirst(false);
626 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
627 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
628 QVERIFY(!m_model
->sortFoldersFirst());
629 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
630 QCOMPARE(spyItemsMoved
.count(), 1);
631 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
633 // Sort by Name, ascending, 'Sort Folders First' disabled
634 m_model
->setSortRole("name");
635 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
636 QVERIFY(!m_model
->sortFoldersFirst());
637 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
638 QCOMPARE(spyItemsMoved
.count(), 1);
639 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
641 // Sort by Size, ascending, 'Sort Folders First' disabled
642 m_model
->setSortRole("size");
643 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
644 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
645 QVERIFY(!m_model
->sortFoldersFirst());
646 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
647 QCOMPARE(spyItemsMoved
.count(), 1);
648 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
650 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
651 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
652 "another signal", SkipSingle
);
653 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
655 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
656 m_model
->setSortFoldersFirst(true);
657 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
658 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
659 QVERIFY(m_model
->sortFoldersFirst());
660 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
661 QCOMPARE(spyItemsMoved
.count(), 0);
663 // Sort by Size, descending, 'Sort Folders First' enabled
664 m_model
->setSortOrder(Qt::DescendingOrder
);
665 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
666 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
667 QVERIFY(m_model
->sortFoldersFirst());
668 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
669 QCOMPARE(spyItemsMoved
.count(), 1);
670 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
672 // TODO: Sort by other roles; show/hide hidden files
675 void KFileItemModelTest::testExpansionLevelsCompare_data()
677 QTest::addColumn
<QString
>("urlA");
678 QTest::addColumn
<QString
>("urlB");
679 QTest::addColumn
<int>("result");
681 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
682 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
683 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
684 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
685 QTest::newRow("Same level: /a-1/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
686 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
687 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
690 void KFileItemModelTest::testExpansionLevelsCompare()
692 QSKIP("Temporary deactivated as KFileItemModel::ItemData has been extended "
693 "by a 'parent' member that is required for a correct comparison. For a "
694 "successful test the item-data of all parents must be available.", SkipAll
);
696 QFETCH(QString
, urlA
);
697 QFETCH(QString
, urlB
);
700 const KFileItem
itemA(KUrl(urlA
), QString(), mode_t(-1));
701 const KFileItem
itemB(KUrl(urlB
), QString(), mode_t(-1));
703 KFileItemModel::ItemData a
;
707 KFileItemModel::ItemData b
;
711 QCOMPARE(m_model
->expandedParentsCountCompare(&a
, &b
), result
);
714 void KFileItemModelTest::testIndexForKeyboardSearch()
717 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
718 m_testDir
->createFiles(files
);
720 m_dirLister
->openUrl(m_testDir
->url());
721 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
723 // Search from index 0
724 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
725 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
726 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
727 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
728 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
729 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
730 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
731 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
732 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
733 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
734 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
736 // Start a search somewhere in the middle
737 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
738 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
739 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
740 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
742 // Test searches that go past the last item back to index 0
743 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
744 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
745 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
746 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
748 // Test searches that yield no result
749 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
750 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
751 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
752 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
753 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
755 // Test upper case searches (note that search is case insensitive)
756 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
757 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
758 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
759 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
761 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
764 void KFileItemModelTest::testNameFilter()
767 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
768 m_testDir
->createFiles(files
);
770 m_dirLister
->openUrl(m_testDir
->url());
771 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
773 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
774 QCOMPARE(m_model
->count(), 3);
776 m_model
->setNameFilter("A2"); // Shows only A2
777 QCOMPARE(m_model
->count(), 1);
779 m_model
->setNameFilter("A2"); // Shows only A1
780 QCOMPARE(m_model
->count(), 1);
782 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
783 QCOMPARE(m_model
->count(), 2);
785 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
786 QCOMPARE(m_model
->count(), 2);
788 m_model
->setNameFilter(QString()); // Shows again all items
789 QCOMPARE(m_model
->count(), 5);
792 bool KFileItemModelTest::isModelConsistent() const
794 if (m_model
->m_items
.count() != m_model
->m_itemData
.count()) {
798 for (int i
= 0; i
< m_model
->count(); ++i
) {
799 const KFileItem item
= m_model
->fileItem(i
);
801 qWarning() << "Item" << i
<< "is null";
805 const int itemIndex
= m_model
->index(item
);
806 if (itemIndex
!= i
) {
807 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
815 QStringList
KFileItemModelTest::itemsInModel() const
819 for (int i
= 0; i
< m_model
->count(); i
++) {
820 items
<< m_model
->data(i
).value("name").toString();
826 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
828 #include "kfileitemmodeltest.moc"