]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/placesitemmodeltest.cpp
[PlacesItemModelTest] Fix testGroups() test case
[dolphin.git] / src / tests / placesitemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2017 by Renato Araujo Oliveira <renato.araujo@kdab.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include <QTest>
21 #include <QSignalSpy>
22 #include <QStandardPaths>
23 #include <QAction>
24 #include <QDBusInterface>
25
26 #include <KBookmarkManager>
27 #include <KConfig>
28 #include <KConfigGroup>
29 #include <KAboutData>
30 #include <KFilePlacesModel>
31
32 #include "panels/places/placesitemmodel.h"
33 #include "panels/places/placesitem.h"
34 #include "views/viewproperties.h"
35
36 Q_DECLARE_METATYPE(KItemRangeList)
37 Q_DECLARE_METATYPE(KItemRange)
38
39 #ifdef Q_OS_WIN
40 //c:\ as root for windows
41 #define KDE_ROOT_PATH "C:\\"
42 #else
43 #define KDE_ROOT_PATH "/"
44 #endif
45
46 static QString bookmarksFile()
47 {
48 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel";
49 }
50
51 class PlacesItemModelTest : public QObject
52 {
53 Q_OBJECT
54
55 private slots:
56 void init();
57 void cleanup();
58
59 void initTestCase();
60 void cleanupTestCase();
61
62 void testModelSort();
63 void testGroups();
64 void testDeletePlace();
65 void testPlaceItem_data();
66 void testPlaceItem();
67 void testTearDownDevice();
68 void testDefaultViewProperties_data();
69 void testDefaultViewProperties();
70 void testClear();
71 void testHideItem();
72 void testSystemItems();
73 void testEditBookmark();
74 void testEditAfterCreation();
75 void testEditMetadata();
76 void testRefresh();
77 void testIcons_data();
78 void testIcons();
79 void testDragAndDrop();
80 void testHideDevices();
81 void testDuplicatedEntries();
82 void renameAfterCreation();
83
84 private:
85 PlacesItemModel* m_model;
86 QSet<int> m_tobeRemoved;
87 QMap<QString, QDBusInterface *> m_interfacesMap;
88 int m_expectedModelCount = 15;
89 bool m_hasDesktopFolder = false;
90 bool m_hasDownloadsFolder = false;
91
92 void setBalooEnabled(bool enabled);
93 int indexOf(const QUrl &url);
94 QDBusInterface *fakeManager();
95 QDBusInterface *fakeDevice(const QString &udi);
96 QStringList placesUrls(PlacesItemModel *model = nullptr) const;
97 QStringList initialUrls() const;
98 void createPlaceItem(const QString &text, const QUrl &url, const QString &icon);
99 void removePlaceAfter(int index);
100 void cancelPlaceRemoval(int index);
101 void removeTestUserData();
102 QMimeData *createMimeData(const QList<int> &indexes) const;
103 };
104
105 #define CHECK_PLACES_URLS(urls) \
106 { \
107 QStringList places = placesUrls(); \
108 if (places != urls) { \
109 qWarning() << "Expected:" << urls; \
110 qWarning() << "Got:" << places; \
111 QCOMPARE(places, urls); \
112 } \
113 }
114
115 void PlacesItemModelTest::setBalooEnabled(bool enabled)
116 {
117 KConfig config(QStringLiteral("baloofilerc"));
118 KConfigGroup basicSettings = config.group("Basic Settings");
119 basicSettings.writeEntry("Indexing-Enabled", enabled);
120 config.sync();
121 }
122
123 int PlacesItemModelTest::indexOf(const QUrl &url)
124 {
125 for (int r = 0; r < m_model->count(); r++) {
126 if (m_model->placesItem(r)->url() == url) {
127 return r;
128 }
129 }
130 return -1;
131 }
132
133 QDBusInterface *PlacesItemModelTest::fakeManager()
134 {
135 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
136 }
137
138 QDBusInterface *PlacesItemModelTest::fakeDevice(const QString &udi)
139 {
140 if (m_interfacesMap.contains(udi)) {
141 return m_interfacesMap[udi];
142 }
143
144 QDBusInterface *iface = new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi);
145 m_interfacesMap[udi] = iface;
146
147 return iface;
148 }
149
150 QStringList PlacesItemModelTest::placesUrls(PlacesItemModel *model) const
151 {
152 QStringList urls;
153 if (!model) {
154 model = m_model;
155 }
156
157 for (int row = 0; row < model->count(); ++row) {
158 urls << model->placesItem(row)->url().toDisplayString(QUrl::PreferLocalFile);
159 }
160 return urls;
161 }
162
163 QStringList PlacesItemModelTest::initialUrls() const
164 {
165 static QStringList urls;
166 if (urls.isEmpty()) {
167 urls << QDir::homePath();
168
169 if (m_hasDesktopFolder) {
170 urls << QDir::homePath() + QStringLiteral("/Desktop");
171 }
172
173 if (m_hasDownloadsFolder) {
174 urls << QDir::homePath() + QStringLiteral("/Downloads");
175 }
176
177 urls << QStringLiteral(KDE_ROOT_PATH) << QStringLiteral("trash:/")
178 << QStringLiteral("remote:/")
179 << QStringLiteral("/media/nfs")
180 << QStringLiteral("timeline:/today") << QStringLiteral("timeline:/yesterday")
181 << QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
182 << QStringLiteral("/foreign")
183 << QStringLiteral("/media/floppy0") << QStringLiteral("/media/XO-Y4") << QStringLiteral("/media/cdrom");
184 }
185 return urls;
186 }
187
188 void PlacesItemModelTest::createPlaceItem(const QString &text, const QUrl &url, const QString &icon)
189 {
190 m_model->createPlacesItem(text, url, icon);
191 }
192
193 void PlacesItemModelTest::removePlaceAfter(int index)
194 {
195 m_tobeRemoved.insert(index);
196 }
197
198 void PlacesItemModelTest::cancelPlaceRemoval(int index)
199 {
200 m_tobeRemoved.remove(index);
201 }
202
203 void PlacesItemModelTest::removeTestUserData()
204 {
205 // user hardcoded path to avoid removal of any user personal data
206 QDir dir(QStringLiteral("/home/renato/.qttest/share/placesitemmodeltest"));
207 if (dir.exists()) {
208 QVERIFY(dir.removeRecursively());
209 }
210 }
211
212 QMimeData *PlacesItemModelTest::createMimeData(const QList<int> &indexes) const
213 {
214 QByteArray itemData;
215 QDataStream stream(&itemData, QIODevice::WriteOnly);
216 QList<QUrl> urls;
217
218 for (int index : indexes) {
219 const QUrl itemUrl = m_model->placesItem(index)->url();
220 if (itemUrl.isValid()) {
221 urls << itemUrl;
222 }
223 stream << index;
224 }
225
226 QMimeData* mimeData = new QMimeData();
227 mimeData->setUrls(urls);
228 // copied from PlacesItemModel::internalMimeType()
229 const QString internalMimeType = "application/x-dolphinplacesmodel-" +
230 QString::number((qptrdiff)m_model);
231 mimeData->setData(internalMimeType, itemData);
232 return mimeData;
233 }
234
235 void PlacesItemModelTest::init()
236 {
237 m_model = new PlacesItemModel();
238 // WORKAROUND: need to wait for bookmark to load, check: PlacesItemModel::updateBookmarks
239 QTest::qWait(300);
240 QCOMPARE(m_model->count(), m_expectedModelCount);
241 }
242
243 void PlacesItemModelTest::cleanup()
244 {
245 const auto tobeRemoved = m_tobeRemoved;
246 for (const int i : tobeRemoved) {
247 int before = m_model->count();
248 m_model->deleteItem(i);
249 QTRY_COMPARE(m_model->count(), before - 1);
250 }
251 m_tobeRemoved.clear();
252 delete m_model;
253 m_model = nullptr;
254 removeTestUserData();
255 }
256
257 void PlacesItemModelTest::initTestCase()
258 {
259 QStandardPaths::setTestModeEnabled(true);
260 // remove test user data
261 removeTestUserData();
262
263 const QString fakeHw = QFINDTESTDATA("data/fakecomputer.xml");
264 QVERIFY(!fakeHw.isEmpty());
265 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw));
266
267 setBalooEnabled(true);
268 const QString bookmarsFileName = bookmarksFile();
269 if (QFileInfo::exists(bookmarsFileName)) {
270 // Ensure we'll have a clean bookmark file to start
271 QVERIFY(QFile::remove(bookmarsFileName));
272 }
273
274 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).exists()) {
275 m_hasDesktopFolder = true;
276 m_expectedModelCount++;
277 }
278
279 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)).exists()) {
280 m_hasDownloadsFolder = true;
281 m_expectedModelCount++;
282 }
283
284 qRegisterMetaType<KItemRangeList>();
285 qRegisterMetaType<KItemRange>();
286 }
287
288 void PlacesItemModelTest::cleanupTestCase()
289 {
290 qDeleteAll(m_interfacesMap);
291 QFile::remove(bookmarksFile());
292
293 // Remove any previous properties file
294 removeTestUserData();
295 }
296
297 void PlacesItemModelTest::testModelSort()
298 {
299 CHECK_PLACES_URLS(initialUrls());
300 }
301
302 void PlacesItemModelTest::testGroups()
303 {
304 const auto groups = m_model->groups();
305 int expectedGroupSize = 3;
306 if (m_hasDesktopFolder) {
307 expectedGroupSize++;
308 }
309 if (m_hasDownloadsFolder) {
310 expectedGroupSize++;
311 }
312
313 QCOMPARE(groups.size(), 6);
314
315 QCOMPARE(groups.at(0).first, 0);
316 QCOMPARE(groups.at(0).second.toString(), QStringLiteral("Places"));
317
318 QCOMPARE(groups.at(1).first, expectedGroupSize);
319 QCOMPARE(groups.at(1).second.toString(), QStringLiteral("Remote"));
320
321 QCOMPARE(groups.at(2).first, expectedGroupSize + 2);
322 QCOMPARE(groups.at(2).second.toString(), QStringLiteral("Recently Saved"));
323
324 QCOMPARE(groups.at(3).first, expectedGroupSize + 4);
325 QCOMPARE(groups.at(3).second.toString(), QStringLiteral("Search For"));
326
327 QCOMPARE(groups.at(4).first, expectedGroupSize + 8);
328 QCOMPARE(groups.at(4).second.toString(), QStringLiteral("Devices"));
329
330 QCOMPARE(groups.at(5).first, expectedGroupSize + 9);
331 QCOMPARE(groups.at(5).second.toString(), QStringLiteral("Removable Devices"));
332 }
333
334 void PlacesItemModelTest::testPlaceItem_data()
335 {
336 QTest::addColumn<QUrl>("url");
337 QTest::addColumn<bool>("expectedIsHidden");
338 QTest::addColumn<bool>("expectedIsSystemItem");
339 QTest::addColumn<QString>("expectedGroup");
340 QTest::addColumn<bool>("expectedStorageSetupNeeded");
341
342 // places
343 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
344
345 // baloo -search
346 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
347
348 // baloo - timeline
349 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << false << true << QStringLiteral("Recently Saved") << false;
350
351 // devices
352 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
353 }
354
355 void PlacesItemModelTest::testPlaceItem()
356 {
357 QFETCH(QUrl, url);
358 QFETCH(bool, expectedIsHidden);
359 QFETCH(bool, expectedIsSystemItem);
360 QFETCH(QString, expectedGroup);
361 QFETCH(bool, expectedStorageSetupNeeded);
362
363 const int index = indexOf(url);
364 PlacesItem *item = m_model->placesItem(index);
365 QCOMPARE(item->url(), url);
366 QCOMPARE(item->isHidden(), expectedIsHidden);
367 QCOMPARE(item->isSystemItem(), expectedIsSystemItem);
368 QCOMPARE(item->group(), expectedGroup);
369 QCOMPARE(item->storageSetupNeeded(), expectedStorageSetupNeeded);
370 }
371
372 void PlacesItemModelTest::testDeletePlace()
373 {
374 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
375 QStringList urls = initialUrls();
376 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
377 QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
378
379 PlacesItemModel *model = new PlacesItemModel();
380
381 // create a new place
382 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
383 urls.insert(5, tempUrl.toLocalFile());
384
385 // check if the new entry was created
386 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
387 CHECK_PLACES_URLS(urls);
388 QTRY_COMPARE(model->count(), m_model->count());
389
390 // delete item
391 m_model->deleteItem(5);
392
393 // make sure that the new item is removed
394 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
395 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
396 CHECK_PLACES_URLS(initialUrls());
397 QTRY_COMPARE(model->count(), m_model->count());
398 }
399
400 void PlacesItemModelTest::testTearDownDevice()
401 {
402 const QUrl mediaUrl = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
403 int index = indexOf(mediaUrl);
404 QVERIFY(index != -1);
405
406 auto ejectAction = m_model->ejectAction(index);
407 QVERIFY(!ejectAction);
408
409 auto teardownAction = m_model->teardownAction(index);
410 QVERIFY(teardownAction);
411
412 QCOMPARE(m_model->count(), m_expectedModelCount);
413
414 QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
415 fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
416 QTRY_COMPARE(m_model->count(), 16);
417 QCOMPARE(spyItemsRemoved.count(), 1);
418 const QList<QVariant> spyItemsRemovedArgs = spyItemsRemoved.takeFirst();
419 const KItemRangeList removedRange = spyItemsRemovedArgs.at(0).value<KItemRangeList>();
420 QCOMPARE(removedRange.size(), 1);
421 QCOMPARE(removedRange.first().index, index);
422 QCOMPARE(removedRange.first().count, 1);
423
424 QCOMPARE(indexOf(mediaUrl), -1);
425
426 QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted);
427 fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
428 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
429 QCOMPARE(spyItemsInserted.count(), 1);
430 index = indexOf(mediaUrl);
431
432 const QList<QVariant> args = spyItemsInserted.takeFirst();
433 const KItemRangeList insertedRange = args.at(0).value<KItemRangeList>();
434 QCOMPARE(insertedRange.size(), 1);
435 QCOMPARE(insertedRange.first().index, index);
436 QCOMPARE(insertedRange.first().count, 1);
437 }
438
439 void PlacesItemModelTest::testDefaultViewProperties_data()
440 {
441 QTest::addColumn<QUrl>("url");
442 QTest::addColumn<DolphinView::Mode>("expectedViewMode");
443 QTest::addColumn<bool>("expectedPreviewShow");
444 QTest::addColumn<QList<QByteArray> >("expectedVisibleRole");
445
446 // places
447 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView << true << QList<QByteArray>({"text"});
448
449 // baloo -search
450 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "path"});
451
452 // audio files
453 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "artist", "album"});
454
455 // baloo - timeline
456 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << DolphinView::DetailsView << true << QList<QByteArray>({"text", "modificationtime"});
457
458 // devices
459 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView << true << QList<QByteArray>({"text"});
460
461 }
462
463 void PlacesItemModelTest::testDefaultViewProperties()
464 {
465 QFETCH(QUrl, url);
466 QFETCH(DolphinView::Mode, expectedViewMode);
467 QFETCH(bool, expectedPreviewShow);
468 QFETCH(QList<QByteArray>, expectedVisibleRole);
469
470 ViewProperties properties(KFilePlacesModel::convertedUrl(url));
471 QCOMPARE(properties.viewMode(), expectedViewMode);
472 QCOMPARE(properties.previewsShown(), expectedPreviewShow);
473 QCOMPARE(properties.visibleRoles(), expectedVisibleRole);
474 }
475
476 void PlacesItemModelTest::testClear()
477 {
478 QCOMPARE(m_model->count(), m_expectedModelCount);
479 m_model->clear();
480 QCOMPARE(m_model->count(), 0);
481 QCOMPARE(m_model->hiddenCount(), 0);
482 m_model->refresh();
483 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
484 }
485
486 void PlacesItemModelTest::testHideItem()
487 {
488 const QUrl mediaUrl = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
489 const int index = indexOf(mediaUrl);
490
491 PlacesItem *item = m_model->placesItem(index);
492
493 QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
494 QList<QVariant> spyItemsRemovedArgs;
495 KItemRangeList removedRange;
496
497 QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted);
498 QList<QVariant> spyItemsInsertedArgs;
499 KItemRangeList insertedRange;
500 QVERIFY(item);
501
502 // hide an item
503 item->setHidden(true);
504
505 // check if items removed was fired
506 QTRY_COMPARE(m_model->count(), 16);
507 QCOMPARE(spyItemsRemoved.count(), 1);
508 spyItemsRemovedArgs = spyItemsRemoved.takeFirst();
509 removedRange = spyItemsRemovedArgs.at(0).value<KItemRangeList>();
510 QCOMPARE(removedRange.size(), 1);
511 QCOMPARE(removedRange.first().index, index);
512 QCOMPARE(removedRange.first().count, 1);
513
514 // allow model to show hidden items
515 m_model->setHiddenItemsShown(true);
516
517 // check if the items inserted was fired
518 spyItemsInsertedArgs = spyItemsInserted.takeFirst();
519 insertedRange = spyItemsInsertedArgs.at(0).value<KItemRangeList>();
520 QCOMPARE(insertedRange.size(), 1);
521 QCOMPARE(insertedRange.first().index, index);
522 QCOMPARE(insertedRange.first().count, 1);
523
524 // mark item as visible
525 item = m_model->placesItem(index);
526 item->setHidden(false);
527
528 // mark model to hide invisible items
529 m_model->setHiddenItemsShown(true);
530
531 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
532 }
533
534 void PlacesItemModelTest::testSystemItems()
535 {
536 QCOMPARE(m_model->count(), m_expectedModelCount);
537 for (int r = 0; r < m_model->count(); r++) {
538 QCOMPARE(m_model->placesItem(r)->isSystemItem(), !m_model->placesItem(r)->device().isValid());
539 }
540
541 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
542
543 // create a new entry (non system item)
544 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
545
546 // check if the new entry was created
547 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
548
549 // make sure the new place get removed
550 removePlaceAfter(5);
551
552 QList<QVariant> args = itemsInsertedSpy.takeFirst();
553 KItemRangeList range = args.at(0).value<KItemRangeList>();
554 QCOMPARE(range.first().index, 5);
555 QCOMPARE(range.first().count, 1);
556 QVERIFY(!m_model->placesItem(5)->isSystemItem());
557 QCOMPARE(m_model->count(), 18);
558
559 QTest::qWait(300);
560 // check if the removal signal is correct
561 QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
562 m_model->deleteItem(5);
563 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
564 args = itemsRemovedSpy.takeFirst();
565 range = args.at(0).value<KItemRangeList>();
566 QCOMPARE(range.first().index, 5);
567 QCOMPARE(range.first().count, 1);
568 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
569
570 //cancel removal (it was removed above)
571 cancelPlaceRemoval(5);
572 }
573
574 void PlacesItemModelTest::testEditBookmark()
575 {
576 QScopedPointer<PlacesItemModel> other(new PlacesItemModel());
577
578 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
579
580 // make sure that the new item will be removed later
581 removePlaceAfter(5);
582
583 QSignalSpy itemsChangedSply(m_model, &PlacesItemModel::itemsChanged);
584
585 // modify place text
586 m_model->item(3)->setText(QStringLiteral("Renamed place"));
587 m_model->refresh();
588
589 // check if the correct signal was fired
590 QTRY_COMPARE(itemsChangedSply.count(), 1);
591 QList<QVariant> args = itemsChangedSply.takeFirst();
592 KItemRangeList range = args.at(0).value<KItemRangeList>();
593 QCOMPARE(range.first().index, 3);
594 QCOMPARE(range.first().count, 1);
595 QSet<QByteArray> roles = args.at(1).value<QSet<QByteArray> >();
596 QCOMPARE(roles.size(), 1);
597 QCOMPARE(*roles.begin(), QByteArrayLiteral("text"));
598 QCOMPARE(m_model->item(3)->text(), QStringLiteral("Renamed place"));
599
600 // check if the item was updated in the other model
601 QTRY_COMPARE(other->item(3)->text(), QStringLiteral("Renamed place"));
602 }
603
604 void PlacesItemModelTest::testEditAfterCreation()
605 {
606 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
607 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
608
609 // create a new place
610 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
611 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
612
613 PlacesItemModel *model = new PlacesItemModel();
614 QTRY_COMPARE(model->count(), m_model->count());
615
616 // make sure that the new item will be removed later
617 removePlaceAfter(5);
618
619 // modify place text
620 PlacesItem *item = m_model->placesItem(3);
621 item->setText(QStringLiteral("Renamed place"));
622 m_model->refresh();
623
624 // check if the second model got the changes
625 QTRY_COMPARE(model->count(), m_model->count());
626 QTRY_COMPARE(model->placesItem(3)->text(), m_model->placesItem(3)->text());
627 QTRY_COMPARE(model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
628 m_model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
629 QTRY_COMPARE(model->placesItem(3)->icon(), m_model->placesItem(3)->icon());
630 QTRY_COMPARE(model->placesItem(3)->url(), m_model->placesItem(3)->url());
631 }
632
633 void PlacesItemModelTest::testEditMetadata()
634 {
635 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
636 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
637
638 // create a new place
639 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
640 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
641
642 // check if the new entry was created
643 PlacesItemModel *model = new PlacesItemModel();
644 QTRY_COMPARE(model->count(), m_model->count());
645
646 // make sure that the new item will be removed later
647 removePlaceAfter(5);
648
649 // modify place metadata
650 PlacesItem *item = m_model->placesItem(3);
651 item->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
652 m_model->refresh();
653
654 // check if the place was modified in both models
655 QTRY_COMPARE(model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
656 KAboutData::applicationData().componentName());
657 QTRY_COMPARE(model->placesItem(3)->text(), m_model->placesItem(3)->text());
658 QTRY_COMPARE(model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
659 m_model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
660 QTRY_COMPARE(model->placesItem(3)->icon(), m_model->placesItem(3)->icon());
661 QTRY_COMPARE(model->placesItem(3)->url(), m_model->placesItem(3)->url());
662 }
663
664 void PlacesItemModelTest::testRefresh()
665 {
666 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
667 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
668
669 // create a new place
670 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
671 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
672
673 PlacesItemModel *model = new PlacesItemModel();
674 QTRY_COMPARE(model->count(), m_model->count());
675
676 // make sure that the new item will be removed later
677 removePlaceAfter(5);
678
679 PlacesItem *item = m_model->placesItem(5);
680 PlacesItem *sameItem = model->placesItem(5);
681 QCOMPARE(item->text(), sameItem->text());
682
683 // modify place text
684 item->setText(QStringLiteral("Renamed place"));
685
686 // item from another model is not affected at the moment
687 QVERIFY(item->text() != sameItem->text());
688
689 // propagate change
690 m_model->refresh();
691
692 // item must be equal
693 QTRY_COMPARE(item->text(), sameItem->text());
694 }
695
696 void PlacesItemModelTest::testIcons_data()
697 {
698 QTest::addColumn<QUrl>("url");
699 QTest::addColumn<QString>("expectedIconName");
700
701 // places
702 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
703
704 // baloo -search
705 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
706
707 // baloo - timeline
708 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << QStringLiteral("go-jump-today");
709
710 // devices
711 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
712 }
713
714 void PlacesItemModelTest::testIcons()
715 {
716 QFETCH(QUrl, url);
717 QFETCH(QString, expectedIconName);
718
719 PlacesItem *item = m_model->placesItem(indexOf(url));
720 QCOMPARE(item->icon(), expectedIconName);
721
722 for (int r = 0; r < m_model->count(); r++) {
723 QVERIFY(!m_model->placesItem(r)->icon().isEmpty());
724 }
725 }
726
727 void PlacesItemModelTest::testDragAndDrop()
728 {
729 QList<QVariant> args;
730 KItemRangeList range;
731 QStringList urls = initialUrls();
732 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
733 QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
734
735 CHECK_PLACES_URLS(initialUrls());
736 // Move the home directory to the end of the places group
737 QMimeData *dropData = createMimeData(QList<int>() << 0);
738 m_model->dropMimeDataBefore(m_model->count() - 1, dropData);
739 urls.move(0, 4);
740 delete dropData;
741
742 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
743 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
744
745 // remove item from actual position
746 args = itemsRemovedSpy.takeFirst();
747 range = args.at(0).value<KItemRangeList>();
748 QCOMPARE(range.size(), 1);
749 QCOMPARE(range.at(0).count, 1);
750 QCOMPARE(range.at(0).index, 0);
751
752 // insert intem in his group
753 args = itemsInsertedSpy.takeFirst();
754 range = args.at(0).value<KItemRangeList>();
755 QCOMPARE(range.size(), 1);
756 QCOMPARE(range.at(0).count, 1);
757 QCOMPARE(range.at(0).index, 4);
758
759 CHECK_PLACES_URLS(urls);
760
761 itemsInsertedSpy.clear();
762 itemsRemovedSpy.clear();
763
764 // Move home directory item back to its original position
765 dropData = createMimeData(QList<int>() << 4);
766 m_model->dropMimeDataBefore(0, dropData);
767 urls.move(4, 0);
768 delete dropData;
769
770 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
771 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
772
773 // remove item from actual position
774 args = itemsRemovedSpy.takeFirst();
775 range = args.at(0).value<KItemRangeList>();
776 QCOMPARE(range.size(), 1);
777 QCOMPARE(range.at(0).count, 1);
778 QCOMPARE(range.at(0).index, 4);
779
780 // insert intem in the requested position
781 args = itemsInsertedSpy.takeFirst();
782 range = args.at(0).value<KItemRangeList>();
783 QCOMPARE(range.size(), 1);
784 QCOMPARE(range.at(0).count, 1);
785 QCOMPARE(range.at(0).index, 0);
786
787 CHECK_PLACES_URLS(urls);
788 }
789
790 void PlacesItemModelTest::testHideDevices()
791 {
792 QSignalSpy itemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
793 QStringList urls = initialUrls();
794
795 m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, true);
796 QTRY_VERIFY(m_model->isGroupHidden(KFilePlacesModel::RemovableDevicesType));
797 QTRY_COMPARE(itemsRemoved.count(), 3);
798
799 // remove removable-devices
800 urls.removeOne(QStringLiteral("/media/floppy0"));
801 urls.removeOne(QStringLiteral("/media/XO-Y4"));
802 urls.removeOne(QStringLiteral("/media/cdrom"));
803
804 // check if the correct urls was removed
805 CHECK_PLACES_URLS(urls);
806
807 delete m_model;
808 m_model = new PlacesItemModel();
809 QTRY_COMPARE(m_model->count(), urls.count());
810 CHECK_PLACES_URLS(urls);
811
812 // revert changes
813 m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, false);
814 urls = initialUrls();
815 QTRY_COMPARE(m_model->count(), urls.count());
816 CHECK_PLACES_URLS(urls);
817 }
818
819 void PlacesItemModelTest::testDuplicatedEntries()
820 {
821 QStringList urls = initialUrls();
822 // create a duplicated entry on bookmark
823 KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
824 KBookmarkGroup root = bookmarkManager->root();
825 KBookmark bookmark = root.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {});
826
827 const QString id = QUuid::createUuid().toString();
828 bookmark.setMetaDataItem(QStringLiteral("ID"), id);
829 bookmark.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
830 bookmarkManager->emitChanged(bookmarkManager->root());
831
832 PlacesItemModel *newModel = new PlacesItemModel();
833 QTRY_COMPARE(placesUrls(newModel).count(QStringLiteral("search:/videos")), 1);
834 QTRY_COMPARE(urls, placesUrls(newModel));
835 delete newModel;
836 }
837
838 void PlacesItemModelTest::renameAfterCreation()
839 {
840 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
841 QStringList urls = initialUrls();
842 PlacesItemModel *model = new PlacesItemModel();
843
844 CHECK_PLACES_URLS(urls);
845 QTRY_COMPARE(model->count(), m_model->count());
846
847 // create a new place
848 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
849 urls.insert(5, tempUrl.toLocalFile());
850
851 // make sure that the new item will be removed later
852 removePlaceAfter(5);
853
854 CHECK_PLACES_URLS(urls);
855 QCOMPARE(model->count(), m_model->count());
856
857
858 // modify place text
859 QSignalSpy changedSpy(m_model, &PlacesItemModel::itemsChanged);
860
861 PlacesItem *item = m_model->placesItem(3);
862 item->setText(QStringLiteral("New Temporary Dir"));
863 item->setUrl(item->url());
864 item->setIcon(item->icon());
865 m_model->refresh();
866
867 QTRY_COMPARE(changedSpy.count(), 1);
868
869 // check if the place was modified in both models
870 QTRY_COMPARE(m_model->placesItem(3)->text(), QStringLiteral("New Temporary Dir"));
871 QTRY_COMPARE(model->placesItem(3)->text(), QStringLiteral("New Temporary Dir"));
872 }
873
874 QTEST_MAIN(PlacesItemModelTest)
875
876 #include "placesitemmodeltest.moc"