diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -57,3 +57,9 @@ ecm_add_test(dolphinmainwindowtest.cpp TEST_NAME dolphinmainwindowtest LINK_LIBRARIES dolphinprivate dolphinstatic Qt5::Test) + +# PlacesItemModelTest +ecm_add_test(placesitemmodeltest.cpp +TEST_NAME placesitemmodeltest +LINK_LIBRARIES dolphinprivate dolphinstatic Qt5::Test) + diff --git a/src/tests/placesitemmodeltest.cpp b/src/tests/placesitemmodeltest.cpp new file mode 100644 --- /dev/null +++ b/src/tests/placesitemmodeltest.cpp @@ -0,0 +1,327 @@ +/*************************************************************************** + * Copyright (C) 2017 by Renato Araujo Oliveira * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "panels/places/placesitemmodel.h" +#include "panels/places/placesitem.h" +#include "views/viewproperties.h" +#include "kitemviews/kitemrange.h" + +Q_DECLARE_METATYPE(KItemRangeList) +Q_DECLARE_METATYPE(PlacesItem::GroupType) +//Q_DECLARE_METATYPE(DolphinView::Mode) + +static QString bookmarksFile() +{ + return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel"; +} + +class PlacesItemModelTest : public QObject +{ + Q_OBJECT + +private slots: + void init(); + void cleanup(); + + void initTestCase(); + void cleanupTestCase(); + + void testGroups(); + void testPlaceItem_data(); + void testPlaceItem(); + void testTearDownDevice(); + void testDefaultViewProperties_data(); + void testDefaultViewProperties(); + void testClear(); + void testHideItem(); + +private: + PlacesItemModel* m_model; + QMap m_interfacesMap; + + void setBalooEnabled(bool enabled); + int indexOf(const QUrl &url); + QDBusInterface *fakeManager(); + QDBusInterface *fakeDevice(const QString &udi); +}; + +void PlacesItemModelTest::setBalooEnabled(bool enabled) +{ + KConfig config(QStringLiteral("baloofilerc")); + KConfigGroup basicSettings = config.group("Basic Settings"); + basicSettings.writeEntry("Indexing-Enabled", enabled); + config.sync(); +} + +int PlacesItemModelTest::indexOf(const QUrl &url) +{ + for(int r = 0; r < m_model->count(); r++) { + if (m_model->placesItem(r)->url() == url) + return r; + } + return -1; +} + +QDBusInterface *PlacesItemModelTest::fakeManager() +{ + return fakeDevice(QStringLiteral("/org/kde/solid/fakehw")); +} + +QDBusInterface *PlacesItemModelTest::fakeDevice(const QString &udi) +{ + if (m_interfacesMap.contains(udi)) { + return m_interfacesMap[udi]; + } + + QDBusInterface *iface = new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi); + m_interfacesMap[udi] = iface; + + return iface; +} + +void PlacesItemModelTest::init() +{ + m_model = new PlacesItemModel(); +} + +void PlacesItemModelTest::cleanup() +{ + delete m_model; + m_model = 0; +} + +void PlacesItemModelTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); + + const QString fakeHw = QFINDTESTDATA("fakecomputer.xml"); + QVERIFY(!fakeHw.isEmpty()); + qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw)); + + setBalooEnabled(true); + // Ensure we'll have a clean bookmark file to start + QFile::remove(bookmarksFile()); + + qRegisterMetaType(); +} + +void PlacesItemModelTest::cleanupTestCase() +{ + qDeleteAll(m_interfacesMap); + QFile::remove(bookmarksFile()); +} + +void PlacesItemModelTest::testGroups() +{ + auto groups = m_model->groups(); + + QCOMPARE(groups.size(), 4); + QCOMPARE(groups[0].first, 0); + QCOMPARE(groups[0].second.toString(), QStringLiteral("Places")); + QCOMPARE(groups[1].first, 4); + QCOMPARE(groups[1].second.toString(), QStringLiteral("Recently Saved")); + QCOMPARE(groups[2].first, 8); + QCOMPARE(groups[2].second.toString(), QStringLiteral("Search For")); + QCOMPARE(groups[3].first, 12); + QCOMPARE(groups[3].second.toString(), QStringLiteral("Devices")); +} + +void PlacesItemModelTest::testPlaceItem_data() +{ + QTest::addColumn("url"); + QTest::addColumn("expectedIsHidden"); + QTest::addColumn("expectedIsSystemItem"); + QTest::addColumn("expectedGroupType"); + QTest::addColumn("expectedStorageSetupNeeded"); + + // places + QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << PlacesItem::PlacesType << false; + + // baloo -search + QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << PlacesItem::SearchForType << false; + + // baloo - timeline + QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << false << true << PlacesItem::RecentlySavedType << false; + + // devices + QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << PlacesItem::DevicesType << false; +} + +void PlacesItemModelTest::testPlaceItem() +{ + QFETCH(QUrl, url); + QFETCH(bool, expectedIsHidden); + QFETCH(bool, expectedIsSystemItem); + QFETCH(PlacesItem::GroupType, expectedGroupType); + QFETCH(bool, expectedStorageSetupNeeded); + + const int index = indexOf(url); + PlacesItem *item = m_model->placesItem(index); + QCOMPARE(item->url(), url); + QCOMPARE(item->isHidden(), expectedIsHidden); + QCOMPARE(item->isSystemItem(), expectedIsSystemItem); + QCOMPARE(item->groupType(), expectedGroupType); + QCOMPARE(item->storageSetupNeeded(), expectedStorageSetupNeeded); +} + +void PlacesItemModelTest::testTearDownDevice() +{ + const QUrl mediaUrl("file:///media/XO-Y4"); + int index = indexOf(mediaUrl); + QVERIFY(index != -1); + + auto actEject = m_model->ejectAction(index); + QVERIFY(!actEject); + + auto act = m_model->teardownAction(index); + QVERIFY(act); + QCOMPARE(act->text(), QStringLiteral("Safely Remove")); + + QCOMPARE(m_model->count(), 17); + + QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved); + fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096"); + QTRY_COMPARE(m_model->count(), 16); + QCOMPARE(spyItemsRemoved.count(), 1); + const QList spyItemsRemovedArgs = spyItemsRemoved.takeFirst(); + const KItemRangeList removedRange = spyItemsRemovedArgs.at(0).value(); + QCOMPARE(removedRange.size(), 1); + QCOMPARE(removedRange.first().index, index); + QCOMPARE(removedRange.first().count, 1); + + QCOMPARE(indexOf(mediaUrl), -1); + + QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted); + fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096"); + QTRY_COMPARE(m_model->count(), 17); + QCOMPARE(spyItemsInserted.count(), 1); + index = indexOf(mediaUrl); + + const QList args = spyItemsInserted.takeFirst(); + const KItemRangeList insertedRange = args.at(0).value(); + QCOMPARE(insertedRange.size(), 1); + QCOMPARE(insertedRange.first().index, index); + QCOMPARE(insertedRange.first().count, 1); +} + +void PlacesItemModelTest::testDefaultViewProperties_data() +{ + QTest::addColumn("url"); + QTest::addColumn("expectedViewMode"); + QTest::addColumn("expectedPreviewShow"); + QTest::addColumn >("expectedVisibleRole"); + + // places + QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView << false << QList({"text"}); + + // baloo -search + QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView << false << QList({"text", "path"}); + + // baloo - timeline + QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << DolphinView::DetailsView << false << QList({"text", "modificationtime"}); + + // devices + QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView << false << QList({"text"}); +} + +void PlacesItemModelTest::testDefaultViewProperties() +{ + QFETCH(QUrl, url); + QFETCH(DolphinView::Mode, expectedViewMode); + QFETCH(bool, expectedPreviewShow); + QFETCH(QList, expectedVisibleRole); + + ViewProperties vprop(m_model->convertedUrl(url)); + QCOMPARE(vprop.viewMode(), expectedViewMode); + QCOMPARE(vprop.previewsShown(), expectedPreviewShow); + QCOMPARE(vprop.visibleRoles(), expectedVisibleRole); +} + +void PlacesItemModelTest::testClear() +{ + QCOMPARE(m_model->count(), 17); + m_model->clear(); + QCOMPARE(m_model->count(), 0); + QCOMPARE(m_model->hiddenCount(), 0); +} + +void PlacesItemModelTest::testHideItem() +{ + const QUrl mediaUrl("file:///media/XO-Y4"); + const int index = indexOf(mediaUrl); + + PlacesItem *item = m_model->placesItem(index); + + QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved); + QList spyItemsRemovedArgs; + KItemRangeList removedRange; + + QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted); + QList spyItemsInsertedArgs; + KItemRangeList insertedRange; + + QVERIFY(item); + + // hide an item + item->setHidden(true); + + // check if items removed was fired + QTRY_COMPARE(m_model->count(), 16); + QCOMPARE(spyItemsRemoved.count(), 1); + spyItemsRemovedArgs = spyItemsRemoved.takeFirst(); + removedRange = spyItemsRemovedArgs.at(0).value(); + QCOMPARE(removedRange.size(), 1); + QCOMPARE(removedRange.first().index, index); + QCOMPARE(removedRange.first().count, 1); + + // allow model to show hidden items + m_model->setHiddenItemsShown(true); + + // check if the items inserted was fired + spyItemsInsertedArgs = spyItemsInserted.takeFirst(); + insertedRange = spyItemsInsertedArgs.at(0).value(); + QCOMPARE(insertedRange.size(), 1); + QCOMPARE(insertedRange.first().index, index); + QCOMPARE(insertedRange.first().count, 1); + + // mark item as visible + item = m_model->placesItem(index); + item->setHidden(false); + + // mark model to hide invisible items + m_model->setHiddenItemsShown(true); + + QTRY_COMPARE(m_model->count(), 17); +} + +QTEST_MAIN(PlacesItemModelTest) + +#include "placesitemmodeltest.moc"