]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
The filenamesearch ioslave has been moved to kio-extras, remove it from Dolphin.
[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(QStringList() << "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(QStringList() << "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 KUrl urlA = fileItemA.url();
408 urlA.setFileName("a.txt");
409 fileItemA.setUrl(urlA);
410
411 m_model->slotRefreshItems(QList<QPair<KFileItem, KFileItem> >() << 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<KUrl> allFolders;
520 allFolders << KUrl(m_testDir->name() + 'a') << KUrl(m_testDir->name() + "a/a") << KUrl(m_testDir->name() + "a/a-1");
521
522 m_model->loadDirectory(m_testDir->url());
523 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
524
525 // So far, the model contains only "a/"
526 QCOMPARE(m_model->count(), 1);
527 QVERIFY(m_model->isExpandable(0));
528 QVERIFY(!m_model->isExpanded(0));
529 QVERIFY(m_model->expandedDirectories().empty());
530
531 QSignalSpy spyInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
532
533 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
534 m_model->setExpanded(0, true);
535 QVERIFY(m_model->isExpanded(0));
536 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
537 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
538 QCOMPARE(m_model->expandedDirectories(), QSet<KUrl>() << KUrl(m_testDir->name() + 'a'));
539
540 QCOMPARE(spyInserted.count(), 1);
541 KItemRangeList itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
542 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
543
544 QVERIFY(m_model->isExpandable(1));
545 QVERIFY(!m_model->isExpanded(1));
546 QVERIFY(m_model->isExpandable(2));
547 QVERIFY(!m_model->isExpanded(2));
548
549 // Expand the folder "a/a/" -> "a/a/1" becomes visible
550 m_model->setExpanded(1, true);
551 QVERIFY(m_model->isExpanded(1));
552 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
553 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
554 QCOMPARE(m_model->expandedDirectories(), QSet<KUrl>() << KUrl(m_testDir->name() + 'a') << KUrl(m_testDir->name() + "a/a"));
555
556 QCOMPARE(spyInserted.count(), 1);
557 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
558 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
559
560 QVERIFY(!m_model->isExpandable(2));
561 QVERIFY(!m_model->isExpanded(2));
562
563 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
564 m_model->setExpanded(3, true);
565 QVERIFY(m_model->isExpanded(3));
566 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
567 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
568 QCOMPARE(m_model->expandedDirectories(), allFolders);
569
570 QCOMPARE(spyInserted.count(), 1);
571 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
572 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
573
574 QVERIFY(!m_model->isExpandable(4));
575 QVERIFY(!m_model->isExpanded(4));
576
577 QSignalSpy spyRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
578
579 // Collapse the top-level folder -> all other items should disappear
580 m_model->setExpanded(0, false);
581 QVERIFY(!m_model->isExpanded(0));
582 QCOMPARE(m_model->count(), 1);
583 QVERIFY(!m_model->expandedDirectories().contains(KUrl(m_testDir->name() + 'a'))); // TODO: Make sure that child URLs are also removed
584
585 QCOMPARE(spyRemoved.count(), 1);
586 itemRangeList = spyRemoved.takeFirst().at(0).value<KItemRangeList>();
587 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
588 QVERIFY(m_model->isConsistent());
589
590 // Clear the model, reload the folder and try to restore the expanded folders.
591 m_model->clear();
592 QCOMPARE(m_model->count(), 0);
593 QVERIFY(m_model->expandedDirectories().empty());
594
595 m_model->loadDirectory(m_testDir->url());
596 m_model->restoreExpandedDirectories(allFolders);
597 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
598 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
599 QVERIFY(m_model->isExpanded(0));
600 QVERIFY(m_model->isExpanded(1));
601 QVERIFY(!m_model->isExpanded(2));
602 QVERIFY(m_model->isExpanded(3));
603 QVERIFY(!m_model->isExpanded(4));
604 QCOMPARE(m_model->expandedDirectories(), allFolders);
605 QVERIFY(m_model->isConsistent());
606
607 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
608 // This is how DolphinView restores the expanded folders when navigating in history.
609 m_model->loadDirectory(KUrl(m_testDir->name() + "a/a/"));
610 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
611 QCOMPARE(m_model->count(), 1); // 1 item: "1"
612 m_model->restoreExpandedDirectories(allFolders);
613 m_model->loadDirectory(m_testDir->url());
614 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
615 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
616 QCOMPARE(m_model->expandedDirectories(), allFolders);
617
618 // Remove all expanded items by changing the roles
619 spyRemoved.clear();
620 m_model->setRoles(originalModelRoles);
621 QVERIFY(!m_model->isExpanded(0));
622 QCOMPARE(m_model->count(), 1);
623 QVERIFY(!m_model->expandedDirectories().contains(KUrl(m_testDir->name() + 'a')));
624
625 QCOMPARE(spyRemoved.count(), 1);
626 itemRangeList = spyRemoved.takeFirst().at(0).value<KItemRangeList>();
627 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
628 QVERIFY(m_model->isConsistent());
629 }
630
631 void KFileItemModelTest::testExpandParentItems()
632 {
633 // Create a tree structure of folders:
634 // a 1/
635 // a 1/b1/
636 // a 1/b1/c1/
637 // a2/
638 // a2/b2/
639 // a2/b2/c2/
640 // a2/b2/c2/d2/
641 QSet<QByteArray> modelRoles = m_model->roles();
642 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
643 m_model->setRoles(modelRoles);
644
645 QStringList files;
646 files << "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
647 m_testDir->createFiles(files);
648
649 m_model->loadDirectory(m_testDir->url());
650 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
651
652 // So far, the model contains only "a 1/" and "a2/".
653 QCOMPARE(m_model->count(), 2);
654 QVERIFY(m_model->expandedDirectories().empty());
655
656 // Expand the parents of "a2/b2/c2".
657 m_model->expandParentDirectories(KUrl(m_testDir->name() + "a2/b2/c2"));
658 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
659
660 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
661 // It's important that only the parents of "a1/b1/c1" are expanded.
662 QCOMPARE(m_model->count(), 4);
663 QVERIFY(!m_model->isExpanded(0));
664 QVERIFY(m_model->isExpanded(1));
665 QVERIFY(m_model->isExpanded(2));
666 QVERIFY(!m_model->isExpanded(3));
667
668 // Expand the parents of "a 1/b1".
669 m_model->expandParentDirectories(KUrl(m_testDir->name() + "a 1/b1"));
670 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
671
672 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
673 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
674 QCOMPARE(m_model->count(), 5);
675 QVERIFY(m_model->isExpanded(0));
676 QVERIFY(!m_model->isExpanded(1));
677 QVERIFY(m_model->isExpanded(2));
678 QVERIFY(m_model->isExpanded(3));
679 QVERIFY(!m_model->isExpanded(4));
680 QVERIFY(m_model->isConsistent());
681
682 // Expand "a 1/b1/".
683 m_model->setExpanded(1, true);
684 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
685 QCOMPARE(m_model->count(), 6);
686 QVERIFY(m_model->isExpanded(0));
687 QVERIFY(m_model->isExpanded(1));
688 QVERIFY(!m_model->isExpanded(2));
689 QVERIFY(m_model->isExpanded(3));
690 QVERIFY(m_model->isExpanded(4));
691 QVERIFY(!m_model->isExpanded(5));
692 QVERIFY(m_model->isConsistent());
693
694 // Collapse "a 1/b1/" again, and verify that the previous state is restored.
695 m_model->setExpanded(1, false);
696 QCOMPARE(m_model->count(), 5);
697 QVERIFY(m_model->isExpanded(0));
698 QVERIFY(!m_model->isExpanded(1));
699 QVERIFY(m_model->isExpanded(2));
700 QVERIFY(m_model->isExpanded(3));
701 QVERIFY(!m_model->isExpanded(4));
702 QVERIFY(m_model->isConsistent());
703 }
704
705 /**
706 * Renaming an expanded folder by prepending its name with a dot makes it
707 * hidden. Verify that this does not cause an inconsistent model state and
708 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
709 */
710 void KFileItemModelTest::testMakeExpandedItemHidden()
711 {
712 QSet<QByteArray> modelRoles = m_model->roles();
713 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
714 m_model->setRoles(modelRoles);
715
716 QStringList files;
717 m_testDir->createFile("1a/2a/3a");
718 m_testDir->createFile("1a/2a/3b");
719 m_testDir->createFile("1a/2b");
720 m_testDir->createFile("1b");
721
722 m_model->loadDirectory(m_testDir->url());
723 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
724
725 // So far, the model contains only "1a/" and "1b".
726 QCOMPARE(m_model->count(), 2);
727 m_model->setExpanded(0, true);
728 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
729
730 // Now "1a/2a" and "1a/2b" have appeared.
731 QCOMPARE(m_model->count(), 4);
732 m_model->setExpanded(1, true);
733 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
734 QCOMPARE(m_model->count(), 6);
735
736 // Rename "1a/2" and make it hidden.
737 const QString oldPath = m_model->fileItem(0).url().path() + "/2a";
738 const QString newPath = m_model->fileItem(0).url().path() + "/.2a";
739
740 KIO::SimpleJob* job = KIO::rename(oldPath, newPath, KIO::HideProgressInfo);
741 bool ok = job->exec();
742 QVERIFY(ok);
743 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
744
745 // "1a/2" and its subfolders have disappeared now.
746 QVERIFY(m_model->isConsistent());
747 QCOMPARE(m_model->count(), 3);
748
749 m_model->setExpanded(0, false);
750 QCOMPARE(m_model->count(), 2);
751
752 }
753
754 void KFileItemModelTest::testRemoveFilteredExpandedItems()
755 {
756 QSet<QByteArray> originalModelRoles = m_model->roles();
757 QSet<QByteArray> modelRoles = originalModelRoles;
758 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
759 m_model->setRoles(modelRoles);
760
761 QStringList files;
762 files << "folder/child" << "file"; // missing folders are created automatically
763 m_testDir->createFiles(files);
764
765 m_model->loadDirectory(m_testDir->url());
766 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
767
768 // So far, the model contains only "folder/" and "file".
769 QCOMPARE(m_model->count(), 2);
770 QVERIFY(m_model->isExpandable(0));
771 QVERIFY(!m_model->isExpandable(1));
772 QVERIFY(!m_model->isExpanded(0));
773 QVERIFY(!m_model->isExpanded(1));
774 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
775
776 // Expand "folder" -> "folder/child" becomes visible.
777 m_model->setExpanded(0, true);
778 QVERIFY(m_model->isExpanded(0));
779 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
780 QCOMPARE(itemsInModel(), QStringList() << "folder" << "child" << "file");
781
782 // Add a name filter.
783 m_model->setNameFilter("f");
784 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
785
786 m_model->setNameFilter("fo");
787 QCOMPARE(itemsInModel(), QStringList() << "folder");
788
789 // Remove all expanded items by changing the roles
790 m_model->setRoles(originalModelRoles);
791 QVERIFY(!m_model->isExpanded(0));
792 QCOMPARE(itemsInModel(), QStringList() << "folder");
793
794 // Remove the name filter and verify that "folder/child" does not reappear.
795 m_model->setNameFilter(QString());
796 QCOMPARE(itemsInModel(), QStringList() << "folder" << "file");
797 }
798
799 void KFileItemModelTest::testSorting()
800 {
801 // Create some files with different sizes and modification times to check the different sorting options
802 QDateTime now = QDateTime::currentDateTime();
803
804 QSet<QByteArray> roles;
805 roles.insert("text");
806 roles.insert("isExpanded");
807 roles.insert("isExpandable");
808 roles.insert("expandedParentsCount");
809 m_model->setRoles(roles);
810
811 m_testDir->createDir("c/c-2");
812 m_testDir->createFile("c/c-2/c-3");
813 m_testDir->createFile("c/c-1");
814
815 m_testDir->createFile("a", "A file", now.addDays(-3));
816 m_testDir->createFile("b", "A larger file", now.addDays(0));
817 m_testDir->createDir("c", now.addDays(-2));
818 m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
819 m_testDir->createFile("e", "An even larger file", now.addDays(-4));
820 m_testDir->createFile(".f");
821
822 m_model->loadDirectory(m_testDir->url());
823 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
824
825 int index = m_model->index(KUrl(m_testDir->url().url() + 'c'));
826 m_model->setExpanded(index, true);
827 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
828
829 index = m_model->index(KUrl(m_testDir->url().url() + "c/c-2"));
830 m_model->setExpanded(index, true);
831 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
832
833 // Default: Sort by Name, ascending
834 QCOMPARE(m_model->sortRole(), QByteArray("text"));
835 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
836 QVERIFY(m_model->sortDirectoriesFirst());
837 QVERIFY(!m_model->showHiddenFiles());
838 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
839
840 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
841
842 // Sort by Name, ascending, 'Sort Folders First' disabled
843 m_model->setSortDirectoriesFirst(false);
844 QCOMPARE(m_model->sortRole(), QByteArray("text"));
845 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
846 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
847 QCOMPARE(spyItemsMoved.count(), 1);
848 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 6));
849 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
850
851 // Sort by Name, descending
852 m_model->setSortDirectoriesFirst(true);
853 m_model->setSortOrder(Qt::DescendingOrder);
854 QCOMPARE(m_model->sortRole(), QByteArray("text"));
855 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
856 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
857 QCOMPARE(spyItemsMoved.count(), 2);
858 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 6));
859 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2);
860 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
861 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
862
863 // Sort by Date, descending
864 m_model->setSortDirectoriesFirst(true);
865 m_model->setSortRole("date");
866 QCOMPARE(m_model->sortRole(), QByteArray("date"));
867 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
868 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
869 QCOMPARE(spyItemsMoved.count(), 1);
870 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
871 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 5 << 4 << 6);
872
873 // Sort by Date, ascending
874 m_model->setSortOrder(Qt::AscendingOrder);
875 QCOMPARE(m_model->sortRole(), QByteArray("date"));
876 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
877 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
878 QCOMPARE(spyItemsMoved.count(), 1);
879 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
880 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
881
882 // Sort by Date, ascending, 'Sort Folders First' disabled
883 m_model->setSortDirectoriesFirst(false);
884 QCOMPARE(m_model->sortRole(), QByteArray("date"));
885 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
886 QVERIFY(!m_model->sortDirectoriesFirst());
887 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
888 QCOMPARE(spyItemsMoved.count(), 1);
889 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 6));
890 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
891
892 // Sort by Name, ascending, 'Sort Folders First' disabled
893 m_model->setSortRole("text");
894 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
895 QVERIFY(!m_model->sortDirectoriesFirst());
896 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
897 QCOMPARE(spyItemsMoved.count(), 1);
898 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 8));
899 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
900
901 // Sort by Size, ascending, 'Sort Folders First' disabled
902 m_model->setSortRole("size");
903 QCOMPARE(m_model->sortRole(), QByteArray("size"));
904 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
905 QVERIFY(!m_model->sortDirectoriesFirst());
906 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
907 QCOMPARE(spyItemsMoved.count(), 1);
908 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 8));
909 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
910
911 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
912 m_model->setSortDirectoriesFirst(true);
913 QCOMPARE(m_model->sortRole(), QByteArray("size"));
914 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
915 QVERIFY(m_model->sortDirectoriesFirst());
916 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
917 QCOMPARE(spyItemsMoved.count(), 0);
918
919 // Sort by Size, descending, 'Sort Folders First' enabled
920 m_model->setSortOrder(Qt::DescendingOrder);
921 QCOMPARE(m_model->sortRole(), QByteArray("size"));
922 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
923 QVERIFY(m_model->sortDirectoriesFirst());
924 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
925 QCOMPARE(spyItemsMoved.count(), 1);
926 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
927 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
928
929 // TODO: Sort by other roles; show/hide hidden files
930 }
931
932 void KFileItemModelTest::testIndexForKeyboardSearch()
933 {
934 QStringList files;
935 files << "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
936 m_testDir->createFiles(files);
937
938 m_model->loadDirectory(m_testDir->url());
939 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
940
941 // Search from index 0
942 QCOMPARE(m_model->indexForKeyboardSearch("a", 0), 0);
943 QCOMPARE(m_model->indexForKeyboardSearch("aa", 0), 1);
944 QCOMPARE(m_model->indexForKeyboardSearch("i", 0), 2);
945 QCOMPARE(m_model->indexForKeyboardSearch("image", 0), 2);
946 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 0), 2);
947 QCOMPARE(m_model->indexForKeyboardSearch("image.png", 0), 3);
948 QCOMPARE(m_model->indexForKeyboardSearch("t", 0), 4);
949 QCOMPARE(m_model->indexForKeyboardSearch("text", 0), 4);
950 QCOMPARE(m_model->indexForKeyboardSearch("text1", 0), 5);
951 QCOMPARE(m_model->indexForKeyboardSearch("text2", 0), 6);
952 QCOMPARE(m_model->indexForKeyboardSearch("text11", 0), 7);
953
954 // Start a search somewhere in the middle
955 QCOMPARE(m_model->indexForKeyboardSearch("a", 1), 1);
956 QCOMPARE(m_model->indexForKeyboardSearch("i", 3), 3);
957 QCOMPARE(m_model->indexForKeyboardSearch("t", 5), 5);
958 QCOMPARE(m_model->indexForKeyboardSearch("text1", 6), 7);
959
960 // Test searches that go past the last item back to index 0
961 QCOMPARE(m_model->indexForKeyboardSearch("a", 2), 0);
962 QCOMPARE(m_model->indexForKeyboardSearch("i", 7), 2);
963 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 3), 2);
964 QCOMPARE(m_model->indexForKeyboardSearch("text2", 7), 6);
965
966 // Test searches that yield no result
967 QCOMPARE(m_model->indexForKeyboardSearch("aaa", 0), -1);
968 QCOMPARE(m_model->indexForKeyboardSearch("b", 0), -1);
969 QCOMPARE(m_model->indexForKeyboardSearch("image.svg", 0), -1);
970 QCOMPARE(m_model->indexForKeyboardSearch("text3", 0), -1);
971 QCOMPARE(m_model->indexForKeyboardSearch("text3", 5), -1);
972
973 // Test upper case searches (note that search is case insensitive)
974 QCOMPARE(m_model->indexForKeyboardSearch("A", 0), 0);
975 QCOMPARE(m_model->indexForKeyboardSearch("aA", 0), 1);
976 QCOMPARE(m_model->indexForKeyboardSearch("TexT", 5), 5);
977 QCOMPARE(m_model->indexForKeyboardSearch("IMAGE", 4), 2);
978
979 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
980 }
981
982 void KFileItemModelTest::testNameFilter()
983 {
984 QStringList files;
985 files << "A1" << "A2" << "Abc" << "Bcd" << "Cde";
986 m_testDir->createFiles(files);
987
988 m_model->loadDirectory(m_testDir->url());
989 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
990
991 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
992 QCOMPARE(m_model->count(), 3);
993
994 m_model->setNameFilter("A2"); // Shows only A2
995 QCOMPARE(m_model->count(), 1);
996
997 m_model->setNameFilter("A2"); // Shows only A1
998 QCOMPARE(m_model->count(), 1);
999
1000 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
1001 QCOMPARE(m_model->count(), 2);
1002
1003 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
1004 QCOMPARE(m_model->count(), 2);
1005
1006 m_model->setNameFilter(QString()); // Shows again all items
1007 QCOMPARE(m_model->count(), 5);
1008 }
1009
1010 /**
1011 * Verifies that we do not crash when adding a KFileItem with an empty path.
1012 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
1013 * tried to always read the first character of the path, even if the path is empty.
1014 */
1015 void KFileItemModelTest::testEmptyPath()
1016 {
1017 QSet<QByteArray> roles;
1018 roles.insert("text");
1019 roles.insert("isExpanded");
1020 roles.insert("isExpandable");
1021 roles.insert("expandedParentsCount");
1022 m_model->setRoles(roles);
1023
1024 const KUrl emptyUrl;
1025 QVERIFY(emptyUrl.path().isEmpty());
1026
1027 const KUrl url("file:///test/");
1028
1029 KFileItemList items;
1030 items << KFileItem(emptyUrl, QString(), KFileItem::Unknown) << KFileItem(url, QString(), KFileItem::Unknown);
1031 m_model->slotItemsAdded(emptyUrl, items);
1032 m_model->slotCompleted();
1033 }
1034
1035 /**
1036 * Verifies that the 'isExpanded' state of folders does not change when the
1037 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
1038 */
1039 void KFileItemModelTest::testRefreshExpandedItem()
1040 {
1041 QSet<QByteArray> modelRoles = m_model->roles();
1042 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1043 m_model->setRoles(modelRoles);
1044
1045 QStringList files;
1046 files << "a/1" << "a/2" << "3" << "4";
1047 m_testDir->createFiles(files);
1048
1049 m_model->loadDirectory(m_testDir->url());
1050 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1051 QCOMPARE(m_model->count(), 3); // "a/", "3", "4"
1052
1053 m_model->setExpanded(0, true);
1054 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1055 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1056 QVERIFY(m_model->isExpanded(0));
1057
1058 QSignalSpy spyItemsChanged(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)));
1059
1060 const KFileItem item = m_model->fileItem(0);
1061 m_model->slotRefreshItems(QList<QPair<KFileItem, KFileItem> >() << qMakePair(item, item));
1062 QVERIFY(!spyItemsChanged.isEmpty());
1063
1064 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
1065 QVERIFY(m_model->isExpanded(0));
1066 }
1067
1068 /**
1069 * Verify that removing hidden files and folders from the model does not
1070 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
1071 */
1072 void KFileItemModelTest::testRemoveHiddenItems()
1073 {
1074 m_testDir->createDir(".a");
1075 m_testDir->createDir(".b");
1076 m_testDir->createDir("c");
1077 m_testDir->createDir("d");
1078 m_testDir->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
1079
1080 QSignalSpy spyItemsInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
1081 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1082
1083 m_model->setShowHiddenFiles(true);
1084 m_model->loadDirectory(m_testDir->url());
1085 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1086 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1087 QCOMPARE(spyItemsInserted.count(), 1);
1088 QCOMPARE(spyItemsRemoved.count(), 0);
1089 KItemRangeList itemRangeList = spyItemsInserted.takeFirst().at(0).value<KItemRangeList>();
1090 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
1091
1092 m_model->setShowHiddenFiles(false);
1093 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
1094 QCOMPARE(spyItemsInserted.count(), 0);
1095 QCOMPARE(spyItemsRemoved.count(), 1);
1096 itemRangeList = spyItemsRemoved.takeFirst().at(0).value<KItemRangeList>();
1097 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
1098
1099 m_model->setShowHiddenFiles(true);
1100 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
1101 QCOMPARE(spyItemsInserted.count(), 1);
1102 QCOMPARE(spyItemsRemoved.count(), 0);
1103 itemRangeList = spyItemsInserted.takeFirst().at(0).value<KItemRangeList>();
1104 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
1105
1106 m_model->clear();
1107 QCOMPARE(itemsInModel(), QStringList());
1108 QCOMPARE(spyItemsInserted.count(), 0);
1109 QCOMPARE(spyItemsRemoved.count(), 1);
1110 itemRangeList = spyItemsRemoved.takeFirst().at(0).value<KItemRangeList>();
1111 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
1112
1113 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
1114 // Verify that this does not make the model crash.
1115 m_model->setShowHiddenFiles(false);
1116 }
1117
1118 /**
1119 * Verify that filtered items are removed when their parent is collapsed.
1120 */
1121 void KFileItemModelTest::collapseParentOfHiddenItems()
1122 {
1123 QSet<QByteArray> modelRoles = m_model->roles();
1124 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1125 m_model->setRoles(modelRoles);
1126
1127 QStringList files;
1128 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1129 m_testDir->createFiles(files);
1130
1131 m_model->loadDirectory(m_testDir->url());
1132 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1133 QCOMPARE(m_model->count(), 1); // Only "a/"
1134
1135 // Expand "a/".
1136 m_model->setExpanded(0, true);
1137 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1138 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1139
1140 // Expand "a/b/".
1141 m_model->setExpanded(1, true);
1142 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1143 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1144
1145 // Expand "a/b/c/".
1146 m_model->setExpanded(2, true);
1147 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1148 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"
1149
1150 // Set a name filter that matches nothing -> only the expanded folders remain.
1151 m_model->setNameFilter("xyz");
1152 QCOMPARE(m_model->count(), 3);
1153 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1154
1155 // Collapse the folder "a/".
1156 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1157 m_model->setExpanded(0, false);
1158 QCOMPARE(spyItemsRemoved.count(), 1);
1159 QCOMPARE(m_model->count(), 1);
1160 QCOMPARE(itemsInModel(), QStringList() << "a");
1161
1162 // Remove the filter -> no files should appear (and we should not get a crash).
1163 m_model->setNameFilter(QString());
1164 QCOMPARE(m_model->count(), 1);
1165 }
1166
1167 /**
1168 * Verify that filtered items are removed when their parent is deleted.
1169 */
1170 void KFileItemModelTest::removeParentOfHiddenItems()
1171 {
1172 QSet<QByteArray> modelRoles = m_model->roles();
1173 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1174 m_model->setRoles(modelRoles);
1175
1176 QStringList files;
1177 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1178 m_testDir->createFiles(files);
1179
1180 m_model->loadDirectory(m_testDir->url());
1181 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1182 QCOMPARE(m_model->count(), 1); // Only "a/"
1183
1184 // Expand "a/".
1185 m_model->setExpanded(0, true);
1186 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1187 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1188
1189 // Expand "a/b/".
1190 m_model->setExpanded(1, true);
1191 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1192 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1193
1194 // Expand "a/b/c/".
1195 m_model->setExpanded(2, true);
1196 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1197 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"
1198
1199 // Set a name filter that matches nothing -> only the expanded folders remain.
1200 m_model->setNameFilter("xyz");
1201 QCOMPARE(m_model->count(), 3);
1202 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1203
1204 // Simulate the deletion of the directory "a/b/".
1205 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1206 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1207 QCOMPARE(spyItemsRemoved.count(), 1);
1208 QCOMPARE(m_model->count(), 1);
1209 QCOMPARE(itemsInModel(), QStringList() << "a");
1210
1211 // Remove the filter -> only the file "a/1" should appear.
1212 m_model->setNameFilter(QString());
1213 QCOMPARE(m_model->count(), 2);
1214 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1215 }
1216
1217 /**
1218 * Create a tree structure where parent-child relationships can not be
1219 * determined by parsing the URLs, and verify that KFileItemModel
1220 * handles them correctly.
1221 */
1222 void KFileItemModelTest::testGeneralParentChildRelationships()
1223 {
1224 QSet<QByteArray> modelRoles = m_model->roles();
1225 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1226 m_model->setRoles(modelRoles);
1227
1228 QStringList files;
1229 files << "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1230 m_testDir->createFiles(files);
1231
1232 m_model->loadDirectory(m_testDir->url());
1233 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1234 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1235
1236 // Expand all folders.
1237 m_model->setExpanded(0, true);
1238 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1239 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1240
1241 m_model->setExpanded(1, true);
1242 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1243 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1244
1245 m_model->setExpanded(3, true);
1246 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1247 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1248
1249 m_model->setExpanded(4, true);
1250 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1251 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1252
1253 // Add some more children and grand-children.
1254 const KUrl parent1 = m_model->fileItem(0).url();
1255 const KUrl parent2 = m_model->fileItem(3).url();
1256 const KUrl realChild1 = m_model->fileItem(1).url();
1257 const KUrl realChild2 = m_model->fileItem(4).url();
1258
1259 m_model->slotItemsAdded(parent1, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown));
1260 m_model->slotCompleted();
1261 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1262
1263 m_model->slotItemsAdded(parent2, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown));
1264 m_model->slotCompleted();
1265 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1266
1267 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown));
1268 m_model->slotCompleted();
1269 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1270
1271 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown));
1272 m_model->slotCompleted();
1273 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1274
1275 m_model->slotItemsAdded(realChild2, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown));
1276 m_model->slotCompleted();
1277 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1278
1279 // Set a name filter that matches nothing -> only expanded folders remain.
1280 QSignalSpy itemsRemovedSpy(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1281 m_model->setNameFilter("xyz");
1282 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1283 QCOMPARE(itemsRemovedSpy.count(), 1);
1284 QList<QVariant> arguments = itemsRemovedSpy.takeFirst();
1285 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
1286 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1287
1288 // Collapse "parent1".
1289 m_model->setExpanded(0, false);
1290 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1291 QCOMPARE(itemsRemovedSpy.count(), 1);
1292 arguments = itemsRemovedSpy.takeFirst();
1293 itemRangeList = arguments.at(0).value<KItemRangeList>();
1294 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1));
1295
1296 // Remove "parent2".
1297 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1298 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1299 QCOMPARE(itemsRemovedSpy.count(), 1);
1300 arguments = itemsRemovedSpy.takeFirst();
1301 itemRangeList = arguments.at(0).value<KItemRangeList>();
1302 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
1303
1304 // Clear filter, verify that no items reappear.
1305 m_model->setNameFilter(QString());
1306 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1307 }
1308
1309 void KFileItemModelTest::testNameRoleGroups()
1310 {
1311 QStringList files;
1312 files << "b.txt" << "c.txt" << "d.txt" << "e.txt";
1313
1314 m_testDir->createFiles(files);
1315
1316 m_model->setGroupedSorting(true);
1317 m_model->loadDirectory(m_testDir->url());
1318 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1319 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1320
1321 QList<QPair<int, QVariant> > expectedGroups;
1322 expectedGroups << QPair<int, QVariant>(0, QLatin1String("B"));
1323 expectedGroups << QPair<int, QVariant>(1, QLatin1String("C"));
1324 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1325 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1326 QCOMPARE(m_model->groups(), expectedGroups);
1327
1328 // Rename d.txt to a.txt.
1329 QHash<QByteArray, QVariant> data;
1330 data.insert("text", "a.txt");
1331 m_model->setData(2, data);
1332 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)), DefaultTimeout));
1333 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1334
1335 expectedGroups.clear();
1336 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1337 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1338 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1339 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1340 QCOMPARE(m_model->groups(), expectedGroups);
1341
1342 // Rename c.txt to d.txt.
1343 data.insert("text", "d.txt");
1344 m_model->setData(2, data);
1345 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(groupsChanged()), DefaultTimeout));
1346 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1347
1348 expectedGroups.clear();
1349 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1350 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1351 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1352 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1353 QCOMPARE(m_model->groups(), expectedGroups);
1354
1355 // Change d.txt back to c.txt, but this time using the dir lister's refreshItems() signal.
1356 const KFileItem fileItemD = m_model->fileItem(2);
1357 KFileItem fileItemC = fileItemD;
1358 KUrl urlC = fileItemC.url();
1359 urlC.setFileName("c.txt");
1360 fileItemC.setUrl(urlC);
1361
1362 m_model->slotRefreshItems(QList<QPair<KFileItem, KFileItem> >() << qMakePair(fileItemD, fileItemC));
1363 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(groupsChanged()), DefaultTimeout));
1364 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1365
1366 expectedGroups.clear();
1367 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1368 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1369 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1370 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1371 QCOMPARE(m_model->groups(), expectedGroups);
1372 }
1373
1374 void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
1375 {
1376 QSet<QByteArray> modelRoles = m_model->roles();
1377 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1378 m_model->setRoles(modelRoles);
1379
1380 QStringList files;
1381 files << "a/b.txt" << "a/c.txt" << "d/e.txt" << "d/f.txt";
1382
1383 m_testDir->createFiles(files);
1384
1385 m_model->setGroupedSorting(true);
1386 m_model->loadDirectory(m_testDir->url());
1387 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1388 QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
1389
1390 QList<QPair<int, QVariant> > expectedGroups;
1391 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1392 expectedGroups << QPair<int, QVariant>(1, QLatin1String("D"));
1393 QCOMPARE(m_model->groups(), expectedGroups);
1394
1395 // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").
1396 expectedGroups.clear();
1397 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1398 expectedGroups << QPair<int, QVariant>(3, QLatin1String("D"));
1399
1400 m_model->setExpanded(0, true);
1401 QVERIFY(m_model->isExpanded(0));
1402 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1403 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
1404 QCOMPARE(m_model->groups(), expectedGroups);
1405
1406 m_model->setExpanded(3, true);
1407 QVERIFY(m_model->isExpanded(3));
1408 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1409 QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
1410 QCOMPARE(m_model->groups(), expectedGroups);
1411 }
1412
1413 void KFileItemModelTest::testInconsistentModel()
1414 {
1415 QSet<QByteArray> modelRoles = m_model->roles();
1416 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1417 m_model->setRoles(modelRoles);
1418
1419 QStringList files;
1420 files << "a/b/c1.txt" << "a/b/c2.txt";
1421
1422 m_testDir->createFiles(files);
1423
1424 m_model->loadDirectory(m_testDir->url());
1425 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1426 QCOMPARE(itemsInModel(), QStringList() << "a");
1427
1428 // Expand "a/" and "a/b/".
1429 m_model->setExpanded(0, true);
1430 QVERIFY(m_model->isExpanded(0));
1431 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1432 QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
1433
1434 m_model->setExpanded(1, true);
1435 QVERIFY(m_model->isExpanded(1));
1436 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1437 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
1438
1439 // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
1440 // Such a thing can in principle happen when performing a search, and there
1441 // are files which
1442 // (a) match the search string, and
1443 // (b) are children of a folder that matches the search string and is expanded.
1444 //
1445 // Note that the first item in the list of added items must be new (i.e., not
1446 // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
1447 // it receives items that are in the model already and ignore them.
1448 KUrl url(m_model->directory().url() + "/a2");
1449 KFileItem newItem(KFileItem::Unknown, KFileItem::Unknown, url);
1450
1451 KFileItemList items;
1452 items << newItem << m_model->fileItem(2) << m_model->fileItem(3);
1453 m_model->slotItemsAdded(m_model->directory(), items);
1454 m_model->slotCompleted();
1455 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
1456
1457 m_model->setExpanded(0, false);
1458
1459 // Test that the right items have been removed, see
1460 // https://bugs.kde.org/show_bug.cgi?id=324371
1461 QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
1462
1463 // Test that resorting does not cause a crash, see
1464 // https://bugs.kde.org/show_bug.cgi?id=325359
1465 // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
1466 m_model->resortAllItems();
1467
1468 }
1469
1470 void KFileItemModelTest::testChangeRolesForFilteredItems()
1471 {
1472 QSet<QByteArray> modelRoles = m_model->roles();
1473 modelRoles << "owner";
1474 m_model->setRoles(modelRoles);
1475
1476 QStringList files;
1477 files << "a.txt" << "aa.txt" << "aaa.txt";
1478 m_testDir->createFiles(files);
1479
1480 m_model->loadDirectory(m_testDir->url());
1481 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1482 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1483
1484 for (int index = 0; index < m_model->count(); ++index) {
1485 // All items should have the "text" and "owner" roles, but not "group".
1486 QVERIFY(m_model->data(index).contains("text"));
1487 QVERIFY(m_model->data(index).contains("owner"));
1488 QVERIFY(!m_model->data(index).contains("group"));
1489 }
1490
1491 // Add a filter, such that only "aaa.txt" remains in the model.
1492 m_model->setNameFilter("aaa");
1493 QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
1494
1495 // Add the "group" role.
1496 modelRoles << "group";
1497 m_model->setRoles(modelRoles);
1498
1499 // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
1500 m_model->setNameFilter("aa");
1501 QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
1502
1503 for (int index = 0; index < m_model->count(); ++index) {
1504 // All items should have the "text", "owner", and "group" roles.
1505 QVERIFY(m_model->data(index).contains("text"));
1506 QVERIFY(m_model->data(index).contains("owner"));
1507 QVERIFY(m_model->data(index).contains("group"));
1508 }
1509
1510 // Remove the "owner" role.
1511 modelRoles.remove("owner");
1512 m_model->setRoles(modelRoles);
1513
1514 // Clear the filter, and verify that all items have the expected roles
1515 m_model->setNameFilter(QString());
1516 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
1517
1518 for (int index = 0; index < m_model->count(); ++index) {
1519 // All items should have the "text" and "group" roles, but now "owner".
1520 QVERIFY(m_model->data(index).contains("text"));
1521 QVERIFY(!m_model->data(index).contains("owner"));
1522 QVERIFY(m_model->data(index).contains("group"));
1523 }
1524 }
1525
1526 void KFileItemModelTest::testChangeSortRoleWhileFiltering()
1527 {
1528 KFileItemList items;
1529
1530 KIO::UDSEntry entry;
1531 entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, 0100000); // S_IFREG might not be defined on non-Unix platforms.
1532 entry.insert(KIO::UDSEntry::UDS_ACCESS, 07777);
1533 entry.insert(KIO::UDSEntry::UDS_SIZE, 0);
1534 entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0);
1535 entry.insert(KIO::UDSEntry::UDS_GROUP, "group");
1536 entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, 0);
1537
1538 entry.insert(KIO::UDSEntry::UDS_NAME, "a.txt");
1539 entry.insert(KIO::UDSEntry::UDS_USER, "user-b");
1540 items.append(KFileItem(entry, m_testDir->url(), false, true));
1541
1542 entry.insert(KIO::UDSEntry::UDS_NAME, "b.txt");
1543 entry.insert(KIO::UDSEntry::UDS_USER, "user-c");
1544 items.append(KFileItem(entry, m_testDir->url(), false, true));
1545
1546 entry.insert(KIO::UDSEntry::UDS_NAME, "c.txt");
1547 entry.insert(KIO::UDSEntry::UDS_USER, "user-a");
1548 items.append(KFileItem(entry, m_testDir->url(), false, true));
1549
1550 m_model->slotItemsAdded(m_testDir->url(), items);
1551 m_model->slotCompleted();
1552
1553 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
1554
1555 // Add a filter.
1556 m_model->setNameFilter("a");
1557 QCOMPARE(itemsInModel(), QStringList() << "a.txt");
1558
1559 // Sort by "owner".
1560 m_model->setSortRole("owner");
1561
1562 // Clear the filter, and verify that the items are sorted correctly.
1563 m_model->setNameFilter(QString());
1564 QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
1565 }
1566
1567 void KFileItemModelTest::testRefreshFilteredItems()
1568 {
1569 QStringList files;
1570 files << "a.txt" << "b.txt" << "c.jpg" << "d.jpg";
1571 m_testDir->createFiles(files);
1572
1573 m_model->loadDirectory(m_testDir->url());
1574 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1575 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
1576
1577 const KFileItem fileItemC = m_model->fileItem(2);
1578
1579 // Show only the .txt files.
1580 m_model->setNameFilter(".txt");
1581 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
1582
1583 // Rename one of the .jpg files.
1584 KFileItem fileItemE = fileItemC;
1585 KUrl urlE = fileItemE.url();
1586 urlE.setFileName("e.jpg");
1587 fileItemE.setUrl(urlE);
1588
1589 m_model->slotRefreshItems(QList<QPair<KFileItem, KFileItem> >() << qMakePair(fileItemC, fileItemE));
1590
1591 // Show all files again, and verify that the model has updated the file name.
1592 m_model->setNameFilter(QString());
1593 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
1594 }
1595
1596 void KFileItemModelTest::testCreateMimeData()
1597 {
1598 QSet<QByteArray> modelRoles = m_model->roles();
1599 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1600 m_model->setRoles(modelRoles);
1601
1602 QStringList files;
1603 files << "a/1";
1604 m_testDir->createFiles(files);
1605
1606 m_model->loadDirectory(m_testDir->url());
1607 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1608 QCOMPARE(itemsInModel(), QStringList() << "a");
1609
1610 // Expand "a/".
1611 m_model->setExpanded(0, true);
1612 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1613 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1614
1615 // Verify that creating the MIME data for a child of an expanded folder does
1616 // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
1617 KItemSet selection;
1618 selection.insert(1);
1619 QMimeData* mimeData = m_model->createMimeData(selection);
1620 delete mimeData;
1621 }
1622
1623 void KFileItemModelTest::testCollapseFolderWhileLoading()
1624 {
1625 QSet<QByteArray> modelRoles = m_model->roles();
1626 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1627 m_model->setRoles(modelRoles);
1628
1629 QStringList files;
1630 files << "a2/b/c1.txt";
1631 m_testDir->createFiles(files);
1632
1633 m_model->loadDirectory(m_testDir->url());
1634 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1635 QCOMPARE(itemsInModel(), QStringList() << "a2");
1636
1637 // Expand "a2/".
1638 m_model->setExpanded(0, true);
1639 QVERIFY(m_model->isExpanded(0));
1640 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1641 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1642
1643 // Expand "a2/b/".
1644 m_model->setExpanded(1, true);
1645 QVERIFY(m_model->isExpanded(1));
1646 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1647 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1648
1649 // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
1650 // signal is not emitted yet.
1651 const KFileItem fileItemC1 = m_model->fileItem(2);
1652 KFileItem fileItemC2 = fileItemC1;
1653 KUrl urlC2 = fileItemC2.url();
1654 urlC2.setFileName("c2.txt");
1655 fileItemC2.setUrl(urlC2);
1656
1657 const KUrl urlB = m_model->fileItem(1).url();
1658 m_model->slotItemsAdded(urlB, KFileItemList() << fileItemC2);
1659 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
1660
1661 // Collapse "a2/". This should also remove all its (indirect) children from
1662 // the model and from the model's m_pendingItemsToInsert member.
1663 m_model->setExpanded(0, false);
1664 QCOMPARE(itemsInModel(), QStringList() << "a2");
1665
1666 // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
1667 // is still in m_pendingItemsToInsert, then we might get a crash, see
1668 // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
1669 // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
1670 // without parent in the model.
1671 m_model->slotCompleted();
1672 QCOMPARE(itemsInModel(), QStringList() << "a2");
1673
1674 // Expand "a2/" again.
1675 m_model->setExpanded(0, true);
1676 QVERIFY(m_model->isExpanded(0));
1677 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1678 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1679
1680 // Now simulate that a new folder "a1/" is appears, but that the dir lister's
1681 // completed() signal is not emitted yet.
1682 const KFileItem fileItemA2 = m_model->fileItem(0);
1683 KFileItem fileItemA1 = fileItemA2;
1684 KUrl urlA1 = fileItemA1.url();
1685 urlA1.setFileName("a1");
1686 fileItemA1.setUrl(urlA1);
1687
1688 m_model->slotItemsAdded(m_model->directory(), KFileItemList() << fileItemA1);
1689 QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
1690
1691 // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
1692 // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
1693 // confuse the code which collapses the folder.
1694 m_model->setExpanded(0, false);
1695 QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
1696 QVERIFY(!m_model->isExpanded(0));
1697 QVERIFY(!m_model->isExpanded(1));
1698 }
1699
1700 void KFileItemModelTest::testDeleteFileMoreThanOnce()
1701 {
1702 QStringList files;
1703 files << "a.txt" << "b.txt" << "c.txt" << "d.txt";
1704 m_testDir->createFiles(files);
1705
1706 m_model->loadDirectory(m_testDir->url());
1707 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1708 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
1709
1710 const KFileItem fileItemB = m_model->fileItem(1);
1711
1712 // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
1713 KFileItemList list;
1714 list << fileItemB << fileItemB;
1715 m_model->slotItemsDeleted(list);
1716
1717 QVERIFY(m_model->isConsistent());
1718 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
1719 }
1720
1721 QStringList KFileItemModelTest::itemsInModel() const
1722 {
1723 QStringList items;
1724 for (int i = 0; i < m_model->count(); i++) {
1725 items << m_model->fileItem(i).text();
1726 }
1727 return items;
1728 }
1729
1730 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
1731
1732 #include "kfileitemmodeltest.moc"