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