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