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