]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Fix crash #1 when filtering 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 "kitemviews/kfileitemmodel.h"
25 #include "testdir.h"
26
27 void myMessageOutput(QtMsgType type, const char* msg)
28 {
29 switch (type) {
30 case QtDebugMsg:
31 break;
32 case QtWarningMsg:
33 break;
34 case QtCriticalMsg:
35 fprintf(stderr, "Critical: %s\n", msg);
36 break;
37 case QtFatalMsg:
38 fprintf(stderr, "Fatal: %s\n", msg);
39 abort();
40 default:
41 break;
42 }
43 }
44
45 namespace {
46 const int DefaultTimeout = 5000;
47 };
48
49 Q_DECLARE_METATYPE(KItemRangeList)
50 Q_DECLARE_METATYPE(QList<int>)
51
52 class KFileItemModelTest : public QObject
53 {
54 Q_OBJECT
55
56 private slots:
57 void init();
58 void cleanup();
59
60 void testDefaultRoles();
61 void testDefaultSortRole();
62 void testDefaultGroupedSorting();
63 void testNewItems();
64 void testRemoveItems();
65 void testSetData();
66 void testSetDataWithModifiedSortRole_data();
67 void testSetDataWithModifiedSortRole();
68 void testModelConsistencyWhenInsertingItems();
69 void testItemRangeConsistencyWhenInsertingItems();
70 void testExpandItems();
71 void testSorting();
72
73 void testExpansionLevelsCompare_data();
74 void testExpansionLevelsCompare();
75
76 void testIndexForKeyboardSearch();
77
78 void testNameFilter();
79
80 private:
81 bool isModelConsistent() const;
82 QStringList itemsInModel() const;
83
84 private:
85 KFileItemModel* m_model;
86 KDirLister* m_dirLister;
87 TestDir* m_testDir;
88 };
89
90 void KFileItemModelTest::init()
91 {
92 // The item-model tests result in a huge number of debugging
93 // output from kdelibs. Only show critical and fatal messages.
94 qInstallMsgHandler(myMessageOutput);
95
96 qRegisterMetaType<KItemRange>("KItemRange");
97 qRegisterMetaType<KItemRangeList>("KItemRangeList");
98 qRegisterMetaType<KFileItemList>("KFileItemList");
99
100 m_testDir = new TestDir();
101 m_dirLister = new KDirLister();
102 m_model = new KFileItemModel(m_dirLister);
103 }
104
105 void KFileItemModelTest::cleanup()
106 {
107 delete m_model;
108 m_model = 0;
109
110 delete m_dirLister;
111 m_dirLister = 0;
112
113 delete m_testDir;
114 m_testDir = 0;
115 }
116
117 void KFileItemModelTest::testDefaultRoles()
118 {
119 const QSet<QByteArray> roles = m_model->roles();
120 QCOMPARE(roles.count(), 2);
121 QVERIFY(roles.contains("name"));
122 QVERIFY(roles.contains("isDir"));
123 }
124
125 void KFileItemModelTest::testDefaultSortRole()
126 {
127 QCOMPARE(m_model->sortRole(), QByteArray("name"));
128
129 QStringList files;
130 files << "c.txt" << "a.txt" << "b.txt";
131
132 m_testDir->createFiles(files);
133
134 m_dirLister->openUrl(m_testDir->url());
135 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
136
137 QCOMPARE(m_model->count(), 3);
138 QCOMPARE(m_model->data(0)["name"].toString(), QString("a.txt"));
139 QCOMPARE(m_model->data(1)["name"].toString(), QString("b.txt"));
140 QCOMPARE(m_model->data(2)["name"].toString(), QString("c.txt"));
141 }
142
143 void KFileItemModelTest::testDefaultGroupedSorting()
144 {
145 QCOMPARE(m_model->groupedSorting(), false);
146 }
147
148 void KFileItemModelTest::testNewItems()
149 {
150 QStringList files;
151 files << "a.txt" << "b.txt" << "c.txt";
152 m_testDir->createFiles(files);
153
154 m_dirLister->openUrl(m_testDir->url());
155 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
156
157 QCOMPARE(m_model->count(), 3);
158
159 QVERIFY(isModelConsistent());
160 }
161
162 void KFileItemModelTest::testRemoveItems()
163 {
164 m_testDir->createFile("a.txt");
165 m_testDir->createFile("b.txt");
166 m_dirLister->openUrl(m_testDir->url());
167 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
168 QCOMPARE(m_model->count(), 2);
169 QVERIFY(isModelConsistent());
170
171 m_testDir->removeFile("a.txt");
172 m_dirLister->updateDirectory(m_testDir->url());
173 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsRemoved(KItemRangeList)), DefaultTimeout));
174 QCOMPARE(m_model->count(), 1);
175 QVERIFY(isModelConsistent());
176 }
177
178 void KFileItemModelTest::testSetData()
179 {
180 m_testDir->createFile("a.txt");
181
182 m_dirLister->openUrl(m_testDir->url());
183 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
184
185 QHash<QByteArray, QVariant> values;
186 values.insert("customRole1", "Test1");
187 values.insert("customRole2", "Test2");
188
189 QSignalSpy itemsChangedSpy(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)));
190 m_model->setData(0, values);
191 QCOMPARE(itemsChangedSpy.count(), 1);
192
193 values = m_model->data(0);
194 QCOMPARE(values.value("customRole1").toString(), QString("Test1"));
195 QCOMPARE(values.value("customRole2").toString(), QString("Test2"));
196 QVERIFY(isModelConsistent());
197 }
198
199 void KFileItemModelTest::testSetDataWithModifiedSortRole_data()
200 {
201 QTest::addColumn<int>("changedIndex");
202 QTest::addColumn<int>("changedRating");
203 QTest::addColumn<bool>("expectMoveSignal");
204 QTest::addColumn<int>("ratingIndex0");
205 QTest::addColumn<int>("ratingIndex1");
206 QTest::addColumn<int>("ratingIndex2");
207
208 // Default setup:
209 // Index 0 = rating 2
210 // Index 1 = rating 4
211 // Index 2 = rating 6
212
213 QTest::newRow("Index 0: Rating 3") << 0 << 3 << false << 3 << 4 << 6;
214 QTest::newRow("Index 0: Rating 5") << 0 << 5 << true << 4 << 5 << 6;
215 QTest::newRow("Index 0: Rating 8") << 0 << 8 << true << 4 << 6 << 8;
216
217 QTest::newRow("Index 2: Rating 1") << 2 << 1 << true << 1 << 2 << 4;
218 QTest::newRow("Index 2: Rating 3") << 2 << 3 << true << 2 << 3 << 4;
219 QTest::newRow("Index 2: Rating 5") << 2 << 5 << false << 2 << 4 << 5;
220 }
221
222 void KFileItemModelTest::testSetDataWithModifiedSortRole()
223 {
224 QFETCH(int, changedIndex);
225 QFETCH(int, changedRating);
226 QFETCH(bool, expectMoveSignal);
227 QFETCH(int, ratingIndex0);
228 QFETCH(int, ratingIndex1);
229 QFETCH(int, ratingIndex2);
230
231 // Changing the value of a sort-role must result in
232 // a reordering of the items.
233 QCOMPARE(m_model->sortRole(), QByteArray("name"));
234
235 QStringList files;
236 files << "a.txt" << "b.txt" << "c.txt";
237 m_testDir->createFiles(files);
238
239 m_dirLister->openUrl(m_testDir->url());
240 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
241
242 // Fill the "rating" role of each file:
243 // a.txt -> 2
244 // b.txt -> 4
245 // c.txt -> 6
246
247 QHash<QByteArray, QVariant> ratingA;
248 ratingA.insert("rating", 2);
249 m_model->setData(0, ratingA);
250
251 QHash<QByteArray, QVariant> ratingB;
252 ratingB.insert("rating", 4);
253 m_model->setData(1, ratingB);
254
255 QHash<QByteArray, QVariant> ratingC;
256 ratingC.insert("rating", 6);
257 m_model->setData(2, ratingC);
258
259 m_model->setSortRole("rating");
260 QCOMPARE(m_model->data(0).value("rating").toInt(), 2);
261 QCOMPARE(m_model->data(1).value("rating").toInt(), 4);
262 QCOMPARE(m_model->data(2).value("rating").toInt(), 6);
263
264 // Now change the rating from a.txt. This usually results
265 // in reordering of the items.
266 QHash<QByteArray, QVariant> rating;
267 rating.insert("rating", changedRating);
268 m_model->setData(changedIndex, rating);
269
270 if (expectMoveSignal) {
271 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)), DefaultTimeout));
272 }
273
274 QCOMPARE(m_model->data(0).value("rating").toInt(), ratingIndex0);
275 QCOMPARE(m_model->data(1).value("rating").toInt(), ratingIndex1);
276 QCOMPARE(m_model->data(2).value("rating").toInt(), ratingIndex2);
277 QVERIFY(isModelConsistent());
278 }
279
280 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
281 {
282 //QSKIP("Temporary disabled", SkipSingle);
283
284 // KFileItemModel prevents that inserting a punch of items sequentially
285 // results in an itemsInserted()-signal for each item. Instead internally
286 // a timeout is given that collects such operations and results in only
287 // one itemsInserted()-signal. However in this test we want to stress
288 // KFileItemModel to do a lot of insert operation and hence decrease
289 // the timeout to 1 millisecond.
290 m_model->m_minimumUpdateIntervalTimer->setInterval(1);
291
292 m_testDir->createFile("1");
293 m_dirLister->openUrl(m_testDir->url());
294 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
295 QCOMPARE(m_model->count(), 1);
296
297 // Insert 10 items for 20 times. After each insert operation the model consistency
298 // is checked.
299 QSet<int> insertedItems;
300 for (int i = 0; i < 20; ++i) {
301 QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
302
303 for (int j = 0; j < 10; ++j) {
304 int itemName = qrand();
305 while (insertedItems.contains(itemName)) {
306 itemName = qrand();
307 }
308 insertedItems.insert(itemName);
309
310 m_testDir->createFile(QString::number(itemName));
311 }
312
313 m_dirLister->updateDirectory(m_testDir->url());
314 if (spy.count() == 0) {
315 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
316 }
317
318 QVERIFY(isModelConsistent());
319 }
320
321 QCOMPARE(m_model->count(), 201);
322 }
323
324 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
325 {
326 QStringList files;
327 files << "B" << "E" << "G";
328 m_testDir->createFiles(files);
329
330 // Due to inserting the 3 items one item-range with index == 0 and
331 // count == 3 must be given
332 QSignalSpy spy1(m_model, SIGNAL(itemsInserted(KItemRangeList)));
333 m_dirLister->openUrl(m_testDir->url());
334 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
335
336 QCOMPARE(spy1.count(), 1);
337 QList<QVariant> arguments = spy1.takeFirst();
338 KItemRangeList itemRangeList = arguments.at(0).value<KItemRangeList>();
339 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 3));
340
341 // The indexes of the item-ranges must always be related to the model before
342 // the items have been inserted. Having:
343 // 0 1 2
344 // B E G
345 // and inserting A, C, D, F the resulting model will be:
346 // 0 1 2 3 4 5 6
347 // A B C D E F G
348 // and the item-ranges must be:
349 // index: 0, count: 1 for A
350 // index: 1, count: 2 for B, C
351 // index: 2, count: 1 for G
352
353 files.clear();
354 files << "A" << "C" << "D" << "F";
355 m_testDir->createFiles(files);
356
357 QSignalSpy spy2(m_model, SIGNAL(itemsInserted(KItemRangeList)));
358 m_dirLister->updateDirectory(m_testDir->url());
359 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
360
361 QCOMPARE(spy2.count(), 1);
362 arguments = spy2.takeFirst();
363 itemRangeList = arguments.at(0).value<KItemRangeList>();
364 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
365 }
366
367 void KFileItemModelTest::testExpandItems()
368 {
369 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
370 // Besides testing the basic item expansion functionality, the test makes sure that
371 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
372 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
373 // first three characters.
374 QSet<QByteArray> modelRoles = m_model->roles();
375 modelRoles << "isExpanded" << "expansionLevel";
376 m_model->setRoles(modelRoles);
377
378 QStringList files;
379 files << "a/a/1" << "a/a-1/1"; // missing folders are created automatically
380 m_testDir->createFiles(files);
381
382 // Store the URLs of all folders in a set.
383 QSet<KUrl> allFolders;
384 allFolders << KUrl(m_testDir->name() + "a") << KUrl(m_testDir->name() + "a/a") << KUrl(m_testDir->name() + "a/a-1");
385
386 m_dirLister->openUrl(m_testDir->url());
387 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
388
389 // So far, the model contains only "a/"
390 QCOMPARE(m_model->count(), 1);
391 QVERIFY(m_model->isExpandable(0));
392 QVERIFY(!m_model->isExpanded(0));
393 QVERIFY(m_model->expandedUrls().empty());
394
395 QSignalSpy spyInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
396
397 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
398 m_model->setExpanded(0, true);
399 QVERIFY(m_model->isExpanded(0));
400 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
401 QCOMPARE(m_model->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
402 QCOMPARE(m_model->expandedUrls(), QSet<KUrl>() << KUrl(m_testDir->name() + "a"));
403
404 QCOMPARE(spyInserted.count(), 1);
405 KItemRangeList itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
406 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
407
408 QVERIFY(m_model->isExpandable(1));
409 QVERIFY(!m_model->isExpanded(1));
410 QVERIFY(m_model->isExpandable(2));
411 QVERIFY(!m_model->isExpanded(2));
412
413 // Expand the folder "a/a/" -> "a/a/1" becomes visible
414 m_model->setExpanded(1, true);
415 QVERIFY(m_model->isExpanded(1));
416 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
417 QCOMPARE(m_model->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
418 QCOMPARE(m_model->expandedUrls(), QSet<KUrl>() << KUrl(m_testDir->name() + "a") << KUrl(m_testDir->name() + "a/a"));
419
420 QCOMPARE(spyInserted.count(), 1);
421 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
422 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
423
424 QVERIFY(!m_model->isExpandable(2));
425 QVERIFY(!m_model->isExpanded(2));
426
427 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
428 m_model->setExpanded(3, true);
429 QVERIFY(m_model->isExpanded(3));
430 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
431 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
432 QCOMPARE(m_model->expandedUrls(), allFolders);
433
434 QCOMPARE(spyInserted.count(), 1);
435 itemRangeList = spyInserted.takeFirst().at(0).value<KItemRangeList>();
436 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
437
438 QVERIFY(!m_model->isExpandable(4));
439 QVERIFY(!m_model->isExpanded(4));
440
441 QSignalSpy spyRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
442
443 // Collapse the top-level folder -> all other items should disappear
444 m_model->setExpanded(0, false);
445 QVERIFY(!m_model->isExpanded(0));
446 QCOMPARE(m_model->count(), 1);
447 QVERIFY(!m_model->expandedUrls().contains(KUrl(m_testDir->name() + "a"))); // TODO: Make sure that child URLs are also removed
448
449 QCOMPARE(spyRemoved.count(), 1);
450 itemRangeList = spyRemoved.takeFirst().at(0).value<KItemRangeList>();
451 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
452
453 // Clear the model, reload the folder and try to restore the expanded folders.
454 m_model->clear();
455 QCOMPARE(m_model->count(), 0);
456 QVERIFY(m_model->expandedUrls().empty());
457
458 m_dirLister->openUrl(m_testDir->url());
459 m_model->restoreExpandedUrls(allFolders);
460 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(loadingCompleted()), DefaultTimeout));
461 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
462 QVERIFY(m_model->isExpanded(0));
463 QVERIFY(m_model->isExpanded(1));
464 QVERIFY(!m_model->isExpanded(2));
465 QVERIFY(m_model->isExpanded(3));
466 QVERIFY(!m_model->isExpanded(4));
467 QCOMPARE(m_model->expandedUrls(), allFolders);
468
469 // Move to a sub folder, then call restoreExpandedFolders() *before* going back.
470 // This is how DolphinView restores the expanded folders when navigating in history.
471 m_dirLister->openUrl(KUrl(m_testDir->name() + "a/a/"));
472 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(loadingCompleted()), DefaultTimeout));
473 QCOMPARE(m_model->count(), 1); // 1 item: "1"
474 m_model->restoreExpandedUrls(allFolders);
475 m_dirLister->openUrl(m_testDir->url());
476 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(loadingCompleted()), DefaultTimeout));
477 QCOMPARE(m_model->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
478 QCOMPARE(m_model->expandedUrls(), allFolders);
479 }
480
481 void KFileItemModelTest::testSorting()
482 {
483 // Create some files with different sizes and modification times to check the different sorting options
484 QDateTime now = QDateTime::currentDateTime();
485
486 m_testDir->createFile("a", "A file", now.addDays(-3));
487 m_testDir->createFile("b", "A larger file", now.addDays(0));
488 m_testDir->createDir("c", now.addDays(-2));
489 m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
490 m_testDir->createFile("e", "An even larger file", now.addDays(-4));
491 m_testDir->createFile(".f");
492
493 m_dirLister->openUrl(m_testDir->url());
494 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
495
496 // Default: Sort by Name, ascending
497 QCOMPARE(m_model->sortRole(), QByteArray("name"));
498 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
499 QVERIFY(m_model->sortFoldersFirst());
500 //QVERIFY(!m_model->showHiddenFiles());
501 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
502
503 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
504
505 // Sort by Name, descending
506 m_model->setSortOrder(Qt::DescendingOrder);
507 QCOMPARE(m_model->sortRole(), QByteArray("name"));
508 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
509 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
510 QCOMPARE(spyItemsMoved.count(), 1);
511 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 4 << 3 << 2 << 1);
512
513 // Sort by Date, descending
514 m_model->setSortRole("date");
515 QCOMPARE(m_model->sortRole(), QByteArray("date"));
516 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
517 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
518 QCOMPARE(spyItemsMoved.count(), 1);
519 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 4 << 2 << 1 << 3);
520
521 // Sort by Date, ascending
522 m_model->setSortOrder(Qt::AscendingOrder);
523 QCOMPARE(m_model->sortRole(), QByteArray("date"));
524 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
525 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
526 QCOMPARE(spyItemsMoved.count(), 1);
527 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 4 << 3 << 2 << 1);
528
529 // Sort by Date, ascending, 'Sort Folders First' disabled
530 m_model->setSortFoldersFirst(false);
531 QCOMPARE(m_model->sortRole(), QByteArray("date"));
532 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
533 QVERIFY(!m_model->sortFoldersFirst());
534 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
535 QCOMPARE(spyItemsMoved.count(), 1);
536 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 2 << 0 << 1 << 3 << 4);
537
538 // Sort by Name, ascending, 'Sort Folders First' disabled
539 m_model->setSortRole("name");
540 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
541 QVERIFY(!m_model->sortFoldersFirst());
542 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
543 QCOMPARE(spyItemsMoved.count(), 1);
544 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 4 << 0 << 2 << 3 << 1);
545
546 // Sort by Size, ascending, 'Sort Folders First' disabled
547 m_model->setSortRole("size");
548 QCOMPARE(m_model->sortRole(), QByteArray("size"));
549 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
550 QVERIFY(!m_model->sortFoldersFirst());
551 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
552 QCOMPARE(spyItemsMoved.count(), 1);
553 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 1 << 2 << 0 << 4 << 3);
554
555 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
556 m_model->setSortFoldersFirst(true);
557 QCOMPARE(m_model->sortRole(), QByteArray("size"));
558 QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
559 QVERIFY(m_model->sortFoldersFirst());
560 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
561 QCOMPARE(spyItemsMoved.count(), 0);
562
563 // Sort by Size, descending, 'Sort Folders First' enabled
564 m_model->setSortOrder(Qt::DescendingOrder);
565 QCOMPARE(m_model->sortRole(), QByteArray("size"));
566 QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder);
567 QVERIFY(m_model->sortFoldersFirst());
568 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
569 QCOMPARE(spyItemsMoved.count(), 1);
570 QCOMPARE(spyItemsMoved.takeFirst().at(1).value<QList<int> >(), QList<int>() << 0 << 4 << 3 << 2 << 1);
571
572 // TODO: Sort by other roles; show/hide hidden files
573 }
574
575 void KFileItemModelTest::testExpansionLevelsCompare_data()
576 {
577 QTest::addColumn<QString>("urlA");
578 QTest::addColumn<QString>("urlB");
579 QTest::addColumn<int>("result");
580
581 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
582 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
583 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
584 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
585 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
586 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
587 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
588 }
589
590 void KFileItemModelTest::testExpansionLevelsCompare()
591 {
592 QFETCH(QString, urlA);
593 QFETCH(QString, urlB);
594 QFETCH(int, result);
595
596 const KFileItem a(KUrl(urlA), QString(), mode_t(-1));
597 const KFileItem b(KUrl(urlB), QString(), mode_t(-1));
598 QCOMPARE(m_model->expansionLevelsCompare(a, b), result);
599 }
600
601 void KFileItemModelTest::testIndexForKeyboardSearch()
602 {
603 QStringList files;
604 files << "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
605 m_testDir->createFiles(files);
606
607 m_dirLister->openUrl(m_testDir->url());
608 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
609
610 // Search from index 0
611 QCOMPARE(m_model->indexForKeyboardSearch("a", 0), 0);
612 QCOMPARE(m_model->indexForKeyboardSearch("aa", 0), 1);
613 QCOMPARE(m_model->indexForKeyboardSearch("i", 0), 2);
614 QCOMPARE(m_model->indexForKeyboardSearch("image", 0), 2);
615 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 0), 2);
616 QCOMPARE(m_model->indexForKeyboardSearch("image.png", 0), 3);
617 QCOMPARE(m_model->indexForKeyboardSearch("t", 0), 4);
618 QCOMPARE(m_model->indexForKeyboardSearch("text", 0), 4);
619 QCOMPARE(m_model->indexForKeyboardSearch("text1", 0), 5);
620 QCOMPARE(m_model->indexForKeyboardSearch("text2", 0), 6);
621 QCOMPARE(m_model->indexForKeyboardSearch("text11", 0), 7);
622
623 // Start a search somewhere in the middle
624 QCOMPARE(m_model->indexForKeyboardSearch("a", 1), 1);
625 QCOMPARE(m_model->indexForKeyboardSearch("i", 3), 3);
626 QCOMPARE(m_model->indexForKeyboardSearch("t", 5), 5);
627 QCOMPARE(m_model->indexForKeyboardSearch("text1", 6), 7);
628
629 // Test searches that go past the last item back to index 0
630 QCOMPARE(m_model->indexForKeyboardSearch("a", 2), 0);
631 QCOMPARE(m_model->indexForKeyboardSearch("i", 7), 2);
632 QCOMPARE(m_model->indexForKeyboardSearch("image.jpg", 3), 2);
633 QCOMPARE(m_model->indexForKeyboardSearch("text2", 7), 6);
634
635 // Test searches that yield no result
636 QCOMPARE(m_model->indexForKeyboardSearch("aaa", 0), -1);
637 QCOMPARE(m_model->indexForKeyboardSearch("b", 0), -1);
638 QCOMPARE(m_model->indexForKeyboardSearch("image.svg", 0), -1);
639 QCOMPARE(m_model->indexForKeyboardSearch("text3", 0), -1);
640 QCOMPARE(m_model->indexForKeyboardSearch("text3", 5), -1);
641
642 // Test upper case searches (note that search is case insensitive)
643 QCOMPARE(m_model->indexForKeyboardSearch("A", 0), 0);
644 QCOMPARE(m_model->indexForKeyboardSearch("aA", 0), 1);
645 QCOMPARE(m_model->indexForKeyboardSearch("TexT", 5), 5);
646 QCOMPARE(m_model->indexForKeyboardSearch("IMAGE", 4), 2);
647
648 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
649 }
650
651 void KFileItemModelTest::testNameFilter()
652 {
653 QStringList files;
654 files << "A1" << "A2" << "Abc" << "Bcd" << "Cde";
655 m_testDir->createFiles(files);
656
657 m_dirLister->openUrl(m_testDir->url());
658 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
659
660 m_model->setNameFilter("A"); // Shows A1, A2 and Abc
661 QCOMPARE(m_model->count(), 3);
662
663 m_model->setNameFilter("A2"); // Shows only A2
664 QCOMPARE(m_model->count(), 1);
665
666 m_model->setNameFilter("A2"); // Shows only A1
667 QCOMPARE(m_model->count(), 1);
668
669 m_model->setNameFilter("Bc"); // Shows "Abc" and "Bcd"
670 QCOMPARE(m_model->count(), 2);
671
672 m_model->setNameFilter("bC"); // Shows "Abc" and "Bcd"
673 QCOMPARE(m_model->count(), 2);
674
675 m_model->setNameFilter(QString()); // Shows again all items
676 QCOMPARE(m_model->count(), 5);
677 }
678
679 bool KFileItemModelTest::isModelConsistent() const
680 {
681 if (m_model->m_items.count() != m_model->m_itemData.count()) {
682 return false;
683 }
684
685 for (int i = 0; i < m_model->count(); ++i) {
686 const KFileItem item = m_model->fileItem(i);
687 if (item.isNull()) {
688 qWarning() << "Item" << i << "is null";
689 return false;
690 }
691
692 const int itemIndex = m_model->index(item);
693 if (itemIndex != i) {
694 qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
695 return false;
696 }
697 }
698
699 return true;
700 }
701
702 QStringList KFileItemModelTest::itemsInModel() const
703 {
704 QStringList items;
705
706 for (int i = 0; i < m_model->count(); i++) {
707 items << m_model->data(i).value("name").toString();
708 }
709
710 return items;
711 }
712
713 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
714
715 #include "kfileitemmodeltest.moc"