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