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