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();
87 PlacesItemModel
* m_model
;
88 QSet
<int> m_tobeRemoved
;
89 QMap
<QString
, QDBusInterface
*> m_interfacesMap
;
91 void setBalooEnabled(bool enabled
);
92 int indexOf(const QUrl
&url
);
93 QDBusInterface
*fakeManager();
94 QDBusInterface
*fakeDevice(const QString
&udi
);
95 QStringList
placesUrls(PlacesItemModel
*model
= nullptr) const;
96 QStringList
initialUrls() const;
97 void createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
);
98 void removePlaceAfter(int index
);
99 void cancelPlaceRemoval(int index
);
100 void removeTestUserData();
101 QMimeData
*createMimeData(const QList
<int> &indexes
) const;
104 #define CHECK_PLACES_URLS(urls) \
106 QStringList places = placesUrls(); \
107 if (places != urls) { \
108 qWarning() << "Expected:" << urls; \
109 qWarning() << "Got:" << places; \
110 QCOMPARE(places, urls); \
114 void PlacesItemModelTest::setBalooEnabled(bool enabled
)
116 KConfig
config(QStringLiteral("baloofilerc"));
117 KConfigGroup basicSettings
= config
.group("Basic Settings");
118 basicSettings
.writeEntry("Indexing-Enabled", enabled
);
122 int PlacesItemModelTest::indexOf(const QUrl
&url
)
124 for (int r
= 0; r
< m_model
->count(); r
++) {
125 if (m_model
->placesItem(r
)->url() == url
) {
132 QDBusInterface
*PlacesItemModelTest::fakeManager()
134 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
137 QDBusInterface
*PlacesItemModelTest::fakeDevice(const QString
&udi
)
139 if (m_interfacesMap
.contains(udi
)) {
140 return m_interfacesMap
[udi
];
143 QDBusInterface
*iface
= new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi
);
144 m_interfacesMap
[udi
] = iface
;
149 QStringList
PlacesItemModelTest::placesUrls(PlacesItemModel
*model
) const
156 for (int row
= 0; row
< model
->count(); ++row
) {
157 urls
<< model
->placesItem(row
)->url().toDisplayString(QUrl::PreferLocalFile
);
162 QStringList
PlacesItemModelTest::initialUrls() const
164 static QStringList urls
;
165 if (urls
.isEmpty()) {
166 urls
<< QDir::homePath() << QStringLiteral(KDE_ROOT_PATH
) << QStringLiteral("trash:/")
167 << QStringLiteral("remote:/")
168 << QStringLiteral("timeline:/today") << QStringLiteral("timeline:/yesterday") << QStringLiteral("timeline:/thismonth") << QStringLiteral("timeline:/lastmonth")
169 << QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
170 << QStringLiteral("/media/nfs") << QStringLiteral("/foreign")
171 << QStringLiteral("/media/floppy0") << QStringLiteral("/media/XO-Y4") << QStringLiteral("/media/cdrom");
176 void PlacesItemModelTest::createPlaceItem(const QString
&text
, const QUrl
&url
, const QString
&icon
)
178 m_model
->createPlacesItem(text
, url
, icon
);
181 void PlacesItemModelTest::removePlaceAfter(int index
)
183 m_tobeRemoved
.insert(index
);
186 void PlacesItemModelTest::cancelPlaceRemoval(int index
)
188 m_tobeRemoved
.remove(index
);
191 void PlacesItemModelTest::removeTestUserData()
193 // user hardcoded path to avoid removal of any user personal data
194 QDir
dir(QStringLiteral("/home/renato/.qttest/share/placesitemmodeltest"));
196 QVERIFY(dir
.removeRecursively());
200 QMimeData
*PlacesItemModelTest::createMimeData(const QList
<int> &indexes
) const
203 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
206 for (int index
: indexes
) {
207 const QUrl itemUrl
= m_model
->placesItem(index
)->url();
208 if (itemUrl
.isValid()) {
214 QMimeData
* mimeData
= new QMimeData();
215 mimeData
->setUrls(urls
);
216 // copied from PlacesItemModel::internalMimeType()
217 const QString internalMimeType
= "application/x-dolphinplacesmodel-" +
218 QString::number((qptrdiff
)m_model
);
219 mimeData
->setData(internalMimeType
, itemData
);
223 void PlacesItemModelTest::init()
225 m_model
= new PlacesItemModel();
226 // WORKAROUND: need to wait for bookmark to load, check: PlacesItemModel::updateBookmarks
228 QCOMPARE(m_model
->count(), 17);
231 void PlacesItemModelTest::cleanup()
233 for (int i
: m_tobeRemoved
) {
234 int before
= m_model
->count();
235 m_model
->deleteItem(i
);
236 QTRY_COMPARE(m_model
->count(), before
- 1);
238 m_tobeRemoved
.clear();
241 removeTestUserData();
244 void PlacesItemModelTest::initTestCase()
246 QStandardPaths::setTestModeEnabled(true);
247 // remove test user data
248 removeTestUserData();
250 const QString fakeHw
= QFINDTESTDATA("data/fakecomputer.xml");
251 QVERIFY(!fakeHw
.isEmpty());
252 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw
));
254 setBalooEnabled(true);
255 const QString bookmarsFileName
= bookmarksFile();
256 if (QFileInfo::exists(bookmarsFileName
)) {
257 // Ensure we'll have a clean bookmark file to start
258 QVERIFY(QFile::remove(bookmarsFileName
));
261 qRegisterMetaType
<KItemRangeList
>();
262 qRegisterMetaType
<KItemRange
>();
265 void PlacesItemModelTest::cleanupTestCase()
267 qDeleteAll(m_interfacesMap
);
268 QFile::remove(bookmarksFile());
270 // Remove any previous properties file
271 removeTestUserData();
274 void PlacesItemModelTest::testModelSort()
276 CHECK_PLACES_URLS(initialUrls());
279 void PlacesItemModelTest::testGroups()
281 const auto groups
= m_model
->groups();
283 QCOMPARE(groups
.size(), 6);
285 QCOMPARE(groups
.at(0).first
, 0);
286 QCOMPARE(groups
.at(0).second
.toString(), QStringLiteral("Places"));
288 QCOMPARE(groups
.at(1).first
, 3);
289 QCOMPARE(groups
.at(1).second
.toString(), QStringLiteral("Remote"));
291 QCOMPARE(groups
.at(2).first
, 4);
292 QCOMPARE(groups
.at(2).second
.toString(), QStringLiteral("Recently Saved"));
294 QCOMPARE(groups
.at(3).first
, 8);
295 QCOMPARE(groups
.at(3).second
.toString(), QStringLiteral("Search For"));
297 QCOMPARE(groups
.at(4).first
, 12);
298 QCOMPARE(groups
.at(4).second
.toString(), QStringLiteral("Devices"));
300 QCOMPARE(groups
.at(5).first
, 14);
301 QCOMPARE(groups
.at(5).second
.toString(), QStringLiteral("Removable Devices"));
304 void PlacesItemModelTest::testPlaceItem_data()
306 QTest::addColumn
<QUrl
>("url");
307 QTest::addColumn
<bool>("expectedIsHidden");
308 QTest::addColumn
<bool>("expectedIsSystemItem");
309 QTest::addColumn
<QString
>("expectedGroup");
310 QTest::addColumn
<bool>("expectedStorageSetupNeeded");
313 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
316 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
319 QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << false << true << QStringLiteral("Recently Saved") << false;
322 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
325 void PlacesItemModelTest::testPlaceItem()
328 QFETCH(bool, expectedIsHidden
);
329 QFETCH(bool, expectedIsSystemItem
);
330 QFETCH(QString
, expectedGroup
);
331 QFETCH(bool, expectedStorageSetupNeeded
);
333 const int index
= indexOf(url
);
334 PlacesItem
*item
= m_model
->placesItem(index
);
335 QCOMPARE(item
->url(), url
);
336 QCOMPARE(item
->isHidden(), expectedIsHidden
);
337 QCOMPARE(item
->isSystemItem(), expectedIsSystemItem
);
338 QCOMPARE(item
->group(), expectedGroup
);
339 QCOMPARE(item
->storageSetupNeeded(), expectedStorageSetupNeeded
);
342 void PlacesItemModelTest::testDeletePlace()
344 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
345 QStringList urls
= initialUrls();
346 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
347 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
349 PlacesItemModel
*model
= new PlacesItemModel();
351 // create a new place
352 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
353 urls
.insert(3, tempUrl
.toLocalFile());
355 // check if the new entry was created
356 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
357 CHECK_PLACES_URLS(urls
);
358 QTRY_COMPARE(model
->count(), m_model
->count());
361 m_model
->deleteItem(3);
363 // make sure that the new item is removed
364 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
365 QTRY_COMPARE(m_model
->count(), 17);
366 CHECK_PLACES_URLS(initialUrls());
367 QTRY_COMPARE(model
->count(), m_model
->count());
370 void PlacesItemModelTest::testTearDownDevice()
372 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
373 int index
= indexOf(mediaUrl
);
374 QVERIFY(index
!= -1);
376 auto ejectAction
= m_model
->ejectAction(index
);
377 QVERIFY(!ejectAction
);
379 auto teardownAction
= m_model
->teardownAction(index
);
380 QVERIFY(teardownAction
);
382 QCOMPARE(m_model
->count(), 17);
384 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
385 fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
386 QTRY_COMPARE(m_model
->count(), 16);
387 QCOMPARE(spyItemsRemoved
.count(), 1);
388 const QList
<QVariant
> spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
389 const KItemRangeList removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
390 QCOMPARE(removedRange
.size(), 1);
391 QCOMPARE(removedRange
.first().index
, index
);
392 QCOMPARE(removedRange
.first().count
, 1);
394 QCOMPARE(indexOf(mediaUrl
), -1);
396 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
397 fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
398 QTRY_COMPARE(m_model
->count(), 17);
399 QCOMPARE(spyItemsInserted
.count(), 1);
400 index
= indexOf(mediaUrl
);
402 const QList
<QVariant
> args
= spyItemsInserted
.takeFirst();
403 const KItemRangeList insertedRange
= args
.at(0).value
<KItemRangeList
>();
404 QCOMPARE(insertedRange
.size(), 1);
405 QCOMPARE(insertedRange
.first().index
, index
);
406 QCOMPARE(insertedRange
.first().count
, 1);
409 void PlacesItemModelTest::testDefaultViewProperties_data()
411 QTest::addColumn
<QUrl
>("url");
412 QTest::addColumn
<DolphinView::Mode
>("expectedViewMode");
413 QTest::addColumn
<bool>("expectedPreviewShow");
414 QTest::addColumn
<QList
<QByteArray
> >("expectedVisibleRole");
417 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
420 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "path"});
423 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView
<< false << QList
<QByteArray
>({"text", "artist", "album"});
426 QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << DolphinView::DetailsView
<< true << QList
<QByteArray
>({"text", "modificationtime"});
429 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView
<< true << QList
<QByteArray
>({"text"});
433 void PlacesItemModelTest::testDefaultViewProperties()
436 QFETCH(DolphinView::Mode
, expectedViewMode
);
437 QFETCH(bool, expectedPreviewShow
);
438 QFETCH(QList
<QByteArray
>, expectedVisibleRole
);
440 ViewProperties
properties(KFilePlacesModel::convertedUrl(url
));
441 QCOMPARE(properties
.viewMode(), expectedViewMode
);
442 QCOMPARE(properties
.previewsShown(), expectedPreviewShow
);
443 QCOMPARE(properties
.visibleRoles(), expectedVisibleRole
);
446 void PlacesItemModelTest::testClear()
448 QCOMPARE(m_model
->count(), 17);
450 QCOMPARE(m_model
->count(), 0);
451 QCOMPARE(m_model
->hiddenCount(), 0);
453 QTRY_COMPARE(m_model
->count(), 17);
456 void PlacesItemModelTest::testHideItem()
458 const QUrl mediaUrl
= QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
459 const int index
= indexOf(mediaUrl
);
461 PlacesItem
*item
= m_model
->placesItem(index
);
463 QSignalSpy
spyItemsRemoved(m_model
, &PlacesItemModel::itemsRemoved
);
464 QList
<QVariant
> spyItemsRemovedArgs
;
465 KItemRangeList removedRange
;
467 QSignalSpy
spyItemsInserted(m_model
, &PlacesItemModel::itemsInserted
);
468 QList
<QVariant
> spyItemsInsertedArgs
;
469 KItemRangeList insertedRange
;
473 item
->setHidden(true);
475 // check if items removed was fired
476 QTRY_COMPARE(m_model
->count(), 16);
477 QCOMPARE(spyItemsRemoved
.count(), 1);
478 spyItemsRemovedArgs
= spyItemsRemoved
.takeFirst();
479 removedRange
= spyItemsRemovedArgs
.at(0).value
<KItemRangeList
>();
480 QCOMPARE(removedRange
.size(), 1);
481 QCOMPARE(removedRange
.first().index
, index
);
482 QCOMPARE(removedRange
.first().count
, 1);
484 // allow model to show hidden items
485 m_model
->setHiddenItemsShown(true);
487 // check if the items inserted was fired
488 spyItemsInsertedArgs
= spyItemsInserted
.takeFirst();
489 insertedRange
= spyItemsInsertedArgs
.at(0).value
<KItemRangeList
>();
490 QCOMPARE(insertedRange
.size(), 1);
491 QCOMPARE(insertedRange
.first().index
, index
);
492 QCOMPARE(insertedRange
.first().count
, 1);
494 // mark item as visible
495 item
= m_model
->placesItem(index
);
496 item
->setHidden(false);
498 // mark model to hide invisible items
499 m_model
->setHiddenItemsShown(true);
501 QTRY_COMPARE(m_model
->count(), 17);
504 void PlacesItemModelTest::testSystemItems()
506 QCOMPARE(m_model
->count(), 17);
507 for (int r
= 0; r
< m_model
->count(); r
++) {
508 QCOMPARE(m_model
->placesItem(r
)->isSystemItem(), !m_model
->placesItem(r
)->device().isValid());
511 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
513 // create a new entry (non system item)
514 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
516 // check if the new entry was created
517 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
519 // make sure the new place get removed
522 QList
<QVariant
> args
= itemsInsertedSpy
.takeFirst();
523 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
524 QCOMPARE(range
.first().index
, 3);
525 QCOMPARE(range
.first().count
, 1);
526 QVERIFY(!m_model
->placesItem(3)->isSystemItem());
527 QCOMPARE(m_model
->count(), 18);
530 // check if the removal signal is correct
531 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
532 m_model
->deleteItem(3);
533 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
534 args
= itemsRemovedSpy
.takeFirst();
535 range
= args
.at(0).value
<KItemRangeList
>();
536 QCOMPARE(range
.first().index
, 3);
537 QCOMPARE(range
.first().count
, 1);
538 QTRY_COMPARE(m_model
->count(), 17);
540 //cancel removal (it was removed above)
541 cancelPlaceRemoval(3);
544 void PlacesItemModelTest::testEditBookmark()
546 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
547 QScopedPointer
<PlacesItemModel
> other(new PlacesItemModel());
549 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
)), QString());
551 // make sure that the new item will be removed later
554 QSignalSpy
itemsChangedSply(m_model
, &PlacesItemModel::itemsChanged
);
557 m_model
->item(3)->setText(QStringLiteral("Renamed place"));
560 // check if the correct signal was fired
561 QTRY_COMPARE(itemsChangedSply
.count(), 1);
562 QList
<QVariant
> args
= itemsChangedSply
.takeFirst();
563 KItemRangeList range
= args
.at(0).value
<KItemRangeList
>();
564 QCOMPARE(range
.first().index
, 3);
565 QCOMPARE(range
.first().count
, 1);
566 QSet
<QByteArray
> roles
= args
.at(1).value
<QSet
<QByteArray
> >();
567 QCOMPARE(roles
.size(), 1);
568 QCOMPARE(*roles
.begin(), QByteArrayLiteral("text"));
569 QCOMPARE(m_model
->item(3)->text(), QStringLiteral("Renamed place"));
571 // check if the item was updated in the other model
572 QTRY_COMPARE(other
->item(3)->text(), QStringLiteral("Renamed place"));
575 void PlacesItemModelTest::testEditAfterCreation()
577 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
578 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
580 // create a new place
581 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
582 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
584 PlacesItemModel
*model
= new PlacesItemModel();
585 QTRY_COMPARE(model
->count(), m_model
->count());
587 // make sure that the new item will be removed later
591 PlacesItem
*item
= m_model
->placesItem(3);
592 item
->setText(QStringLiteral("Renamed place"));
595 // check if the second model got the changes
596 QTRY_COMPARE(model
->count(), m_model
->count());
597 QTRY_COMPARE(model
->placesItem(3)->text(), m_model
->placesItem(3)->text());
598 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
599 m_model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
600 QTRY_COMPARE(model
->placesItem(3)->icon(), m_model
->placesItem(3)->icon());
601 QTRY_COMPARE(model
->placesItem(3)->url(), m_model
->placesItem(3)->url());
604 void PlacesItemModelTest::testEditMetadata()
606 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
607 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
609 // create a new place
610 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
611 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
613 // check if the new entry was created
614 PlacesItemModel
*model
= new PlacesItemModel();
615 QTRY_COMPARE(model
->count(), m_model
->count());
617 // make sure that the new item will be removed later
620 // modify place metadata
621 PlacesItem
*item
= m_model
->placesItem(3);
622 item
->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
625 // check if the place was modified in both models
626 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
627 KAboutData::applicationData().componentName());
628 QTRY_COMPARE(model
->placesItem(3)->text(), m_model
->placesItem(3)->text());
629 QTRY_COMPARE(model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
630 m_model
->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
631 QTRY_COMPARE(model
->placesItem(3)->icon(), m_model
->placesItem(3)->icon());
632 QTRY_COMPARE(model
->placesItem(3)->url(), m_model
->placesItem(3)->url());
635 void PlacesItemModelTest::testRefresh()
637 const QUrl tempUrl
= QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation
));
638 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
640 // create a new place
641 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl
, QString());
642 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
644 PlacesItemModel
*model
= new PlacesItemModel();
645 QTRY_COMPARE(model
->count(), m_model
->count());
647 // make sure that the new item will be removed later
650 PlacesItem
*item
= m_model
->placesItem(3);
651 PlacesItem
*sameItem
= model
->placesItem(3);
652 QCOMPARE(item
->text(), sameItem
->text());
655 item
->setText(QStringLiteral("Renamed place"));
657 // item from another model is not affected at the moment
658 QVERIFY(item
->text() != sameItem
->text());
663 // item must be equal
664 QTRY_COMPARE(item
->text(), sameItem
->text());
667 void PlacesItemModelTest::testIcons_data()
669 QTest::addColumn
<QUrl
>("url");
670 QTest::addColumn
<QString
>("expectedIconName");
673 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
676 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
679 QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << QStringLiteral("view-calendar-month");
682 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
685 void PlacesItemModelTest::testIcons()
688 QFETCH(QString
, expectedIconName
);
690 PlacesItem
*item
= m_model
->placesItem(indexOf(url
));
691 QCOMPARE(item
->icon(), expectedIconName
);
693 for (int r
= 0; r
< m_model
->count(); r
++) {
694 QVERIFY(!m_model
->placesItem(r
)->icon().isEmpty());
698 void PlacesItemModelTest::testDragAndDrop()
700 QList
<QVariant
> args
;
701 KItemRangeList range
;
702 QStringList urls
= initialUrls();
703 QSignalSpy
itemsInsertedSpy(m_model
, &PlacesItemModel::itemsInserted
);
704 QSignalSpy
itemsRemovedSpy(m_model
, &PlacesItemModel::itemsRemoved
);
706 CHECK_PLACES_URLS(initialUrls());
707 // 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
708 QMimeData
*dropData
= createMimeData(QList
<int>() << 1);
709 m_model
->dropMimeDataBefore(m_model
->count() - 1, dropData
);
713 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
714 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
716 // remove item from actual position
717 args
= itemsRemovedSpy
.takeFirst();
718 range
= args
.at(0).value
<KItemRangeList
>();
719 QCOMPARE(range
.size(), 1);
720 QCOMPARE(range
.at(0).count
, 1);
721 QCOMPARE(range
.at(0).index
, 1);
723 // insert intem in his group
724 args
= itemsInsertedSpy
.takeFirst();
725 range
= args
.at(0).value
<KItemRangeList
>();
726 QCOMPARE(range
.size(), 1);
727 QCOMPARE(range
.at(0).count
, 1);
728 QCOMPARE(range
.at(0).index
, 2);
730 CHECK_PLACES_URLS(urls
);
732 itemsInsertedSpy
.clear();
733 itemsRemovedSpy
.clear();
735 // Move the KDE_ROOT_PATH to his original position
736 dropData
= createMimeData(QList
<int>() << 2);
737 m_model
->dropMimeDataBefore(1, dropData
);
741 QTRY_COMPARE(itemsInsertedSpy
.count(), 1);
742 QTRY_COMPARE(itemsRemovedSpy
.count(), 1);
744 // remove item from actual position
745 args
= itemsRemovedSpy
.takeFirst();
746 range
= args
.at(0).value
<KItemRangeList
>();
747 QCOMPARE(range
.size(), 1);
748 QCOMPARE(range
.at(0).count
, 1);
749 QCOMPARE(range
.at(0).index
, 2);
751 // insert intem in the requested position
752 args
= itemsInsertedSpy
.takeFirst();
753 range
= args
.at(0).value
<KItemRangeList
>();
754 QCOMPARE(range
.size(), 1);
755 QCOMPARE(range
.at(0).count
, 1);
756 QCOMPARE(range
.at(0).index
, 1);
758 CHECK_PLACES_URLS(urls
);
761 QTEST_MAIN(PlacesItemModelTest
)
763 #include "placesitemmodeltest.moc"