2 * SPDX-FileCopyrightText: 2017 Renato Araujo Oliveira <renato.araujo@kdab.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include <QStandardPaths>
11 #include <QDBusInterface>
13 #include <KBookmarkManager>
15 #include <KConfigGroup>
17 #include <KFilePlacesModel>
18 #include <KProtocolInfo>
20 #include "dolphin_generalsettings.h"
21 #include "panels/places/placesitemmodel.h"
22 #include "panels/places/placesitem.h"
23 #include "views/viewproperties.h"
25 Q_DECLARE_METATYPE(KItemRangeList
)
26 Q_DECLARE_METATYPE(KItemRange
)
28 static QString
bookmarksFile()
30 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation
) + "/user-places.xbel";
33 class PlacesItemModelTest
: public QObject
42 void cleanupTestCase();
46 void testDeletePlace();
47 void testPlaceItem_data();
49 void testTearDownDevice();
50 void testDefaultViewProperties_data();
51 void testDefaultViewProperties();
54 void testSystemItems();
55 void testEditBookmark();
56 void testEditAfterCreation();
57 void testEditMetadata();
59 void testIcons_data();
61 void testDragAndDrop();
62 void testHideDevices();
63 void testDuplicatedEntries();
64 void renameAfterCreation();
67 PlacesItemModel
* m_model
;
68 QSet
<int> m_tobeRemoved
;
69 QMap
<QString
, QDBusInterface
*> m_interfacesMap
;
70 int m_expectedModelCount
= 14;
71 bool m_hasDesktopFolder
= false;
72 bool m_hasDocumentsFolder
= false;
73 bool m_hasDownloadsFolder
= false;
74 bool m_hasMusicFolder
= false;
75 bool m_hasPicturesFolder
= false;
76 bool m_hasVideosFolder
= false;
78 void setBalooEnabled(bool enabled
);
79 int indexOf(const QUrl
&url
);
80 QDBusInterface
*fakeManager();
81 QDBusInterface
*fakeDevice(const QString
&udi
);
82 QStringList
placesUrls(PlacesItemModel
*model
= nullptr) const;
83 QStringList
initialUrls() const;
84 void createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
);
85 void schedulePlaceRemoval(int index
);
86 void cancelPlaceRemoval(int index
);
87 QMimeData
*createMimeData(const QList
<int> &indexes
) const;
88 void increaseIndexIfNeeded(int &index
) const;
89 QTemporaryDir m_tempHomeDir
;
92 #define CHECK_PLACES_URLS(urls) \
94 QStringList places = placesUrls(); \
95 if (places != urls) { \
96 qWarning() << "Expected:" << urls; \
97 qWarning() << "Got:" << places; \
98 QCOMPARE(places, urls); \
102 void PlacesItemModelTest::setBalooEnabled(bool enabled
)
104 KConfig
config(QStringLiteral("baloofilerc"));
105 KConfigGroup basicSettings
= config
.group("Basic Settings");
106 basicSettings
.writeEntry("Indexing-Enabled", enabled
);
110 int PlacesItemModelTest::indexOf(const QUrl
&url
)
112 for (int r
= 0; r
< m_model
->count(); r
++) {
113 if (m_model
->placesItem(r
)->url() == url
) {
120 QDBusInterface
*PlacesItemModelTest::fakeManager()
122 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
125 QDBusInterface
*PlacesItemModelTest::fakeDevice(const QString
&udi
)
127 if (m_interfacesMap
.contains(udi
)) {
128 return m_interfacesMap
[udi
];
131 QDBusInterface
*iface
= new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi
);
132 m_interfacesMap
[udi
] = iface
;
137 QStringList
PlacesItemModelTest::placesUrls(PlacesItemModel
*model
) const
144 for (int row
= 0; row
< model
->count(); ++row
) {
145 urls
<< model
->placesItem(row
)->url().toDisplayString(QUrl::PreferLocalFile
);
150 QStringList
PlacesItemModelTest::initialUrls() const
152 static QStringList urls
;
153 if (urls
.isEmpty()) {
154 urls
<< QDir::homePath();
156 if (m_hasDesktopFolder
) {
157 urls
<< QDir::homePath() + QStringLiteral("/Desktop");
160 if (m_hasDocumentsFolder
) {
161 urls
<< QDir::homePath() + QStringLiteral("/Documents");
164 if (m_hasDownloadsFolder
) {
165 urls
<< QDir::homePath() + QStringLiteral("/Downloads");
168 if (m_hasMusicFolder
) {
169 urls
<< QDir::homePath() + QStringLiteral("/Music");
172 if (m_hasPicturesFolder
) {
173 urls
<< QDir::homePath() + QStringLiteral("/Pictures");
176 if (m_hasVideosFolder
) {
177 urls
<< QDir::homePath() + QStringLiteral("/Videos");
180 urls
<< QStringLiteral("trash:/")
181 << QStringLiteral("remote:/")
182 << QStringLiteral("/media/nfs");
184 if (qEnvironmentVariableIsSet("KDE_FULL_SESSION") && KProtocolInfo::isKnownProtocol(QStringLiteral("recentlyused"))) {
185 urls
<< QStringLiteral("recentlyused:/files");
186 urls
<< QStringLiteral("recentlyused:/locations");
188 urls
<< QStringLiteral("timeline:/today")
189 << QStringLiteral("timeline:/yesterday");
192 urls
<< QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
193 << QStringLiteral("/foreign")
194 << QStringLiteral("/media/floppy0") << QStringLiteral("/media/XO-Y4") << QStringLiteral("/media/cdrom");
199 void PlacesItemModelTest::createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
)
201 m_model
->createPlacesItem(text
, url
, icon
);
204 void PlacesItemModelTest::schedulePlaceRemoval(int index
)
206 m_tobeRemoved
.insert(index
);
209 void PlacesItemModelTest::cancelPlaceRemoval(int index
)
211 m_tobeRemoved
.remove(index
);
214 QMimeData
*PlacesItemModelTest::createMimeData(const QList
<int> &indexes
) const
217 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
220 for (int index
: indexes
) {
221 const QUrl itemUrl
= m_model
->placesItem(index
)->url();
222 if (itemUrl
.isValid()) {
228 QMimeData
* mimeData
= new QMimeData();
229 mimeData
->setUrls(urls
);
230 // copied from PlacesItemModel::internalMimeType()
231 const QString internalMimeType
= "application/x-dolphinplacesmodel-" +
232 QString::number((qptrdiff
)m_model
);
233 mimeData
->setData(internalMimeType
, itemData
);
237 void PlacesItemModelTest::increaseIndexIfNeeded(int &index
) const
239 if (m_hasDesktopFolder
) {
242 if (m_hasDocumentsFolder
) {
245 if (m_hasDownloadsFolder
) {
248 if (m_hasMusicFolder
) {
251 if (m_hasPicturesFolder
) {
254 if (m_hasVideosFolder
) {
259 void PlacesItemModelTest::init()
261 m_model
= new PlacesItemModel();
262 // WORKAROUND: need to wait for bookmark to load
264 QCOMPARE(m_model
->count(), m_expectedModelCount
);
267 void PlacesItemModelTest::cleanup()
269 const auto tobeRemoved
= m_tobeRemoved
;
270 for (const int i
: tobeRemoved
) {
271 int before
= m_model
->count();
272 m_model
->deleteItem(i
);
273 QTRY_COMPARE(m_model
->count(), before
- 1);
275 m_tobeRemoved
.clear();
280 void PlacesItemModelTest::initTestCase()
282 QVERIFY(m_tempHomeDir
.isValid());
283 QVERIFY(qputenv("HOME", m_tempHomeDir
.path().toUtf8()));
284 QVERIFY(qputenv("KDE_FORK_SLAVES", "yes"));
286 QStandardPaths::setTestModeEnabled(true);
288 const QString fakeHw
= QFINDTESTDATA("data/fakecomputer.xml");
289 QVERIFY(!fakeHw
.isEmpty());
290 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw
));
292 setBalooEnabled(true);
293 const QString bookmarsFileName
= bookmarksFile();
294 if (QFileInfo::exists(bookmarsFileName
)) {
295 // Ensure we'll have a clean bookmark file to start
296 QVERIFY(QFile::remove(bookmarsFileName
));
299 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation
)).exists()) {
300 m_hasDesktopFolder
= true;
301 m_expectedModelCount
++;
304 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation
)).exists()) {
305 m_hasDocumentsFolder
= true;
306 m_expectedModelCount
++;
309 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation
)).exists()) {
310 m_hasDownloadsFolder
= true;
311 m_expectedModelCount
++;
314 if (QDir(QStandardPaths::writableLocation(QStandardPaths::MusicLocation
)).exists()) {
315 m_hasMusicFolder
= true;
316 m_expectedModelCount
++;
319 if (QDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation
)).exists()) {
320 m_hasPicturesFolder
= true;
321 m_expectedModelCount
++;
324 if (QDir(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation
)).exists()) {
325 m_hasVideosFolder
= true;
326 m_expectedModelCount
++;
329 qRegisterMetaType
<KItemRangeList
>();
330 qRegisterMetaType
<KItemRange
>();
333 void PlacesItemModelTest::cleanupTestCase()
335 qDeleteAll(m_interfacesMap
);
336 QFile::remove(bookmarksFile());
339 void PlacesItemModelTest::testModelSort()
341 CHECK_PLACES_URLS(initialUrls());
344 void PlacesItemModelTest::testGroups()
346 const auto groups
= m_model
->groups();
347 int expectedRemoteIndex
= 2;
348 increaseIndexIfNeeded(expectedRemoteIndex
);
350 QCOMPARE(groups
.size(), 6);
352 QCOMPARE(groups
.at(0).first
, 0);
353 QCOMPARE(groups
.at(0).second
.toString(), QStringLiteral("Places"));
355 QCOMPARE(groups
.at(1).first
, expectedRemoteIndex
);
356 QCOMPARE(groups
.at(1).second
.toString(), QStringLiteral("Remote"));
358 QCOMPARE(groups
.at(2).first
, expectedRemoteIndex
+ 2);
359 QCOMPARE(groups
.at(2).second
.toString(), QStringLiteral("Recent"));
361 QCOMPARE(groups
.at(3).first
, expectedRemoteIndex
+ 4);
362 QCOMPARE(groups
.at(3).second
.toString(), QStringLiteral("Search For"));
364 QCOMPARE(groups
.at(4).first
, expectedRemoteIndex
+ 8);
365 QCOMPARE(groups
.at(4).second
.toString(), QStringLiteral("Devices"));
367 QCOMPARE(groups
.at(5).first
, expectedRemoteIndex
+ 9);
368 QCOMPARE(groups
.at(5).second
.toString(), QStringLiteral("Removable Devices"));
371 void PlacesItemModelTest::testPlaceItem_data()
373 QTest::addColumn
<QUrl
>("url");
374 QTest::addColumn
<bool>("expectedIsHidden");
375 QTest::addColumn
<bool>("expectedIsSystemItem");
376 QTest::addColumn
<QString
>("expectedGroup");
377 QTest::addColumn
<bool>("expectedStorageSetupNeeded");
380 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
383 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
386 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
389 void PlacesItemModelTest::testPlaceItem()
392 QFETCH(bool, expectedIsHidden
);
393 QFETCH(bool, expectedIsSystemItem
);
394 QFETCH(QString
, expectedGroup
);
395 QFETCH(bool, expectedStorageSetupNeeded
);
397 const int index
= indexOf(url
);
398 PlacesItem
*item
= m_model
->placesItem(index
);
399 QCOMPARE(item
->url(), url
);
400 QCOMPARE(item
->isHidden(), expectedIsHidden
);
401 QCOMPARE(item
->isSystemItem(), expectedIsSystemItem
);
402 QCOMPARE(item
->group(), expectedGroup
);
403 QCOMPARE(item
->storageSetupNeeded(), expectedStorageSetupNeeded
);
406 void PlacesItemModelTest::testDeletePlace()
408 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
409 QStringList urls
= initialUrls();
410 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
411 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
413 PlacesItemModel
*model
= new PlacesItemModel();
415 int tempDirIndex
= 2;
416 increaseIndexIfNeeded(tempDirIndex
);
418 // create a new place
419 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
420 urls
.insert(tempDirIndex
, tempUrl
.toLocalFile());
422 // check if the new entry was created
423 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
424 CHECK_PLACES_URLS(urls
);
425 QTRY_COMPARE(model
->count(), m_model
->count());
428 m_model
->deleteItem(tempDirIndex
);
430 // make sure that the new item is removed
431 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
432 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
);
433 CHECK_PLACES_URLS(initialUrls());
434 QTRY_COMPARE(model
->count(), m_model
->count());
437 void PlacesItemModelTest::testTearDownDevice()
439 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
440 int index
= indexOf(mediaUrl
);
441 QVERIFY(index
!= -1);
443 auto ejectAction
= m_model
->ejectAction(index
);
444 QVERIFY(!ejectAction
);
446 auto teardownAction
= m_model
->teardownAction(index
);
447 QVERIFY(teardownAction
);
449 QCOMPARE(m_model
->count(), m_expectedModelCount
);
451 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
452 fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
453 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
- 1);
454 QCOMPARE(spyItemsRemoved
.count(), 1);
455 const QList
<QVariant
> spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
456 const KItemRangeList removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
457 QCOMPARE(removedRange
.size(), 1);
458 QCOMPARE(removedRange
.first().index
, index
);
459 QCOMPARE(removedRange
.first().count
, 1);
461 QCOMPARE(indexOf(mediaUrl
), -1);
463 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
464 fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
465 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
);
466 QCOMPARE(spyItemsInserted
.count(), 1);
467 index
= indexOf(mediaUrl
);
469 const QList
<QVariant
> args
= spyItemsInserted
.takeFirst();
470 const KItemRangeList insertedRange
= args
.at(0).value
<KItemRangeList
>();
471 QCOMPARE(insertedRange
.size(), 1);
472 QCOMPARE(insertedRange
.first().index
, index
);
473 QCOMPARE(insertedRange
.first().count
, 1);
476 void PlacesItemModelTest::testDefaultViewProperties_data()
478 QTest::addColumn
<QUrl
>("url");
479 QTest::addColumn
<DolphinView::Mode
>("expectedViewMode");
480 QTest::addColumn
<bool>("expectedPreviewShow");
481 QTest::addColumn
<QList
<QByteArray
> >("expectedVisibleRole");
484 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
487 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "path"});
490 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "artist", "album"});
493 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
497 void PlacesItemModelTest::testDefaultViewProperties()
500 QFETCH(DolphinView::Mode
, expectedViewMode
);
501 QFETCH(bool, expectedPreviewShow
);
502 QFETCH(QList
<QByteArray
>, expectedVisibleRole
);
504 // In order to test the default view properties, turn off the global view properties and re-init the test to reload the model.
505 GeneralSettings
* settings
= GeneralSettings::self();
506 settings
->setGlobalViewProps(false);
511 ViewProperties
properties(KFilePlacesModel::convertedUrl(url
));
512 QCOMPARE(properties
.viewMode(), expectedViewMode
);
513 QCOMPARE(properties
.previewsShown(), expectedPreviewShow
);
514 QCOMPARE(properties
.visibleRoles(), expectedVisibleRole
);
516 settings
->setGlobalViewProps(true);
520 void PlacesItemModelTest::testClear()
522 QCOMPARE(m_model
->count(), m_expectedModelCount
);
524 QCOMPARE(m_model
->count(), 0);
525 QCOMPARE(m_model
->hiddenCount(), 0);
527 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
);
530 void PlacesItemModelTest::testHideItem()
532 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
533 const int index
= indexOf(mediaUrl
);
535 PlacesItem
*item
= m_model
->placesItem(index
);
537 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
538 QList
<QVariant
> spyItemsRemovedArgs
;
539 KItemRangeList removedRange
;
541 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
542 QList
<QVariant
> spyItemsInsertedArgs
;
543 KItemRangeList insertedRange
;
547 item
->setHidden(true);
549 // check if items removed was fired
550 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
- 1);
551 QCOMPARE(spyItemsRemoved
.count(), 1);
552 spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
553 removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
554 QCOMPARE(removedRange
.size(), 1);
555 QCOMPARE(removedRange
.first().index
, index
);
556 QCOMPARE(removedRange
.first().count
, 1);
558 // allow model to show hidden items
559 m_model
->setHiddenItemsShown(true);
561 // check if the items inserted was fired
562 spyItemsInsertedArgs
= spyItemsInserted
.takeFirst();
563 insertedRange
= spyItemsInsertedArgs
.at(0).value
<KItemRangeList
>();
564 QCOMPARE(insertedRange
.size(), 1);
565 QCOMPARE(insertedRange
.first().index
, index
);
566 QCOMPARE(insertedRange
.first().count
, 1);
568 // mark item as visible
569 item
= m_model
->placesItem(index
);
570 item
->setHidden(false);
572 // mark model to hide invisible items
573 m_model
->setHiddenItemsShown(true);
575 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
);
578 void PlacesItemModelTest::testSystemItems()
580 int tempDirIndex
= 2;
581 increaseIndexIfNeeded(tempDirIndex
);
583 QCOMPARE(m_model
->count(), m_expectedModelCount
);
584 for (int r
= 0; r
< m_model
->count(); r
++) {
585 QCOMPARE(m_model
->placesItem(r
)->isSystemItem(), !m_model
->placesItem(r
)->device().isValid());
588 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
590 // create a new entry (non system item)
591 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
593 // check if the new entry was created
594 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
596 // make sure the new place get removed
597 schedulePlaceRemoval(tempDirIndex
);
599 QList
<QVariant
> args
= itemsInsertedSpy
.takeFirst();
600 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
601 QCOMPARE(range
.first().index
, tempDirIndex
);
602 QCOMPARE(range
.first().count
, 1);
603 QVERIFY(!m_model
->placesItem(tempDirIndex
)->isSystemItem());
604 QCOMPARE(m_model
->count(), m_expectedModelCount
+ 1);
607 // check if the removal signal is correct
608 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
609 m_model
->deleteItem(tempDirIndex
);
610 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
611 args
= itemsRemovedSpy
.takeFirst();
612 range
= args
.at(0).value
<KItemRangeList
>();
613 QCOMPARE(range
.first().index
, tempDirIndex
);
614 QCOMPARE(range
.first().count
, 1);
615 QTRY_COMPARE(m_model
->count(), m_expectedModelCount
);
617 //cancel removal (it was removed above)
618 cancelPlaceRemoval(tempDirIndex
);
621 void PlacesItemModelTest::testEditBookmark()
623 int tempDirIndex
= 2;
624 increaseIndexIfNeeded(tempDirIndex
);
626 QScopedPointer
<PlacesItemModel
> other(new PlacesItemModel());
628 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
630 // make sure that the new item will be removed later
631 schedulePlaceRemoval(tempDirIndex
);
633 QSignalSpy
itemsChangedSply(m_model
, &PlacesItemModel::itemsChanged
);
636 m_model
->item(tempDirIndex
)->setText(QStringLiteral("Renamed place"));
639 // check if the correct signal was fired
640 QTRY_COMPARE(itemsChangedSply
.count(), 1);
641 QList
<QVariant
> args
= itemsChangedSply
.takeFirst();
642 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
643 QCOMPARE(range
.first().index
, tempDirIndex
);
644 QCOMPARE(range
.first().count
, 1);
645 QSet
<QByteArray
> roles
= args
.at(1).value
<QSet
<QByteArray
> >();
646 QCOMPARE(roles
.size(), 1);
647 QCOMPARE(*roles
.begin(), QByteArrayLiteral("text"));
648 QCOMPARE(m_model
->item(tempDirIndex
)->text(), QStringLiteral("Renamed place"));
650 // check if the item was updated in the other model
651 QTRY_COMPARE(other
->item(tempDirIndex
)->text(), QStringLiteral("Renamed place"));
654 void PlacesItemModelTest::testEditAfterCreation()
656 int tempDirIndex
= 2;
657 increaseIndexIfNeeded(tempDirIndex
);
659 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
660 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
662 // create a new place
663 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
664 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
666 PlacesItemModel
*model
= new PlacesItemModel();
667 QTRY_COMPARE(model
->count(), m_model
->count());
669 // make sure that the new item will be removed later
670 schedulePlaceRemoval(tempDirIndex
);
673 PlacesItem
*item
= m_model
->placesItem(tempDirIndex
);
674 item
->setText(QStringLiteral("Renamed place"));
677 // check if the second model got the changes
678 QTRY_COMPARE(model
->count(), m_model
->count());
679 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->text(), m_model
->placesItem(tempDirIndex
)->text());
680 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
681 m_model
->placesItem(tempDirIndex
)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
682 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->icon(), m_model
->placesItem(tempDirIndex
)->icon());
683 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->url(), m_model
->placesItem(tempDirIndex
)->url());
686 void PlacesItemModelTest::testEditMetadata()
688 int tempDirIndex
= 2;
689 increaseIndexIfNeeded(tempDirIndex
);
691 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
692 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
694 // create a new place
695 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
696 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
698 // check if the new entry was created
699 PlacesItemModel
*model
= new PlacesItemModel();
700 QTRY_COMPARE(model
->count(), m_model
->count());
702 // make sure that the new item will be removed later
703 schedulePlaceRemoval(tempDirIndex
);
705 // modify place metadata
706 auto bookmark
= m_model
->placesItem(tempDirIndex
)->bookmark();
707 bookmark
.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
710 // check if the place was modified in both models
711 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
712 KAboutData::applicationData().componentName());
713 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->text(), m_model
->placesItem(tempDirIndex
)->text());
714 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
715 m_model
->placesItem(tempDirIndex
)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
716 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->icon(), m_model
->placesItem(tempDirIndex
)->icon());
717 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->url(), m_model
->placesItem(tempDirIndex
)->url());
720 void PlacesItemModelTest::testRefresh()
722 int tempDirIndex
= 2;
723 increaseIndexIfNeeded(tempDirIndex
);
725 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
726 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
728 // create a new place
729 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
730 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
732 PlacesItemModel
*model
= new PlacesItemModel();
733 QTRY_COMPARE(model
->count(), m_model
->count());
735 // make sure that the new item will be removed later
736 schedulePlaceRemoval(tempDirIndex
);
738 PlacesItem
*item
= m_model
->placesItem(tempDirIndex
);
739 PlacesItem
*sameItem
= model
->placesItem(tempDirIndex
);
740 QCOMPARE(item
->text(), sameItem
->text());
743 item
->setText(QStringLiteral("Renamed place"));
745 // item from another model is not affected at the moment
746 QVERIFY(item
->text() != sameItem
->text());
751 // item must be equal
752 QTRY_COMPARE(item
->text(), sameItem
->text());
755 void PlacesItemModelTest::testIcons_data()
757 QTest::addColumn
<QUrl
>("url");
758 QTest::addColumn
<QString
>("expectedIconName");
761 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
764 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
767 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
770 void PlacesItemModelTest::testIcons()
773 QFETCH(QString
, expectedIconName
);
775 PlacesItem
*item
= m_model
->placesItem(indexOf(url
));
776 QCOMPARE(item
->icon(), expectedIconName
);
778 for (int r
= 0; r
< m_model
->count(); r
++) {
779 QVERIFY(!m_model
->placesItem(r
)->icon().isEmpty());
783 void PlacesItemModelTest::testDragAndDrop()
785 int lastIndex
= 1; // last index of places group
786 increaseIndexIfNeeded(lastIndex
);
788 QList
<QVariant
> args
;
789 KItemRangeList range
;
790 QStringList urls
= initialUrls();
792 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
793 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
795 CHECK_PLACES_URLS(initialUrls());
796 // Move the home directory to the end of the places group
797 QMimeData
*dropData
= createMimeData(QList
<int>() << 0);
798 m_model
->dropMimeDataBefore(m_model
->count() - 1, dropData
);
799 urls
.move(0, lastIndex
);
802 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
803 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
805 // remove item from actual position
806 args
= itemsRemovedSpy
.takeFirst();
807 range
= args
.at(0).value
<KItemRangeList
>();
808 QCOMPARE(range
.size(), 1);
809 QCOMPARE(range
.at(0).count
, 1);
810 QCOMPARE(range
.at(0).index
, 0);
812 // insert intem in his group
813 args
= itemsInsertedSpy
.takeFirst();
814 range
= args
.at(0).value
<KItemRangeList
>();
815 QCOMPARE(range
.size(), 1);
816 QCOMPARE(range
.at(0).count
, 1);
817 QCOMPARE(range
.at(0).index
, lastIndex
);
819 CHECK_PLACES_URLS(urls
);
821 itemsInsertedSpy
.clear();
822 itemsRemovedSpy
.clear();
824 // Move home directory item back to its original position
825 dropData
= createMimeData(QList
<int>() << lastIndex
);
826 m_model
->dropMimeDataBefore(0, dropData
);
827 urls
.move(lastIndex
, 0);
830 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
831 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
833 // remove item from actual position
834 args
= itemsRemovedSpy
.takeFirst();
835 range
= args
.at(0).value
<KItemRangeList
>();
836 QCOMPARE(range
.size(), 1);
837 QCOMPARE(range
.at(0).count
, 1);
838 QCOMPARE(range
.at(0).index
, lastIndex
);
840 // insert intem in the requested position
841 args
= itemsInsertedSpy
.takeFirst();
842 range
= args
.at(0).value
<KItemRangeList
>();
843 QCOMPARE(range
.size(), 1);
844 QCOMPARE(range
.at(0).count
, 1);
845 QCOMPARE(range
.at(0).index
, 0);
847 CHECK_PLACES_URLS(urls
);
850 void PlacesItemModelTest::testHideDevices()
852 QSignalSpy
itemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
853 QStringList urls
= initialUrls();
855 m_model
->setGroupHidden(KFilePlacesModel::RemovableDevicesType
, true);
856 QTRY_VERIFY(m_model
->isGroupHidden(KFilePlacesModel::RemovableDevicesType
));
857 QTRY_COMPARE(itemsRemoved
.count(), 3);
859 // remove removable-devices
860 urls
.removeOne(QStringLiteral("/media/floppy0"));
861 urls
.removeOne(QStringLiteral("/media/XO-Y4"));
862 urls
.removeOne(QStringLiteral("/media/cdrom"));
864 // check if the correct urls was removed
865 CHECK_PLACES_URLS(urls
);
868 m_model
= new PlacesItemModel();
869 QTRY_COMPARE(m_model
->count(), urls
.count());
870 CHECK_PLACES_URLS(urls
);
873 m_model
->setGroupHidden(KFilePlacesModel::RemovableDevicesType
, false);
874 urls
= initialUrls();
875 QTRY_COMPARE(m_model
->count(), urls
.count());
876 CHECK_PLACES_URLS(urls
);
879 void PlacesItemModelTest::testDuplicatedEntries()
881 QStringList urls
= initialUrls();
882 // create a duplicated entry on bookmark
883 KBookmarkManager
*bookmarkManager
= KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
884 KBookmarkGroup root
= bookmarkManager
->root();
885 KBookmark bookmark
= root
.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {});
887 const QString id
= QUuid::createUuid().toString();
888 bookmark
.setMetaDataItem(QStringLiteral("ID"), id
);
889 bookmark
.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
890 bookmarkManager
->emitChanged(bookmarkManager
->root());
892 PlacesItemModel
*newModel
= new PlacesItemModel();
893 QTRY_COMPARE(placesUrls(newModel
).count(QStringLiteral("search:/videos")), 1);
894 QTRY_COMPARE(urls
, placesUrls(newModel
));
898 void PlacesItemModelTest::renameAfterCreation()
900 int tempDirIndex
= 2;
901 increaseIndexIfNeeded(tempDirIndex
);
903 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
904 QStringList urls
= initialUrls();
905 PlacesItemModel
*model
= new PlacesItemModel();
907 CHECK_PLACES_URLS(urls
);
908 QTRY_COMPARE(model
->count(), m_model
->count());
910 // create a new place
911 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
912 urls
.insert(tempDirIndex
, tempUrl
.toLocalFile());
914 // make sure that the new item will be removed later
915 schedulePlaceRemoval(tempDirIndex
);
917 CHECK_PLACES_URLS(urls
);
918 QCOMPARE(model
->count(), m_model
->count());
922 QSignalSpy
changedSpy(m_model
, &PlacesItemModel::itemsChanged
);
924 PlacesItem
*item
= m_model
->placesItem(tempDirIndex
);
925 item
->setText(QStringLiteral("New Temporary Dir"));
926 item
->setUrl(item
->url());
927 item
->setIcon(item
->icon());
930 QTRY_COMPARE(changedSpy
.count(), 1);
932 // check if the place was modified in both models
933 QTRY_COMPARE(m_model
->placesItem(tempDirIndex
)->text(), QStringLiteral("New Temporary Dir"));
934 QTRY_COMPARE(model
->placesItem(tempDirIndex
)->text(), QStringLiteral("New Temporary Dir"));
937 QTEST_MAIN(PlacesItemModelTest
)
939 #include "placesitemmodeltest.moc"