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