]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Keep current item and selection when resorting, part 1
[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 namespace {
28 const int DefaultTimeout = 5000;
29 };
30
31 Q_DECLARE_METATYPE(KItemRangeList)
32 Q_DECLARE_METATYPE(QList<int>)
33
34 class KFileItemModelTest : public QObject
35 {
36 Q_OBJECT
37
38 private slots:
39 void init();
40 void cleanup();
41
42 void testDefaultRoles();
43 void testDefaultSortRole();
44 void testDefaultGroupRole();
45 void testNewItems();
46 void testRemoveItems();
47 void testModelConsistencyWhenInsertingItems();
48 void testItemRangeConsistencyWhenInsertingItems();
49 void testExpandItems();
50 void testSorting();
51
52 void testExpansionLevelsCompare_data();
53 void testExpansionLevelsCompare();
54
55 void testIndexForKeyboardSearch();
56
57 private:
58 bool isModelConsistent() const;
59 QStringList itemsInModel() const;
60
61 private:
62 KFileItemModel* m_model;
63 KDirLister* m_dirLister;
64 TestDir* m_testDir;
65 };
66
67 void KFileItemModelTest::init()
68 {
69 qRegisterMetaType<KItemRange>("KItemRange");
70 qRegisterMetaType<KItemRangeList>("KItemRangeList");
71 qRegisterMetaType<KFileItemList>("KFileItemList");
72
73 m_testDir = new TestDir();
74 m_dirLister = new KDirLister();
75 m_model = new KFileItemModel(m_dirLister);
76 }
77
78 void KFileItemModelTest::cleanup()
79 {
80 delete m_model;
81 m_model = 0;
82
83 delete m_dirLister;
84 m_dirLister = 0;
85
86 delete m_testDir;
87 m_testDir = 0;
88 }
89
90 void KFileItemModelTest::testDefaultRoles()
91 {
92 const QSet<QByteArray> roles = m_model->roles();
93 QCOMPARE(roles.count(), 2);
94 QVERIFY(roles.contains("name"));
95 QVERIFY(roles.contains("isDir"));
96 }
97
98 void KFileItemModelTest::testDefaultSortRole()
99 {
100 QCOMPARE(m_model->sortRole(), QByteArray("name"));
101
102 QStringList files;
103 files << "c.txt" << "a.txt" << "b.txt";
104
105 m_testDir->createFiles(files);
106
107 m_dirLister->openUrl(m_testDir->url());
108 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
109
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"));
114 }
115
116 void KFileItemModelTest::testDefaultGroupRole()
117 {
118 QVERIFY(m_model->groupRole().isEmpty());
119 }
120
121 void KFileItemModelTest::testNewItems()
122 {
123 QStringList files;
124 files << "a.txt" << "b.txt" << "c.txt";
125 m_testDir->createFiles(files);
126
127 m_dirLister->openUrl(m_testDir->url());
128 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
129
130 QCOMPARE(m_model->count(), 3);
131
132 QVERIFY(isModelConsistent());
133 }
134
135 void KFileItemModelTest::testRemoveItems()
136 {
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);
141
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);
146 }
147
148 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
149 {
150 QSKIP("Temporary disabled", SkipSingle);
151
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);
159
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);
164
165 // Insert 10 items for 20 times. After each insert operation the model consistency
166 // is checked.
167 QSet<int> insertedItems;
168 for (int i = 0; i < 20; ++i) {
169 QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
170
171 for (int j = 0; j < 10; ++j) {
172 int itemName = qrand();
173 while (insertedItems.contains(itemName)) {
174 itemName = qrand();
175 }
176 insertedItems.insert(itemName);
177
178 m_testDir->createFile(QString::number(itemName));
179 }
180
181 m_dirLister->updateDirectory(m_testDir->url());
182 if (spy.count() == 0) {
183 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
184 }
185
186 QVERIFY(isModelConsistent());
187 }
188
189 QCOMPARE(m_model->count(), 201);
190 }
191
192 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
193 {
194 QStringList files;
195 files << "B" << "E" << "G";
196 m_testDir->createFiles(files);
197
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));
203
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));
208
209 // The indexes of the item-ranges must always be related to the model before
210 // the items have been inserted. Having:
211 // 0 1 2
212 // B E G
213 // and inserting A, C, D, F the resulting model will be:
214 // 0 1 2 3 4 5 6
215 // A B C D E F G
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
220
221 files.clear();
222 files << "A" << "C" << "D" << "F";
223 m_testDir->createFiles(files);
224
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));
228
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));
233 }
234
235 void KFileItemModelTest::testExpandItems()
236 {
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);
245
246 QStringList files;
247 files << "a/a/1" << "a/a-1/1"; // missing folders are created automatically
248 m_testDir->createFiles(files);
249
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");
253
254 m_dirLister->openUrl(m_testDir->url());
255 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
256
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());
262
263 QSignalSpy spyInserted(m_model, SIGNAL(itemsInserted(KItemRangeList)));
264
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"));
271
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
275
276 QVERIFY(m_model->isExpandable(1));
277 QVERIFY(!m_model->isExpanded(1));
278 QVERIFY(m_model->isExpandable(2));
279 QVERIFY(!m_model->isExpanded(2));
280
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"));
287
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
291
292 QVERIFY(!m_model->isExpandable(2));
293 QVERIFY(!m_model->isExpanded(2));
294
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);
301
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
305
306 QVERIFY(!m_model->isExpandable(4));
307 QVERIFY(!m_model->isExpanded(4));
308
309 QSignalSpy spyRemoved(m_model, SIGNAL(itemsRemoved(KItemRangeList)));
310
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
316
317 QCOMPARE(spyRemoved.count(), 1);
318 itemRangeList = spyRemoved.takeFirst().at(0).value<KItemRangeList>();
319 QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 4)); // 4 items removed
320
321 // Clear the model, reload the folder and try to restore the expanded folders.
322 m_model->clear();
323 QCOMPARE(m_model->count(), 0);
324 QVERIFY(m_model->expandedUrls().empty());
325
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);
336 }
337
338 void KFileItemModelTest::testSorting()
339 {
340 // Create some files with different sizes and modification times to check the different sorting options
341 QDateTime now = QDateTime::currentDateTime();
342
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");
349
350 m_dirLister->openUrl(m_testDir->url());
351 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
352
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");
359
360 QSignalSpy spyItemsMoved(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)));
361
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);
369
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);
377
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);
385
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);
394
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);
402
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);
411
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);
419
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);
428
429 // TODO: Sort by other roles; show/hide hidden files
430 }
431
432 void KFileItemModelTest::testExpansionLevelsCompare_data()
433 {
434 QTest::addColumn<QString>("urlA");
435 QTest::addColumn<QString>("urlB");
436 QTest::addColumn<int>("result");
437
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;
445 }
446
447 void KFileItemModelTest::testExpansionLevelsCompare()
448 {
449 QFETCH(QString, urlA);
450 QFETCH(QString, urlB);
451 QFETCH(int, result);
452
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);
456 }
457
458 void KFileItemModelTest::testIndexForKeyboardSearch()
459 {
460 QStringList files;
461 files << "a" << "aa" << "Image.jpg" << "Image.png" << "Text" << "Text1" << "Text2" << "Text11";
462 m_testDir->createFiles(files);
463
464 m_dirLister->openUrl(m_testDir->url());
465 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
466
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);
479
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);
485
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);
491
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);
498
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);
504
505 // TODO: Maybe we should also test keyboard searches in directories which are not sorted by Name?
506 }
507
508 bool KFileItemModelTest::isModelConsistent() const
509 {
510 for (int i = 0; i < m_model->count(); ++i) {
511 const KFileItem item = m_model->fileItem(i);
512 if (item.isNull()) {
513 qWarning() << "Item" << i << "is null";
514 return false;
515 }
516
517 const int itemIndex = m_model->index(item);
518 if (itemIndex != i) {
519 qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
520 return false;
521 }
522 }
523
524 return true;
525 }
526
527 QStringList KFileItemModelTest::itemsInModel() const
528 {
529 QStringList items;
530
531 for (int i = 0; i < m_model->count(); i++) {
532 items << m_model->data(i).value("name").toString();
533 }
534
535 return items;
536 }
537
538 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
539
540 #include "kfileitemmodeltest.moc"