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