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