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