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