]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/placesitemmodeltest.cpp
Merge branch 'Applications/18.04'
[dolphin.git] / src / tests / placesitemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2017 by Renato Araujo Oliveira <renato.araujo@kdab.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include <QTest>
21 #include <QSignalSpy>
22 #include <QStandardPaths>
23 #include <QAction>
24 #include <QDBusInterface>
25
26 #include <KBookmarkManager>
27 #include <KConfig>
28 #include <KConfigGroup>
29 #include <KAboutData>
30 #include <KFilePlacesModel>
31
32 #include "panels/places/placesitemmodel.h"
33 #include "panels/places/placesitem.h"
34 #include "views/viewproperties.h"
35
36 Q_DECLARE_METATYPE(KItemRangeList)
37 Q_DECLARE_METATYPE(KItemRange)
38
39 #ifdef Q_OS_WIN
40 //c:\ as root for windows
41 #define KDE_ROOT_PATH "C:\\"
42 #else
43 #define KDE_ROOT_PATH "/"
44 #endif
45
46 static QString bookmarksFile()
47 {
48 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel";
49 }
50
51 class PlacesItemModelTest : public QObject
52 {
53 Q_OBJECT
54
55 private slots:
56 void init();
57 void cleanup();
58
59 void initTestCase();
60 void cleanupTestCase();
61
62 void testModelSort();
63 void testGroups();
64 void testDeletePlace();
65 void testPlaceItem_data();
66 void testPlaceItem();
67 void testTearDownDevice();
68 void testDefaultViewProperties_data();
69 void testDefaultViewProperties();
70 void testClear();
71 void testHideItem();
72 void testSystemItems();
73 void testEditBookmark();
74 void testEditAfterCreation();
75 void testEditMetadata();
76 void testRefresh();
77 void testIcons_data();
78 void testIcons();
79 void testDragAndDrop();
80 void testHideDevices();
81 void testDuplicatedEntries();
82 void renameAfterCreation();
83
84 private:
85 PlacesItemModel* m_model;
86 QSet<int> m_tobeRemoved;
87 QMap<QString, QDBusInterface *> m_interfacesMap;
88
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;
100 };
101
102 #define CHECK_PLACES_URLS(urls) \
103 { \
104 QStringList places = placesUrls(); \
105 if (places != urls) { \
106 qWarning() << "Expected:" << urls; \
107 qWarning() << "Got:" << places; \
108 QCOMPARE(places, urls); \
109 } \
110 }
111
112 void PlacesItemModelTest::setBalooEnabled(bool enabled)
113 {
114 KConfig config(QStringLiteral("baloofilerc"));
115 KConfigGroup basicSettings = config.group("Basic Settings");
116 basicSettings.writeEntry("Indexing-Enabled", enabled);
117 config.sync();
118 }
119
120 int PlacesItemModelTest::indexOf(const QUrl &url)
121 {
122 for (int r = 0; r < m_model->count(); r++) {
123 if (m_model->placesItem(r)->url() == url) {
124 return r;
125 }
126 }
127 return -1;
128 }
129
130 QDBusInterface *PlacesItemModelTest::fakeManager()
131 {
132 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
133 }
134
135 QDBusInterface *PlacesItemModelTest::fakeDevice(const QString &udi)
136 {
137 if (m_interfacesMap.contains(udi)) {
138 return m_interfacesMap[udi];
139 }
140
141 QDBusInterface *iface = new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi);
142 m_interfacesMap[udi] = iface;
143
144 return iface;
145 }
146
147 QStringList PlacesItemModelTest::placesUrls(PlacesItemModel *model) const
148 {
149 QStringList urls;
150 if (!model) {
151 model = m_model;
152 }
153
154 for (int row = 0; row < model->count(); ++row) {
155 urls << model->placesItem(row)->url().toDisplayString(QUrl::PreferLocalFile);
156 }
157 return urls;
158 }
159
160 QStringList PlacesItemModelTest::initialUrls() const
161 {
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");
174 }
175 return urls;
176 }
177
178 void PlacesItemModelTest::createPlaceItem(const QString &text, const QUrl &url, const QString &icon)
179 {
180 m_model->createPlacesItem(text, url, icon);
181 }
182
183 void PlacesItemModelTest::removePlaceAfter(int index)
184 {
185 m_tobeRemoved.insert(index);
186 }
187
188 void PlacesItemModelTest::cancelPlaceRemoval(int index)
189 {
190 m_tobeRemoved.remove(index);
191 }
192
193 void PlacesItemModelTest::removeTestUserData()
194 {
195 // user hardcoded path to avoid removal of any user personal data
196 QDir dir(QStringLiteral("/home/renato/.qttest/share/placesitemmodeltest"));
197 if (dir.exists()) {
198 QVERIFY(dir.removeRecursively());
199 }
200 }
201
202 QMimeData *PlacesItemModelTest::createMimeData(const QList<int> &indexes) const
203 {
204 QByteArray itemData;
205 QDataStream stream(&itemData, QIODevice::WriteOnly);
206 QList<QUrl> urls;
207
208 for (int index : indexes) {
209 const QUrl itemUrl = m_model->placesItem(index)->url();
210 if (itemUrl.isValid()) {
211 urls << itemUrl;
212 }
213 stream << index;
214 }
215
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);
222 return mimeData;
223 }
224
225 void PlacesItemModelTest::init()
226 {
227 m_model = new PlacesItemModel();
228 // WORKAROUND: need to wait for bookmark to load, check: PlacesItemModel::updateBookmarks
229 QTest::qWait(300);
230 QCOMPARE(m_model->count(), 17);
231 }
232
233 void PlacesItemModelTest::cleanup()
234 {
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);
240 }
241 m_tobeRemoved.clear();
242 delete m_model;
243 m_model = nullptr;
244 removeTestUserData();
245 }
246
247 void PlacesItemModelTest::initTestCase()
248 {
249 QStandardPaths::setTestModeEnabled(true);
250 // remove test user data
251 removeTestUserData();
252
253 const QString fakeHw = QFINDTESTDATA("data/fakecomputer.xml");
254 QVERIFY(!fakeHw.isEmpty());
255 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw));
256
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));
262 }
263
264 qRegisterMetaType<KItemRangeList>();
265 qRegisterMetaType<KItemRange>();
266 }
267
268 void PlacesItemModelTest::cleanupTestCase()
269 {
270 qDeleteAll(m_interfacesMap);
271 QFile::remove(bookmarksFile());
272
273 // Remove any previous properties file
274 removeTestUserData();
275 }
276
277 void PlacesItemModelTest::testModelSort()
278 {
279 CHECK_PLACES_URLS(initialUrls());
280 }
281
282 void PlacesItemModelTest::testGroups()
283 {
284 const auto groups = m_model->groups();
285
286
287 QCOMPARE(groups.size(), 6);
288
289 QCOMPARE(groups.at(0).first, 0);
290 QCOMPARE(groups.at(0).second.toString(), QStringLiteral("Places"));
291
292 QCOMPARE(groups.at(1).first, 5);
293 QCOMPARE(groups.at(1).second.toString(), QStringLiteral("Remote"));
294
295 QCOMPARE(groups.at(2).first, 7);
296 QCOMPARE(groups.at(2).second.toString(), QStringLiteral("Recently Saved"));
297
298 QCOMPARE(groups.at(3).first, 9);
299 QCOMPARE(groups.at(3).second.toString(), QStringLiteral("Search For"));
300
301 QCOMPARE(groups.at(4).first, 13);
302 QCOMPARE(groups.at(4).second.toString(), QStringLiteral("Devices"));
303
304 QCOMPARE(groups.at(5).first, 14);
305 QCOMPARE(groups.at(5).second.toString(), QStringLiteral("Removable Devices"));
306 }
307
308 void PlacesItemModelTest::testPlaceItem_data()
309 {
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");
315
316 // places
317 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
318
319 // baloo -search
320 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
321
322 // baloo - timeline
323 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << false << true << QStringLiteral("Recently Saved") << false;
324
325 // devices
326 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
327 }
328
329 void PlacesItemModelTest::testPlaceItem()
330 {
331 QFETCH(QUrl, url);
332 QFETCH(bool, expectedIsHidden);
333 QFETCH(bool, expectedIsSystemItem);
334 QFETCH(QString, expectedGroup);
335 QFETCH(bool, expectedStorageSetupNeeded);
336
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);
344 }
345
346 void PlacesItemModelTest::testDeletePlace()
347 {
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);
352
353 PlacesItemModel *model = new PlacesItemModel();
354
355 // create a new place
356 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
357 urls.insert(5, tempUrl.toLocalFile());
358
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());
363
364 // delete item
365 m_model->deleteItem(5);
366
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());
372 }
373
374 void PlacesItemModelTest::testTearDownDevice()
375 {
376 const QUrl mediaUrl = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
377 int index = indexOf(mediaUrl);
378 QVERIFY(index != -1);
379
380 auto ejectAction = m_model->ejectAction(index);
381 QVERIFY(!ejectAction);
382
383 auto teardownAction = m_model->teardownAction(index);
384 QVERIFY(teardownAction);
385
386 QCOMPARE(m_model->count(), 17);
387
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);
397
398 QCOMPARE(indexOf(mediaUrl), -1);
399
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);
405
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);
411 }
412
413 void PlacesItemModelTest::testDefaultViewProperties_data()
414 {
415 QTest::addColumn<QUrl>("url");
416 QTest::addColumn<DolphinView::Mode>("expectedViewMode");
417 QTest::addColumn<bool>("expectedPreviewShow");
418 QTest::addColumn<QList<QByteArray> >("expectedVisibleRole");
419
420 // places
421 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView << true << QList<QByteArray>({"text"});
422
423 // baloo -search
424 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "path"});
425
426 // audio files
427 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "artist", "album"});
428
429 // baloo - timeline
430 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << DolphinView::DetailsView << true << QList<QByteArray>({"text", "modificationtime"});
431
432 // devices
433 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView << true << QList<QByteArray>({"text"});
434
435 }
436
437 void PlacesItemModelTest::testDefaultViewProperties()
438 {
439 QFETCH(QUrl, url);
440 QFETCH(DolphinView::Mode, expectedViewMode);
441 QFETCH(bool, expectedPreviewShow);
442 QFETCH(QList<QByteArray>, expectedVisibleRole);
443
444 ViewProperties properties(KFilePlacesModel::convertedUrl(url));
445 QCOMPARE(properties.viewMode(), expectedViewMode);
446 QCOMPARE(properties.previewsShown(), expectedPreviewShow);
447 QCOMPARE(properties.visibleRoles(), expectedVisibleRole);
448 }
449
450 void PlacesItemModelTest::testClear()
451 {
452 QCOMPARE(m_model->count(), 17);
453 m_model->clear();
454 QCOMPARE(m_model->count(), 0);
455 QCOMPARE(m_model->hiddenCount(), 0);
456 m_model->refresh();
457 QTRY_COMPARE(m_model->count(), 17);
458 }
459
460 void PlacesItemModelTest::testHideItem()
461 {
462 const QUrl mediaUrl = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
463 const int index = indexOf(mediaUrl);
464
465 PlacesItem *item = m_model->placesItem(index);
466
467 QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
468 QList<QVariant> spyItemsRemovedArgs;
469 KItemRangeList removedRange;
470
471 QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted);
472 QList<QVariant> spyItemsInsertedArgs;
473 KItemRangeList insertedRange;
474 QVERIFY(item);
475
476 // hide an item
477 item->setHidden(true);
478
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);
487
488 // allow model to show hidden items
489 m_model->setHiddenItemsShown(true);
490
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);
497
498 // mark item as visible
499 item = m_model->placesItem(index);
500 item->setHidden(false);
501
502 // mark model to hide invisible items
503 m_model->setHiddenItemsShown(true);
504
505 QTRY_COMPARE(m_model->count(), 17);
506 }
507
508 void PlacesItemModelTest::testSystemItems()
509 {
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());
513 }
514
515 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
516
517 // create a new entry (non system item)
518 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
519
520 // check if the new entry was created
521 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
522
523 // make sure the new place get removed
524 removePlaceAfter(5);
525
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);
532
533 QTest::qWait(300);
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);
543
544 //cancel removal (it was removed above)
545 cancelPlaceRemoval(5);
546 }
547
548 void PlacesItemModelTest::testEditBookmark()
549 {
550 QScopedPointer<PlacesItemModel> other(new PlacesItemModel());
551
552 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
553
554 // make sure that the new item will be removed later
555 removePlaceAfter(5);
556
557 QSignalSpy itemsChangedSply(m_model, &PlacesItemModel::itemsChanged);
558
559 // modify place text
560 m_model->item(3)->setText(QStringLiteral("Renamed place"));
561 m_model->refresh();
562
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"));
573
574 // check if the item was updated in the other model
575 QTRY_COMPARE(other->item(3)->text(), QStringLiteral("Renamed place"));
576 }
577
578 void PlacesItemModelTest::testEditAfterCreation()
579 {
580 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
581 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
582
583 // create a new place
584 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
585 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
586
587 PlacesItemModel *model = new PlacesItemModel();
588 QTRY_COMPARE(model->count(), m_model->count());
589
590 // make sure that the new item will be removed later
591 removePlaceAfter(5);
592
593 // modify place text
594 PlacesItem *item = m_model->placesItem(3);
595 item->setText(QStringLiteral("Renamed place"));
596 m_model->refresh();
597
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());
605 }
606
607 void PlacesItemModelTest::testEditMetadata()
608 {
609 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
610 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
611
612 // create a new place
613 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
614 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
615
616 // check if the new entry was created
617 PlacesItemModel *model = new PlacesItemModel();
618 QTRY_COMPARE(model->count(), m_model->count());
619
620 // make sure that the new item will be removed later
621 removePlaceAfter(5);
622
623 // modify place metadata
624 PlacesItem *item = m_model->placesItem(3);
625 item->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
626 m_model->refresh();
627
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());
636 }
637
638 void PlacesItemModelTest::testRefresh()
639 {
640 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
641 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
642
643 // create a new place
644 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
645 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
646
647 PlacesItemModel *model = new PlacesItemModel();
648 QTRY_COMPARE(model->count(), m_model->count());
649
650 // make sure that the new item will be removed later
651 removePlaceAfter(5);
652
653 PlacesItem *item = m_model->placesItem(5);
654 PlacesItem *sameItem = model->placesItem(5);
655 QCOMPARE(item->text(), sameItem->text());
656
657 // modify place text
658 item->setText(QStringLiteral("Renamed place"));
659
660 // item from another model is not affected at the moment
661 QVERIFY(item->text() != sameItem->text());
662
663 // propagate change
664 m_model->refresh();
665
666 // item must be equal
667 QTRY_COMPARE(item->text(), sameItem->text());
668 }
669
670 void PlacesItemModelTest::testIcons_data()
671 {
672 QTest::addColumn<QUrl>("url");
673 QTest::addColumn<QString>("expectedIconName");
674
675 // places
676 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
677
678 // baloo -search
679 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
680
681 // baloo - timeline
682 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << QStringLiteral("go-jump-today");
683
684 // devices
685 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
686 }
687
688 void PlacesItemModelTest::testIcons()
689 {
690 QFETCH(QUrl, url);
691 QFETCH(QString, expectedIconName);
692
693 PlacesItem *item = m_model->placesItem(indexOf(url));
694 QCOMPARE(item->icon(), expectedIconName);
695
696 for (int r = 0; r < m_model->count(); r++) {
697 QVERIFY(!m_model->placesItem(r)->icon().isEmpty());
698 }
699 }
700
701 void PlacesItemModelTest::testDragAndDrop()
702 {
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);
708
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);
713 urls.move(0, 4);
714 delete dropData;
715
716 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
717 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
718
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);
725
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);
732
733 CHECK_PLACES_URLS(urls);
734
735 itemsInsertedSpy.clear();
736 itemsRemovedSpy.clear();
737
738 // Move home directory item back to its original position
739 dropData = createMimeData(QList<int>() << 4);
740 m_model->dropMimeDataBefore(0, dropData);
741 urls.move(4, 0);
742 delete dropData;
743
744 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
745 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
746
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);
753
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);
760
761 CHECK_PLACES_URLS(urls);
762 }
763
764 void PlacesItemModelTest::testHideDevices()
765 {
766 QSignalSpy itemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
767 QStringList urls = initialUrls();
768
769 m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, true);
770 QTRY_VERIFY(m_model->isGroupHidden(KFilePlacesModel::RemovableDevicesType));
771 QTRY_COMPARE(itemsRemoved.count(), 3);
772
773 // remove removable-devices
774 urls.removeOne(QStringLiteral("/media/floppy0"));
775 urls.removeOne(QStringLiteral("/media/XO-Y4"));
776 urls.removeOne(QStringLiteral("/media/cdrom"));
777
778 // check if the correct urls was removed
779 CHECK_PLACES_URLS(urls);
780
781 delete m_model;
782 m_model = new PlacesItemModel();
783 QTRY_COMPARE(m_model->count(), urls.count());
784 CHECK_PLACES_URLS(urls);
785
786 // revert changes
787 m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, false);
788 urls = initialUrls();
789 QTRY_COMPARE(m_model->count(), urls.count());
790 CHECK_PLACES_URLS(urls);
791 }
792
793 void PlacesItemModelTest::testDuplicatedEntries()
794 {
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"), {});
800
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());
805
806 PlacesItemModel *newModel = new PlacesItemModel();
807 QTRY_COMPARE(placesUrls(newModel).count(QStringLiteral("search:/videos")), 1);
808 QTRY_COMPARE(urls, placesUrls(newModel));
809 delete newModel;
810 }
811
812 void PlacesItemModelTest::renameAfterCreation()
813 {
814 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
815 QStringList urls = initialUrls();
816 PlacesItemModel *model = new PlacesItemModel();
817
818 CHECK_PLACES_URLS(urls);
819 QTRY_COMPARE(model->count(), m_model->count());
820
821 // create a new place
822 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
823 urls.insert(5, tempUrl.toLocalFile());
824
825 // make sure that the new item will be removed later
826 removePlaceAfter(5);
827
828 CHECK_PLACES_URLS(urls);
829 QCOMPARE(model->count(), m_model->count());
830
831
832 // modify place text
833 QSignalSpy changedSpy(m_model, &PlacesItemModel::itemsChanged);
834
835 PlacesItem *item = m_model->placesItem(3);
836 item->setText(QStringLiteral("New Temporary Dir"));
837 item->setUrl(item->url());
838 item->setIcon(item->icon());
839 m_model->refresh();
840
841 QTRY_COMPARE(changedSpy.count(), 1);
842
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"));
846 }
847
848 QTEST_MAIN(PlacesItemModelTest)
849
850 #include "placesitemmodeltest.moc"