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