]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Exit the deleted directory when it is removed
[dolphin.git] / src / tests / kfileitemmodeltest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include <QRandomGenerator>
9 #include <QTest>
10 #include <QSignalSpy>
11 #include <QStandardPaths>
12 #include <QTimer>
13 #include <QMimeData>
14
15 #include <KDirLister>
16 #include <kio/job.h>
17 #include <kio_version.h>
18
19
20 #include "kitemviews/kfileitemmodel.h"
21 #include "testdir.h"
22
23 void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
24 {
25 Q_UNUSED(context)
26
27 switch (type) {
28 case QtDebugMsg:
29 break;
30 case QtWarningMsg:
31 break;
32 case QtCriticalMsg:
33 fprintf(stderr, "Critical: %s\n", msg.toLocal8Bit().data());
34 break;
35 case QtFatalMsg:
36 fprintf(stderr, "Fatal: %s\n", msg.toLocal8Bit().data());
37 abort();
38 default:
39 break;
40 }
41 }
42
43 Q_DECLARE_METATYPE(KItemRange)
44 Q_DECLARE_METATYPE(KItemRangeList)
45 Q_DECLARE_METATYPE(QList<int>)
46
47 class KFileItemModelTest : public QObject
48 {
49 Q_OBJECT
50
51 private Q_SLOTS:
52 void init();
53 void initTestCase();
54 void cleanup();
55
56 void testDefaultRoles();
57 void testDefaultSortRole();
58 void testDefaultGroupedSorting();
59 void testNewItems();
60 void testRemoveItems();
61 void testDirLoadingCompleted();
62 void testSetData();
63 void testSetDataWithModifiedSortRole_data();
64 void testSetDataWithModifiedSortRole();
65 void testChangeSortRole();
66 void testResortAfterChangingName();
67 void testModelConsistencyWhenInsertingItems();
68 void testItemRangeConsistencyWhenInsertingItems();
69 void testExpandItems();
70 void testExpandParentItems();
71 void testMakeExpandedItemHidden();
72 void testRemoveFilteredExpandedItems();
73 void testSorting();
74 void testIndexForKeyboardSearch();
75 void testNameFilter();
76 void testEmptyPath();
77 void testRefreshExpandedItem();
78 void testAddItemToFilteredExpandedFolder();
79 void testDeleteItemsWithExpandedFolderWithFilter();
80 void testRefreshItemsWithFilter();
81 void testRefreshExpandedFolderWithFilter();
82 void testRemoveHiddenItems();
83 void collapseParentOfHiddenItems();
84 void removeParentOfHiddenItems();
85 void testGeneralParentChildRelationships();
86 void testNameRoleGroups();
87 void testNameRoleGroupsWithExpandedItems();
88 void testInconsistentModel();
89 void testChangeRolesForFilteredItems();
90 void testChangeSortRoleWhileFiltering();
91 void testRefreshFilteredItems();
92 void testCollapseFolderWhileLoading();
93 void testCreateMimeData();
94 void testDeleteFileMoreThanOnce();
95 void testInsertAfterExpand();
96 void testCurrentDirRemoved();
97
98 private:
99 QStringList itemsInModel() const;
100
101 private:
102 KFileItemModel* m_model;
103 TestDir* m_testDir;
104 };
105
106 void KFileItemModelTest::initTestCase()
107 {
108 QStandardPaths::setTestModeEnabled(true);
109 }
110
111 void KFileItemModelTest::init()
112 {
113 // The item-model tests result in a huge number of debugging
114 // output from kdelibs. Only show critical and fatal messages.
115 qInstallMessageHandler(myMessageOutput);
116
117 qRegisterMetaType<KItemRange>("KItemRange");
118 qRegisterMetaType<KItemRangeList>("KItemRangeList");
119 qRegisterMetaType<KFileItemList>("KFileItemList");
120
121 m_testDir = new TestDir();
122 m_model = new KFileItemModel();
123 m_model->m_dirLister->setAutoUpdate(false);
124
125 // Reduce the timer interval to make the test run faster.
126 m_model->m_resortAllItemsTimer->setInterval(0);
127 }
128
129 void KFileItemModelTest::cleanup()
130 {
131 delete m_model;
132 m_model = nullptr;
133
134 delete m_testDir;
135 m_testDir = nullptr;
136 }
137
138 void KFileItemModelTest::testDefaultRoles()
139 {
140 const QSet<QByteArray> roles = m_model->roles();
141 QCOMPARE(roles.count(), 4);
142 QVERIFY(roles.contains("text"));
143 QVERIFY(roles.contains("isDir"));
144 QVERIFY(roles.contains("isLink"));
145 QVERIFY(roles.contains("isHidden"));
146 }
147
148 void KFileItemModelTest::testDefaultSortRole()
149 {
150 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
151 QVERIFY(itemsInsertedSpy.isValid());
152
153 QCOMPARE(m_model->sortRole(), QByteArray("text"));
154
155 m_testDir->createFiles({"c.txt", "a.txt", "b.txt"});
156
157 m_model->loadDirectory(m_testDir->url());
158 QVERIFY(itemsInsertedSpy.wait());
159
160 QCOMPARE(m_model->count(), 3);
161 QCOMPARE(m_model->data(0).value("text").toString(), QString("a.txt"));
162 QCOMPARE(m_model->data(1).value("text").toString(), QString("b.txt"));
163 QCOMPARE(m_model->data(2).value("text").toString(), QString("c.txt"));
164 }
165
166 void KFileItemModelTest::testDefaultGroupedSorting()
167 {
168 QCOMPARE(m_model->groupedSorting(), false);
169 }
170
171 void KFileItemModelTest::testNewItems()
172 {
173 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
174
175 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
176
177 m_model->loadDirectory(m_testDir->url());
178 QVERIFY(itemsInsertedSpy.wait());
179
180 QCOMPARE(m_model->count(), 3);
181
182 QVERIFY(m_model->isConsistent());
183 }
184
185 void KFileItemModelTest::testRemoveItems()
186 {
187 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
188 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
189
190 m_testDir->createFiles({"a.txt", "b.txt"});
191 m_model->loadDirectory(m_testDir->url());
192 QVERIFY(itemsInsertedSpy.wait());
193 QCOMPARE(m_model->count(), 2);
194 QVERIFY(m_model->isConsistent());
195
196 m_testDir->removeFile("a.txt");
197 m_model->m_dirLister->updateDirectory(m_testDir->url());
198 QVERIFY(itemsRemovedSpy.wait());
199 QCOMPARE(m_model->count(), 1);
200 QVERIFY(m_model->isConsistent());
201 }
202
203 void KFileItemModelTest::testDirLoadingCompleted()
204 {
205 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
206 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
207 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
208
209 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
210
211 m_model->loadDirectory(m_testDir->url());
212 QVERIFY(loadingCompletedSpy.wait());
213 QCOMPARE(loadingCompletedSpy.count(), 1);
214 QCOMPARE(itemsInsertedSpy.count(), 1);
215 QCOMPARE(itemsRemovedSpy.count(), 0);
216 QCOMPARE(m_model->count(), 3);
217
218 m_testDir->createFiles({"d.txt", "e.txt"});
219 m_model->m_dirLister->updateDirectory(m_testDir->url());
220 QVERIFY(loadingCompletedSpy.wait());
221 QCOMPARE(loadingCompletedSpy.count(), 2);
222 QCOMPARE(itemsInsertedSpy.count(), 2);
223 QCOMPARE(itemsRemovedSpy.count(), 0);
224 QCOMPARE(m_model->count(), 5);
225
226 m_testDir->removeFile("a.txt");
227 m_testDir->createFile("f.txt");
228 m_model->m_dirLister->updateDirectory(m_testDir->url());
229 QVERIFY(loadingCompletedSpy.wait());
230 QCOMPARE(loadingCompletedSpy.count(), 3);
231 QCOMPARE(itemsInsertedSpy.count(), 3);
232 QCOMPARE(itemsRemovedSpy.count(), 1);
233 QCOMPARE(m_model->count(), 5);
234
235 m_testDir->removeFile("b.txt");
236 m_model->m_dirLister->updateDirectory(m_testDir->url());
237 QVERIFY(itemsRemovedSpy.wait());
238 QCOMPARE(loadingCompletedSpy.count(), 4);
239 QCOMPARE(itemsInsertedSpy.count(), 3);
240 QCOMPARE(itemsRemovedSpy.count(), 2);
241 QCOMPARE(m_model->count(), 4);
242
243 QVERIFY(m_model->isConsistent());
244 }
245
246 void KFileItemModelTest::testSetData()
247 {
248 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
249 QVERIFY(itemsInsertedSpy.isValid());
250 QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged);
251 QVERIFY(itemsChangedSpy.isValid());
252
253 m_testDir->createFile("a.txt");
254
255 m_model->loadDirectory(m_testDir->url());
256 QVERIFY(itemsInsertedSpy.wait());
257
258 QHash<QByteArray, QVariant> values;
259 values.insert("customRole1", "Test1");
260 values.insert("customRole2", "Test2");
261
262 m_model->setData(0, values);
263 QCOMPARE(itemsChangedSpy.count(), 1);
264
265 values = m_model->data(0);
266 QCOMPARE(values.value("customRole1").toString(), QString("Test1"));
267 QCOMPARE(values.value("customRole2").toString(), QString("Test2"));
268 QVERIFY(m_model->isConsistent());
269 }
270
271 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
272 {
273 QTest::addColumn<int>("changedIndex");
274 QTest::addColumn<int>("changedRating");
275 QTest::addColumn<bool>("expectMoveSignal");
276 QTest::addColumn<int>("ratingIndex0");
277 QTest::addColumn<int>("ratingIndex1");
278 QTest::addColumn<int>("ratingIndex2");
279
280 // Default setup:
281 // Index 0 = rating 2
282 // Index 1 = rating 4
283 // Index 2 = rating 6
284
285 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
286 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
287 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
288
289 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
290 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
291 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
292 }
293
294 void KFileItemModelTest::testSetDataWithModifiedSortRole()
295 {
296 QFETCH(int, changedIndex);
297 QFETCH(int, changedRating);
298 QFETCH(bool, expectMoveSignal);
299 QFETCH(int, ratingIndex0);
300 QFETCH(int, ratingIndex1);
301 QFETCH(int, ratingIndex2);
302
303 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
304 QVERIFY(itemsInsertedSpy.isValid());
305 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
306 QVERIFY(itemsMovedSpy.isValid());
307
308 // Changing the value of a sort-role must result in
309 // a reordering of the items.
310 QCOMPARE(m_model->sortRole(), QByteArray("text"));
311 m_model->setSortRole("rating");
312 QCOMPARE(m_model->sortRole(), QByteArray("rating"));
313
314 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
315
316 m_model->loadDirectory(m_testDir->url());
317 QVERIFY(itemsInsertedSpy.wait());
318
319 // Fill the "rating" role of each file:
320 // a.txt -> 2
321 // b.txt -> 4
322 // c.txt -> 6
323
324 QHash<QByteArray, QVariant> ratingA;
325 ratingA.insert("rating", 2);
326 m_model->setData(0, ratingA);
327
328 QHash<QByteArray, QVariant> ratingB;
329 ratingB.insert("rating", 4);
330 m_model->setData(1, ratingB);
331
332 QHash<QByteArray, QVariant> ratingC;
333 ratingC.insert("rating", 6);
334 m_model->setData(2, ratingC);
335
336 QCOMPARE(m_model->data(0).value("rating").toInt(), 2);
337 QCOMPARE(m_model->data(1).value("rating").toInt(), 4);
338 QCOMPARE(m_model->data(2).value("rating").toInt(), 6);
339
340 // Now change the rating from a.txt. This usually results
341 // in reordering of the items.
342 QHash<QByteArray, QVariant> rating;
343 rating.insert("rating", changedRating);
344 m_model->setData(changedIndex, rating);
345
346 if (expectMoveSignal) {
347 QVERIFY(itemsMovedSpy.wait());
348 }
349
350 QCOMPARE(m_model->data(0).value("rating").toInt(), ratingIndex0);
351 QCOMPARE(m_model->data(1).value("rating").toInt(), ratingIndex1);
352 QCOMPARE(m_model->data(2).value("rating").toInt(), ratingIndex2);
353 QVERIFY(m_model->isConsistent());
354 }
355
356 void KFileItemModelTest::testChangeSortRole()
357 {
358 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
359 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
360 QVERIFY(itemsMovedSpy.isValid());
361
362 QCOMPARE(m_model->sortRole(), QByteArray("text"));
363
364 m_testDir->createFiles({"a.txt", "b.jpg", "c.txt"});
365
366 m_model->loadDirectory(m_testDir->url());
367 QVERIFY(itemsInsertedSpy.wait());
368 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.jpg" << "c.txt");
369
370 // Simulate that KFileItemModelRolesUpdater determines the mime type.
371 // Resorting the files by 'type' will only work immediately if their
372 // mime types are known.
373 for (int index = 0; index < m_model->count(); ++index) {
374 m_model->fileItem(index).determineMimeType();
375 }
376
377 // Now: sort by type.
378 m_model->setSortRole("type");
379 QCOMPARE(m_model->sortRole(), QByteArray("type"));
380 QVERIFY(!itemsMovedSpy.isEmpty());
381
382 // The actual order of the files might depend on the translation of the
383 // result of KFileItem::mimeComment() in the user's language.
384 QStringList version1;
385 version1 << "b.jpg" << "a.txt" << "c.txt";
386
387 QStringList version2;
388 version2 << "a.txt" << "c.txt" << "b.jpg";
389
390 const bool ok1 = (itemsInModel() == version1);
391 const bool ok2 = (itemsInModel() == version2);
392
393 QVERIFY(ok1 || ok2);
394 }
395
396 void KFileItemModelTest::testResortAfterChangingName()
397 {
398 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
399 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
400 QVERIFY(itemsMovedSpy.isValid());
401
402 // We sort by size in a directory where all files have the same size.
403 // Therefore, the files are sorted by their names.
404 m_model->setSortRole("size");
405
406 m_testDir->createFiles({"a.txt", "b.txt", "c.txt"});
407
408 m_model->loadDirectory(m_testDir->url());
409 QVERIFY(itemsInsertedSpy.wait());
410 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
411
412 // We rename a.txt to d.txt. Even though the size has not changed at all,
413 // the model must re-sort the items.
414 QHash<QByteArray, QVariant> data;
415 data.insert("text", "d.txt");
416 m_model->setData(0, data);
417
418 QVERIFY(itemsMovedSpy.wait());
419 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
420
421 // We rename d.txt back to a.txt using the dir lister's refreshItems() signal.
422 const KFileItem fileItemD = m_model->fileItem(2);
423 KFileItem fileItemA = fileItemD;
424 QUrl urlA = fileItemA.url().adjusted(QUrl::RemoveFilename);
425 urlA.setPath(urlA.path() + "a.txt");
426 fileItemA.setUrl(urlA);
427
428 m_model->slotRefreshItems({qMakePair(fileItemD, fileItemA)});
429
430 QVERIFY(itemsMovedSpy.wait());
431 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
432 }
433
434 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
435 {
436 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
437
438 // KFileItemModel prevents that inserting a punch of items sequentially
439 // results in an itemsInserted()-signal for each item. Instead internally
440 // a timeout is given that collects such operations and results in only
441 // one itemsInserted()-signal. However in this test we want to stress
442 // KFileItemModel to do a lot of insert operation and hence decrease
443 // the timeout to 1 millisecond.
444 m_testDir->createFile("1");
445 m_model->loadDirectory(m_testDir->url());
446 QVERIFY(itemsInsertedSpy.wait());
447 QCOMPARE(m_model->count(), 1);
448
449 // Insert 10 items for 20 times. After each insert operation the model consistency
450 // is checked.
451 QSet<int> insertedItems;
452 for (int i = 0; i < 20; ++i) {
453 itemsInsertedSpy.clear();
454
455 for (int j = 0; j < 10; ++j) {
456 int itemName = QRandomGenerator::global()->generate();
457 while (insertedItems.contains(itemName)) {
458 itemName = QRandomGenerator::global()->generate();
459 }
460 insertedItems.insert(itemName);
461
462 m_testDir->createFile(QString::number(itemName));
463 }
464
465 m_model->m_dirLister->updateDirectory(m_testDir->url());
466 if (itemsInsertedSpy.isEmpty()) {
467 QVERIFY(itemsInsertedSpy.wait());
468 }
469
470 QVERIFY(m_model->isConsistent());
471 }
472
473 QCOMPARE(m_model->count(), 201);
474 }
475
476 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
477 {
478 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
479
480 m_testDir->createFiles({"B", "E", "G"});
481
482 // Due to inserting the 3 items one item-range with index == 0 and
483 // count == 3 must be given
484 m_model->loadDirectory(m_testDir->url());
485 QVERIFY(itemsInsertedSpy.wait());
486
487 QCOMPARE(itemsInsertedSpy.count(), 1);
488 QList<QVariant> arguments = itemsInsertedSpy.takeFirst();
489 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
490 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 3));
491
492 // The indexes of the item-ranges must always be related to the model before
493 // the items have been inserted. Having:
494 // 0 1 2
495 // B E G
496 // and inserting A, C, D, F the resulting model will be:
497 // 0 1 2 3 4 5 6
498 // A B C D E F G
499 // and the item-ranges must be:
500 // index: 0, count: 1 for A
501 // index: 1, count: 2 for B, C
502 // index: 2, count: 1 for G
503
504 m_testDir->createFiles({"A", "C", "D", "F"});
505
506 m_model->m_dirLister->updateDirectory(m_testDir->url());
507 QVERIFY(itemsInsertedSpy.wait());
508
509 QCOMPARE(itemsInsertedSpy.count(), 1);
510 arguments = itemsInsertedSpy.takeFirst();
511 itemRangeList = arguments.at(0).value<KItemRangeList>();
512 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
513 }
514
515 void KFileItemModelTest::testExpandItems()
516 {
517 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
518 QVERIFY(itemsInsertedSpy.isValid());
519 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
520 QVERIFY(itemsRemovedSpy.isValid());
521 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
522 QVERIFY(loadingCompletedSpy.isValid());
523
524 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
525 // Besides testing the basic item expansion functionality, the test makes sure that
526 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
527 // yields the correct result for "a/a/1" and "a/a-1/", which is non-trivial because they share the
528 // first three characters.
529 QSet<QByteArray> originalModelRoles = m_model->roles();
530 QSet<QByteArray> modelRoles = originalModelRoles;
531 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
532 m_model->setRoles(modelRoles);
533
534 m_testDir->createFiles({"a/a/1", "a/a-1/1"});
535
536 // Store the URLs of all folders in a set.
537 QSet<QUrl> allFolders;
538 allFolders << QUrl::fromLocalFile(m_testDir->path() + "/a")
539 << QUrl::fromLocalFile(m_testDir->path() + "/a/a")
540 << QUrl::fromLocalFile(m_testDir->path() + "/a/a-1");
541
542 m_model->loadDirectory(m_testDir->url());
543 QVERIFY(itemsInsertedSpy.wait());
544
545 // So far, the model contains only "a/"
546 QCOMPARE(m_model->count(), 1);
547 QVERIFY(m_model->isExpandable(0));
548 QVERIFY(!m_model->isExpanded(0));
549 QVERIFY(m_model->expandedDirectories().empty());
550
551 QCOMPARE(itemsInsertedSpy.count(), 1);
552 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
553 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
554
555 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
556 m_model->setExpanded(0, true);
557 QVERIFY(m_model->isExpanded(0));
558 QVERIFY(itemsInsertedSpy.wait());
559 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
560 QCOMPARE(m_model->expandedDirectories(), QSet<QUrl>() << QUrl::fromLocalFile(m_testDir->path() + "/a"));
561
562 QCOMPARE(itemsInsertedSpy.count(), 1);
563 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
564 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
565
566 QVERIFY(m_model->isExpandable(1));
567 QVERIFY(!m_model->isExpanded(1));
568 QVERIFY(m_model->isExpandable(2));
569 QVERIFY(!m_model->isExpanded(2));
570
571 // Expand the folder "a/a/" -> "a/a/1" becomes visible
572 m_model->setExpanded(1, true);
573 QVERIFY(m_model->isExpanded(1));
574 QVERIFY(itemsInsertedSpy.wait());
575 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
576 QCOMPARE(m_model->expandedDirectories(), QSet<QUrl>() << QUrl::fromLocalFile(m_testDir->path() + "/a")
577 << QUrl::fromLocalFile(m_testDir->path() + "/a/a"));
578
579 QCOMPARE(itemsInsertedSpy.count(), 1);
580 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
581 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
582
583 QVERIFY(!m_model->isExpandable(2));
584 QVERIFY(!m_model->isExpanded(2));
585
586 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
587 m_model->setExpanded(3, true);
588 QVERIFY(m_model->isExpanded(3));
589 QVERIFY(itemsInsertedSpy.wait());
590 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
591 QCOMPARE(m_model->expandedDirectories(), allFolders);
592
593 QCOMPARE(itemsInsertedSpy.count(), 1);
594 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
595 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
596
597 QVERIFY(!m_model->isExpandable(4));
598 QVERIFY(!m_model->isExpanded(4));
599
600 // Collapse the top-level folder -> all other items should disappear
601 m_model->setExpanded(0, false);
602 QVERIFY(!m_model->isExpanded(0));
603 QCOMPARE(m_model->count(), 1);
604 QVERIFY(!m_model->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir->path() + "/a"))); // TODO: Make sure that child URLs are also removed
605
606 QCOMPARE(itemsRemovedSpy.count(), 1);
607 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
608 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
609 QVERIFY(m_model->isConsistent());
610
611 // Clear the model, reload the folder and try to restore the expanded folders.
612 m_model->clear();
613 QCOMPARE(m_model->count(), 0);
614 QVERIFY(m_model->expandedDirectories().empty());
615
616 m_model->loadDirectory(m_testDir->url());
617 m_model->restoreExpandedDirectories(allFolders);
618 QVERIFY(loadingCompletedSpy.wait());
619 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
620 QVERIFY(m_model->isExpanded(0));
621 QVERIFY(m_model->isExpanded(1));
622 QVERIFY(!m_model->isExpanded(2));
623 QVERIFY(m_model->isExpanded(3));
624 QVERIFY(!m_model->isExpanded(4));
625 QCOMPARE(m_model->expandedDirectories(), allFolders);
626 QVERIFY(m_model->isConsistent());
627
628 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
629 // This is how DolphinView restores the expanded folders when navigating in history.
630 m_model->loadDirectory(QUrl::fromLocalFile(m_testDir->path() + "/a/a/"));
631 QVERIFY(loadingCompletedSpy.wait());
632 QCOMPARE(m_model->count(), 1); // 1 item: "1"
633 m_model->restoreExpandedDirectories(allFolders);
634 m_model->loadDirectory(m_testDir->url());
635 QVERIFY(loadingCompletedSpy.wait());
636 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
637 QCOMPARE(m_model->expandedDirectories(), allFolders);
638
639 // Remove all expanded items by changing the roles
640 itemsRemovedSpy.clear();
641 m_model->setRoles(originalModelRoles);
642 QVERIFY(!m_model->isExpanded(0));
643 QCOMPARE(m_model->count(), 1);
644 QVERIFY(!m_model->expandedDirectories().contains(QUrl::fromLocalFile(m_testDir->path() + "/a")));
645
646 QCOMPARE(itemsRemovedSpy.count(), 1);
647 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
648 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
649 QVERIFY(m_model->isConsistent());
650 }
651
652 void KFileItemModelTest::testExpandParentItems()
653 {
654 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
655 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
656 QVERIFY(loadingCompletedSpy.isValid());
657
658 // Create a tree structure of folders:
659 // a 1/
660 // a 1/b1/
661 // a 1/b1/c1/
662 // a2/
663 // a2/b2/
664 // a2/b2/c2/
665 // a2/b2/c2/d2/
666 QSet<QByteArray> modelRoles = m_model->roles();
667 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
668 m_model->setRoles(modelRoles);
669
670 m_testDir->createFiles({"a 1/b1/c1/file.txt", "a2/b2/c2/d2/file.txt"});
671
672 m_model->loadDirectory(m_testDir->url());
673 QVERIFY(itemsInsertedSpy.wait());
674
675 // So far, the model contains only "a 1/" and "a2/".
676 QCOMPARE(m_model->count(), 2);
677 QVERIFY(m_model->expandedDirectories().empty());
678
679 // Expand the parents of "a2/b2/c2".
680 m_model->expandParentDirectories(QUrl::fromLocalFile(m_testDir->path() + "a2/b2/c2"));
681 QVERIFY(loadingCompletedSpy.wait());
682
683 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
684 // It's important that only the parents of "a1/b1/c1" are expanded.
685 QCOMPARE(m_model->count(), 4);
686 QVERIFY(!m_model->isExpanded(0));
687 QVERIFY(m_model->isExpanded(1));
688 QVERIFY(m_model->isExpanded(2));
689 QVERIFY(!m_model->isExpanded(3));
690
691 // Expand the parents of "a 1/b1".
692 m_model->expandParentDirectories(QUrl::fromLocalFile(m_testDir->path() + "a 1/b1"));
693 QVERIFY(loadingCompletedSpy.wait());
694
695 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
696 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
697 QCOMPARE(m_model->count(), 5);
698 QVERIFY(m_model->isExpanded(0));
699 QVERIFY(!m_model->isExpanded(1));
700 QVERIFY(m_model->isExpanded(2));
701 QVERIFY(m_model->isExpanded(3));
702 QVERIFY(!m_model->isExpanded(4));
703 QVERIFY(m_model->isConsistent());
704
705 // Expand "a 1/b1/".
706 m_model->setExpanded(1, true);
707 QVERIFY(loadingCompletedSpy.wait());
708 QCOMPARE(m_model->count(), 6);
709 QVERIFY(m_model->isExpanded(0));
710 QVERIFY(m_model->isExpanded(1));
711 QVERIFY(!m_model->isExpanded(2));
712 QVERIFY(m_model->isExpanded(3));
713 QVERIFY(m_model->isExpanded(4));
714 QVERIFY(!m_model->isExpanded(5));
715 QVERIFY(m_model->isConsistent());
716
717 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
718 m_model->setExpanded(1, false);
719 QCOMPARE(m_model->count(), 5);
720 QVERIFY(m_model->isExpanded(0));
721 QVERIFY(!m_model->isExpanded(1));
722 QVERIFY(m_model->isExpanded(2));
723 QVERIFY(m_model->isExpanded(3));
724 QVERIFY(!m_model->isExpanded(4));
725 QVERIFY(m_model->isConsistent());
726 }
727
728 /**
729 * Renaming an expanded folder by prepending its name with a dot makes it
730 * hidden. Verify that this does not cause an inconsistent model state and
731 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
732 */
733 void KFileItemModelTest::testMakeExpandedItemHidden()
734 {
735 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
736 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
737
738 QSet<QByteArray> modelRoles = m_model->roles();
739 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
740 m_model->setRoles(modelRoles);
741
742 m_testDir->createFiles({"1a/2a/3a", "1a/2a/3b", "1a/2b", "1b"});
743
744 m_model->loadDirectory(m_testDir->url());
745 QVERIFY(itemsInsertedSpy.wait());
746
747 // So far, the model contains only "1a/" and "1b".
748 QCOMPARE(m_model->count(), 2);
749 m_model->setExpanded(0, true);
750 QVERIFY(itemsInsertedSpy.wait());
751
752 // Now "1a/2a" and "1a/2b" have appeared.
753 QCOMPARE(m_model->count(), 4);
754 m_model->setExpanded(1, true);
755 QVERIFY(itemsInsertedSpy.wait());
756 QCOMPARE(m_model->count(), 6);
757
758 // Rename "1a/2" and make it hidden.
759 const QUrl oldUrl = QUrl::fromLocalFile(m_model->fileItem(0).url().path() + "/2a");
760 const QUrl newUrl = QUrl::fromLocalFile(m_model->fileItem(0).url().path() + "/.2a");
761
762 KIO::SimpleJob* job = KIO::rename(oldUrl, newUrl, KIO::HideProgressInfo);
763 bool ok = job->exec();
764 QVERIFY(ok);
765 QVERIFY(itemsRemovedSpy.wait());
766
767 // "1a/2" and its subfolders have disappeared now.
768 QVERIFY(m_model->isConsistent());
769 QCOMPARE(m_model->count(), 3);
770
771 m_model->setExpanded(0, false);
772 QCOMPARE(m_model->count(), 2);
773
774 }
775
776 void KFileItemModelTest::testRemoveFilteredExpandedItems()
777 {
778 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
779
780 QSet<QByteArray> originalModelRoles = m_model->roles();
781 QSet<QByteArray> modelRoles = originalModelRoles;
782 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
783 m_model->setRoles(modelRoles);
784
785 m_testDir->createFiles({"folder/child", "file"});
786
787 m_model->loadDirectory(m_testDir->url());
788 QVERIFY(itemsInsertedSpy.wait());
789
790 // So far, the model contains only "folder/" and "file".
791 QCOMPARE(m_model->count(), 2);
792 QVERIFY(m_model->isExpandable(0));
793 QVERIFY(!m_model->isExpandable(1));
794 QVERIFY(!m_model->isExpanded(0));
795 QVERIFY(!m_model->isExpanded(1));
796 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
797
798 // Expand "folder" -> "folder/child" becomes visible.
799 m_model->setExpanded(0, true);
800 QVERIFY(m_model->isExpanded(0));
801 QVERIFY(itemsInsertedSpy.wait());
802 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
803
804 // Add a name filter.
805 m_model->setNameFilter("f");
806 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
807
808 m_model->setNameFilter("fo");
809 QCOMPARE(itemsInModel(), QStringList() << "folder");
810
811 // Remove all expanded items by changing the roles
812 m_model->setRoles(originalModelRoles);
813 QVERIFY(!m_model->isExpanded(0));
814 QCOMPARE(itemsInModel(), QStringList() << "folder");
815
816 // Remove the name filter and verify that "folder/child" does not reappear.
817 m_model->setNameFilter(QString());
818 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
819 }
820
821 void KFileItemModelTest::testSorting()
822 {
823 // testDir structure is as follows
824 // ./
825 // ├─ .g/
826 // ├─ a
827 // ├─ b
828 // ├─ c/
829 // │ ├─ c-2/
830 // │ │ ├─ c-3
831 // │ ├─ c-1
832 // ├─ .f
833 // ├─ d
834 // ├─ e
835
836 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
837 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
838 QVERIFY(itemsMovedSpy.isValid());
839
840 // Create some files with different sizes and modification times to check the different sorting options
841 QDateTime now = QDateTime::currentDateTime();
842
843 QSet<QByteArray> roles;
844 roles.insert("text");
845 roles.insert("isExpanded");
846 roles.insert("isExpandable");
847 roles.insert("expandedParentsCount");
848 m_model->setRoles(roles);
849
850 m_testDir->createDir("c/c-2");
851 m_testDir->createFile("c/c-2/c-3");
852 m_testDir->createFile("c/c-1");
853
854 m_testDir->createFile("a", "A file", now.addDays(-3));
855 m_testDir->createFile("b", "A larger file", now.addDays(0));
856 m_testDir->createDir("c", now.addDays(-2));
857 m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
858 m_testDir->createFile("e", "An even larger file", now.addDays(-4));
859 m_testDir->createFile(".f");
860 m_testDir->createDir(".g");
861
862 m_model->loadDirectory(m_testDir->url());
863 QVERIFY(itemsInsertedSpy.wait());
864 QCOMPARE(itemsInsertedSpy.count(), 1);
865 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
866 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 5));
867
868 int index = m_model->index(QUrl(m_testDir->url().url() + "/c"));
869 m_model->setExpanded(index, true);
870 QVERIFY(itemsInsertedSpy.wait());
871 QCOMPARE(itemsInsertedSpy.count(), 1);
872 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
873 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
874
875 index = m_model->index(QUrl(m_testDir->url().url() + "/c/c-2"));
876 m_model->setExpanded(index, true);
877 QVERIFY(itemsInsertedSpy.wait());
878 QCOMPARE(itemsInsertedSpy.count(), 1);
879 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
880 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1));
881
882 // Default: Sort by Name, ascending
883 QCOMPARE(m_model->sortRole(), QByteArray("text"));
884 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
885 QVERIFY(m_model->sortDirectoriesFirst());
886 QVERIFY(!m_model->showHiddenFiles());
887 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
888
889 // Sort by Name, ascending, 'Sort Folders First' disabled
890 m_model->setSortDirectoriesFirst(false);
891 QCOMPARE(m_model->sortRole(), QByteArray("text"));
892 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
893 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
894 QCOMPARE(itemsMovedSpy.count(), 1);
895 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 6));
896 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
897
898 // Sort by Name, descending
899 m_model->setSortDirectoriesFirst(true);
900 m_model->setSortOrder(Qt::DescendingOrder);
901 QCOMPARE(m_model->sortRole(), QByteArray("text"));
902 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
903 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
904 QCOMPARE(itemsMovedSpy.count(), 2);
905 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 6));
906 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2);
907 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
908 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
909
910 // Sort by Date, descending
911 m_model->setSortDirectoriesFirst(true);
912 m_model->setSortRole("modificationtime");
913 QCOMPARE(m_model->sortRole(), QByteArray("modificationtime"));
914 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
915 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
916 QCOMPARE(itemsMovedSpy.count(), 1);
917 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
918 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 5 << 4 << 6);
919
920 // Sort by Date, ascending
921 m_model->setSortOrder(Qt::AscendingOrder);
922 QCOMPARE(m_model->sortRole(), QByteArray("modificationtime"));
923 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
924 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
925 QCOMPARE(itemsMovedSpy.count(), 1);
926 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
927 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
928
929 // Sort by Date, ascending, 'Sort Folders First' disabled
930 m_model->setSortDirectoriesFirst(false);
931 QCOMPARE(m_model->sortRole(), QByteArray("modificationtime"));
932 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
933 QVERIFY(!m_model->sortDirectoriesFirst());
934 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
935 QCOMPARE(itemsMovedSpy.count(), 1);
936 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 6));
937 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
938
939 // Sort by Name, ascending, 'Sort Folders First' disabled
940 m_model->setSortRole("text");
941 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
942 QVERIFY(!m_model->sortDirectoriesFirst());
943 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
944 QCOMPARE(itemsMovedSpy.count(), 1);
945 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 8));
946 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
947
948 // Sort by Size, ascending, 'Sort Folders First' disabled
949 m_model->setSortRole("size");
950 QCOMPARE(m_model->sortRole(), QByteArray("size"));
951 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
952 QVERIFY(!m_model->sortDirectoriesFirst());
953 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
954 QCOMPARE(itemsMovedSpy.count(), 1);
955 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 8));
956 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
957
958 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
959 m_model->setSortDirectoriesFirst(true);
960 QCOMPARE(m_model->sortRole(), QByteArray("size"));
961 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
962 QVERIFY(m_model->sortDirectoriesFirst());
963 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
964 QCOMPARE(itemsMovedSpy.count(), 0);
965
966 // Sort by Size, descending, 'Sort Folders First' enabled
967 m_model->setSortOrder(Qt::DescendingOrder);
968 QCOMPARE(m_model->sortRole(), QByteArray("size"));
969 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
970 QVERIFY(m_model->sortDirectoriesFirst());
971 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
972 QCOMPARE(itemsMovedSpy.count(), 1);
973 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
974 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
975
976 // 'Show Hidden Files' enabled
977 m_model->setShowHiddenFiles(true);
978 QVERIFY(m_model->showHiddenFiles());
979 QVERIFY(!m_model->sortHiddenLast());
980 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << ".g" << "d" << "e" << "b" << "a" << ".f");
981 QCOMPARE(itemsMovedSpy.count(), 0);
982 QCOMPARE(itemsInsertedSpy.count(), 1);
983 QCOMPARE(itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>(), KItemRangeList() << KItemRange(4, 1) << KItemRange(8, 1));
984
985 // 'Sort Hidden Files Last' enabled
986 m_model->setSortHiddenLast(true);
987 QVERIFY(m_model->sortHiddenLast());
988 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a" << ".g" << ".f");
989 QCOMPARE(itemsMovedSpy.count(), 1);
990 QCOMPARE(itemsInsertedSpy.count(), 0);
991 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 5));
992 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 8 << 4 << 5 << 6 << 7);
993
994 // Sort by Name
995 m_model->setSortRole("text");
996 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a" << ".g" << ".f");
997 QCOMPARE(itemsMovedSpy.count(), 1);
998 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 2));
999 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 5 << 4);
1000
1001 // Sort ascending
1002 m_model->setSortOrder(Qt::AscendingOrder);
1003 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e" << ".g" << ".f");
1004 QCOMPARE(itemsMovedSpy.count(), 1);
1005 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(4, 4));
1006 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
1007
1008 // 'Sort Folders First' disabled
1009 m_model->setSortDirectoriesFirst(false);
1010 QVERIFY(!m_model->sortDirectoriesFirst());
1011 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e" << ".f" << ".g");
1012 QCOMPARE(itemsMovedSpy.count(), 1);
1013 QCOMPARE(itemsMovedSpy.first().at(0).value<KItemRange>(), KItemRange(0, 10));
1014 QCOMPARE(itemsMovedSpy.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8);
1015
1016 }
1017
1018 void KFileItemModelTest::testIndexForKeyboardSearch()
1019 {
1020 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1021
1022 m_testDir->createFiles({"a", "aa", "Image.jpg", "Image.png", "Text", "Text1", "Text2", "Text11"});
1023
1024 m_model->loadDirectory(m_testDir->url());
1025 QVERIFY(itemsInsertedSpy.wait());
1026
1027 // Search from index 0
1028 QCOMPARE(m_model->indexForKeyboardSearch("a", 0), 0);
1029 QCOMPARE(m_model->indexForKeyboardSearch("aa", 0), 1);
1030 QCOMPARE(m_model->indexForKeyboardSearch("i", 0), 2);
1031 QCOMPARE(m_model->indexForKeyboardSearch("image", 0), 2);
1032 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 0), 2);
1033 QCOMPARE(m_model->indexForKeyboardSearch("image.png", 0), 3);
1034 QCOMPARE(m_model->indexForKeyboardSearch("t", 0), 4);
1035 QCOMPARE(m_model->indexForKeyboardSearch("text", 0), 4);
1036 QCOMPARE(m_model->indexForKeyboardSearch("text1", 0), 5);
1037 QCOMPARE(m_model->indexForKeyboardSearch("text2", 0), 6);
1038 QCOMPARE(m_model->indexForKeyboardSearch("text11", 0), 7);
1039
1040 // Start a search somewhere in the middle
1041 QCOMPARE(m_model->indexForKeyboardSearch("a", 1), 1);
1042 QCOMPARE(m_model->indexForKeyboardSearch("i", 3), 3);
1043 QCOMPARE(m_model->indexForKeyboardSearch("t", 5), 5);
1044 QCOMPARE(m_model->indexForKeyboardSearch("text1", 6), 7);
1045
1046 // Test searches that go past the last item back to index 0
1047 QCOMPARE(m_model->indexForKeyboardSearch("a", 2), 0);
1048 QCOMPARE(m_model->indexForKeyboardSearch("i", 7), 2);
1049 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 3), 2);
1050 QCOMPARE(m_model->indexForKeyboardSearch("text2", 7), 6);
1051
1052 // Test searches that yield no result
1053 QCOMPARE(m_model->indexForKeyboardSearch("aaa", 0), -1);
1054 QCOMPARE(m_model->indexForKeyboardSearch("b", 0), -1);
1055 QCOMPARE(m_model->indexForKeyboardSearch("image.svg", 0), -1);
1056 QCOMPARE(m_model->indexForKeyboardSearch("text3", 0), -1);
1057 QCOMPARE(m_model->indexForKeyboardSearch("text3", 5), -1);
1058
1059 // Test upper case searches (note that search is case insensitive)
1060 QCOMPARE(m_model->indexForKeyboardSearch("A", 0), 0);
1061 QCOMPARE(m_model->indexForKeyboardSearch("aA", 0), 1);
1062 QCOMPARE(m_model->indexForKeyboardSearch("TexT", 5), 5);
1063 QCOMPARE(m_model->indexForKeyboardSearch("IMAGE", 4), 2);
1064
1065 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
1066 }
1067
1068 void KFileItemModelTest::testNameFilter()
1069 {
1070 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1071
1072 m_testDir->createFiles({"A1", "A2", "Abc", "Bcd", "Cde"});
1073
1074 m_model->loadDirectory(m_testDir->url());
1075 QVERIFY(itemsInsertedSpy.wait());
1076
1077 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
1078 QCOMPARE(m_model->count(), 3);
1079
1080 m_model->setNameFilter("A2"); // Shows only A2
1081 QCOMPARE(m_model->count(), 1);
1082
1083 m_model->setNameFilter("A2"); // Shows only A1
1084 QCOMPARE(m_model->count(), 1);
1085
1086 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1087 QCOMPARE(m_model->count(), 2);
1088
1089 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1090 QCOMPARE(m_model->count(), 2);
1091
1092 m_model->setNameFilter(QString()); // Shows again all items
1093 QCOMPARE(m_model->count(), 5);
1094 }
1095
1096 /**
1097 * Verifies that we do not crash when adding a KFileItem with an empty path.
1098 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1099 * tried to always read the first character of the path, even if the path is empty.
1100 */
1101 void KFileItemModelTest::testEmptyPath()
1102 {
1103 QSet<QByteArray> roles;
1104 roles.insert("text");
1105 roles.insert("isExpanded");
1106 roles.insert("isExpandable");
1107 roles.insert("expandedParentsCount");
1108 m_model->setRoles(roles);
1109
1110 const QUrl emptyUrl;
1111 QVERIFY(emptyUrl.path().isEmpty());
1112
1113 const QUrl url("file:///test/");
1114
1115 KFileItemList items;
1116 items << KFileItem(emptyUrl, QString(), KFileItem::Unknown) << KFileItem(url, QString(), KFileItem::Unknown);
1117 m_model->slotItemsAdded(emptyUrl, items);
1118 m_model->slotCompleted();
1119 }
1120
1121 /**
1122 * Verifies that the 'isExpanded' state of folders does not change when the
1123 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1124 */
1125 void KFileItemModelTest::testRefreshExpandedItem()
1126 {
1127 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1128 QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged);
1129 QVERIFY(itemsChangedSpy.isValid());
1130
1131 QSet<QByteArray> modelRoles = m_model->roles();
1132 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1133 m_model->setRoles(modelRoles);
1134
1135 m_testDir->createFiles({"a/1", "a/2", "3", "4"});
1136
1137 m_model->loadDirectory(m_testDir->url());
1138 QVERIFY(itemsInsertedSpy.wait());
1139 QCOMPARE(m_model->count(), 3); // "a/", "3", "4"
1140
1141 m_model->setExpanded(0, true);
1142 QVERIFY(itemsInsertedSpy.wait());
1143 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1144 QVERIFY(m_model->isExpanded(0));
1145
1146 const KFileItem item = m_model->fileItem(0);
1147 m_model->slotRefreshItems({qMakePair(item, item)});
1148 QVERIFY(!itemsChangedSpy.isEmpty());
1149
1150 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1151 QVERIFY(m_model->isExpanded(0));
1152 }
1153
1154 /**
1155 * Verifies that adding an item to an expanded folder that's filtered makes the parental chain visible.
1156 */
1157 void KFileItemModelTest::testAddItemToFilteredExpandedFolder()
1158 {
1159 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1160 QSignalSpy fileItemsChangedSpy(m_model, &KFileItemModel::fileItemsChanged);
1161
1162 QSet<QByteArray> modelRoles = m_model->roles();
1163 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1164 m_model->setRoles(modelRoles);
1165
1166 m_testDir->createFile("a/b/file");
1167
1168 m_model->loadDirectory(m_testDir->url());
1169 QVERIFY(itemsInsertedSpy.wait());
1170 QCOMPARE(m_model->count(), 1); // "a
1171
1172 // Expand "a/".
1173 m_model->setExpanded(0, true);
1174 QVERIFY(itemsInsertedSpy.wait());
1175
1176 // Expand "a/b/".
1177 m_model->setExpanded(1, true);
1178 QVERIFY(itemsInsertedSpy.wait());
1179
1180 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1181
1182 const QUrl urlB = m_model->fileItem(1).url();
1183
1184 // Set a filter that matches ".txt" extension
1185 m_model->setNameFilter("*.txt");
1186 QCOMPARE(m_model->count(), 0); // Everything got hidden since we don't have a .txt file yet
1187
1188 m_model->slotItemsAdded(urlB, KFileItemList() << KFileItem(QUrl("a/b/newItem.txt")));
1189 m_model->slotCompleted();
1190
1191 // Entire parental chain should now be shown
1192 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/b/newItem.txt"
1193 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "newItem.txt");
1194
1195 // Items should be indented in hierarchy
1196 QCOMPARE(m_model->expandedParentsCount(0), 0);
1197 QCOMPARE(m_model->expandedParentsCount(1), 1);
1198 QCOMPARE(m_model->expandedParentsCount(2), 2);
1199 }
1200
1201 /**
1202 * Verifies that deleting the last filter-passing child from expanded folders
1203 * makes the parental chain hidden.
1204 */
1205 void KFileItemModelTest::testDeleteItemsWithExpandedFolderWithFilter()
1206 {
1207 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1208 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1209
1210 QSet<QByteArray> modelRoles = m_model->roles();
1211 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1212 m_model->setRoles(modelRoles);
1213
1214 m_testDir->createFile("a/b/file");
1215
1216 m_model->loadDirectory(m_testDir->url());
1217 QVERIFY(itemsInsertedSpy.wait());
1218 QCOMPARE(m_model->count(), 1); // "a
1219
1220 // Expand "a/".
1221 m_model->setExpanded(0, true);
1222 QVERIFY(itemsInsertedSpy.wait());
1223
1224 // Expand "a/b/".
1225 m_model->setExpanded(1, true);
1226 QVERIFY(itemsInsertedSpy.wait());
1227
1228 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/b/file"
1229
1230 // Set a filter that matches "file" extension
1231 m_model->setNameFilter("file");
1232 QCOMPARE(m_model->count(), 3); // Everything is still shown
1233
1234 // Delete "file"
1235 QCOMPARE(itemsRemovedSpy.count(), 0);
1236 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(2));
1237 QCOMPARE(itemsRemovedSpy.count(), 1);
1238
1239 // Entire parental chain should now be filtered
1240 QCOMPARE(m_model->count(), 0);
1241 QCOMPARE(m_model->m_filteredItems.size(), 2);
1242 }
1243
1244 /**
1245 * Verifies that the fileItemsChanged signal is raised with the correct index after renaming files with filter set.
1246 * The rename operation will cause one item to be filtered out and another item to be reordered.
1247 */
1248 void KFileItemModelTest::testRefreshItemsWithFilter()
1249 {
1250 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1251 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1252 QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged);
1253 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
1254
1255 // Creates three .txt files
1256 m_testDir->createFiles({"b.txt", "c.txt", "d.txt"});
1257
1258 m_model->loadDirectory(m_testDir->url());
1259 QVERIFY(itemsInsertedSpy.wait());
1260
1261 QCOMPARE(m_model->count(), 3); // "b.txt", "c.txt", "d.txt"
1262
1263 // Set a filter that matches ".txt" extension
1264 m_model->setNameFilter("*.txt");
1265 QCOMPARE(m_model->count(), 3); // Still all items are shown
1266 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt");
1267
1268 // Objects used to rename
1269 const KFileItem fileItemC_txt = m_model->fileItem(1);
1270 KFileItem fileItemC_cfg = fileItemC_txt;
1271 fileItemC_cfg.setUrl(QUrl("c.cfg"));
1272
1273 const KFileItem fileItemD_txt = m_model->fileItem(2);
1274 KFileItem fileItemA_txt = fileItemD_txt;
1275 fileItemA_txt.setUrl(QUrl("a.txt"));
1276
1277 // Rename "c.txt" to "c.cfg"; and rename "d.txt" to "a.txt"
1278 QCOMPARE(itemsRemovedSpy.count(), 0);
1279 QCOMPARE(itemsChangedSpy.count(), 0);
1280 m_model->slotRefreshItems({qMakePair(fileItemC_txt, fileItemC_cfg), qMakePair(fileItemD_txt, fileItemA_txt)});
1281 QCOMPARE(itemsRemovedSpy.count(), 1);
1282 QCOMPARE(itemsChangedSpy.count(), 1);
1283 QCOMPARE(m_model->count(), 2); // Only "a.txt" and "b.txt". "c.cfg" got filtered out
1284
1285 QList<QVariant> arguments = itemsChangedSpy.takeLast();
1286 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
1287
1288 // We started with the order "b.txt", "c.txt", "d.txt"
1289 // "d.txt" started with index "2"
1290 // "c.txt" got renamed and got filtered out
1291 // "d.txt" index shifted from index "2" to "1"
1292 // So we expect index "1" in this argument, meaning "d.txt" was renamed
1293 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1));
1294
1295 // Re-sorting is done asynchronously:
1296 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "a.txt"); // Files should still be in the incorrect order
1297 QVERIFY(itemsMovedSpy.wait());
1298 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt"); // Files were re-sorted and should now be in the correct order
1299 }
1300
1301
1302 /**
1303 * Verifies that parental chains are hidden and shown as needed while their children get filtered/unfiltered due to renaming.
1304 * Also verifies that the "isExpanded" and "expandedParentsCount" values are kept for expanded folders that get refreshed.
1305 */
1306 void KFileItemModelTest::testRefreshExpandedFolderWithFilter() {
1307 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1308 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1309
1310 QSet<QByteArray> modelRoles = m_model->roles();
1311 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1312 m_model->setRoles(modelRoles);
1313
1314 m_testDir->createFile("a/b/someFolder/someFile");
1315
1316 m_model->loadDirectory(m_testDir->url());
1317 QVERIFY(itemsInsertedSpy.wait());
1318
1319 QCOMPARE(m_model->count(), 1); // Only "a/"
1320
1321 // Expand "a/".
1322 m_model->setExpanded(0, true);
1323 QVERIFY(itemsInsertedSpy.wait());
1324
1325 // Expand "a/b/".
1326 m_model->setExpanded(1, true);
1327 QVERIFY(itemsInsertedSpy.wait());
1328
1329 // Expand "a/b/someFolder/".
1330 m_model->setExpanded(2, true);
1331 QVERIFY(itemsInsertedSpy.wait());
1332 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/b/", "a/b/someFolder", "a/b/someFolder/someFile"
1333
1334 // Set a filter that matches the expanded folder "someFolder"
1335 m_model->setNameFilter("someFolder");
1336 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/b/someFolder"
1337
1338 // Objects used to rename
1339 const KFileItem fileItemA = m_model->fileItem(0);
1340 KFileItem fileItemARenamed = fileItemA;
1341 fileItemARenamed.setUrl(QUrl("a_renamed"));
1342
1343 const KFileItem fileItemSomeFolder = m_model->fileItem(2);
1344 KFileItem fileItemRenamedFolder = fileItemSomeFolder;
1345 fileItemRenamedFolder.setUrl(QUrl("/a_renamed/b/renamedFolder"));
1346
1347 // Rename "a" to "a_renamed"
1348 // This way we test if the algorithm is sane as to NOT hide "a_renamed" since it will have visible children
1349 m_model->slotRefreshItems({qMakePair(fileItemA, fileItemARenamed)});
1350 QCOMPARE(m_model->count(), 3); // Entire parental chain must still be shown
1351 QCOMPARE(itemsInModel(), QStringList() << "a_renamed" << "b" << "someFolder");
1352
1353 // Rename "a_renamed" back to "a"; and "someFolder" to "renamedFolder"
1354 m_model->slotRefreshItems({qMakePair(fileItemARenamed, fileItemA), qMakePair(fileItemSomeFolder, fileItemRenamedFolder)});
1355 QCOMPARE(m_model->count(), 0); // Entire parental chain became hidden
1356
1357 // Rename "renamedFolder" back to "someFolder". Filter is passing again
1358 m_model->slotRefreshItems({qMakePair(fileItemRenamedFolder, fileItemSomeFolder)});
1359 QCOMPARE(m_model->count(), 3); // Entire parental chain is shown again
1360 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "someFolder");
1361
1362 // slotRefreshItems() should preserve "isExpanded" and "expandedParentsCount" values explicitly in this case
1363 QCOMPARE(m_model->m_itemData.at(2)->values.value("isExpanded").toBool(), true);
1364 QCOMPARE(m_model->m_itemData.at(2)->values.value("expandedParentsCount"), 2);
1365 }
1366
1367 /**
1368 * Verify that removing hidden files and folders from the model does not
1369 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1370 */
1371 void KFileItemModelTest::testRemoveHiddenItems()
1372 {
1373 m_testDir->createDir(".a");
1374 m_testDir->createDir(".b");
1375 m_testDir->createDir("c");
1376 m_testDir->createDir("d");
1377 m_testDir->createFiles({".f", ".g", "h", "i"});
1378
1379 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1380 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1381
1382 m_model->setShowHiddenFiles(true);
1383 m_model->loadDirectory(m_testDir->url());
1384 QVERIFY(itemsInsertedSpy.wait());
1385 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1386 QCOMPARE(itemsInsertedSpy.count(), 1);
1387 QCOMPARE(itemsRemovedSpy.count(), 0);
1388 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
1389 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
1390
1391 m_model->setShowHiddenFiles(false);
1392 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1393 QCOMPARE(itemsInsertedSpy.count(), 0);
1394 QCOMPARE(itemsRemovedSpy.count(), 1);
1395 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
1396 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1397
1398 m_model->setShowHiddenFiles(true);
1399 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1400 QCOMPARE(itemsInsertedSpy.count(), 1);
1401 QCOMPARE(itemsRemovedSpy.count(), 0);
1402 itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
1403 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1404
1405 m_model->clear();
1406 QCOMPARE(itemsInModel(), QStringList());
1407 QCOMPARE(itemsInsertedSpy.count(), 0);
1408 QCOMPARE(itemsRemovedSpy.count(), 1);
1409 itemRangeList = itemsRemovedSpy.takeFirst().at(0).value<KItemRangeList>();
1410 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
1411
1412 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1413 // Verify that this does not make the model crash.
1414 m_model->setShowHiddenFiles(false);
1415 }
1416
1417 /**
1418 * Verify that filtered items are removed when their parent is collapsed.
1419 */
1420 void KFileItemModelTest::collapseParentOfHiddenItems()
1421 {
1422 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1423 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1424
1425 QSet<QByteArray> modelRoles = m_model->roles();
1426 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1427 m_model->setRoles(modelRoles);
1428
1429 m_testDir->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1430
1431 m_model->loadDirectory(m_testDir->url());
1432 QVERIFY(itemsInsertedSpy.wait());
1433 QCOMPARE(m_model->count(), 1); // Only "a/"
1434
1435 // Expand "a/".
1436 m_model->setExpanded(0, true);
1437 QVERIFY(itemsInsertedSpy.wait());
1438 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1439
1440 // Expand "a/b/".
1441 m_model->setExpanded(1, true);
1442 QVERIFY(itemsInsertedSpy.wait());
1443 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1444
1445 // Expand "a/b/c/".
1446 m_model->setExpanded(2, true);
1447 QVERIFY(itemsInsertedSpy.wait());
1448 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"
1449
1450 // Set a name filter that matches nothing -> nothing should remain.
1451 m_model->setNameFilter("xyz");
1452 QCOMPARE(itemsRemovedSpy.count(), 1);
1453 QCOMPARE(m_model->count(), 0); //Everything is hidden
1454 QCOMPARE(itemsInModel(), QStringList());
1455
1456 //Filter by the file names. Folder "d" will be hidden since it was collapsed
1457 m_model->setNameFilter("1");
1458 QCOMPARE(itemsRemovedSpy.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1459 QCOMPARE(m_model->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
1460
1461 // Collapse the folder "a/".
1462 m_model->setExpanded(0, false);
1463 QCOMPARE(itemsRemovedSpy.count(), 2);
1464 QCOMPARE(m_model->count(), 1);
1465 QCOMPARE(itemsInModel(), QStringList() << "a");
1466
1467 // Remove the filter -> "a" should still appear (and we should not get a crash).
1468 m_model->setNameFilter(QString());
1469 QCOMPARE(itemsRemovedSpy.count(), 2); // nothing was removed, itemsRemovedSpy count will remain the same:
1470 QCOMPARE(m_model->count(), 1);
1471 QCOMPARE(itemsInModel(), QStringList() << "a");
1472 }
1473
1474 /**
1475 * Verify that filtered items are removed when their parent is deleted.
1476 */
1477 void KFileItemModelTest::removeParentOfHiddenItems()
1478 {
1479 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1480 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1481
1482 QSet<QByteArray> modelRoles = m_model->roles();
1483 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1484 m_model->setRoles(modelRoles);
1485
1486 m_testDir->createFiles({"a/1", "a/b/1", "a/b/c/1", "a/b/c/d/1"});
1487
1488 m_model->loadDirectory(m_testDir->url());
1489 QVERIFY(itemsInsertedSpy.wait());
1490 QCOMPARE(m_model->count(), 1); // Only "a/"
1491
1492 // Expand "a/".
1493 m_model->setExpanded(0, true);
1494 QVERIFY(itemsInsertedSpy.wait());
1495 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1496
1497 // Expand "a/b/".
1498 m_model->setExpanded(1, true);
1499 QVERIFY(itemsInsertedSpy.wait());
1500 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1501
1502 // Expand "a/b/c/".
1503 m_model->setExpanded(2, true);
1504 QVERIFY(itemsInsertedSpy.wait());
1505 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"
1506
1507 // Set a name filter that matches nothing -> nothing should remain.
1508 m_model->setNameFilter("xyz");
1509 QCOMPARE(itemsRemovedSpy.count(), 1);
1510 QCOMPARE(m_model->count(), 0);
1511 QCOMPARE(itemsInModel(), QStringList());
1512
1513 // Filter by "c". Folder "b" will also be shown because it is its parent.
1514 m_model->setNameFilter("c");
1515 QCOMPARE(itemsRemovedSpy.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
1516 QCOMPARE(m_model->count(), 3);
1517 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1518
1519 // Simulate the deletion of the directory "a/b/".
1520 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1521 QCOMPARE(itemsRemovedSpy.count(), 2);
1522 QCOMPARE(m_model->count(), 0); // "a" will be filtered out since it doesn't pass the filter and doesn't have visible children
1523
1524 // Remove the filter -> only the file "a/1" should appear.
1525 m_model->setNameFilter(QString());
1526 QCOMPARE(m_model->count(), 2);
1527 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1528 }
1529
1530 /**
1531 * Create a tree structure where parent-child relationships can not be
1532 * determined by parsing the URLs, and verify that KFileItemModel
1533 * handles them correctly.
1534 */
1535 void KFileItemModelTest::testGeneralParentChildRelationships()
1536 {
1537 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1538 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
1539
1540 QSet<QByteArray> modelRoles = m_model->roles();
1541 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1542 m_model->setRoles(modelRoles);
1543
1544 m_testDir->createFiles({"parent1/realChild1/realGrandChild1", "parent2/realChild2/realGrandChild2"});
1545
1546 m_model->loadDirectory(m_testDir->url());
1547 QVERIFY(itemsInsertedSpy.wait());
1548 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1549
1550 // Expand all folders.
1551 m_model->setExpanded(0, true);
1552 QVERIFY(itemsInsertedSpy.wait());
1553 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1554
1555 m_model->setExpanded(1, true);
1556 QVERIFY(itemsInsertedSpy.wait());
1557 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1558
1559 m_model->setExpanded(3, true);
1560 QVERIFY(itemsInsertedSpy.wait());
1561 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1562
1563 m_model->setExpanded(4, true);
1564 QVERIFY(itemsInsertedSpy.wait());
1565 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1566
1567 // Add some more children and grand-children.
1568 const QUrl parent1 = m_model->fileItem(0).url();
1569 const QUrl parent2 = m_model->fileItem(3).url();
1570 const QUrl realChild1 = m_model->fileItem(1).url();
1571 const QUrl realChild2 = m_model->fileItem(4).url();
1572
1573 m_model->slotItemsAdded(parent1, KFileItemList() << KFileItem(QUrl("child1"), QString(), KFileItem::Unknown));
1574 m_model->slotCompleted();
1575 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1576
1577 m_model->slotItemsAdded(parent2, KFileItemList() << KFileItem(QUrl("child2"), QString(), KFileItem::Unknown));
1578 m_model->slotCompleted();
1579 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1580
1581 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(QUrl("grandChild1"), QString(), KFileItem::Unknown));
1582 m_model->slotCompleted();
1583 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1584
1585 m_model->slotItemsAdded(realChild2, KFileItemList() << KFileItem(QUrl("grandChild2"), QString(), KFileItem::Unknown));
1586 m_model->slotCompleted();
1587 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1588
1589 // Set a name filter that matches nothing -> nothing will remain.
1590 m_model->setNameFilter("xyz");
1591 QCOMPARE(itemsInModel(), QStringList());
1592 QCOMPARE(itemsRemovedSpy.count(), 1);
1593 QList<QVariant> arguments = itemsRemovedSpy.takeFirst();
1594 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
1595 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 10));
1596
1597 // Set a name filter that matches only "realChild". Their prarents should still show.
1598 m_model->setNameFilter("realChild");
1599 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1600 QCOMPARE(itemsRemovedSpy.count(), 0); // nothing was removed, itemsRemovedSpy will not be called this time
1601
1602 // Collapse "parent1".
1603 m_model->setExpanded(0, false);
1604 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1605 QCOMPARE(itemsRemovedSpy.count(), 1);
1606 arguments = itemsRemovedSpy.takeFirst();
1607 itemRangeList = arguments.at(0).value<KItemRangeList>();
1608 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1));
1609
1610 // Remove "parent2".
1611 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1612 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1613 QCOMPARE(itemsRemovedSpy.count(), 1);
1614 arguments = itemsRemovedSpy.takeFirst();
1615 itemRangeList = arguments.at(0).value<KItemRangeList>();
1616 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
1617
1618 // Clear filter, verify that no items reappear.
1619 m_model->setNameFilter(QString());
1620 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1621 }
1622
1623 void KFileItemModelTest::testNameRoleGroups()
1624 {
1625 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1626 QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
1627 QVERIFY(itemsMovedSpy.isValid());
1628 QSignalSpy groupsChangedSpy(m_model, &KFileItemModel::groupsChanged);
1629 QVERIFY(groupsChangedSpy.isValid());
1630
1631 m_testDir->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"});
1632
1633 m_model->setGroupedSorting(true);
1634 m_model->loadDirectory(m_testDir->url());
1635 QVERIFY(itemsInsertedSpy.wait());
1636 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1637
1638 QList<QPair<int, QVariant> > expectedGroups;
1639 expectedGroups << QPair<int, QVariant>(0, QLatin1String("B"));
1640 expectedGroups << QPair<int, QVariant>(1, QLatin1String("C"));
1641 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1642 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1643 QCOMPARE(m_model->groups(), expectedGroups);
1644
1645 // Rename d.txt to a.txt.
1646 QHash<QByteArray, QVariant> data;
1647 data.insert("text", "a.txt");
1648 m_model->setData(2, data);
1649 QVERIFY(itemsMovedSpy.wait());
1650 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1651
1652 expectedGroups.clear();
1653 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1654 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1655 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1656 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1657 QCOMPARE(m_model->groups(), expectedGroups);
1658
1659 // Rename c.txt to d.txt.
1660 data.insert("text", "d.txt");
1661 m_model->setData(2, data);
1662 QVERIFY(groupsChangedSpy.wait());
1663 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1664
1665 expectedGroups.clear();
1666 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1667 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1668 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1669 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1670 QCOMPARE(m_model->groups(), expectedGroups);
1671
1672 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1673 const KFileItem fileItemD = m_model->fileItem(2);
1674 KFileItem fileItemC = fileItemD;
1675 QUrl urlC = fileItemC.url().adjusted(QUrl::RemoveFilename);
1676 urlC.setPath(urlC.path() + "c.txt");
1677 fileItemC.setUrl(urlC);
1678
1679 m_model->slotRefreshItems({qMakePair(fileItemD, fileItemC)});
1680 QVERIFY(groupsChangedSpy.wait());
1681 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1682
1683 expectedGroups.clear();
1684 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1685 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1686 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1687 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1688 QCOMPARE(m_model->groups(), expectedGroups);
1689 }
1690
1691 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1692 {
1693 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1694
1695 QSet<QByteArray> modelRoles = m_model->roles();
1696 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1697 m_model->setRoles(modelRoles);
1698
1699 m_testDir->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
1700
1701 m_model->setGroupedSorting(true);
1702 m_model->loadDirectory(m_testDir->url());
1703 QVERIFY(itemsInsertedSpy.wait());
1704 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1705
1706 QList<QPair<int, QVariant> > expectedGroups;
1707 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1708 expectedGroups << QPair<int, QVariant>(1, QLatin1String("D"));
1709 QCOMPARE(m_model->groups(), expectedGroups);
1710
1711 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1712 expectedGroups.clear();
1713 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1714 expectedGroups << QPair<int, QVariant>(3, QLatin1String("D"));
1715
1716 m_model->setExpanded(0, true);
1717 QVERIFY(m_model->isExpanded(0));
1718 QVERIFY(itemsInsertedSpy.wait());
1719 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1720 QCOMPARE(m_model->groups(), expectedGroups);
1721
1722 m_model->setExpanded(3, true);
1723 QVERIFY(m_model->isExpanded(3));
1724 QVERIFY(itemsInsertedSpy.wait());
1725 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1726 QCOMPARE(m_model->groups(), expectedGroups);
1727 }
1728
1729 void KFileItemModelTest::testInconsistentModel()
1730 {
1731 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1732
1733 QSet<QByteArray> modelRoles = m_model->roles();
1734 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1735 m_model->setRoles(modelRoles);
1736
1737 m_testDir->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
1738
1739 m_model->loadDirectory(m_testDir->url());
1740 QVERIFY(itemsInsertedSpy.wait());
1741 QCOMPARE(itemsInModel(), QStringList() << "a");
1742
1743 // Expand "a/" and "a/b/".
1744 m_model->setExpanded(0, true);
1745 QVERIFY(m_model->isExpanded(0));
1746 QVERIFY(itemsInsertedSpy.wait());
1747 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1748
1749 m_model->setExpanded(1, true);
1750 QVERIFY(m_model->isExpanded(1));
1751 QVERIFY(itemsInsertedSpy.wait());
1752 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1753
1754 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1755 // Such a thing can in principle happen when performing a search, and there
1756 // are files which
1757 // (a) match the search string, and
1758 // (b) are children of a folder that matches the search string and is expanded.
1759 //
1760 // Note that the first item in the list of added items must be new (i.e., not
1761 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1762 // it receives items that are in the model already and ignore them.
1763 QUrl url(m_model->directory().url() + "/a2");
1764 KFileItem newItem(url);
1765
1766 KFileItemList items;
1767 items << newItem << m_model->fileItem(2) << m_model->fileItem(3);
1768 m_model->slotItemsAdded(m_model->directory(), items);
1769 m_model->slotCompleted();
1770 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1771
1772 m_model->setExpanded(0, false);
1773
1774 // Test that the right items have been removed, see
1775 // https://bugs.kde.org/show_bug.cgi?id=324371
1776 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1777
1778 // Test that resorting does not cause a crash, see
1779 // https://bugs.kde.org/show_bug.cgi?id=325359
1780 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1781 m_model->resortAllItems();
1782
1783 }
1784
1785 void KFileItemModelTest::testChangeRolesForFilteredItems()
1786 {
1787 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1788
1789 QSet<QByteArray> modelRoles = m_model->roles();
1790 modelRoles << "owner";
1791 m_model->setRoles(modelRoles);
1792
1793 m_testDir->createFiles({"a.txt", "aa.txt", "aaa.txt"});
1794
1795 m_model->loadDirectory(m_testDir->url());
1796 QVERIFY(itemsInsertedSpy.wait());
1797 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1798
1799 for (int index = 0; index < m_model->count(); ++index) {
1800 // All items should have the "text" and "owner" roles, but not "group".
1801 QVERIFY(m_model->data(index).contains("text"));
1802 QVERIFY(m_model->data(index).contains("owner"));
1803 QVERIFY(!m_model->data(index).contains("group"));
1804 }
1805
1806 // Add a filter, such that only "aaa.txt" remains in the model.
1807 m_model->setNameFilter("aaa");
1808 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1809
1810 // Add the "group" role.
1811 modelRoles << "group";
1812 m_model->setRoles(modelRoles);
1813
1814 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1815 m_model->setNameFilter("aa");
1816 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1817
1818 for (int index = 0; index < m_model->count(); ++index) {
1819 // All items should have the "text", "owner", and "group" roles.
1820 QVERIFY(m_model->data(index).contains("text"));
1821 QVERIFY(m_model->data(index).contains("owner"));
1822 QVERIFY(m_model->data(index).contains("group"));
1823 }
1824
1825 // Remove the "owner" role.
1826 modelRoles.remove("owner");
1827 m_model->setRoles(modelRoles);
1828
1829 // Clear the filter, and verify that all items have the expected roles
1830 m_model->setNameFilter(QString());
1831 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1832
1833 for (int index = 0; index < m_model->count(); ++index) {
1834 // All items should have the "text" and "group" roles, but now "owner".
1835 QVERIFY(m_model->data(index).contains("text"));
1836 QVERIFY(!m_model->data(index).contains("owner"));
1837 QVERIFY(m_model->data(index).contains("group"));
1838 }
1839 }
1840
1841 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1842 {
1843 KFileItemList items;
1844
1845 KIO::UDSEntry entry[3];
1846
1847 entry[0].fastInsert(KIO::UDSEntry::UDS_NAME, "a.txt");
1848 entry[0].fastInsert(KIO::UDSEntry::UDS_USER, "user-b");
1849
1850 entry[1].fastInsert(KIO::UDSEntry::UDS_NAME, "b.txt");
1851 entry[1].fastInsert(KIO::UDSEntry::UDS_USER, "user-c");
1852
1853 entry[2].fastInsert(KIO::UDSEntry::UDS_NAME, "c.txt");
1854 entry[2].fastInsert(KIO::UDSEntry::UDS_USER, "user-a");
1855
1856 for (int i = 0; i < 3; ++i) {
1857 entry[i].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1858 entry[i].fastInsert(KIO::UDSEntry::UDS_ACCESS, 07777);
1859 entry[i].fastInsert(KIO::UDSEntry::UDS_SIZE, 0);
1860 entry[i].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0);
1861 entry[i].fastInsert(KIO::UDSEntry::UDS_GROUP, "group");
1862 entry[i].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME, 0);
1863 items.append(KFileItem(entry[i], m_testDir->url(), false, true));
1864 }
1865
1866 m_model->slotItemsAdded(m_testDir->url(), items);
1867 m_model->slotCompleted();
1868
1869 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1870
1871 // Add a filter.
1872 m_model->setNameFilter("a");
1873 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1874
1875 // Sort by "owner".
1876 m_model->setSortRole("owner");
1877
1878 // Clear the filter, and verify that the items are sorted correctly.
1879 m_model->setNameFilter(QString());
1880 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1881 }
1882
1883 void KFileItemModelTest::testRefreshFilteredItems()
1884 {
1885 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1886
1887 m_testDir->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
1888
1889 m_model->loadDirectory(m_testDir->url());
1890 QVERIFY(itemsInsertedSpy.wait());
1891 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1892
1893 const KFileItem fileItemC = m_model->fileItem(2);
1894
1895 // Show only the .txt files.
1896 m_model->setNameFilter(".txt");
1897 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1898
1899 // Rename one of the .jpg files.
1900 KFileItem fileItemE = fileItemC;
1901 QUrl urlE = fileItemE.url().adjusted(QUrl::RemoveFilename);
1902 urlE.setPath(urlE.path() + "/e.jpg");
1903 fileItemE.setUrl(urlE);
1904
1905 m_model->slotRefreshItems({qMakePair(fileItemC, fileItemE)});
1906
1907 // Show all files again, and verify that the model has updated the file name.
1908 m_model->setNameFilter(QString());
1909 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1910 }
1911
1912 void KFileItemModelTest::testCreateMimeData()
1913 {
1914 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1915
1916 QSet<QByteArray> modelRoles = m_model->roles();
1917 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1918 m_model->setRoles(modelRoles);
1919
1920 m_testDir->createFile("a/1");
1921
1922 m_model->loadDirectory(m_testDir->url());
1923 QVERIFY(itemsInsertedSpy.wait());
1924 QCOMPARE(itemsInModel(), QStringList() << "a");
1925
1926 // Expand "a/".
1927 m_model->setExpanded(0, true);
1928 QVERIFY(itemsInsertedSpy.wait());
1929 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1930
1931 // Verify that creating the MIME data for a child of an expanded folder does
1932 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1933 KItemSet selection;
1934 selection.insert(1);
1935 QMimeData* mimeData = m_model->createMimeData(selection);
1936 delete mimeData;
1937 }
1938
1939 void KFileItemModelTest::testCollapseFolderWhileLoading()
1940 {
1941 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
1942
1943 QSet<QByteArray> modelRoles = m_model->roles();
1944 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1945 m_model->setRoles(modelRoles);
1946
1947 m_testDir->createFile("a2/b/c1.txt");
1948
1949 m_model->loadDirectory(m_testDir->url());
1950 QVERIFY(itemsInsertedSpy.wait());
1951 QCOMPARE(itemsInModel(), QStringList() << "a2");
1952
1953 // Expand "a2/".
1954 m_model->setExpanded(0, true);
1955 QVERIFY(m_model->isExpanded(0));
1956 QVERIFY(itemsInsertedSpy.wait());
1957 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1958
1959 // Expand "a2/b/".
1960 m_model->setExpanded(1, true);
1961 QVERIFY(m_model->isExpanded(1));
1962 QVERIFY(itemsInsertedSpy.wait());
1963 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1964
1965 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1966 // signal is not emitted yet.
1967 const KFileItem fileItemC1 = m_model->fileItem(2);
1968 KFileItem fileItemC2 = fileItemC1;
1969 QUrl urlC2 = fileItemC2.url();
1970 urlC2 = urlC2.adjusted(QUrl::RemoveFilename);
1971 urlC2.setPath(urlC2.path() + "c2.txt");
1972 fileItemC2.setUrl(urlC2);
1973
1974 const QUrl urlB = m_model->fileItem(1).url();
1975 m_model->slotItemsAdded(urlB, KFileItemList() << fileItemC2);
1976 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1977
1978 // Collapse "a2/". This should also remove all its (indirect) children from
1979 // the model and from the model's m_pendingItemsToInsert member.
1980 m_model->setExpanded(0, false);
1981 QCOMPARE(itemsInModel(), QStringList() << "a2");
1982
1983 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1984 // is still in m_pendingItemsToInsert, then we might get a crash, see
1985 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1986 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1987 // without parent in the model.
1988 m_model->slotCompleted();
1989 QCOMPARE(itemsInModel(), QStringList() << "a2");
1990
1991 // Expand "a2/" again.
1992 m_model->setExpanded(0, true);
1993 QVERIFY(m_model->isExpanded(0));
1994 QVERIFY(itemsInsertedSpy.wait());
1995 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1996
1997 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1998 // completed() signal is not emitted yet.
1999 const KFileItem fileItemA2 = m_model->fileItem(0);
2000 KFileItem fileItemA1 = fileItemA2;
2001 QUrl urlA1 = fileItemA1.url().adjusted(QUrl::RemoveFilename);
2002 urlA1.setPath(urlA1.path() + "a1");
2003 fileItemA1.setUrl(urlA1);
2004
2005 m_model->slotItemsAdded(m_model->directory(), KFileItemList() << fileItemA1);
2006 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
2007
2008 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
2009 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
2010 // confuse the code which collapses the folder.
2011 m_model->setExpanded(0, false);
2012 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
2013 QVERIFY(!m_model->isExpanded(0));
2014 QVERIFY(!m_model->isExpanded(1));
2015 }
2016
2017 void KFileItemModelTest::testDeleteFileMoreThanOnce()
2018 {
2019 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
2020
2021 m_testDir->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
2022
2023 m_model->loadDirectory(m_testDir->url());
2024 QVERIFY(itemsInsertedSpy.wait());
2025 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
2026
2027 const KFileItem fileItemB = m_model->fileItem(1);
2028
2029 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
2030 KFileItemList list;
2031 list << fileItemB << fileItemB;
2032 m_model->slotItemsDeleted(list);
2033
2034 QVERIFY(m_model->isConsistent());
2035 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
2036 }
2037
2038 void KFileItemModelTest::testInsertAfterExpand()
2039 {
2040 m_model->m_dirLister->setAutoUpdate(true);
2041
2042 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
2043 QVERIFY(itemsInsertedSpy.isValid());
2044 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
2045 QVERIFY(itemsRemovedSpy.isValid());
2046 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
2047 QVERIFY(loadingCompletedSpy.isValid());
2048
2049 // Test expanding subfolders in a folder with the items "a/", "a/a/"
2050 QSet<QByteArray> originalModelRoles = m_model->roles();
2051 QSet<QByteArray> modelRoles = originalModelRoles;
2052 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
2053 m_model->setRoles(modelRoles);
2054
2055 m_testDir->createFile("a/b/1");
2056
2057 m_model->loadDirectory(m_testDir->url());
2058 QVERIFY(itemsInsertedSpy.wait());
2059
2060 // So far, the model contains only "a/"
2061 QCOMPARE(m_model->count(), 1);
2062 QVERIFY(m_model->isExpandable(0));
2063 QVERIFY(!m_model->isExpanded(0));
2064 QVERIFY(m_model->expandedDirectories().empty());
2065
2066 QCOMPARE(itemsInsertedSpy.count(), 1);
2067 {
2068 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
2069 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1)); // 1 new item "a/" with index 0
2070 QCOMPARE(m_model->expandedParentsCount(0), 0);
2071
2072 }
2073 itemsInsertedSpy.clear();
2074
2075 // Expand the folder "a/" -> "a/b" become visible
2076 m_model->setExpanded(0, true);
2077 QVERIFY(m_model->isExpanded(0));
2078 QVERIFY(itemsInsertedSpy.wait());
2079 QCOMPARE(m_model->count(), 2); // 3 items: "a/", "a/a/"
2080 QCOMPARE(m_model->expandedDirectories(), QSet<QUrl>({QUrl::fromLocalFile(m_testDir->path() + "/a")}));
2081
2082 QCOMPARE(itemsInsertedSpy.count(), 1);
2083 {
2084 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
2085 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1)); // 1 new item "a/b" with index 1
2086 QCOMPARE(m_model->expandedParentsCount(1), 1);
2087
2088 }
2089 itemsInsertedSpy.clear();
2090
2091 // Expand "a/b" -> "a/b/1" becomes visible
2092 m_model->setExpanded(1, true);
2093 QVERIFY(itemsInsertedSpy.wait());
2094 QCOMPARE(m_model->expandedDirectories(), QSet({QUrl::fromLocalFile(m_testDir->path() + "/a"),
2095 QUrl::fromLocalFile(m_testDir->path() + "/a/b")}));
2096
2097 QCOMPARE(itemsInsertedSpy.count(), 1);
2098 {
2099 KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value<KItemRangeList>();
2100 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/b/1" with index 2
2101 QCOMPARE(m_model->expandedParentsCount(2), 2);
2102 }
2103 itemsInsertedSpy.clear();
2104
2105 // Collapse "a" whilst leaving "b" expanded
2106 m_model->setExpanded(0, false);
2107
2108 // Insert additional files into "a/b/"
2109 m_testDir->createFile("a/b/2");
2110 #if KIO_VERSION < QT_VERSION_CHECK(5, 92, 0)
2111 QEXPECT_FAIL("", "Requires new API from frameworks", Abort);
2112 #endif
2113
2114 QVERIFY(!itemsInsertedSpy.wait(5000));
2115
2116 QCOMPARE(itemsInModel(), {"a"});
2117
2118 m_model->setExpanded(0, true);;
2119 QTRY_COMPARE(itemsInModel(), QStringList({"a", "b", "1", "2"}));
2120
2121 QCOMPARE(m_model->expandedParentsCount(0), 0); // a
2122 QCOMPARE(m_model->expandedParentsCount(1), 1); // a/b
2123 QCOMPARE(m_model->expandedParentsCount(2), 2); // a/b/1
2124 QCOMPARE(m_model->expandedParentsCount(3), 2) ;// a/b/2
2125
2126 }
2127
2128 void KFileItemModelTest::testCurrentDirRemoved()
2129 {
2130 m_model->m_dirLister->setAutoUpdate(true);
2131 QSignalSpy currentDirectoryRemovedSpy(m_model, &KFileItemModel::currentDirectoryRemoved);
2132 QVERIFY(currentDirectoryRemovedSpy.isValid());
2133 QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
2134 QVERIFY(loadingCompletedSpy.isValid());
2135 QSignalSpy dirListerClearSpy(m_model->m_dirLister, &KCoreDirLister::clear);
2136 QVERIFY(dirListerClearSpy.isValid());
2137
2138 m_testDir->createFiles({"dir/a.txt", "dir/b.txt"});
2139 m_model->loadDirectory(QUrl::fromLocalFile(m_testDir->path() + "/dir/"));
2140 QVERIFY(loadingCompletedSpy.wait());
2141 QCOMPARE(m_model->count(), 2);
2142 QVERIFY(m_model->isConsistent());
2143
2144 m_testDir->removeDir("dir");
2145 QVERIFY(currentDirectoryRemovedSpy.wait());
2146
2147 // dirLister calls clear
2148 QCOMPARE(dirListerClearSpy.count(), 2);
2149 QVERIFY(m_model->isConsistent());
2150 QVERIFY(m_model->m_itemData.isEmpty());
2151 QCOMPARE(m_model->count(), 0);
2152 }
2153
2154 QStringList KFileItemModelTest::itemsInModel() const
2155 {
2156 QStringList items;
2157 for (int i = 0; i < m_model->count(); i++) {
2158 items << m_model->fileItem(i).text();
2159 }
2160 return items;
2161 }
2162
2163 QTEST_MAIN(KFileItemModelTest)
2164
2165 #include "kfileitemmodeltest.moc"