summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <[email protected]>2017-11-27 09:36:40 -0700
committerNathaniel Graham <[email protected]>2017-11-27 09:37:24 -0700
commit89e4e8dd0a589c99e451e9f0aa4855e5720652ad (patch)
tree194f9adda380b65ed2ee1e5a060c270f1b71955b
parent11e2b186af64e5a962f5020be100eafc28e50e18 (diff)
Created unit test for PlacesItemModel
Summary: Make sure that PlacesItemModel is covered by unit test Reviewers: ervin, elvisangelaccio, mwolff Reviewed By: ervin, elvisangelaccio, mwolff Subscribers: mwolff, elvisangelaccio, ngraham, #dolphin Differential Revision: https://phabricator.kde.org/D8630
-rw-r--r--src/tests/CMakeLists.txt8
-rw-r--r--src/tests/data/fakecomputer.xml387
-rw-r--r--src/tests/placesitemmodeltest.cpp536
3 files changed, 930 insertions, 1 deletions
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 13bd963..c656173 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -58,4 +58,10 @@ ecm_add_test(dolphinmainwindowtest.cpp
TEST_NAME dolphinmainwindowtest
LINK_LIBRARIES dolphinprivate dolphinstatic Qt5::Test)
-ecm_add_test(draganddrophelpertest.cpp LINK_LIBRARIES dolphinprivate Qt5::Test) \ No newline at end of file
+ecm_add_test(draganddrophelpertest.cpp LINK_LIBRARIES dolphinprivate Qt5::Test)
+
+# PlacesItemModelTest
+ecm_add_test(placesitemmodeltest.cpp
+TEST_NAME placesitemmodeltest
+LINK_LIBRARIES dolphinprivate dolphinstatic Qt5::Test)
+
diff --git a/src/tests/data/fakecomputer.xml b/src/tests/data/fakecomputer.xml
new file mode 100644
index 0000000..48be046
--- /dev/null
+++ b/src/tests/data/fakecomputer.xml
@@ -0,0 +1,387 @@
+<!-- Please note that in this file we indent more than necessary so that the
+ device tree is visible -->
+
+<machine>
+ <!-- This is a computer -->
+ <device udi="/org/kde/solid/fakehw/computer">
+ <property key="name">Computer</property>
+ <property key="vendor">Solid</property>
+ </device>
+
+ <!-- Two CPUs -->
+ <device udi="/org/kde/solid/fakehw/acpi_CPU0">
+ <property key="name">Solid Processor #0</property>
+ <property key="interfaces">Processor</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ <property key="number">0</property>
+ <property key="maxSpeed">3200</property>
+ <property key="canChangeFrequency">true</property>
+ <property key="instructionSets">mmx,sse</property>
+ </device>
+ <device udi="/org/kde/solid/fakehw/acpi_CPU1">
+ <property key="name">Solid Processor #1</property>
+ <property key="interfaces">Processor</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ <property key="number">1</property>
+ <property key="maxSpeed">3200</property>
+ <property key="canChangeFrequency">true</property>
+ </device>
+
+ <!-- Platform Device for a floppy drive -->
+ <device udi="/org/kde/solid/fakehw/platform_floppy_0">
+ <property key="name">Platform Device (floppy)</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ </device>
+ <!-- The actual floppy device -->
+ <device udi="/org/kde/solid/fakehw/platform_floppy_0_storage">
+ <property key="name">PC Floppy Drive</property>
+ <property key="interfaces">StorageDrive,Block</property>
+ <property key="parent">/org/kde/solid/fakehw/platform_floppy_0</property>
+
+ <property key="minor">0</property>
+ <property key="major">2</property>
+ <property key="device">/dev/fd0</property>
+
+ <property key="bus">platform</property>
+ <property key="driveType">floppy</property>
+ <property key="isRemovable">true</property>
+ <property key="isEjectRequired">false</property>
+ <property key="isHotpluggable">false</property>
+ <property key="isMediaCheckEnabled">false</property>
+ </device>
+ <!-- A (generally) virtual volume tracking the floppy drive state -->
+ <device udi="/org/kde/solid/fakehw/platform_floppy_0_storage_virt_volume">
+ <property key="name">Floppy Disk</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/platform_floppy_0_storage</property>
+
+ <property key="minor">0</property>
+ <property key="major">2</property>
+ <property key="device">/dev/fd0</property>
+
+ <property key="isIgnored">false</property>
+ <property key="isMounted">true</property>
+ <property key="mountPoint">/media/floppy0</property>
+ <property key="usage">filesystem</property>
+ </device>
+
+
+
+ <!-- Primary IDE controller -->
+ <device udi="/org/kde/solid/fakehw/pci_001">
+ <property key="name">99021 IDE Controller #1</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ </device>
+
+ <!-- Master device... -->
+ <device udi="/org/kde/solid/fakehw/pci_001_ide_0_0">
+ <property key="name">IDE device (master)</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_001</property>
+ </device>
+ <!-- ... is a 250GB disk... -->
+ <device udi="/org/kde/solid/fakehw/storage_serial_HD56890I">
+ <property key="name">HD250GB</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="interfaces">StorageDrive,Block</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_001_ide_0_0</property>
+
+ <property key="minor">0</property>
+ <property key="major">3</property>
+ <property key="device">/dev/hda</property>
+
+ <property key="bus">scsi</property>
+ <property key="driveType">disk</property>
+ <property key="isRemovable">false</property>
+ <property key="isEjectRequired">false</property>
+ <property key="isHotpluggable">false</property>
+ <property key="isMediaCheckEnabled">false</property>
+ <property key="product">HD250GBSATA</property>
+ </device>
+ <!-- ... with five partitions:
+ - one physical partition (the root /, ext3, 20GB)
+ - one extended containing three logical volumes:
+ - a swap volume (2GB)
+ - /home volume (xfs, 208GB)
+ - /foreign volume (ntfs, 20GB)
+ -->
+ <device udi="/org/kde/solid/fakehw/volume_uuid_feedface">
+ <property key="name">/</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_serial_HD56890I</property>
+
+ <property key="minor">1</property>
+ <property key="major">3</property>
+ <property key="device">/dev/hda1</property>
+
+ <property key="isIgnored">true</property>
+ <property key="isMounted">true</property>
+ <property key="mountPoint">/</property>
+ <property key="usage">filesystem</property>
+ <property key="fsType">ext3</property>
+ <property key="label">Root</property>
+ <property key="uuid">feedface</property>
+ <property key="size">21474836480</property>
+ </device>
+ <device udi="/org/kde/solid/fakehw/volume_uuid_c0ffee">
+ <property key="name">/home</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_serial_HD56890I</property>
+
+ <property key="minor">6</property>
+ <property key="major">3</property>
+ <property key="device">/dev/hda6</property>
+
+ <property key="isIgnored">true</property>
+ <property key="isMounted">true</property>
+ <property key="mountPoint">/home</property>
+ <property key="usage">filesystem</property>
+ <property key="fsType">xfs</property>
+ <property key="label">Home</property>
+ <property key="uuid">c0ffee</property>
+ <property key="size">223338299392</property>
+ </device>
+ <device udi="/org/kde/solid/fakehw/volume_uuid_f00ba7">
+ <property key="name">/foreign</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_serial_HD56890I</property>
+
+ <property key="minor">7</property>
+ <property key="major">3</property>
+ <property key="device">/dev/hda7</property>
+
+ <property key="isIgnored">false</property>
+ <property key="isMounted">true</property>
+ <property key="mountPoint">/foreign</property>
+ <property key="usage">filesystem</property>
+ <property key="fsType">ntfs</property>
+ <property key="label">Foreign</property>
+ <property key="uuid">f00ba7</property>
+ <property key="size">21474836480</property>
+ </device>
+ <device udi="/org/kde/solid/fakehw/volume_part2_size_1024">
+ <property key="name">StorageVolume</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_serial_HD56890I</property>
+
+ <property key="minor">2</property>
+ <property key="major">3</property>
+ <property key="device">/dev/hda2</property>
+
+ <property key="isIgnored">true</property>
+ <property key="isMounted">false</property>
+ <property key="usage">other</property>
+ <property key="size">1024</property>
+ </device>
+ <device udi="/org/kde/solid/fakehw/volume_part5_size_1048576">
+ <property key="name">StorageVolume (swap)</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_serial_HD56890I</property>
+
+ <property key="minor">5</property>
+ <property key="major">3</property>
+ <property key="device">/dev/hda5</property>
+
+ <property key="isIgnored">true</property>
+ <property key="isMounted">false</property>
+ <property key="usage">other</property>
+ <property key="fsType">swap</property>
+ <property key="size">2147483648</property>
+ </device>
+
+
+ <!-- Secondary IDE controller -->
+ <device udi="/org/kde/solid/fakehw/pci_002">
+ <property key="name">99021 IDE Controller #2</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ </device>
+
+ <!-- Master device... -->
+ <device udi="/org/kde/solid/fakehw/pci_002_ide_1_0">
+ <property key="name">IDE device (master)</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_002</property>
+ </device>
+ <!-- ... is a DVD writer... -->
+ <device udi="/org/kde/solid/fakehw/storage_model_solid_writer">
+ <property key="name">Solid IDE DVD Writer</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="interfaces">Block,StorageDrive,OpticalDrive</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_002_ide_1_0</property>
+
+ <property key="minor">0</property>
+ <property key="major">22</property>
+ <property key="device">/dev/hdc</property>
+
+ <property key="bus">ide</property>
+ <property key="driveType">cdrom</property>
+ <property key="isRemovable">true</property>
+ <property key="isEjectRequired">true</property>
+ <property key="isHotpluggable">false</property>
+ <property key="isMediaCheckEnabled">true</property>
+ <property key="product">Solid DVD Writer</property>
+
+ <property key="supportedMedia">cdr,cdrw,dvd,dvdr,dvdrw</property>
+ <property key="readSpeed">4234</property>
+ <property key="writeSpeed">4234</property>
+ <property key="writeSpeeds">4234,2822,2117,1411,706</property>
+ </device>
+ <!-- ... with a cd-r in it -->
+ <device udi="/org/kde/solid/fakehw/volume_uuid_5011">
+ <property key="name">FooDistro i386</property>
+ <property key="interfaces">Block,StorageVolume,OpticalDisc,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_model_solid_writer</property>
+
+ <property key="discType">cd_rw</property>
+ <property key="isAppendable">false</property>
+ <property key="isRewritable">true</property>
+ <property key="isBlank">false</property>
+ <property key="availableContent">data</property>
+ <property key="mountPoint">/media/cdrom</property>
+
+ <property key="uuid">5011</property>
+ <property key="size">731047936</property>
+ <property key="label">FooDistro i386</property>
+ </device>
+
+ <!-- Slave device... -->
+ <device udi="/org/kde/solid/fakehw/pci_002_ide_1_1">
+ <property key="name">IDE device (slave)</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_002</property>
+ </device>
+ <!-- ... is a DVD reader... -->
+ <device udi="/org/kde/solid/fakehw/storage_model_solid_reader">
+ <property key="name">Solid IDE DVD Reader</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="interfaces">Block,StorageDrive,OpticalDrive</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_002_ide_1_1</property>
+
+ <property key="minor">0</property>
+ <property key="major">22</property>
+ <property key="device">/dev/hdc</property>
+
+ <property key="bus">ide</property>
+ <property key="driveType">cdrom</property>
+ <property key="isRemovable">true</property>
+ <property key="isEjectRequired">true</property>
+ <property key="isHotpluggable">false</property>
+ <property key="isMediaCheckEnabled">true</property>
+ <property key="product">Solid DVD Reader</property>
+
+ <property key="supportedMedia">cdr,cdrw,dvd,dvdr,dvdrw,dvdram,dvdplusr,dvdplusrw</property>
+ <property key="readSpeed">4234</property>
+ </device>
+ <!-- ... with a DVD Video in it -->
+ <device udi="/org/kde/solid/fakehw/volume_label_SOLIDMAN_BEGINS">
+ <property key="name">SolidMan Begins</property>
+ <property key="interfaces">Block,StorageVolume,OpticalDisc</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_model_solid_reader</property>
+
+ <property key="discType">dvd_rom</property>
+ <property key="isAppendable">false</property>
+ <property key="isRewritable">false</property>
+ <property key="isBlank">false</property>
+ <property key="availableContent">dvdvideo</property>
+
+ <property key="uuid">5012</property>
+ <property key="size">8033075200</property>
+ <property key="label">SolidMan Begins</property>
+ </device>
+
+
+
+ <!-- First USB Controller -->
+ <device udi="/org/kde/solid/fakehw/pci_8086_265c">
+ <property key="name">99021 USB2 EHCI Controller #1</property>
+ <property key="vendor">Acme Corporation</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ </device>
+ <!-- Host Controller -->
+ <device udi="/org/kde/solid/fakehw/usb_device_0_0_1d_7">
+ <property key="name">EHCI Host Controller</property>
+ <property key="vendor">Kernel ehci_hcd</property>
+ <property key="parent">/org/kde/solid/fakehw/pci_8086_265c</property>
+ </device>
+ <!-- USB Device -->
+ <device udi="/org/kde/solid/fakehw/usb_device_4e8_5041">
+ <property key="name">Acme XO-Y4</property>
+ <property key="parent">/org/kde/solid/fakehw/usb_device_0_0_1d_7</property>
+ </device>
+ <!-- Mass Storage Interface -->
+ <device udi="/org/kde/solid/fakehw/usb_device_4e8_5041_if0">
+ <property key="name">USB Mass Storage Inferface</property>
+ <property key="parent">/org/kde/solid/fakehw/usb_device_4e8_5041</property>
+ </device>
+ <!-- SCSI Adapter -->
+ <device udi="/org/kde/solid/fakehw/usb_device_4e8_5041_if0_scsi_host">
+ <property key="name">SCSI Host Adapter</property>
+ <property key="parent">/org/kde/solid/fakehw/usb_device_4e8_5041_if0</property>
+ </device>
+ <!-- SCSI Device -->
+ <device udi="/org/kde/solid/fakehw/usb_device_4e8_5041_if0_scsi_host_scsi_device_lun0">
+ <property key="name">SCSI Device</property>
+ <property key="parent">/org/kde/solid/fakehw/usb_device_4e8_5041_if0_scsi_host</property>
+ </device>
+ <!-- We finally find the storage device, which is a portable media player... -->
+ <device udi="/org/kde/solid/fakehw/storage_serial_XOY4_5206">
+ <property key="name">XO-Y4</property>
+ <property key="vendor">Acme Electronics</property>
+ <property key="interfaces">StorageDrive,Block,PortableMediaPlayer</property>
+ <property key="parent">/org/kde/solid/fakehw/usb_device_4e8_5041_if0_scsi_host_scsi_device_lun0</property>
+
+ <property key="minor">0</property>
+ <property key="major">8</property>
+ <property key="device">/dev/sda</property>
+
+ <property key="bus">usb</property>
+ <property key="driveType">disk</property>
+ <property key="isRemovable">true</property>
+ <property key="isEjectRequired">true</property>
+ <property key="isHotpluggable">true</property>
+ <property key="isMediaCheckEnabled">true</property>
+ <property key="product">XO-Y4</property>
+
+ <property key="accessMethod">MassStorage</property>
+ <property key="outputFormats">audio/x-mp3</property>
+ <property key="inputFormats">audio/x-wav,audio/x-mp3,audio/vorbis</property>
+ <property key="playlistFormats">audio/x-mpegurl</property>
+ </device>
+ <!-- ... with a partition since it's a USB Mass Storage device -->
+ <device udi="/org/kde/solid/fakehw/volume_part1_size_993284096">
+ <property key="name">StorageVolume (vfat)</property>
+ <property key="interfaces">Block,StorageVolume,StorageAccess</property>
+ <property key="parent">/org/kde/solid/fakehw/storage_serial_XOY4_5206</property>
+
+ <property key="minor">1</property>
+ <property key="major">8</property>
+ <property key="device">/dev/sda1</property>
+
+ <property key="isIgnored">false</property>
+ <property key="isMounted">true</property>
+ <property key="mountPoint">/media/XO-Y4</property>
+ <property key="usage">filesystem</property>
+ <property key="fsType">vfat</property>
+ <property key="size">993284096</property>
+ </device>
+
+ <device udi="/org/kde/solid/fakehw/fstab">
+ <property key="name">Network Shares</property>
+ <property key="product">Network Shares</property>
+ <property key="vendor">KDE</property>
+ <property key="parent">/org/kde/solid/fakehw/computer</property>
+ </device>
+ <device udi="/org/kde/solid/fakehw/fstab/thehost/solidpath">
+ <property key="parent">/org/kde/solid/fstab</property>
+ <property key="interfaces">NetworkShare,StorageAccess</property>
+ <property key="vendor">/solidpath</property>
+ <property key="product">thehost</property>
+ <property key="type">nfs</property>
+ <property key="url">nfs://thehost/solid-path</property>
+ <property key="filePath">/media/nfs</property>
+ <property key="isIgnored">false</property>
+ <property key="isMounted">true</property>
+ <property key="mountPoint">/media/nfs</property>
+ </device>
+</machine>
diff --git a/src/tests/placesitemmodeltest.cpp b/src/tests/placesitemmodeltest.cpp
new file mode 100644
index 0000000..5eb65e0
--- /dev/null
+++ b/src/tests/placesitemmodeltest.cpp
@@ -0,0 +1,536 @@
+/***************************************************************************
+ * Copyright (C) 2017 by Renato Araujo Oliveira <[email protected]> *
+ * *
+ * 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 <QTest>
+#include <QSignalSpy>
+#include <QDebug>
+#include <QList>
+#include <QByteArray>
+#include <QStandardPaths>
+#include <QAction>
+#include <QDBusInterface>
+
+#include <KBookmarkManager>
+#include <KConfig>
+#include <KConfigGroup>
+#include <KAboutData>
+
+#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)
+
+#ifdef Q_OS_WIN
+//c:\ as root for windows
+#define KDE_ROOT_PATH "C:\\"
+#else
+#define KDE_ROOT_PATH "/"
+#endif
+
+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 testModelSort();
+ void testModelMove();
+ void testGroups();
+ void testPlaceItem_data();
+ void testPlaceItem();
+ void testTearDownDevice();
+ void testDefaultViewProperties_data();
+ void testDefaultViewProperties();
+ void testClear();
+ void testHideItem();
+ void testSystemItems();
+ void testEditBookmark();
+ void testEditAfterCreation();
+ void testEditMetadata();
+
+private:
+ PlacesItemModel* m_model;
+ QMap<QString, QDBusInterface *> m_interfacesMap;
+
+ void setBalooEnabled(bool enabled);
+ int indexOf(const QUrl &url);
+ QDBusInterface *fakeManager();
+ QDBusInterface *fakeDevice(const QString &udi);
+ QStringList placesUrls() const;
+ QStringList initialUrls() const;
+ void createPlaceItem(const QString &text, const QUrl &url, const QString &icon);
+};
+
+#define CHECK_PLACES_URLS(urls) \
+ QStringList tmp(urls); \
+ QStringList places = placesUrls(); \
+ while(!places.isEmpty()) { \
+ tmp.removeOne(places.takeFirst()); \
+ } \
+ if (!tmp.isEmpty()) { \
+ qWarning() << "Expected:" << urls; \
+ qWarning() << "Got:" << places; \
+ QCOMPARE(places, urls); \
+ }
+
+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;
+}
+
+QStringList PlacesItemModelTest::placesUrls() const
+{
+ QStringList urls;
+ for (int row = 0; row < m_model->count(); ++row) {
+ urls << m_model->placesItem(row)->url().toDisplayString(QUrl::PreferLocalFile);
+ }
+ return urls;
+}
+
+void PlacesItemModelTest::createPlaceItem(const QString &text, const QUrl &url, const QString &icon)
+{
+ PlacesItem *item = m_model->createPlacesItem(text,
+ url,
+ icon);
+ QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
+ m_model->appendItemToGroup(item);
+ QTRY_COMPARE(itemsInsertedSpy.count(), 1);
+}
+
+void PlacesItemModelTest::init()
+{
+ m_model = new PlacesItemModel();
+ // WORKAROUND: need to wait for bookmark to load, check: PlacesItemModel::updateBookmarks
+ QTest::qWait(300);
+ QCOMPARE(m_model->count(), 17);
+}
+
+void PlacesItemModelTest::cleanup()
+{
+ delete m_model;
+ m_model = nullptr;
+}
+
+void PlacesItemModelTest::initTestCase()
+{
+ QStandardPaths::setTestModeEnabled(true);
+
+ const QString fakeHw = QFINDTESTDATA("data/fakecomputer.xml");
+ QVERIFY(!fakeHw.isEmpty());
+ qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw));
+
+ setBalooEnabled(true);
+ const QString bookmarsFileName = bookmarksFile();
+ if (QFileInfo::exists(bookmarsFileName)) {
+ // Ensure we'll have a clean bookmark file to start
+ QVERIFY(QFile::remove(bookmarsFileName));
+ }
+
+ qRegisterMetaType<KItemRangeList>();
+}
+
+void PlacesItemModelTest::cleanupTestCase()
+{
+ qDeleteAll(m_interfacesMap);
+ QFile::remove(bookmarksFile());
+}
+
+QStringList PlacesItemModelTest::initialUrls() const
+{
+ QStringList urls;
+
+ urls << QDir::homePath() << QStringLiteral("remote:/") << QStringLiteral(KDE_ROOT_PATH) << QStringLiteral("trash:/")
+ << QStringLiteral("timeline:/today") << QStringLiteral("timeline:/yesterday") << QStringLiteral("timeline:/thismonth") << QStringLiteral("timeline:/lastmonth")
+ << QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
+ << QStringLiteral("/media/cdrom") << QStringLiteral("/foreign") << QStringLiteral("/media/XO-Y4") << QStringLiteral("/media/nfs") << QStringLiteral("/media/floppy0");
+
+ return urls;
+}
+
+void PlacesItemModelTest::testModelSort()
+{
+ CHECK_PLACES_URLS(initialUrls());
+}
+
+void PlacesItemModelTest::testModelMove()
+{
+ QStringList urls = initialUrls();
+ KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
+ KBookmarkGroup root = bookmarkManager->root();
+ KBookmark systemRoot = m_model->placesItem(1)->bookmark();
+ KBookmark last = m_model->placesItem(m_model->count() - 1)->bookmark();
+
+ // try to move the "root" path to the end of the list
+ root.moveBookmark(systemRoot, last);
+ bookmarkManager->emitChanged(root);
+
+ // make sure that the items still grouped and the "root" item was moved to the end of places group instead
+ urls.move(1, 2);
+ CHECK_PLACES_URLS(urls);
+}
+
+void PlacesItemModelTest::testGroups()
+{
+ const auto groups = m_model->groups();
+
+ QCOMPARE(groups.size(), 4);
+ QCOMPARE(groups.at(0).first, 0);
+ QCOMPARE(groups.at(0).second.toString(), QStringLiteral("Places"));
+ QCOMPARE(groups.at(1).first, 4);
+ QCOMPARE(groups.at(1).second.toString(), QStringLiteral("Recently Saved"));
+ QCOMPARE(groups.at(2).first, 8);
+ QCOMPARE(groups.at(2).second.toString(), QStringLiteral("Search For"));
+ QCOMPARE(groups.at(3).first, 12);
+ QCOMPARE(groups.at(3).second.toString(), QStringLiteral("Devices"));
+}
+
+void PlacesItemModelTest::testPlaceItem_data()
+{
+ QTest::addColumn<QUrl>("url");
+ QTest::addColumn<bool>("expectedIsHidden");
+ QTest::addColumn<bool>("expectedIsSystemItem");
+ QTest::addColumn<PlacesItem::GroupType>("expectedGroupType");
+ QTest::addColumn<bool>("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 = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
+ int index = indexOf(mediaUrl);
+ QVERIFY(index != -1);
+
+ auto ejectAction = m_model->ejectAction(index);
+ QVERIFY(!ejectAction);
+
+ auto teardownAction = m_model->teardownAction(index);
+ QVERIFY(teardownAction);
+
+ 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<QVariant> spyItemsRemovedArgs = spyItemsRemoved.takeFirst();
+ const KItemRangeList removedRange = spyItemsRemovedArgs.at(0).value<KItemRangeList>();
+ 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<QVariant> args = spyItemsInserted.takeFirst();
+ const KItemRangeList insertedRange = args.at(0).value<KItemRangeList>();
+ QCOMPARE(insertedRange.size(), 1);
+ QCOMPARE(insertedRange.first().index, index);
+ QCOMPARE(insertedRange.first().count, 1);
+}
+
+void PlacesItemModelTest::testDefaultViewProperties_data()
+{
+ QTest::addColumn<QUrl>("url");
+ QTest::addColumn<DolphinView::Mode>("expectedViewMode");
+ QTest::addColumn<bool>("expectedPreviewShow");
+ QTest::addColumn<QList<QByteArray> >("expectedVisibleRole");
+
+ // places
+ QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView << true << QList<QByteArray>({"text"});
+
+ // baloo -search
+ QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "path"});
+
+ // audio files
+ QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "artist", "album"});
+
+ // baloo - timeline
+ QTest::newRow("Baloo - Last Month") << QUrl("timeline:/lastmonth") << DolphinView::DetailsView << true << QList<QByteArray>({"text", "modificationtime"});
+
+ // devices
+ QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView << true << QList<QByteArray>({"text"});
+
+}
+
+void PlacesItemModelTest::testDefaultViewProperties()
+{
+ QFETCH(QUrl, url);
+ QFETCH(DolphinView::Mode, expectedViewMode);
+ QFETCH(bool, expectedPreviewShow);
+ QFETCH(QList<QByteArray>, expectedVisibleRole);
+
+ ViewProperties properties(m_model->convertedUrl(url));
+ QCOMPARE(properties.viewMode(), expectedViewMode);
+ QCOMPARE(properties.previewsShown(), expectedPreviewShow);
+ QCOMPARE(properties.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 = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
+ const int index = indexOf(mediaUrl);
+
+ PlacesItem *item = m_model->placesItem(index);
+
+ QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
+ QList<QVariant> spyItemsRemovedArgs;
+ KItemRangeList removedRange;
+
+ QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted);
+ QList<QVariant> 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<KItemRangeList>();
+ 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<KItemRangeList>();
+ 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);
+}
+
+void PlacesItemModelTest::testSystemItems()
+{
+ QCOMPARE(m_model->count(), 17);
+ for (int r = 0; r < m_model->count(); r++) {
+ QCOMPARE(m_model->placesItem(r)->isSystemItem(), !m_model->placesItem(r)->device().isValid());
+ }
+
+ // create a new entry (non system item)
+ PlacesItem *item = m_model->createPlacesItem(QStringLiteral("Temporary Dir"),
+ QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)),
+ QString());
+
+ QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
+ m_model->appendItemToGroup(item);
+
+ // check if the new entry was created
+ QTRY_COMPARE(itemsInsertedSpy.count(), 1);
+ QList<QVariant> args = itemsInsertedSpy.takeFirst();
+ KItemRangeList range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.first().index, 4);
+ QCOMPARE(range.first().count, 1);
+ QVERIFY(!m_model->placesItem(4)->isSystemItem());
+ QCOMPARE(m_model->count(), 18);
+
+ // remove new entry
+ QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
+ m_model->removeItem(4);
+ m_model->saveBookmarks();
+ QTRY_COMPARE(itemsRemovedSpy.count(), 1);
+ args = itemsRemovedSpy.takeFirst();
+ range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.first().index, 4);
+ QCOMPARE(range.first().count, 1);
+ QTRY_COMPARE(m_model->count(), 17);
+}
+
+void PlacesItemModelTest::testEditBookmark()
+{
+ QScopedPointer<PlacesItemModel> other(new PlacesItemModel());
+
+ createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
+
+ QSignalSpy itemsChangedSply(m_model, &PlacesItemModel::itemsChanged);
+ m_model->item(4)->setText(QStringLiteral("Renamed place"));
+ m_model->saveBookmarks();
+ QTRY_COMPARE(itemsChangedSply.count(), 1);
+ QList<QVariant> args = itemsChangedSply.takeFirst();
+ KItemRangeList range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.first().index, 4);
+ QCOMPARE(range.first().count, 1);
+ QSet<QByteArray> roles = args.at(1).value<QSet<QByteArray> >();
+ QCOMPARE(roles.size(), 1);
+ QCOMPARE(*roles.begin(), QByteArrayLiteral("text"));
+ QCOMPARE(m_model->item(4)->text(), QStringLiteral("Renamed place"));
+
+ // check if the item was updated in the other model
+ QTRY_COMPARE(other->item(4)->text(), QStringLiteral("Renamed place"));
+
+ // remove new entry
+ QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
+ m_model->removeItem(4);
+ m_model->saveBookmarks();
+ QTRY_COMPARE(itemsRemovedSpy.count(), 1);
+ args = itemsRemovedSpy.takeFirst();
+ range = args.at(0).value<KItemRangeList>();
+ QCOMPARE(range.first().index, 4);
+ QCOMPARE(range.first().count, 1);
+ QTRY_COMPARE(m_model->count(), 17);
+}
+
+void PlacesItemModelTest::testEditAfterCreation()
+{
+ createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
+
+ PlacesItemModel *model = new PlacesItemModel();
+ QTRY_COMPARE(model->count(), m_model->count());
+
+ PlacesItem *item = m_model->placesItem(4);
+ item->setText(QStringLiteral("Renamed place"));
+ m_model->saveBookmarks();
+
+ QTRY_COMPARE(model->count(), m_model->count());
+ QTRY_COMPARE(model->placesItem(4)->text(), m_model->placesItem(4)->text());
+ QTRY_COMPARE(model->placesItem(4)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
+ m_model->placesItem(4)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
+ QTRY_COMPARE(model->placesItem(4)->icon(), m_model->placesItem(4)->icon());
+ QTRY_COMPARE(model->placesItem(4)->url(), m_model->placesItem(4)->url());
+
+ m_model->removeItem(4);
+ m_model->saveBookmarks();
+ QTRY_COMPARE(model->count(), m_model->count());
+}
+
+void PlacesItemModelTest::testEditMetadata()
+{
+ createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
+
+ PlacesItemModel *model = new PlacesItemModel();
+ QTRY_COMPARE(model->count(), m_model->count());
+
+ PlacesItem *item = m_model->placesItem(4);
+ item->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
+ m_model->saveBookmarks();
+
+ QTRY_COMPARE(model->count(), m_model->count());
+ QTRY_COMPARE(model->placesItem(4)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
+ KAboutData::applicationData().componentName());
+ QTRY_COMPARE(model->placesItem(4)->text(), m_model->placesItem(4)->text());
+ QTRY_COMPARE(model->placesItem(4)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
+ m_model->placesItem(4)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
+ QTRY_COMPARE(model->placesItem(4)->icon(), m_model->placesItem(4)->icon());
+ QTRY_COMPARE(model->placesItem(4)->url(), m_model->placesItem(4)->url());
+
+ m_model->removeItem(4);
+ m_model->saveBookmarks();
+ QTRY_COMPARE(model->count(), m_model->count());
+}
+
+QTEST_MAIN(PlacesItemModelTest)
+
+#include "placesitemmodeltest.moc"