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