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