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