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