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