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