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