1 /***************************************************************************
2 * Copyright (C) 2017 by Renato Araujo Oliveira <renato.araujo@kdab.com> *
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. *
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. *
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 ***************************************************************************/
25 #include <QStandardPaths>
27 #include <QDBusInterface>
30 #include <KBookmarkManager>
32 #include <KConfigGroup>
34 #include <KFilePlacesModel>
36 #include "panels/places/placesitemmodel.h"
37 #include "panels/places/placesitem.h"
38 #include "views/viewproperties.h"
39 #include "kitemviews/kitemrange.h"
41 Q_DECLARE_METATYPE(KItemRangeList
)
42 Q_DECLARE_METATYPE(KItemRange
)
45 //c:\ as root for windows
46 #define KDE_ROOT_PATH "C:\\"
48 #define KDE_ROOT_PATH "/"
51 static QString
bookmarksFile()
53 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation
) + "/user-places.xbel";
56 class PlacesItemModelTest
: public QObject
65 void cleanupTestCase();
69 void testDeletePlace();
70 void testPlaceItem_data();
72 void testTearDownDevice();
73 void testDefaultViewProperties_data();
74 void testDefaultViewProperties();
77 void testSystemItems();
78 void testEditBookmark();
79 void testEditAfterCreation();
80 void testEditMetadata();
82 void testIcons_data();
84 void testDragAndDrop();
85 void testHideDevices();
86 void testDuplicatedEntries();
89 PlacesItemModel
* m_model
;
90 QSet
<int> m_tobeRemoved
;
91 QMap
<QString
, QDBusInterface
*> m_interfacesMap
;
93 void setBalooEnabled(bool enabled
);
94 int indexOf(const QUrl
&url
);
95 QDBusInterface
*fakeManager();
96 QDBusInterface
*fakeDevice(const QString
&udi
);
97 QStringList
placesUrls(PlacesItemModel
*model
= nullptr) const;
98 QStringList
initialUrls() const;
99 void createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
);
100 void removePlaceAfter(int index
);
101 void cancelPlaceRemoval(int index
);
102 void removeTestUserData();
103 QMimeData
*createMimeData(const QList
<int> &indexes
) const;
106 #define CHECK_PLACES_URLS(urls) \
108 QStringList places = placesUrls(); \
109 if (places != urls) { \
110 qWarning() << "Expected:" << urls; \
111 qWarning() << "Got:" << places; \
112 QCOMPARE(places, urls); \
116 void PlacesItemModelTest::setBalooEnabled(bool enabled
)
118 KConfig
config(QStringLiteral("baloofilerc"));
119 KConfigGroup basicSettings
= config
.group("Basic Settings");
120 basicSettings
.writeEntry("Indexing-Enabled", enabled
);
124 int PlacesItemModelTest::indexOf(const QUrl
&url
)
126 for (int r
= 0; r
< m_model
->count(); r
++) {
127 if (m_model
->placesItem(r
)->url() == url
) {
134 QDBusInterface
*PlacesItemModelTest::fakeManager()
136 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
139 QDBusInterface
*PlacesItemModelTest::fakeDevice(const QString
&udi
)
141 if (m_interfacesMap
.contains(udi
)) {
142 return m_interfacesMap
[udi
];
145 QDBusInterface
*iface
= new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi
);
146 m_interfacesMap
[udi
] = iface
;
151 QStringList
PlacesItemModelTest::placesUrls(PlacesItemModel
*model
) const
158 for (int row
= 0; row
< model
->count(); ++row
) {
159 urls
<< model
->placesItem(row
)->url().toDisplayString(QUrl::PreferLocalFile
);
164 QStringList
PlacesItemModelTest::initialUrls() const
166 static QStringList urls
;
167 if (urls
.isEmpty()) {
168 urls
<< QDir::homePath() << QStringLiteral(KDE_ROOT_PATH
) << QStringLiteral("trash:/")
169 << QStringLiteral("remote:/")
170 << QStringLiteral("timeline:/today") << QStringLiteral("timeline:/yesterday") << QStringLiteral("timeline:/thismonth") << QStringLiteral("timeline:/lastmonth")
171 << QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
172 << QStringLiteral("/media/nfs") << QStringLiteral("/foreign")
173 << QStringLiteral("/media/floppy0") << QStringLiteral("/media/XO-Y4") << QStringLiteral("/media/cdrom");
178 void PlacesItemModelTest::createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
)
180 m_model
->createPlacesItem(text
, url
, icon
);
183 void PlacesItemModelTest::removePlaceAfter(int index
)
185 m_tobeRemoved
.insert(index
);
188 void PlacesItemModelTest::cancelPlaceRemoval(int index
)
190 m_tobeRemoved
.remove(index
);
193 void PlacesItemModelTest::removeTestUserData()
195 // user hardcoded path to avoid removal of any user personal data
196 QDir
dir(QStringLiteral("/home/renato/.qttest/share/placesitemmodeltest"));
198 QVERIFY(dir
.removeRecursively());
202 QMimeData
*PlacesItemModelTest::createMimeData(const QList
<int> &indexes
) const
205 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
208 for (int index
: indexes
) {
209 const QUrl itemUrl
= m_model
->placesItem(index
)->url();
210 if (itemUrl
.isValid()) {
216 QMimeData
* mimeData
= new QMimeData();
217 mimeData
->setUrls(urls
);
218 // copied from PlacesItemModel::internalMimeType()
219 const QString internalMimeType
= "application/x-dolphinplacesmodel-" +
220 QString::number((qptrdiff
)m_model
);
221 mimeData
->setData(internalMimeType
, itemData
);
225 void PlacesItemModelTest::init()
227 m_model
= new PlacesItemModel();
228 // WORKAROUND: need to wait for bookmark to load, check: PlacesItemModel::updateBookmarks
230 QCOMPARE(m_model
->count(), 17);
233 void PlacesItemModelTest::cleanup()
235 for (int i
: m_tobeRemoved
) {
236 int before
= m_model
->count();
237 m_model
->deleteItem(i
);
238 QTRY_COMPARE(m_model
->count(), before
- 1);
240 m_tobeRemoved
.clear();
243 removeTestUserData();
246 void PlacesItemModelTest::initTestCase()
248 QStandardPaths::setTestModeEnabled(true);
249 // remove test user data
250 removeTestUserData();
252 const QString fakeHw
= QFINDTESTDATA("data/fakecomputer.xml");
253 QVERIFY(!fakeHw
.isEmpty());
254 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw
));
256 setBalooEnabled(true);
257 const QString bookmarsFileName
= bookmarksFile();
258 if (QFileInfo::exists(bookmarsFileName
)) {
259 // Ensure we'll have a clean bookmark file to start
260 QVERIFY(QFile::remove(bookmarsFileName
));
263 qRegisterMetaType
<KItemRangeList
>();
264 qRegisterMetaType
<KItemRange
>();
267 void PlacesItemModelTest::cleanupTestCase()
269 qDeleteAll(m_interfacesMap
);
270 QFile::remove(bookmarksFile());
272 // Remove any previous properties file
273 removeTestUserData();
276 void PlacesItemModelTest::testModelSort()
278 CHECK_PLACES_URLS(initialUrls());
281 void PlacesItemModelTest::testGroups()
283 const auto groups
= m_model
->groups();
285 QCOMPARE(groups
.size(), 6);
287 QCOMPARE(groups
.at(0).first
, 0);
288 QCOMPARE(groups
.at(0).second
.toString(), QStringLiteral("Places"));
290 QCOMPARE(groups
.at(1).first
, 3);
291 QCOMPARE(groups
.at(1).second
.toString(), QStringLiteral("Remote"));
293 QCOMPARE(groups
.at(2).first
, 4);
294 QCOMPARE(groups
.at(2).second
.toString(), QStringLiteral("Recently Saved"));
296 QCOMPARE(groups
.at(3).first
, 8);
297 QCOMPARE(groups
.at(3).second
.toString(), QStringLiteral("Search For"));
299 QCOMPARE(groups
.at(4).first
, 12);
300 QCOMPARE(groups
.at(4).second
.toString(), QStringLiteral("Devices"));
302 QCOMPARE(groups
.at(5).first
, 14);
303 QCOMPARE(groups
.at(5).second
.toString(), QStringLiteral("Removable Devices"));
306 void PlacesItemModelTest::testPlaceItem_data()
308 QTest::addColumn
<QUrl
>("url");
309 QTest::addColumn
<bool>("expectedIsHidden");
310 QTest::addColumn
<bool>("expectedIsSystemItem");
311 QTest::addColumn
<QString
>("expectedGroup");
312 QTest::addColumn
<bool>("expectedStorageSetupNeeded");
315 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
318 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
321 QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << false << true << QStringLiteral("Recently Saved") << false;
324 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
327 void PlacesItemModelTest::testPlaceItem()
330 QFETCH(bool, expectedIsHidden
);
331 QFETCH(bool, expectedIsSystemItem
);
332 QFETCH(QString
, expectedGroup
);
333 QFETCH(bool, expectedStorageSetupNeeded
);
335 const int index
= indexOf(url
);
336 PlacesItem
*item
= m_model
->placesItem(index
);
337 QCOMPARE(item
->url(), url
);
338 QCOMPARE(item
->isHidden(), expectedIsHidden
);
339 QCOMPARE(item
->isSystemItem(), expectedIsSystemItem
);
340 QCOMPARE(item
->group(), expectedGroup
);
341 QCOMPARE(item
->storageSetupNeeded(), expectedStorageSetupNeeded
);
344 void PlacesItemModelTest::testDeletePlace()
346 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
347 QStringList urls
= initialUrls();
348 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
349 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
351 PlacesItemModel
*model
= new PlacesItemModel();
353 // create a new place
354 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
355 urls
.insert(3, tempUrl
.toLocalFile());
357 // check if the new entry was created
358 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
359 CHECK_PLACES_URLS(urls
);
360 QTRY_COMPARE(model
->count(), m_model
->count());
363 m_model
->deleteItem(3);
365 // make sure that the new item is removed
366 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
367 QTRY_COMPARE(m_model
->count(), 17);
368 CHECK_PLACES_URLS(initialUrls());
369 QTRY_COMPARE(model
->count(), m_model
->count());
372 void PlacesItemModelTest::testTearDownDevice()
374 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
375 int index
= indexOf(mediaUrl
);
376 QVERIFY(index
!= -1);
378 auto ejectAction
= m_model
->ejectAction(index
);
379 QVERIFY(!ejectAction
);
381 auto teardownAction
= m_model
->teardownAction(index
);
382 QVERIFY(teardownAction
);
384 QCOMPARE(m_model
->count(), 17);
386 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
387 fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
388 QTRY_COMPARE(m_model
->count(), 16);
389 QCOMPARE(spyItemsRemoved
.count(), 1);
390 const QList
<QVariant
> spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
391 const KItemRangeList removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
392 QCOMPARE(removedRange
.size(), 1);
393 QCOMPARE(removedRange
.first().index
, index
);
394 QCOMPARE(removedRange
.first().count
, 1);
396 QCOMPARE(indexOf(mediaUrl
), -1);
398 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
399 fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
400 QTRY_COMPARE(m_model
->count(), 17);
401 QCOMPARE(spyItemsInserted
.count(), 1);
402 index
= indexOf(mediaUrl
);
404 const QList
<QVariant
> args
= spyItemsInserted
.takeFirst();
405 const KItemRangeList insertedRange
= args
.at(0).value
<KItemRangeList
>();
406 QCOMPARE(insertedRange
.size(), 1);
407 QCOMPARE(insertedRange
.first().index
, index
);
408 QCOMPARE(insertedRange
.first().count
, 1);
411 void PlacesItemModelTest::testDefaultViewProperties_data()
413 QTest::addColumn
<QUrl
>("url");
414 QTest::addColumn
<DolphinView::Mode
>("expectedViewMode");
415 QTest::addColumn
<bool>("expectedPreviewShow");
416 QTest::addColumn
<QList
<QByteArray
> >("expectedVisibleRole");
419 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
422 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "path"});
425 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "artist", "album"});
428 QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << DolphinView::DetailsView
<< true << QList
<QByteArray
>({"text", "modificationtime"});
431 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
435 void PlacesItemModelTest::testDefaultViewProperties()
438 QFETCH(DolphinView::Mode
, expectedViewMode
);
439 QFETCH(bool, expectedPreviewShow
);
440 QFETCH(QList
<QByteArray
>, expectedVisibleRole
);
442 ViewProperties
properties(KFilePlacesModel::convertedUrl(url
));
443 QCOMPARE(properties
.viewMode(), expectedViewMode
);
444 QCOMPARE(properties
.previewsShown(), expectedPreviewShow
);
445 QCOMPARE(properties
.visibleRoles(), expectedVisibleRole
);
448 void PlacesItemModelTest::testClear()
450 QCOMPARE(m_model
->count(), 17);
452 QCOMPARE(m_model
->count(), 0);
453 QCOMPARE(m_model
->hiddenCount(), 0);
455 QTRY_COMPARE(m_model
->count(), 17);
458 void PlacesItemModelTest::testHideItem()
460 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
461 const int index
= indexOf(mediaUrl
);
463 PlacesItem
*item
= m_model
->placesItem(index
);
465 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
466 QList
<QVariant
> spyItemsRemovedArgs
;
467 KItemRangeList removedRange
;
469 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
470 QList
<QVariant
> spyItemsInsertedArgs
;
471 KItemRangeList insertedRange
;
475 item
->setHidden(true);
477 // check if items removed was fired
478 QTRY_COMPARE(m_model
->count(), 16);
479 QCOMPARE(spyItemsRemoved
.count(), 1);
480 spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
481 removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
482 QCOMPARE(removedRange
.size(), 1);
483 QCOMPARE(removedRange
.first().index
, index
);
484 QCOMPARE(removedRange
.first().count
, 1);
486 // allow model to show hidden items
487 m_model
->setHiddenItemsShown(true);
489 // check if the items inserted was fired
490 spyItemsInsertedArgs
= spyItemsInserted
.takeFirst();
491 insertedRange
= spyItemsInsertedArgs
.at(0).value
<KItemRangeList
>();
492 QCOMPARE(insertedRange
.size(), 1);
493 QCOMPARE(insertedRange
.first().index
, index
);
494 QCOMPARE(insertedRange
.first().count
, 1);
496 // mark item as visible
497 item
= m_model
->placesItem(index
);
498 item
->setHidden(false);
500 // mark model to hide invisible items
501 m_model
->setHiddenItemsShown(true);
503 QTRY_COMPARE(m_model
->count(), 17);
506 void PlacesItemModelTest::testSystemItems()
508 QCOMPARE(m_model
->count(), 17);
509 for (int r
= 0; r
< m_model
->count(); r
++) {
510 QCOMPARE(m_model
->placesItem(r
)->isSystemItem(), !m_model
->placesItem(r
)->device().isValid());
513 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
515 // create a new entry (non system item)
516 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
518 // check if the new entry was created
519 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
521 // make sure the new place get removed
524 QList
<QVariant
> args
= itemsInsertedSpy
.takeFirst();
525 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
526 QCOMPARE(range
.first().index
, 3);
527 QCOMPARE(range
.first().count
, 1);
528 QVERIFY(!m_model
->placesItem(3)->isSystemItem());
529 QCOMPARE(m_model
->count(), 18);
532 // check if the removal signal is correct
533 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
534 m_model
->deleteItem(3);
535 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
536 args
= itemsRemovedSpy
.takeFirst();
537 range
= args
.at(0).value
<KItemRangeList
>();
538 QCOMPARE(range
.first().index
, 3);
539 QCOMPARE(range
.first().count
, 1);
540 QTRY_COMPARE(m_model
->count(), 17);
542 //cancel removal (it was removed above)
543 cancelPlaceRemoval(3);
546 void PlacesItemModelTest::testEditBookmark()
548 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
549 QScopedPointer
<PlacesItemModel
> other(new PlacesItemModel());
551 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
553 // make sure that the new item will be removed later
556 QSignalSpy
itemsChangedSply(m_model
, &PlacesItemModel::itemsChanged
);
559 m_model
->item(3)->setText(QStringLiteral("Renamed place"));
562 // check if the correct signal was fired
563 QTRY_COMPARE(itemsChangedSply
.count(), 1);
564 QList
<QVariant
> args
= itemsChangedSply
.takeFirst();
565 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
566 QCOMPARE(range
.first().index
, 3);
567 QCOMPARE(range
.first().count
, 1);
568 QSet
<QByteArray
> roles
= args
.at(1).value
<QSet
<QByteArray
> >();
569 QCOMPARE(roles
.size(), 1);
570 QCOMPARE(*roles
.begin(), QByteArrayLiteral("text"));
571 QCOMPARE(m_model
->item(3)->text(), QStringLiteral("Renamed place"));
573 // check if the item was updated in the other model
574 QTRY_COMPARE(other
->item(3)->text(), QStringLiteral("Renamed place"));
577 void PlacesItemModelTest::testEditAfterCreation()
579 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
580 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
582 // create a new place
583 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
584 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
586 PlacesItemModel
*model
= new PlacesItemModel();
587 QTRY_COMPARE(model
->count(), m_model
->count());
589 // make sure that the new item will be removed later
593 PlacesItem
*item
= m_model
->placesItem(3);
594 item
->setText(QStringLiteral("Renamed place"));
597 // check if the second model got the changes
598 QTRY_COMPARE(model
->count(), m_model
->count());
599 QTRY_COMPARE(model
->placesItem(3)->text(), m_model
->placesItem(3)->text());
600 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
601 m_model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
602 QTRY_COMPARE(model
->placesItem(3)->icon(), m_model
->placesItem(3)->icon());
603 QTRY_COMPARE(model
->placesItem(3)->url(), m_model
->placesItem(3)->url());
606 void PlacesItemModelTest::testEditMetadata()
608 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
609 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
611 // create a new place
612 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
613 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
615 // check if the new entry was created
616 PlacesItemModel
*model
= new PlacesItemModel();
617 QTRY_COMPARE(model
->count(), m_model
->count());
619 // make sure that the new item will be removed later
622 // modify place metadata
623 PlacesItem
*item
= m_model
->placesItem(3);
624 item
->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
627 // check if the place was modified in both models
628 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
629 KAboutData::applicationData().componentName());
630 QTRY_COMPARE(model
->placesItem(3)->text(), m_model
->placesItem(3)->text());
631 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
632 m_model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
633 QTRY_COMPARE(model
->placesItem(3)->icon(), m_model
->placesItem(3)->icon());
634 QTRY_COMPARE(model
->placesItem(3)->url(), m_model
->placesItem(3)->url());
637 void PlacesItemModelTest::testRefresh()
639 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
640 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
642 // create a new place
643 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
644 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
646 PlacesItemModel
*model
= new PlacesItemModel();
647 QTRY_COMPARE(model
->count(), m_model
->count());
649 // make sure that the new item will be removed later
652 PlacesItem
*item
= m_model
->placesItem(3);
653 PlacesItem
*sameItem
= model
->placesItem(3);
654 QCOMPARE(item
->text(), sameItem
->text());
657 item
->setText(QStringLiteral("Renamed place"));
659 // item from another model is not affected at the moment
660 QVERIFY(item
->text() != sameItem
->text());
665 // item must be equal
666 QTRY_COMPARE(item
->text(), sameItem
->text());
669 void PlacesItemModelTest::testIcons_data()
671 QTest::addColumn
<QUrl
>("url");
672 QTest::addColumn
<QString
>("expectedIconName");
675 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
678 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
681 QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << QStringLiteral("view-calendar-month");
684 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
687 void PlacesItemModelTest::testIcons()
690 QFETCH(QString
, expectedIconName
);
692 PlacesItem
*item
= m_model
->placesItem(indexOf(url
));
693 QCOMPARE(item
->icon(), expectedIconName
);
695 for (int r
= 0; r
< m_model
->count(); r
++) {
696 QVERIFY(!m_model
->placesItem(r
)->icon().isEmpty());
700 void PlacesItemModelTest::testDragAndDrop()
702 QList
<QVariant
> args
;
703 KItemRangeList range
;
704 QStringList urls
= initialUrls();
705 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
706 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
708 CHECK_PLACES_URLS(initialUrls());
709 // Move the KDE_ROOT_PATH at the end of the places list will case it to be moved to the end of the places group
710 QMimeData
*dropData
= createMimeData(QList
<int>() << 1);
711 m_model
->dropMimeDataBefore(m_model
->count() - 1, dropData
);
715 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
716 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
718 // remove item from actual position
719 args
= itemsRemovedSpy
.takeFirst();
720 range
= args
.at(0).value
<KItemRangeList
>();
721 QCOMPARE(range
.size(), 1);
722 QCOMPARE(range
.at(0).count
, 1);
723 QCOMPARE(range
.at(0).index
, 1);
725 // insert intem in his group
726 args
= itemsInsertedSpy
.takeFirst();
727 range
= args
.at(0).value
<KItemRangeList
>();
728 QCOMPARE(range
.size(), 1);
729 QCOMPARE(range
.at(0).count
, 1);
730 QCOMPARE(range
.at(0).index
, 2);
732 CHECK_PLACES_URLS(urls
);
734 itemsInsertedSpy
.clear();
735 itemsRemovedSpy
.clear();
737 // Move the KDE_ROOT_PATH to his original position
738 dropData
= createMimeData(QList
<int>() << 2);
739 m_model
->dropMimeDataBefore(1, dropData
);
743 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
744 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
746 // remove item from actual position
747 args
= itemsRemovedSpy
.takeFirst();
748 range
= args
.at(0).value
<KItemRangeList
>();
749 QCOMPARE(range
.size(), 1);
750 QCOMPARE(range
.at(0).count
, 1);
751 QCOMPARE(range
.at(0).index
, 2);
753 // insert intem in the requested position
754 args
= itemsInsertedSpy
.takeFirst();
755 range
= args
.at(0).value
<KItemRangeList
>();
756 QCOMPARE(range
.size(), 1);
757 QCOMPARE(range
.at(0).count
, 1);
758 QCOMPARE(range
.at(0).index
, 1);
760 CHECK_PLACES_URLS(urls
);
763 void PlacesItemModelTest::testHideDevices()
765 QSignalSpy
itemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
766 QStringList urls
= initialUrls();
768 m_model
->setGroupHidden(KFilePlacesModel::RemovableDevicesType
, true);
769 QTRY_VERIFY(m_model
->isGroupHidden(KFilePlacesModel::RemovableDevicesType
));
770 QTRY_COMPARE(itemsRemoved
.count(), 3);
772 // remove removable-devices
773 urls
.removeOne(QStringLiteral("/media/floppy0"));
774 urls
.removeOne(QStringLiteral("/media/XO-Y4"));
775 urls
.removeOne(QStringLiteral("/media/cdrom"));
777 // check if the correct urls was removed
778 CHECK_PLACES_URLS(urls
);
781 m_model
= new PlacesItemModel();
782 QTRY_COMPARE(m_model
->count(), urls
.count());
783 CHECK_PLACES_URLS(urls
);
786 m_model
->setGroupHidden(KFilePlacesModel::RemovableDevicesType
, false);
787 urls
= initialUrls();
788 QTRY_COMPARE(m_model
->count(), urls
.count());
789 CHECK_PLACES_URLS(urls
);
792 void PlacesItemModelTest::testDuplicatedEntries()
794 QStringList urls
= initialUrls();
795 // create a duplicated entry on bookmark
796 KBookmarkManager
*bookmarkManager
= KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
797 KBookmarkGroup root
= bookmarkManager
->root();
798 KBookmark bookmark
= root
.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {});
800 const QString id
= QUuid::createUuid().toString();
801 bookmark
.setMetaDataItem(QStringLiteral("ID"), id
);
802 bookmark
.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
803 bookmarkManager
->emitChanged(bookmarkManager
->root());
805 PlacesItemModel
*newModel
= new PlacesItemModel();
806 QTRY_COMPARE(placesUrls(newModel
).count(QStringLiteral("search:/videos")), 1);
807 QTRY_COMPARE(urls
, placesUrls(newModel
));
811 QTEST_MAIN(PlacesItemModelTest
)
813 #include "placesitemmodeltest.moc"