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