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(KItemRangeList
)
53 Q_DECLARE_METATYPE(QList
<int>)
55 class KFileItemModelTest
: public QObject
63 void testDefaultRoles();
64 void testDefaultSortRole();
65 void testDefaultGroupedSorting();
67 void testRemoveItems();
68 void testDirLoadingCompleted();
70 void testSetDataWithModifiedSortRole_data();
71 void testSetDataWithModifiedSortRole();
72 void testModelConsistencyWhenInsertingItems();
73 void testItemRangeConsistencyWhenInsertingItems();
74 void testExpandItems();
75 void testExpandParentItems();
76 void testMakeExpandedItemHidden();
78 void testIndexForKeyboardSearch();
79 void testNameFilter();
81 void testRemoveHiddenItems();
82 void collapseParentOfHiddenItems();
83 void removeParentOfHiddenItems();
86 QStringList
itemsInModel() const;
89 KFileItemModel
* m_model
;
93 void KFileItemModelTest::init()
95 // The item-model tests result in a huge number of debugging
96 // output from kdelibs. Only show critical and fatal messages.
97 qInstallMsgHandler(myMessageOutput
);
99 qRegisterMetaType
<KItemRange
>("KItemRange");
100 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
101 qRegisterMetaType
<KFileItemList
>("KFileItemList");
103 m_testDir
= new TestDir();
104 m_model
= new KFileItemModel();
105 m_model
->m_dirLister
->setAutoUpdate(false);
108 void KFileItemModelTest::cleanup()
117 void KFileItemModelTest::testDefaultRoles()
119 const QSet
<QByteArray
> roles
= m_model
->roles();
120 QCOMPARE(roles
.count(), 3);
121 QVERIFY(roles
.contains("text"));
122 QVERIFY(roles
.contains("isDir"));
123 QVERIFY(roles
.contains("isLink"));
126 void KFileItemModelTest::testDefaultSortRole()
128 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
131 files
<< "c.txt" << "a.txt" << "b.txt";
133 m_testDir
->createFiles(files
);
135 m_model
->loadDirectory(m_testDir
->url());
136 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
138 QCOMPARE(m_model
->count(), 3);
139 QCOMPARE(m_model
->data(0)["text"].toString(), QString("a.txt"));
140 QCOMPARE(m_model
->data(1)["text"].toString(), QString("b.txt"));
141 QCOMPARE(m_model
->data(2)["text"].toString(), QString("c.txt"));
144 void KFileItemModelTest::testDefaultGroupedSorting()
146 QCOMPARE(m_model
->groupedSorting(), false);
149 void KFileItemModelTest::testNewItems()
152 files
<< "a.txt" << "b.txt" << "c.txt";
153 m_testDir
->createFiles(files
);
155 m_model
->loadDirectory(m_testDir
->url());
156 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
158 QCOMPARE(m_model
->count(), 3);
160 QVERIFY(m_model
->isConsistent());
163 void KFileItemModelTest::testRemoveItems()
165 m_testDir
->createFile("a.txt");
166 m_testDir
->createFile("b.txt");
167 m_model
->loadDirectory(m_testDir
->url());
168 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
169 QCOMPARE(m_model
->count(), 2);
170 QVERIFY(m_model
->isConsistent());
172 m_testDir
->removeFile("a.txt");
173 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
174 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
175 QCOMPARE(m_model
->count(), 1);
176 QVERIFY(m_model
->isConsistent());
179 void KFileItemModelTest::testDirLoadingCompleted()
181 QSignalSpy
loadingCompletedSpy(m_model
, SIGNAL(directoryLoadingCompleted()));
182 QSignalSpy
itemsInsertedSpy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
183 QSignalSpy
itemsRemovedSpy(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
185 m_testDir
->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
187 m_model
->loadDirectory(m_testDir
->url());
188 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
189 QCOMPARE(loadingCompletedSpy
.count(), 1);
190 QCOMPARE(itemsInsertedSpy
.count(), 1);
191 QCOMPARE(itemsRemovedSpy
.count(), 0);
192 QCOMPARE(m_model
->count(), 3);
194 m_testDir
->createFiles(QStringList() << "d.txt" << "e.txt");
195 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
196 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
197 QCOMPARE(loadingCompletedSpy
.count(), 2);
198 QCOMPARE(itemsInsertedSpy
.count(), 2);
199 QCOMPARE(itemsRemovedSpy
.count(), 0);
200 QCOMPARE(m_model
->count(), 5);
202 m_testDir
->removeFile("a.txt");
203 m_testDir
->createFile("f.txt");
204 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
205 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
206 QCOMPARE(loadingCompletedSpy
.count(), 3);
207 QCOMPARE(itemsInsertedSpy
.count(), 3);
208 QCOMPARE(itemsRemovedSpy
.count(), 1);
209 QCOMPARE(m_model
->count(), 5);
211 m_testDir
->removeFile("b.txt");
212 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
213 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
214 QCOMPARE(loadingCompletedSpy
.count(), 4);
215 QCOMPARE(itemsInsertedSpy
.count(), 3);
216 QCOMPARE(itemsRemovedSpy
.count(), 2);
217 QCOMPARE(m_model
->count(), 4);
219 QVERIFY(m_model
->isConsistent());
222 void KFileItemModelTest::testSetData()
224 m_testDir
->createFile("a.txt");
226 m_model
->loadDirectory(m_testDir
->url());
227 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
229 QHash
<QByteArray
, QVariant
> values
;
230 values
.insert("customRole1", "Test1");
231 values
.insert("customRole2", "Test2");
233 QSignalSpy
itemsChangedSpy(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
234 m_model
->setData(0, values
);
235 QCOMPARE(itemsChangedSpy
.count(), 1);
237 values
= m_model
->data(0);
238 QCOMPARE(values
.value("customRole1").toString(), QString("Test1"));
239 QCOMPARE(values
.value("customRole2").toString(), QString("Test2"));
240 QVERIFY(m_model
->isConsistent());
243 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
245 QTest::addColumn
<int>("changedIndex");
246 QTest::addColumn
<int>("changedRating");
247 QTest::addColumn
<bool>("expectMoveSignal");
248 QTest::addColumn
<int>("ratingIndex0");
249 QTest::addColumn
<int>("ratingIndex1");
250 QTest::addColumn
<int>("ratingIndex2");
253 // Index 0 = rating 2
254 // Index 1 = rating 4
255 // Index 2 = rating 6
257 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
258 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
259 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
261 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
262 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
263 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
266 void KFileItemModelTest::testSetDataWithModifiedSortRole()
268 QFETCH(int, changedIndex
);
269 QFETCH(int, changedRating
);
270 QFETCH(bool, expectMoveSignal
);
271 QFETCH(int, ratingIndex0
);
272 QFETCH(int, ratingIndex1
);
273 QFETCH(int, ratingIndex2
);
275 // Changing the value of a sort-role must result in
276 // a reordering of the items.
277 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
280 files
<< "a.txt" << "b.txt" << "c.txt";
281 m_testDir
->createFiles(files
);
283 m_model
->loadDirectory(m_testDir
->url());
284 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
286 // Fill the "rating" role of each file:
291 QHash
<QByteArray
, QVariant
> ratingA
;
292 ratingA
.insert("rating", 2);
293 m_model
->setData(0, ratingA
);
295 QHash
<QByteArray
, QVariant
> ratingB
;
296 ratingB
.insert("rating", 4);
297 m_model
->setData(1, ratingB
);
299 QHash
<QByteArray
, QVariant
> ratingC
;
300 ratingC
.insert("rating", 6);
301 m_model
->setData(2, ratingC
);
303 m_model
->setSortRole("rating");
304 QCOMPARE(m_model
->data(0).value("rating").toInt(), 2);
305 QCOMPARE(m_model
->data(1).value("rating").toInt(), 4);
306 QCOMPARE(m_model
->data(2).value("rating").toInt(), 6);
308 // Now change the rating from a.txt. This usually results
309 // in reordering of the items.
310 QHash
<QByteArray
, QVariant
> rating
;
311 rating
.insert("rating", changedRating
);
312 m_model
->setData(changedIndex
, rating
);
314 if (expectMoveSignal
) {
315 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)), DefaultTimeout
));
318 QCOMPARE(m_model
->data(0).value("rating").toInt(), ratingIndex0
);
319 QCOMPARE(m_model
->data(1).value("rating").toInt(), ratingIndex1
);
320 QCOMPARE(m_model
->data(2).value("rating").toInt(), ratingIndex2
);
321 QVERIFY(m_model
->isConsistent());
324 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
326 //QSKIP("Temporary disabled", SkipSingle);
328 // KFileItemModel prevents that inserting a punch of items sequentially
329 // results in an itemsInserted()-signal for each item. Instead internally
330 // a timeout is given that collects such operations and results in only
331 // one itemsInserted()-signal. However in this test we want to stress
332 // KFileItemModel to do a lot of insert operation and hence decrease
333 // the timeout to 1 millisecond.
334 m_testDir
->createFile("1");
335 m_model
->loadDirectory(m_testDir
->url());
336 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
337 QCOMPARE(m_model
->count(), 1);
339 // Insert 10 items for 20 times. After each insert operation the model consistency
341 QSet
<int> insertedItems
;
342 for (int i
= 0; i
< 20; ++i
) {
343 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
345 for (int j
= 0; j
< 10; ++j
) {
346 int itemName
= qrand();
347 while (insertedItems
.contains(itemName
)) {
350 insertedItems
.insert(itemName
);
352 m_testDir
->createFile(QString::number(itemName
));
355 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
356 if (spy
.count() == 0) {
357 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
360 QVERIFY(m_model
->isConsistent());
363 QCOMPARE(m_model
->count(), 201);
366 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
369 files
<< "B" << "E" << "G";
370 m_testDir
->createFiles(files
);
372 // Due to inserting the 3 items one item-range with index == 0 and
373 // count == 3 must be given
374 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
375 m_model
->loadDirectory(m_testDir
->url());
376 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
378 QCOMPARE(spy1
.count(), 1);
379 QList
<QVariant
> arguments
= spy1
.takeFirst();
380 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
381 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
383 // The indexes of the item-ranges must always be related to the model before
384 // the items have been inserted. Having:
387 // and inserting A, C, D, F the resulting model will be:
390 // and the item-ranges must be:
391 // index: 0, count: 1 for A
392 // index: 1, count: 2 for B, C
393 // index: 2, count: 1 for G
396 files
<< "A" << "C" << "D" << "F";
397 m_testDir
->createFiles(files
);
399 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
400 m_model
->m_dirLister
->updateDirectory(m_testDir
->url());
401 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
403 QCOMPARE(spy2
.count(), 1);
404 arguments
= spy2
.takeFirst();
405 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
406 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
409 void KFileItemModelTest::testExpandItems()
411 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
412 // Besides testing the basic item expansion functionality, the test makes sure that
413 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
414 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
415 // first three characters.
416 QSet
<QByteArray
> modelRoles
= m_model
->roles();
417 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
418 m_model
->setRoles(modelRoles
);
421 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
422 m_testDir
->createFiles(files
);
424 // Store the URLs of all folders in a set.
425 QSet
<KUrl
> allFolders
;
426 allFolders
<< KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
428 m_model
->loadDirectory(m_testDir
->url());
429 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
431 // So far, the model contains only "a/"
432 QCOMPARE(m_model
->count(), 1);
433 QVERIFY(m_model
->isExpandable(0));
434 QVERIFY(!m_model
->isExpanded(0));
435 QVERIFY(m_model
->expandedDirectories().empty());
437 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
439 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
440 m_model
->setExpanded(0, true);
441 QVERIFY(m_model
->isExpanded(0));
442 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
443 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
444 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a'));
446 QCOMPARE(spyInserted
.count(), 1);
447 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
448 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
450 QVERIFY(m_model
->isExpandable(1));
451 QVERIFY(!m_model
->isExpanded(1));
452 QVERIFY(m_model
->isExpandable(2));
453 QVERIFY(!m_model
->isExpanded(2));
455 // Expand the folder "a/a/" -> "a/a/1" becomes visible
456 m_model
->setExpanded(1, true);
457 QVERIFY(m_model
->isExpanded(1));
458 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
459 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
460 QCOMPARE(m_model
->expandedDirectories(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + 'a') << KUrl(m_testDir
->name() + "a/a"));
462 QCOMPARE(spyInserted
.count(), 1);
463 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
464 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
466 QVERIFY(!m_model
->isExpandable(2));
467 QVERIFY(!m_model
->isExpanded(2));
469 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
470 m_model
->setExpanded(3, true);
471 QVERIFY(m_model
->isExpanded(3));
472 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
473 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
474 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
476 QCOMPARE(spyInserted
.count(), 1);
477 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
478 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
480 QVERIFY(!m_model
->isExpandable(4));
481 QVERIFY(!m_model
->isExpanded(4));
483 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
485 // Collapse the top-level folder -> all other items should disappear
486 m_model
->setExpanded(0, false);
487 QVERIFY(!m_model
->isExpanded(0));
488 QCOMPARE(m_model
->count(), 1);
489 QVERIFY(!m_model
->expandedDirectories().contains(KUrl(m_testDir
->name() + 'a'))); // TODO: Make sure that child URLs are also removed
491 QCOMPARE(spyRemoved
.count(), 1);
492 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
493 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
494 QVERIFY(m_model
->isConsistent());
496 // Clear the model, reload the folder and try to restore the expanded folders.
498 QCOMPARE(m_model
->count(), 0);
499 QVERIFY(m_model
->expandedDirectories().empty());
501 m_model
->loadDirectory(m_testDir
->url());
502 m_model
->restoreExpandedDirectories(allFolders
);
503 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
504 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
505 QVERIFY(m_model
->isExpanded(0));
506 QVERIFY(m_model
->isExpanded(1));
507 QVERIFY(!m_model
->isExpanded(2));
508 QVERIFY(m_model
->isExpanded(3));
509 QVERIFY(!m_model
->isExpanded(4));
510 QCOMPARE(m_model
->expandedDirectories(), allFolders
);
511 QVERIFY(m_model
->isConsistent());
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_model
->loadDirectory(KUrl(m_testDir
->name() + "a/a/"));
516 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
517 QCOMPARE(m_model
->count(), 1); // 1 item: "1"
518 m_model
->restoreExpandedDirectories(allFolders
);
519 m_model
->loadDirectory(m_testDir
->url());
520 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), 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
->expandedDirectories(), 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_model
->loadDirectory(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
->expandedDirectories().empty());
550 // Expand the parents of "a2/b2/c2".
551 m_model
->expandParentDirectories(KUrl(m_testDir
->name() + "a2/b2/c2"));
552 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), 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
->expandParentDirectories(KUrl(m_testDir
->name() + "a 1/b1"));
564 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), 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));
574 QVERIFY(m_model
->isConsistent());
578 * Renaming an expanded folder by prepending its name with a dot makes it
579 * hidden. Verify that this does not cause an inconsistent model state and
580 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
582 void KFileItemModelTest::testMakeExpandedItemHidden()
584 QSet
<QByteArray
> modelRoles
= m_model
->roles();
585 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
586 m_model
->setRoles(modelRoles
);
589 m_testDir
->createFile("1a/2a/3a");
590 m_testDir
->createFile("1a/2a/3b");
591 m_testDir
->createFile("1a/2b");
592 m_testDir
->createFile("1b");
594 m_model
->loadDirectory(m_testDir
->url());
595 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
597 // So far, the model contains only "1a/" and "1b".
598 QCOMPARE(m_model
->count(), 2);
599 m_model
->setExpanded(0, true);
600 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
602 // Now "1a/2a" and "1a/2b" have appeared.
603 QCOMPARE(m_model
->count(), 4);
604 m_model
->setExpanded(1, true);
605 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
606 QCOMPARE(m_model
->count(), 6);
608 // Rename "1a/2" and make it hidden.
609 const QString oldPath
= m_model
->fileItem(0).url().path() + "/2a";
610 const QString newPath
= m_model
->fileItem(0).url().path() + "/.2a";
612 KIO::SimpleJob
* job
= KIO::rename(oldPath
, newPath
, KIO::HideProgressInfo
);
613 bool ok
= job
->exec();
615 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
617 // "1a/2" and its subfolders have disappeared now.
618 QVERIFY(m_model
->isConsistent());
619 QCOMPARE(m_model
->count(), 3);
621 m_model
->setExpanded(0, false);
622 QCOMPARE(m_model
->count(), 2);
626 void KFileItemModelTest::testSorting()
628 // Create some files with different sizes and modification times to check the different sorting options
629 QDateTime now
= QDateTime::currentDateTime();
631 QSet
<QByteArray
> roles
;
632 roles
.insert("text");
633 roles
.insert("isExpanded");
634 roles
.insert("isExpandable");
635 roles
.insert("expandedParentsCount");
636 m_model
->setRoles(roles
);
638 m_testDir
->createDir("c/c-2");
639 m_testDir
->createFile("c/c-2/c-3");
640 m_testDir
->createFile("c/c-1");
642 m_testDir
->createFile("a", "A file", now
.addDays(-3));
643 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
644 m_testDir
->createDir("c", now
.addDays(-2));
645 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
646 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
647 m_testDir
->createFile(".f");
649 m_model
->loadDirectory(m_testDir
->url());
650 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
652 int index
= m_model
->index(KUrl(m_testDir
->url().url() + 'c'));
653 m_model
->setExpanded(index
, true);
654 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
656 index
= m_model
->index(KUrl(m_testDir
->url().url() + "c/c-2"));
657 m_model
->setExpanded(index
, true);
658 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
660 // Default: Sort by Name, ascending
661 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
662 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
663 QVERIFY(m_model
->sortDirectoriesFirst());
664 QVERIFY(!m_model
->showHiddenFiles());
665 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
667 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
669 // Sort by Name, ascending, 'Sort Folders First' disabled
670 m_model
->setSortDirectoriesFirst(false);
671 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
672 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
673 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
674 QCOMPARE(spyItemsMoved
.count(), 1);
675 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
677 // Sort by Name, descending
678 m_model
->setSortDirectoriesFirst(true);
679 m_model
->setSortOrder(Qt::DescendingOrder
);
680 QCOMPARE(m_model
->sortRole(), QByteArray("text"));
681 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
682 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
683 QCOMPARE(spyItemsMoved
.count(), 2);
684 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
685 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
687 // Sort by Date, descending
688 m_model
->setSortDirectoriesFirst(true);
689 m_model
->setSortRole("date");
690 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
691 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
692 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
693 QCOMPARE(spyItemsMoved
.count(), 1);
694 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
696 // Sort by Date, ascending
697 m_model
->setSortOrder(Qt::AscendingOrder
);
698 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
699 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
700 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
701 QCOMPARE(spyItemsMoved
.count(), 1);
702 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
704 // Sort by Date, ascending, 'Sort Folders First' disabled
705 m_model
->setSortDirectoriesFirst(false);
706 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
707 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
708 QVERIFY(!m_model
->sortDirectoriesFirst());
709 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
710 QCOMPARE(spyItemsMoved
.count(), 1);
711 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
713 // Sort by Name, ascending, 'Sort Folders First' disabled
714 m_model
->setSortRole("text");
715 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
716 QVERIFY(!m_model
->sortDirectoriesFirst());
717 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
718 QCOMPARE(spyItemsMoved
.count(), 1);
719 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
721 // Sort by Size, ascending, 'Sort Folders First' disabled
722 m_model
->setSortRole("size");
723 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
724 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
725 QVERIFY(!m_model
->sortDirectoriesFirst());
726 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
727 QCOMPARE(spyItemsMoved
.count(), 1);
728 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
730 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
731 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
732 "another signal", SkipSingle
);
733 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
735 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
736 m_model
->setSortDirectoriesFirst(true);
737 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
738 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
739 QVERIFY(m_model
->sortDirectoriesFirst());
740 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
741 QCOMPARE(spyItemsMoved
.count(), 0);
743 // Sort by Size, descending, 'Sort Folders First' enabled
744 m_model
->setSortOrder(Qt::DescendingOrder
);
745 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
746 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
747 QVERIFY(m_model
->sortDirectoriesFirst());
748 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
749 QCOMPARE(spyItemsMoved
.count(), 1);
750 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
752 // TODO: Sort by other roles; show/hide hidden files
755 void KFileItemModelTest::testIndexForKeyboardSearch()
758 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
759 m_testDir
->createFiles(files
);
761 m_model
->loadDirectory(m_testDir
->url());
762 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
764 // Search from index 0
765 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
766 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
767 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
768 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
769 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
770 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
771 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
772 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
773 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
774 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
775 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
777 // Start a search somewhere in the middle
778 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
779 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
780 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
781 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
783 // Test searches that go past the last item back to index 0
784 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
785 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
786 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
787 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
789 // Test searches that yield no result
790 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
791 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
792 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
793 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
794 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
796 // Test upper case searches (note that search is case insensitive)
797 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
798 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
799 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
800 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
802 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
805 void KFileItemModelTest::testNameFilter()
808 files
<< "A1" << "A2" << "Abc" << "Bcd" << "Cde";
809 m_testDir
->createFiles(files
);
811 m_model
->loadDirectory(m_testDir
->url());
812 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
814 m_model
->setNameFilter("A"); // Shows A1, A2 and Abc
815 QCOMPARE(m_model
->count(), 3);
817 m_model
->setNameFilter("A2"); // Shows only A2
818 QCOMPARE(m_model
->count(), 1);
820 m_model
->setNameFilter("A2"); // Shows only A1
821 QCOMPARE(m_model
->count(), 1);
823 m_model
->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
824 QCOMPARE(m_model
->count(), 2);
826 m_model
->setNameFilter("bC"); // Shows "Abc" and "Bcd"
827 QCOMPARE(m_model
->count(), 2);
829 m_model
->setNameFilter(QString()); // Shows again all items
830 QCOMPARE(m_model
->count(), 5);
834 * Verifies that we do not crash when adding a KFileItem with an empty path.
835 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
836 * tried to always read the first character of the path, even if the path is empty.
838 void KFileItemModelTest::testEmptyPath()
840 QSet
<QByteArray
> roles
;
841 roles
.insert("text");
842 roles
.insert("isExpanded");
843 roles
.insert("isExpandable");
844 roles
.insert("expandedParentsCount");
845 m_model
->setRoles(roles
);
848 QVERIFY(emptyUrl
.path().isEmpty());
850 const KUrl
url("file:///test/");
853 items
<< KFileItem(emptyUrl
, QString(), KFileItem::Unknown
) << KFileItem(url
, QString(), KFileItem::Unknown
);
854 m_model
->slotItemsAdded(emptyUrl
, items
);
855 m_model
->slotCompleted();
859 * Verify that removing hidden files and folders from the model does not
860 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
862 void KFileItemModelTest::testRemoveHiddenItems()
864 m_testDir
->createDir(".a");
865 m_testDir
->createDir(".b");
866 m_testDir
->createDir("c");
867 m_testDir
->createDir("d");
868 m_testDir
->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
870 QSignalSpy
spyItemsInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
871 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
873 m_model
->setShowHiddenFiles(true);
874 m_model
->loadDirectory(m_testDir
->url());
875 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
876 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
877 QCOMPARE(spyItemsInserted
.count(), 1);
878 QCOMPARE(spyItemsRemoved
.count(), 0);
879 KItemRangeList itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
880 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
882 m_model
->setShowHiddenFiles(false);
883 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
884 QCOMPARE(spyItemsInserted
.count(), 0);
885 QCOMPARE(spyItemsRemoved
.count(), 1);
886 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
887 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
889 m_model
->setShowHiddenFiles(true);
890 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
891 QCOMPARE(spyItemsInserted
.count(), 1);
892 QCOMPARE(spyItemsRemoved
.count(), 0);
893 itemRangeList
= spyItemsInserted
.takeFirst().at(0).value
<KItemRangeList
>();
894 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
897 QCOMPARE(itemsInModel(), QStringList());
898 QCOMPARE(spyItemsInserted
.count(), 0);
899 QCOMPARE(spyItemsRemoved
.count(), 1);
900 itemRangeList
= spyItemsRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
901 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 8));
903 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
904 // Verify that this does not make the model crash.
905 m_model
->setShowHiddenFiles(false);
909 * Verify that filtered items are removed when their parent is collapsed.
911 void KFileItemModelTest::collapseParentOfHiddenItems()
913 QSet
<QByteArray
> modelRoles
= m_model
->roles();
914 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
915 m_model
->setRoles(modelRoles
);
918 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
919 m_testDir
->createFiles(files
);
921 m_model
->loadDirectory(m_testDir
->url());
922 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
923 QCOMPARE(m_model
->count(), 1); // Only "a/"
926 m_model
->setExpanded(0, true);
927 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
928 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
931 m_model
->setExpanded(1, true);
932 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
933 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
936 m_model
->setExpanded(2, true);
937 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
938 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"
940 // Set a name filter that matches nothing -> only the expanded folders remain.
941 m_model
->setNameFilter("xyz");
942 QCOMPARE(m_model
->count(), 3);
943 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
945 // Collapse the folder "a/".
946 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
947 m_model
->setExpanded(0, false);
948 QCOMPARE(spyItemsRemoved
.count(), 1);
949 QCOMPARE(m_model
->count(), 1);
950 QCOMPARE(itemsInModel(), QStringList() << "a");
952 // Remove the filter -> no files should appear (and we should not get a crash).
953 m_model
->setNameFilter(QString());
954 QCOMPARE(m_model
->count(), 1);
958 * Verify that filtered items are removed when their parent is deleted.
960 void KFileItemModelTest::removeParentOfHiddenItems()
962 QSet
<QByteArray
> modelRoles
= m_model
->roles();
963 modelRoles
<< "isExpanded" << "isExpandable" << "expandedParentsCount";
964 m_model
->setRoles(modelRoles
);
967 files
<< "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
968 m_testDir
->createFiles(files
);
970 m_model
->loadDirectory(m_testDir
->url());
971 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
972 QCOMPARE(m_model
->count(), 1); // Only "a/"
975 m_model
->setExpanded(0, true);
976 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
977 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/b/", "a/1"
980 m_model
->setExpanded(1, true);
981 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
982 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
985 m_model
->setExpanded(2, true);
986 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
987 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"
989 // Set a name filter that matches nothing -> only the expanded folders remain.
990 m_model
->setNameFilter("xyz");
991 QCOMPARE(m_model
->count(), 3);
992 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
994 // Simulate the deletion of the directory "a/b/".
995 QSignalSpy
spyItemsRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
996 m_model
->slotItemsDeleted(KFileItemList() << m_model
->fileItem(1));
997 QCOMPARE(spyItemsRemoved
.count(), 1);
998 QCOMPARE(m_model
->count(), 1);
999 QCOMPARE(itemsInModel(), QStringList() << "a");
1001 // Remove the filter -> only the file "a/1" should appear.
1002 m_model
->setNameFilter(QString());
1003 QCOMPARE(m_model
->count(), 2);
1004 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1007 QStringList
KFileItemModelTest::itemsInModel() const
1010 for (int i
= 0; i
< m_model
->count(); i
++) {
1011 items
<< m_model
->data(i
).value("text").toString();
1016 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
1018 #include "kfileitemmodeltest.moc"