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