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