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 ***************************************************************************/
22 #include <QStandardPaths>
24 #include <QDBusInterface>
26 #include <KBookmarkManager>
28 #include <KConfigGroup>
30 #include <KFilePlacesModel>
32 #include "panels/places/placesitemmodel.h"
33 #include "panels/places/placesitem.h"
34 #include "views/viewproperties.h"
36 Q_DECLARE_METATYPE(KItemRangeList
)
37 Q_DECLARE_METATYPE(KItemRange
)
40 //c:\ as root for windows
41 #define KDE_ROOT_PATH "C:\\"
43 #define KDE_ROOT_PATH "/"
46 static QString
bookmarksFile()
48 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation
) + "/user-places.xbel";
51 class PlacesItemModelTest
: public QObject
60 void cleanupTestCase();
64 void testDeletePlace();
65 void testPlaceItem_data();
67 void testTearDownDevice();
68 void testDefaultViewProperties_data();
69 void testDefaultViewProperties();
72 void testSystemItems();
73 void testEditBookmark();
74 void testEditAfterCreation();
75 void testEditMetadata();
77 void testIcons_data();
79 void testDragAndDrop();
80 void testHideDevices();
81 void testDuplicatedEntries();
82 void renameAfterCreation();
85 PlacesItemModel
* m_model
;
86 QSet
<int> m_tobeRemoved
;
87 QMap
<QString
, QDBusInterface
*> m_interfacesMap
;
89 void setBalooEnabled(bool enabled
);
90 int indexOf(const QUrl
&url
);
91 QDBusInterface
*fakeManager();
92 QDBusInterface
*fakeDevice(const QString
&udi
);
93 QStringList
placesUrls(PlacesItemModel
*model
= nullptr) const;
94 QStringList
initialUrls() const;
95 void createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
);
96 void removePlaceAfter(int index
);
97 void cancelPlaceRemoval(int index
);
98 void removeTestUserData();
99 QMimeData
*createMimeData(const QList
<int> &indexes
) const;
102 #define CHECK_PLACES_URLS(urls) \
104 QStringList places = placesUrls(); \
105 if (places != urls) { \
106 qWarning() << "Expected:" << urls; \
107 qWarning() << "Got:" << places; \
108 QCOMPARE(places, urls); \
112 void PlacesItemModelTest::setBalooEnabled(bool enabled
)
114 KConfig
config(QStringLiteral("baloofilerc"));
115 KConfigGroup basicSettings
= config
.group("Basic Settings");
116 basicSettings
.writeEntry("Indexing-Enabled", enabled
);
120 int PlacesItemModelTest::indexOf(const QUrl
&url
)
122 for (int r
= 0; r
< m_model
->count(); r
++) {
123 if (m_model
->placesItem(r
)->url() == url
) {
130 QDBusInterface
*PlacesItemModelTest::fakeManager()
132 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
135 QDBusInterface
*PlacesItemModelTest::fakeDevice(const QString
&udi
)
137 if (m_interfacesMap
.contains(udi
)) {
138 return m_interfacesMap
[udi
];
141 QDBusInterface
*iface
= new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi
);
142 m_interfacesMap
[udi
] = iface
;
147 QStringList
PlacesItemModelTest::placesUrls(PlacesItemModel
*model
) const
154 for (int row
= 0; row
< model
->count(); ++row
) {
155 urls
<< model
->placesItem(row
)->url().toDisplayString(QUrl::PreferLocalFile
);
160 QStringList
PlacesItemModelTest::initialUrls() const
162 static QStringList urls
;
163 if (urls
.isEmpty()) {
164 urls
<< QDir::homePath()
165 << QDir::homePath() + QStringLiteral("/Desktop")
166 << QDir::homePath() + QStringLiteral("/Downloads")
167 << QStringLiteral(KDE_ROOT_PATH
) << QStringLiteral("trash:/")
168 << QStringLiteral("remote:/")
169 << QStringLiteral("/media/nfs")
170 << QStringLiteral("timeline:/today") << QStringLiteral("timeline:/yesterday")
171 << QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
172 << 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 const auto tobeRemoved
= m_tobeRemoved
;
236 for (const int i
: tobeRemoved
) {
237 int before
= m_model
->count();
238 m_model
->deleteItem(i
);
239 QTRY_COMPARE(m_model
->count(), before
- 1);
241 m_tobeRemoved
.clear();
244 removeTestUserData();
247 void PlacesItemModelTest::initTestCase()
249 QStandardPaths::setTestModeEnabled(true);
250 // remove test user data
251 removeTestUserData();
253 const QString fakeHw
= QFINDTESTDATA("data/fakecomputer.xml");
254 QVERIFY(!fakeHw
.isEmpty());
255 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw
));
257 setBalooEnabled(true);
258 const QString bookmarsFileName
= bookmarksFile();
259 if (QFileInfo::exists(bookmarsFileName
)) {
260 // Ensure we'll have a clean bookmark file to start
261 QVERIFY(QFile::remove(bookmarsFileName
));
264 qRegisterMetaType
<KItemRangeList
>();
265 qRegisterMetaType
<KItemRange
>();
268 void PlacesItemModelTest::cleanupTestCase()
270 qDeleteAll(m_interfacesMap
);
271 QFile::remove(bookmarksFile());
273 // Remove any previous properties file
274 removeTestUserData();
277 void PlacesItemModelTest::testModelSort()
279 CHECK_PLACES_URLS(initialUrls());
282 void PlacesItemModelTest::testGroups()
284 const auto groups
= m_model
->groups();
287 QCOMPARE(groups
.size(), 6);
289 QCOMPARE(groups
.at(0).first
, 0);
290 QCOMPARE(groups
.at(0).second
.toString(), QStringLiteral("Places"));
292 QCOMPARE(groups
.at(1).first
, 5);
293 QCOMPARE(groups
.at(1).second
.toString(), QStringLiteral("Remote"));
295 QCOMPARE(groups
.at(2).first
, 7);
296 QCOMPARE(groups
.at(2).second
.toString(), QStringLiteral("Recently Saved"));
298 QCOMPARE(groups
.at(3).first
, 9);
299 QCOMPARE(groups
.at(3).second
.toString(), QStringLiteral("Search For"));
301 QCOMPARE(groups
.at(4).first
, 13);
302 QCOMPARE(groups
.at(4).second
.toString(), QStringLiteral("Devices"));
304 QCOMPARE(groups
.at(5).first
, 14);
305 QCOMPARE(groups
.at(5).second
.toString(), QStringLiteral("Removable Devices"));
308 void PlacesItemModelTest::testPlaceItem_data()
310 QTest::addColumn
<QUrl
>("url");
311 QTest::addColumn
<bool>("expectedIsHidden");
312 QTest::addColumn
<bool>("expectedIsSystemItem");
313 QTest::addColumn
<QString
>("expectedGroup");
314 QTest::addColumn
<bool>("expectedStorageSetupNeeded");
317 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
320 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
323 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << false << true << QStringLiteral("Recently Saved") << false;
326 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
329 void PlacesItemModelTest::testPlaceItem()
332 QFETCH(bool, expectedIsHidden
);
333 QFETCH(bool, expectedIsSystemItem
);
334 QFETCH(QString
, expectedGroup
);
335 QFETCH(bool, expectedStorageSetupNeeded
);
337 const int index
= indexOf(url
);
338 PlacesItem
*item
= m_model
->placesItem(index
);
339 QCOMPARE(item
->url(), url
);
340 QCOMPARE(item
->isHidden(), expectedIsHidden
);
341 QCOMPARE(item
->isSystemItem(), expectedIsSystemItem
);
342 QCOMPARE(item
->group(), expectedGroup
);
343 QCOMPARE(item
->storageSetupNeeded(), expectedStorageSetupNeeded
);
346 void PlacesItemModelTest::testDeletePlace()
348 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
349 QStringList urls
= initialUrls();
350 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
351 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
353 PlacesItemModel
*model
= new PlacesItemModel();
355 // create a new place
356 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
357 urls
.insert(5, tempUrl
.toLocalFile());
359 // check if the new entry was created
360 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
361 CHECK_PLACES_URLS(urls
);
362 QTRY_COMPARE(model
->count(), m_model
->count());
365 m_model
->deleteItem(5);
367 // make sure that the new item is removed
368 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
369 QTRY_COMPARE(m_model
->count(), 17);
370 CHECK_PLACES_URLS(initialUrls());
371 QTRY_COMPARE(model
->count(), m_model
->count());
374 void PlacesItemModelTest::testTearDownDevice()
376 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
377 int index
= indexOf(mediaUrl
);
378 QVERIFY(index
!= -1);
380 auto ejectAction
= m_model
->ejectAction(index
);
381 QVERIFY(!ejectAction
);
383 auto teardownAction
= m_model
->teardownAction(index
);
384 QVERIFY(teardownAction
);
386 QCOMPARE(m_model
->count(), 17);
388 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
389 fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
390 QTRY_COMPARE(m_model
->count(), 16);
391 QCOMPARE(spyItemsRemoved
.count(), 1);
392 const QList
<QVariant
> spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
393 const KItemRangeList removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
394 QCOMPARE(removedRange
.size(), 1);
395 QCOMPARE(removedRange
.first().index
, index
);
396 QCOMPARE(removedRange
.first().count
, 1);
398 QCOMPARE(indexOf(mediaUrl
), -1);
400 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
401 fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
402 QTRY_COMPARE(m_model
->count(), 17);
403 QCOMPARE(spyItemsInserted
.count(), 1);
404 index
= indexOf(mediaUrl
);
406 const QList
<QVariant
> args
= spyItemsInserted
.takeFirst();
407 const KItemRangeList insertedRange
= args
.at(0).value
<KItemRangeList
>();
408 QCOMPARE(insertedRange
.size(), 1);
409 QCOMPARE(insertedRange
.first().index
, index
);
410 QCOMPARE(insertedRange
.first().count
, 1);
413 void PlacesItemModelTest::testDefaultViewProperties_data()
415 QTest::addColumn
<QUrl
>("url");
416 QTest::addColumn
<DolphinView::Mode
>("expectedViewMode");
417 QTest::addColumn
<bool>("expectedPreviewShow");
418 QTest::addColumn
<QList
<QByteArray
> >("expectedVisibleRole");
421 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
424 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "path"});
427 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "artist", "album"});
430 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << DolphinView::DetailsView
<< true << QList
<QByteArray
>({"text", "modificationtime"});
433 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
437 void PlacesItemModelTest::testDefaultViewProperties()
440 QFETCH(DolphinView::Mode
, expectedViewMode
);
441 QFETCH(bool, expectedPreviewShow
);
442 QFETCH(QList
<QByteArray
>, expectedVisibleRole
);
444 ViewProperties
properties(KFilePlacesModel::convertedUrl(url
));
445 QCOMPARE(properties
.viewMode(), expectedViewMode
);
446 QCOMPARE(properties
.previewsShown(), expectedPreviewShow
);
447 QCOMPARE(properties
.visibleRoles(), expectedVisibleRole
);
450 void PlacesItemModelTest::testClear()
452 QCOMPARE(m_model
->count(), 17);
454 QCOMPARE(m_model
->count(), 0);
455 QCOMPARE(m_model
->hiddenCount(), 0);
457 QTRY_COMPARE(m_model
->count(), 17);
460 void PlacesItemModelTest::testHideItem()
462 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
463 const int index
= indexOf(mediaUrl
);
465 PlacesItem
*item
= m_model
->placesItem(index
);
467 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
468 QList
<QVariant
> spyItemsRemovedArgs
;
469 KItemRangeList removedRange
;
471 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
472 QList
<QVariant
> spyItemsInsertedArgs
;
473 KItemRangeList insertedRange
;
477 item
->setHidden(true);
479 // check if items removed was fired
480 QTRY_COMPARE(m_model
->count(), 16);
481 QCOMPARE(spyItemsRemoved
.count(), 1);
482 spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
483 removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
484 QCOMPARE(removedRange
.size(), 1);
485 QCOMPARE(removedRange
.first().index
, index
);
486 QCOMPARE(removedRange
.first().count
, 1);
488 // allow model to show hidden items
489 m_model
->setHiddenItemsShown(true);
491 // check if the items inserted was fired
492 spyItemsInsertedArgs
= spyItemsInserted
.takeFirst();
493 insertedRange
= spyItemsInsertedArgs
.at(0).value
<KItemRangeList
>();
494 QCOMPARE(insertedRange
.size(), 1);
495 QCOMPARE(insertedRange
.first().index
, index
);
496 QCOMPARE(insertedRange
.first().count
, 1);
498 // mark item as visible
499 item
= m_model
->placesItem(index
);
500 item
->setHidden(false);
502 // mark model to hide invisible items
503 m_model
->setHiddenItemsShown(true);
505 QTRY_COMPARE(m_model
->count(), 17);
508 void PlacesItemModelTest::testSystemItems()
510 QCOMPARE(m_model
->count(), 17);
511 for (int r
= 0; r
< m_model
->count(); r
++) {
512 QCOMPARE(m_model
->placesItem(r
)->isSystemItem(), !m_model
->placesItem(r
)->device().isValid());
515 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
517 // create a new entry (non system item)
518 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
520 // check if the new entry was created
521 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
523 // make sure the new place get removed
526 QList
<QVariant
> args
= itemsInsertedSpy
.takeFirst();
527 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
528 QCOMPARE(range
.first().index
, 5);
529 QCOMPARE(range
.first().count
, 1);
530 QVERIFY(!m_model
->placesItem(5)->isSystemItem());
531 QCOMPARE(m_model
->count(), 18);
534 // check if the removal signal is correct
535 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
536 m_model
->deleteItem(5);
537 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
538 args
= itemsRemovedSpy
.takeFirst();
539 range
= args
.at(0).value
<KItemRangeList
>();
540 QCOMPARE(range
.first().index
, 5);
541 QCOMPARE(range
.first().count
, 1);
542 QTRY_COMPARE(m_model
->count(), 17);
544 //cancel removal (it was removed above)
545 cancelPlaceRemoval(5);
548 void PlacesItemModelTest::testEditBookmark()
550 QScopedPointer
<PlacesItemModel
> other(new PlacesItemModel());
552 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
554 // make sure that the new item will be removed later
557 QSignalSpy
itemsChangedSply(m_model
, &PlacesItemModel::itemsChanged
);
560 m_model
->item(3)->setText(QStringLiteral("Renamed place"));
563 // check if the correct signal was fired
564 QTRY_COMPARE(itemsChangedSply
.count(), 1);
565 QList
<QVariant
> args
= itemsChangedSply
.takeFirst();
566 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
567 QCOMPARE(range
.first().index
, 3);
568 QCOMPARE(range
.first().count
, 1);
569 QSet
<QByteArray
> roles
= args
.at(1).value
<QSet
<QByteArray
> >();
570 QCOMPARE(roles
.size(), 1);
571 QCOMPARE(*roles
.begin(), QByteArrayLiteral("text"));
572 QCOMPARE(m_model
->item(3)->text(), QStringLiteral("Renamed place"));
574 // check if the item was updated in the other model
575 QTRY_COMPARE(other
->item(3)->text(), QStringLiteral("Renamed place"));
578 void PlacesItemModelTest::testEditAfterCreation()
580 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
581 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
583 // create a new place
584 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
585 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
587 PlacesItemModel
*model
= new PlacesItemModel();
588 QTRY_COMPARE(model
->count(), m_model
->count());
590 // make sure that the new item will be removed later
594 PlacesItem
*item
= m_model
->placesItem(3);
595 item
->setText(QStringLiteral("Renamed place"));
598 // check if the second model got the changes
599 QTRY_COMPARE(model
->count(), m_model
->count());
600 QTRY_COMPARE(model
->placesItem(3)->text(), m_model
->placesItem(3)->text());
601 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
602 m_model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
603 QTRY_COMPARE(model
->placesItem(3)->icon(), m_model
->placesItem(3)->icon());
604 QTRY_COMPARE(model
->placesItem(3)->url(), m_model
->placesItem(3)->url());
607 void PlacesItemModelTest::testEditMetadata()
609 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
610 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
612 // create a new place
613 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
614 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
616 // check if the new entry was created
617 PlacesItemModel
*model
= new PlacesItemModel();
618 QTRY_COMPARE(model
->count(), m_model
->count());
620 // make sure that the new item will be removed later
623 // modify place metadata
624 PlacesItem
*item
= m_model
->placesItem(3);
625 item
->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
628 // check if the place was modified in both models
629 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
630 KAboutData::applicationData().componentName());
631 QTRY_COMPARE(model
->placesItem(3)->text(), m_model
->placesItem(3)->text());
632 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
633 m_model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
634 QTRY_COMPARE(model
->placesItem(3)->icon(), m_model
->placesItem(3)->icon());
635 QTRY_COMPARE(model
->placesItem(3)->url(), m_model
->placesItem(3)->url());
638 void PlacesItemModelTest::testRefresh()
640 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
641 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
643 // create a new place
644 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
645 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
647 PlacesItemModel
*model
= new PlacesItemModel();
648 QTRY_COMPARE(model
->count(), m_model
->count());
650 // make sure that the new item will be removed later
653 PlacesItem
*item
= m_model
->placesItem(5);
654 PlacesItem
*sameItem
= model
->placesItem(5);
655 QCOMPARE(item
->text(), sameItem
->text());
658 item
->setText(QStringLiteral("Renamed place"));
660 // item from another model is not affected at the moment
661 QVERIFY(item
->text() != sameItem
->text());
666 // item must be equal
667 QTRY_COMPARE(item
->text(), sameItem
->text());
670 void PlacesItemModelTest::testIcons_data()
672 QTest::addColumn
<QUrl
>("url");
673 QTest::addColumn
<QString
>("expectedIconName");
676 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
679 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
682 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << QStringLiteral("go-jump-today");
685 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
688 void PlacesItemModelTest::testIcons()
691 QFETCH(QString
, expectedIconName
);
693 PlacesItem
*item
= m_model
->placesItem(indexOf(url
));
694 QCOMPARE(item
->icon(), expectedIconName
);
696 for (int r
= 0; r
< m_model
->count(); r
++) {
697 QVERIFY(!m_model
->placesItem(r
)->icon().isEmpty());
701 void PlacesItemModelTest::testDragAndDrop()
703 QList
<QVariant
> args
;
704 KItemRangeList range
;
705 QStringList urls
= initialUrls();
706 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
707 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
709 CHECK_PLACES_URLS(initialUrls());
710 // Move the home directory to the end of the places group
711 QMimeData
*dropData
= createMimeData(QList
<int>() << 0);
712 m_model
->dropMimeDataBefore(m_model
->count() - 1, dropData
);
716 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
717 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
719 // remove item from actual position
720 args
= itemsRemovedSpy
.takeFirst();
721 range
= args
.at(0).value
<KItemRangeList
>();
722 QCOMPARE(range
.size(), 1);
723 QCOMPARE(range
.at(0).count
, 1);
724 QCOMPARE(range
.at(0).index
, 0);
726 // insert intem in his group
727 args
= itemsInsertedSpy
.takeFirst();
728 range
= args
.at(0).value
<KItemRangeList
>();
729 QCOMPARE(range
.size(), 1);
730 QCOMPARE(range
.at(0).count
, 1);
731 QCOMPARE(range
.at(0).index
, 4);
733 CHECK_PLACES_URLS(urls
);
735 itemsInsertedSpy
.clear();
736 itemsRemovedSpy
.clear();
738 // Move home directory item back to its original position
739 dropData
= createMimeData(QList
<int>() << 4);
740 m_model
->dropMimeDataBefore(0, dropData
);
744 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
745 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
747 // remove item from actual position
748 args
= itemsRemovedSpy
.takeFirst();
749 range
= args
.at(0).value
<KItemRangeList
>();
750 QCOMPARE(range
.size(), 1);
751 QCOMPARE(range
.at(0).count
, 1);
752 QCOMPARE(range
.at(0).index
, 4);
754 // insert intem in the requested position
755 args
= itemsInsertedSpy
.takeFirst();
756 range
= args
.at(0).value
<KItemRangeList
>();
757 QCOMPARE(range
.size(), 1);
758 QCOMPARE(range
.at(0).count
, 1);
759 QCOMPARE(range
.at(0).index
, 0);
761 CHECK_PLACES_URLS(urls
);
764 void PlacesItemModelTest::testHideDevices()
766 QSignalSpy
itemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
767 QStringList urls
= initialUrls();
769 m_model
->setGroupHidden(KFilePlacesModel::RemovableDevicesType
, true);
770 QTRY_VERIFY(m_model
->isGroupHidden(KFilePlacesModel::RemovableDevicesType
));
771 QTRY_COMPARE(itemsRemoved
.count(), 3);
773 // remove removable-devices
774 urls
.removeOne(QStringLiteral("/media/floppy0"));
775 urls
.removeOne(QStringLiteral("/media/XO-Y4"));
776 urls
.removeOne(QStringLiteral("/media/cdrom"));
778 // check if the correct urls was removed
779 CHECK_PLACES_URLS(urls
);
782 m_model
= new PlacesItemModel();
783 QTRY_COMPARE(m_model
->count(), urls
.count());
784 CHECK_PLACES_URLS(urls
);
787 m_model
->setGroupHidden(KFilePlacesModel::RemovableDevicesType
, false);
788 urls
= initialUrls();
789 QTRY_COMPARE(m_model
->count(), urls
.count());
790 CHECK_PLACES_URLS(urls
);
793 void PlacesItemModelTest::testDuplicatedEntries()
795 QStringList urls
= initialUrls();
796 // create a duplicated entry on bookmark
797 KBookmarkManager
*bookmarkManager
= KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
798 KBookmarkGroup root
= bookmarkManager
->root();
799 KBookmark bookmark
= root
.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {});
801 const QString id
= QUuid::createUuid().toString();
802 bookmark
.setMetaDataItem(QStringLiteral("ID"), id
);
803 bookmark
.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
804 bookmarkManager
->emitChanged(bookmarkManager
->root());
806 PlacesItemModel
*newModel
= new PlacesItemModel();
807 QTRY_COMPARE(placesUrls(newModel
).count(QStringLiteral("search:/videos")), 1);
808 QTRY_COMPARE(urls
, placesUrls(newModel
));
812 void PlacesItemModelTest::renameAfterCreation()
814 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
815 QStringList urls
= initialUrls();
816 PlacesItemModel
*model
= new PlacesItemModel();
818 CHECK_PLACES_URLS(urls
);
819 QTRY_COMPARE(model
->count(), m_model
->count());
821 // create a new place
822 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
823 urls
.insert(5, tempUrl
.toLocalFile());
825 // make sure that the new item will be removed later
828 CHECK_PLACES_URLS(urls
);
829 QCOMPARE(model
->count(), m_model
->count());
833 QSignalSpy
changedSpy(m_model
, &PlacesItemModel::itemsChanged
);
835 PlacesItem
*item
= m_model
->placesItem(3);
836 item
->setText(QStringLiteral("New Temporary Dir"));
837 item
->setUrl(item
->url());
838 item
->setIcon(item
->icon());
841 QTRY_COMPARE(changedSpy
.count(), 1);
843 // check if the place was modified in both models
844 QTRY_COMPARE(m_model
->placesItem(3)->text(), QStringLiteral("New Temporary Dir"));
845 QTRY_COMPARE(model
->placesItem(3)->text(), QStringLiteral("New Temporary Dir"));
848 QTEST_MAIN(PlacesItemModelTest
)
850 #include "placesitemmodeltest.moc"