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