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