]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / tests / kfileitemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include <qtest_kde.h>
22
23 #include <KDirLister>
24 #include <kio/job.h>
25
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
28 #include "testdir.h"
29
30 void myMessageOutput(QtMsgType type, const char* msg)
31 {
32 switch (type) {
33 case QtDebugMsg:
34 break;
35 case QtWarningMsg:
36 break;
37 case QtCriticalMsg:
38 fprintf(stderr, "Critical: %s\n", msg);
39 break;
40 case QtFatalMsg:
41 fprintf(stderr, "Fatal: %s\n", msg);
42 abort();
43 default:
44 break;
45 }
46 }
47
48 namespace {
49 const int DefaultTimeout = 5000;
50 };
51
52 Q_DECLARE_METATYPE(KItemRangeList)
53 Q_DECLARE_METATYPE(QList<int>)
54
55 class KFileItemModelTest : public QObject
56 {
57 Q_OBJECT
58
59 private slots:
60 void init();
61 void cleanup();
62
63 void testDefaultRoles();
64 void testDefaultSortRole();
65 void testDefaultGroupedSorting();
66 void testNewItems();
67 void testRemoveItems();
68 void testDirLoadingCompleted();
69 void testSetData();
70 void testSetDataWithModifiedSortRole_data();
71 void testSetDataWithModifiedSortRole();
72 void testModelConsistencyWhenInsertingItems();
73 void testItemRangeConsistencyWhenInsertingItems();
74 void testExpandItems();
75 void testExpandParentItems();
76 void testMakeExpandedItemHidden();
77 void testSorting();
78 void testIndexForKeyboardSearch();
79 void testNameFilter();
80 void testEmptyPath();
81 void testRemoveHiddenItems();
82 void collapseParentOfHiddenItems();
83 void removeParentOfHiddenItems();
84
85 private:
86 QStringList itemsInModel() const;
87
88 private:
89 KFileItemModel* m_model;
90 TestDir* m_testDir;
91 };
92
93 void KFileItemModelTest::init()
94 {
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);
98
99 qRegisterMetaType<KItemRange>("KItemRange");
100 qRegisterMetaType<KItemRangeList>("KItemRangeList");
101 qRegisterMetaType<KFileItemList>("KFileItemList");
102
103 m_testDir = new TestDir();
104 m_model = new KFileItemModel();
105 m_model->m_dirLister->setAutoUpdate(false);
106 }
107
108 void KFileItemModelTest::cleanup()
109 {
110 delete m_model;
111 m_model = 0;
112
113 delete m_testDir;
114 m_testDir = 0;
115 }
116
117 void KFileItemModelTest::testDefaultRoles()
118 {
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"));
124 }
125
126 void KFileItemModelTest::testDefaultSortRole()
127 {
128 QCOMPARE(m_model->sortRole(), QByteArray("text"));
129
130 QStringList files;
131 files << "c.txt" << "a.txt" << "b.txt";
132
133 m_testDir->createFiles(files);
134
135 m_model->loadDirectory(m_testDir->url());
136 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
137
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"));
142 }
143
144 void KFileItemModelTest::testDefaultGroupedSorting()
145 {
146 QCOMPARE(m_model->groupedSorting(), false);
147 }
148
149 void KFileItemModelTest::testNewItems()
150 {
151 QStringList files;
152 files << "a.txt" << "b.txt" << "c.txt";
153 m_testDir->createFiles(files);
154
155 m_model->loadDirectory(m_testDir->url());
156 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
157
158 QCOMPARE(m_model->count(), 3);
159
160 QVERIFY(m_model->isConsistent());
161 }
162
163 void KFileItemModelTest::testRemoveItems()
164 {
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());
171
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());
177 }
178
179 void KFileItemModelTest::testDirLoadingCompleted()
180 {
181 QSignalSpy loadingCompletedSpy(m_model, SIGNAL(directoryLoadingCompleted()));
182 QSignalSpy itemsInsertedSpy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
183 QSignalSpy itemsRemovedSpy(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
184
185 m_testDir->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
186
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);
193
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);
201
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);
210
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);
218
219 QVERIFY(m_model->isConsistent());
220 }
221
222 void KFileItemModelTest::testSetData()
223 {
224 m_testDir->createFile("a.txt");
225
226 m_model->loadDirectory(m_testDir->url());
227 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
228
229 QHash<QByteArray, QVariant> values;
230 values.insert("customRole1", "Test1");
231 values.insert("customRole2", "Test2");
232
233 QSignalSpy itemsChangedSpy(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)));
234 m_model->setData(0, values);
235 QCOMPARE(itemsChangedSpy.count(), 1);
236
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());
241 }
242
243 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
244 {
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");
251
252 // Default setup:
253 // Index 0 = rating 2
254 // Index 1 = rating 4
255 // Index 2 = rating 6
256
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;
260
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;
264 }
265
266 void KFileItemModelTest::testSetDataWithModifiedSortRole()
267 {
268 QFETCH(int, changedIndex);
269 QFETCH(int, changedRating);
270 QFETCH(bool, expectMoveSignal);
271 QFETCH(int, ratingIndex0);
272 QFETCH(int, ratingIndex1);
273 QFETCH(int, ratingIndex2);
274
275 // Changing the value of a sort-role must result in
276 // a reordering of the items.
277 QCOMPARE(m_model->sortRole(), QByteArray("text"));
278
279 QStringList files;
280 files << "a.txt" << "b.txt" << "c.txt";
281 m_testDir->createFiles(files);
282
283 m_model->loadDirectory(m_testDir->url());
284 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
285
286 // Fill the "rating" role of each file:
287 // a.txt -> 2
288 // b.txt -> 4
289 // c.txt -> 6
290
291 QHash<QByteArray, QVariant> ratingA;
292 ratingA.insert("rating", 2);
293 m_model->setData(0, ratingA);
294
295 QHash<QByteArray, QVariant> ratingB;
296 ratingB.insert("rating", 4);
297 m_model->setData(1, ratingB);
298
299 QHash<QByteArray, QVariant> ratingC;
300 ratingC.insert("rating", 6);
301 m_model->setData(2, ratingC);
302
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);
307
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);
313
314 if (expectMoveSignal) {
315 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)), DefaultTimeout));
316 }
317
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());
322 }
323
324 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
325 {
326 //QSKIP("Temporary disabled", SkipSingle);
327
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);
338
339 // Insert 10 items for 20 times. After each insert operation the model consistency
340 // is checked.
341 QSet<int> insertedItems;
342 for (int i = 0; i < 20; ++i) {
343 QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
344
345 for (int j = 0; j < 10; ++j) {
346 int itemName = qrand();
347 while (insertedItems.contains(itemName)) {
348 itemName = qrand();
349 }
350 insertedItems.insert(itemName);
351
352 m_testDir->createFile(QString::number(itemName));
353 }
354
355 m_model->m_dirLister->updateDirectory(m_testDir->url());
356 if (spy.count() == 0) {
357 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
358 }
359
360 QVERIFY(m_model->isConsistent());
361 }
362
363 QCOMPARE(m_model->count(), 201);
364 }
365
366 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
367 {
368 QStringList files;
369 files << "B" << "E" << "G";
370 m_testDir->createFiles(files);
371
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));
377
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));
382
383 // The indexes of the item-ranges must always be related to the model before
384 // the items have been inserted. Having:
385 // 0 1 2
386 // B E G
387 // and inserting A, C, D, F the resulting model will be:
388 // 0 1 2 3 4 5 6
389 // A B C D E F G
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
394
395 files.clear();
396 files << "A" << "C" << "D" << "F";
397 m_testDir->createFiles(files);
398
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));
402
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));
407 }
408
409 void KFileItemModelTest::testExpandItems()
410 {
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);
419
420 QStringList files;
421 files << "a/a/1" << "a/a-1/1"; // missing folders are created automatically
422 m_testDir->createFiles(files);
423
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");
427
428 m_model->loadDirectory(m_testDir->url());
429 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
430
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());
436
437 QSignalSpy spyInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
438
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'));
445
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
449
450 QVERIFY(m_model->isExpandable(1));
451 QVERIFY(!m_model->isExpanded(1));
452 QVERIFY(m_model->isExpandable(2));
453 QVERIFY(!m_model->isExpanded(2));
454
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"));
461
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
465
466 QVERIFY(!m_model->isExpandable(2));
467 QVERIFY(!m_model->isExpanded(2));
468
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);
475
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
479
480 QVERIFY(!m_model->isExpandable(4));
481 QVERIFY(!m_model->isExpanded(4));
482
483 QSignalSpy spyRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
484
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
490
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());
495
496 // Clear the model, reload the folder and try to restore the expanded folders.
497 m_model->clear();
498 QCOMPARE(m_model->count(), 0);
499 QVERIFY(m_model->expandedDirectories().empty());
500
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());
512
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);
523 }
524
525 void KFileItemModelTest::testExpandParentItems()
526 {
527 // Create a tree structure of folders:
528 // a 1/
529 // a 1/b1/
530 // a 1/b1/c1/
531 // a2/
532 // a2/b2/
533 // a2/b2/c2/
534 // a2/b2/c2/d2/
535 QSet<QByteArray> modelRoles = m_model->roles();
536 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
537 m_model->setRoles(modelRoles);
538
539 QStringList files;
540 files << "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
541 m_testDir->createFiles(files);
542
543 m_model->loadDirectory(m_testDir->url());
544 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
545
546 // So far, the model contains only "a 1/" and "a2/".
547 QCOMPARE(m_model->count(), 2);
548 QVERIFY(m_model->expandedDirectories().empty());
549
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));
553
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));
561
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));
565
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());
575 }
576
577 /**
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
581 */
582 void KFileItemModelTest::testMakeExpandedItemHidden()
583 {
584 QSet<QByteArray> modelRoles = m_model->roles();
585 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
586 m_model->setRoles(modelRoles);
587
588 QStringList files;
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");
593
594 m_model->loadDirectory(m_testDir->url());
595 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
596
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));
601
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);
607
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";
611
612 KIO::SimpleJob* job = KIO::rename(oldPath, newPath, KIO::HideProgressInfo);
613 bool ok = job->exec();
614 QVERIFY(ok);
615 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
616
617 // "1a/2" and its subfolders have disappeared now.
618 QVERIFY(m_model->isConsistent());
619 QCOMPARE(m_model->count(), 3);
620
621 m_model->setExpanded(0, false);
622 QCOMPARE(m_model->count(), 2);
623
624 }
625
626 void KFileItemModelTest::testSorting()
627 {
628 // Create some files with different sizes and modification times to check the different sorting options
629 QDateTime now = QDateTime::currentDateTime();
630
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);
637
638 m_testDir->createDir("c/c-2");
639 m_testDir->createFile("c/c-2/c-3");
640 m_testDir->createFile("c/c-1");
641
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");
648
649 m_model->loadDirectory(m_testDir->url());
650 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
651
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));
655
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));
659
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");
666
667 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
668
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);
676
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);
686
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);
695
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);
703
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);
712
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);
720
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);
729
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.
734
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);
742
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);
751
752 // TODO: Sort by other roles; show/hide hidden files
753 }
754
755 void KFileItemModelTest::testIndexForKeyboardSearch()
756 {
757 QStringList files;
758 files << "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
759 m_testDir->createFiles(files);
760
761 m_model->loadDirectory(m_testDir->url());
762 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
763
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);
776
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);
782
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);
788
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);
795
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);
801
802 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
803 }
804
805 void KFileItemModelTest::testNameFilter()
806 {
807 QStringList files;
808 files << "A1" << "A2" << "Abc" << "Bcd" << "Cde";
809 m_testDir->createFiles(files);
810
811 m_model->loadDirectory(m_testDir->url());
812 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
813
814 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
815 QCOMPARE(m_model->count(), 3);
816
817 m_model->setNameFilter("A2"); // Shows only A2
818 QCOMPARE(m_model->count(), 1);
819
820 m_model->setNameFilter("A2"); // Shows only A1
821 QCOMPARE(m_model->count(), 1);
822
823 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
824 QCOMPARE(m_model->count(), 2);
825
826 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
827 QCOMPARE(m_model->count(), 2);
828
829 m_model->setNameFilter(QString()); // Shows again all items
830 QCOMPARE(m_model->count(), 5);
831 }
832
833 /**
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.
837 */
838 void KFileItemModelTest::testEmptyPath()
839 {
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);
846
847 const KUrl emptyUrl;
848 QVERIFY(emptyUrl.path().isEmpty());
849
850 const KUrl url("file:///test/");
851
852 KFileItemList items;
853 items << KFileItem(emptyUrl, QString(), KFileItem::Unknown) << KFileItem(url, QString(), KFileItem::Unknown);
854 m_model->slotItemsAdded(emptyUrl, items);
855 m_model->slotCompleted();
856 }
857
858 /**
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
861 */
862 void KFileItemModelTest::testRemoveHiddenItems()
863 {
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");
869
870 QSignalSpy spyItemsInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
871 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
872
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));
881
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));
888
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));
895
896 m_model->clear();
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));
902
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);
906 }
907
908 /**
909 * Verify that filtered items are removed when their parent is collapsed.
910 */
911 void KFileItemModelTest::collapseParentOfHiddenItems()
912 {
913 QSet<QByteArray> modelRoles = m_model->roles();
914 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
915 m_model->setRoles(modelRoles);
916
917 QStringList files;
918 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
919 m_testDir->createFiles(files);
920
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/"
924
925 // Expand "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"
929
930 // Expand "a/b/".
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"
934
935 // Expand "a/b/c/".
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"
939
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");
944
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");
951
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);
955 }
956
957 /**
958 * Verify that filtered items are removed when their parent is deleted.
959 */
960 void KFileItemModelTest::removeParentOfHiddenItems()
961 {
962 QSet<QByteArray> modelRoles = m_model->roles();
963 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
964 m_model->setRoles(modelRoles);
965
966 QStringList files;
967 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
968 m_testDir->createFiles(files);
969
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/"
973
974 // Expand "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"
978
979 // Expand "a/b/".
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"
983
984 // Expand "a/b/c/".
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"
988
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");
993
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");
1000
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");
1005 }
1006
1007 QStringList KFileItemModelTest::itemsInModel() const
1008 {
1009 QStringList items;
1010 for (int i = 0; i < m_model->count(); i++) {
1011 items << m_model->data(i).value("text").toString();
1012 }
1013 return items;
1014 }
1015
1016 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
1017
1018 #include "kfileitemmodeltest.moc"