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>
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
30 void myMessageOutput(QtMsgType type
, const char* msg
)
38 fprintf(stderr
, "Critical: %s\n", msg
);
41 fprintf(stderr
, "Fatal: %s\n", msg
);
49 const int DefaultTimeout
= 5000;
52 Q_DECLARE_METATYPE(KItemRange
)
53 Q_DECLARE_METATYPE(KItemRangeList
)
54 Q_DECLARE_METATYPE(QList
<int>)
56 class KFileItemModelTest
: public QObject
64 void testDefaultRoles();
65 void testDefaultSortRole();
66 void testDefaultGroupedSorting();
68 void testRemoveItems();
69 void testDirLoadingCompleted();
71 void testSetDataWithModifiedSortRole_data();
72 void testSetDataWithModifiedSortRole();
73 void testChangeSortRole();
74 void testResortAfterChangingName();
75 void testModelConsistencyWhenInsertingItems();
76 void testItemRangeConsistencyWhenInsertingItems();
77 void testExpandItems();
78 void testExpandParentItems();
79 void testMakeExpandedItemHidden();
80 void testRemoveFilteredExpandedItems();
82 void testIndexForKeyboardSearch();
83 void testNameFilter();
85 void testRefreshExpandedItem();
86 void testRemoveHiddenItems();
87 void collapseParentOfHiddenItems();
88 void removeParentOfHiddenItems();
89 void testGeneralParentChildRelationships();
90 void testNameRoleGroups();
91 void testNameRoleGroupsWithExpandedItems();
94 QStringList
itemsInModel() const;
97 KFileItemModel
* m_model
;
101 void KFileItemModelTest::init()
103 // The item-model tests result in a huge number of debugging
104 // output from kdelibs. Only show critical and fatal messages.
105 qInstallMsgHandler(myMessageOutput
);
107 qRegisterMetaType
<KItemRange
>("KItemRange");
108 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
109 qRegisterMetaType
<KFileItemList
>("KFileItemList");
111 m_testDir
= new TestDir();
112 m_model
= new KFileItemModel();
113 m_model
->m_dirLister
->setAutoUpdate(false);
115 // Reduce the timer interval to make the test run faster.
116 m_model
->m_resortAllItemsTimer
->setInterval(0);
119 void KFileItemModelTest::cleanup()
128 void KFileItemModelTest::testDefaultRoles()
130 const QSet
<QByteArray
> roles
= m_model
->roles();
131 QCOMPARE(roles
.count(), 3);
132 QVERIFY(roles
.contains("text"));
133 QVERIFY(roles
.contains("isDir"));
134 QVERIFY(roles
.contains("isLink"));
137 void KFileItemModelTest::testDefaultSortRole()
139 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
142 files
<< "c.txt" << "a.txt" << "b.txt";
144 m_testDir
->createFiles(files
);
146 m_model
->loadDirectory(m_testDir
->url());
147 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
149 QCOMPARE(m_model
->count(), 3);
150 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
151 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
152 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
155 void KFileItemModelTest::testDefaultGroupedSorting()
157 QCOMPARE(m_model
->groupedSorting(), false);
160 void KFileItemModelTest::testNewItems()
163 files
<< "a.txt" << "b.txt" << "c.txt";
164 m_testDir
->createFiles(files
);
166 m_model
->loadDirectory(m_testDir
->url());
167 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
169 QCOMPARE(m_model
->count(), 3);
171 QVERIFY(m_model
->isConsistent());
174 void KFileItemModelTest::testRemoveItems()
176 m_testDir
->createFile("a.txt");
177 m_testDir
->createFile("b.txt");
178 m_model
->loadDirectory(m_testDir
->url());
179 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
180 QCOMPARE(m_model
->count(), 2);
181 QVERIFY(m_model
->isConsistent());
183 m_testDir
->removeFile("a.txt");
184 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
186 QCOMPARE(m_model
->count(), 1);
187 QVERIFY(m_model
->isConsistent());
190 void KFileItemModelTest::testDirLoadingCompleted()
192 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
193 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
194 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
196 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
198 m_model
->loadDirectory(m_testDir
->url());
199 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
200 QCOMPARE(loadingCompletedSpy
.count(), 1);
201 QCOMPARE(itemsInsertedSpy
.count(), 1);
202 QCOMPARE(itemsRemovedSpy
.count(), 0);
203 QCOMPARE(m_model
->count(), 3);
205 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
206 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
207 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
208 QCOMPARE(loadingCompletedSpy
.count(), 2);
209 QCOMPARE(itemsInsertedSpy
.count(), 2);
210 QCOMPARE(itemsRemovedSpy
.count(), 0);
211 QCOMPARE(m_model
->count(), 5);
213 m_testDir
->removeFile("a.txt");
214 m_testDir
->createFile("f.txt");
215 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
216 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
217 QCOMPARE(loadingCompletedSpy
.count(), 3);
218 QCOMPARE(itemsInsertedSpy
.count(), 3);
219 QCOMPARE(itemsRemovedSpy
.count(), 1);
220 QCOMPARE(m_model
->count(), 5);
222 m_testDir
->removeFile("b.txt");
223 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
224 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
225 QCOMPARE(loadingCompletedSpy
.count(), 4);
226 QCOMPARE(itemsInsertedSpy
.count(), 3);
227 QCOMPARE(itemsRemovedSpy
.count(), 2);
228 QCOMPARE(m_model
->count(), 4);
230 QVERIFY(m_model
->isConsistent());
233 void KFileItemModelTest::testSetData()
235 m_testDir
->createFile("a.txt");
237 m_model
->loadDirectory(m_testDir
->url());
238 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
240 QHash
<QByteArray
, QVariant
> values
;
241 values
.insert("customRole1", "Test1");
242 values
.insert("customRole2", "Test2");
244 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
245 m_model
->setData(0, values
);
246 QCOMPARE(itemsChangedSpy
.count(), 1);
248 values
= m_model
->data(0);
249 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
250 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
251 QVERIFY(m_model
->isConsistent());
254 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
256 QTest::addColumn
<int>("changedIndex");
257 QTest::addColumn
<int>("changedRating");
258 QTest::addColumn
<bool>("expectMoveSignal");
259 QTest::addColumn
<int>("ratingIndex0");
260 QTest::addColumn
<int>("ratingIndex1");
261 QTest::addColumn
<int>("ratingIndex2");
264 // Index 0 = rating 2
265 // Index 1 = rating 4
266 // Index 2 = rating 6
268 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
269 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
270 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
272 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
273 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
274 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
277 void KFileItemModelTest::testSetDataWithModifiedSortRole()
279 QFETCH(int, changedIndex
);
280 QFETCH(int, changedRating
);
281 QFETCH(bool, expectMoveSignal
);
282 QFETCH(int, ratingIndex0
);
283 QFETCH(int, ratingIndex1
);
284 QFETCH(int, ratingIndex2
);
286 // Changing the value of a sort-role must result in
287 // a reordering of the items.
288 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
289 m_model
->setSortRole("rating");
290 QCOMPARE(m_model
->sortRole(), QByteArray("rating"));
293 files
<< "a.txt" << "b.txt" << "c.txt";
294 m_testDir
->createFiles(files
);
296 m_model
->loadDirectory(m_testDir
->url());
297 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
299 // Fill the "rating" role of each file:
304 QHash
<QByteArray
, QVariant
> ratingA
;
305 ratingA
.insert("rating", 2);
306 m_model
->setData(0, ratingA
);
308 QHash
<QByteArray
, QVariant
> ratingB
;
309 ratingB
.insert("rating", 4);
310 m_model
->setData(1, ratingB
);
312 QHash
<QByteArray
, QVariant
> ratingC
;
313 ratingC
.insert("rating", 6);
314 m_model
->setData(2, ratingC
);
316 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
317 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
318 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
320 // Now change the rating from a.txt. This usually results
321 // in reordering of the items.
322 QHash
<QByteArray
, QVariant
> rating
;
323 rating
.insert("rating", changedRating
);
324 m_model
->setData(changedIndex
, rating
);
326 if (expectMoveSignal
) {
327 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
330 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
331 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
332 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
333 QVERIFY(m_model
->isConsistent());
336 void KFileItemModelTest::testChangeSortRole()
338 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
341 files
<< "a.txt" << "b.jpg" << "c.txt";
342 m_testDir
->createFiles(files
);
344 m_model
->loadDirectory(m_testDir
->url());
345 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
346 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
348 // Simulate that KFileItemModelRolesUpdater determines the mime type.
349 // Resorting the files by 'type' will only work immediately if their
350 // mime types are known.
351 for (int index
= 0; index
< m_model
->count(); ++index
) {
352 m_model
->fileItem(index
).determineMimeType();
355 // Now: sort by type.
356 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
357 m_model
->setSortRole("type");
358 QCOMPARE(m_model
->sortRole(), QByteArray("type"));
359 QVERIFY(!spyItemsMoved
.isEmpty());
361 // The actual order of the files might depend on the translation of the
362 // result of KFileItem::mimeComment() in the user's language.
363 QStringList version1
;
364 version1
<< "b.jpg" << "a.txt" << "c.txt";
366 QStringList version2
;
367 version2
<< "a.txt" << "c.txt" << "b.jpg";
369 const bool ok1
= (itemsInModel() == version1
);
370 const bool ok2
= (itemsInModel() == version2
);
375 void KFileItemModelTest::testResortAfterChangingName()
377 // We sort by size in a directory where all files have the same size.
378 // Therefore, the files are sorted by their names.
379 m_model
->setSortRole("size");
382 files
<< "a.txt" << "b.txt" << "c.txt";
383 m_testDir
->createFiles(files
);
385 m_model
->loadDirectory(m_testDir
->url());
386 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
387 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
389 // We rename a.txt to d.txt. Even though the size has not changed at all,
390 // the model must re-sort the items.
391 QHash
<QByteArray
, QVariant
> data
;
392 data
.insert("text", "d.txt");
393 m_model
->setData(0, data
);
395 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
396 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
398 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
399 const KFileItem fileItemD
= m_model
->fileItem(2);
400 KFileItem fileItemA
= fileItemD
;
401 KUrl urlA
= fileItemA
.url();
402 urlA
.setFileName("a.txt");
403 fileItemA
.setUrl(urlA
);
405 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemA
));
407 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
408 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
411 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
413 //QSKIP("Temporary disabled", SkipSingle);
415 // KFileItemModel prevents that inserting a punch of items sequentially
416 // results in an itemsInserted()-signal for each item. Instead internally
417 // a timeout is given that collects such operations and results in only
418 // one itemsInserted()-signal. However in this test we want to stress
419 // KFileItemModel to do a lot of insert operation and hence decrease
420 // the timeout to 1 millisecond.
421 m_testDir
->createFile("1");
422 m_model
->loadDirectory(m_testDir
->url());
423 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
424 QCOMPARE(m_model
->count(), 1);
426 // Insert 10 items for 20 times. After each insert operation the model consistency
428 QSet
<int> insertedItems
;
429 for (int i
= 0; i
< 20; ++i
) {
430 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
432 for (int j
= 0; j
< 10; ++j
) {
433 int itemName
= qrand();
434 while (insertedItems
.contains(itemName
)) {
437 insertedItems
.insert(itemName
);
439 m_testDir
->createFile(QString::number(itemName
));
442 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
443 if (spy
.count() == 0) {
444 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
447 QVERIFY(m_model
->isConsistent());
450 QCOMPARE(m_model
->count(), 201);
453 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
456 files
<< "B" << "E" << "G";
457 m_testDir
->createFiles(files
);
459 // Due to inserting the 3 items one item-range with index == 0 and
460 // count == 3 must be given
461 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
462 m_model
->loadDirectory(m_testDir
->url());
463 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
465 QCOMPARE(spy1
.count(), 1);
466 QList
<QVariant
> arguments
= spy1
.takeFirst();
467 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
468 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
470 // The indexes of the item-ranges must always be related to the model before
471 // the items have been inserted. Having:
474 // and inserting A, C, D, F the resulting model will be:
477 // and the item-ranges must be:
478 // index: 0, count: 1 for A
479 // index: 1, count: 2 for B, C
480 // index: 2, count: 1 for G
483 files
<< "A" << "C" << "D" << "F";
484 m_testDir
->createFiles(files
);
486 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
487 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
488 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
490 QCOMPARE(spy2
.count(), 1);
491 arguments
= spy2
.takeFirst();
492 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
493 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
496 void KFileItemModelTest::testExpandItems()
498 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
499 // Besides testing the basic item expansion functionality, the test makes sure that
500 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
501 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
502 // first three characters.
503 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
504 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
505 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
506 m_model
->setRoles(modelRoles
);
509 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
510 m_testDir
->createFiles(files
);
512 // Store the URLs of all folders in a set.
513 QSet
<KUrl
> allFolders
;
514 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
516 m_model
->loadDirectory(m_testDir
->url());
517 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
519 // So far, the model contains only "a/"
520 QCOMPARE(m_model
->count(), 1);
521 QVERIFY(m_model
->isExpandable(0));
522 QVERIFY(!m_model
->isExpanded(0));
523 QVERIFY(m_model
->expandedDirectories().empty());
525 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
527 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
528 m_model
->setExpanded(0, true);
529 QVERIFY(m_model
->isExpanded(0));
530 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
531 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
532 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
534 QCOMPARE(spyInserted
.count(), 1);
535 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
536 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
538 QVERIFY(m_model
->isExpandable(1));
539 QVERIFY(!m_model
->isExpanded(1));
540 QVERIFY(m_model
->isExpandable(2));
541 QVERIFY(!m_model
->isExpanded(2));
543 // Expand the folder "a/a/" -> "a/a/1" becomes visible
544 m_model
->setExpanded(1, true);
545 QVERIFY(m_model
->isExpanded(1));
546 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
547 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
548 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
550 QCOMPARE(spyInserted
.count(), 1);
551 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
552 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
554 QVERIFY(!m_model
->isExpandable(2));
555 QVERIFY(!m_model
->isExpanded(2));
557 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
558 m_model
->setExpanded(3, true);
559 QVERIFY(m_model
->isExpanded(3));
560 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
561 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
562 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
564 QCOMPARE(spyInserted
.count(), 1);
565 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
566 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
568 QVERIFY(!m_model
->isExpandable(4));
569 QVERIFY(!m_model
->isExpanded(4));
571 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
573 // Collapse the top-level folder -> all other items should disappear
574 m_model
->setExpanded(0, false);
575 QVERIFY(!m_model
->isExpanded(0));
576 QCOMPARE(m_model
->count(), 1);
577 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
579 QCOMPARE(spyRemoved
.count(), 1);
580 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
581 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
582 QVERIFY(m_model
->isConsistent());
584 // Clear the model, reload the folder and try to restore the expanded folders.
586 QCOMPARE(m_model
->count(), 0);
587 QVERIFY(m_model
->expandedDirectories().empty());
589 m_model
->loadDirectory(m_testDir
->url());
590 m_model
->restoreExpandedDirectories(allFolders
);
591 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
592 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
593 QVERIFY(m_model
->isExpanded(0));
594 QVERIFY(m_model
->isExpanded(1));
595 QVERIFY(!m_model
->isExpanded(2));
596 QVERIFY(m_model
->isExpanded(3));
597 QVERIFY(!m_model
->isExpanded(4));
598 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
599 QVERIFY(m_model
->isConsistent());
601 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
602 // This is how DolphinView restores the expanded folders when navigating in history.
603 m_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
604 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
605 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
606 m_model
->restoreExpandedDirectories(allFolders
);
607 m_model
->loadDirectory(m_testDir
->url());
608 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
609 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
610 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
612 // Remove all expanded items by changing the roles
614 m_model
->setRoles(originalModelRoles
);
615 QVERIFY(!m_model
->isExpanded(0));
616 QCOMPARE(m_model
->count(), 1);
617 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a')));
619 QCOMPARE(spyRemoved
.count(), 1);
620 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
621 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
622 QVERIFY(m_model
->isConsistent());
625 void KFileItemModelTest::testExpandParentItems()
627 // Create a tree structure of folders:
635 QSet
<QByteArray
> modelRoles
= m_model
->roles();
636 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
637 m_model
->setRoles(modelRoles
);
640 files
<< "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
641 m_testDir
->createFiles(files
);
643 m_model
->loadDirectory(m_testDir
->url());
644 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
646 // So far, the model contains only "a 1/" and "a2/".
647 QCOMPARE(m_model
->count(), 2);
648 QVERIFY(m_model
->expandedDirectories().empty());
650 // Expand the parents of "a2/b2/c2".
651 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
652 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
654 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
655 // It's important that only the parents of "a1/b1/c1" are expanded.
656 QCOMPARE(m_model
->count(), 4);
657 QVERIFY(!m_model
->isExpanded(0));
658 QVERIFY(m_model
->isExpanded(1));
659 QVERIFY(m_model
->isExpanded(2));
660 QVERIFY(!m_model
->isExpanded(3));
662 // Expand the parents of "a 1/b1".
663 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
664 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
666 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
667 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
668 QCOMPARE(m_model
->count(), 5);
669 QVERIFY(m_model
->isExpanded(0));
670 QVERIFY(!m_model
->isExpanded(1));
671 QVERIFY(m_model
->isExpanded(2));
672 QVERIFY(m_model
->isExpanded(3));
673 QVERIFY(!m_model
->isExpanded(4));
674 QVERIFY(m_model
->isConsistent());
677 m_model
->setExpanded(1, true);
678 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
679 QCOMPARE(m_model
->count(), 6);
680 QVERIFY(m_model
->isExpanded(0));
681 QVERIFY(m_model
->isExpanded(1));
682 QVERIFY(!m_model
->isExpanded(2));
683 QVERIFY(m_model
->isExpanded(3));
684 QVERIFY(m_model
->isExpanded(4));
685 QVERIFY(!m_model
->isExpanded(5));
686 QVERIFY(m_model
->isConsistent());
688 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
689 m_model
->setExpanded(1, false);
690 QCOMPARE(m_model
->count(), 5);
691 QVERIFY(m_model
->isExpanded(0));
692 QVERIFY(!m_model
->isExpanded(1));
693 QVERIFY(m_model
->isExpanded(2));
694 QVERIFY(m_model
->isExpanded(3));
695 QVERIFY(!m_model
->isExpanded(4));
696 QVERIFY(m_model
->isConsistent());
700 * Renaming an expanded folder by prepending its name with a dot makes it
701 * hidden. Verify that this does not cause an inconsistent model state and
702 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
704 void KFileItemModelTest::testMakeExpandedItemHidden()
706 QSet
<QByteArray
> modelRoles
= m_model
->roles();
707 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
708 m_model
->setRoles(modelRoles
);
711 m_testDir
->createFile("1a/2a/3a");
712 m_testDir
->createFile("1a/2a/3b");
713 m_testDir
->createFile("1a/2b");
714 m_testDir
->createFile("1b");
716 m_model
->loadDirectory(m_testDir
->url());
717 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
719 // So far, the model contains only "1a/" and "1b".
720 QCOMPARE(m_model
->count(), 2);
721 m_model
->setExpanded(0, true);
722 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
724 // Now "1a/2a" and "1a/2b" have appeared.
725 QCOMPARE(m_model
->count(), 4);
726 m_model
->setExpanded(1, true);
727 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
728 QCOMPARE(m_model
->count(), 6);
730 // Rename "1a/2" and make it hidden.
731 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
732 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
734 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
735 bool ok
= job
->exec();
737 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
739 // "1a/2" and its subfolders have disappeared now.
740 QVERIFY(m_model
->isConsistent());
741 QCOMPARE(m_model
->count(), 3);
743 m_model
->setExpanded(0, false);
744 QCOMPARE(m_model
->count(), 2);
748 void KFileItemModelTest::testRemoveFilteredExpandedItems()
750 QSet
<QByteArray
> originalModelRoles
= m_model
->roles();
751 QSet
<QByteArray
> modelRoles
= originalModelRoles
;
752 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
753 m_model
->setRoles(modelRoles
);
756 files
<< "folder/child" << "file"; // missing folders are created automatically
757 m_testDir
->createFiles(files
);
759 m_model
->loadDirectory(m_testDir
->url());
760 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
762 // So far, the model contains only "folder/" and "file".
763 QCOMPARE(m_model
->count(), 2);
764 QVERIFY(m_model
->isExpandable(0));
765 QVERIFY(!m_model
->isExpandable(1));
766 QVERIFY(!m_model
->isExpanded(0));
767 QVERIFY(!m_model
->isExpanded(1));
768 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
770 // Expand "folder" -> "folder/child" becomes visible.
771 m_model
->setExpanded(0, true);
772 QVERIFY(m_model
->isExpanded(0));
773 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
774 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
776 // Add a name filter.
777 m_model
->setNameFilter("f");
778 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
780 m_model
->setNameFilter("fo");
781 QCOMPARE(itemsInModel(), QStringList() << "folder");
783 // Remove all expanded items by changing the roles
784 m_model
->setRoles(originalModelRoles
);
785 QVERIFY(!m_model
->isExpanded(0));
786 QCOMPARE(itemsInModel(), QStringList() << "folder");
788 // Remove the name filter and verify that "folder/child" does not reappear.
789 m_model
->setNameFilter(QString());
790 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
793 void KFileItemModelTest::testSorting()
795 // Create some files with different sizes and modification times to check the different sorting options
796 QDateTime now
= QDateTime::currentDateTime();
798 QSet
<QByteArray
> roles
;
799 roles
.insert("text");
800 roles
.insert("isExpanded");
801 roles
.insert("isExpandable");
802 roles
.insert("expandedParentsCount");
803 m_model
->setRoles(roles
);
805 m_testDir
->createDir("c/c-2");
806 m_testDir
->createFile("c/c-2/c-3");
807 m_testDir
->createFile("c/c-1");
809 m_testDir
->createFile("a", "A file", now
.addDays(-3));
810 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
811 m_testDir
->createDir("c", now
.addDays(-2));
812 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
813 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
814 m_testDir
->createFile(".f");
816 m_model
->loadDirectory(m_testDir
->url());
817 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
819 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
820 m_model
->setExpanded(index
, true);
821 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
823 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
824 m_model
->setExpanded(index
, true);
825 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
827 // Default: Sort by Name, ascending
828 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
829 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
830 QVERIFY(m_model
->sortDirectoriesFirst());
831 QVERIFY(!m_model
->showHiddenFiles());
832 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
834 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
836 // Sort by Name, ascending, 'Sort Folders First' disabled
837 m_model
->setSortDirectoriesFirst(false);
838 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
839 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
840 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
841 QCOMPARE(spyItemsMoved
.count(), 1);
842 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
843 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
845 // Sort by Name, descending
846 m_model
->setSortDirectoriesFirst(true);
847 m_model
->setSortOrder(Qt::DescendingOrder
);
848 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
849 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
850 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
851 QCOMPARE(spyItemsMoved
.count(), 2);
852 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
853 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2);
854 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
855 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
857 // Sort by Date, descending
858 m_model
->setSortDirectoriesFirst(true);
859 m_model
->setSortRole("date");
860 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
861 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
862 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
863 QCOMPARE(spyItemsMoved
.count(), 1);
864 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
865 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 5 << 4 << 6);
867 // Sort by Date, ascending
868 m_model
->setSortOrder(Qt::AscendingOrder
);
869 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
870 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
871 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
872 QCOMPARE(spyItemsMoved
.count(), 1);
873 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
874 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
876 // Sort by Date, ascending, 'Sort Folders First' disabled
877 m_model
->setSortDirectoriesFirst(false);
878 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
879 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
880 QVERIFY(!m_model
->sortDirectoriesFirst());
881 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
882 QCOMPARE(spyItemsMoved
.count(), 1);
883 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 6));
884 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1);
886 // Sort by Name, ascending, 'Sort Folders First' disabled
887 m_model
->setSortRole("text");
888 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
889 QVERIFY(!m_model
->sortDirectoriesFirst());
890 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
891 QCOMPARE(spyItemsMoved
.count(), 1);
892 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
893 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
895 // Sort by Size, ascending, 'Sort Folders First' disabled
896 m_model
->setSortRole("size");
897 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
898 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
899 QVERIFY(!m_model
->sortDirectoriesFirst());
900 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
901 QCOMPARE(spyItemsMoved
.count(), 1);
902 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(0, 8));
903 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
905 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
906 m_model
->setSortDirectoriesFirst(true);
907 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
908 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
909 QVERIFY(m_model
->sortDirectoriesFirst());
910 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
911 QCOMPARE(spyItemsMoved
.count(), 0);
913 // Sort by Size, descending, 'Sort Folders First' enabled
914 m_model
->setSortOrder(Qt::DescendingOrder
);
915 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
916 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
917 QVERIFY(m_model
->sortDirectoriesFirst());
918 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
919 QCOMPARE(spyItemsMoved
.count(), 1);
920 QCOMPARE(spyItemsMoved
.first().at(0).value
<KItemRange
>(), KItemRange(4, 4));
921 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 6 << 5 << 4);
923 // TODO: Sort by other roles; show/hide hidden files
926 void KFileItemModelTest::testIndexForKeyboardSearch()
929 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
930 m_testDir
->createFiles(files
);
932 m_model
->loadDirectory(m_testDir
->url());
933 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
935 // Search from index 0
936 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
937 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
938 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
939 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
940 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
941 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
942 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
943 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
944 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
945 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
946 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
948 // Start a search somewhere in the middle
949 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
950 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
951 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
952 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
954 // Test searches that go past the last item back to index 0
955 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
956 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
957 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
958 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
960 // Test searches that yield no result
961 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
962 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
963 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
964 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
965 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
967 // Test upper case searches (note that search is case insensitive)
968 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
969 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
970 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
971 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
973 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
976 void KFileItemModelTest::testNameFilter()
979 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
980 m_testDir
->createFiles(files
);
982 m_model
->loadDirectory(m_testDir
->url());
983 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
985 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
986 QCOMPARE(m_model
->count(), 3);
988 m_model
->setNameFilter("A2"); // Shows only A2
989 QCOMPARE(m_model
->count(), 1);
991 m_model
->setNameFilter("A2"); // Shows only A1
992 QCOMPARE(m_model
->count(), 1);
994 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
995 QCOMPARE(m_model
->count(), 2);
997 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
998 QCOMPARE(m_model
->count(), 2);
1000 m_model
->setNameFilter(QString()); // Shows again all items
1001 QCOMPARE(m_model
->count(), 5);
1005 * Verifies that we do not crash when adding a KFileItem with an empty path.
1006 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1007 * tried to always read the first character of the path, even if the path is empty.
1009 void KFileItemModelTest::testEmptyPath()
1011 QSet
<QByteArray
> roles
;
1012 roles
.insert("text");
1013 roles
.insert("isExpanded");
1014 roles
.insert("isExpandable");
1015 roles
.insert("expandedParentsCount");
1016 m_model
->setRoles(roles
);
1018 const KUrl emptyUrl
;
1019 QVERIFY(emptyUrl
.path().isEmpty());
1021 const KUrl
url("file:///test/");
1023 KFileItemList items
;
1024 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
1025 m_model
->slotItemsAdded(emptyUrl
, items
);
1026 m_model
->slotCompleted();
1030 * Verifies that the 'isExpanded' state of folders does not change when the
1031 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1033 void KFileItemModelTest::testRefreshExpandedItem()
1035 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1036 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1037 m_model
->setRoles(modelRoles
);
1040 files
<< "a/1" << "a/2" << "3" << "4";
1041 m_testDir
->createFiles(files
);
1043 m_model
->loadDirectory(m_testDir
->url());
1044 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1045 QCOMPARE(m_model
->count(), 3); // "a/", "3", "4"
1047 m_model
->setExpanded(0, true);
1048 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1049 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1050 QVERIFY(m_model
->isExpanded(0));
1052 QSignalSpy
spyItemsChanged(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1054 const KFileItem item
= m_model
->fileItem(0);
1055 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(item
, item
));
1056 QVERIFY(!spyItemsChanged
.isEmpty());
1058 QCOMPARE(m_model
->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1059 QVERIFY(m_model
->isExpanded(0));
1063 * Verify that removing hidden files and folders from the model does not
1064 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1066 void KFileItemModelTest::testRemoveHiddenItems()
1068 m_testDir
->createDir(".a");
1069 m_testDir
->createDir(".b");
1070 m_testDir
->createDir("c");
1071 m_testDir
->createDir("d");
1072 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1074 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
1075 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1077 m_model
->setShowHiddenFiles(true);
1078 m_model
->loadDirectory(m_testDir
->url());
1079 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1080 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1081 QCOMPARE(spyItemsInserted
.count(), 1);
1082 QCOMPARE(spyItemsRemoved
.count(), 0);
1083 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1084 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1086 m_model
->setShowHiddenFiles(false);
1087 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1088 QCOMPARE(spyItemsInserted
.count(), 0);
1089 QCOMPARE(spyItemsRemoved
.count(), 1);
1090 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1091 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1093 m_model
->setShowHiddenFiles(true);
1094 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1095 QCOMPARE(spyItemsInserted
.count(), 1);
1096 QCOMPARE(spyItemsRemoved
.count(), 0);
1097 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
1098 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1101 QCOMPARE(itemsInModel(), QStringList());
1102 QCOMPARE(spyItemsInserted
.count(), 0);
1103 QCOMPARE(spyItemsRemoved
.count(), 1);
1104 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
1105 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
1107 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1108 // Verify that this does not make the model crash.
1109 m_model
->setShowHiddenFiles(false);
1113 * Verify that filtered items are removed when their parent is collapsed.
1115 void KFileItemModelTest::collapseParentOfHiddenItems()
1117 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1118 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1119 m_model
->setRoles(modelRoles
);
1122 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1123 m_testDir
->createFiles(files
);
1125 m_model
->loadDirectory(m_testDir
->url());
1126 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1127 QCOMPARE(m_model
->count(), 1); // Only "a/"
1130 m_model
->setExpanded(0, true);
1131 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1132 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1135 m_model
->setExpanded(1, true);
1136 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1137 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1140 m_model
->setExpanded(2, true);
1141 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1142 QCOMPARE(m_model
->count(), 7); // 7 items: "a/", "a/b/", "a/b/c", "a/b/c/d/", "a/b/c/1", "a/b/1", "a/1"
1144 // Set a name filter that matches nothing -> only the expanded folders remain.
1145 m_model
->setNameFilter("xyz");
1146 QCOMPARE(m_model
->count(), 3);
1147 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1149 // Collapse the folder "a/".
1150 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1151 m_model
->setExpanded(0, false);
1152 QCOMPARE(spyItemsRemoved
.count(), 1);
1153 QCOMPARE(m_model
->count(), 1);
1154 QCOMPARE(itemsInModel(), QStringList() << "a");
1156 // Remove the filter -> no files should appear (and we should not get a crash).
1157 m_model
->setNameFilter(QString());
1158 QCOMPARE(m_model
->count(), 1);
1162 * Verify that filtered items are removed when their parent is deleted.
1164 void KFileItemModelTest::removeParentOfHiddenItems()
1166 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1167 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1168 m_model
->setRoles(modelRoles
);
1171 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1172 m_testDir
->createFiles(files
);
1174 m_model
->loadDirectory(m_testDir
->url());
1175 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1176 QCOMPARE(m_model
->count(), 1); // Only "a/"
1179 m_model
->setExpanded(0, true);
1180 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1181 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1184 m_model
->setExpanded(1, true);
1185 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1186 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1189 m_model
->setExpanded(2, true);
1190 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1191 QCOMPARE(m_model
->count(), 7); // 7 items: "a/", "a/b/", "a/b/c", "a/b/c/d/", "a/b/c/1", "a/b/1", "a/1"
1193 // Set a name filter that matches nothing -> only the expanded folders remain.
1194 m_model
->setNameFilter("xyz");
1195 QCOMPARE(m_model
->count(), 3);
1196 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1198 // Simulate the deletion of the directory "a/b/".
1199 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1200 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1201 QCOMPARE(spyItemsRemoved
.count(), 1);
1202 QCOMPARE(m_model
->count(), 1);
1203 QCOMPARE(itemsInModel(), QStringList() << "a");
1205 // Remove the filter -> only the file "a/1" should appear.
1206 m_model
->setNameFilter(QString());
1207 QCOMPARE(m_model
->count(), 2);
1208 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1212 * Create a tree structure where parent-child relationships can not be
1213 * determined by parsing the URLs, and verify that KFileItemModel
1214 * handles them correctly.
1216 void KFileItemModelTest::testGeneralParentChildRelationships()
1218 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1219 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1220 m_model
->setRoles(modelRoles
);
1223 files
<< "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1224 m_testDir
->createFiles(files
);
1226 m_model
->loadDirectory(m_testDir
->url());
1227 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1228 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1230 // Expand all folders.
1231 m_model
->setExpanded(0, true);
1232 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1233 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1235 m_model
->setExpanded(1, true);
1236 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1237 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1239 m_model
->setExpanded(3, true);
1240 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1241 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1243 m_model
->setExpanded(4, true);
1244 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1245 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1247 // Add some more children and grand-children.
1248 const KUrl parent1
= m_model
->fileItem(0).url();
1249 const KUrl parent2
= m_model
->fileItem(3).url();
1250 const KUrl realChild1
= m_model
->fileItem(1).url();
1251 const KUrl realChild2
= m_model
->fileItem(4).url();
1253 m_model
->slotItemsAdded(parent1
, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown
));
1254 m_model
->slotCompleted();
1255 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1257 m_model
->slotItemsAdded(parent2
, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown
));
1258 m_model
->slotCompleted();
1259 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1261 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1262 m_model
->slotCompleted();
1263 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1265 m_model
->slotItemsAdded(realChild1
, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown
));
1266 m_model
->slotCompleted();
1267 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1269 m_model
->slotItemsAdded(realChild2
, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown
));
1270 m_model
->slotCompleted();
1271 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1273 // Set a name filter that matches nothing -> only expanded folders remain.
1274 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
1275 m_model
->setNameFilter("xyz");
1276 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1277 QCOMPARE(itemsRemovedSpy
.count(), 1);
1278 QList
<QVariant
> arguments
= itemsRemovedSpy
.takeFirst();
1279 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1280 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1282 // Collapse "parent1".
1283 m_model
->setExpanded(0, false);
1284 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1285 QCOMPARE(itemsRemovedSpy
.count(), 1);
1286 arguments
= itemsRemovedSpy
.takeFirst();
1287 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1288 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 1));
1290 // Remove "parent2".
1291 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
1292 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1293 QCOMPARE(itemsRemovedSpy
.count(), 1);
1294 arguments
= itemsRemovedSpy
.takeFirst();
1295 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
1296 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2));
1298 // Clear filter, verify that no items reappear.
1299 m_model
->setNameFilter(QString());
1300 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1303 void KFileItemModelTest::testNameRoleGroups()
1306 files
<< "b.txt" << "c.txt" << "d.txt" << "e.txt";
1308 m_testDir
->createFiles(files
);
1310 m_model
->setGroupedSorting(true);
1311 m_model
->loadDirectory(m_testDir
->url());
1312 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1313 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1315 QList
<QPair
<int, QVariant
> > expectedGroups
;
1316 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("B"));
1317 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("C"));
1318 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1319 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1320 QCOMPARE(m_model
->groups(), expectedGroups
);
1322 // Rename d.txt to a.txt.
1323 QHash
<QByteArray
, QVariant
> data
;
1324 data
.insert("text", "a.txt");
1325 m_model
->setData(2, data
);
1326 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
1327 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1329 expectedGroups
.clear();
1330 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1331 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1332 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1333 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1334 QCOMPARE(m_model
->groups(), expectedGroups
);
1336 // Rename c.txt to d.txt.
1337 data
.insert("text", "d.txt");
1338 m_model
->setData(2, data
);
1339 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1340 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1342 expectedGroups
.clear();
1343 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1344 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1345 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("D"));
1346 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1347 QCOMPARE(m_model
->groups(), expectedGroups
);
1349 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1350 const KFileItem fileItemD
= m_model
->fileItem(2);
1351 KFileItem fileItemC
= fileItemD
;
1352 KUrl urlC
= fileItemC
.url();
1353 urlC
.setFileName("c.txt");
1354 fileItemC
.setUrl(urlC
);
1356 m_model
->slotRefreshItems(QList
<QPair
<KFileItem
, KFileItem
> >() << qMakePair(fileItemD
, fileItemC
));
1357 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(groupsChanged()), DefaultTimeout
));
1358 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1360 expectedGroups
.clear();
1361 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1362 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("B"));
1363 expectedGroups
<< QPair
<int, QVariant
>(2, QLatin1String("C"));
1364 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("E"));
1365 QCOMPARE(m_model
->groups(), expectedGroups
);
1368 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1370 QSet
<QByteArray
> modelRoles
= m_model
->roles();
1371 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
1372 m_model
->setRoles(modelRoles
);
1375 files
<< "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1377 m_testDir
->createFiles(files
);
1379 m_model
->setGroupedSorting(true);
1380 m_model
->loadDirectory(m_testDir
->url());
1381 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1382 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1384 QList
<QPair
<int, QVariant
> > expectedGroups
;
1385 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1386 expectedGroups
<< QPair
<int, QVariant
>(1, QLatin1String("D"));
1387 QCOMPARE(m_model
->groups(), expectedGroups
);
1389 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1390 expectedGroups
.clear();
1391 expectedGroups
<< QPair
<int, QVariant
>(0, QLatin1String("A"));
1392 expectedGroups
<< QPair
<int, QVariant
>(3, QLatin1String("D"));
1394 m_model
->setExpanded(0, true);
1395 QVERIFY(m_model
->isExpanded(0));
1396 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1397 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1398 QCOMPARE(m_model
->groups(), expectedGroups
);
1400 m_model
->setExpanded(3, true);
1401 QVERIFY(m_model
->isExpanded(3));
1402 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
1403 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1404 QCOMPARE(m_model
->groups(), expectedGroups
);
1407 QStringList
KFileItemModelTest::itemsInModel() const
1410 for (int i
= 0; i
< m_model
->count(); i
++) {
1411 items
<< m_model
->fileItem(i
).text();
1416 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1418 #include "kfileitemmodeltest.moc"