1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
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. *
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. *
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 ***************************************************************************/
21 #include <qtest_kde.h>
24 #include "kitemviews/kfileitemmodel.h"
28 const int DefaultTimeout
= 5000;
31 Q_DECLARE_METATYPE(KItemRangeList
)
33 class KFileItemModelTest
: public QObject
41 void testDefaultRoles();
42 void testDefaultSortRole();
43 void testDefaultGroupRole();
45 void testRemoveItems();
46 void testModelConsistencyWhenInsertingItems();
47 void testItemRangeConsistencyWhenInsertingItems();
48 void testExpandItems();
51 void testExpansionLevelsCompare_data();
52 void testExpansionLevelsCompare();
54 void testIndexForKeyboardSearch();
57 bool isModelConsistent() const;
58 QStringList
itemsInModel() const;
61 KFileItemModel
* m_model
;
62 KDirLister
* m_dirLister
;
66 void KFileItemModelTest::init()
68 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
69 qRegisterMetaType
<KFileItemList
>("KFileItemList");
71 m_testDir
= new TestDir();
72 m_dirLister
= new KDirLister();
73 m_model
= new KFileItemModel(m_dirLister
);
76 void KFileItemModelTest::cleanup()
88 void KFileItemModelTest::testDefaultRoles()
90 const QSet
<QByteArray
> roles
= m_model
->roles();
91 QCOMPARE(roles
.count(), 2);
92 QVERIFY(roles
.contains("name"));
93 QVERIFY(roles
.contains("isDir"));
96 void KFileItemModelTest::testDefaultSortRole()
98 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
101 files
<< "c.txt" << "a.txt" << "b.txt";
103 m_testDir
->createFiles(files
);
105 m_dirLister
->openUrl(m_testDir
->url());
106 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
108 QCOMPARE(m_model
->count(), 3);
109 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
110 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
111 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
114 void KFileItemModelTest::testDefaultGroupRole()
116 QVERIFY(m_model
->groupRole().isEmpty());
119 void KFileItemModelTest::testNewItems()
122 files
<< "a.txt" << "b.txt" << "c.txt";
123 m_testDir
->createFiles(files
);
125 m_dirLister
->openUrl(m_testDir
->url());
126 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
128 QCOMPARE(m_model
->count(), 3);
130 QVERIFY(isModelConsistent());
133 void KFileItemModelTest::testRemoveItems()
135 m_testDir
->createFile("a.txt");
136 m_dirLister
->openUrl(m_testDir
->url());
137 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
138 QCOMPARE(m_model
->count(), 1);
140 m_testDir
->removeFile("a.txt");
141 m_dirLister
->updateDirectory(m_testDir
->url());
142 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
143 QCOMPARE(m_model
->count(), 0);
146 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
148 QSKIP("Temporary disabled", SkipSingle
);
150 // KFileItemModel prevents that inserting a punch of items sequentially
151 // results in an itemsInserted()-signal for each item. Instead internally
152 // a timeout is given that collects such operations and results in only
153 // one itemsInserted()-signal. However in this test we want to stress
154 // KFileItemModel to do a lot of insert operation and hence decrease
155 // the timeout to 1 millisecond.
156 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
158 m_testDir
->createFile("1");
159 m_dirLister
->openUrl(m_testDir
->url());
160 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
161 QCOMPARE(m_model
->count(), 1);
163 // Insert 10 items for 20 times. After each insert operation the model consistency
165 QSet
<int> insertedItems
;
166 for (int i
= 0; i
< 20; ++i
) {
167 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
169 for (int j
= 0; j
< 10; ++j
) {
170 int itemName
= qrand();
171 while (insertedItems
.contains(itemName
)) {
174 insertedItems
.insert(itemName
);
176 m_testDir
->createFile(QString::number(itemName
));
179 m_dirLister
->updateDirectory(m_testDir
->url());
180 if (spy
.count() == 0) {
181 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
184 QVERIFY(isModelConsistent());
187 QCOMPARE(m_model
->count(), 201);
190 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
193 files
<< "B" << "E" << "G";
194 m_testDir
->createFiles(files
);
196 // Due to inserting the 3 items one item-range with index == 0 and
197 // count == 3 must be given
198 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
199 m_dirLister
->openUrl(m_testDir
->url());
200 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
202 QCOMPARE(spy1
.count(), 1);
203 QList
<QVariant
> arguments
= spy1
.takeFirst();
204 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
205 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
207 // The indexes of the item-ranges must always be related to the model before
208 // the items have been inserted. Having:
211 // and inserting A, C, D, F the resulting model will be:
214 // and the item-ranges must be:
215 // index: 0, count: 1 for A
216 // index: 1, count: 2 for B, C
217 // index: 2, count: 1 for G
220 files
<< "A" << "C" << "D" << "F";
221 m_testDir
->createFiles(files
);
223 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
224 m_dirLister
->updateDirectory(m_testDir
->url());
225 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
227 QCOMPARE(spy2
.count(), 1);
228 arguments
= spy2
.takeFirst();
229 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
230 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
233 void KFileItemModelTest::testExpandItems()
235 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
236 // Besides testing the basic item expansion functionality, the test makes sure that
237 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
238 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
239 // first three characters.
240 QSet
<QByteArray
> modelRoles
= m_model
->roles();
241 modelRoles
<< "isExpanded" << "expansionLevel";
242 m_model
->setRoles(modelRoles
);
245 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
246 m_testDir
->createFiles(files
);
248 // Store the URLs of all folders in a set.
249 QSet
<KUrl
> allFolders
;
250 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
252 m_dirLister
->openUrl(m_testDir
->url());
253 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
255 // So far, the model contains only "a/"
256 QCOMPARE(m_model
->count(), 1);
257 QVERIFY(m_model
->isExpandable(0));
258 QVERIFY(!m_model
->isExpanded(0));
259 QVERIFY(m_model
->expandedUrls().empty());
261 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
263 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
264 m_model
->setExpanded(0, true);
265 QVERIFY(m_model
->isExpanded(0));
266 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
267 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
268 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
270 QCOMPARE(spyInserted
.count(), 1);
271 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
272 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
274 QVERIFY(m_model
->isExpandable(1));
275 QVERIFY(!m_model
->isExpanded(1));
276 QVERIFY(m_model
->isExpandable(2));
277 QVERIFY(!m_model
->isExpanded(2));
279 // Expand the folder "a/a/" -> "a/a/1" becomes visible
280 m_model
->setExpanded(1, true);
281 QVERIFY(m_model
->isExpanded(1));
282 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
283 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
284 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
286 QCOMPARE(spyInserted
.count(), 1);
287 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
288 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
290 QVERIFY(!m_model
->isExpandable(2));
291 QVERIFY(!m_model
->isExpanded(2));
293 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
294 m_model
->setExpanded(3, true);
295 QVERIFY(m_model
->isExpanded(3));
296 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
297 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
298 QCOMPARE(m_model
->expandedUrls(), allFolders
);
300 QCOMPARE(spyInserted
.count(), 1);
301 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
302 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
304 QVERIFY(!m_model
->isExpandable(4));
305 QVERIFY(!m_model
->isExpanded(4));
307 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
309 // Collapse the top-level folder -> all other items should disappear
310 m_model
->setExpanded(0, false);
311 QVERIFY(!m_model
->isExpanded(0));
312 QCOMPARE(m_model
->count(), 1);
313 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
315 QCOMPARE(spyRemoved
.count(), 1);
316 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
317 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
319 // Clear the model, reload the folder and try to restore the expanded folders.
321 QCOMPARE(m_model
->count(), 0);
322 QVERIFY(m_model
->expandedUrls().empty());
324 m_dirLister
->openUrl(m_testDir
->url());
325 m_model
->restoreExpandedUrls(allFolders
);
326 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
327 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
328 QVERIFY(m_model
->isExpanded(0));
329 QVERIFY(m_model
->isExpanded(1));
330 QVERIFY(!m_model
->isExpanded(2));
331 QVERIFY(m_model
->isExpanded(3));
332 QVERIFY(!m_model
->isExpanded(4));
333 QCOMPARE(m_model
->expandedUrls(), allFolders
);
336 void KFileItemModelTest::testSorting()
338 // Create some files with different sizes and modification times to check the different sorting options
339 QDateTime now
= QDateTime::currentDateTime();
341 m_testDir
->createFile("a", "A file", now
.addDays(-3));
342 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
343 m_testDir
->createDir("c", now
.addDays(-2));
344 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
345 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
346 m_testDir
->createFile(".f");
348 m_dirLister
->openUrl(m_testDir
->url());
349 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
351 // Default: Sort by Name, ascending
352 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
353 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
354 QVERIFY(m_model
->sortFoldersFirst());
355 //QVERIFY(!m_model->showHiddenFiles());
356 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
358 // Sort by Name, descending
359 m_model
->setSortOrder(Qt::DescendingOrder
);
360 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
361 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
362 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
364 // Sort by Date, decending
365 m_model
->setSortRole("date");
366 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
367 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
368 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
370 // Sort by Date, ascending
371 m_model
->setSortOrder(Qt::AscendingOrder
);
372 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
373 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
374 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
376 // Sort by Date, ascending, 'Sort Folders First' disabled
377 m_model
->setSortFoldersFirst(false);
378 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
379 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
380 QVERIFY(!m_model
->sortFoldersFirst());
381 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
383 // Default: Sort by Name, ascending, 'Sort Folders First' disabled
384 m_model
->setSortRole("name");
385 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
386 QVERIFY(!m_model
->sortFoldersFirst());
387 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
389 // Sort by Size, ascending, 'Sort Folders First' enabled
390 m_model
->setSortRole("size");
391 m_model
->setSortFoldersFirst(true);
392 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
393 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
394 QVERIFY(m_model
->sortFoldersFirst());
395 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
397 // Sort by Size, descending, 'Sort Folders First' enabled
398 m_model
->setSortOrder(Qt::DescendingOrder
);
399 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
400 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
401 QVERIFY(m_model
->sortFoldersFirst());
402 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
404 // TODO: How shall the sorting by size be done if 'Sort Folders First' is disabled?
406 // TODO: Sort by other roles; show/hide hidden files
409 void KFileItemModelTest::testExpansionLevelsCompare_data()
411 QTest::addColumn
<QString
>("urlA");
412 QTest::addColumn
<QString
>("urlB");
413 QTest::addColumn
<int>("result");
415 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
416 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
417 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
418 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
419 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
420 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
421 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
424 void KFileItemModelTest::testExpansionLevelsCompare()
426 QFETCH(QString
, urlA
);
427 QFETCH(QString
, urlB
);
430 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
431 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
432 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
435 void KFileItemModelTest::testIndexForKeyboardSearch()
438 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
439 m_testDir
->createFiles(files
);
441 m_dirLister
->openUrl(m_testDir
->url());
442 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
444 // Search from index 0
445 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
446 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
447 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
448 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
449 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
450 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
451 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
452 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
453 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
454 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
455 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
457 // Start a search somewhere in the middle
458 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
459 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
460 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
461 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
463 // Test searches that go past the last item back to index 0
464 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
465 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
466 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
467 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
469 // Test searches that yield no result
470 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
471 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
472 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
473 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
474 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
476 // Test upper case searches (note that search is case insensitive)
477 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
478 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
479 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
480 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
482 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
485 bool KFileItemModelTest::isModelConsistent() const
487 for (int i
= 0; i
< m_model
->count(); ++i
) {
488 const KFileItem item
= m_model
->fileItem(i
);
490 qWarning() << "Item" << i
<< "is null";
494 const int itemIndex
= m_model
->index(item
);
495 if (itemIndex
!= i
) {
496 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
504 QStringList
KFileItemModelTest::itemsInModel() const
508 for (int i
= 0; i
< m_model
->count(); i
++) {
509 items
<< m_model
->data(i
).value("name").toString();
515 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
517 #include "kfileitemmodeltest.moc"