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