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