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