]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Fix crash when dropping URLs on the URL navigator's drop down menus
[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 testChangeSortRole();
73 void testModelConsistencyWhenInsertingItems();
74 void testItemRangeConsistencyWhenInsertingItems();
75 void testExpandItems();
76 void testExpandParentItems();
77 void testMakeExpandedItemHidden();
78 void testSorting();
79 void testIndexForKeyboardSearch();
80 void testNameFilter();
81 void testEmptyPath();
82 void testRefreshExpandedItem();
83 void testRemoveHiddenItems();
84 void collapseParentOfHiddenItems();
85 void removeParentOfHiddenItems();
86 void testGeneralParentChildRelationships();
87
88 private:
89 QStringList itemsInModel() const;
90
91 private:
92 KFileItemModel* m_model;
93 TestDir* m_testDir;
94 };
95
96 void KFileItemModelTest::init()
97 {
98 // The item-model tests result in a huge number of debugging
99 // output from kdelibs. Only show critical and fatal messages.
100 qInstallMsgHandler(myMessageOutput);
101
102 qRegisterMetaType<KItemRange>("KItemRange");
103 qRegisterMetaType<KItemRangeList>("KItemRangeList");
104 qRegisterMetaType<KFileItemList>("KFileItemList");
105
106 m_testDir = new TestDir();
107 m_model = new KFileItemModel();
108 m_model->m_dirLister->setAutoUpdate(false);
109 }
110
111 void KFileItemModelTest::cleanup()
112 {
113 delete m_model;
114 m_model = 0;
115
116 delete m_testDir;
117 m_testDir = 0;
118 }
119
120 void KFileItemModelTest::testDefaultRoles()
121 {
122 const QSet<QByteArray> roles = m_model->roles();
123 QCOMPARE(roles.count(), 3);
124 QVERIFY(roles.contains("text"));
125 QVERIFY(roles.contains("isDir"));
126 QVERIFY(roles.contains("isLink"));
127 }
128
129 void KFileItemModelTest::testDefaultSortRole()
130 {
131 QCOMPARE(m_model->sortRole(), QByteArray("text"));
132
133 QStringList files;
134 files << "c.txt" << "a.txt" << "b.txt";
135
136 m_testDir->createFiles(files);
137
138 m_model->loadDirectory(m_testDir->url());
139 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
140
141 QCOMPARE(m_model->count(), 3);
142 QCOMPARE(m_model->data(0)["text"].toString(), QString("a.txt"));
143 QCOMPARE(m_model->data(1)["text"].toString(), QString("b.txt"));
144 QCOMPARE(m_model->data(2)["text"].toString(), QString("c.txt"));
145 }
146
147 void KFileItemModelTest::testDefaultGroupedSorting()
148 {
149 QCOMPARE(m_model->groupedSorting(), false);
150 }
151
152 void KFileItemModelTest::testNewItems()
153 {
154 QStringList files;
155 files << "a.txt" << "b.txt" << "c.txt";
156 m_testDir->createFiles(files);
157
158 m_model->loadDirectory(m_testDir->url());
159 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
160
161 QCOMPARE(m_model->count(), 3);
162
163 QVERIFY(m_model->isConsistent());
164 }
165
166 void KFileItemModelTest::testRemoveItems()
167 {
168 m_testDir->createFile("a.txt");
169 m_testDir->createFile("b.txt");
170 m_model->loadDirectory(m_testDir->url());
171 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
172 QCOMPARE(m_model->count(), 2);
173 QVERIFY(m_model->isConsistent());
174
175 m_testDir->removeFile("a.txt");
176 m_model->m_dirLister->updateDirectory(m_testDir->url());
177 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
178 QCOMPARE(m_model->count(), 1);
179 QVERIFY(m_model->isConsistent());
180 }
181
182 void KFileItemModelTest::testDirLoadingCompleted()
183 {
184 QSignalSpy loadingCompletedSpy(m_model, SIGNAL(directoryLoadingCompleted()));
185 QSignalSpy itemsInsertedSpy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
186 QSignalSpy itemsRemovedSpy(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
187
188 m_testDir->createFiles(QStringList() << "a.txt" << "b.txt" << "c.txt");
189
190 m_model->loadDirectory(m_testDir->url());
191 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
192 QCOMPARE(loadingCompletedSpy.count(), 1);
193 QCOMPARE(itemsInsertedSpy.count(), 1);
194 QCOMPARE(itemsRemovedSpy.count(), 0);
195 QCOMPARE(m_model->count(), 3);
196
197 m_testDir->createFiles(QStringList() << "d.txt" << "e.txt");
198 m_model->m_dirLister->updateDirectory(m_testDir->url());
199 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
200 QCOMPARE(loadingCompletedSpy.count(), 2);
201 QCOMPARE(itemsInsertedSpy.count(), 2);
202 QCOMPARE(itemsRemovedSpy.count(), 0);
203 QCOMPARE(m_model->count(), 5);
204
205 m_testDir->removeFile("a.txt");
206 m_testDir->createFile("f.txt");
207 m_model->m_dirLister->updateDirectory(m_testDir->url());
208 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
209 QCOMPARE(loadingCompletedSpy.count(), 3);
210 QCOMPARE(itemsInsertedSpy.count(), 3);
211 QCOMPARE(itemsRemovedSpy.count(), 1);
212 QCOMPARE(m_model->count(), 5);
213
214 m_testDir->removeFile("b.txt");
215 m_model->m_dirLister->updateDirectory(m_testDir->url());
216 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
217 QCOMPARE(loadingCompletedSpy.count(), 4);
218 QCOMPARE(itemsInsertedSpy.count(), 3);
219 QCOMPARE(itemsRemovedSpy.count(), 2);
220 QCOMPARE(m_model->count(), 4);
221
222 QVERIFY(m_model->isConsistent());
223 }
224
225 void KFileItemModelTest::testSetData()
226 {
227 m_testDir->createFile("a.txt");
228
229 m_model->loadDirectory(m_testDir->url());
230 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
231
232 QHash<QByteArray, QVariant> values;
233 values.insert("customRole1", "Test1");
234 values.insert("customRole2", "Test2");
235
236 QSignalSpy itemsChangedSpy(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)));
237 m_model->setData(0, values);
238 QCOMPARE(itemsChangedSpy.count(), 1);
239
240 values = m_model->data(0);
241 QCOMPARE(values.value("customRole1").toString(), QString("Test1"));
242 QCOMPARE(values.value("customRole2").toString(), QString("Test2"));
243 QVERIFY(m_model->isConsistent());
244 }
245
246 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
247 {
248 QTest::addColumn<int>("changedIndex");
249 QTest::addColumn<int>("changedRating");
250 QTest::addColumn<bool>("expectMoveSignal");
251 QTest::addColumn<int>("ratingIndex0");
252 QTest::addColumn<int>("ratingIndex1");
253 QTest::addColumn<int>("ratingIndex2");
254
255 // Default setup:
256 // Index 0 = rating 2
257 // Index 1 = rating 4
258 // Index 2 = rating 6
259
260 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
261 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
262 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
263
264 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
265 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
266 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
267 }
268
269 void KFileItemModelTest::testSetDataWithModifiedSortRole()
270 {
271 QFETCH(int, changedIndex);
272 QFETCH(int, changedRating);
273 QFETCH(bool, expectMoveSignal);
274 QFETCH(int, ratingIndex0);
275 QFETCH(int, ratingIndex1);
276 QFETCH(int, ratingIndex2);
277
278 // Changing the value of a sort-role must result in
279 // a reordering of the items.
280 QCOMPARE(m_model->sortRole(), QByteArray("text"));
281 m_model->setSortRole("rating");
282 QCOMPARE(m_model->sortRole(), QByteArray("rating"));
283
284 QStringList files;
285 files << "a.txt" << "b.txt" << "c.txt";
286 m_testDir->createFiles(files);
287
288 m_model->loadDirectory(m_testDir->url());
289 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
290
291 // Fill the "rating" role of each file:
292 // a.txt -> 2
293 // b.txt -> 4
294 // c.txt -> 6
295
296 QHash<QByteArray, QVariant> ratingA;
297 ratingA.insert("rating", 2);
298 m_model->setData(0, ratingA);
299
300 QHash<QByteArray, QVariant> ratingB;
301 ratingB.insert("rating", 4);
302 m_model->setData(1, ratingB);
303
304 QHash<QByteArray, QVariant> ratingC;
305 ratingC.insert("rating", 6);
306 m_model->setData(2, ratingC);
307
308 QCOMPARE(m_model->data(0).value("rating").toInt(), 2);
309 QCOMPARE(m_model->data(1).value("rating").toInt(), 4);
310 QCOMPARE(m_model->data(2).value("rating").toInt(), 6);
311
312 // Now change the rating from a.txt. This usually results
313 // in reordering of the items.
314 QHash<QByteArray, QVariant> rating;
315 rating.insert("rating", changedRating);
316 m_model->setData(changedIndex, rating);
317
318 if (expectMoveSignal) {
319 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)), DefaultTimeout));
320 }
321
322 QCOMPARE(m_model->data(0).value("rating").toInt(), ratingIndex0);
323 QCOMPARE(m_model->data(1).value("rating").toInt(), ratingIndex1);
324 QCOMPARE(m_model->data(2).value("rating").toInt(), ratingIndex2);
325 QVERIFY(m_model->isConsistent());
326 }
327
328 void KFileItemModelTest::testChangeSortRole()
329 {
330 QCOMPARE(m_model->sortRole(), QByteArray("text"));
331
332 QStringList files;
333 files << "a.txt" << "b.jpg" << "c.txt";
334 m_testDir->createFiles(files);
335
336 m_model->loadDirectory(m_testDir->url());
337 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
338 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
339
340 // Simulate that KFileItemModelRolesUpdater determines the mime type.
341 // Resorting the files by 'type' will only work immediately if their
342 // mime types are known.
343 for (int index = 0; index < m_model->count(); ++index) {
344 m_model->fileItem(index).determineMimeType();
345 }
346
347 // Now: sort by type.
348 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
349 m_model->setSortRole("type");
350 QCOMPARE(m_model->sortRole(), QByteArray("type"));
351 QVERIFY(!spyItemsMoved.isEmpty());
352
353 // The actual order of the files might depend on the translation of the
354 // result of KFileItem::mimeComment() in the user's language.
355 QStringList version1;
356 version1 << "b.jpg" << "a.txt" << "c.txt";
357
358 QStringList version2;
359 version2 << "a.txt" << "c.txt" << "b.jpg";
360
361 const bool ok1 = (itemsInModel() == version1);
362 const bool ok2 = (itemsInModel() == version2);
363
364 QVERIFY(ok1 || ok2);
365 }
366
367 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
368 {
369 //QSKIP("Temporary disabled", SkipSingle);
370
371 // KFileItemModel prevents that inserting a punch of items sequentially
372 // results in an itemsInserted()-signal for each item. Instead internally
373 // a timeout is given that collects such operations and results in only
374 // one itemsInserted()-signal. However in this test we want to stress
375 // KFileItemModel to do a lot of insert operation and hence decrease
376 // the timeout to 1 millisecond.
377 m_testDir->createFile("1");
378 m_model->loadDirectory(m_testDir->url());
379 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
380 QCOMPARE(m_model->count(), 1);
381
382 // Insert 10 items for 20 times. After each insert operation the model consistency
383 // is checked.
384 QSet<int> insertedItems;
385 for (int i = 0; i < 20; ++i) {
386 QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
387
388 for (int j = 0; j < 10; ++j) {
389 int itemName = qrand();
390 while (insertedItems.contains(itemName)) {
391 itemName = qrand();
392 }
393 insertedItems.insert(itemName);
394
395 m_testDir->createFile(QString::number(itemName));
396 }
397
398 m_model->m_dirLister->updateDirectory(m_testDir->url());
399 if (spy.count() == 0) {
400 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
401 }
402
403 QVERIFY(m_model->isConsistent());
404 }
405
406 QCOMPARE(m_model->count(), 201);
407 }
408
409 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
410 {
411 QStringList files;
412 files << "B" << "E" << "G";
413 m_testDir->createFiles(files);
414
415 // Due to inserting the 3 items one item-range with index == 0 and
416 // count == 3 must be given
417 QSignalSpy spy1(m_model, SIGNAL(itemsInserted(KItemRangeList)));
418 m_model->loadDirectory(m_testDir->url());
419 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
420
421 QCOMPARE(spy1.count(), 1);
422 QList<QVariant> arguments = spy1.takeFirst();
423 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
424 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 3));
425
426 // The indexes of the item-ranges must always be related to the model before
427 // the items have been inserted. Having:
428 // 0 1 2
429 // B E G
430 // and inserting A, C, D, F the resulting model will be:
431 // 0 1 2 3 4 5 6
432 // A B C D E F G
433 // and the item-ranges must be:
434 // index: 0, count: 1 for A
435 // index: 1, count: 2 for B, C
436 // index: 2, count: 1 for G
437
438 files.clear();
439 files << "A" << "C" << "D" << "F";
440 m_testDir->createFiles(files);
441
442 QSignalSpy spy2(m_model, SIGNAL(itemsInserted(KItemRangeList)));
443 m_model->m_dirLister->updateDirectory(m_testDir->url());
444 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
445
446 QCOMPARE(spy2.count(), 1);
447 arguments = spy2.takeFirst();
448 itemRangeList = arguments.at(0).value<KItemRangeList>();
449 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
450 }
451
452 void KFileItemModelTest::testExpandItems()
453 {
454 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
455 // Besides testing the basic item expansion functionality, the test makes sure that
456 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
457 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
458 // first three characters.
459 QSet<QByteArray> modelRoles = m_model->roles();
460 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
461 m_model->setRoles(modelRoles);
462
463 QStringList files;
464 files << "a/a/1" << "a/a-1/1"; // missing folders are created automatically
465 m_testDir->createFiles(files);
466
467 // Store the URLs of all folders in a set.
468 QSet<KUrl> allFolders;
469 allFolders << KUrl(m_testDir->name() + 'a') << KUrl(m_testDir->name() + "a/a") << KUrl(m_testDir->name() + "a/a-1");
470
471 m_model->loadDirectory(m_testDir->url());
472 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
473
474 // So far, the model contains only "a/"
475 QCOMPARE(m_model->count(), 1);
476 QVERIFY(m_model->isExpandable(0));
477 QVERIFY(!m_model->isExpanded(0));
478 QVERIFY(m_model->expandedDirectories().empty());
479
480 QSignalSpy spyInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
481
482 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
483 m_model->setExpanded(0, true);
484 QVERIFY(m_model->isExpanded(0));
485 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
486 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
487 QCOMPARE(m_model->expandedDirectories(), QSet<KUrl>() << KUrl(m_testDir->name() + 'a'));
488
489 QCOMPARE(spyInserted.count(), 1);
490 KItemRangeList itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
491 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
492
493 QVERIFY(m_model->isExpandable(1));
494 QVERIFY(!m_model->isExpanded(1));
495 QVERIFY(m_model->isExpandable(2));
496 QVERIFY(!m_model->isExpanded(2));
497
498 // Expand the folder "a/a/" -> "a/a/1" becomes visible
499 m_model->setExpanded(1, true);
500 QVERIFY(m_model->isExpanded(1));
501 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
502 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
503 QCOMPARE(m_model->expandedDirectories(), QSet<KUrl>() << KUrl(m_testDir->name() + 'a') << KUrl(m_testDir->name() + "a/a"));
504
505 QCOMPARE(spyInserted.count(), 1);
506 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
507 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
508
509 QVERIFY(!m_model->isExpandable(2));
510 QVERIFY(!m_model->isExpanded(2));
511
512 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
513 m_model->setExpanded(3, true);
514 QVERIFY(m_model->isExpanded(3));
515 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
516 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
517 QCOMPARE(m_model->expandedDirectories(), allFolders);
518
519 QCOMPARE(spyInserted.count(), 1);
520 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
521 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
522
523 QVERIFY(!m_model->isExpandable(4));
524 QVERIFY(!m_model->isExpanded(4));
525
526 QSignalSpy spyRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
527
528 // Collapse the top-level folder -> all other items should disappear
529 m_model->setExpanded(0, false);
530 QVERIFY(!m_model->isExpanded(0));
531 QCOMPARE(m_model->count(), 1);
532 QVERIFY(!m_model->expandedDirectories().contains(KUrl(m_testDir->name() + 'a'))); // TODO: Make sure that child URLs are also removed
533
534 QCOMPARE(spyRemoved.count(), 1);
535 itemRangeList = spyRemoved.takeFirst().at(0).value<KItemRangeList>();
536 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
537 QVERIFY(m_model->isConsistent());
538
539 // Clear the model, reload the folder and try to restore the expanded folders.
540 m_model->clear();
541 QCOMPARE(m_model->count(), 0);
542 QVERIFY(m_model->expandedDirectories().empty());
543
544 m_model->loadDirectory(m_testDir->url());
545 m_model->restoreExpandedDirectories(allFolders);
546 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
547 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
548 QVERIFY(m_model->isExpanded(0));
549 QVERIFY(m_model->isExpanded(1));
550 QVERIFY(!m_model->isExpanded(2));
551 QVERIFY(m_model->isExpanded(3));
552 QVERIFY(!m_model->isExpanded(4));
553 QCOMPARE(m_model->expandedDirectories(), allFolders);
554 QVERIFY(m_model->isConsistent());
555
556 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
557 // This is how DolphinView restores the expanded folders when navigating in history.
558 m_model->loadDirectory(KUrl(m_testDir->name() + "a/a/"));
559 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
560 QCOMPARE(m_model->count(), 1); // 1 item: "1"
561 m_model->restoreExpandedDirectories(allFolders);
562 m_model->loadDirectory(m_testDir->url());
563 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
564 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
565 QCOMPARE(m_model->expandedDirectories(), allFolders);
566 }
567
568 void KFileItemModelTest::testExpandParentItems()
569 {
570 // Create a tree structure of folders:
571 // a 1/
572 // a 1/b1/
573 // a 1/b1/c1/
574 // a2/
575 // a2/b2/
576 // a2/b2/c2/
577 // a2/b2/c2/d2/
578 QSet<QByteArray> modelRoles = m_model->roles();
579 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
580 m_model->setRoles(modelRoles);
581
582 QStringList files;
583 files << "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
584 m_testDir->createFiles(files);
585
586 m_model->loadDirectory(m_testDir->url());
587 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
588
589 // So far, the model contains only "a 1/" and "a2/".
590 QCOMPARE(m_model->count(), 2);
591 QVERIFY(m_model->expandedDirectories().empty());
592
593 // Expand the parents of "a2/b2/c2".
594 m_model->expandParentDirectories(KUrl(m_testDir->name() + "a2/b2/c2"));
595 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
596
597 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
598 // It's important that only the parents of "a1/b1/c1" are expanded.
599 QCOMPARE(m_model->count(), 4);
600 QVERIFY(!m_model->isExpanded(0));
601 QVERIFY(m_model->isExpanded(1));
602 QVERIFY(m_model->isExpanded(2));
603 QVERIFY(!m_model->isExpanded(3));
604
605 // Expand the parents of "a 1/b1".
606 m_model->expandParentDirectories(KUrl(m_testDir->name() + "a 1/b1"));
607 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
608
609 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
610 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
611 QCOMPARE(m_model->count(), 5);
612 QVERIFY(m_model->isExpanded(0));
613 QVERIFY(!m_model->isExpanded(1));
614 QVERIFY(m_model->isExpanded(2));
615 QVERIFY(m_model->isExpanded(3));
616 QVERIFY(!m_model->isExpanded(4));
617 QVERIFY(m_model->isConsistent());
618 }
619
620 /**
621 * Renaming an expanded folder by prepending its name with a dot makes it
622 * hidden. Verify that this does not cause an inconsistent model state and
623 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
624 */
625 void KFileItemModelTest::testMakeExpandedItemHidden()
626 {
627 QSet<QByteArray> modelRoles = m_model->roles();
628 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
629 m_model->setRoles(modelRoles);
630
631 QStringList files;
632 m_testDir->createFile("1a/2a/3a");
633 m_testDir->createFile("1a/2a/3b");
634 m_testDir->createFile("1a/2b");
635 m_testDir->createFile("1b");
636
637 m_model->loadDirectory(m_testDir->url());
638 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
639
640 // So far, the model contains only "1a/" and "1b".
641 QCOMPARE(m_model->count(), 2);
642 m_model->setExpanded(0, true);
643 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
644
645 // Now "1a/2a" and "1a/2b" have appeared.
646 QCOMPARE(m_model->count(), 4);
647 m_model->setExpanded(1, true);
648 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
649 QCOMPARE(m_model->count(), 6);
650
651 // Rename "1a/2" and make it hidden.
652 const QString oldPath = m_model->fileItem(0).url().path() + "/2a";
653 const QString newPath = m_model->fileItem(0).url().path() + "/.2a";
654
655 KIO::SimpleJob* job = KIO::rename(oldPath, newPath, KIO::HideProgressInfo);
656 bool ok = job->exec();
657 QVERIFY(ok);
658 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
659
660 // "1a/2" and its subfolders have disappeared now.
661 QVERIFY(m_model->isConsistent());
662 QCOMPARE(m_model->count(), 3);
663
664 m_model->setExpanded(0, false);
665 QCOMPARE(m_model->count(), 2);
666
667 }
668
669 void KFileItemModelTest::testSorting()
670 {
671 // Create some files with different sizes and modification times to check the different sorting options
672 QDateTime now = QDateTime::currentDateTime();
673
674 QSet<QByteArray> roles;
675 roles.insert("text");
676 roles.insert("isExpanded");
677 roles.insert("isExpandable");
678 roles.insert("expandedParentsCount");
679 m_model->setRoles(roles);
680
681 m_testDir->createDir("c/c-2");
682 m_testDir->createFile("c/c-2/c-3");
683 m_testDir->createFile("c/c-1");
684
685 m_testDir->createFile("a", "A file", now.addDays(-3));
686 m_testDir->createFile("b", "A larger file", now.addDays(0));
687 m_testDir->createDir("c", now.addDays(-2));
688 m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
689 m_testDir->createFile("e", "An even larger file", now.addDays(-4));
690 m_testDir->createFile(".f");
691
692 m_model->loadDirectory(m_testDir->url());
693 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
694
695 int index = m_model->index(KUrl(m_testDir->url().url() + 'c'));
696 m_model->setExpanded(index, true);
697 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
698
699 index = m_model->index(KUrl(m_testDir->url().url() + "c/c-2"));
700 m_model->setExpanded(index, true);
701 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
702
703 // Default: Sort by Name, ascending
704 QCOMPARE(m_model->sortRole(), QByteArray("text"));
705 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
706 QVERIFY(m_model->sortDirectoriesFirst());
707 QVERIFY(!m_model->showHiddenFiles());
708 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
709
710 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
711
712 // Sort by Name, ascending, 'Sort Folders First' disabled
713 m_model->setSortDirectoriesFirst(false);
714 QCOMPARE(m_model->sortRole(), QByteArray("text"));
715 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
716 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
717 QCOMPARE(spyItemsMoved.count(), 1);
718 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
719
720 // Sort by Name, descending
721 m_model->setSortDirectoriesFirst(true);
722 m_model->setSortOrder(Qt::DescendingOrder);
723 QCOMPARE(m_model->sortRole(), QByteArray("text"));
724 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
725 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
726 QCOMPARE(spyItemsMoved.count(), 2);
727 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 6 << 7);
728 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
729
730 // Sort by Date, descending
731 m_model->setSortDirectoriesFirst(true);
732 m_model->setSortRole("date");
733 QCOMPARE(m_model->sortRole(), QByteArray("date"));
734 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
735 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
736 QCOMPARE(spyItemsMoved.count(), 1);
737 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 1 << 2 << 3 << 7 << 5 << 4 << 6);
738
739 // Sort by Date, ascending
740 m_model->setSortOrder(Qt::AscendingOrder);
741 QCOMPARE(m_model->sortRole(), QByteArray("date"));
742 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
743 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
744 QCOMPARE(spyItemsMoved.count(), 1);
745 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 1 << 2 << 3 << 7 << 6 << 5 << 4);
746
747 // Sort by Date, ascending, 'Sort Folders First' disabled
748 m_model->setSortDirectoriesFirst(false);
749 QCOMPARE(m_model->sortRole(), QByteArray("date"));
750 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
751 QVERIFY(!m_model->sortDirectoriesFirst());
752 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
753 QCOMPARE(spyItemsMoved.count(), 1);
754 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7);
755
756 // Sort by Name, ascending, 'Sort Folders First' disabled
757 m_model->setSortRole("text");
758 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
759 QVERIFY(!m_model->sortDirectoriesFirst());
760 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
761 QCOMPARE(spyItemsMoved.count(), 1);
762 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
763
764 // Sort by Size, ascending, 'Sort Folders First' disabled
765 m_model->setSortRole("size");
766 QCOMPARE(m_model->sortRole(), QByteArray("size"));
767 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
768 QVERIFY(!m_model->sortDirectoriesFirst());
769 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
770 QCOMPARE(spyItemsMoved.count(), 1);
771 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
772
773 QSKIP("2 tests of testSorting() are temporary deactivated as in KFileItemModel resortAllItems() "
774 "always emits a itemsMoved() signal. Before adjusting the tests think about probably introducing "
775 "another signal", SkipSingle);
776 // Internal note: Check comment in KFileItemModel::resortAllItems() for details.
777
778 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
779 m_model->setSortDirectoriesFirst(true);
780 QCOMPARE(m_model->sortRole(), QByteArray("size"));
781 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
782 QVERIFY(m_model->sortDirectoriesFirst());
783 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
784 QCOMPARE(spyItemsMoved.count(), 0);
785
786 // Sort by Size, descending, 'Sort Folders First' enabled
787 m_model->setSortOrder(Qt::DescendingOrder);
788 QCOMPARE(m_model->sortRole(), QByteArray("size"));
789 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
790 QVERIFY(m_model->sortDirectoriesFirst());
791 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
792 QCOMPARE(spyItemsMoved.count(), 1);
793 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 4 << 3 << 2 << 1);
794
795 // TODO: Sort by other roles; show/hide hidden files
796 }
797
798 void KFileItemModelTest::testIndexForKeyboardSearch()
799 {
800 QStringList files;
801 files << "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
802 m_testDir->createFiles(files);
803
804 m_model->loadDirectory(m_testDir->url());
805 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
806
807 // Search from index 0
808 QCOMPARE(m_model->indexForKeyboardSearch("a", 0), 0);
809 QCOMPARE(m_model->indexForKeyboardSearch("aa", 0), 1);
810 QCOMPARE(m_model->indexForKeyboardSearch("i", 0), 2);
811 QCOMPARE(m_model->indexForKeyboardSearch("image", 0), 2);
812 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 0), 2);
813 QCOMPARE(m_model->indexForKeyboardSearch("image.png", 0), 3);
814 QCOMPARE(m_model->indexForKeyboardSearch("t", 0), 4);
815 QCOMPARE(m_model->indexForKeyboardSearch("text", 0), 4);
816 QCOMPARE(m_model->indexForKeyboardSearch("text1", 0), 5);
817 QCOMPARE(m_model->indexForKeyboardSearch("text2", 0), 6);
818 QCOMPARE(m_model->indexForKeyboardSearch("text11", 0), 7);
819
820 // Start a search somewhere in the middle
821 QCOMPARE(m_model->indexForKeyboardSearch("a", 1), 1);
822 QCOMPARE(m_model->indexForKeyboardSearch("i", 3), 3);
823 QCOMPARE(m_model->indexForKeyboardSearch("t", 5), 5);
824 QCOMPARE(m_model->indexForKeyboardSearch("text1", 6), 7);
825
826 // Test searches that go past the last item back to index 0
827 QCOMPARE(m_model->indexForKeyboardSearch("a", 2), 0);
828 QCOMPARE(m_model->indexForKeyboardSearch("i", 7), 2);
829 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 3), 2);
830 QCOMPARE(m_model->indexForKeyboardSearch("text2", 7), 6);
831
832 // Test searches that yield no result
833 QCOMPARE(m_model->indexForKeyboardSearch("aaa", 0), -1);
834 QCOMPARE(m_model->indexForKeyboardSearch("b", 0), -1);
835 QCOMPARE(m_model->indexForKeyboardSearch("image.svg", 0), -1);
836 QCOMPARE(m_model->indexForKeyboardSearch("text3", 0), -1);
837 QCOMPARE(m_model->indexForKeyboardSearch("text3", 5), -1);
838
839 // Test upper case searches (note that search is case insensitive)
840 QCOMPARE(m_model->indexForKeyboardSearch("A", 0), 0);
841 QCOMPARE(m_model->indexForKeyboardSearch("aA", 0), 1);
842 QCOMPARE(m_model->indexForKeyboardSearch("TexT", 5), 5);
843 QCOMPARE(m_model->indexForKeyboardSearch("IMAGE", 4), 2);
844
845 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
846 }
847
848 void KFileItemModelTest::testNameFilter()
849 {
850 QStringList files;
851 files << "A1" << "A2" << "Abc" << "Bcd" << "Cde";
852 m_testDir->createFiles(files);
853
854 m_model->loadDirectory(m_testDir->url());
855 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
856
857 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
858 QCOMPARE(m_model->count(), 3);
859
860 m_model->setNameFilter("A2"); // Shows only A2
861 QCOMPARE(m_model->count(), 1);
862
863 m_model->setNameFilter("A2"); // Shows only A1
864 QCOMPARE(m_model->count(), 1);
865
866 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
867 QCOMPARE(m_model->count(), 2);
868
869 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
870 QCOMPARE(m_model->count(), 2);
871
872 m_model->setNameFilter(QString()); // Shows again all items
873 QCOMPARE(m_model->count(), 5);
874 }
875
876 /**
877 * Verifies that we do not crash when adding a KFileItem with an empty path.
878 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
879 * tried to always read the first character of the path, even if the path is empty.
880 */
881 void KFileItemModelTest::testEmptyPath()
882 {
883 QSet<QByteArray> roles;
884 roles.insert("text");
885 roles.insert("isExpanded");
886 roles.insert("isExpandable");
887 roles.insert("expandedParentsCount");
888 m_model->setRoles(roles);
889
890 const KUrl emptyUrl;
891 QVERIFY(emptyUrl.path().isEmpty());
892
893 const KUrl url("file:///test/");
894
895 KFileItemList items;
896 items << KFileItem(emptyUrl, QString(), KFileItem::Unknown) << KFileItem(url, QString(), KFileItem::Unknown);
897 m_model->slotItemsAdded(emptyUrl, items);
898 m_model->slotCompleted();
899 }
900
901 /**
902 * Verifies that the 'isExpanded' state of folders does not change when the
903 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
904 */
905 void KFileItemModelTest::testRefreshExpandedItem()
906 {
907 QSet<QByteArray> modelRoles = m_model->roles();
908 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
909 m_model->setRoles(modelRoles);
910
911 QStringList files;
912 files << "a/1" << "a/2" << "3" << "4";
913 m_testDir->createFiles(files);
914
915 m_model->loadDirectory(m_testDir->url());
916 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
917 QCOMPARE(m_model->count(), 3); // "a/", "3", "4"
918
919 m_model->setExpanded(0, true);
920 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
921 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
922 QVERIFY(m_model->isExpanded(0));
923
924 QSignalSpy spyItemsChanged(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)));
925
926 const KFileItem item = m_model->fileItem(0);
927 m_model->slotRefreshItems(QList<QPair<KFileItem, KFileItem> >() << qMakePair(item, item));
928 QVERIFY(!spyItemsChanged.isEmpty());
929
930 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
931 QVERIFY(m_model->isExpanded(0));
932 }
933
934 /**
935 * Verify that removing hidden files and folders from the model does not
936 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
937 */
938 void KFileItemModelTest::testRemoveHiddenItems()
939 {
940 m_testDir->createDir(".a");
941 m_testDir->createDir(".b");
942 m_testDir->createDir("c");
943 m_testDir->createDir("d");
944 m_testDir->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
945
946 QSignalSpy spyItemsInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
947 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
948
949 m_model->setShowHiddenFiles(true);
950 m_model->loadDirectory(m_testDir->url());
951 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
952 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
953 QCOMPARE(spyItemsInserted.count(), 1);
954 QCOMPARE(spyItemsRemoved.count(), 0);
955 KItemRangeList itemRangeList = spyItemsInserted.takeFirst().at(0).value<KItemRangeList>();
956 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
957
958 m_model->setShowHiddenFiles(false);
959 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
960 QCOMPARE(spyItemsInserted.count(), 0);
961 QCOMPARE(spyItemsRemoved.count(), 1);
962 itemRangeList = spyItemsRemoved.takeFirst().at(0).value<KItemRangeList>();
963 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
964
965 m_model->setShowHiddenFiles(true);
966 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
967 QCOMPARE(spyItemsInserted.count(), 1);
968 QCOMPARE(spyItemsRemoved.count(), 0);
969 itemRangeList = spyItemsInserted.takeFirst().at(0).value<KItemRangeList>();
970 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
971
972 m_model->clear();
973 QCOMPARE(itemsInModel(), QStringList());
974 QCOMPARE(spyItemsInserted.count(), 0);
975 QCOMPARE(spyItemsRemoved.count(), 1);
976 itemRangeList = spyItemsRemoved.takeFirst().at(0).value<KItemRangeList>();
977 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
978
979 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
980 // Verify that this does not make the model crash.
981 m_model->setShowHiddenFiles(false);
982 }
983
984 /**
985 * Verify that filtered items are removed when their parent is collapsed.
986 */
987 void KFileItemModelTest::collapseParentOfHiddenItems()
988 {
989 QSet<QByteArray> modelRoles = m_model->roles();
990 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
991 m_model->setRoles(modelRoles);
992
993 QStringList files;
994 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
995 m_testDir->createFiles(files);
996
997 m_model->loadDirectory(m_testDir->url());
998 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
999 QCOMPARE(m_model->count(), 1); // Only "a/"
1000
1001 // Expand "a/".
1002 m_model->setExpanded(0, true);
1003 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1004 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1005
1006 // Expand "a/b/".
1007 m_model->setExpanded(1, true);
1008 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1009 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1010
1011 // Expand "a/b/c/".
1012 m_model->setExpanded(2, true);
1013 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1014 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"
1015
1016 // Set a name filter that matches nothing -> only the expanded folders remain.
1017 m_model->setNameFilter("xyz");
1018 QCOMPARE(m_model->count(), 3);
1019 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1020
1021 // Collapse the folder "a/".
1022 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1023 m_model->setExpanded(0, false);
1024 QCOMPARE(spyItemsRemoved.count(), 1);
1025 QCOMPARE(m_model->count(), 1);
1026 QCOMPARE(itemsInModel(), QStringList() << "a");
1027
1028 // Remove the filter -> no files should appear (and we should not get a crash).
1029 m_model->setNameFilter(QString());
1030 QCOMPARE(m_model->count(), 1);
1031 }
1032
1033 /**
1034 * Verify that filtered items are removed when their parent is deleted.
1035 */
1036 void KFileItemModelTest::removeParentOfHiddenItems()
1037 {
1038 QSet<QByteArray> modelRoles = m_model->roles();
1039 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1040 m_model->setRoles(modelRoles);
1041
1042 QStringList files;
1043 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1044 m_testDir->createFiles(files);
1045
1046 m_model->loadDirectory(m_testDir->url());
1047 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1048 QCOMPARE(m_model->count(), 1); // Only "a/"
1049
1050 // Expand "a/".
1051 m_model->setExpanded(0, true);
1052 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1053 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1054
1055 // Expand "a/b/".
1056 m_model->setExpanded(1, true);
1057 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1058 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1059
1060 // Expand "a/b/c/".
1061 m_model->setExpanded(2, true);
1062 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1063 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"
1064
1065 // Set a name filter that matches nothing -> only the expanded folders remain.
1066 m_model->setNameFilter("xyz");
1067 QCOMPARE(m_model->count(), 3);
1068 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1069
1070 // Simulate the deletion of the directory "a/b/".
1071 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1072 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1073 QCOMPARE(spyItemsRemoved.count(), 1);
1074 QCOMPARE(m_model->count(), 1);
1075 QCOMPARE(itemsInModel(), QStringList() << "a");
1076
1077 // Remove the filter -> only the file "a/1" should appear.
1078 m_model->setNameFilter(QString());
1079 QCOMPARE(m_model->count(), 2);
1080 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1081 }
1082
1083 /**
1084 * Create a tree structure where parent-child relationships can not be
1085 * determined by parsing the URLs, and verify that KFileItemModel
1086 * handles them correctly.
1087 */
1088 void KFileItemModelTest::testGeneralParentChildRelationships()
1089 {
1090 QSet<QByteArray> modelRoles = m_model->roles();
1091 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1092 m_model->setRoles(modelRoles);
1093
1094 QStringList files;
1095 files << "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1096 m_testDir->createFiles(files);
1097
1098 m_model->loadDirectory(m_testDir->url());
1099 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1100 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1101
1102 // Expand all folders.
1103 m_model->setExpanded(0, true);
1104 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1105 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1106
1107 m_model->setExpanded(1, true);
1108 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1109 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1110
1111 m_model->setExpanded(3, true);
1112 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1113 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1114
1115 m_model->setExpanded(4, true);
1116 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1117 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1118
1119 // Add some more children and grand-children.
1120 const KUrl parent1 = m_model->fileItem(0).url();
1121 const KUrl parent2 = m_model->fileItem(3).url();
1122 const KUrl realChild1 = m_model->fileItem(1).url();
1123 const KUrl realChild2 = m_model->fileItem(4).url();
1124
1125 m_model->slotItemsAdded(parent1, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown));
1126 m_model->slotCompleted();
1127 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1128
1129 m_model->slotItemsAdded(parent2, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown));
1130 m_model->slotCompleted();
1131 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1132
1133 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown));
1134 m_model->slotCompleted();
1135 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1136
1137 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown));
1138 m_model->slotCompleted();
1139 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1140
1141 m_model->slotItemsAdded(realChild2, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown));
1142 m_model->slotCompleted();
1143 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1144
1145 // Set a name filter that matches nothing -> only expanded folders remain.
1146 QSignalSpy itemsRemovedSpy(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1147 m_model->setNameFilter("xyz");
1148 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1149 QCOMPARE(itemsRemovedSpy.count(), 1);
1150 QList<QVariant> arguments = itemsRemovedSpy.takeFirst();
1151 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
1152 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1153
1154 // Collapse "parent1".
1155 m_model->setExpanded(0, false);
1156 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1157 QCOMPARE(itemsRemovedSpy.count(), 1);
1158 arguments = itemsRemovedSpy.takeFirst();
1159 itemRangeList = arguments.at(0).value<KItemRangeList>();
1160 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1));
1161
1162 // Remove "parent2".
1163 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1164 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1165 QCOMPARE(itemsRemovedSpy.count(), 1);
1166 arguments = itemsRemovedSpy.takeFirst();
1167 itemRangeList = arguments.at(0).value<KItemRangeList>();
1168 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
1169
1170 // Clear filter, verify that no items reappear.
1171 m_model->setNameFilter(QString());
1172 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1173 }
1174
1175 QStringList KFileItemModelTest::itemsInModel() const
1176 {
1177 QStringList items;
1178 for (int i = 0; i < m_model->count(); i++) {
1179 items << m_model->data(i).value("text").toString();
1180 }
1181 return items;
1182 }
1183
1184 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
1185
1186 #include "kfileitemmodeltest.moc"