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