+ QTRY_COMPARE(model->placesItem(tempDirIndex)->text(), m_model->placesItem(tempDirIndex)->text());
+ QTRY_COMPARE(model->placesItem(tempDirIndex)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
+ m_model->placesItem(tempDirIndex)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
+ QTRY_COMPARE(model->placesItem(tempDirIndex)->icon(), m_model->placesItem(tempDirIndex)->icon());
+ QTRY_COMPARE(model->placesItem(tempDirIndex)->url(), m_model->placesItem(tempDirIndex)->url());
+}
+
+void PlacesItemModelTest::testRefresh()
+{
+ int tempDirIndex = 2;
+ if (m_hasDesktopFolder) {
+ tempDirIndex++;
+ }
+ if (m_hasDocumentsFolder) {
+ tempDirIndex++;
+ }
+ if (m_hasDownloadsFolder) {
+ tempDirIndex++;
+ }
+
+ const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
+ QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
+
+ // create a new place
+ createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
+ QTRY_COMPARE(itemsInsertedSpy.count(), 1);
+
+ PlacesItemModel *model = new PlacesItemModel();
+ QTRY_COMPARE(model->count(), m_model->count());
+
+ // make sure that the new item will be removed later
+ schedulePlaceRemoval(tempDirIndex);
+
+ PlacesItem *item = m_model->placesItem(tempDirIndex);
+ PlacesItem *sameItem = model->placesItem(tempDirIndex);
+ QCOMPARE(item->text(), sameItem->text());
+
+ // modify place text
+ item->setText(QStringLiteral("Renamed place"));
+
+ // item from another model is not affected at the moment
+ QVERIFY(item->text() != sameItem->text());
+
+ // propagate change
+ m_model->refresh();
+
+ // item must be equal
+ QTRY_COMPARE(item->text(), sameItem->text());
+}
+
+void PlacesItemModelTest::testIcons_data()
+{
+ QTest::addColumn<QUrl>("url");
+ QTest::addColumn<QString>("expectedIconName");
+
+ // places
+ QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
+
+ // baloo -search
+ QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
+
+ // baloo - timeline
+ QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << QStringLiteral("go-jump-today");
+
+ // devices
+ QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
+}
+
+void PlacesItemModelTest::testIcons()
+{
+ QFETCH(QUrl, url);
+ QFETCH(QString, expectedIconName);
+
+ PlacesItem *item = m_model->placesItem(indexOf(url));
+ QCOMPARE(item->icon(), expectedIconName);
+
+ for (int r = 0; r < m_model->count(); r++) {
+ QVERIFY(!m_model->placesItem(r)->icon().isEmpty());
+ }
+}
+
+void PlacesItemModelTest::testDragAndDrop()
+{
+ int lastIndex = 1; // last index of places group
+ if (m_hasDesktopFolder) {
+ lastIndex++;
+ }
+ if (m_hasDocumentsFolder) {
+ lastIndex++;
+ }
+ if (m_hasDownloadsFolder) {
+ lastIndex++;
+ }
+
+ QList<QVariant> args;
+ KItemRangeList range;
+ QStringList urls = initialUrls();
+
+ QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
+ QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
+
+ CHECK_PLACES_URLS(initialUrls());
+ // Move the home directory to the end of the places group
+ QMimeData *dropData = createMimeData(QList<int>() << 0);
+ m_model->dropMimeDataBefore(m_model->count() - 1, dropData);
+ urls.move(0, lastIndex);
+ delete dropData;
+
+ QTRY_COMPARE(itemsInsertedSpy.count(), 1);
+ QTRY_COMPARE(itemsRemovedSpy.count(), 1);
+
+ // remove item from actual position
+ args = itemsRemovedSpy.takeFirst();
+ range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.size(), 1);
+ QCOMPARE(range.at(0).count, 1);
+ QCOMPARE(range.at(0).index, 0);
+
+ // insert intem in his group
+ args = itemsInsertedSpy.takeFirst();
+ range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.size(), 1);
+ QCOMPARE(range.at(0).count, 1);
+ QCOMPARE(range.at(0).index, lastIndex);
+
+ CHECK_PLACES_URLS(urls);
+
+ itemsInsertedSpy.clear();
+ itemsRemovedSpy.clear();
+
+ // Move home directory item back to its original position
+ dropData = createMimeData(QList<int>() << lastIndex);
+ m_model->dropMimeDataBefore(0, dropData);
+ urls.move(lastIndex, 0);
+ delete dropData;
+
+ QTRY_COMPARE(itemsInsertedSpy.count(), 1);
+ QTRY_COMPARE(itemsRemovedSpy.count(), 1);
+
+ // remove item from actual position
+ args = itemsRemovedSpy.takeFirst();
+ range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.size(), 1);
+ QCOMPARE(range.at(0).count, 1);
+ QCOMPARE(range.at(0).index, lastIndex);
+
+ // insert intem in the requested position
+ args = itemsInsertedSpy.takeFirst();
+ range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.size(), 1);
+ QCOMPARE(range.at(0).count, 1);
+ QCOMPARE(range.at(0).index, 0);
+
+ CHECK_PLACES_URLS(urls);
+}
+
+void PlacesItemModelTest::testHideDevices()
+{
+ QSignalSpy itemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
+ QStringList urls = initialUrls();
+
+ m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, true);
+ QTRY_VERIFY(m_model->isGroupHidden(KFilePlacesModel::RemovableDevicesType));
+ QTRY_COMPARE(itemsRemoved.count(), 3);
+
+ // remove removable-devices
+ urls.removeOne(QStringLiteral("/media/floppy0"));
+ urls.removeOne(QStringLiteral("/media/XO-Y4"));
+ urls.removeOne(QStringLiteral("/media/cdrom"));
+
+ // check if the correct urls was removed
+ CHECK_PLACES_URLS(urls);
+
+ delete m_model;
+ m_model = new PlacesItemModel();
+ QTRY_COMPARE(m_model->count(), urls.count());
+ CHECK_PLACES_URLS(urls);
+
+ // revert changes
+ m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, false);
+ urls = initialUrls();
+ QTRY_COMPARE(m_model->count(), urls.count());
+ CHECK_PLACES_URLS(urls);
+}
+
+void PlacesItemModelTest::testDuplicatedEntries()
+{
+ QStringList urls = initialUrls();
+ // create a duplicated entry on bookmark
+ KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
+ KBookmarkGroup root = bookmarkManager->root();
+ KBookmark bookmark = root.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {});
+
+ const QString id = QUuid::createUuid().toString();
+ bookmark.setMetaDataItem(QStringLiteral("ID"), id);
+ bookmark.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
+ bookmarkManager->emitChanged(bookmarkManager->root());
+
+ PlacesItemModel *newModel = new PlacesItemModel();
+ QTRY_COMPARE(placesUrls(newModel).count(QStringLiteral("search:/videos")), 1);
+ QTRY_COMPARE(urls, placesUrls(newModel));
+ delete newModel;
+}
+
+void PlacesItemModelTest::renameAfterCreation()
+{
+ int tempDirIndex = 2;
+ if (m_hasDesktopFolder) {
+ tempDirIndex++;
+ }
+ if (m_hasDocumentsFolder) {
+ tempDirIndex++;
+ }
+ if (m_hasDownloadsFolder) {
+ tempDirIndex++;
+ }
+
+ const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
+ QStringList urls = initialUrls();
+ PlacesItemModel *model = new PlacesItemModel();
+
+ CHECK_PLACES_URLS(urls);