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
)
32 Q_DECLARE_METATYPE(QList
<int>)
34 class KFileItemModelTest
: public QObject
42 void testDefaultRoles();
43 void testDefaultSortRole();
44 void testDefaultGroupRole();
46 void testRemoveItems();
47 void testModelConsistencyWhenInsertingItems();
48 void testItemRangeConsistencyWhenInsertingItems();
49 void testExpandItems();
52 void testExpansionLevelsCompare_data();
53 void testExpansionLevelsCompare();
55 void testIndexForKeyboardSearch();
58 bool isModelConsistent() const;
59 QStringList
itemsInModel() const;
62 KFileItemModel
* m_model
;
63 KDirLister
* m_dirLister
;
67 void KFileItemModelTest::init()
69 qRegisterMetaType
<KItemRange
>("KItemRange");
70 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
71 qRegisterMetaType
<KFileItemList
>("KFileItemList");
73 m_testDir
= new TestDir();
74 m_dirLister
= new KDirLister();
75 m_model
= new KFileItemModel(m_dirLister
);
78 void KFileItemModelTest::cleanup()
90 void KFileItemModelTest::testDefaultRoles()
92 const QSet
<QByteArray
> roles
= m_model
->roles();
93 QCOMPARE(roles
.count(), 2);
94 QVERIFY(roles
.contains("name"));
95 QVERIFY(roles
.contains("isDir"));
98 void KFileItemModelTest::testDefaultSortRole()
100 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
103 files
<< "c.txt" << "a.txt" << "b.txt";
105 m_testDir
->createFiles(files
);
107 m_dirLister
->openUrl(m_testDir
->url());
108 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
110 QCOMPARE(m_model
->count(), 3);
111 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
112 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
113 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
116 void KFileItemModelTest::testDefaultGroupRole()
118 QVERIFY(m_model
->groupRole().isEmpty());
121 void KFileItemModelTest::testNewItems()
124 files
<< "a.txt" << "b.txt" << "c.txt";
125 m_testDir
->createFiles(files
);
127 m_dirLister
->openUrl(m_testDir
->url());
128 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
130 QCOMPARE(m_model
->count(), 3);
132 QVERIFY(isModelConsistent());
135 void KFileItemModelTest::testRemoveItems()
137 m_testDir
->createFile("a.txt");
138 m_dirLister
->openUrl(m_testDir
->url());
139 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
140 QCOMPARE(m_model
->count(), 1);
142 m_testDir
->removeFile("a.txt");
143 m_dirLister
->updateDirectory(m_testDir
->url());
144 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), DefaultTimeout
));
145 QCOMPARE(m_model
->count(), 0);
148 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
150 QSKIP("Temporary disabled", SkipSingle
);
152 // KFileItemModel prevents that inserting a punch of items sequentially
153 // results in an itemsInserted()-signal for each item. Instead internally
154 // a timeout is given that collects such operations and results in only
155 // one itemsInserted()-signal. However in this test we want to stress
156 // KFileItemModel to do a lot of insert operation and hence decrease
157 // the timeout to 1 millisecond.
158 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
160 m_testDir
->createFile("1");
161 m_dirLister
->openUrl(m_testDir
->url());
162 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
163 QCOMPARE(m_model
->count(), 1);
165 // Insert 10 items for 20 times. After each insert operation the model consistency
167 QSet
<int> insertedItems
;
168 for (int i
= 0; i
< 20; ++i
) {
169 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
171 for (int j
= 0; j
< 10; ++j
) {
172 int itemName
= qrand();
173 while (insertedItems
.contains(itemName
)) {
176 insertedItems
.insert(itemName
);
178 m_testDir
->createFile(QString::number(itemName
));
181 m_dirLister
->updateDirectory(m_testDir
->url());
182 if (spy
.count() == 0) {
183 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
186 QVERIFY(isModelConsistent());
189 QCOMPARE(m_model
->count(), 201);
192 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
195 files
<< "B" << "E" << "G";
196 m_testDir
->createFiles(files
);
198 // Due to inserting the 3 items one item-range with index == 0 and
199 // count == 3 must be given
200 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
201 m_dirLister
->openUrl(m_testDir
->url());
202 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
204 QCOMPARE(spy1
.count(), 1);
205 QList
<QVariant
> arguments
= spy1
.takeFirst();
206 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
207 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 3));
209 // The indexes of the item-ranges must always be related to the model before
210 // the items have been inserted. Having:
213 // and inserting A, C, D, F the resulting model will be:
216 // and the item-ranges must be:
217 // index: 0, count: 1 for A
218 // index: 1, count: 2 for B, C
219 // index: 2, count: 1 for G
222 files
<< "A" << "C" << "D" << "F";
223 m_testDir
->createFiles(files
);
225 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
226 m_dirLister
->updateDirectory(m_testDir
->url());
227 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
229 QCOMPARE(spy2
.count(), 1);
230 arguments
= spy2
.takeFirst();
231 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
232 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(0, 1) << KItemRange(1, 2) << KItemRange(2, 1));
235 void KFileItemModelTest::testExpandItems()
237 // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1".
238 // Besides testing the basic item expansion functionality, the test makes sure that
239 // KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b)
240 // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
241 // first three characters.
242 QSet
<QByteArray
> modelRoles
= m_model
->roles();
243 modelRoles
<< "isExpanded" << "expansionLevel";
244 m_model
->setRoles(modelRoles
);
247 files
<< "a/a/1" << "a/a-1/1"; // missing folders are created automatically
248 m_testDir
->createFiles(files
);
250 // Store the URLs of all folders in a set.
251 QSet
<KUrl
> allFolders
;
252 allFolders
<< KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a") << KUrl(m_testDir
->name() + "a/a-1");
254 m_dirLister
->openUrl(m_testDir
->url());
255 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
257 // So far, the model contains only "a/"
258 QCOMPARE(m_model
->count(), 1);
259 QVERIFY(m_model
->isExpandable(0));
260 QVERIFY(!m_model
->isExpanded(0));
261 QVERIFY(m_model
->expandedUrls().empty());
263 QSignalSpy
spyInserted(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
265 // Expand the folder "a/" -> "a/a/" and "a/a-1/" become visible
266 m_model
->setExpanded(0, true);
267 QVERIFY(m_model
->isExpanded(0));
268 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
269 QCOMPARE(m_model
->count(), 3); // 3 items: "a/", "a/a/", "a/a-1/"
270 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a"));
272 QCOMPARE(spyInserted
.count(), 1);
273 KItemRangeList itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
274 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 2)); // 2 new items "a/a/" and "a/a-1/" with indices 1 and 2
276 QVERIFY(m_model
->isExpandable(1));
277 QVERIFY(!m_model
->isExpanded(1));
278 QVERIFY(m_model
->isExpandable(2));
279 QVERIFY(!m_model
->isExpanded(2));
281 // Expand the folder "a/a/" -> "a/a/1" becomes visible
282 m_model
->setExpanded(1, true);
283 QVERIFY(m_model
->isExpanded(1));
284 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
285 QCOMPARE(m_model
->count(), 4); // 4 items: "a/", "a/a/", "a/a/1", "a/a-1/"
286 QCOMPARE(m_model
->expandedUrls(), QSet
<KUrl
>() << KUrl(m_testDir
->name() + "a") << KUrl(m_testDir
->name() + "a/a"));
288 QCOMPARE(spyInserted
.count(), 1);
289 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
290 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(2, 1)); // 1 new item "a/a/1" with index 2
292 QVERIFY(!m_model
->isExpandable(2));
293 QVERIFY(!m_model
->isExpanded(2));
295 // Expand the folder "a/a-1/" -> "a/a-1/1" becomes visible
296 m_model
->setExpanded(3, true);
297 QVERIFY(m_model
->isExpanded(3));
298 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
299 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
300 QCOMPARE(m_model
->expandedUrls(), allFolders
);
302 QCOMPARE(spyInserted
.count(), 1);
303 itemRangeList
= spyInserted
.takeFirst().at(0).value
<KItemRangeList
>();
304 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(4, 1)); // 1 new item "a/a-1/1" with index 4
306 QVERIFY(!m_model
->isExpandable(4));
307 QVERIFY(!m_model
->isExpanded(4));
309 QSignalSpy
spyRemoved(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)));
311 // Collapse the top-level folder -> all other items should disappear
312 m_model
->setExpanded(0, false);
313 QVERIFY(!m_model
->isExpanded(0));
314 QCOMPARE(m_model
->count(), 1);
315 QVERIFY(!m_model
->expandedUrls().contains(KUrl(m_testDir
->name() + "a"))); // TODO: Make sure that child URLs are also removed
317 QCOMPARE(spyRemoved
.count(), 1);
318 itemRangeList
= spyRemoved
.takeFirst().at(0).value
<KItemRangeList
>();
319 QCOMPARE(itemRangeList
, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
321 // Clear the model, reload the folder and try to restore the expanded folders.
323 QCOMPARE(m_model
->count(), 0);
324 QVERIFY(m_model
->expandedUrls().empty());
326 m_dirLister
->openUrl(m_testDir
->url());
327 m_model
->restoreExpandedUrls(allFolders
);
328 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(loadingCompleted()), DefaultTimeout
));
329 QCOMPARE(m_model
->count(), 5); // 5 items: "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1"
330 QVERIFY(m_model
->isExpanded(0));
331 QVERIFY(m_model
->isExpanded(1));
332 QVERIFY(!m_model
->isExpanded(2));
333 QVERIFY(m_model
->isExpanded(3));
334 QVERIFY(!m_model
->isExpanded(4));
335 QCOMPARE(m_model
->expandedUrls(), allFolders
);
338 void KFileItemModelTest::testSorting()
340 // Create some files with different sizes and modification times to check the different sorting options
341 QDateTime now
= QDateTime::currentDateTime();
343 m_testDir
->createFile("a", "A file", now
.addDays(-3));
344 m_testDir
->createFile("b", "A larger file", now
.addDays(0));
345 m_testDir
->createDir("c", now
.addDays(-2));
346 m_testDir
->createFile("d", "The largest file in this directory", now
.addDays(-1));
347 m_testDir
->createFile("e", "An even larger file", now
.addDays(-4));
348 m_testDir
->createFile(".f");
350 m_dirLister
->openUrl(m_testDir
->url());
351 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
353 // Default: Sort by Name, ascending
354 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
355 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
356 QVERIFY(m_model
->sortFoldersFirst());
357 //QVERIFY(!m_model->showHiddenFiles());
358 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "d" << "e");
360 QSignalSpy
spyItemsMoved(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)));
362 // Sort by Name, descending
363 m_model
->setSortOrder(Qt::DescendingOrder
);
364 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
365 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
366 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "d" << "b" << "a");
367 QCOMPARE(spyItemsMoved
.count(), 1);
368 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
370 // Sort by Date, descending
371 m_model
->setSortRole("date");
372 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
373 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
374 QCOMPARE(itemsInModel(), QStringList() << "c" << "b" << "d" << "a" << "e");
375 QCOMPARE(spyItemsMoved
.count(), 1);
376 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 2 << 1 << 3);
378 // Sort by Date, ascending
379 m_model
->setSortOrder(Qt::AscendingOrder
);
380 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
381 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
382 QCOMPARE(itemsInModel(), QStringList() << "c" << "e" << "a" << "d" << "b");
383 QCOMPARE(spyItemsMoved
.count(), 1);
384 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
386 // Sort by Date, ascending, 'Sort Folders First' disabled
387 m_model
->setSortFoldersFirst(false);
388 QCOMPARE(m_model
->sortRole(), QByteArray("date"));
389 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
390 QVERIFY(!m_model
->sortFoldersFirst());
391 QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "d" << "b");
392 QCOMPARE(spyItemsMoved
.count(), 1);
393 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 2 << 0 << 1 << 3 << 4);
395 // Sort by Name, ascending, 'Sort Folders First' disabled
396 m_model
->setSortRole("name");
397 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
398 QVERIFY(!m_model
->sortFoldersFirst());
399 QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "d" << "e");
400 QCOMPARE(spyItemsMoved
.count(), 1);
401 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 4 << 0 << 2 << 3 << 1);
403 // Sort by Size, ascending, 'Sort Folders First' disabled
404 m_model
->setSortRole("size");
405 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
406 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
407 QVERIFY(!m_model
->sortFoldersFirst());
408 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
409 QCOMPARE(spyItemsMoved
.count(), 1);
410 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 1 << 2 << 0 << 4 << 3);
412 // In 'Sort by Size' mode, folders are always first -> changing 'Sort Folders First' does not resort the model
413 m_model
->setSortFoldersFirst(true);
414 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
415 QCOMPARE(m_model
->sortOrder(), Qt::AscendingOrder
);
416 QVERIFY(m_model
->sortFoldersFirst());
417 QCOMPARE(itemsInModel(), QStringList() << "c" << "a" << "b" << "e" << "d");
418 QCOMPARE(spyItemsMoved
.count(), 0);
420 // Sort by Size, descending, 'Sort Folders First' enabled
421 m_model
->setSortOrder(Qt::DescendingOrder
);
422 QCOMPARE(m_model
->sortRole(), QByteArray("size"));
423 QCOMPARE(m_model
->sortOrder(), Qt::DescendingOrder
);
424 QVERIFY(m_model
->sortFoldersFirst());
425 QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "e" << "b" << "a");
426 QCOMPARE(spyItemsMoved
.count(), 1);
427 QCOMPARE(spyItemsMoved
.takeFirst().at(1).value
<QList
<int> >(), QList
<int>() << 0 << 4 << 3 << 2 << 1);
429 // TODO: Sort by other roles; show/hide hidden files
432 void KFileItemModelTest::testExpansionLevelsCompare_data()
434 QTest::addColumn
<QString
>("urlA");
435 QTest::addColumn
<QString
>("urlB");
436 QTest::addColumn
<int>("result");
438 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
439 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
440 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
441 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
442 QTest::newRow("Same level: /a-/1 > /a/1") << "/a-1/1" << "/a/1" << +1;
443 QTest::newRow("Different levels: /a/a/1 < /a/a-1") << "/a/a/1" << "/a/a-1" << -1;
444 QTest::newRow("Different levels: /a/a-1 > /a/a/1") << "/a/a-1" << "/a/a/1" << +1;
447 void KFileItemModelTest::testExpansionLevelsCompare()
449 QFETCH(QString
, urlA
);
450 QFETCH(QString
, urlB
);
453 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
454 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
455 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
458 void KFileItemModelTest::testIndexForKeyboardSearch()
461 files
<< "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
462 m_testDir
->createFiles(files
);
464 m_dirLister
->openUrl(m_testDir
->url());
465 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
467 // Search from index 0
468 QCOMPARE(m_model
->indexForKeyboardSearch("a", 0), 0);
469 QCOMPARE(m_model
->indexForKeyboardSearch("aa", 0), 1);
470 QCOMPARE(m_model
->indexForKeyboardSearch("i", 0), 2);
471 QCOMPARE(m_model
->indexForKeyboardSearch("image", 0), 2);
472 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 0), 2);
473 QCOMPARE(m_model
->indexForKeyboardSearch("image.png", 0), 3);
474 QCOMPARE(m_model
->indexForKeyboardSearch("t", 0), 4);
475 QCOMPARE(m_model
->indexForKeyboardSearch("text", 0), 4);
476 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 0), 5);
477 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 0), 6);
478 QCOMPARE(m_model
->indexForKeyboardSearch("text11", 0), 7);
480 // Start a search somewhere in the middle
481 QCOMPARE(m_model
->indexForKeyboardSearch("a", 1), 1);
482 QCOMPARE(m_model
->indexForKeyboardSearch("i", 3), 3);
483 QCOMPARE(m_model
->indexForKeyboardSearch("t", 5), 5);
484 QCOMPARE(m_model
->indexForKeyboardSearch("text1", 6), 7);
486 // Test searches that go past the last item back to index 0
487 QCOMPARE(m_model
->indexForKeyboardSearch("a", 2), 0);
488 QCOMPARE(m_model
->indexForKeyboardSearch("i", 7), 2);
489 QCOMPARE(m_model
->indexForKeyboardSearch("image.jpg", 3), 2);
490 QCOMPARE(m_model
->indexForKeyboardSearch("text2", 7), 6);
492 // Test searches that yield no result
493 QCOMPARE(m_model
->indexForKeyboardSearch("aaa", 0), -1);
494 QCOMPARE(m_model
->indexForKeyboardSearch("b", 0), -1);
495 QCOMPARE(m_model
->indexForKeyboardSearch("image.svg", 0), -1);
496 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 0), -1);
497 QCOMPARE(m_model
->indexForKeyboardSearch("text3", 5), -1);
499 // Test upper case searches (note that search is case insensitive)
500 QCOMPARE(m_model
->indexForKeyboardSearch("A", 0), 0);
501 QCOMPARE(m_model
->indexForKeyboardSearch("aA", 0), 1);
502 QCOMPARE(m_model
->indexForKeyboardSearch("TexT", 5), 5);
503 QCOMPARE(m_model
->indexForKeyboardSearch("IMAGE", 4), 2);
505 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
508 bool KFileItemModelTest::isModelConsistent() const
510 for (int i
= 0; i
< m_model
->count(); ++i
) {
511 const KFileItem item
= m_model
->fileItem(i
);
513 qWarning() << "Item" << i
<< "is null";
517 const int itemIndex
= m_model
->index(item
);
518 if (itemIndex
!= i
) {
519 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
527 QStringList
KFileItemModelTest::itemsInModel() const
531 for (int i
= 0; i
< m_model
->count(); i
++) {
532 items
<< m_model
->data(i
).value("name").toString();
538 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
540 #include "kfileitemmodeltest.moc"