]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[dolphin.git] / src / tests / kfileitemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include <qtest_kde.h>
22
23 #include <KDirLister>
24 #include <kio/job.h>
25
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/private/kfileitemmodeldirlister.h"
28 #include "testdir.h"
29
30 void myMessageOutput(QtMsgType type, const char* msg)
31 {
32 switch (type) {
33 case QtDebugMsg:
34 break;
35 case QtWarningMsg:
36 break;
37 case QtCriticalMsg:
38 fprintf(stderr, "Critical: %s\n", msg);
39 break;
40 case QtFatalMsg:
41 fprintf(stderr, "Fatal: %s\n", msg);
42 abort();
43 default:
44 break;
45 }
46 }
47
48 namespace {
49 const int DefaultTimeout = 5000;
50 };
51
52 Q_DECLARE_METATYPE(KItemRange)
53 Q_DECLARE_METATYPE(KItemRangeList)
54 Q_DECLARE_METATYPE(QList<int>)
55
56 class KFileItemModelTest : public QObject
57 {
58 Q_OBJECT
59
60 private slots:
61 void init();
62 void cleanup();
63
64 void testDefaultRoles();
65 void testDefaultSortRole();
66 void testDefaultGroupedSorting();
67 void testNewItems();
68 void testRemoveItems();
69 void testDirLoadingCompleted();
70 void testSetData();
71 void testSetDataWithModifiedSortRole_data();
72 void testSetDataWithModifiedSortRole();
73 void testChangeSortRole();
74 void 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::testModelConsistencyWhenInsertingItems()
373 {
374 //QSKIP("Temporary disabled", SkipSingle);
375
376 // KFileItemModel prevents that inserting a punch of items sequentially
377 // results in an itemsInserted()-signal for each item. Instead internally
378 // a timeout is given that collects such operations and results in only
379 // one itemsInserted()-signal. However in this test we want to stress
380 // KFileItemModel to do a lot of insert operation and hence decrease
381 // the timeout to 1 millisecond.
382 m_testDir->createFile("1");
383 m_model->loadDirectory(m_testDir->url());
384 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
385 QCOMPARE(m_model->count(), 1);
386
387 // Insert 10 items for 20 times. After each insert operation the model consistency
388 // is checked.
389 QSet<int> insertedItems;
390 for (int i = 0; i < 20; ++i) {
391 QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
392
393 for (int j = 0; j < 10; ++j) {
394 int itemName = qrand();
395 while (insertedItems.contains(itemName)) {
396 itemName = qrand();
397 }
398 insertedItems.insert(itemName);
399
400 m_testDir->createFile(QString::number(itemName));
401 }
402
403 m_model->m_dirLister->updateDirectory(m_testDir->url());
404 if (spy.count() == 0) {
405 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
406 }
407
408 QVERIFY(m_model->isConsistent());
409 }
410
411 QCOMPARE(m_model->count(), 201);
412 }
413
414 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
415 {
416 QStringList files;
417 files << "B" << "E" << "G";
418 m_testDir->createFiles(files);
419
420 // Due to inserting the 3 items one item-range with index == 0 and
421 // count == 3 must be given
422 QSignalSpy spy1(m_model, SIGNAL(itemsInserted(KItemRangeList)));
423 m_model->loadDirectory(m_testDir->url());
424 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
425
426 QCOMPARE(spy1.count(), 1);
427 QList<QVariant> arguments = spy1.takeFirst();
428 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
429 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 3));
430
431 // The indexes of the item-ranges must always be related to the model before
432 // the items have been inserted. Having:
433 // 0 1 2
434 // B E G
435 // and inserting A, C, D, F the resulting model will be:
436 // 0 1 2 3 4 5 6
437 // A B C D E F G
438 // and the item-ranges must be:
439 // index: 0, count: 1 for A
440 // index: 1, count: 2 for B, C
441 // index: 2, count: 1 for G
442
443 files.clear();
444 files << "A" << "C" << "D" << "F";
445 m_testDir->createFiles(files);
446
447 QSignalSpy spy2(m_model, SIGNAL(itemsInserted(KItemRangeList)));
448 m_model->m_dirLister->updateDirectory(m_testDir->url());
449 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
450
451 QCOMPARE(spy2.count(), 1);
452 arguments = spy2.takeFirst();
453 itemRangeList = arguments.at(0).value<KItemRangeList>();
454 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
455 }
456
457 void KFileItemModelTest::testExpandItems()
458 {
459 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
460 // Besides testing the basic item expansion functionality, the test makes sure that
461 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
462 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
463 // first three characters.
464 QSet<QByteArray> modelRoles = m_model->roles();
465 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
466 m_model->setRoles(modelRoles);
467
468 QStringList files;
469 files << "a/a/1" << "a/a-1/1"; // missing folders are created automatically
470 m_testDir->createFiles(files);
471
472 // Store the URLs of all folders in a set.
473 QSet<KUrl> allFolders;
474 allFolders << KUrl(m_testDir->name() + 'a') << KUrl(m_testDir->name() + "a/a") << KUrl(m_testDir->name() + "a/a-1");
475
476 m_model->loadDirectory(m_testDir->url());
477 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
478
479 // So far, the model contains only "a/"
480 QCOMPARE(m_model->count(), 1);
481 QVERIFY(m_model->isExpandable(0));
482 QVERIFY(!m_model->isExpanded(0));
483 QVERIFY(m_model->expandedDirectories().empty());
484
485 QSignalSpy spyInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
486
487 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
488 m_model->setExpanded(0, true);
489 QVERIFY(m_model->isExpanded(0));
490 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
491 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
492 QCOMPARE(m_model->expandedDirectories(), QSet<KUrl>() << KUrl(m_testDir->name() + 'a'));
493
494 QCOMPARE(spyInserted.count(), 1);
495 KItemRangeList itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
496 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
497
498 QVERIFY(m_model->isExpandable(1));
499 QVERIFY(!m_model->isExpanded(1));
500 QVERIFY(m_model->isExpandable(2));
501 QVERIFY(!m_model->isExpanded(2));
502
503 // Expand the folder "a/a/" -> "a/a/1" becomes visible
504 m_model->setExpanded(1, true);
505 QVERIFY(m_model->isExpanded(1));
506 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
507 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
508 QCOMPARE(m_model->expandedDirectories(), QSet<KUrl>() << KUrl(m_testDir->name() + 'a') << KUrl(m_testDir->name() + "a/a"));
509
510 QCOMPARE(spyInserted.count(), 1);
511 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
512 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
513
514 QVERIFY(!m_model->isExpandable(2));
515 QVERIFY(!m_model->isExpanded(2));
516
517 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
518 m_model->setExpanded(3, true);
519 QVERIFY(m_model->isExpanded(3));
520 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
521 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
522 QCOMPARE(m_model->expandedDirectories(), allFolders);
523
524 QCOMPARE(spyInserted.count(), 1);
525 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
526 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
527
528 QVERIFY(!m_model->isExpandable(4));
529 QVERIFY(!m_model->isExpanded(4));
530
531 QSignalSpy spyRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
532
533 // Collapse the top-level folder -> all other items should disappear
534 m_model->setExpanded(0, false);
535 QVERIFY(!m_model->isExpanded(0));
536 QCOMPARE(m_model->count(), 1);
537 QVERIFY(!m_model->expandedDirectories().contains(KUrl(m_testDir->name() + 'a'))); // TODO: Make sure that child URLs are also removed
538
539 QCOMPARE(spyRemoved.count(), 1);
540 itemRangeList = spyRemoved.takeFirst().at(0).value<KItemRangeList>();
541 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
542 QVERIFY(m_model->isConsistent());
543
544 // Clear the model, reload the folder and try to restore the expanded folders.
545 m_model->clear();
546 QCOMPARE(m_model->count(), 0);
547 QVERIFY(m_model->expandedDirectories().empty());
548
549 m_model->loadDirectory(m_testDir->url());
550 m_model->restoreExpandedDirectories(allFolders);
551 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
552 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
553 QVERIFY(m_model->isExpanded(0));
554 QVERIFY(m_model->isExpanded(1));
555 QVERIFY(!m_model->isExpanded(2));
556 QVERIFY(m_model->isExpanded(3));
557 QVERIFY(!m_model->isExpanded(4));
558 QCOMPARE(m_model->expandedDirectories(), allFolders);
559 QVERIFY(m_model->isConsistent());
560
561 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
562 // This is how DolphinView restores the expanded folders when navigating in history.
563 m_model->loadDirectory(KUrl(m_testDir->name() + "a/a/"));
564 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
565 QCOMPARE(m_model->count(), 1); // 1 item: "1"
566 m_model->restoreExpandedDirectories(allFolders);
567 m_model->loadDirectory(m_testDir->url());
568 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
569 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
570 QCOMPARE(m_model->expandedDirectories(), allFolders);
571 }
572
573 void KFileItemModelTest::testExpandParentItems()
574 {
575 // Create a tree structure of folders:
576 // a 1/
577 // a 1/b1/
578 // a 1/b1/c1/
579 // a2/
580 // a2/b2/
581 // a2/b2/c2/
582 // a2/b2/c2/d2/
583 QSet<QByteArray> modelRoles = m_model->roles();
584 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
585 m_model->setRoles(modelRoles);
586
587 QStringList files;
588 files << "a 1/b1/c1/file.txt" << "a2/b2/c2/d2/file.txt"; // missing folders are created automatically
589 m_testDir->createFiles(files);
590
591 m_model->loadDirectory(m_testDir->url());
592 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
593
594 // So far, the model contains only "a 1/" and "a2/".
595 QCOMPARE(m_model->count(), 2);
596 QVERIFY(m_model->expandedDirectories().empty());
597
598 // Expand the parents of "a2/b2/c2".
599 m_model->expandParentDirectories(KUrl(m_testDir->name() + "a2/b2/c2"));
600 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
601
602 // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/".
603 // It's important that only the parents of "a1/b1/c1" are expanded.
604 QCOMPARE(m_model->count(), 4);
605 QVERIFY(!m_model->isExpanded(0));
606 QVERIFY(m_model->isExpanded(1));
607 QVERIFY(m_model->isExpanded(2));
608 QVERIFY(!m_model->isExpanded(3));
609
610 // Expand the parents of "a 1/b1".
611 m_model->expandParentDirectories(KUrl(m_testDir->name() + "a 1/b1"));
612 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
613
614 // The model should now contain "a 1/", "a 1/b1/", "a2/", "a2/b2", and "a2/b2/c2/".
615 // It's important that only the parents of "a 1/b1/" and "a2/b2/c2/" are expanded.
616 QCOMPARE(m_model->count(), 5);
617 QVERIFY(m_model->isExpanded(0));
618 QVERIFY(!m_model->isExpanded(1));
619 QVERIFY(m_model->isExpanded(2));
620 QVERIFY(m_model->isExpanded(3));
621 QVERIFY(!m_model->isExpanded(4));
622 QVERIFY(m_model->isConsistent());
623 }
624
625 /**
626 * Renaming an expanded folder by prepending its name with a dot makes it
627 * hidden. Verify that this does not cause an inconsistent model state and
628 * a crash later on, see https://bugs.kde.org/show_bug.cgi?id=311947
629 */
630 void KFileItemModelTest::testMakeExpandedItemHidden()
631 {
632 QSet<QByteArray> modelRoles = m_model->roles();
633 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
634 m_model->setRoles(modelRoles);
635
636 QStringList files;
637 m_testDir->createFile("1a/2a/3a");
638 m_testDir->createFile("1a/2a/3b");
639 m_testDir->createFile("1a/2b");
640 m_testDir->createFile("1b");
641
642 m_model->loadDirectory(m_testDir->url());
643 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
644
645 // So far, the model contains only "1a/" and "1b".
646 QCOMPARE(m_model->count(), 2);
647 m_model->setExpanded(0, true);
648 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
649
650 // Now "1a/2a" and "1a/2b" have appeared.
651 QCOMPARE(m_model->count(), 4);
652 m_model->setExpanded(1, true);
653 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
654 QCOMPARE(m_model->count(), 6);
655
656 // Rename "1a/2" and make it hidden.
657 const QString oldPath = m_model->fileItem(0).url().path() + "/2a";
658 const QString newPath = m_model->fileItem(0).url().path() + "/.2a";
659
660 KIO::SimpleJob* job = KIO::rename(oldPath, newPath, KIO::HideProgressInfo);
661 bool ok = job->exec();
662 QVERIFY(ok);
663 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
664
665 // "1a/2" and its subfolders have disappeared now.
666 QVERIFY(m_model->isConsistent());
667 QCOMPARE(m_model->count(), 3);
668
669 m_model->setExpanded(0, false);
670 QCOMPARE(m_model->count(), 2);
671
672 }
673
674 void KFileItemModelTest::testSorting()
675 {
676 // Create some files with different sizes and modification times to check the different sorting options
677 QDateTime now = QDateTime::currentDateTime();
678
679 QSet<QByteArray> roles;
680 roles.insert("text");
681 roles.insert("isExpanded");
682 roles.insert("isExpandable");
683 roles.insert("expandedParentsCount");
684 m_model->setRoles(roles);
685
686 m_testDir->createDir("c/c-2");
687 m_testDir->createFile("c/c-2/c-3");
688 m_testDir->createFile("c/c-1");
689
690 m_testDir->createFile("a", "A file", now.addDays(-3));
691 m_testDir->createFile("b", "A larger file", now.addDays(0));
692 m_testDir->createDir("c", now.addDays(-2));
693 m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
694 m_testDir->createFile("e", "An even larger file", now.addDays(-4));
695 m_testDir->createFile(".f");
696
697 m_model->loadDirectory(m_testDir->url());
698 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
699
700 int index = m_model->index(KUrl(m_testDir->url().url() + 'c'));
701 m_model->setExpanded(index, true);
702 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
703
704 index = m_model->index(KUrl(m_testDir->url().url() + "c/c-2"));
705 m_model->setExpanded(index, true);
706 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
707
708 // Default: Sort by Name, ascending
709 QCOMPARE(m_model->sortRole(), QByteArray("text"));
710 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
711 QVERIFY(m_model->sortDirectoriesFirst());
712 QVERIFY(!m_model->showHiddenFiles());
713 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e");
714
715 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
716
717 // Sort by Name, ascending, 'Sort Folders First' disabled
718 m_model->setSortDirectoriesFirst(false);
719 QCOMPARE(m_model->sortRole(), QByteArray("text"));
720 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
721 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
722 QCOMPARE(spyItemsMoved.count(), 1);
723 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 6));
724 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
725
726 // Sort by Name, descending
727 m_model->setSortDirectoriesFirst(true);
728 m_model->setSortOrder(Qt::DescendingOrder);
729 QCOMPARE(m_model->sortRole(), QByteArray("text"));
730 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
731 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a");
732 QCOMPARE(spyItemsMoved.count(), 2);
733 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 6));
734 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2);
735 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
736 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
737
738 // Sort by Date, descending
739 m_model->setSortDirectoriesFirst(true);
740 m_model->setSortRole("date");
741 QCOMPARE(m_model->sortRole(), QByteArray("date"));
742 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
743 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e");
744 QCOMPARE(spyItemsMoved.count(), 1);
745 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
746 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 5 << 4 << 6);
747
748 // Sort by Date, ascending
749 m_model->setSortOrder(Qt::AscendingOrder);
750 QCOMPARE(m_model->sortRole(), QByteArray("date"));
751 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
752 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b");
753 QCOMPARE(spyItemsMoved.count(), 1);
754 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
755 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
756
757 // Sort by Date, ascending, 'Sort Folders First' disabled
758 m_model->setSortDirectoriesFirst(false);
759 QCOMPARE(m_model->sortRole(), QByteArray("date"));
760 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
761 QVERIFY(!m_model->sortDirectoriesFirst());
762 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b");
763 QCOMPARE(spyItemsMoved.count(), 1);
764 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 6));
765 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 4 << 5 << 3 << 0 << 1);
766
767 // Sort by Name, ascending, 'Sort Folders First' disabled
768 m_model->setSortRole("text");
769 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
770 QVERIFY(!m_model->sortDirectoriesFirst());
771 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e");
772 QCOMPARE(spyItemsMoved.count(), 1);
773 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 8));
774 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 0 << 2 << 3 << 4 << 5 << 6 << 1);
775
776 // Sort by Size, ascending, 'Sort Folders First' disabled
777 m_model->setSortRole("size");
778 QCOMPARE(m_model->sortRole(), QByteArray("size"));
779 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
780 QVERIFY(!m_model->sortDirectoriesFirst());
781 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
782 QCOMPARE(spyItemsMoved.count(), 1);
783 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(0, 8));
784 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 5 << 0 << 3 << 1 << 2 << 7 << 6);
785
786 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
787 m_model->setSortDirectoriesFirst(true);
788 QCOMPARE(m_model->sortRole(), QByteArray("size"));
789 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
790 QVERIFY(m_model->sortDirectoriesFirst());
791 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "e" << "d");
792 QCOMPARE(spyItemsMoved.count(), 0);
793
794 // Sort by Size, descending, 'Sort Folders First' enabled
795 m_model->setSortOrder(Qt::DescendingOrder);
796 QCOMPARE(m_model->sortRole(), QByteArray("size"));
797 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
798 QVERIFY(m_model->sortDirectoriesFirst());
799 QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a");
800 QCOMPARE(spyItemsMoved.count(), 1);
801 QCOMPARE(spyItemsMoved.first().at(0).value<KItemRange>(), KItemRange(4, 4));
802 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 7 << 6 << 5 << 4);
803
804 // TODO: Sort by other roles; show/hide hidden files
805 }
806
807 void KFileItemModelTest::testIndexForKeyboardSearch()
808 {
809 QStringList files;
810 files << "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
811 m_testDir->createFiles(files);
812
813 m_model->loadDirectory(m_testDir->url());
814 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
815
816 // Search from index 0
817 QCOMPARE(m_model->indexForKeyboardSearch("a", 0), 0);
818 QCOMPARE(m_model->indexForKeyboardSearch("aa", 0), 1);
819 QCOMPARE(m_model->indexForKeyboardSearch("i", 0), 2);
820 QCOMPARE(m_model->indexForKeyboardSearch("image", 0), 2);
821 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 0), 2);
822 QCOMPARE(m_model->indexForKeyboardSearch("image.png", 0), 3);
823 QCOMPARE(m_model->indexForKeyboardSearch("t", 0), 4);
824 QCOMPARE(m_model->indexForKeyboardSearch("text", 0), 4);
825 QCOMPARE(m_model->indexForKeyboardSearch("text1", 0), 5);
826 QCOMPARE(m_model->indexForKeyboardSearch("text2", 0), 6);
827 QCOMPARE(m_model->indexForKeyboardSearch("text11", 0), 7);
828
829 // Start a search somewhere in the middle
830 QCOMPARE(m_model->indexForKeyboardSearch("a", 1), 1);
831 QCOMPARE(m_model->indexForKeyboardSearch("i", 3), 3);
832 QCOMPARE(m_model->indexForKeyboardSearch("t", 5), 5);
833 QCOMPARE(m_model->indexForKeyboardSearch("text1", 6), 7);
834
835 // Test searches that go past the last item back to index 0
836 QCOMPARE(m_model->indexForKeyboardSearch("a", 2), 0);
837 QCOMPARE(m_model->indexForKeyboardSearch("i", 7), 2);
838 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 3), 2);
839 QCOMPARE(m_model->indexForKeyboardSearch("text2", 7), 6);
840
841 // Test searches that yield no result
842 QCOMPARE(m_model->indexForKeyboardSearch("aaa", 0), -1);
843 QCOMPARE(m_model->indexForKeyboardSearch("b", 0), -1);
844 QCOMPARE(m_model->indexForKeyboardSearch("image.svg", 0), -1);
845 QCOMPARE(m_model->indexForKeyboardSearch("text3", 0), -1);
846 QCOMPARE(m_model->indexForKeyboardSearch("text3", 5), -1);
847
848 // Test upper case searches (note that search is case insensitive)
849 QCOMPARE(m_model->indexForKeyboardSearch("A", 0), 0);
850 QCOMPARE(m_model->indexForKeyboardSearch("aA", 0), 1);
851 QCOMPARE(m_model->indexForKeyboardSearch("TexT", 5), 5);
852 QCOMPARE(m_model->indexForKeyboardSearch("IMAGE", 4), 2);
853
854 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
855 }
856
857 void KFileItemModelTest::testNameFilter()
858 {
859 QStringList files;
860 files << "A1" << "A2" << "Abc" << "Bcd" << "Cde";
861 m_testDir->createFiles(files);
862
863 m_model->loadDirectory(m_testDir->url());
864 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
865
866 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
867 QCOMPARE(m_model->count(), 3);
868
869 m_model->setNameFilter("A2"); // Shows only A2
870 QCOMPARE(m_model->count(), 1);
871
872 m_model->setNameFilter("A2"); // Shows only A1
873 QCOMPARE(m_model->count(), 1);
874
875 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
876 QCOMPARE(m_model->count(), 2);
877
878 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
879 QCOMPARE(m_model->count(), 2);
880
881 m_model->setNameFilter(QString()); // Shows again all items
882 QCOMPARE(m_model->count(), 5);
883 }
884
885 /**
886 * Verifies that we do not crash when adding a KFileItem with an empty path.
887 * Before this issue was fixed, KFileItemModel::expandedParentsCountCompare()
888 * tried to always read the first character of the path, even if the path is empty.
889 */
890 void KFileItemModelTest::testEmptyPath()
891 {
892 QSet<QByteArray> roles;
893 roles.insert("text");
894 roles.insert("isExpanded");
895 roles.insert("isExpandable");
896 roles.insert("expandedParentsCount");
897 m_model->setRoles(roles);
898
899 const KUrl emptyUrl;
900 QVERIFY(emptyUrl.path().isEmpty());
901
902 const KUrl url("file:///test/");
903
904 KFileItemList items;
905 items << KFileItem(emptyUrl, QString(), KFileItem::Unknown) << KFileItem(url, QString(), KFileItem::Unknown);
906 m_model->slotItemsAdded(emptyUrl, items);
907 m_model->slotCompleted();
908 }
909
910 /**
911 * Verifies that the 'isExpanded' state of folders does not change when the
912 * 'refreshItems' signal is received, see https://bugs.kde.org/show_bug.cgi?id=299675.
913 */
914 void KFileItemModelTest::testRefreshExpandedItem()
915 {
916 QSet<QByteArray> modelRoles = m_model->roles();
917 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
918 m_model->setRoles(modelRoles);
919
920 QStringList files;
921 files << "a/1" << "a/2" << "3" << "4";
922 m_testDir->createFiles(files);
923
924 m_model->loadDirectory(m_testDir->url());
925 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
926 QCOMPARE(m_model->count(), 3); // "a/", "3", "4"
927
928 m_model->setExpanded(0, true);
929 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
930 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
931 QVERIFY(m_model->isExpanded(0));
932
933 QSignalSpy spyItemsChanged(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)));
934
935 const KFileItem item = m_model->fileItem(0);
936 m_model->slotRefreshItems(QList<QPair<KFileItem, KFileItem> >() << qMakePair(item, item));
937 QVERIFY(!spyItemsChanged.isEmpty());
938
939 QCOMPARE(m_model->count(), 5); // "a/", "a/1", "a/2", "3", "4"
940 QVERIFY(m_model->isExpanded(0));
941 }
942
943 /**
944 * Verify that removing hidden files and folders from the model does not
945 * result in a crash, see https://bugs.kde.org/show_bug.cgi?id=314046
946 */
947 void KFileItemModelTest::testRemoveHiddenItems()
948 {
949 m_testDir->createDir(".a");
950 m_testDir->createDir(".b");
951 m_testDir->createDir("c");
952 m_testDir->createDir("d");
953 m_testDir->createFiles(QStringList() << ".f" << ".g" << "h" << "i");
954
955 QSignalSpy spyItemsInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
956 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
957
958 m_model->setShowHiddenFiles(true);
959 m_model->loadDirectory(m_testDir->url());
960 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
961 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
962 QCOMPARE(spyItemsInserted.count(), 1);
963 QCOMPARE(spyItemsRemoved.count(), 0);
964 KItemRangeList itemRangeList = spyItemsInserted.takeFirst().at(0).value<KItemRangeList>();
965 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
966
967 m_model->setShowHiddenFiles(false);
968 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i");
969 QCOMPARE(spyItemsInserted.count(), 0);
970 QCOMPARE(spyItemsRemoved.count(), 1);
971 itemRangeList = spyItemsRemoved.takeFirst().at(0).value<KItemRangeList>();
972 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2));
973
974 m_model->setShowHiddenFiles(true);
975 QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i");
976 QCOMPARE(spyItemsInserted.count(), 1);
977 QCOMPARE(spyItemsRemoved.count(), 0);
978 itemRangeList = spyItemsInserted.takeFirst().at(0).value<KItemRangeList>();
979 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2));
980
981 m_model->clear();
982 QCOMPARE(itemsInModel(), QStringList());
983 QCOMPARE(spyItemsInserted.count(), 0);
984 QCOMPARE(spyItemsRemoved.count(), 1);
985 itemRangeList = spyItemsRemoved.takeFirst().at(0).value<KItemRangeList>();
986 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 8));
987
988 // Hiding hidden files makes the dir lister emit its itemsDeleted signal.
989 // Verify that this does not make the model crash.
990 m_model->setShowHiddenFiles(false);
991 }
992
993 /**
994 * Verify that filtered items are removed when their parent is collapsed.
995 */
996 void KFileItemModelTest::collapseParentOfHiddenItems()
997 {
998 QSet<QByteArray> modelRoles = m_model->roles();
999 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1000 m_model->setRoles(modelRoles);
1001
1002 QStringList files;
1003 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1004 m_testDir->createFiles(files);
1005
1006 m_model->loadDirectory(m_testDir->url());
1007 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1008 QCOMPARE(m_model->count(), 1); // Only "a/"
1009
1010 // Expand "a/".
1011 m_model->setExpanded(0, true);
1012 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1013 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1014
1015 // Expand "a/b/".
1016 m_model->setExpanded(1, true);
1017 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1018 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1019
1020 // Expand "a/b/c/".
1021 m_model->setExpanded(2, true);
1022 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1023 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"
1024
1025 // Set a name filter that matches nothing -> only the expanded folders remain.
1026 m_model->setNameFilter("xyz");
1027 QCOMPARE(m_model->count(), 3);
1028 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1029
1030 // Collapse the folder "a/".
1031 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1032 m_model->setExpanded(0, false);
1033 QCOMPARE(spyItemsRemoved.count(), 1);
1034 QCOMPARE(m_model->count(), 1);
1035 QCOMPARE(itemsInModel(), QStringList() << "a");
1036
1037 // Remove the filter -> no files should appear (and we should not get a crash).
1038 m_model->setNameFilter(QString());
1039 QCOMPARE(m_model->count(), 1);
1040 }
1041
1042 /**
1043 * Verify that filtered items are removed when their parent is deleted.
1044 */
1045 void KFileItemModelTest::removeParentOfHiddenItems()
1046 {
1047 QSet<QByteArray> modelRoles = m_model->roles();
1048 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1049 m_model->setRoles(modelRoles);
1050
1051 QStringList files;
1052 files << "a/1" << "a/b/1" << "a/b/c/1" << "a/b/c/d/1";
1053 m_testDir->createFiles(files);
1054
1055 m_model->loadDirectory(m_testDir->url());
1056 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1057 QCOMPARE(m_model->count(), 1); // Only "a/"
1058
1059 // Expand "a/".
1060 m_model->setExpanded(0, true);
1061 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1062 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/b/", "a/1"
1063
1064 // Expand "a/b/".
1065 m_model->setExpanded(1, true);
1066 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1067 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/b/", "a/b/c", "a/b/1", "a/1"
1068
1069 // Expand "a/b/c/".
1070 m_model->setExpanded(2, true);
1071 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1072 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"
1073
1074 // Set a name filter that matches nothing -> only the expanded folders remain.
1075 m_model->setNameFilter("xyz");
1076 QCOMPARE(m_model->count(), 3);
1077 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c");
1078
1079 // Simulate the deletion of the directory "a/b/".
1080 QSignalSpy spyItemsRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1081 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1082 QCOMPARE(spyItemsRemoved.count(), 1);
1083 QCOMPARE(m_model->count(), 1);
1084 QCOMPARE(itemsInModel(), QStringList() << "a");
1085
1086 // Remove the filter -> only the file "a/1" should appear.
1087 m_model->setNameFilter(QString());
1088 QCOMPARE(m_model->count(), 2);
1089 QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
1090 }
1091
1092 /**
1093 * Create a tree structure where parent-child relationships can not be
1094 * determined by parsing the URLs, and verify that KFileItemModel
1095 * handles them correctly.
1096 */
1097 void KFileItemModelTest::testGeneralParentChildRelationships()
1098 {
1099 QSet<QByteArray> modelRoles = m_model->roles();
1100 modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
1101 m_model->setRoles(modelRoles);
1102
1103 QStringList files;
1104 files << "parent1/realChild1/realGrandChild1" << "parent2/realChild2/realGrandChild2";
1105 m_testDir->createFiles(files);
1106
1107 m_model->loadDirectory(m_testDir->url());
1108 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1109 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2");
1110
1111 // Expand all folders.
1112 m_model->setExpanded(0, true);
1113 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1114 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2");
1115
1116 m_model->setExpanded(1, true);
1117 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1118 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2");
1119
1120 m_model->setExpanded(3, true);
1121 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1122 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2");
1123
1124 m_model->setExpanded(4, true);
1125 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1126 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "parent2" << "realChild2" << "realGrandChild2");
1127
1128 // Add some more children and grand-children.
1129 const KUrl parent1 = m_model->fileItem(0).url();
1130 const KUrl parent2 = m_model->fileItem(3).url();
1131 const KUrl realChild1 = m_model->fileItem(1).url();
1132 const KUrl realChild2 = m_model->fileItem(4).url();
1133
1134 m_model->slotItemsAdded(parent1, KFileItemList() << KFileItem(KUrl("child1"), QString(), KFileItem::Unknown));
1135 m_model->slotCompleted();
1136 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2");
1137
1138 m_model->slotItemsAdded(parent2, KFileItemList() << KFileItem(KUrl("child2"), QString(), KFileItem::Unknown));
1139 m_model->slotCompleted();
1140 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1141
1142 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown));
1143 m_model->slotCompleted();
1144 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1145
1146 m_model->slotItemsAdded(realChild1, KFileItemList() << KFileItem(KUrl("grandChild1"), QString(), KFileItem::Unknown));
1147 m_model->slotCompleted();
1148 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "realGrandChild2" << "child2");
1149
1150 m_model->slotItemsAdded(realChild2, KFileItemList() << KFileItem(KUrl("grandChild2"), QString(), KFileItem::Unknown));
1151 m_model->slotCompleted();
1152 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "grandChild1" << "realGrandChild1" << "child1" << "parent2" << "realChild2" << "grandChild2" << "realGrandChild2" << "child2");
1153
1154 // Set a name filter that matches nothing -> only expanded folders remain.
1155 QSignalSpy itemsRemovedSpy(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
1156 m_model->setNameFilter("xyz");
1157 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "realChild1" << "parent2" << "realChild2");
1158 QCOMPARE(itemsRemovedSpy.count(), 1);
1159 QList<QVariant> arguments = itemsRemovedSpy.takeFirst();
1160 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
1161 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 3) << KItemRange(7, 3));
1162
1163 // Collapse "parent1".
1164 m_model->setExpanded(0, false);
1165 QCOMPARE(itemsInModel(), QStringList() << "parent1" << "parent2" << "realChild2");
1166 QCOMPARE(itemsRemovedSpy.count(), 1);
1167 arguments = itemsRemovedSpy.takeFirst();
1168 itemRangeList = arguments.at(0).value<KItemRangeList>();
1169 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 1));
1170
1171 // Remove "parent2".
1172 m_model->slotItemsDeleted(KFileItemList() << m_model->fileItem(1));
1173 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1174 QCOMPARE(itemsRemovedSpy.count(), 1);
1175 arguments = itemsRemovedSpy.takeFirst();
1176 itemRangeList = arguments.at(0).value<KItemRangeList>();
1177 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2));
1178
1179 // Clear filter, verify that no items reappear.
1180 m_model->setNameFilter(QString());
1181 QCOMPARE(itemsInModel(), QStringList() << "parent1");
1182 }
1183
1184 void KFileItemModelTest::testNameRoleGroups()
1185 {
1186 QStringList files;
1187 files << "b.txt" << "c.txt" << "d.txt" << "e.txt";
1188
1189 m_testDir->createFiles(files);
1190
1191 m_model->setGroupedSorting(true);
1192 m_model->loadDirectory(m_testDir->url());
1193 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
1194 QCOMPARE(itemsInModel(), QStringList() << "b.txt" << "c.txt" << "d.txt" << "e.txt");
1195
1196 QList<QPair<int, QVariant> > expectedGroups;
1197 expectedGroups << QPair<int, QVariant>(0, QLatin1String("B"));
1198 expectedGroups << QPair<int, QVariant>(1, QLatin1String("C"));
1199 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1200 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1201 QCOMPARE(m_model->groups(), expectedGroups);
1202
1203 // Rename d.txt to a.txt.
1204 QHash<QByteArray, QVariant> data;
1205 data.insert("text", "a.txt");
1206 m_model->setData(2, data);
1207 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)), DefaultTimeout));
1208 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "e.txt");
1209
1210 expectedGroups.clear();
1211 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1212 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1213 expectedGroups << QPair<int, QVariant>(2, QLatin1String("C"));
1214 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1215 QCOMPARE(m_model->groups(), expectedGroups);
1216
1217 // Rename c.txt to d.txt.
1218 data.insert("text", "d.txt");
1219 m_model->setData(2, data);
1220 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(groupsChanged()), DefaultTimeout));
1221 QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.txt" << "e.txt");
1222
1223 expectedGroups.clear();
1224 expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
1225 expectedGroups << QPair<int, QVariant>(1, QLatin1String("B"));
1226 expectedGroups << QPair<int, QVariant>(2, QLatin1String("D"));
1227 expectedGroups << QPair<int, QVariant>(3, QLatin1String("E"));
1228 QCOMPARE(m_model->groups(), expectedGroups);
1229 }
1230
1231 QStringList KFileItemModelTest::itemsInModel() const
1232 {
1233 QStringList items;
1234 for (int i = 0; i < m_model->count(); i++) {
1235 items << m_model->data(i).value("text").toString();
1236 }
1237 return items;
1238 }
1239
1240 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
1241
1242 #include "kfileitemmodeltest.moc"