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