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"
25 #include "kitemviews/private/kfileitemmodeldirlister.h"
28 void myMessageOutput(QtMsgType type
, const char* msg
)
36 fprintf(stderr
, "Critical: %s\n", msg
);
39 fprintf(stderr
, "Fatal: %s\n", msg
);
47 const int DefaultTimeout
= 5000;
50 Q_DECLARE_METATYPE(KItemRangeList
)
51 Q_DECLARE_METATYPE(QList
<int>)
53 class KFileItemModelTest
: public QObject
61 void testDefaultRoles();
62 void testDefaultSortRole();
63 void testDefaultGroupedSorting();
65 void testRemoveItems();
66 void testDirLoadingCompleted();
68 void testSetDataWithModifiedSortRole_data();
69 void testSetDataWithModifiedSortRole();
70 void testModelConsistencyWhenInsertingItems();
71 void testItemRangeConsistencyWhenInsertingItems();
72 void testExpandItems();
73 void testExpandParentItems();
75 void testIndexForKeyboardSearch();
76 void testNameFilter();
80 QStringList
itemsInModel() const;
83 KFileItemModel
* m_model
;
87 void KFileItemModelTest::init()
89 // The item-model tests result in a huge number of debugging
90 // output from kdelibs. Only show critical and fatal messages.
91 qInstallMsgHandler(myMessageOutput
);
93 qRegisterMetaType
<KItemRange
>("KItemRange");
94 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
95 qRegisterMetaType
<KFileItemList
>("KFileItemList");
97 m_testDir
= new TestDir();
98 m_model
= new KFileItemModel();
99 m_model
->m_dirLister
->setAutoUpdate(false);
102 void KFileItemModelTest::cleanup()
111 void KFileItemModelTest::testDefaultRoles()
113 const QSet
<QByteArray
> roles
= m_model
->roles();
114 QCOMPARE(roles
.count(), 3);
115 QVERIFY(roles
.contains("text"));
116 QVERIFY(roles
.contains("isDir"));
117 QVERIFY(roles
.contains("isLink"));
120 void KFileItemModelTest::testDefaultSortRole()
122 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
125 files
<< "c.txt" << "a.txt" << "b.txt";
127 m_testDir
->createFiles(files
);
129 m_model
->loadDirectory(m_testDir
->url());
130 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
132 QCOMPARE(m_model
->count(), 3);
133 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
134 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
135 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
138 void KFileItemModelTest::testDefaultGroupedSorting()
140 QCOMPARE(m_model
->groupedSorting(), false);
143 void KFileItemModelTest::testNewItems()
146 files
<< "a.txt" << "b.txt" << "c.txt";
147 m_testDir
->createFiles(files
);
149 m_model
->loadDirectory(m_testDir
->url());
150 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
152 QCOMPARE(m_model
->count(), 3);
154 QVERIFY(m_model
->isConsistent());
157 void KFileItemModelTest::testRemoveItems()
159 m_testDir
->createFile("a.txt");
160 m_testDir
->createFile("b.txt");
161 m_model
->loadDirectory(m_testDir
->url());
162 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
163 QCOMPARE(m_model
->count(), 2);
164 QVERIFY(m_model
->isConsistent());
166 m_testDir
->removeFile("a.txt");
167 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
168 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
169 QCOMPARE(m_model
->count(), 1);
170 QVERIFY(m_model
->isConsistent());
173 void KFileItemModelTest::testDirLoadingCompleted()
175 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
176 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
177 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
179 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
181 m_model
->loadDirectory(m_testDir
->url());
182 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
183 QCOMPARE(loadingCompletedSpy
.count(), 1);
184 QCOMPARE(itemsInsertedSpy
.count(), 1);
185 QCOMPARE(itemsRemovedSpy
.count(), 0);
186 QCOMPARE(m_model
->count(), 3);
188 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
189 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
190 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
191 QCOMPARE(loadingCompletedSpy
.count(), 2);
192 QCOMPARE(itemsInsertedSpy
.count(), 2);
193 QCOMPARE(itemsRemovedSpy
.count(), 0);
194 QCOMPARE(m_model
->count(), 5);
196 m_testDir
->removeFile("a.txt");
197 m_testDir
->createFile("f.txt");
198 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
199 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
200 QCOMPARE(loadingCompletedSpy
.count(), 3);
201 QCOMPARE(itemsInsertedSpy
.count(), 3);
202 QCOMPARE(itemsRemovedSpy
.count(), 1);
203 QCOMPARE(m_model
->count(), 5);
205 m_testDir
->removeFile("b.txt");
206 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
207 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
208 QCOMPARE(loadingCompletedSpy
.count(), 4);
209 QCOMPARE(itemsInsertedSpy
.count(), 3);
210 QCOMPARE(itemsRemovedSpy
.count(), 2);
211 QCOMPARE(m_model
->count(), 4);
213 QVERIFY(m_model
->isConsistent());
216 void KFileItemModelTest::testSetData()
218 m_testDir
->createFile("a.txt");
220 m_model
->loadDirectory(m_testDir
->url());
221 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
223 QHash
<QByteArray
, QVariant
> values
;
224 values
.insert("customRole1", "Test1");
225 values
.insert("customRole2", "Test2");
227 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
228 m_model
->setData(0, values
);
229 QCOMPARE(itemsChangedSpy
.count(), 1);
231 values
= m_model
->data(0);
232 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
233 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
234 QVERIFY(m_model
->isConsistent());
237 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
239 QTest::addColumn
<int>("changedIndex");
240 QTest::addColumn
<int>("changedRating");
241 QTest::addColumn
<bool>("expectMoveSignal");
242 QTest::addColumn
<int>("ratingIndex0");
243 QTest::addColumn
<int>("ratingIndex1");
244 QTest::addColumn
<int>("ratingIndex2");
247 // Index 0 = rating 2
248 // Index 1 = rating 4
249 // Index 2 = rating 6
251 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
252 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
253 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
255 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
256 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
257 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
260 void KFileItemModelTest::testSetDataWithModifiedSortRole()
262 QFETCH(int, changedIndex
);
263 QFETCH(int, changedRating
);
264 QFETCH(bool, expectMoveSignal
);
265 QFETCH(int, ratingIndex0
);
266 QFETCH(int, ratingIndex1
);
267 QFETCH(int, ratingIndex2
);
269 // Changing the value of a sort-role must result in
270 // a reordering of the items.
271 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
274 files
<< "a.txt" << "b.txt" << "c.txt";
275 m_testDir
->createFiles(files
);
277 m_model
->loadDirectory(m_testDir
->url());
278 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
280 // Fill the "rating" role of each file:
285 QHash
<QByteArray
, QVariant
> ratingA
;
286 ratingA
.insert("rating", 2);
287 m_model
->setData(0, ratingA
);
289 QHash
<QByteArray
, QVariant
> ratingB
;
290 ratingB
.insert("rating", 4);
291 m_model
->setData(1, ratingB
);
293 QHash
<QByteArray
, QVariant
> ratingC
;
294 ratingC
.insert("rating", 6);
295 m_model
->setData(2, ratingC
);
297 m_model
->setSortRole("rating");
298 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
299 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
300 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
302 // Now change the rating from a.txt. This usually results
303 // in reordering of the items.
304 QHash
<QByteArray
, QVariant
> rating
;
305 rating
.insert("rating", changedRating
);
306 m_model
->setData(changedIndex
, rating
);
308 if (expectMoveSignal
) {
309 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
312 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
313 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
314 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
315 QVERIFY(m_model
->isConsistent());
318 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
320 //QSKIP("Temporary disabled", SkipSingle);
322 // KFileItemModel prevents that inserting a punch of items sequentially
323 // results in an itemsInserted()-signal for each item. Instead internally
324 // a timeout is given that collects such operations and results in only
325 // one itemsInserted()-signal. However in this test we want to stress
326 // KFileItemModel to do a lot of insert operation and hence decrease
327 // the timeout to 1 millisecond.
328 m_testDir
->createFile("1");
329 m_model
->loadDirectory(m_testDir
->url());
330 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
331 QCOMPARE(m_model
->count(), 1);
333 // Insert 10 items for 20 times. After each insert operation the model consistency
335 QSet
<int> insertedItems
;
336 for (int i
= 0; i
< 20; ++i
) {
337 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
339 for (int j
= 0; j
< 10; ++j
) {
340 int itemName
= qrand();
341 while (insertedItems
.contains(itemName
)) {
344 insertedItems
.insert(itemName
);
346 m_testDir
->createFile(QString::number(itemName
));
349 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
350 if (spy
.count() == 0) {
351 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
354 QVERIFY(m_model
->isConsistent());
357 QCOMPARE(m_model
->count(), 201);
360 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
363 files
<< "B" << "E" << "G";
364 m_testDir
->createFiles(files
);
366 // Due to inserting the 3 items one item-range with index == 0 and
367 // count == 3 must be given
368 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
369 m_model
->loadDirectory(m_testDir
->url());
370 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
372 QCOMPARE(spy1
.count(), 1);
373 QList
<QVariant
> arguments
= spy1
.takeFirst();
374 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
375 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
377 // The indexes of the item-ranges must always be related to the model before
378 // the items have been inserted. Having:
381 // and inserting A, C, D, F the resulting model will be:
384 // and the item-ranges must be:
385 // index: 0, count: 1 for A
386 // index: 1, count: 2 for B, C
387 // index: 2, count: 1 for G
390 files
<< "A" << "C" << "D" << "F";
391 m_testDir
->createFiles(files
);
393 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
394 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
395 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
397 QCOMPARE(spy2
.count(), 1);
398 arguments
= spy2
.takeFirst();
399 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
400 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
403 void KFileItemModelTest::testExpandItems()
405 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
406 // Besides testing the basic item expansion functionality, the test makes sure that
407 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
408 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
409 // first three characters.
410 QSet
<QByteArray
> modelRoles
= m_model
->roles();
411 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
412 m_model
->setRoles(modelRoles
);
415 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
416 m_testDir
->createFiles(files
);
418 // Store the URLs of all folders in a set.
419 QSet
<KUrl
> allFolders
;
420 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
422 m_model
->loadDirectory(m_testDir
->url());
423 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
425 // So far, the model contains only "a/"
426 QCOMPARE(m_model
->count(), 1);
427 QVERIFY(m_model
->isExpandable(0));
428 QVERIFY(!m_model
->isExpanded(0));
429 QVERIFY(m_model
->expandedDirectories().empty());
431 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
433 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
434 m_model
->setExpanded(0, true);
435 QVERIFY(m_model
->isExpanded(0));
436 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
437 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
438 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
440 QCOMPARE(spyInserted
.count(), 1);
441 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
442 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
444 QVERIFY(m_model
->isExpandable(1));
445 QVERIFY(!m_model
->isExpanded(1));
446 QVERIFY(m_model
->isExpandable(2));
447 QVERIFY(!m_model
->isExpanded(2));
449 // Expand the folder "a/a/" -> "a/a/1" becomes visible
450 m_model
->setExpanded(1, true);
451 QVERIFY(m_model
->isExpanded(1));
452 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
453 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
454 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
456 QCOMPARE(spyInserted
.count(), 1);
457 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
458 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
460 QVERIFY(!m_model
->isExpandable(2));
461 QVERIFY(!m_model
->isExpanded(2));
463 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
464 m_model
->setExpanded(3, true);
465 QVERIFY(m_model
->isExpanded(3));
466 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
467 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
468 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
470 QCOMPARE(spyInserted
.count(), 1);
471 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
472 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
474 QVERIFY(!m_model
->isExpandable(4));
475 QVERIFY(!m_model
->isExpanded(4));
477 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
479 // Collapse the top-level folder -> all other items should disappear
480 m_model
->setExpanded(0, false);
481 QVERIFY(!m_model
->isExpanded(0));
482 QCOMPARE(m_model
->count(), 1);
483 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
485 QCOMPARE(spyRemoved
.count(), 1);
486 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
487 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
489 // Clear the model, reload the folder and try to restore the expanded folders.
491 QCOMPARE(m_model
->count(), 0);
492 QVERIFY(m_model
->expandedDirectories().empty());
494 m_model
->loadDirectory(m_testDir
->url());
495 m_model
->restoreExpandedDirectories(allFolders
);
496 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
497 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
498 QVERIFY(m_model
->isExpanded(0));
499 QVERIFY(m_model
->isExpanded(1));
500 QVERIFY(!m_model
->isExpanded(2));
501 QVERIFY(m_model
->isExpanded(3));
502 QVERIFY(!m_model
->isExpanded(4));
503 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
505 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
506 // This is how DolphinView restores the expanded folders when navigating in history.
507 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
508 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
509 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
510 m_model
->restoreExpandedDirectories(allFolders
);
511 m_model
->loadDirectory(m_testDir
->url());
512 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
513 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
514 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
517 void KFileItemModelTest::testExpandParentItems()
519 // Create a tree structure of folders:
527 QSet
<QByteArray
> modelRoles
= m_model
->roles();
528 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
529 m_model
->setRoles(modelRoles
);
532 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
533 m_testDir
->createFiles(files
);
535 m_model
->loadDirectory(m_testDir
->url());
536 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
538 // So far, the model contains only "a 1/" and "a2/".
539 QCOMPARE(m_model
->count(), 2);
540 QVERIFY(m_model
->expandedDirectories().empty());
542 // Expand the parents of "a2/b2/c2".
543 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
544 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
546 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
547 // It's important that only the parents of "a1/b1/c1" are expanded.
548 QCOMPARE(m_model
->count(), 4);
549 QVERIFY(!m_model
->isExpanded(0));
550 QVERIFY(m_model
->isExpanded(1));
551 QVERIFY(m_model
->isExpanded(2));
552 QVERIFY(!m_model
->isExpanded(3));
554 // Expand the parents of "a 1/b1".
555 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
556 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
558 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
559 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
560 QCOMPARE(m_model
->count(), 5);
561 QVERIFY(m_model
->isExpanded(0));
562 QVERIFY(!m_model
->isExpanded(1));
563 QVERIFY(m_model
->isExpanded(2));
564 QVERIFY(m_model
->isExpanded(3));
565 QVERIFY(!m_model
->isExpanded(4));
568 void KFileItemModelTest::testSorting()
570 // Create some files with different sizes and modification times to check the different sorting options
571 QDateTime now
= QDateTime::currentDateTime();
573 QSet
<QByteArray
> roles
;
574 roles
.insert("text");
575 roles
.insert("isExpanded");
576 roles
.insert("isExpandable");
577 roles
.insert("expandedParentsCount");
578 m_model
->setRoles(roles
);
580 m_testDir
->createDir("c/c-2");
581 m_testDir
->createFile("c/c-2/c-3");
582 m_testDir
->createFile("c/c-1");
584 m_testDir
->createFile("a", "A file", now
.addDays(-3));
585 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
586 m_testDir
->createDir("c", now
.addDays(-2));
587 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
588 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
589 m_testDir
->createFile(".f");
591 m_model
->loadDirectory(m_testDir
->url());
592 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
594 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
595 m_model
->setExpanded(index
, true);
596 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
598 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
599 m_model
->setExpanded(index
, true);
600 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
602 // Default: Sort by Name, ascending
603 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
604 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
605 QVERIFY(m_model
->sortDirectoriesFirst());
606 QVERIFY(!m_model
->showHiddenFiles());
607 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
609 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
611 // Sort by Name, ascending, 'Sort Folders First' disabled
612 m_model
->setSortDirectoriesFirst(false);
613 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
614 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
615 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
616 QCOMPARE(spyItemsMoved
.count(), 1);
617 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
619 // Sort by Name, descending
620 m_model
->setSortDirectoriesFirst(true);
621 m_model
->setSortOrder(Qt::DescendingOrder
);
622 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
623 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
624 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
625 QCOMPARE(spyItemsMoved
.count(), 2);
626 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
627 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
629 // Sort by Date, descending
630 m_model
->setSortDirectoriesFirst(true);
631 m_model
->setSortRole("date");
632 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
633 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
634 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
635 QCOMPARE(spyItemsMoved
.count(), 1);
636 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
638 // Sort by Date, ascending
639 m_model
->setSortOrder(Qt::AscendingOrder
);
640 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
641 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
642 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
643 QCOMPARE(spyItemsMoved
.count(), 1);
644 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
646 // Sort by Date, ascending, 'Sort Folders First' disabled
647 m_model
->setSortDirectoriesFirst(false);
648 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
649 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
650 QVERIFY(!m_model
->sortDirectoriesFirst());
651 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
652 QCOMPARE(spyItemsMoved
.count(), 1);
653 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
655 // Sort by Name, ascending, 'Sort Folders First' disabled
656 m_model
->setSortRole("text");
657 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
658 QVERIFY(!m_model
->sortDirectoriesFirst());
659 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
660 QCOMPARE(spyItemsMoved
.count(), 1);
661 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
663 // Sort by Size, ascending, 'Sort Folders First' disabled
664 m_model
->setSortRole("size");
665 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
666 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
667 QVERIFY(!m_model
->sortDirectoriesFirst());
668 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
669 QCOMPARE(spyItemsMoved
.count(), 1);
670 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
672 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
673 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
674 "another signal", SkipSingle
);
675 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
677 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
678 m_model
->setSortDirectoriesFirst(true);
679 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
680 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
681 QVERIFY(m_model
->sortDirectoriesFirst());
682 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
683 QCOMPARE(spyItemsMoved
.count(), 0);
685 // Sort by Size, descending, 'Sort Folders First' enabled
686 m_model
->setSortOrder(Qt::DescendingOrder
);
687 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
688 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
689 QVERIFY(m_model
->sortDirectoriesFirst());
690 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
691 QCOMPARE(spyItemsMoved
.count(), 1);
692 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
694 // TODO: Sort by other roles; show/hide hidden files
697 void KFileItemModelTest::testIndexForKeyboardSearch()
700 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
701 m_testDir
->createFiles(files
);
703 m_model
->loadDirectory(m_testDir
->url());
704 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
706 // Search from index 0
707 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
708 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
709 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
710 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
711 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
712 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
713 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
714 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
715 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
716 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
717 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
719 // Start a search somewhere in the middle
720 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
721 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
722 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
723 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
725 // Test searches that go past the last item back to index 0
726 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
727 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
728 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
729 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
731 // Test searches that yield no result
732 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
733 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
734 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
735 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
736 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
738 // Test upper case searches (note that search is case insensitive)
739 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
740 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
741 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
742 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
744 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
747 void KFileItemModelTest::testNameFilter()
750 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
751 m_testDir
->createFiles(files
);
753 m_model
->loadDirectory(m_testDir
->url());
754 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
756 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
757 QCOMPARE(m_model
->count(), 3);
759 m_model
->setNameFilter("A2"); // Shows only A2
760 QCOMPARE(m_model
->count(), 1);
762 m_model
->setNameFilter("A2"); // Shows only A1
763 QCOMPARE(m_model
->count(), 1);
765 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
766 QCOMPARE(m_model
->count(), 2);
768 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
769 QCOMPARE(m_model
->count(), 2);
771 m_model
->setNameFilter(QString()); // Shows again all items
772 QCOMPARE(m_model
->count(), 5);
776 * Verifies that we do not crash when adding a KFileItem with an empty path.
777 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
778 * tried to always read the first character of the path, even if the path is empty.
780 void KFileItemModelTest::testEmptyPath()
782 QSet
<QByteArray
> roles
;
783 roles
.insert("text");
784 roles
.insert("isExpanded");
785 roles
.insert("isExpandable");
786 roles
.insert("expandedParentsCount");
787 m_model
->setRoles(roles
);
790 QVERIFY(emptyUrl
.path().isEmpty());
792 const KUrl
url("file:///test/");
795 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
796 m_model
->slotNewItems(items
);
797 m_model
->slotCompleted();
800 QStringList
KFileItemModelTest::itemsInModel() const
803 for (int i
= 0; i
< m_model
->count(); i
++) {
804 items
<< m_model
->data(i
).value("text").toString();
809 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
811 #include "kfileitemmodeltest.moc"