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