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