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